From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796075147; b=ptBVUKsLlO1NJBVmYLb7nQdOJHAtaOlSZBmcODmSOJd9+pwg1CLlkY9L7r/h/p0PEGvB ap0+69LAkOYKEV6HX24iqSi/XMYBX+nykBQjbmsqjAgYt74o5G14EgOeNIwm6jOlRGGE7 GRpcr9T+Qzy21X0PvVMyCTCG0FRksjnVDYp3v1wbn5BzklU9WecPchhbzWCR4Ju5Go42/ 5omLqh8QvyEenZVsi/XrClwkQ3YJKfAe9I0P/AB3YRuThKOZQtrgKYMaNj8BfbIKJwEIe AYd2uPX8giJX+/f9T7nAM4/6gHEgCzrUxGoh/nu6cKSIt0pz2wlZvkcDPmiRgN+iufw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796075147; 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=mwEAj4vimadmP7PqVR7e1/KGkFch7VkqjABSknI45J8=; b=mdlfm5HeX+K4lu28va68FLphnd4goE0RjASTC6TPfbKklSsUXAacoJJPSH/s2gb/6e6r Afo6j9ejVjWj4M8EmKtCoQq930QHamk/NbTBQ0l40aDDq1DrU1LtaEVwtCplCXatef3Sw 4qtvPPDaGabJuvmD5apv2lJJPpc4SvXS16IQoM4ljBJxrwOfin32mBZ3OWqwvsknl48QC KccUcvtZgZgEGDEh18G8xYsb6oZobTk/v/DwiCryEoI9GurReLUcfgkYQsDSVZDk7D6YM mnOv+4rwmM+CzJt/W35UTSNQHPV0pJxgowNx9WRpY+zVptp7Vshp/YaCsj9fJ0A8g6w== 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=Sqnz9lXu; arc=none smtp.remote-ip=209.85.128.198 Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.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 ED53F606A690 for ; Mon, 30 Nov 2026 21:45:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ED53F606A690 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=Sqnz9lXu Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-8b18c67095dso146076207b3.2 for ; Mon, 30 Nov 2026 13:45:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796075146; x=1796679946; 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=mwEAj4vimadmP7PqVR7e1/KGkFch7VkqjABSknI45J8=; b=Sqnz9lXuE3bnuXUvA9lgA3KcPLR5jRflPDhnh5DNxMFL/QDoqB0gMO4EjcE1mwdaGZ yD5kcgEBkmIGHVOVsvtlRjT7sCXbk7FHJANvWUYB02sGekU/GVJ2fRb7DO5SRjn0blNv /bEUlz5lnHZQemkEF/O/s4hR7dEXIhaWVYVscI0GfFgPV5A15CcSy97K2gtXBxztnCoq gYAXwCf+19YxIudqQw3tZMxRKoetoZntSjRTV19xzXZ5AMQ831foIyiexGtokfd1uaJs F6os8dzVCeEb8uX8FbRuQ+VbTjpcGdMiKczqr/h582XYbMuYbrDMBcHj76aSvRf3oG9o ff3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796075146; x=1796679946; 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=mwEAj4vimadmP7PqVR7e1/KGkFch7VkqjABSknI45J8=; b=GwIZiBsGZPUzDXxPeWz6PkY9pdBhDfMGtr3xRmAQMTg4UtioggF2FCSZwueYL1Iauy +0eRo23dR8x3ts43XKUILwTyCfhfm4cAEmAsPGZ8gLWmogjLT3qSfmxNTLD9ctgRPHkz hRF5GTqcH9vO+h8o4Z8lwcFI/FGA0sTxTPpJUqcAGC7wx1Lc+QIVuPZYeTcGP89FEd6a qbBapNpHjVNjtG6RNEpQUt43IrP0VQBzoKTb/I2NUFuJdPyA1dGM8DWQEN1Wi/52V2l/ Cb7GM3uzprl8gS4fH3y2njPsDMT4f5HARHdGhQjqvjTtaXaLHgbOAVZbTsg88W49CJSO 7FBw== X-Forwarded-Encrypted: i=1; AL+hErHsYAGkAZNDWbxBkzwc+Ni2gBl3wQLWjRm2catnzgZUttwVwp4eISXNFSSHLmMJ97d8Y3dDFeBk4m+GhkU=@vger.kernel.org X-Gm-Message-State: APuU4Z93641DLZZTylAsJz1tnLQuga5m+ET6yXLUPRsy9+0ot052v7pP 9tbUPVhxfd7QTzukv9FDJZnNxtivVxmP2Kqrx427jssHb24B+Zq7qIUH7bza6/gxFuBCve5LOGy jmi//Xz52fLHshfDk0A== X-Received: from ywbgf4.prod.google.com ([2002:a05:690c:4184:b0:80b:2889:c46]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:6e12:b0:8b4:cdcb:8e25 with SMTP id 00721157ae682-8b52050ef2bmr146734037b3.61.1796075145738; Mon, 30 Nov 2026 13:45:45 -0800 (PST) Date: Mon, 30 Nov 2026 21:44:48 +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-3-8b76e65073ab@google.com> Subject: [PATCH bpf-next v2 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons 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 Content-Type: text/plain; charset="utf-8" Update codegen_attach_detach() to iterate over BPF_MAP_TYPE_STRUCT_OPS maps with BPF_F_LINK set. Generate dedicated per-map attach functions calling skel_link_create(), chain them in skel__attach(), and close and zero the corresponding link FDs in skel__detach(). Also skip gen_st_ops_shadow() when generating light skeletons so skel->struct_ops is not emitted between skel->maps and skel->progs, preserving the ctx layout expected by bpf_gen__finish(). Finally, reject struct_ops maps without BPF_F_LINK in light skeletons. Because the loader program runs BPF_MAP_UPDATE_ELEM during skel__load(), non-link struct_ops maps would register prematurely before skel__attach() and leak in the kernel if a later load step fails. Signed-off-by: Siddharth Nayyar --- tools/bpf/bpftool/gen.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index ee680f7c2116..da5f7c9d99ab 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -569,6 +569,8 @@ static void codegen_asserts(struct bpf_object *obj, const char *obj_name) static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name) { struct bpf_program *prog; + struct bpf_map *map; + char ident[256]; bpf_object__for_each_program(prog, obj) { const char *tp_name; @@ -608,6 +610,30 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name) ", bpf_program__name(prog)); } + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + if (!(bpf_map__map_flags(map) & BPF_F_LINK)) + continue; + + codegen("\ + \n\ + \n\ + static inline int \n\ + %1$s__%2$s__attach(struct %1$s *skel) \n\ + { \n\ + int map_fd = skel->maps.%2$s.map_fd; \n\ + int fd = skel_link_create(map_fd, 0, BPF_STRUCT_OPS);\n\ + \n\ + if (fd > 0) \n\ + skel->links.%2$s_fd = fd; \n\ + return fd; \n\ + } \n\ + ", obj_name, ident); + } + codegen("\ \n\ \n\ @@ -625,6 +651,20 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name) ", obj_name, bpf_program__name(prog)); } + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + if (!(bpf_map__map_flags(map) & BPF_F_LINK)) + continue; + + codegen("\ + \n\ + ret = ret < 0 ? ret : %1$s__%2$s__attach(skel); \n\ + ", obj_name, ident); + } + codegen("\ \n\ return ret < 0 ? ret : 0; \n\ @@ -642,6 +682,21 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name) ", bpf_program__name(prog)); } + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) + continue; + if (!(bpf_map__map_flags(map) & BPF_F_LINK)) + continue; + + codegen("\ + \n\ + skel_closenz(skel->links.%1$s_fd); \n\ + skel->links.%1$s_fd = 0; \n\ + ", ident); + } + codegen("\ \n\ } \n\ @@ -1323,8 +1378,15 @@ static int do_skeleton(int argc, char **argv) continue; } - if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) + if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) { + if (use_loader && !(bpf_map__map_flags(map) & BPF_F_LINK)) { + p_err("struct_ops map '%s' requires BPF_F_LINK for light skeleton", + bpf_map__name(map)); + err = -ENOTSUP; + goto out; + } attach_map_cnt++; + } map_cnt++; } @@ -1385,9 +1447,11 @@ static int do_skeleton(int argc, char **argv) } btf = bpf_object__btf(obj); - err = gen_st_ops_shadow(obj_name, btf, obj); - if (err) - goto out; + if (!use_loader) { + err = gen_st_ops_shadow(obj_name, btf, obj); + if (err) + goto out; + } if (prog_cnt) { printf("\tstruct {\n"); -- 2.56.1.839.g22a8290db9-goog