Skip to content

Commit 0c991eb

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Implement bpf_prog replacement for an active bpf_cgroup_link
Add new operation (LINK_UPDATE), which allows to replace active bpf_prog from under given bpf_link. Currently this is only supported for bpf_cgroup_link, but will be extended to other kinds of bpf_links in follow-up patches. For bpf_cgroup_link, implemented functionality matches existing semantics for direct bpf_prog attachment (including BPF_F_REPLACE flag). User can either unconditionally set new bpf_prog regardless of which bpf_prog is currently active under given bpf_link, or, optionally, can specify expected active bpf_prog. If active bpf_prog doesn't match expected one, no changes are performed, old bpf_link stays intact and attached, operation returns a failure. cgroup_bpf_replace() operation is resolving race between auto-detachment and bpf_prog update in the same fashion as it's done for bpf_link detachment, except in this case update has no way of succeeding because of target cgroup marked as dying. So in this case error is returned. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200330030001.2312810-3-andriin@fb.com
1 parent af6eea5 commit 0c991eb

5 files changed

Lines changed: 186 additions & 0 deletions

File tree

‎include/linux/bpf-cgroup.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ int __cgroup_bpf_attach(struct cgroup *cgrp,
100100
int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
101101
struct bpf_cgroup_link *link,
102102
enum bpf_attach_type type);
103+
int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
104+
struct bpf_prog *new_prog);
103105
int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
104106
union bpf_attr __user *uattr);
105107

@@ -110,6 +112,8 @@ int cgroup_bpf_attach(struct cgroup *cgrp,
110112
u32 flags);
111113
int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
112114
enum bpf_attach_type type);
115+
int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *old_prog,
116+
struct bpf_prog *new_prog);
113117
int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
114118
union bpf_attr __user *uattr);
115119

@@ -350,6 +354,7 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr,
350354
#else
351355

352356
struct bpf_prog;
357+
struct bpf_link;
353358
struct cgroup_bpf {};
354359
static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
355360
static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
@@ -373,6 +378,13 @@ static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
373378
return -EINVAL;
374379
}
375380

381+
static inline int cgroup_bpf_replace(struct bpf_link *link,
382+
struct bpf_prog *old_prog,
383+
struct bpf_prog *new_prog)
384+
{
385+
return -EINVAL;
386+
}
387+
376388
static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
377389
union bpf_attr __user *uattr)
378390
{

‎include/uapi/linux/bpf.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ enum bpf_cmd {
112112
BPF_MAP_UPDATE_BATCH,
113113
BPF_MAP_DELETE_BATCH,
114114
BPF_LINK_CREATE,
115+
BPF_LINK_UPDATE,
115116
};
116117

117118
enum bpf_map_type {
@@ -577,6 +578,17 @@ union bpf_attr {
577578
__u32 attach_type; /* attach type */
578579
__u32 flags; /* extra flags */
579580
} link_create;
581+
582+
struct { /* struct used by BPF_LINK_UPDATE command */
583+
__u32 link_fd; /* link fd */
584+
/* new program fd to update link with */
585+
__u32 new_prog_fd;
586+
__u32 flags; /* extra flags */
587+
/* expected link's program fd; is specified only if
588+
* BPF_F_REPLACE flag is set in flags */
589+
__u32 old_prog_fd;
590+
} link_update;
591+
580592
} __attribute__((aligned(8)));
581593

582594
/* The description below is an attempt at providing documentation to eBPF

‎kernel/bpf/cgroup.c‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,86 @@ int __cgroup_bpf_attach(struct cgroup *cgrp,
500500
return err;
501501
}
502502

503+
/* Swap updated BPF program for given link in effective program arrays across
504+
* all descendant cgroups. This function is guaranteed to succeed.
505+
*/
506+
static void replace_effective_prog(struct cgroup *cgrp,
507+
enum bpf_attach_type type,
508+
struct bpf_cgroup_link *link)
509+
{
510+
struct bpf_prog_array_item *item;
511+
struct cgroup_subsys_state *css;
512+
struct bpf_prog_array *progs;
513+
struct bpf_prog_list *pl;
514+
struct list_head *head;
515+
struct cgroup *cg;
516+
int pos;
517+
518+
css_for_each_descendant_pre(css, &cgrp->self) {
519+
struct cgroup *desc = container_of(css, struct cgroup, self);
520+
521+
if (percpu_ref_is_zero(&desc->bpf.refcnt))
522+
continue;
523+
524+
/* find position of link in effective progs array */
525+
for (pos = 0, cg = desc; cg; cg = cgroup_parent(cg)) {
526+
if (pos && !(cg->bpf.flags[type] & BPF_F_ALLOW_MULTI))
527+
continue;
528+
529+
head = &cg->bpf.progs[type];
530+
list_for_each_entry(pl, head, node) {
531+
if (!prog_list_prog(pl))
532+
continue;
533+
if (pl->link == link)
534+
goto found;
535+
pos++;
536+
}
537+
}
538+
found:
539+
BUG_ON(!cg);
540+
progs = rcu_dereference_protected(
541+
desc->bpf.effective[type],
542+
lockdep_is_held(&cgroup_mutex));
543+
item = &progs->items[pos];
544+
WRITE_ONCE(item->prog, link->link.prog);
545+
}
546+
}
547+
548+
/**
549+
* __cgroup_bpf_replace() - Replace link's program and propagate the change
550+
* to descendants
551+
* @cgrp: The cgroup which descendants to traverse
552+
* @link: A link for which to replace BPF program
553+
* @type: Type of attach operation
554+
*
555+
* Must be called with cgroup_mutex held.
556+
*/
557+
int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
558+
struct bpf_prog *new_prog)
559+
{
560+
struct list_head *progs = &cgrp->bpf.progs[link->type];
561+
struct bpf_prog *old_prog;
562+
struct bpf_prog_list *pl;
563+
bool found = false;
564+
565+
if (link->link.prog->type != new_prog->type)
566+
return -EINVAL;
567+
568+
list_for_each_entry(pl, progs, node) {
569+
if (pl->link == link) {
570+
found = true;
571+
break;
572+
}
573+
}
574+
if (!found)
575+
return -ENOENT;
576+
577+
old_prog = xchg(&link->link.prog, new_prog);
578+
replace_effective_prog(cgrp, link->type, link);
579+
bpf_prog_put(old_prog);
580+
return 0;
581+
}
582+
503583
static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
504584
struct bpf_prog *prog,
505585
struct bpf_cgroup_link *link,

