Skip to content

Commit a591a4d

Browse files
chentao-kernelSasha Levin
authored and
Sasha Levin
committed
bpf: Check percpu map value size first
[ Upstream commit 1d24478 ] Percpu map is often used, but the map value size limit often ignored, like issue: iovisor/bcc#2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory". Signed-off-by: Jinke Han <[email protected]> Signed-off-by: Tao Chen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Jiri Olsa <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent 3ece74a commit a591a4d

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

kernel/bpf/arraymap.c

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ int array_map_alloc_check(union bpf_attr *attr)
7171
* access the elements.
7272
*/
7373
return -E2BIG;
74+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
75+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
76+
return -E2BIG;
7477

7578
return 0;
7679
}

kernel/bpf/hashtab.c

+3
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
291291
* kmalloc-able later in htab_map_update_elem()
292292
*/
293293
return -E2BIG;
294+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
295+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
296+
return -E2BIG;
294297

295298
return 0;
296299
}

0 commit comments

Comments
 (0)