Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp836299ybl; Fri, 9 Aug 2019 14:48:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqwYYhby4wTuGjbvpL1f7Eashm2L+wLhDwGW7Q3tUW6ywQGb+d6Ll3yeo4uzulq7ftERFIAB X-Received: by 2002:a63:c44c:: with SMTP id m12mr18931100pgg.396.1565387324169; Fri, 09 Aug 2019 14:48:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565387324; cv=none; d=google.com; s=arc-20160816; b=qh61Q7zaqb7v0wxxZ5Tz3yBAHn8BMQi3tsoTeIkT30HSp57zX8qOXh9Nttporm4sxz wPwAoG5+JuYvOkCfmXgB7KNm64Rgb3+Q8q7MMoe3cvAJ+Gef0P+d/o0XBIvH3vfJseyM aVTaqxpVTZJ1c1hHlTG+gE6bRiEGVa07OCI+wyVn56WdaH8xQE9EA1XYuq4RnzA0SELS dppZzO2A9fyiB4v1F2Cozb0at/FT1QdcSaoICz6gGb1HiTE9FGMMk5fCRZXZb93ctjBD h51lh8w62MGFdKWdfoNF94GUTW9sqloM6pSE/6DugWoEnivV/7wYnU9Rc3AYH9Qon2By U3xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=ZgLj+e4X7MDRK6SM7/arel7PF4jkctUY0AARU+B4fj4=; b=ASokfjzV4c9SJuaTe6VDEKp9FwuZdu0YEXYDuOovbwd2ljwP0B4vhLOHOOEFZWalcd PfPT0lQ7dPKMoyoLTpyzmhp0ifO9IvhyPpyU6k5eRtF39J4c+cGCkM80vhs0RkPLHwNz 8I8HmZaJXp2DBTuIcdl66HyWB63wJjm9IMXlymgqPBtBs/nhMX2YiDu1Syhlw696ZWe2 SUYfFWSeo0pdSmEiqWGQN79BjfWLu7vpKxTgOpHJ73VrYv7LJ5ewxEznrgDFuNNvahE9 Iq6vcIt+IKDQ4lGPTnkUUn9ixW65Teof1bclbIiMURkxsBsz9ysvISQWrHU2ek5SpFdx auGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=zyeWSpM1; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=yvf9ucN5; 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 x11si7522714pga.416.2019.08.09.14.48.29; Fri, 09 Aug 2019 14:48:44 -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; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=zyeWSpM1; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=yvf9ucN5; 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 S1728492AbfHIVrg (ORCPT + 99 others); Fri, 9 Aug 2019 17:47:36 -0400 Received: from new1-smtp.messagingengine.com ([66.111.4.221]:38663 "EHLO new1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726137AbfHIVrg (ORCPT ); Fri, 9 Aug 2019 17:47:36 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 9A38D1D5F; Fri, 9 Aug 2019 17:47:34 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 09 Aug 2019 17:47:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=ZgLj+e4X7MDRK 6SM7/arel7PF4jkctUY0AARU+B4fj4=; b=zyeWSpM1MKhfw7iatIvgVkIO6zdkK rOytYdiL90VfA/k00XzpVcp3WW/YfE8K6nNp1iaYSn63E2N7FKof5HBQbPR3xvPN d75E10YNQ0yK41tg16HB9CoGAukLFMKOEIj8+CG74nmGP5rZeX0q7On3HjBVVUAy SkKtCOAr2TVlcUG+RpwTxHdKS7BnGF1diebpjq7AyRJ/5QQqrDT5YuN4PDSJKkzj 7LE/9fH5G7NL9O3wsuieCW9qpUsVk0B8UoPoQk0ydkGxTo9ZR11jGEF7Kdmd2ZAM 2vURoBvCzEA2pjZljQv1UIpWQdq5DsqnnTdfMefP7KPJzPrY5G5mktYQA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=ZgLj+e4X7MDRK6SM7/arel7PF4jkctUY0AARU+B4fj4=; b=yvf9ucN5 eXGOfdPkwl0v+SqaPocO3rDycuj4yrdJwGj0rZ0u3S0g9dydrpKqs3CComcYMStF 9dFUMzKlKz8vOJsjarOTpYOgBwQ5OOHJ7NN5uz9qtIZuCDDZUXDe+dGMNecTJE/9 iiZrQBaTpbqloI9sBW8HUZ9KQ9Z/6JLYGb2MNgq1fEMjpaoTgXE4u9i5EHfuAU8l PEkVu7YAwA+qpdib+PJ1bldBPsjEtdPdfN6TTTYP2uXP0oAOWl8sUPgkQ3EGlEDC 6R/ADdQ98X75WSXxEtbamBgjr0FQtY5uzmbvxvwGLPJjw2amlQgnoADnyaELAgwr c7IetGQKt5s6GA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddruddukedgtdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddufeeknecurf grrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiiinecuvehluhhsthgv rhfuihiivgepud X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.138]) by mail.messagingengine.com (Postfix) with ESMTPA id E8984380075; Fri, 9 Aug 2019 17:47:32 -0400 (EDT) From: Daniel Xu To: songliubraving@fb.com, yhs@fb.com, andriin@fb.com, peterz@infraded.org, mingo@redhat.com, acme@kernel.org Cc: Daniel Xu , ast@fb.com, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 bpf-next 2/4] libbpf: Add helpers to extract perf fd from bpf_link Date: Fri, 9 Aug 2019 14:46:40 -0700 Message-Id: <20190809214642.12078-3-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190809214642.12078-1-dxu@dxuuu.xyz> References: <20190809214642.12078-1-dxu@dxuuu.xyz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It is sometimes necessary to perform ioctl's on the underlying perf fd. There is not currently a way to extract the fd given a bpf_link, so add a a pair of casting and getting helpers. The casting and getting helpers are nice because they let us define broad categories of links that makes it clear to users what they can expect to extract from what type of link. Signed-off-by: Daniel Xu --- tools/lib/bpf/libbpf.c | 19 +++++++++++++++++++ tools/lib/bpf/libbpf.h | 8 ++++++++ tools/lib/bpf/libbpf.map | 6 ++++++ 3 files changed, 33 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ead915aec349..f4d750863abd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4004,6 +4004,25 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link) return err; } +const struct bpf_link_fd *bpf_link__as_fd(const struct bpf_link *link) +{ + if (!link) + return NULL; + + if (link->destroy != &bpf_link__destroy_perf_event) + return NULL; + + return (struct bpf_link_fd *)link; +} + +int bpf_link_fd__fd(const struct bpf_link_fd *link) +{ + if (!link) + return -1; + + return link->fd; +} + struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, int pfd) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 8a9d462a6f6d..4498b6ae459a 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -166,6 +166,14 @@ LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); LIBBPF_API void bpf_program__unload(struct bpf_program *prog); struct bpf_link; +struct bpf_link_fd; + +/* casting APIs */ +LIBBPF_API const struct bpf_link_fd * +bpf_link__as_fd(const struct bpf_link *link); + +/* getters APIs */ +LIBBPF_API int bpf_link_fd__fd(const struct bpf_link_fd *link); LIBBPF_API int bpf_link__destroy(struct bpf_link *link); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index f9d316e873d8..b58dd0f0259c 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -184,3 +184,9 @@ LIBBPF_0.0.4 { perf_buffer__new_raw; perf_buffer__poll; } LIBBPF_0.0.3; + +LIBBPF_0.0.5 { + global: + bpf_link__as_fd; + bpf_link_fd__fd; +} LIBBPF_0.0.4; -- 2.20.1