mm/mempool.c: set @elems slots to NULL once freed
Subsystems (2)
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
@@ -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
+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.
Signed-off-by Kevin Lourenco <[email protected]>--- mm/mempool.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/mempool.c +4
-2@@ -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);
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
Signed-off-by Kevin Lourenco <[email protected]>--- mm/mempool.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/mempool.c +4
-2@@ -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);
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.
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?
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.
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.