From patchwork Fri Sep 6 17:59:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13794492 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 185D4E6FE38 for ; Fri, 6 Sep 2024 18:00:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 749366B007B; Fri, 6 Sep 2024 14:00:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F8A36B0083; Fri, 6 Sep 2024 14:00:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5256F6B0085; Fri, 6 Sep 2024 14:00:12 -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 2B8136B007B for ; Fri, 6 Sep 2024 14:00:12 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BDE12160CD8 for ; Fri, 6 Sep 2024 18:00:11 +0000 (UTC) X-FDA: 82535077422.06.537AAC8 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11021121.outbound.protection.outlook.com [52.101.62.121]) by imf14.hostedemail.com (Postfix) with ESMTP id BDC1F100011 for ; Fri, 6 Sep 2024 18:00:08 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b=PdoZZFnL; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf14.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.62.121 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=1725645499; 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=WQSo1UX9S/CNZQca4NZv/6iyrYiuSwlvSyXh4ln+EaSoOSbSPkUaAEL03kJeN1U3N3KZx2 g21AFop8Q5nUEeLcjSdJnAMOzm1TeAOk0/xO1MeE6W+CFAb/PU6mfP6JksQaIHJjG6YujX 79Oeii5Gh+1zSCmoJ8ucIcsfSzjA3TM= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1725645499; a=rsa-sha256; cv=pass; b=SVuIwd4yUBJxyHq3vTI52vqEH9fH1a728ErAVL5KPiavbPVNtPJPLOtvoYrPtxq4hwAhAB /a/5q0lWJO62MOxrd0jmmT+Fm0DIZQZdeOouG+sZhURx7G2z4fIq4jJXK72NnWQPx4U5xE Cqd6POqfH1wmsrtF7RaqnK6VfQqdXP0= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b=PdoZZFnL; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; spf=pass (imf14.hostedemail.com: domain of yang@os.amperecomputing.com designates 52.101.62.121 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=c7MW3EOyqcvvzPYUoPLshymHRdy1m7pCEFTSngF1VcbEp3Lakgku7Oqvy3jeeUqXE7fZ/K01BQ0J40SogeE56S6R69zcSpnCBgh7bcXJnFnJSCTZLRLOpCd83tYRrjpfDPCuMhdQjc6Kyrlcf+RTyi66sNvkzc0msF5UQpK/6x+iWVXRUmLWIGZ9Rm+H6pfJcHfP/9KmgbkPOOVR8JXWZ6pCus3Mmtz7inT654vlijrmpjRNqNqXnXM3SLud6FcpUj1fJ6F8MAwQV1MSqNemBrGfbfRNBA6YcWtRFvUXRkO32JaHy6HCoxxHtTSll7kDRg54eMktzCgYM/zm7RMlOA== 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=uJVE7MzXii3PlQbLNNC96PAVPZewCPjSMdnM8pD0Pbsrp2M58nXJzkgxonLGpSTADKnWOru6SMKOaNVlqNTQeSjDYW4kAo9f4rkx9p7qru9Q7/NpeJqzFqzGHkum/PMagPIS4TyBudNSssr2wHjPwY4jB2bit3ugcYzzGd0/hcWRt6LlUu8V4qMGUXAk1+CThF0h7wbhtwubMOuGT37UW8hALUYrS18qdclEszMKMWMBoIVPZtnLOgXbC/zd86T4x/U9RataiLPQatMiez4r8m4RFl28D9XQFl6mK8jxgyzJTET0PNooCls92LiVMGftPg1f4I/CQjB8nhTu+HTTBg== 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=PdoZZFnLo9IW411W6Wc4bb+0uUi/uDn+x/Bpo5J6/DnBSLiI5GJy72E/kvEcp0gEfpiZr0N675Aasva10Njrd457mCv8lL3aACy36pbCC+gfZts/c6V1YRHaKJaxeAcVupOhb642ta/YdNCB0PsntdzvImsY5f0Nm3ARufEh5dM= Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by BY3PR01MB6561.prod.exchangelabs.com (2603:10b6:a03:362::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.27; Fri, 6 Sep 2024 18:00:04 +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.7918.024; Fri, 6 Sep 2024 18:00:04 +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: [v3 PATCH 2/2] selftests: arm64: add hugetlb mte tests Date: Fri, 6 Sep 2024 10:59:45 -0700 Message-ID: <20240906175945.3636193-2-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240906175945.3636193-1-yang@os.amperecomputing.com> References: <20240906175945.3636193-1-yang@os.amperecomputing.com> X-ClientProxiedBy: CH0PR03CA0241.namprd03.prod.outlook.com (2603:10b6:610:e5::6) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|BY3PR01MB6561:EE_ X-MS-Office365-Filtering-Correlation-Id: c218cdd6-f292-4938-b274-08dcce9dbc65 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-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zy7kAj0nQUc7pU0vJtI053/vV0hSE5DbSJK/O8axa8S1UmHi/NVaM6t2q7V94rjsfx7XxAIy/dcwyJNdln14ykKCCecipRFZ5p6+JCN6RhEL+F2MIloAxolu+RM0ERRKtAvyDhLzD57Kpk2hq4WDnUyt0w7U8zRukvqgITstXicuNbeTxBgbk+p/C4GETrafWMTmVp7EY/RxiOwQ4Vv3nRIMjs4bCB0iwNKoM/kDbRXLSutQ9UIiiOOu8y3NtySS4vBDzr2+auaDZdkMO4bkmxQe/dw2VTXFzCb+KSEihxhDuwJEBoRRsj6SsCNp5/t5GiYoxKVqLPAohwWCzYJqUHN8/O13MHOWc1BS1A16eA== 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: BhAEOjlHXWFktXh5qmzQ+VvirSIG9iYXUtZO4dAQzjtFsbBIZreC7v+CAS2APmRRoDqH6lSwHGlujYd9VKEKWA0VUaW097as7+f3LcoaJlpdT7CdASXuLjq0Ux/bnK6ZbjYtATzW89BJqZ3SYFe3toZi7yaHYUubrdTS5PYnARUdKbd80g3Zo3bvdVdfVyl1tTMrOz3x9g9oh59nCxMRMhSSL1Gaek7I/+CR+o/8x7j85X3B6dXi38YAzBInDlybwi+gtbjSD1GqCe0esp4rUpjL7oG+jQLyHcax+BKu9pn9vPnE/7qdnURqgLeOThcHemkzGblWqH45Jz94x0lpGvHaBRbAgGlfh+dqBjctcnwOWRgYBEZCp/ti/A4bLSOZEHGOdwnBnokFa1yJk7iJMr5K/1WcvpSi849HIxq7MD7clujjHwX6/iFzjAusu892QeXPc4hsv+cs2ouWUEtvlYCiwSBVOtL3zp/wcqabZ7DdKjC33vqnkR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dw3+pIxr/JnW/mknQ1COwhLjbpBWR0dOHPYT/lDnkj6E+4FHfU01e4MHdJ+N4= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: c218cdd6-f292-4938-b274-08dcce9dbc65 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Sep 2024 18:00:04.8513 (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: lrFN5SCVKP/JZXLL2ZZEWuJBkDpc2i8ztoblSeDfUTNRa7Wu9N1MIojppy9sWMJ7q19WL+wYWmMhg1sSMEHAPDQ9VqNiXhWcVDUZACsdjec= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY3PR01MB6561 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: BDC1F100011 X-Stat-Signature: yn755whpf6q7tmzhnmj5mgr9wgupgbh7 X-Rspam-User: X-HE-Tag: 1725645608-434867 X-HE-Meta: U2FsdGVkX183qKevnCRwrsXb0ZBN178hZC2Ki2C+sWevF3mpmvBOb5p/i3oM9Rp6Q73okJX0YLbRpPUYHqd+h03EJuu4Hi8EJQKhaiO4hlRj6I7qxVxVrYHP/cB8pNHf30aEoq6KJhS2vvVuhdNJDwfYhpp5OAB0WCzpTuFk4NAhV9yeSfktMqmLVyu5Y0Rk3qYJUmOKu3+IR6AahzOaRZcCjKn0Jxf80kyhamVMbq3TO1kBytawlM4AA+gQWnZctfWnT716UlB4jeVdr3cp9vzAzaEBhvDgUx/Zx1B8Rwiezu2CliJssnzq+X0Tsh3iKW7CIoANiMh122+fNVzPPA80wR8V1auKGIGiRnVBi/e8RWQE12pmATcwMNvtXbiRwNlyBu/yuqvfz9AE0rlX6/nMbCIx27skG5BgQSArSdOiCN9tg1UrSA+fBZPWORbhY798jet7e7AV6EUh/75n1F0TmLxGr5v1YwSRy4jLNe1b07/gmW/mC6gEGutn1hR+j2sl/ngGj3Oijx1h/9L6MdRt8znhQsPV9r3bjcMnFNGtvRLxwTWJf/yEc19pUFawwZEf7b36qPb9JUnpxwlx2GBgDgmo6dtg49ghWvHpBfBTwJDj7mwpmaBlysGvtyweUGyvbBrRitexeokwWFyzatXLSu2dlMksLvs4ailRNxRXa3/mMi2etqdjAfepqlOY6TCsOby4AAosLkpvIKP7fBoKcOFkV2baj+8y/F9mQC0qLRDAjG7/ebRoq1xCELwpe9tNy92A+yhnFrdP+AusTMEQx1o+z/IfwcCCYk9CDyIVDoZ3rU1X/7kSrovCIAOqDFm6R7GoZPhsQUeBF2rnqPdmN2SfqAa7KofEJ6u9sEPJu3jSP8Db2OvoxtimM2wR9Whgyy2QaHEvw4eZiEHAV5FlSteSC7qrjzRrWfTQjGHESpe7t0eC5O5sMNkORYl+5I/tjv4BUhddaZjTxwa Ph0ZEKsB 14WwZU2RSjA0/URjPNHxS18K3YNrvuLwRiDZzA3QjPEH5JFPIMj2gK5+7OANhZ7Ty1EWMPmlxUMknSZeExPvh+7+2b4mcYZvHuxy5y8MzPRGR3QWLAJ+yEIId7YFIcYTXtBlIIJ6sDbvmnQgvgW4+sSudKMwo2s5mDsh39Ayy+L5y2EQByK77mtK2khYWvObw0HB+7ZdN2ony5Cf6gpv5ychahsIWX+B/2406pCeG50tQL07ZG9gNFHlJjRLZsShe5aNYMeZCd+VcrXt09+Upp2Q/TTjUp6XgmqXJMsLGLPG6xkwywguYb/EHC5iDXGGuX2r1HTArX9sJU7FPcYJIPRXQJw== 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; +}