From patchwork Fri Aug 16 22:31:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1148484 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="QtItbTs7"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="VrF2OtFd"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 469J3j1dcBz9sN1 for ; Sat, 17 Aug 2019 08:32:33 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727868AbfHPWcT (ORCPT ); Fri, 16 Aug 2019 18:32:19 -0400 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:38807 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727854AbfHPWcS (ORCPT ); Fri, 16 Aug 2019 18:32:18 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 846A82050; Fri, 16 Aug 2019 18:32:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 16 Aug 2019 18:32:17 -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=C/zyYjiT4fsHO Q1Isf9yVG5DdN4diUqRbFGQAlHIG50=; b=QtItbTs7c+hOk+jN883LnOmBmsnaD 9yGq/Ns6ScYz1xXTMlsDMPehi/CMyBJ6KftA8Icv/CQSk/sZHrpFbmvSYgkcAkJ4 7h6QbaXPsXPs78/IxFuBti0eaz0ddA9PnCh0umo1mZ9EGnP77lJmiQYk3B82a9Uk Jc4fZ1Et3SDf54uR13sHfjPQ+cxWw31QpLcbdLyXdcMMFA4Txbypmn6mTnL7vobs 360LAq+Nx4oRG0RSKUiKHdyJ8EJv0TJtCdG/rT75KTVDStWI/EESXnA2ApDLt4Wb ofSYj67aWnyg1iuZHiOtZZyvLafiTY7blH+Vhrb0da4pLIYkaXjCrDq8w== 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=C/zyYjiT4fsHOQ1Isf9yVG5DdN4diUqRbFGQAlHIG50=; b=VrF2OtFd +it9Ogj4lDi49bGcgYXPnXcqm5/rjIs9YhVkNFPHWwu4wW1uluMjSwnvHVd8eKV/ 3ZhSxHcOTOD2nNusmeby3J/xuC1EoRoS5DKgU5XR/7HLSYJes2HakOhW/5/AR5mm VO7NMFWO4v43YYiizIr/9WDModkQFWXO8/Mwb1CtB26JmEUK0zvEmePNdKUwymeP VQLuaDJ8O2LyQGg2KK31K+haBrG+eRlIooZfjIqRUE7ajwcx/3LuaQmEayFT3bRU z6L83ZWxo2D/K0W7pFFHnx+K34WB6KZedcA3mwGSe/BQej1DLEjBpf2sSqwE/Qnm EPswz+VEfztfxA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudefgedgtdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddufeeknecurf grrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiiinecuvehluhhsthgv rhfuihiivgeptd X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.138]) by mail.messagingengine.com (Postfix) with ESMTPA id A5B0F80062; Fri, 16 Aug 2019 18:32:15 -0400 (EDT) From: Daniel Xu To: bpf@vger.kernel.org, songliubraving@fb.com, yhs@fb.com, andriin@fb.com, peterz@infradead.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, kernel-team@fb.com Subject: [PATCH v3 bpf-next 1/4] tracing/probe: Add PERF_EVENT_IOC_QUERY_PROBE ioctl Date: Fri, 16 Aug 2019 15:31:46 -0700 Message-Id: <20190816223149.5714-2-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190816223149.5714-1-dxu@dxuuu.xyz> References: <20190816223149.5714-1-dxu@dxuuu.xyz> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org It's useful to know [uk]probe's nmissed and nhit stats. For example with tracing tools, it's important to know when events may have been lost. debugfs currently exposes a control file to get this information, but it is not compatible with probes registered with the perf API. While bpf programs may be able to manually count nhit, there is no way to gather nmissed. In other words, it is currently not possible to retrieve information about FD-based probes. This patch adds a new ioctl that lets users query nmissed (as well as nhit for completeness). We currently only add support for [uk]probes but leave the possibility open for other probes like tracepoint. Signed-off-by: Daniel Xu --- include/linux/trace_events.h | 12 ++++++++++++ include/uapi/linux/perf_event.h | 23 +++++++++++++++++++++++ kernel/events/core.c | 20 ++++++++++++++++++++ kernel/trace/trace_kprobe.c | 24 ++++++++++++++++++++++++ kernel/trace/trace_uprobe.c | 24 ++++++++++++++++++++++++ 5 files changed, 103 insertions(+) diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index 5150436783e8..61558f19696a 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -586,6 +586,12 @@ extern int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, const char **symbol, u64 *probe_offset, u64 *probe_addr, bool perf_type_tracepoint); +extern int perf_kprobe_event_query(struct perf_event *event, void __user *info); +#else +int perf_kprobe_event_query(struct perf_event *event, void __user *info) +{ + return -EOPNOTSUPP; +} #endif #ifdef CONFIG_UPROBE_EVENTS extern int perf_uprobe_init(struct perf_event *event, @@ -594,6 +600,12 @@ extern void perf_uprobe_destroy(struct perf_event *event); extern int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, const char **filename, u64 *probe_offset, bool perf_type_tracepoint); +extern int perf_uprobe_event_query(struct perf_event *event, void __user *info); +#else +int perf_uprobe_event_query(struct perf_event *event, void __user *info) +{ + return -EOPNOTSUPP; +} #endif extern int ftrace_profile_set_filter(struct perf_event *event, int event_id, char *filter_str); diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 7198ddd0c6b1..8783d29a807a 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -447,6 +447,28 @@ struct perf_event_query_bpf { __u32 ids[0]; }; +/* + * Structure used by below PERF_EVENT_IOC_QUERY_PROBE command + * to query information about the probe attached to the perf + * event. Currently only supports [uk]probes. + */ +struct perf_event_query_probe { + /* + * Size of structure for forward/backward compatibility + */ + __u64 size; + /* + * Set by the kernel to indicate number of times this probe + * was temporarily disabled + */ + __u64 nmissed; + /* + * Set by the kernel to indicate number of times this probe + * was hit + */ + __u64 nhit; +}; + /* * Ioctls that can be done on a perf event fd: */ @@ -462,6 +484,7 @@ struct perf_event_query_bpf { #define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32) #define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *) #define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_QUERY_PROBE _IOR('$', 12, struct perf_event_query_probe *) enum perf_event_ioc_flags { PERF_IOC_FLAG_GROUP = 1U << 0, diff --git a/kernel/events/core.c b/kernel/events/core.c index 0463c1151bae..ed33d50511a3 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5060,6 +5060,8 @@ static int perf_event_set_filter(struct perf_event *event, void __user *arg); static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); static int perf_copy_attr(struct perf_event_attr __user *uattr, struct perf_event_attr *attr); +static int perf_probe_event_query(struct perf_event *event, + void __user *info); static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) { @@ -5143,6 +5145,10 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon return perf_event_modify_attr(event, &new_attr); } +#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) + case PERF_EVENT_IOC_QUERY_PROBE: + return perf_probe_event_query(event, (void __user *)arg); +#endif default: return -ENOTTY; } @@ -8833,6 +8839,20 @@ static inline void perf_tp_register(void) #endif } +static int perf_probe_event_query(struct perf_event *event, + void __user *info) +{ +#ifdef CONFIG_KPROBE_EVENTS + if (event->attr.type == perf_kprobe.type) + return perf_kprobe_event_query(event, (void __user *)info); +#endif +#ifdef CONFIG_UPROBE_EVENTS + if (event->attr.type == perf_uprobe.type) + return perf_uprobe_event_query(event, (void __user *)info); +#endif + return -EINVAL; +} + static void perf_event_free_filter(struct perf_event *event) { ftrace_profile_free_filter(event); diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 9d483ad9bb6c..89fbe3e97562 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -196,6 +196,30 @@ bool trace_kprobe_error_injectable(struct trace_event_call *call) return within_error_injection_list(trace_kprobe_address(tk)); } +int perf_kprobe_event_query(struct perf_event *event, void __user *info) +{ + struct perf_event_query_probe __user *uquery = info; + struct perf_event_query_probe query = {}; + struct trace_event_call *call = event->tp_event; + struct trace_kprobe *tk = (struct trace_kprobe *)call->data; + u64 ncopy; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + if (copy_from_user(&query, uquery, + offsetofend(struct perf_event_query_probe, size))) + return -EFAULT; + + ncopy = min_t(u64, query.size, sizeof(query)); + query.nhit = trace_kprobe_nhit(tk); + query.nmissed = tk->rp.kp.nmissed; + + if (copy_to_user(uquery, &query, ncopy)) + return -EFAULT; + + return 0; +} + static int register_kprobe_event(struct trace_kprobe *tk); static int unregister_kprobe_event(struct trace_kprobe *tk); diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c index 1ceedb9146b1..ecdf2bdb91a7 100644 --- a/kernel/trace/trace_uprobe.c +++ b/kernel/trace/trace_uprobe.c @@ -1333,6 +1333,30 @@ static inline void init_trace_event_call(struct trace_uprobe *tu) call->data = tu; } +int perf_uprobe_event_query(struct perf_event *event, void __user *info) +{ + struct perf_event_query_probe __user *uquery = info; + struct perf_event_query_probe query = {}; + struct trace_event_call *call = event->tp_event; + struct trace_uprobe *tu = (struct trace_uprobe *)call->data; + u64 ncopy; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + if (copy_from_user(&query, uquery, + offsetofend(struct perf_event_query_probe, size))) + return -EFAULT; + + ncopy = min_t(u64, query.size, sizeof(query)); + query.nhit = tu->nhit; + query.nmissed = 0; + + if (copy_to_user(uquery, &query, ncopy)) + return -EFAULT; + + return 0; +} + static int register_uprobe_event(struct trace_uprobe *tu) { init_trace_event_call(tu); From patchwork Fri Aug 16 22:31:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1148481 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="NMhyH+ji"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="c9etP3Rm"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 469J3V2g4pz9sNf for ; Sat, 17 Aug 2019 08:32:22 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727834AbfHPWcU (ORCPT ); Fri, 16 Aug 2019 18:32:20 -0400 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:44639 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727878AbfHPWcU (ORCPT ); Fri, 16 Aug 2019 18:32:20 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 44E342007; Fri, 16 Aug 2019 18:32:19 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 16 Aug 2019 18:32:19 -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=nULmcyQSNr8cA ELFcrkB+0TUhwAeBpe1wH20rK6qw/k=; b=NMhyH+jiRhrSy2brSY3lU1nZBqFP1 W8GX++tTQhQA4yyPYYyUnFSbZgHUj+vMi5pi41nUZMgvkTOoZrNJOjIYfwGv8+Ei IqjOEcQP1kYh+BVN15OgmdKLk01jSEwtHVbJbhGPbVimC34HjRB78VzeoYIfOno/ mWNQrKy9YHz9/6nPCTia9UWV6qDg2a19eazMLb/xOY0TwZtRK+Z1cRt13VaUYXH4 tViHao4oFagpIccy75kwLUTC1KqpAaV0ErTXP5KVVgnTYpXxUmP02ZB2MdUhNZ5k bvPLJqDL3gZeMaS8GV98OS3hgDsFNXRieMCKZIW8xLczQ4pk2twkQW8RA== 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=nULmcyQSNr8cAELFcrkB+0TUhwAeBpe1wH20rK6qw/k=; b=c9etP3Rm xbrP3YguVt+iLsILMZzSo3wa24oUzSg+zC6xrZPgE7QjP7UrbgX5xibfPkbt7VLY Cd2FbkOtV+aH+s/sNeJXlvobJrEdqCPAyGm+nVZ5uwZKPiICiF3yRlFCKWVkTqs9 Pwbc4PwBGGJnBlrUZRuA6RZU5suMrRdVCctfvDilXMNF9Ew/5x413/P2f/IrDFuM T/8m0HhEywbWKtvGbpFBCGj1WdnqgcUZhp9hCcuDV/DBtWIhgfTf80YYU2r2/v8k ho3H4cPgA6OY7ZvVNdW/9aCkWhb3XrGBKy0BMgTLJ+XLak4C6S89CIwr5xzfJmMp HFcCoODihIbl+A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudefgedgtdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdlfeehmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddufeeknecurf grrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiiinecuvehluhhsthgv rhfuihiivgeptd X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.138]) by mail.messagingengine.com (Postfix) with ESMTPA id 4CCD58005B; Fri, 16 Aug 2019 18:32:17 -0400 (EDT) From: Daniel Xu To: bpf@vger.kernel.org, songliubraving@fb.com, yhs@fb.com, andriin@fb.com, peterz@infradead.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, kernel-team@fb.com Subject: [PATCH v3 bpf-next 2/4] libbpf: Add helpers to extract perf fd from bpf_link Date: Fri, 16 Aug 2019 15:31:47 -0700 Message-Id: <20190816223149.5714-3-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190816223149.5714-1-dxu@dxuuu.xyz> References: <20190816223149.5714-1-dxu@dxuuu.xyz> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@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. Acked-by: Song Liu Signed-off-by: Daniel Xu Acked-by: Andrii Nakryiko --- tools/lib/bpf/libbpf.c | 21 +++++++++++++++++++++ tools/lib/bpf/libbpf.h | 13 +++++++++++++ tools/lib/bpf/libbpf.map | 4 ++++ 3 files changed, 38 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2233f919dd88..41588e13be2b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4876,6 +4876,7 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, struct bpf_link { int (*destroy)(struct bpf_link *link); + enum bpf_link_type type; }; int bpf_link__destroy(struct bpf_link *link) @@ -4909,6 +4910,24 @@ 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->type != LIBBPF_LINK_FD) + return NULL; + + return (struct bpf_link_fd *)link; +} + +enum bpf_link_type bpf_link__type(const struct bpf_link *link) +{ + return link->type; +} + +int bpf_link_fd__fd(const struct bpf_link_fd *link) +{ + return link->fd; +} + struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, int pfd) { @@ -4932,6 +4951,7 @@ struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, if (!link) return ERR_PTR(-ENOMEM); link->link.destroy = &bpf_link__destroy_perf_event; + link->link.type = LIBBPF_LINK_FD; link->fd = pfd; if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) { @@ -5225,6 +5245,7 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, link = malloc(sizeof(*link)); if (!link) return ERR_PTR(-ENOMEM); + link->link.type = LIBBPF_LINK_FD; link->link.destroy = &bpf_link__destroy_fd; pfd = bpf_raw_tracepoint_open(tp_name, prog_fd); diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index e8f70977d137..2ddef5315ff9 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -166,7 +166,20 @@ LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); LIBBPF_API void bpf_program__unload(struct bpf_program *prog); +enum bpf_link_type { + LIBBPF_LINK_FD, +}; + 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 enum bpf_link_type bpf_link__type(const struct bpf_link *link); +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 4e72df8e98ba..ee9945177100 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -186,4 +186,8 @@ LIBBPF_0.0.4 { } LIBBPF_0.0.3; LIBBPF_0.0.5 { + global: + bpf_link__type; + bpf_link__as_fd; + bpf_link_fd__fd; } LIBBPF_0.0.4; From patchwork Fri Aug 16 22:31:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1148482 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="sBi2zD9k"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="YON8VEx/"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 469J3W0cVdz9sN6 for ; Sat, 17 Aug 2019 08:32:23 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727908AbfHPWcW (ORCPT ); Fri, 16 Aug 2019 18:32:22 -0400 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:42903 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727898AbfHPWcV (ORCPT ); Fri, 16 Aug 2019 18:32:21 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id C9A75209B; Fri, 16 Aug 2019 18:32:20 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 16 Aug 2019 18:32:20 -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=jMcpWT3BOBrXz HHXvcq6IicYWXcTtdD131rXZuAKD/k=; b=sBi2zD9kXPOlU5CRSdZQOqZG92xVV udytZmUiUas0dHf3S5eUTR7aD5s57b6eTKMVuxt/MuIAhDG9ii0sXYsnMMz/ZdhA W0l5YEPIKgtIuHZjEtJe+d34fxltyO11agaxXwomPISjwh5gXFiOq/p2ktcRheOi RBgSSnJf6XbpeRxKdB8U0Gz9XabpsO7MduiZY+X7+ynSsYwCcFDDIJRmZxBXvYGO zIEgZrF3RmzZT0gB29jBcpg7QLRNEhCcuRM+09IOeYVeGHMy8s/xbA/rCSoX9bFo ZTFrjnVf5Gpon9m+NZBP/cg/cEYfjuUXrS5DHKH4GJWwICGeZLeW/2ThA== 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=jMcpWT3BOBrXzHHXvcq6IicYWXcTtdD131rXZuAKD/k=; b=YON8VEx/ txy7eXFH0RSC5B7+xW2N7rwgULBKKA/0iCMnuq+rQZ49KcHRoMa6vFwm2gZVVm4i 0IiDJBwOt8oQa9XUl3cSaPB039qkTrELOlRCABJf8AOZ3EI+fplO/P0VAA09uL0E qy8GimGdXXRtAhG2OjMQKJyiydYDEBQiCwvVZ/2YkOcADaf8sXGObsoB6Z9Q1oPv tXwNK3jP4jmRjyjtx1RJV09uguCdPc9/7Nk41OuROBwpV3zkWfP8U5y9QRl2BXZo dOgKMUzbXJhHwlVOCPTIdnJ5xB6xxlvoJHKE7/w+QBlOOhZdgwT8oREJv072R/PA yUSqe5892qAwqw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudefgedgtdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddufeeknecurf grrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiiinecuvehluhhsthgv rhfuihiivgeptd X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.138]) by mail.messagingengine.com (Postfix) with ESMTPA id E8FD080061; Fri, 16 Aug 2019 18:32:18 -0400 (EDT) From: Daniel Xu To: bpf@vger.kernel.org, songliubraving@fb.com, yhs@fb.com, andriin@fb.com, peterz@infradead.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, kernel-team@fb.com Subject: [PATCH v3 bpf-next 3/4] tracing/probe: Sync perf_event.h to tools Date: Fri, 16 Aug 2019 15:31:48 -0700 Message-Id: <20190816223149.5714-4-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190816223149.5714-1-dxu@dxuuu.xyz> References: <20190816223149.5714-1-dxu@dxuuu.xyz> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Signed-off-by: Daniel Xu --- tools/include/uapi/linux/perf_event.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h index 7198ddd0c6b1..8783d29a807a 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -447,6 +447,28 @@ struct perf_event_query_bpf { __u32 ids[0]; }; +/* + * Structure used by below PERF_EVENT_IOC_QUERY_PROBE command + * to query information about the probe attached to the perf + * event. Currently only supports [uk]probes. + */ +struct perf_event_query_probe { + /* + * Size of structure for forward/backward compatibility + */ + __u64 size; + /* + * Set by the kernel to indicate number of times this probe + * was temporarily disabled + */ + __u64 nmissed; + /* + * Set by the kernel to indicate number of times this probe + * was hit + */ + __u64 nhit; +}; + /* * Ioctls that can be done on a perf event fd: */ @@ -462,6 +484,7 @@ struct perf_event_query_bpf { #define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32) #define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *) #define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_QUERY_PROBE _IOR('$', 12, struct perf_event_query_probe *) enum perf_event_ioc_flags { PERF_IOC_FLAG_GROUP = 1U << 0, From patchwork Fri Aug 16 22:31:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1148483 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="KEJzdryq"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="qHDoRGmQ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 469J3X6SJJz9sN6 for ; Sat, 17 Aug 2019 08:32:24 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727898AbfHPWcY (ORCPT ); Fri, 16 Aug 2019 18:32:24 -0400 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:60457 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727912AbfHPWcX (ORCPT ); Fri, 16 Aug 2019 18:32:23 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 84455205D; Fri, 16 Aug 2019 18:32:22 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 16 Aug 2019 18:32:22 -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=yesvY/W0FWL4U zYMEQ08IeYM3+h3idTX3RnYsVHvMY0=; b=KEJzdryqdsreY/zOG+9TDeMhXebTA dSu6Ie9PB6la/pHE+elY2HEHANsahAmqvO5b9UzZvsee9kzoOT/JrVR7nXexSrvU 5kcGaYtdfjBlGdSBU73LP63oCBcJZL7sGlG00zaPWAmF2njS1vW0B5a5XSf59AHc KYe4W//16dJCxHOu7BKX3veneLd2+FA57tUDHwn9sXHlLR++wwjCND8aBhJD2vxB sjsOdoWBuSCrQQESVL9ht3iHdUB8yi3LHzn/A7GS9JeUlkmXo5bknNJw7SpXuC90 IlavQ+D0tWatG5cLKWXiFYTV/Uj3tDn3tYXyjcy5d2STyfLsHfkJf8/kg== 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=yesvY/W0FWL4UzYMEQ08IeYM3+h3idTX3RnYsVHvMY0=; b=qHDoRGmQ sY9XUMpvH2HeJictVWrtMlX+uhCyG2iVK9+q9Z4CIaJTXqg1KJsSAsacisd9LJXv Ph/wHjahDNSfJeUsL6nuWhRachX2NpMEq6SLPZzQUpMyXeiY6VLlg1DvNAf8LZhk X6611uueDr1R//hexekAPxC3i9YxfRYqlzX5mZlcc902szsTlnQUui5FuK1RpZ88 Tp2mFmoM3YqUjwyBA2I2kDp4PIdnZxODLVLaNsxfV95xDsYyywvzrxnKyR+Z8Tdq YDrH05kJahHYfALxLekfBoNS0wFYhb+B8GON6aeyWsvHvWvU2CmlAvruTyH2DT5O hj63aECBPgNMhw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudefgedgtdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdlfeehmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddufeeknecurf grrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiiinecuvehluhhsthgv rhfuihiivgepfe X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.138]) by mail.messagingengine.com (Postfix) with ESMTPA id 973B48005B; Fri, 16 Aug 2019 18:32:20 -0400 (EDT) From: Daniel Xu To: bpf@vger.kernel.org, songliubraving@fb.com, yhs@fb.com, andriin@fb.com, peterz@infradead.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, kernel-team@fb.com Subject: [PATCH v3 bpf-next 4/4] tracing/probe: Add self test for PERF_EVENT_IOC_QUERY_PROBE Date: Fri, 16 Aug 2019 15:31:49 -0700 Message-Id: <20190816223149.5714-5-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190816223149.5714-1-dxu@dxuuu.xyz> References: <20190816223149.5714-1-dxu@dxuuu.xyz> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Acked-by: Andrii Nakryiko Signed-off-by: Daniel Xu --- .../selftests/bpf/prog_tests/attach_probe.c | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c index 5ecc267d98b0..c14db7557881 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -27,17 +27,27 @@ void test_attach_probe(void) const char *kretprobe_name = "kretprobe/sys_nanosleep"; const char *uprobe_name = "uprobe/trigger_func"; const char *uretprobe_name = "uretprobe/trigger_func"; + struct perf_event_query_probe kprobe_query = {}; + struct perf_event_query_probe kretprobe_query = {}; + struct perf_event_query_probe uprobe_query = {}; + struct perf_event_query_probe uretprobe_query = {}; const int kprobe_idx = 0, kretprobe_idx = 1; const int uprobe_idx = 2, uretprobe_idx = 3; const char *file = "./test_attach_probe.o"; struct bpf_program *kprobe_prog, *kretprobe_prog; struct bpf_program *uprobe_prog, *uretprobe_prog; struct bpf_object *obj; + const struct bpf_link_fd *kprobe_fd_link; + const struct bpf_link_fd *kretprobe_fd_link; + const struct bpf_link_fd *uprobe_fd_link; + const struct bpf_link_fd *uretprobe_fd_link; int err, prog_fd, duration = 0, res; struct bpf_link *kprobe_link = NULL; struct bpf_link *kretprobe_link = NULL; struct bpf_link *uprobe_link = NULL; struct bpf_link *uretprobe_link = NULL; + int kprobe_fd, kretprobe_fd; + int uprobe_fd, uretprobe_fd; int results_map_fd; size_t uprobe_offset; ssize_t base_addr; @@ -116,6 +126,54 @@ void test_attach_probe(void) /* trigger & validate kprobe && kretprobe */ usleep(1); + kprobe_fd_link = bpf_link__as_fd(kprobe_link); + if (CHECK(!kprobe_fd_link, "kprobe_link_as_fd", + "failed to cast link to fd link\n")) + goto cleanup; + + kprobe_fd = bpf_link_fd__fd(kprobe_fd_link); + if (CHECK(kprobe_fd < 0, "kprobe_get_perf_fd", + "failed to get perf fd from kprobe link\n")) + goto cleanup; + + kretprobe_fd_link = bpf_link__as_fd(kretprobe_link); + if (CHECK(!kretprobe_fd_link, "kretprobe_link_as_fd", + "failed to cast link to fd link\n")) + goto cleanup; + + kretprobe_fd = bpf_link_fd__fd(kretprobe_fd_link); + if (CHECK(kretprobe_fd < 0, "kretprobe_get_perf_fd", + "failed to get perf fd from kretprobe link\n")) + goto cleanup; + + kprobe_query.size = sizeof(kprobe_query); + err = ioctl(kprobe_fd, PERF_EVENT_IOC_QUERY_PROBE, &kprobe_query); + if (CHECK(err, "get_kprobe_ioctl", + "failed to issue kprobe query ioctl\n")) + goto cleanup; + if (CHECK(kprobe_query.nmissed > 0, "get_kprobe_ioctl", + "read incorrect nmissed from kprobe_ioctl: %llu\n", + kprobe_query.nmissed)) + goto cleanup; + if (CHECK(kprobe_query.nhit == 0, "get_kprobe_ioctl", + "read incorrect nhit from kprobe_ioctl: %llu\n", + kprobe_query.nhit)) + goto cleanup; + + kretprobe_query.size = sizeof(kretprobe_query); + err = ioctl(kretprobe_fd, PERF_EVENT_IOC_QUERY_PROBE, &kretprobe_query); + if (CHECK(err, "get_kretprobe_ioctl", + "failed to issue kretprobe query ioctl\n")) + goto cleanup; + if (CHECK(kretprobe_query.nmissed > 0, "get_kretprobe_ioctl", + "read incorrect nmissed from kretprobe_ioctl: %llu\n", + kretprobe_query.nmissed)) + goto cleanup; + if (CHECK(kretprobe_query.nhit <= 0, "get_kretprobe_ioctl", + "read incorrect nhit from kretprobe_ioctl: %llu\n", + kretprobe_query.nhit)) + goto cleanup; + err = bpf_map_lookup_elem(results_map_fd, &kprobe_idx, &res); if (CHECK(err, "get_kprobe_res", "failed to get kprobe res: %d\n", err)) @@ -135,6 +193,54 @@ void test_attach_probe(void) /* trigger & validate uprobe & uretprobe */ get_base_addr(); + uprobe_fd_link = bpf_link__as_fd(uprobe_link); + if (CHECK(!uprobe_fd_link, "uprobe_link_as_fd", + "failed to cast link to fd link\n")) + goto cleanup; + + uprobe_fd = bpf_link_fd__fd(uprobe_fd_link); + if (CHECK(uprobe_fd < 0, "uprobe_get_perf_fd", + "failed to get perf fd from uprobe link\n")) + goto cleanup; + + uretprobe_fd_link = bpf_link__as_fd(uretprobe_link); + if (CHECK(!uretprobe_fd_link, "uretprobe_link_as_fd", + "failed to cast link to fd link\n")) + goto cleanup; + + uretprobe_fd = bpf_link_fd__fd(uretprobe_fd_link); + if (CHECK(uretprobe_fd < 0, "uretprobe_get_perf_fd", + "failed to get perf fd from uretprobe link\n")) + goto cleanup; + + uprobe_query.size = sizeof(uprobe_query); + err = ioctl(uprobe_fd, PERF_EVENT_IOC_QUERY_PROBE, &uprobe_query); + if (CHECK(err, "get_uprobe_ioctl", + "failed to issue uprobe query ioctl\n")) + goto cleanup; + if (CHECK(uprobe_query.nmissed > 0, "get_uprobe_ioctl", + "read incorrect nmissed from uprobe_ioctl: %llu\n", + uprobe_query.nmissed)) + goto cleanup; + if (CHECK(uprobe_query.nhit == 0, "get_uprobe_ioctl", + "read incorrect nhit from uprobe_ioctl: %llu\n", + uprobe_query.nhit)) + goto cleanup; + + uretprobe_query.size = sizeof(uretprobe_query); + err = ioctl(uretprobe_fd, PERF_EVENT_IOC_QUERY_PROBE, &uretprobe_query); + if (CHECK(err, "get_uretprobe_ioctl", + "failed to issue uretprobe query ioctl\n")) + goto cleanup; + if (CHECK(uretprobe_query.nmissed > 0, "get_uretprobe_ioctl", + "read incorrect nmissed from uretprobe_ioctl: %llu\n", + uretprobe_query.nmissed)) + goto cleanup; + if (CHECK(uretprobe_query.nhit <= 0, "get_uretprobe_ioctl", + "read incorrect nhit from uretprobe_ioctl: %llu\n", + uretprobe_query.nhit)) + goto cleanup; + err = bpf_map_lookup_elem(results_map_fd, &uprobe_idx, &res); if (CHECK(err, "get_uprobe_res", "failed to get uprobe res: %d\n", err))