xref: /linux/tools/testing/selftests/bpf/progs/iters_task.c (revision 4eca0ef49af9b2b0c52ef2b58e045ab34629796b)
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;
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")
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, BPF_TASK_ITER_ALL_PROCS)
30 		if (pos->pid == target_pid)
31 			procs_cnt++;
32 
33 	bpf_for_each(task, pos, cur_task, BPF_TASK_ITER_PROC_THREADS)
34 		proc_threads_cnt++;
35 
36 	bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_THREADS)
37 		if (pos->tgid == target_pid)
38 			threads_cnt++;
39 	bpf_rcu_read_unlock();
40 	return 0;
41 }
42