Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6689072ybi; Wed, 29 May 2019 11:31:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqws/TuZ/EN7hipmDKwdjicpKhxC/0/0k+L8/zVzV1X2l6iBeUiGxb2y3+vKulCzmU8dKUIx X-Received: by 2002:a63:2c14:: with SMTP id s20mr121437551pgs.182.1559154686652; Wed, 29 May 2019 11:31:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559154686; cv=none; d=google.com; s=arc-20160816; b=qti58l72TkPZfSdzvpVQZ9QaIICIqUvOnDXp7Yt2jlgLrDuPWJeP6mdLTT/cb/x/wG uywluo9c9ncQKwsJX1vVcEJ6bylykSRsxRO1k3/Oe1uE0q15pIz3PuVWskxOY9SCCh63 JPOUam+v5VHN7P2w5FawZg4G5zBqipJZ6ZqFqo1ApXJY36KdNteMI5mHtaaQzL27vSwP MCBP9vzhOGAnOojQREsHcg6unAN5bPqQCkeQm5rD6Z/qbBVaEnPfHWaO1EY01ZRovU+l 0xbsvJGUHOs07XijtZ1QmePbFO4QThZmuvMcghdqRAPwrIvukIQt8lcgyAdZG7r58JY/ h4tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from; bh=aNehU0T82bX29NHznfBPAdFxOm1+HTXkUF2uvjhuo+A=; b=DErSzDQ8K3JKh9V1QOtBMrUs6FIeS7ZNeM6lB0JDE5GWpy6jeRF86ZXvyBpbaPoSlq UJ3TUKRXN0BTnBjhfpOYmfRuV7TSsF7u47dfkRyiRGLvLz6J1bDIxto86lu1UUZRzwAB uu4vwT6yrxz33wIA3G8V3rMEiJibjboTKWuHOKCm5iFkQL6ym7WYwL9/H7cf3uJYb1pK Y9DQ3mUMd6IcBRs9Wm/QSw7NWnRU4ewgPKN66qFpXE9xP8sYZWDfpjl/+4cLMYkkKMix vEJ8+NjiQjhCHk2cM/yvV1eWt3o6ZD6FGiJ+KLgfj7EeJq04NnEC6Nl4ERAExVlx+LLI naEQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a15si491890pfa.85.2019.05.29.11.31.09; Wed, 29 May 2019 11:31:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727408AbfE2SaB (ORCPT + 99 others); Wed, 29 May 2019 14:30:01 -0400 Received: from mx2.suse.de ([195.135.220.15]:44890 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725917AbfE2SaB (ORCPT ); Wed, 29 May 2019 14:30:01 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 554F4AF55; Wed, 29 May 2019 18:29:59 +0000 (UTC) From: Michal Rostecki Cc: Michal Rostecki , Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , netdev@vger.kernel.org (open list:BPF (Safe dynamic programs and tools)), bpf@vger.kernel.org (open list:BPF (Safe dynamic programs and tools)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH bpf v4] libbpf: Return btf_fd for load_sk_storage_btf Date: Wed, 29 May 2019 20:31:09 +0200 Message-Id: <20190529183109.17317-1-mrostecki@opensuse.org> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before this change, function load_sk_storage_btf expected that libbpf__probe_raw_btf was returning a BTF descriptor, but in fact it was returning an information about whether the probe was successful (0 or 1). load_sk_storage_btf was using that value as an argument of the close function, which was resulting in closing stdout and thus terminating the process which called that function. That bug was visible in bpftool. `bpftool feature` subcommand was always exiting too early (because of closed stdout) and it didn't display all requested probes. `bpftool -j feature` or `bpftool -p feature` were not returning a valid json object. This change renames the libbpf__probe_raw_btf function to libbpf__load_raw_btf, which now returns a BTF descriptor, as expected in load_sk_storage_btf. v2: - Fix typo in the commit message. v3: - Simplify BTF descriptor handling in bpf_object__probe_btf_* functions. - Rename libbpf__probe_raw_btf function to libbpf__load_raw_btf and return a BTF descriptor. v4: - Fix typo in the commit message. Fixes: d7c4b3980c18 ("libbpf: detect supported kernel BTF features and sanitize BTF") Signed-off-by: Michal Rostecki Acked-by: Andrii Nakryiko --- tools/lib/bpf/libbpf.c | 28 ++++++++++++++++------------ tools/lib/bpf/libbpf_internal.h | 4 ++-- tools/lib/bpf/libbpf_probes.c | 13 ++++--------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 197b574406b3..5d046cc7b207 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1645,14 +1645,16 @@ static int bpf_object__probe_btf_func(struct bpf_object *obj) /* FUNC x */ /* [3] */ BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), }; - int res; + int btf_fd; - res = libbpf__probe_raw_btf((char *)types, sizeof(types), - strs, sizeof(strs)); - if (res < 0) - return res; - if (res > 0) + btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), + strs, sizeof(strs)); + if (btf_fd >= 0) { obj->caps.btf_func = 1; + close(btf_fd); + return 1; + } + return 0; } @@ -1670,14 +1672,16 @@ static int bpf_object__probe_btf_datasec(struct bpf_object *obj) BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), BTF_VAR_SECINFO_ENC(2, 0, 4), }; - int res; + int btf_fd; - res = libbpf__probe_raw_btf((char *)types, sizeof(types), - strs, sizeof(strs)); - if (res < 0) - return res; - if (res > 0) + btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), + strs, sizeof(strs)); + if (btf_fd >= 0) { obj->caps.btf_datasec = 1; + close(btf_fd); + return 1; + } + return 0; } diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index f3025b4d90e1..dfab8012185c 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -34,7 +34,7 @@ do { \ #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__) #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__) -int libbpf__probe_raw_btf(const char *raw_types, size_t types_len, - const char *str_sec, size_t str_len); +int libbpf__load_raw_btf(const char *raw_types, size_t types_len, + const char *str_sec, size_t str_len); #endif /* __LIBBPF_LIBBPF_INTERNAL_H */ diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c index 5e2aa83f637a..6635a31a7a16 100644 --- a/tools/lib/bpf/libbpf_probes.c +++ b/tools/lib/bpf/libbpf_probes.c @@ -133,8 +133,8 @@ bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex) return errno != EINVAL && errno != EOPNOTSUPP; } -int libbpf__probe_raw_btf(const char *raw_types, size_t types_len, - const char *str_sec, size_t str_len) +int libbpf__load_raw_btf(const char *raw_types, size_t types_len, + const char *str_sec, size_t str_len) { struct btf_header hdr = { .magic = BTF_MAGIC, @@ -157,14 +157,9 @@ int libbpf__probe_raw_btf(const char *raw_types, size_t types_len, memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len); btf_fd = bpf_load_btf(raw_btf, btf_len, NULL, 0, false); - if (btf_fd < 0) { - free(raw_btf); - return 0; - } - close(btf_fd); free(raw_btf); - return 1; + return btf_fd; } static int load_sk_storage_btf(void) @@ -190,7 +185,7 @@ static int load_sk_storage_btf(void) BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */ }; - return libbpf__probe_raw_btf((char *)types, sizeof(types), + return libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs)); } -- 2.21.0