From patchwork Tue Sep 21 16:33:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12508329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53A1AC4332F for ; Tue, 21 Sep 2021 16:33:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F354C60F6D for ; Tue, 21 Sep 2021 16:33:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F354C60F6D Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 929D7940013; Tue, 21 Sep 2021 12:33:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D6F0940007; Tue, 21 Sep 2021 12:33:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C671940013; Tue, 21 Sep 2021 12:33:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0199.hostedemail.com [216.40.44.199]) by kanga.kvack.org (Postfix) with ESMTP id 6DC1F940007 for ; Tue, 21 Sep 2021 12:33:32 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2547B1812ED86 for ; Tue, 21 Sep 2021 16:33:32 +0000 (UTC) X-FDA: 78612126264.04.3753197 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf13.hostedemail.com (Postfix) with ESMTP id E6C9A10DFED1 for ; Tue, 21 Sep 2021 16:33:31 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id h10-20020a05620a284a00b003d30e8c8cb5so180945306qkp.11 for ; Tue, 21 Sep 2021 09:33:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=XXlfpcBmbRno5SA+gst6Gevlk7ltg9389UQ9dhNVCvA=; b=Wf+QnXwZGfTR93ea68ZPKzFik9oK5cF0gl/6r7BXDTKkCEkNlT5puLNHR76CLQ4TxS sqnivS0sK15CMFeU/ykdAnnhQlLBTCOEwfM3X+Sc1uvAZ3q2dU+XX2CTC6tqtw/qpobw jEpk3zQXOidiuHrp/6oAP/MWm+Ybq220aML/CuZnSDiuYveXKux6S17aaiLnikWbL+QL k+qzoGVvrmTGw0A2trsphU4lw0U4aTCe3vkkP2ZuPFRvH5Eg6dTZcXR2p3I8D0o3GQAN wmv53dN1fi7EhnyFpRlW+rbaFzAB2o8DOg/NIAJK4uDK/coDPCIvKWAdo9GuKq7tFxG7 uVsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XXlfpcBmbRno5SA+gst6Gevlk7ltg9389UQ9dhNVCvA=; b=vSd5+hMmr6on1nKciNqwnPF/VRffF+Jnn8LxGdvGxApLOO21AaTiw2kE63ccLaa141 p1TnvMQV5s0q/WPtja3dRnKHjFBMFzXa903Y6kjCrwDPKxXx62P7mpFOlQlaERDHyM1K 2zV+qe5LwTRFMJNjl1Lb6HSZ12NezonX9oiTMCm6XGlYgiimJUKGrlDuwOsoXR66qbD2 4vpgpFjZhBbSK7KlfjhJ7IR42CX0Vynn2pgFQFYIPbFI3N+boPx5CkMYKiivCfcYhA+3 vHfY8wC3IOo+/8UkqyxELYDvx+qLTCX4Z3SM4GbSCE1/FY3nBaqSMSoJfv6AfAOBdoVJ 3juA== X-Gm-Message-State: AOAM532j8dHeDM12tmz5zuiBJeKvaaSGD/ayEnfURUd5D9iSqTIb48EG 2AKwZqUvE6c62wRUOvYlRi/VjB4nUHAuiwfvJATb X-Google-Smtp-Source: ABdhPJwgz8JIJfqh2iJ8cYLJJQmL1F5G7QJHu8shYN6bVP9STb44sPjeNHitaFyPqxCJcvfDRzpn0Hr1vzMSdjOiXvAJ X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:ed57:a230:8c3d:64d8]) (user=axelrasmussen job=sendgmr) by 2002:a05:6902:124e:: with SMTP id t14mr39259664ybu.106.1632242011222; Tue, 21 Sep 2021 09:33:31 -0700 (PDT) Date: Tue, 21 Sep 2021 09:33:23 -0700 In-Reply-To: <20210921163323.944352-1-axelrasmussen@google.com> Message-Id: <20210921163323.944352-3-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210921163323.944352-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog Subject: [PATCH 3/3] userfaultfd/selftests: don't rely on GNU extensions for random numbers From: Axel Rasmussen To: Andrew Morton , Peter Xu , Shuah Khan Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Axel Rasmussen X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E6C9A10DFED1 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Wf+QnXwZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3WwlKYQ0KCKYGdKRXGYSaYYKTMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--axelrasmussen.bounces.google.com designates 209.85.222.201 as permitted sender) smtp.mailfrom=3WwlKYQ0KCKYGdKRXGYSaYYKTMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--axelrasmussen.bounces.google.com X-Stat-Signature: 7qdihganz176u539fdjsb85qz1br37yh X-HE-Tag: 1632242011-568897 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Two arguments for doing this: First, and maybe most importantly, the resulting code is significantly shorter / simpler. Then, we avoid using GNU libc extensions. Why does this matter? It makes testing userfaultfd with the selftest easier e.g. on distros which use something other than glibc (e.g., Alpine, which uses musl); basically, it makes the test more portable. Signed-off-by: Axel Rasmussen Reviewed-by: Peter Xu --- tools/testing/selftests/vm/userfaultfd.c | 26 ++++-------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index aad5211f5012..9d9f60e71524 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -57,6 +57,7 @@ #include #include #include +#include #include "../kselftest.h" @@ -528,22 +529,10 @@ static void continue_range(int ufd, __u64 start, __u64 len) static void *locking_thread(void *arg) { unsigned long cpu = (unsigned long) arg; - struct random_data rand; unsigned long page_nr = *(&(page_nr)); /* uninitialized warning */ - int32_t rand_nr; unsigned long long count; - char randstate[64]; - unsigned int seed; - if (bounces & BOUNCE_RANDOM) { - seed = (unsigned int) time(NULL) - bounces; - if (!(bounces & BOUNCE_RACINGFAULTS)) - seed += cpu; - bzero(&rand, sizeof(rand)); - bzero(&randstate, sizeof(randstate)); - if (initstate_r(seed, randstate, sizeof(randstate), &rand)) - err("initstate_r failed"); - } else { + if (!(bounces & BOUNCE_RANDOM)) { page_nr = -bounces; if (!(bounces & BOUNCE_RACINGFAULTS)) page_nr += cpu * nr_pages_per_cpu; @@ -551,15 +540,8 @@ static void *locking_thread(void *arg) while (!finished) { if (bounces & BOUNCE_RANDOM) { - if (random_r(&rand, &rand_nr)) - err("random_r failed"); - page_nr = rand_nr; - if (sizeof(page_nr) > sizeof(rand_nr)) { - if (random_r(&rand, &rand_nr)) - err("random_r failed"); - page_nr |= (((unsigned long) rand_nr) << 16) << - 16; - } + if (getrandom(&page_nr, sizeof(page_nr), 0) != sizeof(page_nr)) + err("getrandom failed"); } else page_nr += 1; page_nr %= nr_pages;