Commit 44cb03f1 authored by Yafang Shao's avatar Yafang Shao Committed by Daniel Borkmann

selftests/bpf: Add selftest for bpf_task_under_cgroup() in sleepable prog

The result is as follows:

  $ tools/testing/selftests/bpf/test_progs --name=task_under_cgroup
  #237     task_under_cgroup:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Without the previous patch, there will be RCU warnings in dmesg when
CONFIG_PROVE_RCU is enabled. While with the previous patch, there will
be no warnings.
Signed-off-by: default avatarYafang Shao <laoar.shao@gmail.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarStanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/20231007135945.4306-2-laoar.shao@gmail.com
parent 29a7e00f
...@@ -30,8 +30,15 @@ void test_task_under_cgroup(void) ...@@ -30,8 +30,15 @@ void test_task_under_cgroup(void)
if (!ASSERT_OK(ret, "test_task_under_cgroup__load")) if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
goto cleanup; goto cleanup;
ret = test_task_under_cgroup__attach(skel); /* First, attach the LSM program, and then it will be triggered when the
if (!ASSERT_OK(ret, "test_task_under_cgroup__attach")) * TP_BTF program is attached.
*/
skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
if (!ASSERT_OK_PTR(skel->links.lsm_run, "attach_lsm"))
goto cleanup;
skel->links.tp_btf_run = bpf_program__attach_trace(skel->progs.tp_btf_run);
if (!ASSERT_OK_PTR(skel->links.tp_btf_run, "attach_tp_btf"))
goto cleanup; goto cleanup;
pid = fork(); pid = fork();
......
...@@ -18,7 +18,7 @@ const volatile __u64 cgid; ...@@ -18,7 +18,7 @@ const volatile __u64 cgid;
int remote_pid; int remote_pid;
SEC("tp_btf/task_newtask") SEC("tp_btf/task_newtask")
int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags) int BPF_PROG(tp_btf_run, struct task_struct *task, u64 clone_flags)
{ {
struct cgroup *cgrp = NULL; struct cgroup *cgrp = NULL;
struct task_struct *acquired; struct task_struct *acquired;
...@@ -48,4 +48,30 @@ int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags) ...@@ -48,4 +48,30 @@ int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags)
return 0; return 0;
} }
SEC("lsm.s/bpf")
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
{
struct cgroup *cgrp = NULL;
struct task_struct *task;
int ret = 0;
task = bpf_get_current_task_btf();
if (local_pid != task->pid)
return 0;
if (cmd != BPF_LINK_CREATE)
return 0;
/* 1 is the root cgroup */
cgrp = bpf_cgroup_from_id(1);
if (!cgrp)
goto out;
if (!bpf_task_under_cgroup(task, cgrp))
ret = -1;
bpf_cgroup_release(cgrp);
out:
return ret;
}
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment