From patchwork Tue Dec 27 03:08:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 13082049 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 D9C19C4708D for ; Tue, 27 Dec 2022 03:09:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1C5C8E000C; Mon, 26 Dec 2022 22:08:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B6EB48E0005; Mon, 26 Dec 2022 22:08:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D93F8E000C; Mon, 26 Dec 2022 22:08:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 234218E000C for ; Mon, 26 Dec 2022 22:08:56 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DBA16140717 for ; Tue, 27 Dec 2022 03:08:55 +0000 (UTC) X-FDA: 80286604230.30.A1AB355 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by imf13.hostedemail.com (Postfix) with ESMTP id F3EC820006 for ; Tue, 27 Dec 2022 03:08:53 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MESakWQw; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf13.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.151) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672110534; 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=YeALEwNO/xMU2Q7tN0wfJtLKXIf9sYx85DUZKQ3UlP0=; b=K5FTErzJnpkxl1fV595pF+iR0gYqop+ydOU3BlfDABPCtTkXV/mg9/bkLAq4w1rVaQ+1LY YShReVtXdFrBPcMNateCROv537SEQptaMN6IxBU+DpYeIJk36Yafacfhx9k2aO+cp3ibLK DUUlnnM/vx9nqGZkIxbQQmOKE1AKrFs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MESakWQw; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf13.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.151) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672110534; a=rsa-sha256; cv=none; b=JuofBPbk18d/rIDCx6mEdbbBUa8gZq568GWrFufujJAkEYvhQuW77gxjDCpOHk3XtgdYW6 OGyvuJlAq+9bQ61AtdunBqACTHCevasy/p6CwjxHpVplo2GoQSsDU6v9q9Dx30rxs/wLnm GPb6hGXB+d6n4YqLEqKSaCakxRP1N9Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1672110534; x=1703646534; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Xl8eyXVMqbT7yHbfHamtG0/Bq0s3VLeHRKgBe7wTJ3s=; b=MESakWQwF05n6mkVAhoj8CPdefWytE5uLPCEIVBvy6hTc/X+zfLfo+Qs M8gSTYgOSG2oXpMihXMZNcV/FcGr5+hBIjf7vZo0fwdvfZ9Fpq/wABCai DReBdwrPZkNVtkBVhED8tZi3HFTjg4izS4TCui7j061TVVOa5YBsNPP8a dy1ULiw4JvLUvLqIU5X3KbVPgSBNcfgMCAvglLTmADoNZDhglPiia/VWd zdUH0hfaMZnRsGWoeNexthKO0PYSNEO5j7//wGuTT8c/D9tcQmbGiaIwC IXSDOSXJag0tZQECxrabOJq0F5dtVv4SNGdZpD9GUrKHjCSq478Jn4Uyv A==; X-IronPort-AV: E=McAfee;i="6500,9779,10572"; a="300994728" X-IronPort-AV: E=Sophos;i="5.96,277,1665471600"; d="scan'208";a="300994728" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Dec 2022 19:08:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10572"; a="741605226" X-IronPort-AV: E=Sophos;i="5.96,277,1665471600"; d="scan'208";a="741605226" Received: from ppogotov-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.62.152]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Dec 2022 19:08:48 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 17E9B10BBBB; Tue, 27 Dec 2022 06:08:37 +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: [PATCHv13 14/16] selftests/x86/lam: Add inherit test cases for linear-address masking Date: Tue, 27 Dec 2022 06:08:27 +0300 Message-Id: <20221227030829.12508-15-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20221227030829.12508-1-kirill.shutemov@linux.intel.com> References: <20221227030829.12508-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: F3EC820006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 5x4885d73g716dyj6htcuh1gwyi37dgo X-HE-Tag: 1672110533-501902 X-HE-Meta: U2FsdGVkX1855coNgVsjD7BNHxhxxUDMkB1TwZOwj7x1vQpkxHMHNcgIZrnEDonAiNZ2iqLkxZEpoqeP3XqpBVOWIin+HOt35VLy7iYcjnLRr1SAzfBmY7Ayv0L25cGFL+XIGfRO7o56KLEH7EqLnJ+9eZFobXq7Mk62mycjqL87dG8RZK1Jfns4cafrb/V66UM7f3UrrZBOkskKyIZJDQjID864AZQZB/29v3mTt3AmEqqKrbP6IqK9spV/c/1Eua29M1Q4o8jDhckbWy2sxEZNjR588kpID1j5FxxWNmY90DCqoAgffSIZ6G2OUKBBIJbw/f+mvdww9nRxJFr3tUkhpyOYJryGurfL5aldtN/d633/f4uocT0aV+lWw9rX59OmXDMJrfI5Ct+qeXO3Ht/M5EjERGGn+F7QuqkKpvZfGKsTdYvsU7oNPDl1k2pkGqT/sZadb9TRWIL9VGKIxbZHE1KkzkBZRpJxrQJ47SvbBKNbGC14I9PXCukG4wc72h6rpJCcfV0pwbBEpYxy9ptGILfCGroqRXeesDTvmoVPcaUBwi+wcl0pXytEHPQ5aAY0VOuMpewW59P53yRKNMtNkUOmtq1dbHFvW/BAsQWjM+zAFdaJ5Hy4FW5VqdqOSVds5KE9V3G4oKA2J9m0vAr6RbAbZRxI5XAOnAxzZ2Z/VmYxIkXTls6WQAqZ5bYKuyjP83OFJzQqT1MgL2nGbLvA+KjJOQ/uhEuxuF6yGtD1pupQRfoRn4ib80JCtY9eL+H9LB5VJqW3p3nsjrz0ClZl9qBDYemM3SlSdmfl7AfMQAE3swmbalamS2olUhZ93cMdBCwxyiaikDzg8459Jvdy3KxbUEJF4fjpGz00Pabh8V5NrnC1q/lMEMtrTg267KGfgtyClHRpC5SuIT2ndK3TOqN1GO0IuYwY4XdfUB89Y/Hk161ldldAZxTKKOCpHyZuRameIJg= 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 --- 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 8ea1fcef4c9f..cfc9073c0262 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();