From nobody Mon Aug 07 01:00:18 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RJyfV4XPXz4pk66; Mon, 7 Aug 2023 01:00:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RJyfV3vmMz3Y3H; Mon, 7 Aug 2023 01:00:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1691370018; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/JpETDS7mBPSVyAikM2jTre9+AaFigfCPIehYSbUbdA=; b=EFPDzafMAWsU+Q87ps/AVfI4JOqJWFrfOQLDrjwdM4Hmx8r3v29KGX9P5Fa1Xz3ufh5skS /ghPSzLv4Ehj4YZoBXY3ZEnpt4wPEhojEy+pYgJ0QbEPNi8epsd2iqR4ROKXf0RjlOBvNA nv3Gapjgk+oqdJBTlX1HycBlcnDA2fhO4K/PspaIadomc2UGH06s9JTtPQXSVHWTGRVPlC 9wTYYAPdxcFHtqYZubvX8YTXyKj+2ykE2h1eaIwn3q1HyOeSr3M83BvSbOZeu1kMMuv4LQ 0i6k4qpV5jKCRwrygieqUk0XincRv5eyq4awaq0r0WAKseGF7n+SwD8Stp572w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1691370018; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/JpETDS7mBPSVyAikM2jTre9+AaFigfCPIehYSbUbdA=; b=RDOCnrvoYZm77oy3tpBkRxXVHEaMIHGzRJPNquUwhRVzGAWS+lebn7YB1Yq0wetQ5mBcuv VSobscJ07hytBZEANqkh97Uu4gAs6xhSNeBUZBALKVUNmdPVvPlSdY1/IKULqahoaA4ZUw 9TROkbdmvvNwGOZS2DWoyQ2udSu72k5Yea5vZ1Dok+T5Jawz4CgijXyYdsVf/cyZHfJv+e oEiKFIGMwUziF15PPYFgOtQ4eVa734tRjSzQibrDCChHes49u6rwOu8Kf5p+rSKlQEXNym FTaHHNX1+hqGDQ9sMSNlQ2ezvqcfjFvtZLDcwZvGwoOkyQdBUrk+VzJlkoUiIw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1691370018; a=rsa-sha256; cv=none; b=xjgMLFPcpa8fnVP40OBWhF9EG38hnI2ZRAAjwfiTN7sz3Y0Hol5lLCBccQYDu6dV+h4O3V +pqMo5KrRvOnOPzN2ibPB3GlCLoGzXnKsEnRZnSAJW+U0XpPbzeFTPlGIXYITUtIxC5J7t PB4ViScCv4XRBfsfyJ8Qk/I8dxFyz1SiLOuyv6AXLFqXqSs0Z3s8Ip8QQ1AO0gzccw5YJ4 4mJnSvAhnfhfHw9CmzpPSTvfxutfx/w7433sPbJpoERWV6YRYRtQZEVHggjCezZDRkQW9/ AaLiNwHiKYQeFTIt/EXwaC4gakrVqgy/mpT3y7eh+uc6cAEZCrzDuNkAPql9KQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RJyfV30cVzY1r; Mon, 7 Aug 2023 01:00:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37710ISe087011; Mon, 7 Aug 2023 01:00:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37710InY087010; Mon, 7 Aug 2023 01:00:18 GMT (envelope-from git) Date: Mon, 7 Aug 2023 01:00:18 GMT Message-Id: <202308070100.37710InY087010@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: 1dcd1a539275 - main - LinuxKPI: implement get_random_u32_inclusive() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe Auto-Submitted: auto-generated The branch main has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe commit 1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe Author: Bjoern A. Zeeb AuthorDate: 2023-05-16 21:03:30 +0000 Commit: Bjoern A. Zeeb CommitDate: 2023-08-07 00:52:41 +0000 LinuxKPI: implement get_random_u32_inclusive() Implementation of get_random_u32_inclusive(). This is needed by an update for wireless drivers. Sponsored by: The FreeBSD Foundation MFC after: 10 days Differential Revision: https://reviews.freebsd.org/D40121 --- sys/compat/linuxkpi/common/include/linux/random.h | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/random.h b/sys/compat/linuxkpi/common/include/linux/random.h index fafb87cae9fe..561953be277a 100644 --- a/sys/compat/linuxkpi/common/include/linux/random.h +++ b/sys/compat/linuxkpi/common/include/linux/random.h @@ -4,6 +4,10 @@ * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. * All rights reserved. + * Copyright 2023 The FreeBSD Foundation + * + * Portions of this software was developed by Björn Zeeb + * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -36,8 +40,6 @@ #include #include -#define get_random_u32() get_random_int() - static inline void get_random_bytes(void *buf, int nbytes) { @@ -54,6 +56,30 @@ get_random_int(void) return (val); } +#define get_random_u32() get_random_int() + +/* + * See "Fast Random Integer Generation in an Interval" by Daniel Lemire + * [https://arxiv.org/pdf/1805.10941.pdf] for implementation insights. + */ +static inline uint32_t +get_random_u32_inclusive(uint32_t floor, uint32_t ceil) +{ + uint64_t x; + uint32_t t, v; + + MPASS(ceil >= floor); + + v = get_random_u32(); + t = ceil - floor + 1; + x = (uint64_t)t * v; + while (x < t) + x = (uint64_t)t * get_random_u32(); + v = x >> 32; + + return (floor + v); +} + static inline u_long get_random_long(void) {