On Fri, Oct 17, 2025 at 9:04 PM Xu Kuohai <[email protected]> wrote:
>
> From: Xu Kuohai <[email protected]>
>
> Add overwrite mode test for BPF ring buffer. The test creates a BPF ring
> buffer in overwrite mode, then repeatedly reserves and commits records
> to check if the ring buffer works as expected both before and after
> overwriting occurs.
>
> Signed-off-by: Xu Kuohai <[email protected]>
> ---
>  tools/testing/selftests/bpf/Makefile          |  3 +-
>  .../selftests/bpf/prog_tests/ringbuf.c        | 64 ++++++++++++
>  .../bpf/progs/test_ringbuf_overwrite.c        | 98 +++++++++++++++++++
>  3 files changed, 164 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_overwrite.c
>
> diff --git a/tools/testing/selftests/bpf/Makefile 
> b/tools/testing/selftests/bpf/Makefile
> index f00587d4ede6..43d133bf514d 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -498,7 +498,8 @@ LINKED_SKELS := test_static_linked.skel.h 
> linked_funcs.skel.h               \
>
>  LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c  \
>         core_kern.c core_kern_overflow.c test_ringbuf.c                 \
> -       test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c
> +       test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c    \
> +       test_ringbuf_overwrite.c
>
>  LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c 
> b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
> index d1e4cb28a72c..5264af1dc768 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
> @@ -17,6 +17,7 @@
>  #include "test_ringbuf_n.lskel.h"
>  #include "test_ringbuf_map_key.lskel.h"
>  #include "test_ringbuf_write.lskel.h"
> +#include "test_ringbuf_overwrite.lskel.h"
>
>  #define EDONE 7777
>
> @@ -497,6 +498,67 @@ static void ringbuf_map_key_subtest(void)
>         test_ringbuf_map_key_lskel__destroy(skel_map_key);
>  }
>
> +static void ringbuf_overwrite_mode_subtest(void)
> +{
> +       unsigned long size, len1, len2, len3, len4, len5;
> +       unsigned long expect_avail_data, expect_prod_pos, expect_over_pos;
> +       struct test_ringbuf_overwrite_lskel *skel;
> +       int err;
> +
> +       skel = test_ringbuf_overwrite_lskel__open();
> +       if (!ASSERT_OK_PTR(skel, "skel_open"))
> +               return;
> +
> +       size = 0x1000;

this will fail on architecture with page size != 4KB, I adjusted this
to use page_size, len1 to page_size / 2 and len2 to page_size / 4

> +       len1 = 0x800;
> +       len2 = 0x400;
> +       len3 = size - len1 - len2 - BPF_RINGBUF_HDR_SZ * 3; /* 0x3e8 */
> +       len4 = len3 - 8; /* 0x3e0 */
> +       len5 = len3; /* retry with len3 */
> +

[...]

Reply via email to