Skip to contentSkip to conversation

rust: dma: return zero for Coherent reads past EOF

PATCH Applied Younes Akhouayri 4 participants 5 messages Jul 30 – Aug 3

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rust: dma: return zero for Coherent reads past EOF

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.

---
Changes in v2:
- Use ordinary subtraction after the explicit EOF check, which makes
underflow impossible.
- Link to v1: https://patch.msgid.link/[email protected]

---
 rust/kernel/dma.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/kernel/dma.rs +5 -1
index 200def84fb69..556424b88fb9 100644
@@ -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,

Signature · 1 line
Younes Akhouayri <[email protected]>
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · On Fri Jul 31, 2026 at 1:34 AM JST, Younes Akhouayri via B4 Relay wrote:

From: Younes Akhouayri <[email protected]>

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.

Quoted 7 lines

Return before calling write_dma() when the offset is at or beyond the allocation, matching simple_read_from_buffer() EOF semantics.

reviewer
#2 lore raw
In reply to Younes Akhouayri
Quoted 29 lines · On Thu, 30 Jul 2026 18:34:38 +0200 Younes Akhouayri via B4 Relay…

From: Younes Akhouayri <[email protected]>

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.

---
Changes in v2:
- Use ordinary subtraction after the explicit EOF check, which makes
underflow impossible.
- Link to v1: https://patch.msgid.link/[email protected]

---
 rust/kernel/dma.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/kernel/dma.rs +0 -0
index 200def84fb69..556424b88fb9 100644

rust/kernel/dma.rs fn write_to_slice( · line 1008

Quoted 4 lines
rust/kernel/dma.rs +0 -1
@@ -1005,7 +1005,11 @@ fn write_to_slice(
             return Ok(0);
         };
 
-        let count = self.size().saturating_sub(offset_val).min(writer.len());

Nit: I would probably just check if count is zero next to this line.

Quoted 18 lines
rust/kernel/dma.rs +5 -1
+        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,

Signature · 1 line
Younes Akhouayri <[email protected]>
reviewer Reviewed-by
#3 lore raw
Quoted 35 lines · On Sun, 02 Aug 2026 16:54:40 +0300 Onur Özkan <[email protected]> wrote:
Quoted 32 lines · On Thu, 30 Jul 2026 18:34:38 +0200 Younes Akhouayri via B4 Relay…

From: Younes Akhouayri <[email protected]>

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.

---
Changes in v2:
- Use ordinary subtraction after the explicit EOF check, which makes
underflow impossible.
- Link to v1: https://patch.msgid.link/[email protected]

---
 rust/kernel/dma.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
rust/kernel/dma.rs +0 -0
index 200def84fb69..556424b88fb9 100644
@@ -1005,7 +1005,11 @@ fn write_to_slice(
             return Ok(0);
         };
 
Quoted 3 lines

rust/kernel/dma.rs fn write_to_slice( · line 1008

Quoted 1 line
rust/kernel/dma.rs +0 -1
-        let count = self.size().saturating_sub(offset_val).min(writer.len());

Nit: I would probably just check if count is zero next to this line.

I just noticed there is already been quite a bit of discussion about this topic so you can ignore my comment since it was just a "nit" anyway. I don't think there's much difference either way, so there's probably no need to reignite the discussion.

Thanks for the fix.

Quoted 19 lines
Quoted 19 lines
rust/kernel/dma.rs +5 -1
+        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,

Signature · 1 line
Younes Akhouayri <[email protected]>
maintainer Applied
#4 lore raw
In reply to Younes Akhouayri
Quoted 1 line · Younes Akhouayri

[PATCH v2] rust: dma: return zero for Coherent reads past EOF

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] rust: dma: return zero for Coherent reads past EOF

      commit: 5391b147d9fd

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.