Re: [PATCH] rnull: do not zero past the backing page
From: Philipp Stanner
Date: Fri Dec 04 2026 - 07:31:29 EST
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@xxxxxxxxxxx>
> Assisted-By: Claude Opus 5.5 <noreply@xxxxxxxxxxxxx>
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);