Skip to contentSkip to conversation

mempool: fix min_nr exceeding the elements array after concurrent resizes

PATCH Applied Nickolai Zeldovich 5 participants 17 messages Oct 16 – Nov 4

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

PATCH 1/1 mempool: fix min_nr exceeding the elements array after concurrent resizes

mempool_resize() shrinks a pool by dropping pool->lock around every free_fn() call and, once the pool holds no more than new_min_nr elements, stores new_min_nr into pool->min_nr. new_min_nr was only compared against pool->min_nr before the lock was first dropped, so that store assumes nothing changed meanwhile. The kernel-doc allows mempool_alloc()/mempool_free() to run while a resize is in progress and the grow path also copes with concurrent resizes ("Raced, other resize will do our work"), but the shrink path does not: while its lock is dropped another resize may shrink the pool below new_min_nr and a third one may grow it again to a size between the two, replacing pool->elements with an array of exactly that size. Storing the stale new_min_nr afterwards leaves pool->min_nr larger than the elements array.

Interleaving on a pool with min_nr = curr_nr = 10:

CPU A: mempool_resize(pool, 8)
takes the lock, 8 <= 10 so it shrinks: remove_element(), drops the lock, calls free_fn() and is preempted there
CPU B: mempool_resize(pool, 3)
completes: curr_nr = 3, min_nr = 3, elements still has 10 slots
CPU B: mempool_resize(pool, 5)
completes: elements = kmalloc_objs(5), min_nr = 5, curr_nr = 5
CPU A: retakes the lock, 8 < curr_nr (5) is false, stores min_nr = 8
while elements has 5 slots

Every later mempool_free() sees curr_nr < min_nr and add_element() stores the returned element at elements[5], [6] and [7], past the end of the 5-slot kmalloc allocation: a heap out-of-bounds write, i.e. slab corruption, a KASAN report, or a crash in whichever unrelated object sits next to the array. The invariant that breaks is that pool->min_nr never exceeds the size of pool->elements; every other path that changes min_nr or elements keeps it.

An in-tree caller can hit this: fs/smb/client resizes the global cifs_req_poolp from cifs_demultiplex_thread() (grow, when a TCP session is created) and from clean_demultiplex_info() (shrink, when one goes away). The two are coordinated only through the atomic tcpSesAllocCount, so mounts and unmounts racing on one client run the resizes above concurrently; pool->free is kmem_cache_free() there and the window is a preemption inside it. The other callers (block/blk-zoned.c, drivers/s390/scsi/zfcp_erp.c) serialise their resizes.

The shrink path has stored new_min_nr unconditionally since mempool_resize() was introduced, while the grow path has always re-checked pool->min_nr after reacquiring the lock.

Re-check new_min_nr against pool->min_nr under the lock before storing it. If a concurrent resize has shrunk the pool below new_min_nr in the meantime, keep that resize's min_nr (and whatever array it installed), as the grow path already does; otherwise the store is unchanged. The elements the loop has already freed stay freed, which is right either way because the pool's limit is now below new_min_nr.

Found by formal verification of mm/mempool.c against the kernel binary. Reproduced with a userspace build of the affected functions in which the free_fn() callback drives the interleaving above: before the fix min_nr ends at 8 with a 5-slot array and the next mempool_free() writes past it (AddressSanitizer heap-buffer-overflow); after the fix min_nr stays at 5 and the element is handed to free_fn(). Not reproduced on a running kernel, where the window is a single preemption inside free_fn().

Assisted-by LLM

---

 mm/mempool.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/mempool.c +3 -1
index cb74e718b2c6..95533e2975ce 100644
@@ -378,7 +378,9 @@ int mempool_resize(struct mempool *pool, int new_min_nr)
 			pool->free(element, pool->pool_data);
 			spin_lock_irqsave(&pool->lock, flags);
 		}
-		pool->min_nr = new_min_nr;
+		/* Raced, the array may now be smaller than new_min_nr */
+		if (new_min_nr <= pool->min_nr)
+			pool->min_nr = new_min_nr;
 		goto out_unlock;
 	}
 	spin_unlock_irqrestore(&pool->lock, flags);

