Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp731508pxu; Sun, 25 Oct 2020 15:21:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnSxPNKq8v7I3R7DR0qp6kFx1W9BSAV8G4FxgkIYKS1VwVSfl5t4kKHx0NfvBnivnoLEll X-Received: by 2002:a17:906:bc57:: with SMTP id s23mr12787064ejv.94.1603664482657; Sun, 25 Oct 2020 15:21:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603664482; cv=none; d=google.com; s=arc-20160816; b=xf8NiTwjhwk1D7g1IA+NlWjbtAshunBLVUV6f5jfmKgn5zf8Pz8PVqunB6dXjWUt/j k/GGiEjutKnsjZdZ6NoMpn5crW4yF8KkUiD/a1BzFfysowzPY/uPNGD3efU1OfZMMb0L UdfLAbfXRV7UVb4sCsdYXcFhRMBpgQtsmjm+KFAnG4AMUJ4K6rgNlarQtZfPoQQLxcR/ iphfLipQMHM7mdQ4borFR5I8hy6Nl1O5LGJrHjUkwLoUTora11ZnyURRGdByPG3zFF0w 8dXwPv7/snNehXWntn2ChVpncYb+Uw0zNrz+KccfOhkqA9w3BPHw+DPM+khIqSuaXjro viEw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cCvtSrWUoPfjEX3MMgBAPpFowxLdp4ZIN2gqdndxr2k=; b=AHlHVKRhcKw9aH1u+Lmk2poIjG+8RZpHQ0nEV8kne5bvJtPXLMEPWf0IG6ZRAni2wf e+nLEC+OXWrefrSeKlj0y73Gch9TWyqboOucDEa62vsf146qMYPcEy7nwvUiLwb7X/ex tEvDbQEsevmpz9ECqfKHoZTmeuPf8E1Vx32D9jE/VU8ls9vN76yKjoz7M2ZTEx4RAo8G A8zpfiuJ8kUaSAWykCjqluMEte3flLeQl78wNm0cKA8lQ45ot6GKY6KKSqIhVNZvxp80 RCpS32/EXvxFZtNZeYnKcPbCvxgdN62tQbSdEqD28gwXPLQyZvdhaiSXffrZ7NWxqAb4 VxKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=YVUcgWiM; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qu2si5454795ejb.177.2020.10.25.15.21.00; Sun, 25 Oct 2020 15:21:22 -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=@rasmusvillemoes.dk header.s=google header.b=YVUcgWiM; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1418981AbgJYVs5 (ORCPT + 99 others); Sun, 25 Oct 2020 17:48:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1418982AbgJYVsv (ORCPT ); Sun, 25 Oct 2020 17:48:51 -0400 Received: from mail-ed1-x543.google.com (mail-ed1-x543.google.com [IPv6:2a00:1450:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BF12C0613D0 for ; Sun, 25 Oct 2020 14:48:51 -0700 (PDT) Received: by mail-ed1-x543.google.com with SMTP id bc23so7394386edb.5 for ; Sun, 25 Oct 2020 14:48:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cCvtSrWUoPfjEX3MMgBAPpFowxLdp4ZIN2gqdndxr2k=; b=YVUcgWiMnFEVymldu6NwvFVQ/iODVY2Me/b603T8P+yLTFzayAhQ4TcXi6GVUOGlXT C5e5z4KjVtabsaTt+bW1YrqRjERzD41hasjbeCPU8ka+c9X9qSZk2k0GCEgK2/G1I24/ YpFolPYq4MNEte2aEV8C/AnTtCKsZX3uGXoBI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cCvtSrWUoPfjEX3MMgBAPpFowxLdp4ZIN2gqdndxr2k=; b=gYpr0R9++kwsvKNcVYFYk+Cm11VlDs521hK5MZuJVMAfqK0+ng0R+PVADtOv6x2kj/ q22Ne+uIQCid7b09fsuXnzY/qfJg8VH+DoeUqookFVoG6cxBV22Y8GgaYD6CYCvYx1N9 Y58WlLszIYH3f/KxLgqPTN09EVlQ+N36lmW2hN1hXNqMHCIkmsfQfXM0NMhJTC3pEpCW yr4aTRjJKV67TWcoEl7XrgglrZyf4jwXf678IwSbSVqJ3AmogV/ADgwBQ8sWfc7SAtNj ieGh1TkBCz+jioB8PordAIdrDwQ0lHj3jaJ9mpmfSMA7OubiSTGiPF4dSTeL4EZ31lmD rghw== X-Gm-Message-State: AOAM5311lTc5Hen2S1ygHGTVRGBBO+zH0W8QsuF6vsSLVYB/YY92047J w+A0rk35OxEYRRMQsBYBw19X8g== X-Received: by 2002:aa7:cd42:: with SMTP id v2mr10886769edw.191.1603662530159; Sun, 25 Oct 2020 14:48:50 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:49 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 3/4] kselftest_module.h: add struct rnd_state and seed parameter Date: Sun, 25 Oct 2020 22:48:41 +0100 Message-Id: <20201025214842.5924-4-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some test suites make use of random numbers to increase the test coverage when the test suite gets run on different machines and increase the chance of some corner case bug being discovered - and I'm planning on extending some existing ones in that direction as well. However, should a bug be found this way, it's important that the exact same series of tests can be repeated to verify the bug is fixed. That means the random numbers must be obtained deterministically from a generator private to the test module. To avoid adding boilerplate to various test modules, put some logic into kselftest_module.h: If the module declares that it will use random numbers, add a "seed" module parameter. If not explicitly given when the module is loaded (or via kernel command line), obtain a random one. In either case, print the seed used, and repeat that information if there was at least one test failing. Signed-off-by: Rasmus Villemoes --- tools/testing/selftests/kselftest_module.h | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kselftest_module.h b/tools/testing/selftests/kselftest_module.h index c81c0b0c054befaf665b..43f3ca58fcd550b8ac83 100644 --- a/tools/testing/selftests/kselftest_module.h +++ b/tools/testing/selftests/kselftest_module.h @@ -3,14 +3,31 @@ #define __KSELFTEST_MODULE_H #include +#include +#include /* * Test framework for writing test modules to be loaded by kselftest. * See Documentation/dev-tools/kselftest.rst for an example test module. */ +/* + * If the test module makes use of random numbers, define KSTM_RANDOM + * to 1 before including this header. Then a module parameter "seed" + * will be defined. If not given, a random one will be obtained. In + * either case, the used seed is reported, so the exact same series of + * tests can be repeated by loading the module with that seed + * given. + */ + +#ifndef KSTM_RANDOM +#define KSTM_RANDOM 0 +#endif + static unsigned int total_tests __initdata; static unsigned int failed_tests __initdata; +static struct rnd_state rnd_state __initdata; +static u64 seed __initdata; #define KSTM_CHECK_ZERO(x) do { \ total_tests++; \ @@ -22,11 +39,13 @@ static unsigned int failed_tests __initdata; static inline int kstm_report(unsigned int total_tests, unsigned int failed_tests) { - if (failed_tests == 0) + if (failed_tests == 0) { pr_info("all %u tests passed\n", total_tests); - else + } else { pr_warn("failed %u out of %u tests\n", failed_tests, total_tests); - + if (KSTM_RANDOM) + pr_info("random seed used was 0x%016llx\n", seed); + } return failed_tests ? -EINVAL : 0; } @@ -34,6 +53,12 @@ static inline int kstm_report(unsigned int total_tests, unsigned int failed_test static int __init __module##_init(void) \ { \ pr_info("loaded.\n"); \ + if (KSTM_RANDOM) { \ + if (!seed) \ + seed = get_random_u64(); \ + prandom_seed_state(&rnd_state, seed); \ + pr_info("random seed = 0x%016llx\n", seed); \ + } \ selftest(); \ return kstm_report(total_tests, failed_tests); \ } \ @@ -44,4 +69,8 @@ static void __exit __module##_exit(void) \ module_init(__module##_init); \ module_exit(__module##_exit) +#if KSTM_RANDOM +module_param(seed, ullong, 0444); +#endif + #endif /* __KSELFTEST_MODULE_H */ -- 2.23.0