* [PATCH bpf-next v3] selftests/bpf: Avoid flaky resize value test for percpu data
@ 2026-08-26 14:56 Leon Hwang
0 siblings, 0 replies; only message in thread
From: Leon Hwang @ 2026-08-26 14:56 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Leon Hwang, linux-kselftest,
linux-kernel
The bpf_map__set_value_size() test for percpu data relies on the tail
'cpu_id' field in the ".percpu" map. The test would fail, when 'cpu_id' is
not the last field in the map.
To avoid the flaky test, factor out a subtest to use a dedicated
".percpu.arr" map that only has one field 'int arr[1]'. And, skip the
subtest when fail to load skel with errno E2BIG and the new value size is
larger than ALIGN(32KiB, PAGE_SIZE).
And, drop 'args' in test_percpu_data_on_cpus(), which isn't used in bpf
prog side.
And, enhance the test to cover 'set' and 'nums[6]'.
Fixes: 4c9241bd731a ("selftests/bpf: Add tests to verify global percpu data")
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
---
v2 -> v3:
* Drop 4KiB page size guard. (Andrii)
* Expect -E2BIG, and skip the test, when PAGE_SIZE is >= 64KiB. (Andrii)
* v2: https://lore.kernel.org/bpf/20260818153202.90714-1-leon.hwang@linux.dev/
v1 -> v2:
* Factor out a subtest to test bpf_map__set_value_size().
* Run the new subtest only on 4K page systems. (Sashiko)
* v1: https://lore.kernel.org/bpf/20260814173206.93082-5-leon.hwang@linux.dev/
---
.../bpf/prog_tests/global_data_init.c | 96 ++++++++++++++-----
.../bpf/progs/test_global_percpu_data.c | 10 +-
2 files changed, 80 insertions(+), 26 deletions(-)
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 5671c31085cd..7c539cbcf3a1 100644
--- a/tools/testing/selftests/bpf/prog_tests/global_data_init.c
+++ b/tools/testing/selftests/bpf/prog_tests/global_data_init.c
@@ -68,12 +68,9 @@ static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f
{
struct test_global_percpu_data__percpu *data = NULL;
int i, err, key = 0, num_online, run = 0;
- __u64 args[2] = {0x1234ULL, 0x5678ULL};
size_t data_sz;
bool *online;
LIBBPF_OPTS(bpf_test_run_opts, topts,
- .ctx_in = args,
- .ctx_size_in = sizeof(args),
.flags = BPF_F_TEST_RUN_ON_CPU,
);
@@ -81,7 +78,7 @@ static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f
if (!ASSERT_OK(err, "parse_cpu_mask_file"))
return;
- data_sz = map ? bpf_map__value_size(map) : sizeof(*data);
+ data_sz = sizeof(*data);
data = calloc(1, data_sz);
if (!ASSERT_OK_PTR(data, "calloc percpu data"))
goto out;
@@ -109,7 +106,7 @@ static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f
break;
ASSERT_EQ(*runp, ++run, "run");
- ASSERT_EQ(data->cpu_id[0], i, "cpu_id");
+ ASSERT_EQ(data->cpu_id, i, "cpu_id");
ASSERT_EQ(data->data, 1, "data");
ASSERT_TRUE(data->set, "set");
ASSERT_EQ(data->nums[6], 0xc0de, "nums[6]");
@@ -127,7 +124,6 @@ static void test_global_percpu_data_init(void)
{
struct test_global_percpu_data__percpu init_value = {};
struct test_global_percpu_data__percpu *init_data;
- const __u32 desired_sz = sysconf(_SC_PAGE_SIZE);
struct test_global_percpu_data *skel = NULL;
size_t init_data_sz;
struct bpf_map *map;
@@ -163,9 +159,11 @@ static void test_global_percpu_data_init(void)
if (!ASSERT_EQ(bpf_map__type(map), BPF_MAP_TYPE_PERCPU_ARRAY, "bpf_map__type"))
goto out;
+ init_value.set = 1;
init_value.data = 2;
init_value.nums[6] = -1;
init_value.struct_data.i = 2;
+ init_value.struct_data.set = 1;
init_value.struct_data.nums[6] = -1;
err = bpf_map__set_initial_value(map, &init_value, sizeof(init_value));
if (!ASSERT_OK(err, "bpf_map__set_initial_value"))
@@ -177,35 +175,24 @@ static void test_global_percpu_data_init(void)
ASSERT_EQ(init_data->data, init_value.data, "init_value data");
ASSERT_EQ(init_data->set, init_value.set, "init_value set");
+ ASSERT_EQ(init_data->nums[6], init_value.nums[6], "init_value nums[6]");
ASSERT_EQ(init_data->struct_data.i, init_value.struct_data.i, "init_value struct_data.i");
+ ASSERT_EQ(init_data->struct_data.set, init_value.struct_data.set,
+ "init_value struct_data.set");
ASSERT_EQ(init_data->struct_data.nums[6], init_value.struct_data.nums[6],
"init_value struct_data.nums[6]");
ASSERT_EQ(init_data_sz, sizeof(init_value), "init_value size");
ASSERT_EQ((void *) init_data, (void *) skel->percpu, "skel->percpu eq init_data");
ASSERT_EQ(skel->percpu->data, init_value.data, "skel->percpu->data");
ASSERT_EQ(skel->percpu->set, init_value.set, "skel->percpu->set");
+ ASSERT_EQ(skel->percpu->nums[6], init_value.nums[6], "skel->percpu->nums[6]");
ASSERT_EQ(skel->percpu->struct_data.i, init_value.struct_data.i,
"skel->percpu->struct_data.i");
+ ASSERT_EQ(skel->percpu->struct_data.set, init_value.struct_data.set,
+ "skel->percpu->struct_data.set");
ASSERT_EQ(skel->percpu->struct_data.nums[6], init_value.struct_data.nums[6],
"skel->percpu->struct_data.nums[6]");
- ASSERT_GT(desired_sz, sizeof(init_value), "desired_sz");
- err = bpf_map__set_value_size(map, desired_sz);
- if (!ASSERT_OK(err, "bpf_map__set_value_size"))
- goto out;
- if (!ASSERT_EQ(bpf_map__value_size(map), desired_sz, "percpu value size"))
- goto out;
- if (!ASSERT_NEQ(bpf_map__btf_value_type_id(map), 0, "percpu BTF value type"))
- goto out;
-
- init_data = bpf_map__initial_value(map, &init_data_sz);
- if (!ASSERT_OK_PTR(init_data, "resized bpf_map__initial_value"))
- goto out;
- if (!ASSERT_EQ(init_data_sz, desired_sz, "resized initial value size"))
- goto out;
- if (!ASSERT_EQ(init_data->data, init_value.data, "resized initial value data"))
- goto out;
-
err = test_global_percpu_data__load(skel);
if (!ASSERT_OK(err, "test_global_percpu_data__load"))
goto out;
@@ -236,6 +223,67 @@ static void test_global_percpu_data_lskel(void)
test_global_percpu_data_lskel__destroy(lskel);
}
+#define PCPU_MIN_UNIT_SIZE (32 << 10)
+
+static void test_global_percpu_data_map_resize(void)
+{
+ const __u32 page_size = sysconf(_SC_PAGE_SIZE);
+ const __u32 pcpu_max_value_size = ALIGN(PCPU_MIN_UNIT_SIZE, page_size);
+ const __u32 desired_sz = page_size * 2;
+ struct test_global_percpu_data *skel;
+ const int arr_value = -1;
+ struct bpf_map *map;
+ int err, prog_fd;
+ size_t data_sz;
+
+ skel = test_global_percpu_data__open();
+ if (!ASSERT_OK_PTR(skel, "test_global_percpu_data__open"))
+ return;
+
+ map = skel->maps.percpu_arr;
+ skel->percpu_arr->arr[0] = arr_value;
+ err = bpf_map__set_value_size(map, desired_sz);
+ if (!ASSERT_OK(err, "bpf_map__set_value_size"))
+ goto out;
+ if (!ASSERT_EQ(bpf_map__value_size(map), desired_sz, "bpf_map__value_size"))
+ goto out;
+ if (!ASSERT_NEQ(bpf_map__btf_value_type_id(map), 0, "bpf_map__btf_value_type_id"))
+ goto out;
+
+ skel->percpu_arr = bpf_map__initial_value(map, &data_sz);
+ if (!ASSERT_EQ(data_sz, desired_sz, "data_sz"))
+ goto out;
+ if (!ASSERT_OK_PTR(skel->percpu_arr, "skel->percpu_arr"))
+ goto out;
+ if (!ASSERT_EQ(skel->percpu_arr->arr[0], arr_value, "arr[0]"))
+ goto out;
+
+ err = test_global_percpu_data__load(skel);
+ if (bpf_map__value_size(map) > pcpu_max_value_size) {
+ if (ASSERT_EQ(err, -E2BIG, "test_global_percpu_data__load E2BIG"))
+ /*
+ * When the PAGE_SIZE is 64KiB, the new value size will
+ * be 128KiB. Creating a percpu_array map with the new
+ * value size will fail with -E2BIG due to percpu_array
+ * map value size limit 64KiB, which is calculated by
+ * PFN_ALIGN(32KiB).
+ * Let's skip the test for such case.
+ */
+ test__skip();
+ goto out;
+ }
+ if (!ASSERT_OK(err, "test_global_percpu_data__load"))
+ goto out;
+
+ map = skel->maps.percpu;
+ prog_fd = bpf_program__fd(skel->progs.update_percpu_data);
+ test_percpu_data_on_cpus(map, bpf_map__fd(map), prog_fd, &skel->bss->run);
+ ASSERT_EQ(skel->bss->arr_sum, arr_value * skel->bss->run, "arr_sum");
+
+out:
+ test_global_percpu_data__destroy(skel);
+}
+
static int create_rdonly_percpu_array(void)
{
LIBBPF_OPTS(bpf_map_create_opts, map_opts,
@@ -388,6 +436,8 @@ 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("map_resize"))
+ test_global_percpu_data_map_resize();
if (test__start_subtest("rdonly_direct_read"))
test_global_percpu_data_rdonly_direct_read();
if (test__start_subtest("rdonly_direct_write"))
diff --git a/tools/testing/selftests/bpf/progs/test_global_percpu_data.c b/tools/testing/selftests/bpf/progs/test_global_percpu_data.c
index 5dc21b3b4cb5..2765cd46e9af 100644
--- a/tools/testing/selftests/bpf/progs/test_global_percpu_data.c
+++ b/tools/testing/selftests/bpf/progs/test_global_percpu_data.c
@@ -8,9 +8,12 @@ int loong SEC(".percpu.looooooooong");
int data3 SEC(".data.percpu");
int data2 SEC(".percpu.data");
+/* Used for testing bpf_map__set_value_size(). */
+int arr[1] SEC(".percpu.arr");
+int arr_sum;
+
int run;
-/* cpu_id as array to verify map value resizing. */
-int cpu_id[1] SEC(".percpu");
+int cpu_id SEC(".percpu");
int data SEC(".percpu") = -1;
int nums[7] SEC(".percpu");
bool set SEC(".percpu") = false;
@@ -34,7 +37,8 @@ int update_percpu_data(void *ctx)
data = 1;
run++;
set = true;
- cpu_id[0] = bpf_get_smp_processor_id();
+ cpu_id = bpf_get_smp_processor_id();
+ arr_sum += arr[0];
return 0;
}
--
2.55.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-08-26 14:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 14:56 [PATCH bpf-next v3] selftests/bpf: Avoid flaky resize value test for percpu data Leon Hwang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.