* [PATCH] rust: dmi: add basic DMI matching abstraction
@ 2026-11-22 4:28 Nick Dowsett via B4 Relay
2026-11-22 10:37 ` Miguel Ojeda
0 siblings, 1 reply; 4+ messages in thread
From: Nick Dowsett via B4 Relay @ 2026-11-22 4:28 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Jean Delvare
Cc: linux-kernel, rust-for-linux, Nick Dowsett
From: Nick Dowsett <nick42d@gmail.com>
Exposes `linux/dmi.h` to bindgen, adds MAINTAINERS entry and adds an MVP
of `kernel::dmi` module.
Signed-off-by: Nick Dowsett <nick42d@gmail.com>
---
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(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b8..92a227e2345e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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 <corbet@lwn.net>
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 3791af0aadc7..ed4ce2ca351d 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -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>
diff --git a/rust/kernel/dmi.rs b/rust/kernel/dmi.rs
new file mode 100644
index 000000000000..410a1a84ce08
--- /dev/null
+++ b/rust/kernel/dmi.rs
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Abstractions for DMI (Desktop Management Interface).
+//!
+//! C header: [`include/linux/dmi.h`](srctree/include/linux/device-id/dmi.h)
+
+use crate::{
+ bindings,
+ prelude::*, //
+};
+
+/// Corresponds to each DMI identifier type from [`enum dmi_field`].
+///
+/// # Invariant
+///
+/// Each variant must correspond to a valid [`enum dmi_field`] variant.
+///
+/// [`enum dmi_field`]: srctree/include/linux/device-id/dmi.h
+#[repr(u32)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[expect(missing_docs, reason = "C version of these consts are undocumented")]
+pub enum Field {
+ None = bindings::dmi_field_DMI_NONE,
+ BiosVendor = bindings::dmi_field_DMI_BIOS_VENDOR,
+ BiosVersion = bindings::dmi_field_DMI_BIOS_VERSION,
+ BiosDate = bindings::dmi_field_DMI_BIOS_DATE,
+ BiosRelease = bindings::dmi_field_DMI_BIOS_RELEASE,
+ EcFirmwareRelease = bindings::dmi_field_DMI_EC_FIRMWARE_RELEASE,
+ SysVendor = bindings::dmi_field_DMI_SYS_VENDOR,
+ ProductName = bindings::dmi_field_DMI_PRODUCT_NAME,
+ ProductVersion = bindings::dmi_field_DMI_PRODUCT_VERSION,
+ ProductSerial = bindings::dmi_field_DMI_PRODUCT_SERIAL,
+ ProductUuid = bindings::dmi_field_DMI_PRODUCT_UUID,
+ ProductSku = bindings::dmi_field_DMI_PRODUCT_SKU,
+ ProductFamily = bindings::dmi_field_DMI_PRODUCT_FAMILY,
+ BoardVendor = bindings::dmi_field_DMI_BOARD_VENDOR,
+ BoardName = bindings::dmi_field_DMI_BOARD_NAME,
+ BoardVersion = bindings::dmi_field_DMI_BOARD_VERSION,
+ BoardSerial = bindings::dmi_field_DMI_BOARD_SERIAL,
+ BoardAssetTag = bindings::dmi_field_DMI_BOARD_ASSET_TAG,
+ ChassisVendor = bindings::dmi_field_DMI_CHASSIS_VENDOR,
+ ChassisType = bindings::dmi_field_DMI_CHASSIS_TYPE,
+ ChassisVersion = bindings::dmi_field_DMI_CHASSIS_VERSION,
+ ChassisSerial = bindings::dmi_field_DMI_CHASSIS_SERIAL,
+ ChassisAssetTag = bindings::dmi_field_DMI_CHASSIS_ASSET_TAG,
+ OemString = bindings::dmi_field_DMI_OEM_STRING,
+}
+
+/// Returns the DMI string for `field`, or [`None`] if unavailable or `CONFIG_DMI` is disabled.
+///
+/// # Examples
+///
+/// ```
+/// # use kernel::dmi::{self, Field};
+/// if let Some(vendor) = dmi::get_system_info(Field::SysVendor) {
+/// pr_debug!("Vendor string is {vendor}");
+/// }
+/// ```
+pub fn get_system_info(field: Field) -> Option<&'static CStr> {
+ // NOTE: This can be upgraded to cfg-select post Rust 1.95.
+ #[cfg(CONFIG_DMI)]
+ {
+ // SAFETY:
+ // - `field` is a valid variant of `enum dmi_field`.
+ // - `dmi_get_system_info` is safe to call after DMI has been set up during init.
+ // This occurs during `setup_arch` on x86/loongarch/mips and `core_initcall`
+ // on arm64/riscv - before any Rust code is run.
+ let ptr = unsafe { bindings::dmi_get_system_info(field as c_int) };
+ if ptr.is_null() {
+ return None;
+ }
+ // SAFETY: If non-null, `dmi_get_system_info` returns a C string that
+ // lives for the life of the kernel.
+ Some(unsafe { CStr::from_char_ptr(ptr) })
+ }
+ #[cfg(not(CONFIG_DMI))]
+ {
+ // Squash `unused_variables` error.
+ let _ = field;
+ None
+ }
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 1ab41129d557..22f10c5d97e8 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -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,
--
Nick Dowsett <nick42d@gmail.com>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: dmi: add basic DMI matching abstraction
2026-11-22 4:28 [PATCH] rust: dmi: add basic DMI matching abstraction Nick Dowsett via B4 Relay
@ 2026-11-22 10:37 ` Miguel Ojeda
2026-11-23 0:31 ` Nick Dowsett
0 siblings, 1 reply; 4+ messages in thread
From: Miguel Ojeda @ 2026-11-22 10:37 UTC (permalink / raw)
To: nick42d
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Jean Delvare, linux-kernel,
rust-for-linux
On Sun, Nov 22, 2026 at 5:28 AM Nick Dowsett via B4 Relay
<devnull+nick42d.gmail.com@kernel.org> wrote:
>
> `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.
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...
> +/// # Invariant
# Invariants
> +#[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.)
> +/// # use kernel::dmi::{self, Field};
Vertical formatting.
> +pub fn get_system_info(field: Field) -> Option<&'static CStr> {
> + // 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.
> + if ptr.is_null() {
> + return None;
> + }
Newline after this?
> + // 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: dmi: add basic DMI matching abstraction
2026-11-22 10:37 ` Miguel Ojeda
@ 2026-11-23 0:31 ` Nick Dowsett
2026-11-23 1:10 ` Miguel Ojeda
0 siblings, 1 reply; 4+ messages in thread
From: Nick Dowsett @ 2026-11-23 0:31 UTC (permalink / raw)
To: Miguel Ojeda, nick42d
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Jean Delvare, linux-kernel,
rust-for-linux
Hi Miguel,
Thanks very much for taking a look!
On Sun Nov 22, 2026 at 6:37 PM AWST, Miguel Ojeda wrote:
> On Sun, Nov 22, 2026 at 5:28 AM Nick Dowsett via B4 Relay
> <devnull+nick42d.gmail.com@kernel.org> wrote:
>>
>> `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.
>
> 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.
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).
>> +/// # 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?
>> +pub fn get_system_info(field: Field) -> Option<&'static CStr> {
>> + // 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.
>
>> + if ptr.is_null() {
>> + return None;
>> + }
>
> Newline after this?
>
>> + // 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: dmi: add basic DMI matching abstraction
2026-11-23 0:31 ` Nick Dowsett
@ 2026-11-23 1:10 ` Miguel Ojeda
0 siblings, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2026-11-23 1:10 UTC (permalink / raw)
To: Nick Dowsett
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, Jean Delvare, linux-kernel,
rust-for-linux
On Mon, Nov 23, 2026 at 1:31 AM Nick Dowsett <nick42d@gmail.com> wrote:
>
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 1:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 4:28 [PATCH] rust: dmi: add basic DMI matching abstraction Nick Dowsett via B4 Relay
2026-11-22 10:37 ` Miguel Ojeda
2026-11-23 0:31 ` Nick Dowsett
2026-11-23 1:10 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox