Linux block layer
 help / color / mirror / Atom feed
* [PATCH] rnull: do not zero past the backing page
@ 2026-12-04 10:44 ` Vitaly Wool
  2026-12-04 12:31   ` Philipp Stanner
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Vitaly Wool @ 2026-12-04 10:44 UTC (permalink / raw)
  To: Andreas Hindborg
  Cc: Boqun Feng, Jens Axboe, Miguel Ojeda, Gary Guo, Bjorn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Ozkan,
	linux-block, rust-for-linux, linux-kernel, Vitaly Wool

When a read hits a backing page that was never written, rnull fills the
segment with zeroes up to the end of the segment's current page, which
is a page of the bio, not of the backing store. If the read does not
start at the beginning of the backing page, the zeroes run on into the
next backing page, which may hold data, and the read returns zeroes for
it. For instance, with 512 byte blocks, a direct read of 4K at sector 7
into a page aligned buffer, where only sector 8 was written, returns
zeroes for sector 8. Fix this.

Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 drivers/block/rnull/rnull.rs | 26 +++++++++++++-------------
 rust/kernel/block/bio/vec.rs | 10 +++++-----
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 9f99fe46826a..88bbd97845ee 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -707,19 +707,19 @@ fn read<'a, 'b, 'c>(
         while !segment.at_end() {
             let page = access.get_read_page(sector);
 
-            match page {
-                Some(page) => {
-                    // CAST: Page offset always fits in 32 bits.
-                    let page_offset = ((sector & u64::from(block::PAGE_SECTOR_MASK))
-                        << block::SECTOR_SHIFT) as usize;
-
-                    // CAST: Casting from `usize` to `u64` never overflows.
-                    sector += segment.write(&page.page().as_slice()[page_offset..]) as u64
-                        >> block::SECTOR_SHIFT;
-                }
-                // CAST: Casting from `usize` to `u64` never overflows.
-                None => sector += bytes_to_sectors(segment.zero_page() as u64),
-            }
+            // CAST: Page offset always fits in 32 bits.
+            let page_offset =
+                ((sector & u64::from(block::PAGE_SECTOR_MASK)) << block::SECTOR_SHIFT) as usize;
+
+            // Copy at most up to the end of the backing page: the next one may hold data even if
+            // this one does not.
+            let copied = match page {
+                Some(page) => segment.write(&page.page().as_slice()[page_offset..]),
+                None => segment.zero_page(PAGE_SIZE - page_offset),
+            };
+
+            // CAST: Casting from `usize` to `u64` never overflows.
+            sector += copied as u64 >> block::SECTOR_SHIFT;
         }
 
         Ok(())
