Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1453896iob; Fri, 29 Apr 2022 05:48:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxp6/PvqRyR2X2yX3aniwz5iaMYisY/kbPmfjdD1Ckb5ws/vdGAuE2rSjV8LYF3YkuurwGI X-Received: by 2002:a17:902:8214:b0:158:b5c2:1d02 with SMTP id x20-20020a170902821400b00158b5c21d02mr39469653pln.27.1651236496462; Fri, 29 Apr 2022 05:48:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651236496; cv=none; d=google.com; s=arc-20160816; b=jwH4eCxmcSrjMy0h4tGitQ9KWAiLpGlXBzXMlcpju2jRVeSmshw4PV20aPV1fZ0I25 uHx3DLfOeWjInJku5Q7AGyH5ZDXBnq8zfY9Wnf5jDIjuXy7OPMx/InJ58uxUtAx8rnmV nrrtvKzXn1ebypGW49i7yK+QYP6EjV0y7EICjSvfXak8FwF/qfMuhmSivVR6xVrwsDVN rx+FdnS0rtvk3FQ2FZcsDbhvniBcDghp3+LdeXdrxE871lZjWnAsXP2y1zICDcy6ubCs ZDZ6OmqW2Bc/xIdNi6pCmnwDgYTfYd97/1EDlg7OApU0/v6hi8DpieamCFqC+iv+Btu2 8eyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=C/TaLoolfNzNNtDFWtWykoil81y3fx0B/GtBAYMoTB8=; b=YJ5WcgwKez0qj7OsqKPAKFheBg9ddx02KyrMbWrPFKaWVhIzkUvE09SNOPfeokbatj i6eaGsdoWhjf47XDlAkRKHim6cJiQ45EVjQ91TIpyVXW57V+WrxnTrmoeauRd6+tt+Tp jxv7kTiQBEwqXIU2eKkU+tIJmiDS+RimYmwYYdyub+AUQeWq7DXf9AfYBJnMCjpf2H6o oGpltwdzgwl+GLS29ijl2c1CWYXzQepT0ffK5YQdukjbwtxDWNddCkU7VQG4A81pqOya y68XhvdVf1W/kunotxDMgN5HiLzobzi7bENaSmcJrSStIdkQxybmdXY1V22yMtiB/l8u kBeQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t133-20020a63788b000000b003ab2f093e45si7444906pgc.597.2022.04.29.05.48.00; Fri, 29 Apr 2022 05:48:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348842AbiD1PVc (ORCPT + 99 others); Thu, 28 Apr 2022 11:21:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349146AbiD1PVN (ORCPT ); Thu, 28 Apr 2022 11:21:13 -0400 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EC7BB36AA; Thu, 28 Apr 2022 08:17:51 -0700 (PDT) Received: from sslproxy06.your-server.de ([78.46.172.3]) by www62.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1nk5tl-0007Y6-IA; Thu, 28 Apr 2022 17:17:41 +0200 Received: from [85.1.206.226] (helo=linux.home) by sslproxy06.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nk5tl-000VU1-8h; Thu, 28 Apr 2022 17:17:41 +0200 Subject: Re: [PATCH RESEND bpf-next] bpftool: Use sysfs vmlinux when dumping BTF by ID To: Larysa Zaremba , Alexei Starovoitov , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , Maciej Fijalkowski , Alexander Lobakin References: <20220428111442.111805-1-larysa.zaremba@intel.com> From: Daniel Borkmann Message-ID: Date: Thu, 28 Apr 2022 17:17:40 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20220428111442.111805-1-larysa.zaremba@intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.5/26526/Thu Apr 28 10:21:25 2022) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/28/22 1:14 PM, Larysa Zaremba wrote: > Currently, dumping almost all BTFs specified by id requires > using the -B option to pass the base BTF. For most cases > the vmlinux BTF sysfs path should work. > > This patch simplifies dumping by ID usage by attempting to > use vmlinux BTF from sysfs, if the first try of loading BTF by ID > fails with certain conditions. > > Signed-off-by: Larysa Zaremba > Reviewed-by: Alexander Lobakin > --- > tools/bpf/bpftool/btf.c | 35 ++++++++++++++++++++++++++--------- > 1 file changed, 26 insertions(+), 9 deletions(-) > > diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c > index a2c665beda87..557f65e2de5c 100644 > --- a/tools/bpf/bpftool/btf.c > +++ b/tools/bpf/bpftool/btf.c > @@ -459,6 +459,22 @@ static int dump_btf_c(const struct btf *btf, > return err; > } > > +static const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux"; > + > +static struct btf *get_vmlinux_btf_from_sysfs(void) > +{ > + struct btf *base; > + > + base = btf__parse(sysfs_vmlinux, NULL); > + if (libbpf_get_error(base)) { > + p_err("failed to parse vmlinux BTF at '%s': %ld\n", > + sysfs_vmlinux, libbpf_get_error(base)); > + base = NULL; > + } Could we reuse libbpf's btf__load_vmlinux_btf() which probes well-known locations? > + return base; > +} > + > static int do_dump(int argc, char **argv) > { > struct btf *btf = NULL, *base = NULL; > @@ -536,18 +552,11 @@ static int do_dump(int argc, char **argv) > NEXT_ARG(); > } else if (is_prefix(src, "file")) { > const char sysfs_prefix[] = "/sys/kernel/btf/"; > - const char sysfs_vmlinux[] = "/sys/kernel/btf/vmlinux"; > > if (!base_btf && > strncmp(*argv, sysfs_prefix, sizeof(sysfs_prefix) - 1) == 0 && > - strcmp(*argv, sysfs_vmlinux) != 0) { > - base = btf__parse(sysfs_vmlinux, NULL); > - if (libbpf_get_error(base)) { > - p_err("failed to parse vmlinux BTF at '%s': %ld\n", > - sysfs_vmlinux, libbpf_get_error(base)); > - base = NULL; > - } > - } > + strcmp(*argv, sysfs_vmlinux)) > + base = get_vmlinux_btf_from_sysfs(); > > btf = btf__parse_split(*argv, base ?: base_btf); > err = libbpf_get_error(btf); > @@ -593,6 +602,14 @@ static int do_dump(int argc, char **argv) > if (!btf) { > btf = btf__load_from_kernel_by_id_split(btf_id, base_btf); > err = libbpf_get_error(btf); > + if (err == -EINVAL && !base_btf) { > + btf__free(base); > + base = get_vmlinux_btf_from_sysfs(); > + p_info("Warning: valid base BTF was not specified with -B option, falling back on standard base BTF (sysfs vmlinux)"); > + btf = btf__load_from_kernel_by_id_split(btf_id, base); > + err = libbpf_get_error(btf); > + } > + > if (err) { > p_err("get btf by id (%u): %s", btf_id, strerror(err)); > goto done; >