From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796075147; b=5YvXq1EcXsiNxDCJWmM7YcNdS5Z5kIXQxb/Mosw5eUyOIbhjS1OX2RJ1snXZkmJ3vXMC IcGrPWXJcFLA82wpjbbKRulSAZMsOai1jKOSkskZYx9F9S0/y9X3Ov038uDMyrZhAfBWN as4MXr7eZyq0UfHSkxVo5FIq/XVAR1LhbNmLbNbU9A/Qemlnu2b1HeJUyV49+xlVMNlED vigxhNvhwajBvAv6t0sFb+tDekxnrZoh2JLg5TTscDgo09eTp1ZMEmWCxzPjxcosVDdss M6+imxVnV7Wqu5+bmrvbS+LveY0QZjZDsXzWHcvNrrbGcHs2oBhnNYaV1JB3DwI47mw== 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-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=4xSHiaT5U65gsH+JnANsYTWjq920qnwKDILugB0N7u99xGPZT/PvNU4hkP/QAaK2gzhB xA/dQEIdFpzzZtmGkiK9aAqVIP0974hYPRlVMhktiull0tm4HXCqJ3S484vSiB0UZhGLF +6VPjis0O5rJuy0XV6SLlHPB0pqXMBkcrHazM8DEXJDkQjBpcDO9BTMym/rHt9/2dhuhX BpEFViAJdvF3U4thFwELi4AKRM/U8PRjpAxOC053znzIQQeAQZ6Q4E6PTpG+xmKhR9apg Ma5z+cUZ4YGQpLq3I3B8IxtndlRfh3V6wA7mhfa/YZHu1mzy1ax6GT54YqNZ8Pn1wEw== 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.199 Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) (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 0042A606A693 for ; Mon, 30 Nov 2026 21:45:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0042A606A693 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-f199.google.com with SMTP id 00721157ae682-8b18c67095dso146076217b3.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=Pykt8OkA+F1RBWVUZGDF5lM/mXtWmPm/GxElVXtWRX2AAkKmpP1pAztGW4s/4vDQ5q GlDWyXZ7cIifRtIn/SVj/siqeIdgPuZMa6Uw291+dF3Hj+DBN4yJttbhScT5Rp1RdG/7 4lt/f4F5r0JyKJAvQPf6WlayjLBa4VbI0eYY+GLfe0QA0W+v8ql6Ua8gcW5CGrJMyeky nCJZDCY9v3ESmU03rVjVuI7+9G8pLbN52rCX4trdWFAe+64mzsAGWPphwKibNN6Hd6vQ nwNF9ZEQxYJeGRvJzlFwRShlP94uDeL97bgr2rox8l/Gf8XCM2P+EbEL7/YGz0paXKMt cHpA== X-Gm-Message-State: APuU4Z8Q6FISdDeQfEhVi+wcazZ0wmEv+IaZn5oYcnKBIXX3CE+b7uZ3 X7X4U7V6Ufk43FaLNMmLzavc3tWptJV4GcVeMQtCBp3L+Kxorh16XnJEg04tGmnxeC4GG+HwZz2 Ko3Se6l+qi5sLZtF7KA== 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: 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-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