Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3767692pxv; Mon, 19 Jul 2021 08:17:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEyhLaRlqFLwp2YV/vR/CI5v63E3GLsOgmaHKhqScS8isPH4wkHAeHU5o0Ifw4WirxD/CG X-Received: by 2002:a92:bf0b:: with SMTP id z11mr18239855ilh.60.1626707844615; Mon, 19 Jul 2021 08:17:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626707844; cv=none; d=google.com; s=arc-20160816; b=NaEj/6BktxPb3lSkv+qM4q46YQmpe7VfKo4I7yrOQf4bgzvKFh+3OkRRTDvp2Tt322 1RWkjpQ6L7q9E6K0GwOlRsa6H+DUS3QfuNwd0qGWFDw/cDLPZr/4PEQc/rr5rLMmPqMT cLLWjUYBpZKmGQLSQdNqKZL8boY8XxNgHwZnjz1Olo7J5ue1OX7uDHYu9M1Qiefv/8zR lAmN2aB9UTJvuYcvtwJKt7MU3XpRV4zwgjy5F2val5TldmrZHy3d38GvmXMDPy4XOfgF 1jxjaAUr+pjrtAKTdENd6ANBjK4APwuBwq+fDJV87MyUop1YN/5wCayNM0Ei+QqXdtMD Maag== 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=38ZS/7raWyLuSBOtEN+BHLmLc3i6KI+vwdbkDu+h8qA=; b=Gs0bGjwxg/7w/+3dglA/xQ/4rFNcX9ZMjJm346E9QibuZZpSXg1MkqbOg8FtX1tXOm yJrRd5xDWeEGOZoh1MPrtUB4zf84EbxYkcoau8O5bb38O5+SzgnFUbdziZXw/QtM4gDr gWHapWjCUhH+He+kR3o7AWoPlqVEFXLgq9L4rLBHschfv+IGT/xNXHkDrZYApgX1Wg6G DqYKa7gWBYD13uvuHrWYswq9zp9UhXh7kPIAPoAR7yX0TA9DpaWZRwXVkTru1Kbhw2Gw 6Ix/ToaQhaOcQsFUxBCR8xzWEO3iU+vAFFPY6xioWWQ7GUVd+/Kc6fSLvz5diD10naYo IVtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LnwbTQnj; 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 q10si23907186ilu.60.2021.07.19.08.17.12; Mon, 19 Jul 2021 08:17:24 -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=LnwbTQnj; 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 S241091AbhGSOfZ (ORCPT + 99 others); Mon, 19 Jul 2021 10:35:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:38986 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244233AbhGSO32 (ORCPT ); Mon, 19 Jul 2021 10:29:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1FC2F61287; Mon, 19 Jul 2021 15:09:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626707340; bh=zeHqsCcQ31O/wt2L4qPT2BBmQLyfLs2ZaJFSVRXw3E0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LnwbTQnjJkv39qdUJZQRahUoM+uSVLz6VFmhMe9Fx/FA8KNqO7S6ZTrLpJ2IJspX5 3AnazzgBN279E0MGvlW59E4p5CnhNXpYm1Kq9hjTETgRFD2B97lFXGADOlc93K9Xgv XUj0jW6aU5NBizt33b35v+/T/IfrQ0+rQFKjmvvM= 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 4.9 121/245] selftests/vm/pkeys: fix alloc_random_pkey() to make it really, really random Date: Mon, 19 Jul 2021 16:51:03 +0200 Message-Id: <20210719144944.329974715@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144940.288257948@linuxfoundation.org> References: <20210719144940.288257948@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/x86/protection_keys.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/x86/protection_keys.c b/tools/testing/selftests/x86/protection_keys.c index 5338e668b5e6..d78736845b8e 100644 --- a/tools/testing/selftests/x86/protection_keys.c +++ b/tools/testing/selftests/x86/protection_keys.c @@ -609,7 +609,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; @@ -1387,6 +1386,8 @@ int main(void) { int nr_iterations = 22; + srand((unsigned int)time(NULL)); + setup_handlers(); printf("has pku: %d\n", cpu_has_pku()); -- 2.30.2