Skip to contentSkip to conversation

bpf: Followups for global percpu data series

PATCH In review Leon Hwang 4 participants 12 messages Aug 14 – Aug 17

Subsystems (6)
Kselftest BPF BPF selftests BPF core +2 more subsystems
lore
Share & tools
lore

Leon Hwang

author
#0 lore raw

cover bpf: Followups for global percpu data series

Address some LLM reviews in
"[PATCH bpf-next v12 00/10] bpf: Introduce global percpu data" [1].

[1] https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (5):
bpf: Drop duplicate blank line in verifier
libbpf: Avoid unnecessary mmap resize for percpu data maps
bpftool: Update comments about skel data maps selftests/bpf: Avoid flaky resize value test for percpu data selftests/bpf: Improve readability in iter test for percpu data

 kernel/bpf/verifier.c                         |  1 -
 tools/bpf/bpftool/gen.c                       |  4 +-
 tools/lib/bpf/libbpf.c                        | 17 ++++----
 .../bpf/prog_tests/global_data_init.c         | 43 ++++++++++---------
 .../bpf/progs/test_global_percpu_data.c       | 14 +++---
 5 files changed, 41 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/5 bpf: Drop duplicate blank line in verifier

Drop a duplicate blank line in verifier for cleanup.

No functional change intended.

---
 kernel/bpf/verifier.c | 1 -
 1 file changed, 1 deletion(-)
kernel/bpf/verifier.c +0 -1
index 6ac1afced20b..81eae8f70acd 100644
@@ -12113,7 +12113,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me
 			ref_tname = btf_name_by_offset(btf, ref_t->name_off);
 		}
 
-
 		if (bpf_register_is_null(reg) && type_may_be_null(kf_arg_type))
 			continue;
 
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH bpf-next 1/5] bpf: Drop duplicate blank line in verifier

Quoted 15 lines · Leon Hwang

Drop a duplicate blank line in verifier for cleanup.

No functional change intended.

---
 kernel/bpf/verifier.c | 1 -
 1 file changed, 1 deletion(-)
kernel/bpf/verifier.c +0 -0
index 6ac1afced20b..81eae8f70acd 100644

kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12116

Quoted 4 lines
kernel/bpf/verifier.c +0 -1
@@ -12113,7 +12113,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me
                        ref_tname = btf_name_by_offset(btf, ref_t->name_off);
                }
 
-

seriously... just because AI tells you to do something, doesn't mean you have to do it. I'm not applying a patch that's removing a single empty line.

Quoted 6 lines
kernel/bpf/verifier.c +0 -1
                if (bpf_register_is_null(reg) && type_may_be_null(kf_arg_type))
                        continue;
 
--

2.55.0

author
#3 lore raw

PATCH 2/5 libbpf: Avoid unnecessary mmap resize for percpu data maps

In reply to Leon Hwang

Use array_map_mmap_sz() for PERCPU_ARRAY like ARRAY in bpf_map_mmap_sz(). This lets bpf_map__set_value_size() skip mmap(), memcpy(), and munmap() when the old and new value sizes occupy the same number of pages.

Fix some typos btw:

* mmapble -> mmapable
* satisified -> satisfied
* relocatin -> relocation
* atach_btf_obj_fd -> attach_btf_obj_fd
* len_secnd -> len_second
* precendence -> precedence

---
 tools/lib/bpf/libbpf.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +8 -9
index e574870fb716..b749c01742ee 100644
@@ -1841,9 +1841,8 @@ static size_t bpf_map_mmap_sz(const struct bpf_map *map)
 
 	switch (map->def.type) {
 	case BPF_MAP_TYPE_ARRAY:
-		return array_map_mmap_sz(map->def.value_size, map->def.max_entries);
 	case BPF_MAP_TYPE_PERCPU_ARRAY:
-		return map->def.value_size;
+		return array_map_mmap_sz(map->def.value_size, map->def.max_entries);
 	case BPF_MAP_TYPE_ARENA:
 		return page_sz * map->def.max_entries;
 	default:
@@ -1951,7 +1950,7 @@ static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map)
 		return false;
 
 	/*
-	 * The internal PERCPU maps are not mmapble because the underlying
+	 * The internal PERCPU maps are not mmapable because the underlying
 	 * percpu_array maps do not have mmap support.
 	 */
 	if (map->libbpf_type == LIBBPF_MAP_PERCPU)
