From patchwork Thu Jul 29 16:20:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409133 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DEA5C4338F for ; Thu, 29 Jul 2021 16:20:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E7EBF60EBB for ; Thu, 29 Jul 2021 16:20:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229523AbhG2QUo (ORCPT ); Thu, 29 Jul 2021 12:20:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230376AbhG2QUn (ORCPT ); Thu, 29 Jul 2021 12:20:43 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F28DC061765 for ; Thu, 29 Jul 2021 09:20:39 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id p5so7641965wro.7 for ; Thu, 29 Jul 2021 09:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hFXn2xFkYP75TNOyXFEIXsA4f61R2isd0RjJQoMsXiQ=; b=Z8zymT69buBCqb364LcZlwP5MAbJ/jnbTwbo56NWVNE2uzzhjhNj8AfL2UnLWk9gQp RYMDmza4Quq77p4yN4g9sHMwQFsAC+dIrThx9RMGnJ9XYlJTQXrGXC6jLMcdfXVR8UQ3 MYhU6LenUP0BbnYrjOHZJPq2oGNPf4n7d8wevCNLyCGIwpfwgpIVSBdoK40CHaondCaF 8Cc2sPFE0kbtQMhLiAwj2GPy6qPscpZOszFbFmRgpG5t7BQTYJAq0n3pzCiL55Ev42CP GG9zs4SS8mV6mOCVK8rY0NJIi/t8X96rvnqFrk2yZqT/J5vLsLg0guDmKbxOCqt6hihU nI9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hFXn2xFkYP75TNOyXFEIXsA4f61R2isd0RjJQoMsXiQ=; b=IGscEc9zQ2lJU84kgUS+xLxFPGGpFnO+XCXlHm8c6RqefWmMJ3+5XNnqfICQaPaLtK Ie6IPOq5YPskA0tYcyQOcdlMPwyRCvM86IoBOAEPP9xjRVwIXLxsLRXBqhcX/zfzUtWf 43dZ6VJe3v4+6v/mvWwaNrmYJYJVfJ8aVT95H48FW4Pc2LV0RausPyux+6Yyq0Y1enXp BiI4iSxz+GXwBNm6vknY8OxfkunMoOQZ3gjzIkqoPWdpzZ6WQmr+oqj2UCoADuYV9KzS 6BgbKvf3OlvSSuyjV52OI0MOuvYaO9E6kUGwpFDV1qoI/OyvzMnBR90XSg/BYizYBx6S M0Pw== X-Gm-Message-State: AOAM530AHcjxufXXrrmJCO6w+QG/v003OI+7f+wiEOlpD+AwWyK/Xusy +jjPKWFfw4FN+BdeCbJ5qn1lOY1PBLSLSlvgf9Y= X-Google-Smtp-Source: ABdhPJxPkmh27hp2PJRclfyYOar0gCS+x3Aqe7PlO1hJIWsHzkx2/AI/aOn5jS097cvpDKICKH6hAA== X-Received: by 2002:adf:9e01:: with SMTP id u1mr5810636wre.12.1627575638046; Thu, 29 Jul 2021 09:20:38 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:37 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v3 1/8] libbpf: return non-null error on failures in libbpf_find_prog_btf_id() Date: Thu, 29 Jul 2021 17:20:21 +0100 Message-Id: <20210729162028.29512-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Variable "err" is initialised to -EINVAL so that this error code is returned when something goes wrong in libbpf_find_prog_btf_id(). However, a recent change in the function made use of the variable in such a way that it is set to 0 if retrieving linear information on the program is successful, and this 0 value remains if we error out on failures at later stages. Let's fix this by setting err to -EINVAL later in the function. Fixes: e9fc3ce99b34 ("libbpf: Streamline error reporting for high-level APIs") Signed-off-by: Quentin Monnet --- tools/lib/bpf/libbpf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a1ca6fb0c6d8..7b2b5d261a08 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8317,7 +8317,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) struct bpf_prog_info_linear *info_linear; struct bpf_prog_info *info; struct btf *btf = NULL; - int err = -EINVAL; + int err; info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0); err = libbpf_get_error(info_linear); @@ -8326,6 +8326,8 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) attach_prog_fd); return err; } + + err = -EINVAL; info = &info_linear->info; if (!info->btf_id) { pr_warn("The target program doesn't have BTF\n"); From patchwork Thu Jul 29 16:20:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409137 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B867C4320A for ; Thu, 29 Jul 2021 16:20:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 44EE860F5E for ; Thu, 29 Jul 2021 16:20:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229970AbhG2QUs (ORCPT ); Thu, 29 Jul 2021 12:20:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231416AbhG2QUo (ORCPT ); Thu, 29 Jul 2021 12:20:44 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B1B6C0613D3 for ; Thu, 29 Jul 2021 09:20:40 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id m19so4105705wms.0 for ; Thu, 29 Jul 2021 09:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Nw65+paVQXUOMSFGAQ4tePwCAzumt7oYHuBlsDEPx6I=; b=b7BYnH9VuW+Uq8w0ARUy30TtoaOLYJ8WP2aAZOFjUuCQFY4NT0ENMI6yGpbB96RMcQ EJ8lLUI10U8ZmEmesfq5FkatOTMRBmAY6rUbeclAlkDYoYBF40HOtx4YuZfgzIn2jXRt FIvckvwFH/o+MQXkHItxv3YV0MLMoBrND12x4QuBlyuRaAewRnN7Jg91Ys1Kj9ScVSJy NzXbDp4og5FqjfuNO+2Sc+TmILa2Mge1Od69T9jIBA09aY3yHYWt9akXdahOlJ/Umyct KX5lln1zJM1KmF4dM0S1+YzTG6w5ola3mwn0zl+nfDvynFp2tUyo4EO0z2d4LU0WqGeb n76w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Nw65+paVQXUOMSFGAQ4tePwCAzumt7oYHuBlsDEPx6I=; b=PHFzKMPzk9cvg0F3lUuQpFZ6B8ABDE1/rqIRj4BpMycoYEsbwrpNSUiOQ6/tWtQtE+ G+uL0o5UHzhhc5WgsZZef7xWUWTFRWbclAxVE/fbczxM5yhhfPOmlr4S6Nq8ifSQnVkg vTHcDjWFxRELEAIUI2HSFA3kfehS9GsGy561fGVWwao2Lm4w6fGhnUYvwyhSNg+PlR3+ VuwRT5RsIVhkdlqqql2sSD6gByh/yQrN3goPXF+glt7m+XHtA/pEEcwibbhEGAHPIhw2 k19D7B0ouZ8YWh/qGurew4Mb1EUkQn/0ob+1Oxq1dN5FlcaV/F2bHPvw/NWeXSBnZMJ8 qTIw== X-Gm-Message-State: AOAM531RAUd9KPdtYRggTfc38JeJFLMRHZ+TwiO44eehm13ghuPXuSRT 527oqcQzuvQiSm996H00E8N2YQ== X-Google-Smtp-Source: ABdhPJw2HZYYsn3cvXNLFg/nOYdOCnpuicrIQev50z7zPVPyq/3Z94MnIUTglBpfRHFib5FwySTtoA== X-Received: by 2002:a1c:7706:: with SMTP id t6mr5700289wmi.36.1627575639147; Thu, 29 Jul 2021 09:20:39 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:38 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet , John Fastabend Subject: [PATCH bpf-next v3 2/8] libbpf: rename btf__load() as btf__load_into_kernel() Date: Thu, 29 Jul 2021 17:20:22 +0100 Message-Id: <20210729162028.29512-3-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net As part of the effort to move towards a v1.0 for libbpf, rename btf__load() function, used to "upload" BTF information into the kernel, as btf__load_into_kernel(). This new name better reflects what the function does. References: - https://github.com/libbpf/libbpf/issues/278 - https://github.com/libbpf/libbpf/wiki/Libbpf:-the-road-to-v1.0#btfh-apis Signed-off-by: Quentin Monnet Acked-by: John Fastabend --- tools/lib/bpf/btf.c | 3 ++- tools/lib/bpf/btf.h | 1 + tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf.map | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index b46760b93bb4..7e0de560490e 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -1180,7 +1180,7 @@ int btf__finalize_data(struct bpf_object *obj, struct btf *btf) static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian); -int btf__load(struct btf *btf) +int btf__load_into_kernel(struct btf *btf) { __u32 log_buf_size = 0, raw_size; char *log_buf = NULL; @@ -1228,6 +1228,7 @@ int btf__load(struct btf *btf) free(log_buf); return libbpf_err(err); } +int btf__load(struct btf *) __attribute__((alias("btf__load_into_kernel"))); int btf__fd(const struct btf *btf) { diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 374e9f15de2e..fd8a21d936ef 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -46,6 +46,7 @@ LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_b LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf); LIBBPF_API int btf__load(struct btf *btf); +LIBBPF_API int btf__load_into_kernel(struct btf *btf); LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, const char *type_name); LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf, diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 7b2b5d261a08..9a657d6d7da3 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2769,7 +2769,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) */ btf__set_fd(kern_btf, 0); } else { - err = btf__load(kern_btf); + err = btf__load_into_kernel(kern_btf); } if (sanitize) { if (!err) { diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index c240d488eb5e..4d80eb8c56b0 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -375,6 +375,7 @@ LIBBPF_0.5.0 { bpf_map_lookup_and_delete_elem_flags; bpf_program__attach_kprobe_opts; bpf_object__gen_loader; + btf__load_into_kernel; btf_dump__dump_type_data; libbpf_set_strict_mode; } LIBBPF_0.4.0; From patchwork Thu Jul 29 16:20:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409135 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D07BC4338F for ; Thu, 29 Jul 2021 16:20:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 598FC60EBB for ; Thu, 29 Jul 2021 16:20:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231760AbhG2QUr (ORCPT ); Thu, 29 Jul 2021 12:20:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231668AbhG2QUp (ORCPT ); Thu, 29 Jul 2021 12:20:45 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACB60C0613D5 for ; Thu, 29 Jul 2021 09:20:41 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id n12so7657318wrr.2 for ; Thu, 29 Jul 2021 09:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WpzKuMYPOOo+opYvgS0cCtV4tL2VnVBIMo4lNV54X2A=; b=TTEXmlfC+Q8I3qXy7YVvc9OFAJCu2F3mWWpgxrQvRNH+ck8brFpuYMMoi0+VktDVyL 8OEbSzLRtbY3oSMOPTo5omt2sbwqMr1by/oc/HWCzEzMg2Fg5W4IBGhVXifNPz3KxcHS Zvv/mihY70YeIwt33voOzV6ZeQNzcziYfwlZ6Bb88ADrEWPzcFbLJEY6AR5S1XwMAqD5 Dn+zzn+0G16pcK1cdqFMIVmLPJvOkCVjzMvtoJG9TbrrhEtC3xKw0xFpEOv5diy192jp Ykr/2utdbakW5mtmJTHgXT7LN6ba4ezwlPFEY3+eI0l52rWuciMYMLx7xH4GTf4v3liC s7Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WpzKuMYPOOo+opYvgS0cCtV4tL2VnVBIMo4lNV54X2A=; b=Visthg12HLpQduN3L2hr/dKBdgKOWIRYnSAOwVoFxVIFXjJI8890PPlg/ysXm8xs0c wKIFZ7XymiNaYQZRfxNLpmsJ6jVFUYxVQVRJaSxcQImmROQ4LrbDVAfh5R5BP5hS2xGv DtxtfCk2qBgQocNcreBwsijhx8M5+UQUVaXsiSjkBDQFTO9sK4W7x6OVRp+m6xPw7yKz LXCycwlKD+0p1pKzISnJkNu/bxauLTNE4f0RfULimM50e45w1MVLQXs6L3texnMzkmgJ VMGbKyDUNOZAQcMev3PG0XSHK+WOeiwonBw4rvQ9HXuuAZW95u8dh97iPprtq94kCGZd 6Xkw== X-Gm-Message-State: AOAM532KsiRiE1+YY5sVL9YzA3nSBBnLs49twE2rXII4A73/u0fEM33o EcpvS4YjmlC6T+RHbtEpL4XxbA== X-Google-Smtp-Source: ABdhPJxAZJFxVjOsG4dFTmMC24NdvSZAPVL+ch7lG5vJAjZ6aNax9Hn7256v7Z7aSle8dY13l8s/Rw== X-Received: by 2002:adf:9cc7:: with SMTP id h7mr3206863wre.406.1627575640270; Thu, 29 Jul 2021 09:20:40 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:39 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet , John Fastabend Subject: [PATCH bpf-next v3 3/8] libbpf: rename btf__get_from_id() as btf__load_from_kernel_by_id() Date: Thu, 29 Jul 2021 17:20:23 +0100 Message-Id: <20210729162028.29512-4-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Rename function btf__get_from_id() as btf__load_from_kernel_by_id() to better indicate what the function does. Change the new function so that, instead of requiring a pointer to the pointer to update and returning with an error code, it takes a single argument (the id of the BTF object) and returns the corresponding pointer. This is more in line with the existing constructors. The other tools calling the (soon-to-be) deprecated btf__get_from_id() function will be updated in a future commit. References: - https://github.com/libbpf/libbpf/issues/278 - https://github.com/libbpf/libbpf/wiki/Libbpf:-the-road-to-v1.0#btfh-apis Signed-off-by: Quentin Monnet Acked-by: John Fastabend --- tools/lib/bpf/btf.c | 25 +++++++++++++++++-------- tools/lib/bpf/btf.h | 3 ++- tools/lib/bpf/libbpf.c | 5 +++-- tools/lib/bpf/libbpf.map | 1 + 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 7e0de560490e..948c29fee447 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -1383,21 +1383,30 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf) return btf; } +struct btf *btf__load_from_kernel_by_id(__u32 id) +{ + struct btf *btf; + int btf_fd; + + btf_fd = bpf_btf_get_fd_by_id(id); + if (btf_fd < 0) + return libbpf_err_ptr(-errno); + + btf = btf_get_from_fd(btf_fd, NULL); + close(btf_fd); + + return libbpf_ptr(btf); +} + int btf__get_from_id(__u32 id, struct btf **btf) { struct btf *res; - int err, btf_fd; + int err; *btf = NULL; - btf_fd = bpf_btf_get_fd_by_id(id); - if (btf_fd < 0) - return libbpf_err(-errno); - - res = btf_get_from_fd(btf_fd, NULL); + res = btf__load_from_kernel_by_id(id); err = libbpf_get_error(res); - close(btf_fd); - if (err) return libbpf_err(err); diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index fd8a21d936ef..698afde03c2e 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -43,6 +43,8 @@ LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf); LIBBPF_API struct btf *btf__parse_raw(const char *path); LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf); +LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id); +LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf); LIBBPF_API int btf__load(struct btf *btf); @@ -67,7 +69,6 @@ LIBBPF_API void btf__set_fd(struct btf *btf, int fd); LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset); -LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, __u32 expected_key_size, __u32 expected_value_size, diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 9a657d6d7da3..313883179919 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8316,7 +8316,7 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) { struct bpf_prog_info_linear *info_linear; struct bpf_prog_info *info; - struct btf *btf = NULL; + struct btf *btf; int err; info_linear = bpf_program__get_prog_info_linear(attach_prog_fd, 0); @@ -8333,7 +8333,8 @@ static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) pr_warn("The target program doesn't have BTF\n"); goto out; } - if (btf__get_from_id(info->btf_id, &btf)) { + btf = btf__load_from_kernel_by_id(info->btf_id); + if (libbpf_get_error(btf)) { pr_warn("Failed to get BTF of the program\n"); goto out; } diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 4d80eb8c56b0..3a9c6939301e 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -375,6 +375,7 @@ LIBBPF_0.5.0 { bpf_map_lookup_and_delete_elem_flags; bpf_program__attach_kprobe_opts; bpf_object__gen_loader; + btf__load_from_kernel_by_id; btf__load_into_kernel; btf_dump__dump_type_data; libbpf_set_strict_mode; From patchwork Thu Jul 29 16:20:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409139 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E2ABC4338F for ; Thu, 29 Jul 2021 16:20:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0B9BC60E9B for ; Thu, 29 Jul 2021 16:20:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231736AbhG2QUs (ORCPT ); Thu, 29 Jul 2021 12:20:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231755AbhG2QUq (ORCPT ); Thu, 29 Jul 2021 12:20:46 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAE35C061796 for ; Thu, 29 Jul 2021 09:20:42 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id r2so7668305wrl.1 for ; Thu, 29 Jul 2021 09:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o/hBpf5viYMSq0AktZJVEsskRhUKYFmlrZVQvNFOpzo=; b=aPG03mWax//aD/qbmVyFxwq6o62C0oVHpmZQ8NuF1QD51r1GW1N9UJNTvCEj6SgpqP 0fWt+9xP332zDVioHfUFDm/Th7gMUUDWLcuhQOIgdxKG2HE/mZjlnTgASFr8nfCxTkjd xtceWzdMaGBpMHwovj4TpxUZlhciVofJy8ylEQpHG0jPCpKK0M50PHngYBMMpalFcSCH njNmwjiXaseMtDeOKsX9MEUhab6diZhhL6yvId7vuAPrgNzDYmB+ZxcRPYJ225SUU78c IYrpF9beII/kcrFDMXyxiAQxiUiGRoGWBNRe6/Fhskl+A6yo9UwLAAFfTyKd4GoKF7rG msxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o/hBpf5viYMSq0AktZJVEsskRhUKYFmlrZVQvNFOpzo=; b=NBeXJs05cnb/fY+yzdS72+gt8erNJwOOPhSC9TrTWv1bz7xxlPDZnG/AcJeyMoe1Yj HEPI8bsf3xruCRgI38jkSX/6X8qBCxOHlz8HUovOqNq3mO8PWtL0NYXHcVxO5i4EmpI1 epCUwOvZVvKSKYlNPLkr2cpe22Tq3QvTZhrfcTGzFRXfqmHPUkky9dA0Fy5rsj8Pw7Qh rQukojiTF0CZMAzOcTPHuN/S3RMzs5/MvwN1BkEf39zbs5n1YruMgLjzWn5Upepdotfv 8hGR0fWQEitIO6bQ5QoiGpamz6Dzv0on2VXLKCJH+Hziwuxwf8U8272A9abGi7ezyU/8 d6Gw== X-Gm-Message-State: AOAM532G0DUCGXIfRazelqXF3yCLNt+agDGuiMw4yTRcP6WHq0hEdhDz 0OGaaOzv2PPqSgZvULc3NdMTOw== X-Google-Smtp-Source: ABdhPJyk16ft4kg/WHYcIwhutv0sVyhiF5DTjy4XrboD3DhZ/6XjRcq4MdmmY7LP3LRKNw5JHfMbkg== X-Received: by 2002:a05:6000:10c3:: with SMTP id b3mr5806832wrx.271.1627575641341; Thu, 29 Jul 2021 09:20:41 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:40 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v3 4/8] tools: free BTF objects at various locations Date: Thu, 29 Jul 2021 17:20:24 +0100 Message-Id: <20210729162028.29512-5-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Make sure to call btf__free() (and not simply free(), which does not free all pointers stored in the struct) on pointers to struct btf objects retrieved at various locations. These were found while updating the calls to btf__get_from_id(). Fixes: 999d82cbc044 ("tools/bpf: enhance test_btf file testing to test func info") Fixes: 254471e57a86 ("tools/bpf: bpftool: add support for func types") Fixes: 7b612e291a5a ("perf tools: Synthesize PERF_RECORD_* for loaded BPF programs") Fixes: d56354dc4909 ("perf tools: Save bpf_prog_info and BTF of new BPF programs") Fixes: 47c09d6a9f67 ("bpftool: Introduce "prog profile" command") Fixes: fa853c4b839e ("perf stat: Enable counting events for BPF programs") Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/prog.c | 5 ++++- tools/perf/util/bpf-event.c | 4 ++-- tools/perf/util/bpf_counter.c | 3 ++- tools/testing/selftests/bpf/prog_tests/btf.c | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index cc48726740ad..9d709b427665 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -781,6 +781,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, kernel_syms_destroy(&dd); } + btf__free(btf); + return 0; } @@ -2002,8 +2004,8 @@ static char *profile_target_name(int tgt_fd) struct bpf_prog_info_linear *info_linear; struct bpf_func_info *func_info; const struct btf_type *t; + struct btf *btf = NULL; char *name = NULL; - struct btf *btf; info_linear = bpf_program__get_prog_info_linear( tgt_fd, 1UL << BPF_PROG_INFO_FUNC_INFO); @@ -2027,6 +2029,7 @@ static char *profile_target_name(int tgt_fd) } name = strdup(btf__name_by_offset(btf, t->name_off)); out: + btf__free(btf); free(info_linear); return name; } diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index cdecda1ddd36..17a9844e4fbf 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -296,7 +296,7 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session, out: free(info_linear); - free(btf); + btf__free(btf); return err ? -1 : 0; } @@ -486,7 +486,7 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id) perf_env__fetch_btf(env, btf_id, btf); out: - free(btf); + btf__free(btf); close(fd); } diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 8150e03367bb..beca55129b0b 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -64,8 +64,8 @@ static char *bpf_target_prog_name(int tgt_fd) struct bpf_prog_info_linear *info_linear; struct bpf_func_info *func_info; const struct btf_type *t; + struct btf *btf = NULL; char *name = NULL; - struct btf *btf; info_linear = bpf_program__get_prog_info_linear( tgt_fd, 1UL << BPF_PROG_INFO_FUNC_INFO); @@ -89,6 +89,7 @@ static char *bpf_target_prog_name(int tgt_fd) } name = strdup(btf__name_by_offset(btf, t->name_off)); out: + btf__free(btf); free(info_linear); return name; } diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c index 857e3f26086f..68e415f4d33c 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf.c +++ b/tools/testing/selftests/bpf/prog_tests/btf.c @@ -4386,6 +4386,7 @@ static void do_test_file(unsigned int test_num) fprintf(stderr, "OK"); done: + btf__free(btf); free(func_info); bpf_object__close(obj); } From patchwork Thu Jul 29 16:20:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409141 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56088C4338F for ; Thu, 29 Jul 2021 16:20:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3141F60EBB for ; Thu, 29 Jul 2021 16:20:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231520AbhG2QUt (ORCPT ); Thu, 29 Jul 2021 12:20:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230314AbhG2QUr (ORCPT ); Thu, 29 Jul 2021 12:20:47 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11E1BC0613D3 for ; Thu, 29 Jul 2021 09:20:44 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id l18so7646140wrv.5 for ; Thu, 29 Jul 2021 09:20:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xKR/VqjQCMViD/BBhgCDfJMife0SJ78QiGT3qxry5Ck=; b=wj3GcmAssPxsnNCPm8nkXstw6PRG+kqXSN2c7HhMXxCx65AGPK/Ism78nRtIeAzOPk /jV/ThM4D4Z7DHbFzEfNA7g9sNgtKR1JiNpexUFZM8axviBqXtiM1E4URIVNWmSC7Suf A4QJNBEZn9/jsPAmuERwkaZNmGdHQWJ0RELZjmAJk9GZy6A+5o1k5G3QLQdkrvWHb8WO WNyX5jv0rN0tX5YSu5KA89p/Yo54/EGS+35JpPN8k8JdDy+dXxyKwyxmNoCWcDRAP2kv JvkW/7Ft4lnbjdGH+gipd/b01uX0rTztDYD3/Px9/NY90s6kFvHLHuM9+evoPW20ArxS tYVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xKR/VqjQCMViD/BBhgCDfJMife0SJ78QiGT3qxry5Ck=; b=qvm/TZ2/JAdtj0daPnmCDcwAz1WqoMiLZltXd/EO7ESb5XjBgtzj2XhY7KJdRKBfEG XpoSC5yiFXtpCeX8npov19XBUB8ZReZAvtdKdSIxCIoD8Noo/m3Qw4DgEcANawPhxbPz QYBgpIbir3XhDYZ2MchqzP3Z6QQ9G9Yv9EzjKFk2QXcK2NRmyNjxdwxnLZlusBOs07Cn HNGiu8mPIaaksbzmuAY0YPUaK1/r+B8QegpjqH7HgwO7mLWGJbWvLTgJylk7g3l+daYb tb/m0I4dgPyilVqTNU5iJJ46xk7Khu6aewlq49scHsKpRqsX9eZq8XDPNRK4LqSwhy8H osfg== X-Gm-Message-State: AOAM5307sTaJdAtACLAecSF/OU4e6CDKf4tyq9CH8eZM08POLPCNm4KF yENlL5e7vZC98sQDXs+5UVVjVQ== X-Google-Smtp-Source: ABdhPJw9fkBHZl8iN922EREZu9iMKWAgHw70CYpO/b/GYjI0EglCWiMLEmHJS97IhfD0eMddpDqHBQ== X-Received: by 2002:a5d:4410:: with SMTP id z16mr5770950wrq.173.1627575642574; Thu, 29 Jul 2021 09:20:42 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:41 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet , John Fastabend Subject: [PATCH bpf-next v3 5/8] tools: replace btf__get_from_id() with btf__load_from_kernel_by_id() Date: Thu, 29 Jul 2021 17:20:25 +0100 Message-Id: <20210729162028.29512-6-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Replace the calls to function btf__get_from_id(), which we plan to deprecate before the library reaches v1.0, with calls to btf__load_from_kernel_by_id() in tools/ (bpftool, perf, selftests). Update the surrounding code accordingly (instead of passing a pointer to the btf struct, get it as a return value from the function). Signed-off-by: Quentin Monnet Acked-by: John Fastabend --- tools/bpf/bpftool/btf.c | 8 ++----- tools/bpf/bpftool/btf_dumper.c | 6 +++-- tools/bpf/bpftool/map.c | 14 ++++++------ tools/bpf/bpftool/prog.c | 24 +++++++++++++------- tools/perf/util/bpf-event.c | 7 +++--- tools/perf/util/bpf_counter.c | 9 ++++++-- tools/testing/selftests/bpf/prog_tests/btf.c | 3 ++- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 385d5c955cf3..9162a18e84c0 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -580,16 +580,12 @@ static int do_dump(int argc, char **argv) } if (!btf) { - err = btf__get_from_id(btf_id, &btf); + btf = btf__load_from_kernel_by_id(btf_id); + err = libbpf_get_error(btf); if (err) { p_err("get btf by id (%u): %s", btf_id, strerror(err)); goto done; } - if (!btf) { - err = -ENOENT; - p_err("can't find btf with ID (%u)", btf_id); - goto done; - } } if (dump_c) { diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c index 7ca54d046362..9c25286a5c73 100644 --- a/tools/bpf/bpftool/btf_dumper.c +++ b/tools/bpf/bpftool/btf_dumper.c @@ -64,8 +64,10 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d, } info = &prog_info->info; - if (!info->btf_id || !info->nr_func_info || - btf__get_from_id(info->btf_id, &prog_btf)) + if (!info->btf_id || !info->nr_func_info) + goto print; + prog_btf = btf__load_from_kernel_by_id(info->btf_id); + if (libbpf_get_error(prog_btf)) goto print; finfo = u64_to_ptr(info->func_info); func_type = btf__type_by_id(prog_btf, finfo->type_id); diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 09ae0381205b..7e7f748bb0be 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -807,10 +807,11 @@ static struct btf *get_map_kv_btf(const struct bpf_map_info *info) } else if (info->btf_value_type_id) { int err; - err = btf__get_from_id(info->btf_id, &btf); - if (err || !btf) { + btf = btf__load_from_kernel_by_id(info->btf_id); + err = libbpf_get_error(btf); + if (err) { p_err("failed to get btf"); - btf = err ? ERR_PTR(err) : ERR_PTR(-ESRCH); + btf = ERR_PTR(err); } } @@ -1039,11 +1040,10 @@ static void print_key_value(struct bpf_map_info *info, void *key, void *value) { json_writer_t *btf_wtr; - struct btf *btf = NULL; - int err; + struct btf *btf; - err = btf__get_from_id(info->btf_id, &btf); - if (err) { + btf = btf__load_from_kernel_by_id(info->btf_id); + if (libbpf_get_error(btf)) { p_err("failed to get btf"); return; } diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 9d709b427665..b1996b8f1d42 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -249,10 +249,10 @@ static void show_prog_metadata(int fd, __u32 num_maps) struct bpf_map_info map_info; struct btf_var_secinfo *vsi; bool printed_header = false; - struct btf *btf = NULL; unsigned int i, vlen; void *value = NULL; const char *name; + struct btf *btf; int err; if (!num_maps) @@ -263,8 +263,8 @@ static void show_prog_metadata(int fd, __u32 num_maps) if (!value) return; - err = btf__get_from_id(map_info.btf_id, &btf); - if (err || !btf) + btf = btf__load_from_kernel_by_id(map_info.btf_id); + if (libbpf_get_error(btf)) goto out_free; t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id); @@ -646,9 +646,12 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, member_len = info->xlated_prog_len; } - if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) { - p_err("failed to get btf"); - return -1; + if (info->btf_id) { + btf = btf__load_from_kernel_by_id(info->btf_id); + if (libbpf_get_error(btf)) { + p_err("failed to get btf"); + return -1; + } } func_info = u64_to_ptr(info->func_info); @@ -2014,12 +2017,17 @@ static char *profile_target_name(int tgt_fd) return NULL; } - if (info_linear->info.btf_id == 0 || - btf__get_from_id(info_linear->info.btf_id, &btf)) { + if (info_linear->info.btf_id == 0) { p_err("prog FD %d doesn't have valid btf", tgt_fd); goto out; } + btf = btf__load_from_kernel_by_id(info_linear->info.btf_id); + if (libbpf_get_error(btf)) { + p_err("failed to load btf for prog FD %d", tgt_fd); + goto out; + } + func_info = u64_to_ptr(info_linear->info.func_info); t = btf__type_by_id(btf, func_info[0].type_id); if (!t) { diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 17a9844e4fbf..996d025b8ed8 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -223,10 +223,10 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session, free(info_linear); return -1; } - if (btf__get_from_id(info->btf_id, &btf)) { + btf = btf__load_from_kernel_by_id(info->btf_id); + if (libbpf_get_error(btf)) { pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id); err = -1; - btf = NULL; goto out; } perf_env__fetch_btf(env, info->btf_id, btf); @@ -478,7 +478,8 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id) if (btf_id == 0) goto out; - if (btf__get_from_id(btf_id, &btf)) { + btf = btf__load_from_kernel_by_id(btf_id); + if (libbpf_get_error(btf)) { pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, btf_id); goto out; diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index beca55129b0b..ba0f20853651 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -74,12 +74,17 @@ static char *bpf_target_prog_name(int tgt_fd) return NULL; } - if (info_linear->info.btf_id == 0 || - btf__get_from_id(info_linear->info.btf_id, &btf)) { + if (info_linear->info.btf_id == 0) { pr_debug("prog FD %d doesn't have valid btf\n", tgt_fd); goto out; } + btf = btf__load_from_kernel_by_id(info_linear->info.btf_id); + if (libbpf_get_error(btf)) { + pr_debug("failed to load btf for prog FD %d\n", tgt_fd); + goto out; + } + func_info = u64_to_ptr(info_linear->info.func_info); t = btf__type_by_id(btf, func_info[0].type_id); if (!t) { diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c index 68e415f4d33c..649f87382c8d 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf.c +++ b/tools/testing/selftests/bpf/prog_tests/btf.c @@ -4350,7 +4350,8 @@ static void do_test_file(unsigned int test_num) goto done; } - err = btf__get_from_id(info.btf_id, &btf); + btf = btf__load_from_kernel_by_id(info.btf_id); + err = libbpf_get_error(btf); if (CHECK(err, "cannot get btf from kernel, err: %d", err)) goto done; From patchwork Thu Jul 29 16:20:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409145 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5020C432BE for ; Thu, 29 Jul 2021 16:20:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8C4AC60720 for ; Thu, 29 Jul 2021 16:20:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231841AbhG2QVA (ORCPT ); Thu, 29 Jul 2021 12:21:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231835AbhG2QUt (ORCPT ); Thu, 29 Jul 2021 12:20:49 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2860FC0613C1 for ; Thu, 29 Jul 2021 09:20:45 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id n12so7657516wrr.2 for ; Thu, 29 Jul 2021 09:20:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8W7uiFN+SJdzF6xclbZpXJzf6AmnLjLyfeJ7DbdGIKw=; b=aN+EuT3ZbVqP3J1wjy1Cn9wS0dzKMuCTPu1TIbaHVuNJNLRzHQmhj9Ej6vpmIVCcMI TbPDhMU0gHjPKY7m2AqoSnezCttz3VLqMAZCdQ29j7yg88+aGT/Dva+PHrOUveFWNjy4 HKh2ewKKEemz/QOlkJi467GYG0O8UphXEGJxZnqiZ034RZVG1dl+Ltk+75VJkiIZ9P0m P0HFO9L7P1RuD/eBa9BVBYBqn966KpUf4ALQFbNqN+rsYgRq1smV+JQQPONnBYDT/4jJ OOAxCKbm3YN0d5WeI69jE4x2kimtL6GxJkLnMWLfSLhPVkTJEQVkBFiqm4AwwFWbKIIq hgFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8W7uiFN+SJdzF6xclbZpXJzf6AmnLjLyfeJ7DbdGIKw=; b=hAcXk9FD/WTBu0gXxecjeYGzSYLP1+sFn528wNXIlJLT5HQI627qWGR+A26fhSmiYZ 5S7i9aOkxeyUAsRVhbjlKtjZssWRxp+9d3Tlpr2gu4yzSjnRIoeVKxLA7KdZDf1Wp/P3 eUOH0o9KA1zvhU8l4LO2r6BNQzzHG2TdyEmbKRapis/RI75H1/4b5Y8xYfEfLnGCZ0mP QoLBs2dN41XINhy5O97wG9iiiLKnMv51t/NTqbRlDT5m0YlifJDy22Hv1Fh3Ob08iVJ6 lfGyh/tf9xU2A4LLnG4bzMVLAOQ5bujC+rIaJZACPl4zXlVBS4iyoO4/7qWStaZiWYI8 6/qA== X-Gm-Message-State: AOAM532yf1y7K3TBj53vhp/7R/BCfdzluuNV2V+NGqpU+3Lrbo94/c8/ aSStBvUKYofviwMh2iI8W/ByBIR2JYaQaJGgMv0= X-Google-Smtp-Source: ABdhPJzonNAIxUh4oB78IxcbYHXNkyO+8Z0KxQRHu9BshNTcAZlUYkcfOmCrcF/BRJJ/a5Eursx+TQ== X-Received: by 2002:a5d:4e8f:: with SMTP id e15mr5603050wru.415.1627575643732; Thu, 29 Jul 2021 09:20:43 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:43 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet Subject: [PATCH bpf-next v3 6/8] libbpf: prepare deprecation of btf__get_from_id(), btf__load() Date: Thu, 29 Jul 2021 17:20:26 +0100 Message-Id: <20210729162028.29512-7-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Introduce a macro LIBBPF_DEPRECATED_SINCE(major, minor, message) to prepare the deprecation of two API functions. This macro mark the functions as deprecated when libbpf's version reaches the values passed as an argument. Prepare deprecation for btf__get_from_id() and btf__load(), respectively replaced by btf__load_from_kernel_by_id() and btf__load_into_kernel(), for version 0.6 of the library. References: - https://github.com/libbpf/libbpf/issues/278 - https://github.com/libbpf/libbpf/wiki/Libbpf:-the-road-to-v1.0#btfh-apis Side notes: - Because of the constraints from the preprocessor, we have to write a few lines of macro magic for each version used to prepare deprecation (0.6 for now). - Checkpatch complains about the absence of parentheses around the definition for LIBBPF_DEPRECATED_SINCE, but the compiler profusely complains if we attempt to add them. Signed-off-by: Quentin Monnet --- tools/lib/bpf/Makefile | 3 +++ tools/lib/bpf/btf.h | 2 ++ tools/lib/bpf/libbpf_common.h | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile index ec14aa725bb0..095d5dc30d50 100644 --- a/tools/lib/bpf/Makefile +++ b/tools/lib/bpf/Makefile @@ -8,6 +8,7 @@ LIBBPF_VERSION := $(shell \ grep -oE '^LIBBPF_([0-9.]+)' libbpf.map | \ sort -rV | head -n1 | cut -d'_' -f2) LIBBPF_MAJOR_VERSION := $(firstword $(subst ., ,$(LIBBPF_VERSION))) +LIBBPF_MINOR_VERSION := $(firstword $(subst ., ,$(subst $(LIBBPF_MAJOR_VERSION)., ,$(LIBBPF_VERSION)))) MAKEFLAGS += --no-print-directory @@ -86,6 +87,8 @@ override CFLAGS += -Werror -Wall override CFLAGS += $(INCLUDES) override CFLAGS += -fvisibility=hidden override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 +override CFLAGS += -DLIBBPF_MAJOR_VERSION=$(LIBBPF_MAJOR_VERSION) +override CFLAGS += -DLIBBPF_MINOR_VERSION=$(LIBBPF_MINOR_VERSION) # flags specific for shared library SHLIB_FLAGS := -DSHARED -fPIC diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 698afde03c2e..a6039ca66895 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -44,9 +44,11 @@ LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_b LIBBPF_API struct btf *btf__parse_raw(const char *path); LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf); LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id); +LIBBPF_DEPRECATED_SINCE(0, 6, "use btf__load_from_kernel_by_id instead") LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf); +LIBBPF_DEPRECATED_SINCE(0, 6, "use btf__load_into_kernel instead") LIBBPF_API int btf__load(struct btf *btf); LIBBPF_API int btf__load_into_kernel(struct btf *btf); LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, diff --git a/tools/lib/bpf/libbpf_common.h b/tools/lib/bpf/libbpf_common.h index 947d8bd8a7bb..7218d6156ed7 100644 --- a/tools/lib/bpf/libbpf_common.h +++ b/tools/lib/bpf/libbpf_common.h @@ -17,6 +17,25 @@ #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg))) +#define __LIBBPF_GET_VERSION(major, minor) (((major) << 8) + (minor)) +#define __LIBBPF_CURRENT_VERSION \ + __LIBBPF_GET_VERSION(LIBBPF_MAJOR_VERSION, LIBBPF_MINOR_VERSION) +#define __LIBBPF_CURRENT_VERSION_GEQ(major, minor) \ + (__LIBBPF_CURRENT_VERSION >= __LIBBPF_GET_VERSION(major, minor)) +/* Add checks for other versions below when planning deprecation of API symbols + * with the LIBBPF_DEPRECATED_SINCE macro. + */ +#if __LIBBPF_CURRENT_VERSION_GEQ(0, 6) +#define __LIBBPF_MARK_DEPRECATED_0_6(X) X +#else +#define __LIBBPF_MARK_DEPRECATED_0_6(X) +#endif + +/* Mark a symbol as deprecated when libbpf version is >= {major}.{minor} */ +#define LIBBPF_DEPRECATED_SINCE(major, minor, msg) \ + __LIBBPF_MARK_DEPRECATED_ ## major ## _ ## minor \ + (LIBBPF_DEPRECATED("v" # major "." # minor "+, " msg)) + /* Helper macro to declare and initialize libbpf options struct * * This dance with uninitialized declaration, followed by memset to zero, From patchwork Thu Jul 29 16:20:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409147 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B829DC4320E for ; Thu, 29 Jul 2021 16:20:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A519660F4A for ; Thu, 29 Jul 2021 16:20:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231888AbhG2QVA (ORCPT ); Thu, 29 Jul 2021 12:21:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231841AbhG2QUu (ORCPT ); Thu, 29 Jul 2021 12:20:50 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 310ADC061765 for ; Thu, 29 Jul 2021 09:20:46 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id n11so4089446wmd.2 for ; Thu, 29 Jul 2021 09:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GLAH0UGomdUat0GatMlVuuZxkfmHz9v5KV19sL9pJ5o=; b=v3x8PWC4siRBKA6XN/dbpeJJAURFGja0rv6yXY3XF5iKlWwU/OCiJ5A4tepDF31VUL AraSFvisTTJAE/oHdUf99FAH3S5mtgeg7FG/dwjnMvksrKgRfbPTUn0eKCIa9Bn6pqLu rRYnFwrzS/t37SJGShz8ffttnnODwGj67cw9FVIvAxKhkzLuF4UGErcY7uha6RaMxVBj LwfdIiQhDx6dO7pqYrmlE64jXV+b9qDAjxN7YCe0ri4wIo8R6Q6Lx4LFp/voEUTspRmS 71OH4uOWtr+tcNNWBo95V2Ksi/Psgd8M11Vjyu0rFgW+ZX8VWPG35ThEeZZsEqpwYZE2 8moA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GLAH0UGomdUat0GatMlVuuZxkfmHz9v5KV19sL9pJ5o=; b=HNo6YWfraltpVDvJ8qseTHDLowzBErH6sls/wAmA1fcu5Z5kkng6wF7v3KaCMjWYze VWdZ9xqaABBrT2xCIsVaOA0pSbTSEzjgeuxjnK8NUbHNUMNoud28LAh8iTDhcvprAwJz ToEIcLp0eq+u1z8bJFoB23NNnUJn5v2EHM3Kjgq3N+ZHMAwPwrVlDJ6oAHQ/bB9vf/1H ooEaO1MAzdFSUKSF4pO2jXzclQsTXj24rKoX+Te4yx1UeViEnfQ4HM8pTLTO05eIJuC4 I2l3cgGQ76ErPYcH6KsZxQXpZzXaoSz8swJG94BcUm0c8XpKyaEBJV0mIziqKNb6SRW/ cBPw== X-Gm-Message-State: AOAM530UZKrmq5n20obzcUHXzo/DiOOZIOCZHITJEQYlda+D8QJWQNP8 iDiuXSd07fiIUn4gQ34I8E+4U9jOC8rqOiCsbwo= X-Google-Smtp-Source: ABdhPJyFWIZLqWsppsfu4zePJLVoOV33Wp4buOgmpKS9ZSqax4M268T4rOSvk9BKoJ02UeRrse6T0g== X-Received: by 2002:a7b:cc16:: with SMTP id f22mr15212938wmh.99.1627575644791; Thu, 29 Jul 2021 09:20:44 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:44 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet , John Fastabend Subject: [PATCH bpf-next v3 7/8] libbpf: add split BTF support for btf__load_from_kernel_by_id() Date: Thu, 29 Jul 2021 17:20:27 +0100 Message-Id: <20210729162028.29512-8-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a new API function btf__load_from_kernel_by_id_split(), which takes a pointer to a base BTF object in order to support split BTF objects when retrieving BTF information from the kernel. Reference: https://github.com/libbpf/libbpf/issues/314 Signed-off-by: Quentin Monnet Acked-by: John Fastabend --- tools/lib/bpf/btf.c | 9 +++++++-- tools/lib/bpf/btf.h | 1 + tools/lib/bpf/libbpf.map | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 948c29fee447..cafa4f6bd9b1 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -1383,7 +1383,7 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf) return btf; } -struct btf *btf__load_from_kernel_by_id(__u32 id) +struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf) { struct btf *btf; int btf_fd; @@ -1392,12 +1392,17 @@ struct btf *btf__load_from_kernel_by_id(__u32 id) if (btf_fd < 0) return libbpf_err_ptr(-errno); - btf = btf_get_from_fd(btf_fd, NULL); + btf = btf_get_from_fd(btf_fd, base_btf); close(btf_fd); return libbpf_ptr(btf); } +struct btf *btf__load_from_kernel_by_id(__u32 id) +{ + return btf__load_from_kernel_by_id_split(id, NULL); +} + int btf__get_from_id(__u32 id, struct btf **btf) { struct btf *res; diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index a6039ca66895..358046ffc7bd 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -44,6 +44,7 @@ LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_b LIBBPF_API struct btf *btf__parse_raw(const char *path); LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf); LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id); +LIBBPF_API struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf); LIBBPF_DEPRECATED_SINCE(0, 6, "use btf__load_from_kernel_by_id instead") LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 3a9c6939301e..5aca3686ca5e 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -376,6 +376,7 @@ LIBBPF_0.5.0 { bpf_program__attach_kprobe_opts; bpf_object__gen_loader; btf__load_from_kernel_by_id; + btf__load_from_kernel_by_id_split; btf__load_into_kernel; btf_dump__dump_type_data; libbpf_set_strict_mode; From patchwork Thu Jul 29 16:20:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 12409143 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0749EC4320A for ; Thu, 29 Jul 2021 16:20:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E33BF6023F for ; Thu, 29 Jul 2021 16:20:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231865AbhG2QU7 (ORCPT ); Thu, 29 Jul 2021 12:20:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231888AbhG2QUu (ORCPT ); Thu, 29 Jul 2021 12:20:50 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F3D4C061796 for ; Thu, 29 Jul 2021 09:20:47 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id b128so4082359wmb.4 for ; Thu, 29 Jul 2021 09:20:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Z05HCoFLItaKC/XfP7RbKcIL37D0WS7BgQCbfiZH5KI=; b=jxDgMroLj69sJyQunbHN90KUHo7cdp6vMQUQxOWZ+jYCfzJT6ntkMIVyueJFx+ll/Z Fhcv0NlQ4qgMet/tBhdWBMIVKgLVUcrwEDbXH7jZ6X0yWP8a6wMvbEiZgavgDc4BI9jq Sp+ftR+Ji7hd3ItCE15XF/kt5jCl2I3s5hO4BrePsK+i4bQGVtzmpJLB2mQH90Q0aokL EPb001vVZDemV/bQgEIvphtnNKnna1cXChiw8hcz5I2aY1QvNxDQSzyrKKFWjQYZDD59 lKwCA0FH/+3ubxF6Bi97qw9csBI24IOg1faUaiCxfhUZ9seUOMxZjrWP6VY+c+5FAX2v PqSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Z05HCoFLItaKC/XfP7RbKcIL37D0WS7BgQCbfiZH5KI=; b=bgq9c3FGHh0eGNeRiyNt1h0JboQYz02NqCzoO+eFE7X9A/ykhGz9sW8ozSePVm4O9w aSynq2vn9yEL8YGZlJnwuKAZkngatlL0E+E9K9XcsL0aIIYc3VAy5zYdq/1AWrI2HOir 2m/hKSsXgoehNNDW4mPjlD6aWX36SqugG5M8Rq6n7mWvrhh5QattoZ0KWYWxCESRjdsD FAWcraoRgXEjJMtFzgF4hBO7PWZdZ3agOAY/IUEOUvE+CzxhOAgNVludAD1Xy8VambPM /+cDmnvLm4indU08Mq3skDdQ151/Vqpog85snGQmn2zzRCljp3D3CjUGNEshSENRoMQg PDww== X-Gm-Message-State: AOAM5319ca6nsI2XTmaR+k/F8L/US5JXjs/eJUuqrUfJxGvFT9BoLt6o NdDtIbj9ClzFjgokv03wKjZN8Q== X-Google-Smtp-Source: ABdhPJykEHbtQpATL57HjR8Hg3TNVxlkhYAggER0KgGdEua9vw102i45WLOE42KlioSBTXeekAAK9w== X-Received: by 2002:a05:600c:4fcf:: with SMTP id o15mr15389124wmq.116.1627575645838; Thu, 29 Jul 2021 09:20:45 -0700 (PDT) Received: from localhost.localdomain ([149.86.66.250]) by smtp.gmail.com with ESMTPSA id c10sm3854853wmb.40.2021.07.29.09.20.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 09:20:45 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Quentin Monnet , John Fastabend Subject: [PATCH bpf-next v3 8/8] tools: bpftool: support dumping split BTF by id Date: Thu, 29 Jul 2021 17:20:28 +0100 Message-Id: <20210729162028.29512-9-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210729162028.29512-1-quentin@isovalent.com> References: <20210729162028.29512-1-quentin@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Split BTF objects are typically BTF objects for kernel modules, which are incrementally built on top of kernel BTF instead of redefining all kernel symbols they need. We can use bpftool with its -B command-line option to dump split BTF objects. It works well when the handle provided for the BTF object to dump is a "path" to the BTF object, typically under /sys/kernel/btf, because bpftool internally calls btf__parse_split() which can take a "base_btf" pointer and resolve the BTF reconstruction (although in that case, the "-B" option is unnecessary because bpftool performs autodetection). However, it did not work so far when passing the BTF object through its id, because bpftool would call btf__get_from_id() which did not provide a way to pass a "base_btf" pointer. In other words, the following works: # bpftool btf dump file /sys/kernel/btf/i2c_smbus -B /sys/kernel/btf/vmlinux But this was not possible: # bpftool btf dump id 6 -B /sys/kernel/btf/vmlinux The libbpf API has recently changed, and btf__get_from_id() has been deprecated in favour of btf__load_from_kernel_by_id() and its version with support for split BTF, btf__load_from_kernel_by_id_split(). Let's update bpftool to make it able to dump the BTF object in the second case as well. Signed-off-by: Quentin Monnet Acked-by: John Fastabend --- tools/bpf/bpftool/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 9162a18e84c0..0ce3643278d4 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -580,7 +580,7 @@ static int do_dump(int argc, char **argv) } if (!btf) { - btf = btf__load_from_kernel_by_id(btf_id); + btf = btf__load_from_kernel_by_id_split(btf_id, base_btf); err = libbpf_get_error(btf); if (err) { p_err("get btf by id (%u): %s", btf_id, strerror(err));