rust: scatterlist: honor the device's maximum segment size

SGTable::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.

Fixes: 05aa6fb1c2 ("rust: scatterlist: Add abstraction for sg_table")
Signed-off-by: Matteo Kloiber <kernel@matt3o12.de>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20260913210828.125655-3-kernel@matt3o12.de
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Matteo Kloiber 2026-09-13 23:08:28 +02:00 committed by Danilo Krummrich
parent fcbfaffee5
commit c15c5befd6
2 changed files with 15 additions and 2 deletions

View File

@ -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);
}

View File

@ -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`.