Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.219.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146426; b=NA/UP2pKWg2e/kphf2mMH1vpzsZk3y8UC2UGGJMQbBKpyn69q3iqRnqJrjej0w/GYi3a uNUdzdrZlwgP5WcZ3ZUakxaPbjbrCHv3cN6C6S0j/fnN+iJ2pG+Q6+lnNnQBq2BoRuSzw OmEDrzaw/KJ1By4XePV7vDPvCnwWCT9DQRnJpUwiXHsEnIJUp/v3Mmxrfb4JGId5BbUhd DutcgYz/0lHldCeuFXBulFLfZfOMb22MW9dy4gbDUwdVOkjUCydJRC4zlsmSS05zwdPV0 RpjDwd6wCrgKQmzDXYxhB8+n9piGWSYBrGcoQCIuDuZ0NpMmUiQYHiOH63IpHXACTmw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146426; 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=xnbsAEy9TfIl3uoRanQjTyqGXJ80t8e4HBawna4CxAw=; b=hEXS2twic0a/jVfjiR5MSpth2wa717hAQqGYhuNj/SOD3YdM/uOXE6IUiLDzMhVKManI gYe0Sorhw2TLDS41stVvHSabHWA+7RIYoNbDXu07bQe3lRa2lsUaO/qiAmmAT3KZzaBMu RByImGGjOjlWFAasPSI4rnxmisKlVDwm6t1oc0c3Sr3v5vcE3QxAeYn52WE73PjzTQ0MX V26Gr1p6BDMlgbJYZG5ZCaQ0hricw68Tlohb/PwUgJqqlFs545eqYUwkrmokUfY4eMcUx 9dB56/IQJ8wBQMPtcxRaoaO4yPNmkCbjyR+c5c/jnFhIm4zTMkKr4PpKCdBYsFOsGQA== 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=avFxYXfU; arc=none smtp.remote-ip=209.85.219.70 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (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 933BA606A32A for ; Tue, 1 Dec 2026 17:33:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 933BA606A32A 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=avFxYXfU Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-92101559a2bso191509726d6.3 for ; Tue, 01 Dec 2026 09:33:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796146425; x=1796751225; 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=xnbsAEy9TfIl3uoRanQjTyqGXJ80t8e4HBawna4CxAw=; b=avFxYXfU2X083Qs6nMiIlIU3tX8XoIzWfq4uZ8PV0Prr0yR0JhRhPU/OktAVeoO/+s h0bQL1iH7h3MrnYOGrv0vmyg6Abc3h+lH1nkdS3fyozNstedhe1ncK96viCrEeRhcOMO V/WCW+5rD3ZlyQ4MisBa8E6zwE8X25KqzHRIri8Dw0q1Ji3KsoYi7/YMCrd1Qj5GkPos sutK02oh9+qzpDuWXnIuxQtNHgxhhJ64y555wiIyA/aVBhJ7TGNehqq20LRc4pWQL7BP dum5D8gHiEOThXJ0JjYL62XVCi5FbZURBhOZ5a6YPanreMxda50MqzwLeiP1YoE7u9bV SdCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796146425; x=1796751225; 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=xnbsAEy9TfIl3uoRanQjTyqGXJ80t8e4HBawna4CxAw=; b=Y1mhenqzM9FfrTab74/Fl6tjkii4mclbORNuEifQBt2jBK3H46rorHDkA0ILUuC+UD qTR6C07j3KozPbsly/jcyTv+22f8ZFwF6cwTzsPMFs7xjL139Zab+2JCEzBhc4EpNWj/ DMOxkQodizPN+wAIBFkARBJzLZOIEeuReaR5GMM78DjGatCYEE4Y/QzMaM5RKZ4WOv+j eN1zE1uGpzMuApMkydwduBXAuxfB1w9IG6Fv1P+JHxExIdigp2jJ5mvxjtoKSj8u55aP zsxnqozCLSFIPRIygcVWu9z+M1yHfCzvgo5/XrS2N2Vo0seEm1+SmopMAj5Lisw58SNc qNmw== X-Gm-Message-State: APuU4Z9YE9uXDwSY/d8FfVu0mxYYsbyN3cOiss+cPhgZ7w2ODS9sSP2e sJbcrj9FXdajKQ/0PbfYnntOdVbm+KzyKkVwv9o4sdZBGukzSbmAY8U6u1bBFCek6Ym5dypK7mY YXH31xGEygYRsvN8TLQ== X-Received: from qvbq39.prod.google.com ([2002:a05:6214:8627:b0:921:23e0:4e7e]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:202d:b0:921:c5d3:dfde with SMTP id 6a1803df08f44-921c5d3e3a5mr1500256d6.49.1796146423720; Tue, 01 Dec 2026 09:33:43 -0800 (PST) Date: Tue, 01 Dec 2026 17:33:37 +0000 In-Reply-To: <20261201-lskel-struct-ops-v3-0-057f3d23ac0c@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: <20261201-lskel-struct-ops-v3-0-057f3d23ac0c@google.com> X-Mailer: b4 0.14.3 Message-ID: <20261201-lskel-struct-ops-v3-1-057f3d23ac0c@google.com> Subject: [PATCH bpf-next v3 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 bounds, 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 | 266 +++++++++++++++++++++++++++++++++++++++-- kernel/bpf/syscall.c | 14 ++- tools/include/uapi/linux/bpf.h | 17 ++- 5 files changed, 314 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..e3608ec9da09 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,186 @@ 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 + * module_put(*mod) and 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, struct module **mod) +{ + 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); + *mod = NULL; + if (btf_is_module(*kern_btf)) { + *mod = btf_try_get_module(*kern_btf); + if (!*mod) { + btf_put(*kern_btf); + return ERR_PTR(-EINVAL); + } + } + st_ops_desc = bpf_struct_ops_find_value(*kern_btf, value_id); + if (!st_ops_desc) { + module_put(*mod); + *mod = NULL; + 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; + struct module *mod; + int err = 0; + + st_ops_desc = find_desc_by_local_type(*btf, attr->attach_btf_id, &kern_btf, &mod); + 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)) { + err = -EINVAL; + goto out; + } + 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) { + err = -ENOENT; + goto out; + } + 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; +out: + module_put(mod); + if (err) + btf_put(kern_btf); + return err; +} + +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; +} + +static bool btf_member_is_bitfield(const struct btf_type *t, + const struct btf_member *member, + const struct btf_type *mtype, u32 size) +{ + if (__btf_member_bitfield_size(t, member) || + (__btf_member_bit_offset(t, member) % 8)) + return true; + if (btf_type_is_int(mtype)) { + if (btf_int_offset(mtype) || + (btf_int_bits(mtype) % 8) || + btf_int_bits(mtype) / 8 != size) + return true; + } + return false; +} + +/* + * '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, moff, kern_moff, 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; + + mtype = btf_resolve_size(btf, btf_type_by_id(btf, member->type), &size); + if (IS_ERR(mtype) || btf_member_is_bitfield(t, member, mtype, size)) + goto err; + moff = __btf_member_bit_offset(t, member) / 8; + if (moff > t->size || size > t->size - moff) + goto err; + src = data + moff; + + 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; + } + kern_mtype = btf_resolve_size(kern_btf, + btf_type_by_id(kern_btf, kern_member->type), + &kern_size); + if (IS_ERR(kern_mtype) || + btf_member_is_bitfield(kern_t, kern_member, kern_mtype, kern_size) || + size != kern_size) + goto err; + kern_moff = __btf_member_bit_offset(kern_t, kern_member) / 8; + if (kern_moff > kern_t->size || kern_size > kern_t->size - kern_moff) + goto err; + if (BTF_INFO_KIND(mtype->info) != BTF_INFO_KIND(kern_mtype->info)) + goto err; + memcpy(value->data + kern_moff, src, size); + } + 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 +1152,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 +1231,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 +1266,53 @@ 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, mod); + 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) { + module_put(*mod); + *mod = NULL; + btf_put(*kern_btf); + btf_put(btf); + return ERR_PTR(-EINVAL); + } + 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; +} + static u32 count_func_ptrs(const struct btf *btf, const struct btf_type *t) { int i; @@ -1086,13 +1331,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 +1414,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 +1431,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