@@ -2831,7 +2830,7 @@ static size_t adjust_ringbuf_sz(size_t sz)
 		return 0;
 	/* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be
 	 * a power-of-2 multiple of kernel's page size. If user diligently
-	 * satisified these conditions, pass the size through.
+	 * satisfied these conditions, pass the size through.
 	 */
 	if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz))
 		return sz;
@@ -6989,7 +6988,7 @@ bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
  *    +-----------+------+------+
  *
  * At this point, we relocate subA calls, then go one level up and finish with
- * relocatin mainA calls. mainA is done.
+ * relocation mainA calls. mainA is done.
  *
  * For mainB process is similar but results in different order. We start with
  * mainB and skip subA and subB, as mainB never calls them (at least
@@ -7936,7 +7935,7 @@ static int libbpf_prepare_prog_load(struct bpf_program *prog,
 		prog->attach_btf_id = btf_type_id;
 
 		/* but by now libbpf common logic is not utilizing
-		 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
+		 * prog->attach_btf_obj_fd/prog->attach_btf_id anymore because
 		 * this callback is called after opts were populated by
 		 * libbpf, so this callback has to update opts explicitly here
 		 */
@@ -14219,7 +14218,7 @@ perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
 		if (((void *)ehdr) + ehdr_size > base + mmap_size) {
 			void *copy_start = ehdr;
 			size_t len_first = base + mmap_size - copy_start;
-			size_t len_secnd = ehdr_size - len_first;
+			size_t len_second = ehdr_size - len_first;
 
 			if (*copy_size < ehdr_size) {
 				free(*copy_mem);
@@ -14233,7 +14232,7 @@ perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
 			}
 
 			memcpy(*copy_mem, copy_start, len_first);
-			memcpy(*copy_mem + len_first, base, len_secnd);
+			memcpy(*copy_mem + len_first, base, len_second);
 			ehdr = *copy_mem;
 		}
 
@@ -14251,7 +14250,7 @@ struct perf_buffer;
 
 struct perf_buffer_params {
 	struct perf_event_attr *attr;
-	/* if event_cb is specified, it takes precendence */
+	/* if event_cb is specified, it takes precedence */
 	perf_buffer_event_fn event_cb;
 	/* sample_cb and lost_cb are higher-level common-case callbacks */
 	perf_buffer_sample_fn sample_cb;
Signature · 1 line
2.55.0
author
#4 lore raw

PATCH 3/5 bpftool: Update comments about skel data maps

In reply to Leon Hwang

Since is_skel_data() includes the non-memory-mapped percpu_array map, the "memory-mapped maps" in comments are incorrect for percpu data.

Update "memory-mapped internal maps" to "skel data maps" for better explanation.

---
 tools/bpf/bpftool/gen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +2 -2
index a50540ef6521..e5e82255af24 100644
@@ -303,7 +303,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
 		return -errno;
 
 	bpf_object__for_each_map(map, obj) {
-		/* only generate definitions for memory-mapped internal maps */
+		/* only generate definitions for skel data maps */
 		if (!is_skel_data(map, map_ident, sizeof(map_ident)))
 			continue;
 
@@ -510,7 +510,7 @@ static size_t bpf_map_mmap_sz(const struct bpf_map *map)
 	return map_sz;
 }
 
-/* Emit type size asserts for all top-level fields in memory-mapped internal maps. */
+/* Emit type size asserts for all top-level fields in skel data maps. */
 static void codegen_asserts(struct bpf_object *obj, const char *obj_name)
 {
 	struct btf *btf = bpf_object__btf(obj);
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH bpf-next 3/5] bpftool: Update comments about skel data maps

Quoted 6 lines · Leon Hwang

Since is_skel_data() includes the non-memory-mapped percpu_array map, the "memory-mapped maps" in comments are incorrect for percpu data.

Update "memory-mapped internal maps" to "skel data maps" for better explanation.

What is a "skel data map"? "memory-mapped internal map" is fine as is, leave it be

Quoted 31 lines
---
 tools/bpf/bpftool/gen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +2 -3
index a50540ef6521..e5e82255af24 100644
@@ -303,7 +303,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
                return -errno;
 
        bpf_object__for_each_map(map, obj) {
-               /* only generate definitions for memory-mapped internal maps */
+               /* only generate definitions for skel data maps */
                if (!is_skel_data(map, map_ident, sizeof(map_ident)))
                        continue;
 
@@ -510,7 +510,7 @@ static size_t bpf_map_mmap_sz(const struct bpf_map *map)
        return map_sz;
 }
 
-/* Emit type size asserts for all top-level fields in memory-mapped internal maps. */
+/* Emit type size asserts for all top-level fields in skel data maps. */
 static void codegen_asserts(struct bpf_object *obj, const char *obj_name)
 {
        struct btf *btf = bpf_object__btf(obj);
--

2.55.0

author
#6 lore raw

PATCH 4/5 selftests/bpf: Avoid flaky resize value test for percpu data

In reply to Leon Hwang

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, use a dedicated ".percpu.arr" map that only has one field 'int arr[1]' to ensure that bpf_map__set_value_size() won't fail.

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]' to distinguish a broken copy.

Fixes 4c9241bd731a ("selftests/bpf: Add tests to verify global percpu data")
---
 .../bpf/prog_tests/global_data_init.c         | 37 ++++++++++---------
 .../bpf/progs/test_global_percpu_data.c       |  8 ++--
 2 files changed, 25 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/prog_tests/global_data_init.c +20 -17
index 7d6bda909295..2f0dbeb88934 100644
@@ -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]");
@@ -126,11 +123,11 @@ static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f
 static void test_global_percpu_data_init(void)
 {
 	struct test_global_percpu_data__percpu init_value = {};
+	const __u32 desired_sz = sysconf(_SC_PAGE_SIZE) * 2;
 	struct test_global_percpu_data__percpu *init_data;
-	const __u32 desired_sz = sysconf(_SC_PAGE_SIZE);
 	struct test_global_percpu_data *skel = NULL;
+	struct bpf_map *map, *map_percpu_arr;
 	size_t init_data_sz;
-	struct bpf_map *map;
 	int prog_fd, err;
 
 	skel = test_global_percpu_data__open();
@@ -163,9 +160,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,33 +176,37 @@ 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);
+	skel->percpu_arr->arr[0] = -1;
+	map_percpu_arr = skel->maps.percpu_arr;
+	err = bpf_map__set_value_size(map_percpu_arr, 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"))
+	if (!ASSERT_EQ(bpf_map__value_size(map_percpu_arr), desired_sz, "percpu value size"))
 		goto out;
-
-	init_data = bpf_map__initial_value(map, &init_data_sz);
-	if (!ASSERT_OK_PTR(init_data, "resized bpf_map__initial_value"))
+	if (!ASSERT_NEQ(bpf_map__btf_value_type_id(map_percpu_arr), 0, "percpu BTF value type"))
 		goto out;
-	if (!ASSERT_EQ(init_data_sz, desired_sz, "resized initial value size"))
+	skel->percpu_arr = bpf_map__initial_value(map_percpu_arr, &init_data_sz);
+	if (!ASSERT_OK_PTR(skel->percpu_arr, "resized percpu_arr value"))
 		goto out;
-	if (!ASSERT_EQ(init_data->data, init_value.data, "resized initial value data"))
+	if (!ASSERT_EQ(skel->percpu_arr->arr[0], -1, "arr[0]"))
 		goto out;
 
 	err = test_global_percpu_data__load(skel);
tools/testing/selftests/bpf/progs/test_global_percpu_data.c +5 -3
index 71ff8d1bf49e..12556590b647 100644
@@ -8,9 +8,11 @@ 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 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 +36,7 @@ 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();
 	return 0;
 }
 
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH bpf-next 4/5] selftests/bpf: Avoid flaky resize value test for percpu data

Does multiplying by 2 here break the test on 64K page systems?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +0 -0
index 7d6bda909295..2f0dbeb88934 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/global_data_init.c static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f · line 130

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +1 -1
@@ -126,11 +123,11 @@ static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f
 static void test_global_percpu_data_init(void)
 {
 	struct test_global_percpu_data__percpu init_value = {};
+	const __u32 desired_sz = sysconf(_SC_PAGE_SIZE) * 2;
 	struct test_global_percpu_data__percpu *init_data;
-	const __u32 desired_sz = sysconf(_SC_PAGE_SIZE);

Does multiplying by 2 here break the test on 64K page systems?

Per-CPU array value sizes are capped by the kernel:

/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)

          return -E2BIG;

in kernel/bpf/arraymap.c:array_map_alloc_check(), where PCPU_MIN_UNIT_SIZE is defined as:

  #define PCPU_MIN_UNIT_SIZE  PFN_ALIGN(32 << 10)

which equals max(32768, PAGE_SIZE).

The old code was safe by construction: desired_sz == PAGE_SIZE <= PFN_ALIGN(32768) == PCPU_MIN_UNIT_SIZE. Doubling it breaks that:

  PAGE_SIZE  4096 -> limit 32768, request   8192  OK
  PAGE_SIZE 16384 -> limit 32768, request  32768  OK (exactly at limit)
  PAGE_SIZE 65536 -> limit 65536, request 131072  -E2BIG

On kernels built with 64K pages (arm64 CONFIG_ARM64_64K_PAGES, ppc64) bpf_map__set_value_size() still succeeds (it is purely userspace bookkeeping), all four assertions that follow it pass, and then map creation fails inside test_global_percpu_data__load():

tools/testing/selftests/bpf/prog_tests/global_data_init.c static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f · line 130

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +0 -0
 	err = test_global_percpu_data__load(skel);
 	if (!ASSERT_OK(err, "test_global_percpu_data__load"))
 		goto out;

The "global_percpu_data/init" subtest fails, and test_percpu_data_on_cpus() is never reached. That is a new failure introduced by this patch (with desired_sz == PAGE_SIZE the same configuration created a 65536-byte per-CPU value, which is exactly at the limit and accepted).

Note that the neighbouring resize test in global_map_resize.c deliberately does not scale a per-CPU map by page size for this reason. At line 46 it uses:

  new_sz = sizeof(skel->data_percpu_arr->percpu_arr[0]) *
           libbpf_num_possible_cpus();

A size that is independent of PAGE_SIZE would still force the mmap region to move, which is what the new skel->percpu_arr reassignment and the arr[0] == -1 check are there to exercise, without hitting the per-CPU allocator bound.

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +1 -1
 	struct test_global_percpu_data *skel = NULL;
+	struct bpf_map *map, *map_percpu_arr;
 	size_t init_data_sz;
-	struct bpf_map *map;

int prog_fd, err;

[ ... ]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +1 -0
@@ -177,33 +176,37 @@ 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]");

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +3 -2
-	ASSERT_GT(desired_sz, sizeof(init_value), "desired_sz");
-	err = bpf_map__set_value_size(map, desired_sz);
+	skel->percpu_arr->arr[0] = -1;
+	map_percpu_arr = skel->maps.percpu_arr;
+	err = bpf_map__set_value_size(map_percpu_arr, desired_sz);
 	if (!ASSERT_OK(err, "bpf_map__set_value_size"))
 		goto out;