base-commit: 90201351a862182bdabfc1ad1c97b1e1b6ed5931

Signature · 1 line
2.53.0
maintainer
#1 lore raw

[+Cc Christoph Hellwig and Vitaly Wool ]

Quoted 34 lines · Nickolai Zeldovich

mempool_resize() shrinks a pool by dropping pool->lock around every free_fn() call and, once the pool holds no more than new_min_nr elements, stores new_min_nr into pool->min_nr. new_min_nr was only compared against pool->min_nr before the lock was first dropped, so that store assumes nothing changed meanwhile. The kernel-doc allows mempool_alloc()/mempool_free() to run while a resize is in progress and the grow path also copes with concurrent resizes ("Raced, other resize will do our work"), but the shrink path does not: while its lock is dropped another resize may shrink the pool below new_min_nr and a third one may grow it again to a size between the two, replacing pool->elements with an array of exactly that size. Storing the stale new_min_nr afterwards leaves pool->min_nr larger than the elements array.

Interleaving on a pool with min_nr = curr_nr = 10:

CPU A: mempool_resize(pool, 8)
takes the lock, 8 <= 10 so it shrinks: remove_element(), drops the lock, calls free_fn() and is preempted there
CPU B: mempool_resize(pool, 3)
completes: curr_nr = 3, min_nr = 3, elements still has 10 slots
CPU B: mempool_resize(pool, 5)
completes: elements = kmalloc_objs(5), min_nr = 5, curr_nr = 5
CPU A: retakes the lock, 8 < curr_nr (5) is false, stores min_nr = 8
while elements has 5 slots

Every later mempool_free() sees curr_nr < min_nr and add_element() stores the returned element at elements[5], [6] and [7], past the end of the 5-slot kmalloc allocation: a heap out-of-bounds write, i.e. slab corruption, a KASAN report, or a crash in whichever unrelated object sits next to the array. The invariant that breaks is that pool->min_nr never exceeds the size of pool->elements; every other path that changes min_nr or elements keeps it.

Quoted 9 lines

An in-tree caller can hit this: fs/smb/client resizes the global cifs_req_poolp from cifs_demultiplex_thread() (grow, when a TCP session is created) and from clean_demultiplex_info() (shrink, when one goes away). The two are coordinated only through the atomic tcpSesAllocCount, so mounts and unmounts racing on one client run the resizes above concurrently; pool->free is kmem_cache_free() there and the window is a preemption inside it. The other callers (block/blk-zoned.c, drivers/s390/scsi/zfcp_erp.c) serialise their resizes.

The bug looks valid in theory, not sure if how easily it can be reproduced in practice as resizing should be quite rare.

(A bug is a bug and should be fixed, but no need to hurry in this case)

Quoted 3 lines

The shrink path has stored new_min_nr unconditionally since mempool_resize() was introduced, while the grow path has always re-checked pool->min_nr after reacquiring the lock.

So it's Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") ?

https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/?id=6892188b54d484098b79b0c9813c20e543124e2d

...from a historical commit before git era, 1da177e4c3f4.

Quoted 42 lines

Re-check new_min_nr against pool->min_nr under the lock before storing it. If a concurrent resize has shrunk the pool below new_min_nr in the meantime, keep that resize's min_nr (and whatever array it installed), as the grow path already does; otherwise the store is unchanged. The elements the loop has already freed stay freed, which is right either way because the pool's limit is now below new_min_nr.

Found by formal verification of mm/mempool.c against the kernel binary. Reproduced with a userspace build of the affected functions in which the free_fn() callback drives the interleaving above: before the fix min_nr ends at 8 with a 5-slot array and the next mempool_free() writes past it (AddressSanitizer heap-buffer-overflow); after the fix min_nr stays at 5 and the element is handed to free_fn(). Not reproduced on a running kernel, where the window is a single preemption inside free_fn().

Assisted-by LLM

---

 mm/mempool.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/mempool.c +3 -1
index cb74e718b2c6..95533e2975ce 100644
@@ -378,7 +378,9 @@ int mempool_resize(struct mempool *pool, int new_min_nr)
 			pool->free(element, pool->pool_data);
 			spin_lock_irqsave(&pool->lock, flags);
 		}
