Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp150045imw; Tue, 12 Jul 2022 16:33:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tumSaPJ+agmrHqrUYy9hhZNAp6/P3+b3PQ5vhFITREAIimZSkjorjAB9eU3gXZVgWTWesD X-Received: by 2002:a05:6402:5516:b0:43a:42f9:24d6 with SMTP id fi22-20020a056402551600b0043a42f924d6mr818035edb.204.1657668790364; Tue, 12 Jul 2022 16:33:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657668790; cv=none; d=google.com; s=arc-20160816; b=qacAsoaKaUTx/+GrnqZ7QuEwWkYwPvJ3y893N5rLEYwWODJmKWDfMzdXZrm/HjDdKm 0eMiavs9+2l8Pcs5g7JOy8LUc/Fp9qrJ4UV1LF1opLDXQSf6qLQk7vKzIIUP1MaBt8Rn JrfW55rRS7yXWhV824t4kXWxTNdk2K85yPgZdERM/vo3ykOHQZBfSOPpSMp5lCf3PkZr uzWjsU+DhB7NqVB28Axgm02N2cOuJjc6XxxJG1UciId//hF7+ByxMs61JD4xZJLl+1sX DnySqVghsbmk9u3mo591GmOL4vgFkik2P5CFTHf5N2LRizhM+Ggz0BPD3goYWxciA8di EXkg== 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=4N3iq6346ZyCjOHabos6w+ZqftD1B6E6osWRIZPXXVQ=; b=QxJgpDD/3kIQ0zmQNOSEUhk6AbRv1eqUz1/RbcyQ/ZHDvBOjGHFpUlhcEaPkcipAmH CuHH8j11Ncyi7QOu268VoHp2A4eWhUc0mFbFaU/Eps3q2YprEiZVKnCZK1gfW/yKEU6M vHPkTlC/LeCoO+2o5rJAme2VPAz6YfK2vTNVjhxJNDDLSaI5UaHuKnGDe6/brTwDPvoQ REfk067w7JQOKS9G6kjdIzGZDfcLmHrO4HKdiqtTy1PNgzjwU5RLdjt5Tf8K/ilVrleq TtXz+bo3qt1zLnQwmgliBbS5hOldjQY4FpyVALHeNEeffojJFLcs4qbaD8ZGW0fpSFJc ILrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=L9uV19Mv; 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 z5-20020a170906240500b0072a38f3f0e8si15784656eja.215.2022.07.12.16.32.45; Tue, 12 Jul 2022 16:33:10 -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=L9uV19Mv; 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 S234346AbiGLXO2 (ORCPT + 99 others); Tue, 12 Jul 2022 19:14:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233928AbiGLXNi (ORCPT ); Tue, 12 Jul 2022 19:13:38 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E7F0F47 for ; Tue, 12 Jul 2022 16:13:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1657667614; x=1689203614; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MGBbR/k1AssmUS4ywd/fm01GaItlo8jGpggyRDWPYcs=; b=L9uV19MvuJ5sfhn9qCFEPqAbv6PuhKKQqMlPGBoG0aNUCs9kDg80XCPd ROIh0zZWEo/cUPxDaZkwHbKugXmQPvKGvyI2cwJ05CsH1tmQq6ZwKx2mG QoFhBBsigP1BP36CpVch1hfsX/HR6qWcmMVlPJ3lz1pMimCWX2npR3DZG AQNUVDjVHIchbmon0052EeIbs59CbQkd8sTIRugCelf5x0kj2yOfPwuxi Uz8frz3lKSLUqhXttYbj4oDLaDH/fNVhoqNfekHO7VdwC5XAWKu2oOCRo v8dLjhM03u+h7VRtK8J5VuFAnx/Ofam9CezlVbnWPTUCOBp1J+hJg+Ws7 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10406"; a="285808446" X-IronPort-AV: E=Sophos;i="5.92,266,1650956400"; d="scan'208";a="285808446" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2022 16:13:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,266,1650956400"; d="scan'208";a="841547745" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga006.fm.intel.com with ESMTP; 12 Jul 2022 16:13:26 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 39A185F6; Wed, 13 Jul 2022 02:13:30 +0300 (EEST) 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: [PATCHv5 11/13] selftests/x86/lam: Add inherit test cases for linear-address masking Date: Wed, 13 Jul 2022 02:13:26 +0300 Message-Id: <20220712231328.5294-12-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220712231328.5294-1-kirill.shutemov@linux.intel.com> References: <20220712231328.5294-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,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 | 124 +++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c index d2ae75b3bdc0..fcac5feb47d0 100644 --- a/tools/testing/selftests/x86/lam.c +++ b/tools/testing/selftests/x86/lam.c @@ -34,7 +34,9 @@ #define FUNC_SYSCALL 0x4 #define FUNC_URING 0x8 -#define TEST_MASK 0xf +#define FUNC_INHERITE 0x10 + +#define TEST_MASK 0x1f #define LOW_ADDR (0x1UL << 30) #define HIGH_ADDR (0x3UL << 48) @@ -142,6 +144,28 @@ static int set_lam(unsigned long lam) return ret; } +/* + * 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) { @@ -580,6 +604,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; @@ -674,11 +764,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:mmap; 0x4:syscall; 0x8:io_uring.\n"); + printf("\t\t0x1:malloc; 0x2:mmap; 0x4:syscall; 0x8:io_uring; 0x10:inherit;\n"); printf("\t-h: help\n"); } @@ -698,7 +803,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; } @@ -712,6 +817,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)); @@ -724,6 +839,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