Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2864338pxv; Mon, 12 Jul 2021 03:55:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSghBa4MWWa7ObXbTjml3gYn6ay7jQV9OY0X4ll0Ujs1GKRZqTLebTn7kMUgZigbTd/D9P X-Received: by 2002:a92:d84a:: with SMTP id h10mr21275186ilq.55.1626087313911; Mon, 12 Jul 2021 03:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626087313; cv=none; d=google.com; s=arc-20160816; b=Ck9KtgEF2xVOxEJkZS/jgaBfoqeuLCzouOEskR6Cn3DnQ2YKi3tyU7PRhFl8BKRtjU JSyo2+R0D/doh0DWexx8CPv7Sp6lrWphQPebhDCeVy3cXeczDdReghFc1AqxMUJZgAus NJltAMbK65D/AkvW0NC4XangTcbL1ZBvEhEH6MS0TbX3KQG+y/Fnx+MbcmJEn0nrJPsT K9hXXj8lawo887rAMVqn+5ZcQk9bBAxGitsKhYfBWcvQ5x7W6QDLTyYE36q0tYyG8sj+ 2SEWemSgUCVepFRq6IMQng6Y/aLM1Ff9uNF7Fa43UCNiJgqcapbWju+mLpoWuZFOzasq 17MQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BpmWkTnGprklRYA6g+jrihmlWX78zGB5RWL0eYAWpn0=; b=ABjQ8+l2N8rzNOWwSpucAGccUJiaocv2L0sIXHlCe4/V/9BE+UeMuRD3lx7e6+JGcI HUQy9ioJ2EUSfxC4RLYtFMvlIlGok2T83OTBFr34LkpDPIgSkKkny784NnrrOpxeW1/H B3XSIgtKSu+Hp8nPRHslZEgt+ZTG167fRpxBTil7bgu0uNQOLoBackT9/oEgrHUwRI7a +W/lXN8//jUdGTBgOFFwIxrTVgYQ4LZRXCdMerQojJ58NhPNFSQXCycwf1fPsGA3z3NR J4K7fjTK/tRsZrzEMm4TL8rMf9VM7kXQRCXPZprTlvsX/i7vPBIgrApwl6FqyCAUYIea Lfnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1vQ6I0br; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l17si3087483ilk.49.2021.07.12.03.55.02; Mon, 12 Jul 2021 03:55:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1vQ6I0br; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382186AbhGLJAl (ORCPT + 99 others); Mon, 12 Jul 2021 05:00:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:55670 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353960AbhGLIDV (ORCPT ); Mon, 12 Jul 2021 04:03:21 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7ABA0611AC; Mon, 12 Jul 2021 07:59:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626076744; bh=BKSmCAPJJnJ6CPF5gE5xkJss2QAFBPNPP4U/ycNb9sI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1vQ6I0brB50pKCHpl+qhgaAOMwlNFcrobdAedVsG4fvo960f+QIQ1jWG+mG19m05i CNr192LGfy5dTt2SYgqiPjakMPuk0+lbtB8LWrNuxJ8UgGRDG2VVIgcdaNZTdtwNWy pKhN60r6QmzC8QeW4rJbChUu3W6/EiwD7PqixNIg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Hansen , Thomas Gleixner , "Aneesh Kumar K.V" , Ram Pai , Sandipan Das , Florian Weimer , "Desnes A. Nunes do Rosario" , Ingo Molnar , Thiago Jung Bauermann , Michael Ellerman , Michal Hocko , Michal Suchanek , Shuah Khan , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.13 775/800] selftests/vm/pkeys: fix alloc_random_pkey() to make it really, really random Date: Mon, 12 Jul 2021 08:13:18 +0200 Message-Id: <20210712061049.316910958@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060912.995381202@linuxfoundation.org> References: <20210712060912.995381202@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dave Hansen [ Upstream commit f36ef407628835a7d7fb3d235b1f1aac7022d9a3 ] Patch series "selftests/vm/pkeys: Bug fixes and a new test". There has been a lot of activity on the x86 front around the XSAVE architecture which is used to context-switch processor state (among other things). In addition, AMD has recently joined the protection keys club by adding processor support for PKU. The AMD implementation helped uncover a kernel bug around the PKRU "init state", which actually applied to Intel's implementation but was just harder to hit. This series adds a test which is expected to help find this class of bug both on AMD and Intel. All the work around pkeys on x86 also uncovered a few bugs in the selftest. This patch (of 4): The "random" pkey allocation code currently does the good old: srand((unsigned int)time(NULL)); *But*, it unfortunately does this on every random pkey allocation. There may be thousands of these a second. time() has a one second resolution. So, each time alloc_random_pkey() is called, the PRNG is *RESET* to time(). This is nasty. Normally, if you do: srand(); foo = rand(); bar = rand(); You'll be quite guaranteed that 'foo' and 'bar' are different. But, if you do: srand(1); foo = rand(); srand(1); bar = rand(); You are quite guaranteed that 'foo' and 'bar' are the *SAME*. The recent "fix" effectively forced the test case to use the same "random" pkey for the whole test, unless the test run crossed a second boundary. Only run srand() once at program startup. This explains some very odd and persistent test failures I've been seeing. Link: https://lkml.kernel.org/r/20210611164153.91B76FB8@viggo.jf.intel.com Link: https://lkml.kernel.org/r/20210611164155.192D00FF@viggo.jf.intel.com Fixes: 6e373263ce07 ("selftests/vm/pkeys: fix alloc_random_pkey() to make it really random") Signed-off-by: Dave Hansen Signed-off-by: Thomas Gleixner Tested-by: Aneesh Kumar K.V Cc: Ram Pai Cc: Sandipan Das Cc: Florian Weimer Cc: "Desnes A. Nunes do Rosario" Cc: Ingo Molnar Cc: Thiago Jung Bauermann Cc: Michael Ellerman Cc: Michal Hocko Cc: Michal Suchanek Cc: Shuah Khan Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- tools/testing/selftests/vm/protection_keys.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index fdbb602ecf32..9ee0ae5d3e06 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -561,7 +561,6 @@ int alloc_random_pkey(void) int nr_alloced = 0; int random_index; memset(alloced_pkeys, 0, sizeof(alloced_pkeys)); - srand((unsigned int)time(NULL)); /* allocate every possible key and make a note of which ones we got */ max_nr_pkey_allocs = NR_PKEYS; @@ -1552,6 +1551,8 @@ int main(void) int nr_iterations = 22; int pkeys_supported = is_pkeys_supported(); + srand((unsigned int)time(NULL)); + setup_handlers(); printf("has pkeys: %d\n", pkeys_supported); -- 2.30.2