Skip to content

Commit 83c0ed6

Browse files
committed
selftests/bpf: Fix map_ptr test cases for CPUMAP, HASHMAP
The map_ptr test fails on a 4 vCPU QEMU/armhf system with error E2BIG: root@qemu-armhf:/usr/libexec/kselftests-bpf# ./test_progs -a map_ptr test_map_ptr:PASS:skel_open 0 nsec test_map_ptr:FAIL:skel_load unexpected error: -7 (errno 7) torvalds#192 map_ptr:FAIL This results from trying to create a CPUMAP map with 'max_entries' set to 8, since the kernel limits this to the number of logical CPUs (i.e. 4). Fix by determining the number of CPUs and setting 'max_entries' from userspace, similar to the RINGBUF map setup. The last fix reveals another bug where the HASHMAP subtest hard-codes the expected htab element size assuming a 64-bit host, which fails on 32-bit armhf because 'struct htab_elem' depends on host word size. As above, fix this by determining the correct element size and writing the expected value from userspace. Since the 'struct htab_elem' declaration is not in uapi, create a local version that captures the same type/size and alignment variation with system word size. While not ideal, this is still better than hard-coding a single value. Signed-off-by: Tony Ambardar <[email protected]>
1 parent e5d39e4 commit 83c0ed6

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

tools/testing/selftests/bpf/prog_tests/map_ptr.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ void test_map_ptr(void)
1212
char buf[128];
1313
int err;
1414
int page_size = getpagesize();
15+
int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
16+
/*
17+
* Need to calc size of 'struct htab_elem' since it depends on
18+
* host word size, but it's neither in a header nor can we include
19+
* vmlinux.h here because that fails to compile. Instead, use a
20+
* simplified version of the struct for 32/64-bit sizing info.
21+
*/
22+
struct dummy_htab_elem {
23+
struct {
24+
void* a[4];
25+
int b;
26+
};
27+
int c;
28+
char d[] __attribute__((__aligned__(8)));
29+
};
1530
LIBBPF_OPTS(bpf_test_run_opts, topts,
1631
.data_in = &pkt_v4,
1732
.data_size_in = sizeof(pkt_v4),
@@ -25,12 +40,19 @@ void test_map_ptr(void)
2540
return;
2641

2742
skel->maps.m_ringbuf.max_entries = page_size;
43+
skel->maps.m_cpumap.max_entries = num_cpus;
2844

2945
err = map_ptr_kern_lskel__load(skel);
3046
if (!ASSERT_OK(err, "skel_load"))
3147
goto cleanup;
3248

3349
skel->bss->page_size = page_size;
50+
skel->bss->num_cpus = num_cpus;
51+
/*
52+
* Determine size of struct htab_elem + key + val, noting the
53+
* kernel rounds up our u32 key and val sizes by 8.
54+
*/
55+
skel->bss->htab_elem_size = sizeof(struct dummy_htab_elem) + 8 + 8;
3456

3557
err = bpf_prog_test_run_opts(skel->progs.cg_skb.prog_fd, &topts);
3658

tools/testing/selftests/bpf/progs/map_ptr_kern.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ _Static_assert(MAX_ENTRIES < LOOP_BOUND, "MAX_ENTRIES must be < LOOP_BOUND");
1212

1313
enum bpf_map_type g_map_type = BPF_MAP_TYPE_UNSPEC;
1414
__u32 g_line = 0;
15-
int page_size = 0; /* userspace should set it */
15+
16+
int page_size = 0; /* userspace should set these */
17+
int num_cpus = 0;
18+
int htab_elem_size = 0;
1619

1720
#define VERIFY_TYPE(type, func) ({ \
1821
g_map_type = type; \
@@ -114,7 +117,7 @@ static inline int check_hash(void)
114117
VERIFY(check_default_noinline(&hash->map, map));
115118

116119
VERIFY(hash->n_buckets == MAX_ENTRIES);
117-
VERIFY(hash->elem_size == 64);
120+
VERIFY(hash->elem_size == htab_elem_size);
118121

119122
VERIFY(hash->count.counter == 0);
120123
VERIFY(bpf_map_sum_elem_count(map) == 0);
@@ -463,7 +466,7 @@ static inline int check_cpumap(void)
463466
struct bpf_cpu_map *cpumap = (struct bpf_cpu_map *)&m_cpumap;
464467
struct bpf_map *map = (struct bpf_map *)&m_cpumap;
465468

466-
VERIFY(check_default(&cpumap->map, map));
469+
VERIFY(check(&cpumap->map, map, sizeof(__u32), sizeof(__u32), num_cpus));
467470

468471
return 1;
469472
}

0 commit comments

Comments
 (0)