Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Add attach_type field to bpf_link

Attach_type will be set when a link is created by user. It is better to
record attach_type in bpf_link generically and have it available
universally for all link types. So add the attach_type field in bpf_link
and move the sleepable field to avoid unnecessary gap padding.

Signed-off-by: Tao Chen <chen.dylane@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20250710032038.888700-2-chen.dylane@linux.dev

authored by

Tao Chen and committed by
Andrii Nakryiko
b725441f d81526a6

+66 -42
+1 -1
drivers/net/netkit.c
··· 775 775 struct bpf_prog *prog) 776 776 { 777 777 bpf_link_init(&nkl->link, BPF_LINK_TYPE_NETKIT, 778 - &netkit_link_lops, prog); 778 + &netkit_link_lops, prog, attr->link_create.attach_type); 779 779 nkl->location = attr->link_create.attach_type; 780 780 nkl->dev = dev; 781 781 return bpf_link_prime(&nkl->link, link_primer);
+17 -11
include/linux/bpf.h
··· 1729 1729 enum bpf_link_type type; 1730 1730 const struct bpf_link_ops *ops; 1731 1731 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 + 1737 1733 u32 flags; 1734 + enum bpf_attach_type attach_type; 1735 + 1738 1736 /* rcu is used before freeing, work can be used to schedule that 1739 1737 * RCU-based freeing before that, so they never overlap 1740 1738 */ ··· 1740 1742 struct rcu_head rcu; 1741 1743 struct work_struct work; 1742 1744 }; 1745 + /* whether BPF link itself has "sleepable" semantics, which can differ 1746 + * from underlying BPF program having a "sleepable" semantics, as BPF 1747 + * link's semantics is determined by target attach hook 1748 + */ 1749 + bool sleepable; 1743 1750 }; 1744 1751 1745 1752 struct bpf_link_ops { ··· 2037 2034 2038 2035 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM) 2039 2036 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 2040 - int cgroup_atype); 2037 + int cgroup_atype, 2038 + enum bpf_attach_type attach_type); 2041 2039 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog); 2042 2040 #else 2043 2041 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 2044 - int cgroup_atype) 2042 + int cgroup_atype, 2043 + enum bpf_attach_type attach_type) 2045 2044 { 2046 2045 return -EOPNOTSUPP; 2047 2046 } ··· 2533 2528 int bpf_prog_new_fd(struct bpf_prog *prog); 2534 2529 2535 2530 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 2536 - const struct bpf_link_ops *ops, struct bpf_prog *prog); 2531 + const struct bpf_link_ops *ops, struct bpf_prog *prog, 2532 + enum bpf_attach_type attach_type); 2537 2533 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 2538 2534 const struct bpf_link_ops *ops, struct bpf_prog *prog, 2539 - bool sleepable); 2535 + enum bpf_attach_type attach_type, bool sleepable); 2540 2536 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer); 2541 2537 int bpf_link_settle(struct bpf_link_primer *primer); 2542 2538 void bpf_link_cleanup(struct bpf_link_primer *primer); ··· 2889 2883 2890 2884 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 2891 2885 const struct bpf_link_ops *ops, 2892 - struct bpf_prog *prog) 2886 + struct bpf_prog *prog, enum bpf_attach_type attach_type) 2893 2887 { 2894 2888 } 2895 2889 2896 2890 static inline void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 2897 2891 const struct bpf_link_ops *ops, struct bpf_prog *prog, 2898 - bool sleepable) 2892 + enum bpf_attach_type attach_type, bool sleepable) 2899 2893 { 2900 2894 } 2901 2895
+2 -1
kernel/bpf/bpf_iter.c
··· 552 552 if (!link) 553 553 return -ENOMEM; 554 554 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); 556 557 link->tinfo = tinfo; 557 558 558 559 err = bpf_link_prime(&link->link, &link_primer);
+3 -2
kernel/bpf/bpf_struct_ops.c
··· 808 808 goto reset_unlock; 809 809 } 810 810 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); 812 812 *plink++ = &link->link; 813 813 814 814 ksym = kzalloc(sizeof(*ksym), GFP_USER); ··· 1351 1351 err = -ENOMEM; 1352 1352 goto err_out; 1353 1353 } 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); 1355 1356 1356 1357 err = bpf_link_prime(&link->link, &link_primer); 1357 1358 if (err)
+2 -2
kernel/bpf/cgroup.c
··· 867 867 cgrp->bpf.flags[atype] = saved_flags; 868 868 869 869 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); 871 871 if (err) 872 872 goto cleanup; 873 873 } ··· 1495 1495 goto out_put_cgroup; 1496 1496 } 1497 1497 bpf_link_init(&link->link, BPF_LINK_TYPE_CGROUP, &bpf_cgroup_link_lops, 1498 - prog); 1498 + prog, attr->link_create.attach_type); 1499 1499 link->cgroup = cgrp; 1500 1500 link->type = attr->link_create.attach_type; 1501 1501
+1 -1
kernel/bpf/net_namespace.c
··· 501 501 goto out_put_net; 502 502 } 503 503 bpf_link_init(&net_link->link, BPF_LINK_TYPE_NETNS, 504 - &bpf_netns_link_ops, prog); 504 + &bpf_netns_link_ops, prog, type); 505 505 net_link->net = net; 506 506 net_link->type = type; 507 507 net_link->netns_type = netns_type;
+22 -13
kernel/bpf/syscall.c
··· 3069 3069 */ 3070 3070 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type, 3071 3071 const struct bpf_link_ops *ops, struct bpf_prog *prog, 3072 - bool sleepable) 3072 + enum bpf_attach_type attach_type, bool sleepable) 3073 3073 { 3074 3074 WARN_ON(ops->dealloc && ops->dealloc_deferred); 3075 3075 atomic64_set(&link->refcnt, 1); ··· 3078 3078 link->id = 0; 3079 3079 link->ops = ops; 3080 3080 link->prog = prog; 3081 + link->attach_type = attach_type; 3081 3082 } 3082 3083 3083 3084 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, 3084 - const struct bpf_link_ops *ops, struct bpf_prog *prog) 3085 + const struct bpf_link_ops *ops, struct bpf_prog *prog, 3086 + enum bpf_attach_type attach_type) 3085 3087 { 3086 - bpf_link_init_sleepable(link, type, ops, prog, false); 3088 + bpf_link_init_sleepable(link, type, ops, prog, attach_type, false); 3087 3089 } 3088 3090 3089 3091 static void bpf_link_free_id(int id) ··· 3445 3443 static int bpf_tracing_prog_attach(struct bpf_prog *prog, 3446 3444 int tgt_prog_fd, 3447 3445 u32 btf_id, 3448 - u64 bpf_cookie) 3446 + u64 bpf_cookie, 3447 + enum bpf_attach_type attach_type) 3449 3448 { 3450 3449 struct bpf_link_primer link_primer; 3451 3450 struct bpf_prog *tgt_prog = NULL; ··· 3514 3511 goto out_put_prog; 3515 3512 } 3516 3513 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING, 3517 - &bpf_tracing_link_lops, prog); 3514 + &bpf_tracing_link_lops, prog, attach_type); 3515 + 3518 3516 link->attach_type = prog->expected_attach_type; 3519 3517 link->link.cookie = bpf_cookie; 3520 3518 ··· 4053 4049 err = -ENOMEM; 4054 4050 goto out_put_file; 4055 4051 } 4056 - bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog); 4052 + bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog, 4053 + attr->link_create.attach_type); 4057 4054 link->perf_file = perf_file; 4058 4055 4059 4056 err = bpf_link_prime(&link->link, &link_primer); ··· 4086 4081 #endif /* CONFIG_PERF_EVENTS */ 4087 4082 4088 4083 static int bpf_raw_tp_link_attach(struct bpf_prog *prog, 4089 - const char __user *user_tp_name, u64 cookie) 4084 + const char __user *user_tp_name, u64 cookie, 4085 + enum bpf_attach_type attach_type) 4090 4086 { 4091 4087 struct bpf_link_primer link_primer; 4092 4088 struct bpf_raw_tp_link *link; ··· 4110 4104 tp_name = prog->aux->attach_func_name; 4111 4105 break; 4112 4106 } 4113 - return bpf_tracing_prog_attach(prog, 0, 0, 0); 4107 + return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type); 4114 4108 case BPF_PROG_TYPE_RAW_TRACEPOINT: 4115 4109 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: 4116 4110 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0) ··· 4132 4126 goto out_put_btp; 4133 4127 } 4134 4128 bpf_link_init_sleepable(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, 4135 - &bpf_raw_tp_link_lops, prog, 4129 + &bpf_raw_tp_link_lops, prog, attach_type, 4136 4130 tracepoint_is_faultable(btp->tp)); 4137 4131 link->btp = btp; 4138 4132 link->cookie = cookie; ··· 4174 4168 4175 4169 tp_name = u64_to_user_ptr(attr->raw_tracepoint.name); 4176 4170 cookie = attr->raw_tracepoint.cookie; 4177 - fd = bpf_raw_tp_link_attach(prog, tp_name, cookie); 4171 + fd = bpf_raw_tp_link_attach(prog, tp_name, cookie, prog->expected_attach_type); 4178 4172 if (fd < 0) 4179 4173 bpf_prog_put(prog); 4180 4174 return fd; ··· 5531 5525 ret = bpf_tracing_prog_attach(prog, 5532 5526 attr->link_create.target_fd, 5533 5527 attr->link_create.target_btf_id, 5534 - attr->link_create.tracing.cookie); 5528 + attr->link_create.tracing.cookie, 5529 + attr->link_create.attach_type); 5535 5530 break; 5536 5531 case BPF_PROG_TYPE_LSM: 5537 5532 case BPF_PROG_TYPE_TRACING: ··· 5541 5534 goto out; 5542 5535 } 5543 5536 if (prog->expected_attach_type == BPF_TRACE_RAW_TP) 5544 - ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie); 5537 + ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie, 5538 + attr->link_create.attach_type); 5545 5539 else if (prog->expected_attach_type == BPF_TRACE_ITER) 5546 5540 ret = bpf_iter_link_attach(attr, uattr, prog); 5547 5541 else if (prog->expected_attach_type == BPF_LSM_CGROUP) ··· 5551 5543 ret = bpf_tracing_prog_attach(prog, 5552 5544 attr->link_create.target_fd, 5553 5545 attr->link_create.target_btf_id, 5554 - attr->link_create.tracing.cookie); 5546 + attr->link_create.tracing.cookie, 5547 + attr->link_create.attach_type); 5555 5548 break; 5556 5549 case BPF_PROG_TYPE_FLOW_DISSECTOR: 5557 5550 case BPF_PROG_TYPE_SK_LOOKUP:
+2 -1
kernel/bpf/tcx.c
··· 301 301 struct net_device *dev, 302 302 struct bpf_prog *prog) 303 303 { 304 - bpf_link_init(&tcx->link, BPF_LINK_TYPE_TCX, &tcx_link_lops, prog); 304 + bpf_link_init(&tcx->link, BPF_LINK_TYPE_TCX, &tcx_link_lops, prog, 305 + attr->link_create.attach_type); 305 306 tcx->location = attr->link_create.attach_type; 306 307 tcx->dev = dev; 307 308 return bpf_link_prime(&tcx->link, link_primer);
+6 -4
kernel/bpf/trampoline.c
··· 674 674 675 675 static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog, 676 676 bpf_func_t bpf_func, 677 - int cgroup_atype) 677 + int cgroup_atype, 678 + enum bpf_attach_type attach_type) 678 679 { 679 680 struct bpf_shim_tramp_link *shim_link = NULL; 680 681 struct bpf_prog *p; ··· 702 701 p->expected_attach_type = BPF_LSM_MAC; 703 702 bpf_prog_inc(p); 704 703 bpf_link_init(&shim_link->link.link, BPF_LINK_TYPE_UNSPEC, 705 - &bpf_shim_tramp_link_lops, p); 704 + &bpf_shim_tramp_link_lops, p, attach_type); 706 705 bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype); 707 706 708 707 return shim_link; ··· 727 726 } 728 727 729 728 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 730 - int cgroup_atype) 729 + int cgroup_atype, 730 + enum bpf_attach_type attach_type) 731 731 { 732 732 struct bpf_shim_tramp_link *shim_link = NULL; 733 733 struct bpf_attach_target_info tgt_info = {}; ··· 765 763 766 764 /* Allocate and install new shim. */ 767 765 768 - shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype); 766 + shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype, attach_type); 769 767 if (!shim_link) { 770 768 err = -ENOMEM; 771 769 goto err;
+2 -2
kernel/trace/bpf_trace.c
··· 2986 2986 } 2987 2987 2988 2988 bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI, 2989 - &bpf_kprobe_multi_link_lops, prog); 2989 + &bpf_kprobe_multi_link_lops, prog, attr->link_create.attach_type); 2990 2990 2991 2991 err = bpf_link_prime(&link->link, &link_primer); 2992 2992 if (err) ··· 3441 3441 link->link.flags = flags; 3442 3442 3443 3443 bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI, 3444 - &bpf_uprobe_multi_link_lops, prog); 3444 + &bpf_uprobe_multi_link_lops, prog, attr->link_create.attach_type); 3445 3445 3446 3446 for (i = 0; i < cnt; i++) { 3447 3447 uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry),
+2 -1
net/bpf/bpf_dummy_struct_ops.c
··· 171 171 } 172 172 /* prog doesn't take the ownership of the reference from caller */ 173 173 bpf_prog_inc(prog); 174 - bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_link_lops, prog); 174 + bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_link_lops, prog, 175 + prog->expected_attach_type); 175 176 176 177 op_idx = prog->expected_attach_type; 177 178 err = bpf_struct_ops_prepare_trampoline(tlinks, link,
+2 -1
net/core/dev.c
··· 10364 10364 goto unlock; 10365 10365 } 10366 10366 10367 - bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog); 10367 + bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog, 10368 + attr->link_create.attach_type); 10368 10369 link->dev = dev; 10369 10370 link->flags = attr->link_create.flags; 10370 10371
+2 -1
net/core/sock_map.c
··· 1866 1866 } 1867 1867 1868 1868 attach_type = attr->link_create.attach_type; 1869 - bpf_link_init(&sockmap_link->link, BPF_LINK_TYPE_SOCKMAP, &sock_map_link_ops, prog); 1869 + bpf_link_init(&sockmap_link->link, BPF_LINK_TYPE_SOCKMAP, &sock_map_link_ops, prog, 1870 + attach_type); 1870 1871 sockmap_link->map = map; 1871 1872 sockmap_link->attach_type = attach_type; 1872 1873
+2 -1
net/netfilter/nf_bpf_link.c
··· 225 225 if (!link) 226 226 return -ENOMEM; 227 227 228 - bpf_link_init(&link->link, BPF_LINK_TYPE_NETFILTER, &bpf_nf_link_lops, prog); 228 + bpf_link_init(&link->link, BPF_LINK_TYPE_NETFILTER, &bpf_nf_link_lops, prog, 229 + attr->link_create.attach_type); 229 230 230 231 link->hook_ops.hook = nf_hook_run_bpf; 231 232 link->hook_ops.hook_ops_type = NF_HOOK_OP_BPF;