Skip to content

Commit 2d602c8

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Support GET_FD_BY_ID and GET_NEXT_ID for bpf_link
Add support to look up bpf_link by ID and iterate over all existing bpf_links in the system. GET_FD_BY_ID code handles not-yet-ready bpf_link by checking that its ID hasn't been set to non-zero value yet. Setting bpf_link's ID is done as the very last step in finalizing bpf_link, together with installing FD. This approach allows users of bpf_link in kernel code to not worry about races between user-space and kernel code that hasn't finished attaching and initializing bpf_link. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200429001614.1544-4-andriin@fb.com
1 parent a3b80e1 commit 2d602c8

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ enum bpf_cmd {
113113
BPF_MAP_DELETE_BATCH,
114114
BPF_LINK_CREATE,
115115
BPF_LINK_UPDATE,
116+
BPF_LINK_GET_FD_BY_ID,
117+
BPF_LINK_GET_NEXT_ID,
116118
};
117119

118120
enum bpf_map_type {

‎kernel/bpf/syscall.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,6 +3713,48 @@ static int link_update(union bpf_attr *attr)
37133713
return ret;
37143714
}
37153715

3716+
static int bpf_link_inc_not_zero(struct bpf_link *link)
3717+
{
3718+
return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT;
3719+
}
3720+
3721+
#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
3722+
3723+
static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
3724+
{
3725+
struct bpf_link *link;
3726+
u32 id = attr->link_id;
3727+
int fd, err;
3728+
3729+
if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
3730+
return -EINVAL;
3731+
3732+
if (!capable(CAP_SYS_ADMIN))
3733+
return -EPERM;
3734+
3735+
spin_lock_bh(&link_idr_lock);
3736+
link = idr_find(&link_idr, id);
3737+
/* before link is "settled", ID is 0, pretend it doesn't exist yet */
3738+
if (link) {
3739+
if (link->id)
3740+
err = bpf_link_inc_not_zero(link);
3741+
else
3742+
err = -EAGAIN;
3743+
} else {
3744+
err = -ENOENT;
3745+
}
3746+
spin_unlock_bh(&link_idr_lock);
3747+
3748+
if (err)
3749+
return err;
3750+
3751+
fd = bpf_link_new_fd(link);
3752+
if (fd < 0)
3753+
bpf_link_put(link);
3754+
3755+
return fd;
3756+
}
3757+
37163758
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
37173759
{
37183760
union bpf_attr attr;
@@ -3830,6 +3872,13 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
38303872
case BPF_LINK_UPDATE:
38313873
err = link_update(&attr);
38323874
break;
3875+
case BPF_LINK_GET_FD_BY_ID:
3876+
err = bpf_link_get_fd_by_id(&attr);
3877+
break;
3878+
case BPF_LINK_GET_NEXT_ID:
3879+
err = bpf_obj_get_next_id(&attr, uattr,
3880+
&link_idr, &link_idr_lock);
3881+
break;
38333882
default:
38343883
err = -EINVAL;
38353884
break;

0 commit comments

Comments
 (0)