Skip to content

Commit eeec23a

Browse files
committed
Merge branch 'move-attach_type-into-bpf_link'
Tao Chen says: ==================== Move attach_type into bpf_link Andrii suggested moving the attach_type into bpf_link, the previous discussion is as follows: https://lore.kernel.org/bpf/CAEf4BzY7TZRjxpCJM-+LYgEqe23YFj5Uv3isb7gat2-HU4OSng@mail.gmail.com patch1 add attach_type in bpf_link, and pass it to bpf_link_init, which will init the attach_type field. patch2-7 remove the attach_type in struct bpf_xx_link, update the info with bpf_link attach_type. There are some functions finally call bpf_link_init but do not have bpf_attr from user or do not need to init attach_type from user like bpf_raw_tracepoint_open, now use prog->expected_attach_type to init attach_type. bpf_struct_ops_map_update_elem bpf_raw_tracepoint_open bpf_struct_ops_test_run Feedback of any kind is welcome, thanks. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents d81526a + 4254873 commit eeec23a

File tree

16 files changed

+90
-78
lines changed

16 files changed

+90
-78
lines changed

drivers/net/netkit.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ struct netkit {
3232
struct netkit_link {
3333
struct bpf_link link;
3434
struct net_device *dev;
35-
u32 location;
3635
};
3736

3837
static __always_inline int
@@ -733,8 +732,8 @@ static void netkit_link_fdinfo(const struct bpf_link *link, struct seq_file *seq
733732

734733
seq_printf(seq, "ifindex:\t%u\n", ifindex);
735734
seq_printf(seq, "attach_type:\t%u (%s)\n",
736-
nkl->location,
737-
nkl->location == BPF_NETKIT_PRIMARY ? "primary" : "peer");
735+
link->attach_type,
736+
link->attach_type == BPF_NETKIT_PRIMARY ? "primary" : "peer");
738737
}
739738

740739
static int netkit_link_fill_info(const struct bpf_link *link,
@@ -749,7 +748,7 @@ static int netkit_link_fill_info(const struct bpf_link *link,
749748
rtnl_unlock();
750749

751750
info->netkit.ifindex = ifindex;
752-
info->netkit.attach_type = nkl->location;
751+
info->netkit.attach_type = link->attach_type;
753752
return 0;
754753
}
755754

@@ -775,8 +774,7 @@ static int netkit_link_init(struct netkit_link *nkl,
775774
struct bpf_prog *prog)
776775
{
777776
bpf_link_init(&nkl->link, BPF_LINK_TYPE_NETKIT,
778-
&netkit_link_lops, prog);
779-
nkl->location = attr->link_create.attach_type;
777+
&netkit_link_lops, prog, attr->link_create.attach_type);
780778
nkl->dev = dev;
781779
return bpf_link_prime(&nkl->link, link_primer);
782780
}

