Re: [PATCH bpf-next v7 09/11] selftests/bpf: Add test to verify accessing rdonly percpu_array

From: Andrii Nakryiko

Date: Tue Jun 23 2026 - 18:48:04 EST


On Mon, Jun 22, 2026 at 7:38 AM Leon Hwang <leon.hwang@xxxxxxxxx> wrote:
>
> Since percpu_array supports direct-read, it should not break accessing

what does "it" refer to here?

> rdonly percpu_array.
>
> Add a test to verify that adding '.map_direct_value_addr' to percpu_array
> won't break the case.

what case?

I think you are testing (read-only) direct access to read-only per-CPU
array, is that right? Just write that out? Though, really, negative
test (we *cannot* access read-only per-cpu map for write operation)
would be more trustworthy for this...


>
> Assisted-by: Codex:gpt-5.5-xhigh
> Signed-off-by: Leon Hwang <leon.hwang@xxxxxxxxx>
> ---
> .../bpf/prog_tests/global_data_init.c | 44 +++++++++++++++++++
> 1 file changed, 44 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/global_data_init.c b/tools/testing/selftests/bpf/prog_tests/global_data_init.c
> index ea7e4e3d91cf..f59d83919058 100644
> --- a/tools/testing/selftests/bpf/prog_tests/global_data_init.c
> +++ b/tools/testing/selftests/bpf/prog_tests/global_data_init.c
> @@ -232,6 +232,48 @@ static void test_global_percpu_data_lskel(void)
> free(online);
> }
>
> +static void test_global_percpu_data_rdonly_direct_read(void)
> +{
> + LIBBPF_OPTS(bpf_map_create_opts, map_opts,
> + .map_flags = BPF_F_RDONLY_PROG,
> + );
> + struct bpf_insn insns[] = {
> + BPF_ST_MEM(BPF_W, BPF_REG_10, -8, 0),
> + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
> + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
> + BPF_LD_MAP_FD(BPF_REG_1, 0),
> + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
> + BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
> + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0),
> + BPF_EXIT_INSN(),
> + };
> + int key = 0, map_fd, prog_fd = -1, err;
> + __u64 value = 0;
> +
> + map_fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, "percpu_ro_map", sizeof(int),
> + sizeof(__u64), 1, &map_opts);
> + if (!ASSERT_GE(map_fd, 0, "bpf_map_create"))
> + return;
> +
> + err = bpf_map_update_elem(map_fd, &key, &value, BPF_F_ALL_CPUS);
> + if (!ASSERT_OK(err, "bpf_map_update_elem"))
> + goto out;
> +
> + err = bpf_map_freeze(map_fd);
> + if (!ASSERT_OK(err, "bpf_map_freeze"))
> + goto out;
> +
> + insns[3].imm = map_fd;
> + prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "percpu_ro_prog", "GPL", insns,
> + ARRAY_SIZE(insns), NULL);
> + ASSERT_GE(prog_fd, 0, "bpf_prog_load");
> +
> +out:
> + if (prog_fd >= 0)
> + close(prog_fd);
> + close(map_fd);
> +}
> +
> void test_global_percpu_data(void)
> {
> if (!feat_supported(NULL, FEAT_PERCPU_DATA)) {
> @@ -243,4 +285,6 @@ void test_global_percpu_data(void)
> test_global_percpu_data_init();
> if (test__start_subtest("lskel"))
> test_global_percpu_data_lskel();
> + if (test__start_subtest("rdonly_direct_read"))
> + test_global_percpu_data_rdonly_direct_read();
> }
> --
> 2.54.0
>