From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796075146; b=bbxvo3CXbBtljbWO8kbW+yBuu7z3qX5pUBokJcUyhM7VdAg+TjD180w7wWEgiPK8+LKB cTF29pnLdSLRgSgP9WIYNQvQDu63vQabQux/FU418veigmdgKQQJDIbi+euAm4PVeVVPs wtMd2ilMHDbzCHzHsZ4R/LB2mOBnSDWysEhfcnZEIxS5rNZa30/ekcHsi996f3dk2/x0J hbjb9WccVBjemK0sM9LKHVwQox/SSB3YdzLPLFNFfUVUXoJmB7ZqY+boRqyWe1KpaGocQ twrLypBEJabB+QE7j6D7dz6EOvztHVuqIuBwBdUFMkdPtyz8YNc3tAUIq+Cf0MDktxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796075146; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=STGratr6bgWUr6z82Sht+KAKNxnwVhtbscxzJVs0/S4=; b=b9so9kF+XtEzGWzyQqmsD1O14pO+SvlHVKtmev0GUOsyHOpyW7Q5smyWEKXrlEFAckLS j/cIMe1lWAXryE3vOi6qdCu5QM87dPnbbCXCIpweTSu6qQcRfUIKo3cjG30UJmUfQAzzp U/bfCIMlTsNFOmovMcSJHrWlEKYueJ7T7dpdpAM14Kb7GX4ogm5cKuCNCSjBm7h8Fa2RT bKWy6VlhBwYYggx8LLQOcG00CZvyZVxXf9djYqoiaR1XOmRKxAmusPXzCzX/QV7W4Tsrb Jr86myZgoG/c0Hwz65f3wV+NMimzTfnIteiLIPepbHLCCZ6APoZ3RrjK4/MBSa+YxMA== 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=asDXQLHq; arc=none smtp.remote-ip=209.85.160.198 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (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 08894606A681 for ; Mon, 30 Nov 2026 21:45:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 08894606A681 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=asDXQLHq Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-55d32f54d21so209099071cf.2 for ; Mon, 30 Nov 2026 13:45:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796075145; x=1796679945; 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=STGratr6bgWUr6z82Sht+KAKNxnwVhtbscxzJVs0/S4=; b=asDXQLHqqIti73SP/aIPcG5uS3Y2sRaeQck9rzifcZXKjhxMN1BNKO8zwyo4HI2Eyt OxEILsAQH5OsUx6hbWJ1vIdsTdFpL8Z4YYprV4EXxRnGpx0VRS8iKCiqz5Jr3kJpB7ny RhlCBOJ4VNzbP7Ci2Or8xycenvJXVbyPtEmUgkHLVdkMXogNFnIgwNdc5TydwommETv5 P9zXkkISrdKXCqdVzKuxCGxWb9faTaYKvwEwcYhM+ORGWSZy2D2A7xELoh1sgTqJS8Ni V+sjssn8v8F9gnoI0HFXBpDcK7rCuHtFue6ub9RJTxevyNN+OexCUjEm8HwzuDzX5P1p ajzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796075145; x=1796679945; 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=STGratr6bgWUr6z82Sht+KAKNxnwVhtbscxzJVs0/S4=; b=KUhROFu+KEONbyz2UHFXNRG5Ck6S2LFs1ZUZlOr/JP6XPvZei3ptbGFcYBeHkjYhkh rwdxmf5J+h+ahbT/NY4ZhnbRUz9YYdtZwgUbZdjS87WcKMTKoPFuzBc4HnXqh97umRsh NDnhKDstQ4004O1M7yRx3/QpoSPcW3JZ48DBHbQgr0C3pJ++jx4cFvv5CNyekM6s73T1 Qf2kjIK0iz3nINAE/EC5jMZkw38bBVU2VVzUPkBqLJS3jBzpIFSVRgjSF1cBB8Z5Eyur 6ALU2eyyecdpUEUd2ekuQIRzGAcvQDlCTiDJT0kZm831lxmaENQUS6Gu92CpSuaEv1Cv EMwQ== X-Forwarded-Encrypted: i=1; AL+hErGvSdlAH3m3YlnG1u00VxI/wom/ojphlU0nbW1rxHNH9RekBTOCTF91/iwQIwG3KYd/u9tPwxAUafOhDM0=@vger.kernel.org X-Gm-Message-State: APuU4Z9bgdYdNK/WyMoR9KhRT3tbqBr/NGifv5zXEohsVz686/3MkW/y KliPnce4MQeqNRLuixvIZtUzL7DzJj3lcLgAiOhi6dnn0G/D2us4Fu2Sw1OZ9wG6x20R8qcT19L bMvYWBjDbiQ+EAlvB6A== X-Received: from qvbcv7.prod.google.com ([2002:ad4:4d87:0:b0:921:7ad4:8617]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:ac8:6545:0:b0:55b:90c0:69f8 with SMTP id d75a77b69052e-55d76aa76bemr579445751cf.56.1796075144613; Mon, 30 Nov 2026 13:45:44 -0800 (PST) Date: Mon, 30 Nov 2026 21:44:47 +0000 In-Reply-To: <20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com> X-Mailing-List: linux-kernel@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-2-8b76e65073ab@google.com> Subject: [PATCH bpf-next v2 2/4] libbpf: Support struct_ops in light skeleton loader 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" Teach libbpf's gen_loader to create, load, and populate BPF_MAP_TYPE_STRUCT_OPS maps and BPF_PROG_TYPE_STRUCT_OPS programs using the BPF object's own BTF, relying on the kernel to resolve the target struct_ops type and members by name: - In bpf_object__init_kern_struct_ops_maps(), sync callback pointers from st_ops->data, validate callback programs, and record the local struct type ID and member index in prog->attach_btf_id and prog->expected_attach_type when generating a loader program. - Keep create_attr.btf_value_type_id set in bpf_object__create_map() so bpf_gen__map_create() passes the local struct's BTF type ID and size along with the object's btf_fd. - In bpf_gen__prog_load(), populate attach_btf_obj_fd from btf_fd on the loader stack for BPF_PROG_TYPE_STRUCT_OPS programs. - Add bpf_gen__struct_ops_update_elem() and wire it up in bpf_object_prepare_struct_ops() to embed the local struct value (with userspace bpf_program pointers zeroed out), copy initial_value when unsigned, write each loaded callback's FD at its local member offset, and emit BPF_MAP_UPDATE_ELEM. Suggested-by: Alexei Starovoitov Signed-off-by: Siddharth Nayyar --- tools/lib/bpf/bpf_gen_internal.h | 3 ++ tools/lib/bpf/gen_loader.c | 35 +++++++++++++ tools/lib/bpf/libbpf.c | 110 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/bpf_gen_internal.h b/tools/lib/bpf/bpf_gen_internal.h index c30e05b66abc..6fc3eb02f173 100644 --- a/tools/lib/bpf/bpf_gen_internal.h +++ b/tools/lib/bpf/bpf_gen_internal.h @@ -76,6 +76,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_opts *load_attr, int prog_idx); void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size, __u64 flags); +void bpf_gen__struct_ops_update_elem(struct bpf_gen *gen, int map_idx, void *value, + __u32 value_size, const __u32 *fd_offs, + const int *prog_idxs, int cnt); void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx); int bpf_gen__func_ptr_map_create(struct bpf_gen *gen, const char *map_name, int obj_map_idx, void *value, __u32 value_size, const __u32 *ptr_offs, diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c index 27b4bc9e55ac..749a86c43e7f 100644 --- a/tools/lib/bpf/gen_loader.c +++ b/tools/lib/bpf/gen_loader.c @@ -1093,6 +1093,10 @@ void bpf_gen__prog_load(struct bpf_gen *gen, /* populate union bpf_attr with btf_fd saved in the stack earlier */ move_stack2blob(gen, attr_field(prog_load_attr, prog_btf_fd), 4, stack_off(btf_fd)); + if (prog_type == BPF_PROG_TYPE_STRUCT_OPS) + /* attach_btf_id is a struct in the BTF of the object */ + move_stack2blob(gen, attr_field(prog_load_attr, attach_btf_obj_fd), 4, + stack_off(btf_fd)); if (gen->attach_kind) { emit_find_attach_target(gen); /* populate union bpf_attr with btf_id and btf_obj_fd found by helper */ @@ -1198,6 +1202,37 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue, emit_map_update_elem(gen, map_idx, &attr, attr_size, key, value, value_size); } +/* + * pvalue is the struct as the object has it. The kernel moves the members to + * where it has them. The fds of the programs prog_idxs go to fd_offs. + */ +void bpf_gen__struct_ops_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue, + __u32 value_size, const __u32 *fd_offs, + const int *prog_idxs, int cnt) +{ + int attr_size = offsetofend(union bpf_attr, flags); + int value, key, zero = 0, i; + union bpf_attr attr; + + memset(&attr, 0, attr_size); + value = add_data(gen, pvalue, value_size); + key = add_data(gen, &zero, sizeof(zero)); + + /* see bpf_gen__map_update_elem() */ + if (!OPTS_GET(gen->opts, gen_hash, false)) + emit_copy_initial_value(gen, map_idx, value, value_size, 0); + + for (i = 0; i < cnt; i++) { + emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_2, BPF_PSEUDO_MAP_IDX_VALUE, + 0, 0, 0, blob_prog_fd_off(gen, prog_idxs[i]))); + emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0)); + emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE, + 0, 0, 0, value + fd_offs[i])); + emit(gen, BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0)); + } + emit_map_update_elem(gen, map_idx, &attr, attr_size, key, value, value_size); +} + void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int slot, int inner_map_idx) { diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 61a388868373..e66e848eb463 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1459,6 +1459,63 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map) return 0; } +/* + * The kernel finds its struct_ops and the members by the names that they have + * in obj->btf. + */ +static int bpf_map__init_gen_struct_ops(struct bpf_map *map) +{ + const struct btf *btf = map->obj->btf; + struct bpf_struct_ops *st_ops = map->st_ops; + const struct btf_type *type = btf__type_by_id(btf, st_ops->type_id); + const struct btf_member *member = btf_members(type); + __u32 i, vlen = btf_vlen(type); + + map->mod_btf_fd = -1; + for (i = 0; i < vlen; i++, member++) { + const struct btf_type *mtype; + struct bpf_program *prog; + const char *mname; + __u32 moff; + + mname = btf__name_by_offset(btf, member->name_off); + if (btf_member_bitfield_size(type, i)) { + pr_warn("struct_ops init_kern %s: local bitfield %s is not supported\n", + map->name, mname); + return -ENOTSUP; + } + + mtype = skip_mods_and_typedefs(btf, member->type, NULL); + if (!btf_is_ptr(mtype)) + continue; + mtype = skip_mods_and_typedefs(btf, mtype->type, NULL); + if (!btf_is_func_proto(mtype)) + continue; + + moff = btf_member_bit_offset(type, i) / 8; + prog = *(void **)(st_ops->data + moff); + if (st_ops->progs[i] && st_ops->progs[i] != prog) + st_ops->progs[i]->load_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED; + st_ops->progs[i] = prog; + if (!prog) + continue; + + if (!is_valid_st_ops_program(map->obj, prog)) { + pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n", + map->name, mname); + return -ENOTSUP; + } + + /* The kernel rejects a prog that two maps use for different members */ + if (prog->attach_btf_id) + continue; + prog->attach_btf_id = st_ops->type_id; + prog->expected_attach_type = i; + } + + return 0; +} + static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) { struct bpf_map *map; @@ -1474,7 +1531,10 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj) if (!map->autocreate) continue; - err = bpf_map__init_kern_struct_ops(map); + if (obj->gen_loader) + err = bpf_map__init_gen_struct_ops(map); + else + err = bpf_map__init_kern_struct_ops(map); if (err) return err; } @@ -5615,7 +5675,8 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b map->btf_value_type_id = 0; break; case BPF_MAP_TYPE_STRUCT_OPS: - create_attr.btf_value_type_id = 0; + if (!obj->gen_loader) + create_attr.btf_value_type_id = 0; break; default: break; @@ -9767,6 +9828,46 @@ static int bpf_map_prepare_vdata(const struct bpf_map *map) return 0; } +/* The loader keeps the fds of the programs in the order of bpf_object__load_progs() */ +static int bpf_program_gen_idx(const struct bpf_program *prog) +{ + const struct bpf_program *p; + int idx = 0; + + for (p = prog->obj->programs; p != prog; p++) + if (!prog_is_subprog(prog->obj, p) && + p->load_strategy == BPF_PROG_LOAD_STRATEGY_AUTO) + idx++; + return idx; +} + +static int bpf_map_gen_struct_ops_update(struct bpf_map *map) +{ + struct bpf_struct_ops *st_ops = map->st_ops; + __u32 i, cnt = 0, *fd_offs = st_ops->kern_func_off; + struct bpf_object *obj = map->obj; + const struct btf_type *type; + int *prog_idxs; + + type = btf__type_by_id(obj->btf, st_ops->type_id); + prog_idxs = calloc(btf_vlen(type), sizeof(*prog_idxs)); + if (!prog_idxs) + return -ENOMEM; + + for (i = 0; i < btf_vlen(type); i++) { + if (!st_ops->progs[i]) + continue; + fd_offs[cnt] = btf_member_bit_offset(type, i) / 8; + /* struct bpf_program * doesn't go into the blob. The loader stores the fd here. */ + memset(st_ops->data + fd_offs[cnt], 0, sizeof(__u64)); + prog_idxs[cnt++] = bpf_program_gen_idx(st_ops->progs[i]); + } + bpf_gen__struct_ops_update_elem(obj->gen_loader, map - obj->maps, st_ops->data, + type->size, fd_offs, prog_idxs, cnt); + free(prog_idxs); + return 0; +} + static int bpf_object_prepare_struct_ops(struct bpf_object *obj) { struct bpf_map *map; @@ -9781,7 +9882,10 @@ static int bpf_object_prepare_struct_ops(struct bpf_object *obj) if (!map->autocreate) continue; - err = bpf_map_prepare_vdata(map); + if (obj->gen_loader) + err = bpf_map_gen_struct_ops_update(map); + else + err = bpf_map_prepare_vdata(map); if (err) return err; } -- 2.56.1.839.g22a8290db9-goog