Skip to content

Commit a3b80e1

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Allocate ID for bpf_link
Generate ID for each bpf_link using IDR, similarly to bpf_map and bpf_prog. bpf_link creation, initialization, attachment, and exposing to user-space through FD and ID is a complicated multi-step process, abstract it away through bpf_link_primer and bpf_link_prime(), bpf_link_settle(), and bpf_link_cleanup() internal API. They guarantee that until bpf_link is properly attached, user-space won't be able to access partially-initialized bpf_link either from FD or ID. All this allows to simplify bpf_link attachment and error handling code. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200429001614.1544-3-andriin@fb.com
1 parent f9d0412 commit a3b80e1

4 files changed

Lines changed: 118 additions & 57 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,22 +1085,31 @@ int bpf_prog_new_fd(struct bpf_prog *prog);
10851085

10861086
struct bpf_link {
10871087
atomic64_t refcnt;
1088+
u32 id;
10881089
const struct bpf_link_ops *ops;
10891090
struct bpf_prog *prog;
10901091
struct work_struct work;
10911092
};
10921093

1094+
struct bpf_link_primer {
1095+
struct bpf_link *link;
1096+
struct file *file;
1097+
int fd;
1098+
u32 id;
1099+
};
1100+
10931101
struct bpf_link_ops {
10941102
void (*release)(struct bpf_link *link);
10951103
void (*dealloc)(struct bpf_link *link);
10961104
int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
10971105
struct bpf_prog *old_prog);
10981106
};
10991107

1100-
void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
1101-
struct bpf_prog *prog);
1102-
void bpf_link_cleanup(struct bpf_link *link, struct file *link_file,
1103-
int link_fd);
1108+
void bpf_link_init(struct bpf_link *link,
1109+
const struct bpf_link_ops *ops, struct bpf_prog *prog);
1110+
int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1111+
int bpf_link_settle(struct bpf_link_primer *primer);
1112+
void bpf_link_cleanup(struct bpf_link_primer *primer);
11041113
void bpf_link_inc(struct bpf_link *link);
11051114
void bpf_link_put(struct bpf_link *link);
11061115
int bpf_link_new_fd(struct bpf_link *link);

‎include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ union bpf_attr {
523523
__u32 prog_id;
524524
__u32 map_id;
525525
__u32 btf_id;
526+
__u32 link_id;
526527
};
527528
__u32 next_id;
528529
__u32 open_flags;

‎kernel/bpf/cgroup.c‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,10 @@ const struct bpf_link_ops bpf_cgroup_link_lops = {
841841

842842
int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
843843
{
844+
struct bpf_link_primer link_primer;
844845
struct bpf_cgroup_link *link;
845-
struct file *link_file;
846846
struct cgroup *cgrp;
847-
int err, link_fd;
847+
int err;
848848

849849
if (attr->link_create.flags)
850850
return -EINVAL;
@@ -862,22 +862,20 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
862862
link->cgroup = cgrp;
863863
link->type = attr->link_create.attach_type;
864864

865-
link_file = bpf_link_new_file(&link->link, &link_fd);
866-
if (IS_ERR(link_file)) {
865+
err = bpf_link_prime(&link->link, &link_primer);
866+
if (err) {
867867
kfree(link);
868-
err = PTR_ERR(link_file);
869868
goto out_put_cgroup;
870869
}
871870

872871
err = cgroup_bpf_attach(cgrp, NULL, NULL, link, link->type,
873872
BPF_F_ALLOW_MULTI);
874873
if (err) {
875-
bpf_link_cleanup(&link->link, link_file, link_fd);
874+
bpf_link_cleanup(&link_primer);
876875
goto out_put_cgroup;
877876
}
878877

879-
fd_install(link_fd, link_file);
880-
return link_fd;
878+
return bpf_link_settle(&link_primer);
881879

882880
out_put_cgroup:
883881
cgroup_put(cgrp);

‎kernel/bpf/syscall.c‎

Lines changed: 98 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ static DEFINE_IDR(prog_idr);
4242
static DEFINE_SPINLOCK(prog_idr_lock);
4343
static DEFINE_IDR(map_idr);
4444
static DEFINE_SPINLOCK(map_idr_lock);
45+
static DEFINE_IDR(link_idr);
46+
static DEFINE_SPINLOCK(link_idr_lock);
4547

4648
int sysctl_unprivileged_bpf_disabled __read_mostly;
4749

@@ -2181,25 +2183,38 @@ static int bpf_obj_get(const union bpf_attr *attr)
21812183
attr->file_flags);
21822184
}
21832185

2184-
void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
2185-
struct bpf_prog *prog)
2186+
void bpf_link_init(struct bpf_link *link,
2187+
const struct bpf_link_ops *ops, struct bpf_prog *prog)
21862188
{
21872189
atomic64_set(&link->refcnt, 1);
2190+
link->id = 0;
21882191
link->ops = ops;
21892192
link->prog = prog;
21902193
}
21912194

