diff mbox series

[bpf-next,2/2] selftests/bpf: Add selftest for sleepable bpf_task_under_cgroup()

Message ID 20231005083953.1281-2-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next,1/2] bpf: Fix missed rcu read lock in bpf_task_under_cgroup() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 4 maintainers not CCed: linux-kselftest@vger.kernel.org shuah@kernel.org mykolal@fb.com zhoufeng.zf@bytedance.com
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 83 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck

Commit Message

Yafang Shao Oct. 5, 2023, 8:39 a.m. UTC
The result 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

And no error messages in dmesg.

Without the prev patch, there will be RCU warnings in dmesg.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 .../selftests/bpf/prog_tests/task_under_cgroup.c   |  8 +++++--
 .../selftests/bpf/progs/test_task_under_cgroup.c   | 28 +++++++++++++++++++++-
 2 files changed, 33 insertions(+), 3 deletions(-)

Comments

Stanislav Fomichev Oct. 5, 2023, 5:13 p.m. UTC | #1
On 10/05, Yafang Shao wrote:
> The result 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
> 
> And no error messages in dmesg.
> 
> Without the prev patch, there will be RCU warnings in dmesg.
> 
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/task_under_cgroup.c   |  8 +++++--
>  .../selftests/bpf/progs/test_task_under_cgroup.c   | 28 +++++++++++++++++++++-
>  2 files changed, 33 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> index 4224727..d1a5a5c 100644
> --- a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> @@ -30,8 +30,12 @@ void test_task_under_cgroup(void)
>  	if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
>  		goto cleanup;
>  
> -	ret = test_task_under_cgroup__attach(skel);
> -	if (!ASSERT_OK(ret, "test_task_under_cgroup__attach"))
> +	skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
> +	if (!ASSERT_OK_PTR(skel->links.lsm_run, "attach_lsm"))
> +		goto cleanup;
> +

So we rely on the second attach here to trigger the program above?
Maybe add a comment? Otherwise we might risk loosing this dependency
after some refactoring...

Other than that, both patches look good to me, feel free to use for both
if/when you resend:

Acked-by: Stanislav Fomichev <sdf@google.com>
Yafang Shao Oct. 6, 2023, 2:17 a.m. UTC | #2
On Fri, Oct 6, 2023 at 1:13 AM Stanislav Fomichev <sdf@google.com> wrote:
>
> On 10/05, Yafang Shao wrote:
> > The result 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
> >
> > And no error messages in dmesg.
> >
> > Without the prev patch, there will be RCU warnings in dmesg.
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  .../selftests/bpf/prog_tests/task_under_cgroup.c   |  8 +++++--
> >  .../selftests/bpf/progs/test_task_under_cgroup.c   | 28 +++++++++++++++++++++-
> >  2 files changed, 33 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> > index 4224727..d1a5a5c 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> > @@ -30,8 +30,12 @@ void test_task_under_cgroup(void)
> >       if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
> >               goto cleanup;
> >
> > -     ret = test_task_under_cgroup__attach(skel);
> > -     if (!ASSERT_OK(ret, "test_task_under_cgroup__attach"))
> > +     skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
> > +     if (!ASSERT_OK_PTR(skel->links.lsm_run, "attach_lsm"))
> > +             goto cleanup;
> > +
>
> So we rely on the second attach here to trigger the program above?

Right.

> Maybe add a comment? Otherwise we might risk loosing this dependency
> after some refactoring...

Sure. will add a comment.

>
> Other than that, both patches look good to me, feel free to use for both
> if/when you resend:
>
> Acked-by: Stanislav Fomichev <sdf@google.com>

Thanks for your review.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
index 4224727..d1a5a5c 100644
--- a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
+++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
@@ -30,8 +30,12 @@  void test_task_under_cgroup(void)
 	if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
 		goto cleanup;
 
-	ret = test_task_under_cgroup__attach(skel);
-	if (!ASSERT_OK(ret, "test_task_under_cgroup__attach"))
+	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;
 
 	pid = fork();
diff --git a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
index 56cdc0a..7e750309 100644
--- a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
+++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
@@ -18,7 +18,7 @@ 
 int remote_pid;
 
 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 task_struct *acquired;
@@ -48,4 +48,30 @@  int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags)
 	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";