Skip to contentSkip to conversation

mm/mempool.c: set @elems slots to NULL once freed

PATCH In review [email protected] 3 participants 7 messages Dec 23, 2025 – Jan 8

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

PATCH 1/1 mm/mempool.c: set @elems slots to NULL once freed

From: Kevin Lourenco <[email protected]>

This is documented in the function comment as "...and sets their slots in @elems to NULL.", but it was not followed.

We need to follow the NULL assignment, because elements newly returned to the pool must not be touched under any circumstances by the user.

---
 mm/mempool.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/mempool.c +4 -2
index c290e5261b47..1a2060304fd4 100644
@@ -674,7 +674,8 @@ unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
 	if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
 		spin_lock_irqsave(&pool->lock, flags);
 		while (pool->curr_nr < pool->min_nr && freed < count) {
-			add_element(pool, elems[freed++]);
+			add_element(pool, elems[freed]);
+			elems[freed++] = NULL;
 			added = true;
 		}
 		spin_unlock_irqrestore(&pool->lock, flags);
@@ -683,7 +684,8 @@ unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
 		/* Handle the min_nr = 0 edge case: */
 		spin_lock_irqsave(&pool->lock, flags);
 		if (likely(pool->curr_nr == 0)) {
-			add_element(pool, elems[freed++]);
+			add_element(pool, elems[freed]);
+			elems[freed++] = NULL;
 			added = true;
 		}
 		spin_unlock_irqrestore(&pool->lock, flags);
Signature · 1 line
2.47.3
#1 lore raw

+Cc Christoph

Quoted 37 lines · On 12/24/25 00:10, [email protected] wrote:

From: Kevin Lourenco <[email protected]>

This is documented in the function comment as "...and sets their slots in @elems to NULL.", but it was not followed.

We need to follow the NULL assignment, because elements newly returned to the pool must not be touched under any circumstances by the user.

---
 mm/mempool.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/mempool.c +4 -2
index c290e5261b47..1a2060304fd4 100644
@@ -674,7 +674,8 @@ unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
 	if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
 		spin_lock_irqsave(&pool->lock, flags);
 		while (pool->curr_nr < pool->min_nr && freed < count) {
-			add_element(pool, elems[freed++]);
+			add_element(pool, elems[freed]);
+			elems[freed++] = NULL;
 			added = true;
 		}
 		spin_unlock_irqrestore(&pool->lock, flags);
@@ -683,7 +684,8 @@ unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
 		/* Handle the min_nr = 0 edge case: */
 		spin_lock_irqsave(&pool->lock, flags);
 		if (likely(pool->curr_nr == 0)) {
-			add_element(pool, elems[freed++]);
+			add_element(pool, elems[freed]);
+			elems[freed++] = NULL;
 			added = true;
 		}
 		spin_unlock_irqrestore(&pool->lock, flags);
#2 lore raw
In reply to [email protected]
Quoted 7 lines · On 12/24/25 00:10, [email protected] wrote:

From: Kevin Lourenco <[email protected]>

This is documented in the function comment as "...and sets their slots in @elems to NULL.", but it was not followed.

We need to follow the NULL assignment, because elements newly returned to the pool must not be touched under any circumstances by the user.

Well alternatively we could update the comment if no user needs to rely on this. Since it was modeled after __alloc_pages_bulk()/release_pages() and release_pages() doesn't do this, we could also avoid it for perf reasons. Christoph, what do you think?

Quoted 29 lines
---
 mm/mempool.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/mempool.c +4 -2
index c290e5261b47..1a2060304fd4 100644
@@ -674,7 +674,8 @@ unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
 	if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
 		spin_lock_irqsave(&pool->lock, flags);
 		while (pool->curr_nr < pool->min_nr && freed < count) {
-			add_element(pool, elems[freed++]);
+			add_element(pool, elems[freed]);
+			elems[freed++] = NULL;
 			added = true;
 		}
 		spin_unlock_irqrestore(&pool->lock, flags);
@@ -683,7 +684,8 @@ unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
 		/* Handle the min_nr = 0 edge case: */
 		spin_lock_irqsave(&pool->lock, flags);
 		if (likely(pool->curr_nr == 0)) {
-			add_element(pool, elems[freed++]);
+			add_element(pool, elems[freed]);
+			elems[freed++] = NULL;
 			added = true;
 		}
 		spin_unlock_irqrestore(&pool->lock, flags);
#3 lore raw
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · On 12/24/25 00:10, [email protected] wrote:

From: Kevin Lourenco <[email protected]>

This is documented in the function comment as "...and sets their slots in @elems to NULL.", but it was not followed.

Quoted 7 lines
Quoted 2 lines

We need to follow the NULL assignment, because elements newly returned to the pool must not be touched under any circumstances by the user.

Well alternatively we could update the comment if no user needs to rely on this. Since it was modeled after __alloc_pages_bulk()/release_pages() and release_pages() doesn't do this, we could also avoid it for perf reasons. Christoph, what do you think?

Not sure. I hate the magic null skipping behavior in __alloc_pages_bulk()/release_pages(), but given that we try to stay compatible it might be best to do this.

#4 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Vlastimil Babka
Quoted 7 lines · On 12/24/25 00:10, [email protected] wrote:

From: Kevin Lourenco <[email protected]>

This is documented in the function comment as "...and sets their slots in @elems to NULL.", but it was not followed.

We need to follow the NULL assignment, because elements newly returned to the pool must not be touched under any circumstances by the user.

Quoted 8 lines
Quoted 4 lines

Well alternatively we could update the comment if no user needs to rely on this. Since it was modeled after __alloc_pages_bulk()/release_pages() and release_pages() doesn't do this, we could also avoid it for perf reasons. Christoph, what do you think?

Not sure. I hate the magic null skipping behavior in __alloc_pages_bulk()/release_pages(), but given that we try to stay compatible it might be best to do this.

Well but release_pages() is null skipping but not NULL creating?

#5 lore raw
Quoted 5 lines · Vlastimil Babka
Quoted 3 lines

Not sure. I hate the magic null skipping behavior in __alloc_pages_bulk()/release_pages(), but given that we try to stay compatible it might be best to do this.

Well but release_pages() is null skipping but not NULL creating?

True. Let me think about this a bit more.

#6 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 5 lines · Vlastimil Babka
Quoted 3 lines

Not sure. I hate the magic null skipping behavior in __alloc_pages_bulk()/release_pages(), but given that we try to stay compatible it might be best to do this.

Well but release_pages() is null skipping but not NULL creating?

True. Let me think about this a bit more.

I thought about this a bit, and can't think of any benefit in zeroing the slot. So I think just updating the documentation here might be the better choice.