Skip to contentSkip to conversation

mempool_alloc_bulk and various mempool improvements

PATCH In review Christoph Hellwig 3 participants 19 messages Nov 11, 2025 – Nov 13, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi all,

this series adds a bulk version of mempool_alloc that makes allocating multiple objects deadlock safe.

The initial users is the blk-crypto-fallback code:

https://lore.kernel.org/linux-block/[email protected]/

with which v1 was posted, but I also have a few other users in mind.

Changes since v1:
- fix build for !CONFIG_FAULT_INJECTION
- improve the kerneldoc comments further
- refactor the code so that the mempool_alloc fastpath does not have to deal with arrays
- don't support !__GFP_DIRECT_RECLAIM for bulk allocations
- do poll allocations even if not all elements are available
- s/elem/elems/
- use a separate failure injection know for the bulk allocator

diffstat:

 include/linux/fault-inject.h |    8 -
 include/linux/mempool.h      |    7 
 mm/mempool.c                 |  341 ++++++++++++++++++++++++++++---------------
 3 files changed, 240 insertions(+), 116 deletions(-)
#1 lore raw

PATCH 1/7 fault-inject: make enum fault_flags available unconditionally

This will allow using should_fail_ex from code without having to make it conditional on CONFIG_FAULT_INJECTION.

---
 include/linux/fault-inject.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/fault-inject.h +4 -4
index 8c829d28dcf3..58fd14c82270 100644
@@ -8,6 +8,10 @@
 struct dentry;
 struct kmem_cache;
 
+enum fault_flags {
+	FAULT_NOWARN =	1 << 0,
+};
+
 #ifdef CONFIG_FAULT_INJECTION
 
 #include <linux/atomic.h>
@@ -36,10 +40,6 @@ struct fault_attr {
 	struct dentry *dname;
 };
 
-enum fault_flags {
-	FAULT_NOWARN =	1 << 0,
-};
-
 #define FAULT_ATTR_INITIALIZER {					\
 		.interval = 1,						\
 		.times = ATOMIC_INIT(1),				\
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/7 mempool: update kerneldoc comments

In reply to Christoph Hellwig

Use proper formatting, use full sentences and reduce some verbosity in function parameter descriptions.

---
 mm/mempool.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)
