bpf: Add bpf_iter_cpumask
Subsystems (4)
v6 cover bpf: Add bpf_iter_cpumask
Three new kfuncs, namely bpf_iter_cpumask_{new,next,destroy}, have been added for the new bpf_iter_cpumask functionality. These kfuncs enable the iteration of percpu data, such as runqueues, system_group_pcpu, and more.
In our specific use case, we leverage the cgroup iterator to traverse percpu data, subsequently exposing it to userspace through a seq file. Refer to example in patch #2 for the usage.
Changes:
- v5 -> v6:
- Various improvements on the comments (Andrii)
- Use a static function instead as Kumar's patch[0] has been merged. (Anrii, Yonghong)
[0]. https://lore.kernel.org/bpf/170719262630.31872.2248639771567354367.git-patchwork-notify@kernel.org
- v4 -> v5:
- Use cpumask_size() instead of sizeof(struct cpumask) (David)
- Refactor the selftests as suggsted by David
- Improve the doc as suggested by David
- v3 -> v4:
- Use a copy of cpumask to avoid potential use-after-free (Yonghong)
- Various code improvement in selftests (Yonghong)
- v2 -> v3:
- Define KF_RCU_PROTECTED for bpf_iter_cpumask_new (Alexei)
- Code improvement in selftests
- Fix build error in selftest due to CONFIG_PSI=n reported by kernel test robot <[email protected]>
- v1 -> v2:
- Avoid changing cgroup subsystem (Tejun)
- Remove bpf_cpumask_set_from_pid(), and use bpf_cpumask_copy() instead (Tejun)
- Use int cpu; field in bpf_iter_cpumask_kern (Andrii)
Yafang Shao (5):
bpf: Add bpf_iter_cpumask kfuncs
bpf, docs: Add document for cpumask iter selftests/bpf: Fix error checking for cpumask_success__load() selftests/bpf: Mark cpumask kfunc declarations as __weak selftests/bpf: Add selftests for cpumask iter
Documentation/bpf/cpumasks.rst | 60 +++++++ kernel/bpf/cpumask.c | 79 +++++++++ tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/cpumask.c | 158 +++++++++++++++++- .../selftests/bpf/progs/cpumask_common.h | 60 +++---- .../bpf/progs/cpumask_iter_failure.c | 99 +++++++++++ .../bpf/progs/cpumask_iter_success.c | 126 ++++++++++++++ 7 files changed, 552 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_success.c
Signature · 1 line
2.39.1
PATCH v6 1/5 bpf: Add bpf_iter_cpumask kfuncs
Add three new kfuncs for bpf_iter_cpumask.
- bpf_iter_cpumask_new
KF_RCU is defined because the cpumask must be a RCU trusted pointer such as task->cpus_ptr.
- bpf_iter_cpumask_next
- bpf_iter_cpumask_destroy
These new kfuncs facilitate the iteration of percpu data, such as runqueues, psi_cgroup_cpu, and more.
--- kernel/bpf/cpumask.c | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)
kernel/bpf/cpumask.c +79 -0
@@ -422,6 +422,82 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)return cpumask_weight (cpumask); } +struct bpf_iter_cpumask { +__u64 __opaque[2 ]; +}__aligned(8) ; + +struct bpf_iter_cpumask_kern { +struct cpumask *mask; +int cpu; +}__aligned(8) ; + +/** +* bpf_iter_cpumask_new() - Initialize a new CPU mask iterator for a given CPU mask +* @it: The new bpf_iter_cpumask to be created. +* @mask: The cpumask to be iterated over. +* +* This function initializes a new bpf_iter_cpumask structure for iterating over +* the specified CPU mask. It assigns the provided cpumask to the newly created +* bpf_iter_cpumask @it for subsequent iteration operations. +* +* On success, 0 is returned. On failure, ERR is returned. +*/ +__bpf_kfunc int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it; + +BUILD_BUG_ON (sizeof (struct bpf_iter_cpumask_kern ) >sizeof (struct bpf_iter_cpumask )); +BUILD_BUG_ON (__alignof__(struct bpf_iter_cpumask_kern ) != + __alignof__(struct bpf_iter_cpumask )); + + kit->mask =bpf_mem_alloc (&bpf_global_ma,cpumask_size ()); +if (!kit->mask) +return -ENOMEM ; + +cpumask_copy (kit->mask, mask); + kit->cpu =-1 ; +return 0 ; +} + +/** +* bpf_iter_cpumask_next() - Get the next CPU in a bpf_iter_cpumask +* @it: The bpf_iter_cpumask +* +* This function returns a pointer to a number representing the ID of the +* next CPU in CPU mask. It allows sequential access to CPUs within the +* cpumask. If there are no further CPUs available, it returns NULL. +*/ +__bpf_kfunc int *bpf_iter_cpumask_next(struct bpf_iter_cpumask *it) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it; +const struct cpumask *mask = kit->mask; +int cpu; + +if (!mask) +return NULL ; + cpu =cpumask_next (kit->cpu, mask); +if (cpu >= nr_cpu_ids) +return NULL ; + + kit->cpu = cpu; +return &kit->cpu; +} + +/** +* bpf_iter_cpumask_destroy() - Destroy a bpf_iter_cpumask +* @it: The bpf_iter_cpumask to be destroyed. +* +* Destroy the resource associated with the bpf_iter_cpumask. +*/ +__bpf_kfunc void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it; + +if (!kit->mask) +return ; +bpf_mem_free (&bpf_global_ma, kit->mask); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (cpumask_kfunc_btf_ids)
@@ -450,6 +526,9 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU) +BTF_ID_FLAGS(func, bpf_iter_cpumask_new, KF_ITER_NEW | KF_RCU) +BTF_ID_FLAGS(func, bpf_iter_cpumask_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_cpumask_destroy, KF_ITER_DESTROY)BTF_KFUNCS_END (cpumask_kfunc_btf_ids) static const struct btf_kfunc_id_set cpumask_kfunc_set = {
Signature · 1 line
2.39.1
Re: [PATCH v6 bpf-next 1/5] bpf: Add bpf_iter_cpumask kfuncs
Quoted 47 lines · Yafang Shao
Add three new kfuncs for
bpf_iter_cpumask.
-bpf_iter_cpumask_new
KF_RCUis defined because the cpumask must be a RCU trusted pointer such astask->cpus_ptr.
-bpf_iter_cpumask_next
-bpf_iter_cpumask_destroyThese new kfuncs facilitate the iteration of percpu data, such as runqueues,
psi_cgroup_cpu, and more.Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/cpumask.c | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)kernel/bpf/cpumask.c +23
-0@@ -422,6 +422,82 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)return cpumask_weight (cpumask); } +struct bpf_iter_cpumask { +__u64 __opaque[2 ]; +}__aligned(8) ; + +struct bpf_iter_cpumask_kern { +struct cpumask *mask; +int cpu; +}__aligned(8) ; + +/** +* bpf_iter_cpumask_new() - Initialize a new CPU mask iterator for a given CPU mask +* @it: The new bpf_iter_cpumask to be created. +* @mask: The cpumask to be iterated over. +* +* This function initializes a new bpf_iter_cpumask structure for iterating over +* the specified CPU mask. It assigns the provided cpumask to the newly created +* bpf_iter_cpumask @it for subsequent iteration operations. +* +* On success, 0 is returned. On failure, ERR is returned. +*/ +__bpf_kfunc int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it;
kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 457
Quoted 10 lines
kernel/bpf/cpumask.c +10
-0+ +BUILD_BUG_ON (sizeof (struct bpf_iter_cpumask_kern ) >sizeof (struct bpf_iter_cpumask )); +BUILD_BUG_ON (__alignof__(struct bpf_iter_cpumask_kern ) != + __alignof__(struct bpf_iter_cpumask )); + + kit->mask =bpf_mem_alloc (&bpf_global_ma,cpumask_size ()); +if (!kit->mask) +return -ENOMEM ; + +cpumask_copy (kit->mask, mask);
Since it's mem_alloc plus memcpy how about we make it more generic ?
Instead of cpumask specific let's pass arbitrary "void *unsafe_addr, u32 size"
allocate that much and probe_read_kernel into the buffer?
kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 478
Quoted 9 lines
kernel/bpf/cpumask.c +9
-0+__bpf_kfunc int *bpf_iter_cpumask_next(struct bpf_iter_cpumask *it) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it; +const struct cpumask *mask = kit->mask; +int cpu; + +if (!mask) +return NULL ; + cpu =cpumask_next (kit->cpu, mask);
Instead of cpumask_next() call find_next_bit()
kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 480
Quoted 2 lines
kernel/bpf/cpumask.c +2
-0+if (cpu >= nr_cpu_ids) +return NULL ;
instead of nr_cpu_ids we can check size in bits of copied bit array.
kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 469
Quoted 1 line
kernel/bpf/cpumask.c +0
-0BTF_ID_FLAGS (func, bpf_iter_cpumask_new,KF_ITER_NEW |KF_RCU )
KF_RCU is also not needed.
Such iterator will be callable from anywhere and on any address.
wdyt?
Re: [PATCH v6 bpf-next 1/5] bpf: Add bpf_iter_cpumask kfuncs
Quoted 83 lines · Alexei Starovoitov
Quoted 47 lines · Yafang Shao
Add three new kfuncs for
bpf_iter_cpumask.
-bpf_iter_cpumask_new
KF_RCUis defined because the cpumask must be a RCU trusted pointer such astask->cpus_ptr.
-bpf_iter_cpumask_next
-bpf_iter_cpumask_destroyThese new kfuncs facilitate the iteration of percpu data, such as runqueues,
psi_cgroup_cpu, and more.Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/cpumask.c | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)kernel/bpf/cpumask.c +23
-0@@ -422,6 +422,82 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)return cpumask_weight (cpumask); } +struct bpf_iter_cpumask { +__u64 __opaque[2 ]; +}__aligned(8) ; + +struct bpf_iter_cpumask_kern { +struct cpumask *mask; +int cpu; +}__aligned(8) ; + +/** +* bpf_iter_cpumask_new() - Initialize a new CPU mask iterator for a given CPU mask +* @it: The new bpf_iter_cpumask to be created. +* @mask: The cpumask to be iterated over. +* +* This function initializes a new bpf_iter_cpumask structure for iterating over +* the specified CPU mask. It assigns the provided cpumask to the newly created +* bpf_iter_cpumask @it for subsequent iteration operations. +* +* On success, 0 is returned. On failure, ERR is returned. +*/ +__bpf_kfunc int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it;kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 457
Quoted 10 lines
kernel/bpf/cpumask.c +10
-0+ +BUILD_BUG_ON (sizeof (struct bpf_iter_cpumask_kern ) >sizeof (struct bpf_iter_cpumask )); +BUILD_BUG_ON (__alignof__(struct bpf_iter_cpumask_kern ) != + __alignof__(struct bpf_iter_cpumask )); + + kit->mask =bpf_mem_alloc (&bpf_global_ma,cpumask_size ()); +if (!kit->mask) +return -ENOMEM ; + +cpumask_copy (kit->mask, mask);Since it's
mem_allocplus memcpy how about we make it more generic ?
Instead of cpumask specific let's pass arbitrary "void*unsafe_addr, u32 size"allocate that much and
probe_read_kernelinto the buffer?kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 478
Quoted 9 lines
kernel/bpf/cpumask.c +9
-0+__bpf_kfunc int *bpf_iter_cpumask_next(struct bpf_iter_cpumask *it) +{ +struct bpf_iter_cpumask_kern *kit = (void *)it; +const struct cpumask *mask = kit->mask; +int cpu; + +if (!mask) +return NULL ; + cpu =cpumask_next (kit->cpu, mask);Instead of
cpumask_next()callfind_next_bit()kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 480
Quoted 2 lines
kernel/bpf/cpumask.c +2
-0+if (cpu >= nr_cpu_ids) +return NULL ;
Quoted 8 lines
instead of
nr_cpu_idswe can check size in bits of copied bit array.kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 469
Quoted 1 line
kernel/bpf/cpumask.c +0
-0BTF_ID_FLAGS (func, bpf_iter_cpumask_new,KF_ITER_NEW |KF_RCU )
KF_RCUis also not needed.
Such iterator will be callable from anywhere and on any address.wdyt?
Good suggestion. A more generic bitmap iter is better. I will analyze it.
Signature · 2 lines
Regards Yafang
PATCH v6 2/5 bpf, docs: Add document for cpumask iter
In reply to Yafang ShaoThis patch adds the document for the newly added cpumask iterator kfuncs.
--- Documentation/bpf/cpumasks.rst | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
Documentation/bpf/cpumasks.rst +60 -0
@@ -372,6 +372,66 @@ used. .. _tools/testing/selftests/bpf/progs/cpumask_success.c: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/cpumask_success.c +3.3 cpumask iterator +-------------------- + +The cpumask iterator facilitates the iteration of per-CPU data, including +runqueues, system_group_pcpu, and other such structures. To leverage the cpumask +iterator, one can employ the bpf_for_each() macro. + +Here's an example illustrating how to determine the number of running tasks on +each CPU. + +.. code-block:: c + + /** + * Here's an example demonstrating the functionality of the cpumask iterator. + * We retrieve the cpumask associated with the specified pid, iterate through + * its elements, and ultimately expose per-CPU data to userspace through a + * seq file. + */ + const struct rq runqueues __ksym __weak; + u32 target_pid; + + SEC("iter/cgroup") + int BPF_PROG(cpu_cgroup, struct bpf_iter_meta *meta, struct cgroup *cgrp) + { + u32 nr_running = 0, nr_cpus = 0, nr_null_rq = 0; + struct task_struct *p; + struct rq *rq; + int *cpu; + + /* epilogue */ + if (cgrp == NULL) + return 0; + + p = bpf_task_from_pid(target_pid); + if (!p) + return 1; + + BPF_SEQ_PRINTF(meta->seq, "%4s %s\n", "CPU", "nr_running"); + bpf_for_each(cpumask, cpu, p->cpus_ptr) { + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, *cpu); + if (!rq) { + nr_null_rq += 1; + continue; + } + nr_cpus += 1; + + if (!rq->nr_running) + continue; + + nr_running += rq->nr_running; + BPF_SEQ_PRINTF(meta->seq, "%4u %u\n", *cpu, rq->nr_running); + } + BPF_SEQ_PRINTF(meta->seq, "Summary: nr_cpus %u, nr_running %u, nr_null_rq %u\n", + nr_cpus, nr_running, nr_null_rq); + + bpf_task_release(p); + return 0; + } + +---- 4. Adding BPF cpumask kfuncs ============================
Signature · 1 line
2.39.1
PATCH v6 3/5 selftests/bpf: Fix error checking for cpumask_success__load()
In reply to Yafang ShaoWe should verify the return value of cpumask_success__load().
--- tools/testing/selftests/bpf/prog_tests/cpumask.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/cpumask.c +3 -3
@@ -27,7 +27,7 @@ static void verify_success(const char *prog_name)struct bpf_program *prog;struct bpf_link *link =NULL ;pid_t child_pid; -int status; +int status, err; skel =cpumask_success__open ();if (!ASSERT_OK_PTR (skel,"cpumask_success__open" ))
@@ -36,8 +36,8 @@ static void verify_success(const char *prog_name) skel->bss->pid =getpid (); skel->bss->nr_cpus =libbpf_num_possible_cpus (); -cpumask_success__load (skel); -if (!ASSERT_OK_PTR (skel,"cpumask_success__load" )) + err =cpumask_success__load (skel); +if (!ASSERT_OK (err,"cpumask_success__load" )) goto cleanup; prog =bpf_object__find_program_by_name (skel->obj, prog_name);
Signature · 1 line
2.39.1
PATCH v6 4/5 selftests/bpf: Mark cpumask kfunc declarations as __weak
In reply to Yafang ShaoAfter the series "Annotate kfuncs in .BTF_ids section"[0], kfuncs can be generated from bpftool. Let's mark the existing cpumask kfunc declarations __weak so they don't conflict with definitions that will eventually come from vmlinux.h.
[0]. https://lore.kernel.org/all/[email protected]
--- .../selftests/bpf/progs/cpumask_common.h | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-)
tools/testing/selftests/bpf/progs/cpumask_common.h +29 -28
@@ -23,41 +23,42 @@ struct array_map {__uint (max_entries,1 ); } __cpumask_map SEC(".maps"); -struct bpf_cpumask *bpf_cpumask_create (void ) __ksym; -void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym; -struct bpf_cpumask *bpf_cpumask_acquire (struct bpf_cpumask *cpumask) __ksym; -u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym; -u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym; +struct bpf_cpumask *bpf_cpumask_create (void ) __ksym__weak ; +void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym __weak; +struct bpf_cpumask *bpf_cpumask_acquire (struct bpf_cpumask *cpumask) __ksym__weak ; +u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym __weak; +u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym __weak; u32 bpf_cpumask_first_and(const struct cpumask *src1, - const struct cpumask *src2) __ksym; -void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym; -bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym; -void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym; + const struct cpumask *src2) __ksym __weak; +void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym __weak; +void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym __weak; bool bpf_cpumask_and(struct bpf_cpumask *cpumask, const struct cpumask *src1, - const struct cpumask *src2) __ksym; + const struct cpumask *src2) __ksym __weak; void bpf_cpumask_or(struct bpf_cpumask *cpumask, const struct cpumask *src1, - const struct cpumask *src2) __ksym; + const struct cpumask *src2) __ksym __weak; void bpf_cpumask_xor(struct bpf_cpumask *cpumask, const struct cpumask *src1, - const struct cpumask *src2) __ksym; -bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym; -bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym; -bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym; -bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym; -bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym; -void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym; -u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym; -u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym; -u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym; - -void bpf_rcu_read_lock(void) __ksym; -void bpf_rcu_read_unlock(void) __ksym; + const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym __weak; +void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym __weak; +u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym __weak; +u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, + const struct cpumask *src2) __ksym __weak; +u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym __weak; + +void bpf_rcu_read_lock(void) __ksym __weak; +void bpf_rcu_read_unlock(void) __ksym __weak;static inline const struct cpumask *cast (struct bpf_cpumask *cpumask) {
Signature · 1 line
2.39.1
Re: [PATCH v6 bpf-next 4/5] selftests/bpf: Mark cpumask kfunc declarations as __weak
Quoted 5 lines · Yafang Shao
After the series "Annotate kfuncs in .BTF_ids section"[0], kfuncs can be generated from bpftool. Let's mark the existing cpumask kfunc declarations
__weakso they don't conflict with definitions that will eventually come fromvmlinux.h.
Quoted 8 lines
[0]. https://lore.kernel.org/all/[email protected]
Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Yafang Shao <[email protected]>Cc Daniel Xu <[email protected]>--- .../selftests/bpf/progs/cpumask_common.h | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-)
Thanks!
[..]
PATCH v6 5/5 selftests/bpf: Add selftests for cpumask iter
In reply to Yafang ShaoAdd selftests for the newly added cpumask iter.
- cpumask_iter_success
- The number of CPUs should be expected when iterating over the cpumask
- percpu data extracted from the percpu struct should be expected
- It can work in both non-sleepable and sleepable prog
- RCU lock is only required by bpf_iter_cpumask_new()
- It is fine without calling bpf_iter_cpumask_next()
- cpumask_iter_failure
- RCU lock is required in sleepable prog
- The cpumask to be iterated over can't be NULL
- bpf_iter_cpumask_destroy() is required after calling bpf_iter_cpumask_new()
- bpf_iter_cpumask_destroy() can only destroy an initialized iter
- bpf_iter_cpumask_next() must use an initialized iter
The result as follows,
#64/37 cpumask/test_cpumask_iter:OK #64/38 cpumask/test_cpumask_iter_sleepable:OK #64/39 cpumask/test_cpumask_iter_sleepable:OK #64/40 cpumask/test_cpumask_iter_next_no_rcu:OK #64/41 cpumask/test_cpumask_iter_no_next:OK #64/42 cpumask/test_cpumask_iter:OK #64/43 cpumask/test_cpumask_iter_no_rcu:OK #64/44 cpumask/test_cpumask_iter_no_destroy:OK #64/45 cpumask/test_cpumask_iter_null_pointer:OK #64/46 cpumask/test_cpumask_iter_next_uninit:OK #64/47 cpumask/test_cpumask_iter_destroy_uninit:OK #64 cpumask:OK
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/cpumask.c | 152 ++++++++++++++++++ .../selftests/bpf/progs/cpumask_common.h | 3 + .../bpf/progs/cpumask_iter_failure.c | 99 ++++++++++++ .../bpf/progs/cpumask_iter_success.c | 126 +++++++++++++++ 5 files changed, 381 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_success.c
tools/testing/selftests/bpf/config +1 -0
@@ -78,6 +78,7 @@ CONFIG_NF_CONNTRACK_MARK=y CONFIG_NF_DEFRAG_IPV4=y CONFIG_NF_DEFRAG_IPV6=y CONFIG_NF_NAT=y +CONFIG_PSI=y CONFIG_RC_CORE=y CONFIG_SECURITY=y CONFIG_SECURITYFS=y
tools/testing/selftests/bpf/prog_tests/cpumask.c +152 -0
171 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cpumask_common.h +3 -0
@@ -56,6 +56,9 @@ u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym __weak; u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym __weak; +int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) __ksym __weak; +int *bpf_iter_cpumask_next (struct bpf_iter_cpumask *it) __ksym__weak ; +void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it) __ksym __weak; void bpf_rcu_read_lock(void) __ksym __weak; void bpf_rcu_read_unlock(void) __ksym __weak;
tools/testing/selftests/bpf/progs/cpumask_iter_failure.c new file +99 -0
99 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cpumask_iter_success.c new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.39.1
Quoted 35 lines · Yafang Shao
Three new kfuncs, namely bpf_iter_cpumask_{new,next,destroy}, have been added for the new
bpf_iter_cpumaskfunctionality. These kfuncs enable the iteration of percpu data, such as runqueues,system_group_pcpu, and more.In our specific use case, we leverage the cgroup iterator to traverse percpu data, subsequently exposing it to userspace through a seq file. Refer to example in patch #2 for the usage.
Changes:
- v5 -> v6:
- Various improvements on the comments (Andrii)
- Use a static function instead as Kumar's patch[0] has been merged. (Anrii, Yonghong)[0]. https://lore.kernel.org/bpf/170719262630.31872.2248639771567354367.git-patchwork-notify@kernel.org
- v4 -> v5:
- Usecpumask_size()instead of sizeof(struct cpumask) (David)
- Refactor the selftests as suggsted by David
- Improve the doc as suggested by David
- v3 -> v4:
- Use a copy of cpumask to avoid potential use-after-free (Yonghong)
- Various code improvement in selftests (Yonghong)
- v2 -> v3:
- DefineKF_RCU_PROTECTEDforbpf_iter_cpumask_new(Alexei)
- Code improvement in selftests
- Fix build error in selftest due toCONFIG_PSI=nreported by kernel test robot <[email protected]>
- v1 -> v2:
- Avoid changing cgroup subsystem (Tejun)
- Removebpf_cpumask_set_from_pid(), and usebpf_cpumask_copy()instead (Tejun)
- Useint cpu;field inbpf_iter_cpumask_kern(Andrii)
Quoted 5 lines
Yafang Shao (5):
bpf: Addbpf_iter_cpumaskkfuncs
bpf, docs: Add document for cpumask iter selftests/bpf: Fix error checking forcpumask_success__load()selftests/bpf: Mark cpumask kfunc declarations as__weak
I've applied patches 3 and 4, as they are general clean ups and improvements independent from the actual iterator implementation. Thanks!
Quoted 16 lines
selftests/bpf: Add selftests for cpumask iter
Documentation/bpf/cpumasks.rst | 60 +++++++ kernel/bpf/cpumask.c | 79 +++++++++ tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/cpumask.c | 158 +++++++++++++++++- .../selftests/bpf/progs/cpumask_common.h | 60 +++---- .../bpf/progs/cpumask_iter_failure.c | 99 +++++++++++ .../bpf/progs/cpumask_iter_success.c | 126 ++++++++++++++ 7 files changed, 552 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_success.c--
2.39.1