-		pool->min_nr = new_min_nr;
+		/* Raced, the array may now be smaller than new_min_nr */
+		if (new_min_nr <= pool->min_nr)
+			pool->min_nr = new_min_nr;
 		goto out_unlock;
 	}
 	spin_unlock_irqrestore(&pool->lock, flags);

base-commit: 90201351a862182bdabfc1ad1c97b1e1b6ed5931

Signature · 1 line
2.53.0
Signature · 2 lines
Cheers,
Harry / Hyeonggon
#2 lore raw
Quoted 4 lines · Harry Yoo

The bug looks valid in theory, not sure if how easily it can be reproduced in practice as resizing should be quite rare.

(A bug is a bug and should be fixed, but no need to hurry in this case)

I agree; seems unlikely to happen, but might as well fix it at some point.

Quoted 1 line

So it's Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") ?

Yes; I didn't think it was useful to include this "catch-all" tag, but technically, that's correct.

Thanks,

Nickolai.

maintainer
#4 lore raw
Quoted 3 lines · Vitaly Wool
Quoted 1 line

[+Cc Christoph Hellwig and Vitaly Wool ]

Thanks for CC'ing me on this.

No problem.

Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Quoted 4 lines

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

~Vitaly

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#5 lore raw
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se. It's just a better way of doing things.

~Vitaly

maintainer
#6 lore raw
Quoted 9 lines · Vitaly Wool
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se.

I mean the patch you've submitted does two things (wihch is not good).

It should be split into two patches that 1) fixes the bug and 2) optimizes it.

(Setting aside the question of whether the optimization is worth the
complexity)

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#7 lore raw
Quoted 12 lines · Harry Yoo
Quoted 7 lines · Vitaly Wool
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Quoted 7 lines
Quoted 2 lines

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se.

I mean the patch you've submitted does two things (wihch is not good).

It should be split into two patches that 1) fixes the bug and 2) optimizes it.

I don’t think this is a fair statement. I would say that the patch I’ve submitted improves mempool_resize() to be rid of unnecessary reallocations and data copying, and that (improved) implementation is *not* prone to the race found in the previous version of this function.

~Vitaly

maintainer
#8 lore raw
Quoted 17 lines · Vitaly Wool
Quoted 17 lines · Harry Yoo
Quoted 9 lines · Vitaly Wool
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se.

I mean the patch you've submitted does two things (wihch is not good).

Quoted 7 lines
Quoted 2 lines

It should be split into two patches that 1) fixes the bug and 2) optimizes it.

I don’t think this is a fair statement. I would say that the patch I’ve submitted improves mempool_resize() to be rid of unnecessary reallocations and data copying, and that (improved) implementation is *not* prone to the race found in the previous version of this function.

We backport bugfixes to -stable, but don't want to backport performance optimizations to -stable in general.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#9 lore raw
In reply to Vitaly Wool
Quoted 17 lines · Vitaly Wool
Quoted 17 lines · Harry Yoo
Quoted 9 lines · Vitaly Wool
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se.

I mean the patch you've submitted does two things (wihch is not good).

Quoted 7 lines
Quoted 2 lines

It should be split into two patches that 1) fixes the bug and 2) optimizes it.

I don’t think this is a fair statement. I would say that the patch I’ve submitted improves mempool_resize() to be rid of unnecessary reallocations and data copying, and that (improved) implementation is *not* prone to the race found in the previous version of this function.

But it makes the code more complex:

 2 files changed, 28 insertions(+), 16 deletions(-)

without an in-tree use case.

Quoted 1 line

~Vitaly

#10 lore raw
Quoted 30 lines · Vlastimil Babka (SUSE)
Quoted 17 lines · Vitaly Wool
Quoted 17 lines · Harry Yoo
Quoted 9 lines · Vitaly Wool
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se.

I mean the patch you've submitted does two things (wihch is not good).

Quoted 7 lines
Quoted 2 lines

It should be split into two patches that 1) fixes the bug and 2) optimizes it.