mm/mempool.c +22 -19
index 1c38e873e546..1f4701713203 100644
@@ -372,18 +372,20 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 EXPORT_SYMBOL(mempool_resize);
 
 /**
- * mempool_alloc - allocate an element from a specific memory pool
- * @pool:      pointer to the memory pool which was allocated via
- *             mempool_create().
- * @gfp_mask:  the usual allocation bitmask.
+ * mempool_alloc - allocate an element from a memory pool
+ * @pool:	pointer to the memory pool
+ * @gfp_mask:	GFP_* flags.  %__GFP_ZERO is not supported.
  *
- * this function only sleeps if the alloc_fn() function sleeps or
- * returns NULL. Note that due to preallocation, this function
- * *never* fails when called from process contexts. (it might
- * fail if called from an IRQ context.)
- * Note: using __GFP_ZERO is not supported.
+ * Allocate an element from @pool.  This is done by first calling into the
+ * alloc_fn supplied at pool initialization time, and dipping into the reserved
+ * pool when alloc_fn fails to allocate an element.
  *
- * Return: pointer to the allocated element or %NULL on error.
+ * This function only sleeps if the alloc_fn callback sleeps, or when waiting
+ * for elements to become available in the pool.
+ *
+ * Return: pointer to the allocated element or %NULL when failing to allocate
+ * an element.  Allocation failure can only happen when @gfp_mask does not
+ * include %__GFP_DIRECT_RECLAIM.
  */
 void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask)
 {
@@ -456,11 +458,10 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
 
 /**
  * mempool_alloc_preallocated - allocate an element from preallocated elements
- *                              belonging to a specific memory pool
- * @pool:      pointer to the memory pool which was allocated via
- *             mempool_create().
+ *                              belonging to a memory pool
+ * @pool:	pointer to the memory pool
  *
- * This function is similar to mempool_alloc, but it only attempts allocating
+ * This function is similar to mempool_alloc(), but it only attempts allocating
  * an element from the preallocated elements. It does not sleep and immediately
  * returns if no preallocated elements are available.
  *
@@ -492,12 +493,14 @@ void *mempool_alloc_preallocated(mempool_t *pool)
 EXPORT_SYMBOL(mempool_alloc_preallocated);
 
 /**
- * mempool_free - return an element to the pool.
- * @element:   pool element pointer.
- * @pool:      pointer to the memory pool which was allocated via
- *             mempool_create().
+ * mempool_free - return an element to a mempool
+ * @element:	pointer to element
+ * @pool:	pointer to the memory pool
+ *
+ * Returns @element to @pool if it needs replenishing, else frees it using
+ * the free_fn callback in @pool.
  *
- * this function only sleeps if the free_fn() function sleeps.
+ * This function only sleeps if the free_fn callback sleeps.
  */
 void mempool_free(void *element, mempool_t *pool)
 {
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/7 mempool: add error injection support

In reply to Christoph Hellwig

Add a call to should_fail_ex that forces mempool to actually allocate from the pool to stress the mempool implementation when enabled through debugfs. By default should_fail{,_ex} prints a very verbose stack trace that clutters the kernel log, slows down execution and triggers the kernel bug detection in xfstests. Pass FAULT_NOWARN and print a single-line message notating the caller instead so that full tests can be run with fault injection.

---
 mm/mempool.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
mm/mempool.c +18 -3
index 1f4701713203..76ba4d5e656f 100644
@@ -9,7 +9,7 @@
  *  started by Ingo Molnar, Copyright (C) 2001
  *  debugging by David Rientjes, Copyright (C) 2015
  */
-
+#include <linux/fault-inject.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/highmem.h>
@@ -20,6 +20,15 @@
 #include <linux/writeback.h>
 #include "slab.h"
 
+static DECLARE_FAULT_ATTR(fail_mempool_alloc);
+
+static int __init mempool_faul_inject_init(void)
+{
+	return PTR_ERR_OR_ZERO(fault_create_debugfs_attr("fail_mempool_alloc",
+			NULL, &fail_mempool_alloc));
+}
+late_initcall(mempool_faul_inject_init);
+
 #ifdef CONFIG_SLUB_DEBUG_ON
 static void poison_error(mempool_t *pool, void *element, size_t size,
 			 size_t byte)
@@ -404,9 +413,15 @@ void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask)
 	gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
 
 repeat_alloc:
+	if (should_fail_ex(&fail_mempool_alloc, 1, FAULT_NOWARN)) {
+		pr_info("forcing mempool usage for pool %pS\n",
+				(void *)_RET_IP_);
+		element = NULL;
+	} else {
+		element = pool->alloc(gfp_temp, pool->pool_data);
+	}
 
-	element = pool->alloc(gfp_temp, pool->pool_data);
-	if (likely(element != NULL))
+	if (likely(element))
 		return element;
 
 	spin_lock_irqsave(&pool->lock, flags);
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/7 mempool: factor out a mempool_adjust_gfp helper

In reply to Christoph Hellwig

Add a helper to better isolate and document the gfp flags adjustments.

---
 mm/mempool.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
mm/mempool.c +14 -7
index 76ba4d5e656f..912364e279e9 100644
@@ -380,6 +380,19 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 }
 EXPORT_SYMBOL(mempool_resize);
 
+/*
+ * Adjust the gfp flags for mempool allocations, as we never want to dip into
+ * the global emergency reserves or retry in the page allocator.
+ *
+ * The first pass also doesn't want to go reclaim, but the next passes do, so
+ * return a separate subset for that first iteration.
+ */
+static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask)
+{
+	*gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
+	return *gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
+}
+
 /**
  * mempool_alloc - allocate an element from a memory pool
  * @pool:	pointer to the memory pool
@@ -398,20 +411,14 @@ EXPORT_SYMBOL(mempool_resize);
  */
 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;
