Skip to contentSkip to conversation

rust: Add support for feeding entropy to randomness pool

PATCH Superseded Matthew Maurer 2 participants 3 messages Dec 12, 2025 – Dec 13, 2025 Rust

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: Add support for feeding entropy to randomness pool

Adds just enough support to allow device drivers to feed entropy to the central pool.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/rand.rs | 14 ++++++++++++++
 2 files changed, 15 insertions(+)
rust/kernel/lib.rs +1 -0
index f812cf12004286962985a068665443dc22c389a2..f93886ef4eec9c4356799f4b55916bc12c10c621 100644
@@ -128,6 +128,7 @@
 pub mod print;
 pub mod processor;
 pub mod ptr;
+pub mod rand;
 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
 pub mod pwm;
 pub mod rbtree;
rust/kernel/rand.rs new file +14 -0
new file mode 100644
index 0000000000000000000000000000000000000000..b3fb30f40a8950ac7b47d48129eb89024a1cbd26
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.
+//!
+//! C header: [`include/linux/random.h`](../../../../include/linux/random.h)
+
+use crate::bindings;
+use crate::ffi::c_void;
+
+/// Adds the given buffer to the entropy pool.
+pub fn add_device_randomness(buf: &[u8]) {
+    // SAFETY: We just need the pointer to be valid for the length, which a slice provides.
+    unsafe { bindings::add_device_randomness(buf.as_ptr().cast::<c_void>(), buf.len()) };
+}

---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251029-add-entropy-f57e12ebe110

Best regards,

Signature · 1 line
Matthew Maurer <[email protected]>
#1 lore raw
Quoted 15 lines · Matthew Maurer

Adds just enough support to allow device drivers to feed entropy to the central pool.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/rand.rs | 14 ++++++++++++++
 2 files changed, 15 insertions(+)
rust/kernel/lib.rs +0 -0
index f812cf12004286962985a068665443dc22c389a2..f93886ef4eec9c4356799f4b55916bc12c10c621 100644

rust/kernel/lib.rs line 131

Quoted 4 lines
rust/kernel/lib.rs +1 -0
@@ -128,6 +128,7 @@
 pub mod print;
 pub mod processor;
 pub mod ptr;
+pub mod rand;

I've just noticed that the rebase misalphabetized this. I'll send up a v2 after folks make any comments they want, but figure I don't need to spam you for now.

Quoted 32 lines
rust/kernel/lib.rs +0 -0
 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
 pub mod pwm;
 pub mod rbtree;
rust/kernel/rand.rs new file +14 -1
new file mode 100644
index 0000000000000000000000000000000000000000..b3fb30f40a8950ac7b47d48129eb89024a1cbd26
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.
+//!
+//! C header: [`include/linux/random.h`](../../../../include/linux/random.h)
+
+use crate::bindings;
+use crate::ffi::c_void;
+
+/// Adds the given buffer to the entropy pool.
+pub fn add_device_randomness(buf: &[u8]) {
+    // SAFETY: We just need the pointer to be valid for the length, which a slice provides.
+    unsafe { bindings::add_device_randomness(buf.as_ptr().cast::<c_void>(), buf.len()) };
+}
 
---

base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251029-add-entropy-f57e12ebe110

Best regards,
--
Matthew Maurer <[email protected]>

#2 lore raw
In reply to Matthew Maurer

On Fri, 12 Dec 2025 23:19:07 +0000
Matthew Maurer <[email protected]> wrote:

Hi, Matthew.

I think exposing add_device_randomness() in rust/kernel makes sense, especially for Rust device drivers that already have access to hardware-specific noise sources.

Quoted 33 lines

Adds just enough support to allow device drivers to feed entropy to the central pool.

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/rand.rs | 14 ++++++++++++++
 2 files changed, 15 insertions(+)
rust/kernel/lib.rs +0 -0

index f812cf12004286962985a068665443dc22c389a2..f93886ef4eec9c4356799f4b55916bc12c10c621 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs

rust/kernel/lib.rs +1 -0
@@ -128,6 +128,7 @@
 pub mod print;
 pub mod processor;
 pub mod ptr;
+pub mod rand;
 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
 pub mod pwm;
 pub mod rbtree;
rust/kernel/rand.rs new file +0 -0
new file mode 100644

index 0000000000000000000000000000000000000000..b3fb30f40a8950ac7b47d48129eb89024a1cbd26 --- /dev/null +++ b/rust/kernel/rand.rs

rust/kernel/rand.rs new file +5 -0
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.
+//!
+//! C header:

[include/linux/random.h](../../../../include/linux/random.h) +

rust/kernel/rand.rs line 10

Quoted 4 lines
rust/kernel/rand.rs new file +4 -0
+use crate::bindings;
+use crate::ffi::c_void;
+
+/// Adds the given buffer to the entropy pool.

I wonder if it would be useful to document more explicitly that this function does not credit entropy, matching the C-side semantics, to avoid semantic misuse by drivers.

Quoted 3 lines
rust/kernel/rand.rs new file +2 -0
+pub fn add_device_randomness(buf: &[u8]) {
+    // SAFETY: We just need the pointer to be valid for the length,

which a slice provides.

From a Rust safety-contract perspective, this wrapper looks fine to be safe, since the pointer is not retained and the data is only read synchronously.(may be you )

Quoted 9 lines

+ unsafe {
bindings::add_device_randomness(buf.as_ptr().cast::<c_void>(), buf.len()) }; +}

---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251029-add-entropy-f57e12ebe110

Best regards,