Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1986344iob; Fri, 29 Apr 2022 18:27:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyg69FGUZR4QhD4jh/lXAfrjXqvaBsOKHocKREI8vj4nKEZ6r3y1C+Sr2gMKyzhWM/d+ni8 X-Received: by 2002:a05:6512:3fa1:b0:44a:f351:5a00 with SMTP id x33-20020a0565123fa100b0044af3515a00mr1446807lfa.19.1651282033088; Fri, 29 Apr 2022 18:27:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651282033; cv=none; d=google.com; s=arc-20160816; b=HnZ86FXDVx0z2fnzfiazEIsmjQE8b0N1/q0JH8M4mqFdFgEfWaUs/wYKhalcI91rjD eJ+fkP+lOqEDId2OQU7fFe57rNtqp/gkwkHnMe0BbmaERb9irEu/ley2piCjkzzUxLSt 080fe7+8MvJzDFlG8tWWhzPhRaYyv8FZlawCogMX9K946qOqFwCbimS+L+j4l56kIshT pK1vjYfCQUHQhz+yVV/+mN84gpeSCJbhtdWt/vlFRBrdYFWQnjBees/FYvkX/G77c4sZ SF1KWBA4QLHmrzLebIC5d0fK+POcWTO5vrFm+K1JdD2RIEvhg9SaFLlGTBbFxj7XICeK O3oQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=TP9FOM/MARt2Tvknd5/CbQC+6EmkUZ/cjkfr5a5DNsM=; b=aPX6Q7wWxq78YoG0oPEtY5IzfbJRfARNzrVHkZ4oDLCQ/k4sVLrhnZ9Nk9It5m9mTl nlGwu/rd3QXO5z95vAgVh3at0ke664jRcOmpyFG1/w/9CDwe3vkRIykgkDxEZHj1nE7T EvZhWuKJ+WrYfD7RNpW8puqhUDc/fgV3lowdLBsZX/lwCLMySSS4RGYRt60paSrWKcD/ ts7DgHz0nynmTgN8ocRZ3+0PhR3p4SBpOZnLgJc7UqfEXsYHIjGX4BQBt8gycyiVJQ7X GrQlonx1hmlTY5Kxd1zrPpKmQdnUShni7rut3rskdDdyJ9NcoSvfjkykX3RRmanG8lP3 F5TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=p1RIC0UW; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w7-20020a0565120b0700b00472187b57f8si10094306lfu.78.2022.04.29.18.26.46; Fri, 29 Apr 2022 18:27:13 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=p1RIC0UW; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238172AbiD2FDy (ORCPT + 99 others); Fri, 29 Apr 2022 01:03:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238106AbiD2FDq (ORCPT ); Fri, 29 Apr 2022 01:03:46 -0400 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C95C82B19B; Thu, 28 Apr 2022 22:00:29 -0700 (PDT) Received: by mail-io1-xd2c.google.com with SMTP id f2so8407089ioh.7; Thu, 28 Apr 2022 22:00:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TP9FOM/MARt2Tvknd5/CbQC+6EmkUZ/cjkfr5a5DNsM=; b=p1RIC0UWdZbUky9WniEr1CIS+0mf7VJfeHzT+2KRXkz7q5BMIyhW5itqZZMW4WmmL4 Oj+XOM4135a3zZX70YKL0vYcyerHCnnB35dmRnB5zNW1B2Inpo+86CQQm9qna1H4cOiE OaSVkqPajU7HJmUNk6ghdIvYyCev6nE/VZOJ4a2m8ic14ecqK2hfyxHdIHrjP2spBLlZ sphfaChR1Sg9C5wuMCYxYUQOp/R7Y2zP0gqD4jwAC3Eop4AN1fv0SIT3b6UMohT1WHHo nUAeRSm3U8pnwoJB2IEaveZG3wT8AOQBTBfWPRKfycV3BkklZRMB/7zLriDz3uTmSiQv 38gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TP9FOM/MARt2Tvknd5/CbQC+6EmkUZ/cjkfr5a5DNsM=; b=XTvx0a1xHsRIZTpx8Cmx9Tw4owGNJsdHMLjnu8q+wl8gQk+vc5lDNnFN7XShiRMY0F bCIu93yBg5x4tRHjk2ndxepaAZCNV6sX4FmHyPFie3AOT+FGGXVwakZIpwK8UPSAeXVB tt1Teqtd3GExfcEcGdik8eAv7PTAqgmTqY5OoTcZ8maGI6I78s6Kc/S2za4kBhZAXir+ WQKO0o73Cw5LcsGrGZncZr2csz2eQiBFAeTLrc8G7qPzdu1a/jW+NUlnbAEJIyS/uCJT d+k8NUnwU2zG5bIhokagZ0rsC4x0qmcrDUKyW+RiDxTABq1ApUxzuksMqcLC7ol8EjoD Lwcw== X-Gm-Message-State: AOAM532XB5uG6nIqsVTXkt/z18XIqO8Mliob+VkBTnduazxdNT2cFVP9 W1hNr7geToIRYbVoeErrP5XjY9PmpBpyZzB+oBMJYIrB X-Received: by 2002:a5d:9316:0:b0:657:a364:ceb with SMTP id l22-20020a5d9316000000b00657a3640cebmr6772620ion.63.1651208429196; Thu, 28 Apr 2022 22:00:29 -0700 (PDT) MIME-Version: 1.0 References: <20220428111442.111805-1-larysa.zaremba@intel.com> In-Reply-To: From: Andrii Nakryiko Date: Thu, 28 Apr 2022 22:00:18 -0700 Message-ID: Subject: Re: [PATCH RESEND bpf-next] bpftool: Use sysfs vmlinux when dumping BTF by ID To: Daniel Borkmann Cc: Larysa Zaremba , Alexei Starovoitov , Andrii Nakryiko , Networking , bpf , open list , Martin KaFai Lau , Song Liu , Yonghong Song , Maciej Fijalkowski , Alexander Lobakin Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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 Thu, Apr 28, 2022 at 8:17 AM Daniel Borkmann wrote: > > 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? Systems that don't have /sys/kernel/btf/vmlinux exposed definitely don't support base BTF, so there is no point in trying to find vmlinux BTF anywhere else (which is only necessary for old kernels). So I think it should be fine as is, except we shouldn't guess when base BTF is needed, it should always be for kernel module BTFs only. > > > + 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; > > >