mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vlastimil Babka <vbabka@suse.cz>
To: Christoph Hellwig <hch@lst.de>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Harry Yoo <harry.yoo@oracle.com>,
	Eric Biggers <ebiggers@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 6/7] mempool: fix a wakeup race when sleeping for elements
Date: Wed, 12 Nov 2025 11:53:39 +0100	[thread overview]
Message-ID: <dec19492-bc23-49a0-a2b0-1cd8b5c7726e@suse.cz> (raw)
In-Reply-To: <20251111135300.752962-7-hch@lst.de>

On 11/11/25 14:52, Christoph Hellwig wrote:
> Waiters always need to re-check their condition after adding themselves
> to the waitqueue, as otherwise they might miss a wakeup.  Check
> for elements in the pool and use them before going to sleep.
> 
> The workaround mentioned was probably due to this, but seems genuinely
> useful for other reasons, so keep it and update the comment describing
> it.

Thanks for looking into that historic comment :)
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  mm/mempool.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/mm/mempool.c b/mm/mempool.c
> index 850362f4ca7a..8cf3b5705b7f 100644
> --- a/mm/mempool.c
> +++ b/mm/mempool.c
> @@ -388,6 +388,7 @@ static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask)
>  	spin_lock_irqsave(&pool->lock, flags);
>  	if (unlikely(!pool->curr_nr))
>  		goto fail;
> +alloc:
>  	element = remove_element(pool);
>  	spin_unlock_irqrestore(&pool->lock, flags);
>  
> @@ -406,13 +407,17 @@ static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask)
>  		DEFINE_WAIT(wait);
>  
>  		prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
> +		if (pool->curr_nr) {
> +			finish_wait(&pool->wait, &wait);
> +			goto alloc;
> +		}
>  		spin_unlock_irqrestore(&pool->lock, flags);
I think the race already cannot exist, thanks to the pool->lock being
unlocked after prepare_to_wait()?
The freeing path can't bump pool->curr_nr without the lock, so the condition
you added can't even be true, no?

>  
>  		/*
> -		 * Wait for someone else to return an element to @pool.
> -		 *
> -		 * FIXME: this should be io_schedule().  The timeout is there as
> -		 * a workaround for some DM problems in 2.6.18.
> +		 * Wait for someone else to return an element to @pool, but wake
> +		 * up occasionally as memory pressure might have reduced even
> +		 * and the normal allocation in alloc_fn could succeed even if
> +		 * no element was returned.
>  		 */
>  		io_schedule_timeout(5 * HZ);
>  		finish_wait(&pool->wait, &wait);


  reply	other threads:[~2025-11-12 10:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-11 13:52 mempool_alloc_bulk and various mempool improvements Christoph Hellwig
2025-11-11 13:52 ` [PATCH 1/7] fault-inject: make enum fault_flags available unconditionally Christoph Hellwig
2025-11-11 13:52 ` [PATCH 2/7] mempool: update kerneldoc comments Christoph Hellwig
2025-11-11 13:52 ` [PATCH 3/7] mempool: add error injection support Christoph Hellwig
2025-11-11 13:52 ` [PATCH 4/7] mempool: factor out a mempool_adjust_gfp helper Christoph Hellwig
2025-11-11 13:52 ` [PATCH 5/7] mempool: factor out a mempool_alloc_from_pool helper Christoph Hellwig
2025-11-11 13:52 ` [PATCH 6/7] mempool: fix a wakeup race when sleeping for elements Christoph Hellwig
2025-11-12 10:53   ` Vlastimil Babka [this message]
2025-11-12 15:38     ` Christoph Hellwig
2025-11-11 13:52 ` [PATCH 7/7] mempool: add mempool_{alloc,free}_bulk Christoph Hellwig
2025-11-12 12:20   ` Vlastimil Babka
2025-11-12 15:47     ` Christoph Hellwig
2025-11-12 15:56       ` Vlastimil Babka
2025-11-12 15:58         ` Christoph Hellwig
2025-11-12 12:22 ` mempool_alloc_bulk and various mempool improvements Vlastimil Babka
2025-11-12 15:50   ` Christoph Hellwig
2025-11-12 15:57     ` Vlastimil Babka
2025-11-12 17:34     ` Eric Biggers
2025-11-13  5:52       ` Christoph Hellwig

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=dec19492-bc23-49a0-a2b0-1cd8b5c7726e@suse.cz \
    --to=vbabka@suse.cz \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=ebiggers@kernel.org \
    --cc=harry.yoo@oracle.com \
    --cc=hch@lst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    /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

all inboxes | Powered by JetHome®