Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp5161128rwb; Tue, 6 Sep 2022 20:33:12 -0700 (PDT) X-Google-Smtp-Source: AA6agR4e2ItWP0iIXdZ+nPmoixH68KXxJv7iDjQuQcliZrScaNgCSNKOM5gcC5WHQtyv0Mp+a/P8 X-Received: by 2002:aa7:c415:0:b0:44d:f432:3e84 with SMTP id j21-20020aa7c415000000b0044df4323e84mr1387031edq.56.1662521592321; Tue, 06 Sep 2022 20:33:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662521592; cv=none; d=google.com; s=arc-20160816; b=rokoJJT2Rh8YG9XpZfa54Vp7x713u61TNugooxYxkbkbszfqs3nSxE0Er9qTlqFeBh FvCdvY4O8vdb5toftqmuLsdx1o/pGIZPyb2OqkAv9yAq+vtk88yyKQrE8ya79UDap/D+ HiwwNu5pQvML8f7YzjVjhY9nbaVjcRUuXAGlEBOnhaqdccsJ3duBh4SwdZRSSJW9M5qt XB7+YnpdqzPuNz0cXgmsXuqI0K9RQSX1QntmsDocadoMkU+oFn7EiyXyUmmRQ4qi/787 CnnluxGl1tAXM7pwPN+JxPTkYriRcZH8bIvVe4/UYH1LaaPeXeqxeQ/hwDN/TNmxfIxm 6KKw== 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:date:cc:to:from:subject:message-id :dkim-signature; bh=bo5Ij4kaB5dKADV83OfoGdk2yc0YbLko+Jkp1wedmU0=; b=bkaV+39NameRg62gK3IP+BxoVtYkGm+uWYEsj7sXeGfsMqT/7gm7Q0ff3Jl9Ck8k5K vrStlScI0m3BizIei11MXqkZHAzAc20Z5sRWX8klkmyOiEJSQwzSMSvBQ8kVZC3679lI 3eF/IIncsUbJiZIeCtRdAVR0kPKZXYTYsWuTjo1W6SlyGsJJXah+Y8qnA0d8v87l8x+m uLQZJfMF3ALuXFEBX/ekBUF6IbCmwxzK4Id1luQsBzjob1Gb/rn2pVxzrGdLV0Urtskw l71VI9wH3kgaDUuQT5Ne+Ll26JtsOzFShD7PxGeO8i8n/+xrSAyLL5tR1HQoCDybL4ce d6cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DZAdM2nk; 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 nc21-20020a1709071c1500b007708368889dsi355788ejc.904.2022.09.06.20.32.44; Tue, 06 Sep 2022 20:33:12 -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=DZAdM2nk; 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 S230030AbiIGDTz (ORCPT + 99 others); Tue, 6 Sep 2022 23:19:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229778AbiIGDTx (ORCPT ); Tue, 6 Sep 2022 23:19:53 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4313E15FF7 for ; Tue, 6 Sep 2022 20:19:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662520790; x=1694056790; h=message-id:subject:from:to:cc:date:in-reply-to: references:mime-version:content-transfer-encoding; bh=RAcEtiEwWb9QLnBtlHAEAi1539PX2fBlXv8J0Kty2F0=; b=DZAdM2nkm7VU/55HzZ1Qj0Knv78VimbjuyJV4qJzqNiY+fL9ciyRATcG bEiNcw4V4wcz7MijkZrBnRN4MVdPSoYDtCSazDWgL55Z9HV7r29fZaTKK C9BwxLmsg3PzZZIfL6iWeil5Co8WFboQUIgdddpoFzy7MhncnZBhdzvcN H2FD97EDHhNeleP25DGO/ZvK5pDZmTpw5T1xsJiAOs8AmstzmCQYbZIEi YOSjzFWAMyd1gC71DgQvRIlbTDGZY/nOAJntd0W50nbTx4ktHwiuNjRkF ZGTk+jYelWq9mlXfuvjzdinPPF3vtAnXMTv+1ohIl+6uoOFA0VczqgW1P Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10462"; a="279780677" X-IronPort-AV: E=Sophos;i="5.93,295,1654585200"; d="scan'208";a="279780677" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2022 20:19:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,295,1654585200"; d="scan'208";a="789903282" Received: from sqa-gate.sh.intel.com (HELO robert-ivt.tsp.org) ([10.239.48.212]) by orsmga005.jf.intel.com with ESMTP; 06 Sep 2022 20:19:45 -0700 Message-ID: Subject: Re: [PATCHv8 09/11] selftests/x86/lam: Add mmap and SYSCALL test cases for linear-address masking From: Robert Hoo To: "Kirill A. Shutemov" , 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 Date: Wed, 07 Sep 2022 11:19:45 +0800 In-Reply-To: <20220830010104.1282-10-kirill.shutemov@linux.intel.com> References: <20220830010104.1282-1-kirill.shutemov@linux.intel.com> <20220830010104.1282-10-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5 (3.28.5-10.el7) Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,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 On Tue, 2022-08-30 at 04:01 +0300, Kirill A. Shutemov wrote: > From: Weihong Zhang > > Add mmap and SYSCALL test cases. > > SYSCALL test cases: > > - LAM supports set metadata in high bits 62:57 (LAM_U57) of a user > pointer, pass > the pointer to SYSCALL, SYSCALL can dereference the pointer and > return correct > result. > > - Disable LAM, pass a pointer with metadata in high bits to SYSCALL, > SYSCALL returns -1 (EFAULT). > > MMAP test cases: > > - Enable LAM_U57, MMAP with low address (below bits 47), set > metadata > in high bits of the address, dereference the address should be > allowed. > > - Enable LAM_U57, MMAP with high address (above bits 47), set > metadata > in high bits of the address, dereference the address should be > allowed. > > Signed-off-by: Weihong Zhang > Signed-off-by: Kirill A. Shutemov > Acked-by: Peter Zijlstra (Intel) > --- > tools/testing/selftests/x86/lam.c | 135 > +++++++++++++++++++++++++++++- > 1 file changed, 132 insertions(+), 3 deletions(-) > > diff --git a/tools/testing/selftests/x86/lam.c > b/tools/testing/selftests/x86/lam.c > index 900a3a0fb709..b88e007ee0a3 100644 > --- a/tools/testing/selftests/x86/lam.c > +++ b/tools/testing/selftests/x86/lam.c > @@ -7,6 +7,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -29,11 +30,18 @@ > /* Specified test function bits */ > #define FUNC_MALLOC 0x1 > #define FUNC_BITS 0x2 > +#define FUNC_MMAP 0x4 > +#define FUNC_SYSCALL 0x8 > > -#define TEST_MASK 0x3 > +#define TEST_MASK 0xf > + > +#define LOW_ADDR (0x1UL << 30) > +#define HIGH_ADDR (0x3UL << 48) > > #define MALLOC_LEN 32 > > +#define PAGE_SIZE (4 << 10) > + > struct testcases { > unsigned int later; > int expected; /* 2: SIGSEGV Error; 1: other errors */ > @@ -49,6 +57,7 @@ jmp_buf segv_env; > static void segv_handler(int sig) > { > ksft_print_msg("Get segmentation fault(%d).", sig); > + > siglongjmp(segv_env, 1); > } > > @@ -61,6 +70,16 @@ static inline int cpu_has_lam(void) > return (cpuinfo[0] & (1 << 26)); > } > > +/* Check 5-level page table feature in CPUID.(EAX=07H, > ECX=00H):ECX.[bit 16] */ > +static inline int cpu_has_la57(void) > +{ > + unsigned int cpuinfo[4]; > + > + __cpuid_count(0x7, 0, cpuinfo[0], cpuinfo[1], cpuinfo[2], > cpuinfo[3]); > + > + return (cpuinfo[2] & (1 << 16)); > +} > + > /* > * Set tagged address and read back untag mask. > * check if the untagged mask is expected. > @@ -213,6 +232,68 @@ static int handle_malloc(struct testcases *test) > return ret; > } > > +static int handle_mmap(struct testcases *test) > +{ > + void *ptr; > + unsigned int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED; > + int ret = 0; > + > + if (test->later == 0 && test->lam != 0) > + if (set_lam(test->lam) != 0) > + return 1; > + > + ptr = mmap((void *)test->addr, PAGE_SIZE, PROT_READ | > PROT_WRITE, > + flags, -1, 0); > + if (ptr == MAP_FAILED) { > + if (test->addr == HIGH_ADDR) > + if (!cpu_has_la57()) > + return 3; /* unsupport LA57 */ I think here return 3 to indicate skip cases. Perhaps you can enable skip case like this? Just FYI, I'm not familiar with selftests yet. @@ -321,8 +323,10 @@ static int handle_mmap(struct testcases *test) flags, -1, 0); if (ptr == MAP_FAILED) { if (test->addr == HIGH_ADDR) - if (!cpu_has_la57()) + if (!cpu_has_la57()) { + perror("Unsupport LA57. Skip"); return 3; /* unsupport LA57 */ + } return 1; } @@ -746,12 +750,16 @@ static void run_test(struct testcases *test, int count) /* fork a process to run test case */ ret = fork_test(t); + tests_cnt++; + if (ret == 3) { + ksft_test_result_skip(t->msg); + continue; + } if (ret != 0) ret = (t->expected == ret); else ret = !(t->expected); - tests_cnt++; ksft_test_result(ret, t->msg); } } > + return 1; > + } > + > + if (test->later != 0 && test->lam != 0) > + if (set_lam(test->lam) != 0) > + ret = 1; > + > + if (ret == 0) { > + if (sigsetjmp(segv_env, 1) == 0) { > + signal(SIGSEGV, segv_handler); > + ret = handle_lam_test(ptr, test->lam); > + } else { > + ret = 2; > + } > + } > + > + munmap(ptr, PAGE_SIZE); > + return ret; > +} > + > +static int handle_syscall(struct testcases *test) > +{ > + struct utsname unme, *pu; > + int ret = 0; > + > + if (test->later == 0 && test->lam != 0) > + if (set_lam(test->lam) != 0) > + return 1; > + > + if (sigsetjmp(segv_env, 1) == 0) { > + signal(SIGSEGV, segv_handler); > + pu = (struct utsname *)set_metadata((uint64_t)&unme, > test->lam); > + ret = uname(pu); > + if (ret < 0) > + ret = 1; > + } else { > + ret = 2; > + } > + > + if (test->later != 0 && test->lam != 0) > + if (set_lam(test->lam) != -1 && ret == 0) > + ret = 1; > + > + return ret; > +} > + > static int fork_test(struct testcases *test) > { > int ret, child_ret; > @@ -268,7 +349,6 @@ static struct testcases malloc_cases[] = { > }, > }; > > - > static struct testcases bits_cases[] = { > { > .test_func = handle_max_bits, > @@ -276,11 +356,54 @@ static struct testcases bits_cases[] = { > }, > }; > > +static struct testcases syscall_cases[] = { > + { > + .later = 0, > + .lam = LAM_U57_BITS, > + .test_func = handle_syscall, > + .msg = "SYSCALL: LAM_U57. syscall with metadata\n", > + }, > + { > + .later = 1, > + .expected = 1, > + .lam = LAM_U57_BITS, > + .test_func = handle_syscall, > + .msg = "SYSCALL:[Negative] Disable LAM. Dereferencing > pointer with metadata.\n", > + }, > +}; > + > +static struct testcases mmap_cases[] = { > + { > + .later = 1, > + .expected = 0, > + .lam = LAM_U57_BITS, > + .addr = HIGH_ADDR, > + .test_func = handle_mmap, > + .msg = "MMAP: First mmap high address, then set > LAM_U57.\n", > + }, > + { > + .later = 0, > + .expected = 0, > + .lam = LAM_U57_BITS, > + .addr = HIGH_ADDR, > + .test_func = handle_mmap, > + .msg = "MMAP: First LAM_U57, then High address.\n", > + }, > + { > + .later = 0, > + .expected = 0, > + .lam = LAM_U57_BITS, > + .addr = LOW_ADDR, > + .test_func = handle_mmap, > + .msg = "MMAP: First LAM_U57, then Low address.\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;\n"); > + printf("\t\t0x1:malloc; 0x2:max_bits; 0x4:mmap; > 0x8:syscall.\n"); > printf("\t-h: help\n"); > } > > @@ -320,6 +443,12 @@ int main(int argc, char **argv) > if (tests & FUNC_BITS) > run_test(bits_cases, ARRAY_SIZE(bits_cases)); > > + if (tests & FUNC_MMAP) > + run_test(mmap_cases, ARRAY_SIZE(mmap_cases)); > + > + if (tests & FUNC_SYSCALL) > + run_test(syscall_cases, ARRAY_SIZE(syscall_cases)); > + > ksft_set_plan(tests_cnt); > > return ksft_exit_pass();