Skip to content

Commit e7f1c0f

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Generalize data copying for percpu maps
While adding support for the BPF_F_CPU and BPF_F_ALL_CPUS flags, the data copying logic of the following percpu map types needs to be updated: * percpu_array * percpu_hash * lru_percpu_hash * percpu_cgroup_storage Following Andrii’s suggestion[0], this patch refactors the data copying logic by introducing two helpers: * `bpf_percpu_copy_to_user()` * `bpf_percpu_copy_from_user()` This prepares the codebase for the upcoming CPU flag support. [0] https://lore.kernel.org/bpf/[email protected]/ Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Leon Hwang <[email protected]>
1 parent c32e265 commit e7f1c0f

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

include/linux/bpf.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,34 @@ static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src
547547
bpf_obj_memcpy(map->record, dst, src, map->value_size, true);
548548
}
549549

550+
#ifdef CONFIG_BPF_SYSCALL
551+
static inline void bpf_percpu_copy_to_user(struct bpf_map *map, void __percpu *pptr, void *value,
552+
u32 size)
553+
{
554+
int cpu, off = 0;
555+
556+
for_each_possible_cpu(cpu) {
557+
copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
558+
check_and_init_map_value(map, value + off);
559+
off += size;
560+
}
561+
}
562+
563+
void bpf_obj_free_fields(const struct btf_record *rec, void *obj);
564+
565+
static inline void bpf_percpu_copy_from_user(struct bpf_map *map, void __percpu *pptr, void *value,
566+
u32 size)
567+
{
568+
int cpu, off = 0;
569+
570+
for_each_possible_cpu(cpu) {
571+
copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off);
572+
bpf_obj_free_fields(map->record, per_cpu_ptr(pptr, cpu));
573+
off += size;
574+
}
575+
}
576+
#endif
577+
550578
static inline void bpf_obj_swap_uptrs(const struct btf_record *rec, void *dst, void *src)
551579
{
552580
unsigned long *src_uptr, *dst_uptr;
@@ -2417,7 +2445,6 @@ struct btf_record *btf_record_dup(const struct btf_record *rec);
24172445
bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b);
24182446
void bpf_obj_free_timer(const struct btf_record *rec, void *obj);
24192447
void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj);
2420-
void bpf_obj_free_fields(const struct btf_record *rec, void *obj);
24212448
void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu);
24222449

24232450
struct bpf_map *bpf_map_get(u32 ufd);

kernel/bpf/arraymap.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
300300
struct bpf_array *array = container_of(map, struct bpf_array, map);
301301
u32 index = *(u32 *)key;
302302
void __percpu *pptr;
303-
int cpu, off = 0;
304303
u32 size;
305304

306305
if (unlikely(index >= array->map.max_entries))
@@ -313,11 +312,7 @@ int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
313312
size = array->elem_size;
314313
rcu_read_lock();
315314
pptr = array->pptrs[index & array->index_mask];
316-
for_each_possible_cpu(cpu) {
317-
copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
318-
check_and_init_map_value(map, value + off);
319-
off += size;
320-
}
315+
bpf_percpu_copy_to_user(map, pptr, value, size);
321316
rcu_read_unlock();
322317
return 0;
323318
}
@@ -387,7 +382,6 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
387382
struct bpf_array *array = container_of(map, struct bpf_array, map);
388383
u32 index = *(u32 *)key;
389384
void __percpu *pptr;
390-
int cpu, off = 0;
391385
u32 size;
392386

393387
if (unlikely(map_flags > BPF_EXIST))
@@ -411,11 +405,7 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
411405
size = array->elem_size;
412406
rcu_read_lock();
413407
pptr = array->pptrs[index & array->index_mask];
414-
for_each_possible_cpu(cpu) {
415-
copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off);
416-
bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu));
417-
off += size;
418-
}
408+
bpf_percpu_copy_from_user(map, pptr, value, size);
419409
rcu_read_unlock();
420410
return 0;
421411
}

