From patchwork Thu Sep 12 20:41:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13802642 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 3E074EEE261 for ; Thu, 12 Sep 2024 20:43:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding: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:In-Reply-To:References:List-Owner; bh=dneYPSdL4kP0HiWqY714LZp4M6R402gIr+ydgNGo/0M=; b=jj81BIiaocG8rx2aMKCJ7jtC8x Lmq4hGBXSz2sG46RQLTYt2//GMtt5V/7PO7iI9XQtnBUnhxEv0I7mkwN7r1bC8Bq8Pw/29XOwPTXA y2uqg3qh1yTFVhJHhI9TzknWe0WynHMFv5Ja70cjIaZwvoM60tpPYI/ZU46o+Ahx1H0UW9Sg14Bu2 NPplfq8CSqVRQLaKjUiFBODrGSsrk/GPl3lv/1QDpMyRfCIJXAeroumWJNfi/QjBHwnVbLET4ghxs 8jcGWkpY1fgd1PDH7qM+uj3Z6mwg1rGRm3328y7fsJw99M9TwVFzOSe2zvJfzb3PAu/otIIzD3f0n iwxy6/Ow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1soqeZ-0000000ECEy-02hy; Thu, 12 Sep 2024 20:42:59 +0000 Received: from mail-southcentralusazlp170120000.outbound.protection.outlook.com ([2a01:111:f403:c10d::] helo=SN4PR2101CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1soqdS-0000000EC7Z-43dM for linux-arm-kernel@lists.infradead.org; Thu, 12 Sep 2024 20:41:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=XzcCqdibmi8LYSlZkl+vh0SUUs2OX5ifwqwjQU7oyK2xp8F3ZeQxtiGsHcwUhnW5fR34IIRdryjKq7a8ik47XiLy4R/wZoauFZvjJNqnBO9722ZQJ4e3z/LiTLuuxloR+Tr6n8GMnRZi7QAUTjAnwmP9cziF6PBagYmiqv84rjpORuAxrp8yU+ZBdTil/WzcyiUrdwWii2Y+quhQdxqpd1CeUDqArwv4yBboM4r4wqMlS4clsWRqvZI6RGKn8+auMGriq+MJ6xHknxrz9KbRXYV0Yr88XQzE7EmPB1hDurPxOLz5qeypbywrTju5hXWpdz/sabhjcvV9sumMcxRs1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dneYPSdL4kP0HiWqY714LZp4M6R402gIr+ydgNGo/0M=; b=XMHZZzO6O7EMRSOag014IM0n28x/aGKgN22C/zt0sbv62uruPzpDJI5rzb2sUJNp8DPBMtNmYnx2dqhCIYv1LNrfXfTbgOjT7Y035h+wJeTGGcyTGjjYk1CXStAW6623/b+VGeu7UQXMLqZwthHr1HuMGj/Y0LXrmQxdn2PqDhi1GsFtlGDLwOa+uyww8eF2foxb9OZ9auE4FPSd2oSXUxHK7V2vhOoJoQt4cDKNumNfOUTjiDwhJXO9eGw1bIpAK9g/QpxKeUo1wkU93cNWwmOWzGPZA/7Xhl8wZ4YPuHEygM2N7/xu8/11vXt0XAyP0f88xvyjqPfc5kKD/c2Wnw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dneYPSdL4kP0HiWqY714LZp4M6R402gIr+ydgNGo/0M=; b=cfW79OGYY233FMjHXgOMtyX1UT3DXDGKMxZje3yv+rR9mxAAYKDP0X6zQJDlVv/e6YtOJoU3+rYLiuDitWHjv+S3eGXXd/uYmEk1fMU2VnIF/4iDy/cqtcxZ6evCrdLPCS6LWa+YGJAGhM5uin/ahO76kX4PLQxz8RSwPQprSO0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by SA6PR01MB8853.prod.exchangelabs.com (2603:10b6:806:42b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.25; Thu, 12 Sep 2024 20:41:44 +0000 Received: from CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460]) by CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460%5]) with mapi id 15.20.7962.016; Thu, 12 Sep 2024 20:41:43 +0000 From: Yang Shi To: catalin.marinas@arm.com, will@kernel.org, muchun.song@linux.dev, david@redhat.com, akpm@linux-foundation.org Cc: yang@os.amperecomputing.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 1/2] hugetlb: arm64: add mte support Date: Thu, 12 Sep 2024 13:41:28 -0700 Message-ID: <20240912204129.1432995-1-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 X-ClientProxiedBy: CH0PR04CA0031.namprd04.prod.outlook.com (2603:10b6:610:77::6) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|SA6PR01MB8853:EE_ X-MS-Office365-Filtering-Correlation-Id: 6c337b75-4246-40a3-3a4d-08dcd36b4fd9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|52116014|376014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: lXL+TTWj9HLgPYvdU1Gb5aqBWBcpcd54h2UiAUjrpydi61JjFvGSiND9PqCcaZl2HWQdLlUwZYXTEpyTVMzcgA1W0Wkym0ZIbQQ+WyqUb1WtHmRxvLxJZVEOzduhbapc+Zquv9jtUayrlzt1nSTgdSBAQMjGadyNaI4/xUQdQvvmHDkcpjRqs+CrZwMoQuqIHtPnRMui7e4UiNyV780ajlgOOliDGmUIjnK6YajNO5MNMqLu+Dhr1o2htzCyiEh45ln8qz9QJb8vAvlD/dNkliuttSSpBgN5bZqyvAoCkdDw8NxpKiOn2KCtyaOsD1gdP0NNkWPoEtd3Sz17TKZK2IcI4n6PqJkn+sPKLw95sAJiIzqrYW+TAXxXn56R9BPGrJT6eAdyd8cTUzczeK+Ee3V4JMtZY10ma9F6+ZhjanmzQc6eB31zRh2KgWNQVFPi4Qj4q0D7zSUY61BsCT3Uy4yTYTOBLZGjhkCfe7STMrXYeokAjLbUr7SbMYC1FEe8aWi4XSLJMymabD6SP8Zj4AQvPNxxg54k6qwatBawUwEx1xU7Uh5YZyiE87PmGFngjalchfaH2vyBQ3RiEtJRfzp9svaB/+L84rjgeBN82ZNntVuomnPMAOtCgjUsp+YN6NiOYQdXQNQNol64uCVMagI8VcjroJVIAfDmjOR+nSWjhHGoG0WHt0BREjYHCt4nPSM47SMvjVvAuGEEdLKwggNObySs5YDZPddfsmiTnY9yaRdVddXbpGWJSSBFuQNRtlW6Hd3h1pVBDiWur3qKYOyzocQGi3NhkO2Do1bAPVMP0+9fufrSUKQ5q0EetJvcLzlGJyQlFMm5kHOSqTL3zEHBTvJrKUfGoFwOooI7qN+gr2EkNjH2TlChLJ6F7T/9PbBTVAIihE20S27X2jU5P5RIVwvBAGuM7qNjv7A8IWzJ2X8J3tV4jCYzCgn3y4XT7r7I9L9T+Ylg5KiOkAmcTxnRqcSPHGIMLgvxfNfzi/uP//3ak5LsfxSSMhQzJxB4S0mrX4KfhG6YWr/cjQtetqSpBYvxSqknW4lGt92EVevYU+kBiuYlchFyIgtmmbB6wIZrcxiRuJqNQ4nPJ+lXDJQOy470UxaDfWiru720s5nuVNwc9l8b9IfK6PgQjD/iSxzsNkUPr/8bF2TMNfk/sAanHNV4O23Vl2IMKgA409s/j2HdiIzcIaepRHj0f9NR9X0l68GheWuYeyibcBOpLAr1bWn1ZxvQYZQ4JuN7hZ8LSrJzxGbQcg6bOZZM/BpOZFXeRy6hvEsW317hxl96KRVJs+onaBU872rsSPI/PjX1x8F1t/zyVIvai2TyYj1EjL4wA+CI6fdgfNPKccUPj5SX1mB7W6ph9rAkzLvQ8bdQAsRF36kpV5KDrMp/lFVt4xNFr6tF9jIeIhn1Ihd9HA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH0PR01MB6873.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(52116014)(376014)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MsJh0X7LBq2XvWASUtLVAvhDpkuue/+K843P4fW6NxINRzm8JJu1eQJoLBxU+co0hGxQSzrxqEd7jiSjqhHUrDFaVmKcMNG4JoayHK3GdNnNhT6xdLk0MN9z3ddgHPfoTXfiZRsQoQTGlGecGU57yNbg6HBVwSbWcR6OOa8sz0F0TeEGaPX9Lt2Qtyd4eyjtqkTsNwXuqTMwNPaxXqF3Gj7ahtGXg3Mlvty1QxQd7Nan+6JCug+wKSbbdFGRC3XgCy1xmQ7FF5jvJcBKYkYZgW90cIpEaHNik9Xabr8ofD9DdsCRGUXyx620Sju/uhz7YXIOTGiR5Y84GNRBm3yByzP0Qsaq2YA9oF/nsZJjxS/MDS+aK9ICegTopWUaXbMBdyqvoJ2QkB+Voh0xtqgkPTZ2a3BPucL1vcsgLW3PBnZmnRfLKoFItdTLOYJAwZtJKo7HmpR6jaAUoknIWnCL4c/442SDPsHgGw7Su9Txv9aE/vMy0NE+xnK1L+DjZjuvk3bHGrzvyC7kG9IgEt1sFsqmNjcOs/6s8plpbvUqpuUFo8YNHIJsKbB6Glo/Rgi3GKaycUNMWQFYNGkSULi0LcnLDmLIvDIvD7gAN0hKoygbNdNVPfvzPOG1TD8k4wEC9EZXGlG/8vqDwjkld+AY0+XK+V6zV/ONFz4ZEzW4DZuizCVlgukTYv7W7ovq1DrDmrV78chyK5TvfqOd4KZXQ1fL3BEHdI/6Yb3XIdugYpYhBMvtGhNjtfqf/a0IbnLNprQHsyU81Un7mD9V0Hf+gvBOBtH3pXxydpt/UY8EjeYXMvPvudtbv2OoP4WGnGMrX/+ZGLagRFnrMBf93VKr78p0WocYgsWmXFo/JTQ3xtkfu67W0xvMFUHwx41E7HwuSrfoz8K4zW6ilXMwYmwzvGEj9skcZ7uoHDILl1cq0izrW0CzqSvoLzBs9IrI/gSDTgfYC+RPPUIZrvpjHuIExuYiWVI9q3MkDjFlZupY73CCv4V04GRGVb6C9cMuW/HnUIPpfTnDOsmZRuv2d4qYWYeJBHlARqJ9dEgtbT4IxnlCpUVNcKsUIxrCRpWgXo85ELw+CRu1/VJY4Agh76TUqel+tkmclQkScGDyl0DpSYtPaUjoU1VGiWkqFYrNyUnfqadfsLWsJB/pQS4RBeQ9v/NpYGofCcjh5wbyceOQvW7LvqScyptOdxJvwLmVcvZwg2ypxs5+LhneIEhmpR4q/k63g07d4xiwupG8vwDeCnKW946aOcRMTUsg5a012+lPfYhiYg2We9MRXJI7QtszpbkMEojZlQJyiFhO1WciIoLWPKNIit0Dd4PBvSMXubDfDlIjf8QdfdV9PBnoxfbifht/iAZmshszERxMlAFRSXkc9pi8jSYBubv9AsOVgUEwkG8g/9LeZvIu0wiJPG7KRDiJ7UtrLAhGkIbzKW2YlaCbgvZLp3peXot+Hzz/e0pahiTRn4aEuziM7Rp0T0KSGO4TF5AdS3fxdcOk42Ww0esI25f3+QM3KWQoQNSrcAJgp4dtx7SUKUBflEizf1KQPlM6HpOLE8qhEnCMYXHFa7SOo4FhtqGMFuyPFxZyWmE8BTo58gTnjkfeLgkaqaYmtqkP1hJiFJQ6zzklrAZKm14= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6c337b75-4246-40a3-3a4d-08dcd36b4fd9 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2024 20:41:43.6099 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: dB2o83Fvu/IuIliSNzuAU9cN2vfepPRtSi6lcqN2/vP2vNKJVCDXT6PcrGwngwAxDubXO2BziIR3O/Q0YyOgPyzl2hnUqY8+gYWYdAMatYY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR01MB8853 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240912_134151_089601_36587B56 X-CRM114-Status: GOOD ( 25.71 ) 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 Enable MTE support for hugetlb. The MTE page flags will be set on the folio only. When copying hugetlb folio (for example, CoW), the tags for all subpages will be copied when copying the first subpage. When freeing hugetlb folio, the MTE flags will be cleared. Signed-off-by: Yang Shi --- arch/arm64/include/asm/hugetlb.h | 8 ++++ arch/arm64/include/asm/mman.h | 3 +- arch/arm64/include/asm/mte.h | 67 ++++++++++++++++++++++++++++++++ arch/arm64/kernel/hibernate.c | 6 +++ arch/arm64/kernel/mte.c | 27 ++++++++++++- arch/arm64/kvm/guest.c | 16 ++++++-- arch/arm64/kvm/mmu.c | 11 ++++++ arch/arm64/mm/copypage.c | 34 +++++++++++++--- fs/hugetlbfs/inode.c | 2 +- 9 files changed, 162 insertions(+), 12 deletions(-) v4: * Fixed the comment from David. v3: * Fixed the build error when !CONFIG_ARM64_MTE. * Incorporated the comment from David to have hugetlb folio specific APIs for manipulating the page flags. * Don't assume the first page is the head page since huge page copy can start from any subpage. v2: * Reimplemented the patch to fix the comments from Catalin. * Added test cases (patch #2) per Catalin. diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/hugetlb.h index 293f880865e8..c6dff3e69539 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -11,6 +11,7 @@ #define __ASM_HUGETLB_H #include +#include #include #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION @@ -21,6 +22,13 @@ extern bool arch_hugetlb_migration_supported(struct hstate *h); static inline void arch_clear_hugetlb_flags(struct folio *folio) { clear_bit(PG_dcache_clean, &folio->flags); + +#ifdef CONFIG_ARM64_MTE + if (system_supports_mte()) { + clear_bit(PG_mte_tagged, &folio->flags); + clear_bit(PG_mte_lock, &folio->flags); + } +#endif } #define arch_clear_hugetlb_flags arch_clear_hugetlb_flags diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h index 5966ee4a6154..304dfc499e68 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -28,7 +28,8 @@ static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags) * backed by tags-capable memory. The vm_flags may be overridden by a * filesystem supporting MTE (RAM-based). */ - if (system_supports_mte() && (flags & MAP_ANONYMOUS)) + if (system_supports_mte() && + (flags & (MAP_ANONYMOUS | MAP_HUGETLB))) return VM_MTE_ALLOWED; return 0; diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h index 0f84518632b4..af8b92840d54 100644 --- a/arch/arm64/include/asm/mte.h +++ b/arch/arm64/include/asm/mte.h @@ -41,6 +41,8 @@ void mte_free_tag_storage(char *storage); static inline void set_page_mte_tagged(struct page *page) { + VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); + /* * Ensure that the tags written prior to this function are visible * before the page flags update. @@ -51,6 +53,8 @@ static inline void set_page_mte_tagged(struct page *page) static inline bool page_mte_tagged(struct page *page) { + VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); + bool ret = test_bit(PG_mte_tagged, &page->flags); /* @@ -76,6 +80,8 @@ static inline bool page_mte_tagged(struct page *page) */ static inline bool try_page_mte_tagging(struct page *page) { + VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); + if (!test_and_set_bit(PG_mte_lock, &page->flags)) return true; @@ -157,6 +163,67 @@ static inline int mte_ptrace_copy_tags(struct task_struct *child, #endif /* CONFIG_ARM64_MTE */ +#if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_ARM64_MTE) +static inline void folio_set_hugetlb_mte_tagged(struct folio *folio) +{ + VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); + + /* + * Ensure that the tags written prior to this function are visible + * before the folio flags update. + */ + smp_wmb(); + set_bit(PG_mte_tagged, &folio->flags); + +} + +static inline bool folio_test_hugetlb_mte_tagged(struct folio *folio) +{ + VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); + + bool ret = test_bit(PG_mte_tagged, &folio->flags); + + /* + * If the folio is tagged, ensure ordering with a likely subsequent + * read of the tags. + */ + if (ret) + smp_rmb(); + return ret; +} + +static inline bool try_folio_hugetlb_mte_tagging(struct folio *folio) +{ + VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); + + if (!test_and_set_bit(PG_mte_lock, &folio->flags)) + return true; + + /* + * The tags are either being initialised or may have been initialised + * already. Check if the PG_mte_tagged flag has been set or wait + * otherwise. + */ + smp_cond_load_acquire(&folio->flags, VAL & (1UL << PG_mte_tagged)); + + return false; +} +#else +static inline void folio_set_hugetlb_mte_tagged(struct folio *folio) +{ +} + +static inline bool folio_test_hugetlb_mte_tagged(struct folio *folio) +{ + return false; +} + +static inline bool try_folio_hugetlb_mte_tagging(struct folio *folio) +{ + return false; +} +#endif + static inline void mte_disable_tco_entry(struct task_struct *task) { if (!system_supports_mte()) diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 02870beb271e..74626d5cdf1d 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -266,9 +266,15 @@ static int swsusp_mte_save_tags(void) max_zone_pfn = zone_end_pfn(zone); for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) { struct page *page = pfn_to_online_page(pfn); + struct folio *folio; if (!page) continue; + folio = page_folio(page); + + if (folio_test_hugetlb(folio) && + !folio_test_hugetlb_mte_tagged(folio)) + continue; if (!page_mte_tagged(page)) continue; diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 6174671be7c1..4337acc63d2f 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -38,7 +38,24 @@ EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode); void mte_sync_tags(pte_t pte, unsigned int nr_pages) { struct page *page = pte_page(pte); - unsigned int i; + struct folio *folio = page_folio(page); + unsigned long i; + + if (folio_test_hugetlb(folio)) { + unsigned long nr = folio_nr_pages(folio); + + /* Hugetlb MTE flags are set for head page only */ + if (try_folio_hugetlb_mte_tagging(folio)) { + for (i = 0; i < nr; i++, page++) + mte_clear_page_tags(page_address(page)); + folio_set_hugetlb_mte_tagged(folio); + } + + /* ensure the tags are visible before the PTE is set */ + smp_wmb(); + + return; + } /* if PG_mte_tagged is set, tags have already been initialised */ for (i = 0; i < nr_pages; i++, page++) { @@ -410,6 +427,7 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr, void *maddr; struct page *page = get_user_page_vma_remote(mm, addr, gup_flags, &vma); + struct folio *folio; if (IS_ERR(page)) { err = PTR_ERR(page); @@ -428,7 +446,12 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr, put_page(page); break; } - WARN_ON_ONCE(!page_mte_tagged(page)); + + folio = page_folio(page); + if (folio_test_hugetlb(folio)) + WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio)); + else + WARN_ON_ONCE(!page_mte_tagged(page)); /* limit access to the end of the page */ offset = offset_in_page(addr); diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 11098eb7eb44..d7e161ca67e6 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1050,6 +1050,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, void *maddr; unsigned long num_tags; struct page *page; + struct folio *folio; if (is_error_noslot_pfn(pfn)) { ret = -EFAULT; @@ -1062,10 +1063,13 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, ret = -EFAULT; goto out; } + folio = page_folio(page); maddr = page_address(page); if (!write) { - if (page_mte_tagged(page)) + if ((folio_test_hugetlb(folio) && + folio_test_hugetlb_mte_tagged(folio)) || + page_mte_tagged(page)) num_tags = mte_copy_tags_to_user(tags, maddr, MTE_GRANULES_PER_PAGE); else @@ -1079,14 +1083,20 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, * __set_ptes() in the VMM but still overriding the * tags, hence ignoring the return value. */ - try_page_mte_tagging(page); + if (folio_test_hugetlb(folio)) + try_folio_hugetlb_mte_tagging(folio); + else + try_page_mte_tagging(page); num_tags = mte_copy_tags_from_user(maddr, tags, MTE_GRANULES_PER_PAGE); /* uaccess failed, don't leave stale tags */ if (num_tags != MTE_GRANULES_PER_PAGE) mte_clear_page_tags(maddr); - set_page_mte_tagged(page); + if (folio_test_hugetlb(folio)) + folio_set_hugetlb_mte_tagged(folio); + else + set_page_mte_tagged(page); kvm_release_pfn_dirty(pfn); } diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index a509b63bd4dd..8eeb4f6969de 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1401,10 +1401,21 @@ static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn, { unsigned long i, nr_pages = size >> PAGE_SHIFT; struct page *page = pfn_to_page(pfn); + struct folio *folio = page_folio(page); if (!kvm_has_mte(kvm)) return; + if (folio_test_hugetlb(folio)) { + /* Hugetlb has MTE flags set on head page only */ + if (try_folio_hugetlb_mte_tagging(folio)) { + for (i = 0; i < nr_pages; i++, page++) + mte_clear_page_tags(page_address(page)); + folio_set_hugetlb_mte_tagged(folio); + } + return; + } + for (i = 0; i < nr_pages; i++, page++) { if (try_page_mte_tagging(page)) { mte_clear_page_tags(page_address(page)); diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c index a7bb20055ce0..c8687ccc2633 100644 --- a/arch/arm64/mm/copypage.c +++ b/arch/arm64/mm/copypage.c @@ -18,17 +18,41 @@ void copy_highpage(struct page *to, struct page *from) { void *kto = page_address(to); void *kfrom = page_address(from); + struct folio *src = page_folio(from); + struct folio *dst = page_folio(to); + unsigned int i, nr_pages; copy_page(kto, kfrom); if (kasan_hw_tags_enabled()) page_kasan_tag_reset(to); - if (system_supports_mte() && page_mte_tagged(from)) { - /* It's a new page, shouldn't have been tagged yet */ - WARN_ON_ONCE(!try_page_mte_tagging(to)); - mte_copy_page_tags(kto, kfrom); - set_page_mte_tagged(to); + if (system_supports_mte()) { + if (folio_test_hugetlb(src) && + folio_test_hugetlb_mte_tagged(src)) { + if (!try_folio_hugetlb_mte_tagging(dst)) + return; + + /* + * Populate tags for all subpages. + * + * Don't assume the first page is head page since + * huge page copy may start from any subpage. + */ + nr_pages = folio_nr_pages(src); + for (i = 0; i < nr_pages; i++) { + kfrom = page_address(folio_page(src, i)); + kto = page_address(folio_page(dst, i)); + mte_copy_page_tags(kto, kfrom); + } + folio_set_hugetlb_mte_tagged(dst); + } else if (page_mte_tagged(from)) { + /* It's a new page, shouldn't have been tagged yet */ + WARN_ON_ONCE(!try_page_mte_tagging(to)); + + mte_copy_page_tags(kto, kfrom); + set_page_mte_tagged(to); + } } } EXPORT_SYMBOL(copy_highpage); diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 9f6cff356796..f944e8e7126b 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -110,7 +110,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) * way when do_mmap unwinds (may be important on powerpc * and ia64). */ - vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND); + vm_flags_set(vma, VM_HUGETLB | VM_DONTEXPAND | VM_MTE_ALLOWED); vma->vm_ops = &hugetlb_vm_ops; ret = seal_check_write(info->seals, vma); From patchwork Thu Sep 12 20:41:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13802643 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 F1C29EEE25C for ; Thu, 12 Sep 2024 20:44:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding: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=rZEmI5ze4VGJmlbeh/nIhqSgSXgQnJWJLInFVw0UZe0=; b=j6O0cMjGn6V/fFHYwrrKPW+5Sc ZEjbdm7NlwKRornEz4zw4owvqwUe4XekyKmzLoFYOENw46FZ5TObY2Lv7WRjJQgdr4C1d9bviJHGc UvGzWY/425JuhKdIdWUpqwJLnKu7Sg0XePJm04In8SDnwD3dPW8ddaF7W/UhNnK1ZCeXX89/D7CMG dT864VXeCDZtP4LYC1ahV30DZWlGlRy1FiCnt7NYBK+EupEy2gmQnXWULymkX2I4OYYD308GZiP24 2ZQjgRVNPPRT6rnPLZ2f2PCH041i9RwzgI/SicZrESQafzQoh5h9tzATtqlTY1bKvkepoIXLEw+8z FosujqMA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1soqfa-0000000ECLo-0qkd; Thu, 12 Sep 2024 20:44:02 +0000 Received: from mail-southcentralusazlp170120000.outbound.protection.outlook.com ([2a01:111:f403:c10d::] helo=SN4PR2101CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1soqdV-0000000EC7Z-2sTf for linux-arm-kernel@lists.infradead.org; Thu, 12 Sep 2024 20:41:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Cyh47sI3XM9NU1NAi6CD+jJYK6MQaMuQVsrKaE9RGEc3k2Rslf5cc7lQJgzSIHTq78TZKBLnfUfPg+1qVFYohiiTuD3sMK+2LgTFGTj3yYjOjIz6MX0Ohj1Tv6QRQNfU3UTyJJiisBTip2nPaB8tgsJo4R2vhZ+wMJi2c2o9XYQAMDE9maX1yFFbLdLfVWcwBXKVsXGj/edYBKWdXOhXADGxhU1DBbPd9zIylxI5IkQzNcfvimw6t1pl6Ehaeznp2nMYs+yb4PBbSrOrkoFGfAgePVzCrMlqB08lfvDcABpz4im3V4bzDkSH2Y7NY7E0i/wtXZJHahR1vLvKJKyBsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rZEmI5ze4VGJmlbeh/nIhqSgSXgQnJWJLInFVw0UZe0=; b=DEPqP6CX9e24Bsqb78rSZpFVdj4aSEarDvjU1ntlzVEBk3Ce3CYtnDj/hf07dU0RDbiSYUd0tPCf4pJB8f4F4RqpRBpZM5LMp/jNFvj6s38tAD8PFmCdcalhy0zw5ZNhhdZYT1RWzvXMAAYvB4s8ivnrgB4G53twgZH8fFBpYQqB66BIOEfYcnIoO/Bg0CupWey1lZEaR8KoyyGsRa1pjmccSQ2pblFQIsg/fhuxdj5+FNdsW7BpZtsXz4zhA67Rm6xgPtdYmKOoqfkxP664mLXb84/dNXyRymTCqrZ/79bEZnydEtNwIO4TcOOfNtaWij7pou8YnUvJrd74wfpoHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rZEmI5ze4VGJmlbeh/nIhqSgSXgQnJWJLInFVw0UZe0=; b=ijO4/GNKEkuaL3hG6VlkYUZIDgl6phK5YHAhgDpAnILSw7++YrCNVKYiLSI3O4kFRtiZtvXJ5HqBNLXoSUin+5SUHWPOOliXoQ4TJXfUzV7U5NATno1K1BFueK/YXZjoFs5/QplXvzGT2nCjvpfGl4oaisUMMCEudiYQbJ+xYPU= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by SA6PR01MB8853.prod.exchangelabs.com (2603:10b6:806:42b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.25; Thu, 12 Sep 2024 20:41:44 +0000 Received: from CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460]) by CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460%5]) with mapi id 15.20.7962.016; Thu, 12 Sep 2024 20:41:44 +0000 From: Yang Shi To: catalin.marinas@arm.com, will@kernel.org, muchun.song@linux.dev, david@redhat.com, akpm@linux-foundation.org Cc: yang@os.amperecomputing.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 2/2] selftests: arm64: add hugetlb mte tests Date: Thu, 12 Sep 2024 13:41:29 -0700 Message-ID: <20240912204129.1432995-2-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240912204129.1432995-1-yang@os.amperecomputing.com> References: <20240912204129.1432995-1-yang@os.amperecomputing.com> X-ClientProxiedBy: CH0PR04CA0031.namprd04.prod.outlook.com (2603:10b6:610:77::6) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|SA6PR01MB8853:EE_ X-MS-Office365-Filtering-Correlation-Id: fd04d617-291d-4888-6c1a-08dcd36b5060 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|52116014|376014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: y6wv3yUqu+hxkqdL5HxLHDpLiQi3nE3FjktHpeFJQThBHjTbJ8JcFTMLrnSxkZJynz0u7qIRXPKg+u+SQEvu9knonZ0E7LG5xF61fG2c7oTYSr1GhYTPD5HPr9sTBalyoLX9i0HS3jB3Hb5HnSOmoIU3Tl2SYMfim/ZOhQMHjs4WosBBOE40A20Fk1Qb7NWG0Mo6jSqXojyJ4onmHulHKs7Zobvl+OYlhRD4ZE4Q1RxCFnl5Au0a85GhWaxogbiaMyvM3W5tTtvlIKE5DqC7/swRgf6eJnBKbMqVmmMAlnnQ1dKYYH+kSKoh/Upt4R1RJdtbjGKICzGmU+BgaUVE6V0IU/1/6fSbWywbDhDKHG0XIk78OOLHNighnsqm7Mqs0OkzcIH+bkvEN2Z1xbYD3N5Tad0grrj4pwjfrPZsn9W02SDY1v5OQFW/t0I4xu/hFPsdTbDIPaKQQ+8y8MiD7a4XkE7R+cYJ5fv0Odkf4jEEorb63r9V9PdlubnOLSGNtjYnuXuKdjwU8zbWRD0qxs8YUVOvAkpx92hba7eWV7vn41+9jmITZWIVeIYPs9HUTmvVFByzHhOUo3fjDqIZi4lJeryYH96NBTIS3OBcEVFVBk1oHhcUymheDZQJ6AQ9OgRUT8uz5cJfgjcXMGKUHs3BrLnBheMtaH+E/lRlaerbivbUIldIiugdvzZ9JsBPyeNrVNopKnIqvGtDi1hxZJPva0A7DxpYfl8tQ1Ssdbh6AsQMP3TiUjsrsXlhmoTUMbxEDRobfIvyZxenn16LcLxe3pYx5L+J7aHhVj9krieluovJUSFuUvKNkJDohVVRIkUCewuj3vOEwmtxZ8IVSw3Ey29FVyWdulkN3ptA11rXBLEkGs8jAekJIfzpNqE1nHu1pR/tdp5r7k/D6JOZFtehvnSbMPNPdOpz6dq634KJ08g58FU01y39nilMn0w+FrpY+TrLAM73Sw2phbG7LRHipfN8vrMM3unKS/V2WplFzADlDb8Vc//7sM5pR7fHDCig6Tf7bJ+RSiBhjCa1Zb5pauhytL935siWXgWlQSBj9FVdfYEbn45zNy8tGV34q34nP9s8rDc+D60QbexL96EpF4wWzRLW2W4G0QGUJjBd0c/SYuJVe7WZAxSn4WNZ2QYaF1SolBvk73jhMka9hcRSora0wQiGybepsQhCV/qaSg/E0Dc0TjXG4zw/FxtrobInbKoPMyaM1kiAHgWUdTwdMq7u/LQfGt28oKlc5zkmepcEycWfbiaWuM3NAVYeH+6hxqWZ81R2zK+pawC5fEvcfQ6AmEHskdws7bBPdJ1g/RBwlL0ABVNgplSe7L9TmOG5/ykSd62sPtHie0G8oFRctKEQf/yYwT7dsaY24t7PnBop9EcVR1pHpAhgYT7kD8dSmuakn7mZL144P8zaig== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH0PR01MB6873.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(52116014)(376014)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: epCtaV2k2+PbK0uAoQXcNDifEKRuk7gw5efaWU1w8py5Bj9gXVOo2GGsXNOve7r3Sm2Q8W3Vqh+iLZU0kiTvXhMd3w7OEGvzmveUdD0xdCq3xpMC5nOJ60s8zwTHCWFYlYBkBLbSVwRoUCsvjDntATIG+YKDCtGM7YPXGoVURl8NtDIna0mUyVwdvP6HUk9cR1vHMAPFRqzWKKLuNL9DapZ+0WL3BjuZr+20dTPhhqE7CJVMC4k4fgO8rWbhDYIRmypa+fQ6OhaIYslgKrmjTMUiecjIGOwfGWH0AKkNrC63j2XT8WlE93g0D/qPexcq4Ar+F2Xz9qNmSTUYS5wwKnVcqHIToGoB2aYE27OpCfsNAZTFjqo2SSvP9o0B2eyUXkGUUfP+O1AWmq8Dvokns2bfC/7f6sYbg0p5ZfgR2jDrb5xFewztvKYFI6N8YW/5SGqds9LGeU71Ci/aBaCb68xWO64qysq7lWfmYB1D1hWvI76wKuNQ/cIuz+VHCga1WNrDDPO2vc/btHbIfuRwMyz05D6Gt8khTGl4JPeEWVBoQTpval4Qjn2EpQcY4pbWz3bP4babpxAGTzEAjQhddjfKSfAV461/OUhsQPMzmRoC61sgQZFedV/5lO24GfO5wMKs+CgZojDCVPjzMAXTVSW126ID2kOV03vra0ofFAc8l+Pnh+DoYe+Q16/Vs5c2mDm697aXko3JDjG2oyx/jiC3OsK5Xqh41B98NxkOV65qonwUQaSVqMM9y0+hxERKSPljQJMbPbc9TViubCGENT2MYeorcYWscQ6bC5ogSPcdss8znt0v1kikQpOBjle7FD/xvbp5fZjXtcT4429eHFvzt+e92N1AEObW5K9Vmxt7IgMRSS7ckukWBLPlk0s0Ib3A3pMqmX8l8j825ShB81hHKU1gaSChxXEU4FdUyGPDV8ZLCgvmbQpGabm5B7+CZl693zstDLSIujl1cuAOLgRAPzMa5nGMQLUn8j8o5MpYNrjdAFRbO6KHTOeNtUJwX1jJhMzuIfDYDpFNSbw82z9zi7CFXJ3QHaC959kw+oBZiWWt/vWTJz6dEiw4Igpq2OS7coRWBm8ybkfNU71qIgWz/A+eP45xzv+kmzCXSiyeDKKcNrFmuTv6630ZnjinQMLmVtMzRNPxLu6qQfbmzQQS40rfioQKJScsCxFyDzuFAT249kXejxnSnVGtxsOsRMFs6LLoX5Z35vKtI5dwvlTwdVjcHdD2f8GOmbw0RMAZ6mep1BNLeuYApTZzr22vxYTUL++fpo0hIHQOE+hzTnJkfiI4zg4UZUpnXE3/F0zbWv52k1JqyMhI//gUrp0JsuV2oMB9DHWO67QdZ0XuKmbkYZmbKi9lb3c+L3ASSBHkKPyFZ8LIrH0dIWaTpaaSZL7t5SKrFTb6EFJtFORzkyeKu7Um2floN36McB78jow0XqR+Wkql0LCO0CrK+5EZCPmfoZwadWonOkH4EvxVte69pEWF1xrG+0nsTUeWfnkkwPH0kjer7ZTn0uux3n0Mw/HOrCiqfONs5egtfsdLkRTx7+ldAMrWWFURLtajC+HjUYV/Hd1KChBNvmVK6Q/fnoOMRMHqHGvZ7UFKgOj6oxyz2VamJJFp/tXEarb8TqI= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: fd04d617-291d-4888-6c1a-08dcd36b5060 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2024 20:41:44.4482 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2i9lusvYMSA5fp5qhgvQgGJ3x+AJFZ+Xr3N+COZoVmET7x4qPfk4NNw2IpRBaqS401oJxs1BsRMF6XBWEcktSGq1U/ZAUvkv3L4s1n0Cpko= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR01MB8853 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240912_134153_761105_15BC3D25 X-CRM114-Status: GOOD ( 17.80 ) 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 The tests cover mmap, mprotect hugetlb with MTE prot and COW. Signed-off-by: Yang Shi --- .../arm64/mte/check_hugetlb_options.c | 285 ++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 tools/testing/selftests/arm64/mte/check_hugetlb_options.c diff --git a/tools/testing/selftests/arm64/mte/check_hugetlb_options.c b/tools/testing/selftests/arm64/mte/check_hugetlb_options.c new file mode 100644 index 000000000000..303260a6dc65 --- /dev/null +++ b/tools/testing/selftests/arm64/mte/check_hugetlb_options.c @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2024 Ampere Computing LLC + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kselftest.h" +#include "mte_common_util.h" +#include "mte_def.h" + +#define TAG_CHECK_ON 0 +#define TAG_CHECK_OFF 1 + +static unsigned long default_huge_page_size(void) +{ + unsigned long hps = 0; + char *line = NULL; + size_t linelen = 0; + FILE *f = fopen("/proc/meminfo", "r"); + + if (!f) + return 0; + while (getline(&line, &linelen, f) > 0) { + if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) { + hps <<= 10; + break; + } + } + + free(line); + fclose(f); + return hps; +} + +static bool is_hugetlb_allocated(void) +{ + unsigned long hps = 0; + char *line = NULL; + size_t linelen = 0; + FILE *f = fopen("/proc/meminfo", "r"); + + if (!f) + return false; + while (getline(&line, &linelen, f) > 0) { + if (sscanf(line, "Hugetlb: %lu kB", &hps) == 1) { + hps <<= 10; + break; + } + } + + free(line); + fclose(f); + + if (hps > 0) + return true; + + return false; +} + +static void write_sysfs(char *str, unsigned long val) +{ + FILE *f; + + f = fopen(str, "w"); + if (!f) { + ksft_print_msg("ERR: missing %s\n", str); + return; + } + fprintf(f, "%lu", val); + fclose(f); +} + +static void allocate_hugetlb() +{ + write_sysfs("/proc/sys/vm/nr_hugepages", 2); +} + +static void free_hugetlb() +{ + write_sysfs("/proc/sys/vm/nr_hugepages", 0); +} + +static int check_child_tag_inheritance(char *ptr, int size, int mode) +{ + int i, parent_tag, child_tag, fault, child_status; + pid_t child; + + parent_tag = MT_FETCH_TAG((uintptr_t)ptr); + fault = 0; + + child = fork(); + if (child == -1) { + ksft_print_msg("FAIL: child process creation\n"); + return KSFT_FAIL; + } else if (child == 0) { + mte_initialize_current_context(mode, (uintptr_t)ptr, size); + /* Do copy on write */ + memset(ptr, '1', size); + mte_wait_after_trig(); + if (cur_mte_cxt.fault_valid == true) { + fault = 1; + goto check_child_tag_inheritance_err; + } + for (i = 0; i < size; i += MT_GRANULE_SIZE) { + child_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i))); + if (parent_tag != child_tag) { + ksft_print_msg("FAIL: child mte tag (%d) mismatch\n", i); + fault = 1; + goto check_child_tag_inheritance_err; + } + } +check_child_tag_inheritance_err: + _exit(fault); + } + /* Wait for child process to terminate */ + wait(&child_status); + if (WIFEXITED(child_status)) + fault = WEXITSTATUS(child_status); + else + fault = 1; + return (fault) ? KSFT_FAIL : KSFT_PASS; +} + +static int check_mte_memory(char *ptr, int size, int mode, int tag_check) +{ + mte_initialize_current_context(mode, (uintptr_t)ptr, size); + memset(ptr, '1', size); + mte_wait_after_trig(); + if (cur_mte_cxt.fault_valid == true) + return KSFT_FAIL; + + return KSFT_PASS; +} + +static int check_hugetlb_memory_mapping(int mem_type, int mode, int mapping, int tag_check) +{ + char *ptr, *map_ptr; + int result; + unsigned long map_size; + + map_size = default_huge_page_size(); + + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); + map_ptr = (char *)mte_allocate_memory(map_size, mem_type, mapping, false); + if (check_allocated_memory(map_ptr, map_size, mem_type, false) != KSFT_PASS) + return KSFT_FAIL; + + mte_initialize_current_context(mode, (uintptr_t)map_ptr, map_size); + /* Only mte enabled memory will allow tag insertion */ + ptr = mte_insert_tags((void *)map_ptr, map_size); + if (!ptr || cur_mte_cxt.fault_valid == true) { + ksft_print_msg("FAIL: Insert tags on anonymous mmap memory\n"); + munmap((void *)map_ptr, map_size); + return KSFT_FAIL; + } + result = check_mte_memory(ptr, map_size, mode, tag_check); + mte_clear_tags((void *)ptr, map_size); + mte_free_memory((void *)map_ptr, map_size, mem_type, false); + if (result == KSFT_FAIL) + return KSFT_FAIL; + + return KSFT_PASS; +} + +static int check_clear_prot_mte_flag(int mem_type, int mode, int mapping) +{ + char *map_ptr; + int prot_flag, result; + unsigned long map_size; + + prot_flag = PROT_READ | PROT_WRITE; + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); + map_size = default_huge_page_size(); + map_ptr = (char *)mte_allocate_memory_tag_range(map_size, mem_type, mapping, + 0, 0); + if (check_allocated_memory_range(map_ptr, map_size, mem_type, + 0, 0) != KSFT_PASS) + return KSFT_FAIL; + /* Try to clear PROT_MTE property and verify it by tag checking */ + if (mprotect(map_ptr, map_size, prot_flag)) { + mte_free_memory_tag_range((void *)map_ptr, map_size, mem_type, + 0, 0); + ksft_print_msg("FAIL: mprotect not ignoring clear PROT_MTE property\n"); + return KSFT_FAIL; + } + result = check_mte_memory(map_ptr, map_size, mode, TAG_CHECK_ON); + mte_free_memory_tag_range((void *)map_ptr, map_size, mem_type, 0, 0); + if (result != KSFT_PASS) + return KSFT_FAIL; + + return KSFT_PASS; +} + +static int check_child_hugetlb_memory_mapping(int mem_type, int mode, int mapping) +{ + char *ptr; + int result; + unsigned long map_size; + + map_size = default_huge_page_size(); + + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); + ptr = (char *)mte_allocate_memory_tag_range(map_size, mem_type, mapping, + 0, 0); + if (check_allocated_memory_range(ptr, map_size, mem_type, + 0, 0) != KSFT_PASS) + return KSFT_FAIL; + result = check_child_tag_inheritance(ptr, map_size, mode); + mte_free_memory_tag_range((void *)ptr, map_size, mem_type, 0, 0); + if (result == KSFT_FAIL) + return result; + + return KSFT_PASS; +} + +int main(int argc, char *argv[]) +{ + int err; + + err = mte_default_setup(); + if (err) + return err; + + /* Register signal handlers */ + mte_register_signal(SIGBUS, mte_default_handler); + mte_register_signal(SIGSEGV, mte_default_handler); + + allocate_hugetlb(); + + if (!is_hugetlb_allocated()) { + ksft_print_msg("ERR: Unable allocate hugetlb pages\n"); + return KSFT_FAIL; + } + + /* Set test plan */ + ksft_set_plan(12); + + mte_enable_pstate_tco(); + + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_OFF), + "Check hugetlb memory with private mapping, sync error mode, mmap memory and tag check off\n"); + + mte_disable_pstate_tco(); + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_NONE_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_OFF), + "Check hugetlb memory with private mapping, no error mode, mmap memory and tag check off\n"); + + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, sync error mode, mmap memory and tag check on\n"); + evaluate_test(check_hugetlb_memory_mapping(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, sync error mode, mmap/mprotect memory and tag check on\n"); + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, async error mode, mmap memory and tag check on\n"); + evaluate_test(check_hugetlb_memory_mapping(USE_MPROTECT, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, async error mode, mmap/mprotect memory and tag check on\n"); + + evaluate_test(check_clear_prot_mte_flag(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check clear PROT_MTE flags with private mapping, sync error mode and mmap memory\n"); + evaluate_test(check_clear_prot_mte_flag(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check clear PROT_MTE flags with private mapping and sync error mode and mmap/mprotect memory\n"); + + evaluate_test(check_child_hugetlb_memory_mapping(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap memory\n"); + evaluate_test(check_child_hugetlb_memory_mapping(USE_MMAP, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap memory\n"); + evaluate_test(check_child_hugetlb_memory_mapping(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap/mprotect memory\n"); + evaluate_test(check_child_hugetlb_memory_mapping(USE_MPROTECT, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap/mprotect memory\n"); + + mte_restore_setup(); + free_hugetlb(); + ksft_print_cnts(); + return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL; +}