Skip to content

Commit 064a766

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_array maps
Introduce support for the BPF_F_ALL_CPUS flag in percpu_array maps to allow updating values for all CPUs with a single value for both update_elem and update_batch APIs. Introduce support for the BPF_F_CPU flag in percpu_array maps to allow: * update value for specified CPU for both update_elem and update_batch APIs. * lookup value for specified CPU for both lookup_elem and lookup_batch APIs. The BPF_F_CPU flag is passed via: * map_flags of lookup_elem and update_elem APIs along with embedded cpu info. * elem_flags of lookup_batch and update_batch APIs along with embedded cpu info. Signed-off-by: Leon Hwang <[email protected]>
1 parent 6dd6fc3 commit 064a766

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

include/linux/bpf.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,7 @@ int map_set_for_each_callback_args(struct bpf_verifier_env *env,
27182718
struct bpf_func_state *callee);
27192719

27202720
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
2721-
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
2721+
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags);
27222722
int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
27232723
u64 flags);
27242724
int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
@@ -3770,7 +3770,12 @@ struct bpf_prog *bpf_prog_find_from_stack(void);
37703770

37713771
static inline bool bpf_map_supports_cpu_flags(enum bpf_map_type map_type)
37723772
{
3773-
return false;
3773+
switch (map_type) {
3774+
case BPF_MAP_TYPE_PERCPU_ARRAY:
3775+
return true;
3776+
default:
3777+
return false;
3778+
}
37743779
}
37753780

37763781
static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 allowed_flags)

kernel/bpf/arraymap.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static void *percpu_array_map_lookup_percpu_elem(struct bpf_map *map, void *key,
307307
return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu);
308308
}
309309

310-
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
310+
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 map_flags)
311311
{
312312
struct bpf_array *array = container_of(map, struct bpf_array, map);
313313
u32 index = *(u32 *)key;
@@ -325,11 +325,18 @@ int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
325325
size = array->elem_size;
326326
rcu_read_lock();
327327
pptr = array->pptrs[index & array->index_mask];
328+
if (map_flags & BPF_F_CPU) {
329+
cpu = map_flags >> 32;
330+
copy_map_value_long(map, value, per_cpu_ptr(pptr, cpu));
331+
check_and_init_map_value(map, value);
332+
goto unlock;
333+
}
328334
for_each_possible_cpu(cpu) {
329335
copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
330336
check_and_init_map_value(map, value + off);
331337
off += size;
332338
}
339+
unlock:
333340
rcu_read_unlock();
334341
return 0;
335342
}
@@ -402,7 +409,7 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
402409
int cpu, off = 0;
403410
u32 size;
404411

405-
if (unlikely(map_flags > BPF_EXIST))
412+
if (unlikely((map_flags & BPF_F_LOCK) || (u32)map_flags > BPF_F_ALL_CPUS))
406413
/* unknown flags */
407414
return -EINVAL;
408415

@@ -423,11 +430,22 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
423430
size = array->elem_size;
424431
rcu_read_lock();
425432
pptr = array->pptrs[index & array->index_mask];
433+
if (map_flags & BPF_F_CPU) {
434+
cpu = map_flags >> 32;
435+
copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value);
436+
bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu));
437+
goto unlock;
438+
}
426439
for_each_possible_cpu(cpu) {
427440
copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off);
428441
bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu));
429-
off += size;
442+
/* same user-provided value is used if BPF_F_ALL_CPUS is
443+
* specified, otherwise value is an array of per-CPU values.
444+
*/
445+
if (!(map_flags & BPF_F_ALL_CPUS))
446+
off += size;
430447
}
448+
unlock:
431449
rcu_read_unlock();
432450
return 0;
433451
}

kernel/bpf/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
318318
map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
319319
err = bpf_percpu_hash_copy(map, key, value);
320320
} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
321-
err = bpf_percpu_array_copy(map, key, value);
321+
err = bpf_percpu_array_copy(map, key, value, flags);
322322
} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
323323
err = bpf_percpu_cgroup_storage_copy(map, key, value);
324324
} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {

0 commit comments

Comments
 (0)