Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1303912pxb; Wed, 6 Apr 2022 14:21:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrMkwEPSCffajy7aEI4LSxW4b5BIYbzcSr5m7jOSfNq7SHpwTpZpEnWnsq1RcKgwLJOuF8 X-Received: by 2002:a17:902:9b95:b0:151:533b:9197 with SMTP id y21-20020a1709029b9500b00151533b9197mr10766872plp.66.1649280114099; Wed, 06 Apr 2022 14:21:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649280114; cv=none; d=google.com; s=arc-20160816; b=Xgy2bQt+gwh+8FlS7VHDRI0Y7QHt1nO6iLwee3SQ8MAh+UcrB3lYoNpmmvt2tD1qy4 ho8Q/4zgoWRdOrYEJr4IAMsVt6/MplbMlxYHewRUkBywQBrBrqnwqB5IB2QM4ddoWe1X vdS/jKcq+5B3jKehX7rKco4AYHkuhq5vfMR+MfXlN/IHjal1dUteXzE82O1WK+yh9cE2 es0X5I+3zTPzBM0AyKv7498m9VqdRYsIUDO86PZT5HeNbY2f6ag7N4ZTIrP5hQC8Ii6e jLp2puYcJ37ahrDSIppjUooSH6D4IuEwRed1Ut7nHToeO+VYTiEBVHAhQM4Go3IeX/pb aP/A== 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=L+y/psu2UG1xovGrsIwAcxdC/+ASNtBYjQ9xzRUQ+vvkm35tKDDvpDRlzRssnWApWY wMbsme5pIxvGI3u+v6wzFyat4nyzhkJaVeBDp/MXO0cyZAacTKZl4s+JNs/qKZ3uQbnG YCAq+oG//eFqz5JwrupOnWMWYVZvDqApZVO8po25a3S3rQE6g2EFhqKPlG95SKhe1qZU /jjgik/651fNDO0ifQ+Xqj5sVe/C0Oh+thW+c/Htg588uX8Y2DBcKJbSxWugFIGq6MU1 oh0efw6TQewxkb5LhYC1s9uOdG2iL27GyP/wqxxHHL7sa970H5RutVveCQWQ7M42ki85 Zqfw== 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 p10-20020a17090ab90a00b001c7021d5cf2si5086713pjr.127.2022.04.06.14.21.39; Wed, 06 Apr 2022 14:21:54 -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 S234277AbiDFUPK (ORCPT + 99 others); Wed, 6 Apr 2022 16:15:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236178AbiDFUNg (ORCPT ); Wed, 6 Apr 2022 16:13:36 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A33A264C03; Wed, 6 Apr 2022 11:09:35 -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 ams.source.kernel.org (Postfix) with ESMTPS id 22E8DB8253C; Wed, 6 Apr 2022 18:09:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7968CC385A1; Wed, 6 Apr 2022 18:09:30 +0000 (UTC) From: Catalin Marinas To: Linus Torvalds , Andreas Gruenbacher , Josef Bacik Cc: Al Viro , Andrew Morton , 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 v3 2/3] arm64: Add support for user sub-page fault probing Date: Wed, 6 Apr 2022 19:09:21 +0100 Message-Id: <20220406180922.1522433-3-catalin.marinas@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220406180922.1522433-1-catalin.marinas@arm.com> References: <20220406180922.1522433-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, T_SCC_BODY_TEXT_LINE 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; +}