From patchwork Wed Jan 22 20:22:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1227419 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 (no SPF record) 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.a=rsa-sha256 header.s=fm2 header.b=JTKFSBjZ; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=Ojk+cIoL; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 482xgK5qqGz9sP3 for ; Thu, 23 Jan 2020 07:23:25 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726780AbgAVUXZ (ORCPT ); Wed, 22 Jan 2020 15:23:25 -0500 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:43767 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725827AbgAVUXZ (ORCPT ); Wed, 22 Jan 2020 15:23:25 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 247234B78; Wed, 22 Jan 2020 15:23:24 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 22 Jan 2020 15:23:24 -0500 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=fm2; bh=8Xg2pG8bpEa91 uOgyrlRmXd7HBJkZ4W+CJgY/E8Av+s=; b=JTKFSBjZXfSFsYHt0RLx++rzBSOBa e5qAkczXA2TjexLX9r8D2q5o4Lt9i9BEENgjGjLBU9uIOQoHIoSY3qtLlrw/tyOa ezdwYV6yLbR+4+q+/JjhOCUfNbp0GGtU0Gin31bwrk6Tz4pZPTBz+AEyNnFNK9YD sbShPPUXG3uEgozYXS/QVg/yyr2hKO4OhYSD0LC92EmRTKr4py1wjMLr+o8EWAlj p2xvHmTcqkr7szQ20Ktp7mywYjTpTGcq8mit7wsdM6sefCNqmoxbYge2oHRDnfbd J4M5htmqTaaV8rkaTAv1I7YJxBbPdvUplQ8gb+N18ZkPElyfbe+aFQGIA== 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= fm1; bh=8Xg2pG8bpEa91uOgyrlRmXd7HBJkZ4W+CJgY/E8Av+s=; b=Ojk+cIoL mQrM8XO1l2OHwWjdnbet1YqfUuK800SAjWk7Lay3tbzDRKHMMRukA0hg0dRZXgRf 03Vhdw9rpBtE+8wO/+U3cciHsfPPaGGgVGMiiO6v5fYHqFYVChT+JJkHmOSSn2RV +gDEIQ9WbTVvkVO0xOVAOuGEVF4FEkqauER1qdyVfPpeibq07K4ho1xpgqQGrKhN 6RZIWv5+GsQ6Io421xKR1dwWQcsXwm+jaomR6j1+XgpUdRIK0C4hE3WDnDZtV3Hv Vt9t6d6AzJoZnzJQKAumbl7bvkRKzMYcqK3TX4C/0EC6/+4g1uGAOsipSg/GKxAd hRtGbxHVzarUUg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrvddtgddufeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddvnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhu uhdrgiihii X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.2]) by mail.messagingengine.com (Postfix) with ESMTPA id DC9E5328005C; Wed, 22 Jan 2020 15:23:21 -0500 (EST) From: Daniel Xu To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, songliubraving@fb.com, yhs@fb.com, andriin@fb.com Cc: Daniel Xu , linux-kernel@vger.kernel.org, kernel-team@fb.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org Subject: [PATCH v2 bpf-next 1/3] bpf: Add bpf_perf_prog_read_branches() helper Date: Wed, 22 Jan 2020 12:22:18 -0800 Message-Id: <20200122202220.21335-2-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200122202220.21335-1-dxu@dxuuu.xyz> References: <20200122202220.21335-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 Branch records are a CPU feature that can be configured to record certain branches that are taken during code execution. This data is particularly interesting for profile guided optimizations. perf has had branch record support for a while but the data collection can be a bit coarse grained. We (Facebook) have seen in experiments that associating metadata with branch records can improve results (after postprocessing). We generally use bpf_probe_read_*() to get metadata out of userspace. That's why bpf support for branch records is useful. Aside from this particular use case, having branch data available to bpf progs can be useful to get stack traces out of userspace applications that omit frame pointers. Signed-off-by: Daniel Xu --- include/uapi/linux/bpf.h | 13 ++++++++++++- kernel/trace/bpf_trace.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 033d90a2282d..7350c5be6158 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2885,6 +2885,16 @@ union bpf_attr { * **-EPERM** if no permission to send the *sig*. * * **-EAGAIN** if bpf program can try again. + * + * int bpf_perf_prog_read_branches(struct bpf_perf_event_data *ctx, void *buf, u32 buf_size) + * Description + * For en eBPF program attached to a perf event, retrieve the + * branch records (struct perf_branch_entry) associated to *ctx* + * and store it in the buffer pointed by *buf* up to size + * *buf_size* bytes. + * Return + * On success, number of bytes written to *buf*. On error, a + * negative value. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -3004,7 +3014,8 @@ union bpf_attr { FN(probe_read_user_str), \ FN(probe_read_kernel_str), \ FN(tcp_send_ack), \ - FN(send_signal_thread), + FN(send_signal_thread), \ + FN(perf_prog_read_branches), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 19e793aa441a..24c51272a1f7 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1028,6 +1028,35 @@ static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { .arg3_type = ARG_CONST_SIZE, }; +BPF_CALL_3(bpf_perf_prog_read_branches, struct bpf_perf_event_data_kern *, ctx, + void *, buf, u32, size) +{ + struct perf_branch_stack *br_stack = ctx->data->br_stack; + u32 to_copy = 0, to_clear = size; + int err = -EINVAL; + + if (unlikely(!br_stack)) + goto clear; + + to_copy = min_t(u32, br_stack->nr * sizeof(struct perf_branch_entry), size); + to_clear -= to_copy; + + memcpy(buf, br_stack->entries, to_copy); + err = to_copy; +clear: + memset(buf + to_copy, 0, to_clear); + return err; +} + +static const struct bpf_func_proto bpf_perf_prog_read_branches_proto = { + .func = bpf_perf_prog_read_branches, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_UNINIT_MEM, + .arg3_type = ARG_CONST_SIZE, +}; + static const struct bpf_func_proto * pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1040,6 +1069,8 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_get_stack_proto_tp; case BPF_FUNC_perf_prog_read_value: return &bpf_perf_prog_read_value_proto; + case BPF_FUNC_perf_prog_read_branches: + return &bpf_perf_prog_read_branches_proto; default: return tracing_func_proto(func_id, prog); } From patchwork Wed Jan 22 20:22:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1227420 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 (no SPF record) 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.a=rsa-sha256 header.s=fm2 header.b=0O6Ruah9; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=T42WWgXW; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 482xgR69kXz9sNF for ; Thu, 23 Jan 2020 07:23:31 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729083AbgAVUX2 (ORCPT ); Wed, 22 Jan 2020 15:23:28 -0500 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:38577 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725827AbgAVUX1 (ORCPT ); Wed, 22 Jan 2020 15:23:27 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 4DC2A4504; Wed, 22 Jan 2020 15:23:27 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 22 Jan 2020 15:23:27 -0500 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=fm2; bh=0lc3ggXWtNHW3 XBXf+vL7fkfrAUrcoDfJDlx3i3Rwns=; b=0O6Ruah91lPy8YuirZBjhjkNnfa6s RSiAe0/UibO97cXFo8gKD1B/xYPPYCvstFdf29UdQUOcAoKywJ4UeblUrB+QPz2o Zt4c0UTI4PyGNPlnR1XeRSOMfrnOCDChTWWAjhyqq9p9OrrfiZyJWWCk2M4GrROI JUzSBJf2xqZyqi4mC+ndXaGU/3f04spF5e80AtOyvUgRU4xr/sPlQLC3qr5mA3yB bklYZZlBrcLOX1ChWTxhoyDs2a7RKN3HeEhTFR+Qe3XuMuGwtLNT3subXQzQb2pU 6WtwTt5t9qr934HGRg7RiGqwdmZQUkoUHQkzJ897ZJ6Ih96Td6zgoNhxw== 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= fm1; bh=0lc3ggXWtNHW3XBXf+vL7fkfrAUrcoDfJDlx3i3Rwns=; b=T42WWgXW tSejbulwyquvE1x1OygjbzusjTzgFvYTWPDxY3vhVqXMR89vwHkW6a+xa2H8xJC3 D+53HD+ge2t5mD1uu5rRoCDiZdje/vZwkfbxn9XpPyMEwNXrg2IjgLkEHW1um0cf z94UGUu3KiwM7iV1G1kOF1x6KEqb3qtHAgeLpc4yktt28ojHrC1iSoz6her/tcti iyn0LrvSqRgTjBF5zNDkHqFe5QAiDdFPHctA86R906IlREL/xRDbLls+rhWCwHfa XwPPpI7u4DKwxqCGEulJg7mbNylMIPyBgmCBPG56ZuopEaSVEgaoOkQURNhavF7v 8eetuQd6rD9IGA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrvddtgddufeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddvnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhu uhdrgiihii X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.2]) by mail.messagingengine.com (Postfix) with ESMTPA id 2ED28328005D; Wed, 22 Jan 2020 15:23:24 -0500 (EST) From: Daniel Xu To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, songliubraving@fb.com, yhs@fb.com, andriin@fb.com Cc: Daniel Xu , linux-kernel@vger.kernel.org, kernel-team@fb.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org Subject: [PATCH v2 bpf-next 2/3] tools/bpf: Sync uapi header bpf.h Date: Wed, 22 Jan 2020 12:22:19 -0800 Message-Id: <20200122202220.21335-3-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200122202220.21335-1-dxu@dxuuu.xyz> References: <20200122202220.21335-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/bpf.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 033d90a2282d..7350c5be6158 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2885,6 +2885,16 @@ union bpf_attr { * **-EPERM** if no permission to send the *sig*. * * **-EAGAIN** if bpf program can try again. + * + * int bpf_perf_prog_read_branches(struct bpf_perf_event_data *ctx, void *buf, u32 buf_size) + * Description + * For en eBPF program attached to a perf event, retrieve the + * branch records (struct perf_branch_entry) associated to *ctx* + * and store it in the buffer pointed by *buf* up to size + * *buf_size* bytes. + * Return + * On success, number of bytes written to *buf*. On error, a + * negative value. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -3004,7 +3014,8 @@ union bpf_attr { FN(probe_read_user_str), \ FN(probe_read_kernel_str), \ FN(tcp_send_ack), \ - FN(send_signal_thread), + FN(send_signal_thread), \ + FN(perf_prog_read_branches), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call From patchwork Wed Jan 22 20:22:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 1227421 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 (no SPF record) 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.a=rsa-sha256 header.s=fm2 header.b=McQIShDL; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=sS9LuZ1r; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 482xgS68ldz9sRW for ; Thu, 23 Jan 2020 07:23:32 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729110AbgAVUXc (ORCPT ); Wed, 22 Jan 2020 15:23:32 -0500 Received: from new2-smtp.messagingengine.com ([66.111.4.224]:51427 "EHLO new2-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725827AbgAVUXc (ORCPT ); Wed, 22 Jan 2020 15:23:32 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 536A95241; Wed, 22 Jan 2020 15:23:31 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 22 Jan 2020 15:23:31 -0500 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=fm2; bh=B6/8oJXsr9XZS S6XJye1iDWSNpgFZi8UWgVVOMbMvTc=; b=McQIShDLeZfG5oCqRtyZq9lvRPT92 4sifD80vPNg5EPVENCk4mkHBYiWv9GRhYivRLJ1VYb0ZNPxF/3hM4Rl94HyPuXaO votXT42SgPT5Br13B8JK6XQEuJFjW7jERWukc021RM9G/0eA1a7a9LttfbtH0wx6 qi0FjHpfA8jwh4EpQ7uC084cRfz91ZMp4VWFDCR5Ifdd5DADLI7eSHQxdXEDdpzW RN2moyhwMl/v6wB/4oUuXWEvjPYrugkOQa/UHgYjtJ4uthzpOKy8Aju/r1JuN6iL QB4QgGQJCC6RHKE/K98vnffp9knxOHZkD9bz2yJ7A8oBcwZ5OFbw6HvXg== 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= fm1; bh=B6/8oJXsr9XZSS6XJye1iDWSNpgFZi8UWgVVOMbMvTc=; b=sS9LuZ1r qrgwEkFDJODQW3NFxGIJ55RxVKYXPBrkzu43evJiAXoLf4y5q93pfAfFTpsXPmKe OI0cJk4DmfdLX1zmsV2dL9b4gWuCQ9PR1iFoOSjkcm4jcApq/OQHxZsgrTEaO+Bf 5x2SZjj4A8vG8Xsxni2AvDiuoeRMbJcQUXQMb4tES45wFRxIvc96OFPiS7r7SzbG Y7DP02ujE93o8aF9HmmOvdaGIG+RuNP4gDbO5HykDizunbgxkZfL+snsQEZJnn1/ ZNQ7oXlbirV65IBL6cjj5Js2aSDONi5VVX8wsBsNsjB3quP4LMvjP31lkDA9XrCj Y3Q5+4mwINRzPA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrvddtgddufeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf fufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegu gihusegugihuuhhurdighiiiqeenucfkphepudelledrvddtuddrieegrddvnecuvehluh hsthgvrhfuihiivgepvdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhu uhdrgiihii X-ME-Proxy: Received: from dlxu-fedora-R90QNFJV.thefacebook.com (unknown [199.201.64.2]) by mail.messagingengine.com (Postfix) with ESMTPA id 3497A3280063; Wed, 22 Jan 2020 15:23:29 -0500 (EST) From: Daniel Xu To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, songliubraving@fb.com, yhs@fb.com, andriin@fb.com Cc: Daniel Xu , linux-kernel@vger.kernel.org, kernel-team@fb.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org Subject: [PATCH v2 bpf-next 3/3] selftests/bpf: add bpf_perf_prog_read_branches() selftest Date: Wed, 22 Jan 2020 12:22:20 -0800 Message-Id: <20200122202220.21335-4-dxu@dxuuu.xyz> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200122202220.21335-1-dxu@dxuuu.xyz> References: <20200122202220.21335-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 --- .../selftests/bpf/prog_tests/perf_branches.c | 106 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_branches.c | 39 +++++++ 2 files changed, 145 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_branches.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_branches.c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_branches.c b/tools/testing/selftests/bpf/prog_tests/perf_branches.c new file mode 100644 index 000000000000..1d8c3bf3ab39 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_branches.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include "libbpf_internal.h" + +static void on_sample(void *ctx, int cpu, void *data, __u32 size) +{ + int pbe_size = sizeof(struct perf_branch_entry); + int ret = *(int *)data, duration = 0; + + // It's hard to validate the contents of the branch entries b/c it + // would require some kind of disassembler and also encoding the + // valid jump instructions for supported architectures. So just check + // the easy stuff for now. + CHECK(ret < 0, "read_branches", "err %d\n", ret); + CHECK(ret % pbe_size != 0, "read_branches", + "bytes written=%d not multiple of struct size=%d\n", + ret, pbe_size); + + *(int *)ctx = 1; +} + +void test_perf_branches(void) +{ + int err, prog_fd, i, pfd = -1, duration = 0, ok = 0; + const char *file = "./test_perf_branches.o"; + const char *prog_name = "perf_event"; + struct perf_buffer_opts pb_opts = {}; + struct perf_event_attr attr = {}; + struct bpf_map *perf_buf_map; + struct bpf_program *prog; + struct bpf_object *obj; + struct perf_buffer *pb; + struct bpf_link *link; + volatile int j = 0; + cpu_set_t cpu_set; + + /* load program */ + err = bpf_prog_load(file, BPF_PROG_TYPE_PERF_EVENT, &obj, &prog_fd); + if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno)) { + obj = NULL; + goto out_close; + } + + prog = bpf_object__find_program_by_title(obj, prog_name); + if (CHECK(!prog, "find_probe", "prog '%s' not found\n", prog_name)) + goto out_close; + + /* load map */ + perf_buf_map = bpf_object__find_map_by_name(obj, "perf_buf_map"); + if (CHECK(!perf_buf_map, "find_perf_buf_map", "not found\n")) + goto out_close; + + /* create perf event */ + attr.size = sizeof(attr); + attr.type = PERF_TYPE_HARDWARE; + attr.config = PERF_COUNT_HW_CPU_CYCLES; + attr.freq = 1; + attr.sample_freq = 4000; + attr.sample_type = PERF_SAMPLE_BRANCH_STACK; + attr.branch_sample_type = PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_ANY; + pfd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, PERF_FLAG_FD_CLOEXEC); + if (CHECK(pfd < 0, "perf_event_open", "err %d\n", pfd)) + goto out_close; + + /* attach perf_event */ + link = bpf_program__attach_perf_event(prog, pfd); + if (CHECK(IS_ERR(link), "attach_perf_event", "err %ld\n", PTR_ERR(link))) + goto out_close_perf; + + /* set up perf buffer */ + pb_opts.sample_cb = on_sample; + pb_opts.ctx = &ok; + pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts); + if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) + goto out_detach; + + /* generate some branches on cpu 0 */ + CPU_ZERO(&cpu_set); + CPU_SET(0, &cpu_set); + err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set); + if (err && CHECK(err, "set_affinity", "cpu #0, err %d\n", err)) + goto out_free_pb; + for (i = 0; i < 1000000; ++i) + ++j; + + /* read perf buffer */ + err = perf_buffer__poll(pb, 500); + if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err)) + goto out_free_pb; + + if(CHECK(!ok, "ok", "not ok\n")) + goto out_free_pb; + +out_free_pb: + perf_buffer__free(pb); +out_detach: + bpf_link__destroy(link); +out_close_perf: + close(pfd); +out_close: + bpf_object__close(obj); +} diff --git a/tools/testing/selftests/bpf/progs/test_perf_branches.c b/tools/testing/selftests/bpf/progs/test_perf_branches.c new file mode 100644 index 000000000000..c210065e21c8 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_perf_branches.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook + +#include +#include +#include "bpf_helpers.h" +#include "bpf_trace_helpers.h" + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); +} perf_buf_map SEC(".maps"); + +struct fake_perf_branch_entry { + __u64 _a; + __u64 _b; + __u64 _c; +}; + +SEC("perf_event") +int perf_branches(void *ctx) +{ + int ret; + struct fake_perf_branch_entry entries[4]; + + ret = bpf_perf_prog_read_branches(ctx, + entries, + sizeof(entries)); + /* ignore spurious events */ + if (!ret) + return 1; + + bpf_perf_event_output(ctx, &perf_buf_map, BPF_F_CURRENT_CPU, + &ret, sizeof(ret)); + return 0; +} + +char _license[] SEC("license") = "GPL";