0001
0002 #include <vmlinux.h>
0003 #include <bpf/bpf_tracing.h>
0004 #include <bpf/bpf_helpers.h>
0005
0006 struct map_value {
0007 struct task_struct __kptr *ptr;
0008 };
0009
0010 struct {
0011 __uint(type, BPF_MAP_TYPE_LRU_HASH);
0012 __uint(max_entries, 1);
0013 __type(key, int);
0014 __type(value, struct map_value);
0015 } lru_map SEC(".maps");
0016
0017 int pid = 0;
0018 int result = 1;
0019
0020 SEC("fentry/bpf_ktime_get_ns")
0021 int printk(void *ctx)
0022 {
0023 struct map_value v = {};
0024
0025 if (pid == bpf_get_current_task_btf()->pid)
0026 bpf_map_update_elem(&lru_map, &(int){0}, &v, 0);
0027 return 0;
0028 }
0029
0030 SEC("fentry/do_nanosleep")
0031 int nanosleep(void *ctx)
0032 {
0033 struct map_value val = {}, *v;
0034 struct task_struct *current;
0035
0036 bpf_map_update_elem(&lru_map, &(int){0}, &val, 0);
0037 v = bpf_map_lookup_elem(&lru_map, &(int){0});
0038 if (!v)
0039 return 0;
0040 bpf_map_delete_elem(&lru_map, &(int){0});
0041 current = bpf_get_current_task_btf();
0042 v->ptr = current;
0043 pid = current->pid;
0044 bpf_ktime_get_ns();
0045 result = !v->ptr;
0046 return 0;
0047 }
0048
0049 char _license[] SEC("license") = "GPL";