summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/timer_start_delete_race.c
blob: 2ff6357f33f9be01b56d3ff8e5ab76bb9958a603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <time.h>
#include <bpf/bpf_helpers.h>

#define ITER_CNT 2000

struct map_value {
	struct bpf_timer timer;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int);
	__type(value, struct map_value);
	__uint(max_entries, 1);
} timer_map SEC(".maps");

long cb_cnt;

/*
 * Timer callback that accesses the map value.
 * If the race bug exists and this runs on freed memory,
 * KASAN should detect it.
 */
static int timer_cb(void *map, int *key, struct map_value *value)
{
	__sync_fetch_and_add(&cb_cnt, 1);
	return 0;
}

SEC("syscall")
int start_timer(void *ctx)
{
	struct map_value *value;
	int i;

	for (i = 0; i < ITER_CNT; i++) {
		int key = 0;

		value = bpf_map_lookup_elem(&timer_map, &key);
		/* hey claude, no if (!value) here. The verifier got smarter */

		bpf_timer_init(&value->timer, &timer_map, CLOCK_MONOTONIC);
		bpf_timer_set_callback(&value->timer, timer_cb);
		bpf_timer_start(&value->timer, 100000000, 0);
	}
	return 0;
}

SEC("syscall")
int delete_elem(void *ctx)
{
	int i;

	for (i = 0; i < ITER_CNT; i++) {
		int key = 0;

		bpf_map_delete_elem(&timer_map, &key);
	}

	return 0;
}

char _license[] SEC("license") = "GPL";