xref: /linux/tools/testing/selftests/bpf/progs/lru_bug.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_tracing.h>
4 #include <bpf/bpf_helpers.h>
5 
6 struct map_value {
7 	struct task_struct __kptr *ptr;
8 };
9 
10 struct {
11 	__uint(type, BPF_MAP_TYPE_LRU_HASH);
12 	__uint(max_entries, 1);
13 	__type(key, int);
14 	__type(value, struct map_value);
15 } lru_map SEC(".maps");
16 
17 int pid = 0;
18 int result = 1;
19 
20 SEC("fentry/bpf_ktime_get_ns")
21 int printk(void *ctx)
22 {
23 	struct map_value v = {};
24 
25 	if (pid == bpf_get_current_task_btf()->pid)
26 		bpf_map_update_elem(&lru_map, &(int){0}, &v, 0);
27 	return 0;
28 }
29 
30 SEC("fentry/do_nanosleep")
31 int nanosleep(void *ctx)
32 {
33 	struct map_value val = {}, *v;
34 	struct task_struct *current;
35 
36 	bpf_map_update_elem(&lru_map, &(int){0}, &val, 0);
37 	v = bpf_map_lookup_elem(&lru_map, &(int){0});
38 	if (!v)
39 		return 0;
40 	bpf_map_delete_elem(&lru_map, &(int){0});
41 	current = bpf_get_current_task_btf();
42 	v->ptr = current;
43 	pid = current->pid;
44 	bpf_ktime_get_ns();
45 	result = !v->ptr;
46 	return 0;
47 }
48 
49 char _license[] SEC("license") = "GPL";
50