1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
4 #ifndef _TASK_KFUNC_COMMON_H
5 #define _TASK_KFUNC_COMMON_H
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
12 struct __tasks_kfunc_map_value {
13 struct task_struct __kptr * task;
17 __uint(type, BPF_MAP_TYPE_HASH);
19 __type(value, struct __tasks_kfunc_map_value);
20 __uint(max_entries, 1);
21 } __tasks_kfunc_map SEC(".maps");
23 struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
24 void bpf_task_release(struct task_struct *p) __ksym;
25 struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
26 void bpf_rcu_read_lock(void) __ksym;
27 void bpf_rcu_read_unlock(void) __ksym;
29 static inline struct __tasks_kfunc_map_value *tasks_kfunc_map_value_lookup(struct task_struct *p)
34 status = bpf_probe_read_kernel(&pid, sizeof(pid), &p->pid);
38 return bpf_map_lookup_elem(&__tasks_kfunc_map, &pid);
41 static inline int tasks_kfunc_map_insert(struct task_struct *p)
43 struct __tasks_kfunc_map_value local, *v;
45 struct task_struct *acquired, *old;
48 status = bpf_probe_read_kernel(&pid, sizeof(pid), &p->pid);
53 status = bpf_map_update_elem(&__tasks_kfunc_map, &pid, &local, BPF_NOEXIST);
57 v = bpf_map_lookup_elem(&__tasks_kfunc_map, &pid);
59 bpf_map_delete_elem(&__tasks_kfunc_map, &pid);
63 acquired = bpf_task_acquire(p);
67 old = bpf_kptr_xchg(&v->task, acquired);
69 bpf_task_release(old);
76 #endif /* _TASK_KFUNC_COMMON_H */