mempool: fix min_nr exceeding the elements array after concurrent resizes
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().
---
mm/mempool.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/mempool.c +3 -1
@@ -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
Harry Yoo
maintainer[+Cc Christoph Hellwig and Vitaly Wool ]
Quoted 34 lines · Nickolai Zeldovich
mempool_resize()shrinks a pool by droppingpool->lockaround everyfree_fn()call and, once the pool holds no more thannew_min_nrelements, storesnew_min_nrintopool->min_nr.new_min_nrwas only compared againstpool->min_nrbefore 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 belownew_min_nrand a third one may grow it again to a size between the two, replacingpool->elementswith an array of exactly that size. Storing the stalenew_min_nrafterwards leavespool->min_nrlarger 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, callsfree_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, storesmin_nr= 8
while elements has 5 slotsEvery later
mempool_free()seescurr_nr<min_nrandadd_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 thatpool->min_nrnever exceeds the size ofpool->elements; every other path that changesmin_nror elements keeps it.
Quoted 9 lines
An in-tree caller can hit this:
fs/smb/clientresizes the globalcifs_req_poolpfromcifs_demultiplex_thread()(grow, when a TCP session is created) and fromclean_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->freeiskmem_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_nrunconditionally sincemempool_resize()was introduced, while the grow path has always re-checkedpool->min_nrafter reacquiring the lock.
So it's Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") ?
...from a historical commit before git era, 1da177e4c3f4.
Quoted 42 lines
Re-check
new_min_nragainstpool->min_nrunder the lock before storing it. If a concurrent resize has shrunk the pool belownew_min_nrin the meantime, keep that resize'smin_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 belownew_min_nr.Found by formal verification of
mm/mempool.cagainst the kernel binary. Reproduced with a userspace build of the affected functions in which thefree_fn()callback drives the interleaving above: before the fixmin_nrends at 8 with a 5-slot array and the nextmempool_free()writes past it (AddressSanitizer heap-buffer-overflow); after the fixmin_nrstays at 5 and the element is handed tofree_fn(). Not reproduced on a running kernel, where the window is a single preemption insidefree_fn().Assisted-by LLMSigned-off-by Nickolai Zeldovich <[email protected]>---
mm/mempool.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)mm/mempool.c +3
-1@@ -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:
90201351a862182bdabfc1ad1c97b1e1b6ed5931Signature · 1 line
2.53.0
Signature · 2 lines
Cheers, Harry / Hyeonggon
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.
Quoted 1 line
[+Cc Christoph Hellwig and Vitaly Wool ]
Thanks for CC'ing me on this. 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?
[1] https://lore.kernel.org/linux-mm/[email protected]/
~Vitaly
Harry Yoo
maintainerQuoted 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 existingmempool_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
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 existingmempool_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
Harry Yoo
maintainerQuoted 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 existingmempool_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
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 existingmempool_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
Harry Yoo
maintainerQuoted 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 existingmempool_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
Vlastimil Babka (SUSE)
maintainerQuoted 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 existingmempool_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
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 existingmempool_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
Harry Yoo
maintainerQuoted 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 existingmempool_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
Vlastimil Babka (SUSE)
maintainerQuoted 36 lines · Harry Yoo
[+Cc Christoph Hellwig and Vitaly Wool ]
Quoted 33 lines · Nickolai Zeldovich
mempool_resize()shrinks a pool by droppingpool->lockaround everyfree_fn()call and, once the pool holds no more thannew_min_nrelements, storesnew_min_nrintopool->min_nr.new_min_nrwas only compared againstpool->min_nrbefore 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 belownew_min_nrand a third one may grow it again to a size between the two, replacingpool->elementswith an array of exactly that size. Storing the stalenew_min_nrafterwards leavespool->min_nrlarger 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, callsfree_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, storesmin_nr= 8
while elements has 5 slotsEvery later
mempool_free()seescurr_nr<min_nrandadd_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 thatpool->min_nrnever exceeds the size ofpool->elements; every other path that changesmin_nror 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/clientresizes the globalcifs_req_poolpfromcifs_demultiplex_thread()(grow, when a TCP session is created) and fromclean_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->freeiskmem_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_nrunconditionally sincemempool_resize()was introduced, while the grow path has always re-checkedpool->min_nrafter reacquiring the lock.
Quoted 12 lines
So it's Fixes:
1da177e4c3f4("Linux-2.6.12-rc2") ?...from a historical commit before git era,
1da177e4c3f4.Quoted 6 lines
Re-check
new_min_nragainstpool->min_nrunder the lock before storing it. If a concurrent resize has shrunk the pool belownew_min_nrin the meantime, keep that resize'smin_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 belownew_min_nr.
Again too verbose IMHO.
Quoted 7 lines
Quoted 7 lines
Found by formal verification of
mm/mempool.cagainst the kernel binary. Reproduced with a userspace build of the affected functions in which thefree_fn()callback drives the interleaving above: before the fixmin_nrends at 8 with a 5-slot array and the nextmempool_free()writes past it (AddressSanitizer heap-buffer-overflow); after the fixmin_nrstays at 5 and the element is handed tofree_fn(). Not reproduced on a running kernel, where the window is a single preemption insidefree_fn().
Alright.
Quoted 7 lines
Quoted 7 lines
Assisted-by LLMSigned-off-by Nickolai Zeldovich <[email protected]>---
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@@ -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:
90201351a862182bdabfc1ad1c97b1e1b6ed5931Signature · 1 line
2.53.0
Harry Yoo
maintainerI 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@@ -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 gotoout_unlock.
However I didn't adjust this. the end result:
Quoted 20 lines
mempool: fix
min_nrexceeding the elements array after concurrent resizesWhen
mempool_resize()shrinks a pool, it releases and re-acquirespool->lockfor everypool->free()call until the pool holds no more thannew_min_nrelements, and then updatespool->min_nrtonew_min_nr.The shrink path has stored
new_min_nrunconditionally since a historical commit6892188b54d4("[PATCH] Re: [patch] change mempool to not alter managed elements"). However, unconditionally updatingpool->min_nris incorrect when a concurrentmempool_resize()has shrunk the pool size belownew_min_nr.Re-check
new_min_nragainstpool->min_nrunder the lock and updatepool->min_nronly ifnew_min_nris not larger thanpool->min_nr.This was discovered by formal verification of
mm/mempool.cagainst 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/clientresizes the globalcifs_req_poolpfromcifs_demultiplex_thread()(grow, when a TCP session is created) and fromclean_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->freeiskmem_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 LLMFixes 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
Signed-off-by Nickolai Zeldovich <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>[ [email protected]: refine changelog per Vlastimil's comment ]Signed-off-by Harry Yoo (Meta) <[email protected]>
This will be queued to slab/for-next-fixes soon.
Thanks!
Signature · 2 lines
Cheers, Harry / Hyeonggon
Christoph Hellwig
Reviewed-byThe patch looks good:
and I'm a bit surprised that someone folks later in the thread still don't get the basic idea of separating fixes from random "improvements".
Quoted 14 lines · Nickolai Zeldovich
mempool_resize()shrinks a pool by droppingpool->lockaround everyfree_fn()call and, once the pool holds no more thannew_min_nrelements, storesnew_min_nrintopool->min_nr.new_min_nrwas only compared againstpool->min_nrbefore 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 belownew_min_nrand a third one may grow it again to a size between the two, replacingpool->elementswith an array of exactly that size. Storing the stalenew_min_nrafterwards leavespool->min_nrlarger 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
Harry Yoo
maintainerQuoted 15 lines · Harry Yoo (Meta)
Quoted 14 lines · Nickolai Zeldovich
mempool_resize()shrinks a pool by droppingpool->lockaround everyfree_fn()call and, once the pool holds no more thannew_min_nrelements, storesnew_min_nrintopool->min_nr.new_min_nrwas only compared againstpool->min_nrbefore 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 belownew_min_nrand a third one may grow it again to a size between the two, replacingpool->elementswith an array of exactly that size. Storing the stalenew_min_nrafterwards leavespool->min_nrlarger than the elements array.
Quoted 6 lines
Quoted 1 line
[...]
Applied to
mm/slab.gitslab/for-next-fixes, thanks![1/1] mempool: fix
min_nrexceeding the elements array after concurrent resizescommit: 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