Skip to content

Commit

Permalink
bpf/selftests: Check errno when percpu map value size exceeds
Browse files Browse the repository at this point in the history
This test case checks the errno message when percpu map value size
exceeds PCPU_MIN_UNIT_SIZE.

root@debian:~# ./test_progs -t map_init
 torvalds#160/1   map_init/pcpu_map_init:OK
 torvalds#160/2   map_init/pcpu_lru_map_init:OK
 torvalds#160/3   map_init/pcpu map value size:OK
 torvalds#160     map_init:OK
Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
Signed-off-by: jinke han <jinkehan@didiglobal.com>
  • Loading branch information
chentao-kernel authored and intel-lab-lkp committed Sep 9, 2024
1 parent 7f50b19 commit 301b498
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/map_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#define TEST_VALUE 0x1234
#define FILL_VALUE 0xdeadbeef
#define PCPU_MIN_UNIT_SIZE 32768

static int nr_cpus;
static int duration;
Expand Down Expand Up @@ -118,6 +119,35 @@ static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)

return 0;
}
/*
* percpu map value size is bound by PCPU_MIN_UNIT_SIZE
* check the errno when the value exceed PCPU_MIN_UNIT_SIZE
*/
static void test_pcpu_map_value_size(void)
{
struct test_map_init *skel;
int err;
int value_sz = PCPU_MIN_UNIT_SIZE + 1;
enum bpf_map_type map_types[] = { BPF_MAP_TYPE_PERCPU_ARRAY,
BPF_MAP_TYPE_PERCPU_HASH,
BPF_MAP_TYPE_LRU_PERCPU_HASH };
for (int i = 0; i < ARRAY_SIZE(map_types); i++) {
skel = test_map_init__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return;
err = bpf_map__set_type(skel->maps.hashmap2, map_types[i]);
if (!ASSERT_OK(err, "bpf_map__set_type"))
goto error;
err = bpf_map__set_value_size(skel->maps.hashmap2, value_sz);
if (!ASSERT_OK(err, "bpf_map__set_value_size"))
goto error;

err = test_map_init__load(skel);
ASSERT_EQ(err, -E2BIG, "skel_load");
error:
test_map_init__destroy(skel);
}
}

/* Add key=1 elem with values set for all CPUs
* Delete elem key=1
Expand Down Expand Up @@ -211,4 +241,6 @@ void test_map_init(void)
test_pcpu_map_init();
if (test__start_subtest("pcpu_lru_map_init"))
test_pcpu_lru_map_init();
if (test__start_subtest("pcpu map value size"))
test_pcpu_map_value_size();
}
6 changes: 6 additions & 0 deletions tools/testing/selftests/bpf/progs/test_map_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ struct {
__type(value, __u64);
} hashmap1 SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, __u64);
} hashmap2 SEC(".maps");

SEC("tp/syscalls/sys_enter_getpgid")
int sysenter_getpgid(const void *ctx)
Expand Down

0 comments on commit 301b498

Please sign in to comment.