bpf: add new BPF_CGROUP_ITER_CHILDREN control option
Subsystems (4)
PATCH v2 1/2 bpf: add new BPF_CGROUP_ITER_CHILDREN control option
Currently, the BPF cgroup iterator supports walking descendants in either pre-order (BPF_CGROUP_ITER_DESCENDANTS_PRE) or post-order (BPF_CGROUP_ITER_DESCENDANTS_POST). These modes perform an exhaustive depth-first search (DFS) of the hierarchy. In scenarios where a BPF program may need to inspect only the direct children of a given parent cgroup, a full DFS is unnecessarily expensive.
This patch introduces a new BPF cgroup iterator control option, BPF_CGROUP_ITER_CHILDREN. This control option restricts the traversal to the immediate children of a specified parent cgroup, allowing for more targeted and efficient iteration, particularly when exhaustive depth-first search (DFS) traversal is not required.
---
v2:
- Undone the unnecessary formating changes applied to the bpf_cgroup_iter_order enum.
- Renamed the new bpf_cgroup_iter_order enum entry from BPF_CGROUP_ITER_CHILDREN_ONLY to BPF_CGROUP_ITER_CHILDREN, as per recommendation from Alexei.
include/uapi/linux/bpf.h | 8 ++++++++ kernel/bpf/cgroup_iter.c | 26 +++++++++++++++++++++----- tools/include/uapi/linux/bpf.h | 8 ++++++++ 3 files changed, 37 insertions(+), 5 deletions(-)
include/uapi/linux/bpf.h +8 -0
@@ -119,6 +119,14 @@ enum bpf_cgroup_iter_order {BPF_CGROUP_ITER_DESCENDANTS_PRE ,/* walk descendants in pre-order. */ BPF_CGROUP_ITER_DESCENDANTS_POST ,/* walk descendants in post-order. */ BPF_CGROUP_ITER_ANCESTORS_UP ,/* walk ancestors upward. */ +/* +* Walks the immediate children of the specified parent +* cgroup_subsys_state. Unlike BPF_CGROUP_ITER_DESCENDANTS_PRE, +* BPF_CGROUP_ITER_DESCENDANTS_POST, and BPF_CGROUP_ITER_ANCESTORS_UP +* the iterator does not include the specified parent as one of the +* returned iterator elements. +*/ + BPF_CGROUP_ITER_CHILDREN, }; union bpf_iter_link_info {
kernel/bpf/cgroup_iter.c +21 -5
@@ -8,12 +8,13 @@#include "../cgroup/cgroup-internal.h" /* cgroup_mutex and cgroup_is_dead */ -/* cgroup_iter provides four modes of traversal to the cgroup hierarchy. +/* cgroup_iter provides five modes of traversal to the cgroup hierarchy. * * 1. Walk the descendants of a cgroup in pre-order. * 2. Walk the descendants of a cgroup in post-order. * 3. Walk the ancestors of a cgroup. * 4. Show the given cgroup only. +* 5. Walk the children of a given parent cgroup. * * For walking descendants, cgroup_iter can walk in either pre-order or * post-order. For walking ancestors, the iter walks up from a cgroup to
@@ -78,6 +79,8 @@ static void *cgroup_iter_seq_start(struct seq_file *seq, loff_t *pos)return css_next_descendant_pre (NULL , p->start_css); else if (p->order == BPF_CGROUP_ITER_DESCENDANTS_POST) return css_next_descendant_post(NULL, p->start_css); + else if (p->order == BPF_CGROUP_ITER_CHILDREN) + return css_next_child(NULL, p->start_css); else /* BPF_CGROUP_ITER_SELF_ONLY and BPF_CGROUP_ITER_ANCESTORS_UP */ return p->start_css; }
@@ -113,6 +116,8 @@ static void *cgroup_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)return css_next_descendant_post (curr, p->start_css); else if (p->order == BPF_CGROUP_ITER_ANCESTORS_UP) return curr->parent; + else if (p->order == BPF_CGROUP_ITER_CHILDREN) + return css_next_child(curr, p->start_css); else /* BPF_CGROUP_ITER_SELF_ONLY */ return NULL; }
@@ -200,11 +205,16 @@ static int bpf_iter_attach_cgroup(struct bpf_prog *prog,int order = linfo->cgroup.order;struct cgroup *cgrp; -if (order !=BPF_CGROUP_ITER_DESCENDANTS_PRE && - order !=BPF_CGROUP_ITER_DESCENDANTS_POST && - order !=BPF_CGROUP_ITER_ANCESTORS_UP && - order !=BPF_CGROUP_ITER_SELF_ONLY ) +switch (order) { +case BPF_CGROUP_ITER_DESCENDANTS_PRE : +case BPF_CGROUP_ITER_DESCENDANTS_POST : +case BPF_CGROUP_ITER_ANCESTORS_UP : +case BPF_CGROUP_ITER_SELF_ONLY : +case BPF_CGROUP_ITER_CHILDREN : +break ; +default :return -EINVAL ; + }if (fd && id)return -EINVAL ;
@@ -257,6 +267,8 @@ static void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux,seq_puts (seq,"order: descendants_post\n" ); else if (aux->cgroup.order == BPF_CGROUP_ITER_ANCESTORS_UP) seq_puts(seq, "order: ancestors_up\n"); + else if (aux->cgroup.order == BPF_CGROUP_ITER_CHILDREN) + seq_puts(seq, "order: children\n"); else /* BPF_CGROUP_ITER_SELF_ONLY */ seq_puts(seq, "order: self_only\n"); }
@@ -320,6 +332,7 @@ __bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it,case BPF_CGROUP_ITER_DESCENDANTS_PRE :case BPF_CGROUP_ITER_DESCENDANTS_POST :case BPF_CGROUP_ITER_ANCESTORS_UP : +case BPF_CGROUP_ITER_CHILDREN :break ;default :return -EINVAL ;
@@ -345,6 +358,9 @@ __bpf_kfunc struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *icase BPF_CGROUP_ITER_DESCENDANTS_POST : kit->pos =css_next_descendant_post (kit->pos, kit->start);break ; +case BPF_CGROUP_ITER_CHILDREN : + kit->pos =css_next_child (kit->pos, kit->start); +break ;case BPF_CGROUP_ITER_ANCESTORS_UP : kit->pos = kit->pos ? kit->pos->parent : kit->start; }
tools/include/uapi/linux/bpf.h +8 -0
@@ -119,6 +119,14 @@ enum bpf_cgroup_iter_order {BPF_CGROUP_ITER_DESCENDANTS_PRE ,/* walk descendants in pre-order. */ BPF_CGROUP_ITER_DESCENDANTS_POST ,/* walk descendants in post-order. */ BPF_CGROUP_ITER_ANCESTORS_UP ,/* walk ancestors upward. */ +/* +* Walks the immediate children of the specified parent +* cgroup_subsys_state. Unlike BPF_CGROUP_ITER_DESCENDANTS_PRE, +* BPF_CGROUP_ITER_DESCENDANTS_POST, and BPF_CGROUP_ITER_ANCESTORS_UP +* the iterator does not include the specified parent as one of the +* returned iterator elements. +*/ + BPF_CGROUP_ITER_CHILDREN, }; union bpf_iter_link_info {
Signature · 1 line
2.53.0.rc1.217.geba53bf80e-goog
PATCH v2 2/2 bpf/selftests: cover BPF_CGROUP_ITER_CHILDREN control option
Extend some of the existing CSS iterator selftests such that they cover the newly introduced BPF_CGROUP_ITER_CHILDREN iterator control option.
---
v2:
- Renamed the new bpf_cgroup_iter_order enum entry from BPF_CGROUP_ITER_CHILDREN_ONLY to BPF_CGROUP_ITER_CHILDREN, as per recommendation from Alexei.
tools/testing/selftests/bpf/prog_tests/cgroup_iter.c | 12 ++++++++++++ tools/testing/selftests/bpf/prog_tests/iters.c | 8 +++++++- tools/testing/selftests/bpf/progs/iters_css.c | 9 ++++++--- 3 files changed, 25 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/cgroup_iter.c +12 -0
@@ -190,6 +190,16 @@ static void test_walk_self_only(struct cgroup_iter *skel) BPF_CGROUP_ITER_SELF_ONLY, "self_only"); } +static void test_walk_children (struct cgroup_iter *skel) +{ +snprintf (expected_output,sizeof (expected_output), +PROLOGUE "%8llu\n%8llu\n" EPILOGUE , cg_id[CHILD1 ], + cg_id[CHILD2 ]); + +read_from_cgroup_iter (skel->progs.cgroup_id_printer, cg_fd[PARENT ], +BPF_CGROUP_ITER_CHILDREN ,"children" ); +} +static void test_walk_dead_self_only (struct cgroup_iter *skel) {DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts, opts);
@@ -325,6 +335,8 @@ void test_cgroup_iter(void)test_walk_dead_self_only (skel);if (test__start_subtest ("cgroup_iter__self_only_css_task" ))test_walk_self_only_css_task (); +if (test__start_subtest ("cgroup_iter__children" )) +test_walk_children (skel); out:cgroup_iter__destroy (skel);
tools/testing/selftests/bpf/prog_tests/iters.c +7 -1
@@ -253,6 +253,11 @@ static void subtest_css_iters(void) { "/cg1/cg2" }, { "/cg1/cg2/cg3" }, { "/cg1/cg2/cg3/cg4" }, + { "/cg1/cg5" }, + { "/cg1/cg5/cg6" }, + { "/cg1/cg7" }, + { "/cg1/cg7/cg8" }, + { "/cg1/cg7/cg8/cg9" }, };int err, cg_nr =ARRAY_SIZE (cgs);int i;
@@ -284,7 +289,8 @@ static void subtest_css_iters(void)ASSERT_EQ (skel->bss->post_order_cnt, cg_nr,"post_order_cnt" );ASSERT_EQ (skel->bss->last_cg_id,get_cgroup_id (cgs[0 ].path),"last_cg_id" ); -ASSERT_EQ (skel->bss->tree_high, cg_nr -1 ,"tree_high" ); +ASSERT_EQ (skel->bss->children_cnt,3 ,"children_cnt" ); +ASSERT_EQ (skel->bss->tree_high,3 ,"tree_high" );iters_css__detach (skel); cleanup:cleanup_cgroup_environment ();
tools/testing/selftests/bpf/progs/iters_css.c +6 -3
@@ -12,8 +12,7 @@ char _license[] SEC("license") = "GPL";pid_t target_pid;u64 root_cg_id, leaf_cg_id;u64 first_cg_id, last_cg_id; - -int pre_order_cnt, post_order_cnt, tree_high; +int pre_order_cnt, post_order_cnt, children_cnt, tree_high;struct cgroup *bpf_cgroup_from_id (u64 cgid) __ksym; void bpf_cgroup_release(struct cgroup *p) __ksym;
@@ -43,7 +42,7 @@ int iter_css_for_each(const void *ctx) } root_css = &root_cgrp->self; leaf_css = &leaf_cgrp->self; - pre_order_cnt = post_order_cnt = tree_high =0 ; + pre_order_cnt = post_order_cnt = children_cnt = tree_high =0 ; first_cg_id = last_cg_id =0 ;bpf_rcu_read_lock ();
@@ -60,6 +59,10 @@ int iter_css_for_each(const void *ctx) first_cg_id = cur_cgrp->kn->id; } +bpf_for_each (css, pos, root_css,BPF_CGROUP_ITER_CHILDREN ) { + children_cnt++; + } +bpf_for_each (css, pos, leaf_css,BPF_CGROUP_ITER_ANCESTORS_UP ) tree_high++;
Signature · 1 line
2.53.0.rc1.217.geba53bf80e-goog
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Tue, 27 Jan 2026 08:51:10 +0000 you wrote:
Currently, the BPF cgroup iterator supports walking descendants in either pre-order (
BPF_CGROUP_ITER_DESCENDANTS_PRE) or post-order (BPF_CGROUP_ITER_DESCENDANTS_POST). These modes perform an exhaustive depth-first search (DFS) of the hierarchy. In scenarios where a BPF program may need to inspect only the direct children of a given parent cgroup, a full DFS is unnecessarily expensive.[...]
Here is the summary with links:
- [bpf-next,v2,1/2] bpf: add new BPF_CGROUP_ITER_CHILDREN control option
https://git.kernel.org/bpf/bpf-next/c/752b807028e6 - [bpf-next,v2,2/2] bpf/selftests: cover BPF_CGROUP_ITER_CHILDREN control option https://git.kernel.org/bpf/bpf-next/c/1456ebb291dd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html