From nobody Sun Feb 8 15:46:34 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 1C308C6FD1C for ; Wed, 22 Mar 2023 17:04:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231387AbjCVRD6 (ORCPT ); Wed, 22 Mar 2023 13:03:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231230AbjCVRDa (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 AD3CB3E1CF; Wed, 22 Mar 2023 10:03:09 -0700 (PDT) Date: Wed, 22 Mar 2023 17:03:04 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1679504585; 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=qIOQQ6QAOhmEc1Jv3UNJoeUJNGrkot34nIu0dzXOFaA=; b=jP64/fpYNpaH4RgGfdyvszLN1CGkPfLiWbYjwFIPDgV52rxVIABCy9HUJ3EoV41MtiwY38 g6Xo0w18bgrMwc4SbwOMb/y+/xPT7Vtu27XNIkFBULKNvpWtINUzk3/xP9sHGI/ZCfNdI+ UpUOi2ildz4EKXWnOy9TXwSrK5gYZQTkPSeSsjTd4nERKk6/K+KY4IVSI9uBQ30zOKXZUE YXh8YmiipMeKNJt2fO5Hn7QpSasX2+CdrNIeu3+21/d0rZ3MUTja0GVBipoERU7balGd5Y 8WtcrYeWieHK48v7PT2eHr3erN3IywNyH017Eo+IxgtfnvN6erXZDVwlWDycVQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1679504585; 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=qIOQQ6QAOhmEc1Jv3UNJoeUJNGrkot34nIu0dzXOFaA=; b=KDU6M4UzogOhwYj2St5lJo8ly04Uhzc7C0kOMxgNMiNIZkKT8SjI0QYCFahZ2c2ejntUrS B/MXXGnonnpQUFCw== 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 inherit 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: <167950458499.5837.336137416334759518.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: 833c12ce0f4307675beb60b194833c6c7cb506d7 Gitweb: https://git.kernel.org/tip/833c12ce0f4307675beb60b194833c6c7= cb506d7 Author: Weihong Zhang AuthorDate: Sun, 12 Mar 2023 14:26:10 +03:00 Committer: Dave Hansen CommitterDate: Thu, 16 Mar 2023 13:08:40 -07:00 selftests/x86/lam: Add inherit test cases for linear-address masking LAM is enabled per-thread and gets inherited on fork(2)/clone(2). exec() reverts LAM status to the default disabled state. There are two test scenarios: - Fork test cases: These cases were used to test the inheritance of LAM for per-thread, Child process generated by fork() should inherit LAM feature from parent process, Child process can get the LAM mode same as parent process. - Execve test cases: Processes generated by execve() are different from processes generated by fork(), these processes revert LAM status to disabled status. 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-16-kirill.shutemov%4= 0linux.intel.com --- tools/testing/selftests/x86/lam.c | 125 ++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x8= 6/lam.c index 52750eb..ebabd43 100644 --- a/tools/testing/selftests/x86/lam.c +++ b/tools/testing/selftests/x86/lam.c @@ -37,8 +37,9 @@ #define FUNC_MMAP 0x4 #define FUNC_SYSCALL 0x8 #define FUNC_URING 0x10 +#define FUNC_INHERITE 0x20 =20 -#define TEST_MASK 0x1f +#define TEST_MASK 0x3f =20 #define LOW_ADDR (0x1UL << 30) #define HIGH_ADDR (0x3UL << 48) @@ -174,6 +175,28 @@ static unsigned long get_default_tag_bits(void) return lam; } =20 +/* + * Set tagged address and read back untag mask. + * check if the untag mask is expected. + */ +static int get_lam(void) +{ + uint64_t ptr =3D 0; + int ret =3D -1; + /* Get untagged mask */ + if (syscall(SYS_arch_prctl, ARCH_GET_UNTAG_MASK, &ptr) =3D=3D -1) + return -1; + + /* Check mask returned is expected */ + if (ptr =3D=3D ~(LAM_U57_MASK)) + ret =3D LAM_U57_BITS; + else if (ptr =3D=3D -1ULL) + ret =3D LAM_NONE; + + + return ret; +} + /* According to LAM mode, set metadata in high bits */ static uint64_t set_metadata(uint64_t src, unsigned long lam) { @@ -581,7 +604,7 @@ out: =20 switch (lam) { case LAM_U57_BITS: /* Clear bits 62:57 */ - addr =3D (addr & ~(0x3fULL << 57)); + addr =3D (addr & ~(LAM_U57_MASK)); break; } free((void *)addr); @@ -632,6 +655,72 @@ static int fork_test(struct testcases *test) return ret; } =20 +static int handle_execve(struct testcases *test) +{ + int ret, child_ret; + int lam =3D test->lam; + pid_t pid; + + pid =3D fork(); + if (pid < 0) { + perror("Fork failed."); + ret =3D 1; + } else if (pid =3D=3D 0) { + char path[PATH_MAX]; + + /* Set LAM mode in parent process */ + if (set_lam(lam) !=3D 0) + return 1; + + /* Get current binary's path and the binary was run by execve */ + if (readlink("/proc/self/exe", path, PATH_MAX) <=3D 0) + exit(-1); + + /* run binary to get LAM mode and return to parent process */ + if (execlp(path, path, "-t 0x0", NULL) < 0) { + perror("error on exec"); + exit(-1); + } + } else { + wait(&child_ret); + ret =3D WEXITSTATUS(child_ret); + if (ret !=3D LAM_NONE) + return 1; + } + + return 0; +} + +static int handle_inheritance(struct testcases *test) +{ + int ret, child_ret; + int lam =3D test->lam; + pid_t pid; + + /* Set LAM mode in parent process */ + if (set_lam(lam) !=3D 0) + return 1; + + pid =3D fork(); + if (pid < 0) { + perror("Fork failed."); + return 1; + } else if (pid =3D=3D 0) { + /* Set LAM mode in parent process */ + int child_lam =3D get_lam(); + + exit(child_lam); + } else { + wait(&child_ret); + ret =3D WEXITSTATUS(child_ret); + + if (lam !=3D ret) + return 1; + } + + return 0; +} + static void run_test(struct testcases *test, int count) { int i, ret =3D 0; @@ -740,11 +829,26 @@ static struct testcases mmap_cases[] =3D { }, }; =20 +static struct testcases inheritance_cases[] =3D { + { + .expected =3D 0, + .lam =3D LAM_U57_BITS, + .test_func =3D handle_inheritance, + .msg =3D "FORK: LAM_U57, child process should get LAM mode same as paren= t\n", + }, + { + .expected =3D 0, + .lam =3D LAM_U57_BITS, + .test_func =3D handle_execve, + .msg =3D "EXECVE: LAM_U57, child process should get disabled LAM mode\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; 0x4:mmap; 0x8:syscall; 0x10:io_urin= g.\n"); + printf("\t\t0x1:malloc; 0x2:max_bits; 0x4:mmap; 0x8:syscall; 0x10:io_urin= g; 0x20:inherit;\n"); printf("\t-h: help\n"); } =20 @@ -764,7 +868,7 @@ int main(int argc, char **argv) switch (c) { case 't': tests =3D strtoul(optarg, NULL, 16); - if (!(tests & TEST_MASK)) { + if (tests && !(tests & TEST_MASK)) { ksft_print_msg("Invalid argument!\n"); return -1; } @@ -778,6 +882,16 @@ int main(int argc, char **argv) } } =20 + /* + * When tests is 0, it is not a real test case; + * the option used by test case(execve) to check the lam mode in + * process generated by execve, the process read back lam mode and + * check with lam mode in parent process. + */ + if (!tests) + return (get_lam()); + + /* Run test cases */ if (tests & FUNC_MALLOC) run_test(malloc_cases, ARRAY_SIZE(malloc_cases)); =20 @@ -793,6 +907,9 @@ int main(int argc, char **argv) if (tests & FUNC_URING) run_test(uring_cases, ARRAY_SIZE(uring_cases)); =20 + if (tests & FUNC_INHERITE) + run_test(inheritance_cases, ARRAY_SIZE(inheritance_cases)); + ksft_set_plan(tests_cnt); =20 return ksft_exit_pass();