rust: dmi: add basic DMI matching abstraction
PATCH 1/1 rust: dmi: add basic DMI matching abstraction
Exposes linux/dmi.h to bindgen, adds MAINTAINERS entry and adds an MVP of kernel::dmi module.
---
This patch adds an initial Rust abstraction for DMI, exposing dmi::Field and get_system_info().
It is intended as a minimal prerequisite for the clevo-laptop Rust platform driver (submitted separately), which needs to check dmi::Field::BoardName.
Review note for RFL reviewers:dmi::get_system_info is currently marked safe under the assumption that Rust code is always called after DMI is initialised. Would be keen to have this validated. Alternatively I think a minimal patch to C could actually make this safe earlier if required.
--- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/kernel/dmi.rs | 82 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 4 files changed, 85 insertions(+)
MAINTAINERS +1 -0
@@ -7977,6 +7977,7 @@ F: Documentation/ABI/testing/sysfs-firmware-dmi-tables F: drivers/firmware/dmi-id.c F: drivers/firmware/dmi_scan.c F: include/linux/dmi.h +F: rust/kernel/dmi.rs DOCUMENTATION M: Jonathan Corbet <[email protected]>
rust/bindings/bindings_helper.h +1 -0
@@ -56,6 +56,7 @@#include <linux/dma-fence.h> #include <linux/dma-mapping.h> #include <linux/dma-resv.h> +#include <linux/dmi.h> #include <linux/errname.h> #include <linux/ethtool.h> #include <linux/fdtable.h>
rust/kernel/dmi.rs new file +82 -0
82 lines not on this page · raw message
rust/kernel/lib.rs +1 -0
@@ -69,6 +69,7 @@pub mod dma;#[cfg( CONFIG_DMA_SHARED_BUFFER )] pub mod dma_buf; +pub mod dmi;pub mod driver;#[cfg( CONFIG_DRM = "y" )] pub mod drm;
---
base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
change-id: 20261121-rust-dmi-c49d2041d7e2
Best regards,
Signature · 1 line
Nick Dowsett <[email protected]>
Quoted 5 lines · On Sun, Nov 22, 2026 at 5:28 AM Nick Dowsett via B4 Relay…
dmi::get_system_infois currently marked safe under the assumption that Rust code is always called after DMI is initialised. Would be keen to have this validated. Alternatively I think a minimal patch to C could actually make this safe earlier if required.
Please avoid soundness holes unless there is really no other way out and/or it would be very, very onerous to do. The bar is really high here, i.e. if this is just an unsafe function, then please simply mark it as such, which in turns allows us to document why and to force others to ensure it is correct when calling it etc.
Of course, if there is a way to make it safe (whether with a change on the C side or with API design on the Rust side etc.), then that is even better.
Since I noticed this while going through the list, let me throw some nits from a quick pass at the code if you don't mind...
Quoted 1 line
+/// # Invariant
# Invariants
Quoted 1 line
+#[expect(missing_docs, reason = "C version of these consts are undocumented")]
(It may be a nice time to take the chance to document these in C, but out-of-scope for the patch, of course.)
Quoted 1 line
+/// # use kernel::dmi::{self, Field};
Vertical formatting.
rust/kernel/dmi.rs line 60
Quoted 2 lines
rust/kernel/dmi.rs new file +2
-0+pub fn get_system_info (field:Field ) ->Option <&'staticCStr > { +// NOTE: This can be upgraded to cfg-select post Rust 1.95.
cfg_select!
Though I am not convinced we want this sort of comment -- it is true that we have similar comments for explicit workarounds we want to remove later on, but in a case like this it is the "standard" way of writing the code at least at the moment, rather than an actual workaround. And we can always grep/LLM for these improvements when we bump the minimum version.
rust/kernel/dmi.rs line 71
Quoted 3 lines
rust/kernel/dmi.rs new file +3
-0+if ptr.is_null () { +return None ; + }
Newline after this?
rust/kernel/dmi.rs line 73
Quoted 2 lines
rust/kernel/dmi.rs new file +2
-0+// SAFETY: If non-null, `dmi_get_system_info` returns a C string that +// lives for the life of the kernel.
I would word sentences like this stating what is known to be true and why (since that adds information), rather than conditionally (since that "repeats" the contract of the callee), e.g.
ptr has just been checked to be non-null, therefore ...
Thanks!
Cheers,
Miguel
Hi Miguel,
Thanks very much for taking a look!
Quoted 14 lines · Miguel Ojeda
Quoted 5 lines · On Sun, Nov 22, 2026 at 5:28 AM Nick Dowsett via B4 Relay…
dmi::get_system_infois currently marked safe under the assumption that Rust code is always called after DMI is initialised. Would be keen to have this validated. Alternatively I think a minimal patch to C could actually make this safe earlier if required.Please avoid soundness holes unless there is really no other way out and/or it would be very, very onerous to do. The bar is really high here, i.e. if this is just an
unsafefunction, then please simply mark it as such, which in turns allows us to document why and to force others to ensure it is correct when calling it etc.
Quoted 3 lines
Of course, if there is a way to make it safe (whether with a change on the C side or with API design on the Rust side etc.), then that is even better.
Sounds good from my side. I think initially it's OK to mark this unsafe
- the precondition will be easy to satisfy for the callers. Seperately I can then investigate a C patch (I think using atomics for dmi_available).
Quoted 3 lines
Quoted 1 line
+/// # use kernel::dmi::{self, Field};
Vertical formatting.
Just checking here - is this still applicable for doctests? I copied some of the example from another subsystem. There could be some conciseness benefits from not doing this here?
Quoted 16 lines
rust/kernel/dmi.rs line 60
Quoted 2 lines
rust/kernel/dmi.rs new file +2
-0+pub fn get_system_info (field:Field ) ->Option <&'staticCStr > { +// NOTE: This can be upgraded to cfg-select post Rust 1.95.
cfg_select!Though I am not convinced we want this sort of comment -- it is true that we have similar comments for explicit workarounds we want to remove later on, but in a case like this it is the "standard" way of writing the code at least at the moment, rather than an actual workaround. And we can always grep/LLM for these improvements when we bump the minimum version.
rust/kernel/dmi.rs line 71
Quoted 3 lines
rust/kernel/dmi.rs new file +3
-0+if ptr.is_null () { +return None ; + }
Quoted 8 lines
Newline after this?
rust/kernel/dmi.rs line 73
Quoted 2 lines
rust/kernel/dmi.rs new file +2
-0+// SAFETY: If non-null, `dmi_get_system_info` returns a C string that +// lives for the life of the kernel. I would word sentences like this stating what is known to be true and why (since that adds information), rather than conditionally (since that "repeats" the contract of the callee), e.g.
All ok with me, I shall address in v2 within the next couple of days.
Cheers,
Nick
Quoted 4 lines · Nick Dowsett
Just checking here - is this still applicable for doctests? I copied some of the example from another subsystem. There could be some conciseness benefits from not doing this here?
Yeah, it keeps the rule simple (everything does the same thing) and also helps others get accustomed to the style when reading examples. In any case, for the example above it is hidden, so it doesn't matter much for the rendered form.
(And, ideally, one day, rustfmt will be able to apply our formatting to the examples, so it would create a bigger diff if we don't follow the vertical formatting anyway.)
Thanks!
Cheers,
Miguel
Quoted 6 lines · Miguel Ojeda
Quoted 4 lines · Nick Dowsett
Just checking here - is this still applicable for doctests? I copied some of the example from another subsystem. There could be some conciseness benefits from not doing this here?
Quoted 8 lines
Yeah, it keeps the rule simple (everything does the same thing) and also helps others get accustomed to the style when reading examples. In any case, for the example above it is hidden, so it doesn't matter much for the rendered form.
(And, ideally, one day,
rustfmtwill be able to apply our formatting to the examples, so it would create a bigger diff if we don't follow the vertical formatting anyway.)
Sounds reasonable to me, no problems. This amendment will be in v2.
Nick