Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp663301rdh; Wed, 7 Feb 2024 16:50:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV0BA9GZ19AidqQWRBV2IXb9v0RoD9EjuU6677ZbrLI+APoO/CVyOpJuzUu/S7BBKpK0SP4gpvHFQRvlQM4ifwEpTlGkiXTnvHgqN8D7g== X-Google-Smtp-Source: AGHT+IHKglq998UP6eD2ix4jBwnWv+F+NZCSqJjkIfqLwbZyQG+h6YxHKuqlWaOUJD3uzoVq02FL X-Received: by 2002:a05:6a20:9394:b0:19e:3c7f:3d2b with SMTP id x20-20020a056a20939400b0019e3c7f3d2bmr9143371pzh.9.1707353439514; Wed, 07 Feb 2024 16:50:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707353439; cv=pass; d=google.com; s=arc-20160816; b=sLpApmY8j7qER3r50RB1EIIjkHEC1V8BpKAd4e5FFOhr1kTsrEv+X5DvasRQrIA4Io hArBaTZuQ9piQ68aZBPCryE8RnJ/Xg94SOAFdo7mxwRUr9QQsu3EXulSJYD+QJR+xUby 1PP1JWg14QQ0FFtC3CuI6H0LcIdmec9ojd+Bu6P43wbWpCJyIJB/c+HTif8i9RHeD+7g Xv34lwYAXD5OnuuyaoIVXmkXJKcvhfucmGSBePJrc2xVLU3ROyVGD3YutljPy7m2BLx1 tzYsmXfd0aygh3MUjMJIhKI6jfAykre7iqkCbcMs8Wm8zUDhUBfpmzbrD+UuNt36njTw 9rJw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=XhnRrncVP4lzPLUrvGryxvbt7W38XLsNAzwHJ2tdxwE=; fh=EQLvN9rFb11O19YsTP0r0Qs6v2wRtY0Cp3UVAbNIfsU=; b=UNDanUMPN4rn2eMMe6baP6Wpy1xiyBeeDFO5Wh/+mcAniAq+PIL10ArZrh6f8lz+yj lpRBk+rCCN7pDQ1ajKte5jphxAhr8L+0f+G/WGK7ugOcn7FioOGBlAgWghHMuIz4FzWx fFD+wxoYdq2xzTDwfliHHvzBrVNe04Wkh5ridCyeBzdCaGlBeXaLdyr6uNrAQZjSvtSl DWpKwEzujP63YTU3AL4lCg1cAB4rjj1fJIabuaPi+gB6B5ajJ9PCOCFKtVETblCC8uv8 ru8cWVEDow7JgrZmZ6n+Ap/br1lONKPcZXMJZIESnZmdSwKFP+GitFnet8a985SijyGa La1Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bv4AqPGC; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-57355-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57355-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Forwarded-Encrypted: i=2; AJvYcCXSQrZrt+YY5ExRXMcfux71cyZGgICh/OU0Vidm6Zui8cpP1M71qMAo+Uo/RaXXr6UoMetzQsQx1knU5k/fXtEl4ZZSCjzJWbBegPwRyw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id f18-20020a170902ce9200b001d944e92bebsi2973560plg.326.2024.02.07.16.50.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 16:50:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-57355-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bv4AqPGC; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-57355-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-57355-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 28D442871C4 for ; Thu, 8 Feb 2024 00:50:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C9A411CB8; Thu, 8 Feb 2024 00:50:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bv4AqPGC" Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D920214267; Thu, 8 Feb 2024 00:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707353430; cv=none; b=N0/1c0/80JhMrcSZdWxSaeH6kgS0DPsE5HmZnTVRmMWH7Rsd5gJxUGY9ESJZiB+fWiudofmbOdy4HKohUbrvb5ODRHm/R/TPHx8W+YrqXq5GK0M863arHQnId101AFkOllXt1mlSJbvIQuY86H3sQTUwUiqmrmrKXwVfRIZJJy0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707353430; c=relaxed/simple; bh=nXpn5BlvGSY0sc64IXz6i7AkTu8HDnw+rRcIZuYB39M=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=PUHcbecSn9astc/NfXDvETA+VG7/iTg2TEzAkNOI2bel/9BOgwjMwkXQhGNo4vf58RN3DLMMHaW5YkoJW9NayrPgBwo/1sHZ6w4OKTdOdSdjgIGcVsNLB8/vA4LI+mGO2aNfYkmtFGAUxoCNa3gEw9nBtZs7458ySz/lRVs2nKc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bv4AqPGC; arc=none smtp.client-ip=209.85.167.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3bfedaaeeacso675466b6e.0; Wed, 07 Feb 2024 16:50:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707353428; x=1707958228; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XhnRrncVP4lzPLUrvGryxvbt7W38XLsNAzwHJ2tdxwE=; b=bv4AqPGC104QzX5+m5JWIuEtk1ITVITOczNCVZQuc0ix0puVrBkSGGEYLFhB50lEv9 oR7d6K0MogepQQcsyRL8mSdOx8dzHo1cSdqE8+b03pFrQM6K+m8g8L8KfvrsKZzsDf5H GcV3fc/RKRmHxQouydKa13aPtTO0pG5ohct0iAxUG7SZ2WXO2dDqdqmPPGgQjeU3vXoH LMDjJ0U8KzH0t9NAmvB56dSrDzhA3AEewMXK/r9UUkWfcAaZptQQganeLFQLGhOP6+N2 tW1CZ9zZYK1GwJZNqope5x5z6v0j9k76PgU8GFjDEFhJC63Vp0HfdI2G5xiZDxt/VDVD dP9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707353428; x=1707958228; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XhnRrncVP4lzPLUrvGryxvbt7W38XLsNAzwHJ2tdxwE=; b=o/DNrr6A35XBuJXoA7DzaTwc8Z+FEsxEtrdVOIh2kRG4zkLDZftp4ZOhCZzUKvosSn vgPZjPV16Jo0Rw9wQbMf7JeUOoRXhEXvbojizkyEGyRbPmNwwQ/o9rZFoEzYW7EUn+KU IKIvff6oZ3HOSg+aGH24EBC49Znhorr9h0aU1vmQrYEzuWTjqC5ggbc4lXGa3r0ePOGu yo17eZrVt/EetAgHZ7D65/LX6keHvzbMMPsgBMNYHhF0n+/PtVcEEBlRqH0Tg7YbmTHC dJ/hr7CsvgUXYzTqJAfpGHrMDCfXB9Ih35+Aibgx6P/DGuDtCh/KQ2S1YcUgCNbpa1jU ZCxg== X-Forwarded-Encrypted: i=1; AJvYcCX0t+aycd2gi1kWQ+0cd3WUSFGAT0Vg0bs29I0Bji+VPntxWRMggYB1sCwx0pgKU1NmiIxRfQx3NoWw0ULRoxBhdq0iV3BdHJDrOpCP9EZadAXe7hKzixvI5+95UvOEsL5g X-Gm-Message-State: AOJu0YwRs3XGRaaQBFDlal7m+u3JmhRQ1dJUEOX0PD4rYvuEaTfGMySo Nue8H1523632apJtBKKCetJDwfoZxWv2BQtx3s1UqBZ/JCMDwvh3yf0sbISbrn4zoEji/VkW90V +7K2iYkiEDbbhmNss4rT9/WxDa9Y= X-Received: by 2002:a05:6808:22a5:b0:3bf:e45c:cd6 with SMTP id bo37-20020a05680822a500b003bfe45c0cd6mr8387192oib.26.1707353427806; Wed, 07 Feb 2024 16:50:27 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <9b8ebd13300e28bd92a2e6de4fb04f85c1b6ce7c.1707080349.git.dxu@dxuuu.xyz> In-Reply-To: <9b8ebd13300e28bd92a2e6de4fb04f85c1b6ce7c.1707080349.git.dxu@dxuuu.xyz> From: Andrii Nakryiko Date: Wed, 7 Feb 2024 16:50:15 -0800 Message-ID: Subject: Re: [PATCH bpf-next v2 2/2] bpftool: Support dumping kfunc prototypes from BTF To: Daniel Xu Cc: quentin@isovalent.com, daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org, olsajiri@gmail.com, alan.maguire@oracle.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Feb 4, 2024 at 1:07=E2=80=AFPM Daniel Xu wrote: > > This patch enables dumping kfunc prototypes from bpftool. This is useful > b/c with this patch, end users will no longer have to manually define > kfunc prototypes. For the kernel tree, this also means we can drop > kfunc prototypes from: > > tools/testing/selftests/bpf/bpf_kfuncs.h > tools/testing/selftests/bpf/bpf_experimental.h > > Example usage: > > $ make PAHOLE=3D/home/dxu/dev/pahole/build/pahole -j30 vmlinux > > $ ./tools/bpf/bpftool/bpftool btf dump file ./vmlinux format c | = rg "__ksym;" | head -3 > extern void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) __= weak __ksym; > extern void cgroup_rstat_flush(struct cgroup *cgrp) __weak __ksym= ; > extern struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)= __weak __ksym; > > Note that this patch is only effective after the enabling pahole [0] > change is merged and the resulting feature enabled with > --btf_features=3Ddecl_tag_kfuncs. > > [0]: https://lore.kernel.org/bpf/cover.1707071969.git.dxu@dxuuu.xyz/ > > Signed-off-by: Daniel Xu > --- > tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 45 insertions(+) > > diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c > index 91fcb75babe3..0fd78a476286 100644 > --- a/tools/bpf/bpftool/btf.c > +++ b/tools/bpf/bpftool/btf.c > @@ -20,6 +20,8 @@ > #include "json_writer.h" > #include "main.h" > > +#define KFUNC_DECL_TAG "bpf_kfunc" > + > static const char * const btf_kind_str[NR_BTF_KINDS] =3D { > [BTF_KIND_UNKN] =3D "UNKNOWN", > [BTF_KIND_INT] =3D "INT", > @@ -454,6 +456,39 @@ static int dump_btf_raw(const struct btf *btf, > return 0; > } > > +static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf) > +{ > + DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts); nit: use shorter LIBBPF_OPTS, DECLARE_LIBBPF_OPTS is a "deprecated" macro name I hid, but didn't remove > + int cnt =3D btf__type_cnt(btf); > + int i; > + > + for (i =3D 1; i < cnt; i++) { > + const struct btf_type *t =3D btf__type_by_id(btf, i); > + const struct btf_type *kft; > + const char *name; > + int err; > + > + if (!btf_is_decl_tag(t)) > + continue; > + > + name =3D btf__name_by_offset(btf, t->name_off); > + if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG))= ) > + continue; should we do a bit more sanity checking here? Check that component_idx =3D -1 (entire func) and pointee type is FUNC? > + > + printf("extern "); > + > + kft =3D btf__type_by_id(btf, t->type); nit: reuse t? > + opts.field_name =3D btf__name_by_offset(btf, kft->name_of= f); > + err =3D btf_dump__emit_type_decl(d, kft->type, &opts); > + if (err) > + return err; > + > + printf(" __weak __ksym;\n\n"); why extra endline? though I'd ensure two empty lines before the first kfunc declaration to visually separate it from other type. Maybe even add a comment like `/* BPF kfuncs */` or something like that? > + } > + > + return 0; > +} > + > static void __printf(2, 0) btf_dump_printf(void *ctx, > const char *fmt, va_list args) > { > @@ -476,6 +511,12 @@ static int dump_btf_c(const struct btf *btf, > printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n"); > printf("#pragma clang attribute push (__attribute__((preserve_acc= ess_index)), apply_to =3D record)\n"); > printf("#endif\n\n"); > + printf("#ifndef __ksym\n"); > + printf("#define __ksym __attribute__((section(\".ksyms\")))\n"); > + printf("#endif\n\n"); > + printf("#ifndef __weak\n"); > + printf("#define __weak __attribute__((weak))\n"); > + printf("#endif\n\n"); > > if (root_type_cnt) { > for (i =3D 0; i < root_type_cnt; i++) { > @@ -491,6 +532,10 @@ static int dump_btf_c(const struct btf *btf, > if (err) > goto done; > } > + > + err =3D dump_btf_kfuncs(d, btf); > + if (err) > + goto done; > } > > printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n"); > -- > 2.42.1 >