From patchwork Wed Nov 24 19:20:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 12693575 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 540BEC433F5 for ; Wed, 24 Nov 2021 19:21: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=hfIPgDpvSLeL5n6chTu5JjzPzkmHRodBX7DxDegOhCI=; b=osqcSTpNhAh2Oz c18AC/30u1h42Pl1QOVdPHc0jBpm6t78Sy/EWCrrlug37OtE/9tbnFcd2oAA+bpLf2Kfb5XAffp4O H60E3QSuXF8X2wipQfTxcnhmp3RzXVgQjxoL8H5uY5qSMPjjWY2KPm6FUeQ0GJB75jZY9ta8+Pppp 1I5/LohzWfRIZJUbl0masWWgC8eN+O3UPJDUeFgX7T6erS0W/ypzM4kt2edsbHQnQ96/ivCvNjcbO vlE1omQJCUMMJf3zTHDVXXM6Qvzxa0q8rXj1/v7VfLMPTkB/pFU5iBqA0QJIl6Jc4KvE8bksAuOzu Kg8ssy+wSEHqcRpOyvvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mpxoy-005dLp-TB; Wed, 24 Nov 2021 19:20:45 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mpxol-005dHV-O8 for linux-arm-kernel@lists.infradead.org; Wed, 24 Nov 2021 19:20:33 +0000 Received: by mail.kernel.org (Postfix) with ESMTPSA id 62DD960F45; Wed, 24 Nov 2021 19:20:29 +0000 (UTC) From: Catalin Marinas To: Linus Torvalds , Josef Bacik , David Sterba Cc: Andreas Gruenbacher , Al Viro , Andrew Morton , Will Deacon , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-btrfs@vger.kernel.org Subject: [PATCH 1/3] mm: Introduce fault_in_exact_writeable() to probe for sub-page faults Date: Wed, 24 Nov 2021 19:20:22 +0000 Message-Id: <20211124192024.2408218-2-catalin.marinas@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211124192024.2408218-1-catalin.marinas@arm.com> References: <20211124192024.2408218-1-catalin.marinas@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211124_112031_850215_93D70C2C X-CRM114-Status: GOOD ( 17.85 ) 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 On hardware with features like arm64 MTE or SPARC ADI, an access fault can be triggered at sub-page granularity. Depending on how the fault_in_*() functions are used, the caller can get into a live-lock by continuously retrying the fault-in on an address different from the one where the uaccess failed. In the majority of cases progress is ensured by the following conditions: 1. copy_{to,from}_user() guarantees at least one byte access if the user address is not faulting; 2. The fault_in_*() is attempted on the next address that could not be accessed by copy_*_user(). In the places where the above conditions are not met or the fault-in/uaccess loop does not have a mechanism to bail out, the fault_in_exact_writeable() ensures that the arch code will probe the range in question at a sub-page fault granularity (e.g. 16 bytes for arm64 MTE). For large ranges, this is significantly more expensive than the non-exact versions which probe a single byte in each page or use GUP. The architecture code has to select ARCH_HAS_SUBPAGE_FAULTS and implement probe_user_writeable(). Signed-off-by: Catalin Marinas --- arch/Kconfig | 7 +++++++ include/linux/pagemap.h | 1 + include/linux/uaccess.h | 21 +++++++++++++++++++++ mm/gup.c | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index 26b8ed11639d..02502b3362aa 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -27,6 +27,13 @@ config HAVE_IMA_KEXEC config SET_FS bool +config ARCH_HAS_SUBPAGE_FAULTS + bool + help + Select if the architecture can check permissions at sub-page + granularity (e.g. arm64 MTE). The probe_user_*() functions + must be implemented. + config HOTPLUG_SMT bool diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 1a0c646eb6ff..4bae32d6b2e3 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -910,6 +910,7 @@ void folio_add_wait_queue(struct folio *folio, wait_queue_entry_t *waiter); * Fault in userspace address range. */ size_t fault_in_writeable(char __user *uaddr, size_t size); +size_t fault_in_exact_writeable(char __user *uaddr, size_t size); size_t fault_in_safe_writeable(const char __user *uaddr, size_t size); size_t fault_in_readable(const char __user *uaddr, size_t size); diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index ac0394087f7d..08169fb38905 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -271,6 +271,27 @@ static inline bool pagefault_disabled(void) */ #define faulthandler_disabled() (pagefault_disabled() || in_atomic()) +#ifndef CONFIG_ARCH_HAS_SUBPAGE_FAULTS +/** + * probe_user_writable: probe for sub-page faults in the user range + * @uaddr: start of address range + * @size: size of address range + * + * Returns the number of bytes not accessible (like copy_to_user() and + * copy_from_user()). + * + * Architectures that can generate sub-page faults (e.g. arm64 MTE) should + * implement this function. It is expected that the caller checked for the + * write permission of each page in the range either by put_user() or GUP. + * The architecture port can implement a more efficient get_user() probing of + * the range if sub-page faults are triggered by either a load or store. + */ +static inline size_t probe_user_writable(void __user *uaddr, size_t size) +{ + return 0; +} +#endif + #ifndef ARCH_HAS_NOCACHE_UACCESS static inline __must_check unsigned long diff --git a/mm/gup.c b/mm/gup.c index 2c51e9748a6a..1c360d9fdc8e 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1693,6 +1693,25 @@ size_t fault_in_writeable(char __user *uaddr, size_t size) } EXPORT_SYMBOL(fault_in_writeable); +/** + * fault_in_exact_writeable - fault in userspace address range for writing, + * potentially checking for sub-page faults + * @uaddr: start of address range + * @size: size of address range + * + * Returns the number of bytes not faulted in (like copy_to_user() and + * copy_from_user()). + */ +size_t fault_in_exact_writeable(char __user *uaddr, size_t size) +{ + size_t accessible = size - fault_in_writeable(uaddr, size); + + if (accessible) + accessible -= probe_user_writable(uaddr, accessible); + return size - accessible; +} +EXPORT_SYMBOL(fault_in_exact_writeable); + /* * fault_in_safe_writeable - fault in an address range for writing * @uaddr: start of address range