From: Christoph Hellwig <hch@lst.de>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Christoph Hellwig <hch@lst.de>,
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>,
Eric Biggers <ebiggers@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 7/7] mempool: add mempool_{alloc,free}_bulk
Date: Wed, 12 Nov 2025 16:47:54 +0100 [thread overview]
Message-ID: <20251112154754.GB7209@lst.de> (raw)
In-Reply-To: <c691c9c1-a513-4db3-95f6-3d24111680b7@suse.cz>
On Wed, Nov 12, 2025 at 01:20:21PM +0100, Vlastimil Babka wrote:
> > + if (IS_ERR(fault_create_debugfs_attr("fail_mempool_alloc", NULL,
> > + &fail_mempool_alloc)) ||
> > + IS_ERR(fault_create_debugfs_attr("fail_mempool_alloc_bulk", NULL,
> > + &fail_mempool_alloc_bulk)))
> > + return -ENOMEM;
>
> Pedantically speaking the error (from debugfs_create_dir()) might be
> different, probably doesn't matter in practice.
Yeah, this is an initcall, so the exact error really does not matter.
But adding an error variable isn't that annyoing, so I'll switch to
that.
> > unsigned long flags;
> > - void *element;
> > + unsigned int i;
> >
> > spin_lock_irqsave(&pool->lock, flags);
> > - if (unlikely(!pool->curr_nr))
> > + if (unlikely(pool->curr_nr < count - allocated))
>
> So we might be pessimistic here when some of the elements in the array
> already are not NULL so we need in fact less. Might be an issue if callers
> were relying on this for forward progress? It would be simpler to just tell
> them not to...
Yes. I think alloc_pages_bulk always allocates from the beginning
and doesn't leave random holes? That's what a quick look at the code
suggest, unfortunately the documentation for it totally sucks.
> > + * @pool: pointer to the memory pool
> > + * @elems: partially or fully populated elements array
> > + * @count: size (in entries) of @elem
> > + * @gfp_mask: GFP_* flags. %__GFP_ZERO is not supported.
>
> We should say __GFP_DIRECT_RECLAIM is mandatory...
It's not really going to fit in there :) Maybe I should have ignored
Eric's request to mention __GFP_ZERO here and just keep everything
together.
> > +repeat_alloc:
> > + /*
> > + * Try to allocate the elements using the allocation callback. If that
> > + * succeeds or we were not allowed to sleep, return now. Don't dip into
> > + * the reserved pools for !__GFP_DIRECT_RECLAIM allocations as they
> > + * aren't guaranteed to succeed and chances of getting an allocation
> > + * from the allocators using GFP_ATOMIC is higher than stealing one of
> > + * the few items from our usually small pool.
> > + */
>
> Hm but the code doesn't do what the comment says, AFAICS? It will try
> dipping into the pool and might succeed if there are elements, only will not
> wait for them there?
Yeah, that comment is actually stale from an older version.
>
> > + for (; i < count; i++) {
> > + if (!elems[i]) {
> > + elems[i] = pool->alloc(gfp_temp, pool->pool_data);
> > + if (unlikely(!elems[i]))
> > + goto use_pool;
> > + }
> > + }
> > +
> > + return 0;
> > +
> > +use_pool:
>
> So should we bail out here with -ENOMEM when !(gfp_mask & __GFP_DIRECT_RECLAIM)?
No, I don't want the !__GFP_DIRECT_RECLAIM handling. It's a mess,
and while for mempool_alloc having it for compatibility might make some
sense, I'd rather avoid it for this new interface where the semantics
of failing allocations are going to be really annoying.
> > + if (!mempool_alloc_from_pool(pool, elems, count, i, gfp_temp)) {
> > + gfp_temp = gfp_mask;
> > + goto repeat_alloc;
>
> Because this seems to be an infinite loop otherwise?
You mean if someone passed in !__GFP_DIRECT_RECLAIM and got the warning
above? Yes, IFF that code makes it to production and then runs into
a low-memory situation it would. But it's an API abuse. The other
option would be to just force __GFP_DIRECT_RECLAIM.
next prev parent reply other threads:[~2025-11-12 15:48 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
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 [this message]
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=20251112154754.GB7209@lst.de \
--to=hch@lst.de \
--cc=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=ebiggers@kernel.org \
--cc=harry.yoo@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=vbabka@suse.cz \
/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;
as well as URLs for NNTP newsgroup(s).