-	gfp_t gfp_temp;
 
 	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
 	might_alloc(gfp_mask);
 
-	gfp_mask |= __GFP_NOMEMALLOC;	/* don't allocate emergency reserves */
-	gfp_mask |= __GFP_NORETRY;	/* don't loop in __alloc_pages */
-	gfp_mask |= __GFP_NOWARN;	/* failures are OK */
-
-	gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
-
 repeat_alloc:
 	if (should_fail_ex(&fail_mempool_alloc, 1, FAULT_NOWARN)) {
 		pr_info("forcing mempool usage for pool %pS\n",
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/7 mempool: factor out a mempool_alloc_from_pool helper

In reply to Christoph Hellwig

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.

---
 mm/mempool.c | 121 ++++++++++++++++++++++++---------------------------
 1 file changed, 57 insertions(+), 64 deletions(-)
mm/mempool.c +57 -64
index 912364e279e9..850362f4ca7a 100644
@@ -380,6 +380,50 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 }
 EXPORT_SYMBOL(mempool_resize);
 
+static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask)
+{
+	unsigned long flags;
+	void *element;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	if (unlikely(!pool->curr_nr))
+		goto fail;
+	element = remove_element(pool);
+	spin_unlock_irqrestore(&pool->lock, flags);
+
+	/* Paired with rmb in mempool_free(), read comment there. */
+	smp_wmb();
+
+	/*
+	 * Update the allocation stack trace as this is more useful for
+	 * debugging.
+	 */
+	kmemleak_update_trace(element);
+	return element;
+
+fail:
+	if (gfp_mask & __GFP_DIRECT_RECLAIM) {
+		DEFINE_WAIT(wait);
+
+		prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
+		spin_unlock_irqrestore(&pool->lock, flags);
+
+		/*
+		 * 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.
+		 */
+		io_schedule_timeout(5 * HZ);
+		finish_wait(&pool->wait, &wait);
+	} else {
+		/* We must not sleep if __GFP_DIRECT_RECLAIM is not set. */
+		spin_unlock_irqrestore(&pool->lock, flags);
+	}
+
+	return NULL;
+}
+
 /*
  * Adjust the gfp flags for mempool allocations, as we never want to dip into
  * the global emergency reserves or retry in the page allocator.
@@ -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) {
+			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);
 
@@ -492,25 +503,7 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  */
 void *mempool_alloc_preallocated(mempool_t *pool)
 {
-	void *element;
-	unsigned long flags;
-
-	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();
-		/*
-		 * Update the allocation stack trace as this is more useful
-		 * for debugging.
-		 */
-		kmemleak_update_trace(element);
-		return element;
-	}
-	spin_unlock_irqrestore(&pool->lock, flags);
-
-	return NULL;
+	return mempool_alloc_from_pool(pool, GFP_NOWAIT);
 }
 EXPORT_SYMBOL(mempool_alloc_preallocated);
 
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/7 mempool: fix a wakeup race when sleeping for elements

In reply to Christoph Hellwig

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.

---
 mm/mempool.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
