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: 13802640 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 C736BEEE25C for ; Thu, 12 Sep 2024 20:42:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2BA4D6B0088; Thu, 12 Sep 2024 16:42:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2697B6B0089; Thu, 12 Sep 2024 16:42:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BCF66B008A; Thu, 12 Sep 2024 16:42:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DC60A6B0088 for ; Thu, 12 Sep 2024 16:42:05 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 65D3E1C4760 for ; Thu, 12 Sep 2024 20:42:05 +0000 (UTC) X-FDA: 82557258210.01.125F6CD Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11022084.outbound.protection.outlook.com [40.93.195.84]) by imf30.hostedemail.com (Postfix) with ESMTP id 8BE7880015 for ; Thu, 12 Sep 2024 20:42:02 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b="ijO4/GNK"; spf=pass (imf30.hostedemail.com: domain of yang@os.amperecomputing.com designates 40.93.195.84 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726173669; 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=XAShQ5BgaeEGKz7hyCSR8uYUTd8KrgSHKVOXI+f4kG+nnKWRVF9CEp5LoFpE7WMGi5yY7k lzx86o6iiKMZXRnWpQIh21Awv9WJS2UEkPlPs0jHuylhaWa+dUbjXb3rqUMKJXZSws8JYL SgBzmWPJCZo9FYHkh4riBVCa81pX+8A= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=os.amperecomputing.com header.s=selector2 header.b="ijO4/GNK"; spf=pass (imf30.hostedemail.com: domain of yang@os.amperecomputing.com designates 40.93.195.84 as permitted sender) smtp.mailfrom=yang@os.amperecomputing.com; dmarc=pass (policy=quarantine) header.from=amperecomputing.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1726173669; a=rsa-sha256; cv=pass; b=4907LA+2I97ihbnHQr1W40tigTgIVNGWgufQT1BwkOmAP38uDKxkPDI8KtSVF/JQhWJqaJ rgLBSjQxT51loCS3kzdhpFE7VUVb321JBEzvUO7/9Dh2cegOMBkd+H2k3YKoflysLBt+Gm 2e0Sf/xsfJC0ZbEdPfqgvfpkrsz4QUQ= 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= 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|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f7bJ+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/5SGqds9LGeU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RTx7+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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 8BE7880015 X-Stat-Signature: a1bec7j84bjx63a8rg6b6xu76xixiirm X-HE-Tag: 1726173722-397850 X-HE-Meta: U2FsdGVkX18bLDgnVZfw0VwTt30LbGgb6t2t5La+noXckkpHUKkmP3hj1coB0ZKSUNlFuG5l/oehpNMhubfAW+hxGbiKfp/Or8/tEGzwuI3SCcSZAZhGn+8Md5J3u88jfY4zb+PIOiIWvyCkCcuGOFljDZn8pCqJhD2aL/VF3PU3DLNfIdB6i1dAhzMFMQ6y8O4I92VBvUifySTElFaavrqvc/Dsw92fbe6r0CGwXTEhkqShg3v14z2VRdW+6IKh7wDiuM8JqN9H4T6KY2kNvSCLYzHIecG6+1Tyl0w3SdTE9rjlaCxS/m6s1p45N+8x7fCn7HftnvWaAVlgBrZdU4B8jrOqFlnvvcAz9vprBeY+eghfo6wXV930xIAYLuPTIx8Ui2sIxgTsLSJAjO6ajDtMQeixE32qdpFsbg001SSCjvmFcF1qxYlUmArV3Vk737gUUw/zJmZmQoR10kIzNvVa+t9Dhm3+8JJAJpx1oVXXGrJIhXIu8FBfqWHOelew8za9GnUY7/h0ywryFlu0yNouzuo/8dNGXc6su05lShjagiXc0eBFYTSEZg2FYnMStrGkVfSFYFefGvXDCSq3HvNsUoN8pjGdHeL3ddfmvK70pD7dWFSnb42EgjcDqNmareXqWUGdJx6Y04KikrRTPhqcr0f7t+jZw2Tbg5Ii1G4tkppVqfjVc94vNspQY1PW0ulf9VtAGRig4XvnPT+JbksRdon+h3ja1by4oO5u6RABF5nNOdOEW6NP8rvzNazfQABeCjgR2+SVDH1eEVeiT8FgAbr+IAhacAheXfFP67IG16gsOZaqGvK8/9SMprqxokgOzMmrtGR+Wz6M+KFofomLDbxHcg/CosM7xi8sTpNZ7e5TVBSeG2eGq1BOc0ZKazPTKCyEIbv0qrz2pgnvF9+heVYspduVND/soOALUdutTC0+lLdDoE4voI+2AyY+p5w2HlHrg+B1GXQe4gY X1hO7ArP WmmMf1B3xXHCOjAsTZmHWc4p7qWAy7nNYjhvzZSsetY3h7N039SlGrOIgG3vKx025gVtAPxmO0soOYPhdXAr8WpNkSHD8b3tYaixDdbOy6GX0s9MnTbuuuF9cXNFpf9AZmn4y0UZrH/pymNLOzq3qa2Dt9iSmoNLxInD6T1yEPCLK1m7SBvZULdv8QaAVhrOaGPqovnVNhfGImnLPw9/slevP74doCSvXYFfFBJytZNflpWePf8gfy2/LNdMliKdqhcJwgTyL/fy24F4Xf2NlnHGPrX5z43K++Gf8SHqltd77LPTuWzIihHQJHXgmtwKNTX2cddKGqQNRfdBp62HiLr2k2A== 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; +}