Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Boris Burkov <boris@bur.io>
To: Yichong Chen <chenyichong@uniontech.com>
Cc: Chris Mason <clm@fb.com>, David Sterba <dsterba@suse.com>,
	Matthew Wilcox <willy@infradead.org>,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] btrfs: add verity Merkle folio to page cache after reading it
Date: Thu, 16 Jul 2026 09:23:49 -0700	[thread overview]
Message-ID: <20260716162349.GA3278562@zen.localdomain> (raw)
In-Reply-To: <20260707055517.570586-1-chenyichong@uniontech.com>

On Tue, Jul 07, 2026 at 01:55:17PM +0800, Yichong Chen wrote:
> btrfs_read_merkle_tree_page() allocates a folio and adds it to the page
> cache before reading the Merkle tree data from btree items.

So does ext4 via (eventually) do_read_cache_folio().

> 
> If read_key_bytes() fails, the folio is still locked and present in the
> page cache.  A later read can find the locked, not-uptodate folio instead
> of retrying the read.

I think the better fix is to fall through to retrying when we see a
not-uptodate folio in the mapping after __filemap_get_folio(), which I
believe is how the ext4 code will behave in that case.

> 
> Avoid installing the folio in the page cache until after the Merkle tree
> read has succeeded.  This keeps the failure path simple: the newly
> allocated folio is not visible to the page cache yet and only needs to be
> released.

I don't think this order is correct. filemap_add_folio() locks the folio
so if you do it in this order, you no longer lock the folio while
reading which is a non-trivial change. I don't immediately see what is
wrong with that off the top of my head for read only verity past eof
pages but it feels sloppy at the very least.

Thanks,
Boris

> 
> Fixes: 06ed09351b67 ("btrfs: convert btrfs_read_merkle_tree_page() to use a folio")
> Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
> ---
> v2:
> - Avoid calling filemap_remove_folio(), which is not exported.
> - Add the folio to the page cache only after read_key_bytes() succeeds.
> 
>  fs/btrfs/verity.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/fs/btrfs/verity.c b/fs/btrfs/verity.c
> index 983365a73541..d705ce3b386a 100644
> --- a/fs/btrfs/verity.c
> +++ b/fs/btrfs/verity.c
> @@ -735,15 +735,6 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
>  	if (!folio)
>  		return ERR_PTR(-ENOMEM);
>  
> -	ret = filemap_add_folio(inode->i_mapping, folio, index, GFP_NOFS);
> -	if (ret) {
> -		folio_put(folio);
> -		/* Did someone else insert a folio here? */
> -		if (ret == -EEXIST)
> -			goto again;
> -		return ERR_PTR(ret);
> -	}
> -
>  	/*
>  	 * Merkle item keys are indexed from byte 0 in the merkle tree.
>  	 * They have the form:
> @@ -759,6 +750,15 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
>  	if (ret < PAGE_SIZE)
>  		folio_zero_segment(folio, ret, PAGE_SIZE);
>  
> +	ret = filemap_add_folio(inode->i_mapping, folio, index, GFP_NOFS);
> +	if (ret) {
> +		folio_put(folio);
> +		/* Did someone else insert a folio here? */
> +		if (ret == -EEXIST)
> +			goto again;
> +		return ERR_PTR(ret);
> +	}
> +
>  	folio_mark_uptodate(folio);
>  	folio_unlock(folio);
>  
> -- 
> 2.51.0

  reply	other threads:[~2026-07-16 16:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06  5:39 [PATCH] btrfs: remove verity Merkle folio from page cache on error Yichong Chen
2026-07-07  5:35 ` Yichong Chen
2026-07-07  5:55 ` [PATCH v2] btrfs: add verity Merkle folio to page cache after reading it Yichong Chen
2026-07-16 16:23   ` Boris Burkov [this message]
2026-07-20  3:12     ` Yichong Chen
2026-08-15  0:46 ` [PATCH] btrfs: remove verity Merkle folio from page cache on error kernel test robot
2026-08-17  2:37   ` Yichong Chen
2026-08-17  5:23     ` Philip Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260716162349.GA3278562@zen.localdomain \
    --to=boris@bur.io \
    --cc=chenyichong@uniontech.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox