Skip to contentSkip to conversation

rust: Add support for feeding entropy to randomness pool

PATCH Superseded Matthew Maurer 5 participants 7 messages Dec 26, 2025 – Dec 30, 2025 Rust

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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.

---
Changes in v3:
- Fixed doclink to be srctree-based instead of relative.
- Switched to prelude import instead of fine-grained imports.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Added more details in the docs about the API, specifically about it
not crediting entropy and when it ought to be used.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/rand.rs | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
rust/kernel/lib.rs +1 -0
index f812cf12004286962985a068665443dc22c389a2..bf64752d276b0bdea06ac0de8a5e219190129377 100644
@@ -130,6 +130,7 @@
 pub mod ptr;
 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
 pub mod pwm;
+pub mod rand;
 pub mod rbtree;
 pub mod regulator;
 pub mod revocable;
rust/kernel/rand.rs new file +18 -0
new file mode 100644
index 0000000000000000000000000000000000000000..3e0aef2f53af122f510b34c4743a20452aaad9ad
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.
+//!
+//! C header: [`include/linux/random.h`](srctree/include/linux/random.h)
+
+use crate::prelude::*;
+
+/// Adds the given buffer to the entropy pool, but does not credit any entropy.
+///
+/// This is intended for use mixing in data that is likely to differ between devices or boots, but
+/// may otherwise be predictable. Examples include MAC addresses or RTC values. This slightly
+/// improves randomness in entropy-constrained environments (especially common for embedded
+/// devices).
+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]>
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 37 lines · Matthew Maurer

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

---
Changes in v3:
- Fixed doclink to be srctree-based instead of relative.
- Switched to prelude import instead of fine-grained imports.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Added more details in the docs about the API, specifically about it
not crediting entropy and when it ought to be used.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/lib.rs  |  1 +
 rust/kernel/rand.rs | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
rust/kernel/lib.rs +1 -0
index f812cf12004286962985a068665443dc22c389a2..bf64752d276b0bdea06ac0de8a5e219190129377 100644
@@ -130,6 +130,7 @@
 pub mod ptr;
 #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
 pub mod pwm;
+pub mod rand;
 pub mod rbtree;
 pub mod regulator;
 pub mod revocable;
rust/kernel/rand.rs new file +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..3e0aef2f53af122f510b34c4743a20452aaad9ad

rust/kernel/rand.rs line 3

Quoted 3 lines
rust/kernel/rand.rs new file +3 -0
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.

Let's promote this comment to a sentence at least. :)

rust/kernel/rand.rs line 11

Quoted 8 lines
rust/kernel/rand.rs new file +8 -0
+//!
+//! C header: [`include/linux/random.h`](srctree/include/linux/random.h)
+
+use crate::prelude::*;
+
+/// Adds the given buffer to the entropy pool, but does not credit any entropy.
+///
+/// This is intended for use mixing in data that is likely to differ between devices or boots, but

Not a native speaker, but "use mixing" sounds a bit off?

With this, and fwiw,

#2 lore raw
In reply to Matthew Maurer

rust/kernel/rand.rs line 11

Quoted 3 lines · Matthew Maurer
rust/kernel/rand.rs new file +3 -0
+/// Adds the given buffer to the entropy pool, but does not credit any entropy.
+///
+/// This is intended for use mixing in data that is likely to differ between devices or boots, but

Yeah, I agree with Alexandre here. I think this would probably be better written as: "This function mixes in data that is likely to differ between devices or boots". Or, "add_device_randomness mixes in data that is likely to differ between devices or boots." If you would prefer to avoid saying "this function".

Quoted 17 lines
rust/kernel/rand.rs new file +7 -1
+/// may otherwise be predictable. Examples include MAC addresses or RTC values. This slightly
+/// improves randomness in entropy-constrained environments (especially common for embedded
+/// devices).
+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]>
#3 lore raw
Quoted 10 lines · Brendan Shephard

rust/kernel/rand.rs line 11

Quoted 3 lines · Matthew Maurer
rust/kernel/rand.rs new file +3 -0
+/// Adds the given buffer to the entropy pool, but does not credit any entropy.
+///
+/// This is intended for use mixing in data that is likely to differ between devices or boots, but

Yeah, I agree with Alexandre here. I think this would probably be better written as: "This function mixes in data that is likely to differ between devices or boots". Or, "add_device_randomness mixes in data that is likely to differ between devices or boots." If you would prefer to avoid saying "this function".

I believe the grammar to be correct here - if it helps, the "use" in this case is the "yoos" variant of use, not the "yooz" variant.

That said, if it's confusing, it's confusing. I'll reword it as suggested.

Quoted 17 lines
Quoted 17 lines
rust/kernel/rand.rs new file +7 -1
+/// may otherwise be predictable. Examples include MAC addresses or RTC values. This slightly
+/// improves randomness in entropy-constrained environments (especially common for embedded
+/// devices).
+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]>

#5 lore raw
In reply to Matthew Maurer

rust/kernel/rand.rs line 5

Quoted 11 lines · Matthew Maurer
rust/kernel/rand.rs new file +5 -0
new file mode 100644
index 0000000000000000000000000000000000000000..3e0aef2f53af122f510b34c4743a20452aaad9ad
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.
+//!
+//! C header: [`include/linux/random.h`](srctree/include/linux/random.h)

So why rand and not random? If there is good reason to not use random.rs then can you add that reason to commit message.

Argillander

#6 lore raw
Quoted 16 lines · Kari Argillander

rust/kernel/rand.rs line 5

Quoted 11 lines · Matthew Maurer
rust/kernel/rand.rs new file +5 -0
new file mode 100644
index 0000000000000000000000000000000000000000..3e0aef2f53af122f510b34c4743a20452aaad9ad
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Randomness.
+//!
+//! C header: [`include/linux/random.h`](srctree/include/linux/random.h)

So why rand and not random? If there is good reason to not use random.rs then can you add that reason to commit message.

I have no reason for rand over random and no strong preference. I'll switch it to random in the next rev unless someone else chimes in.

Quoted 2 lines

Argillander