Skip to content

Commit 73b11c2

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: Add support for forced LINK_DETACH command
Add LINK_DETACH command to force-detach bpf_link without destroying it. It has the same behavior as auto-detaching of bpf_link due to cgroup dying for bpf_cgroup_link or net_device being destroyed for bpf_xdp_link. In such case, bpf_link is still a valid kernel object, but is defuncts and doesn't hold BPF program attached to corresponding BPF hook. This functionality allows users with enough access rights to manually force-detach attached bpf_link without killing respective owner process. This patch implements LINK_DETACH for cgroup, xdp, and netns links, mostly re-using existing link release handling code. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Song Liu <songliubraving@fb.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200731182830.286260-2-andriin@fb.com
1 parent 4939b28 commit 73b11c2

6 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ struct bpf_link {
793793
struct bpf_link_ops {
794794
void (*release)(struct bpf_link *link);
795795
void (*dealloc)(struct bpf_link *link);
796+
int (*detach)(struct bpf_link *link);
796797
int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
797798
struct bpf_prog *old_prog);
798799
void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);

‎include/uapi/linux/bpf.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ enum bpf_cmd {
117117
BPF_LINK_GET_NEXT_ID,
118118
BPF_ENABLE_STATS,
119119
BPF_ITER_CREATE,
120+
BPF_LINK_DETACH,
120121
};
121122

122123
enum bpf_map_type {
@@ -634,6 +635,10 @@ union bpf_attr {
634635
__u32 old_prog_fd;
635636
} link_update;
636637

638+
struct {
639+
__u32 link_fd;
640+
} link_detach;
641+
637642
struct { /* struct used by BPF_ENABLE_STATS command */
638643
__u32 type;
639644
} enable_stats;

‎kernel/bpf/cgroup.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
814814
{
815815
struct bpf_cgroup_link *cg_link =
816816
container_of(link, struct bpf_cgroup_link, link);
817+
struct cgroup *cg;
817818

818819
/* link might have been auto-detached by dying cgroup already,
819820
* in that case our work is done here
@@ -832,8 +833,12 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
832833
WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
833834
cg_link->type));
834835

836+
cg = cg_link->cgroup;
837+
cg_link->cgroup = NULL;
838+
835839
mutex_unlock(&cgroup_mutex);
836-
cgroup_put(cg_link->cgroup);
840+
841+
cgroup_put(cg);
837842
}
838843

839844
static void bpf_cgroup_link_dealloc(struct bpf_link *link)
@@ -844,6 +849,13 @@ static void bpf_cgroup_link_dealloc(struct bpf_link *link)
844849
kfree(cg_link);
845850
}
846851

852+
static int bpf_cgroup_link_detach(struct bpf_link *link)
853+
{
854+
bpf_cgroup_link_release(link);
855+
856+
return 0;
857+
}
858+
847859
static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
848860
struct seq_file *seq)
849861
{
@@ -883,6 +895,7 @@ static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
883895
static const struct bpf_link_ops bpf_cgroup_link_lops = {
884896
.release = bpf_cgroup_link_release,
885897
.dealloc = bpf_cgroup_link_dealloc,
898+
.detach = bpf_cgroup_link_detach,
886899
.update_prog = cgroup_bpf_replace,
887900
.show_fdinfo = bpf_cgroup_link_show_fdinfo,
888901
.fill_link_info = bpf_cgroup_link_fill_link_info,

‎kernel/bpf/net_namespace.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,16 @@ static void bpf_netns_link_release(struct bpf_link *link)
142142
bpf_prog_array_free(old_array);
143143

144144
out_unlock:
145+
net_link->net = NULL;
145146
mutex_unlock(&netns_bpf_mutex);
146147
}
147148

149+
static int bpf_netns_link_detach(struct bpf_link *link)
150+
{
151+
bpf_netns_link_release(link);
152+
return 0;
153+
}
154+
148155
static void bpf_netns_link_dealloc(struct bpf_link *link)
149156
{
150157
struct bpf_netns_link *net_link =
@@ -228,6 +235,7 @@ static void bpf_netns_link_show_fdinfo(const struct bpf_link *link,
228235
static const struct bpf_link_ops bpf_netns_link_ops = {
229236
.release = bpf_netns_link_release,
230237
.dealloc = bpf_netns_link_dealloc,
238+
.detach = bpf_netns_link_detach,
231239
.update_prog = bpf_netns_link_update_prog,
232240
.fill_link_info = bpf_netns_link_fill_info,
233241
.show_fdinfo = bpf_netns_link_show_fdinfo,

‎kernel/bpf/syscall.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,6 +3991,29 @@ static int link_update(union bpf_attr *attr)
39913991
return ret;
39923992
}
39933993

3994+
#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
3995+
3996+
static int link_detach(union bpf_attr *attr)
3997+
{
3998+
struct bpf_link *link;
3999+
int ret;
4000+
4001+
if (CHECK_ATTR(BPF_LINK_DETACH))
4002+
return -EINVAL;
4003+
4004+
link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4005+
if (IS_ERR(link))
4006+
return PTR_ERR(link);
4007+
4008+
if (link->ops->detach)
4009+
ret = link->ops->detach(link);
4010+
else
4011+
ret = -EOPNOTSUPP;
4012+
4013+
bpf_link_put(link);
4014+
return ret;
4015+
}
4016+
39944017
static int bpf_link_inc_not_zero(struct bpf_link *link)
39954018
{
39964019
return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT;
@@ -4240,6 +4263,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
42404263
case BPF_ITER_CREATE:
42414264
err = bpf_iter_create(&attr);
42424265
break;
4266+
case BPF_LINK_DETACH:
4267+
err = link_detach(&attr);
4268+
break;
42434269
default:
42444270
err = -EINVAL;
42454271
break;

‎net/core/dev.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8979,12 +8979,20 @@ static void bpf_xdp_link_release(struct bpf_link *link)
89798979
/* if racing with net_device's tear down, xdp_link->dev might be
89808980
* already NULL, in which case link was already auto-detached
89818981
*/
8982-
if (xdp_link->dev)
8982+
if (xdp_link->dev) {
89838983
WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
8984+
xdp_link->dev = NULL;
8985+
}
89848986

89858987
rtnl_unlock();
89868988
}
89878989

8990+
static int bpf_xdp_link_detach(struct bpf_link *link)
8991+
{
8992+
bpf_xdp_link_release(link);
8993+
return 0;
8994+
}
8995+
89888996
static void bpf_xdp_link_dealloc(struct bpf_link *link)
89898997
{
89908998
struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
@@ -9066,6 +9074,7 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
90669074
static const struct bpf_link_ops bpf_xdp_link_lops = {
90679075
.release = bpf_xdp_link_release,
90689076
.dealloc = bpf_xdp_link_dealloc,
9077+
.detach = bpf_xdp_link_detach,
90699078
.show_fdinfo = bpf_xdp_link_show_fdinfo,
90709079
.fill_link_info = bpf_xdp_link_fill_link_info,
90719080
.update_prog = bpf_xdp_link_update,

0 commit comments

Comments
 (0)