mm/mempool.c +9 -4
index 850362f4ca7a..8cf3b5705b7f 100644
@@ -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);
 
 		/*
-		 * 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);
Signature · 1 line
2.47.3
#7 lore raw

Re: [PATCH 6/7] mempool: fix a wakeup race when sleeping for elements

Quoted 7 lines · Christoph Hellwig

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 :)

Quoted 18 lines
---
 mm/mempool.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
mm/mempool.c +1 -0
index 850362f4ca7a..8cf3b5705b7f 100644
@@ -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);
 

mm/mempool.c static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask) · line 414

Quoted 8 lines
mm/mempool.c +4 -0
@@ -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?

Quoted 13 lines
mm/mempool.c +4 -4
 		/*
-		 * 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);
#8 lore raw

Re: [PATCH 6/7] mempool: fix a wakeup race when sleeping for elements

Quoted 7 lines · Vlastimil Babka
Quoted 7 lines
mm/mempool.c +1 -0
+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);
 
Quoted 10 lines

mm/mempool.c static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask) · line 414

Quoted 6 lines
mm/mempool.c +4 -0
 		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?

You're right, I'll drop this again.

#9 lore raw

PATCH 7/7 mempool: add mempool_{alloc,free}_bulk

In reply to Christoph Hellwig

Add a version of the mempool allocator that works for batch allocations of multiple objects. Calling mempool_alloc in a loop is not safe because it could deadlock if multiple threads are performing such an allocation at the same time.

As an extra benefit the interface is build so that the same array can be used for alloc_pages_bulk / release_pages so that at least for page backed mempools the fast path can use a nice batch optimization.

---
 include/linux/mempool.h |   7 ++
 mm/mempool.c            | 182 ++++++++++++++++++++++++++++++----------
 2 files changed, 145 insertions(+), 44 deletions(-)
include/linux/mempool.h +7 -0
index 34941a4b9026..486ed50776db 100644
@@ -66,9 +66,16 @@ extern void mempool_destroy(mempool_t *pool);
 extern void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask) __malloc;
 #define mempool_alloc(...)						\
 	alloc_hooks(mempool_alloc_noprof(__VA_ARGS__))
+int mempool_alloc_bulk_noprof(mempool_t *pool, void **elem,
+		unsigned int count, gfp_t gfp_mask, unsigned long caller_ip);
+#define mempool_alloc_bulk(pool, elem, count, gfp_mask)			\
+	alloc_hooks(mempool_alloc_bulk_noprof(pool, elem, count, gfp_mask, \
+			_RET_IP_))
 
 extern void *mempool_alloc_preallocated(mempool_t *pool) __malloc;
 extern void mempool_free(void *element, mempool_t *pool);
+unsigned int mempool_free_bulk(mempool_t *pool, void **elem,
+		unsigned int count);
 
 /*
  * A mempool_alloc_t and mempool_free_t that get the memory from
mm/mempool.c +138 -44

268 lines not on this page · raw message

Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 7/7] mempool: add mempool_{alloc,free}_bulk

Quoted 45 lines · Christoph Hellwig

Add a version of the mempool allocator that works for batch allocations of multiple objects. Calling mempool_alloc in a loop is not safe because it could deadlock if multiple threads are performing such an allocation at the same time.

As an extra benefit the interface is build so that the same array can be used for alloc_pages_bulk / release_pages so that at least for page backed mempools the fast path can use a nice batch optimization.

---
 include/linux/mempool.h |   7 ++
 mm/mempool.c            | 182 ++++++++++++++++++++++++++++++----------
 2 files changed, 145 insertions(+), 44 deletions(-)
include/linux/mempool.h +7 -0
index 34941a4b9026..486ed50776db 100644
@@ -66,9 +66,16 @@ extern void mempool_destroy(mempool_t *pool);
 extern void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask) __malloc;
 #define mempool_alloc(...)						\
 	alloc_hooks(mempool_alloc_noprof(__VA_ARGS__))
+int mempool_alloc_bulk_noprof(mempool_t *pool, void **elem,
+		unsigned int count, gfp_t gfp_mask, unsigned long caller_ip);
+#define mempool_alloc_bulk(pool, elem, count, gfp_mask)			\
+	alloc_hooks(mempool_alloc_bulk_noprof(pool, elem, count, gfp_mask, \
+			_RET_IP_))
 
 extern void *mempool_alloc_preallocated(mempool_t *pool) __malloc;
 extern void mempool_free(void *element, mempool_t *pool);
+unsigned int mempool_free_bulk(mempool_t *pool, void **elem,
+		unsigned int count);
 
 /*
  * A mempool_alloc_t and mempool_free_t that get the memory from
mm/mempool.c +1 -0
index 8cf3b5705b7f..e2f05bec633b 100644
@@ -21,11 +21,16 @@
 #include "slab.h"
 
 static DECLARE_FAULT_ATTR(fail_mempool_alloc);
+static DECLARE_FAULT_ATTR(fail_mempool_alloc_bulk);

mm/mempool.c line 32

Quoted 10 lines
mm/mempool.c +5 -2
 
 static int __init mempool_faul_inject_init(void)
 {
-	return PTR_ERR_OR_ZERO(fault_create_debugfs_attr("fail_mempool_alloc",
-			NULL, &fail_mempool_alloc));
+	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.

Quoted 10 lines
mm/mempool.c +2 -1
+	return 0;
 }
 late_initcall(mempool_faul_inject_init);
 
@@ -380,16 +385,21 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 }
 EXPORT_SYMBOL(mempool_resize);
 
-static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask)
+static bool mempool_alloc_from_pool(struct mempool *pool, void **elems,

mm/mempool.c int mempool_resize(mempool_t *pool, int new_min_nr) · line 396

Quoted 10 lines
mm/mempool.c +4 -2
+		unsigned int count, unsigned int allocated,
+		gfp_t gfp_mask)
 {
 	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...

Quoted 40 lines
mm/mempool.c +9 -5
 		goto fail;
 alloc:
-	element = remove_element(pool);
+	for (; allocated < count; allocated++) {
+		if (!elems[allocated])
+			elems[allocated] = remove_element(pool);
+	}
 	spin_unlock_irqrestore(&pool->lock, flags);
 
 	/* Paired with rmb in mempool_free(), read comment there. */
