From patchwork Mon Jul 4 14:53:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905457 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9FA74C433EF for ; Mon, 4 Jul 2022 14:56:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=qfqfdqiDGa2OiKQ3QYDkax0Po1B1/ZzpMGgTx1xXHNI=; b=Bk4dwm1khc4HHo y6/REsv3BFRvFPkK3f4Zew138NOS8I/hXywdkOFIK0Eln9U3QfHZuQlQWAhOiSlWv42kfI+sEmT8V zHSwWzWbWRbK4LSV80J2JUBTH6ZUbWfOaq8THsOJgGdYBduSSG6qSrSEo8uqqGtOnppwd0SmAQSkg 2et4IgdKo/0Tnx0jf2nJYKnZGyvEeUBX1KVzZ7on5+jwjg/XUDmLqKb84o/L9HCmWojNBjrPkiK0s MtAmpbn2kVutlE0KvIVTZx3QnbQ5YEVHmaqjjqv/zIUrE62X8mZpH/dKtvrlpLt8EY/1Zp6muMyU8 X2fwK4H0DPRyLUlfWZ1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NTV-009XKa-83; Mon, 04 Jul 2022 14:54:57 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NTR-009XJK-Bk for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:54:55 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A188CD6E; Mon, 4 Jul 2022 07:54:50 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 834FB3F792; Mon, 4 Jul 2022 07:54:46 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 1/8] perf arm64: Send pointer auth masks to ring buffer Date: Mon, 4 Jul 2022 15:53:25 +0100 Message-Id: <20220704145333.22557-2-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075453_538811_740F19A8 X-CRM114-Status: GOOD ( 26.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Perf report cannot produce callgraphs using dwarf on arm64 where pointer authentication is enabled. This is because libunwind and libdw cannot unmangle instruction pointers that have a pointer authentication code (PAC) embedded in them. libunwind and libdw need to be given an instruction mask which they can use to arrive at the correct return address that does not contain the PAC. The bits in the return address that contain the PAC can differ by process, so this patch adds a new sample field PERF_SAMPLE_ARCH_1 to allow the kernel to send the masks up to userspace perf. This field can be used in a architecture specific fashion, but on aarch64, it contains the ptrauth mask information. Signed-off-by: Andrew Kilroy --- arch/arm64/include/asm/arch_sample_data.h | 38 +++++++++++++++++++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/arch_sample_data.c | 37 ++++++++++++++++++++++ include/linux/perf_event.h | 24 ++++++++++++++ include/uapi/linux/perf_event.h | 5 ++- kernel/events/core.c | 35 +++++++++++++++++++++ 6 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/include/asm/arch_sample_data.h create mode 100644 arch/arm64/kernel/arch_sample_data.c diff --git a/arch/arm64/include/asm/arch_sample_data.h b/arch/arm64/include/asm/arch_sample_data.h new file mode 100644 index 000000000000..83fda293b1fc --- /dev/null +++ b/arch/arm64/include/asm/arch_sample_data.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ASM_ARCH_SAMPLE_DATA_H +#define _ASM_ARCH_SAMPLE_DATA_H + +#include + +/* + * Structure holding masks to help userspace stack unwinding + * in the presence of arm64 pointer authentication. + */ +struct ptrauth_info { + /* + * Bits 0, 1, 2, 3, 4 may be set to on, to indicate which keys are being used + * The APIAKEY, APIBKEY, APDAKEY, APDBKEY, or the APGAKEY respectively. + * Where all bits are off, pointer authentication is not in use for the + * process. + */ + u64 enabled_keys; + + /* + * The on bits represent which bits in an instruction pointer + * constitute the pointer authentication code. + */ + u64 insn_mask; + + /* + * The on bits represent which bits in a data pointer constitute the + * pointer authentication code. + */ + u64 data_mask; +}; + +struct arch_sample_data { + struct ptrauth_info ptrauth; +}; + +#endif diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index fa7981d0d917..843c6e0e2393 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -44,7 +44,7 @@ obj-$(CONFIG_KUSER_HELPERS) += kuser32.o obj-$(CONFIG_FUNCTION_TRACER) += ftrace.o entry-ftrace.o obj-$(CONFIG_MODULES) += module.o obj-$(CONFIG_ARM64_MODULE_PLTS) += module-plts.o -obj-$(CONFIG_PERF_EVENTS) += perf_regs.o perf_callchain.o +obj-$(CONFIG_PERF_EVENTS) += perf_regs.o perf_callchain.o arch_sample_data.o obj-$(CONFIG_HW_PERF_EVENTS) += perf_event.o obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o obj-$(CONFIG_CPU_PM) += sleep.o suspend.o diff --git a/arch/arm64/kernel/arch_sample_data.c b/arch/arm64/kernel/arch_sample_data.c new file mode 100644 index 000000000000..2d47e8db0dbe --- /dev/null +++ b/arch/arm64/kernel/arch_sample_data.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +inline void perf_output_sample_arch_1(struct perf_output_handle *handle, + struct perf_event_header *header, + struct perf_sample_data *data, + struct perf_event *event) +{ + perf_output_put(handle, data->arch.ptrauth.enabled_keys); + perf_output_put(handle, data->arch.ptrauth.insn_mask); + perf_output_put(handle, data->arch.ptrauth.data_mask); +} + +inline void perf_prepare_sample_arch_1(struct perf_event_header *header, + struct perf_sample_data *data, + struct perf_event *event, + struct pt_regs *regs) +{ + struct task_struct *task = current; + int keys_result = ptrauth_get_enabled_keys(task); + u64 user_pac_mask = keys_result > 0 ? ptrauth_user_pac_mask() : 0; + + data->arch.ptrauth.enabled_keys = keys_result > 0 ? keys_result : 0; + data->arch.ptrauth.insn_mask = user_pac_mask; + data->arch.ptrauth.data_mask = user_pac_mask; + + header->size += (3 * sizeof(u64)); +} + +inline int perf_event_open_request_arch_1(void) +{ + return 0; +} + + diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index da759560eec5..8a99942989ce 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -999,6 +999,29 @@ int perf_event_read_local(struct perf_event *event, u64 *value, extern u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running); +void perf_output_sample_arch_1(struct perf_output_handle *handle, + struct perf_event_header *header, + struct perf_sample_data *data, + struct perf_event *event); + +void perf_prepare_sample_arch_1(struct perf_event_header *header, + struct perf_sample_data *data, + struct perf_event *event, + struct pt_regs *regs); + +int perf_event_open_request_arch_1(void); + +#if IS_ENABLED(CONFIG_ARM64) + +#define HAS_ARCH_SAMPLE_DATA +#include + +#endif + +#ifndef HAS_ARCH_SAMPLE_DATA +struct arch_sample_data { +}; +#endif struct perf_sample_data { /* @@ -1041,6 +1064,7 @@ struct perf_sample_data { u64 cgroup; u64 data_page_size; u64 code_page_size; + struct arch_sample_data arch; } ____cacheline_aligned; /* default value for data source */ diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index d37629dbad72..821bf5ff6a19 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -162,12 +162,15 @@ enum perf_event_sample_format { PERF_SAMPLE_DATA_PAGE_SIZE = 1U << 22, PERF_SAMPLE_CODE_PAGE_SIZE = 1U << 23, PERF_SAMPLE_WEIGHT_STRUCT = 1U << 24, + PERF_SAMPLE_ARCH_1 = 1U << 25, - PERF_SAMPLE_MAX = 1U << 25, /* non-ABI */ + PERF_SAMPLE_MAX = 1U << 26, /* non-ABI */ __PERF_SAMPLE_CALLCHAIN_EARLY = 1ULL << 63, /* non-ABI; internal use */ }; +#define PERF_SAMPLE_ARM64_PTRAUTH PERF_SAMPLE_ARCH_1 + #define PERF_SAMPLE_WEIGHT_TYPE (PERF_SAMPLE_WEIGHT | PERF_SAMPLE_WEIGHT_STRUCT) /* * values to program into branch_sample_type when PERF_SAMPLE_BRANCH is set diff --git a/kernel/events/core.c b/kernel/events/core.c index 80782cddb1da..89ab8120f4f0 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6957,6 +6957,29 @@ static inline bool perf_sample_save_hw_index(struct perf_event *event) return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX; } +#ifndef HAS_ARCH_SAMPLE_DATA + +inline void perf_output_sample_arch_1(struct perf_output_handle *handle __maybe_unused, + struct perf_event_header *header __maybe_unused, + struct perf_sample_data *data __maybe_unused, + struct perf_event *event __maybe_unused) +{ +} + +inline void perf_prepare_sample_arch_1(struct perf_event_header *header __maybe_unused, + struct perf_sample_data *data __maybe_unused, + struct perf_event *event __maybe_unused, + struct pt_regs *regs __maybe_unused) +{ +} + +inline int perf_event_open_request_arch_1(void) +{ + return -EINVAL; +} + +#endif + void perf_output_sample(struct perf_output_handle *handle, struct perf_event_header *header, struct perf_sample_data *data, @@ -7125,6 +7148,9 @@ void perf_output_sample(struct perf_output_handle *handle, perf_aux_sample_output(event, handle, data); } + if (sample_type & PERF_SAMPLE_ARCH_1) + perf_output_sample_arch_1(handle, header, data, event); + if (!event->attr.watermark) { int wakeup_events = event->attr.wakeup_events; @@ -7427,6 +7453,9 @@ void perf_prepare_sample(struct perf_event_header *header, if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) data->code_page_size = perf_get_page_size(data->ip); + if (sample_type & PERF_SAMPLE_ARCH_1) + perf_prepare_sample_arch_1(header, data, event, regs); + if (sample_type & PERF_SAMPLE_AUX) { u64 size; @@ -12074,6 +12103,12 @@ SYSCALL_DEFINE5(perf_event_open, return err; } + if (attr.sample_type & PERF_SAMPLE_ARCH_1) { + err = perf_event_open_request_arch_1(); + if (err) + return err; + } + /* * In cgroup mode, the pid argument is used to pass the fd * opened to the cgroup directory in cgroupfs. The cpu argument From patchwork Mon Jul 4 14:53:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905459 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 74913CCA479 for ; Mon, 4 Jul 2022 14:56:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ezgvAqkPKa6w1+KeKvTG37HAfHx83D2ZywBkxFEuh8M=; b=NNVMwRjoZonadN vX8le88Gtse0M9kDgEu0G6RGp0uipoBg5jdWFG/pKDwOo0bMxrdk+yz/znOr2VZjK5L0YbNb7wAJd 9kMPXJFNsDkW/fMxR4PNkqRPRyBqFsEvzP5MvWK41wv3kouJrWkBUMhaG9v16MHpUkZWfDnfOueTr xVy4dcUV5Jym2X/mh7EHB0XDXUS/evdSYTnEs+TKeyEto6H0NWcA7IV+EGGNWTpg0kiWFZjT0tLXv WFW7Tt5KpjY3d6oYuaA02aGfvZA6TC8eNCvbWULy9AfqAyoNJBK1J/VceM7UuEfjN/+GKWkKI2kav LxSlm8LzS0Ym5LEx18Vg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NTm-009XOj-GA; Mon, 04 Jul 2022 14:55:14 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NTj-009XNS-5m for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:55:12 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 447C31480; Mon, 4 Jul 2022 07:55:09 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 281413F792; Mon, 4 Jul 2022 07:55:04 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 2/8] perf evsel: Do not request ptrauth sample field if not supported Date: Mon, 4 Jul 2022 15:53:26 +0100 Message-Id: <20220704145333.22557-3-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075511_357360_5643B9FF X-CRM114-Status: GOOD ( 18.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org A subsequent patch alters perf to perf_event_open with the PERF_SAMPLE_ARCH_1 bit on. This patch deals with the case where the kernel does not know about the PERF_SAMPLE_ARCH_1 bit, and does not know to send the pointer authentication masks. In this case the perf_event_open system call returns -EINVAL (-22) and perf exits with an error. This patch causes userspace process to re-attempt the perf_event_open system call but without asking for the PERF_SAMPLE_ARCH_1 sample field, allowing the perf_event_open system call to succeed. The check is placed to disable PERF_SAMPLE_ARCH_1 as the first thing to do in the try_fallback section of evsel__open_cpu() because it's the most recent sample field added, so should probably be the first thing to attempt to turn off. Signed-off-by: Andrew Kilroy --- tools/include/uapi/linux/perf_event.h | 5 ++++- tools/perf/util/evsel.c | 19 +++++++++++++++++++ tools/perf/util/evsel.h | 1 + tools/perf/util/perf_event_attr_fprintf.c | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h index d37629dbad72..821bf5ff6a19 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -162,12 +162,15 @@ enum perf_event_sample_format { PERF_SAMPLE_DATA_PAGE_SIZE = 1U << 22, PERF_SAMPLE_CODE_PAGE_SIZE = 1U << 23, PERF_SAMPLE_WEIGHT_STRUCT = 1U << 24, + PERF_SAMPLE_ARCH_1 = 1U << 25, - PERF_SAMPLE_MAX = 1U << 25, /* non-ABI */ + PERF_SAMPLE_MAX = 1U << 26, /* non-ABI */ __PERF_SAMPLE_CALLCHAIN_EARLY = 1ULL << 63, /* non-ABI; internal use */ }; +#define PERF_SAMPLE_ARM64_PTRAUTH PERF_SAMPLE_ARCH_1 + #define PERF_SAMPLE_WEIGHT_TYPE (PERF_SAMPLE_WEIGHT | PERF_SAMPLE_WEIGHT_STRUCT) /* * values to program into branch_sample_type when PERF_SAMPLE_BRANCH is set diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index ce499c5da8d7..25d8f804f49a 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1830,6 +1830,8 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, static void evsel__disable_missing_features(struct evsel *evsel) { + if (perf_missing_features.ptrauth) + evsel__reset_sample_bit(evsel, ARM64_PTRAUTH); if (perf_missing_features.weight_struct) { evsel__set_sample_bit(evsel, WEIGHT); evsel__reset_sample_bit(evsel, WEIGHT_STRUCT); @@ -1875,6 +1877,20 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, return err; } +static bool evsel__detect_ptrauth_masks_missing(struct evsel *evsel __maybe_unused) +{ +#if defined(__aarch64__) + if (!perf_missing_features.ptrauth && + (evsel->core.attr.sample_type & PERF_SAMPLE_ARM64_PTRAUTH)) { + perf_missing_features.ptrauth = true; + pr_debug2_peo("switching off request for pointer authentication masks\n"); + return true; + } +#endif + + return false; +} + bool evsel__detect_missing_features(struct evsel *evsel) { /* @@ -2114,6 +2130,9 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, return 0; try_fallback: + if (evsel__detect_ptrauth_masks_missing(evsel)) + goto fallback_missing_features; + if (evsel__precise_ip_fallback(evsel)) goto retry_open; diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 73ea48e94079..9690c35088bf 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -188,6 +188,7 @@ struct perf_missing_features { bool data_page_size; bool code_page_size; bool weight_struct; + bool ptrauth; }; extern struct perf_missing_features perf_missing_features; diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/perf_event_attr_fprintf.c index 98af3fa4ea35..d18d5f2c6891 100644 --- a/tools/perf/util/perf_event_attr_fprintf.c +++ b/tools/perf/util/perf_event_attr_fprintf.c @@ -36,7 +36,7 @@ static void __p_sample_type(char *buf, size_t size, u64 value) bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC), bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX), bit_name(CGROUP), bit_name(DATA_PAGE_SIZE), bit_name(CODE_PAGE_SIZE), - bit_name(WEIGHT_STRUCT), + bit_name(WEIGHT_STRUCT), bit_name(ARCH_1), { .name = NULL, } }; #undef bit_name From patchwork Mon Jul 4 14:53:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905460 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B9A5CC433EF for ; Mon, 4 Jul 2022 14:56:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gxVLMP0ahXZhBjD58d3733Re/1uTrAiIf7BUijxTVas=; b=tIR+ZDkXJgCwT0 exOfrhwYiyB+39w/HT0sEkyDzNzEDXceUPYDQhYX2kdztLJMs77plcGx2CiyHvNh5xZ2t7Pfcs3Xn pRLuDGDh2V4BKRe3wEa8VVbwsi6abF6hhRWCkkSrLG/IpEwYHkvrKRtSl6VbWRTGQl4XOz1Bsezv0 qHvtX2weUGaQ32ebU6WLHm94Cnp7QI2sjOaeS2Otn8uJ2adnwnsdGC1+wVLqAKgPIeZ/DTO/0WUyI B8ZCCnVG4SrlfI9bT+yzlzjz/nS0ADczzl559uR/1LjN0pLQVDVtrhhumkHQXs9qWbyK6gC2YjtTG gKBo4XS9GMuhD9I3iKhw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NU2-009XTY-Jh; Mon, 04 Jul 2022 14:55:30 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NTz-009XSI-Ks for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:55:29 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 843B114BF; Mon, 4 Jul 2022 07:55:23 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id BFBCA3F792; Mon, 4 Jul 2022 07:55:18 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: German Gomez , Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 3/8] perf test: Update arm64 tests to expect ptrauth masks Date: Mon, 4 Jul 2022 15:53:27 +0100 Message-Id: <20220704145333.22557-4-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075527_861402_3D6E4ABE X-CRM114-Status: GOOD ( 12.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: German Gomez We will request the pointer auth masks in a followup commit, so take the opportunity to update the relevant tests. Signed-off-by: German Gomez Signed-off-by: Andrew Kilroy --- tools/perf/tests/attr/README | 1 + .../tests/attr/test-record-graph-default-aarch64 | 3 ++- tools/perf/tests/attr/test-record-graph-dwarf | 1 + .../perf/tests/attr/test-record-graph-dwarf-aarch64 | 13 +++++++++++++ tools/perf/tests/attr/test-record-graph-fp-aarch64 | 3 ++- 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tools/perf/tests/attr/test-record-graph-dwarf-aarch64 diff --git a/tools/perf/tests/attr/README b/tools/perf/tests/attr/README index eb3f7d4bb324..9d7f4646920f 100644 --- a/tools/perf/tests/attr/README +++ b/tools/perf/tests/attr/README @@ -47,6 +47,7 @@ Following tests are defined (with perf commands): perf record -g kill (test-record-graph-default) perf record -g kill (test-record-graph-default-aarch64) perf record --call-graph dwarf kill (test-record-graph-dwarf) + perf record --call-graph dwarf kill (test-record-graph-dwarf-aarch64) perf record --call-graph fp kill (test-record-graph-fp) perf record --call-graph fp kill (test-record-graph-fp-aarch64) perf record --group -e cycles,instructions kill (test-record-group) diff --git a/tools/perf/tests/attr/test-record-graph-default-aarch64 b/tools/perf/tests/attr/test-record-graph-default-aarch64 index e98d62efb6f7..948d41c162aa 100644 --- a/tools/perf/tests/attr/test-record-graph-default-aarch64 +++ b/tools/perf/tests/attr/test-record-graph-default-aarch64 @@ -5,5 +5,6 @@ ret = 1 arch = aarch64 [event:base-record] -sample_type=4391 +# handle both with and without ARM64_PTRAUTH +sample_type=4391|33558823 sample_regs_user=1073741824 diff --git a/tools/perf/tests/attr/test-record-graph-dwarf b/tools/perf/tests/attr/test-record-graph-dwarf index ae92061d611d..619bccd886c4 100644 --- a/tools/perf/tests/attr/test-record-graph-dwarf +++ b/tools/perf/tests/attr/test-record-graph-dwarf @@ -2,6 +2,7 @@ command = record args = --no-bpf-event --call-graph dwarf -- kill >/dev/null 2>&1 ret = 1 +arch = !aarch64 [event:base-record] sample_type=45359 diff --git a/tools/perf/tests/attr/test-record-graph-dwarf-aarch64 b/tools/perf/tests/attr/test-record-graph-dwarf-aarch64 new file mode 100644 index 000000000000..daec43b39e2e --- /dev/null +++ b/tools/perf/tests/attr/test-record-graph-dwarf-aarch64 @@ -0,0 +1,13 @@ +[config] +command = record +args = --no-bpf-event --call-graph dwarf -- kill >/dev/null 2>&1 +ret = 1 +arch = aarch64 + +[event:base-record] +# handle both with and without ARM64_PTRAUTH +sample_type=45359|33599791 +exclude_callchain_user=1 +sample_stack_user=8192 +sample_regs_user=* +mmap_data=1 diff --git a/tools/perf/tests/attr/test-record-graph-fp-aarch64 b/tools/perf/tests/attr/test-record-graph-fp-aarch64 index cbeea9971285..bc0880f71e8e 100644 --- a/tools/perf/tests/attr/test-record-graph-fp-aarch64 +++ b/tools/perf/tests/attr/test-record-graph-fp-aarch64 @@ -5,5 +5,6 @@ ret = 1 arch = aarch64 [event:base-record] -sample_type=4391 +# handle both with and without ARM64_PTRAUTH +sample_type=4391|33558823 sample_regs_user=1073741824 From patchwork Mon Jul 4 14:53:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905461 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 49494C433EF for ; Mon, 4 Jul 2022 14:56:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Xf7YPH9mPAoSKw4ty9uz/xOnuSZNAw8/vHAfVo2CfWc=; b=E7JOT12JF3Ns4q pi35x7AdhKhkqgUJOkg9xHk8KmpIbVaNrwC0MSQO1e3DIYxTL80KG6w7KyKGu5XzSXh4fEfyHrIFk lp7FqE+zlt6Xeb1D7IvOkXxAbW3Hx7i2JzqFaqHWW00ckm/CX3VgT9WK0BFKgeZSL/cZQe4FTAa9V 6BoBKc/C7rU6/afXKBeXj8oMSSZJvnP0hKO3+OMyLkun5WpsB89ShgYv17s49YD6Kv0Ps+Tf4GVkf JPzGF1ZQiZAENp69xD4mgm8HHS2ULZElOOV2niwnsx0otji9+uPAI5FikF3olnD7WL1130ma7w+EJ RHdqjHLxqvT9xlEoURfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUI-009Xa7-0M; Mon, 04 Jul 2022 14:55:46 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUD-009XX7-DT for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:55:44 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C28BA150C; Mon, 4 Jul 2022 07:55:39 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8CF6F3F792; Mon, 4 Jul 2022 07:55:35 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 4/8] perf tools: arm64: Read ptrauth data from kernel Date: Mon, 4 Jul 2022 15:53:28 +0100 Message-Id: <20220704145333.22557-5-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075541_560341_01B802EA X-CRM114-Status: GOOD ( 20.48 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch alters the userspace perf program to request the pointer authentication code masks using the PERF_SAMPLE_ARCH_1 sample field and write the data to perf.data file. A subsequent commit will make use of the masks in the data file to do the unwinding. Signed-off-by: Andrew Kilroy --- tools/perf/tests/sample-parsing.c | 2 +- tools/perf/util/event.h | 8 ++++++ tools/perf/util/evsel.c | 45 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c index 07f2411b0ad4..dd78ca279c01 100644 --- a/tools/perf/tests/sample-parsing.c +++ b/tools/perf/tests/sample-parsing.c @@ -381,7 +381,7 @@ static int test__sample_parsing(struct test_suite *test __maybe_unused, int subt * were added. Please actually update the test rather than just change * the condition below. */ - if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) { + if (PERF_SAMPLE_MAX > PERF_SAMPLE_ARCH_1 << 1) { pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n"); return -1; } diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index cdd72e05fd28..b99fc81dd37e 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h @@ -128,6 +128,13 @@ struct aux_sample { void *data; }; + +struct ptrauth_info { + u64 enabled_keys; // arm64 ptrauth is in use if this is non-zero. + u64 insn_mask; + u64 data_mask; +}; + struct perf_sample { u64 ip; u32 pid, tid; @@ -163,6 +170,7 @@ struct perf_sample { struct stack_dump user_stack; struct sample_read read; struct aux_sample aux_sample; + struct ptrauth_info ptrauth; }; #define PERF_MEM_DATA_SRC_NONE \ diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 25d8f804f49a..4627a68a7797 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -887,8 +887,22 @@ static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *o "Falling back to framepointers.\n"); } +#if defined(__aarch64__) + /* + * We need to set ARM64_PTRAUTH in FP mode so that b9f6fbb3b2c2 ("perf arm64: Inject missing + * frames when using 'perf record --call-graph=fp'") continues to work in the presence of + * PACs. + */ + if (param->record_mode == CALLCHAIN_FP) + evsel__set_sample_bit(evsel, ARM64_PTRAUTH); + +#endif + if (param->record_mode == CALLCHAIN_DWARF) { if (!function) { +#if defined(__aarch64__) + evsel__set_sample_bit(evsel, ARM64_PTRAUTH); +#endif evsel__set_sample_bit(evsel, REGS_USER); evsel__set_sample_bit(evsel, STACK_USER); if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) { @@ -2344,6 +2358,17 @@ u64 evsel__bitfield_swap_branch_flags(u64 value) return new_val; } +/* + * To return the normalised arch that is recorded in a perf.data file + */ +static const char *recorded_normalized_arch(struct evsel *evsel) +{ + if (evsel && evsel->evlist && evsel->evlist->env) + return perf_env__arch(evsel->evlist->env); + else + return NULL; +} + int evsel__parse_sample(struct evsel *evsel, union perf_event *event, struct perf_sample *data) { @@ -2681,6 +2706,26 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event, array = (void *)array + sz; } + if (type & PERF_SAMPLE_ARCH_1) { + const char *normlzd_arch = recorded_normalized_arch(evsel); + + if (normlzd_arch) + pr_debug4("PERF_SAMPLE_ARCH_1 is on, detected recorded arch as %s\n", normlzd_arch); + else + pr_debug4("PERF_SAMPLE_ARCH_1 is on, but arch not detected\n"); + + if (normlzd_arch && strcmp(normlzd_arch, "arm64") == 0) { + OVERFLOW_CHECK(array, 3 * sizeof(u64), max_size); + + data->ptrauth.enabled_keys = *array; + array++; + data->ptrauth.insn_mask = *array; + array++; + data->ptrauth.data_mask = *array; + array++; + } + } + return 0; } From patchwork Mon Jul 4 14:53:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905462 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 13167C43334 for ; Mon, 4 Jul 2022 14:57:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=SoZWZ8g9ZNL/i6ZVJ7GXsJiG3/5g0I0QcIjcQWUWBRA=; b=FyB19Vy25Ki1Al MFtKes7pNBMh8Smb/SrXEtJmyXdVB2moLhgtuW6UVer5xnSBRAZ1LzSAzAIPHwLP/TDh0qU0l26oW IF/WHJidiHhZ+lMkyHqkmfaXua2eGlIvVvy4Jde0RUDR2wVGoO0hAJ+O8ySrLUFWGV+JJl+Bnb4j7 RIWSO6yaEF+JPVKcztZblB5s1lyKiQE4+NOqiX+IuyHJhcvRZt/S4Jo12uGDNP4jGgkahG7My5GJ6 caEefIs9ZKu9OiQgCZ+4YnuOLFnQkYNyVuZokgPl0IHGNEecJOD2n1QZePej3lUKJVM5/zChy18Xn 2KyG3Ud/Ov47rR4Z6dRQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUY-009XiW-JC; Mon, 04 Jul 2022 14:56:02 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUU-009Xgq-LQ for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:56:00 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 99143150C; Mon, 4 Jul 2022 07:55:57 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2F8D63F792; Mon, 4 Jul 2022 07:55:53 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 5/8] perf libunwind: Feature check for libunwind ptrauth callback Date: Mon, 4 Jul 2022 15:53:29 +0100 Message-Id: <20220704145333.22557-6-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075558_900332_B3FEB056 X-CRM114-Status: GOOD ( 16.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch prepares for a version of libunwind that is capable of unwinding stacks with pointer authentication. Without this change, anyone compiling perf would have to depend on a very new version of libunwind that has the callback to supply it with pointer authentication masks. This patch detects if libunwind is recent enough, and if so, sets a pre-processor flag that will be used in a subsequent commit to call libunwind appropriately. If libunwind is not recent enough, the pre-processor flag is not set. Signed-off-by: Andrew Kilroy --- tools/build/Makefile.feature | 2 ++ tools/build/feature/Makefile | 4 +++ tools/build/feature/test-all.c | 5 ++++ .../feature/test-libunwind-arm64-ptrauth.c | 26 +++++++++++++++++++ tools/perf/Makefile.config | 10 +++++++ 5 files changed, 47 insertions(+) create mode 100644 tools/build/feature/test-libunwind-arm64-ptrauth.c diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature index 888a0421d43b..a894101342fc 100644 --- a/tools/build/Makefile.feature +++ b/tools/build/Makefile.feature @@ -90,6 +90,7 @@ FEATURE_TESTS_EXTRA := \ libunwind-x86_64 \ libunwind-arm \ libunwind-aarch64 \ + libunwind-arm64-ptrauth \ libunwind-debug-frame \ libunwind-debug-frame-arm \ libunwind-debug-frame-aarch64 \ @@ -128,6 +129,7 @@ FEATURE_DISPLAY ?= \ libpython \ libcrypto \ libunwind \ + libunwind-arm64-ptrauth\ libdw-dwarf-unwind \ zlib \ lzma \ diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile index 7c2a17e23c30..ac23175d5bcb 100644 --- a/tools/build/feature/Makefile +++ b/tools/build/feature/Makefile @@ -45,6 +45,7 @@ FILES= \ test-libunwind-aarch64.bin \ test-libunwind-debug-frame-arm.bin \ test-libunwind-debug-frame-aarch64.bin \ + test-libunwind-arm64-ptrauth.bin \ test-pthread-attr-setaffinity-np.bin \ test-pthread-barrier.bin \ test-stackprotector-all.bin \ @@ -193,6 +194,9 @@ $(OUTPUT)test-libunwind-debug-frame-arm.bin: $(OUTPUT)test-libunwind-debug-frame-aarch64.bin: $(BUILD) -lelf -lunwind-aarch64 +$(OUTPUT)test-libunwind-arm64-ptrauth.bin: + $(BUILD) # -lunwind provided by $(FEATURE_CHECK_LDFLAGS-libunwind-arm64-ptrauth) + $(OUTPUT)test-libaudit.bin: $(BUILD) -laudit diff --git a/tools/build/feature/test-all.c b/tools/build/feature/test-all.c index 5ffafb967b6e..86780c5c78e5 100644 --- a/tools/build/feature/test-all.c +++ b/tools/build/feature/test-all.c @@ -66,6 +66,10 @@ # include "test-libunwind.c" #undef main +#define main main_test_libunwind_arm64_ptrauth +# include "test-libunwind-arm64-ptrauth.c" +#undef main + #define main main_test_libslang # include "test-libslang.c" #undef main @@ -186,6 +190,7 @@ int main(int argc, char *argv[]) main_test_libelf_gelf_getnote(); main_test_libelf_getshdrstrndx(); main_test_libunwind(); + main_test_libunwind_arm64_ptrauth(); main_test_libslang(); main_test_libbfd(); main_test_libbfd_buildid(); diff --git a/tools/build/feature/test-libunwind-arm64-ptrauth.c b/tools/build/feature/test-libunwind-arm64-ptrauth.c new file mode 100644 index 000000000000..51650ceef90e --- /dev/null +++ b/tools/build/feature/test-libunwind-arm64-ptrauth.c @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +static unw_word_t get_insn_mask(unw_addr_space_t addr_space, void *unwind_info_ptr) +{ + return 0; +} + +// This feature test is intending to check if the version +// of the available libunwind library is one that has the +// ptrauth_insn_mask callback function. +// If it doesn't this feature check should fail to compile. +static unw_accessors_t accessors = { + .ptrauth_insn_mask = get_insn_mask, +}; + +int main(void) +{ + unw_addr_space_t addr_space = unw_create_addr_space(&accessors, 0); + + if (addr_space) + return 0; + + return 0; +} diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 73e0762092fe..2578b1d1a502 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -133,6 +133,8 @@ FEATURE_CHECK_CFLAGS-libunwind = $(LIBUNWIND_CFLAGS) FEATURE_CHECK_LDFLAGS-libunwind = $(LIBUNWIND_LDFLAGS) $(LIBUNWIND_LIBS) FEATURE_CHECK_CFLAGS-libunwind-debug-frame = $(LIBUNWIND_CFLAGS) FEATURE_CHECK_LDFLAGS-libunwind-debug-frame = $(LIBUNWIND_LDFLAGS) $(LIBUNWIND_LIBS) +FEATURE_CHECK_CFLAGS-libunwind-arm64-ptrauth = $(LIBUNWIND_CFLAGS) +FEATURE_CHECK_LDFLAGS-libunwind-arm64-ptrauth = $(LIBUNWIND_LDFLAGS) $(LIBUNWIND_LIBS) FEATURE_CHECK_LDFLAGS-libunwind-arm += -lunwind -lunwind-arm FEATURE_CHECK_LDFLAGS-libunwind-aarch64 += -lunwind -lunwind-aarch64 @@ -677,6 +679,14 @@ ifndef NO_LIBUNWIND $(call detected,CONFIG_LOCAL_LIBUNWIND) endif + ifeq ($(have_libunwind), 1) + $(call feature_check,libunwind-arm64-ptrauth) + ifneq ($(feature-libunwind-arm64-ptrauth),1) + CFLAGS += -DNO_LIBUNWIND_ARM64_PTRAUTH + msg := $(warning libunwind cannot produce user stacks in the presence of pointer authentication.); + endif + endif + ifneq ($(have_libunwind), 1) NO_LIBUNWIND := 1 endif From patchwork Mon Jul 4 14:53:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905463 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5E307C43334 for ; Mon, 4 Jul 2022 14:57:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8rt9vIps9BBB2Zhceu9DDdTI9D/jG0R7oRohNyvw44E=; b=AHh+l+L4T02pQr ymY/fg2ktmn36+uA6H0pwjESrU+pmSRqNu8GzMY+MvzpgsLT6MaxqoAoEPfZS6My61pN7yzoVEp7+ ygNJPPvF2v5SXxfTMpi1eXpsZywqylH49PJfiy04kP6RieFKkSf4eWIY/UO7yPn0DKWhzKbMJdTPk 1BtTbLHrnO1EAAwsMeB0z7kWpHOKVhnbJE4PMGKMuA25/BQiwn4njkXeMluKJkXDIo7euE0UlO+t3 fcSNxg8qS4VZtWzAI4amH/dYOxKw0lnvGc1IJiArwvbvYm2Bm1G2+o1VyWiML9UJ2LRzuwqMzy4cD dq8zePyWC0HIMUFKHlkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUs-009Xvn-Ll; Mon, 04 Jul 2022 14:56:23 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUo-009XtV-SH for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:56:20 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EAAFB150C; Mon, 4 Jul 2022 07:56:10 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9972A3F792; Mon, 4 Jul 2022 07:56:07 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 6/8] perf libunwind: arm64 pointer authentication Date: Mon, 4 Jul 2022 15:53:30 +0100 Message-Id: <20220704145333.22557-7-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075618_983760_D49259F9 X-CRM114-Status: UNSURE ( 9.37 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Make use of new changes in libunwind to decode a pointer which has a pointer authentication code (PAC) in it. Before this patch, perf is not able to produce stack traces where the instruction addresses had PACs in them. This commit has a dependency on a libunwind pull request: https://github.com/libunwind/libunwind/pull/360 Signed-off-by: Andrew Kilroy --- tools/perf/util/unwind-libunwind-local.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c index 6e5b8cce47bf..6983a3e76a71 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -652,6 +652,15 @@ static void display_error(int err) } } +#ifndef NO_LIBUNWIND_ARM64_PTRAUTH +static unw_word_t get_insn_mask(unw_addr_space_t addr_space __maybe_unused, void *unwind_info_ptr) +{ + struct unwind_info *ui = unwind_info_ptr; + unw_word_t mask = ui->sample->ptrauth.insn_mask; + return mask; +} +#endif + static unw_accessors_t accessors = { .find_proc_info = find_proc_info, .put_unwind_info = put_unwind_info, @@ -661,6 +670,9 @@ static unw_accessors_t accessors = { .access_fpreg = access_fpreg, .resume = resume, .get_proc_name = get_proc_name, +#ifndef NO_LIBUNWIND_ARM64_PTRAUTH + .ptrauth_insn_mask = get_insn_mask, +#endif }; static int _unwind__prepare_access(struct maps *maps) From patchwork Mon Jul 4 14:53:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905464 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5A3B6C433EF for ; Mon, 4 Jul 2022 14:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=v3oDzIL1Xj8gjPsmofCOf6aKOffmCUtuEiy092vD6ew=; b=ZJPxDz6kDP8/U6 Vu9J/IN0CD6P7UWUrbGbQGK2nHviARfUBYtJRHSnjcviGSzk8DRASFcSb56pee5WCtbWKxOhuKlQg rI+9WY864yWdkxnr2GCRFQKPU9Bq5cBO19f1RjYKGXAwEX53NgJlVWNgsLvYNeKBFgKk8iu2Lsmwr bhhRcy8qm/s89jz8HAk5DDa86KuLvp2h1zPIOvEuqWvDNVgjMecBOYTOY6G2dy+q/BIjGTehQxNID hmem4hQXWAn4/5G4BoOblWFt52FV57zLLMX65A7PzmpHM5a/BHsEyZbw7L4NLnJDZSOehHpzh6Ju+ XptGlRIz4m5ieum/Wr2g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NV8-009Y4z-00; Mon, 04 Jul 2022 14:56:38 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NUu-009Xwi-1y for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:56:25 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4CD2E150C; Mon, 4 Jul 2022 07:56:23 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E23E03F792; Mon, 4 Jul 2022 07:56:18 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 7/8] perf tools: Print ptrauth struct in perf report Date: Mon, 4 Jul 2022 15:53:31 +0100 Message-Id: <20220704145333.22557-8-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075624_199258_7C020610 X-CRM114-Status: UNSURE ( 9.21 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch prints a perf sample's ptrauth struct so that the PAC masks can be seen. To aid debugging. Signed-off-by: Andrew Kilroy --- tools/perf/util/session.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 37f833c3c81b..6b56e638d4dd 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1329,6 +1329,13 @@ char *get_page_size_name(u64 size, char *str) return str; } +static void ptrauth__printf(struct ptrauth_info *ptrauth) +{ + printf(" . ptrauth enabled keys: 0x%016"PRIx64"\n", ptrauth->enabled_keys); + printf(" . ptrauth instruction mask: 0x%016"PRIx64"\n", ptrauth->insn_mask); + printf(" . ptrauth data mask: 0x%016"PRIx64"\n", ptrauth->data_mask); +} + static void dump_sample(struct evsel *evsel, union perf_event *event, struct perf_sample *sample, const char *arch) { @@ -1385,6 +1392,14 @@ static void dump_sample(struct evsel *evsel, union perf_event *event, if (sample_type & PERF_SAMPLE_READ) sample_read__printf(sample, evsel->core.attr.read_format); + + if (sample_type & PERF_SAMPLE_ARCH_1) { + const char *normlzd_arch = perf_env__arch(evsel->evlist->env); + + if (normlzd_arch && strcmp(normlzd_arch, "arm64") == 0) + ptrauth__printf(&sample->ptrauth); + } + } static void dump_read(struct evsel *evsel, union perf_event *event) From patchwork Mon Jul 4 14:53:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Kilroy X-Patchwork-Id: 12905465 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 57D26C43334 for ; Mon, 4 Jul 2022 14:58:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=hygigPA00M984M2K3F/1BEKwcxcsNTgUB3Ez1vjqlng=; b=BK92oEDAxYpDhc n7uDhhSvhUNMbkSKG/6kZp995AuHUIJ1sQSeD+kmQjcYXRGHTzZXZx5lcHzML6CWCo16368bYNgU9 eUxQadGyRb6/ekJPTZCNYKwGYJ6STeT9+4eoqSX0z2hOGKjHkcHUSHGbP19n0sQTDDf56lHWyr6BP cWTtzAk2tw7FD9zsb2p6oeaipz6OxykSQBAxhgITeVDJj0995YrFfjU9GXMEstbC+/6/Gka3brBLm J6Scp7mQ19qz4xi1rk0U5Gn2YWgvQqiZV5Y5Q1srPL7ycsMgwNiDCeb45nR8rH1mH863RzqRUl2S5 JyFOsayX6caka/muM2CQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NVT-009YGy-89; Mon, 04 Jul 2022 14:56:59 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8NV8-009Y5F-36 for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 14:56:40 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 027F7150C; Mon, 4 Jul 2022 07:56:36 -0700 (PDT) Received: from e124483.cambridge.arm.com (e124483.cambridge.arm.com [10.1.29.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id DB2973F792; Mon, 4 Jul 2022 07:56:31 -0700 (PDT) From: Andrew Kilroy To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: German Gomez , Andrew Kilroy , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Tom Rix , linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 8/8] perf test arm64: Test unwinding with PACs on gcc & clang compilers Date: Mon, 4 Jul 2022 15:53:32 +0100 Message-Id: <20220704145333.22557-9-andrew.kilroy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704145333.22557-1-andrew.kilroy@arm.com> References: <20220704145333.22557-1-andrew.kilroy@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_075638_299460_1119BEA6 X-CRM114-Status: GOOD ( 18.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: German Gomez Adds self test to test unwindings in the presence of PACs for different versions of gcc and clang. Example run: | $ ./perf test 74 | 74: Arm64 unwinding with PAC support : | 74.1: gcc-9 : Ok | 74.2: gcc-10 : Ok | 74.3: gcc-11 : Ok | 74.4: gcc-12 : Ok | 74.5: clang-12 : Skip (not installed) | 74.6: clang-13 : Skip (not installed) | 74.7: clang-14 : Ok Signed-off-by: German Gomez Signed-off-by: Andrew Kilroy --- tools/perf/Makefile.perf | 1 + tools/perf/tests/Build | 1 + tools/perf/tests/arm_unwind_pac.c | 113 +++++++++++++++++++++++++++++ tools/perf/tests/arm_unwind_pac.sh | 57 +++++++++++++++ tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/tests.h | 1 + 6 files changed, 174 insertions(+) create mode 100644 tools/perf/tests/arm_unwind_pac.c create mode 100755 tools/perf/tests/arm_unwind_pac.sh diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index 8f738e11356d..35d067534cf1 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -1008,6 +1008,7 @@ endif install-tests: all install-gtk $(call QUIET_INSTALL, tests) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ + $(INSTALL) tests/arm_unwind_pac.sh '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) tests/pe-file.exe* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'; \ diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index af2b37ef7c70..a03c189c5e98 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -66,6 +66,7 @@ perf-y += expand-cgroup.o perf-y += perf-time-to-tsc.o perf-y += dlfilter-test.o perf-y += sigtrap.o +perf-y += arm_unwind_pac.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir) diff --git a/tools/perf/tests/arm_unwind_pac.c b/tools/perf/tests/arm_unwind_pac.c new file mode 100644 index 000000000000..11b7e936a72d --- /dev/null +++ b/tools/perf/tests/arm_unwind_pac.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include + +#include "pmu.h" +#include "tests.h" +#include "debug.h" + +#if defined(HAVE_LIBUNWIND_SUPPORT) && defined(__aarch64__) + +#define BUF_MAX 1024 + +static bool test_command_exists(const char *cmd) +{ + char buf[BUF_MAX]; + + scnprintf(buf, BUF_MAX, "which %s", cmd); + return !system(buf); +} + +static int run_dir(const char *d, const char *compiler, const char *compiler_argv) +{ + char buf[BUF_MAX]; + + if (!test_command_exists(compiler)) + return TEST_SKIP; + + scnprintf(buf, BUF_MAX, "%s/arm_unwind_pac.sh %s '%s'", d, compiler, compiler_argv); + return system(buf) ? TEST_FAIL : TEST_OK; +} + +static int run(const char *compiler, const char *compiler_argv) +{ + struct stat st; + char exec_test_path[BUF_MAX]; + char *exec_path; + + /* Check development tree tests. */ + if (!lstat("./tests", &st)) + return run_dir("./tests", compiler, compiler_argv); + + /* Otherwise, check installed path */ + exec_path = get_argv_exec_path(); + if (exec_path == NULL) + return -1; + + snprintf(exec_test_path, BUF_MAX, "%s/tests", exec_path); + if (!lstat(exec_test_path, &st)) + return run_dir(exec_test_path, compiler, compiler_argv); + + return TEST_SKIP; +} + +#define TEST_COMPILER(_test, compiler, args) \ + static int test__##_test(struct test_suite *test __maybe_unused, \ + int subtest __maybe_unused) \ + { \ + return run(compiler, args); \ + } + +/* + * gcc compilers + */ +TEST_COMPILER(gcc8, "gcc-8", "-msign-return-address=all") +TEST_COMPILER(gcc9, "gcc-9", "-mbranch-protection=pac-ret+leaf") +TEST_COMPILER(gcc10, "gcc-10", "-mbranch-protection=pac-ret+leaf") +TEST_COMPILER(gcc11, "gcc-11", "-mbranch-protection=pac-ret+leaf") +TEST_COMPILER(gcc12, "gcc-12", "-mbranch-protection=pac-ret+leaf") + +/* + * clang compilers + */ +TEST_COMPILER(clang12, "clang-12", "-msign-return-address=all") +TEST_COMPILER(clang13, "clang-13", "-msign-return-address=all") +TEST_COMPILER(clang14, "clang-14", "-msign-return-address=all") + +static struct test_case pac_tests[] = { +#define PAC_TEST_CASE(name, test) \ + TEST_CASE_REASON(name, test, "not installed") + + PAC_TEST_CASE("gcc-8", gcc8), + PAC_TEST_CASE("gcc-9", gcc9), + PAC_TEST_CASE("gcc-10", gcc10), + PAC_TEST_CASE("gcc-11", gcc11), + PAC_TEST_CASE("gcc-12", gcc12), + + PAC_TEST_CASE("clang-12", clang12), + PAC_TEST_CASE("clang-13", clang13), + PAC_TEST_CASE("clang-14", clang14), + +#undef PAC_TEST_CASE + { .name = NULL, } +}; + +struct test_suite suite__arm_unwind_pac = { + .desc = "Arm64 unwinding with PAC support", + .test_cases = pac_tests, +}; + +#else // HAVE_LIBUNWIND_SUPPORT && __aarch64__ + +static int test__arm_unwind_pac(struct test_suite *test __maybe_unused, int subtest __maybe_unused) +{ + return TEST_SKIP; +} + +DEFINE_SUITE("Arm64 unwinding with PAC support", arm_unwind_pac); + +#endif // HAVE_LIBUNWIND_SUPPORT && __aarch64__ diff --git a/tools/perf/tests/arm_unwind_pac.sh b/tools/perf/tests/arm_unwind_pac.sh new file mode 100755 index 000000000000..7491dc5f908b --- /dev/null +++ b/tools/perf/tests/arm_unwind_pac.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +COMPILER_CMD="$1" +COMPILER_ARG="$2 -g -fno-omit-frame-pointer -fno-inline" + +echo "COMPILER_CMD=$COMPILER_CMD" +echo "COMPILER_ARG=$COMPILER_ARG" + +TMPDIR=$(mktemp -d /tmp/__perf_test.XXXXX) + +clean_up() { + rm -rf $TMPDIR +} + +trap clean_up exit term int + +cat << EOF > $TMPDIR/program.c +void bar(void) { + for(;;); +} +void foo(void) { + bar(); +} +int main(void) { + foo(); + return 0; +} +EOF + +$COMPILER_CMD $COMPILER_ARG $TMPDIR/program.c -o $TMPDIR/program + +perf record --call-graph=dwarf -e cycles//u -o $TMPDIR/perf-dwarf.data -- $TMPDIR/program & +PID=$! +sleep 1 +kill $PID +wait $PID + +perf record --call-graph=fp -e cycles//u -o $TMPDIR/perf-fp.data -- $TMPDIR/program & +PID=$! +sleep 1 +kill $PID +wait $PID + +perf report --symbols=bar -i $TMPDIR/perf-dwarf.data --stdio > $TMPDIR/report-dwarf +perf report --symbols=bar -i $TMPDIR/perf-fp.data --stdio > $TMPDIR/report-fp + +set -e +set -x + +grep "main" $TMPDIR/report-dwarf +grep "foo" $TMPDIR/report-dwarf +grep "bar" $TMPDIR/report-dwarf + +grep "main" $TMPDIR/report-fp +grep "foo" $TMPDIR/report-fp +grep "bar" $TMPDIR/report-fp diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 81cf241cd109..e121cfd43b8d 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -108,6 +108,7 @@ static struct test_suite *generic_tests[] = { &suite__perf_time_to_tsc, &suite__dlfilter, &suite__sigtrap, + &suite__arm_unwind_pac, NULL, }; diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 5bbb8f6a48fc..459e473a91cd 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -147,6 +147,7 @@ DECLARE_SUITE(expand_cgroup_events); DECLARE_SUITE(perf_time_to_tsc); DECLARE_SUITE(dlfilter); DECLARE_SUITE(sigtrap); +DECLARE_SUITE(arm_unwind_pac); /* * PowerPC and S390 do not support creation of instruction breakpoints using the