include/linux/bpf-cgroup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ struct bpf_cgroup_storage {
103103
struct bpf_cgroup_link {
104104
struct bpf_link link;
105105
struct cgroup *cgroup;
106-
enum bpf_attach_type type;
107106
};
108107

109108
struct bpf_prog_list {

include/linux/bpf.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,19 +1729,22 @@ struct bpf_link {
17291729
enum bpf_link_type type;
17301730
const struct bpf_link_ops *ops;
17311731
struct bpf_prog *prog;
1732-
/* whether BPF link itself has "sleepable" semantics, which can differ
1733-
* from underlying BPF program having a "sleepable" semantics, as BPF
1734-
* link's semantics is determined by target attach hook
1735-
*/
1736-
bool sleepable;
1732+
17371733
u32 flags;
1734+
enum bpf_attach_type attach_type;
1735+
17381736
/* rcu is used before freeing, work can be used to schedule that
17391737
* RCU-based freeing before that, so they never overlap
17401738
*/
17411739
union {
17421740
struct rcu_head rcu;
17431741
struct work_struct work;
17441742
};
1743+
/* whether BPF link itself has "sleepable" semantics, which can differ
1744+
* from underlying BPF program having a "sleepable" semantics, as BPF
1745+
* link's semantics is determined by target attach hook
1746+
*/
1747+
bool sleepable;
17451748
};
17461749

17471750
struct bpf_link_ops {
@@ -1781,7 +1784,6 @@ struct bpf_shim_tramp_link {
17811784

17821785
struct bpf_tracing_link {
17831786
struct bpf_tramp_link link;
1784-
enum bpf_attach_type attach_type;
17851787
struct bpf_trampoline *trampoline;
17861788
struct bpf_prog *tgt_prog;
17871789
};
@@ -2034,11 +2036,13 @@ int bpf_prog_ctx_arg_info_init(struct bpf_prog *prog,
20342036

20352037
#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
20362038
int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
2037-
int cgroup_atype);
2039+
int cgroup_atype,
2040+
enum bpf_attach_type attach_type);
20382041
void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
20392042
#else
20402043
static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
2041-
int cgroup_atype)
2044+
int cgroup_atype,
2045+
enum bpf_attach_type attach_type)
20422046
{
20432047
return -EOPNOTSUPP;
20442048
}
@@ -2528,10 +2532,11 @@ int bpf_map_new_fd(struct bpf_map *map, int flags);
25282532
int bpf_prog_new_fd(struct bpf_prog *prog);
25292533

25302534
void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2531-
const struct bpf_link_ops *ops, struct bpf_prog *prog);
2535+
const struct bpf_link_ops *ops, struct bpf_prog *prog,
2536+
enum bpf_attach_type attach_type);
25322537
void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type,
25332538
const struct bpf_link_ops *ops, struct bpf_prog *prog,
2534-
bool sleepable);
2539+
enum bpf_attach_type attach_type, bool sleepable);
25352540
int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
25362541
int bpf_link_settle(struct bpf_link_primer *primer);
25372542
void bpf_link_cleanup(struct bpf_link_primer *primer);
@@ -2883,13 +2888,13 @@ bpf_prog_inc_not_zero(struct bpf_prog *prog)
28832888

28842889
static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
28852890
const struct bpf_link_ops *ops,
2886-
struct bpf_prog *prog)
2891+
struct bpf_prog *prog, enum bpf_attach_type attach_type)
28872892
{
28882893
}
28892894

28902895
static inline void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type,
28912896
const struct bpf_link_ops *ops, struct bpf_prog *prog,
2892-
bool sleepable)
2897+
enum bpf_attach_type attach_type, bool sleepable)
28932898
{
28942899
}
28952900

include/net/tcx.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ struct tcx_entry {
2020
struct tcx_link {
2121
struct bpf_link link;
2222
struct net_device *dev;
23-
u32 location;
2423
};
2524

2625
static inline void tcx_set_ingress(struct sk_buff *skb, bool ingress)

kernel/bpf/bpf_iter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
552552
if (!link)
553553
return -ENOMEM;
554554

555-
bpf_link_init(&link->link, BPF_LINK_TYPE_ITER, &bpf_iter_link_lops, prog);
555+
bpf_link_init(&link->link, BPF_LINK_TYPE_ITER, &bpf_iter_link_lops, prog,
556+
attr->link_create.attach_type);
556557
link->tinfo = tinfo;
557558

558559
err = bpf_link_prime(&link->link, &link_primer);

kernel/bpf/bpf_struct_ops.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
808808
goto reset_unlock;
809809
}
810810
bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS,
811-
&bpf_struct_ops_link_lops, prog);
811+
&bpf_struct_ops_link_lops, prog, prog->expected_attach_type);
812812
*plink++ = &link->link;
813813

814814
ksym = kzalloc(sizeof(*ksym), GFP_USER);
@@ -1351,7 +1351,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
13511351
err = -ENOMEM;
13521352
goto err_out;
13531353
}
1354-
bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL);
1354+
bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL,
1355+
attr->link_create.attach_type);
13551356

13561357
err = bpf_link_prime(&link->link, &link_primer);
13571358
if (err)

kernel/bpf/cgroup.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,
867867
cgrp->bpf.flags[atype] = saved_flags;
868868

869869
if (type == BPF_LSM_CGROUP) {
870-
err = bpf_trampoline_link_cgroup_shim(new_prog, atype);
870+
err = bpf_trampoline_link_cgroup_shim(new_prog, atype, type);
871871
if (err)
872872
goto cleanup;
873873
}
@@ -984,7 +984,7 @@ static int __cgroup_bpf_replace(struct cgroup *cgrp,
984984
struct hlist_head *progs;
985985
bool found = false;
986986

987-
atype = bpf_cgroup_atype_find(link->type, new_prog->aux->attach_btf_id);
987+
atype = bpf_cgroup_atype_find(link->link.attach_type, new_prog->aux->attach_btf_id);
988988
if (atype < 0)
989989
return -EINVAL;
990990

@@ -1396,8 +1396,8 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
13961396
}
13971397

