Skip to content

Commit fd91de7

Browse files
iamkafaidavem330
authored andcommitted
bpf: Refactor codes handling percpu map
Refactor the codes that populate the value of a htab_elem in a BPF_MAP_TYPE_PERCPU_HASH typed bpf_map. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 961578b commit fd91de7

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

kernel/bpf/hashtab.c

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,24 @@ static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
420420
}
421421
}
422422

423+
static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
424+
void *value, bool onallcpus)
425+
{
426+
if (!onallcpus) {
427+
/* copy true value_size bytes */
428+
memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
429+
} else {
430+
u32 size = round_up(htab->map.value_size, 8);
431+
int off = 0, cpu;
432+
433+
for_each_possible_cpu(cpu) {
434+
bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
435+
value + off, size);
436+
off += size;
437+
}
438+
}
439+
}
440+
423441
static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
424442
void *value, u32 key_size, u32 hash,
425443
bool percpu, bool onallcpus,
@@ -479,18 +497,8 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
479497
}
480498
}
481499

482-
if (!onallcpus) {
483-
/* copy true value_size bytes */
484-
memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
485-
} else {
486-
int off = 0, cpu;
500+
pcpu_copy_value(htab, pptr, value, onallcpus);
487501

488-
for_each_possible_cpu(cpu) {
489-
bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
490-
value + off, size);
491-
off += size;
492-
}
493-
}
494502
if (!prealloc)
495503
htab_elem_set_ptr(l_new, key_size, pptr);
496504
} else {
@@ -606,22 +614,9 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
606614
goto err;
607615

608616
if (l_old) {
609-
void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
610-
u32 size = htab->map.value_size;
611-
612617
/* per-cpu hash map can update value in-place */
613-
if (!onallcpus) {
614-
memcpy(this_cpu_ptr(pptr), value, size);
615-
} else {
616-
int off = 0, cpu;
617-
618-
size = round_up(size, 8);
619-
for_each_possible_cpu(cpu) {
620-
bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
621-
value + off, size);
622-
off += size;
623-
}
624-
}
618+
pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
619+
value, onallcpus);
625620
} else {
626621
l_new = alloc_htab_elem(htab, key, value, key_size,
627622
hash, true, onallcpus, false);

0 commit comments

Comments
 (0)