Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2713557pxv; Sun, 11 Jul 2021 23:33:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyUE+fK4spHfdexm2bhxfJHEZ5T4840D0u8iik75cinO3mUIdqpYkoyA4ZbLi9znKkXCxmJ X-Received: by 2002:a17:906:696:: with SMTP id u22mr13161184ejb.77.1626071609227; Sun, 11 Jul 2021 23:33:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626071609; cv=none; d=google.com; s=arc-20160816; b=iWUAyM2oKZBD3tern8kpOj9Kq6FhGzn5HWIsKPTaDChzxADauQPaXGKTIS/n1D0zLs CqAvZT43Y5xPB8dihrYtn/Y1t1cNzeKUDXI/GJhIZlvqc+Xwh7sFRcor9Pzg1iuVvCQ4 Ej+OPDkzr3CV8lUQLNMgAfd1auFut/RY5KEHmBBeYbe/Fr8iPiPMytE7YZ6iNhE7CruJ XwJDdI8tQEWCz6YtFcNy2ce3zOb37Cqhisl0+TLe+bkH0bhKdXo1n0a7L5O/4RGClxHg ixgMRzYJzEgwh1FohSz+uvotjos/6RIr6Hta4zKMonl3lOo++kyAHLbaFX2WF6akCKut 27Uw== 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=9Q3Sfjw2aT7kpjJjzkRqX/0T216wOcX1BbKMpmempGY=; b=RwFITsJbU0vli0YQ+AYlQeWCxfLpCtq3SVu7m5V8LNTKPjuR5Vuj20GKh+U/rKdbbz D7QK1jrv2wlSfIrmdg64riw0lvGBT5M+KNzPTwAZbfKyaJ11IyKNkxLlXSI//MosdmvS kBkFKYLw5e048SP5aeXzPdJp8/R09JGPtbpVvyJiAg4FPWD3gZT4LAUTtq33x+JTcYdO hpNsbDKnskAFsgmwEVvQgchcIAPG1lzCw/rZZ4TD7vPDiNKJoFFFeTnDKt9FiKIzTjyM 5xW3HzywoL6fMRNynQOhg52fp2eGL3helD+hmuB2YZ7S3vTxsLx/XMZK9wLJW6gnxvsM AARw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=TOqxLLCf; 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 i10si16037913ejw.269.2021.07.11.23.33.06; Sun, 11 Jul 2021 23:33:29 -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=TOqxLLCf; 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 S237585AbhGLGej (ORCPT + 99 others); Mon, 12 Jul 2021 02:34:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:52800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237009AbhGLGab (ORCPT ); Mon, 12 Jul 2021 02:30:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9E5A2601FC; Mon, 12 Jul 2021 06:27:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626071263; bh=nEukOyTbLhZZ9KDODgLCPj+m5m81RFSNtNnCsHQf0PM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TOqxLLCfRQHE7+h5E+1QoEfBdCieOXyUyKk2PjnNsRdJvjbUAo6k7kgF7Uh+RcBWn KO/+txK4oRbwQFGunl0dYTcrrFIjSHXn7YeuKSKCVf3bCsvWltVCeKzP+gUQRqU+D/ g9OIHD8oIOUmApcyz7Cj9yYL9IgcRrsk+T9t9Um8= 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.4 342/348] selftests/vm/pkeys: fix alloc_random_pkey() to make it really, really random Date: Mon, 12 Jul 2021 08:12:06 +0200 Message-Id: <20210712060749.480520738@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060659.886176320@linuxfoundation.org> References: <20210712060659.886176320@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 47191af46617..a3602148e2ea 100644 --- a/tools/testing/selftests/x86/protection_keys.c +++ b/tools/testing/selftests/x86/protection_keys.c @@ -613,7 +613,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; @@ -1479,6 +1478,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