From patchwork Thu Sep 12 20:41:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 13802643 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 F1C29EEE25C for ; Thu, 12 Sep 2024 20:44:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=rZEmI5ze4VGJmlbeh/nIhqSgSXgQnJWJLInFVw0UZe0=; b=j6O0cMjGn6V/fFHYwrrKPW+5Sc ZEjbdm7NlwKRornEz4zw4owvqwUe4XekyKmzLoFYOENw46FZ5TObY2Lv7WRjJQgdr4C1d9bviJHGc UvGzWY/425JuhKdIdWUpqwJLnKu7Sg0XePJm04In8SDnwD3dPW8ddaF7W/UhNnK1ZCeXX89/D7CMG dT864VXeCDZtP4LYC1ahV30DZWlGlRy1FiCnt7NYBK+EupEy2gmQnXWULymkX2I4OYYD308GZiP24 2ZQjgRVNPPRT6rnPLZ2f2PCH041i9RwzgI/SicZrESQafzQoh5h9tzATtqlTY1bKvkepoIXLEw+8z FosujqMA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1soqfa-0000000ECLo-0qkd; Thu, 12 Sep 2024 20:44:02 +0000 Received: from mail-southcentralusazlp170120000.outbound.protection.outlook.com ([2a01:111:f403:c10d::] helo=SN4PR2101CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1soqdV-0000000EC7Z-2sTf for linux-arm-kernel@lists.infradead.org; Thu, 12 Sep 2024 20:41:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Cyh47sI3XM9NU1NAi6CD+jJYK6MQaMuQVsrKaE9RGEc3k2Rslf5cc7lQJgzSIHTq78TZKBLnfUfPg+1qVFYohiiTuD3sMK+2LgTFGTj3yYjOjIz6MX0Ohj1Tv6QRQNfU3UTyJJiisBTip2nPaB8tgsJo4R2vhZ+wMJi2c2o9XYQAMDE9maX1yFFbLdLfVWcwBXKVsXGj/edYBKWdXOhXADGxhU1DBbPd9zIylxI5IkQzNcfvimw6t1pl6Ehaeznp2nMYs+yb4PBbSrOrkoFGfAgePVzCrMlqB08lfvDcABpz4im3V4bzDkSH2Y7NY7E0i/wtXZJHahR1vLvKJKyBsQ== 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=DEPqP6CX9e24Bsqb78rSZpFVdj4aSEarDvjU1ntlzVEBk3Ce3CYtnDj/hf07dU0RDbiSYUd0tPCf4pJB8f4F4RqpRBpZM5LMp/jNFvj6s38tAD8PFmCdcalhy0zw5ZNhhdZYT1RWzvXMAAYvB4s8ivnrgB4G53twgZH8fFBpYQqB66BIOEfYcnIoO/Bg0CupWey1lZEaR8KoyyGsRa1pjmccSQ2pblFQIsg/fhuxdj5+FNdsW7BpZtsXz4zhA67Rm6xgPtdYmKOoqfkxP664mLXb84/dNXyRymTCqrZ/79bEZnydEtNwIO4TcOOfNtaWij7pou8YnUvJrd74wfpoHw== 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=ijO4/GNKEkuaL3hG6VlkYUZIDgl6phK5YHAhgDpAnILSw7++YrCNVKYiLSI3O4kFRtiZtvXJ5HqBNLXoSUin+5SUHWPOOliXoQ4TJXfUzV7U5NATno1K1BFueK/YXZjoFs5/QplXvzGT2nCjvpfGl4oaisUMMCEudiYQbJ+xYPU= 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 SA6PR01MB8853.prod.exchangelabs.com (2603:10b6:806:42b::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.25; Thu, 12 Sep 2024 20:41:44 +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; Thu, 12 Sep 2024 20:41:44 +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: [v4 PATCH 2/2] selftests: arm64: add hugetlb mte tests Date: Thu, 12 Sep 2024 13:41:29 -0700 Message-ID: <20240912204129.1432995-2-yang@os.amperecomputing.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240912204129.1432995-1-yang@os.amperecomputing.com> References: <20240912204129.1432995-1-yang@os.amperecomputing.com> X-ClientProxiedBy: CH0PR04CA0031.namprd04.prod.outlook.com (2603:10b6:610:77::6) To CH0PR01MB6873.prod.exchangelabs.com (2603:10b6:610:112::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH0PR01MB6873:EE_|SA6PR01MB8853:EE_ X-MS-Office365-Filtering-Correlation-Id: fd04d617-291d-4888-6c1a-08dcd36b5060 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|52116014|376014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: y6wv3yUqu+hxkqdL5HxLHDpLiQi3nE3FjktHpeFJQThBHjTbJ8JcFTMLrnSxkZJynz0u7qIRXPKg+u+SQEvu9knonZ0E7LG5xF61fG2c7oTYSr1GhYTPD5HPr9sTBalyoLX9i0HS3jB3Hb5HnSOmoIU3Tl2SYMfim/ZOhQMHjs4WosBBOE40A20Fk1Qb7NWG0Mo6jSqXojyJ4onmHulHKs7Zobvl+OYlhRD4ZE4Q1RxCFnl5Au0a85GhWaxogbiaMyvM3W5tTtvlIKE5DqC7/swRgf6eJnBKbMqVmmMAlnnQ1dKYYH+kSKoh/Upt4R1RJdtbjGKICzGmU+BgaUVE6V0IU/1/6fSbWywbDhDKHG0XIk78OOLHNighnsqm7Mqs0OkzcIH+bkvEN2Z1xbYD3N5Tad0grrj4pwjfrPZsn9W02SDY1v5OQFW/t0I4xu/hFPsdTbDIPaKQQ+8y8MiD7a4XkE7R+cYJ5fv0Odkf4jEEorb63r9V9PdlubnOLSGNtjYnuXuKdjwU8zbWRD0qxs8YUVOvAkpx92hba7eWV7vn41+9jmITZWIVeIYPs9HUTmvVFByzHhOUo3fjDqIZi4lJeryYH96NBTIS3OBcEVFVBk1oHhcUymheDZQJ6AQ9OgRUT8uz5cJfgjcXMGKUHs3BrLnBheMtaH+E/lRlaerbivbUIldIiugdvzZ9JsBPyeNrVNopKnIqvGtDi1hxZJPva0A7DxpYfl8tQ1Ssdbh6AsQMP3TiUjsrsXlhmoTUMbxEDRobfIvyZxenn16LcLxe3pYx5L+J7aHhVj9krieluovJUSFuUvKNkJDohVVRIkUCewuj3vOEwmtxZ8IVSw3Ey29FVyWdulkN3ptA11rXBLEkGs8jAekJIfzpNqE1nHu1pR/tdp5r7k/D6JOZFtehvnSbMPNPdOpz6dq634KJ08g58FU01y39nilMn0w+FrpY+TrLAM73Sw2phbG7LRHipfN8vrMM3unKS/V2WplFzADlDb8Vc//7sM5pR7fHDCig6Tf7bJ+RSiBhjCa1Zb5pauhytL935siWXgWlQSBj9FVdfYEbn45zNy8tGV34q34nP9s8rDc+D60QbexL96EpF4wWzRLW2W4G0QGUJjBd0c/SYuJVe7WZAxSn4WNZ2QYaF1SolBvk73jhMka9hcRSora0wQiGybepsQhCV/qaSg/E0Dc0TjXG4zw/FxtrobInbKoPMyaM1kiAHgWUdTwdMq7u/LQfGt28oKlc5zkmepcEycWfbiaWuM3NAVYeH+6hxqWZ81R2zK+pawC5fEvcfQ6AmEHskdws7bBPdJ1g/RBwlL0ABVNgplSe7L9TmOG5/ykSd62sPtHie0G8oFRctKEQf/yYwT7dsaY24t7PnBop9EcVR1pHpAhgYT7kD8dSmuakn7mZL144P8zaig== 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)(52116014)(376014)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: epCtaV2k2+PbK0uAoQXcNDifEKRuk7gw5efaWU1w8py5Bj9gXVOo2GGsXNOve7r3Sm2Q8W3Vqh+iLZU0kiTvXhMd3w7OEGvzmveUdD0xdCq3xpMC5nOJ60s8zwTHCWFYlYBkBLbSVwRoUCsvjDntATIG+YKDCtGM7YPXGoVURl8NtDIna0mUyVwdvP6HUk9cR1vHMAPFRqzWKKLuNL9DapZ+0WL3BjuZr+20dTPhhqE7CJVMC4k4fgO8rWbhDYIRmypa+fQ6OhaIYslgKrmjTMUiecjIGOwfGWH0AKkNrC63j2XT8WlE93g0D/qPexcq4Ar+F2Xz9qNmSTUYS5wwKnVcqHIToGoB2aYE27OpCfsNAZTFjqo2SSvP9o0B2eyUXkGUUfP+O1AWmq8Dvokns2bfC/7f6sYbg0p5ZfgR2jDrb5xFewztvKYFI6N8YW/5SGqds9LGeU71Ci/aBaCb68xWO64qysq7lWfmYB1D1hWvI76wKuNQ/cIuz+VHCga1WNrDDPO2vc/btHbIfuRwMyz05D6Gt8khTGl4JPeEWVBoQTpval4Qjn2EpQcY4pbWz3bP4babpxAGTzEAjQhddjfKSfAV461/OUhsQPMzmRoC61sgQZFedV/5lO24GfO5wMKs+CgZojDCVPjzMAXTVSW126ID2kOV03vra0ofFAc8l+Pnh+DoYe+Q16/Vs5c2mDm697aXko3JDjG2oyx/jiC3OsK5Xqh41B98NxkOV65qonwUQaSVqMM9y0+hxERKSPljQJMbPbc9TViubCGENT2MYeorcYWscQ6bC5ogSPcdss8znt0v1kikQpOBjle7FD/xvbp5fZjXtcT4429eHFvzt+e92N1AEObW5K9Vmxt7IgMRSS7ckukWBLPlk0s0Ib3A3pMqmX8l8j825ShB81hHKU1gaSChxXEU4FdUyGPDV8ZLCgvmbQpGabm5B7+CZl693zstDLSIujl1cuAOLgRAPzMa5nGMQLUn8j8o5MpYNrjdAFRbO6KHTOeNtUJwX1jJhMzuIfDYDpFNSbw82z9zi7CFXJ3QHaC959kw+oBZiWWt/vWTJz6dEiw4Igpq2OS7coRWBm8ybkfNU71qIgWz/A+eP45xzv+kmzCXSiyeDKKcNrFmuTv6630ZnjinQMLmVtMzRNPxLu6qQfbmzQQS40rfioQKJScsCxFyDzuFAT249kXejxnSnVGtxsOsRMFs6LLoX5Z35vKtI5dwvlTwdVjcHdD2f8GOmbw0RMAZ6mep1BNLeuYApTZzr22vxYTUL++fpo0hIHQOE+hzTnJkfiI4zg4UZUpnXE3/F0zbWv52k1JqyMhI//gUrp0JsuV2oMB9DHWO67QdZ0XuKmbkYZmbKi9lb3c+L3ASSBHkKPyFZ8LIrH0dIWaTpaaSZL7t5SKrFTb6EFJtFORzkyeKu7Um2floN36McB78jow0XqR+Wkql0LCO0CrK+5EZCPmfoZwadWonOkH4EvxVte69pEWF1xrG+0nsTUeWfnkkwPH0kjer7ZTn0uux3n0Mw/HOrCiqfONs5egtfsdLkRTx7+ldAMrWWFURLtajC+HjUYV/Hd1KChBNvmVK6Q/fnoOMRMHqHGvZ7UFKgOj6oxyz2VamJJFp/tXEarb8TqI= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: fd04d617-291d-4888-6c1a-08dcd36b5060 X-MS-Exchange-CrossTenant-AuthSource: CH0PR01MB6873.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2024 20:41:44.4482 (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: 2i9lusvYMSA5fp5qhgvQgGJ3x+AJFZ+Xr3N+COZoVmET7x4qPfk4NNw2IpRBaqS401oJxs1BsRMF6XBWEcktSGq1U/ZAUvkv3L4s1n0Cpko= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR01MB8853 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240912_134153_761105_15BC3D25 X-CRM114-Status: GOOD ( 17.80 ) 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; +}