From patchwork Fri Nov 11 21:58:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 19005 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp985167wru; Fri, 11 Nov 2022 13:59:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf6DofKT+jAkpgSrPSyoUjLpKq8zVoiSjGQo+iOgKx9dBaxtSE95Z+zZiE2k6mIXT1A11yTF X-Received: by 2002:aa7:c95a:0:b0:458:b6dc:2b56 with SMTP id h26-20020aa7c95a000000b00458b6dc2b56mr3239080edt.412.1668203945848; Fri, 11 Nov 2022 13:59:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668203945; cv=none; d=google.com; s=arc-20160816; b=Rq/su1D1o6Ja/hTQ+h+Uw2NDFJONCrNL0SranTlyF8DzEQT4GgouVbdScQSkC2ZgKQ TunmKIaJsX6tdwQFMNm3NAOredKgQzX9R1x4/w3IFe+M17UEo40/Fimb/9Z0FhS0YJ0Z qOQaUPq9+JmGWTJhNg/F0aW9oUqZ7ziqJfpPdivMFXaXX2D8/AI+UkIwbBND03AnM0dZ AEKSshfgXNFAHeKbO1fShRvGtZLtma4vj3wHBn4rjAcuTPV8JsOm8cjmXVolI/Jsr1ys ZnYEgfB2M9HvsQkUz8T3o5v7PXNdhKO92xTOfSLvZDR/Q1q2F51tdINrIgKOcsKYLXOY HrRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=AakrqikpH8annH1JmSdshbJ0IgoBk5P3SChU/3KnNMw=; b=FHifdLN1/fbuHA/5xqGDCqrUvMIDM9VLdtdUWwkFc6r1Zq4raNwPCvpa+6ivwDbFUO UkczOVKxV4LOLVGdTW0clYDMOlGoeoFXCyx7zoCPwv1iUEsNdZutc587C8TuViC+CDt1 LyIv39izIUeVC7CrGzXnMF0FApZyhk6uz4pAobrEs6JyuoRZ5X/WtTUTvARa3E2iqZmX lq74dCRwTq4CmNdMVx+ApcOip7ANdbH64Jb9GiTk+ZddvNIH2G2uDADhFYhGs8j8GKld jHa8T/+VM1AfBBtwe57CMhHGFIoz83ZVOpMrXR3k60QZZeZv0CCjaASBZ/xvqNita+EY o5Qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=pbcwS4qY; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c2-20020a1709060fc200b007ae1723e8d7si2611651ejk.610.2022.11.11.13.58.41; Fri, 11 Nov 2022 13:59:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=pbcwS4qY; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233593AbiKKV6b (ORCPT + 99 others); Fri, 11 Nov 2022 16:58:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230105AbiKKV6Y (ORCPT ); Fri, 11 Nov 2022 16:58:24 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B20701DD; Fri, 11 Nov 2022 13:58:22 -0800 (PST) Date: Fri, 11 Nov 2022 21:58:19 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668203900; 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=AakrqikpH8annH1JmSdshbJ0IgoBk5P3SChU/3KnNMw=; b=pbcwS4qYMDRdv+vEa+Ajifj7d0gzqo70LOYK5I9zfDg8PbeVcyWc6HXcaVo+cuZrpWr40T 1qtpQii7p5Wx6+atC9BpjPNrTtWJRXEdzA9jZTHjUTBfCYxA/6xBD2hGp7NVSi04ZCfjdN mXCn9/WxVSyOTE8LWADR09b7njujfrhHKIb6x82tdCi7onRvlu5veSkdjAvNFA5Fdpt/wL BDtjVZn0v7Hs/HJd/ttWUHRMoV9ceArrOnMCgRfI9tqnfaW6iXqbThzAsHvO76Qnfm48NC CuOiR4yYMzN46p+tJbfw9J9phhNyabO9bgeUoiQ1dm7ZKT3cveTkaiJqY4CqzQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668203900; 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=AakrqikpH8annH1JmSdshbJ0IgoBk5P3SChU/3KnNMw=; b=z2PxnURn2OyMiCPOHp81U3QMIAr72WOO3t+frzLcPg1rrWTTv3qwpEcRkZWGrowCeXUqjs bP8E5jbQWwp7FdBg== 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 , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <166820389946.4906.16863903818809845382.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749238620403620414?= X-GMAIL-MSGID: =?utf-8?q?1749238620403620414?= The following commit has been merged into the x86/mm branch of tip: Commit-ID: ec5e0525225a92f339b485fe0b21eadfa44e1867 Gitweb: https://git.kernel.org/tip/ec5e0525225a92f339b485fe0b21eadfa44e1867 Author: Weihong Zhang AuthorDate: Wed, 09 Nov 2022 19:51:39 +03:00 Committer: Dave Hansen CommitterDate: Fri, 11 Nov 2022 13:29:56 -08: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 Link: https://lore.kernel.org/all/20221109165140.9137-16-kirill.shutemov%40linux.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/x86/lam.c index 8ea1fce..cfc9073 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 @@ out: 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();