From nobody Thu Jul 3 17:35:51 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 4bY3nz6F5Zz61T6C; Thu, 03 Jul 2025 17:35:51 +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 4bY3nz3cpmz3ZxD; Thu, 03 Jul 2025 17:35:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751564151; 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=LXiBFhyxQGktD4QCTK9xhTRtHkLc/HvRShkzul9ArbU=; b=vF/oFmE3cFi43G2BmsIAQL0AZgMOv7IsW1bDCXBnw5AQyXIWfZ/fxrrXA6dljPf9ER70Xr 8kLaWfYfaGt9YeXYwcv5L7PHeQI4HwnVvt9NOD/n0g8F80tKcw6NnuUjwT4QdIOcbys1Ym Jc+IbeBnSOoXKJH12fifOiqk66N1OD7sqC+mmRfDoIMSqveGkk5Z3CFpcmOiAut9dlLxc0 mzSlmIOMqPresk6tir+ohP5F9g/AZs/oe3tLZHz8QpjMcmg850nvFLJX/Orp06w2hCtEud OZySxF1W6yDqMw66/cMBqJmC/H4y6Jfozj/jiw0cKP1YDwWhp8vdAGyfv+MHDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751564151; 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=LXiBFhyxQGktD4QCTK9xhTRtHkLc/HvRShkzul9ArbU=; b=ak98J6utWIYNv9kD+XMhKy5vHJ8xiUZcgkRAnvQXria7QLCZgVkgv0JFDtntSeS2TeiDYX fIdN7gNLVbNEarepN0R7xRYb2gnmbi7Fv8y6CzCBxi2BwaMLH8BaeHGoEVfEdZvv+oQqhp RVSbdcXDj4REH8cTmp0DPQSueIlk1a6/nLWUsB2sgzrJzbQu3OEo0r+HKTMp+Nmj0C4O1t 3mpsj75YSCk5gDIZY5f6IFCL5AuxICJe1FypFjssnq5aZzGEG9NfKBeHojLgcTvUt3wPOE wGdXoge+d+LvixE+rX5aKaYdvXHIEp5y2om8roFYuIgaZ4Xg2ZBVU+c9ajv4Yw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1751564151; a=rsa-sha256; cv=none; b=kSMm3Xi1kRLh8fncOAzqxY2VSiYwAUsVc0JsGnNlz2H0kZD7+u3iv9hX1/vyUpQGUjKJvq BV0Xfo4vsYtgZ5oX/4dkdbKl3Ni2YYT/izEa8a7OAB0dhkTpBa6ct/xwdhSHBp6Ls1OyjS kaE2GuoCA6JD2RSLQFrKOeDU7f3GGLbzPzfnshpZEfjiauXR3HA3ADWafQQMWLMBPginW2 tvzklx5XxYgUlEG88xM11w2jf53zV455q2Kpbc7reIJumnxNgyXkYGlEZpEtz/xmIzfPbj 0Vj6uY8J2P4WpxQ/Gp6WeQP8LaUuaucL8Y52R2k9ihcVZuVRbr0iL0Kaoe5HiA== 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 4bY3nz36H8zPG; Thu, 03 Jul 2025 17:35:51 +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 563HZpNE076139; Thu, 3 Jul 2025 17:35:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 563HZpFL076136; Thu, 3 Jul 2025 17:35:51 GMT (envelope-from git) Date: Thu, 3 Jul 2025 17:35:51 GMT Message-Id: <202507031735.563HZpFL076136@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: e2a96b83404f - main - 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/main X-Git-Reftype: branch X-Git-Commit: e2a96b83404fcee0a079a3c3adbb448b86a38d1e Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=e2a96b83404fcee0a079a3c3adbb448b86a38d1e commit e2a96b83404fcee0a079a3c3adbb448b86a38d1e Author: Mark Johnston AuthorDate: 2025-07-03 17:27:47 +0000 Commit: Mark Johnston CommitDate: 2025-07-03 17:31: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 --- 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 1a8cbd9dcad7..ef12fe2d2e44 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 6d637ab5a53e..9d1c7b1167c8 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -303,14 +303,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 */