Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1942205pxb; Sat, 23 Apr 2022 23:24:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvbWEq2Lf0zUdxRPUoWdOjyGD8h0qU1mmUB8NsXuxX6pod8T4r3Uck/s64I/BXVu99HtOZ X-Received: by 2002:a17:902:b489:b0:158:f23a:c78f with SMTP id y9-20020a170902b48900b00158f23ac78fmr12559604plr.65.1650781441617; Sat, 23 Apr 2022 23:24:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650781441; cv=none; d=google.com; s=arc-20160816; b=0ZMtJrPaLc46JP8HJEzgPnbSuKG9bu2IOh1Hg4y7Gw3+KkNaoeH+Q8YzciQGBRD4TI xHtU23oiyPpaU3mTPaukdi1s8Y7YSF/stnqm/m/BzRrBBrqr/oAKWALF1FdPTxMUx/zG qAkTrOuaLMKZrKumhcmeFQAUTXSVVImGjhQHlMd/GybQdrX+ZvQjCsXfvQy/c4NcGXTK oD2b/3H+q3qkYoMNFqkIVaZJxpg4c7kBhjkvsiSrLDk8iLRKKtlQfyxHr2zThBh8Yo+p gLBEc1u8tfTZN1oZJSBacTCY2fOHko0FOrttshwUCp/mX7rGPNxfHI8nUEUz+307kBb6 eQcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=24RA4oHGL4DYEZlf/y/VtEaR2CLPz0iqGYRoZgYWePI=; b=TvI/JFq/uwTx3Jqi7DElIFk6X9CUcxrxiGpRDTXDx+Xi0nkWp/7pJ9xBhaEAxeENb9 /kli3xOcugDLSiF/Kf0bTMburOOXf9oXZwEB9ExgRd7mEPXQdh6i15WyunJoR3RghUpg nLDmMUyHfxQluZqkghEJr+tPVGDtBw4vboz1Alm6GQivYXfHYfcgKoa3FaxlMST2BzmH iwnIi+a2pKPnOnnEfZIrI5Iu30gDX64sUp8dxYXVstbCP3Gox9V52bMHd78Xbvc24TP2 sg+2XbFz6ZnlJzHJID7ekISGjBCy56RfNNXeasB0zaY4KnSyGErgycPyWl5qtTIV/QIQ fRLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l1-20020a170902eb0100b00153b974f3aesi1912190plb.427.2022.04.23.23.23.46; Sat, 23 Apr 2022 23:24:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234855AbiDWKLM (ORCPT + 99 others); Sat, 23 Apr 2022 06:11:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234831AbiDWKLB (ORCPT ); Sat, 23 Apr 2022 06:11:01 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF8EE1B2B0F; Sat, 23 Apr 2022 03:08:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 46513CE1412; Sat, 23 Apr 2022 10:08:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3090BC385AC; Sat, 23 Apr 2022 10:07:59 +0000 (UTC) From: Catalin Marinas To: Andrew Morton Cc: Linus Torvalds , Andreas Gruenbacher , Josef Bacik , Al Viro , Chris Mason , David Sterba , Will Deacon , linux-fsdevel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/3] arm64: Add support for user sub-page fault probing Date: Sat, 23 Apr 2022 11:07:50 +0100 Message-Id: <20220423100751.1870771-3-catalin.marinas@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220423100751.1870771-1-catalin.marinas@arm.com> References: <20220423100751.1870771-1-catalin.marinas@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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_writeable() function. Note that get_user() is sufficient for the writeable MTE check since the same tag mismatch fault would be triggered by a read. The caller of probe_subpage_writeable() will need to check the pte permissions (put_user, GUP). Signed-off-by: Catalin Marinas Cc: Will Deacon --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/mte.h | 1 + arch/arm64/include/asm/uaccess.h | 15 +++++++++++++++ arch/arm64/kernel/mte.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 57c4c995965f..290b88238103 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -1871,6 +1871,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/mte.h b/arch/arm64/include/asm/mte.h index adcb937342f1..aa523591a44e 100644 --- a/arch/arm64/include/asm/mte.h +++ b/arch/arm64/include/asm/mte.h @@ -47,6 +47,7 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg); long get_mte_ctrl(struct task_struct *task); int mte_ptrace_copy_tags(struct task_struct *child, long request, unsigned long addr, unsigned long data); +size_t mte_probe_user_range(const char __user *uaddr, size_t size); #else /* CONFIG_ARM64_MTE */ diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index e8dce0cc5eaa..6677aa7e9993 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -460,4 +460,19 @@ 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 probed otherwise. + */ +static inline size_t probe_subpage_writeable(const void __user *uaddr, + size_t size) +{ + if (!system_supports_mte()) + return 0; + return mte_probe_user_range(uaddr, size); +} + +#endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */ + #endif /* __ASM_UACCESS_H */ diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 78b3e0f8e997..35697a09926f 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -543,3 +544,32 @@ static int register_mte_tcf_preferred_sysctl(void) return 0; } subsys_initcall(register_mte_tcf_preferred_sysctl); + +/* + * Return 0 on success, the number of bytes not probed otherwise. + */ +size_t mte_probe_user_range(const char __user *uaddr, size_t size) +{ + const char __user *end = uaddr + size; + int err = 0; + char val; + + __raw_get_user(val, uaddr, err); + if (err) + return size; + + uaddr = PTR_ALIGN(uaddr, MTE_GRANULE_SIZE); + 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; + } + (void)val; + + return 0; +}