Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2] btrfs: fix the delalloc range locking if sector size < page size
Date: Tue, 8 Oct 2024 18:58:33 +0200	[thread overview]
Message-ID: <20241008165833.GD1609@twin.jikos.cz> (raw)
In-Reply-To: <305e53f3931e164c8ab3b09c059ea68bd792a4b2.1728256845.git.wqu@suse.com>

On Mon, Oct 07, 2024 at 09:51:40AM +1030, Qu Wenruo wrote:
> Inside lock_delalloc_folios(), there are several problems related to
> sector size < page size handling:
> 
> - Set the writer locks without checking if the folio is still valid
>   We call btrfs_folio_start_writer_lock() just like it's folio_lock().
>   But since the folio may not even be the folio of the current mapping,
>   we can easily screw up the folio->private.
> 
> - The range is not clampped inside the page
>   This means we can over write other bitmaps if the start/len is not
>   properly handled, and trigger the btrfs_subpage_assert().
> 
> - @processed_end is always rounded up to page end
>   If the delalloc range is not page aligned, and we need to retry
>   (returning -EAGAIN), then we will unlock to the page end.
> 
>   Thankfully this is not a huge problem, as now
>   btrfs_folio_end_writer_lock() can handle range larger than the locked
>   range, and only unlock what is already locked.
> 
> Fix all these problems by:
> 
> - Lock and check the folio first, then call
>   btrfs_folio_set_writer_lock()
>   So that if we got a folio not belonging to the inode, we won't
>   touch folio->private.
> 
> - Properly truncate the range inside the page
> 
> - Update @processed_end to the locked range end
> 
> - Remove btrfs_folio_start_writer_lock()
>   Since all callsites only utilize btrfs_folio_set_writer_lock() now,
>   remove the unnecessary btrfs_folio_start_writer_lock().
> 
> Fixes: 1e1de38792e0 ("btrfs: make process_one_page() to handle subpage locking")

This commit is in 5.14, so we'd need backport to 5.15+. The v1 would be
more suitable as it does not remove the unused
btrfs_folio_start_writer_lock(). As it's exported function there should
be no warning.

> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
> Changelog:
> v2:
> - Remove the unused btrfs_folio_start_writer_lock() function
> ---
>  fs/btrfs/extent_io.c | 15 +++++++-------
>  fs/btrfs/subpage.c   | 47 --------------------------------------------
>  fs/btrfs/subpage.h   |  2 --
>  3 files changed, 7 insertions(+), 57 deletions(-)
> 
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index e0b43640849e..0448cee2b983 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c
> @@ -262,22 +262,21 @@ static noinline int lock_delalloc_folios(struct inode *inode,
>  
>  		for (i = 0; i < found_folios; i++) {
>  			struct folio *folio = fbatch.folios[i];
> -			u32 len = end + 1 - start;
> +			u64 range_start = max_t(u64, folio_pos(folio), start);
> +			u32 range_len = min_t(u64, folio_pos(folio) + folio_size(folio),
> +					      end + 1) - range_start;
>  
>  			if (folio == locked_folio)
>  				continue;
>  
> -			if (btrfs_folio_start_writer_lock(fs_info, folio, start,
> -							  len))
> -				goto out;
> -
> +			folio_lock(folio);
>  			if (!folio_test_dirty(folio) || folio->mapping != mapping) {

Should the range_start and range_end be set after folio_lock? One of the
prolblems you're fixing is the folio and mapping mismatch, if I read it
correctly. 

  reply	other threads:[~2024-10-08 16:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-06 23:21 [PATCH v2] btrfs: fix the delalloc range locking if sector size < page size Qu Wenruo
2024-10-08 16:58 ` David Sterba [this message]
2024-10-08 20:58   ` Qu Wenruo

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=20241008165833.GD1609@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=wqu@suse.com \
    /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