From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796075145; b=l3LtW5u7l47dHpPiXa/V95TpPCQJoAgj9Z6qC4aGNtkxX5TfkLsad+taQXXbjIns/aYh m+pEMbSAEgSa4bl1Jh1k6y7bS/ri/CymJ/DUf6zO44zx7EL35b3TK+i6WLjFpo/8Ow0Q5 RYf7BPCGPYVYRRwh5ovAKU9zmftdGY614e1rZ3PkzC761mD8otpH4A93RcfdqEsGmH/HR pF9aqP9eAGMUIUOUtZprk0palTIbJXHDTkAsXqiXLFZMShXS6LmngvpUscZpNTd5Wiy3k z4uO8bnWbnIhTWGQSuqGWEWsuA4moJ3sK/moFK3Xj7mk/IV2/sPyoKSaUrQ1v+tXDQA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796075145; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=BxKAABnHVucjsLecMr0NdXCOScA44TgwDi2+Gxo0mEg=; b=PoibeZhkZLVaM9UOB+KAOk9DWXteCFYMsCJuleFF3FgSCQkAZGLf2/LSgBO1BptsgsNg rsTk8AjFQox4+8eGs5f7jYmocv+v+nxPG8f+tRXXYBzZYiRpUHntV6bsXX1Bs4pVsxDfD b5I+mvEm6KMV9/mkSRDQbOPEV6HGdB9hJwy0kdq5m8EfLreU6oOc+8C7YygCCZsDOqkXM 2vi3hjPSeUVJ3lfgRCZNr5BQFBn8uQNMLeuV04+qLhgcKHo37ohkqrhC9qIbc2mPJVXMg zrlvGhUVCWWN/iX3hqtewLTui/Q5EEiejYRRn2/y8ZhFO9/11gCvbKfAKdrNhhmxq6g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--sidnayyar.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ECHZztqn; arc=none smtp.remote-ip=209.85.222.200 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14160606A32F for ; Mon, 30 Nov 2026 21:45:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 14160606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sidnayyar.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ECHZztqn Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-946038e22f5so1068794285a.0 for ; Mon, 30 Nov 2026 13:45:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796075144; x=1796679944; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=BxKAABnHVucjsLecMr0NdXCOScA44TgwDi2+Gxo0mEg=; b=ECHZztqnJXa0LgBSLaXKSooBbMhhdtxeWe25XZHT2Q90BWcLdWeArLhCAonl5SrsWm B1uljPCvTLc9CTEBgf5IWsIxT8j2cXgpyldFW6riHNphaqdyhIMJy9873DatFPcvsi7v J1wrCySvxKIwYbUz9CksCNYeYck7O3CCjyGZCdhr3BkaLbdxhoG+A9yDs0t4PqNTsSH+ /8wXk70PfKxAXcgXEFGcgsjaGEy2Jg9KXTq4+E7isAlp/hICxnmlidkXwmDaMVyyqkDg 5LBZEW8q+EjvHCmzoDUxz1CHCrLM6xdQzKxZxmAVczLlrQ1blQvMR1WFONN4J3Sw4vbF zWdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796075144; x=1796679944; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=BxKAABnHVucjsLecMr0NdXCOScA44TgwDi2+Gxo0mEg=; b=yDHJDfyegOdZU7nSO9owYte5hGvVfmJtvwJlo0OTEccBBqAjbe5vjMYn2ih34XCpbG KBw4vtN6hCJ065tIIFvVMIerbFj65erDVxF9sukY6DyLarNmW2FhS2SlkAO14U7fv3EB qhCQtBS8e1UnPgDKjYu4Hnc98AI8dq1sJSg/mV7TUwJ30dc6MqHsNjWYfecxY4xFKOAC y/y3mZH9suoccYT30XXZVJhSdvBdXCEfNPR/X5wueLKkzzVCtoyo0oGzqYiigTF7u7Vr VqPX6U5BOjslNNB7OWcZ1mt+zdGLcUPGnW6NMCbT8IZ1Q051NvvrbwvdStSeLdwZeHYY u1mg== X-Gm-Message-State: APuU4Z/ec+5gROUke0q6kWPT3ELXxsqcqgTOhe0pFLuwV1+BcYZNHLbG 8cB16qk7V6EpSyqG0NFqaBdjERfphnwTV+3zRbRYqdI2QIfQ2nHBxJb3+kPaCDb5Ly1Zj7/mGdo OQgT8/doPbnt9eh2zCw== X-Received: from qkax2-n2.prod.google.com ([2002:a05:620a:2242:20b0:93f:31eb:2292]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:2a0a:b0:93f:43a4:a47e with SMTP id af79cd13be357-949bab1dadamr6768390185a.43.1796075143624; Mon, 30 Nov 2026 13:45:43 -0800 (PST) Date: Mon, 30 Nov 2026 21:44:46 +0000 In-Reply-To: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> X-Mailer: b4 0.14.3 Message-ID: <20261130-lskel-struct-ops-v2-1-8b76e65073ab@google.com> Subject: [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF From: Siddharth Nayyar To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Quentin Monnet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Anton Protopopov , Giuliano Procida , Neill Kapron , "=?utf-8?q?Thi=C3=A9baud_Weksteen?=" , Maxime Ripard , Siddharth Nayyar , Alexei Starovoitov Content-Type: text/plain; charset="utf-8" Currently, creating and populating a BPF_MAP_TYPE_STRUCT_OPS map and loading its BPF_PROG_TYPE_STRUCT_OPS programs require userspace to parse vmlinux or module BTF to resolve kernel type IDs, wrapper struct sizes, member indices, and member byte offsets. Light skeleton loaders ship a pre-generated BPF object BTF and loader program without userspace BTF parsing, while the kernel already has access to both the BPF object's BTF (via btf_fd) and vmlinux/module BTFs. Allow BPF_MAP_TYPE_STRUCT_OPS and BPF_PROG_TYPE_STRUCT_OPS to specify their target struct and members using the BPF object's own BTF, with the kernel resolving the corresponding kernel struct_ops type and members by name (stripping any CO-RE ___flavor suffix): - BPF_MAP_CREATE: Accept btf_value_type_id and value_size describing the local struct in btf_fd when btf_vmlinux_value_type_id is 0. Resolve bpf_struct_ops_ in vmlinux or module BTF, pin any owning module, and record local_btf and local_type on the map so other map operations (such as BPF_MAP_LOOKUP_ELEM) continue to see a standard struct_ops map. - BPF_PROG_LOAD: When attach_btf_obj_fd refers to the BPF object's BTF for a BPF_PROG_TYPE_STRUCT_OPS program, resolve attach_btf_id and the local member index in expected_attach_type to the matching kernel BTF, type ID, and member index. - BPF_MAP_UPDATE_ELEM: When local_type is set on the map, accept a value with the local struct's size and layout, validate member types, sizes, and zeroed padding holes, skip zeroed members absent from the running kernel, and copy each member into the kernel struct_ops value buffer. Existing users passing kernel BTF IDs are unaffected. Suggested-by: Alexei Starovoitov Signed-off-by: Siddharth Nayyar --- include/linux/bpf.h | 14 +++ include/uapi/linux/bpf.h | 17 ++- kernel/bpf/bpf_struct_ops.c | 236 +++++++++++++++++++++++++++++++++++++++-- kernel/bpf/syscall.c | 14 ++- tools/include/uapi/linux/bpf.h | 17 ++- 5 files changed, 284 insertions(+), 14 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4f9c2bf64643..74cf542464fb 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2444,6 +2444,8 @@ void bpf_struct_ops_put(const void *kdata); int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff); int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key, void *value); +u32 bpf_struct_ops_map_update_size(const struct bpf_map *map); +int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr); int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes, struct bpf_tramp_node *node, const struct btf_func_model *model, @@ -2522,6 +2524,18 @@ static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, { return -EINVAL; } + +static inline u32 bpf_struct_ops_map_update_size(const struct bpf_map *map) +{ + return map->value_size; +} + +static inline int bpf_struct_ops_resolve_attach(struct btf **btf, + union bpf_attr *attr) +{ + return -EOPNOTSUPP; +} + static inline int bpf_struct_ops_link_create(union bpf_attr *attr) { return -EOPNOTSUPP; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 28904c6f0787..5d44bce55d5e 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1599,6 +1599,14 @@ union bpf_attr { * struct stored as the * map value */ + /* + * BPF_MAP_TYPE_STRUCT_OPS takes one of the two ids. + * btf_value_type_id is a struct in btf_fd, e.g. + * tcp_congestion_ops, and value_size is its size. The kernel + * finds its struct_ops and the members by name. + * BPF_MAP_UPDATE_ELEM takes the struct of btf_fd. + * BPF_MAP_LOOKUP_ELEM returns the struct of the kernel. + */ /* Any per-map-type extra fields * * BPF_MAP_TYPE_BLOOM_FILTER - the lowest 4 bits indicate the @@ -1686,7 +1694,14 @@ union bpf_attr { union { /* valid prog_fd to attach to bpf prog */ __u32 attach_prog_fd; - /* or valid module BTF object fd or 0 to attach to vmlinux */ + /* + * or valid module BTF object fd or 0 to attach to + * vmlinux. BPF_PROG_TYPE_STRUCT_OPS takes the BTF of + * the bpf object too. Then attach_btf_id is a struct + * in that BTF and expected_attach_type is the index + * of its member. The kernel finds its struct_ops and + * the member by name. + */ __u32 attach_btf_obj_fd; }; __u32 core_relo_cnt; /* number of bpf_core_relo */ diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 3d334cd526c0..96eca8493892 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -14,6 +14,7 @@ #include #include #include +#include "../tools/lib/bpf/relo_core.h" struct bpf_struct_ops_value { struct bpf_struct_ops_common_value common; @@ -42,6 +43,12 @@ struct bpf_struct_ops_map { void *image_pages[MAX_TRAMP_IMAGE_PAGES]; /* The owner moduler's btf. */ struct btf *btf; + /* + * Like in CO-RE "local" is what the BTF of the bpf object has. + * When local_type is set map_update_elem() takes this struct. + */ + struct btf *local_btf; + const struct btf_type *local_type; /* uvalue->data stores the kernel struct * (e.g. tcp_congestion_ops) that is more useful * to userspace than the kvalue. For example, @@ -593,7 +600,7 @@ static void bpf_struct_ops_map_free_image(struct bpf_struct_ops_map *st_map) st_map->image_pages_cnt = 0; } -static int check_zero_holes(const struct btf *btf, const struct btf_type *t, void *data) +static int check_zero_holes(const struct btf *btf, const struct btf_type *t, const void *data) { const struct btf_member *member; u32 i, moff, msize, prev_mend = 0; @@ -729,8 +736,152 @@ static void bpf_struct_ops_map_free_ksyms(struct bpf_struct_ops_map *st_map) } } -static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, - void *value, u64 flags) +static const struct btf_member *find_member(const struct btf *btf, + const struct btf_type *t, + const char *name) +{ + const struct btf_member *member; + u32 i; + + if (!*name) + return NULL; + for_each_member(i, t, member) + if (!strcmp(btf_name_by_offset(btf, member->name_off), name)) + return member; + return NULL; +} + +/* + * 'id' is a local struct, e.g. tcp_congestion_ops or tcp_congestion_ops___v2. + * Find bpf_struct_ops_tcp_congestion_ops in the BTF of vmlinux or of a module. + * A module can have it for a struct of vmlinux. The caller does btf_put(*kern_btf). + */ +static const struct bpf_struct_ops_desc * +find_desc_by_local_type(const struct btf *btf, u32 id, struct btf **kern_btf) +{ + const struct btf_type *t = btf_type_by_id(btf, id); + const struct bpf_struct_ops_desc *st_ops_desc; + char name[KSYM_NAME_LEN]; + const char *tname; + s32 value_id; + + if (!t || !__btf_type_is_struct(t)) + return ERR_PTR(-EINVAL); + tname = btf_name_by_offset(btf, t->name_off); + if (snprintf(name, sizeof(name), VALUE_PREFIX "%.*s", + (int)bpf_core_essential_name_len(tname), tname) >= sizeof(name)) + return ERR_PTR(-EINVAL); + value_id = bpf_find_btf_id(name, BTF_KIND_STRUCT, kern_btf); + if (value_id < 0) + return ERR_PTR(value_id); + st_ops_desc = bpf_struct_ops_find_value(*kern_btf, value_id); + if (!st_ops_desc) { + btf_put(*kern_btf); + return ERR_PTR(-EOPNOTSUPP); + } + return st_ops_desc; +} + +/* + * attach_btf_id is a local struct and expected_attach_type is the index of its + * member. Replace them, and the BTF, with what the kernel has under the same names. + */ +int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr) +{ + const struct bpf_struct_ops_desc *st_ops_desc; + const struct btf_member *member; + const struct btf_type *t; + struct btf *kern_btf; + + st_ops_desc = find_desc_by_local_type(*btf, attr->attach_btf_id, &kern_btf); + if (IS_ERR(st_ops_desc)) + return PTR_ERR(st_ops_desc); + t = btf_type_by_id(*btf, attr->attach_btf_id); + if (attr->expected_attach_type >= btf_type_vlen(t)) { + btf_put(kern_btf); + return -EINVAL; + } + member = btf_type_member(t) + attr->expected_attach_type; + member = find_member(kern_btf, st_ops_desc->type, + btf_name_by_offset(*btf, member->name_off)); + if (!member) { + btf_put(kern_btf); + return -ENOENT; + } + attr->attach_btf_id = st_ops_desc->type_id; + attr->expected_attach_type = member - btf_type_member(st_ops_desc->type); + btf_put(*btf); + *btf = kern_btf; + return 0; +} + +u32 bpf_struct_ops_map_update_size(const struct bpf_map *map) +{ + const struct bpf_struct_ops_map *st_map = (const struct bpf_struct_ops_map *)map; + + return st_map->local_type ? st_map->local_type->size : map->value_size; +} + +/* + * 'data' has the layout of the local struct. Copy every member to the place + * of the member with the same name in the struct of the kernel. + */ +static void *local_to_kern_value(const struct bpf_struct_ops_map *st_map, const void *data) +{ + const struct btf_type *t = st_map->local_type, *kern_t = st_map->st_ops_desc->type; + const struct btf *btf = st_map->local_btf, *kern_btf = st_map->btf; + const struct btf_member *member, *kern_member; + const struct btf_type *mtype, *kern_mtype; + struct bpf_struct_ops_value *value; + u32 i, size, kern_size; + int err = -EINVAL; + + value = kvzalloc(st_map->map.value_size, GFP_USER); + if (!value) + return ERR_PTR(-ENOMEM); + + for_each_member(i, t, member) { + const void *src = data + __btf_member_bit_offset(t, member) / 8; + + if (__btf_member_bitfield_size(t, member) || + IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, member->type), &size))) + goto err; + kern_member = find_member(kern_btf, kern_t, + btf_name_by_offset(btf, member->name_off)); + if (!kern_member) { + /* This kernel doesn't have the member. That's fine when it's not in use. */ + if (!memchr_inv(src, 0, size)) + continue; + err = -ENOENT; + goto err; + } + if (__btf_member_bitfield_size(kern_t, kern_member) || + IS_ERR(btf_resolve_size(kern_btf, + btf_type_by_id(kern_btf, kern_member->type), + &kern_size)) || + size != kern_size) + goto err; + mtype = btf_type_skip_modifiers(btf, member->type, NULL); + kern_mtype = btf_type_skip_modifiers(kern_btf, kern_member->type, NULL); + if (BTF_INFO_KIND(mtype->info) != BTF_INFO_KIND(kern_mtype->info)) + goto err; + if (btf_type_is_ptr(mtype) && + !!btf_type_resolve_func_ptr(btf, member->type, NULL) != + !!btf_type_resolve_func_ptr(kern_btf, kern_member->type, NULL)) + goto err; + memcpy(value->data + __btf_member_bit_offset(kern_t, kern_member) / 8, src, size); + } + err = check_zero_holes(btf, t, data); + if (err) + goto err; + return value; +err: + kvfree(value); + return ERR_PTR(err); +} + +static long __bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, + void *value, u64 flags) { struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map; const struct bpf_struct_ops_desc *st_ops_desc = st_map->st_ops_desc; @@ -967,6 +1118,23 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, return err; } +static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, + void *value, u64 flags) +{ + struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map; + long err; + + if (!st_map->local_type) + return __bpf_struct_ops_map_update_elem(map, key, value, flags); + + value = local_to_kern_value(st_map, value); + if (IS_ERR(value)) + return PTR_ERR(value); + err = __bpf_struct_ops_map_update_elem(map, key, value, flags); + kvfree(value); + return err; +} + static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key) { enum bpf_struct_ops_state prev_state; @@ -1029,6 +1197,7 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map) bpf_map_area_free(st_map->ksyms); bpf_struct_ops_map_free_image(st_map); bpf_map_area_free(st_map->uvalue); + btf_put(st_map->local_btf); bpf_map_area_free(st_map); } @@ -1063,11 +1232,57 @@ static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr) { if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 || (attr->map_flags & ~(BPF_F_LINK | BPF_F_VTYPE_BTF_OBJ_FD)) || - !attr->btf_vmlinux_value_type_id) + (!attr->btf_vmlinux_value_type_id && !attr->btf_value_type_id)) return -EINVAL; return 0; } +/* + * btf_value_type_id and value_size are of a local struct. Replace them with + * btf_vmlinux_value_type_id and value_size of the struct_ops with the same name, + * so that everything but map_update_elem() sees a regular struct_ops map. + * Return the local BTF. A module that has the struct_ops is pinned. + */ +static struct btf *local_to_kern_attr(union bpf_attr *attr, const struct btf_type **local_type, + struct btf **kern_btf, struct module **mod) +{ + const struct bpf_struct_ops_desc *st_ops_desc; + struct btf *btf; + + if (attr->btf_key_type_id || (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD)) + return ERR_PTR(-EINVAL); + btf = btf_get_by_fd(attr->btf_fd); + if (IS_ERR(btf)) + return btf; + if (btf_is_kernel(btf)) { + btf_put(btf); + return ERR_PTR(-EACCES); + } + st_ops_desc = find_desc_by_local_type(btf, attr->btf_value_type_id, kern_btf); + if (IS_ERR(st_ops_desc)) { + btf_put(btf); + return ERR_CAST(st_ops_desc); + } + *local_type = btf_type_by_id(btf, attr->btf_value_type_id); + if (attr->value_size != (*local_type)->size) + goto err; + if (btf_is_module(*kern_btf)) { + *mod = btf_try_get_module(*kern_btf); + if (!*mod) + goto err; + } + attr->btf_value_type_id = 0; + attr->btf_vmlinux_value_type_id = st_ops_desc->value_id; + attr->value_size = st_ops_desc->value_type->size; + /* The module holds its BTF. The BTF of vmlinux doesn't go away. */ + btf_put(*kern_btf); + return btf; +err: + btf_put(*kern_btf); + btf_put(btf); + return ERR_PTR(-EINVAL); +} + static u32 count_func_ptrs(const struct btf *btf, const struct btf_type *t) { int i; @@ -1086,13 +1301,17 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) const struct bpf_struct_ops_desc *st_ops_desc; size_t st_map_size; struct bpf_struct_ops_map *st_map; - const struct btf_type *t, *vt; + const struct btf_type *t, *vt, *local_type = NULL; + struct btf *btf, *local_btf = NULL; struct module *mod = NULL; struct bpf_map *map; - struct btf *btf; int ret; - if (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD) { + if (attr->btf_value_type_id) { + local_btf = local_to_kern_attr(attr, &local_type, &btf, &mod); + if (IS_ERR(local_btf)) + return ERR_CAST(local_btf); + } else if (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD) { /* The map holds btf for its whole life time. */ btf = btf_get_by_fd(attr->value_type_btf_obj_fd); if (IS_ERR(btf)) @@ -1165,6 +1384,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) goto errout_free; } st_map->btf = btf; + st_map->local_btf = local_btf; + st_map->local_type = local_type; mutex_init(&st_map->lock); bpf_map_init_from_attr(map, attr); @@ -1180,6 +1401,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) __bpf_struct_ops_map_free(map); errout: module_put(mod); + btf_put(local_btf); return ERR_PTR(ret); } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 198438b95849..3a8c25285236 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1899,6 +1899,8 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) } value_size = bpf_map_value_size(map, attr->flags); + if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) + value_size = bpf_struct_ops_map_update_size(map); value = kvmemdup_bpfptr(uvalue, value_size); if (IS_ERR(value)) { err = PTR_ERR(value); @@ -3044,12 +3046,14 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at goto put_token; } if (!btf_is_kernel(attach_btf)) { - /* attaching through specifying bpf_prog's BTF - * objects directly might be supported eventually - */ - btf_put(attach_btf); + /* only struct_ops can name its target in the BTF of the object */ err = -ENOTSUPP; - goto put_token; + if (type == BPF_PROG_TYPE_STRUCT_OPS) + err = bpf_struct_ops_resolve_attach(&attach_btf, attr); + if (err) { + btf_put(attach_btf); + goto put_token; + } } } } else if (attr->attach_btf_id || multi_func) { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 28904c6f0787..5d44bce55d5e 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1599,6 +1599,14 @@ union bpf_attr { * struct stored as the * map value */ + /* + * BPF_MAP_TYPE_STRUCT_OPS takes one of the two ids. + * btf_value_type_id is a struct in btf_fd, e.g. + * tcp_congestion_ops, and value_size is its size. The kernel + * finds its struct_ops and the members by name. + * BPF_MAP_UPDATE_ELEM takes the struct of btf_fd. + * BPF_MAP_LOOKUP_ELEM returns the struct of the kernel. + */ /* Any per-map-type extra fields * * BPF_MAP_TYPE_BLOOM_FILTER - the lowest 4 bits indicate the @@ -1686,7 +1694,14 @@ union bpf_attr { union { /* valid prog_fd to attach to bpf prog */ __u32 attach_prog_fd; - /* or valid module BTF object fd or 0 to attach to vmlinux */ + /* + * or valid module BTF object fd or 0 to attach to + * vmlinux. BPF_PROG_TYPE_STRUCT_OPS takes the BTF of + * the bpf object too. Then attach_btf_id is a struct + * in that BTF and expected_attach_type is the index + * of its member. The kernel finds its struct_ops and + * the member by name. + */ __u32 attach_btf_obj_fd; }; __u32 core_relo_cnt; /* number of bpf_core_relo */ -- 2.56.1.839.g22a8290db9-goog