Skip to content

Commit a43c228

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 0f13980 commit a43c228

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
@@ -10675,7 +10675,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
1067510675
}
1067610676

1067710677
static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10678-
size_t value_sz, bool check_value_sz)
10678+
size_t value_sz, bool check_value_sz, __u64 flags)
1067910679
{
1068010680
if (!map_is_created(map)) /* map is not yet created */
1068110681
return -ENOENT;
@@ -10702,6 +10702,20 @@ static int validate_map_op(const struct bpf_map *map, size_t key_sz,
1070210702
int num_cpu = libbpf_num_possible_cpus();
1070310703
size_t elem_sz = roundup(map->def.value_size, 8);
1070410704

10705+
if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) {
10706+
if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS)) {
10707+
pr_warn("map '%s': BPF_F_CPU and BPF_F_ALL_CPUS are mutually exclusive\n",
10708+
map->name);
10709+
return -EINVAL;
10710+
}
10711+
if (value_sz != elem_sz) {
10712+
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %zu\n",
10713+
map->name, value_sz, elem_sz);
10714+
return -EINVAL;
10715+
}
10716+
break;
10717+
}
10718+
1070510719
if (value_sz != num_cpu * elem_sz) {
1070610720
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
1070710721
map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
@@ -10726,7 +10740,7 @@ int bpf_map__lookup_elem(const struct bpf_map *map,
1072610740
{
1072710741
int err;
1072810742

10729-
err = validate_map_op(map, key_sz, value_sz, true);
10743+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1073010744
if (err)
1073110745
return libbpf_err(err);
1073210746

@@ -10739,7 +10753,7 @@ int bpf_map__update_elem(const struct bpf_map *map,
1073910753
{
1074010754
int err;
1074110755

10742-
err = validate_map_op(map, key_sz, value_sz, true);
10756+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1074310757
if (err)
1074410758
return libbpf_err(err);
1074510759

@@ -10751,7 +10765,7 @@ int bpf_map__delete_elem(const struct bpf_map *map,
1075110765
{
1075210766
int err;
1075310767

10754-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10768+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, flags);
1075510769
if (err)
1075610770
return libbpf_err(err);
1075710771

@@ -10764,7 +10778,7 @@ int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1076410778
{
1076510779
int err;
1076610780

10767-
err = validate_map_op(map, key_sz, value_sz, true);
10781+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1076810782
if (err)
1076910783
return libbpf_err(err);
1077010784

@@ -10776,7 +10790,7 @@ int bpf_map__get_next_key(const struct bpf_map *map,
1077610790
{
1077710791
int err;
1077810792

10779-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10793+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, 0);
1078010794
if (err)
1078110795
return libbpf_err(err);
1078210796

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+
* `round_up(value_size, 8)` if either **BPF_F_CPU** or **BPF_F_ALL_CPUS** is
1201+
* specified in **flags**, otherwise a product of BPF map value size and number
1202+
* of 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)