From patchwork Tue Oct 1 13:36:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Gouly X-Patchwork-Id: 13818008 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 57C27CF64BE for ; Tue, 1 Oct 2024 13:39:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=c0dYh7PbdCPP/8Ei/6LJSWz1xW+ihra4b2Z1AYIpZB4=; b=zgqEEwE54V1qmQSCbRTouALq/l QDCFGdgck8MgG6kPitDAmLzDnlAKSt1N3a5SlgqsfXiGvQqdrNq8h/xswGIOdRN/XD3YblvwvGyyi ty7CNbwA8GTDLFYcvjhprMApMy7jiHwklSHtGLBdKYvjfKWqpPxeWmVqA0YubO6X7Ypkh/psKMCMg O7+Dd8ZfJCWjIYBEtf9mCwWSvz5hlVXKi8uU30r3GFNe4f3gWyWBGfW0905Az7dHIhOqeAPMaNE84 bu7ILDBvMI106cECbibIarwWobUPb0Pka2vpt1ylCp/j7577a3bZT/rcvyMWFKFpuPgjAMcfGoC1K AtkamikA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svd5d-00000002ye3-3e7F; Tue, 01 Oct 2024 13:38:57 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svd3H-00000002yDy-4Bxn for linux-arm-kernel@bombadil.infradead.org; Tue, 01 Oct 2024 13:36:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=c0dYh7PbdCPP/8Ei/6LJSWz1xW+ihra4b2Z1AYIpZB4=; b=kf6dTMsHKGZuNpQRLXun6zNd9N FZ7u7fmsTImJ8xyyFpx7uqr1LMl6bgIr9BIchXTO+I39pd8Mv0/G3hNFua3HCzeW7oSgxMeZPUBhP RA1uJqTOY47yq/Fvi1vIbyrrBXzcmbU+CRAyfN/VFggsl55vv0vvC3AQMdQTHYo7AaGb5lVXeyTEl ffvlXbwf8XeEb5s3obLEMGrWEw7RM2OCxO/2X6ARZOdSj4usmIMsnx4Gyq6Mh0FgcRv/W1YVswDM8 gQYqchzWklefB69Xue/fj/ubfo2a3s55ft5XPDJMOeDPEWipDN7+6U255998iTfWgVg2ixlEdtfXh 7NDuJlYg==; Received: from foss.arm.com ([217.140.110.172]) by desiato.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svd3E-000000037Qz-1yMi for linux-arm-kernel@lists.infradead.org; Tue, 01 Oct 2024 13:36:30 +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 2F710367; Tue, 1 Oct 2024 06:36:56 -0700 (PDT) Received: from e124191.cambridge.arm.com (e124191.cambridge.arm.com [10.1.197.45]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7C8223F64C; Tue, 1 Oct 2024 06:36:25 -0700 (PDT) From: Joey Gouly To: linux-arm-kernel@lists.infradead.org Cc: catalin.marinas@arm.com, corbet@lwn.net, joey.gouly@arm.com, will@kernel.org, linux-doc@vger.kernel.org Subject: [PATCH v1 1/2] arm64: set POR_EL0 for kernel threads Date: Tue, 1 Oct 2024 14:36:17 +0100 Message-Id: <20241001133618.1547996-2-joey.gouly@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241001133618.1547996-1-joey.gouly@arm.com> References: <20241001133618.1547996-1-joey.gouly@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241001_143628_643443_4F67C28F X-CRM114-Status: UNSURE ( 8.95 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Restrict kernel threads to only have RWX overlays for pkey 0. This matches what arch/x86 does, by defaulting to a restrictive PKRU. Signed-off-by: Joey Gouly Cc: Will Deacon Cc: Catalin Marinas Reviewed-by: Kevin Brodsky --- arch/arm64/kernel/process.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index 0540653fbf38..94f42df249e3 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -410,6 +410,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) memset(childregs, 0, sizeof(struct pt_regs)); childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT; + if (system_supports_poe()) + p->thread.por_el0 = POR_EL0_INIT; + p->thread.cpu_context.x19 = (unsigned long)args->fn; p->thread.cpu_context.x20 = (unsigned long)args->fn_arg; } From patchwork Tue Oct 1 13:36:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Gouly X-Patchwork-Id: 13818210 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 E1D86CEACDD for ; Tue, 1 Oct 2024 14:58:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oahgXb9QBnPZ5f8IbBhC/aWJb7pJS2bNNtA439/HAQQ=; b=UhhguPorXIkNlPVz8o9v7ZRvaB 61Tj8Kdcu9ji4M5x3W+r4XDBP86kQsudAgU+egMFLO1LLErYtq14gzeyS3XN73Uhj0GD3nsOnOovQ 2wjTtHMZV5Hr6aIUuNTsAItTNyQ4yDRghJv0gQjWV5ufnJogioryOR3rfah7ABEhXIwNivDUT//Bz sIN9o5mfWSDB0DUnExNVq0C5nfoN7OofqLMs22QZ1eiA3i9Tn+rvizXmhvHoUXruYe/KqpWfs9AP1 q1UlIstnXEPlkfQfUpOIZTortF92ZDkaV8Wutq8li38q0y60fjrMz4w8um1Fo8wtOu39xBf50tIsC Ntr333nw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sveKg-00000003DhC-38dB; Tue, 01 Oct 2024 14:58:34 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svd3G-00000002yDA-3I3p for linux-arm-kernel@lists.infradead.org; Tue, 01 Oct 2024 13:36:43 +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 A418FDA7; Tue, 1 Oct 2024 06:36:57 -0700 (PDT) Received: from e124191.cambridge.arm.com (e124191.cambridge.arm.com [10.1.197.45]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F29273F64C; Tue, 1 Oct 2024 06:36:26 -0700 (PDT) From: Joey Gouly To: linux-arm-kernel@lists.infradead.org Cc: catalin.marinas@arm.com, corbet@lwn.net, joey.gouly@arm.com, will@kernel.org, linux-doc@vger.kernel.org Subject: [PATCH v1 2/2] Documentation/protection-keys: add AArch64 to documentation Date: Tue, 1 Oct 2024 14:36:18 +0100 Message-Id: <20241001133618.1547996-3-joey.gouly@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241001133618.1547996-1-joey.gouly@arm.com> References: <20241001133618.1547996-1-joey.gouly@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241001_063630_933953_79576B37 X-CRM114-Status: GOOD ( 15.55 ) 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 As POE support was recently added, update the documentation. Also note that kernel threads have a default protection key register value. Signed-off-by: Joey Gouly Cc: Will Deacon Cc: Catalin Marinas Cc: Jonathan Corbet --- Documentation/core-api/protection-keys.rst | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/Documentation/core-api/protection-keys.rst b/Documentation/core-api/protection-keys.rst index bf28ac0401f3..28ef6269041c 100644 --- a/Documentation/core-api/protection-keys.rst +++ b/Documentation/core-api/protection-keys.rst @@ -12,7 +12,11 @@ Pkeys Userspace (PKU) is a feature which can be found on: * Intel server CPUs, Skylake and later * Intel client CPUs, Tiger Lake (11th Gen Core) and later * Future AMD CPUs + * arm64 CPUs with Permission Overlay Extension (FEAT_S1POE), introduced + in Arm v8.8 +x86_64 +====== Pkeys work by dedicating 4 previously Reserved bits in each page table entry to a "protection key", giving 16 possible keys. @@ -28,6 +32,21 @@ register. The feature is only available in 64-bit mode, even though there is theoretically space in the PAE PTEs. These permissions are enforced on data access only and have no effect on instruction fetches. +arm64 +======== +Pkeys use 3 bits in each page table entry, to encod3 a "protection key index", +giving 8 possible keys. + +Protections for each key are defined with a per-CPU user-writable system +register (POR_EL0). This is a 64-bit register, encoding read, write and execute +overrides flags for each protection key index. + +Being a CPU register, POR_EL0 is inherently thread-local, potentially giving +each thread a different set of protections from every other thread. + +Unlike x86_64, the protection key permissions also apply to instruction +fetches. + Syscalls ======== @@ -38,11 +57,10 @@ There are 3 system calls which directly interact with pkeys:: int pkey_mprotect(unsigned long start, size_t len, unsigned long prot, int pkey); -Before a pkey can be used, it must first be allocated with -pkey_alloc(). An application calls the WRPKRU instruction -directly in order to change access permissions to memory covered -with a key. In this example WRPKRU is wrapped by a C function -called pkey_set(). +Before a pkey can be used, it must first be allocated with pkey_alloc(). An +application writes to the architecture specific CPU register directly in order +to change access permissions to memory covered with a key. In this example +this is wrapped by a C function called pkey_set(). :: int real_prot = PROT_READ|PROT_WRITE; @@ -64,9 +82,9 @@ is no longer in use:: munmap(ptr, PAGE_SIZE); pkey_free(pkey); -.. note:: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. - An example implementation can be found in - tools/testing/selftests/x86/protection_keys.c. +.. note:: pkey_set() is a wrapper around writing to the CPU register. + Example implementations can be found in + tools/testing/selftests/mm/pkey-{arm64,powerpc,x86}.h Behavior ======== @@ -96,3 +114,7 @@ with a read():: The kernel will send a SIGSEGV in both cases, but si_code will be set to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when the plain mprotect() permissions are violated. + +Note that kernel accesses from a kthread (such as io_uring), will use a default +value for the protection key register, so will not be consistent with +userspace's value of the register or mprotect.