@@ -399,15 +409,16 @@ static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask)
 	 * Update the allocation stack trace as this is more useful for
 	 * debugging.
 	 */
-	kmemleak_update_trace(element);
-	return element;
+	for (i = 0; i < count; i++)
+		kmemleak_update_trace(elems[i]);
+	return true;
 
 fail:
 	if (gfp_mask & __GFP_DIRECT_RECLAIM) {
 		DEFINE_WAIT(wait);
 
 		prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
-		if (pool->curr_nr) {
+		if (pool->curr_nr >= count - allocated) {
 			finish_wait(&pool->wait, &wait);
 			goto alloc;
 		}
@@ -426,7 +437,7 @@ static void *mempool_alloc_from_pool(struct mempool *pool, gfp_t gfp_mask)
 		spin_unlock_irqrestore(&pool->lock, flags);
 	}
 
-	return NULL;
+	return false;
 }
 
 /*

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 461

Quoted 9 lines
mm/mempool.c +6 -0
@@ -442,6 +453,72 @@ static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask)
 	return *gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
 }
 
+/**
+ * mempool_alloc_bulk - allocate multiple elements from a memory pool
+ * @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...

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 470

Quoted 9 lines
mm/mempool.c +9 -0
+ *
+ * Allocate elements for each slot in @elem that is non-%NULL. This is done by
+ * first calling into the alloc_fn supplied at pool initialization time, and
+ * dipping into the reserved pool when alloc_fn fails to allocate an element.
+ *
+ * This function only sleeps if the alloc_fn callback sleeps, or when waiting
+ * for elements to become available in the pool.
+ *
+ * Return: Always 0.  If it wasn't for %$#^$ alloc tags, it would return void.

My sympathies :) But see below.

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 479

Quoted 9 lines
mm/mempool.c +9 -0
+ */
+int mempool_alloc_bulk_noprof(struct mempool *pool, void **elems,
+		unsigned int count, gfp_t gfp_mask, unsigned long caller_ip)
+{
+	gfp_t gfp_temp = mempool_adjust_gfp(&gfp_mask);
+	unsigned int i = 0;
+
+	VM_WARN_ON_ONCE(count > pool->min_nr);
+	VM_WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT_RECLAIM));

... per here.

Quoted 12 lines
mm/mempool.c +12 -0
+	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
+	might_alloc(gfp_mask);
+
+	/*
+	 * If an error is injected, fail all elements in a bulk allocation so
+	 * that we stress the multiple elements missing path.
+	 */
+	if (should_fail_ex(&fail_mempool_alloc_bulk, 1, FAULT_NOWARN)) {
+		pr_info("forcing mempool usage for pool %pS\n",
+				(void *)_RET_IP_);
+		goto use_pool;
+	}

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 501