I don’t think this is a fair statement. I would say that the patch I’ve submitted improves mempool_resize() to be rid of unnecessary reallocations and data copying, and that (improved) implementation is *not* prone to the race found in the previous version of this function.

Quoted 5 lines

But it makes the code more complex:

2 files changed, 28 insertions(+), 16 deletions(-)

without an in-tree use case.

It does help bulk-zoned, e. g. on a device-mapper table change that grows the pool.

~Vitaly

maintainer
#11 lore raw
Quoted 30 lines · Vitaly Wool
Quoted 30 lines · Vlastimil Babka (SUSE)
Quoted 24 lines · Vitaly Wool
Quoted 12 lines · Harry Yoo
Quoted 7 lines · Vitaly Wool
Quoted 6 lines
Quoted 3 lines

How about we take my patch [1] instead since
it isn't prone to this bug and is a nice optimization to the existing mempool_resize() at the same time?

It is not a good idea to squeeze a bug fix and an optimization into one patch.

Quoted 7 lines
Quoted 2 lines

Generally speaking, yes. But that patch had been submitted before the fix popped up, so there was no sqeezing per se.

I mean the patch you've submitted does two things (wihch is not good).

It should be split into two patches that 1) fixes the bug and 2) optimizes it.

I don’t think this is a fair statement. I would say that the patch I’ve submitted improves mempool_resize() to be rid of unnecessary reallocations and data copying, and that (improved) implementation is *not* prone to the race found in the previous version of this function.

Quoted 8 lines
Quoted 5 lines

But it makes the code more complex:

2 files changed, 28 insertions(+), 16 deletions(-)

without an in-tree use case.

It does help bulk-zoned, e. g. on a device-mapper table change that grows the pool.

Please provide data, rather than a theory without data.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#12 lore raw
In reply to Harry Yoo
Quoted 36 lines · Harry Yoo

[+Cc Christoph Hellwig and Vitaly Wool ]

Quoted 33 lines · Nickolai Zeldovich

mempool_resize() shrinks a pool by dropping pool->lock around every free_fn() call and, once the pool holds no more than new_min_nr elements, stores new_min_nr into pool->min_nr. new_min_nr was only compared against pool->min_nr before the lock was first dropped, so that store assumes nothing changed meanwhile. The kernel-doc allows mempool_alloc()/mempool_free() to run while a resize is in progress and the grow path also copes with concurrent resizes ("Raced, other resize will do our work"), but the shrink path does not: while its lock is dropped another resize may shrink the pool below new_min_nr and a third one may grow it again to a size between the two, replacing pool->elements with an array of exactly that size. Storing the stale new_min_nr afterwards leaves pool->min_nr larger than the elements array.

Interleaving on a pool with min_nr = curr_nr = 10:

CPU A: mempool_resize(pool, 8)
takes the lock, 8 <= 10 so it shrinks: remove_element(), drops the lock, calls free_fn() and is preempted there
CPU B: mempool_resize(pool, 3)
completes: curr_nr = 3, min_nr = 3, elements still has 10 slots
CPU B: mempool_resize(pool, 5)
completes: elements = kmalloc_objs(5), min_nr = 5, curr_nr = 5
CPU A: retakes the lock, 8 < curr_nr (5) is false, stores min_nr = 8
while elements has 5 slots

Every later mempool_free() sees curr_nr < min_nr and add_element() stores the returned element at elements[5], [6] and [7], past the end of the 5-slot kmalloc allocation: a heap out-of-bounds write, i.e. slab corruption, a KASAN report, or a crash in whichever unrelated object sits next to the array. The invariant that breaks is that pool->min_nr never exceeds the size of pool->elements; every other path that changes min_nr or elements keeps it.

Instead of reading all this, it was simpler for me to check what the code does.

Quoted 9 lines
Quoted 9 lines

An in-tree caller can hit this: fs/smb/client resizes the global cifs_req_poolp from cifs_demultiplex_thread() (grow, when a TCP session is created) and from clean_demultiplex_info() (shrink, when one goes away). The two are coordinated only through the atomic tcpSesAllocCount, so mounts and unmounts racing on one client run the resizes above concurrently; pool->free is kmem_cache_free() there and the window is a preemption inside it. The other callers (block/blk-zoned.c, drivers/s390/scsi/zfcp_erp.c) serialise their resizes.

