linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Hugh Dickins <hughd@google.com>
To: Christoph Hellwig <hch@lst.de>, Vlastimil Babka <vbabka@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	 Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	 Roman Gushchin <roman.gushchin@linux.dev>,
	 Harry Yoo <harry.yoo@oracle.com>,
	Suren Baghdasaryan <surenb@google.com>,
	 Michal Hocko <mhocko@suse.com>,
	Brendan Jackman <jackmanb@google.com>,  Zi Yan <ziy@nvidia.com>,
	Eric Biggers <ebiggers@kernel.org>,
	 linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 06/11] mempool: factor out a mempool_alloc_from_pool helper
Date: Sat, 22 Nov 2025 19:42:51 -0800 (PST)	[thread overview]
Message-ID: <a8a2c740-2208-5692-cfc6-f475ae0f7d45@google.com> (raw)
In-Reply-To: <20251113084022.1255121-7-hch@lst.de>

On Thu, 13 Nov 2025, Christoph Hellwig wrote:

> Add a helper for the mempool_alloc slowpath to better separate it from the
> fast path, and also use it to implement mempool_alloc_preallocated which
> shares the same logic.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
...
> @@ -413,8 +457,6 @@ void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask)
>  {
>  	gfp_t gfp_temp = mempool_adjust_gfp(&gfp_mask);
>  	void *element;
> -	unsigned long flags;
> -	wait_queue_entry_t wait;
>  
>  	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
>  	might_alloc(gfp_mask);
> @@ -428,53 +470,22 @@ void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask)
>  		element = pool->alloc(gfp_temp, pool->pool_data);
>  	}
>  
> -	if (likely(element))
> -		return element;
> -
> -	spin_lock_irqsave(&pool->lock, flags);
> -	if (likely(pool->curr_nr)) {
> -		element = remove_element(pool);
> -		spin_unlock_irqrestore(&pool->lock, flags);
> -		/* paired with rmb in mempool_free(), read comment there */
> -		smp_wmb();
> +	if (unlikely(!element)) {
>  		/*
> -		 * Update the allocation stack trace as this is more useful
> -		 * for debugging.
> +		 * Try to allocate an element from the pool.
> +		 *
> +		 * The first pass won't have __GFP_DIRECT_RECLAIM and won't
> +		 * sleep in mempool_alloc_from_pool.  Retry the allocation
> +		 * with all flags set in that case.
>  		 */
> -		kmemleak_update_trace(element);
> -		return element;
> -	}
> -
> -	/*
> -	 * We use gfp mask w/o direct reclaim or IO for the first round.  If
> -	 * alloc failed with that and @pool was empty, retry immediately.
> -	 */
> -	if (gfp_temp != gfp_mask) {
> -		spin_unlock_irqrestore(&pool->lock, flags);
> -		gfp_temp = gfp_mask;
> -		goto repeat_alloc;
> -	}
> -
> -	/* We must not sleep if !__GFP_DIRECT_RECLAIM */
> -	if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
> -		spin_unlock_irqrestore(&pool->lock, flags);
> -		return NULL;
> +		element = mempool_alloc_from_pool(pool, gfp_mask);
> +		if (!element && gfp_temp != gfp_mask) {

No, that is wrong, it breaks the mempool promise: linux-next oopses
in swap_writepage_bdev_async(), which relies on bio_alloc(,,,GFP_NOIO)
to return a good bio.

The refactoring makes it hard to see, but the old version always used
to go back to repeat_alloc at the end, if __GFP_DIRECT_RECLAIM,
whereas here it only does so the first time, when gfp_temp != gfp_mask.

After bisecting to here, I changed that "gfp_temp != gfp_mask" to
"(gfp & __GFP_DIRECT_RECLAIM)", and it worked again.  But other patches
have come in on top, so below is a patch to the final mm/mempool.c...

> +			gfp_temp = gfp_mask;
> +			goto repeat_alloc;
> +		}
>  	}
>  
> -	/* Let's wait for someone else to return an element to @pool */
> -	init_wait(&wait);
> -	prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
> -
> -	spin_unlock_irqrestore(&pool->lock, flags);
> -
> -	/*
> -	 * FIXME: this should be io_schedule().  The timeout is there as a
> -	 * workaround for some DM problems in 2.6.18.
> -	 */
> -	io_schedule_timeout(5*HZ);
> -
> -	finish_wait(&pool->wait, &wait);
> -	goto repeat_alloc;
> +	return element;
>  }
>  EXPORT_SYMBOL(mempool_alloc_noprof);
...