[ ... ]

Quoted 8 lines
tools/testing/selftests/bpf/progs/test_global_percpu_data.c +0 -0
index 71ff8d1bf49e..12556590b647 100644
@@ -8,9 +8,11 @@ int loong SEC(".percpu.looooooooong");
 int data3 SEC(".data.percpu");
 int data2 SEC(".percpu.data");
 

tools/testing/selftests/bpf/progs/test_global_percpu_data.c int loong SEC(".percpu.looooooooong"); · line 18

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_global_percpu_data.c +4 -2
+/* Used for testing bpf_map__set_value_size(). */
+int arr[1] SEC(".percpu.arr");
+
 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;

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31826359148

author
#8 lore raw

Re: [PATCH bpf-next 4/5] selftests/bpf: Avoid flaky resize value test for percpu data

Quoted 55 lines · On 15/8/26 02:24, [email protected] wrote:
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +0 -0
index 7d6bda909295..2f0dbeb88934 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/global_data_init.c static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f · line 130

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +1 -1
@@ -126,11 +123,11 @@ static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f
 static void test_global_percpu_data_init(void)
 {
 	struct test_global_percpu_data__percpu init_value = {};
+	const __u32 desired_sz = sysconf(_SC_PAGE_SIZE) * 2;
 	struct test_global_percpu_data__percpu *init_data;
-	const __u32 desired_sz = sysconf(_SC_PAGE_SIZE);

Does multiplying by 2 here break the test on 64K page systems?

Per-CPU array value sizes are capped by the kernel:

/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)

          return -E2BIG;

