Skip to content

Commit d9514c6

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 7068146 commit d9514c6

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
@@ -10673,7 +10673,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
1067310673
}
1067410674

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

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

10727-
err = validate_map_op(map, key_sz, value_sz, true);
10741+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1072810742
if (err)
1072910743
return libbpf_err(err);
1073010744

@@ -10737,7 +10751,7 @@ int bpf_map__update_elem(const struct bpf_map *map,
1073710751
{
1073810752
int err;
1073910753

10740-
err = validate_map_op(map, key_sz, value_sz, true);
10754+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1074110755
if (err)
1074210756
return libbpf_err(err);
1074310757

@@ -10749,7 +10763,7 @@ int bpf_map__delete_elem(const struct bpf_map *map,
1074910763
{
1075010764
int err;
1075110765

10752-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10766+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, flags);
1075310767
if (err)
1075410768
return libbpf_err(err);
1075510769

@@ -10762,7 +10776,7 @@ int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1076210776
{
1076310777
int err;
1076410778

10765-
err = validate_map_op(map, key_sz, value_sz, true);
10779+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1076610780
if (err)
1076710781
return libbpf_err(err);
1076810782

@@ -10774,7 +10788,7 @@ int bpf_map__get_next_key(const struct bpf_map *map,
1077410788
{
1077510789
int err;
1077610790

10777-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10791+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, 0);
1077810792
if (err)
1077910793
return libbpf_err(err);
1078010794

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)