1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023 Chuyi Zhou <zhouchuyi@bytedance.com> */
3
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include "bpf_misc.h"
8 #include "bpf_experimental.h"
9
10 char _license[] SEC("license") = "GPL";
11
12 pid_t target_pid;
13 int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt;
14
15 void bpf_rcu_read_lock(void) __ksym;
16 void bpf_rcu_read_unlock(void) __ksym;
17
18 SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
iter_task_for_each_sleep(void * ctx)19 int iter_task_for_each_sleep(void *ctx)
20 {
21 struct task_struct *cur_task = bpf_get_current_task_btf();
22 struct task_struct *pos;
23
24 if (cur_task->pid != target_pid)
25 return 0;
26 procs_cnt = threads_cnt = proc_threads_cnt = 0;
27
28 bpf_rcu_read_lock();
29 bpf_for_each(task, pos, NULL, ~0U) {
30 /* Below instructions shouldn't be executed for invalid flags */
31 invalid_cnt++;
32 }
33
34 bpf_for_each(task, pos, NULL, BPF_TASK_ITER_PROC_THREADS) {
35 /* Below instructions shouldn't be executed for invalid task__nullable */
36 invalid_cnt++;
37 }
38
39 bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS)
40 if (pos->pid == target_pid)
41 procs_cnt++;
42
43 bpf_for_each(task, pos, cur_task, BPF_TASK_ITER_PROC_THREADS)
44 proc_threads_cnt++;
45
46 bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_THREADS)
47 if (pos->tgid == target_pid)
48 threads_cnt++;
49 bpf_rcu_read_unlock();
50 return 0;
51 }
52