Skip to content

Commit f9d0412

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Refactor bpf_link update handling
Make bpf_link update support more generic by making it into another bpf_link_ops methods. This allows generic syscall handling code to be agnostic to various conditionally compiled features (e.g., the case of CONFIG_CGROUP_BPF). This also allows to keep link type-specific code to remain static within respective code base. Refactor existing bpf_cgroup_link code and take advantage of this. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200429001614.1544-2-andriin@fb.com
1 parent 9b329d0 commit f9d0412

5 files changed

Lines changed: 34 additions & 49 deletions

File tree

‎include/linux/bpf-cgroup.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ 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);
105103
int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
106104
union bpf_attr __user *uattr);
107105

@@ -112,8 +110,6 @@ int cgroup_bpf_attach(struct cgroup *cgrp,
112110
u32 flags);
113111
int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
114112
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);
117113
int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
118114
union bpf_attr __user *uattr);
119115

@@ -353,7 +349,6 @@ int cgroup_bpf_prog_query(const union bpf_attr *attr,
353349
#else
354350

355351
struct bpf_prog;
356-
struct bpf_link;
357352
struct cgroup_bpf {};
358353
static inline int cgroup_bpf_inherit(struct cgroup *cgrp) { return 0; }
359354
static inline void cgroup_bpf_offline(struct cgroup *cgrp) {}
@@ -377,13 +372,6 @@ static inline int cgroup_bpf_link_attach(const union bpf_attr *attr,
377372
return -EINVAL;
378373
}
379374

380-
static inline int cgroup_bpf_replace(struct bpf_link *link,
381-
struct bpf_prog *old_prog,
382-
struct bpf_prog *new_prog)
383-
{
384-
return -EINVAL;
385-
}
386-
387375
static inline int cgroup_bpf_prog_query(const union bpf_attr *attr,
388376
union bpf_attr __user *uattr)
389377
{

‎include/linux/bpf.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,8 @@ struct bpf_link {
10931093
struct bpf_link_ops {
10941094
void (*release)(struct bpf_link *link);
10951095
void (*dealloc)(struct bpf_link *link);
1096-
1096+
int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1097+
struct bpf_prog *old_prog);
10971098
};
10981099

10991100
void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,

‎kernel/bpf/cgroup.c‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,9 @@ static void replace_effective_prog(struct cgroup *cgrp,
557557
*
558558
* Must be called with cgroup_mutex held.
559559
*/
560-
int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
561-
struct bpf_prog *new_prog)
560+
static int __cgroup_bpf_replace(struct cgroup *cgrp,
561+
struct bpf_cgroup_link *link,
562+
struct bpf_prog *new_prog)
562563
{
563564
struct list_head *progs = &cgrp->bpf.progs[link->type];
564565
struct bpf_prog *old_prog;
@@ -583,6 +584,30 @@ int __cgroup_bpf_replace(struct cgroup *cgrp, struct bpf_cgroup_link *link,
583584
return 0;
584585
}
585586

587+
static int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *new_prog,
588+
struct bpf_prog *old_prog)
589+
{
590+
struct bpf_cgroup_link *cg_link;
591+
int ret;
592+
593+
cg_link = container_of(link, struct bpf_cgroup_link, link);
594+
595+
mutex_lock(&cgroup_mutex);
596+
/* link might have been auto-released by dying cgroup, so fail */
597+
if (!cg_link->cgroup) {
598+
ret = -EINVAL;
599+
goto out_unlock;
600+
}
601+
if (old_prog && link->prog != old_prog) {
602+
ret = -EPERM;
603+
goto out_unlock;
604+
}
605+
ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
606+
out_unlock:
607+
mutex_unlock(&cgroup_mutex);
608+
return ret;
609+
}
610+
586611
static struct bpf_prog_list *find_detach_entry(struct list_head *progs,
587612
struct bpf_prog *prog,
588613
struct bpf_cgroup_link *link,
@@ -811,6 +836,7 @@ static void bpf_cgroup_link_dealloc(struct bpf_link *link)
811836
const struct bpf_link_ops bpf_cgroup_link_lops = {
812837
.release = bpf_cgroup_link_release,
813838
.dealloc = bpf_cgroup_link_dealloc,
839+
.update_prog = cgroup_bpf_replace,
814840
};
815841

816842
int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)

‎kernel/bpf/syscall.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,13 +3645,10 @@ static int link_update(union bpf_attr *attr)
36453645
goto out_put_progs;
36463646
}
36473647

3648-
#ifdef CONFIG_CGROUP_BPF
3649-
if (link->ops == &bpf_cgroup_link_lops) {
3650-
ret = cgroup_bpf_replace(link, old_prog, new_prog);
3651-
goto out_put_progs;
3652-
}
3653-
#endif
3654-
ret = -EINVAL;
3648+
if (link->ops->update_prog)
3649+
ret = link->ops->update_prog(link, new_prog, old_prog);
3650+
else
3651+
ret = EINVAL;
36553652

36563653
out_put_progs:
36573654
if (old_prog)

‎kernel/cgroup/cgroup.c‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6508,33 +6508,6 @@ int cgroup_bpf_attach(struct cgroup *cgrp,
65086508
return ret;
65096509
}
65106510

6511-
int cgroup_bpf_replace(struct bpf_link *link, struct bpf_prog *old_prog,
6512-
struct bpf_prog *new_prog)
6513-
{
6514-
struct bpf_cgroup_link *cg_link;
6515-
int ret;
6516-
6517-
if (link->ops != &bpf_cgroup_link_lops)
6518-
return -EINVAL;
6519-
6520-
cg_link = container_of(link, struct bpf_cgroup_link, link);
6521-
6522-
mutex_lock(&cgroup_mutex);
6523-
/* link might have been auto-released by dying cgroup, so fail */
6524-
if (!cg_link->cgroup) {
6525-
ret = -EINVAL;
6526-
goto out_unlock;
6527-
}
6528-
if (old_prog && link->prog != old_prog) {
6529-
ret = -EPERM;
6530-
goto out_unlock;
6531-
}
6532-
ret = __cgroup_bpf_replace(cg_link->cgroup, cg_link, new_prog);
6533-
out_unlock:
6534-
mutex_unlock(&cgroup_mutex);
6535-
return ret;
6536-
}
6537-
65386511
int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
65396512
enum bpf_attach_type type)
65406513
{

0 commit comments

Comments
 (0)