1130e0f7aSChuyi Zhou // SPDX-License-Identifier: GPL-2.0
2130e0f7aSChuyi Zhou /* Copyright (C) 2023 Chuyi Zhou <zhouchuyi@bytedance.com> */
3130e0f7aSChuyi Zhou
4130e0f7aSChuyi Zhou #include "vmlinux.h"
5130e0f7aSChuyi Zhou #include <bpf/bpf_helpers.h>
6130e0f7aSChuyi Zhou #include <bpf/bpf_tracing.h>
7130e0f7aSChuyi Zhou #include "bpf_misc.h"
8130e0f7aSChuyi Zhou #include "bpf_experimental.h"
9130e0f7aSChuyi Zhou
10130e0f7aSChuyi Zhou char _license[] SEC("license") = "GPL";
11130e0f7aSChuyi Zhou
12130e0f7aSChuyi Zhou pid_t target_pid;
13*5c138a8aSYafang Shao int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt;
14130e0f7aSChuyi Zhou
15130e0f7aSChuyi Zhou void bpf_rcu_read_lock(void) __ksym;
16130e0f7aSChuyi Zhou void bpf_rcu_read_unlock(void) __ksym;
17130e0f7aSChuyi Zhou
18130e0f7aSChuyi Zhou SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
iter_task_for_each_sleep(void * ctx)19130e0f7aSChuyi Zhou int iter_task_for_each_sleep(void *ctx)
20130e0f7aSChuyi Zhou {
21130e0f7aSChuyi Zhou struct task_struct *cur_task = bpf_get_current_task_btf();
22130e0f7aSChuyi Zhou struct task_struct *pos;
23130e0f7aSChuyi Zhou
24130e0f7aSChuyi Zhou if (cur_task->pid != target_pid)
25130e0f7aSChuyi Zhou return 0;
26130e0f7aSChuyi Zhou procs_cnt = threads_cnt = proc_threads_cnt = 0;
27130e0f7aSChuyi Zhou
28130e0f7aSChuyi Zhou bpf_rcu_read_lock();
29*5c138a8aSYafang Shao bpf_for_each(task, pos, NULL, ~0U) {
30*5c138a8aSYafang Shao /* Below instructions shouldn't be executed for invalid flags */
31*5c138a8aSYafang Shao invalid_cnt++;
32*5c138a8aSYafang Shao }
33*5c138a8aSYafang Shao
34*5c138a8aSYafang Shao bpf_for_each(task, pos, NULL, BPF_TASK_ITER_PROC_THREADS) {
35*5c138a8aSYafang Shao /* Below instructions shouldn't be executed for invalid task__nullable */
36*5c138a8aSYafang Shao invalid_cnt++;
37*5c138a8aSYafang Shao }
38*5c138a8aSYafang Shao
39130e0f7aSChuyi Zhou bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS)
40130e0f7aSChuyi Zhou if (pos->pid == target_pid)
41130e0f7aSChuyi Zhou procs_cnt++;
42130e0f7aSChuyi Zhou
43130e0f7aSChuyi Zhou bpf_for_each(task, pos, cur_task, BPF_TASK_ITER_PROC_THREADS)
44130e0f7aSChuyi Zhou proc_threads_cnt++;
45130e0f7aSChuyi Zhou
46130e0f7aSChuyi Zhou bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_THREADS)
47130e0f7aSChuyi Zhou if (pos->tgid == target_pid)
48130e0f7aSChuyi Zhou threads_cnt++;
49130e0f7aSChuyi Zhou bpf_rcu_read_unlock();
50130e0f7aSChuyi Zhou return 0;
51130e0f7aSChuyi Zhou }
52