13981398
WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
1399-
cg_link->type, 0));
1400-
if (cg_link->type == BPF_LSM_CGROUP)
1399+
link->attach_type, 0));
1400+
if (link->attach_type == BPF_LSM_CGROUP)
14011401
bpf_trampoline_unlink_cgroup_shim(cg_link->link.prog);
14021402

14031403
cg = cg_link->cgroup;
@@ -1439,7 +1439,7 @@ static void bpf_cgroup_link_show_fdinfo(const struct bpf_link *link,
14391439
"cgroup_id:\t%llu\n"
14401440
"attach_type:\t%d\n",
14411441
cg_id,
1442-
cg_link->type);
1442+
link->attach_type);
14431443
}
14441444

14451445
static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
@@ -1455,7 +1455,7 @@ static int bpf_cgroup_link_fill_link_info(const struct bpf_link *link,
14551455
cgroup_unlock();
14561456

14571457
info->cgroup.cgroup_id = cg_id;
1458-
info->cgroup.attach_type = cg_link->type;
1458+
info->cgroup.attach_type = link->attach_type;
14591459
return 0;
14601460
}
14611461

@@ -1495,9 +1495,8 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
14951495
goto out_put_cgroup;
14961496
}
14971497
bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops,
1498-
prog);
1498+
prog, attr->link_create.attach_type);
14991499
link->cgroup = cgrp;
1500-
link->type = attr->link_create.attach_type;
15011500

15021501
err = bpf_link_prime(&link->link, &link_primer);
15031502
if (err) {
@@ -1506,7 +1505,7 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
15061505
}
15071506

15081507
err = cgroup_bpf_attach(cgrp, NULL, NULL, link,
1509-
link->type, BPF_F_ALLOW_MULTI | attr->link_create.flags,
1508+
link->link.attach_type, BPF_F_ALLOW_MULTI | attr->link_create.flags,
15101509
attr->link_create.cgroup.relative_fd,
15111510
attr->link_create.cgroup.expected_revision);
15121511
if (err) {

kernel/bpf/net_namespace.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
struct bpf_netns_link {
1313
struct bpf_link link;
14-
enum bpf_attach_type type;
15-
enum netns_bpf_attach_type netns_type;
1614

1715
/* We don't hold a ref to net in order to auto-detach the link
1816
* when netns is going away. Instead we rely on pernet
@@ -21,6 +19,7 @@ struct bpf_netns_link {
2119
*/
2220
struct net *net;
2321
struct list_head node; /* node in list of links attached to net */
22+
enum netns_bpf_attach_type netns_type;
2423
};
2524

2625
/* Protects updates to netns_bpf */
@@ -216,7 +215,7 @@ static int bpf_netns_link_fill_info(const struct bpf_link *link,
216215
mutex_unlock(&netns_bpf_mutex);
217216

218217
info->netns.netns_ino = inum;
219-
info->netns.attach_type = net_link->type;
218+
info->netns.attach_type = link->attach_type;
220219
return 0;
221220
}
222221

@@ -230,7 +229,7 @@ static void bpf_netns_link_show_fdinfo(const struct bpf_link *link,
230229
"netns_ino:\t%u\n"
231230
"attach_type:\t%u\n",
232231
info.netns.netns_ino,
233-
info.netns.attach_type);
232+
link->attach_type);
234233
}
235234

236235
static const struct bpf_link_ops bpf_netns_link_ops = {
@@ -501,9 +500,8 @@ int netns_bpf_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
501500
goto out_put_net;
502501
}
503502
bpf_link_init(&net_link->link, BPF_LINK_TYPE_NETNS,
504-
&bpf_netns_link_ops, prog);
503+
&bpf_netns_link_ops, prog, type);
505504
net_link->net = net;
506-
net_link->type = type;
507505
net_link->netns_type = netns_type;
508506

509507
err = bpf_link_prime(&net_link->link, &link_primer);

0 commit comments

Comments
 (0)