From patchwork Wed Aug 21 18:47:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13771988 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 46DA6C52D7C for ; Wed, 21 Aug 2024 18:49:30 +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=wVvb6nZgquVREbBjdKN5R8e0jFSK4GKpisDwZuRPDiU=; b=y2ncKsIXm8pRoRIvDt+fxCI+XT IOuBAOiye9bK76gRN+fYrOeHyvjP0apyv6Nrx9SjlQAaZvohRsnnuro8RKiUj2NYaFh7JZY8wGxuI gFnRqR0m6QNtq6yCq8aRJTiGELbCvMqC1Bngnrkkg0pBxhJTTTndyzjz1QADeDgw2ZtFOP+VIDjG9 z2RolcHlU7sUAr7aAavgUe8JG9APfpAzNQJHRoR6Pl+kM9d+1OlnJK8dCcpvM6S5mqgP+5ZBGKMcE zP/NK3Ss/xoRE0m1Qi3Z3ChT5OnLsksdE7KgJ3NLpIoXEuc8akmN3y/tPLJcsynWCCW8QFoOTt+yH +Lad74cg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgqOQ-0000000A6Za-2ZUs; Wed, 21 Aug 2024 18:49:14 +0000 Received: from mail-dm6nam12on2071d.outbound.protection.outlook.com ([2a01:111:f403:2417::71d] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgqNh-0000000A6S1-01xV for linux-arm-kernel@lists.infradead.org; Wed, 21 Aug 2024 18:48:30 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=r/x1fQUQS1VXMj5pbKSFJlXeLmiVU8lfla20fI2PE6KaFc5H+yuHtJrU17mhH5E6nQCvsFEusPma5tQJuQI2rW+X6DpmIc0Cwlpds/DymrXY6b0yu3M1W/eIdh9EKvlOyMoR/ZYRMVcDomZawW3H792OOkICql2JjFMSklIKvVCeqPUQZ6++pDEynBcx/zwWJ0H8ZnxiWJ3YjhRJXk9a0xqRpNzbxTrjFhWu3m9DVYlIuklgST6BcRpHiLlAQsxGJO1Y7MdHdtAv9ZAInRLMah8AHwcKpwyRjM0TNGriEMA90lnWXjC1vFFcy5eBYLH2JyI36bdS/L3KiIAHiuNq4Q== 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=wVvb6nZgquVREbBjdKN5R8e0jFSK4GKpisDwZuRPDiU=; b=shSbkq491/9LQrOgUKzYDFLlDfKEStVYPLmRlhyyTc7hwDfqaqEIdvOURnaHJFeW2ILO3ZVvyIK4iqlT8JPAAJF21xfP55itoFF/EtfpiYCtZ+M8GRL+053Y9Gx48SRJEaQuu5e4gybRrhFuwX/z6U43qHJvopqPS0F0u3/c69ouJ3DNkqGxEhcEVBg+RjctTmmMfWFUFEHEeUCf6nne1OfZp+mCPnYr2bnPoDAes9j9kyEVOe1BxW1iDJ7UaYu3QmIYrtFnbJn/zYIwbsinIuzVqgn0kt49ILj3xwhEPVHRjaRpEcB7YYRzDIg0OpSz2Sfhad2LgjMIEw9DxNAtog== 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=wVvb6nZgquVREbBjdKN5R8e0jFSK4GKpisDwZuRPDiU=; b=CUEdeyvwnGRP85LrpsS1uwUJb27V6aWhqb9MAErg4sWmAho1UjUBB2j0S7rgmLtvz8l7ylukmc9/tOBH1SVnaWjQmL+qaJ8z6qfAWo9FlxVqLE+nYMi/v9JVxzgyo1khDCUGPPpX6ApBDeH7v9APCbfACQ85dcZFOsBAZwkvLe0= 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 MW4PR01MB6146.prod.exchangelabs.com (2603:10b6:303:7c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.16; Wed, 21 Aug 2024 18:48:09 +0000 Received: from CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460]) by CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460%4]) with mapi id 15.20.7897.014; Wed, 21 Aug 2024 18:48:09 +0000 From: Yang Shi To: catalin.marinas@arm.com, will@kernel.org, muchun.song@linux.dev, 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: [v2 PATCH 1/2] hugetlb: arm64: add mte support Date: Wed, 21 Aug 2024 11:47:51 -0700 Message-ID: <20240821184752.3535276-1-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 X-ClientProxiedBy: SJ0PR13CA0007.namprd13.prod.outlook.com (2603:10b6:a03:2c0::12) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|MW4PR01MB6146:EE_ X-MS-Office365-Filtering-Correlation-Id: 773244a4-5578-4d65-1c84-08dcc211ccb4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|52116014|366016|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: YPSuqrxLEi3PkwHJq9veBlSfDZL8e6dddlB/coiwlvP9KW5iJ20yHRp+HQ5I+/BNtTbkk5pmSEdwO62udRYeqTp4Uz329gADmfh7U5l3IT+brcHNChEpyWlUSbW66RRJESkZjKFz6AyaQG4EfbKqSTMQ4YGTEiiUCKilMgMuUhoNd3cIgHOW9tfmRWH6otFGAAXyLwpyEN5EVjaReR5v+svV3IB3Umb89HYdeN8c+1iVDLze99duP8KoGYO+LRbNtjWW9ddnk5R06MIa8q7OXw/N2V/TDkdp7EY3rC/4sovVacKNI8hebE2cFxzE7ZEC8euu1fYruiN9bieo1uJ1Q6BPYSTh6t0NZqyDyb0N3E5qhch3OwHBp5KK3iyFXqAUWzuUY7Ssp/XPflfzQ+/TO9PrITg2Ht5hO8XAYX7SgXXp+0g4qBVTSESn/2MVkxgnBKE+o+Dg/QBrpHxCX5Gx/nHVU7lC8MTo0uYrJyVCYS/bJosrfhSATOWryP3CpF98bXSwHM34wmw8Cn79OCP9mKDDQwwf3JH9nQ4U4Dk4wYHjDpwCygqF6ToG3tUCacqUYx6EoKzHh5T8KRdDQ27ipj2hmpnjBJZUy8JxlgsXrAlO2nu6jlKvdvarWGkAM1FwPN2EeLinvNEjT5z5/D3aWL1oHQWEaSwdTC15cmXgJUhzPhSROFtQL2o85pZGvn8k0X+YK7Cj23SqS/9xcKMpAlglj445EpmpNNYLjXFcL3TfkPjm4cWSHRNTn4yqGUY6iCS5S6JoiAldgPS56gzwhcjNyaneFL/F2JT0OzLjihpjciJqPGIc6wmign/F5IWvgZhSOLt5s30qTSF4A9Yd8O4G5E7Nr1OUl5NrUO/wGa7AtJvSUZfxC0DNmugI5R2xVlB8W3LVbT85OLZQVGjNezVO+nvGLDg63WPBX+nL9S1I6aEop3GFJfMNkefOCMpH4NUbdqqOid0WVMFVfLE92HI0YZJc9VDqOtbXDvfH2jT+Oej7mynx4sDXYSJAgfwAf+/hoQH9E5Efi1jcJ9jR6jrYGX2dCfypylWGmrl/UAI8XB67uwbABtGulzF8eXjaR7hQtgu3KJjZiPxV9e6s/ErHUCMhFkUohH3ll5oZUMSGe806gff5ErQoiYt0J9mAlJwMoNKjZlGCCwPQhdg5vA1yDH/x0CRA/UrL1zYTuUKEN9whRODTVGkmp3IevqS1UnSZsMGY6XMXvAxqPhimAlr3LqXR6diX3D/tzi1dh85PvjRZso7ygU0HwB1BJuZGIcCbnNHtW6dbeKhi0X5JBMgPenjYtxtQb9j5Q53407HjX//BsypkfBNxZhyahKvnXvRThmIguYyFjkQhbirj+HHyIweZ3nhEZck0WGnESvTz7TygPMzKG7bV6S5gP0ib5t7+cYSz26ttCmODamslUg== 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)(376014)(52116014)(366016)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: L1bn8roE01kBp7oT6n2k3He0ih/SM0/qNoJMwQdQRPn7MUw1jwuS39/6DunwsHUs+VF+H8g/6MFdfFeOMwTDh+SjAY/cI2WRoPmdG50pax8jMhu7Co0KDspdZ0NNPvo+BEgoVXff1+Inf+3fpwKQpiswSkTZgTTc9ViSTKcCyRooxim6eIqWj/QmHeEnO/83COPuVTHntlIL8bgPmteKld9B75Nx3imsypvRqVQpAyN1jtDza+0PYJJF5I42UN53t1UyRs4gjXQX5f6P5cvbU+No4AZ199ZiEopDIRdThVgaNJxu6AhBgM7NEdl7XoBgoG230qsdisATketwBJUOUbmVb7cc1SXec3USoncIzAdAEV6LbKluOBv854zuieDWYX7Yo/yPiPmmogtWOVyZO5dyL2U6u/9sreAgFpSMNWRl1H0QqTpy1FQLR93VgTOgDsKHtgCekabqzI+s0wRBS8XGyqDsiOB/6R3RJBvmnTT/rtrs6Vc457yckl7zw5PVolXm4u/DzEUjqRrFCHJlAypCRkUyk8PlUG0PLuWpL9h0cgvSYF3dQP+kptWllksVwFKLwz9100Mt3/3hL/r2I2qR0NRfeTyoUwJsWdHKJXcQh83DV8Efy9SDMovoGrs3aOwVxBBq/kFHmo0oJDJY7ez0gyMuX3iJXFD0V6wn4mFdi0ANlD2HVbWHstzMlyoZEo6z6akO7Th7jr3b/HhD8KRyNe4sXnM8FltRdzy7OgHllTTdiBUUB55nv/2oQQ9s3lqgeCsqUR0+LM9bnuky223epkSb+sS1yiq4HoiWZTRwiXxGXCV457026YmK0vGTQZpBh5N4cT3lQBYVwBirq4nRbUBttiIwRQ/9dAKPz/P0321dwxUtzH3tbnqgSQ84xc5E/73mEAXr8p4MCg9KbB6v/CTWHJqx6bmi5MJqQIuofe5Ll1ymmHF+/MSp8x/TQUT/UaUkx+iYCtOxDviCJPs3cdBcFPMR5WK0xaVVhRmI1uai84YFLEqAaHu6YW3FShGa/lyyfGjTS80l/enVukX1i2WYkDJ0eM+TOZE9MLsZkRnNruStxH7SRxtXt/WQbrhMANqBbyawYcA/RtKHBZg2U6Be1i0unaoJ9vPAtCKuBgnzweSgPzm/lHYJqV7ZUK8NjyVTEsNU2Rq6tvnlt053jNCqMfhIoeRAWRVYfeFyCoWcH6JEFrwEpY+TwZGw5snmJrQ3YxwMtLPIbdJ9VKLYbT/CwcnT+Y37KP6JlLHg7BNKeJ6Gx4j8mkxdxASiCnxT3SM5gY5FlSFYqyyJxN7ARTu802sdIMqu5sB0fpPsun/sxnVrfUSeVKbCtEBtSS46cp95lDcJtqre/PY3oVb4sG98MVGr/GgG4GgPNyxD+sBItxmNv6+05qZ9jLMorebcRXvCeain6DKpqgy8HUUCxGbgKQdynF3CtLczk1Fvqq/nyDjawGKs73DQ3nbkDS0AmliYAftF8FsZM5k8SZxCkun6+ZuJ4uNQo7kl6pWKkEkCEffE8lBdB6XhOxztvwQf5E23IjPMs7H2kr03knRhGnYsbr0whaqiPCfWeAV+zla2A5QLF+zYwdfJAyaQNC07TfEy4ysLSQ2HNUBD0FGtxVWonjInF/RxVYs7Hbw= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 773244a4-5578-4d65-1c84-08dcc211ccb4 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2024 18:48:09.2159 (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: c5OZcN1fljJz1h29MEZB8orP+mcUIJ9pO7Ex9gQVXpNN1dBnrN41iM7CZ4nYoG4aMrzbMCkdEwxiPTqp/yJjEklH+J7EUvqHGSDXYVFHdEs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR01MB6146 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240821_114829_115248_275EDCE5 X-CRM114-Status: GOOD ( 23.01 ) 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 head page only. When copying hugetlb folio, the tags for all tail pages will be copied when copying head page. When freeing hugetlb folio, the MTE flags will be cleared. Signed-off-by: Yang Shi --- arch/arm64/include/asm/hugetlb.h | 11 ++++++++++- arch/arm64/include/asm/mman.h | 3 ++- arch/arm64/kernel/hibernate.c | 7 +++++++ arch/arm64/kernel/mte.c | 25 +++++++++++++++++++++++-- arch/arm64/kvm/guest.c | 16 +++++++++++++--- arch/arm64/kvm/mmu.c | 11 +++++++++++ arch/arm64/mm/copypage.c | 25 +++++++++++++++++++++++-- fs/hugetlbfs/inode.c | 2 +- 8 files changed, 90 insertions(+), 10 deletions(-) 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..00a1f75d40ee 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 @@ -20,7 +21,15 @@ 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); + const unsigned long clear_flags = BIT(PG_dcache_clean) | + BIT(PG_mte_tagged) | BIT(PG_mte_lock); + + if (!system_supports_mte()) { + clear_bit(PG_dcache_clean, &folio->flags); + return; + } + + folio->flags &= ~clear_flags; } #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/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 02870beb271e..722e76f29141 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -266,10 +266,17 @@ 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) && + !page_mte_tagged(&folio->page)) + continue; + if (!page_mte_tagged(page)) continue; diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 6174671be7c1..b21f706018f7 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -38,7 +38,22 @@ 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_page_mte_tagging(&folio->page)) { + for (i = 0; i < nr; i++, page++) + mte_clear_page_tags(page_address(page)); + set_page_mte_tagged(&folio->page); + } + + smp_wmb(); + + return; + } /* if PG_mte_tagged is set, tags have already been initialised */ for (i = 0; i < nr_pages; i++, page++) { @@ -410,6 +425,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 +444,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(!page_mte_tagged(&folio->page)); + 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..77e181d96e97 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 (page_mte_tagged(page) || + (folio_test_hugetlb(folio) && + page_mte_tagged(&folio->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_page_mte_tagging(&folio->page); + 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)) + set_page_mte_tagged(&folio->page); + 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 6981b1bc0946..1fa51ac4e3f4 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_page_mte_tagging(&folio->page)) { + for (i = 0; i < nr_pages; i++, page++) + mte_clear_page_tags(page_address(page)); + set_page_mte_tagged(&folio->page); + } + 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..0f3b07d4a5cf 100644 --- a/arch/arm64/mm/copypage.c +++ b/arch/arm64/mm/copypage.c @@ -18,6 +18,9 @@ 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); @@ -27,8 +30,26 @@ void copy_highpage(struct page *to, struct page *from) 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); + + /* Populate tags for all subpages if hugetlb */ + if (folio_test_hugetlb(src)) { + /* + * MTE page flag is just set on the head page of + * hugetlb. If from has MTE flag set, it must be the + * head page. + */ + VM_BUG_ON(!PageHead(from)); + nr_pages = folio_nr_pages(src); + for (i = 0; i < nr_pages; i++, to++, from++) { + kto = page_address(to); + kfrom = page_address(from); + mte_copy_page_tags(kto, kfrom); + } + set_page_mte_tagged(&dst->page); + } else { + 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 Wed Aug 21 18:47:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13771989 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 65D3AC52D6F for ; Wed, 21 Aug 2024 18:50: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=DhMl6yZhZwoCLsfHBbsMjHl5vbOvP0mp8rTT9+FuyxM=; b=NhclQDdDe5Lt23TPM5aYnbF+cE c/LnGCZVcy9GPkbpixLs+EDVlFIQkn/G3Y713eOrXDkmbqCmYi/S3aU1DfotQroWDYyn+C5ZIYzG+ bjNhUGS//nTdKOhR7WK+oZv3hwKlFVoHy4MpA1/z8k+Thw5Jv8RGUagz2FfVsVa8OHau0tIcCAdvv ukEUzE/SJjy5D34rO0O0k2REIAYIymb9Mdxqeuadts40Oc3mPu3+IVM7CKA5Qb03jv2Y4tm8qBeMN CJHE3Yb5iJvf5NpeEe6rnw/FEaeupmT5SKG+Hc4/2iFi20V1v0NWJ67xbFyNBvtaK6ZMcrS6P3bBS c/GxkoaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgqP6-0000000A6fZ-32fe; Wed, 21 Aug 2024 18:49:56 +0000 Received: from mail-dm6nam12on2071d.outbound.protection.outlook.com ([2a01:111:f403:2417::71d] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgqNi-0000000A6S1-2eRg for linux-arm-kernel@lists.infradead.org; Wed, 21 Aug 2024 18:48:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uJVFe7dD1BeXQZ+2Q13fN1n7hGiEJMPt3PrpCxG6KHtUDug51Dkl8deFziCF06lNmGNzFsQ7xv9oerqks3Eau8C+G/tibc5wacGDuCBkB8LQJAz6CtrlN7cs4+gsprmpsq3xoyBnNWHXvUQZrc7qgCQWd7/Z3xckEGjFUYUHj8/RXKz7oFr+zhtFTG9G0hGS43lnkiQolfCgpXSCi08K3Fw77z1eWL4FQalWjHgOVuqF/LNQHmbfyRKXLMQHeHfi3tR3kfBG4RbiHRhaHZXjpihaY4QoKTA30KqgE1Fr/8j0MuRH56bLfb91Q801AAu4S3+luG31lER3Xew021mIog== 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=DhMl6yZhZwoCLsfHBbsMjHl5vbOvP0mp8rTT9+FuyxM=; b=IwBNrhj/k9n8z1uoTTRVNMUfVNybA0jF/3wAlJvcABx1jvMnD2MvqfXj6Ite1eN+hVBkmE3hvesFWPEvq0aIjx40UMsW6RlYSDACcAxP2pxWj2sXeZptUABJU5j14uU4B4IyHgU39sa25xAx9kgxJpgw4ESIrbv1ebPM2cfSOZFZszhlTLndt3ZVA7p6s4g/pw/a3dpFq5AdRGePd4OhAQBO8Q4TYT8nmpBOvSwPutx/fSVGnv3+76uT3PzIGhxJtqtAp7MV2jqbA1EUfKL8KtghnagZGytUi4NW95T/ZLCtrNtmybsYHrBa1FxF8r2ekY+t7POJv10IIltT1pkN/Q== 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=DhMl6yZhZwoCLsfHBbsMjHl5vbOvP0mp8rTT9+FuyxM=; b=DVEYAKHjG0B9VUVy/bp//ujBft60MKv56aiKgy4+ljmfW5ArFBtvHf8IBZaQmNWzaMgWhCnUIyfePljCM1LYwlfUao4lo5C2FVmXX2CSPwZ2AiZ3XRE6qYjo01W8hCPO7uNWbKpXL9aPflfiIp4quS34fbZL9ahg9w0+7FL1lvI= 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 MW4PR01MB6146.prod.exchangelabs.com (2603:10b6:303:7c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.16; Wed, 21 Aug 2024 18:48:10 +0000 Received: from CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460]) by CH0PR01MB6873.prod.exchangelabs.com ([fe80::3850:9112:f3bf:6460%4]) with mapi id 15.20.7897.014; Wed, 21 Aug 2024 18:48:10 +0000 From: Yang Shi To: catalin.marinas@arm.com, will@kernel.org, muchun.song@linux.dev, 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: [v2 PATCH 2/2] selftests: arm64: add hugetlb mte tests Date: Wed, 21 Aug 2024 11:47:52 -0700 Message-ID: <20240821184752.3535276-2-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240821184752.3535276-1-yang@os.amperecomputing.com> References: <20240821184752.3535276-1-yang@os.amperecomputing.com> X-ClientProxiedBy: SJ0PR13CA0007.namprd13.prod.outlook.com (2603:10b6:a03:2c0::12) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|MW4PR01MB6146:EE_ X-MS-Office365-Filtering-Correlation-Id: bac85570-1a6d-4d29-eb1b-08dcc211cd94 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|52116014|366016|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: 9CaN8BmL46/6k149IAc0nW1P3IICQhNbp8Fus/rvTTs2g/8NQUj2vcX/oeGufzrMl3qYivANZuOLQsECKkITCgj1mGIlHxJ/qe4+J8P3QbnxKe/aUKtryHVajqviyLGb/wu++ea8zUMGLvDSvZTUwUEtQ+tD8dhkM5jz3GkcYTcVPz9HK0wQKztTbeM0TlcA8KBiZz+UJ4y4SQKinlSVijwZ/FsaoDepM7Y3hWhsAHOaXJW3J7kyh/iiuQLb9IiAANLYJzBtccLNPTis5gLQrrQasw3/vpCs5KNV3G0VY2cxZAz0S5wK++vO0/OEzsVTwCxhsZ7oXL0kfipH7OMX1VLRXZL/So0YHimzYfbF+NWO+kjleAp7s5om9ye5opcG/2FdN49+jw5R6y+gacXj31qBnZ4Qxk0TzyqRBHZk/CMXO1bT1Lj2GYR9tG9jM6oyabuM5fGQ2/fGPu2mD9TiESpG1ANGLCEce15L+PI5zH/NYYMKChV4wL7e68+wQ8lD4zKZYbOMrdKDc8HKL1eXC+3OEsVxoU5xgYSthdoyvNf2MdWdAURbyHfUvcolCWYS4//wBAYlcCDhQUZRBV4GpvDUjE9BqTG29Ce30/o+hDk+6Acs3KVHgApZGC1o29hCYmEIuCiMbGEYJLv0EswZ14J/nC+uLzxFkze9mv6i2SwlOaG2oy9V2kPxTJkTRoBakBlE0SvDddx90DAV/rEcHEA+N2lNGr7r7DfSSeangbxqAzr0TQXTisGNJkIPHxVNmzUJUbzEYaF1wgDhDCoiCghlrMdbar+s37mybitKgnpssiPZX/N8rzPMhB26yB39RWcQ8FuFggxfm2hoZyumEkJc+APH05b3EFMW6RQ0Z75EfAe9iw025aL+iQkxvt1AWJCMkN/X40A4hneB+7T35N7XjwPCc1SP9sgvG+ZcRiANFfcoIsi3DrQ/GHUTexGWzgp08SQZeYLWM2ErXiGnWDpNE/kkg0rjRmqVw2CiARZZKqHuynTP1Kb62cjvz48dLcG0eWr88IJXaydo0YWPEw+KXW2FkRgTbH7f6tE/AxWGsKS5daBcPPlcf5cfYqFW9m8nBCgX6VblLah+x/X0/DRv2srhn0buhNPfKJcUxyUlNblOpbGv69qdinBZJC2+NZ8qyOSX4grmnGk0VkC/zY5yKUgRaulxCIaKtZOEJWZiBzxyLTymQigxgVuWi3jyYThTgN80OFgEKZPpzRLSYO1poLYsn4h+hplnAQPO4+VfpZd4xOocUAYIs1sIXc7q5o3Z5mV1zJJ66ypkPmGuBz69aLyCUgLh0N6VQcAXCT/O/zUNKQOlXVke1DRaZ7jTv2a+bL0svDrASSSWCXxPn39KB0KvTSrHjAEf+OCNZ2mncCQq1lzy07VOlTzXc9qgGxeYnireQvPIWCGI5KLbZw== 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)(376014)(52116014)(366016)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9Z95Z8/SBkvtyb6DFjFzCyeYVpKEg8uCAwvWp8nK1/X6n20r0haXCWV9j76/JQbu6m8ew+TIwfqYqtlFOFFLMDKvB6OYU/MMt7R7o/xMO/mjcSNT5O6M8rCIlUswBJiy1bqxpx0nxAYFYG/IQ42ssQu/ymy5xeto09TxId0owhYuoXIU/3ze8NLMLqAQFiieKOFxnzoDsdCgUlUAz8nPNKdV+aAjgIzNkDiD82STIn0CJocpJaHOKR2LM7b7MFVHmUUDURxDJqgcwodG/d3Z6/TEb4XOGJ6AexAprUvs9ghSkMVuHx65+ouKlVsCju/0unwJjgkU2/3NKIAiPy16tyMiOxzkX3h8Vwp63pUMcG69wUNvMiofT1aDGJV8vxvd4TqkV3bVtlb+4Cm7DvjNNLGppJ9lNZFBNBIlLB7tSZtxLVpywIkLtKrKQcyvSSMKYDNu6VtF7LElyJB60ShLsnE7y0p0My9wUTutPvbZVC3htwgQBMii72IrIKX7Ql6qsJ5RcW0+U/WttXfj5MqBW9zhwt2aH9Y+iYA1zpxKH2eBKnbbcO+si5+kpm/aQbn8YGhqGYFGAS19Xv6JKIWh7BgaYzR+2z2hE7mbxJSCtOe915goxWqbQbwUmz7pFHkfJy90MXojmZAZf5F2u2/2GiGzSfbcZeXtbpMHi4wmaRNLMBea9mw2JO+OqpCNcKPu5fOQCPkKWbgEdMilMW74UdYM3f5b92FD8aV7Za37PRaRjlPDB05eV/nOgho7Ma3HmaIgBqIvoGrmTzVwifRAQejQpwUeYD85WbqnWIkEPq5kxorllBpdwNM2eWG6PRvT61gN8YZU1n78ruI4sixWKgVtWCuhWaG2vthZovdWa5o/nx21gvC/WG8H7+dyIfYDX75VomyYU9+wad2jLKTA2TtLeQ93cE2xZl24OGgPjPq3NE3TgMAURHheSR1e0MHo1jVy+RT2AuyhD9BuYxbB0nr/pHnhkWnjan27KrNQpBfjsELiJSaUIVRMkMeqFIgMRDQze1P6nhFBtblP4ZeAKkgsXGEuLcofpy64Hcbf16xKtbFB9KCWL07ncabFYdwQYeQZGM6F/GKobbL1sycIZphSoXMGvqQTF57cEyIztg04C7TAgluQX4Iyt3C1/6Ax8WSfsSTJzGZiE26ZYvCJgI/m4V30eUowxrsV7dRURc6rFV0WunX/C4bT7EFaOlL6PiNfsNED8P7m0pwXKBChk6a3FsKT2JRV1rzzBQ2Mk3OjaE9gDLeKldHkjlsrX5khfxAl8LzSD5Evk65SS9YRhlng7T6FxnmSuTK7TytETQV5hUXe2x74Zjc0PwHIa7o2//RpZSGsLhXgtwT83E2DSRnva9JSX4zMFo7jyQSwvV/z8Vodyz9dARN8tZpDYF2myzE9S9hqB1E8fvuYqEdQNRjz34U8FJNRgAf7FwYGR4a83yI0y+xQNWWdTUyC4BZN7vuq25HwHo6tPOicFkgNREUx6rCS1p9MyyH5LAgQqy2vcd6ytX4Do40XD4Qd2OPZ22d52ZqGjg/zd/yQhw254JuyGy//YchBD5OXX2Fu9Hw3d8UvJUBPsOoO3S3Dv68ZtnY3+HniJnR2sQ3m0L6naBTvTH74uL8DLFBbGV4RnUQ= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: bac85570-1a6d-4d29-eb1b-08dcc211cd94 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Aug 2024 18:48:10.0652 (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: VQuI1hFl65snWLrii7JU2q7kKCehXSpzo+8ergL5mOylo1cLgI3xa5hs9tZ1VAD1VRLp91V0d1PKHUIbfQa8qgbAUb8v9GzTUWUv8nVCksk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR01MB6146 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240821_114830_715696_7EB48C58 X-CRM114-Status: GOOD ( 17.87 ) 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..c2402e3d6bca --- /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; +}