Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v8] btrfs: prefer to allocate larger folio for metadata
Date: Fri, 26 Jul 2024 10:57:53 -0400	[thread overview]
Message-ID: <20240726145753.GC3432726@perftesting> (raw)
In-Reply-To: <c9bd53a8c7efb8d0e16048036d0596e17e519dd6.1721902058.git.wqu@suse.com>

On Thu, Jul 25, 2024 at 07:41:16PM +0930, Qu Wenruo wrote:
> Since btrfs metadata is always in fixed size (nodesize, determined at
> mkfs time, default to 16K), and btrfs has the full control of the folios
> (read is triggered internally, no read/readahead call backs), it's the
> best location to experimental larger folios inside btrfs.
> 
> To enable larger folios, the btrfs has to meet the following conditions:
> 
> - The extent buffer start is aligned to nodesize
>   This should be the common case for any btrfs in the last 5 years.
> 
> - The nodesize is larger than page size
> 
> - MM layer can fulfill our larger folio allocation
>   The larger folio will cover exactly the metadata size (nodesize).
> 
> If any of the condition is not met, we just fall back to page sized
> folio and go as usual.
> This means, we can have mixed orders for btrfs metadata.
> 
> Thus there are several new corner cases with the mixed orders:
> 
> 1) New filemap_add_folio() -EEXIST failure cases
>    For mixed order cases, filemap_add_folio() can return -EEXIST
>    meanwhile filemap_lock_folio() returns -ENOENT.
>    We can only retry several times, before falling back to 0 order
>    folios.
> 
> 2) Existing folio size may be different than the one we allocated
>    This is after the existing eb checks.
> 
> 2.1) The existing folio is larger than the allocated one
>      Need to free all allocated folios, and use the existing larger
>      folio instead.
> 
> 2.2) The existing folio has the same size
>      Free the allocated one and reuse the page cache.
>      This is the existing path.
> 
> 2.3) The existing folio is smaller than the allocated one
>      Fall back to re-allocate order 0 folios instead.
> 
> Otherwise all the needed infrastructure is already here, we only need to
> try allocate larger folio as our first try in alloc_eb_folio_array().
> 
> For now, the higher order allocation is only a preferable attempt for
> debug build, before we had enough test coverage and push it to end
> users.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
> Changelog:
> [CHANGELOG]
> v8:
> - Drop the memcgroup optimization as dependency
>   Opting out memcgroup will be pushed as an independent patchset
>   instead. It's not related to the soft lockup.
> 
> - Fix a soft lockup caused by mixed folio orders
> 	 |<- folio ->|
> 	 |  |  |//|//|   |//| is the existing page cache
>   In above case, the filemap_add_folio() will always return -EEXIST
>   but filemap_lock_folio() also returns -ENOENT.
>   Which can lead to a dead loop.
>   Fix it by only retrying 5 times for larger folios, then fall back
>   to 0 order folios.
> 
> - Slightly rewording the commit messages
>   Make it shorter and better organized.
> 
> v7:
> - Fix an accidentally removed line caused by previous modification
>   attempt
>   Previously I was moving that line to the common branch to
>   unconditionally define root_mem_cgroup pointer.
>   But that's later discarded and changed to use macro definition, but
>   forgot to add back the original line.
> 
> v6:
> - Add a new root_mem_cgroup definition for CONFIG_MEMCG=n cases
>   So that users of root_mem_cgroup no longer needs to check
>   CONFIG_MEMCG.
>   This is to fix the compile error for CONFIG_MEMCG=n cases.
> 
> - Slight rewording of the 2nd patch
> 
> v5:
> - Use root memcgroup to attach folios to btree inode filemap
> - Only try higher order folio once without NOFAIL nor extra retry
> 
> v4:
> - Hide the feature behind CONFIG_BTRFS_DEBUG
>   So that end users won't be affected (aka, still per-page based
>   allocation) meanwhile we can do more testing on this new behavior.
> 
> v3:
> - Rebased to the latest for-next branch
> - Use PAGE_ALLOC_COSTLY_ORDER to determine whether to use __GFP_NOFAIL
> - Add a dependency MM patch "mm/page_alloc: unify the warning on NOFAIL
>   and high order allocation"
>   This allows us to use NOFAIL up to 32K nodesize, and makes sure for
>   default 16K nodesize, all metadata would go 16K folios
> 
> v2:
> - Rebased to handle the change in "btrfs: cache folio size and shift in extent_buffer"
> ---
>  fs/btrfs/extent_io.c | 122 ++++++++++++++++++++++++++++++-------------
>  1 file changed, 86 insertions(+), 36 deletions(-)
> 
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index aa7f8148cd0d..0beebcb9be77 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c
> @@ -719,12 +719,28 @@ int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
>   *
>   * For now, the folios populated are always in order 0 (aka, single page).
>   */
> -static int alloc_eb_folio_array(struct extent_buffer *eb, bool nofail)
> +static int alloc_eb_folio_array(struct extent_buffer *eb, int order,
> +				bool nofail)
>  {
>  	struct page *page_array[INLINE_EXTENT_BUFFER_PAGES] = { 0 };
>  	int num_pages = num_extent_pages(eb);
>  	int ret;
>  
> +	if (order) {
> +		gfp_t gfp;
> +
> +		if (order > 0)
> +			gfp = GFP_NOFS | __GFP_NORETRY | __GFP_NOWARN;
> +		else
> +			gfp = nofail ? (GFP_NOFS | __GFP_NOFAIL) : GFP_NOFS;

This check is useless, we're already checking if (order) above.

> +		eb->folios[0] = folio_alloc(gfp, order);
> +		if (likely(eb->folios[0])) {
> +			eb->folio_size = folio_size(eb->folios[0]);
> +			eb->folio_shift = folio_shift(eb->folios[0]);
> +			return 0;
> +		}
> +		/* Fallback to 0 order (single page) allocation. */
> +	}
>  	ret = btrfs_alloc_page_array(num_pages, page_array, nofail);
>  	if (ret < 0)
>  		return ret;
> @@ -2707,7 +2723,7 @@ struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
>  	 */
>  	set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
>  
> -	ret = alloc_eb_folio_array(new, false);
> +	ret = alloc_eb_folio_array(new, 0, false);
>  	if (ret) {
>  		btrfs_release_extent_buffer(new);
>  		return NULL;
> @@ -2740,7 +2756,7 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
>  	if (!eb)
>  		return NULL;
>  
> -	ret = alloc_eb_folio_array(eb, false);
> +	ret = alloc_eb_folio_array(eb, 0, false);
>  	if (ret)
>  		goto err;
>  
> @@ -2955,7 +2971,16 @@ static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start)
>  	return 0;
>  }
>  
> +static void free_all_eb_folios(struct extent_buffer *eb)
> +{
> +	for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) {
> +		if (eb->folios[i])
> +			folio_put(eb->folios[i]);
> +		eb->folios[i] = NULL;
> +	}
> +}
>  
> +#define BTRFS_ADD_FOLIO_RETRY_LIMIT	(5)
>  /*
>   * Return 0 if eb->folios[i] is attached to btree inode successfully.
>   * Return >0 if there is already another extent buffer for the range,
> @@ -2973,6 +2998,8 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
>  	struct address_space *mapping = fs_info->btree_inode->i_mapping;
>  	const unsigned long index = eb->start >> PAGE_SHIFT;
>  	struct folio *existing_folio = NULL;
> +	const int eb_order = folio_order(eb->folios[0]);
> +	int retried = 0;
>  	int ret;
>  
>  	ASSERT(found_eb_ret);
> @@ -2990,18 +3017,25 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
>  	/* The page cache only exists for a very short time, just retry. */
>  	if (IS_ERR(existing_folio)) {
>  		existing_folio = NULL;
> +		retried++;
> +		/*
> +		 * We can have the following case:
> +		 *	|<- folio ->|
> +		 *	|  |  |//|//|
> +		 * Where |//| is the slot that we have a page cache.
> +		 *
> +		 * In above case, filemap_add_folio() will return -EEXIST,
> +		 * but filemap_lock_folio() will return -ENOENT.
> +		 * After several retries, we know it's the above case,
> +		 * and just fallback to order 0 folios instead.
> +		 */
> +		if (eb_order > 0 && retried > BTRFS_ADD_FOLIO_RETRY_LIMIT) {
> +			ASSERT(i == 0);
> +			return -EAGAIN;
> +		}

Ok hold on, I'm just now looking at this code, and am completely confused.

filemap_add_folio inserts our new folio into the mapping and returns with it
locked.  Under what circumstances do we end up with filemap_lock_folio()
returning ENOENT?

I understand in this larger folio case where this could happen, but this code
exists today where we're only allocating 0 order folios.  So does this actually
happen today, or were you future proofing it?

Also it seems like a super bad, no good thing that we can end up being allowed
to insert a folio that overlaps other folios already in the mapping.  So if that
can happen, that seems like the thing that needs to be addressed generically.

This sets off huge alarm bells for me, I'm going to need a more thorough
explanation of how this happens and why it's ok in the normal case.  Thanks,

Josef

  reply	other threads:[~2024-07-26 14:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-25 10:11 [PATCH v8] btrfs: prefer to allocate larger folio for metadata Qu Wenruo
2024-07-26 14:57 ` Josef Bacik [this message]
2024-07-26 22:32   ` Qu Wenruo
2024-07-31 15:11     ` Josef Bacik
2024-07-31 22:37       ` 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=20240726145753.GC3432726@perftesting \
    --to=josef@toxicpanda.com \
    --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