From nobody Sun Feb 8 18:32:31 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09D81C6FD1C for ; Wed, 22 Mar 2023 17:04:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231393AbjCVREB (ORCPT ); Wed, 22 Mar 2023 13:04:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231215AbjCVRDa (ORCPT ); Wed, 22 Mar 2023 13:03:30 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C72B41084; Wed, 22 Mar 2023 10:03:10 -0700 (PDT) Date: Wed, 22 Mar 2023 17:03:05 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1679504586; h=from:from:sender:sender:reply-to: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; bh=7cTypdVlEk+cUcIVwRk8ebFLuEnuuETWIvg7bhwhzjI=; b=4NU7D+ZClc7Yky2M4EGdfufkIAyS6aXeTwX14gfOVqvMcfA+GYf1McFdY6bTSsTBgPm71Y 1jxoL9oLw5fEM/mUuCKLo9EEzJcp7KhydUJuJIxdGBk+uycuMXLw5AiSY+B+WStqCkoqpo ZrCLrhSDOx9RR0wiUz3u56pmJr3zW/Fz3QocuseMgB0WJMK8blhR1bIRU1A6vX2LYGYK9Q ff1Lcu7BcSShl3hrboE20eJK4nCQVxNe03gvuUdlXzx357PgJKkb7t3HjRTpteztECCKbw wMY4F12XUIImyMQM40HEC3MjHzHOzf8AjjMh+PdrKdG6V9hk9SW/RouUAjJeEg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1679504586; h=from:from:sender:sender:reply-to: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; bh=7cTypdVlEk+cUcIVwRk8ebFLuEnuuETWIvg7bhwhzjI=; b=DXgfH08PvwcJ33+yMkWnjjh2ltvwUNSffB+Nk/jiRF5Q8DJbNGLaq5zv6ZiPbK91GoFnRw 55tzmB6J1u1JjWBQ== From: "tip-bot2 for Weihong Zhang" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/mm] selftests/x86/lam: Add mmap and SYSCALL test cases for linear-address masking Cc: Weihong Zhang , "Kirill A. Shutemov" , Dave Hansen , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <167950458566.5837.8050835433502044350.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/mm branch of tip: Commit-ID: e67876962ecfc5c8cb1d871d8f66ad3b21dbf065 Gitweb: https://git.kernel.org/tip/e67876962ecfc5c8cb1d871d8f66ad3b2= 1dbf065 Author: Weihong Zhang AuthorDate: Sun, 12 Mar 2023 14:26:08 +03:00 Committer: Dave Hansen CommitterDate: Thu, 16 Mar 2023 13:08:40 -07:00 selftests/x86/lam: Add mmap and SYSCALL test cases for linear-address maski= ng 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 c= orrect 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 Signed-off-by: Dave Hansen Acked-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/all/20230312112612.31869-14-kirill.shutemov%4= 0linux.intel.com --- 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/x8= 6/lam.c index 268c1d2..39ebfc5 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 =20 @@ -29,11 +30,18 @@ /* Specified test function bits */ #define FUNC_MALLOC 0x1 #define FUNC_BITS 0x2 +#define FUNC_MMAP 0x4 +#define FUNC_SYSCALL 0x8 =20 -#define TEST_MASK 0x3 +#define TEST_MASK 0xf + +#define LOW_ADDR (0x1UL << 30) +#define HIGH_ADDR (0x3UL << 48) =20 #define MALLOC_LEN 32 =20 +#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); } =20 @@ -61,6 +70,16 @@ static inline int cpu_has_lam(void) return (cpuinfo[0] & (1 << 26)); } =20 +/* Check 5-level page table feature in CPUID.(EAX=3D07H, ECX=3D00H):ECX.[b= it 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; } =20 +static int handle_mmap(struct testcases *test) +{ + void *ptr; + unsigned int flags =3D MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED; + int ret =3D 0; + + if (test->later =3D=3D 0 && test->lam !=3D 0) + if (set_lam(test->lam) !=3D 0) + return 1; + + ptr =3D mmap((void *)test->addr, PAGE_SIZE, PROT_READ | PROT_WRITE, + flags, -1, 0); + if (ptr =3D=3D MAP_FAILED) { + if (test->addr =3D=3D HIGH_ADDR) + if (!cpu_has_la57()) + return 3; /* unsupport LA57 */ + return 1; + } + + if (test->later !=3D 0 && test->lam !=3D 0) + if (set_lam(test->lam) !=3D 0) + ret =3D 1; + + if (ret =3D=3D 0) { + if (sigsetjmp(segv_env, 1) =3D=3D 0) { + signal(SIGSEGV, segv_handler); + ret =3D handle_lam_test(ptr, test->lam); + } else { + ret =3D 2; + } + } + + munmap(ptr, PAGE_SIZE); + return ret; +} + +static int handle_syscall(struct testcases *test) +{ + struct utsname unme, *pu; + int ret =3D 0; + + if (test->later =3D=3D 0 && test->lam !=3D 0) + if (set_lam(test->lam) !=3D 0) + return 1; + + if (sigsetjmp(segv_env, 1) =3D=3D 0) { + signal(SIGSEGV, segv_handler); + pu =3D (struct utsname *)set_metadata((uint64_t)&unme, test->lam); + ret =3D uname(pu); + if (ret < 0) + ret =3D 1; + } else { + ret =3D 2; + } + + if (test->later !=3D 0 && test->lam !=3D 0) + if (set_lam(test->lam) !=3D -1 && ret =3D=3D 0) + ret =3D 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 coun= t) struct testcases *t =3D test + i; =20 /* fork a process to run test case */ + tests_cnt++; ret =3D fork_test(t); + + /* return 3 is not support LA57, the case should be skipped */ + if (ret =3D=3D 3) { + ksft_test_result_skip(t->msg); + continue; + } + if (ret !=3D 0) ret =3D (t->expected =3D=3D ret); else ret =3D !(t->expected); =20 - tests_cnt++; ksft_test_result(ret, t->msg); } } @@ -268,7 +356,6 @@ static struct testcases malloc_cases[] =3D { }, }; =20 - static struct testcases bits_cases[] =3D { { .test_func =3D handle_max_bits, @@ -276,11 +363,54 @@ static struct testcases bits_cases[] =3D { }, }; =20 +static struct testcases syscall_cases[] =3D { + { + .later =3D 0, + .lam =3D LAM_U57_BITS, + .test_func =3D handle_syscall, + .msg =3D "SYSCALL: LAM_U57. syscall with metadata\n", + }, + { + .later =3D 1, + .expected =3D 1, + .lam =3D LAM_U57_BITS, + .test_func =3D handle_syscall, + .msg =3D "SYSCALL:[Negative] Disable LAM. Dereferencing pointer with met= adata.\n", + }, +}; + +static struct testcases mmap_cases[] =3D { + { + .later =3D 1, + .expected =3D 0, + .lam =3D LAM_U57_BITS, + .addr =3D HIGH_ADDR, + .test_func =3D handle_mmap, + .msg =3D "MMAP: First mmap high address, then set LAM_U57.\n", + }, + { + .later =3D 0, + .expected =3D 0, + .lam =3D LAM_U57_BITS, + .addr =3D HIGH_ADDR, + .test_func =3D handle_mmap, + .msg =3D "MMAP: First LAM_U57, then High address.\n", + }, + { + .later =3D 0, + .expected =3D 0, + .lam =3D LAM_U57_BITS, + .addr =3D LOW_ADDR, + .test_func =3D handle_mmap, + .msg =3D "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"); } =20 @@ -320,6 +450,12 @@ int main(int argc, char **argv) if (tests & FUNC_BITS) run_test(bits_cases, ARRAY_SIZE(bits_cases)); =20 + 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); =20 return ksft_exit_pass();