‎kernel/bpf/syscall.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,6 +3596,58 @@ static int link_create(union bpf_attr *attr)
35963596
return ret;
35973597
}
35983598

3599+
#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
3600+
3601+
static int link_update(union bpf_attr *attr)
3602+
{
3603+
struct bpf_prog *old_prog = NULL, *new_prog;
3604+
struct bpf_link *link;
3605+
u32 flags;
3606+
int ret;
3607+
3608+
if (!capable(CAP_NET_ADMIN))
3609+
return -EPERM;
3610+
3611+
if (CHECK_ATTR(BPF_LINK_UPDATE))
3612+
return -EINVAL;
3613+
3614+
flags = attr->link_update.flags;
3615+
if (flags & ~BPF_F_REPLACE)
3616+
return -EINVAL;
3617+
3618+
link = bpf_link_get_from_fd(attr->link_update.link_fd);
3619+
if (IS_ERR(link))
3620+
return PTR_ERR(link);
3621+
3622+
new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
3623+
if (IS_ERR(new_prog))
3624+
return PTR_ERR(new_prog);
3625+
3626+
if (flags & BPF_F_REPLACE) {
3627+
old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
3628+
if (IS_ERR(old_prog)) {
3629+
ret = PTR_ERR(old_prog);
3630+
old_prog = NULL;
3631+
goto out_put_progs;
3632+
}
3633+
}
3634+
3635+
#ifdef CONFIG_CGROUP_BPF
3636+
if (link->ops == &bpf_cgroup_link_lops) {
3637+
ret = cgroup_bpf_replace(link, old_prog, new_prog);
3638+
goto out_put_progs;
3639+
}
3640+
#endif
3641+
ret = -EINVAL;
3642+
3643+
out_put_progs:
3644+
if (old_prog)
3645+
bpf_prog_put(old_prog);
3646+
if (ret)
3647+
bpf_prog_put(new_prog);
3648+
return ret;
3649+
}
3650+
35993651
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
36003652
{
36013653
union bpf_attr attr = {};
@@ -3709,6 +3761,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
37093761
case BPF_LINK_CREATE:
37103762
err = link_create(&attr);
37113763
break;
3764+
case BPF_LINK_UPDATE:
3765+
err = link_update(&attr);
3766+
break;
37123767
default:
37133768
err = -EINVAL;
37143769
break;

‎kernel/cgroup/cgroup.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6317,6 +6317,33 @@ int cgroup_bpf_attach(struct cgroup *cgrp,
63176317
return ret;
63186318
}
63196319

6320+
int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *old_prog,
6321+
struct bpf_prog *new_prog)
6322+
{
6323+
struct bpf_cgroup_link *cg_link;
6324+
int ret;
6325+
6326+
if (link->ops != &bpf_cgroup_link_lops)
6327+
return -EINVAL;
6328+
6329+
cg_link = container_of(link, struct bpf_cgroup_link, link);
6330+
6331+
mutex_lock(&cgroup_mutex);
6332+
/* link might have been auto-released by dying cgroup, so fail */
6333+
if (!cg_link->cgroup) {
6334+
ret = -EINVAL;
6335+
goto out_unlock;
6336+
}
6337+
if (old_prog && link->prog != old_prog) {
6338+
ret = -EPERM;
6339+
goto out_unlock;
6340+
}
6341+
ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
6342+
out_unlock:
6343+
mutex_unlock(&cgroup_mutex);
6344+
return ret;
6345+
}
6346+
63206347
int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
63216348
enum bpf_attach_type type)
63226349
{

0 commit comments

Comments
 (0)