bpf: Expand bpf_cgrp_storage to support cgroup1 non-attach case
Subsystems (4)
cover bpf: Expand bpf_cgrp_storage to support cgroup1 non-attach case
In the current cgroup1 environment, associating operations between a cgroup and applications in a BPF program requires storing a mapping of cgroup_id to application either in a hash map or maintaining it in userspace. However, by enabling bpf_cgrp_storage for cgroup1, it becomes possible to conveniently store application-specific information in cgroup-local storage and utilize it within BPF programs. Furthermore, enabling this feature for cgroup1 involves minor modifications for the non-attach case, streamlining the process.
However, when it comes to enabling this functionality for the cgroup1 attach case, it presents challenges. Therefore, the decision is to focus on enabling it solely for the cgroup1 non-attach case at present. If attempting to attach to a cgroup1 fd, the operation will simply fail with the error code -EBADF.
Yafang Shao (3):
bpf: Enable bpf_cgrp_storage for cgroup1 non-attach case selftests/bpf: Add a new cgroup helper open_classid() selftests/bpf: Add selftests for cgroup1 local storage
kernel/bpf/bpf_cgrp_storage.c | 6 +- tools/testing/selftests/bpf/cgroup_helpers.c | 16 ++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + .../selftests/bpf/prog_tests/cgrp_local_storage.c | 92 +++++++++++++++++++++- .../selftests/bpf/progs/cgrp_ls_recursion.c | 84 ++++++++++++++++---- .../selftests/bpf/progs/cgrp_ls_sleepable.c | 67 ++++++++++++++-- tools/testing/selftests/bpf/progs/cgrp_ls_tp_btf.c | 82 +++++++++++++------ 7 files changed, 298 insertions(+), 50 deletions(-)
Signature · 1 line
1.8.3.1
RFC PATCH 1/3 bpf: Enable bpf_cgrp_storage for cgroup1 non-attach case
In the current cgroup1 environment, associating operations between a cgroup and applications in a BPF program requires storing a mapping of cgroup_id to application either in a hash map or maintaining it in userspace. However, by enabling bpf_cgrp_storage for cgroup1, it becomes possible to conveniently store application-specific information in cgroup-local storage and utilize it within BPF programs. Furthermore, enabling this feature for cgroup1 involves minor modifications for the non-attach case, streamlining the process.
However, when it comes to enabling this functionality for the cgroup1 attach case, it presents challenges. Therefore, the decision is to focus on enabling it solely for the cgroup1 non-attach case at present. If attempting to attach to a cgroup1 fd, the operation will simply fail with the error code -EBADF.
--- kernel/bpf/bpf_cgrp_storage.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/bpf/bpf_cgrp_storage.c +3 -3
@@ -82,7 +82,7 @@ static void *bpf_cgrp_storage_lookup_elem(struct bpf_map *map, void *key)int fd; fd = *(int *)key; - cgroup =cgroup_get_from_fd (fd); + cgroup =cgroup_v1v2_get_from_fd (fd);if (IS_ERR (cgroup))return ERR_CAST (cgroup);
@@ -101,7 +101,7 @@ static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key,int fd; fd = *(int *)key; - cgroup =cgroup_get_from_fd (fd); + cgroup =cgroup_v1v2_get_from_fd (fd);if (IS_ERR (cgroup))return PTR_ERR (cgroup);
@@ -131,7 +131,7 @@ static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key)int err, fd; fd = *(int *)key; - cgroup =cgroup_get_from_fd (fd); + cgroup =cgroup_v1v2_get_from_fd (fd);if (IS_ERR (cgroup))return PTR_ERR (cgroup);
Signature · 1 line
2.30.1 (Apple Git-130)
Re: [RFC PATCH bpf-next 1/3] bpf: Enable bpf_cgrp_storage for cgroup1 non-attach case
Quoted 9 lines · Yafang Shao
In the current cgroup1 environment, associating operations between a cgroup and applications in a BPF program requires storing a mapping of
cgroup_idto application either in a hash map or maintaining it in userspace. However, by enablingbpf_cgrp_storagefor cgroup1, it becomes possible to conveniently store application-specific information in cgroup-local storage and utilize it within BPF programs. Furthermore, enabling this feature for cgroup1 involves minor modifications for the non-attach case, streamlining the process.
Quoted 7 lines
However, when it comes to enabling this functionality for the cgroup1 attach case, it presents challenges. Therefore, the decision is to focus on enabling it solely for the cgroup1 non-attach case at present. If attempting to attach to a cgroup1 fd, the operation will simply fail with the error code
-EBADF.Signed-off-by Yafang Shao <[email protected]>
RFC PATCH 2/3 selftests/bpf: Add a new cgroup helper open_classid()
In reply to Yafang ShaoThis new helper allows us to obtain the fd of a net_cls cgroup, which will be utilized in the subsequent patch.
--- tools/testing/selftests/bpf/cgroup_helpers.c | 16 ++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + 2 files changed, 17 insertions(+)
tools/testing/selftests/bpf/cgroup_helpers.c +16 -0
@@ -689,3 +689,19 @@ int get_cgroup1_hierarchy_id(const char *subsys_name)fclose (file);return found ? id :-1 ; } + +/** +* open_classid() - Open a cgroupv1 net_cls classid +* +* This function expects the cgroup work dir to be already created, as we +* open it here. +* +* On success, it returns the file descriptor. On failure it returns -1. +*/ +int open_classid (void ) +{ +char cgroup_workdir[PATH_MAX +1 ]; + +format_classid_path (cgroup_workdir); +return open (cgroup_workdir,O_RDONLY ); +}
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
@@ -33,6 +33,7 @@ void cleanup_cgroup_environment(void);int set_classid (void );int join_classid (void );unsigned long long get_classid_cgroup_id (void ); +int open_classid (void );int setup_classid_environment (void );void cleanup_classid_environment (void );
Signature · 1 line
2.30.1 (Apple Git-130)
Re: [RFC PATCH bpf-next 2/3] selftests/bpf: Add a new cgroup helper open_classid()
Quoted 4 lines · Yafang Shao
This new helper allows us to obtain the fd of a
net_clscgroup, which will be utilized in the subsequent patch.Signed-off-by Yafang Shao <[email protected]>
RFC PATCH 3/3 selftests/bpf: Add selftests for cgroup1 local storage
In reply to Yafang ShaoExpanding the test coverage from cgroup2 to include cgroup1. The result as follows,
Already existing test cases for cgroup2:
#48/1 cgrp_local_storage/tp_btf:OK #48/2 cgrp_local_storage/attach_cgroup:OK #48/3 cgrp_local_storage/recursion:OK #48/4 cgrp_local_storage/negative:OK #48/5 cgrp_local_storage/cgroup_iter_sleepable:OK #48/6 cgrp_local_storage/yes_rcu_lock:OK #48/7 cgrp_local_storage/no_rcu_lock:OK
Expanded test cases for cgroup1:
#48/8 cgrp_local_storage/cgrp1_tp_btf:OK #48/9 cgrp_local_storage/cgrp1_recursion:OK #48/10 cgrp_local_storage/cgrp1_negative:OK #48/11 cgrp_local_storage/cgrp1_iter_sleepable:OK #48/12 cgrp_local_storage/cgrp1_yes_rcu_lock:OK #48/13 cgrp_local_storage/cgrp1_no_rcu_lock:OK
Summary:
#48 cgrp_local_storage:OK
Summary: 1/13 PASSED, 0 SKIPPED, 0 FAILED
--- .../bpf/prog_tests/cgrp_local_storage.c | 92 ++++++++++++++++++- .../selftests/bpf/progs/cgrp_ls_recursion.c | 84 +++++++++++++---- .../selftests/bpf/progs/cgrp_ls_sleepable.c | 67 ++++++++++++-- .../selftests/bpf/progs/cgrp_ls_tp_btf.c | 82 ++++++++++++----- 4 files changed, 278 insertions(+), 47 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +91 -1
@@ -19,6 +19,9 @@ struct socket_cookie {__u64 cookie_value; }; +bool is_cgroup1; +int target_hid; +static void test_tp_btf (int cgroup_fd) {struct cgrp_ls_tp_btf *skel;
@@ -29,6 +32,9 @@ static void test_tp_btf(int cgroup_fd)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ; + skel->bss->is_cgroup1 = is_cgroup1; + skel->bss->target_hid = target_hid; +/* populate a value in map_b */ err =bpf_map_update_elem (bpf_map__fd (skel->maps.map_b), &cgroup_fd, &val1,BPF_ANY );if (!ASSERT_OK (err,"map_update_elem" ))
@@ -130,6 +136,9 @@ static void test_recursion(int cgroup_fd)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ; + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; + err =cgrp_ls_recursion__attach (skel);if (!ASSERT_OK (err,"skel_attach" )) goto out;
@@ -165,6 +174,9 @@ static void test_cgroup_iter_sleepable(int cgroup_fd, __u64 cgroup_id)if (!ASSERT_OK_PTR (skel,"skel_open" ))return ; + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; +bpf_program__set_autoload (skel->progs.cgroup_iter, true); err =cgrp_ls_sleepable__load (skel);if (!ASSERT_OK (err,"skel_load" ))
@@ -202,6 +214,8 @@ static void test_yes_rcu_lock(__u64 cgroup_id)if (!ASSERT_OK_PTR (skel,"skel_open" ))return ; + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; skel->bss->target_pid =syscall (SYS_gettid);bpf_program__set_autoload (skel->progs.yes_rcu_lock, true);
@@ -229,6 +243,9 @@ static void test_no_rcu_lock(void)if (!ASSERT_OK_PTR (skel,"skel_open" ))return ; + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; +bpf_program__set_autoload (skel->progs.no_rcu_lock, true); err =cgrp_ls_sleepable__load (skel);ASSERT_ERR (err,"skel_load" );
@@ -236,7 +253,26 @@ static void test_no_rcu_lock(void)cgrp_ls_sleepable__destroy (skel); } -void test_cgrp_local_storage (void ) +static void test_cgrp1_no_rcu_lock (void ) +{ +struct cgrp_ls_sleepable *skel; +int err; + + skel =cgrp_ls_sleepable__open (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; + + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; + +bpf_program__set_autoload (skel->progs.cgrp1_no_rcu_lock, true); + err =cgrp_ls_sleepable__load (skel); +ASSERT_OK (err,"skel_load" ); + +cgrp_ls_sleepable__destroy (skel); +} + +void cgrp2_local_storage (void ) {__u64 cgroup_id;int cgroup_fd;
@@ -245,6 +281,8 @@ void test_cgrp_local_storage(void)if (!ASSERT_GE (cgroup_fd,0 ,"join_cgroup /cgrp_local_storage" ))return ; + is_cgroup1 =0 ; + target_hid =-1 ; cgroup_id =get_cgroup_id ("/cgrp_local_storage" );if (test__start_subtest ("tp_btf" ))test_tp_btf (cgroup_fd);
@@ -263,3 +301,55 @@ void test_cgrp_local_storage(void)close (cgroup_fd); } + +void cgrp1_local_storage (void ) +{ +int cgrp1_fd, cgrp1_hid, cgrp1_id, err; + +/* Setup cgroup1 hierarchy */ + err =setup_classid_environment (); +if (!ASSERT_OK (err,"setup_classid_environment" )) +return ; + + err =join_classid (); +if (!ASSERT_OK (err,"join_cgroup1" )) + goto cleanup; + + cgrp1_fd =open_classid (); +if (!ASSERT_GE (cgrp1_fd,0 ,"cgroup1 fd" )) + goto cleanup; + + cgrp1_id =get_classid_cgroup_id (); +if (!ASSERT_GE (cgrp1_id,0 ,"cgroup1 id" )) + goto close_fd; + + cgrp1_hid =get_cgroup1_hierarchy_id ("net_cls" ); +if (!ASSERT_GE (cgrp1_hid,0 ,"cgroup1 hid" )) + goto close_fd; + target_hid = cgrp1_hid; + is_cgroup1 =1 ; + +if (test__start_subtest ("cgrp1_tp_btf" )) +test_tp_btf (cgrp1_fd); +if (test__start_subtest ("cgrp1_recursion" )) +test_recursion (cgrp1_fd); +if (test__start_subtest ("cgrp1_negative" )) +test_negative (); +if (test__start_subtest ("cgrp1_iter_sleepable" )) +test_cgroup_iter_sleepable (cgrp1_fd, cgrp1_id); +if (test__start_subtest ("cgrp1_yes_rcu_lock" )) +test_yes_rcu_lock (cgrp1_id); +if (test__start_subtest ("cgrp1_no_rcu_lock" )) +test_cgrp1_no_rcu_lock (); + +close_fd: +close (cgrp1_fd); +cleanup: +cleanup_classid_environment (); +} + +void test_cgrp_local_storage (void ) +{ +cgrp2_local_storage (); +cgrp1_local_storage (); +}
tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c +67 -17
117 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +60 -7
96 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgrp_ls_tp_btf.c +60 -22
122 lines not on this page · raw message
Signature · 1 line
2.30.1 (Apple Git-130)
Re: [RFC PATCH bpf-next 3/3] selftests/bpf: Add selftests for cgroup1 local storage
Quoted 20 lines · Yafang Shao
Expanding the test coverage from cgroup2 to include cgroup1. The result as follows,
Already existing test cases for cgroup2:
#48/1 cgrp_local_storage/tp_btf:OK #48/2 cgrp_local_storage/attach_cgroup:OK #48/3 cgrp_local_storage/recursion:OK #48/4 cgrp_local_storage/negative:OK #48/5 cgrp_local_storage/cgroup_iter_sleepable:OK #48/6 cgrp_local_storage/yes_rcu_lock:OK #48/7 cgrp_local_storage/no_rcu_lock:OKExpanded test cases for cgroup1:
#48/8 cgrp_local_storage/cgrp1_tp_btf:OK #48/9 cgrp_local_storage/cgrp1_recursion:OK #48/10 cgrp_local_storage/cgrp1_negative:OK #48/11 cgrp_local_storage/cgrp1_iter_sleepable:OK #48/12 cgrp_local_storage/cgrp1_yes_rcu_lock:OK #48/13 cgrp_local_storage/cgrp1_no_rcu_lock:OK
Quoted 5 lines
Summary:
#48 cgrp_local_storage:OK
Summary: 1/13 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by Yafang Shao <[email protected]>
LGTM with a few nits below.
Quoted 22 lines
--- .../bpf/prog_tests/cgrp_local_storage.c | 92 ++++++++++++++++++- .../selftests/bpf/progs/cgrp_ls_recursion.c | 84 +++++++++++++---- .../selftests/bpf/progs/cgrp_ls_sleepable.c | 67 ++++++++++++-- .../selftests/bpf/progs/cgrp_ls_tp_btf.c | 82 ++++++++++++----- 4 files changed, 278 insertions(+), 47 deletions(-)tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +3
-0@@ -19,6 +19,9 @@ struct socket_cookie {__u64 cookie_value; }; +bool is_cgroup1; +int target_hid; +static void test_tp_btf (int cgroup_fd) {struct cgrp_ls_tp_btf *skel;
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c static void test_tp_btf(int cgroup_fd) · line 36
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +2
-0@@ -29,6 +32,9 @@ static void test_tp_btf(int cgroup_fd)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ; + skel->bss->is_cgroup1 = is_cgroup1; + skel->bss->target_hid = target_hid;
Let reverse the order like below to be consistent with other code patterns:
+ skel->bss->target_hid = target_hid;
+ skel->bss->is_cgroup1 = is_cgroup1;
Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +4
-0+/* populate a value in map_b */ err =bpf_map_update_elem (bpf_map__fd (skel->maps.map_b), &cgroup_fd, &val1,BPF_ANY );if (!ASSERT_OK (err,"map_update_elem" ))@@ -130,6 +136,9 @@ static void test_recursion(int cgroup_fd)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ; + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; + err =cgrp_ls_recursion__attach (skel);if (!ASSERT_OK (err,"skel_attach" )) goto out;tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +17
-0@@ -17,7 +17,11 @@ struct {__u32 target_pid;__u64 cgroup_id; +int target_hid; +bool is_cgroup1; +struct cgroup *bpf_task_get_cgroup1 (struct task_struct *task,int hierarchy_id) __ksym; +void bpf_cgroup_release(struct cgroup *cgrp) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym;@@ -37,23 +41,56 @@ int cgroup_iter(struct bpf_iter__cgroup *ctx)return 0 ; } +static void __no_rcu_lock (struct cgroup *cgrp) +{ +long *ptr; + +/* Note that trace rcu is held in sleepable prog, so we can use +* bpf_cgrp_storage_get() in sleepable prog. +*/ + ptr =bpf_cgrp_storage_get (&map_a, cgrp,0 , +BPF_LOCAL_STORAGE_GET_F_CREATE ); +if (ptr) + cgroup_id = cgrp->kn->id; +} +
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c int cgroup_iter(struct bpf_iter__cgroup *ctx) · line 64
Quoted 10 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +4
-2SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") -int no_rcu_lock (void *ctx) +int cgrp1_no_rcu_lock (void *ctx) {struct task_struct *task;struct cgroup *cgrp; -long *ptr; + +if (!is_cgroup1) +return 0 ;
Do we need this check? Looks like the user space controls whether it will be loaded or not depending on whether it is cgrp1.
Quoted 12 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +12
-0+ + task =bpf_get_current_task_btf (); +if (task->pid != target_pid) +return 0 ; + +/* bpf_task_get_cgroup1 can work in sleepable prog */ + cgrp =bpf_task_get_cgroup1 (task, target_hid); +if (!cgrp) +return 0 ; + +__no_rcu_lock (cgrp); +bpf_cgroup_release (cgrp);
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c int cgroup_iter(struct bpf_iter__cgroup *ctx) · line 86
Quoted 10 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +10
-0+return 0 ; +} + +SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") +int no_rcu_lock (void *ctx) +{ +struct task_struct *task; + +if (is_cgroup1) +return 0 ;
Same here, check is not needed.
Quoted 38 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +17
-5task =bpf_get_current_task_btf ();if (task->pid != target_pid)return 0 ;/* task->cgroups is untrusted in sleepable prog outside of RCU CS */ - cgrp = task->cgroups->dfl_cgrp; - ptr =bpf_cgrp_storage_get (&map_a, cgrp,0 , -BPF_LOCAL_STORAGE_GET_F_CREATE ); -if (ptr) - cgroup_id = cgrp->kn->id; +__no_rcu_lock (task->cgroups->dfl_cgrp);return 0 ; }@@ -68,6 +105,22 @@ int yes_rcu_lock(void *ctx)if (task->pid != target_pid)return 0 ; +if (is_cgroup1) { +bpf_rcu_read_lock (); + cgrp =bpf_task_get_cgroup1 (task, target_hid); +if (!cgrp) { +bpf_rcu_read_unlock (); +return 0 ; + } + + ptr =bpf_cgrp_storage_get (&map_a, cgrp,0 ,BPF_LOCAL_STORAGE_GET_F_CREATE ); +if (ptr) + cgroup_id = cgrp->kn->id; +bpf_cgroup_release (cgrp); +bpf_rcu_read_unlock (); +return 0 ; + } +bpf_rcu_read_lock (); cgrp = task->cgroups->dfl_cgrp;/* cgrp is trusted under RCU CS */
[...]
Re: [RFC PATCH bpf-next 3/3] selftests/bpf: Add selftests for cgroup1 local storage
Quoted 58 lines · Yonghong Song
Quoted 20 lines · Yafang Shao
Expanding the test coverage from cgroup2 to include cgroup1. The result as follows,
Already existing test cases for cgroup2:
#48/1 cgrp_local_storage/tp_btf:OK #48/2 cgrp_local_storage/attach_cgroup:OK #48/3 cgrp_local_storage/recursion:OK #48/4 cgrp_local_storage/negative:OK #48/5 cgrp_local_storage/cgroup_iter_sleepable:OK #48/6 cgrp_local_storage/yes_rcu_lock:OK #48/7 cgrp_local_storage/no_rcu_lock:OKExpanded test cases for cgroup1:
#48/8 cgrp_local_storage/cgrp1_tp_btf:OK #48/9 cgrp_local_storage/cgrp1_recursion:OK #48/10 cgrp_local_storage/cgrp1_negative:OK #48/11 cgrp_local_storage/cgrp1_iter_sleepable:OK #48/12 cgrp_local_storage/cgrp1_yes_rcu_lock:OK #48/13 cgrp_local_storage/cgrp1_no_rcu_lock:OKQuoted 5 lines
Summary:
#48 cgrp_local_storage:OK
Summary: 1/13 PASSED, 0 SKIPPED, 0 FAILEDSigned-off-by Yafang Shao <[email protected]>LGTM with a few nits below.
Acked-by Yonghong Song <[email protected]>Quoted 25 lines
--- .../bpf/prog_tests/cgrp_local_storage.c | 92 ++++++++++++++++++- .../selftests/bpf/progs/cgrp_ls_recursion.c | 84 +++++++++++++---- .../selftests/bpf/progs/cgrp_ls_sleepable.c | 67 ++++++++++++-- .../selftests/bpf/progs/cgrp_ls_tp_btf.c | 82 ++++++++++++----- 4 files changed, 278 insertions(+), 47 deletions(-)tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +3
-0@@ -19,6 +19,9 @@ struct socket_cookie {__u64 cookie_value; }; +bool is_cgroup1; +int target_hid; +static void test_tp_btf (int cgroup_fd) {struct cgrp_ls_tp_btf *skel;@@ -29,6 +32,9 @@ static void test_tp_btf(int cgroup_fd)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ;
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c static void test_tp_btf(int cgroup_fd) · line 36
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +2
-0+ skel->bss->is_cgroup1 = is_cgroup1; + skel->bss->target_hid = target_hid;Let reverse the order like below to be consistent with other code patterns:
will change it.
Quoted 56 lines
(quoted diff) +2
-0+ skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1;Quoted 54 lines
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c +4
-0+/* populate a value in map_b */ err =bpf_map_update_elem (bpf_map__fd (skel->maps.map_b), &cgroup_fd, &val1,BPF_ANY );if (!ASSERT_OK (err,"map_update_elem" ))@@ -130,6 +136,9 @@ static void test_recursion(int cgroup_fd)if (!ASSERT_OK_PTR (skel,"skel_open_and_load" ))return ; + skel->bss->target_hid = target_hid; + skel->bss->is_cgroup1 = is_cgroup1; + err =cgrp_ls_recursion__attach (skel);if (!ASSERT_OK (err,"skel_attach" )) goto out;tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +18
-1@@ -17,7 +17,11 @@ struct {__u32 target_pid;__u64 cgroup_id; +int target_hid; +bool is_cgroup1; +struct cgroup *bpf_task_get_cgroup1 (struct task_struct *task,int hierarchy_id) __ksym; +void bpf_cgroup_release(struct cgroup *cgrp) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym;@@ -37,23 +41,56 @@ int cgroup_iter(struct bpf_iter__cgroup *ctx)return 0 ; } +static void __no_rcu_lock (struct cgroup *cgrp) +{ +long *ptr; + +/* Note that trace rcu is held in sleepable prog, so we can use +* bpf_cgrp_storage_get() in sleepable prog. +*/ + ptr =bpf_cgrp_storage_get (&map_a, cgrp,0 , +BPF_LOCAL_STORAGE_GET_F_CREATE ); +if (ptr) + cgroup_id = cgrp->kn->id; +} + SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") -int no_rcu_lock (void *ctx) +int cgrp1_no_rcu_lock (void *ctx) {struct task_struct *task;
Quoted 8 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c int cgroup_iter(struct bpf_iter__cgroup *ctx) · line 64
Quoted 5 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +3
-1struct cgroup *cgrp; -long *ptr; + +if (!is_cgroup1) +return 0 ;Do we need this check? Looks like the user space controls whether it will be loaded or not depending on whether it is cgrp1.
will remove this check.
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +16
-0+ + task =bpf_get_current_task_btf (); +if (task->pid != target_pid) +return 0 ; + +/* bpf_task_get_cgroup1 can work in sleepable prog */ + cgrp =bpf_task_get_cgroup1 (task, target_hid); +if (!cgrp) +return 0 ; + +__no_rcu_lock (cgrp); +bpf_cgroup_release (cgrp); +return 0 ; +} + +SEC ("?fentry.s/" SYS_PREFIX "sys_getpgid" )
Quoted 8 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c int cgroup_iter(struct bpf_iter__cgroup *ctx) · line 86
Quoted 6 lines
tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c +6
-0+int no_rcu_lock (void *ctx) +{ +struct task_struct *task; + +if (is_cgroup1) +return 0 ;Same here, check is not needed.
will remove it.
Thanks for your review.
Signature · 2 lines
Regards Yafang
Quoted 15 lines · Yafang Shao
In the current cgroup1 environment, associating operations between a cgroup and applications in a BPF program requires storing a mapping of
cgroup_idto application either in a hash map or maintaining it in userspace. However, by enablingbpf_cgrp_storagefor cgroup1, it becomes possible to conveniently store application-specific information in cgroup-local storage and utilize it within BPF programs. Furthermore, enabling this feature for cgroup1 involves minor modifications for the non-attach case, streamlining the process.However, when it comes to enabling this functionality for the cgroup1 attach case, it presents challenges. Therefore, the decision is to focus on enabling it solely for the cgroup1 non-attach case at present. If attempting to attach to a cgroup1 fd, the operation will simply fail with the error code
-EBADF.
Quoted 4 lines
Yafang Shao (3):
bpf: Enablebpf_cgrp_storagefor cgroup1 non-attach case selftests/bpf: Add a new cgroup helperopen_classid()selftests/bpf: Add selftests for cgroup1 local storage
Thanks.
Signature · 1 line
tejun
Quoted 17 lines · Tejun Heo
Quoted 15 lines · Yafang Shao
In the current cgroup1 environment, associating operations between a cgroup and applications in a BPF program requires storing a mapping of
cgroup_idto application either in a hash map or maintaining it in userspace. However, by enablingbpf_cgrp_storagefor cgroup1, it becomes possible to conveniently store application-specific information in cgroup-local storage and utilize it within BPF programs. Furthermore, enabling this feature for cgroup1 involves minor modifications for the non-attach case, streamlining the process.However, when it comes to enabling this functionality for the cgroup1 attach case, it presents challenges. Therefore, the decision is to focus on enabling it solely for the cgroup1 non-attach case at present. If attempting to attach to a cgroup1 fd, the operation will simply fail with the error code
-EBADF.
Quoted 6 lines
Quoted 4 lines
Yafang Shao (3):
bpf: Enablebpf_cgrp_storagefor cgroup1 non-attach case selftests/bpf: Add a new cgroup helperopen_classid()selftests/bpf: Add selftests for cgroup1 local storageAcked-by Tejun Heo <[email protected]>
Yafang,
please resubmit without RFC tag, so it can get tested by BPF CI.
Quoted 24 lines · Alexei Starovoitov
Quoted 22 lines · Tejun Heo
Quoted 19 lines · Yafang Shao
In the current cgroup1 environment, associating operations between a cgroup and applications in a BPF program requires storing a mapping of
cgroup_idto application either in a hash map or maintaining it in userspace. However, by enablingbpf_cgrp_storagefor cgroup1, it becomes possible to conveniently store application-specific information in cgroup-local storage and utilize it within BPF programs. Furthermore, enabling this feature for cgroup1 involves minor modifications for the non-attach case, streamlining the process.However, when it comes to enabling this functionality for the cgroup1 attach case, it presents challenges. Therefore, the decision is to focus on enabling it solely for the cgroup1 non-attach case at present. If attempting to attach to a cgroup1 fd, the operation will simply fail with the error code
-EBADF.Yafang Shao (3):
bpf: Enablebpf_cgrp_storagefor cgroup1 non-attach case selftests/bpf: Add a new cgroup helperopen_classid()selftests/bpf: Add selftests for cgroup1 local storage
Quoted 5 lines
Quoted 1 line
Acked-by Tejun Heo <[email protected]>Yafang,
please resubmit without RFC tag, so it can get tested by BPF CI.
will do it.
Signature · 2 lines
Regards Yafang