Skip to contentSkip to conversation

mempool: clarify behavior of mempool_alloc_preallocated()

PATCH Applied Thomas Weißschuh 4 participants 6 messages Oct 14, 2025 – Oct 16, 2025

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

PATCH 1/1 mempool: clarify behavior of mempool_alloc_preallocated()

The documentation of that function promises to never sleep. However on PREEMPT_RT a spinlock_t might in fact sleep.

Reword the documentation so users can predict its behavior better.

mempool could also replace spinlock_t with raw_spinlock_t which doesn't sleep even on PREEMPT_RT but that would take away the improved preemptibility of sleeping locks.

---
 mm/mempool.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mempool.c +2 -2
index 1c38e873e546fadcc594f041874eb42774e3df16..cceb09b75ebe35f263a5fb95ff6d400221ecbdd5 100644
@@ -461,8 +461,8 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  *             mempool_create().
  *
  * 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.
+ * an element from the preallocated elements. It only takes a single spinlock_t
+ * and immediately returns if no preallocated elements are available.
  *
  * Return: pointer to the allocated element or %NULL if no elements are
  * available.

---
base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
change-id: 20251014-mempool-doc-625dd4996110

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Thomas Weißschuh

The documentation of that function promises to never sleep. However on PREEMPT_RT a spinlock_t might in fact sleep.

Reword the documentation so users can predict its behavior better.

mempool could also replace spinlock_t with raw_spinlock_t which doesn't sleep even on PREEMPT_RT but that would take away the improved preemptibility of sleeping locks.

Quoted 25 lines
---
 mm/mempool.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mempool.c +2 -3
index 1c38e873e546fadcc594f041874eb42774e3df16..cceb09b75ebe35f263a5fb95ff6d400221ecbdd5 100644
@@ -461,8 +461,8 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  *             mempool_create().
  *
  * 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.
+ * an element from the preallocated elements. It only takes a single spinlock_t
+ * and immediately returns if no preallocated elements are available.
  *
  * Return: pointer to the allocated element or %NULL if no elements are
  * available.
 
---

base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
change-id: 20251014-mempool-doc-625dd4996110

Best regards,

#2 lore raw
In reply to Thomas Weißschuh
Quoted 19 lines · Thomas Weißschuh

The documentation of that function promises to never sleep. However on PREEMPT_RT a spinlock_t might in fact sleep.

Reword the documentation so users can predict its behavior better.

mempool could also replace spinlock_t with raw_spinlock_t which doesn't sleep even on PREEMPT_RT but that would take away the improved preemptibility of sleeping locks.

---
 mm/mempool.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mempool.c +0 -0
index 1c38e873e546fadcc594f041874eb42774e3df16..cceb09b75ebe35f263a5fb95ff6d400221ecbdd5 100644

mm/mempool.c EXPORT_SYMBOL(mempool_alloc_noprof); · line 464

Quoted 6 lines
mm/mempool.c +1 -2
@@ -461,8 +461,8 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  *             mempool_create().
  *
  * 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.
+ * an element from the preallocated elements. It only takes a single spinlock_t

Might it make more sense to say "It may sleep" instead of "takes a single spinlock_t"?

I feel like the fact that we take a spinlock isn't the important part here (especially because we always drop it before returning).

Quoted 13 lines
mm/mempool.c +1 -1
+ * and immediately returns if no preallocated elements are available.
  *
  * Return: pointer to the allocated element or %NULL if no elements are
  * available.
 
---

base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
change-id: 20251014-mempool-doc-625dd4996110

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
#3 lore raw
Quoted 4 lines · Vishal Moola (Oracle)
Quoted 4 lines
mm/mempool.c +0 -0
@@ -461,8 +461,8 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  *             mempool_create().
Quoted 8 lines

mm/mempool.c EXPORT_SYMBOL(mempool_alloc_noprof); · line 464

Quoted 5 lines
mm/mempool.c +1 -2
  *
  * 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.
+ * an element from the preallocated elements. It only takes a single spinlock_t

Might it make more sense to say "It may sleep" instead of "takes a single spinlock_t"?

May sleep usually refers to something that can not be used in an interrupt handler.

Quoted 2 lines

I feel like the fact that we take a spinlock isn't the important part here (especially because we always drop it before returning).

It actually is. A spinlock_t can not be acquired in hardirq context or when interrupts are explicitly disabled via local_irq_disable(). Therefore you should use the function in a local_irq_disable() section.

Sebastian

#4 lore raw
Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Vishal Moola (Oracle)
Quoted 4 lines
mm/mempool.c +0 -0
@@ -461,8 +461,8 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  *             mempool_create().
Quoted 8 lines

mm/mempool.c EXPORT_SYMBOL(mempool_alloc_noprof); · line 464

Quoted 5 lines
mm/mempool.c +1 -2
  *
  * 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.
+ * an element from the preallocated elements. It only takes a single spinlock_t

Might it make more sense to say "It may sleep" instead of "takes a single spinlock_t"?

Quoted 2 lines

May sleep usually refers to something that can not be used in an interrupt handler.

Gotcha.

Quoted 5 lines
Quoted 2 lines

I feel like the fact that we take a spinlock isn't the important part here (especially because we always drop it before returning).

It actually is. A spinlock_t can not be acquired in hardirq context or when interrupts are explicitly disabled via local_irq_disable(). Therefore you should use the function in a local_irq_disable() section.

As someone not too familiar with how the locking intertwines with the scheduler contexts, seeing something like that makes much more sense to me than seeing "it only takes a single spinlock_t."

#5 lore raw
Quoted 22 lines · Vishal Moola (Oracle)
Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Vishal Moola (Oracle)
Quoted 4 lines
mm/mempool.c +0 -0
@@ -461,8 +461,8 @@ EXPORT_SYMBOL(mempool_alloc_noprof);
  *             mempool_create().
Quoted 8 lines

mm/mempool.c EXPORT_SYMBOL(mempool_alloc_noprof); · line 464

Quoted 5 lines
mm/mempool.c +1 -2
  *
  * 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.
+ * an element from the preallocated elements. It only takes a single spinlock_t

Might it make more sense to say "It may sleep" instead of "takes a single spinlock_t"?

Quoted 2 lines

May sleep usually refers to something that can not be used in an interrupt handler.

Gotcha.

Quoted 2 lines
Quoted 2 lines

I feel like the fact that we take a spinlock isn't the important part here (especially because we always drop it before returning).

Quoted 7 lines
Quoted 3 lines

It actually is. A spinlock_t can not be acquired in hardirq context or when interrupts are explicitly disabled via local_irq_disable(). Therefore you should use the function in a local_irq_disable() section.

As someone not too familiar with how the locking intertwines with the scheduler contexts, seeing something like that makes much more sense to me than seeing "it only takes a single spinlock_t."

I am not too happy about this wording but I don't have a better idea either. However "may sleep" is too broad. The spinlock_t is at the very least documented in
Documentation/locking/locktypes.rst

Sebastian