Skip to content

Commit da4612d

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags 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 befd01d commit da4612d

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

include/linux/bpf.h

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

27482748
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
2749-
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
2749+
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value,
2750+
u64 flags);
27502751
int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
27512752
u64 flags);
27522753
int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
@@ -3760,7 +3761,12 @@ struct bpf_prog *bpf_prog_find_from_stack(void);
37603761

37613762
static inline bool bpf_map_supports_cpu_flags(enum bpf_map_type map_type)
37623763
{
3763-
return false;
3764+
switch (map_type) {
3765+
case BPF_MAP_TYPE_PERCPU_ARRAY:
3766+
return true;
3767+
default:
3768+
return false;
3769+
}
37643770
}
37653771

37663772
static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 extra_flags_mask)

kernel/bpf/arraymap.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,29 +295,29 @@ static void *percpu_array_map_lookup_percpu_elem(struct bpf_map *map, void *key,
295295
return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu);
296296
}
297297

298-
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
298+
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 flags)
299299
{
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;
304+
int err;
305305

306306
if (unlikely(index >= array->map.max_entries))
307307
return -ENOENT;
308308

309+
err = bpf_map_check_cpu_flags(flags, false);
310+
if (unlikely(err))
311+
return err;
312+
309313
/* per_cpu areas are zero-filled and bpf programs can only
310314
* access 'value_size' of them, so copying rounded areas
311315
* will not leak any kernel data
312316
*/
313317
size = array->elem_size;
314318
rcu_read_lock();
315319
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-
}
320+
bpf_percpu_copy_to_user(map, pptr, value, size, flags);
321321
rcu_read_unlock();
322322
return 0;
323323
}
@@ -387,12 +387,12 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
387387
struct bpf_array *array = container_of(map, struct bpf_array, map);
388388
u32 index = *(u32 *)key;
389389
void __percpu *pptr;
390-
int cpu, off = 0;
391390
u32 size;
391+
int err;
392392

393-
if (unlikely(map_flags > BPF_EXIST))
394-
/* unknown flags */
395-
return -EINVAL;
393+
err = bpf_map_check_cpu_flags(map_flags, true);
394+
if (unlikely(err))
395+
return err;
396396

397397
if (unlikely(index >= array->map.max_entries))
398398
/* all elements were pre-allocated, cannot insert a new one */
@@ -411,11 +411,7 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
411411
size = array->elem_size;
412412
rcu_read_lock();
413413
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-
}
414+
bpf_percpu_copy_from_user(map, pptr, value, size, map_flags);
419415
rcu_read_unlock();
420416
return 0;
421417
}

kernel/bpf/syscall.c

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

0 commit comments

Comments
 (0)