This is useful to know, but maybe could be also less dense.

Quoted 3 lines

The bug looks valid in theory, not sure if how easily it can be reproduced in practice as resizing should be quite rare.
(A bug is a bug and should be fixed, but no need to hurry in this case)

Ack.

Quoted 4 lines
Quoted 3 lines

The shrink path has stored new_min_nr unconditionally since mempool_resize() was introduced, while the grow path has always re-checked pool->min_nr after reacquiring the lock.

Quoted 12 lines

So it's Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") ?

https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/?id=6892188b54d484098b79b0c9813c20e543124e2d

...from a historical commit before git era, 1da177e4c3f4.

Quoted 6 lines

Re-check new_min_nr against pool->min_nr under the lock before storing it. If a concurrent resize has shrunk the pool below new_min_nr in the meantime, keep that resize's min_nr (and whatever array it installed), as the grow path already does; otherwise the store is unchanged. The elements the loop has already freed stay freed, which is right either way because the pool's limit is now below new_min_nr.

Again too verbose IMHO.

Quoted 7 lines
Quoted 7 lines

Found by formal verification of mm/mempool.c against the kernel binary. Reproduced with a userspace build of the affected functions in which the free_fn() callback drives the interleaving above: before the fix min_nr ends at 8 with a 5-slot array and the next mempool_free() writes past it (AddressSanitizer heap-buffer-overflow); after the fix min_nr stays at 5 and the element is handed to free_fn(). Not reproduced on a running kernel, where the window is a single preemption inside free_fn().

Alright.

Quoted 7 lines
Quoted 7 lines
Assisted-by LLM

---

 mm/mempool.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 11 lines

mm/mempool.c int mempool_resize(struct mempool *pool, int new_min_nr) · line 382

Quoted 11 lines
mm/mempool.c +2 -1
index cb74e718b2c6..95533e2975ce 100644
@@ -378,7 +378,9 @@ int mempool_resize(struct mempool *pool, int new_min_nr)
 			pool->free(element, pool->pool_data);
 			spin_lock_irqsave(&pool->lock, flags);
 		}
-		pool->min_nr = new_min_nr;
+		/* Raced, the array may now be smaller than new_min_nr */
+		if (new_min_nr <= pool->min_nr)

There could be just '<' ?
The outer if uses <= but the == case only leads to an early goto out_unlock.

Quoted 10 lines
Quoted 9 lines
mm/mempool.c +1 -0
+			pool->min_nr = new_min_nr;
 		goto out_unlock;
 	}
 	spin_unlock_irqrestore(&pool->lock, flags);

base-commit: 90201351a862182bdabfc1ad1c97b1e1b6ed5931

Signature · 1 line
2.53.0
maintainer
#13 lore raw

I refined the commit message a bit per Vlastimil's feedback...

Quoted 6 lines · Vlastimil Babka (SUSE)
Quoted 5 lines · Harry Yoo
Quoted 5 lines

---

 mm/mempool.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 14 lines
Quoted 11 lines

mm/mempool.c int mempool_resize(struct mempool *pool, int new_min_nr) · line 382

Quoted 11 lines
mm/mempool.c +2 -1
index cb74e718b2c6..95533e2975ce 100644
@@ -378,7 +378,9 @@ int mempool_resize(struct mempool *pool, int new_min_nr)
 			pool->free(element, pool->pool_data);
 			spin_lock_irqsave(&pool->lock, flags);
 		}
-		pool->min_nr = new_min_nr;
+		/* Raced, the array may now be smaller than new_min_nr */
+		if (new_min_nr <= pool->min_nr)

There could be just '<' ?
The outer if uses <= but the == case only leads to an early goto out_unlock.

However I didn't adjust this. the end result:

Quoted 20 lines

mempool: fix min_nr exceeding the elements array after concurrent resizes

When mempool_resize() shrinks a pool, it releases and re-acquires pool->lock for every pool->free() call until the pool holds no more than new_min_nr elements, and then updates pool->min_nr to new_min_nr.

