From patchwork Thu Sep 30 21:23:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12529341 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AE75C433EF for ; Thu, 30 Sep 2021 21:23:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BC4EE61A59 for ; Thu, 30 Sep 2021 21:23:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BC4EE61A59 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 64CBC9400D1; Thu, 30 Sep 2021 17:23:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6240B94003A; Thu, 30 Sep 2021 17:23:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4EB3A9400D1; Thu, 30 Sep 2021 17:23:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0054.hostedemail.com [216.40.44.54]) by kanga.kvack.org (Postfix) with ESMTP id 3DAD494003A for ; Thu, 30 Sep 2021 17:23:20 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 009128249980 for ; Thu, 30 Sep 2021 21:23:19 +0000 (UTC) X-FDA: 78645515760.28.71D3AF1 Received: from mail-qv1-f73.google.com (mail-qv1-f73.google.com [209.85.219.73]) by imf08.hostedemail.com (Postfix) with ESMTP id B5B1A3001852 for ; Thu, 30 Sep 2021 21:23:19 +0000 (UTC) Received: by mail-qv1-f73.google.com with SMTP id g15-20020ad457af000000b0038266dd4333so11800787qvx.10 for ; Thu, 30 Sep 2021 14:23:19 -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=wbUrvvm0+MYAPUCF6HQK8A4cSFDFNCfNO8ihEMufpf8=; b=DXFhvoYgkBKBGW9TTScr4boM8/R+bDeePetODemKEZwYhJLpOOzrVNl6U0O8viIeqk pp2IrEkYyQyq4j2XkKfWQ4VittBUoFL9eK6QgTF8xsJnQegzB1LmqaFt8EgKiCyym1JF GE/gxRXqPXWqWaj96T8kz7tdyVHIJtP91uuXP/xkt5uGZ9Ozc/hLHZVAjgDUOulIv5Gj JqN+T2CECrPI/0jEt+gTV12f8/TErC9CAukhyWp/vFoE+aRLAIFWUO/ZufY6xbX/VP++ 2SBzxCS4wW06iaipZX8I5I10tTC9VxTqNnPo/gvPYQVZ4WpVfyneUQu3oXKgMzRcQd3o zngA== 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=wbUrvvm0+MYAPUCF6HQK8A4cSFDFNCfNO8ihEMufpf8=; b=lsqafiTGbVkGpmMblEC8SY4GIXZ9xM4zFvjCy85j+DfnPAlgWy44t1/p27a310OiHv 0Oqqm+d6guML/WQ69izz+DgIcakVZmGEhu3vrk7VhwKQxynnCearvPwxis6TieNfFs/u A1qJtjqfNqCfryiuar5s9LDuzElAzVrcsGhuyNljtSXjq0cVBMdW0/LU80N63NUTUlso Ata9saVOF4p+758igu9m+B8zh9JhXVMm4kwoMDArWB/Cpiz1ZxV6Pyjnjhy1I7oIaPfN nVL822Uwn+AfP6Y7nL531CrXKkD6uV6dBwOiRdhgUp5KKDpRgukI2udHcoHQXepAiDdS 9pBQ== X-Gm-Message-State: AOAM53128h9Z6uDkok8Njj6nTxXcyyaPD7lJEogy58e9YMbl5zRkSjl6 3lTKY/ustixzg5jrMvBDi6YFIiZ3koD2R5NPKQWo X-Google-Smtp-Source: ABdhPJywOpI+b7jm0hToN3nP52PlVum8uravic0hgie7sww9kEWkrOanS+hnSp8P208w/79s1xImwJumjKcifP/pZ7/r X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:fa06:3b29:d3c:37e6]) (user=axelrasmussen job=sendgmr) by 2002:a05:6214:12af:: with SMTP id w15mr5967689qvu.66.1633036999024; Thu, 30 Sep 2021 14:23:19 -0700 (PDT) Date: Thu, 30 Sep 2021 14:23:07 -0700 In-Reply-To: <20210930212309.4001967-1-axelrasmussen@google.com> Message-Id: <20210930212309.4001967-2-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210930212309.4001967-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.33.0.800.g4c38ced690-goog Subject: [PATCH v2 1/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-Queue-Id: B5B1A3001852 X-Stat-Signature: d9o7tpgfrcjterbpfr5onkhmgim7bw9m Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DXFhvoYg; spf=pass (imf08.hostedemail.com: domain of 3xypWYQ0KCIYk7ov1k2w422oxqyyqvo.mywvsx47-wwu5kmu.y1q@flex--axelrasmussen.bounces.google.com designates 209.85.219.73 as permitted sender) smtp.mailfrom=3xypWYQ0KCIYk7ov1k2w422oxqyyqvo.mywvsx47-wwu5kmu.y1q@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-HE-Tag: 1633036999-393133 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000052, 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. Reviewed-by: Peter Xu Signed-off-by: Axel Rasmussen --- 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 10ab56c2484a..2a71a91559a7 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" @@ -501,22 +502,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; @@ -524,15 +513,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;