rust: honor the maximum DMA segment size
Subsystems (7)
v2 cover rust: honor the maximum DMA segment size
Booting nova-core against a real GSP with CONFIG_DMA_API_DEBUG=y warns when the ~60 MiB firmware image is mapped as a scatter-gather table:
DMA-API: nova-core 0000:00:03.0: mapping sg segment longer than device claims to support [len=62914560] [max=65536]
There are two separate issues behind it.
Patch 1 has nova-core declare the segment size it actually supports. It re-decomposes every segment into 4 KiB GSP page-table entries, so it has no upper bound on segment length.
Furthermore, I also checked what other GPUs do, and most do the same:
set the max segment size u32::MAX. So this should be the correct behavior for nova as well.
Patch 2 has SGTable::new() honor dma_get_max_seg_size() in addition to dma_max_mapping_size(). The abstraction is generic, so a driver with a real hardware segment limit would otherwise silently be handed segments it cannot express in a single descriptor.
Tested on an RTX 5080 (GB203) passed through to a QEMU guest via VFIO with CONFIG_DMA_API_DEBUG=y: the warning is gone, and GSP boot and RPC still work.
Changes in v2, all from Alexandre Courbot's review:
- Patch 1: use the Assisted-by: LLM form documented in Documentation/process/coding-assistants.rst, and place the tag before the Signed-off-by.
- Patch 1: trim the comment above dma_set_max_seg_size(), and state the SAFETY invariant in full instead of referring to the comment above it.
- Patch 2: rename max_mapping -> max_mapping_size.
- Patch 2: drop the paragraph claiming nova-core is the only user; the Rust DMA sample is one too, and neither is broken by this patch landing without patch 1.
- Patch 2: picked up Alexandre's Reviewed-by.
No functional change since v1.
v1: https://lore.kernel.org/r/[email protected]
Matteo Kloiber (2):
gpu: nova-core: declare unlimited DMA max segment size
rust: scatterlist: honor the device's maximum segment size
drivers/gpu/nova-core/gpu.rs | 7 +++++++ rust/helpers/dma.c | 5 +++++ rust/kernel/scatterlist.rs | 12 ++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-)
Range-diff against v1:
1: bd1a05a64e05 ! 1: 8a8637f02fa1 gpu: nova-core: declare unlimited DMA max segment size
@@ Commit message
CONFIG_DMA_API_DEBUG=y is required to see this warning.
+ Assisted-by: LLM
- Assisted-by: Claude:claude-opus-4-8
## drivers/gpu/nova-core/gpu.rs ##
@@ drivers/gpu/nova-core/gpu.rs: pub(crate) fn new<'a>(
// still constructing it, so no concurrent DMA allocations can exist. unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? };
-+ // Nova re-decomposes SG segments into 4 KiB page-table entries, so it
-+ // has no upper bound on segment length; declare that to the DMA layer.
++ // Nova walks SG segments to build page tables, so their length is
++ // irrelevant to the device.
+ //
-+ // SAFETY: same invariant as above -- still constructing, no concurrent
-+ // DMA mapping can exist.
++ // SAFETY: Gpu owns all DMA allocations for this device, and we are
++ // still constructing it, so no concurrent DMA allocations can exist.
+ unsafe { pdev.dma_set_max_seg_size(u32::MAX) };
+
hal.wait_gfw_boot_completion(bar)
2: 69ea96f9290b ! 2: 4f8a19794529 rust: scatterlist: honor the device's maximum segment size
@@ Commit message
potentially causing problems for future drivers that use this
abstraction.
- nova-core declares an unlimited segment size, so this does not change
- its behavior.
-
Fixes: 05aa6fb1c21d ("rust: scatterlist: Add abstraction for sg_table")
Signed-off-by: Matteo Kloiber <[email protected]>
+ Reviewed-by: Alexandre Courbot <[email protected]>
## rust/helpers/dma.c ##
@@ rust/helpers/dma.c: __rust_helper void rust_helper_dma_set_max_seg_size(struct device *dev,
@@ rust/kernel/scatterlist.rs: fn new(
//
// SAFETY: `dev.as_raw()` is a valid pointer to a `struct device`.
- let max_segment = match unsafe { bindings::dma_max_mapping_size(dev.as_raw()) } {
-+ let max_mapping = match unsafe { bindings::dma_max_mapping_size(dev.as_raw()) } {
++ let max_mapping_size = match unsafe { bindings::dma_max_mapping_size(dev.as_raw()) } {
0 => u32::MAX,
- max_segment => u32::try_from(max_segment).unwrap_or(u32::MAX),
-+ max_mapping => u32::try_from(max_mapping).unwrap_or(u32::MAX),
++ max_mapping_size => u32::try_from(max_mapping_size).unwrap_or(u32::MAX),
};
+ // SAFETY: `dev.as_raw()` is a valid pointer to a `struct device`.
+ let max_seg_size = unsafe { bindings::dma_get_max_seg_size(dev.as_raw()) };
+
-+ let max_segment = max_mapping.min(max_seg_size);
++ let max_segment = max_mapping_size.min(max_seg_size);
+
Ok(try_pin_init!(&this in Self {
// SAFETY:base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.51.2
PATCH v2 1/2 gpu: nova-core: declare unlimited DMA max segment size
The PCI default max_seg_size is 64 KiB. nova-core never raises it, so mapping the GSP firmware image (~60 MiB) as a scatter-gather table triggers a DMA-API debug warning when contiguous pages coalesce into segments that exceed the default:
DMA-API: nova-core 0000:00:03.0: mapping sg segment longer than device claims to support [len=62914560] [max=65536]
CONFIG_DMA_API_DEBUG=y is required to see this warning.
--- drivers/gpu/nova-core/gpu.rs | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/gpu/nova-core/gpu.rs +7 -0
@@ -343,6 +343,13 @@ pub(crate) fn new<'a>(// still constructing it, so no concurrent DMA allocations can exist. unsafe { pdev.dma_set_mask_and_coherent (dma_mask)? }; +// Nova walks SG segments to build page tables, so their length is +// irrelevant to the device. +// +// SAFETY: `Gpu` owns all DMA allocations for this device, and we are +// still constructing it, so no concurrent DMA allocations can exist. +unsafe { pdev.dma_set_max_seg_size (u32::MAX ) }; + hal.wait_gfw_boot_completion (bar) .inspect_err (|_|dev_err! (dev,"GFW boot did not complete\n" ))?; },
Signature · 1 line
2.51.2
Re: [PATCH v2 1/2] gpu: nova-core: declare unlimited DMA max segment size
Quoted 9 lines · Matteo Kloiber
The PCI default
max_seg_sizeis 64 KiB. nova-core never raises it, so mapping the GSP firmware image (~60 MiB) as a scatter-gather table triggers a DMA-API debug warning when contiguous pages coalesce into segments that exceed the default:DMA-API: nova-core 0000:00:03.0: mapping sg segment longer than device claims to support [len=62914560] [max=65536]
[...]
Applied to drm-rust-next, thanks and congrats on your first kernel patch! :)
[1/2] gpu: nova-core: declare unlimited DMA max segment size
commit: 6bd5eaeb9827ca7ba953b68fb27948fdb3e40630
Patch 2 has triggered more discussion as hoped, so let's see where this goes first.
PATCH v2 2/2 rust: scatterlist: honor the device's maximum segment size
In reply to Matteo KloiberSGTable::new() caps segment length at dma_max_mapping_size() only, which limits the DMA mapping path (e.g. swiotlb), not the device itself. The per-device limit from dma_set_max_seg_size() is ignored, so contiguous page segments can be longer than the declared max segment size, potentially causing problems for future drivers that use this abstraction.
--- rust/helpers/dma.c | 5 +++++ rust/kernel/scatterlist.rs | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-)
rust/helpers/dma.c +5 -0
@@ -49,3 +49,8 @@ __rust_helper void rust_helper_dma_set_max_seg_size(struct device *dev, {dma_set_max_seg_size (dev, size); } + +__rust_helper unsigned int rust_helper_dma_get_max_seg_size(struct device *dev) +{ +return dma_get_max_seg_size (dev); +}
rust/kernel/scatterlist.rs +10 -2
@@ -350,15 +350,23 @@ fn new( page_vec.push (page.as_ptr (), flags)?; } +// Cap segments at both the DMA mapping-path limit and the device's declared +// max segment size. +// // `dma_max_mapping_size` returns `size_t`, but `sg_alloc_table_from_pages_segment()` takes // an `unsigned int`. // // SAFETY: `dev.as_raw()` is a valid pointer to a `struct device`. -let max_segment =match unsafe { bindings::dma_max_mapping_size (dev.as_raw ()) } { +let max_mapping_size =match unsafe { bindings::dma_max_mapping_size (dev.as_raw ()) } {0 => u32::MAX , - max_segment => u32::try_from (max_segment).unwrap_or (u32::MAX ), + max_mapping_size => u32::try_from (max_mapping_size).unwrap_or (u32::MAX ), }; +// SAFETY: `dev.as_raw()` is a valid pointer to a `struct device`. +let max_seg_size =unsafe { bindings::dma_get_max_seg_size (dev.as_raw ()) }; + +let max_segment = max_mapping_size.min (max_seg_size); + Ok(try_pin_init!(&this in Self { // SAFETY: // - `page_vec` is a `KVec` of valid `struct page *` obtained from `pages`.
Signature · 1 line
2.51.2
Re: (subset) [PATCH v2 0/2] rust: honor the maximum DMA segment size
In reply to Matteo KloiberQuoted 1 line · Matteo Kloiber
[PATCH v2 0/2] rust: honor the maximum DMA segment size
Applied, thanks!
Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
[2/2] rust: scatterlist: honor the device's maximum segment size
commit: c15c5befd6d6
The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).
The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.