[PATCH] mempool: fix NULL from mempool_alloc_noprof()

mempool_alloc_noprof() with __GFP_DIRECT_RECLAIM used to loop until it
had allocated an element, but recently regressed to returning NULL when
pool->alloc and mempool_alloc_from_pool() have both failed twice, causing
oops in __swap_writepage() (and presumably others relying on mempool).

Fixes: 1d091d2c5bf3 ("mempool: factor out a mempool_alloc_from_pool helper")
Signed-off-by: Hugh Dickins <hughd@google.com>
---
 mm/mempool.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/mempool.c b/mm/mempool.c
index 1558601600ba..dc9f2a1dc35f 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -576,7 +576,7 @@ void *mempool_alloc_noprof(struct mempool *pool, gfp_t gfp_mask)
 		 * with all flags set in that case.
 		 */
 		if (!mempool_alloc_from_pool(pool, &element, 1, 0, gfp_mask) &&
-		    gfp_temp != gfp_mask) {
+		    (gfp_mask & __GFP_DIRECT_RECLAIM)) {
 			gfp_temp = gfp_mask;
 			goto repeat_alloc;
 		}
-- 
2.51.0


  reply	other threads:[~2025-11-23  3:43 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-13  8:39 mempool_alloc_bulk and various mempool improvements v3 Christoph Hellwig
2025-11-13  8:39 ` [PATCH 01/11] fault-inject: make enum fault_flags available unconditionally Christoph Hellwig
2025-11-13  8:39 ` [PATCH 02/11] mm: improve kerneldoc comments for __alloc_pages_bulk Christoph Hellwig
2025-11-13  8:39 ` [PATCH 03/11] mempool: improve kerneldoc comments Christoph Hellwig
2025-11-13  8:39 ` [PATCH 04/11] mempool: add error injection support Christoph Hellwig
2025-11-13  8:39 ` [PATCH 05/11] mempool: factor out a mempool_adjust_gfp helper Christoph Hellwig
2025-11-13  8:39 ` [PATCH 06/11] mempool: factor out a mempool_alloc_from_pool helper Christoph Hellwig
2025-11-23  3:42   ` Hugh Dickins [this message]
2025-11-23 11:34     ` Vlastimil Babka
2025-11-23 17:49       ` Hugh Dickins
2025-11-23 21:22         ` Vlastimil Babka
2025-11-23 23:04           ` Hugh Dickins
2025-11-25 11:32             ` Vlastimil Babka
2025-11-24  6:17         ` Christoph Hellwig
2025-11-24  6:15       ` Christoph Hellwig
2025-11-25 11:34         ` Vlastimil Babka
2025-11-24  6:14     ` Christoph Hellwig
2025-11-13  8:39 ` [PATCH 07/11] mempool: add mempool_{alloc,free}_bulk Christoph Hellwig
2025-11-13  8:39 ` [PATCH 08/11] mempool: legitimize the io_schedule_timeout in mempool_alloc_from_pool Christoph Hellwig
2025-11-13  8:39 ` [PATCH 09/11] mempool: remove mempool_{init,create}_kvmalloc_pool Christoph Hellwig
2025-11-13  8:39 ` [PATCH 10/11] mempool: de-typedef Christoph Hellwig
2025-11-13  8:39 ` [PATCH 11/11] mempool: drop the file name in the top of file comment Christoph Hellwig
2025-11-13 16:12 ` mempool_alloc_bulk and various mempool improvements v3 Vlastimil Babka

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=a8a2c740-2208-5692-cfc6-f475ae0f7d45@google.com \
    --to=hughd@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=ebiggers@kernel.org \
    --cc=harry.yoo@oracle.com \
    --cc=hch@lst.de \
    --cc=jackmanb@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@suse.cz \
    --cc=ziy@nvidia.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