Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit c43622d

Browse files
chentao-kernelgregkh
authored andcommitted
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 aaa880f commit c43622d

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
@@ -458,6 +458,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
458458
* kmalloc-able later in htab_map_update_elem()
459459
*/
460460
return -E2BIG;
461+
/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
462+
if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
463+
return -E2BIG;
461464

462465
return 0;
463466
}

0 commit comments

Comments
 (0)