Skip to content

Commit 12e4462

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. Signed-off-by: Leon Hwang <[email protected]>
1 parent 708e882 commit 12e4462

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
@@ -286,6 +286,14 @@ LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
286286
* Update spin_lock-ed map elements. This must be
287287
* specified if the map value contains a spinlock.
288288
*
289+
* **BPF_F_CPU**
290+
* As for percpu maps, update value on the specified CPU. And the cpu
291+
* info is embedded into the high 32 bits of **opts->elem_flags**.
292+
*
293+
* **BPF_F_ALL_CPUS**
294+
* As for percpu maps, update value across all CPUs. This flag cannot
295+
* be used with BPF_F_CPU at the same time.
296+
*
289297
* @param fd BPF map file descriptor
290298
* @param keys pointer to an array of *count* keys
291299
* @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
@@ -10603,7 +10603,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
1060310603
}
1060410604

1060510605
static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10606-
size_t value_sz, bool check_value_sz)
10606+
size_t value_sz, bool check_value_sz, __u64 flags)
1060710607
{
1060810608
if (!map_is_created(map)) /* map is not yet created */
1060910609
return -ENOENT;
@@ -10630,6 +10630,20 @@ static int validate_map_op(const struct bpf_map *map, size_t key_sz,
1063010630
int num_cpu = libbpf_num_possible_cpus();
1063110631
size_t elem_sz = roundup(map->def.value_size, 8);
1063210632

10633+
if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) {
10634+
if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS)) {
10635+
pr_warn("map '%s': can't use BPF_F_CPU and BPF_F_ALL_CPUS at the same time\n",
10636+
map->name);
10637+
return -EINVAL;
10638+
}
10639+
if (value_sz != elem_sz) {
10640+
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %zu\n",
10641+
map->name, value_sz, elem_sz);
10642+
return -EINVAL;
10643+
}
10644+
break;
10645+
}
10646+
1063310647
if (value_sz != num_cpu * elem_sz) {
1063410648
pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
1063510649
map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
@@ -10654,7 +10668,7 @@ int bpf_map__lookup_elem(const struct bpf_map *map,
1065410668
{
1065510669
int err;
1065610670

10657-
err = validate_map_op(map, key_sz, value_sz, true);
10671+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1065810672
if (err)
1065910673
return libbpf_err(err);
1066010674

@@ -10667,7 +10681,7 @@ int bpf_map__update_elem(const struct bpf_map *map,
1066710681
{
1066810682
int err;
1066910683

10670-
err = validate_map_op(map, key_sz, value_sz, true);
10684+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1067110685
if (err)
1067210686
return libbpf_err(err);
1067310687

@@ -10679,7 +10693,7 @@ int bpf_map__delete_elem(const struct bpf_map *map,
1067910693
{
1068010694
int err;
1068110695

10682-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10696+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, flags);
1068310697
if (err)
1068410698
return libbpf_err(err);
1068510699

@@ -10692,7 +10706,7 @@ int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
1069210706
{
1069310707
int err;
1069410708

10695-
err = validate_map_op(map, key_sz, value_sz, true);
10709+
err = validate_map_op(map, key_sz, value_sz, true, flags);
1069610710
if (err)
1069710711
return libbpf_err(err);
1069810712

@@ -10704,7 +10718,7 @@ int bpf_map__get_next_key(const struct bpf_map *map,
1070410718
{
1070510719
int err;
1070610720

10707-
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10721+
err = validate_map_op(map, key_sz, 0, false /* check_value_sz */, 0);
1070810722
if (err)
1070910723
return libbpf_err(err);
1071010724

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 **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 else 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)