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 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 13558C433F5 for ; Wed, 1 Dec 2021 20:30: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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DbwLDd+gWqMVs04a67p4FpxHWSF26LbfS1bkOK2wj7A=; b=EK0ZA6vgmuNZc5 gHMBp15CcUwFYi9zVnTq4zSngy/+46uzliGhxau1OPX3QBnRRvo64Z1XYDXHEUc638pvG6P1ko0lI 5bqTzr1XTBiIKFd/mOacQjr5+aUV3qn6pjJSUtR47eWNRGDpOy/nSdpl4bvbkVCNf40YbaujSx5pI djv0o9jqOdnUjbC6YxIGMP6UWV6qkBHsj3RahTqa8CDbIfbc9m6HOyGQt2FXca/JoNlHP9ioMBkLM TZjS8xFsjC2+EK2R+iYHLgdqBdsBfqR0nIMqQL2DfXXmeIkJTsy7honAZm1AvV/zrbEqJ7ZswbFOt DEzZFViOZF+uF2pKCCHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msWEC-00A3Pt-PJ; Wed, 01 Dec 2021 20:29:20 +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 1msWE8-00A3Ou-5r for linux-arm-kernel@lists.infradead.org; Wed, 01 Dec 2021 20:29:17 +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 BCA9013D5; Wed, 1 Dec 2021 12:29:14 -0800 (PST) Received: from FVFF77S0Q05N (unknown [10.57.65.205]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AFC133F5A1; Wed, 1 Dec 2021 12:29:12 -0800 (PST) Date: Wed, 1 Dec 2021 20:29:06 +0000 From: Mark Rutland To: Catalin Marinas Cc: Linus Torvalds , Andreas Gruenbacher , Josef Bacik , David Sterba , Al Viro , Andrew Morton , Will Deacon , Matthew Wilcox , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-btrfs@vger.kernel.org Subject: Re: [PATCH v2 3/4] arm64: Add support for user sub-page fault probing Message-ID: References: <20211201193750.2097885-1-catalin.marinas@arm.com> <20211201193750.2097885-4-catalin.marinas@arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20211201193750.2097885-4-catalin.marinas@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211201_122916_355525_71116D45 X-CRM114-Status: GOOD ( 28.51 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Catalin, On Wed, Dec 01, 2021 at 07:37:49PM +0000, Catalin Marinas wrote: > With MTE, even if the pte allows an access, a mismatched tag somewhere > within a page can still cause a fault. Select ARCH_HAS_SUBPAGE_FAULTS if > MTE is enabled and implement the probe_subpage_*() functions. Note that > get_user() is sufficient for the writeable checks since the same tag > mismatch fault would be triggered by a read. > > Signed-off-by: Catalin Marinas > --- > arch/arm64/Kconfig | 1 + > arch/arm64/include/asm/uaccess.h | 59 ++++++++++++++++++++++++++++++++ > 2 files changed, 60 insertions(+) > > diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig > index c4207cf9bb17..dff89fd0d817 100644 > --- a/arch/arm64/Kconfig > +++ b/arch/arm64/Kconfig > @@ -1777,6 +1777,7 @@ config ARM64_MTE > depends on AS_HAS_LSE_ATOMICS > # Required for tag checking in the uaccess routines > depends on ARM64_PAN > + select ARCH_HAS_SUBPAGE_FAULTS > select ARCH_USES_HIGH_VMA_FLAGS > help > Memory Tagging (part of the ARMv8.5 Extensions) provides > diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h > index 6e2e0b7031ab..bcbd24b97917 100644 > --- a/arch/arm64/include/asm/uaccess.h > +++ b/arch/arm64/include/asm/uaccess.h > @@ -445,4 +445,63 @@ static inline int __copy_from_user_flushcache(void *dst, const void __user *src, > } > #endif > > +#ifdef CONFIG_ARCH_HAS_SUBPAGE_FAULTS > + > +/* > + * Return 0 on success, the number of bytes not accessed otherwise. > + */ > +static inline size_t __mte_probe_user_range(const char __user *uaddr, > + size_t size, bool skip_first) > +{ > + const char __user *end = uaddr + size; > + int err = 0; > + char val; > + > + uaddr = PTR_ALIGN_DOWN(uaddr, MTE_GRANULE_SIZE); > + if (skip_first) > + uaddr += MTE_GRANULE_SIZE; Do we need the skipping for a functional reason, or is that an optimization? >From the comments in probe_subpage_writeable() and probe_subpage_safe_writeable() I wasn't sure if the skipping was because we *don't need to* check the first granule, or because we *must not* check the first granule. > + while (uaddr < end) { > + /* > + * A read is sufficient for MTE, the caller should have probed > + * for the pte write permission if required. > + */ > + __raw_get_user(val, uaddr, err); > + if (err) > + return end - uaddr; > + uaddr += MTE_GRANULE_SIZE; > + } I think we may need to account for the residue from PTR_ALIGN_DOWN(), or we can report more bytes not copied than was passed in `size` in the first place, which I think might confused some callers. Consider MTE_GRANULE_SIZE is 16, uaddr is 31, and size is 1 (so end is 32). We align uaddr down to 16, and if we fail the first access we return (32 - 16), i.e. 16. Thanks, Mark. > + (void)val; > + > + return 0; > +} > + > +static inline size_t probe_subpage_writeable(const void __user *uaddr, > + size_t size) > +{ > + if (!system_supports_mte()) > + return 0; > + /* first put_user() done in the caller */ > + return __mte_probe_user_range(uaddr, size, true); > +} > + > +static inline size_t probe_subpage_safe_writeable(const void __user *uaddr, > + size_t size) > +{ > + if (!system_supports_mte()) > + return 0; > + /* the caller used GUP, don't skip the first granule */ > + return __mte_probe_user_range(uaddr, size, false); > +} > + > +static inline size_t probe_subpage_readable(const void __user *uaddr, > + size_t size) > +{ > + if (!system_supports_mte()) > + return 0; > + /* first get_user() done in the caller */ > + return __mte_probe_user_range(uaddr, size, true); > +} > + > +#endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */ > + > #endif /* __ASM_UACCESS_H */ _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel