Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2832851pxv; Mon, 12 Jul 2021 03:04:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXM4gl3GID2SSzzJ5AincNpnGisdP6MZLKMt0T3nY2ILMomD0c5QINfzbvxz0k47Vyp+gl X-Received: by 2002:a92:d07:: with SMTP id 7mr34488671iln.114.1626084265479; Mon, 12 Jul 2021 03:04:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626084265; cv=none; d=google.com; s=arc-20160816; b=qJNBnXXDRCW7+4/5V7rKzhKgznpF+GUVP/okcsT7jwyvruDoxle+KwCtzJFyvjGgLi yCdIAG/ayP2Y5gbNJerXq85VHHtneTv5r1FYO805rtziRWiF0aD92zE8TYt2y0Eo1uN1 yuMDw74/Uahfb3JGRTfrqs4IG1MobfHeELQ8WfNO9SUWJqjynazR43ZRgsDgbt4qaBbx nJtqrTKmx0o3HjsahA5xU8T1phu+jGoh41z3m5vxEmz/vfVrrvEAfrlCZySoxQDFhB6Y kyUow00OMd8whM8xogSy88e1EIEhWA8FlOHH2x4cyLakz9MogJsk0Huvhc5V3ddGFIqj Hxrw== 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=Qwnj1LQLU94PmbEKKE+dXAInIXvN1q5C9x6BjelRrc7YXCKdyWcmwLFYLovXOWYPHL UbWIxjRU5CQwDuvRtuaGZK8hbJybiOU6W26OpkaamV/FLcGWueoVO99cLIcfN1A9FiWP Oas2T6lTZ4bXsGDZP0A/QOe6VlqVz+K/uSje+gDZW+EhB6gxJNbSo9ez42EF9iBaJuTZ 9lo/JBD9QQyIW/AhP8B3Wmk//M7aKVy0r6DrvSehnWngK6F0LRsl+35VO29nBb2bOUMq /cbiYBGPYUWcaR5kKyO19YDhAv9O3ZQwkJL5Une/LaKJB1gCeM7YpAPPqm8WksFXo+E4 Q1FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GQe6PVSw; 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 g13si18764360ilf.35.2021.07.12.03.04.13; Mon, 12 Jul 2021 03:04:25 -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=GQe6PVSw; 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 S245640AbhGLHTm (ORCPT + 99 others); Mon, 12 Jul 2021 03:19:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:55908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238221AbhGLGzM (ORCPT ); Mon, 12 Jul 2021 02:55:12 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 34C5A6100B; Mon, 12 Jul 2021 06:52:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626072743; bh=BKSmCAPJJnJ6CPF5gE5xkJss2QAFBPNPP4U/ycNb9sI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GQe6PVSwL4zXBOqz97sGLP8XYXueQtCqUbyuIfs9Lqmi1gChITgzGrcpRaEfERiIw O2W+sWdimxLocw4CSwFXMPM+maaQDQ0m2auhYttUEGYdyWa3ZX5Y2wIFTmZSAKd+th jvXHj0xMen1siqEfy716ddFv05pX8DdjQfv0JFMQ= 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.10 574/593] selftests/vm/pkeys: fix alloc_random_pkey() to make it really, really random Date: Mon, 12 Jul 2021 08:12:14 +0200 Message-Id: <20210712060958.184692348@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060843.180606720@linuxfoundation.org> References: <20210712060843.180606720@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