Commit 2bea33f9 authored by Andrii Nakryiko's avatar Andrii Nakryiko
Browse files

Merge branch 'bpf-add-percpu-map-value-size-check'

Tao Chen says:

====================
bpf: Add percpu map value size check

Check percpu map value size first and add the test case in selftest.

Change list:
- v2 -> v3:
    - use bpf_map_create API and mv test case in map_percpu_stats.c
- v1 -> v2:
    - round up map value size with 8 bytes in patch 1
    - add selftest case in patch 2
====================

Link: https://lore.kernel.org/r/20240910144111.1464912-1-chen.dylane@gmail.com


Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 300a90b2 7eab3a58
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)
	/* avoid overflow on round_up(map->value_size) */
	if (attr->value_size > INT_MAX)
		return -E2BIG;
	/* 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;

	return 0;
}
+3 −0
Original line number Diff line number Diff line
@@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
		 * kmalloc-able later in htab_map_update_elem()
		 */
		return -E2BIG;
	/* 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;

	return 0;
}
+18 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#define MAX_ENTRIES_HASH_OF_MAPS	64
#define N_THREADS			8
#define MAX_MAP_KEY_SIZE		4
#define PCPU_MIN_UNIT_SIZE		32768

static void map_info(int map_fd, struct bpf_map_info *info)
{
@@ -456,6 +457,22 @@ static void map_percpu_stats_hash_of_maps(void)
	printf("test_%s:PASS\n", __func__);
}

static void map_percpu_stats_map_value_size(void)
{
	int fd;
	int value_sz = PCPU_MIN_UNIT_SIZE + 1;
	struct bpf_map_create_opts opts = { .sz = sizeof(opts) };
	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++) {
		fd = bpf_map_create(map_types[i], NULL, sizeof(__u32), value_sz, 1, &opts);
		CHECK(fd < 0 && errno != E2BIG, "percpu map value size",
			"error: %s\n", strerror(errno));
	}
	printf("test_%s:PASS\n", __func__);
}

void test_map_percpu_stats(void)
{
	map_percpu_stats_hash();
@@ -467,4 +484,5 @@ void test_map_percpu_stats(void)
	map_percpu_stats_percpu_lru_hash();
	map_percpu_stats_percpu_lru_hash_no_common();
	map_percpu_stats_hash_of_maps();
	map_percpu_stats_map_value_size();
}