diff --git a/rust/kernel/block/bio/vec.rs b/rust/kernel/block/bio/vec.rs
index 4d84d07b2cf2..b5bd2d0a461e 100644
--- a/rust/kernel/block/bio/vec.rs
+++ b/rust/kernel/block/bio/vec.rs
@@ -174,15 +174,15 @@ pub fn write(&mut self, src: &[u8]) -> usize {
 
     /// Copy zeroes to the current page of this segment.
     ///
-    /// Copies  `PAGE_SIZE - (self.offset() % PAGE_SIZE` bytes of data to this
-    /// segment starting at `self.offset()`. This call will advance offset and reduce length of
-    /// `self`.
+    /// Copies at most `max_len` zero bytes to this segment starting at `self.offset()`, and never
+    /// past the end of the current page, i.e. at most `PAGE_SIZE - (self.offset() % PAGE_SIZE)`
+    /// bytes. This call will advance offset and reduce length of `self`.
     ///
     /// Returns the number of bytes written to this segment.
     #[inline]
-    pub fn zero_page(&mut self) -> usize {
+    pub fn zero_page(&mut self, max_len: usize) -> usize {
         let offset = self.offset() % PAGE_SIZE;
-        let length = (PAGE_SIZE - offset).min(self.len() as usize);
+        let length = (PAGE_SIZE - offset).min(self.len() as usize).min(max_len);
         let page_idx = self.offset() / PAGE_SIZE;
 
         let dst_page = self.page(page_idx);
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] rnull: do not zero past the backing page
  2026-12-04 10:44 ` [PATCH] rnull: do not zero past the backing page Vitaly Wool
@ 2026-12-04 12:31   ` Philipp Stanner
  2026-12-04 14:06     ` Andreas Hindborg
  2026-12-04 14:04   ` Andreas Hindborg
  2026-12-04 15:03   ` Miguel Ojeda
  2 siblings, 1 reply; 6+ messages in thread
From: Philipp Stanner @ 2026-12-04 12:31 UTC (permalink / raw)
  To: Vitaly Wool, Andreas Hindborg
  Cc: Boqun Feng, Jens Axboe, Miguel Ojeda, Gary Guo, Bjorn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Ozkan,
	linux-block, rust-for-linux, linux-kernel

On Fri, 2026-12-04 at 11:44 +0100, Vitaly Wool wrote:
> When a read hits a backing page that was never written, rnull fills the
> segment with zeroes up to the end of the segment's current page, which
> is a page of the bio, not of the backing store. If the read does not
> start at the beginning of the backing page, the zeroes run on into the
> next backing page, which may hold data, and the read returns zeroes for
> it. For instance, with 512 byte blocks, a direct read of 4K at sector 7
> into a page aligned buffer, where only sector 8 was written, returns
> zeroes for sector 8. Fix this.
> 
> Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
> Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>

So does it need Fixes: ?

> 

[…]

> +            // CAST: Casting from `usize` to `u64` never overflows.
> +            sector += copied as u64 >> block::SECTOR_SHIFT;

If it always works, can't it be written as u64::from(copied)?

>      /// Copy zeroes to the current page of this segment.
>      ///
> -    /// Copies  `PAGE_SIZE - (self.offset() % PAGE_SIZE` bytes of data to this
> -    /// segment starting at `self.offset()`. This call will advance offset and reduce length of
> -    /// `self`.
> +    /// Copies at most `max_len` zero bytes to this segment starting at `self.offset()`, and never
> +    /// past the end of the current page, i.e. at most `PAGE_SIZE - (self.offset() % PAGE_SIZE)`
> +    /// bytes. This call will advance offset and reduce length of `self`.
>      ///
>      /// Returns the number of bytes written to this segment.
>      #[inline]
> -    pub fn zero_page(&mut self) -> usize {
> +    pub fn zero_page(&mut self, max_len: usize) -> usize {

The function name would make me think that this zeros the entire page.
Maybe a different name might make sense?

And what about max_len == 0? Often API functions use that for "write
till the end"


Regards,
P.

>          let offset = self.offset() % PAGE_SIZE;
> -        let length = (PAGE_SIZE - offset).min(self.len() as usize);
> +        let length = (PAGE_SIZE - offset).min(self.len() as usize).min(max_len);
>          let page_idx = self.offset() / PAGE_SIZE;
>  
>          let dst_page = self.page(page_idx);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rnull: do not zero past the backing page
  2026-12-04 10:44 ` [PATCH] rnull: do not zero past the backing page Vitaly Wool
  2026-12-04 12:31   ` Philipp Stanner
@ 2026-12-04 14:04   ` Andreas Hindborg
  2026-12-04 15:09     ` Jens Axboe
  2026-12-04 15:03   ` Miguel Ojeda
  2 siblings, 1 reply; 6+ messages in thread
From: Andreas Hindborg @ 2026-12-04 14:04 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: Boqun Feng, Jens Axboe, Miguel Ojeda, Gary Guo, Bjorn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Ozkan,
	linux-block, rust-for-linux, linux-kernel, Vitaly Wool

"Vitaly Wool" <vitaly.wool@konsulko.se> writes:

> When a read hits a backing page that was never written, rnull fills the
> segment with zeroes up to the end of the segment's current page, which
> is a page of the bio, not of the backing store. If the read does not
> start at the beginning of the backing page, the zeroes run on into the
> next backing page, which may hold data, and the read returns zeroes for
> it. For instance, with 512 byte blocks, a direct read of 4K at sector 7
> into a page aligned buffer, where only sector 8 was written, returns
> zeroes for sector 8. Fix this.
>
> Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
> Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>

This patch is targeting a downstream tree.

It does not bother me that you send these to list, but it might be
confusing to others.

Best regards,
Andreas Hindborg



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rnull: do not zero past the backing page
  2026-12-04 12:31   ` Philipp Stanner
@ 2026-12-04 14:06     ` Andreas Hindborg
  0 siblings, 0 replies; 6+ messages in thread
From: Andreas Hindborg @ 2026-12-04 14:06 UTC (permalink / raw)
  To: phasta, Vitaly Wool
  Cc: Boqun Feng, Jens Axboe, Miguel Ojeda, Gary Guo, Bjorn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Ozkan,
	linux-block, rust-for-linux, linux-kernel

"Philipp Stanner" <phasta@mailbox.org> writes:

> On Fri, 2026-12-04 at 11:44 +0100, Vitaly Wool wrote:
>> When a read hits a backing page that was never written, rnull fills the
>> segment with zeroes up to the end of the segment's current page, which
>> is a page of the bio, not of the backing store. If the read does not
>> start at the beginning of the backing page, the zeroes run on into the
>> next backing page, which may hold data, and the read returns zeroes for
>> it. For instance, with 512 byte blocks, a direct read of 4K at sector 7
>> into a page aligned buffer, where only sector 8 was written, returns
>> zeroes for sector 8. Fix this.
>>
>> Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
>> Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
>
> So does it need Fixes: ?

No, the code it fixes is not merged yet.

>
>>
>
> […]
>
>> +            // CAST: Casting from `usize` to `u64` never overflows.
>> +            sector += copied as u64 >> block::SECTOR_SHIFT;
>
> If it always works, can't it be written as u64::from(copied)?
>
>>      /// Copy zeroes to the current page of this segment.
>>      ///
>> -    /// Copies  `PAGE_SIZE - (self.offset() % PAGE_SIZE` bytes of data to this
>> -    /// segment starting at `self.offset()`. This call will advance offset and reduce length of
>> -    /// `self`.
>> +    /// Copies at most `max_len` zero bytes to this segment starting at `self.offset()`, and never
>> +    /// past the end of the current page, i.e. at most `PAGE_SIZE - (self.offset() % PAGE_SIZE)`
>> +    /// bytes. This call will advance offset and reduce length of `self`.
>>      ///
>>      /// Returns the number of bytes written to this segment.
>>      #[inline]
>> -    pub fn zero_page(&mut self) -> usize {
>> +    pub fn zero_page(&mut self, max_len: usize) -> usize {
>
> The function name would make me think that this zeros the entire page.
> Maybe a different name might make sense?
>
> And what about max_len == 0? Often API functions use that for "write
> till the end"

I'm going to rename it, it is not a good name after the change.

Best regards,
Andreas Hindborg



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rnull: do not zero past the backing page
  2026-12-04 10:44 ` [PATCH] rnull: do not zero past the backing page Vitaly Wool
  2026-12-04 12:31   ` Philipp Stanner
  2026-12-04 14:04   ` Andreas Hindborg
@ 2026-12-04 15:03   ` Miguel Ojeda
  2 siblings, 0 replies; 6+ messages in thread
From: Miguel Ojeda @ 2026-12-04 15:03 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: Andreas Hindborg, Boqun Feng, Jens Axboe, Miguel Ojeda, Gary Guo,
	Bjorn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Ozkan, linux-block, rust-for-linux,
	linux-kernel

On Fri, Dec 4, 2026 at 11:45 AM Vitaly Wool <vitaly.wool@konsulko.se> wrote:
>
> Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>

This tag is not expected anymore. Instead, please explain the tools
used in the cover letter.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] rnull: do not zero past the backing page
  2026-12-04 14:04   ` Andreas Hindborg
@ 2026-12-04 15:09     ` Jens Axboe
  0 siblings, 0 replies; 6+ messages in thread
From: Jens Axboe @ 2026-12-04 15:09 UTC (permalink / raw)
  To: Andreas Hindborg
  Cc: Vitaly Wool, Boqun Feng, Miguel Ojeda, Gary Guo, Bjorn Roy Baron,
	Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot, Onur Ozkan,
	linux-block, rust-for-linux, linux-kernel

On Fri, Dec 4, 2026 at 7:04?AM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
> "Vitaly Wool" <vitaly.wool@konsulko.se> writes:
>
> > When a read hits a backing page that was never written, rnull fills the
> > segment with zeroes up to the end of the segment's current page, which
> > is a page of the bio, not of the backing store. If the read does not
> > start at the beginning of the backing page, the zeroes run on into the
> > next backing page, which may hold data, and the read returns zeroes for
> > it. For instance, with 512 byte blocks, a direct read of 4K at sector 7
> > into a page aligned buffer, where only sector 8 was written, returns
> > zeroes for sector 8. Fix this.
> >
> > Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
> > Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
>
> This patch is targeting a downstream tree.
>
> It does not bother me that you send these to list, but it might be
> confusing to others.

Honestly it is just noise, and I'd say the same for any other
out-of-tree driver/code. If it's not in the upstream kernel, it doesn't
belong on the list, so please don't send it there.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-04 15:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <sGTKQRb-QRAf07fuubXVdjrXgGYXvjpZnRp_7FPf0yGHtvNVjQR5zRxsJkKgLpwjzrtjENdsZdZHzylAZl2YBQ==@protonmail.internalid>
2026-12-04 10:44 ` [PATCH] rnull: do not zero past the backing page Vitaly Wool
2026-12-04 12:31   ` Philipp Stanner
2026-12-04 14:06     ` Andreas Hindborg
2026-12-04 14:04   ` Andreas Hindborg
2026-12-04 15:09     ` Jens Axboe
2026-12-04 15:03   ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox