bpf: Add a generic bits iterator
Subsystems (3)
v4 cover bpf: Add a generic bits iterator
Three new kfuncs, namely bpf_iter_bits_{new,next,destroy}, have been added for the new bpf_iter_bits functionality. These kfuncs enable the iteration of the bits from a given address and a given number of bits.
- bpf_iter_bits_new
Initialize a new bits iterator for a given memory area. Due to the limitation of bpf memalloc, the max number of bits to be iterated over is (4096 * 8).
- bpf_iter_bits_next
Get the next bit in a bpf_iter_bits
- bpf_iter_bits_destroy
Destroy a bpf_iter_bits
The bits iterator can be used in any context and on any address.
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:
- v3->v4:
- Fix endianness error on s390x (Andrii)
- zero-initialize kit->bits_copy and zero out nr_bits (Andrii)
- v2->v3:
- Optimization for u64/u32 mask (Andrii)
- v1->v2:
- Simplify the CPU number verification code to avoid the failure on s390x (Eduard)
- bpf: Add bpf_iter_cpumask
https://lwn.net/Articles/961104/
- bpf: Add new bpf helper bpf_for_each_cpu
https://lwn.net/Articles/939939/
Yafang Shao (2):
bpf: Add bits iterator
selftests/bpf: Add selftest for bits iter
kernel/bpf/helpers.c | 120 ++++++++++++++ .../selftests/bpf/prog_tests/bits_iter.c | 139 +++++++++++++++++ .../bpf/progs/test_bits_iter_failure.c | 54 +++++++ .../bpf/progs/test_bits_iter_success.c | 146 ++++++++++++++++++ 4 files changed, 459 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bits_iter.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_success.c
Signature · 1 line
2.39.1
PATCH v4 1/2 bpf: Add bits iterator
Add three new kfuncs for the bits iterator:
- bpf_iter_bits_new
Initialize a new bits iterator for a given memory area. Due to the limitation of bpf memalloc, the max number of bits that can be iterated over is limited to (4096 * 8).
- bpf_iter_bits_next
Get the next bit in a bpf_iter_bits
- bpf_iter_bits_destroy
Destroy a bpf_iter_bits
The bits iterator facilitates the iteration of the bits of a memory area, such as cpumask. It can be used in any context and on any address.
--- kernel/bpf/helpers.c | 120 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+)
kernel/bpf/helpers.c +120 -0
@@ -2549,6 +2549,123 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } +struct bpf_iter_bits { +__u64 __opaque[2 ]; +}__aligned(8) ; + +struct bpf_iter_bits_kern { +union { +unsigned long *bits; +unsigned long bits_copy; + }; +u32 nr_bits; +int bit; +}__aligned(8) ; + +/** +* bpf_iter_bits_new() - Initialize a new bits iterator for a given memory area +* @it: The new bpf_iter_bits to be created +* @unsafe_ptr__ign: A ponter pointing to a memory area to be iterated over +* @nr_bits: The number of bits to be iterated over. Due to the limitation of +* memalloc, it can't greater than (4096 * 8). +* +* This function initializes a new bpf_iter_bits structure for iterating over +* a memory area which is specified by the @unsafe_ptr__ign and @nr_bits. It +* copy the data of the memory area to the newly created bpf_iter_bits @it for +* subsequent iteration operations. +* +* On success, 0 is returned. On failure, ERR is returned. +*/ +__bpf_kfunc int +bpf_iter_bits_new (struct bpf_iter_bits *it,const void *unsafe_ptr__ign,u32 nr_bits) +{ +u32 size =BITS_TO_LONGS (nr_bits) *sizeof (unsigned long ); +struct bpf_iter_bits_kern *kit = (void *)it; +int err; + +BUILD_BUG_ON (sizeof (struct bpf_iter_bits_kern ) !=sizeof (struct bpf_iter_bits )); +BUILD_BUG_ON (__alignof__(struct bpf_iter_bits_kern ) != + __alignof__(struct bpf_iter_bits )); + +if (!unsafe_ptr__ign || !nr_bits) { + kit->bits =NULL ; +return -EINVAL ; + } + + kit->nr_bits =0 ; + kit->bits_copy =0 ; +/* Optimization for u64/u32 mask */ +if (nr_bits <=64 ) { + err =bpf_probe_read_kernel_common (&kit->bits_copy, size, unsafe_ptr__ign); +if (err) +return -EFAULT ; + + kit->nr_bits = nr_bits; + kit->bit =-1 ; +return 0 ; + } + +/* Fallback to memalloc */ + kit->bits =bpf_mem_alloc (&bpf_global_ma, size); +if (!kit->bits) +return -ENOMEM ; + + err =bpf_probe_read_kernel_common (kit->bits, size, unsafe_ptr__ign); +if (err) { +bpf_mem_free (&bpf_global_ma, kit->bits); +return err; + } + + kit->nr_bits = nr_bits; + kit->bit =-1 ; +return 0 ; +} + +/** +* bpf_iter_bits_next() - Get the next bit in a bpf_iter_bits +* @it: The bpf_iter_bits to be checked +* +* This function returns a pointer to a number representing the value of the +* next bit in the bits. +* +* If there are no further bit available, it returns NULL. +*/ +__bpf_kfunc int *bpf_iter_bits_next(struct bpf_iter_bits *it) +{ +struct bpf_iter_bits_kern *kit = (void *)it; +u32 nr_bits = kit->nr_bits; +const unsigned long *bits; +int bit; + +if (nr_bits ==0 ) +return NULL ; + + bits = nr_bits <=64 ? &kit->bits_copy : kit->bits; + bit =find_next_bit (bits, nr_bits, kit->bit +1 ); +if (bit >= nr_bits) { + kit->nr_bits =0 ; +return NULL ; + } + + kit->bit = bit; +return &kit->bit; +} + +/** +* bpf_iter_bits_destroy() - Destroy a bpf_iter_bits +* @it: The bpf_iter_bits to be destroyed +* +* Destroy the resource associated with the bpf_iter_bits. +*/ +__bpf_kfunc void bpf_iter_bits_destroy(struct bpf_iter_bits *it) +{ +struct bpf_iter_bits_kern *kit = (void *)it; + +if (kit->nr_bits <=64 ) +return ; +bpf_mem_free (&bpf_global_ma, kit->bits); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (generic_btf_ids)
@@ -2625,6 +2742,9 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) +BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) +BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY)BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.39.1
Re: [PATCH bpf-next v4 1/2] bpf: Add bits iterator
Quoted 11 lines · Yafang Shao
Add three new kfuncs for the bits iterator:
-bpf_iter_bits_new
Initialize a new bits iterator for a given memory area. Due to the limitation of bpf memalloc, the max number of bits that can be iterated over is limited to (4096 * 8).
-bpf_iter_bits_next
Get the next bit in abpf_iter_bits
-bpf_iter_bits_destroy
Destroy abpf_iter_bits
Quoted 8 lines
The bits iterator facilitates the iteration of the bits of a memory area, such as cpumask. It can be used in any context and on any address.
Signed-off-by Yafang Shao <[email protected]>--- kernel/bpf/helpers.c | 120 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+)
LGTM
Quoted 141 lines
kernel/bpf/helpers.c +120
-1@@ -2549,6 +2549,123 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } +struct bpf_iter_bits { +__u64 __opaque[2 ]; +}__aligned(8) ; + +struct bpf_iter_bits_kern { +union { +unsigned long *bits; +unsigned long bits_copy; + }; +u32 nr_bits; +int bit; +}__aligned(8) ; + +/** +* bpf_iter_bits_new() - Initialize a new bits iterator for a given memory area +* @it: The new bpf_iter_bits to be created +* @unsafe_ptr__ign: A ponter pointing to a memory area to be iterated over +* @nr_bits: The number of bits to be iterated over. Due to the limitation of +* memalloc, it can't greater than (4096 * 8). +* +* This function initializes a new bpf_iter_bits structure for iterating over +* a memory area which is specified by the @unsafe_ptr__ign and @nr_bits. It +* copy the data of the memory area to the newly created bpf_iter_bits @it for +* subsequent iteration operations. +* +* On success, 0 is returned. On failure, ERR is returned. +*/ +__bpf_kfunc int +bpf_iter_bits_new (struct bpf_iter_bits *it,const void *unsafe_ptr__ign,u32 nr_bits) +{ +u32 size =BITS_TO_LONGS (nr_bits) *sizeof (unsigned long ); +struct bpf_iter_bits_kern *kit = (void *)it; +int err; + +BUILD_BUG_ON (sizeof (struct bpf_iter_bits_kern ) !=sizeof (struct bpf_iter_bits )); +BUILD_BUG_ON (__alignof__(struct bpf_iter_bits_kern ) != + __alignof__(struct bpf_iter_bits )); + +if (!unsafe_ptr__ign || !nr_bits) { + kit->bits =NULL ; +return -EINVAL ; + } + + kit->nr_bits =0 ; + kit->bits_copy =0 ; +/* Optimization for u64/u32 mask */ +if (nr_bits <=64 ) { + err =bpf_probe_read_kernel_common (&kit->bits_copy, size, unsafe_ptr__ign); +if (err) +return -EFAULT ; + + kit->nr_bits = nr_bits; + kit->bit =-1 ; +return 0 ; + } + +/* Fallback to memalloc */ + kit->bits =bpf_mem_alloc (&bpf_global_ma, size); +if (!kit->bits) +return -ENOMEM ; + + err =bpf_probe_read_kernel_common (kit->bits, size, unsafe_ptr__ign); +if (err) { +bpf_mem_free (&bpf_global_ma, kit->bits); +return err; + } + + kit->nr_bits = nr_bits; + kit->bit =-1 ; +return 0 ; +} + +/** +* bpf_iter_bits_next() - Get the next bit in a bpf_iter_bits +* @it: The bpf_iter_bits to be checked +* +* This function returns a pointer to a number representing the value of the +* next bit in the bits. +* +* If there are no further bit available, it returns NULL. +*/ +__bpf_kfunc int *bpf_iter_bits_next(struct bpf_iter_bits *it) +{ +struct bpf_iter_bits_kern *kit = (void *)it; +u32 nr_bits = kit->nr_bits; +const unsigned long *bits; +int bit; + +if (nr_bits ==0 ) +return NULL ; + + bits = nr_bits <=64 ? &kit->bits_copy : kit->bits; + bit =find_next_bit (bits, nr_bits, kit->bit +1 ); +if (bit >= nr_bits) { + kit->nr_bits =0 ; +return NULL ; + } + + kit->bit = bit; +return &kit->bit; +} + +/** +* bpf_iter_bits_destroy() - Destroy a bpf_iter_bits +* @it: The bpf_iter_bits to be destroyed +* +* Destroy the resource associated with the bpf_iter_bits. +*/ +__bpf_kfunc void bpf_iter_bits_destroy(struct bpf_iter_bits *it) +{ +struct bpf_iter_bits_kern *kit = (void *)it; + +if (kit->nr_bits <=64 ) +return ; +bpf_mem_free (&bpf_global_ma, kit->bits); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (generic_btf_ids)@@ -2625,6 +2742,9 @@ BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) BTF_ID_FLAGS(func, bpf_dynptr_size) BTF_ID_FLAGS(func, bpf_dynptr_clone) +BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) +BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY)BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = { --2.39.1
PATCH v4 2/2 selftests/bpf: Add selftest for bits iter
In reply to Yafang ShaoAdd selftests for the newly added bits iter.
- bits_iter_success
- percpu data extracted from the percpu struct should be expected
- RCU lock is not required
- It is fine without calling bpf_iter_cpumask_next()
- It can work as expected when invalid arguments are passed
- bits_iter_failure
- bpf_iter_bits_destroy() is required after calling
bpf_iter_bits_new()
- bpf_iter_bits_destroy() can only destroy an initialized iter
- bpf_iter_bits_next() must use an initialized iter
--- .../selftests/bpf/prog_tests/bits_iter.c | 139 +++++++++++++++++ .../bpf/progs/test_bits_iter_failure.c | 54 +++++++ .../bpf/progs/test_bits_iter_success.c | 146 ++++++++++++++++++ 3 files changed, 339 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bits_iter.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_success.c
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +139 -0
@@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Yafang Shao <[email protected]> */ + +#define _GNU_SOURCE +#include <sched.h> + +#include <test_progs.h> +#include "test_bits_iter_success.skel.h" +#include "test_bits_iter_failure.skel.h" +#include "cgroup_helpers.h" + +static const char *const positive_testcases[] = { +"cpumask_memalloc" , +"cpumask_copy" , +}; + +static const char *const negative_testcases[] = { +"null_pointer" , +"zero_bit" , +"no_mem" , +"invalid_bits" , +}; + +static int read_percpu_data (struct bpf_link *link) +{ +int iter_fd, len; +char buf[128 ]; +size_t left; +char *p; + + iter_fd =bpf_iter_create (bpf_link__fd (link)); +if (!ASSERT_GE (iter_fd,0 ,"iter_fd" )) +return -1 ; + +memset (buf,0 ,sizeof (buf)); + left =ARRAY_SIZE (buf); + p = buf; +while ((len =read (iter_fd, p, left)) >0 ) { + p += len; + left -= len; + } + +close (iter_fd); +return 0 ; +} + +static void verify_iter_success (const char *prog_name,bool negative) +{ +DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts, opts); +struct test_bits_iter_success *skel; +union bpf_iter_link_info linfo; +int cgrp_fd, err, i, nr_cpus; +struct bpf_program *prog; +struct bpf_link *link; +cpu_set_t set; + +if (setup_cgroup_environment ()) +return ; + +/* Utilize the cgroup iter */ + cgrp_fd =get_root_cgroup (); +if (!ASSERT_GE (cgrp_fd,0 ,"create_cgrp" )) + goto cleanup; + + skel =test_bits_iter_success__open (); +if (!ASSERT_OK_PTR (skel,"cpumask_iter_success__open" )) + goto close_fd; + + skel->bss->pid =getpid (); + nr_cpus =libbpf_num_possible_cpus (); + skel->bss->total_nr_cpus = nr_cpus; + + err =test_bits_iter_success__load (skel); +if (!ASSERT_OK (err,"cpumask_iter_success__load" )) + goto destroy; + + prog =bpf_object__find_program_by_name (skel->obj, prog_name); +if (!ASSERT_OK_PTR (prog,"bpf_object__find_program_by_name" )) + goto destroy; + +memset (&linfo,0 ,sizeof (linfo)); + linfo.cgroup.cgroup_fd = cgrp_fd; + linfo.cgroup.order =BPF_CGROUP_ITER_SELF_ONLY ; + opts.link_info = &linfo; + opts.link_info_len =sizeof (linfo); + link =bpf_program__attach_iter (prog, &opts); +if (!ASSERT_OK_PTR (link,"bpf_program__attach" )) + goto destroy; + +if (negative) + goto negative; + +CPU_ZERO (&set); +/* To guarantee the success of "cpumask_copy" at all times */ +if (nr_cpus >16 ) + nr_cpus =16 ; +for (i =0 ; i < nr_cpus; i++) +CPU_SET (i, &set); + err =sched_setaffinity (skel->bss->pid,sizeof (set), &set); +if (!ASSERT_OK (err,"setaffinity_all_cpus" )) + goto free_link; + err =read_percpu_data (link); +if (!ASSERT_OK (err,"read_percpu_data" )) + goto free_link; + +negative: +ASSERT_OK (skel->bss->err,"task_running" ); + +free_link: +bpf_link__destroy (link); +destroy: +test_bits_iter_success__destroy (skel); +close_fd: +close (cgrp_fd); +cleanup: +cleanup_cgroup_environment (); +} + +void test_bits_iter (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (positive_testcases); i++) { +if (!test__start_subtest (positive_testcases[i])) +continue ; + +verify_iter_success (positive_testcases[i], false); + } + +for (i =0 ; i <ARRAY_SIZE (negative_testcases); i++) { +if (!test__start_subtest (negative_testcases[i])) +continue ; + +verify_iter_success (negative_testcases[i], true); + } + +RUN_TESTS (test_bits_iter_success); +RUN_TESTS (test_bits_iter_failure); +}
tools/testing/selftests/bpf/progs/test_bits_iter_failure.c new file +54 -0
54 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_bits_iter_success.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
2.39.1
Re: [PATCH bpf-next v4 2/2] selftests/bpf: Add selftest for bits iter
Quoted 15 lines · Yafang Shao
Add selftests for the newly added bits iter.
-bits_iter_success
- percpu data extracted from the percpu struct should be expected
- RCU lock is not required
- It is fine without callingbpf_iter_cpumask_next()
- It can work as expected when invalid arguments are passed-
bits_iter_failure
-bpf_iter_bits_destroy()is required after callingbpf_iter_bits_new()-
bpf_iter_bits_destroy()can only destroy an initialized iter
-bpf_iter_bits_next()must use an initialized iterSigned-off-by Yafang Shao <[email protected]>
Quoted 9 lines
--- .../selftests/bpf/prog_tests/bits_iter.c | 139 +++++++++++++++++ .../bpf/progs/test_bits_iter_failure.c | 54 +++++++ .../bpf/progs/test_bits_iter_success.c | 146 ++++++++++++++++++ 3 files changed, 339 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bits_iter.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_success.c
I think all the cgroup and bpf_iter related parts of tests seems like an unnecessary complications and just more code to support than necessary. We have tests for cgroup iterators, so we know it will work. You are adding bits iterator, so let's just add tests that validate bits iterator works. We don't need iterator programs and cgroups for that. Can we just leave RUN_TESTS-based tests and not add yet another test runner mini-setup (I mean those positive_testcases and negative_testcases)?
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +0
-0
tools/testing/selftests/bpf/prog_tests/bits_iter.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +9
-0@@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Yafang Shao <[email protected]> */ + +#define _GNU_SOURCE +#include <sched.h> + +#include <test_progs.h> +#include "test_bits_iter_success.skel.h" +#include "test_bits_iter_failure.skel.h"
I'd cut out all of this code I cut out from email.
[...]
tools/testing/selftests/bpf/prog_tests/bits_iter.c line 138
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +3
-0+ +RUN_TESTS (test_bits_iter_success); +RUN_TESTS (test_bits_iter_failure);
and I'd put these into prog_tests/verifier.c
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +1
-0+}tools/testing/selftests/bpf/progs/test_bits_iter_failure.c new file +9
-0@@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Yafang Shao <[email protected]> */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +#include "bpf_misc.h" +#include "task_kfunc_common.h"
tools/testing/selftests/bpf/progs/test_bits_iter_failure.c line 19
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_bits_iter_failure.c new file +10
-0+ +char _license[] SEC("license") = "GPL"; + +int bpf_iter_bits_new(struct bpf_iter_bits *it, const void *unsafe_ptr__ign, + u32 nr_bits) __ksym __weak; +int *bpf_iter_bits_next (struct bpf_iter_bits *it) __ksym__weak ; +void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __ksym __weak; + +SEC("iter.s/cgroup") +__failure __msg("Unreleased reference id=3 alloc_insn=10")
you don't control assembly instruction generation, so let's not rely on specific instruction number in error message
Quoted 35 lines
tools/testing/selftests/bpf/progs/test_bits_iter_failure.c new file +35
-0+int BPF_PROG (no_destroy ,struct bpf_iter_meta *meta,struct cgroup *cgrp) +{ +struct bpf_iter_bits it; +struct task_struct *p; + + p =bpf_task_from_pid (1 ); +if (!p) +return 1 ; + +bpf_iter_bits_new (&it, p->cpus_ptr,8192 ); + +bpf_iter_bits_next (&it); +bpf_task_release (p); +return 0 ; +} + +SEC("iter/cgroup") +__failure __msg("expected an initialized iter_bits as arg #1") +int BPF_PROG(next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ +struct bpf_iter_bits *it =NULL ; + +bpf_iter_bits_next (it); +return 0 ; +} + +SEC("iter/cgroup") +__failure __msg("expected an initialized iter_bits as arg #1") +int BPF_PROG(destroy_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ +struct bpf_iter_bits it = {}; + +bpf_iter_bits_destroy (&it); +return 0 ; +}
[...]
Re: [PATCH bpf-next v4 2/2] selftests/bpf: Add selftest for bits iter
Quoted 27 lines · Andrii Nakryiko
Quoted 15 lines · Yafang Shao
Add selftests for the newly added bits iter.
-bits_iter_success
- percpu data extracted from the percpu struct should be expected
- RCU lock is not required
- It is fine without callingbpf_iter_cpumask_next()
- It can work as expected when invalid arguments are passed-
bits_iter_failure
-bpf_iter_bits_destroy()is required after callingbpf_iter_bits_new()-
bpf_iter_bits_destroy()can only destroy an initialized iter
-bpf_iter_bits_next()must use an initialized iterSigned-off-by Yafang Shao <[email protected]>Quoted 9 lines
--- .../selftests/bpf/prog_tests/bits_iter.c | 139 +++++++++++++++++ .../bpf/progs/test_bits_iter_failure.c | 54 +++++++ .../bpf/progs/test_bits_iter_success.c | 146 ++++++++++++++++++ 3 files changed, 339 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bits_iter.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/test_bits_iter_success.c
Quoted 8 lines
I think all the cgroup and
bpf_iterrelated parts of tests seems like an unnecessary complications and just more code to support than necessary. We have tests for cgroup iterators, so we know it will work. You are adding bits iterator, so let's just add tests that validate bits iterator works. We don't need iterator programs and cgroups for that. Can we just leave RUN_TESTS-based tests and not add yet another test runner mini-setup (I mean thosepositive_testcasesandnegative_testcases)?
Thanks for your suggestion. I will consider your suggestion carefully.
Quoted 10 lines
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +3
-0@@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Yafang Shao <[email protected]> */ +
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c line 9
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +6
-0+#define _GNU_SOURCE +#include <sched.h> + +#include <test_progs.h> +#include "test_bits_iter_success.skel.h" +#include "test_bits_iter_failure.skel.h" I'd cut out all of this code I cut out from email.
will do it.
Quoted 8 lines
[...]
tools/testing/selftests/bpf/prog_tests/bits_iter.c line 138
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +3
-0+ +RUN_TESTS (test_bits_iter_success); +RUN_TESTS (test_bits_iter_failure);and I'd put these into
prog_tests/verifier.c
will do it.
Quoted 22 lines
Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/bits_iter.c new file +1
-0+}tools/testing/selftests/bpf/progs/test_bits_iter_failure.c new file +14
-0@@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Yafang Shao <[email protected]> */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +#include "bpf_misc.h" +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +int bpf_iter_bits_new(struct bpf_iter_bits *it, const void *unsafe_ptr__ign, + u32 nr_bits) __ksym __weak;
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_bits_iter_failure.c line 19
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_bits_iter_failure.c new file +5
-0+int *bpf_iter_bits_next (struct bpf_iter_bits *it) __ksym__weak ; +void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __ksym __weak; + +SEC("iter.s/cgroup") +__failure __msg("Unreleased reference id=3 alloc_insn=10")you don't control assembly instruction generation, so let's not rely on specific instruction number in error message
will do it.
Signature · 2 lines
Regards Yafang