[PATCH v2] rust: dma: return zero for Coherent reads past EOF
From: Younes Akhouayri via B4 Relay
Date: Thu Jul 30 2026 - 12:56:31 EST
From: Younes Akhouayri <git@xxxxxxxxx>
Coherent<T>::write_to_slice() calculates a zero-byte copy when the file
offset is beyond the allocation, but still calls
UserSliceWriter::write_dma(). The latter rejects offsets beyond the
allocation even when the copy length is zero, so a debugfs read past EOF
returns -ERANGE.
Return before calling write_dma() when the offset is at or beyond the
allocation, matching simple_read_from_buffer() EOF semantics.
Fixes: 016818513936 ("rust: dma: implement BinaryWriter for Coherent<[u8]>")
Cc: stable@xxxxxxxxxxxxxxx
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/291566-Library/topic/.E2.9C.94.20Possible.20past-EOF.20bug.20in.20Coherent.3CT.3E.3A.3Awrite_to_slice/near/611677095
Signed-off-by: Younes Akhouayri <git@xxxxxxxxx>
---
Changes in v2:
- Use ordinary subtraction after the explicit EOF check, which makes
underflow impossible.
- Link to v1: https://patch.msgid.link/20260720-fix-dma-coherent-eof-v1-1-d92685db8da6@xxxxxxxxx
---
rust/kernel/dma.rs | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 200def84fb69..556424b88fb9 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -1005,7 +1005,11 @@ fn write_to_slice(
return Ok(0);
};
- let count = self.size().saturating_sub(offset_val).min(writer.len());
+ if offset_val >= self.size() {
+ return Ok(0);
+ }
+
+ let count = (self.size() - offset_val).min(writer.len());
writer.write_dma(self, offset_val, count)?;
---
base-commit: 667d0fb32149f023b8b34a1f6f3d384556eafb5a
change-id: 20260720-fix-dma-coherent-eof-d0d08c91e013
Best regards,
--
Younes Akhouayri <git@xxxxxxxxx>