Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6329301ybi; Wed, 29 May 2019 06:21:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqy1d8rXnPC0aGUTkk95bucitZHb18pWRnO+67wjHfTcvkqyK6nscyEGtKQSsG7JigssRUGt X-Received: by 2002:a17:902:aa0a:: with SMTP id be10mr76579379plb.293.1559136101687; Wed, 29 May 2019 06:21:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559136101; cv=none; d=google.com; s=arc-20160816; b=x1X+hRsblNfpL1XMXP7bk8NDkQ2m0GFX6xyNDeIC1ORQkTkwxgZoSEQlbN84yaRCG9 UyX30WdyQ+vSVf1bByQAQSzFYsBTDBFqrTLtmzJ3V7ee927g40qDNIzpKzhIwx14qDUw FpVZXRxgc5nkRA2c/XRc8PS9wOWPHHdIz2EqhxdbGuhvkd8OivEEINFfq8JtjhEnNxmm flEEiGlabCarBI9LDGynLqiPVd8vK2hqSJOfFni/ipMX/mlxt/hC4w5TrPhfCdqwKEu5 y6rOPayCxxmOuPCHy0ufLq72nw5YP/s2BCJUw4zS4HldBg+Fw/lV2hcCCsgqJEfjkbyZ Nvcw== 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=e6/eQWAw7/6PbvcSbZW86JncWmjTLlChAN0j6CXS6kw=; b=B75AvHDwpigo97O+B2GYnpXCqPYt5dyKqskglRwBuVyRKzMmAlJfs72WhkAsZaa66D zFIsHLEjq3n29ECQmX891d1yAAqIaVwWbFzPsypRVI6/RiSx7NKLPXRLS71ih8j4f549 Hw7B+Plf5xAfF5XPcLizC0gjq+3vDTds9YmXPYZPi12VhogL+KZoqymsj3aVjm/qADks rGVF1kYo1KnM+hajfP3HVROuOJWRWCqGhViU5jKC3XaqBbSdP+sUgbxBcaLmrPpJsQP8 zB7CqZc1+21vSsDpVR15PSvQYPGy0lJpT3ugVBWgFmvHxKqP28YfjP01gYgFuvXc7stD WVKA== 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 q22si1266463pgl.124.2019.05.29.06.21.22; Wed, 29 May 2019 06:21:41 -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 S1727236AbfE2NS6 (ORCPT + 99 others); Wed, 29 May 2019 09:18:58 -0400 Received: from mx2.suse.de ([195.135.220.15]:41218 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726863AbfE2NS6 (ORCPT ); Wed, 29 May 2019 09:18:58 -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 27A7DAEB7; Wed, 29 May 2019 13:18:57 +0000 (UTC) From: Michal Rostecki Cc: Michal Rostecki , 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 v2] libbpf: Return btf_fd in libbpf__probe_raw_btf Date: Wed, 29 May 2019 15:20:00 +0200 Message-Id: <20190529132000.24942-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 Function load_sk_storage_btf expects that libbpf__probe_raw_btf is returning a BTF descriptor, but before this change 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. v2: - Fix typo in the commit message. Fixes: d7c4b3980c18 ("libbpf: detect supported kernel BTF features and sanitize BTF") Signed-off-by: Michal Rostecki --- tools/lib/bpf/libbpf.c | 36 +++++++++++++++++++++-------------- tools/lib/bpf/libbpf_probes.c | 7 +------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 197b574406b3..bc2dca36bced 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1645,15 +1645,19 @@ 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; + int ret; - res = libbpf__probe_raw_btf((char *)types, sizeof(types), - strs, sizeof(strs)); - if (res < 0) - return res; - if (res > 0) + btf_fd = libbpf__probe_raw_btf((char *)types, sizeof(types), + strs, sizeof(strs)); + if (btf_fd < 0) + ret = 0; + else { + ret = 1; obj->caps.btf_func = 1; - return 0; + } + close(btf_fd); + return ret; } static int bpf_object__probe_btf_datasec(struct bpf_object *obj) @@ -1670,15 +1674,19 @@ 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; + int ret; - res = libbpf__probe_raw_btf((char *)types, sizeof(types), - strs, sizeof(strs)); - if (res < 0) - return res; - if (res > 0) + btf_fd = libbpf__probe_raw_btf((char *)types, sizeof(types), + strs, sizeof(strs)); + if (btf_fd < 0) + ret = 0; + else { + ret = 1; obj->caps.btf_datasec = 1; - return 0; + } + close(btf_fd); + return ret; } static int diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c index 5e2aa83f637a..2c2828345514 100644 --- a/tools/lib/bpf/libbpf_probes.c +++ b/tools/lib/bpf/libbpf_probes.c @@ -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) -- 2.21.0