Skip to content

Commit 34ad558

Browse files
iamkafaidavem330
authored andcommitted
bpf: Add BPF_(PROG|MAP)_GET_NEXT_ID command
This patch adds BPF_PROG_GET_NEXT_ID and BPF_MAP_GET_NEXT_ID to allow userspace to iterate all bpf_prog IDs and bpf_map IDs. The API is trying to be consistent with the existing BPF_MAP_GET_NEXT_KEY. It is currently limited to CAP_SYS_ADMIN which we can consider to lift it in followup patches. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Alexei Starovoitov <ast@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f3f1c05 commit 34ad558

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ enum bpf_cmd {
8282
BPF_PROG_ATTACH,
8383
BPF_PROG_DETACH,
8484
BPF_PROG_TEST_RUN,
85+
BPF_PROG_GET_NEXT_ID,
86+
BPF_MAP_GET_NEXT_ID,
8587
};
8688

8789
enum bpf_map_type {
@@ -209,6 +211,11 @@ union bpf_attr {
209211
__u32 repeat;
210212
__u32 duration;
211213
} test;
214+
215+
struct { /* anonymous struct used by BPF_*_GET_NEXT_ID */
216+
__u32 start_id;
217+
__u32 next_id;
218+
};
212219
} __attribute__((aligned(8)));
213220

214221
/* BPF helper function descriptions:

kernel/bpf/syscall.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ static void bpf_map_put_uref(struct bpf_map *map)
166166
void bpf_map_put(struct bpf_map *map)
167167
{
168168
if (atomic_dec_and_test(&map->refcnt)) {
169+
/* bpf_map_free_id() must be called first */
169170
bpf_map_free_id(map);
170171
INIT_WORK(&map->work, bpf_map_free_deferred);
171172
schedule_work(&map->work);
@@ -726,6 +727,7 @@ void bpf_prog_put(struct bpf_prog *prog)
726727
{
727728
if (atomic_dec_and_test(&prog->aux->refcnt)) {
728729
trace_bpf_prog_put_rcu(prog);
730+
/* bpf_prog_free_id() must be called first */
729731
bpf_prog_free_id(prog);
730732
bpf_prog_kallsyms_del(prog);
731733
call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
@@ -1069,6 +1071,34 @@ static int bpf_prog_test_run(const union bpf_attr *attr,
10691071
return ret;
10701072
}
10711073

1074+
#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1075+
1076+
static int bpf_obj_get_next_id(const union bpf_attr *attr,
1077+
union bpf_attr __user *uattr,
1078+
struct idr *idr,
1079+
spinlock_t *lock)
1080+
{
1081+
u32 next_id = attr->start_id;
1082+
int err = 0;
1083+
1084+
if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1085+
return -EINVAL;
1086+
1087+
if (!capable(CAP_SYS_ADMIN))
1088+
return -EPERM;
1089+
1090+
next_id++;
1091+
spin_lock_bh(lock);
1092+
if (!idr_get_next(idr, &next_id))
1093+
err = -ENOENT;
1094+
spin_unlock_bh(lock);
1095+
1096+
if (!err)
1097+
err = put_user(next_id, &uattr->next_id);
1098+
1099+
return err;
1100+
}
1101+
10721102
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
10731103
{
10741104
union bpf_attr attr = {};
@@ -1146,6 +1176,14 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
11461176
case BPF_PROG_TEST_RUN:
11471177
err = bpf_prog_test_run(&attr, uattr);
11481178
break;
1179+
case BPF_PROG_GET_NEXT_ID:
1180+
err = bpf_obj_get_next_id(&attr, uattr,
1181+
&prog_idr, &prog_idr_lock);
1182+
break;
1183+
case BPF_MAP_GET_NEXT_ID:
1184+
err = bpf_obj_get_next_id(&attr, uattr,
1185+
&map_idr, &map_idr_lock);
1186+
break;
11491187
default:
11501188
err = -EINVAL;
11511189
break;

0 commit comments

Comments
 (0)