[RFC PATCH bpf-next 0/4] Add Open-coded process and css iters

From: Chuyi Zhou
Date: Sun Aug 27 2023 - 03:21:46 EST


In some BPF usage scenarios, it will be useful to iterate the process and
css directly in the BPF program. One of the expected scenarios is
customizable OOM victim selection via BPF[1].

Inspired by Dave's task_vma iter[2], this patchset adds three types of
open-coded iterator kfuncs:

1. bpf_for_each(process, p). Just like for_each_process(p) in kernel to
itearing all tasks in the system with rcu_read_lock().

2. bpf_for_each(css_task, task, css). It works like
css_task_iter_{start, next, end} and would be used to iterating
tasks/threads under a css.

3. bpf_for_each(css, pos, root_css, {PRE, POST}). It works like
css_next_descendant_{pre, post} to iterating all descendant css.

BPF programs can use these kfuncs directly or through bpf_for_each macro.

link[1]: https://lore.kernel.org/lkml/20230810081319.65668-1-zhouchuyi@xxxxxxxxxxxxx/
link[2]: https://lore.kernel.org/all/20230810183513.684836-1-davemarchevsky@xxxxxx/

Chuyi Zhou (4):
bpf: Introduce css_task open-coded iterator kfuncs
bpf: Introduce process open coded iterator kfuncs
bpf: Introduce css_descendant open-coded iterator kfuncs
selftests/bpf: Add tests for open-coded task and css iter

include/uapi/linux/bpf.h | 13 ++
kernel/bpf/helpers.c | 9 ++
kernel/bpf/task_iter.c | 109 ++++++++++++++++
tools/include/uapi/linux/bpf.h | 13 ++
tools/lib/bpf/bpf_helpers.h | 18 +++
.../testing/selftests/bpf/prog_tests/iters.c | 123 ++++++++++++++++++
.../testing/selftests/bpf/progs/iters_task.c | 83 ++++++++++++
7 files changed, 368 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/iters_task.c

--
2.20.1