in kernel/bpf/arraymap.c:array_map_alloc_check(), where PCPU_MIN_UNIT_SIZE is defined as:

  #define PCPU_MIN_UNIT_SIZE  PFN_ALIGN(32 << 10)

which equals max(32768, PAGE_SIZE).

The old code was safe by construction: desired_sz == PAGE_SIZE <= PFN_ALIGN(32768) == PCPU_MIN_UNIT_SIZE. Doubling it breaks that:

  PAGE_SIZE  4096 -> limit 32768, request   8192  OK
  PAGE_SIZE 16384 -> limit 32768, request  32768  OK (exactly at limit)
  PAGE_SIZE 65536 -> limit 65536, request 131072  -E2BIG

On kernels built with 64K pages (arm64 CONFIG_ARM64_64K_PAGES, ppc64) bpf_map__set_value_size() still succeeds (it is purely userspace bookkeeping), all four assertions that follow it pass, and then map creation fails inside test_global_percpu_data__load():

tools/testing/selftests/bpf/prog_tests/global_data_init.c static void test_percpu_data_on_cpus(struct bpf_map *map, int map_fd, int prog_f · line 130

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/global_data_init.c +0 -0
 	err = test_global_percpu_data__load(skel);
 	if (!ASSERT_OK(err, "test_global_percpu_data__load"))
 		goto out;

The "global_percpu_data/init" subtest fails, and test_percpu_data_on_cpus() is never reached. That is a new failure introduced by this patch (with desired_sz == PAGE_SIZE the same configuration created a 65536-byte per-CPU value, which is exactly at the limit and accepted).

Note that the neighbouring resize test in global_map_resize.c deliberately does not scale a per-CPU map by page size for this reason. At line 46 it uses:

Quoted 8 lines
  new_sz = sizeof(skel->data_percpu_arr->percpu_arr[0]) *
           libbpf_num_possible_cpus();

A size that is independent of PAGE_SIZE would still force the mmap region to move, which is what the new skel->percpu_arr reassignment and the arr[0] == -1 check are there to exercise, without hitting the per-CPU allocator bound.

To avoid breaking the test on 64K page systems, run the resize subtest only on 4K page systems.

From maintenance view, I think it's better to add a dedicated subtest to test bpf_map__set_value_size() for percpu data. Then, it will be able to skip the subtest when sysconf(_SC_PAGE_SIZE) != 4K.

Thanks,
Leon

author
#9 lore raw

PATCH 5/5 selftests/bpf: Improve readability in iter test for percpu data

In reply to Leon Hwang

The original 'offsetof()' + offset is equal to the new 'offsetof()'. Use the new 'offsetof()' instead.

Rename two variables btw:

* offsetof_num -> num_off
* percpu_data_sum -> sum

---
 tools/testing/selftests/bpf/prog_tests/global_data_init.c   | 6 +++---
 tools/testing/selftests/bpf/progs/test_global_percpu_data.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/global_data_init.c +3 -3
index 2f0dbeb88934..c6d055b9457d 100644
@@ -348,8 +348,8 @@ static void test_global_percpu_data_iter(void)
 		return;
 
 	skel->rodata->num_cpus = num_cpus;
-	skel->rodata->offsetof_num = offsetof(struct test_global_percpu_data__percpu, struct_data);
-	skel->rodata->offsetof_num += sizeof(skel->percpu->struct_data) - sizeof(int);
+	skel->rodata->num_off = offsetof(struct test_global_percpu_data__percpu,
+					 struct_data.nums[6]);
 	skel->rodata->elem_sz = roundup(sizeof(struct test_global_percpu_data__percpu), 8);
 	skel->percpu->struct_data.nums[6] = 0xc0de;
 
@@ -372,7 +372,7 @@ static void test_global_percpu_data_iter(void)
 		do { } while (0);
 	ASSERT_EQ(len, 0, "read iter");
 	ASSERT_TRUE(skel->bss->run_iter, "run_iter");
-	ASSERT_EQ(skel->bss->percpu_data_sum, 0xc0de * num_cpus, "percpu_data_sum");
+	ASSERT_EQ(skel->bss->sum, 0xc0de * num_cpus, "sum");
 
 	close(fd);
 out:
tools/testing/selftests/bpf/progs/test_global_percpu_data.c +3 -3
index 12556590b647..175c44000753 100644
@@ -64,9 +64,9 @@ int verifier_snprintf(void *ctx)
 }
 
 volatile const __u32 num_cpus = 0;
