Skip to content

Commit cd4597e

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
libbpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu maps
Add libbpf support for the BPF_F_CPU flag for percpu maps by embedding the cpu info into the high 32 bits of: 1. **flags**: bpf_map_lookup_elem_flags(), bpf_map__lookup_elem(), bpf_map_update_elem() and bpf_map__update_elem() 2. **opts->elem_flags**: bpf_map_lookup_batch() and bpf_map_update_batch() And the flag can be BPF_F_ALL_CPUS, but cannot be 'BPF_F_CPU | BPF_F_ALL_CPUS'. Behavior: * If the flag is BPF_F_ALL_CPUS, the update is applied across all CPUs. * If the flag is BPF_F_CPU, it updates value only to the specified CPU. * If the flag is BPF_F_CPU, lookup value only from the specified CPU. * lookup does not support BPF_F_ALL_CPUS. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Leon Hwang <[email protected]>
1 parent d76b198 commit cd4597e

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

tools/lib/bpf/bpf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
289289
* Update spin_lock-ed map elements. This must be
290290
* specified if the map value contains a spinlock.
291291
*
292+
* **BPF_F_CPU**
293+
* As for percpu maps, update value on the specified CPU. And the cpu
294+
* info is embedded into the high 32 bits of **opts->elem_flags**.
295+
*
296+
* **BPF_F_ALL_CPUS**
297+
* As for percpu maps, update value across all CPUs. This flag cannot
298+
* be used with BPF_F_CPU at the same time.
299+
*
292300
* @param fd BPF map file descriptor
293301
* @param keys pointer to an array of *count* keys
294302
* @param values pointer to an array of *count* values

tools/lib/bpf/libbpf.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10671,7 +10671,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
1067110671
}
1067210672

1067310673
static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10674-
size_t value_sz, bool check_value_sz)
10674+
size_t value_sz, bool check_value_sz, __u64 flags)
1067510675
{
1067610676
if (!map_is_created(map)) /* map is not yet created */
1067710677
return -ENOENT;
@@ -10698,6 +10698,20 @@ static int validate_map_op(const struct bpf_map *map, size_t key_sz,
1069810698
int num_cpu = libbpf_num_possible_cpus();
1069910699
size_t elem_sz = roundup(map->def.value_size, 8);
1070010700

10701+
if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) {
10702+
if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS)) {
10703+
pr_warn("map '%s': BPF_F_CPU and BPF_F_ALL_CPUS are mutually exclusive\n",
10704+
map->name);
10705+
return -EINVAL;
10706+
}
10707+
if (map->def.value_size != value_sz) {
10708+
pr_warn("map '%s': unexpected value size %zu provided for either BPF_F_CPU or BPF_F_ALL_CPUS, expected %u\n",
10709+
map->name, value_sz, map->def.value_size);
10710+
return -EINVAL;
10711+
}
10712+
break;
10713+
}
10714+
1070110715
if (value_sz != num_cpu * elem_sz) {
1070210716
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
1070310717
map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
@@ -10722,7 +10736,7 @@ int bpf_map__lookup_elem(const struct bpf_map *map,
1072210736
{
1072310737
int err;
1072410738

10725-
err = validate_map_op(map, key_sz, value_sz, true);
10739+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1072610740
if (err)
1072710741
return libbpf_err(err);
1072810742

@@ -10735,7 +10749,7 @@ int bpf_map__update_elem(const struct bpf_map *map,
1073510749
{
1073610750
int err;
1073710751

10738-
err = validate_map_op(map, key_sz, value_sz, true);
10752+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1073910753
if (err)
1074010754
return libbpf_err(err);
1074110755

@@ -10747,7 +10761,7 @@ int bpf_map__delete_elem(const struct bpf_map *map,
1074710761
{
1074810762
int err;
1074910763

10750-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10764+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, flags);
1075110765
if (err)
1075210766
return libbpf_err(err);
1075310767

@@ -10760,7 +10774,7 @@ int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1076010774
{
1076110775
int err;
1076210776

10763-
err = validate_map_op(map, key_sz, value_sz, true);
10777+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1076410778
if (err)
1076510779
return libbpf_err(err);
1076610780

@@ -10772,7 +10786,7 @@ int bpf_map__get_next_key(const struct bpf_map *map,
1077210786
{
1077310787
int err;
1077410788

10775-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10789+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, 0);
1077610790
if (err)
1077710791
return libbpf_err(err);
1077810792

tools/lib/bpf/libbpf.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,12 +1196,13 @@ LIBBPF_API struct bpf_map *bpf_map__inner_map(struct bpf_map *map);
11961196
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
11971197
* @param value pointer to memory in which looked up value will be stored
11981198
* @param value_sz size in byte of value data memory; it has to match BPF map
1199-
* definition's **value_size**. For per-CPU BPF maps value size has to be
1200-
* a product of BPF map value size and number of possible CPUs in the system
1201-
* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1202-
* per-CPU values value size has to be aligned up to closest 8 bytes for
1203-
* alignment reasons, so expected size is: `round_up(value_size, 8)
1204-
* * libbpf_num_possible_cpus()`.
1199+
* definition's **value_size**. For per-CPU BPF maps, value size can be
1200+
* `value_size` if either **BPF_F_CPU** or **BPF_F_ALL_CPUS** is specified
1201+
* in **flags**, otherwise a product of BPF map value size and number of
1202+
* possible CPUs in the system (could be fetched with
1203+
* **libbpf_num_possible_cpus()**). Note also that for per-CPU values value
1204+
* size has to be aligned up to closest 8 bytes, so expected size is:
1205+
* `round_up(value_size, 8) * libbpf_num_possible_cpus()`.
12051206
* @flags extra flags passed to kernel for this operation
12061207
* @return 0, on success; negative error, otherwise
12071208
*
@@ -1219,13 +1220,7 @@ LIBBPF_API int bpf_map__lookup_elem(const struct bpf_map *map,
12191220
* @param key pointer to memory containing bytes of the key
12201221
* @param key_sz size in bytes of key data, needs to match BPF map definition's **key_size**
12211222
* @param value pointer to memory containing bytes of the value
1222-
* @param value_sz size in byte of value data memory; it has to match BPF map
1223-
* definition's **value_size**. For per-CPU BPF maps value size has to be
1224-
* a product of BPF map value size and number of possible CPUs in the system
1225-
* (could be fetched with **libbpf_num_possible_cpus()**). Note also that for
1226-
* per-CPU values value size has to be aligned up to closest 8 bytes for
1227-
* alignment reasons, so expected size is: `round_up(value_size, 8)
1228-
* * libbpf_num_possible_cpus()`.
1223+
* @param value_sz refer to **bpf_map__lookup_elem**'s description.'
12291224
* @flags extra flags passed to kernel for this operation
12301225
* @return 0, on success; negative error, otherwise
12311226
*

0 commit comments

Comments
 (0)