]> Git Repo - linux.git/blob - tools/testing/selftests/bpf/progs/free_timer.c
Linux 6.14-rc3
[linux.git] / tools / testing / selftests / bpf / progs / free_timer.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2025. Huawei Technologies Co., Ltd */
3 #include <linux/bpf.h>
4 #include <time.h>
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/bpf_helpers.h>
7
8 #define MAX_ENTRIES 8
9
10 struct map_value {
11         struct bpf_timer timer;
12 };
13
14 struct {
15         __uint(type, BPF_MAP_TYPE_HASH);
16         __type(key, int);
17         __type(value, struct map_value);
18         __uint(max_entries, MAX_ENTRIES);
19 } map SEC(".maps");
20
21 static int timer_cb(void *map, void *key, struct map_value *value)
22 {
23         volatile int sum = 0;
24         int i;
25
26         bpf_for(i, 0, 1024 * 1024) sum += i;
27
28         return 0;
29 }
30
31 static int start_cb(int key)
32 {
33         struct map_value *value;
34
35         value = bpf_map_lookup_elem(&map, (void *)&key);
36         if (!value)
37                 return 0;
38
39         bpf_timer_init(&value->timer, &map, CLOCK_MONOTONIC);
40         bpf_timer_set_callback(&value->timer, timer_cb);
41         /* Hope 100us will be enough to wake-up and run the overwrite thread */
42         bpf_timer_start(&value->timer, 100000, BPF_F_TIMER_CPU_PIN);
43
44         return 0;
45 }
46
47 static int overwrite_cb(int key)
48 {
49         struct map_value zero = {};
50
51         /* Free the timer which may run on other CPU */
52         bpf_map_update_elem(&map, (void *)&key, &zero, BPF_ANY);
53
54         return 0;
55 }
56
57 SEC("syscall")
58 int BPF_PROG(start_timer)
59 {
60         bpf_loop(MAX_ENTRIES, start_cb, NULL, 0);
61         return 0;
62 }
63
64 SEC("syscall")
65 int BPF_PROG(overwrite_timer)
66 {
67         bpf_loop(MAX_ENTRIES, overwrite_cb, NULL, 0);
68         return 0;
69 }
70
71 char _license[] SEC("license") = "GPL";
This page took 0.034359 seconds and 4 git commands to generate.