-volatile const int offsetof_num;
+volatile const int num_off;
 volatile const int elem_sz;
-__u32 percpu_data_sum = 0;
+__u32 sum = 0;
 bool run_iter = false;
 
 SEC("iter/bpf_map_elem")
@@ -82,7 +82,7 @@ int dump_percpu_data(struct bpf_iter__bpf_map_elem *ctx)
 	run_iter = true;
 
 	for (i = 0; i < num_cpus; i++) {
-		percpu_data_sum += *(int *) (pptr + offsetof_num);
+		sum += *(int *) (pptr + num_off);
 		pptr += elem_sz;
 	}
 	return 0;
Signature · 1 line
2.55.0
#10 lore raw
In reply to Leon Hwang
Quoted 4 lines · Leon Hwang

Address some LLM reviews in
"[PATCH bpf-next v12 00/10] bpf: Introduce global percpu data" [1].

Quoted 9 lines

[1] https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (5):
bpf: Drop duplicate blank line in verifier
libbpf: Avoid unnecessary mmap resize for percpu data maps
bpftool: Update comments about skel data maps selftests/bpf: Avoid flaky resize value test for percpu data selftests/bpf: Improve readability in iter test for percpu data

applied patches 2 and 5, check AI feedback for 4, don't bother with 1 and 3.

