From patchwork Sun Mar 12 11:26:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 13171303 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 4AF3BC6FA99 for ; Sun, 12 Mar 2023 11:26:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 176C48E000B; Sun, 12 Mar 2023 07:26:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 128CB8E0008; Sun, 12 Mar 2023 07:26:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E45C38E000B; Sun, 12 Mar 2023 07:26:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D1BCC8E0008 for ; Sun, 12 Mar 2023 07:26:41 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A82F9C0AC4 for ; Sun, 12 Mar 2023 11:26:41 +0000 (UTC) X-FDA: 80560018602.10.FFA2FCE Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf25.hostedemail.com (Postfix) with ESMTP id A1488A0015 for ; Sun, 12 Mar 2023 11:26:39 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VeABaVzV; spf=none (imf25.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678620399; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/1rHcPra0U9eGBs+/YWoup97Bmtdd9oXHD+y/ukSepw=; b=e0tkwYAywnwwWvv0P8bEWPJTtuCuMYePGGdxGRf9niJtwxaRfgQQdP4IsU/RJkSp19RF5a Xy/vUb0qW34SVPMM7DLnXaBtpManU0vFDekdHfZPnVP8J119jn/ITmXty/++Aeh2gGiOiZ DZfGwdGKg6fJV0RfI0qt3G6GfiPd7W4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VeABaVzV; spf=none (imf25.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678620399; a=rsa-sha256; cv=none; b=BfQqc6877jIlQP7N8edGUdoaWkg/kmUSGxlP/rW0tcPQnq76PEbY/G5F4xflX4qPRYPnyf T592A98Mtp6LGCqJwce6zxuoZmvBN39z8oF39bh155Gy2I+PCvA/Pr0jqRmmMdeIsKykJJ 9Nmph0SAT45SN0dJeL4ZJGV21D/pSfM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678620399; x=1710156399; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P0K/8sKCzyXH2J3CrkxRYfFuqzn3N+K8CC9BjCsCzdQ=; b=VeABaVzV/biv5G1y32IL3l7UOII6I/LU+gY4VbzkynEgDKaeV2rZ/qFp 9NzMdhLgILd3XfgYvs0gzYvZc4AsNGohX6Yl86Z8YSwYt+BWxqHO1SUQ9 h0PKxsKvPxduo2JIr7G7BwXtiqzQfH8zU74BeQUonI9I6RtexmWk594K3 y8oCxvp+OiQYFvOvY7Iqlo5M/81UeiYcdu5CgPkcVEapSktKSiaeQDLyX SLUP14Utjm7sHsedOSl0vHNjXhk3/2SO5KICEJ31C5iFPag5oKif8vTIT 2tRRETHK3AsMrOMs75qU2OlhmuckZpmUggyQF2CRNR59RSGLsHunUCYKC g==; X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="339349983" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="339349983" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2023 04:26:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="852438037" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="852438037" Received: from nmoazzen-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.219.215]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2023 04:26:30 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id D7FE110D7BB; Sun, 12 Mar 2023 14:26:19 +0300 (+03) From: "Kirill A. Shutemov" To: Dave Hansen , Andy Lutomirski , Peter Zijlstra Cc: x86@kernel.org, Kostya Serebryany , Andrey Ryabinin , Andrey Konovalov , Alexander Potapenko , Taras Madan , Dmitry Vyukov , "H . J . Lu" , Andi Kleen , Rick Edgecombe , Bharata B Rao , Jacob Pan , Ashok Raj , Linus Torvalds , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Weihong Zhang , "Kirill A . Shutemov" Subject: [PATCHv16 13/17] selftests/x86/lam: Add mmap and SYSCALL test cases for linear-address masking Date: Sun, 12 Mar 2023 14:26:08 +0300 Message-Id: <20230312112612.31869-14-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230312112612.31869-1-kirill.shutemov@linux.intel.com> References: <20230312112612.31869-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: kgc11fzofcexmqe3e3tsb537si18m9eg X-Rspamd-Queue-Id: A1488A0015 X-HE-Tag: 1678620399-803997 X-HE-Meta: U2FsdGVkX1+GLH+qVx+hbUjAIzEth1MysukNpTyoFOGZG+APFwODiW5hpMymX2e47pVs4XeahedRTkHut9HCeXOJmvQ/MMuDPd6T/7F17eGVe4AsoEdJX8fWwfp/QAzpiyts9DMqaz5dlpynmBHTvgB11VV57ylkMSvPPg3tSiBqk6MErvxLjWie0lV5Xq+esq2tHUWwQzYv09jB50aUNtGxkIyx9+ZVERYq7Kt9JDTS8woWirg9uTt+3a3QNH7uXaxPJvrYmeU/Miwyb6oxOOhsBdwL0T8RiKgz+C9agEcw9zTxQJwTf5+HhsqJVKfGARnBjIAfuteGD20/dDi1VNJww/29isA1+aL8okY1LrUpzpfcy9CsIL1pnfMsvROv29y4P0SRBqaAk5cRQVrHGqZ75k+pDn5LCim99JFns+7SMuyRPHq1hTmtWdB4JFQpqP1P9FHqM52OwiypKaXFvqDCur15C/sAcwFmepdlKEzHdREX4bVByTCqIzNEfNP/HVqjx9INOXaSjpAKea1hYqZl2uVW+k1h0uRj+fEjdfQcObjAMGP/55PRR0BCb2CyQYii/6yAvS/ZTTQqcX11fjtoD8xpOmDrPxeS4aAjd6dM1HnGeI+FoCPVChbNGNwTf8YAMjUBaYt+/acYciHJoXPQd+MOrHPy25xi9eoqCaZWBPMenDW4AuL5m9CNt4t4yx4Estf1uqxy7eSrnLZLmRIsL8DG/d0OaLRcmG0gKIwRqeRzWgqd7rFM9kdQgsicVzbD5OgBhrtGNJRgFNlNGkzPNnrEaT24EUm4Zl5kEIUpf4eCBtvpNwic6SOJA69Xm7A/61WgjFDCAjpeWTqP5UZj7xB028XfawSagUo19nHJr8hIMSRe/d0jfcPEQWzd4QCHj62IQYcOB3wHrCxmG7BcQYbNEAgndCzDUnsOe64kmEH1rsdn//pkn2w8ZXDvSjTH0TOsb6ciuv2EDsr +HOm1rvG xA5njs1PrlyCXwZPl/oNFreyCBIReXKq679/+Ds6C0VnAwIAc8O/osW7/fZCO1EPZLwMhQBIgffapBJdsn8IQ6C6mnHvonui9qbau6V1m75CMFyueYPej1iTKuW7sMkV21BSPWsGb+rlR38+hjr7Dee+yFUrll5cPPaI07Qq0FL8qw85NF+tfNlwThaHl1sEBXdxn3cIQ4vMINDlM4OGXwKMQjEj26/PQznf9rzlm0DhAS2nC1hfEJjdKRLMq+hV1NYIWBKddg+U9zmQoI8viqQh4GR6hXySKfwlt 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: From: Weihong Zhang Add mmap and SYSCALL test cases. SYSCALL test cases: - LAM supports set metadata in high bits 62:57 (LAM_U57) of a user pointer, pass the pointer to SYSCALL, SYSCALL can dereference the pointer and return correct result. - Disable LAM, pass a pointer with metadata in high bits to SYSCALL, SYSCALL returns -1 (EFAULT). MMAP test cases: - Enable LAM_U57, MMAP with low address (below bits 47), set metadata in high bits of the address, dereference the address should be allowed. - Enable LAM_U57, MMAP with high address (above bits 47), set metadata in high bits of the address, dereference the address should be allowed. Signed-off-by: Weihong Zhang Signed-off-by: Kirill A. Shutemov Acked-by: Peter Zijlstra (Intel) --- tools/testing/selftests/x86/lam.c | 144 +++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c index 268c1d2749af..39ebfc511685 100644 --- a/tools/testing/selftests/x86/lam.c +++ b/tools/testing/selftests/x86/lam.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -29,11 +30,18 @@ /* Specified test function bits */ #define FUNC_MALLOC 0x1 #define FUNC_BITS 0x2 +#define FUNC_MMAP 0x4 +#define FUNC_SYSCALL 0x8 -#define TEST_MASK 0x3 +#define TEST_MASK 0xf + +#define LOW_ADDR (0x1UL << 30) +#define HIGH_ADDR (0x3UL << 48) #define MALLOC_LEN 32 +#define PAGE_SIZE (4 << 10) + struct testcases { unsigned int later; int expected; /* 2: SIGSEGV Error; 1: other errors */ @@ -49,6 +57,7 @@ jmp_buf segv_env; static void segv_handler(int sig) { ksft_print_msg("Get segmentation fault(%d).", sig); + siglongjmp(segv_env, 1); } @@ -61,6 +70,16 @@ static inline int cpu_has_lam(void) return (cpuinfo[0] & (1 << 26)); } +/* Check 5-level page table feature in CPUID.(EAX=07H, ECX=00H):ECX.[bit 16] */ +static inline int cpu_has_la57(void) +{ + unsigned int cpuinfo[4]; + + __cpuid_count(0x7, 0, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]); + + return (cpuinfo[2] & (1 << 16)); +} + /* * Set tagged address and read back untag mask. * check if the untagged mask is expected. @@ -213,6 +232,68 @@ static int handle_malloc(struct testcases *test) return ret; } +static int handle_mmap(struct testcases *test) +{ + void *ptr; + unsigned int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED; + int ret = 0; + + if (test->later == 0 && test->lam != 0) + if (set_lam(test->lam) != 0) + return 1; + + ptr = mmap((void *)test->addr, PAGE_SIZE, PROT_READ | PROT_WRITE, + flags, -1, 0); + if (ptr == MAP_FAILED) { + if (test->addr == HIGH_ADDR) + if (!cpu_has_la57()) + return 3; /* unsupport LA57 */ + return 1; + } + + if (test->later != 0 && test->lam != 0) + if (set_lam(test->lam) != 0) + ret = 1; + + if (ret == 0) { + if (sigsetjmp(segv_env, 1) == 0) { + signal(SIGSEGV, segv_handler); + ret = handle_lam_test(ptr, test->lam); + } else { + ret = 2; + } + } + + munmap(ptr, PAGE_SIZE); + return ret; +} + +static int handle_syscall(struct testcases *test) +{ + struct utsname unme, *pu; + int ret = 0; + + if (test->later == 0 && test->lam != 0) + if (set_lam(test->lam) != 0) + return 1; + + if (sigsetjmp(segv_env, 1) == 0) { + signal(SIGSEGV, segv_handler); + pu = (struct utsname *)set_metadata((uint64_t)&unme, test->lam); + ret = uname(pu); + if (ret < 0) + ret = 1; + } else { + ret = 2; + } + + if (test->later != 0 && test->lam != 0) + if (set_lam(test->lam) != -1 && ret == 0) + ret = 1; + + return ret; +} + static int fork_test(struct testcases *test) { int ret, child_ret; @@ -241,13 +322,20 @@ static void run_test(struct testcases *test, int count) struct testcases *t = test + i; /* fork a process to run test case */ + tests_cnt++; ret = fork_test(t); + + /* return 3 is not support LA57, the case should be skipped */ + if (ret == 3) { + ksft_test_result_skip(t->msg); + continue; + } + if (ret != 0) ret = (t->expected == ret); else ret = !(t->expected); - tests_cnt++; ksft_test_result(ret, t->msg); } } @@ -268,7 +356,6 @@ static struct testcases malloc_cases[] = { }, }; - static struct testcases bits_cases[] = { { .test_func = handle_max_bits, @@ -276,11 +363,54 @@ static struct testcases bits_cases[] = { }, }; +static struct testcases syscall_cases[] = { + { + .later = 0, + .lam = LAM_U57_BITS, + .test_func = handle_syscall, + .msg = "SYSCALL: LAM_U57. syscall with metadata\n", + }, + { + .later = 1, + .expected = 1, + .lam = LAM_U57_BITS, + .test_func = handle_syscall, + .msg = "SYSCALL:[Negative] Disable LAM. Dereferencing pointer with metadata.\n", + }, +}; + +static struct testcases mmap_cases[] = { + { + .later = 1, + .expected = 0, + .lam = LAM_U57_BITS, + .addr = HIGH_ADDR, + .test_func = handle_mmap, + .msg = "MMAP: First mmap high address, then set LAM_U57.\n", + }, + { + .later = 0, + .expected = 0, + .lam = LAM_U57_BITS, + .addr = HIGH_ADDR, + .test_func = handle_mmap, + .msg = "MMAP: First LAM_U57, then High address.\n", + }, + { + .later = 0, + .expected = 0, + .lam = LAM_U57_BITS, + .addr = LOW_ADDR, + .test_func = handle_mmap, + .msg = "MMAP: First LAM_U57, then Low address.\n", + }, +}; + static void cmd_help(void) { printf("usage: lam [-h] [-t test list]\n"); printf("\t-t test list: run tests specified in the test list, default:0x%x\n", TEST_MASK); - printf("\t\t0x1:malloc; 0x2:max_bits;\n"); + printf("\t\t0x1:malloc; 0x2:max_bits; 0x4:mmap; 0x8:syscall.\n"); printf("\t-h: help\n"); } @@ -320,6 +450,12 @@ int main(int argc, char **argv) if (tests & FUNC_BITS) run_test(bits_cases, ARRAY_SIZE(bits_cases)); + if (tests & FUNC_MMAP) + run_test(mmap_cases, ARRAY_SIZE(mmap_cases)); + + if (tests & FUNC_SYSCALL) + run_test(syscall_cases, ARRAY_SIZE(syscall_cases)); + ksft_set_plan(tests_cnt); return ksft_exit_pass();