Skip to content

Commit 14dc6f0

Browse files
iamkafaidavem330
authored andcommitted
bpf: Add syscall lookup support for fd array and htab
This patch allows userspace to do BPF_MAP_LOOKUP_ELEM on BPF_MAP_TYPE_PROG_ARRAY, BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS. The lookup returns a prog-id or map-id to the userspace. The userspace can then use the BPF_PROG_GET_FD_BY_ID or BPF_MAP_GET_FD_BY_ID to get a fd. Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 77b0d36 commit 14dc6f0

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct bpf_map_ops {
3636
int fd);
3737
void (*map_fd_put_ptr)(void *ptr);
3838
u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
39+
u32 (*map_fd_sys_lookup_elem)(void *ptr);
3940
};
4041

4142
struct bpf_map {
@@ -288,9 +289,11 @@ int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
288289

289290
int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
290291
void *key, void *value, u64 map_flags);
292+
int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
291293
void bpf_fd_array_map_clear(struct bpf_map *map);
292294
int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
293295
void *key, void *value, u64 map_flags);
296+
int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
294297

295298
/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
296299
* forced to use 'long' read/writes to try to atomically copy long counters.

kernel/bpf/arraymap.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
334334
return NULL;
335335
}
336336

337+
/* only called from syscall */
338+
int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
339+
{
340+
void **elem, *ptr;
341+
int ret = 0;
342+
343+
if (!map->ops->map_fd_sys_lookup_elem)
344+
return -ENOTSUPP;
345+
346+
rcu_read_lock();
347+
elem = array_map_lookup_elem(map, key);
348+
if (elem && (ptr = READ_ONCE(*elem)))
349+
*value = map->ops->map_fd_sys_lookup_elem(ptr);
350+
else
351+
ret = -ENOENT;
352+
rcu_read_unlock();
353+
354+
return ret;
355+
}
356+
337357
/* only called from syscall */
338358
int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
339359
void *key, void *value, u64 map_flags)
@@ -400,6 +420,11 @@ static void prog_fd_array_put_ptr(void *ptr)
400420
bpf_prog_put(ptr);
401421
}
402422

423+
static u32 prog_fd_array_sys_lookup_elem(void *ptr)
424+
{
425+
return ((struct bpf_prog *)ptr)->aux->id;
426+
}
427+
403428
/* decrement refcnt of all bpf_progs that are stored in this map */
404429
void bpf_fd_array_map_clear(struct bpf_map *map)
405430
{
@@ -418,6 +443,7 @@ const struct bpf_map_ops prog_array_map_ops = {
418443
.map_delete_elem = fd_array_map_delete_elem,
419444
.map_fd_get_ptr = prog_fd_array_get_ptr,
420445
.map_fd_put_ptr = prog_fd_array_put_ptr,
446+
.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
421447
};
422448

423449
static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -585,4 +611,5 @@ const struct bpf_map_ops array_of_maps_map_ops = {
585611
.map_delete_elem = fd_array_map_delete_elem,
586612
.map_fd_get_ptr = bpf_map_fd_get_ptr,
587613
.map_fd_put_ptr = bpf_map_fd_put_ptr,
614+
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
588615
};

kernel/bpf/hashtab.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,26 @@ static void fd_htab_map_free(struct bpf_map *map)
12431243
htab_map_free(map);
12441244
}
12451245

1246+
/* only called from syscall */
1247+
int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1248+
{
1249+
void **ptr;
1250+
int ret = 0;
1251+
1252+
if (!map->ops->map_fd_sys_lookup_elem)
1253+
return -ENOTSUPP;
1254+
1255+
rcu_read_lock();
1256+
ptr = htab_map_lookup_elem(map, key);
1257+
if (ptr)
1258+
*value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1259+
else
1260+
ret = -ENOENT;
1261+
rcu_read_unlock();
1262+
1263+
return ret;
1264+
}
1265+
12461266
/* only called from syscall */
12471267
int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
12481268
void *key, void *value, u64 map_flags)
@@ -1305,4 +1325,5 @@ const struct bpf_map_ops htab_of_maps_map_ops = {
13051325
.map_delete_elem = htab_map_delete_elem,
13061326
.map_fd_get_ptr = bpf_map_fd_get_ptr,
13071327
.map_fd_put_ptr = bpf_map_fd_put_ptr,
1328+
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
13081329
};

kernel/bpf/map_in_map.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ void bpf_map_fd_put_ptr(void *ptr)
9595
*/
9696
bpf_map_put(ptr);
9797
}
98+
99+
u32 bpf_map_fd_sys_lookup_elem(void *ptr)
100+
{
101+
return ((struct bpf_map *)ptr)->id;
102+
}

kernel/bpf/map_in_map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,
1919
void *bpf_map_fd_get_ptr(struct bpf_map *map, struct file *map_file,
2020
int ufd);
2121
void bpf_map_fd_put_ptr(void *ptr);
22+
u32 bpf_map_fd_sys_lookup_elem(void *ptr);
2223

2324
#endif

kernel/bpf/syscall.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
#include <linux/kernel.h>
2525
#include <linux/idr.h>
2626

27+
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28+
(map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29+
(map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30+
(map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31+
#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32+
#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33+
2734
DEFINE_PER_CPU(int, bpf_prog_active);
2835
static DEFINE_IDR(prog_idr);
2936
static DEFINE_SPINLOCK(prog_idr_lock);
@@ -411,6 +418,8 @@ static int map_lookup_elem(union bpf_attr *attr)
411418
map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
412419
map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
413420
value_size = round_up(map->value_size, 8) * num_possible_cpus();
421+
else if (IS_FD_MAP(map))
422+
value_size = sizeof(u32);
414423
else
415424
value_size = map->value_size;
416425

@@ -426,9 +435,10 @@ static int map_lookup_elem(union bpf_attr *attr)
426435
err = bpf_percpu_array_copy(map, key, value);
427436
} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
428437
err = bpf_stackmap_copy(map, key, value);
429-
} else if (map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
430-
map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
431-
err = -ENOTSUPP;
438+
} else if (IS_FD_ARRAY(map)) {
439+
err = bpf_fd_array_map_lookup_elem(map, key, value);
440+
} else if (IS_FD_HASH(map)) {
441+
err = bpf_fd_htab_map_lookup_elem(map, key, value);
432442
} else {
433443
rcu_read_lock();
434444
ptr = map->ops->map_lookup_elem(map, key);

0 commit comments

Comments
 (0)