From nobody Wed Nov 29 16:37:52 2023 X-Original-To: dev-commits-src-all@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 4SgQ3j1bNkz52wWy; Wed, 29 Nov 2023 16:37:53 +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 4SgQ3j18ypz3GBc; Wed, 29 Nov 2023 16:37:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1701275873; 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=VB8dtSEoTxITFxvFm1MkQJQiDU2Zx0F2kewWznnT33s=; b=fUQT6jPuodfg7bTs90iA4VbIL6NBCbL47k+TA5t7Ueq1ScRl/q+yxa2ox2Qd+xPV1R9Yk5 6wJEWOCcviMD1lI+OfAoXm1tOvjsdjJimE+5GNwcl7TrkgAiEUR7uDjZjrNXeuItR+QoKf a7xNa5OTy2KvcHWXale2XHM62hXHiuvwnqKC2adC6rxwoiYbd7frK8SAgLqO41hDgBJ/VH d2rcvyVGcI2hAOPbVywzitE7UTPvmOZo5mGDCGh9Yv/G3xiS52UHGxPG+1XDyeADAy354P UFCpkl+z74dg8SICzO+2AnJJco7LeU8aoDRAjH3fPJ/GWrOyyzDTJDMRN8I0Og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1701275873; 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=VB8dtSEoTxITFxvFm1MkQJQiDU2Zx0F2kewWznnT33s=; b=Kq0kAWbhz8Fkc/7sio5df6KNzPEzRbFl4O1tSBg8RMBRyW1PpuaMGeGF9IIHT9SJehCTn8 A/z4S1TjKnUMahfefyWGJygx50KeU7HCmRf4C3fJN439fiFzRo0TuCi04A5saYgsv4Md5R zgTPk//pkMD8CWkCeDxEVQlw/W74vdEr8U91mAiUVkzZLlRX1KEsAm13v92y6xat4r0TT/ gxdohLVM53IJHtUy73Hm8MYOkLNVuc9wC1vOyCOD82lTKGFKFGw+cueIM+H9k/KUJD7olb TEoIaQMetTyqsIN4uglMxpkBlv1yilJmbFOHvamcbrr2fSP1rODzkniW84cmvw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1701275873; a=rsa-sha256; cv=none; b=muP7t4E3JbSsf3+F44Sb9Tb0glcW+K+ZnBbv55xcjwiSpqlEC6itiLGW/hMJ9gyWeiP/o0 b0a8SinFrSb0UINBLwlM5cQkTSrYNDXdj7kY5RXKUZTPvASpRGx4qRbXP9I95Oh+CNScZL wa+Rf39Brp+ikmojyZhL4y6FxfmpW7jwaVB2axvm5oLPLWwBUwP7XXlxeSzzxF/7f5mVg/ L+GvgtevqNQGrRlVgBbk2PWg6NZZWFPTggVigpM9yoHyI7UQPBq5wyDbG/stCggB+RyDUy sansxrKnsrBCFMKkKinblDY2MtGYH4dD3M1FvonJcABsfPB4ek0bKu2M68msXA== 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 4SgQ3j04jcz1C7Q; Wed, 29 Nov 2023 16:37:53 +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 3ATGbqta044383; Wed, 29 Nov 2023 16:37:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3ATGbqWV044380; Wed, 29 Nov 2023 16:37:52 GMT (envelope-from git) Date: Wed, 29 Nov 2023 16:37:52 GMT Message-Id: <202311291637.3ATGbqWV044380@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: 5d493a9556ff - stable/13 - LinuxKPI: implement get_random_u32_inclusive() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 5d493a9556ff9c60519838f7a27df60bf695187a Auto-Submitted: auto-generated The branch stable/13 has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=5d493a9556ff9c60519838f7a27df60bf695187a commit 5d493a9556ff9c60519838f7a27df60bf695187a Author: Bjoern A. Zeeb AuthorDate: 2023-05-16 21:03:30 +0000 Commit: Bjoern A. Zeeb CommitDate: 2023-11-29 16:36:03 +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 (cherry picked from commit 1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe) --- 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 57310377c0c9..5335cca8251c 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 @@ -34,8 +38,6 @@ #include #include -#define get_random_u32() get_random_int() - static inline void get_random_bytes(void *buf, int nbytes) { @@ -52,6 +54,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) {