From patchwork Sun Mar 12 11:26:10 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: 13171312 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 9CCF0C6FD1F for ; Sun, 12 Mar 2023 11:27:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2ADD8E000E; Sun, 12 Mar 2023 07:27:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EBFA28E0015; Sun, 12 Mar 2023 07:27:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C98DE8E000E; Sun, 12 Mar 2023 07:27:13 -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 A35C58E000E for ; Sun, 12 Mar 2023 07:27:13 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8615A1A0835 for ; Sun, 12 Mar 2023 11:27:13 +0000 (UTC) X-FDA: 80560019946.12.B11A56B Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf05.hostedemail.com (Postfix) with ESMTP id 48F1F100012 for ; Sun, 12 Mar 2023 11:27:11 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=I5PqcuJC; spf=none (imf05.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.31) 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=1678620431; 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=99fwD7k8d8h6LPp7vh7F5Wrp1FzWfOzQVN1wtAJK6DE=; b=QFPPs3Fqc2aQ8jh0SeuuxhE2iH9+T7IU7JYVX3omlK878WTCMMzisOCsjcvfNgUH0w/s9j icM9rXX6HX7BM0iCWbv9IDmM3Pdpm+8e5nJLj4LtL5GGMfLDlYKmq0na70L+Xgn1s/oelq pQsVGjznFNu8bGU2fBJba9R+R0aHpoM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=I5PqcuJC; spf=none (imf05.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.31) 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=1678620431; a=rsa-sha256; cv=none; b=pWdj1idLYFej8+MllBIspMqI14mn/iOmWvWFKXzMeNvucZsdIZ9F02fue2R+TJzNVa26Kj VVpISq+o5oarVFdUoZjaDCGRe13pJcn0asi33fy1O4wbLWJkJE9C13YX8xvm8907w3fIoW 1oz8IKCC/cmA/Lct92AA13HqpHUcS9M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678620431; x=1710156431; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rOpAXZn5KS9JrkJNB2EKQ1dUWIxcgetG1myY1VbHNB4=; b=I5PqcuJCVb70BYIuPQUvz7HUOfqSHfoi/4ywbY6IUomjheUo1DUwjH0Z V4kLFreTlxzOAY75BtMzKWio6Lt1qhEPr4tDxs9KW4QBeD9HUkPmvANfA bs9QlkOv1GGD9AFp2GRlbsMf9EPSUzLKlo7r29h7zbO8yr/VsOH8Op3ro 6MJRoav4/5yE9FJ5ujy4wfoivbVMtb1HpD6s111g+NTdErMTn29Y0dMAj NW/GaF9sDoC1H5+AZNnh/Vxmf32rzXuRbRPPi+NMyD4qBY8lyBHKc5ENc r38prlv2/F6NvzcHY0rdbmc6CIIC2VNcY4ODGOcAmEMKKSYCTD106rIl/ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="399577677" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="399577677" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2023 04:26:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="671580759" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="671580759" Received: from nmoazzen-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.219.215]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2023 04:26:38 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id ECA9D10D7BD; 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 15/17] selftests/x86/lam: Add inherit test cases for linear-address masking Date: Sun, 12 Mar 2023 14:26:10 +0300 Message-Id: <20230312112612.31869-16-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-Stat-Signature: tagobsbx7j9ej7y8xmb59prp5oe439cb X-Rspam-User: X-Rspamd-Queue-Id: 48F1F100012 X-Rspamd-Server: rspam06 X-HE-Tag: 1678620431-343841 X-HE-Meta: U2FsdGVkX1+FNHpPP3GwsB1mhSGgKuxOi9k3hVg2QL+OYQk97VvYkDUZ2wMEuXrJlgPzEjbtaM1WEopTp1pKbHOpn2tDDKXeRLSzsk8ZGWDW6Aold29r4CNwTmbOdSZ9au0g7suf2XD2WuhfOxKOdbTrVH2Rm91ORiWwtiZQ8qRrYroTQq27MJRl0WviLhmWVTHi4Wn1BfapCt6xx1zyzCBAjQ89QS8NtE5WWIFfz7qEa9wS/wq+YoazBIrLxXzMQwY9qWokkmDvE8lDzF2y4vzk9K+HvSSet7bK3nQPO7Xd4YsDwWCYPvMOSW3Ojrijbhp8+fKPtCzQBmdn/V09c3kmC5VL6ilIo0jqezQHMr2ydq0MpHgPECrJFZUI6icMrXsVhmn1mL7QxyTZ8XgTT068jwOfOat61gt9vcIwJ/MRyjFW28fxRVDuHx1g1NVuIkaQlcjonkDBmdQu7pZixO4fyI6mXQycInB6kcKj7FsHBmyieGVFeiJ+bQi8cuYE3D3Na5/BW+UIX5/GkTAEq8QlKBdENBL0iweUOamAM4abiCafMIHTqENdNc+M0F3i5352jnADzJKPuxdFmxbI9bE75ryjqnd/4tAFVk8wwDEebvbzcJWceuc7Rse3A3PKaA4O4EaA8GmECYbopQyCtzXWyTUSVr15zvbjQVL46csfGh1XmRzSBY2vXBVk1LtzjjhvIOEhwC/hDO7l930ZbkdEqmbSeM1XXcsEHQFiax7YYlixcNM9IF66BDILWs5cP1j4pN2crg23U/50Urv+lsrUJl2/1AfHspDCKnfPlZHMDDKB8rN8NGnfk7REgPpVNzijb1TXkLgo2P8eBYBOH+R9lJKdgY60x55yoMOPKSZg+TAWaC0/mQhY3c6a1yv78zDLwuwlQiPdOJm42TnV+LBYcx4ZwFmSxL5nXxNEoaTfgCqcUt+ScV3aNdzqfW9VQVaEP6vGvJ5UbAaDzy9 NRR21Gd3 SihMc9QGEMz9mPz6mHcpiuC1cVgRpVQWwVp3sxFtzGUWBXSfW9ONLcIltL+t0ZM3aWJ946sMGLfsmZFa9LkrqHVlmBa46Qr+wwXOUUbuHpWG5nzzurV2vHN0/bwoRjY936rRpfILR8e2Vw4xvh7UK8dIPcmKILLPQxQ+7vnFWlb5FzoT+smnlf53WQqjTzm269eNyMz6oFpZLVMO+xMoxk6sk4vNoVnWbrMwgluhdDpf+TTSbugttY1Mona/uH3bX7Qmpx+FzYnhrTRnnLElaI98wBf2btPd4xZqC 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 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 Acked-by: Peter Zijlstra (Intel) --- 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/x86/lam.c index 52750ebd0887..ebabd4333b7d 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 -#define TEST_MASK 0x1f +#define TEST_MASK 0x3f #define LOW_ADDR (0x1UL << 30) #define HIGH_ADDR (0x3UL << 48) @@ -174,6 +175,28 @@ static unsigned long get_default_tag_bits(void) return lam; } +/* + * Set tagged address and read back untag mask. + * check if the untag mask is expected. + */ +static int get_lam(void) +{ + uint64_t ptr = 0; + int ret = -1; + /* Get untagged mask */ + if (syscall(SYS_arch_prctl, ARCH_GET_UNTAG_MASK, &ptr) == -1) + return -1; + + /* Check mask returned is expected */ + if (ptr == ~(LAM_U57_MASK)) + ret = LAM_U57_BITS; + else if (ptr == -1ULL) + ret = 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 @@ int do_uring(unsigned long lam) switch (lam) { case LAM_U57_BITS: /* Clear bits 62:57 */ - addr = (addr & ~(0x3fULL << 57)); + addr = (addr & ~(LAM_U57_MASK)); break; } free((void *)addr); @@ -632,6 +655,72 @@ static int fork_test(struct testcases *test) return ret; } +static int handle_execve(struct testcases *test) +{ + int ret, child_ret; + int lam = test->lam; + pid_t pid; + + pid = fork(); + if (pid < 0) { + perror("Fork failed."); + ret = 1; + } else if (pid == 0) { + char path[PATH_MAX]; + + /* Set LAM mode in parent process */ + if (set_lam(lam) != 0) + return 1; + + /* Get current binary's path and the binary was run by execve */ + if (readlink("/proc/self/exe", path, PATH_MAX) <= 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 = WEXITSTATUS(child_ret); + if (ret != LAM_NONE) + return 1; + } + + return 0; +} + +static int handle_inheritance(struct testcases *test) +{ + int ret, child_ret; + int lam = test->lam; + pid_t pid; + + /* Set LAM mode in parent process */ + if (set_lam(lam) != 0) + return 1; + + pid = fork(); + if (pid < 0) { + perror("Fork failed."); + return 1; + } else if (pid == 0) { + /* Set LAM mode in parent process */ + int child_lam = get_lam(); + + exit(child_lam); + } else { + wait(&child_ret); + ret = WEXITSTATUS(child_ret); + + if (lam != ret) + return 1; + } + + return 0; +} + static void run_test(struct testcases *test, int count) { int i, ret = 0; @@ -740,11 +829,26 @@ static struct testcases mmap_cases[] = { }, }; +static struct testcases inheritance_cases[] = { + { + .expected = 0, + .lam = LAM_U57_BITS, + .test_func = handle_inheritance, + .msg = "FORK: LAM_U57, child process should get LAM mode same as parent\n", + }, + { + .expected = 0, + .lam = LAM_U57_BITS, + .test_func = handle_execve, + .msg = "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_uring.\n"); + printf("\t\t0x1:malloc; 0x2:max_bits; 0x4:mmap; 0x8:syscall; 0x10:io_uring; 0x20:inherit;\n"); printf("\t-h: help\n"); } @@ -764,7 +868,7 @@ int main(int argc, char **argv) switch (c) { case 't': tests = 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) } } + /* + * 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)); @@ -793,6 +907,9 @@ int main(int argc, char **argv) if (tests & FUNC_URING) run_test(uring_cases, ARRAY_SIZE(uring_cases)); + if (tests & FUNC_INHERITE) + run_test(inheritance_cases, ARRAY_SIZE(inheritance_cases)); + ksft_set_plan(tests_cnt); return ksft_exit_pass();