Skip to content

Commit 1d24478

Browse files
chentao-kernelanakryiko
authored andcommitted
bpf: Check percpu map value size first
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]
1 parent 300a90b commit 1d24478

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

kernel/bpf/arraymap.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr)
7373
/* avoid overflow on round_up(map->value_size) */
7474
if (attr->value_size > INT_MAX)
7575
return -E2BIG;
76+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
77+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
78+
return -E2BIG;
7679

7780
return 0;
7881
}

kernel/bpf/hashtab.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
462462
* kmalloc-able later in htab_map_update_elem()
463463
*/
464464
return -E2BIG;
465+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
466+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
467+
return -E2BIG;
465468

466469
return 0;
467470
}

0 commit comments

Comments
 (0)