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: 13803944 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BCE76FC616C for ; Fri, 13 Sep 2024 18:37:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=rZEmI5ze4VGJmlbeh/nIhqSgSXgQnJWJLInFVw0UZe0=; b=YcglGattx48w+jqsVL5gh44d0d VnqDdw0Ihm9Sn7L6F3LbfBZfx7OoLDNUYCxEHihqc1Vxlq8Um+MVrjyDPqTpmjIwZdfv7MvwcQ5iy WLb7d+iRNM1FBY2vq3TfVUKfvvy2F2OtC0MfJBBDq/S8uuoZsEdbR/rRpPJwNpI+QY3UY7XFYHFWf VusBHKxtby5U/QUxbOso/wxVNp058XWueRtVigKqeYAYK09f043aSLKo2R3je/RoFLSzfqB431A5S QMEDFzQcgC2P5zER4tSYHdyz1nWHk78opNeTYufzhTC+kaV6vXZqVM1/MTTXNaBqi1qUJLkZkJMYS LTvmUG8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1spBAa-0000000Gpae-0Ayd; Fri, 13 Sep 2024 18:37:24 +0000 Received: from mail-westcentralusazon11020115.outbound.protection.outlook.com ([40.93.198.115] helo=CY4PR05CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1spB7h-0000000GpG4-1m0R for linux-arm-kernel@lists.infradead.org; Fri, 13 Sep 2024 18:36:18 +0000 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= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) by 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-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +unckT9GO7gIF8r2vH0uTPfEe94ZdK2MQZ7hNei3r6TNz92qn2weqG8Rydm1+qOQVnOIgnSorQBqnCd3V7wJuFuzY+g4TOirVHeuIsh5bS7TaAOiVZT+2HdhGSVWOBB5huBqTI+8U7cD3sBF6+F89Jfx03U1srst2hSc+2BCPhkCfzP5V7pafmv+lcdWeE+xoKrzimpr4kgPK4hbdsKIKrZ8xAI02iSq4kPp6Pvu1XZWCRI2pzXk+BzpuvjvHiUjesRVuhYAVY/H7H22XQtAQGDX5UDFR1xsTwx0gJtZxVvByfaJF442fwV7PpstOTbe3ZCJ4kLl7tJkaulVYUpBqMbf8kSfc5PxsrX6PJJ60+ru/VUd0wDGlO4XxASUbfAGGOpD8oDDzDjWxFRVV4rKRKL1btRnNrgWrYagiOK12tOKmKPkPbPQtTkVlPVBo4BbU62fEvfikEd2dRiFxDJi9GCdpgPbvGI5p40Tuv0P8uJ9FDJNbGGwyvUg/pD68jVWtXAg0xPFUKEgOjoq9KRVz0hm5KrBSCjf4I2NTRDEwyK54Pmy8eKx5zO60LC00M5DnLoxKu/WMewZCqZF7JL1dZgjPAlW2EMC5/lGC7nV+vFppwfO4+oDgwuot1rQHDvQsX3VuhlTjTxfGrGA91GFUI8moBcalh6XniTxNp0gDG1b8CfpB0fyGwJLHc5bPJRP6j4rZi5ltyVCeFiH+aB8nHWvz9l6hs1fo2WjSq3bBGrlmoHkEA0oeQw+PU8MTIs+Fa92a++UoMcPgD2ssQA9w8TUhJLM+aLfQt/8uF9xKRl+EjnaqdYjtSB99c5pAIAjgcrN1WRsVt5uhDBgzCW4IV9X/7IApOTOqQ+HeGnUTgSFyy4POyoZQD+13pFkBy4dfW2VuQEUFZlpRtwPLQbBwQDvIwOymEOzMWwNDFvoZHShorlVedrI2H4/o+2SzhdcalfUG75Jbjh3uNk07Vzor7QE5poe6o2xeYSJKRLsb2k5uarRO+RtYlykQViIBjI3U/TZdIbXSsXj13/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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240913_113425_539002_A1D0E071 X-CRM114-Status: GOOD ( 17.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The tests cover mmap, mprotect hugetlb with MTE prot and COW. Signed-off-by: Yang Shi --- .../arm64/mte/check_hugetlb_options.c | 285 ++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 tools/testing/selftests/arm64/mte/check_hugetlb_options.c diff --git a/tools/testing/selftests/arm64/mte/check_hugetlb_options.c b/tools/testing/selftests/arm64/mte/check_hugetlb_options.c new file mode 100644 index 000000000000..303260a6dc65 --- /dev/null +++ b/tools/testing/selftests/arm64/mte/check_hugetlb_options.c @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2024 Ampere Computing LLC + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kselftest.h" +#include "mte_common_util.h" +#include "mte_def.h" + +#define TAG_CHECK_ON 0 +#define TAG_CHECK_OFF 1 + +static unsigned long default_huge_page_size(void) +{ + unsigned long hps = 0; + char *line = NULL; + size_t linelen = 0; + FILE *f = fopen("/proc/meminfo", "r"); + + if (!f) + return 0; + while (getline(&line, &linelen, f) > 0) { + if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) { + hps <<= 10; + break; + } + } + + free(line); + fclose(f); + return hps; +} + +static bool is_hugetlb_allocated(void) +{ + unsigned long hps = 0; + char *line = NULL; + size_t linelen = 0; + FILE *f = fopen("/proc/meminfo", "r"); + + if (!f) + return false; + while (getline(&line, &linelen, f) > 0) { + if (sscanf(line, "Hugetlb: %lu kB", &hps) == 1) { + hps <<= 10; + break; + } + } + + free(line); + fclose(f); + + if (hps > 0) + return true; + + return false; +} + +static void write_sysfs(char *str, unsigned long val) +{ + FILE *f; + + f = fopen(str, "w"); + if (!f) { + ksft_print_msg("ERR: missing %s\n", str); + return; + } + fprintf(f, "%lu", val); + fclose(f); +} + +static void allocate_hugetlb() +{ + write_sysfs("/proc/sys/vm/nr_hugepages", 2); +} + +static void free_hugetlb() +{ + write_sysfs("/proc/sys/vm/nr_hugepages", 0); +} + +static int check_child_tag_inheritance(char *ptr, int size, int mode) +{ + int i, parent_tag, child_tag, fault, child_status; + pid_t child; + + parent_tag = MT_FETCH_TAG((uintptr_t)ptr); + fault = 0; + + child = fork(); + if (child == -1) { + ksft_print_msg("FAIL: child process creation\n"); + return KSFT_FAIL; + } else if (child == 0) { + mte_initialize_current_context(mode, (uintptr_t)ptr, size); + /* Do copy on write */ + memset(ptr, '1', size); + mte_wait_after_trig(); + if (cur_mte_cxt.fault_valid == true) { + fault = 1; + goto check_child_tag_inheritance_err; + } + for (i = 0; i < size; i += MT_GRANULE_SIZE) { + child_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i))); + if (parent_tag != child_tag) { + ksft_print_msg("FAIL: child mte tag (%d) mismatch\n", i); + fault = 1; + goto check_child_tag_inheritance_err; + } + } +check_child_tag_inheritance_err: + _exit(fault); + } + /* Wait for child process to terminate */ + wait(&child_status); + if (WIFEXITED(child_status)) + fault = WEXITSTATUS(child_status); + else + fault = 1; + return (fault) ? KSFT_FAIL : KSFT_PASS; +} + +static int check_mte_memory(char *ptr, int size, int mode, int tag_check) +{ + mte_initialize_current_context(mode, (uintptr_t)ptr, size); + memset(ptr, '1', size); + mte_wait_after_trig(); + if (cur_mte_cxt.fault_valid == true) + return KSFT_FAIL; + + return KSFT_PASS; +} + +static int check_hugetlb_memory_mapping(int mem_type, int mode, int mapping, int tag_check) +{ + char *ptr, *map_ptr; + int result; + unsigned long map_size; + + map_size = default_huge_page_size(); + + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); + map_ptr = (char *)mte_allocate_memory(map_size, mem_type, mapping, false); + if (check_allocated_memory(map_ptr, map_size, mem_type, false) != KSFT_PASS) + return KSFT_FAIL; + + mte_initialize_current_context(mode, (uintptr_t)map_ptr, map_size); + /* Only mte enabled memory will allow tag insertion */ + ptr = mte_insert_tags((void *)map_ptr, map_size); + if (!ptr || cur_mte_cxt.fault_valid == true) { + ksft_print_msg("FAIL: Insert tags on anonymous mmap memory\n"); + munmap((void *)map_ptr, map_size); + return KSFT_FAIL; + } + result = check_mte_memory(ptr, map_size, mode, tag_check); + mte_clear_tags((void *)ptr, map_size); + mte_free_memory((void *)map_ptr, map_size, mem_type, false); + if (result == KSFT_FAIL) + return KSFT_FAIL; + + return KSFT_PASS; +} + +static int check_clear_prot_mte_flag(int mem_type, int mode, int mapping) +{ + char *map_ptr; + int prot_flag, result; + unsigned long map_size; + + prot_flag = PROT_READ | PROT_WRITE; + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); + map_size = default_huge_page_size(); + map_ptr = (char *)mte_allocate_memory_tag_range(map_size, mem_type, mapping, + 0, 0); + if (check_allocated_memory_range(map_ptr, map_size, mem_type, + 0, 0) != KSFT_PASS) + return KSFT_FAIL; + /* Try to clear PROT_MTE property and verify it by tag checking */ + if (mprotect(map_ptr, map_size, prot_flag)) { + mte_free_memory_tag_range((void *)map_ptr, map_size, mem_type, + 0, 0); + ksft_print_msg("FAIL: mprotect not ignoring clear PROT_MTE property\n"); + return KSFT_FAIL; + } + result = check_mte_memory(map_ptr, map_size, mode, TAG_CHECK_ON); + mte_free_memory_tag_range((void *)map_ptr, map_size, mem_type, 0, 0); + if (result != KSFT_PASS) + return KSFT_FAIL; + + return KSFT_PASS; +} + +static int check_child_hugetlb_memory_mapping(int mem_type, int mode, int mapping) +{ + char *ptr; + int result; + unsigned long map_size; + + map_size = default_huge_page_size(); + + mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG); + ptr = (char *)mte_allocate_memory_tag_range(map_size, mem_type, mapping, + 0, 0); + if (check_allocated_memory_range(ptr, map_size, mem_type, + 0, 0) != KSFT_PASS) + return KSFT_FAIL; + result = check_child_tag_inheritance(ptr, map_size, mode); + mte_free_memory_tag_range((void *)ptr, map_size, mem_type, 0, 0); + if (result == KSFT_FAIL) + return result; + + return KSFT_PASS; +} + +int main(int argc, char *argv[]) +{ + int err; + + err = mte_default_setup(); + if (err) + return err; + + /* Register signal handlers */ + mte_register_signal(SIGBUS, mte_default_handler); + mte_register_signal(SIGSEGV, mte_default_handler); + + allocate_hugetlb(); + + if (!is_hugetlb_allocated()) { + ksft_print_msg("ERR: Unable allocate hugetlb pages\n"); + return KSFT_FAIL; + } + + /* Set test plan */ + ksft_set_plan(12); + + mte_enable_pstate_tco(); + + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_OFF), + "Check hugetlb memory with private mapping, sync error mode, mmap memory and tag check off\n"); + + mte_disable_pstate_tco(); + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_NONE_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_OFF), + "Check hugetlb memory with private mapping, no error mode, mmap memory and tag check off\n"); + + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, sync error mode, mmap memory and tag check on\n"); + evaluate_test(check_hugetlb_memory_mapping(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, sync error mode, mmap/mprotect memory and tag check on\n"); + evaluate_test(check_hugetlb_memory_mapping(USE_MMAP, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, async error mode, mmap memory and tag check on\n"); + evaluate_test(check_hugetlb_memory_mapping(USE_MPROTECT, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB, TAG_CHECK_ON), + "Check hugetlb memory with private mapping, async error mode, mmap/mprotect memory and tag check on\n"); + + evaluate_test(check_clear_prot_mte_flag(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check clear PROT_MTE flags with private mapping, sync error mode and mmap memory\n"); + evaluate_test(check_clear_prot_mte_flag(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check clear PROT_MTE flags with private mapping and sync error mode and mmap/mprotect memory\n"); + + evaluate_test(check_child_hugetlb_memory_mapping(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap memory\n"); + evaluate_test(check_child_hugetlb_memory_mapping(USE_MMAP, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap memory\n"); + evaluate_test(check_child_hugetlb_memory_mapping(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap/mprotect memory\n"); + evaluate_test(check_child_hugetlb_memory_mapping(USE_MPROTECT, MTE_ASYNC_ERR, MAP_PRIVATE | MAP_HUGETLB), + "Check child hugetlb memory with private mapping, precise mode and mmap/mprotect memory\n"); + + mte_restore_setup(); + free_hugetlb(); + ksft_print_cnts(); + return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL; +}