From mboxrd@z Thu Jan 1 00:00:00 1970 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 EBD1EC4332F for ; Wed, 13 Dec 2023 15:03:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76FA56B0111; Wed, 13 Dec 2023 10:03:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F6246B014B; Wed, 13 Dec 2023 10:03:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D20D6B0111; Wed, 13 Dec 2023 10:03:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 37E8C6B0139 for ; Wed, 13 Dec 2023 10:03:35 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id ADBAEC0714 for ; Wed, 13 Dec 2023 15:03:34 +0000 (UTC) X-FDA: 81562113948.02.D7D0BF5 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf17.hostedemail.com (Postfix) with ESMTP id 71D7E40028 for ; Wed, 13 Dec 2023 15:03:08 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf17.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=1702479788; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kFdd38BHCxZTZtXsCIHBF/VsyxdUnnWWUzA8CXKR/Qw=; b=dgPur/a85ojoPPh0/H4qmII7IyG9ePw6BEHAFMZLr1BrVUs/LrhKsicL4A5XsG5lyH3Waz Am/Y4lK0rRLmhWwYyYiuV/PXRM6vv5WnkrP0rPb/VvRbtdvPUvfgzwEmYFr/PCCuh9aQtn 5JuafcfXeGvEDuF8gL49clP87VWs1+E= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf17.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=1702479788; a=rsa-sha256; cv=none; b=gZkxC8pIrpWfnAsrpWbA7D3yPAB9edmAITSWVn0BGB0TmdkHkW+KLemjHTWAMR/wBakTGU gYt3/OnWkZMuli+aO4C5KSFbGn69HC40bVNFfg4R4oLIVgMAV7WVzOnQ6EUMgX+ZTFt2z6 OOrk3jYkjzLhkUrO8Vglgu1VDAxzjjY= 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 211E2C15; Wed, 13 Dec 2023 07:03:53 -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 06D6E3F738; Wed, 13 Dec 2023 07:03:04 -0800 (PST) Date: Wed, 13 Dec 2023 15:02:59 +0000 From: Joey Gouly To: Catalin Marinas Cc: linux-arm-kernel@lists.infradead.org, akpm@linux-foundation.org, aneesh.kumar@linux.ibm.com, broonie@kernel.org, dave.hansen@linux.intel.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: Re: [PATCH v3 12/25] arm64: handle PKEY/POE faults Message-ID: <20231213150259.GA1129554@e124191.cambridge.arm.com> References: <20231124163510.1835740-1-joey.gouly@arm.com> <20231124163510.1835740-13-joey.gouly@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 71D7E40028 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: xradw5z7s5bhba7b6rdapm3p7qhip89w X-HE-Tag: 1702479788-624517 X-HE-Meta: U2FsdGVkX1/yB0vbL+hyumdDxMT8qO0UdaFzMAs7m0W8RoHiZuY+39v4T6/cc31LAyS3lJxgEhzRYLQT4AIgcCPwICS3IGTYLyeNoAUEKDMSXfOgw0JE1PZyAqDBA9heB5PsUlMM3gh5WNMWzT6wsENs5mkDgnL5NqR4mspEn6SRLulWfgLbfVYjlBowxT/XWLXH0pmYAh1UQtfGVcAV0nwP4Sl0ZIX97nPDqTvME+xoDgwzuYNYhV2dxb6T7/dkFM83mfQaZvykyQie3UlLL9nSeqP4md3mvl/YvjC7VMw1zSjw1gGBggqYcMUtkC+QacfSF+fra1E9jMwy/v6Oj0z5Uv8B0B1oZtcOTNeUMeOFcoq4zrdGgXD3IFt96x8nKMccPuFCEXacPqo4jxH3M4PU+80aSIHkuUBEwd64l0IvjYaEWV7btiG/vt5LKwvYp1nedKf5l50fPgmtIRYByCofW5G7774qbcISqSgNiYY0EnfkLddlZLr0SrE7xEWG9JQkZ07JzI5njKXK4vTEBMGcL7OoGDsbYerSwFbpC/87xOXqrCidCkJeIUpP/x46J99a0cQKe9OqU6rouvor7uy673NAvO/7uDqloF8zdWPLjuap7e3UzKljQR3IGgw4+4p9tDRl5eKN9qwdjK/IjeR+24+a0tKOL9rMXdX6rISwdKfo02THywNHPMsn+ey1Roh4IsdKMt2OG0AJEJIPutPTvUi7inuWzh97gRwcVG3pcUyeyvZJu4SaZLvruKnBp4ZxY2uHRW7JU0DZhZ7SvMAtQfiawDzxo/d8pXdFr7qxJnC56o92O02cxNOWH0jOrp2ovFGNKzN3COKYiMvB3JMKrDkwSChSS7Ussbs1/k7WdOi3d0dF7phsfUUs0y7q1EB2IZ71066B7M7W99HmZCC6Qg/HAXFXE7CgnfI0pRbFQjA47vsrxRgVhNZNN7btPtQE1n55YQz6I9H0QLe 4HRHE6Oh MXpfUyTXSrShL8FPDvMkNnfNg7D5EVB6/v6oU2dUoL5ebBsg0dOUvDybF5D404qYT+75oW2U3ZMQoFztz3bNLnDlGAljw0XKfGdsgvNWi5bobMwMzaQsIiCCYmw== 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: Hi, On Mon, Dec 11, 2023 at 06:18:17PM +0000, Catalin Marinas wrote: > On Fri, Nov 24, 2023 at 04:34:57PM +0000, Joey Gouly wrote: > > @@ -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); > > +} > > Do we actually need this additional arch_vma_access_permitted() check? > The ESR should tell us if it was a POE fault. Permission overlay faults > have priority over the base permission faults, so we'd not need to fall > back to this additional checks. Well, see below, we could do something > slightly smarter here. We want this here as it follows other arch's which will fail with a pkey fault even if the page isn't actually mapped. If the paged isn't mapped we'd get a translation fault, but since we know the type of access and have the pkey in the VMA we check it here. > > I can see x86 and powerpc have similar checks (though at a different > point under the mmap lock) but I'm not familiar with their exception > model, exception priorities. > > > + > > 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); > > Other than the vma_pkey() race, I'm more worried about the vma > completely disappearing, e.g. munmap() in another thread. We end up > dereferencing a free pointer here. We need to do this under the mmap > lock. > > Since we need to do this check under the mmap lock, we could add an > additional check to see if the pkey fault we got was a racy and just > restart the instruction if it no longer faults according to > por_el0_allows_pkey(). However, the code below is too late in the fault > handling to be able to do much other than SIGSEGV. After discussing in person, I agree with the assesment that this is the wrong place to do the check, and after looking at the x86 arch code, I see how they're doing it earlier. Thanks, Joey