Quoted 10 lines
mm/mempool.c +10 -0
+
+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?

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 512

Quoted 11 lines
mm/mempool.c +11 -0
+	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)?

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 515

Quoted 3 lines
mm/mempool.c +3 -0
+	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?

Quoted 6 lines
mm/mempool.c +6 -0
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(mempool_alloc_bulk_noprof);
+
#11 lore raw

Re: [PATCH 7/7] mempool: add mempool_{alloc,free}_bulk

Quoted 8 lines · Vlastimil Babka

mm/mempool.c line 32

Quoted 5 lines
mm/mempool.c +5 -0
+	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.

Quoted 4 lines
Quoted 4 lines
mm/mempool.c +1 -1
 	unsigned long flags;
-	void *element;
+	unsigned int i;
Quoted 8 lines

mm/mempool.c int mempool_resize(mempool_t *pool, int new_min_nr) · line 396

Quoted 3 lines
mm/mempool.c +1 -1
 	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.

Quoted 6 lines
Quoted 4 lines

+ * @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.

Quoted 5 lines
Quoted 5 lines
mm/mempool.c +5 -0
+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
Quoted 8 lines

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 501

Quoted 4 lines
mm/mempool.c +4 -0
+	 * 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.

Quoted 14 lines

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 512

Quoted 12 lines
mm/mempool.c +11 -0
+	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.

Quoted 5 lines

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 515

Quoted 3 lines
mm/mempool.c +3 -0
+	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.

#12 lore raw

Re: [PATCH 7/7] mempool: add mempool_{alloc,free}_bulk

Quoted 27 lines · Christoph Hellwig
Quoted 8 lines · Vlastimil Babka

mm/mempool.c line 32

Quoted 5 lines
mm/mempool.c +5 -0
+	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.

Quoted 4 lines
Quoted 4 lines
mm/mempool.c +1 -1
 	unsigned long flags;
-	void *element;
+	unsigned int i;
Quoted 8 lines

mm/mempool.c int mempool_resize(mempool_t *pool, int new_min_nr) · line 396

Quoted 3 lines
mm/mempool.c +1 -1
 	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...

Quoted 3 lines

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.

Yeah I think it's fine with alloc_pages_bulk. It would only be a concern is the bulk alloc+mempool user used up part of the allocated array, NULLing some earlier pointers but leaving later ones alone, and then attempted to refill it.

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

+ * @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.

I thought it can be multiline, but if not, can refer to the notes below and explain there, yeah.

Quoted 31 lines
Quoted 5 lines
Quoted 5 lines
mm/mempool.c +5 -0
+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
Quoted 8 lines

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 501

Quoted 4 lines
mm/mempool.c +4 -0
+	 * 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.

Quoted 14 lines

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 512

Quoted 12 lines
mm/mempool.c +11 -0
+	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)?

Quoted 4 lines

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.

OK.

Quoted 10 lines
Quoted 5 lines

mm/mempool.c static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask) · line 515

Quoted 3 lines
mm/mempool.c +3 -0
+	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.

True, so let's ignore it.

#13 lore raw

Re: [PATCH 7/7] mempool: add mempool_{alloc,free}_bulk

Quoted 7 lines · Vlastimil Babka
Quoted 2 lines

and doesn't leave random holes? That's what a quick look at the code suggest, unfortunately the documentation for it totally sucks.

Yeah I think it's fine with alloc_pages_bulk. It would only be a concern is the bulk alloc+mempool user used up part of the allocated array, NULLing some earlier pointers but leaving later ones alone, and then attempted to refill it.

We could have a sanity check for that. The cache line is hot anyway, so another few NULL checks aren't going to cost us much.

#14 lore raw
In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

Hi all,

this series adds a bulk version of mempool_alloc that makes allocating multiple objects deadlock safe.

The initial users is the blk-crypto-fallback code:

https://lore.kernel.org/linux-block/[email protected]/

with which v1 was posted, but I also have a few other users in mind.

How do we handle this then once it's settled?

Quoted 15 lines

