Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2102831rwb; Sun, 14 Aug 2022 21:31:39 -0700 (PDT) X-Google-Smtp-Source: AA6agR4coV2Ne6Tm+cpEs1NG8HkGP5NmGbVEo4pucuDwmsfDTYfH53GABkfNdOoxYh7mFr3NM6eU X-Received: by 2002:a17:907:1c89:b0:734:d05c:582e with SMTP id nb9-20020a1709071c8900b00734d05c582emr9139587ejc.282.1660537899497; Sun, 14 Aug 2022 21:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660537899; cv=none; d=google.com; s=arc-20160816; b=CfNgp1N6ZXj58RAyeAtM+x6cdnLvC0m4k+O46EVujRdGcrFRA3zKvdINYQjRBtDMhG 2DRXi/J4otz081hwA/UT9XFPdM2SHtqF+54hIwj5jrWoPx/iO5SC6PrBeNS0sWGwL4t0 wgouznBPhEpih1/UDyjyg6oURBM4m6KN/rYJTX68Fkf64adwcpaP3JoWcD0IDcvOq4M6 w04457pR0f4smRGEIC9m2hMwNtJAgqeX/QVWt5+EeribThQDYc+QEynOeQiZNLFGgXBY YmKgPn0SDG2CuWhU1vepPHB2zbWyUa7VtBBU2wX5PkZKYWVV/yFiINgh/EuJ/3/DhP8B Qrpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tNVwVMDWCNh7LL6M9owTA0mqJAUzvGbe+Y5YS5r/1Ok=; b=S4h1cBZaKZY0JN2vRBZ6NkP3R31aAyOhjGxSRt6C6OD6OdWntfqjLkU6yJSrk7UqmM ls9hlX9u650ZVMHnv5seg1bHbK4mDNBqiT2wK+0j+sGlejDkzh0b9oMy+z/azt2EIqbZ NGkPG0pY9mQx3gk0wyoJJIWCHfRAHeFf+Gm7HMvxwGNyunvMIvri4KKgmmUob+V3Dhsj xYuV/I7PPTppkwEFwoP4yU+tL/lt/UOmw9ZdooVyLKI10dEQTsMboZf+PdgOOKCd8uCr rd8YpJQs/nFpulDNBMgO68a2LYzK7IJBV/scV1EatqkzyQtv2X3kgFeJ9QoMtrt+Ucnm F7kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GBIXTVBM; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id en27-20020a056402529b00b00443dbda68c1si421664edb.176.2022.08.14.21.31.14; Sun, 14 Aug 2022 21:31:39 -0700 (PDT) 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=@intel.com header.s=Intel header.b=GBIXTVBM; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240986AbiHOEPr (ORCPT + 99 others); Mon, 15 Aug 2022 00:15:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240604AbiHOEPW (ORCPT ); Mon, 15 Aug 2022 00:15:22 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CCAC14085 for ; Sun, 14 Aug 2022 21:15:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660536912; x=1692072912; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IDZAMF3H0jOJUSFvgSQfeFD1dZ2psaQLK4sileStRHs=; b=GBIXTVBMC1mJPETz0E4WI9uAwJNGBy8dqLiM5IeaI7LZWDWlW97gRhcj jjgKxaWfPDiXG1w5Hq9wHoG7qPcA8DouvsXFHTXntvXYMfcsQQY1N/vLH ol3I70kB1AeMgBD4bdsESXLW20oBLl0QLKEri5v2IBHZ2e8ABpN8VxEeN bmLHRdYZYyUmHjwiWTAsOw7DNlJazrCGzGOd0PuYqiXJN0VLS88euRDoM lYhRiAPLhcLuqWl6UyN3d4y1N4jSpPkKXx+2eJdYwxSDEQARWF1Yqs/Ku oJ03yF8THjxwYpTNU8dTQH4QMTggqzgr/dO/+aJeGfNNAO97YM9i6FLKn A==; X-IronPort-AV: E=McAfee;i="6400,9594,10439"; a="271658919" X-IronPort-AV: E=Sophos;i="5.93,237,1654585200"; d="scan'208";a="271658919" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Aug 2022 21:15:12 -0700 X-IronPort-AV: E=Sophos;i="5.93,237,1654585200"; d="scan'208";a="635346906" Received: from dmalka-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.249.42.26]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Aug 2022 21:15:07 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 9A162104A70; Mon, 15 Aug 2022 07:18:04 +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 , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Weihong Zhang , "Kirill A . Shutemov" Subject: [PATCHv6 11/11] selftests/x86/lam: Add inherit test cases for linear-address masking Date: Mon, 15 Aug 2022 07:18:03 +0300 Message-Id: <20220815041803.17954-12-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220815041803.17954-1-kirill.shutemov@linux.intel.com> References: <20220815041803.17954-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE 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 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 | 123 +++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c index 19208ba6047a..074fa0cd65ef 100644 --- a/tools/testing/selftests/x86/lam.c +++ b/tools/testing/selftests/x86/lam.c @@ -35,8 +35,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) @@ -169,6 +170,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 == ~(0x3fULL << 57)) + 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 get_metadata(uint64_t src, unsigned long lam) { @@ -623,6 +646,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; @@ -724,11 +813,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"); } @@ -748,7 +852,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; } @@ -762,6 +866,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)); @@ -777,6 +891,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(); -- 2.35.1