2195+
static void bpf_link_free_id(int id)
2196+
{
2197+
if (!id)
2198+
return;
2199+
2200+
spin_lock_bh(&link_idr_lock);
2201+
idr_remove(&link_idr, id);
2202+
spin_unlock_bh(&link_idr_lock);
2203+
}
2204+
21922205
/* Clean up bpf_link and corresponding anon_inode file and FD. After
21932206
* anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2194-
* anon_inode's release() call. This helper manages marking bpf_link as
2195-
* defunct, releases anon_inode file and puts reserved FD.
2207+
* anon_inode's release() call. This helper marksbpf_link as
2208+
* defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2209+
* is not decremented, it's the responsibility of a calling code that failed
2210+
* to complete bpf_link initialization.
21962211
*/
2197-
void bpf_link_cleanup(struct bpf_link *link, struct file *link_file,
2198-
int link_fd)
2212+
void bpf_link_cleanup(struct bpf_link_primer *primer)
21992213
{
2200-
link->prog = NULL;
2201-
fput(link_file);
2202-
put_unused_fd(link_fd);
2214+
primer->link->prog = NULL;
2215+
bpf_link_free_id(primer->id);
2216+
fput(primer->file);
2217+
put_unused_fd(primer->fd);
22032218
}
22042219

22052220
void bpf_link_inc(struct bpf_link *link)
@@ -2210,6 +2225,7 @@ void bpf_link_inc(struct bpf_link *link)
22102225
/* bpf_link_free is guaranteed to be called from process context */
22112226
static void bpf_link_free(struct bpf_link *link)
22122227
{
2228+
bpf_link_free_id(link->id);
22132229
if (link->prog) {
22142230
/* detach BPF program, clean up used resources */
22152231
link->ops->release(link);
@@ -2275,9 +2291,11 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
22752291
bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
22762292
seq_printf(m,
22772293
"link_type:\t%s\n"
2294+
"link_id:\t%u\n"
22782295
"prog_tag:\t%s\n"
22792296
"prog_id:\t%u\n",
22802297
link_type,
2298+
link->id,
22812299
prog_tag,
22822300
prog->aux->id);
22832301
}
@@ -2292,36 +2310,76 @@ static const struct file_operations bpf_link_fops = {
22922310
.write = bpf_dummy_write,
22932311
};
22942312

2295-
int bpf_link_new_fd(struct bpf_link *link)
2313+
static int bpf_link_alloc_id(struct bpf_link *link)
22962314
{
2297-
return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2298-
}
2315+
int id;
2316+
2317+
idr_preload(GFP_KERNEL);
2318+
spin_lock_bh(&link_idr_lock);
2319+
id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2320+
spin_unlock_bh(&link_idr_lock);
2321+
idr_preload_end();
22992322

2300-
/* Similar to bpf_link_new_fd, create anon_inode for given bpf_link, but
2301-
* instead of immediately installing fd in fdtable, just reserve it and
2302-
* return. Caller then need to either install it with fd_install(fd, file) or
2303-
* release with put_unused_fd(fd).
2304-
* This is useful for cases when bpf_link attachment/detachment are
2305-
* complicated and expensive operations and should be delayed until all the fd
2306-
* reservation and anon_inode creation succeeds.
2323+
return id;
2324+
}
2325+
2326+
/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2327+
* reserving unused FD and allocating ID from link_idr. This is to be paired
2328+
* with bpf_link_settle() to install FD and ID and expose bpf_link to
2329+
* user-space, if bpf_link is successfully attached. If not, bpf_link and
2330+
* pre-allocated resources are to be freed with bpf_cleanup() call. All the
2331+
* transient state is passed around in struct bpf_link_primer.
2332+
* This is preferred way to create and initialize bpf_link, especially when
2333+
* there are complicated and expensive operations inbetween creating bpf_link
2334+
* itself and attaching it to BPF hook. By using bpf_link_prime() and
2335+
* bpf_link_settle() kernel code using bpf_link doesn't have to perform
2336+
* expensive (and potentially failing) roll back operations in a rare case
2337+
* that file, FD, or ID can't be allocated.
23072338
*/
2308-
struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd)
2339+
int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
23092340
{
23102341
struct file *file;
2311-
int fd;
2342+
int fd, id;
23122343

23132344
fd = get_unused_fd_flags(O_CLOEXEC);
23142345
if (fd < 0)
2315-
return ERR_PTR(fd);
2346+
return fd;
23162347

23172348
file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
23182349
if (IS_ERR(file)) {
23192350
put_unused_fd(fd);
2320-
return file;
2351+
return PTR_ERR(file);
23212352
}
23222353

2323-
*reserved_fd = fd;
2324-
return file;
2354+
id = bpf_link_alloc_id(link);
2355+
if (id < 0) {
2356+
put_unused_fd(fd);
2357+
fput(file);
2358+
return id;
2359+
}
2360+
2361+
primer->link = link;
2362+
primer->file = file;
2363+
primer->fd = fd;
2364+
primer->id = id;
2365+
return 0;
2366+
}
2367+
2368+
int bpf_link_settle(struct bpf_link_primer *primer)
2369+
{
2370+
/* make bpf_link fetchable by ID */
2371+
spin_lock_bh(&link_idr_lock);
2372+
primer->link->id = primer->id;
2373+
spin_unlock_bh(&link_idr_lock);
2374+
/* make bpf_link fetchable by FD */
2375+
fd_install(primer->fd, primer->file);
2376+
/* pass through installed FD */
2377+
return primer->fd;
2378+
}
2379+
2380+
int bpf_link_new_fd(struct bpf_link *link)
2381+
{
2382+
return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
23252383
}
23262384

