Skip to content

Commit b22006a

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 adae86e commit b22006a

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

include/linux/bpf.h

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

27502750
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
2751-
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
2751+
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags);
27522752
int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
27532753
u64 flags);
27542754
int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
@@ -3815,7 +3815,12 @@ bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image)
38153815

38163816
static inline bool bpf_map_supports_cpu_flags(enum bpf_map_type map_type)
38173817
{
3818-
return false;
3818+
switch (map_type) {
3819+
case BPF_MAP_TYPE_PERCPU_ARRAY:
3820+
return true;
3821+
default:
3822+
return false;
3823+
}
38193824
}
38203825

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

kernel/bpf/arraymap.c

Lines changed: 23 additions & 6 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(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
}
@@ -398,10 +405,11 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
398405
struct bpf_array *array = container_of(map, struct bpf_array, map);
399406
u32 index = *(u32 *)key;
400407
void __percpu *pptr;
401-
int cpu, off = 0;
408+
void *ptr, *val;
402409
u32 size;
410+
int cpu;
403411

404-
if (unlikely(map_flags > BPF_EXIST))
412+
if (unlikely((map_flags & BPF_F_LOCK) || (u32)map_flags > BPF_F_ALL_CPUS))
405413
/* unknown flags */
406414
return -EINVAL;
407415

@@ -422,11 +430,20 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
422430
size = array->elem_size;
423431
rcu_read_lock();
424432
pptr = array->pptrs[index & array->index_mask];
433+
if (map_flags & BPF_F_CPU) {
434+
cpu = map_flags >> 32;
435+
ptr = per_cpu_ptr(pptr, cpu);
436+
copy_map_value(map, ptr, value);
437+
bpf_obj_free_fields(array->map.record, ptr);
438+
goto unlock;
439+
}
425440
for_each_possible_cpu(cpu) {
426-
copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off);
427-
bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu));
428-
off += size;
441+
ptr = per_cpu_ptr(pptr, cpu);
442+
val = (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu;
443+
copy_map_value(map, ptr, val);
444+
bpf_obj_free_fields(array->map.record, ptr);
429445
}
446+
unlock:
430447
rcu_read_unlock();
431448
return 0;
432449
}

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)