Files
linux/tools/testing/selftests/bpf/prog_tests/percpu_alloc.c
Yonghong Song 6adf82a439 selftests/bpf: Add tests for array map with local percpu kptr
Add non-sleepable and sleepable tests with percpu kptr. For
non-sleepable test, four programs are executed in the order of:
  1. allocate percpu data.
  2. assign values to percpu data.
  3. retrieve percpu data.
  4. de-allocate percpu data.

The sleepable prog tried to exercise all above 4 steps in a
single prog. Also for sleepable prog, rcu_read_lock is needed
to protect direct percpu ptr access (from map value) and
following bpf_this_cpu_ptr() and bpf_per_cpu_ptr() helpers.

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20230827152811.2000125-1-yonghong.song@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-09-08 08:42:18 -07:00

79 lines
2.2 KiB
C

// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include "percpu_alloc_array.skel.h"
static void test_array(void)
{
struct percpu_alloc_array *skel;
int err, prog_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);
skel = percpu_alloc_array__open();
if (!ASSERT_OK_PTR(skel, "percpu_alloc_array__open"))
return;
bpf_program__set_autoload(skel->progs.test_array_map_1, true);
bpf_program__set_autoload(skel->progs.test_array_map_2, true);
bpf_program__set_autoload(skel->progs.test_array_map_3, true);
bpf_program__set_autoload(skel->progs.test_array_map_4, true);
skel->rodata->nr_cpus = libbpf_num_possible_cpus();
err = percpu_alloc_array__load(skel);
if (!ASSERT_OK(err, "percpu_alloc_array__load"))
goto out;
err = percpu_alloc_array__attach(skel);
if (!ASSERT_OK(err, "percpu_alloc_array__attach"))
goto out;
prog_fd = bpf_program__fd(skel->progs.test_array_map_1);
err = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(err, "test_run array_map 1-4");
ASSERT_EQ(topts.retval, 0, "test_run array_map 1-4");
ASSERT_EQ(skel->bss->cpu0_field_d, 2, "cpu0_field_d");
ASSERT_EQ(skel->bss->sum_field_c, 1, "sum_field_c");
out:
percpu_alloc_array__destroy(skel);
}
static void test_array_sleepable(void)
{
struct percpu_alloc_array *skel;
int err, prog_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);
skel = percpu_alloc_array__open();
if (!ASSERT_OK_PTR(skel, "percpu_alloc__open"))
return;
bpf_program__set_autoload(skel->progs.test_array_map_10, true);
skel->rodata->nr_cpus = libbpf_num_possible_cpus();
err = percpu_alloc_array__load(skel);
if (!ASSERT_OK(err, "percpu_alloc_array__load"))
goto out;
err = percpu_alloc_array__attach(skel);
if (!ASSERT_OK(err, "percpu_alloc_array__attach"))
goto out;
prog_fd = bpf_program__fd(skel->progs.test_array_map_10);
err = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(err, "test_run array_map_10");
ASSERT_EQ(topts.retval, 0, "test_run array_map_10");
ASSERT_EQ(skel->bss->cpu0_field_d, 2, "cpu0_field_d");
ASSERT_EQ(skel->bss->sum_field_c, 1, "sum_field_c");
out:
percpu_alloc_array__destroy(skel);
}
void test_percpu_alloc(void)
{
if (test__start_subtest("array"))
test_array();
if (test__start_subtest("array_sleepable"))
test_array_sleepable();
}