From patchwork Fri Sep 13 18:34:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13803942 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07522FC6171 for ; Fri, 13 Sep 2024 18:34:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 902948D0005; Fri, 13 Sep 2024 14:34:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 864668D0003; Fri, 13 Sep 2024 14:34:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B7038D0005; Fri, 13 Sep 2024 14:34:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 433898D0003 for ; Fri, 13 Sep 2024 14:34:42 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B88DE4043D for ; Fri, 13 Sep 2024 18:34:41 +0000 (UTC) X-FDA: 82560565962.03.A2EA6E1 Received: from SJ2PR03CU001.outbound.protection.outlook.com (mail-westusazon11022112.outbound.protection.outlook.com [52.101.43.112]) by imf10.hostedemail.com (Postfix) with ESMTP id EF2C4C0007 for ; Fri, 13 Sep 2024 18:34:38 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b=QNSN83Fn; spf=pass (imf10.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.43.112 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726252425; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=SooXbUCPnTkC08Zv/X8+JmCRDZ6femqvNucmjqXycx0=; b=Oh0DXXfAd/61HhtqhwXlceEQbUogL24S29993s5+PGM/Fl2nHXsm6fKxmQif/wJMpFPCOJ Jw/jklcechEbEiVopOL2gP/Ja0TezKSh9MTYkhAPCtogSdDgNQ2t07qC3uB96P9lJdbBFW y7A5EW3lCprY8SCgduC/4mEWEYgTIiI= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b=QNSN83Fn; spf=pass (imf10.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.43.112 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1726252425; a=rsa-sha256; cv=pass; b=gZP65jnb01agQlEO9jjaYKxbQN2Kp+elWdTsffAbH4LKzkJmWxkqzo7fg730giZ2kImrgX 3/djg6y99MUsMXGt3addiVt4ik9H9wz3lNU26v6eTan8U6H+i64jJPKt8DdawDuXpKmY9F cciHPX7Mx68n+Lxw5D68fxIsdOCcU+k= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NLV7980ltA1o8LMbF0gMA60LKOowSOhL9FTOVRMkNdLL472wHYPtYX3FvQE8pmJXY3FwgYZsqD8ukoFYo/k/RzgFxnLji6uzhXrJErDiah658iTk8wAbXYdG/Y9eswzIuc2Fqn6S8Oxt9dhC46xEdXi+6h4nGOsGHriw/QrELQB5DlAYh24/Ahx8CAM9AAdovmDG7SR1htL1CWNt1W0lLjxt2WrHlEjWhMmXkBYn8imML7/U8TpHbw3Di7kqBgYGy+lupic3Cu6NG+Q3fGCh9jpj454Q0bWNtB4gCJXJv61JKuSf3PLUR9oeYY0HBlI4dViFmaUDoFRI9VnU2fyrkQ== 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=SooXbUCPnTkC08Zv/X8+JmCRDZ6femqvNucmjqXycx0=; b=P2guJPhm1+zFhSz6uLs7bfCT/X+tlSgOO1VGHaYJLCP6AyD9iaHDMjvUA4EPPM3jvBqY5rxLEz7U/IyjXYd96qpAlwJog7bX19tjUk4EPlYhrYeYzQeIP5wha3AAI3DLbImyK1U2wwg2Q2iRwhB951a/KJy8fEN7mnbcC9gnCkLSj/OkFzwMKFd1DUpwoVwrxmd9Sw3e3g8YtMwChrx8TeS8P07qKE3jb4TgQtKs9oWomgLc0V4ap3/clB3LZYZPcZloG8AdqVjHzwH9z5baRn0m8sA8xgBPBwXil9YtN96sPVGJSdZcBycOFFCHD7sMCd2JZkaEWNwz8JIqzk2fkg== 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=SooXbUCPnTkC08Zv/X8+JmCRDZ6femqvNucmjqXycx0=; b=QNSN83FnHlaKcm5M3H2gulrJOPSpylA9qPRakAwce63uwvtKZsrNskr0UMHu/ErNCTi0GF2CbHpydpm5zM/OhRwsMSH66cmPEG7fbdt9cE0q0KsiSaUSarae61UUjx0SFKqdv9vwP+Ij4glmcPa6bRnzyiIfQnDkwkYS5v1EnVk= Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by MW4PR01MB6114.prod.exchangelabs.com (2603:10b6:303:79::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.18; Fri, 13 Sep 2024 18:34:19 +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; Fri, 13 Sep 2024 18:34:19 +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: [v5 PATCH 1/2] hugetlb: arm64: add mte support Date: Fri, 13 Sep 2024 11:34:03 -0700 Message-ID: <20240913183404.3517556-1-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 X-ClientProxiedBy: CH0PR03CA0333.namprd03.prod.outlook.com (2603:10b6:610:11a::7) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|MW4PR01MB6114:EE_ X-MS-Office365-Filtering-Correlation-Id: 54dfbfe4-04d4-483d-4126-08dcd422adf6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|1800799024|376014|366016|38350700014; X-Microsoft-Antispam-Message-Info: lY+R9VRNbTO0H8imWR8HYSRCLLZgXgFmWOIefTuPPikgcCIITN1DG2eVXfIXn4Csbv8YVOQPnsrlCwnDCMtkIIWN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5RyM0Pks4IjTbukx97A1WtVA3XNfsiFGfiun7q2JPKZtkYFPA1xCtu3a9k/8LmsNdx7EE/EViyRGbXmq/Uu0mADxImKP3wOe7HOpk318/ljxWrVUs2cbuJdK3srIEsZ5s1OMFjIIRLoUNJvlpsydCtLcfsEsao6IVoNezE2m7sfg8JzlBAuC7zVX4L2gDwh8G1Herh67q58SWUwrhNX5V8vg+8AghSxEgp3GEkRmDRADvGDGbm3tGOuwQ== 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)(52116014)(1800799024)(376014)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jQVaaa/agv/PHNCmOGwVa61TKkuywM9LJH+2XTrs7QepWFc/joenzk5e+mrGOC7iH97Pq+JZmTXpSuDxgQ98s/rTqogjEfEMURpj9IAhE0yaEtsJMuaKOnnizpuTP0r8HV05TEGDfVMpX8wPkSuP0FW2Gb5/IkpOGwWwuRZTnjA9FJR1z4UgD155yct601lXaYZOQEDjhtW3rpa/qVrsk0McxHz87QrfpNhu7kLknjPB7l0PrGbLGKHEEhGxxAcCLZXBvdRPWFjy7LGAfXn90N9qPY+PECpWbxPnC2tdYpbYvY+0Rx+mn0cgwnze/oxw7NIdnnsrqBqLZWLfJwM6rs7+0Zb98ZrxZOVm17R6GJCLcB382XPMGOl7LXk8UY47QEzWRtJaQVEOBN1GNLu4cNKnKx0hBxKTn+xYzzT9DquUdJT4LY+eBJM3HINXH3N0zQtLcQLp1q8bw4giiZepXR4EqWcn6NpUfQekp+g9eqd2+bC02eJlUi867mjZxRbDNNEU78vzXaefbh0KhdIK3HYyfS2HFz2Iwc8wsW2PA8RNppH6dPQawUQIFlKksquIo5X2hGT1jAVuU0mbXyF1akyARCZTPvHnz2T2w8x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-Message-Id: 54dfbfe4-04d4-483d-4126-08dcd422adf6 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Sep 2024 18:34:19.4218 (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: urS81oeCZzi0z2xX8aasiFHupMDH6cwJMLhsEZkXuPXiNO/ud2ZvN6aUGJS37itqm09wvk8d5hXEfObEHF4tQIioYe6k+E/E/ZR08aEDtLc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR01MB6114 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: EF2C4C0007 X-Stat-Signature: 9sw94pt1gj4wrp4xx4mmiqu1cojwazkb X-HE-Tag: 1726252478-252239 X-HE-Meta: U2FsdGVkX1+OE3yidxpvqyrsgFyj0s0VsKooSKT1wnL/etVxXtVUh9Vwe1nR/PEEXLdx9rUqcbaemCRi1VsQ1ZCTp1b0yOKfeLq5FfgbXTrkDqIPdpJJnS5Cn/Ppa4Zxzqs6GBmvi4gRO/PVq7+J6LmAVf1J/pBUDkkjJWGxElqvfFjhJNFg38G+7qhT2T2TcHjzHuE+h+j8QmxK7H3S8v7Mus4t4GRlHZ5RGySd+kM7GWWSv7XG32+3GELNZ0ThYaPSoplI0jHhk/K9sEwlB80FPGNQn+8CLbNa+qfTjWrk95ZLbsDkdQVZoN4rkodtB5upfB7pgTf2enok+IGNWWsP0ghnVt18mo84EXRoTNGBH3uyBUuE1gnuZEdzVhQ20dSXKKCcZ5Mu7IOyR0fmYofz2e4YID3JnPxVrIWr1IwuArjTwG6zxmQYwzL1HJYUZ1OX9hyIsep4ElDlmrg4KqxA2iVamwaoAS2q04QlJTY9FP7wrEwcAxIaAs0eOQmGq3LI2Z6JU5CqiYa5T7OqlyU4U3a99p1l4xLa269umkkMeo46CTx22/DAJlFNJ96CyaDBMDY2L1uLQ+oJrodVqpf+xswI3kijiqQ5Fumfc7q5k/FSjonxJlGkW+Dwumv2TI+MlCxJ1ltN3cGcgRg2h3bFv6RTgbjs6mRDp+E8R8zWVnbWY+mXSsWKz/XvQMzwdptSp4aeaSa2lAyHX01ajPI8KzERdVAwBreJD7eXxoC9bwIzWE5tStkc8kgNs5bcN2CInGX16Y/qsnrEE0hj2Ybqb01ifCKv//tyiTI7pKJskWwVCup6lqmTIjRPy6AEGIqI7uSnPxI/UTjCJ8FCHZcrXCHQJnjJCFwj9e7yZnboI/D8B43YQcJ2voTEXrQpjUMiwNgATYgjkWMClE5QhzLxisoQjjIii/uQdyb691HWzhVwOIia6eD/C2SQ7009VOAtwhhrDIECztG5zbA ADsiCUdc N7NKe5r71ayld0jDSN2KxXrYnKTr14Cg6K3QmH/pVfp7O8gBo5Q0xMJCVPUWNBZoKaiNLQd5/6eqmdHnooQSasQlioXCNsJas3+JnFfIelKNLtsAlBu/jIcdxGuMCW5g6okBdiMlyfe3ijhsFmULMxjqf+Yo2m0TQx7Aas/TDZ3LD+HIFa2dtpQffyISjG0kpOj/e82b3RHXTSOfCnes54SF8Z2tzS7MoC991rHi4e4GK+sc3y2J8nqrAo5g3kos1tcIaaPbdNhcjFWYFFx+eVrwdRGPWhOyg48D6Zuvocnjc1m2/8bMdZcbkEq0oHBBpsv58VqhuphDNk1Il57rUhjGkng== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Reviewed-by: Catalin Marinas Reviewed-by: David Hildenbrand --- 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 | 27 ++++++++++++- fs/hugetlbfs/inode.c | 2 +- 9 files changed, 159 insertions(+), 8 deletions(-) v5: * Indentation fix and renaming per Catalin. 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..03dc43636aba 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 folio_try_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 folio_try_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..2fbfd27ff5f2 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 (folio_try_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..ed9b3f31f3ab 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)) + folio_try_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..962449f9ac2f 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 (folio_try_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..87b3f1a25535 100644 --- a/arch/arm64/mm/copypage.c +++ b/arch/arm64/mm/copypage.c @@ -18,15 +18,40 @@ 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)) { + if (!system_supports_mte()) + return; + + if (folio_test_hugetlb(src) && + folio_test_hugetlb_mte_tagged(src)) { + if (!folio_try_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); } 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 Fri Sep 13 18:34:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13803941 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0692FC616C for ; Fri, 13 Sep 2024 18:34:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 691576B00C5; Fri, 13 Sep 2024 14:34:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 667F96B00C6; Fri, 13 Sep 2024 14:34:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BB746B00C7; Fri, 13 Sep 2024 14:34:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 27BC96B00C5 for ; Fri, 13 Sep 2024 14:34:28 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D73D71C4547 for ; Fri, 13 Sep 2024 18:34:27 +0000 (UTC) X-FDA: 82560565374.13.C0794EB Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11020126.outbound.protection.outlook.com [52.101.56.126]) by imf05.hostedemail.com (Postfix) with ESMTP id 0D7D010000C for ; Fri, 13 Sep 2024 18:34:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b="Zrtx9oV/"; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf05.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.56.126 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1726252435; a=rsa-sha256; cv=pass; b=NEeGsA4x8Ki/rZrzdXUmFzRs0U/BZ1W1X7MgJ2VwYQc01iO2h6X46PfSFtE+dPzZI7/wSx OoGbZSqpsxmDtpD20UQdSn+BFEJy/gZswgZ+PfrKQwjqr4eEJJBCKIDm/eTuPoYJXCBJVo LKBbpZ27b4kzA4+ZXyY5jWrgv2H02PM= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b="Zrtx9oV/"; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf05.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.56.126 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726252435; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rZEmI5ze4VGJmlbeh/nIhqSgSXgQnJWJLInFVw0UZe0=; b=TEjirujyyRU/5R2EESojh6QFFrlFJJ+6greeReeDEcFy9+w7kGWiWxVjju98MQjJ2WPxxD 7lVs9E1liPeGEg0RVXSK2ioMTmaMYs7Rn6v1XRg23SOIJ01EUQqMmSnLbBbidHUWYeoRHi VJBD9s32mybj9sWX1DdqdYPkWvTInB0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JmCF7dvR/FjCV1qwub/s9tNXRRmjtlzkSWB3Tvsy10YUkXgJgKJ5aMBwCG7qrYj7yaOlGza3wPwhRcggYo1d+i3rmpsheMHGzO7DpIb3HpTHQDT6qtXNRrLM1nVO49CHjcHuk53Eb5kpjgbbZtkM9jsofHUdUJ4n6pmxAhGuxocis5srinMg+QLn+2YB56l/E9C132rrN14TSzwuIgdgMrOnf/+DG1qTmiNt/v6uk2eAnPyC4vDiKh15hqyMFv1hjDHDwd5rOausaVlslUR3r3TnfG9WkLofNLvuWdEHMc2xOVNP3CkQK4Amn2H80IFEGgu/aqtawXDN/MlmHC35hQ== 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=bbcmDfQNrtiLOk2r054DFmady/Wf6hZ2oKFKrLkxtz1ySVxUThWYp449EOUcfGp7cZSHQGNj1qCdH6PfG2xdw+9255rgFqoIZD9IzMcQAlIH2RJ0TUJrZXbDgPuoMhqMEe1meYDdA44j8Hvw3zt0XuZGvnKOcE+jm14AWkX41/FI6PJArXzgGCgwJqP4PXHqBRSyRPmIULjg32C22s3N/laMDj6itCZeMhvj1Hl38AfMgaKc3x9O18tx6GRKUZHstuPcCzcQibu07qQALFm7pkNsLbb5HywTmMIFdbqeHKBr2GLxVgmfoBeU7J4nfyd3jQo/Fi/GE2urzZIPGgcNjQ== 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=Zrtx9oV/vfHz0onxikINXskZw6YlN/4gZN6DZFpmInASun7YB2HVK3BKEgp4i72TXysO+BTAIhQDiucraSHtuQe+07rlfr/0NzKfJPgrjoBuHIC1WM6WBdrOxGYfpKFfoNZamYBJEhxl4OXmOHMxaLWyXXv6dsnVON+sMAW8lAk= Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by MW4PR01MB6114.prod.exchangelabs.com (2603:10b6:303:79::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.18; Fri, 13 Sep 2024 18:34:20 +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; Fri, 13 Sep 2024 18:34:20 +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: [v5 PATCH 2/2] selftests: arm64: add hugetlb mte tests Date: Fri, 13 Sep 2024 11:34:04 -0700 Message-ID: <20240913183404.3517556-2-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240913183404.3517556-1-yang@os.amperecomputing.com> References: <20240913183404.3517556-1-yang@os.amperecomputing.com> X-ClientProxiedBy: CH0PR03CA0333.namprd03.prod.outlook.com (2603:10b6:610:11a::7) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|MW4PR01MB6114:EE_ X-MS-Office365-Filtering-Correlation-Id: 19ca3337-9482-4581-d717-08dcd422ae81 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|1800799024|376014|366016|38350700014; X-Microsoft-Antispam-Message-Info: a+3a74xYbR8uKRx10orEXaxyAnKHk++WiPZ7sxiIdIb2ZgON3WCHGjaCdhKP8AwcOAwAbMWUWuYuyXWnPteFCiMVHsqtieMBpHg8cSRJ3QHrgJV/wP0UNBWQDDGyThPMJYm5GJl1rweJpOjEEVxf3tHdj80Csh7uQ63TggJsxLb23/OSNGlUpYCSNPtL9xvSSn7eUQwSs8F/UN9lZRXQJ84WvgAuuH871u8dEeGZtdi7oPjVTAYZLl/t1y6Su4jRnifzeM3a02Mz9+T2xlb9NAaMvfWfvACNBcT9iGSjdLQ9hZAId0YzWGybo+cOQoqTK241ve6G30t0hXQwH6eWHkkWjbEymoMFxT6k9Sep9RfsQKEFIUXf8cvWO4bUfx/xagKUwrX8RncLOuW+VGoIAeYAJoDfhVehpWZxIRSFfHSdF8n7WFyE2Yxkoh7oGOjl9T6B/1our2oK2dBNZTh7UcexDKe7xfGjoyqIR+FgYsE60k/36zSEZI9a42Gt85sZOkNRQhoXWrI1zmAe7xrqJxFT7XJwcQwplMdngHTwt5wY+1uet5hxOILxZIgDSaKmVUEjp262pmkugqo1vsij33nFX1WOOPr4rhDNsoSyBSTmmS0J38mkYJ47xY6BkGgxW9nZ/6UPcGNcrA29Ihug0YUJgiVrzprGKJZtwifePm8vVj7PxwD1HC+pNdbTvpy1bYBijEMTCosqc8TbXljg1c4OIHGk50+DNuBtyB4GDZ+CCiPBeWGHDSeHOteNgCCbiRV3HK+YJzOCF6mP2XMi/Um76/1GbUJfQOrHtd6PzmmdXOzRjba/BTZvGNrrMhiSVfV0yUMA8PGgXsrPri+KTd65gz5UopmeBZqhgyvT5mPgFUa8Ju9My7pGTh78MjpP8mohoDdWUwdv6u9tduHChj8A/Gj5gysaZl5RB9GrpU9MmW2sb9UOey21dGcGjE6mJJ6UG1ZxSYw1fD88UgmeDC/PI8Cq49QWG6eU1iGpSL7uULbR4IX5tZ+imNoUqBywcgqzFSSGcoiYKryjrjM8zumSwNtSmd4+D2HtDg49YSGSBgEnJVfrnWI0bYDxoaovwhz5wrK7Xb+LR8i24EtkI7uNmTahqQnSguB3Ht/D8+E86QQezJe7G97SwR/rpDgkJD1XzPJxfDjLzWx7GwVWL6hyhWMAEmCbzRfylKCNiynauNotnHLdMdVkiduq8Lw9gmFoZ6Crc4GTzt5NFnBCNZCa0M5jvPsgjnUeLGf0MSucbUHPXd8UjG+Mn4rXAmP3MF+SFMnUgZl5hvHDtgxeuVyXKP3gH+wAkXgbPx26jHv+PVlTOnfmDlPtYVkAzuvzEJVFO/b3j0upzUpkbJCHtTnRNlLaV/FgcbKI+SlDHBlHJcgLpvdtXGVu8fTc/JntwGuUgHSDzgjxDkrhDrgIcA== 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)(52116014)(1800799024)(376014)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-Message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sXj13/eAQ8T6lDCbUdMv8s4SpIdPlKWVdnaK94EJl4IX8UWLweTllltzDfU99DCGKwn4I5AvZMYNfphuBKRnXhHLpdyrRMAoPaF8FekO458pFP4FKSdFKZ90p0BXhjWjPgktP0SO832LElpC72YAE2kBVIQN4pZ2el3y4mRZwfiwocTE8Ip/NKR4Z9HKT8/gf1OnmBLbn8Z1qD/FrkT6W+wl2DaFWLEe+8Lb+eZZSokIEP/PHLzlrdCRZII73rzrjy/xEfi6HDf5zWKgd8oYflnFgBoaLKP/LPbZlDV1VHrmhGvXKFT3Ul3jO8oAKSP7OITe7MmKoD8yM2uLozZYpx6plk3QhEFMF8pX26/GsxFQw4Pt95qfhZ57SHB97vrMWZcqNBTtzwbco0ugHvxTGhzaQ+KX0s8OMzrgr3OcuL+zh/iXgMs6EeQMkSNDZbsS4sQ36PxB41+qPKqUmdNj0bmRRZ4BQI7FBBlFTVh3hhpnqL1SnJ4jsDr2IoDmV5ApFRBFSnbSoHhHBBBe1oVLaKeG4TcCNTZH8DAi+1PB66uoRSSJTG+jzYzoSK2yRZ2y9Uhbx9R/fJ3n/g5WaLykapFd5KQlrMz1iU= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 19ca3337-9482-4581-d717-08dcd422ae81 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Sep 2024 18:34:20.2535 (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: zdfgUUTmRlq0U4Ilaj7gw/h0xXCBbwjr+KHrR8Ab6wnUVwqp/4bvUtmap9KeYDBI5u3bdTguF2NwIZ47ucrBUmxbyXqavWzOoNzcXTYY+zg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR01MB6114 X-Rspam-User: X-Rspamd-Queue-Id: 0D7D010000C X-Rspamd-Server: rspam01 X-Stat-Signature: hyxwqm91x6h1693k98xkpd1u8ns8dqh7 X-HE-Tag: 1726252463-839123 X-HE-Meta: U2FsdGVkX1/EOCPwDMevS2a6ANHspNe7po+7JJr/0MEzRequuxGxmyvyVGzLuMPDBlG43f9MbhwiCN1cMQEEFkpIblLwH8TVUc/+EnN+4XHf2IOfsqAvlBGgvxzH9bEz6eoaPCnGiB5AXCFWH/yTvl1iIqW5Gj67x3f6QXWV4Q4i13ffWaDDgUdp/9DPb3Vvk0aB+zWI+666WAcMXN5adeX9+SGvqohuvcZz8sFlFybmB/Ob+rGINZ1pJ+N/qwDXeMOky9O8ulRBHsrc2T0yu2NDf6A1WMJ5RD197NAYEPq3AFxLx0ISiYTMQdZPqc4N2ifQbty5g3Ffl11VAVhm3BfkFgad35YNE37o/VlcNBDxtVPZj0vwFLJl4MXCyg6iO8ppeQVwFxYa0MBftBKDXKfDFMf3KfSA5pseFuq0EODRfzIODoaLE3oTnMxlecdfEQxW04tv2lN1n2v6UhjOv8a+3KMaqVt9NoTiGKXz+ZgXoGz0037lN/o0yKZVdqYnOkxswlR2qjFOYE9XgIeh9JRFpkGMn0Sema6Gf0idv/N7WYlEdhh/c1z2ioonVWLABtJSPEM41ColFwWiKVtlqHMs5KFc1jPv5ogthc44m4eCi38LKM0AtowJ/jaDgOcxLznQE9cuIZFnVAvZyfweDzHAxmYYmRgleHsG0QMEXCVVJmv61la6iGwrlWgit51JLuneCznzaxVIvxZgah2wVf/LEf3BIZvsJo4FdW3MceTI9MYtX6UFGzMPRFqrLSiY80Ld6e9kMEoo0QxM68IAJpMjFiOK6/tT5WePAUWxlD2vzREJdHRQXf1lci72lO7JyK8Jomi1VXE6iHEtSlj9JLBQg7meEmPQR0EIAPoE1uu6JltfOfj3ZEZWg5lRR7bhKtCua5WdmaxVEKerpvURNlNX+OkikcQmqN6FWye2GG8T7tx2RbgvLLcM2yV/Lc7FrcAUA5vVRpeFJJxIaPA 119IrTwU MIR41O3Db5DC74/Q5wrKeWhQuGPtyalxOmgBqXTR7mNtrEjoCKgKjsWyy64fo573HT0gsiIZIcxkTtGQerazxPMcWmbdO/1S8DWpgueL4RocUGB4joYbhzvbQ6IjjcdhCP8igSGobbFJ2Oqrm8gi1a6n0UNgb4cD5EbicuFq3nv0ahbEN6oeVOpQD6HMjPDDf8lxEBwnHjK8WApKdQcwSsco2etOxQGKjDpE8vcyNjaZDoRN4e5jzLNdz62gpBFl3RzEfNSosmNqlUkSTzqqcv/PRZhhn+z/2DB3+8maBxt8GAzzCKnRPMJQkcsAH1aMhll31+lwZez+D3ATp6I3t87WDXZfYBAO8Po6wgp6Y8L5QMBJ1gigt36TXTw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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; +}