kernel/bpf/hashtab.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -944,12 +944,8 @@ static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
944944
copy_map_value(&htab->map, this_cpu_ptr(pptr), value);
945945
} else {
946946
u32 size = round_up(htab->map.value_size, 8);
947-
int off = 0, cpu;
948947

949-
for_each_possible_cpu(cpu) {
950-
copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value + off);
951-
off += size;
952-
}
948+
bpf_percpu_copy_from_user(&htab->map, pptr, value, size);
953949
}
954950
}
955951

@@ -1802,15 +1798,10 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
18021798
memcpy(dst_key, l->key, key_size);
18031799

18041800
if (is_percpu) {
1805-
int off = 0, cpu;
18061801
void __percpu *pptr;
18071802

18081803
pptr = htab_elem_get_ptr(l, map->key_size);
1809-
for_each_possible_cpu(cpu) {
1810-
copy_map_value_long(&htab->map, dst_val + off, per_cpu_ptr(pptr, cpu));
1811-
check_and_init_map_value(&htab->map, dst_val + off);
1812-
off += size;
1813-
}
1804+
bpf_percpu_copy_to_user(&htab->map, pptr, dst_val, size);
18141805
} else {
18151806
value = htab_elem_value(l, key_size);
18161807
if (is_fd_htab(htab)) {
@@ -2370,7 +2361,6 @@ int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
23702361
struct htab_elem *l;
23712362
void __percpu *pptr;
23722363
int ret = -ENOENT;
2373-
int cpu, off = 0;
23742364
u32 size;
23752365

23762366
/* per_cpu areas are zero-filled and bpf programs can only
@@ -2386,11 +2376,7 @@ int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
23862376
* eviction heuristics when user space does a map walk.
23872377
*/
23882378
pptr = htab_elem_get_ptr(l, map->key_size);
2389-
for_each_possible_cpu(cpu) {
2390-
copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
2391-
check_and_init_map_value(map, value + off);
2392-
off += size;
2393-
}
2379+
bpf_percpu_copy_to_user(map, pptr, value, size);
23942380
ret = 0;
23952381
out:
23962382
rcu_read_unlock();

kernel/bpf/local_storage.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
184184
{
185185
struct bpf_cgroup_storage_map *map = map_to_storage(_map);
186186
struct bpf_cgroup_storage *storage;
187-
int cpu, off = 0;
187+
void __percpu *pptr;
188188
u32 size;
189189

190190
rcu_read_lock();
@@ -199,11 +199,8 @@ int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
199199
* will not leak any kernel data
200200
*/
201201
size = round_up(_map->value_size, 8);
202-
for_each_possible_cpu(cpu) {
203-
bpf_long_memcpy(value + off,
204-
per_cpu_ptr(storage->percpu_buf, cpu), size);
205-
off += size;
206-
}
202+
pptr = storage->percpu_buf;
203+
bpf_percpu_copy_to_user(_map, pptr, value, size);
207204
rcu_read_unlock();
208205
return 0;
209206
}
@@ -213,7 +210,7 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
213210
{
214211
struct bpf_cgroup_storage_map *map = map_to_storage(_map);
215212
struct bpf_cgroup_storage *storage;
216-
int cpu, off = 0;
213+
void __percpu *pptr;
217214
u32 size;
218215

219216
if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
@@ -233,11 +230,8 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
233230
* so no kernel data leaks possible
234231
*/
235232
size = round_up(_map->value_size, 8);
236-
for_each_possible_cpu(cpu) {
237-
bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
238-
value + off, size);
239-
off += size;
240-
}
233+
pptr = storage->percpu_buf;
234+
bpf_percpu_copy_from_user(_map, pptr, value, size);
241235
rcu_read_unlock();
242236
return 0;
243237
}

0 commit comments

Comments
 (0)