Changes since v1:
- fix build for !CONFIG_FAULT_INJECTION
- improve the kerneldoc comments further
- refactor the code so that the mempool_alloc fastpath does not have to deal with arrays
- don't support !__GFP_DIRECT_RECLAIM for bulk allocations
- do poll allocations even if not all elements are available
- s/elem/elems/
- use a separate failure injection know for the bulk allocator

diffstat:

 include/linux/fault-inject.h |    8 -
 include/linux/mempool.h      |    7 
 mm/mempool.c                 |  341 ++++++++++++++++++++++++++++---------------
 3 files changed, 240 insertions(+), 116 deletions(-)
#15 lore raw
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Christoph Hellwig

Hi all,

this series adds a bulk version of mempool_alloc that makes allocating multiple objects deadlock safe.

Quoted 7 lines
Quoted 5 lines

The initial users is the blk-crypto-fallback code:

https://lore.kernel.org/linux-block/[email protected]/

with which v1 was posted, but I also have a few other users in mind.

How do we handle this then once it's settled?

Good question. I think there is enough mm material now for a merge through an mm tree. If we get it sorted out for this merge window just merging it through mm even when there is no user yet would be easiest as the blk-crypto work is probably not going to make this merge window with additional review from Eric and I'll most like have another user for the next merge window as well (block bounce buffering for !STABLE_WRITES dio). Otherwise we'll have to operate with shared branches.

#16 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Vlastimil Babka
Quoted 9 lines · Christoph Hellwig

Hi all,

this series adds a bulk version of mempool_alloc that makes allocating multiple objects deadlock safe.

The initial users is the blk-crypto-fallback code:

https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

with which v1 was posted, but I also have a few other users in mind.

How do we handle this then once it's settled?

Good question. I think there is enough mm material now for a merge through an mm tree. If we get it sorted out for this merge window just merging it through mm even when there is no user yet would be easiest as

OK should work hopefully...

Quoted 4 lines

the blk-crypto work is probably not going to make this merge window with additional review from Eric and I'll most like have another user for the next merge window as well (block bounce buffering for !STABLE_WRITES dio). Otherwise we'll have to operate with shared branches.

... and we can avoid the need for shared branches that way.

#17 lore raw
In reply to Christoph Hellwig
Quoted 15 lines · Christoph Hellwig
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Christoph Hellwig

Hi all,

this series adds a bulk version of mempool_alloc that makes allocating multiple objects deadlock safe.

Quoted 7 lines
Quoted 5 lines

The initial users is the blk-crypto-fallback code:

https://lore.kernel.org/linux-block/[email protected]/

with which v1 was posted, but I also have a few other users in mind.

How do we handle this then once it's settled?

Quoted 7 lines

Good question. I think there is enough mm material now for a merge through an mm tree. If we get it sorted out for this merge window just merging it through mm even when there is no user yet would be easiest as the blk-crypto work is probably not going to make this merge window with additional review from Eric and I'll most like have another user for the next merge window as well (block bounce buffering for !STABLE_WRITES dio).

That sounds good to me. Sorry for the slow review on the blk-crypto-fallback changes. How about I also take the two fscrypt cleanups "fscrypt: pass a real sector_t to fscrypt_zeroout_range_inline_crypt" and "fscrypt: keep multiple bios in flight in fscrypt_zeroout_range_inline_crypt" through the fscrypt tree for 6.19 to get them out of the way? They don't depend on anything else.

- Eric

#18 lore raw
Quoted 2 lines · Eric Biggers

That sounds good to me. Sorry for the slow review on the blk-crypto-fallback changes.

No problem. I've got enough on my plate myself..

Quoted 6 lines

How about I also take the two fscrypt
cleanups "fscrypt: pass a real sector_t to fscrypt_zeroout_range_inline_crypt" and "fscrypt: keep multiple bios in flight in fscrypt_zeroout_range_inline_crypt" through the fscrypt tree for 6.19 to get them out of the way? They don't depend on anything else.

Sure. I'll resend them with the review comments addressed. I'll also see if any other of the API cleanups from my experimental branch might be suitable.