* [PATCH bpf-next v5 0/2] Introduce a new kfunc of bpf_task_under_cgroup
@ 2023-05-04 3:15 Feng zhou
2023-05-04 3:15 ` [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc Feng zhou
2023-05-04 3:15 ` [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Feng zhou
0 siblings, 2 replies; 7+ messages in thread
From: Feng zhou @ 2023-05-04 3:15 UTC (permalink / raw)
To: martin.lau, ast, daniel, andrii, song, yhs, john.fastabend,
kpsingh, sdf, haoluo, jolsa, davem, edumazet, kuba, pabeni,
mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze,
wangdongdong.6, zhoufeng.zf
From: Feng Zhou <zhoufeng.zf@bytedance.com>
Trace sched related functions, such as enqueue_task_fair, it is necessary to
specify a task instead of the current task which within a given cgroup.
Feng Zhou (2):
bpf: Add bpf_task_under_cgroup() kfunc
selftests/bpf: Add testcase for bpf_task_under_cgroup
Changelog:
v4->v5: Addressed comments from Yonghong Song
- Some code format modifications.
Details in here:
https://lore.kernel.org/all/20230428071737.43849-1-zhoufeng.zf@bytedance.com/
v3->v4: Addressed comments from Yonghong Song
- Modify test cases and test other tasks, not the current task.
Details in here:
https://lore.kernel.org/all/20230427023019.73576-1-zhoufeng.zf@bytedance.com/
v2->v3: Addressed comments from Alexei Starovoitov
- Modify the comment information of the function.
- Narrow down the testcase's hook point
Details in here:
https://lore.kernel.org/all/20230421090403.15515-1-zhoufeng.zf@bytedance.com/
v1->v2: Addressed comments from Alexei Starovoitov
- Add kfunc instead.
Details in here:
https://lore.kernel.org/all/20230420072657.80324-1-zhoufeng.zf@bytedance.com/
kernel/bpf/helpers.c | 20 +++++++
tools/testing/selftests/bpf/DENYLIST.s390x | 1 +
.../bpf/prog_tests/task_under_cgroup.c | 54 +++++++++++++++++++
.../bpf/progs/test_task_under_cgroup.c | 51 ++++++++++++++++++
4 files changed, 126 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
create mode 100644 tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
--
2.20.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc
2023-05-04 3:15 [PATCH bpf-next v5 0/2] Introduce a new kfunc of bpf_task_under_cgroup Feng zhou
@ 2023-05-04 3:15 ` Feng zhou
2023-05-04 14:29 ` Yonghong Song
2023-05-04 3:15 ` [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Feng zhou
1 sibling, 1 reply; 7+ messages in thread
From: Feng zhou @ 2023-05-04 3:15 UTC (permalink / raw)
To: martin.lau, ast, daniel, andrii, song, yhs, john.fastabend,
kpsingh, sdf, haoluo, jolsa, davem, edumazet, kuba, pabeni,
mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze,
wangdongdong.6, zhoufeng.zf
From: Feng Zhou <zhoufeng.zf@bytedance.com>
Add a kfunc that's similar to the bpf_current_task_under_cgroup.
The difference is that it is a designated task.
When hook sched related functions, sometimes it is necessary to
specify a task instead of the current task.
Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
---
kernel/bpf/helpers.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index bb6b4637ebf2..453cbd312366 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2149,6 +2149,25 @@ __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
return NULL;
return cgrp;
}
+
+/**
+ * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test
+ * task's membership of cgroup ancestry.
+ * @task: the task to be tested
+ * @ancestor: possible ancestor of @task's cgroup
+ *
+ * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
+ * It follows all the same rules as cgroup_is_descendant, and only applies
+ * to the default hierarchy.
+ */
+__bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
+ struct cgroup *ancestor)
+{
+ if (unlikely(!ancestor || !task))
+ return -EINVAL;
+
+ return task_under_cgroup_hierarchy(task, ancestor);
+}
#endif /* CONFIG_CGROUPS */
/**
@@ -2400,6 +2419,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
#endif
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_SET8_END(generic_btf_ids)
--
2.20.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup
2023-05-04 3:15 [PATCH bpf-next v5 0/2] Introduce a new kfunc of bpf_task_under_cgroup Feng zhou
2023-05-04 3:15 ` [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc Feng zhou
@ 2023-05-04 3:15 ` Feng zhou
2023-05-04 14:46 ` Yonghong Song
1 sibling, 1 reply; 7+ messages in thread
From: Feng zhou @ 2023-05-04 3:15 UTC (permalink / raw)
To: martin.lau, ast, daniel, andrii, song, yhs, john.fastabend,
kpsingh, sdf, haoluo, jolsa, davem, edumazet, kuba, pabeni,
mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze,
wangdongdong.6, zhoufeng.zf
From: Feng Zhou <zhoufeng.zf@bytedance.com>
test_progs:
Tests new kfunc bpf_task_under_cgroup().
The bpf program saves the new task's pid within a given cgroup to
the remote_pid, which is convenient for the user-mode program to
verify the test correctness.
The user-mode program creates its own mount namespace, and mounts the
cgroupsv2 hierarchy in there, call the fork syscall, then check if
remote_pid and local_pid are unequal.
Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
---
tools/testing/selftests/bpf/DENYLIST.s390x | 1 +
.../bpf/prog_tests/task_under_cgroup.c | 54 +++++++++++++++++++
.../bpf/progs/test_task_under_cgroup.c | 51 ++++++++++++++++++
3 files changed, 106 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
create mode 100644 tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x b/tools/testing/selftests/bpf/DENYLIST.s390x
index c7463f3ec3c0..5061d9e24c16 100644
--- a/tools/testing/selftests/bpf/DENYLIST.s390x
+++ b/tools/testing/selftests/bpf/DENYLIST.s390x
@@ -26,3 +26,4 @@ user_ringbuf # failed to find kernel BTF type ID of
verif_stats # trace_vprintk__open_and_load unexpected error: -9 (?)
xdp_bonding # failed to auto-attach program 'trace_on_entry': -524 (trampoline)
xdp_metadata # JIT does not support calling kernel function (kfunc)
+test_task_under_cgroup # JIT does not support calling kernel function (kfunc)
diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
new file mode 100644
index 000000000000..fa3a98eae5ef
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Bytedance */
+
+#include <sys/syscall.h>
+#include <test_progs.h>
+#include <cgroup_helpers.h>
+#include "test_task_under_cgroup.skel.h"
+
+#define FOO "/foo"
+
+void test_task_under_cgroup(void)
+{
+ struct test_task_under_cgroup *skel;
+ int ret, foo = -1;
+ pid_t pid;
+
+ foo = test__join_cgroup(FOO);
+ if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
+ return;
+
+ skel = test_task_under_cgroup__open();
+ if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open"))
+ goto cleanup;
+
+ skel->rodata->local_pid = getpid();
+ skel->bss->remote_pid = getpid();
+ skel->rodata->cgid = get_cgroup_id(FOO);
+
+ ret = test_task_under_cgroup__load(skel);
+ 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"))
+ goto cleanup;
+
+ pid = fork();
+ if (pid == 0)
+ exit(0);
+
+ ret = (pid == -1);
+ if (ASSERT_OK(ret, "fork process"))
+ wait(NULL);
+
+ test_task_under_cgroup__detach(skel);
+
+ ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid,
+ "test task_under_cgroup");
+
+cleanup:
+ close(foo);
+
+ test_task_under_cgroup__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
new file mode 100644
index 000000000000..79d98e65c7eb
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Bytedance */
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+
+#include "bpf_misc.h"
+
+struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
+long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) __ksym;
+void bpf_cgroup_release(struct cgroup *p) __ksym;
+struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
+void bpf_task_release(struct task_struct *p) __ksym;
+
+const volatile int local_pid;
+const volatile __u64 cgid;
+int remote_pid;
+
+SEC("tp_btf/task_newtask")
+int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags)
+{
+ struct cgroup *cgrp = NULL;
+ struct task_struct *acquired;
+
+ if (local_pid != (bpf_get_current_pid_tgid() >> 32))
+ return 0;
+
+ acquired = bpf_task_acquire(task);
+ if (!acquired)
+ return 0;
+
+ if (local_pid == acquired->tgid)
+ goto out;
+
+ cgrp = bpf_cgroup_from_id(cgid);
+ if (!cgrp)
+ goto out;
+
+ if (bpf_task_under_cgroup(acquired, cgrp))
+ remote_pid = acquired->tgid;
+
+out:
+ if (acquired)
+ bpf_task_release(acquired);
+ if (cgrp)
+ bpf_cgroup_release(cgrp);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.20.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc
2023-05-04 3:15 ` [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc Feng zhou
@ 2023-05-04 14:29 ` Yonghong Song
2023-05-05 3:20 ` Feng Zhou
0 siblings, 1 reply; 7+ messages in thread
From: Yonghong Song @ 2023-05-04 14:29 UTC (permalink / raw)
To: Feng zhou, martin.lau, ast, daniel, andrii, song, yhs,
john.fastabend, kpsingh, sdf, haoluo, jolsa, davem, edumazet,
kuba, pabeni, mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze, wangdongdong.6
On 5/3/23 8:15 PM, Feng zhou wrote:
> From: Feng Zhou <zhoufeng.zf@bytedance.com>
>
> Add a kfunc that's similar to the bpf_current_task_under_cgroup.
> The difference is that it is a designated task.
>
> When hook sched related functions, sometimes it is necessary to
> specify a task instead of the current task.
>
> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
You can carry my Ack from previous revision since there
is no change to the patch.
Acked-by: Yonghong Song <yhs@fb.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup
2023-05-04 3:15 ` [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Feng zhou
@ 2023-05-04 14:46 ` Yonghong Song
2023-05-05 3:20 ` Feng Zhou
0 siblings, 1 reply; 7+ messages in thread
From: Yonghong Song @ 2023-05-04 14:46 UTC (permalink / raw)
To: Feng zhou, martin.lau, ast, daniel, andrii, song, yhs,
john.fastabend, kpsingh, sdf, haoluo, jolsa, davem, edumazet,
kuba, pabeni, mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze, wangdongdong.6
On 5/3/23 8:15 PM, Feng zhou wrote:
> From: Feng Zhou <zhoufeng.zf@bytedance.com>
>
> test_progs:
> Tests new kfunc bpf_task_under_cgroup().
>
> The bpf program saves the new task's pid within a given cgroup to
> the remote_pid, which is convenient for the user-mode program to
> verify the test correctness.
>
> The user-mode program creates its own mount namespace, and mounts the
> cgroupsv2 hierarchy in there, call the fork syscall, then check if
> remote_pid and local_pid are unequal.
>
> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
Ack with a few nits below. You can carry my Ack in the
next revision.
Acked-by: Yonghong Song <yhs@fb.com>
> ---
> tools/testing/selftests/bpf/DENYLIST.s390x | 1 +
> .../bpf/prog_tests/task_under_cgroup.c | 54 +++++++++++++++++++
> .../bpf/progs/test_task_under_cgroup.c | 51 ++++++++++++++++++
> 3 files changed, 106 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> create mode 100644 tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
>
> diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x b/tools/testing/selftests/bpf/DENYLIST.s390x
> index c7463f3ec3c0..5061d9e24c16 100644
> --- a/tools/testing/selftests/bpf/DENYLIST.s390x
> +++ b/tools/testing/selftests/bpf/DENYLIST.s390x
> @@ -26,3 +26,4 @@ user_ringbuf # failed to find kernel BTF type ID of
> verif_stats # trace_vprintk__open_and_load unexpected error: -9 (?)
> xdp_bonding # failed to auto-attach program 'trace_on_entry': -524 (trampoline)
> xdp_metadata # JIT does not support calling kernel function (kfunc)
> +test_task_under_cgroup # JIT does not support calling kernel function (kfunc)
> diff --git a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> new file mode 100644
> index 000000000000..fa3a98eae5ef
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
> @@ -0,0 +1,54 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2023 Bytedance */
> +
> +#include <sys/syscall.h>
> +#include <test_progs.h>
> +#include <cgroup_helpers.h>
> +#include "test_task_under_cgroup.skel.h"
> +
> +#define FOO "/foo"
> +
> +void test_task_under_cgroup(void)
> +{
> + struct test_task_under_cgroup *skel;
> + int ret, foo = -1;
You do not need to initialize 'foo' here.
> + pid_t pid;
> +
> + foo = test__join_cgroup(FOO);
> + if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
> + return;
> +
> + skel = test_task_under_cgroup__open();
> + if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open"))
> + goto cleanup;
> +
> + skel->rodata->local_pid = getpid();
> + skel->bss->remote_pid = getpid();
> + skel->rodata->cgid = get_cgroup_id(FOO);
> +
> + ret = test_task_under_cgroup__load(skel);
> + 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"))
> + goto cleanup;
> +
> + pid = fork();
> + if (pid == 0)
> + exit(0);
> +
> + ret = (pid == -1);
> + if (ASSERT_OK(ret, "fork process"))
> + wait(NULL);
> +
> + test_task_under_cgroup__detach(skel);
> +
> + ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid,
> + "test task_under_cgroup");
> +
> +cleanup:
> + close(foo);
> +
> + test_task_under_cgroup__destroy(skel);
Let us just do:
cleanup:
test_task_under_cgroup__destroy(skel);
close(foo);
This is the reverse order of test__join_cgroup() and
test_task_under_cgroup__open().
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
> new file mode 100644
> index 000000000000..79d98e65c7eb
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
> @@ -0,0 +1,51 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2023 Bytedance */
> +
> +#include <vmlinux.h>
> +#include <bpf/bpf_tracing.h>
> +#include <bpf/bpf_helpers.h>
> +
> +#include "bpf_misc.h"
> +
> +struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
> +long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) __ksym;
> +void bpf_cgroup_release(struct cgroup *p) __ksym;
> +struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
> +void bpf_task_release(struct task_struct *p) __ksym;
> +
> +const volatile int local_pid;
> +const volatile __u64 cgid;
> +int remote_pid;
> +
> +SEC("tp_btf/task_newtask")
> +int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags)
> +{
> + struct cgroup *cgrp = NULL;
> + struct task_struct *acquired;
> +
> + if (local_pid != (bpf_get_current_pid_tgid() >> 32))
> + return 0;
> +
> + acquired = bpf_task_acquire(task);
> + if (!acquired)
> + return 0;
> +
> + if (local_pid == acquired->tgid)
> + goto out;
> +
> + cgrp = bpf_cgroup_from_id(cgid);
> + if (!cgrp)
> + goto out;
> +
> + if (bpf_task_under_cgroup(acquired, cgrp))
> + remote_pid = acquired->tgid;
> +
> +out:
> + if (acquired)
> + bpf_task_release(acquired);
> + if (cgrp)
> + bpf_cgroup_release(cgrp);
Let us do:
out:
if (cgrp)
bpf_cgroup_release(cgrp);
bpf_task_release(acquired);
> + return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Re: [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc
2023-05-04 14:29 ` Yonghong Song
@ 2023-05-05 3:20 ` Feng Zhou
0 siblings, 0 replies; 7+ messages in thread
From: Feng Zhou @ 2023-05-05 3:20 UTC (permalink / raw)
To: Yonghong Song, martin.lau, ast, daniel, andrii, song, yhs,
john.fastabend, kpsingh, sdf, haoluo, jolsa, davem, edumazet,
kuba, pabeni, mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze, wangdongdong.6
在 2023/5/4 22:29, Yonghong Song 写道:
>
>
> On 5/3/23 8:15 PM, Feng zhou wrote:
>> From: Feng Zhou <zhoufeng.zf@bytedance.com>
>>
>> Add a kfunc that's similar to the bpf_current_task_under_cgroup.
>> The difference is that it is a designated task.
>>
>> When hook sched related functions, sometimes it is necessary to
>> specify a task instead of the current task.
>>
>> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
>
> You can carry my Ack from previous revision since there
> is no change to the patch.
>
> Acked-by: Yonghong Song <yhs@fb.com>
Will do
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup
2023-05-04 14:46 ` Yonghong Song
@ 2023-05-05 3:20 ` Feng Zhou
0 siblings, 0 replies; 7+ messages in thread
From: Feng Zhou @ 2023-05-05 3:20 UTC (permalink / raw)
To: Yonghong Song, martin.lau, ast, daniel, andrii, song, yhs,
john.fastabend, kpsingh, sdf, haoluo, jolsa, davem, edumazet,
kuba, pabeni, mykolal, shuah
Cc: bpf, linux-kernel, netdev, linux-kselftest, yangzhenze, wangdongdong.6
在 2023/5/4 22:46, Yonghong Song 写道:
>
>
> On 5/3/23 8:15 PM, Feng zhou wrote:
>> From: Feng Zhou <zhoufeng.zf@bytedance.com>
>>
>> test_progs:
>> Tests new kfunc bpf_task_under_cgroup().
>>
>> The bpf program saves the new task's pid within a given cgroup to
>> the remote_pid, which is convenient for the user-mode program to
>> verify the test correctness.
>>
>> The user-mode program creates its own mount namespace, and mounts the
>> cgroupsv2 hierarchy in there, call the fork syscall, then check if
>> remote_pid and local_pid are unequal.
>>
>> Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com>
>
> Ack with a few nits below. You can carry my Ack in the
> next revision.
>
> Acked-by: Yonghong Song <yhs@fb.com>
>
>
Will do
>> ---
>> tools/testing/selftests/bpf/DENYLIST.s390x | 1 +
>> .../bpf/prog_tests/task_under_cgroup.c | 54 +++++++++++++++++++
>> .../bpf/progs/test_task_under_cgroup.c | 51 ++++++++++++++++++
>> 3 files changed, 106 insertions(+)
>> create mode 100644
>> tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
>> create mode 100644
>> tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
>>
>> diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x
>> b/tools/testing/selftests/bpf/DENYLIST.s390x
>> index c7463f3ec3c0..5061d9e24c16 100644
>> --- a/tools/testing/selftests/bpf/DENYLIST.s390x
>> +++ b/tools/testing/selftests/bpf/DENYLIST.s390x
>> @@ -26,3 +26,4 @@ user_ringbuf # failed to
>> find kernel BTF type ID of
>> verif_stats #
>> trace_vprintk__open_and_load unexpected error:
>> -9 (?)
>> xdp_bonding # failed to auto-attach
>> program 'trace_on_entry': -524 (trampoline)
>> xdp_metadata # JIT does not support
>> calling kernel function (kfunc)
>> +test_task_under_cgroup # JIT does not support
>> calling kernel function (kfunc)
>> diff --git
>> a/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
>> b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
>> new file mode 100644
>> index 000000000000..fa3a98eae5ef
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/task_under_cgroup.c
>> @@ -0,0 +1,54 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2023 Bytedance */
>> +
>> +#include <sys/syscall.h>
>> +#include <test_progs.h>
>> +#include <cgroup_helpers.h>
>> +#include "test_task_under_cgroup.skel.h"
>> +
>> +#define FOO "/foo"
>> +
>> +void test_task_under_cgroup(void)
>> +{
>> + struct test_task_under_cgroup *skel;
>> + int ret, foo = -1;
>
> You do not need to initialize 'foo' here.
>
>> + pid_t pid;
>> +
>> + foo = test__join_cgroup(FOO);
>> + if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
>> + return;
>> +
>> + skel = test_task_under_cgroup__open();
>> + if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open"))
>> + goto cleanup;
>> +
>> + skel->rodata->local_pid = getpid();
>> + skel->bss->remote_pid = getpid();
>> + skel->rodata->cgid = get_cgroup_id(FOO);
>> +
>> + ret = test_task_under_cgroup__load(skel);
>> + 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"))
>> + goto cleanup;
>> +
>> + pid = fork();
>> + if (pid == 0)
>> + exit(0);
>> +
>> + ret = (pid == -1);
>> + if (ASSERT_OK(ret, "fork process"))
>> + wait(NULL);
>> +
>> + test_task_under_cgroup__detach(skel);
>> +
>> + ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid,
>> + "test task_under_cgroup");
>> +
>> +cleanup:
>> + close(foo);
>> +
>> + test_task_under_cgroup__destroy(skel);
>
> Let us just do:
> cleanup:
> test_task_under_cgroup__destroy(skel);
> close(foo);
>
> This is the reverse order of test__join_cgroup() and
> test_task_under_cgroup__open().
>
>> +}
>> diff --git
>> a/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
>> b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
>> new file mode 100644
>> index 000000000000..79d98e65c7eb
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
>> @@ -0,0 +1,51 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2023 Bytedance */
>> +
>> +#include <vmlinux.h>
>> +#include <bpf/bpf_tracing.h>
>> +#include <bpf/bpf_helpers.h>
>> +
>> +#include "bpf_misc.h"
>> +
>> +struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
>> +long bpf_task_under_cgroup(struct task_struct *task, struct cgroup
>> *ancestor) __ksym;
>> +void bpf_cgroup_release(struct cgroup *p) __ksym;
>> +struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
>> +void bpf_task_release(struct task_struct *p) __ksym;
>> +
>> +const volatile int local_pid;
>> +const volatile __u64 cgid;
>> +int remote_pid;
>> +
>> +SEC("tp_btf/task_newtask")
>> +int BPF_PROG(handle__task_newtask, struct task_struct *task, u64
>> clone_flags)
>> +{
>> + struct cgroup *cgrp = NULL;
>> + struct task_struct *acquired;
>> +
>> + if (local_pid != (bpf_get_current_pid_tgid() >> 32))
>> + return 0;
>> +
>> + acquired = bpf_task_acquire(task);
>> + if (!acquired)
>> + return 0;
>> +
>> + if (local_pid == acquired->tgid)
>> + goto out;
>> +
>> + cgrp = bpf_cgroup_from_id(cgid);
>> + if (!cgrp)
>> + goto out;
>> +
>> + if (bpf_task_under_cgroup(acquired, cgrp))
>> + remote_pid = acquired->tgid;
>> +
>> +out:
>> + if (acquired)
>> + bpf_task_release(acquired);
>> + if (cgrp)
>> + bpf_cgroup_release(cgrp);
>
> Let us do:
> out:
> if (cgrp)
> bpf_cgroup_release(cgrp);
> bpf_task_release(acquired);
>
>> + return 0;
>> +}
>> +
>> +char _license[] SEC("license") = "GPL";
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-05-05 3:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-04 3:15 [PATCH bpf-next v5 0/2] Introduce a new kfunc of bpf_task_under_cgroup Feng zhou
2023-05-04 3:15 ` [PATCH bpf-next v5 1/2] bpf: Add bpf_task_under_cgroup() kfunc Feng zhou
2023-05-04 14:29 ` Yonghong Song
2023-05-05 3:20 ` Feng Zhou
2023-05-04 3:15 ` [PATCH bpf-next v5 2/2] selftests/bpf: Add testcase for bpf_task_under_cgroup Feng zhou
2023-05-04 14:46 ` Yonghong Song
2023-05-05 3:20 ` Feng Zhou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®