From patchwork Thu Oct 20 10:19:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Clark X-Patchwork-Id: 13013219 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 E0774C4332F for ; Thu, 20 Oct 2022 11:06:58 +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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=4Sm1zLMCDqOrvajycv/OdoTI4Zw9a3hBTbkiMQEvMZ4=; b=dCtnGGjE2s3Fu4 93L93QklZX8u1o090PM9JvHRRIlTgnDcBjzbIiFbRr4ypU0UKkWeOMrcDpisHYJ5C7YQIPqHC7h8G ddqyjN1OahHR3CPcxbhjMpBL+1dnJ9Jb7EYhVlDoPZ0T4PIMDNs3Qe2yMmglRAR6w4TwN0e1EiyOS 0j70koBB/PjNbZ5b9FAfy7s5k7NwB2BS/KuEvUFrREjBtQAFnnNrTZIZ++AyD3I8zHyxoLa9kUbON N5JN6d4YBMIGTu5kEBiNItH/PhH7Y/n4984sc44Sj9ySDmsjoBS5gA/rbZZPSxq8pFsh0lK4ReBGA ct/YDZDIFdDnZ341UrXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1olTMp-00DtdS-NY; Thu, 20 Oct 2022 11:05:40 +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 1olSfO-00DTpZ-Tl for linux-arm-kernel@lists.infradead.org; Thu, 20 Oct 2022 10:20:48 +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 DA2EB1063; Thu, 20 Oct 2022 03:20:49 -0700 (PDT) Received: from e121896.arm.com (unknown [10.57.5.110]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id DE2E23F792; Thu, 20 Oct 2022 03:20:40 -0700 (PDT) From: James Clark To: linux-perf-users@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, broonie@kernel.org, acme@kernel.org, Andrew Kilroy , Vince Weaver , Mark Rutland , Peter Zijlstra , James Clark , Ingo Molnar , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Will Deacon , Catalin Marinas Subject: [PATCH v2 1/1] perf arm64: Send pointer auth masks to ring buffer Date: Thu, 20 Oct 2022 11:19:20 +0100 Message-Id: <20221020101921.1219533-2-james.clark@arm.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20221020101921.1219533-1-james.clark@arm.com> References: <20221020101921.1219533-1-james.clark@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221020_032047_068053_25769325 X-CRM114-Status: GOOD ( 26.45 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Andrew Kilroy 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 arm64, it contains the ptrauth mask information. The event will currently fail to open on architectures other than arm64 if PERF_SAMPLE_ARCH_1 is set. It will also fail to open on arm64 if CONFIG_ARM64_PTR_AUTH isn't set, as the data would always be zeros. Cc: Vince Weaver Cc: Arnaldo Carvalho de Melo Cc: Mark Rutland Cc: Peter Zijlstra Signed-off-by: Andrew Kilroy Signed-off-by: James Clark --- arch/arm64/include/asm/perf_event.h | 32 +++++++++++++++++++++++++++++ arch/arm64/kernel/perf_event.c | 32 +++++++++++++++++++++++++++++ include/linux/perf_event.h | 12 +++++++++++ include/uapi/linux/perf_event.h | 4 +++- kernel/events/core.c | 31 ++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/perf_event.h b/arch/arm64/include/asm/perf_event.h index 3eaf462f5752..160fdb8fca1c 100644 --- a/arch/arm64/include/asm/perf_event.h +++ b/arch/arm64/include/asm/perf_event.h @@ -273,4 +273,36 @@ extern unsigned long perf_misc_flags(struct pt_regs *regs); (regs)->pstate = PSR_MODE_EL1h; \ } +#ifdef CONFIG_ARM64_PTR_AUTH +#define HAS_ARCH_SAMPLE_DATA +/* + * 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 /* CONFIG_ARM64_PTR_AUTH */ #endif diff --git a/arch/arm64/kernel/perf_event.c b/arch/arm64/kernel/perf_event.c index cb69ff1e6138..9c209168e055 100644 --- a/arch/arm64/kernel/perf_event.c +++ b/arch/arm64/kernel/perf_event.c @@ -1459,3 +1459,35 @@ void arch_perf_update_userpage(struct perf_event *event, userpg->cap_user_time_zero = 1; userpg->cap_user_time_short = 1; } + +#ifdef CONFIG_ARM64_PTR_AUTH +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); +} + +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 keys_result = ptrauth_get_enabled_keys(current); + 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)); +} + +int perf_event_open_request_arch_1(void) +{ + return 0; +} +#endif /* CONFIG_ARM64_PTR_AUTH */ diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 853f64b6c8c2..f6b0cc93faae 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1065,6 +1065,9 @@ struct perf_sample_data { u64 cgroup; u64 data_page_size; u64 code_page_size; +#ifdef HAS_ARCH_SAMPLE_DATA + struct arch_sample_data arch; +#endif } ____cacheline_aligned; /* default value for data source */ @@ -1674,6 +1677,15 @@ int perf_event_exit_cpu(unsigned int cpu); extern void __weak arch_perf_update_userpage(struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now); +extern 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); +extern void perf_prepare_sample_arch_1(struct perf_event_header *header, + struct perf_sample_data *data, + struct perf_event *event, + struct pt_regs *regs); +extern int perf_event_open_request_arch_1(void); #ifdef CONFIG_MMU extern __weak u64 arch_perf_get_page_size(struct mm_struct *mm, unsigned long addr); diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 85be78e0e7f6..3c8349111422 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -162,10 +162,12 @@ 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 */ }; +#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 b981b879bcd8..8ca0501d608d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5822,6 +5822,25 @@ void __weak arch_perf_update_userpage( { } +void __weak 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 __weak perf_prepare_sample_arch_1(struct perf_event_header *header, + struct perf_sample_data *data, + struct perf_event *event, + struct pt_regs *regs) +{ +} + +int __weak perf_event_open_request_arch_1(void) +{ + return -EINVAL; +} + /* * Callers need to ensure there can be no nesting of this function, otherwise * the seqlock logic goes bad. We can not serialize this because the arch @@ -7142,6 +7161,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; @@ -7466,6 +7488,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; @@ -12140,6 +12165,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