The shrink path has stored new_min_nr unconditionally since a historical commit 6892188b54d4 ("[PATCH] Re: [patch] change mempool to not alter managed elements"). However, unconditionally updating pool->min_nr is incorrect when a concurrent mempool_resize() has shrunk the pool size below new_min_nr.

Re-check new_min_nr against pool->min_nr under the lock and update pool->min_nr only if new_min_nr is not larger than pool->min_nr.

This was discovered by formal verification of mm/mempool.c against the kernel binary. While reproducing it in the kernel has not been successful, in a userspace implementation of mempool, ASAN was able to detect a heap buffer overflow.

Quoted 9 lines

In theory, an in-tree caller can hit this: fs/smb/client resizes the global cifs_req_poolp from cifs_demultiplex_thread() (grow, when a TCP session is created) and from clean_demultiplex_info() (shrink, when one goes away). The two are coordinated only through the atomic tcpSesAllocCount, so mounts and unmounts racing on one client run the resizes above concurrently; pool->free is kmem_cache_free() there and the window is a preemption inside it. The other callers (block/blk-zoned.c, drivers/s390/scsi/zfcp_erp.c) serialise their resizes.

[+Cc folks in COMMON INTERNET FILE SYSTEM CLIENT (CIFS and SMB3)
as the paragraph above mentions it. ]

Quoted 2 lines
Assisted-by LLM
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

Here I'm bit hesitant to add Cc: stable for a 24-years-old theoretical race that hasn't been reproduced in the kernel. But it would be easy to backport if anybody complains.

Quoted 6 lines

This will be queued to slab/for-next-fixes soon.

Thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer Applied
#15 lore raw
In reply to Nickolai Zeldovich
Quoted 14 lines · Nickolai Zeldovich

mempool_resize() shrinks a pool by dropping pool->lock around every free_fn() call and, once the pool holds no more than new_min_nr elements, stores new_min_nr into pool->min_nr. new_min_nr was only compared against pool->min_nr before the lock was first dropped, so that store assumes nothing changed meanwhile. The kernel-doc allows mempool_alloc()/mempool_free() to run while a resize is in progress and the grow path also copes with concurrent resizes ("Raced, other resize will do our work"), but the shrink path does not: while its lock is dropped another resize may shrink the pool below new_min_nr and a third one may grow it again to a size between the two, replacing pool->elements with an array of exactly that size. Storing the stale new_min_nr afterwards leaves pool->min_nr larger than the elements array.

Quoted 1 line

[...]

Applied to mm/slab.git slab/for-next-fixes, thanks!

[1/1] mempool: fix min_nr exceeding the elements array after concurrent resizes

      commit: 8dde7b06d2d819422c736dad7efb9635b6673cc2
Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer
#16 lore raw
Quoted 15 lines · Harry Yoo (Meta)
Quoted 14 lines · Nickolai Zeldovich

mempool_resize() shrinks a pool by dropping pool->lock around every free_fn() call and, once the pool holds no more than new_min_nr elements, stores new_min_nr into pool->min_nr. new_min_nr was only compared against pool->min_nr before the lock was first dropped, so that store assumes nothing changed meanwhile. The kernel-doc allows mempool_alloc()/mempool_free() to run while a resize is in progress and the grow path also copes with concurrent resizes ("Raced, other resize will do our work"), but the shrink path does not: while its lock is dropped another resize may shrink the pool below new_min_nr and a third one may grow it again to a size between the two, replacing pool->elements with an array of exactly that size. Storing the stale new_min_nr afterwards leaves pool->min_nr larger than the elements array.

Quoted 6 lines
Quoted 1 line

[...]

Applied to mm/slab.git slab/for-next-fixes, thanks!

[1/1] mempool: fix min_nr exceeding the elements array after concurrent resizes

      commit: 8dde7b06d2d819422c736dad7efb9635b6673cc2

For the record, I rerouted this from slab/for-next-fixes to slab/for-next as it's not targeting -stable.

mempool: mempool: fix min_nr exceeding the elements array after concurrent resizes
commit: ce6518e61e3b4bf4f97f7ac6204049b9eba7eb75

Signature · 2 lines
Cheers,
Harry / Hyeonggon