From nobody Thu Jul 10 20:06:45 2025 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 4bdQpt08Vlz61Tv5; Thu, 10 Jul 2025 20:06:46 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bdQps5l17z3Whj; Thu, 10 Jul 2025 20:06:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752178005; 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=A4jMd/vLGVXcC3bGno5e0dwOO+TQ+4o2gXV209go5fA=; b=e9MEebvdoDfhzZydp2dkrYfxIcbOnTUPhvjdPXlOV8q47UWqGCjnRrlXzQ+3yLZ4rSpV8W FNnoekUkke/MyXIlRmZkr860s6W+DNnrIlEgOfXhV95I6ZZyFMD1yFkjBSw2psgmnN9XTH r5yegzLO6k81/f+aYjbZpdavrWHFQs5UPDgctWbuieqgKGIF72Kew56jecr73Bfxs6CDUb pBihdX8945UbH+AXu7GXKaYmCFg7cNTZ+YU07NbTRv7lU2el+xPVdW1bynDxeRP+eXC+ZA mi7RKGo/e0TaRgKRghllLnRdbiLqdbVO/3z/sHoMnRzIk87jUDq1YILopPgwDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752178005; 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=A4jMd/vLGVXcC3bGno5e0dwOO+TQ+4o2gXV209go5fA=; b=wkrch+7P2LN2OLDs27YR5KBrt7F8JYKpDsOGdM26LdX4riuGIb/SSJ5U6+10b7Yl+PuE9j lUmI5yczg8EdXB7ZE2Z4+fA29Z3i67q2eR1U4pplB24gQKQ5MVSM6WQu9UO4zcdVzgAS5s TmR49O2A50GeHMAnjX0ct0JUIDCNRg81PDtSdsk8ZWv0k+Kz26Arb6lwKceezsSBM7Tb0p ibHvTb6/1VMT0yAT80bJFMhiqWZcm5IWsSTPXlWS+G0fBFQoI8dXlLPbdRtDIt21iovvAs h563jKmhgSrlG0qR5dlLtkU4oup3vgK5t3urRMX8I0XT50wxR/m9bvuYYyg3mA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1752178005; a=rsa-sha256; cv=none; b=XcQuNFcUy3CqT8ad0ULuBhEeU6l1V7I+SF1xpHK/3K3o7VXN7lfkTza1/fmW3G7nzs0L+3 s3e9rqg/GZ28nQ1yTVExn+xabtLlRWIfbWUNKXsQGx4MsdXcCCJ972PTtoolaecU0WN1DF 3dmmIcL+sc7HqiTEDgB8hK/rK7fWqwESgowfehMFJZGTnUwV4HNOsheNax+bKHTENv0AXq 6/dk5Sr3NJ2C6Xb9JEkAIvRnslmfNdi7m1sVKhbE275dJscibOIRjyM95xbbuqpAlPqwpR KtoFWdiPCRRpSp3gv/Qc/QITcgMrb8m2DvQJGMRd1pSuvsl8nYMrYvjxNim5vQ== 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 4bdQps586Vz1LT0; Thu, 10 Jul 2025 20:06:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 56AK6jn4016664; Thu, 10 Jul 2025 20:06:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 56AK6j5V016661; Thu, 10 Jul 2025 20:06:45 GMT (envelope-from git) Date: Thu, 10 Jul 2025 20:06:45 GMT Message-Id: <202507102006.56AK6j5V016661@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 475a36762f9d - stable/14 - random: Define a macro for getting the CPU cycle count 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 475a36762f9df862b8cce310fccaed3b36ae3cc6 Auto-Submitted: auto-generated The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=475a36762f9df862b8cce310fccaed3b36ae3cc6 commit 475a36762f9df862b8cce310fccaed3b36ae3cc6 Author: Mark Johnston AuthorDate: 2025-07-03 17:27:47 +0000 Commit: Mark Johnston CommitDate: 2025-07-10 13:51:42 +0000 random: Define a macro for getting the CPU cycle count Entropy queue entries always include the low 32 bits of a CPU cycle count reading. Introduce a macro for this instead of hard-coding get_cyclecount() calls everywhere; this is handy for testing purposes since this way, random(4)'s use of the cycle counter (e.g., the number of bits we use) can be changed in one place. No functional change intended. Reviewed by: cem, delphij MFC after: 1 week Sponsored by: Stormshield Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D51113 (cherry picked from commit e2a96b83404fcee0a079a3c3adbb448b86a38d1e) --- sys/dev/random/random_harvestq.c | 9 +++++---- sys/dev/random/random_harvestq.h | 9 +++++++++ sys/dev/random/randomdev.c | 6 +++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index b5e733dcb8e3..a8eaa932e0b2 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -459,7 +459,7 @@ random_early_prime(char *entropy, size_t len) return (0); for (i = 0; i < len; i += sizeof(event.he_entropy)) { - event.he_somecounter = (uint32_t)get_cyclecount(); + event.he_somecounter = random_get_cyclecount(); event.he_size = sizeof(event.he_entropy); event.he_source = RANDOM_CACHED; event.he_destination = @@ -566,7 +566,7 @@ random_harvest_queue_(const void *entropy, u_int size, enum random_entropy_sourc if (ring_in != harvest_context.hc_entropy_ring.out) { /* The ring is not full */ event = harvest_context.hc_entropy_ring.ring + ring_in; - event->he_somecounter = (uint32_t)get_cyclecount(); + event->he_somecounter = random_get_cyclecount(); event->he_source = origin; event->he_destination = harvest_context.hc_destination[origin]++; if (size <= sizeof(event->he_entropy)) { @@ -595,7 +595,8 @@ random_harvest_fast_(const void *entropy, u_int size) u_int pos; pos = harvest_context.hc_entropy_fast_accumulator.pos; - harvest_context.hc_entropy_fast_accumulator.buf[pos] ^= jenkins_hash(entropy, size, (uint32_t)get_cyclecount()); + harvest_context.hc_entropy_fast_accumulator.buf[pos] ^= + jenkins_hash(entropy, size, random_get_cyclecount()); harvest_context.hc_entropy_fast_accumulator.pos = (pos + 1)%RANDOM_ACCUM_MAX; } @@ -612,7 +613,7 @@ random_harvest_direct_(const void *entropy, u_int size, enum random_entropy_sour KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin)); size = MIN(size, sizeof(event.he_entropy)); - event.he_somecounter = (uint32_t)get_cyclecount(); + event.he_somecounter = random_get_cyclecount(); event.he_size = size; event.he_source = origin; event.he_destination = harvest_context.hc_destination[origin]++; diff --git a/sys/dev/random/random_harvestq.h b/sys/dev/random/random_harvestq.h index edeb4ff7a17f..7804bf52aa4f 100644 --- a/sys/dev/random/random_harvestq.h +++ b/sys/dev/random/random_harvestq.h @@ -27,6 +27,9 @@ #ifndef SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED #define SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED +#include +#include + #define HARVESTSIZE 2 /* Max length in words of each harvested entropy unit */ /* These are used to queue harvested packets of entropy. The entropy @@ -40,4 +43,10 @@ struct harvest_event { uint8_t he_source; /* origin of the entropy */ }; +static inline uint32_t +random_get_cyclecount(void) +{ + return ((uint32_t)get_cyclecount()); +} + #endif /* SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED */ diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index 16f0b2c64f1f..69f7fe58f8a3 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -304,14 +304,14 @@ randomdev_accumulate(uint8_t *buf, u_int count) /* Extra timing here is helpful to scrape scheduler jitter entropy */ randomdev_hash_init(&hash); - timestamp = (uint32_t)get_cyclecount(); + timestamp = random_get_cyclecount(); randomdev_hash_iterate(&hash, ×tamp, sizeof(timestamp)); randomdev_hash_iterate(&hash, buf, count); - timestamp = (uint32_t)get_cyclecount(); + timestamp = random_get_cyclecount(); randomdev_hash_iterate(&hash, ×tamp, sizeof(timestamp)); randomdev_hash_finish(&hash, entropy_data); for (i = 0; i < RANDOM_KEYSIZE_WORDS; i += sizeof(event.he_entropy)/sizeof(event.he_entropy[0])) { - event.he_somecounter = (uint32_t)get_cyclecount(); + event.he_somecounter = random_get_cyclecount(); event.he_size = sizeof(event.he_entropy); event.he_source = RANDOM_CACHED; event.he_destination = destination++; /* Harmless cheating */