23272385
struct bpf_link *bpf_link_get_from_fd(u32 ufd)
@@ -2367,9 +2425,9 @@ static const struct bpf_link_ops bpf_tracing_link_lops = {
23672425

23682426
static int bpf_tracing_prog_attach(struct bpf_prog *prog)
23692427
{
2428+
struct bpf_link_primer link_primer;
23702429
struct bpf_tracing_link *link;
2371-
struct file *link_file;
2372-
int link_fd, err;
2430+
int err;
23732431

23742432
switch (prog->type) {
23752433
case BPF_PROG_TYPE_TRACING:
@@ -2404,22 +2462,19 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog)
24042462
}
24052463
bpf_link_init(&link->link, &bpf_tracing_link_lops, prog);
24062464

2407-
link_file = bpf_link_new_file(&link->link, &link_fd);
2408-
if (IS_ERR(link_file)) {
2465+
err = bpf_link_prime(&link->link, &link_primer);
2466+
if (err) {
24092467
kfree(link);
2410-
err = PTR_ERR(link_file);
24112468
goto out_put_prog;
24122469
}
24132470

24142471
err = bpf_trampoline_link_prog(prog);
24152472
if (err) {
2416-
bpf_link_cleanup(&link->link, link_file, link_fd);
2473+
bpf_link_cleanup(&link_primer);
24172474
goto out_put_prog;
24182475
}
24192476

2420-
fd_install(link_fd, link_file);
2421-
return link_fd;
2422-
2477+
return bpf_link_settle(&link_primer);
24232478
out_put_prog:
24242479
bpf_prog_put(prog);
24252480
return err;
@@ -2447,7 +2502,7 @@ static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
24472502
kfree(raw_tp);
24482503
}
24492504

2450-
static const struct bpf_link_ops bpf_raw_tp_lops = {
2505+
static const struct bpf_link_ops bpf_raw_tp_link_lops = {
24512506
.release = bpf_raw_tp_link_release,
24522507
.dealloc = bpf_raw_tp_link_dealloc,
24532508
};
@@ -2456,13 +2511,13 @@ static const struct bpf_link_ops bpf_raw_tp_lops = {
24562511

24572512
static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
24582513
{
2514+
struct bpf_link_primer link_primer;
24592515
struct bpf_raw_tp_link *link;
24602516
struct bpf_raw_event_map *btp;
2461-
struct file *link_file;
24622517
struct bpf_prog *prog;
24632518
const char *tp_name;
24642519
char buf[128];
2465-
int link_fd, err;
2520+
int err;
24662521

24672522
if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
24682523
return -EINVAL;
@@ -2515,24 +2570,22 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
25152570
err = -ENOMEM;
25162571
goto out_put_btp;
25172572
}
2518-
bpf_link_init(&link->link, &bpf_raw_tp_lops, prog);
2573+
bpf_link_init(&link->link, &bpf_raw_tp_link_lops, prog);
25192574
link->btp = btp;
25202575

2521-
link_file = bpf_link_new_file(&link->link, &link_fd);
2522-
if (IS_ERR(link_file)) {
2576+
err = bpf_link_prime(&link->link, &link_primer);
2577+
if (err) {
25232578
kfree(link);
2524-
err = PTR_ERR(link_file);
25252579
goto out_put_btp;
25262580
}
25272581

25282582
err = bpf_probe_register(link->btp, prog);
25292583
if (err) {
2530-
bpf_link_cleanup(&link->link, link_file, link_fd);
2584+
bpf_link_cleanup(&link_primer);
25312585
goto out_put_btp;
25322586
}
25332587

2534-
fd_install(link_fd, link_file);
2535-
return link_fd;
2588+
return bpf_link_settle(&link_primer);
25362589

25372590
out_put_btp:
25382591
bpf_put_raw_tracepoint(btp);
@@ -3464,7 +3517,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
34643517
if (file->f_op == &bpf_link_fops) {
34653518
struct bpf_link *link = file->private_data;
34663519

3467-
if (link->ops == &bpf_raw_tp_lops) {
3520+
if (link->ops == &bpf_raw_tp_link_lops) {
34683521
struct bpf_raw_tp_link *raw_tp =
34693522
container_of(link, struct bpf_raw_tp_link, link);
34703523
struct bpf_raw_event_map *btp = raw_tp->btp;

0 commit comments

Comments
 (0)