From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146428; b=pMmJEn2d67zoKD5eykjJuGlp8Jg/eeJf3HvMdkusgDnQ7GsIS5w8yLVTbBTJbu4/uABn wNIM+rhaPu+wdGjQ086salKTFBCguL3HYlIpHC9DvuAYPLC8iQ0uEYAHSCL++UjINBzWb A+vhny4/sBJiXcVDNPwfLIzbB6aIi0gJbvo3ITklNYsAUafcvcFZOTLBqu3TwOcdBCXsu oD+NG8aEqIVmjohCdlA9qQDeoTrJshaxPUYbgg/fnq92tC7npu0Y0yHgTT5QITIDx7i2m VPwXg+UJZJfHBIsN8Ele8WcspBNdMd1NdOwN6K6HzL0lGsYDBo80KUNA4rCroe4JtHw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146428; 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=fCYKewa12guATpvCSV4HqK+7/Ev9oBR3YSBZiZHpvFPwjaLFS/uL/5KC1m4AJQ77AAUx pPwa3u5+lkPo3hz8ges42P0E0lMqG7ypnsCK+4lQm4sjo3wKtPochrSkcyCfBz9KStC6r fO54WV3ZUHWPSkE4sTEkx4MrjWRZbucxo+H1hcGdduQwxhCpIHmsByCBaTSnQYrbBnHK6 V5JvuS52MqOnE1m2O74Qp9mTnulBqqm2JDdSgKyBkBFwinNHjtX9hF++Am1/DdR13Lgrx Pg5la8hgn81xnPR6aS525Jg1/nICtR3E0r2m+zZtV66CHpBFIr1lrrC2FPr+BLRKKOg== 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=DkgyNftT; arc=none smtp.remote-ip=209.85.221.200 Received: from mail-vk1-f200.google.com (mail-vk1-f200.google.com [209.85.221.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 6BDBE606A334 for ; Tue, 1 Dec 2026 17:33:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6BDBE606A334 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=DkgyNftT Received: by mail-vk1-f200.google.com with SMTP id 71dfb90a1353d-5ece3cd5a00so7546158e0c.3 for ; Tue, 01 Dec 2026 09:33:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796146426; x=1796751226; 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=DkgyNftTBGcaOFR4TXNn1T6vAKNzzi4/U+8uSMvwaziIXalP/vnrp5BP5BX5jVdF9y 2yS5Ij/J4eLwO4i7aJnnLJEIufCCK4hWqXWtI92A8CzJ38Hmv1MItWoGIrZNNQSmKOOu KztMXVdKBnP00Ja7U2MukngKg4vcUhQDW+mxiavsFd8lhy2YuIdxNfgaiSPiFCoSn1eZ svN8RlI5/yXllgKJfnc205ArHT08KzUINZjYosS+u1OubXUInFJDsp33NkHuskRGOd5y W+n+9M5taKNwa8NoTbxvHmDd1b6fzUJmmfogWuVhRUnoWBSjf0ajTf7anf47aBBDuq27 Nshg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796146426; x=1796751226; 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=DsCgJrdpST/hHwlad8NjoX3c7iVD9zk5QcBXISVF/ErOKYTOjjD4BfOO06eE+fPXNU rlm/9/jCVXPY/lwKUIPswIELxmV3Or1/UNtJEP1EpXjKGzQjSlQ2v+KjzUbxosoDvda1 A7YmxzY9mtQ302Wv+hftz54yYGthjeUbpRtA6p5Z4sK07A4capnqRuJ6CXdw0if9dXI8 XLxWaA56vayzoBbTubJ/P7+oLt3b4NIKatspwbNmxpHQJAs5p3M5lIROCLf9sY8Z61l+ cypr26m6uA0OQSsXBevvhNLJCFCd68tGrIQKxA1z7+4pYTnMRyDr42WgC3LfJjnYx/Rc RmFw== X-Forwarded-Encrypted: i=1; AL+hErFxqDGGlu0a3WBrnXLsS+VAv+ujozyyJNQuIB2ON5LolD356Ic3sfZ51s79CrTfof/Ij/RrSuW1s4rTpOg=@vger.kernel.org X-Gm-Message-State: APuU4Z+t2StkNmiFiPO1fz2BFtyb5bxLrg1qU50qph8wf3fz//mdI0Ij cAoo4dgtD8s+FYuKTtnT8jYPNJW0AQUXw/SntIxljp5IltV5I1DZPqYo4wvLQB6CcFCyRKrnD1t DCwRwGwRw9Hse138RiA== X-Received: from qvcd14.prod.google.com ([2002:a05:6214:630e:b0:91f:3a14:b0ce]) (user=sidnayyar job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6122:4690:b0:5ea:bda1:7689 with SMTP id 71dfb90a1353d-5ee8af0b00fmr26630553e0c.4.1796146426047; Tue, 01 Dec 2026 09:33:46 -0800 (PST) Date: Tue, 01 Dec 2026 17:33:39 +0000 In-Reply-To: <20261201-lskel-struct-ops-v3-0-057f3d23ac0c@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: <20261201-lskel-struct-ops-v3-0-057f3d23ac0c@google.com> X-Mailer: b4 0.14.3 Message-ID: <20261201-lskel-struct-ops-v3-3-057f3d23ac0c@google.com> Subject: [PATCH bpf-next v3 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