Skip to content

Commit b9e5206

Browse files
dverbeirkernel-patches-bot
authored andcommitted
bpf: zero-fill re-used per-cpu map element
Zero-fill element values for all cpus, just as when not using prealloc. This is the only way the bpf program can ensure known initial values for cpus other than the current one ('onallcpus' cannot be set when coming from the bpf program). The scenario is: bpf program inserts some elements in a per-cpu map, then deletes some (or userspace does). When later adding new elements using bpf_map_update_elem(), the bpf program can only set the value of the new elements for the current cpu. When prealloc is enabled, previously deleted elements are re-used. Without the fix, values for other cpus remain whatever they were when the re-used entry was previously freed. Fixes: 6c90598 ("bpf: pre-allocate hash map elements") Acked-by: Matthieu Baerts <[email protected]> Signed-off-by: David Verbeiren <[email protected]>
1 parent cca545a commit b9e5206

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

kernel/bpf/hashtab.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
836836
bool prealloc = htab_is_prealloc(htab);
837837
struct htab_elem *l_new, **pl_new;
838838
void __percpu *pptr;
839+
int cpu;
839840

840841
if (prealloc) {
841842
if (old_elem) {
@@ -880,6 +881,17 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
880881
size = round_up(size, 8);
881882
if (prealloc) {
882883
pptr = htab_elem_get_ptr(l_new, key_size);
884+
885+
/* zero-fill element values for all cpus, just as when
886+
* not using prealloc. Only way for bpf program to
887+
* ensure known initial values for cpus other than
888+
* current one (onallcpus=false when coming from bpf
889+
* prog).
890+
*/
891+
if (!onallcpus)
892+
for_each_possible_cpu(cpu)
893+
memset((void *)per_cpu_ptr(pptr, cpu),
894+
0, size);
883895
} else {
884896
/* alloc_percpu zero-fills */
885897
pptr = __alloc_percpu_gfp(size, 8,

0 commit comments

Comments
 (0)