From patchwork Fri Nov 24 16:34:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Gouly X-Patchwork-Id: 13467927 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13D67C61D97 for ; Fri, 24 Nov 2023 16:35:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBA9D6B02B6; Fri, 24 Nov 2023 11:35:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B19376B02BB; Fri, 24 Nov 2023 11:35:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96AE46B02BC; Fri, 24 Nov 2023 11:35:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 827CC6B02B6 for ; Fri, 24 Nov 2023 11:35:54 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 56CA51CBC57 for ; Fri, 24 Nov 2023 16:35:54 +0000 (UTC) X-FDA: 81493399428.16.7F1216A Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 9908D180011 for ; Fri, 24 Nov 2023 16:35:52 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.hostedemail.com: domain of joey.gouly@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=joey.gouly@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700843752; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ioi0H5dZqp3e3yVrlM97PcB/BFIQcKTOr94bnnRXFb0=; b=58mcG0NA5em4z3qsn0vmTt7lhhaPcfgqRNJVncc0SST6HdkkuRkrYL0D2JDcjePTMMlVTU 3vvwKwg7iOuTu6Xam48QrULIy0bYLhoWdcOn6C6Q2CcR6gRi7psPKScoJxUN+5rXPhCt80 RLplprxb26As1aPyAUstitWTvslxdyI= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.hostedemail.com: domain of joey.gouly@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=joey.gouly@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700843752; a=rsa-sha256; cv=none; b=t27bWqBZLW6PTAji3702hOy3XIt3xGyXAhRVu3D9a/hIjNEE4DThBH/9W5l8R5VhL+XjQY Mssk7fYCO1Xjxcnu1tl592vKFM44QryGEXITG9XMsh9/7+iJ09BpBfOsG69N0KYOytvbZW a/tODNlH9oMmTgimXTt5xT2sGx9iKIU= 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 6891B1BD0; Fri, 24 Nov 2023 08:36:38 -0800 (PST) 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 B89F93F73F; Fri, 24 Nov 2023 08:35:49 -0800 (PST) From: Joey Gouly To: linux-arm-kernel@lists.infradead.org Cc: akpm@linux-foundation.org, aneesh.kumar@linux.ibm.com, broonie@kernel.org, catalin.marinas@arm.com, dave.hansen@linux.intel.com, joey.gouly@arm.com, maz@kernel.org, oliver.upton@linux.dev, shuah@kernel.org, will@kernel.org, kvmarm@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, James Morse , Suzuki K Poulose , Zenghui Yu Subject: [PATCH v3 12/25] arm64: handle PKEY/POE faults Date: Fri, 24 Nov 2023 16:34:57 +0000 Message-Id: <20231124163510.1835740-13-joey.gouly@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231124163510.1835740-1-joey.gouly@arm.com> References: <20231124163510.1835740-1-joey.gouly@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9908D180011 X-Stat-Signature: 5ezsh3h4sk9nwn8fdf374re8cop7xajs X-HE-Tag: 1700843752-713673 X-HE-Meta: U2FsdGVkX18C+R5iKva4B4mSV1IOw2/kqqE2dJTul4Sb2ut3y8dozafxTtZqS9+PYxxLw+BgB9T8NA+hjazae90S1rinlByjA3VWyk7/OB+W8Zbz29GRetal0UjAqMsNaK5jNTiYe9JnzdW05IX5FT5tMEYOLeAJszTAWuZVwxr8vxpvRWcIS1z8pwhji+6w6VrRwVefU9LbfC2ObftDPw5d1mbUFn5+aqNsGVL2ZPYKgBkPe9LuJ/rKf1sHwZAEo9l81PRl1IfXiJTgQ8wiTJ4opi2nuj7y80J57HpZj/tweg/IKVQ/Gucdaf+cfgH+UVXWbqmLA2PBuS5hSESyg4Lfkhbo417Iu+3K65TF9mmzSuHLJWMt1oe9pFHT0N5qjtgUuW7kyn6pfdtQzDttKqb5hR3U+MiSzb7Qe8nzfRsdbc2iusnstEBjRMD1C1iJw+eQZWFGmINqE4vOe84HrMZAIIC0Q+A8gThv5TUX4XOr9k0RUxbwK0usKwdMH6uwT/d92WifxCbwrN6vtEr5dZ0Q0AzKO+UiR2Vpk1aJO0GSsTs+czOMP4fIS7JlFfM2GOYTsNVW9xRZZqGgT6oSlHTIXaWJ6wXbWP1F0hx9+ctGNpHPcgvPcNFeYe1q1RcrRDFzEsbfvuGm27qztGca1yWKvHJbFgvxOzk5D0ob8m/Sgqt8ryHR3C9u1+dHfQIJPQWkqEnTpJ8g/sIvKKA04LnENQF9I9SsC9iISJWd1W/Nl8BN9CToMIKRE+bM8eajRmSezgTogYJF8DgwvK6qT+xTIlNPwcrcFHjdNzi5bPbr/xSufQhKaTJ23R28kzEqzHhxkr4DJK7cXnxPQ8BORdpgjBqcqMjm2SPs7KIyNCEqnvNvv9DR6jtD4vESB+wy8jwQepXwdOGheSrlMgbBFt2R5sSqkKZlbjVv9GpPee/xb+szeSznl3M7wKw+D2zMzo94JL5TUz6yQn0PyOw K1XhIgl4 Cl9gYAeZ/sJzPHbr2vlTOVt+HpEnhLlHXpE3jpN9Xfjci4NTkI5Mu73a8PDwm6DMIykIJ5nOq0XPUmGkB9WpMlUxKboifOXHBjbGRRZmx2f1CsY91U3yjbc9cwD7wcjgV6TGGcHUMUEW8FlrCe7hy3FxUA3TrA7kGO1wL8X9X9NK2RjEa6zgY/3O7PvL5KiM5sr+m3yEydglgl/AlXi0SRH/uDp41XpgKAM9F X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: If a memory fault occurs that is due to an overlay/pkey fault, report that to userspace with a SEGV_PKUERR. Signed-off-by: Joey Gouly Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/include/asm/traps.h | 1 + arch/arm64/kernel/traps.c | 12 ++++++++-- arch/arm64/mm/fault.c | 44 +++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/traps.h b/arch/arm64/include/asm/traps.h index eefe766d6161..f6f6f2cb7f10 100644 --- a/arch/arm64/include/asm/traps.h +++ b/arch/arm64/include/asm/traps.h @@ -25,6 +25,7 @@ try_emulate_armv8_deprecated(struct pt_regs *regs, u32 insn) void force_signal_inject(int signal, int code, unsigned long address, unsigned long err); void arm64_notify_segfault(unsigned long addr); void arm64_force_sig_fault(int signo, int code, unsigned long far, const char *str); +void arm64_force_sig_fault_pkey(int signo, int code, unsigned long far, const char *str, int pkey); void arm64_force_sig_mceerr(int code, unsigned long far, short lsb, const char *str); void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far, const char *str); diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index 215e6d7f2df8..1bac6c84d3f5 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -263,16 +263,24 @@ static void arm64_show_signal(int signo, const char *str) __show_regs(regs); } -void arm64_force_sig_fault(int signo, int code, unsigned long far, - const char *str) +void arm64_force_sig_fault_pkey(int signo, int code, unsigned long far, + const char *str, int pkey) { arm64_show_signal(signo, str); if (signo == SIGKILL) force_sig(SIGKILL); + else if (code == SEGV_PKUERR) + force_sig_pkuerr((void __user *)far, pkey); else force_sig_fault(signo, code, (void __user *)far); } +void arm64_force_sig_fault(int signo, int code, unsigned long far, + const char *str) +{ + arm64_force_sig_fault_pkey(signo, code, far, str, 0); +} + void arm64_force_sig_mceerr(int code, unsigned long far, short lsb, const char *str) { diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 460d799e1296..efd263f56da7 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -497,6 +498,23 @@ static void do_bad_area(unsigned long far, unsigned long esr, #define VM_FAULT_BADMAP ((__force vm_fault_t)0x010000) #define VM_FAULT_BADACCESS ((__force vm_fault_t)0x020000) +static bool fault_from_pkey(unsigned long esr, struct vm_area_struct *vma, + unsigned int mm_flags) +{ + unsigned long iss2 = ESR_ELx_ISS2(esr); + + if (!arch_pkeys_enabled()) + return false; + + if (iss2 & ESR_ELx_Overlay) + return true; + + return !arch_vma_access_permitted(vma, + mm_flags & FAULT_FLAG_WRITE, + mm_flags & FAULT_FLAG_INSTRUCTION, + mm_flags & FAULT_FLAG_REMOTE); +} + static vm_fault_t __do_page_fault(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned int mm_flags, unsigned long vm_flags, @@ -688,9 +706,29 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, * Something tried to access memory that isn't in our memory * map. */ - arm64_force_sig_fault(SIGSEGV, - fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR, - far, inf->name); + int fault_kind; + /* + * The pkey value that we return to userspace can be different + * from the pkey that caused the fault. + * + * 1. T1 : mprotect_key(foo, PAGE_SIZE, pkey=4); + * 2. T1 : set AMR to deny access to pkey=4, touches, page + * 3. T1 : faults... + * 4. T2: mprotect_key(foo, PAGE_SIZE, pkey=5); + * 5. T1 : enters fault handler, takes mmap_lock, etc... + * 6. T1 : reaches here, sees vma_pkey(vma)=5, when we really + * faulted on a pte with its pkey=4. + */ + int pkey = vma_pkey(vma); + + if (fault_from_pkey(esr, vma, mm_flags)) + fault_kind = SEGV_PKUERR; + else + fault_kind = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR; + + arm64_force_sig_fault_pkey(SIGSEGV, + fault_kind, + far, inf->name, pkey); } return 0;