Quoted 10 lines
 kernel/bpf/verifier.c                         |  1 -
 tools/bpf/bpftool/gen.c                       |  4 +-
 tools/lib/bpf/libbpf.c                        | 17 ++++----
 .../bpf/prog_tests/global_data_init.c         | 43 ++++++++++---------
 .../bpf/progs/test_global_percpu_data.c       | 14 +++---
 5 files changed, 41 insertions(+), 38 deletions(-)

--
2.55.0

#11 lore raw
In reply to Leon Hwang
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 5 lines · On Sat, 15 Aug 2026 01:32:01 +0800 you wrote:

Address some LLM reviews in
"[PATCH bpf-next v12 00/10] bpf: Introduce global percpu data" [1].

[1] https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

Leon Hwang (5):
bpf: Drop duplicate blank line in verifier
libbpf: Avoid unnecessary mmap resize for percpu data maps
bpftool: Update comments about skel data maps selftests/bpf: Avoid flaky resize value test for percpu data selftests/bpf: Improve readability in iter test for percpu data

[...]

Here is the summary with links:
- [bpf-next,1/5] bpf: Drop duplicate blank line in verifier
(no matching commit)
- [bpf-next,2/5] libbpf: Avoid unnecessary mmap resize for percpu data maps

    https://git.kernel.org/bpf/bpf-next/c/3808171428f5
  - [bpf-next,3/5] bpftool: Update comments about skel data maps
    (no matching commit)
  - [bpf-next,4/5] selftests/bpf: Avoid flaky resize value test for percpu data
    (no matching commit)
  - [bpf-next,5/5] selftests/bpf: Improve readability in iter test for percpu data
    https://git.kernel.org/bpf/bpf-next/c/90bd0329abd4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html