Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp585249imw; Thu, 14 Jul 2022 07:10:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s0up1xMJ2b9UluvUbHZ1T9/eBkzGvvMxWI3fM9SIdM7qhPNb7+bi/44xmW3yi/a5EDDJaA X-Received: by 2002:a17:907:7b9c:b0:72b:5652:a14a with SMTP id ne28-20020a1709077b9c00b0072b5652a14amr9083426ejc.86.1657807821316; Thu, 14 Jul 2022 07:10:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657807821; cv=none; d=google.com; s=arc-20160816; b=usr+v9bXZ/KNnwslf3dxvNZeDkMJi5bzjd52eRmqrZngQlkYlx2z5UblGBZNviNgZV 309r+i825IKzuU47j0A4Y1zaw2SXNzYf5pdmcLP4DYyn8RmxnbExkWFfAgrTOxPA0fpF IbAoRtfvREZHQh8SRJ68yeOiskYjkvFKpVlTXsJul9wwQx54G6zKj8QoXXQ/avoDvvnb t06iOcfpde4PlDVBizKTp/FCMIAsIgTN4k02elXk/6CRj+oIn1s2l5uqhOVfSOjyTP69 zQpPROzfzp7y2+ZqFQeWYMWxZbJOEq8t2mWmh3+1OeInYu9XZ9nzpVPocB5hDUc/8EYc NKzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=9FSo8XtzlTYanivvYEARXQax4+PoAE9EIoVCtOZ5gZk=; b=HI0D/YvH8JDIXLoKJ6SyIBmvNWyJipuL5LAOUqhyiufWu4HwNX9FR91WdZX6GrJShD zNqnFpjC51Fp4wDZik5t2o/10Ie/vBHL6gKZBKpETFnWWAvcjmKUNhtiR2ea/qyPDuoa O22DE3oIoJXRUetHbkqc4dDQPW+mQ4nufPom/Y/h8ltt1/M4XB0NASJBHvR+MoeI0hvm SmWYpKF5tDJcqPnvW7PQIOZF+mJhLStYs2q9PWEMV8oSrS+1SudvSXcW3yJwgbjoQByS cpru7CL1RQskgz87nVenVf5TejBL0KxtfddNMOcC5ogiHYXqsIwLQn0lUpZiYI7zMAyo Xefg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IYZv7YJd; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sd39-20020a1709076e2700b0072b6ff7720dsi2376475ejc.771.2022.07.14.07.09.56; Thu, 14 Jul 2022 07:10:21 -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=@redhat.com header.s=mimecast20190719 header.b=IYZv7YJd; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238802AbiGNNbT (ORCPT + 99 others); Thu, 14 Jul 2022 09:31:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238592AbiGNNbS (ORCPT ); Thu, 14 Jul 2022 09:31:18 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2156C5E32D for ; Thu, 14 Jul 2022 06:31:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1657805475; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9FSo8XtzlTYanivvYEARXQax4+PoAE9EIoVCtOZ5gZk=; b=IYZv7YJdZIEPVJO3cNbvLTIGfy3d5KnhqaQZAF0io+c5/XELG+J6t4b9Slk3NDdJZctqvt GYsf+rCwn4GOdsJga9wkCmz2pFb568VN5XuZWxeU7jJBo3f30YwZLgAsgop4C75bCwofS/ I5R/L+um/82iOh+YrK67vbCJHSlajN8= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-599-akqebN13NAKg0IVxJpQRFw-1; Thu, 14 Jul 2022 09:31:14 -0400 X-MC-Unique: akqebN13NAKg0IVxJpQRFw-1 Received: by mail-qv1-f70.google.com with SMTP id od5-20020a0562142f0500b00473838e0feeso1220761qvb.9 for ; Thu, 14 Jul 2022 06:31:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=9FSo8XtzlTYanivvYEARXQax4+PoAE9EIoVCtOZ5gZk=; b=xOihrWPceTsGKt+jtIqa6XagHDtomFg4KaetTZLE31jpm5rOTTfsrmOzZvjpn45fAU MpxdWaEVFVBV+SLHkXx1/g5D5ooHSKVK7CfJ+g1+s8lFGS+MyttADxJmdFEoZ4693Xxh G1KpVXQBwb2ZtDmMNiQ1N+zFuF3ToU2c2CiUUeB0dgB3frZ29oa8tCczOgEmyD8nBkU5 Qowt4TroyFbBpKGgKShbzp+lKoYM3FcLoOIQTqZdYyIuYKOPQf6Pd8o0DawcQ6KczXD4 SJOPGSaMpqo9xv69ldKfgUQQcP0erJeDT/4hsI88tHKSQoLY3xUQHCxxBEkYfvcWERhW QO2g== X-Gm-Message-State: AJIora++V+9NB1EH9xFqX5oSuOuePkCEAucfu5WmJWp/3uENfgjv6F8j u6+b+webINfQUqnAmmpE17Vj91kht1R7Sv2ELdsIzBXz1fK2XM4NpLYIV1Wq5y86t7e4ouJnWX6 7cv6iqpUXWZnAgBAW/yQHT2CA X-Received: by 2002:a05:620a:16ca:b0:6b5:b971:1aa6 with SMTP id a10-20020a05620a16ca00b006b5b9711aa6mr5223161qkn.144.1657805473437; Thu, 14 Jul 2022 06:31:13 -0700 (PDT) X-Received: by 2002:a05:620a:16ca:b0:6b5:b971:1aa6 with SMTP id a10-20020a05620a16ca00b006b5b9711aa6mr5223126qkn.144.1657805472980; Thu, 14 Jul 2022 06:31:12 -0700 (PDT) Received: from bfoster (c-24-61-119-116.hsd1.ma.comcast.net. [24.61.119.116]) by smtp.gmail.com with ESMTPSA id z125-20020a37b083000000b006a758ce2ae1sm1291694qke.104.2022.07.14.06.31.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Jul 2022 06:31:12 -0700 (PDT) Date: Thu, 14 Jul 2022 09:31:09 -0400 From: Brian Foster To: Alexey Dobriyan Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, dylanbhatch@google.com, skhan@linuxfoundation.org Subject: Re: [PATCH v2] proc: fix test for "vsyscall=xonly" boot option Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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, Jul 12, 2022 at 05:51:45PM +0300, Alexey Dobriyan wrote: > Booting with vsyscall=xonly results in the following vsyscall VMA: > > ffffffffff600000-ffffffffff601000 --xp ... [vsyscall]\n > > Test does read from fixed vsyscall address to determine if kernel > supports vsyscall page but it doesn't work because, well, vsyscall > page is execute only. > > Fix test by trying to execute from the first byte of the page which > contains gettimeofday() stub. This should work because vsyscall > entry points have stable addresses by design. > > Alexey, avoiding parsing .config, /proc/config.gz and > /proc/cmdline at all costs. > > Signed-off-by: Alexey Dobriyan > --- Hi Alexey, I had run into this failure when testing something unrelated, so thanks for the fix. It passes in my test env. That said, some of code duplication stood out to me when I took a closer look. Not a huge deal for a selftest, but any thoughts on any of the following cleanups on top of your diff..? It still passes my quick tests and cuts the patch in half or so.. Brian --- 8< --- diff --git a/tools/testing/selftests/proc/proc-pid-vm.c b/tools/testing/selftests/proc/proc-pid-vm.c index e5962f4794f5..d4dca92d60f3 100644 --- a/tools/testing/selftests/proc/proc-pid-vm.c +++ b/tools/testing/selftests/proc/proc-pid-vm.c @@ -212,32 +212,35 @@ static int make_exe(const uint8_t *payload, size_t len) #endif /* - * 0: vsyscall VMA doesn't exist vsyscall=none - * 1: vsyscall VMA is r-xp vsyscall=emulate + * vsyscall probe return codes: + * + * 0: vsyscall VMA is r-xp vsyscall=emulate + * 1: vsyscall VMA doesn't exist vsyscall=none * 2: vsyscall VMA is --xp vsyscall=xonly */ -static int g_vsyscall; +static volatile int g_vsyscall; static const char *str_vsyscall; -static const char str_vsyscall_0[] = ""; -static const char str_vsyscall_1[] = -"ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n"; -static const char str_vsyscall_2[] = -"ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n"; +static const char *str_vsyscall_arr[] = { +"ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n", +"", +"ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n" +}; #ifdef __x86_64__ static void sigaction_SIGSEGV(int _, siginfo_t *__, void *___) { - _exit(1); + _exit(g_vsyscall); } /* * vsyscall page can't be unmapped, probe it directly. */ -static void vsyscall(void) +static bool vsyscall(void) { pid_t pid; int wstatus; + bool ret = false; pid = fork(); if (pid < 0) { @@ -256,72 +259,34 @@ static void vsyscall(void) (void)sigaction(SIGSEGV, &act, NULL); /* gettimeofday(NULL, NULL); */ + g_vsyscall = 1; asm volatile ( "call %P0" : : "i" (0xffffffffff600000), "D" (NULL), "S" (NULL) : "rax", "rcx", "r11" ); - exit(0); - } - waitpid(pid, &wstatus, 0); - if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0) { - /* vsyscall page exists and is executable. */ - } else { - /* vsyscall page doesn't exist. */ - g_vsyscall = 0; - return; - } - - pid = fork(); - if (pid < 0) { - fprintf(stderr, "fork, errno %d\n", errno); - exit(1); - } - if (pid == 0) { - struct rlimit rlim = {0, 0}; - (void)setrlimit(RLIMIT_CORE, &rlim); - - /* Hide "segfault at ffffffffff600000" messages. */ - struct sigaction act; - memset(&act, 0, sizeof(struct sigaction)); - act.sa_flags = SA_SIGINFO; - act.sa_sigaction = sigaction_SIGSEGV; - (void)sigaction(SIGSEGV, &act, NULL); + g_vsyscall = 2; *(volatile int *)0xffffffffff600000UL; exit(0); } waitpid(pid, &wstatus, 0); - if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0) { - /* vsyscall page is readable and executable. */ - g_vsyscall = 1; - return; + if (WIFEXITED(wstatus)) { + g_vsyscall = WEXITSTATUS(wstatus); + ret = g_vsyscall != 1; } - - /* vsyscall page is executable but unreadable. */ - g_vsyscall = 2; + str_vsyscall = str_vsyscall_arr[g_vsyscall]; + return ret; } int main(void) { int pipefd[2]; int exec_fd; + bool have_vsyscall; - vsyscall(); - switch (g_vsyscall) { - case 0: - str_vsyscall = str_vsyscall_0; - break; - case 1: - str_vsyscall = str_vsyscall_1; - break; - case 2: - str_vsyscall = str_vsyscall_2; - break; - default: - abort(); - } + have_vsyscall = vsyscall(); atexit(ate); @@ -388,7 +353,7 @@ int main(void) rv = read(fd, buf, sizeof(buf)); assert(rv == len); assert(memcmp(buf, buf0, strlen(buf0)) == 0); - if (g_vsyscall > 0) { + if (have_vsyscall) { assert(memcmp(buf + strlen(buf0), str_vsyscall, strlen(str_vsyscall)) == 0); } } @@ -435,7 +400,7 @@ int main(void) assert(memmem(buf, rv, S[i], strlen(S[i]))); } - if (g_vsyscall > 0) { + if (have_vsyscall) { assert(memmem(buf, rv, str_vsyscall, strlen(str_vsyscall))); } }