From patchwork Tue Oct 1 22:52:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13818835 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 C9B52CF318A for ; Tue, 1 Oct 2024 22:52:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5732668002C; Tue, 1 Oct 2024 18:52:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 521EF68002B; Tue, 1 Oct 2024 18:52:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3276168002C; Tue, 1 Oct 2024 18:52:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0BC1068002B for ; Tue, 1 Oct 2024 18:52:44 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B4492120564 for ; Tue, 1 Oct 2024 22:52:43 +0000 (UTC) X-FDA: 82626534606.22.9C20F98 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11022086.outbound.protection.outlook.com [40.93.195.86]) by imf19.hostedemail.com (Postfix) with ESMTP id C6A611A000D for ; Tue, 1 Oct 2024 22:52:40 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b=t+DNRTY0; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf19.hostedemail.com: domain of yang@os.amperecomputing.com designates 40.93.195.86 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=1727823020; 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=91Y4a8uHClU48nQ82l2w0d0MIr7yP0j6WBM6QG1+YLM=; b=XPVa9I+pCgdVfbXXfJpJKZ+IOYt1gLt6wDtRGs/fMJ2J+1N3RGP6sWGjai2IUpF0u/V36g 6QCL/9gVMkB/dPyLe9762z7STd1t11/pk5eTIOp24ElHDbPv40zuUIrM3b9m/MdxX8XcyA B6VAG59x/qQhYufwC6pnqGTDkUisUwE= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1727823020; a=rsa-sha256; cv=pass; b=CUphGOnHQ/TppGoZay5jJbA7uDBxhzNg30M6dF/Qgc/bKGOUdgh4r9hFcRCPP3fdmjtCrY 39xsXote4EX/GWGs3fqaHhbe88NxIqqRxEcn2Beg2+pm3+FsKo/jvXEpYGWTOCi28fy+GZ kVuo1Ro9AP0PDtFScSGVLzf3xllvdGg= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b=t+DNRTY0; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf19.hostedemail.com: domain of yang@os.amperecomputing.com designates 40.93.195.86 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eO0+U51Mozk+bRqldTggeK8edGNN7/WrX8K+74KQ2axCDjJ52TacQolSB90f7bIr8PJo4bYsHTLaK5D+pgE6liXLyaiA3qe85e8twu3TrHrqqLpaLx7CHoM4hks/T5mHdsodcV1Sn/zbrbvnnEz0GufpuT5rnkYwfA97Tz9MPaSBp5I3qWsQ70+Yzx61oUUYhDo0nnBjetxP82kTG0pozOt3Jt1XprYMcwIejpTnpHv7I8y/T6UuYFtMdgL24ofK+HTerjMkcV5azlWDsXvNpDerOSSNyU5ZPlZHPusK297JzpeQxAe44oYJjcLubf39opcfsNvUaWnd9+2lJKNzhA== 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=91Y4a8uHClU48nQ82l2w0d0MIr7yP0j6WBM6QG1+YLM=; b=oZh8wQUslEGlWsGK6pXYfTfDfqPXNQwA2/loj/vc0mLODV4ZqTig3DZGSBg1sMxvdzj0RWGwiL7mXzO3BXQfgaHjSLqtxXD9zUaLSMTfLRE3Acia0eIX6spJ/6MUQP9URJtw6aZi1dQr/B5c5jksbxJRsnQZc0IYaZUm30SWPHuLKHCPs0WUX7QeaoQUGsDw5VJTZQtYhRrZGc6+LsT5D1IKQ1Znvnd3s3BaA2JjIKEvGGGen17NE1wrC4q3DUsHdqLzAl7dRul2gDPzHudmoZ/5XQoLXQ5EIoJ4jkoNsk2SDFHTfiZ4idATiBGdwKiyW3sF2Lwjdz28yUmCoRPADA== 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=91Y4a8uHClU48nQ82l2w0d0MIr7yP0j6WBM6QG1+YLM=; b=t+DNRTY0j1aUWnnXLMr1Y1WJ14WbQfmbl7BpNu1X0x2QftkOlz1D8JlrTkpd2XxzQ3i5UIW5FqFWZnASv0s0jzQWiiM8TkLbKg4OUu3nwuysVii5GK80ldER7PeX0KK7SkZafgsirT/2uSNmIhtMuMToftCfz11riZOTe1zdrtw= Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by CO1PR01MB6678.prod.exchangelabs.com (2603:10b6:303:d9::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7982.28; Tue, 1 Oct 2024 22:52:35 +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.7982.022; Tue, 1 Oct 2024 22:52:34 +0000 From: Yang Shi To: catalin.marinas@arm.com, will@kernel.org, muchun.song@linux.dev, david@redhat.com, akpm@linux-foundation.org, shuah@kernel.org Cc: yang@os.amperecomputing.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [v6 PATCH 1/2] hugetlb: arm64: add mte support Date: Tue, 1 Oct 2024 15:52:19 -0700 Message-ID: <20241001225220.271178-1-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 X-ClientProxiedBy: CY8PR19CA0036.namprd19.prod.outlook.com (2603:10b6:930:6::29) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|CO1PR01MB6678:EE_ X-MS-Office365-Filtering-Correlation-Id: a64e8e08-3094-4e4f-e987-08dce26bbd00 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|7416014|1800799024|52116014|38350700014; X-Microsoft-Antispam-Message-Info: 9Jyc5QHe4drhBfp/1sMVkal1FPHVqqJJ83wimuG7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uN1Tdu8UVM5trgIBamzZHCdHZUBZhyyQVN+lR9OeoKXs6AqRopknCONV4h9MgBibGJoKAq6XOhdsVoMmaHNUdVb2q8lgdMgIim7yOwzlZd0hPCaA0LsrClSIBMIsdVB10fVNdotvKORgcC7LjBs9gb1ZSbxwlMq5qoSOCflm9mHmVr8NGmr35ZhcBlNA+lT8JAm703QgYUZR0UZpQCU1QMNMPM6+4XoNJO0xJkW+4znWybOLQqTIAyDHQ== 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)(376014)(7416014)(1800799024)(52116014)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: i0peh2zsuqvxdELlle3ddQs08k83wIGh4c5Zw7p1DkHxfA2Yc6vX/D6DyEIRpE+WZfESrUE/qjJ0GJLVoKWY6U5ly7vG9EIKHAS70r53CdXVFdEgYm53GPSAWwGWUivHhdbu9ABWugTqJ7+9Cve/2RHIzCmC0l8tKec01DTmr3vek5GZBbC+pH5t4WdirZJgXwhWfvvEIoUqFIdX44dycK40imSYAbmPiqfHciMOwatfkbTHSPhG+ZSPPu/JmMOtTn7N7Vx4E/75tUJ+s3BPFJjzLAsF5Yt0TpHqiUUy9r6Ae8G7QzAQR86BuugjJznhFnft81PmCQQ/wfjQeCto64x1gM759QcqYm3iA84+mkb3y6M6oXHVoyrnYSuTxyUdKQTlKp5KRRnjLyG5mrtImvQGlVBIZxy43Uyk2Qkd3DYmnz4U8kfM+/Z26WyJzpaMcRlxOvQE/AuIsIekEqb8QdivkOHKdq7N3S1HH1bCHiEQTbnKhPcurzzgttpIyVJ3EdPHH52Cx5c8hLvFIoQ+fkJ3uqsyttIAhIrMbO92Nk6jkHSLPzAH6zPqx9f+twqVpO+CafmM3RrDI1PvHRVT/F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rossTenant-Network-Message-Id: a64e8e08-3094-4e4f-e987-08dce26bbd00 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Oct 2024 22:52:34.5358 (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: KgJT56AZHrsNDIAFB/lPhftovu4930b2ko6h2iPvfZJ9w70IJSKhJY+8kqETdiutA4eyqpI3dPux9NVuVTbO8yvnlQ+9BTDaC3mGzvwu3JQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR01MB6678 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C6A611A000D X-Stat-Signature: zy467ykitxpuqegqwadndndcwyrbfqpy X-Rspam-User: X-HE-Tag: 1727823160-39480 X-HE-Meta: U2FsdGVkX1+LgHqnu9nZGtporaF7Z3UhGsjgG1h0rw4nWRUZHxOsmUCeCdgqQU+74v+kpui7GEO2sltsqotRCjpUXJSGuhJZ2Lr0IJ/R1HkwsvyZFwa2f2AkphyXQjROk8/DNkF4nAwbJ+RAqnNiX42tFy64/pRjIACrPPc8PTfCzgIjBcCHt7q3LM4MIxPznYl1eXqVt3toGhtrTtz55crukXoNP+ZQiwd27t6bEFwclAJC2XEVHgM7OvDCj0SBcsvZsFvXZcgQ046yo9cGA8n+x+UTM97ZeDiVNmsII3pQHAJYxFf/6AxxHvnBKSolXG1NOA6TBNXCVj2NA1c+cznK/w/SEOLSq8y1dxnFhzniHM58/PFOzNnR2gucQoDLwtJOti3gaZ2MzI+iIcEefTU1Zi4twllD+AwolQ+p09xtV1n8PsIKxXJfckbxJpST9iLb36DbjQoWXyZfy/HfZt/l1ftdDzpZ+KiaR3NtwKTPQoRo/BXc/R7L7KsCPS2cVPNdA5BDaK0cm69YeHr93pzVY96CXrPL/m4QCQ7kYhKvHJ8sQnK76MvQRDgP2MyGbzVy376aOKbOmZVuuMVpUM6xuHV6GxkvPieFNsioK0yQLBnoR+bM7/UMujPQ8vu1AzOw85H7kaiiVXuDoEVTATxXiKiFxUjob4jAEUoiYJEXmbcOx6xJOSp7bXS3WlVFJglMr3Sg8u6pqI9z+EX3RiJ7KhwtBM0ukDmmJ86tIIW2tzYthHyN7t63Y0qDNQai2+1YY/QJBRFWampRsnbnpTYCR5uQKaax3htvUcBxc4l9miiDUgiaRnGbTBMRTXc/tiHdGfCnDZYIpZFMM45a2RE1/egoeu9j7SSsv2gyoocGHxZZSjs1qWNiYu+f6iXl+UOs730kvAAwXxg4XbGNYl+auWfStirSS+TtMVi4onwf2/nlMHy1fQQpTr6L3yJ1OyC8BJVK3Hy6o2dcRg2 uvJTNrrX pgkcH+zNHA6k3RPUoVQKrY5syDjzdgZzf1pIJXih8e2HXxNYCns9Q6mjA2EmqA2x7cDQiucKnMegrlkXzd/6fxFNZa5WHVrcgjD4FpDJIVpZ7iLC8PkEHxrxDBpAWo9/8aYiUhwKYD/+jQTiv1QSfn5HTeMjs4duPQwqGDX0AMppM9aK4XRDHqhCJSNzZ+qTMwRT1RWPyGgS8eQf/MUC0ZN/qIXA8bYr939Be/CMwjjScLak8beMMQM8N8SY8kTIi3ulctmq2T1oQ4DgaiRSbniIeP49PCEXn127MxsLoydYbonMZlX9g2PryyYUYuM1v2TAkOiN/01802VT58LYrMr8UfmBUItd+F7EOv6hIdTw75yuph+UBY3FeJnTRsa7x8LYe9y6d5oyw683C8w37RS8HLntf9zxfo2zq42G8QAPNB6ANMnTZlS9k7lDT7wDzfduIYUqsxOvfg7d0M4sVXq17LxKjWGDFjQpyE0Q/97LctTpevfxabC6Xxg== 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. Reviewed-by: Catalin Marinas Reviewed-by: David Hildenbrand 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 | 27 ++++++++++++- fs/hugetlbfs/inode.c | 2 +- 9 files changed, 159 insertions(+), 8 deletions(-) v6: * Rebased onto v6.12-rc1. * Fixed the trivial comments from David. * Collected the review tags from Catalin and David. 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 9e39217b4afb..65bc2b07f666 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -38,7 +38,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..6567df8ec8ca 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. @@ -53,6 +55,8 @@ static inline bool page_mte_tagged(struct page *page) { bool ret = test_bit(PG_mte_tagged, &page->flags); + VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page))); + /* * If the page is tagged, ensure ordering with a likely subsequent * read of the tags. @@ -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) +{ + bool ret = test_bit(PG_mte_tagged, &folio->flags); + + VM_WARN_ON_ONCE(!folio_test_hugetlb(folio)); + + /* + * 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 7b11d84f533c..18749e9a6c2d 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 962f985977c2..e738a353b20e 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1055,6 +1055,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; @@ -1068,10 +1069,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 @@ -1085,14 +1089,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 5cf327337e22..f26b3b53d7de 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 Tue Oct 1 22:52:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13818836 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 37F9ACF3189 for ; Tue, 1 Oct 2024 22:52:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB4E468002D; Tue, 1 Oct 2024 18:52:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B651068002B; Tue, 1 Oct 2024 18:52:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98FC568002D; Tue, 1 Oct 2024 18:52:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7412468002B for ; Tue, 1 Oct 2024 18:52:58 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F3257120255 for ; Tue, 1 Oct 2024 22:52:57 +0000 (UTC) X-FDA: 82626535194.22.DF43ACE Received: from BN1PR04CU002.outbound.protection.outlook.com (mail-eastus2azon11020137.outbound.protection.outlook.com [52.101.56.137]) by imf23.hostedemail.com (Postfix) with ESMTP id 2A15C140007 for ; Tue, 1 Oct 2024 22:52:54 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b="P/EYlvK5"; arc=pass ("microsoft.com:s=arcselector10001:i=1"); dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf23.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.56.137 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1727823071; a=rsa-sha256; cv=pass; b=jNUQsv5bDJNQGpzsfg73HaG8xwQUTjBYXuNOgc1tyzJ0nldJbo+hUJmjvyiSWanyA2cerG 19zR31kq39/ALzQU1oLDs5z7Jau9sUW0GdzkSA99jFGAmz63767HIfJBh+xTRESQwbmJFV lDcAByLdiOa0lTZD+dqUXzSZDh+XKpM= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b="P/EYlvK5"; arc=pass ("microsoft.com:s=arcselector10001:i=1"); dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf23.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.56.137 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727823071; 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=dI9ssRDsofkVenyy1qZXmvxLIPiZiOs2/gGfyLvRrNOIgScxXG2Wjd+vIdHRF6aC+OPlBR SYEXzBpOH6O/B38yoLDHjsiZnR5DOiqXxMnAfPDNSLoEOVA5s6lyAZkcTKUxRlF7B0tQ2y ieEeMBawrj1/HtEHlwYPTnNNCuXdMDg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Aqo/ZVaPpOEmPKrIeJKCuson7pBhNjJ5nB2lE3CswpeyvyxRlp4ugZgf3WlptU7Ir5FjE8mcfm9jkLU+4eAoNvdxCZoVB4nvA8ukHywYxCj3uPEUJVMw+MKo9/Sace9JtZ1LhEroHiop7D1xnaonGsBQjQlcRc730gHSwLSKlRmIjhqkMaA3tFEwM1aqLeYLkOjfbKclTczge27JprMx4kKQooz2eduNPrf1ek4c/jI7S7nGiacnjKQdK9SbpWsSJW6CNbqlcWF+R2ZkAbaKvBffgTINp814ciVHLvBuPfAIwO8Dnz8F8nAPGp4Yete1kezfmawPPoUf1/3uztAKKA== 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=ZcVCTO0r7QaNOJaypuYXgX6+XTLigvl8nHYqjvKrHtHRdyH8bUbty3UG1jEcDepSpXkM++WhvY1YG+M3jaFQkJ+SMI8KojG7Gaq9SnUWsXH1ul/6yHaQdJgNaYSHGhGcaugNz4ciM31AIMnBqRMKGtDDAhc6/YIwORc1cWn8anjAVWhairDxE5a4VYlXV35qLWDSM12g4Xt/ekWkT6WIpWb6Gmx8zjMebKAMmUwgDg2Yirv+QZTzOirTWYUJvYcXeG6X9Tstjjf/YYLNjJ6VMZ8QFEKTNjSDZbB4v8NegNP+SA8qLnn33Hn8NbHaxD47kXkoEilQEWTeA4HQ2JkOwg== 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=P/EYlvK5qf7nqMoQR/4Zts9l9tYcC1G8uOCpXlJ8j7DoiUAbFR7FE9ulqFGLZnONmKM3Pj2MpKI9iRsCmUJMKH1DmD1j+nV04sQibSyw+60TIRKWqXC1KZ2wUi+pOe5bUZu2o6v8QU+CkGA/2TW26IernHnudUSxkRR1rX4t2U8= Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by CO1PR01MB6678.prod.exchangelabs.com (2603:10b6:303:d9::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7982.28; Tue, 1 Oct 2024 22:52:36 +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.7982.022; Tue, 1 Oct 2024 22:52:36 +0000 From: Yang Shi To: catalin.marinas@arm.com, will@kernel.org, muchun.song@linux.dev, david@redhat.com, akpm@linux-foundation.org, shuah@kernel.org Cc: yang@os.amperecomputing.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [v6 PATCH 2/2] selftests: arm64: add hugetlb mte tests Date: Tue, 1 Oct 2024 15:52:20 -0700 Message-ID: <20241001225220.271178-2-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20241001225220.271178-1-yang@os.amperecomputing.com> References: <20241001225220.271178-1-yang@os.amperecomputing.com> X-ClientProxiedBy: CY8PR19CA0036.namprd19.prod.outlook.com (2603:10b6:930:6::29) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|CO1PR01MB6678:EE_ X-MS-Office365-Filtering-Correlation-Id: 9e4d0adb-eac8-46ab-d13e-08dce26bbdd1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|7416014|1800799024|52116014|38350700014; X-Microsoft-Antispam-Message-Info: StYg4YMZ61MBY3YhblHT0vpFfAq80ejPClg3h5jiDt5U7mAxIKEjLXBPoK9eGdYhPfGbMDLeqxTiC8CH+kUCch7KQhN9IkE6/0RR1Zy4Fxg7MAQNDRN2Daew88kvQU60mAgHaJbQh+GEjrEhK3SpG3fgJ8wCBweAsARlCYIUb+KEaMrR6wkt2i23AkZ42NP5bsGwCOxis7afra8cxTCQtd2LX3JFCLqsC+uSP2hR27H//76fVLTGx0pKQc+i+yQHdaNA7BuJ3FLzlSdvoADgTafaolPurPK/3CJ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ispam-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)(376014)(7416014)(1800799024)(52116014)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BT+QsbeAZGz6aOHLvIslQic+mNhoDl4oIlykcvuq2Avs9b1ZpFiHT7/HOkoyyrh9aVOJYGrbxMf1RvQSFVHaOdzaixRzOICYmVOmzfMWt8WgG3VpnX6hQd+jGe9YaId8qWChzUE0sQmSc0XO8+zmj8I/xSR9neisff5AS93UJAzNdWcgSahiUzWP1vF9NJPOIAnSkZCRLicS4LKCRSpbhZFaSeo/gbxePXQ17tnYOEkT47mndO4AdMo41jCiWAVl0ucWsWyAcrMiTxLae51d7enhgSuIvEA59gdVj9DjmMA8rGQjQt3cGjM/7w2nCx6gTar3jctkCRYMJPmQXZiCLot9ULOS/5MMxm/ceOKHtG7SY7a57HTqvakuHN5NmkySiLxFka9vPXk+FGr6MdRNHraIbnN1LevdJDtg5ykOGuMtyB13SqM4vnup6vbS/gcx8kNk339phX3qPekKH+y3jSJOnHdzB1IEvOVC/o+6lrNVX59b2DZ+2OZVcUtwpvGW2M10PLPFdE7kNMjQC1ueUIUMcgUDuDykgZzrN344lA2C363naf0D1HDJSquhWBlGNqsHO6RZ+TF8cttOpB1xt2g4wP8zKqCXvb9VY8HgQhoUPKWWJHYuX4MPNwfXrjrptKxyYNft+YBgh964vj1GBQ5f64+4ecKDXYhnmLZEBGAPH1RELDPV06he3l9DpHRI4WaQDAGSK1p60mRLDO+tI0Ag3rbx+4ckj6EEq4R25afsQUknnERD2f8ZkZiOBWW71xHZSE0qB1wpEwQGmMEPkxAPgvEshqexP3Axe5u4pWRgGsiJxFKqhN2Hgcs7mUmKxSII+md55KI+NLfl4otrzQhE7fEP2yDrCYbZE+H70bnSqxtIWMKbR8n1AP/yFtP3OQKzWCrFmUysOPYCBEj6VDkI7/2+wJ+8ZlShDyQu+KBUK0bfkYgHbNz+Fo+GAzxALhB3jJg4ebVrUklOCp6fRPBXJugO+mRcapaA5MtYtrYPrLrdRNM3h5urScmMqBOca40F+uEhSL6KhYy8ca9kCyXom0ouMkACfZMLQ96MksvU8sdouZ1OYWYhF2eiRwo2a58YL76KHfCqE87eBDZwfW4LxCP/pcK2J56FQLtp6fMD0AM38CObczdHe7lEPyPUgoQbGKOXgU8eKJNX+GlCPZgCegaPxkxkZ3FdRk7qeMfDk5hbLeAaWqXPmvYahp1IDHnPVBwK+FV6h2qqY6ohcNa8p3D5v2X1VQNk90M9GIciWJqjThWIALcti1pQ/e5skVFSBiX8C89SOb5EFkBw9sWeVlSQ8ua7Wr4gh1ckZLZjLsUO2ECAtMtBaYalyC9xnxQKBURCePFV65+8/688s+1XYdIvJOmQ15iVW9eAXIwPWBsKRdptJFednQIZqjrZbsRJK8X1z5KHZYXMROx0w3Aiuut1BqW3IooGp5/IMffQBD6Oy+owuRk/1zwRnhMGoi0DYVOwwgB16FelGjyQX50mxaueAHRxGPDGB2D8XqwZoQH7QmaL6HYckD1ZDSIdlueU8URSqmDmyBAV07kqYZ2G4jANmcbBlAKEezz7B5TOsTFoe/pGK3alhBHE1Lz5S+27RCwMCz+W61mYv4tGbAITNcGVcQou1JjVT9SSVM8= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9e4d0adb-eac8-46ab-d13e-08dce26bbdd1 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Oct 2024 22:52:35.4845 (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: pV7IiYHSzIzsVXUFNrIs6YopDV95uybZaatt7dxpvow+FCX4cNKqnz4c6nbIw3v3xK8D0y9PE/RmIQEQkl/Sx7hs0PAD1vrONgK4iwvr9TM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR01MB6678 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2A15C140007 X-Stat-Signature: kfixrtkmo9qkou87jafotkc31zxtutep X-Rspam-User: X-HE-Tag: 1727823174-652058 X-HE-Meta: U2FsdGVkX1+wJ4yiNtj+EbPU1V4KU5rov1Qn4118CEO6IOCFv3gvgLq9yG36lYVNf5glCvMqwts9Js2R+cSIL4q55UL9riN0yMxj1lGYEL3BIMe8M5PYyX0zAVSw/nlO+lPnbPLfFVMdQ1Gbkx70N+pbB7EFs3yDmd/UlStO40+4caSm9/Sy/v68imhGymwTEm2wOruyYv876tsaoTUyFkzs1t83lgcdjx/RBkFHtex+LWlIHsKy4Lk/YVb3fG71llF1otknMsErE39VvHdhpNPR1sR0ZOTg/GFiBQ/nNahoAO63VGp8o39w3nrrrlV8G5TeF4YxUnapyNSxTIBnarzoi+cwVQh4PqcWUrmRmBVqKX8Du/doBM21PyBkR1XvWkE8ejM9YYRMY7D+Cau6+LeIXG2hcyA1JE/UopWiu2Qk7X939Fdg3rKExkMj9XzXVHfCuDX2VtUVap/5xIJJHtpAzRDoo5Sa7SwnzLFcHlyHlz1hJU1wLLC9iFjwzYxFvwX38U00hF13SKI0RlAUtLmAJRFXZo/aX4sm8yd9YqOSm67LRf6Xm2TNfHG+wfOLsdrHPjt8pG32X6RXBqqouCg05TtY+dwLHJCYvmBkzLmZRWG5yoxoYLGf6VlM3yADpTKnqUd66KheznMA23pRiF5gXQ3cGMmS1GOHA7h0uNIxm4S4AAxYKh6ZHh/LW1yiGxZvKq8v/xHYrsk+C7WSrFs8ecqDMttmw7eb4WbsFOjO6Odz3t4tpIc/qnR05b0cDkYPB1ds292dM16TzI7KV4piSb576Z05AnSYhUqbNNnzVj0aibNqx5p8wjOa78dueVENIDOxQfblH6yjScohgSX8L1lNl/PnuT7addNcgW9dHPyIyxRm2XRSqp4yAoLPKfxyl7ZrJVGSIWMdqs3Pr8VMvV4OLBSrmIwNOE2aQ569bL4OsKJmZirxAUhvSkomxcTGNGoiRGwddWO/JVE rlyUXk5S irhoyO80F+r1hrgBToE+5jvxd6dWeAYOH4OX/2mIczUFqw/YAXRT0/AlvbogmXMmt76FUMlBW1T7nLywVzU2v35QQYWTaipRPFPXqoau51c6qT8uh7dsdUJ5Mb9rQ5JCuH7Ap0H+VmtxcwM8SqQRXOzBCOlZ2IGSq9/oNCsF8ixsK4C/GZ/cgAg7oxESMymdl/whHOWQEr1zj/QK2wlt1+QNmbHp6VQnAxk5UdTPtiwBfklOsXZqqe4ebC6JLD+NQrOQ+Z47ewEXT63ymz0WLqcWy/pbdz6ZfcxkwZU8mc7svBAdIK3rACGE/Opt7Z2mPvO5LtePJc9BWElGUfFzBUTve+WedmBX1mOtiLCH+0d2s856EN98Twy++9lHEjzUjiztAui7XOPb5nfv5HrBIOfm0b5HCeIaG+Hq4 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; +}