mm: protect against concurrent vma expansion
Subsystems (2)
PATCH 1/1 mm: protect against concurrent vma expansion
expand_stack() runs with a shared mmap_sem lock. Because of this, there could be multiple concurrent stack expansions in the same mm, which may cause problems in the vma gap update code.
I propose to solve this by taking the mm->page_table_lock around such vma expansions, in order to avoid the concurrency issue. We only have to worry about concurrent expand_stack() calls here, since we hold a shared mmap_sem lock and all vma modificaitons other than expand_stack() are done under an exclusive mmap_sem lock.
I previously tried to achieve the same effect by making sure all growable vmas in a given mm would share the same anon_vma, which we already lock here. However this turned out to be difficult - all of the schemes I tried for refcounting the growable anon_vma and clearing turned out ugly. So, I'm now proposing only the minimal fix.
--- mm/mmap.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
mm/mmap.c +14 -0
@@ -2069,6 +2069,11 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)if (vma->vm_pgoff + (size >>PAGE_SHIFT ) >= vma->vm_pgoff) { error =acct_stack_growth (vma, size, grow);if (!error) { +/* +* page_table_lock to protect against +* concurrent vma expansions +*/ +spin_lock (&vma->vm_mm->page_table_lock);anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;anon_vma_interval_tree_post_update_vma (vma);
@@ -2076,6 +2081,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)vma_gap_update (vma->vm_next); else vma->vm_mm->highest_vm_end = address; +spin_unlock (&vma->vm_mm->page_table_lock); +perf_event_mmap (vma); } }
@@ -2126,11 +2133,18 @@ int expand_downwards(struct vm_area_struct *vma,if (grow <= vma->vm_pgoff) { error =acct_stack_growth (vma, size, grow);if (!error) { +/* +* page_table_lock to protect against +* concurrent vma expansions +*/ +spin_lock (&vma->vm_mm->page_table_lock);anon_vma_interval_tree_pre_update_vma (vma); vma->vm_start = address; vma->vm_pgoff -= grow;anon_vma_interval_tree_post_update_vma (vma);vma_gap_update (vma); +spin_unlock (&vma->vm_mm->page_table_lock); +perf_event_mmap (vma); } }
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 10 lines · On Fri, 30 Nov 2012 22:56:27 -0800 Michel Lespinasse <[email protected]> wrote:
expand_stack()runs with a sharedmmap_semlock. Because of this, there could be multiple concurrent stack expansions in the same mm, which may cause problems in the vma gap update code.I propose to solve this by taking the
mm->page_table_lockaround such vma expansions, in order to avoid the concurrency issue. We only have to worry about concurrentexpand_stack()calls here, since we hold a sharedmmap_semlock and all vma modificaitons other thanexpand_stack()are done under an exclusivemmap_semlock.
Quoted 6 lines
I previously tried to achieve the same effect by making sure all growable vmas in a given mm would share the same
anon_vma, which we already lock here. However this turned out to be difficult - all of the schemes I tried for refcounting the growableanon_vmaand clearing turned out ugly. So, I'm now proposing only the minimal fix.
I think I don't understand the problem fully. Let me demonstrate:
a) vma_lock_anon_vma() doesn't take a lock which is specific to "this" anon_vma. It takes anon_vma->root->mutex. That mutex is shared with vma->vm_next, yes? If so, we have no problem here? (which makes me suspect that the races lies other than where I think it lies).
b) I can see why a broader lock is needed in expand_upwards(): it plays with a different vma: vma->vm_next. But expand_downwards() doesn't do that - it only alters "this" vma. So I'd have thought that vma_lock_anon_vma("this" vma) would be sufficient.
What are the performance costs of this change?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 19 lines · Andrew Morton
Quoted 10 lines · On Fri, 30 Nov 2012 22:56:27 -0800 Michel Lespinasse <[email protected]> wrote:
expand_stack()runs with a sharedmmap_semlock. Because of this, there could be multiple concurrent stack expansions in the same mm, which may cause problems in the vma gap update code.I propose to solve this by taking the
mm->page_table_lockaround such vma expansions, in order to avoid the concurrency issue. We only have to worry about concurrentexpand_stack()calls here, since we hold a sharedmmap_semlock and all vma modificaitons other thanexpand_stack()are done under an exclusivemmap_semlock.Quoted 5 lines
I previously tried to achieve the same effect by making sure all growable vmas in a given mm would share the same
anon_vma, which we already lock here. However this turned out to be difficult - all of the schemes I tried for refcounting the growableanon_vmaand clearing turned out ugly. So, I'm now proposing only the minimal fix.
Quoted 7 lines
I think I don't understand the problem fully. Let me demonstrate:
a)
vma_lock_anon_vma()doesn't take a lock which is specific to "this"anon_vma. It takesanon_vma->root->mutex. That mutex is shared withvma->vm_next, yes? If so, we have no problem here? (which makes me suspect that the races lies other than where I think it lies).
So, the first thing I need to mention is that this fix is NOT for any problem that has been reported (and in particular, not for Sasha's trinity fuzzing issue). It's just me looking at the code and noticing I haven't gotten locking right for the case of concurrent stack expansion.
Regarding vma and vma->vm_next sharing the same root anon_vma mutex - this will often be the case, but not always. find_mergeable_anon_vma() will try to make it so, but it could fail if there was another vma in-between at the time the stack's anon_vmas got assigned (either a non-stack vma that later gets unmapped, or another stack vma that didn't get its own anon_vma assigned yet).
Quoted 4 lines
b) I can see why a broader lock is needed in
expand_upwards(): it plays with a different vma:vma->vm_next. Butexpand_downwards()doesn't do that - it only alters "this" vma. So I'd have thought thatvma_lock_anon_vma("this" vma)would be sufficient.
The issue there is that vma_gap_update() accesses vma->vm_prev, so the issue is actually symetrical with expand_upwards().
Quoted 1 line
What are the performance costs of this change?
It's expected to be small. glibc doesn't use expandable stacks for the threads it creates, so having multiple growable stacks is actually uncommon (another reason why the problem hasn't been observed in practice). Because of this, I don't expect the page table lock to get bounced between threads, so the cost of taking it should be small (compared to the cost of delivering the #PF, let alone handling it in software).
But yes, the initial idea of forcing all growable vmas in an mm to share the same root anon_vma sounded much more appealing at first. Unfortunately I haven't been able to make that work in a simple enough way to be comfortable submitting it this late in the release cycle :/
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 59 lines · On Mon, 3 Dec 2012 16:35:01 -0800 Michel Lespinasse <[email protected]> wrote:
Quoted 19 lines · Andrew Morton
Quoted 10 lines · On Fri, 30 Nov 2012 22:56:27 -0800 Michel Lespinasse <[email protected]> wrote:
expand_stack()runs with a sharedmmap_semlock. Because of this, there could be multiple concurrent stack expansions in the same mm, which may cause problems in the vma gap update code.I propose to solve this by taking the
mm->page_table_lockaround such vma expansions, in order to avoid the concurrency issue. We only have to worry about concurrentexpand_stack()calls here, since we hold a sharedmmap_semlock and all vma modificaitons other thanexpand_stack()are done under an exclusivemmap_semlock.Quoted 5 lines
I previously tried to achieve the same effect by making sure all growable vmas in a given mm would share the same
anon_vma, which we already lock here. However this turned out to be difficult - all of the schemes I tried for refcounting the growableanon_vmaand clearing turned out ugly. So, I'm now proposing only the minimal fix.Quoted 7 lines
I think I don't understand the problem fully. Let me demonstrate:
a)
vma_lock_anon_vma()doesn't take a lock which is specific to "this"anon_vma. It takesanon_vma->root->mutex. That mutex is shared withvma->vm_next, yes? If so, we have no problem here? (which makes me suspect that the races lies other than where I think it lies).So, the first thing I need to mention is that this fix is NOT for any problem that has been reported (and in particular, not for Sasha's trinity fuzzing issue). It's just me looking at the code and noticing I haven't gotten locking right for the case of concurrent stack expansion.
Regarding vma and
vma->vm_nextsharing the same rootanon_vmamutex - this will often be the case, but not always.find_mergeable_anon_vma()will try to make it so, but it could fail if there was another vma in-between at the time the stack'sanon_vmasgot assigned (either a non-stack vma that later gets unmapped, or another stack vma that didn't get its ownanon_vmaassigned yet).Quoted 4 lines
b) I can see why a broader lock is needed in
expand_upwards(): it plays with a different vma:vma->vm_next. Butexpand_downwards()doesn't do that - it only alters "this" vma. So I'd have thought thatvma_lock_anon_vma("this" vma)would be sufficient.The issue there is that
vma_gap_update()accessesvma->vm_prev, so the issue is actually symetrical withexpand_upwards().Quoted 1 line
What are the performance costs of this change?
It's expected to be small. glibc doesn't use expandable stacks for the threads it creates, so having multiple growable stacks is actually uncommon (another reason why the problem hasn't been observed in practice). Because of this, I don't expect the page table lock to get bounced between threads, so the cost of taking it should be small (compared to the cost of delivering the #PF, let alone handling it in software).
Quoted 4 lines
But yes, the initial idea of forcing all growable vmas in an mm to share the same root
anon_vmasounded much more appealing at first. Unfortunately I haven't been able to make that work in a simple enough way to be comfortable submitting it this late in the release cycle :/
hm, OK. Could you please cook up a new changelog which explains these things to the next puzzled reader and send it along?
Ingo is playing in the same area with "mm/rmap: Convert the struct anon_vma::mutex to an rwsem", but as that patch changes vma_lock_anon_vma() to use down_write(), I expect it won't affect anything. But please check it over.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
expand_stack() runs with a shared mmap_sem lock. Because of this, there could be multiple concurrent stack expansions in the same mm, which may cause problems in the vma gap update code.
I propose to solve this by taking the mm->page_table_lock around such vma expansions, in order to avoid the concurrency issue. We only have to worry about concurrent expand_stack() calls here, since we hold a shared mmap_sem lock and all vma modificaitons other than expand_stack() are done under an exclusive mmap_sem lock.
I previously tried to achieve the same effect by making sure all growable vmas in a given mm would share the same anon_vma, which we already lock here. However this turned out to be difficult - all of the schemes I tried for refcounting the growable anon_vma and clearing turned out ugly. So, I'm now proposing only the minimal fix.
The overhead of taking the page table lock during stack expansion is expected to be small: glibc doesn't use expandable stacks for the threads it creates, so having multiple growable stacks is actually uncommon and we don't expect the page table lock to get bounced between threads.
--- mm/mmap.c | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-)
mm/mmap.c +28 -0
@@ -2069,6 +2069,18 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)if (vma->vm_pgoff + (size >>PAGE_SHIFT ) >= vma->vm_pgoff) { error =acct_stack_growth (vma, size, grow);if (!error) { +/* +* vma_gap_update() doesn't support concurrent +* updates, but we only hold a shared mmap_sem +* lock here, so we need to protect against +* concurrent vma expansions. +* vma_lock_anon_vma() doesn't help here, as +* we don't guarantee that all growable vmas +* in a mm share the same root anon vma. +* So, we reuse mm->page_table_lock to guard +* against concurrent vma expansions. +*/ +spin_lock (&vma->vm_mm->page_table_lock);anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;anon_vma_interval_tree_post_update_vma (vma);
@@ -2076,6 +2088,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)vma_gap_update (vma->vm_next); else vma->vm_mm->highest_vm_end = address; +spin_unlock (&vma->vm_mm->page_table_lock); +perf_event_mmap (vma); } }
@@ -2126,11 +2140,25 @@ int expand_downwards(struct vm_area_struct *vma,if (grow <= vma->vm_pgoff) { error =acct_stack_growth (vma, size, grow);if (!error) { +/* +* vma_gap_update() doesn't support concurrent +* updates, but we only hold a shared mmap_sem +* lock here, so we need to protect against +* concurrent vma expansions. +* vma_lock_anon_vma() doesn't help here, as +* we don't guarantee that all growable vmas +* in a mm share the same root anon vma. +* So, we reuse mm->page_table_lock to guard +* against concurrent vma expansions. +*/ +spin_lock (&vma->vm_mm->page_table_lock);anon_vma_interval_tree_pre_update_vma (vma); vma->vm_start = address; vma->vm_pgoff -= grow;anon_vma_interval_tree_post_update_vma (vma);vma_gap_update (vma); +spin_unlock (&vma->vm_mm->page_table_lock); +perf_event_mmap (vma); } }
Signature · 1 line
1.7.7.3
Quoted 9 lines · Michel Lespinasse
expand_stack()runs with a sharedmmap_semlock. Because of this, there could be multiple concurrent stack expansions in the same mm, which may cause problems in the vma gap update code.I propose to solve this by taking the
mm->page_table_lockaround such vma expansions, in order to avoid the concurrency issue. We only have to worry about concurrentexpand_stack()calls here, since we hold a sharedmmap_semlock and all vma modificaitons other thanexpand_stack()are done under an exclusivemmap_semlock.
Hi Michel and Andrew,
One question.
I found that mainly callsite of expand_stack() is #PF, but it holds mmap_sem each time before call expand_stack(), how can hold a *shared* mmap_sem happen?
Quoted 67 lines
I previously tried to achieve the same effect by making sure all growable vmas in a given mm would share the same
anon_vma, which we already lock here. However this turned out to be difficult - all of the schemes I tried for refcounting the growableanon_vmaand clearing turned out ugly. So, I'm now proposing only the minimal fix.The overhead of taking the page table lock during stack expansion is expected to be small: glibc doesn't use expandable stacks for the threads it creates, so having multiple growable stacks is actually uncommon and we don't expect the page table lock to get bounced between threads.
Signed-off-by Michel Lespinasse <[email protected]>--- mm/mmap.c | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-)mm/mmap.c +26
-0@@ -2069,6 +2069,18 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)if (vma->vm_pgoff + (size >>PAGE_SHIFT ) >= vma->vm_pgoff) { error =acct_stack_growth (vma, size, grow);if (!error) { +/* +* vma_gap_update() doesn't support concurrent +* updates, but we only hold a shared mmap_sem +* lock here, so we need to protect against +* concurrent vma expansions. +* vma_lock_anon_vma() doesn't help here, as +* we don't guarantee that all growable vmas +* in a mm share the same root anon vma. +* So, we reuse mm->page_table_lock to guard +* against concurrent vma expansions. +*/ +spin_lock (&vma->vm_mm->page_table_lock);anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;anon_vma_interval_tree_post_update_vma (vma);@@ -2076,6 +2088,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)vma_gap_update (vma->vm_next); else vma->vm_mm->highest_vm_end = address; +spin_unlock (&vma->vm_mm->page_table_lock); +perf_event_mmap (vma); } }@@ -2126,11 +2140,25 @@ int expand_downwards(struct vm_area_struct *vma,if (grow <= vma->vm_pgoff) { error =acct_stack_growth (vma, size, grow);if (!error) { +/* +* vma_gap_update() doesn't support concurrent +* updates, but we only hold a shared mmap_sem +* lock here, so we need to protect against +* concurrent vma expansions. +* vma_lock_anon_vma() doesn't help here, as +* we don't guarantee that all growable vmas +* in a mm share the same root anon vma. +* So, we reuse mm->page_table_lock to guard +* against concurrent vma expansions. +*/ +spin_lock (&vma->vm_mm->page_table_lock);
mm/mmap.c int expand_downwards(struct vm_area_struct *vma, · line 2164
Quoted 10 lines
mm/mmap.c +2
-0anon_vma_interval_tree_pre_update_vma (vma); vma->vm_start = address; vma->vm_pgoff -= grow;anon_vma_interval_tree_post_update_vma (vma);vma_gap_update (vma); +spin_unlock (&vma->vm_mm->page_table_lock); +perf_event_mmap (vma); } }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hi Simon,
Quoted 5 lines · Simon Jeons
One question.
I found that mainly callsite of
expand_stack()is #PF, but it holdsmmap_semeach time before callexpand_stack(), how can hold a *shared*mmap_semhappen?
the #PF handler calls down_read(&mm->mmap_sem) before calling expand_stack.
I think I'm just confusing you with my terminology; shared lock == read lock == several readers might hold it at once (I'd say they share it)
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Michel Lespinasse
Hi Simon,
Quoted 5 lines · Simon Jeons
One question.
I found that mainly callsite of
expand_stack()is #PF, but it holdsmmap_semeach time before callexpand_stack(), how can hold a *shared*mmap_semhappen?
Quoted 5 lines
the #PF handler calls
down_read(&mm->mmap_sem)before callingexpand_stack.I think I'm just confusing you with my terminology; shared lock == read lock == several readers might hold it at once (I'd say they share it)
Sorry for my late response.
Since expand_stack() will modify vma, then why hold a read lock here?
Quoted 1 line
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 12 lines · Simon Jeons
Quoted 11 lines · Michel Lespinasse
Hi Simon,
Quoted 5 lines · Simon Jeons
One question.
I found that mainly callsite of
expand_stack()is #PF, but it holdsmmap_semeach time before callexpand_stack(), how can hold a *shared*mmap_semhappen?the #PF handler calls
down_read(&mm->mmap_sem)before callingexpand_stack.
Quoted 7 lines
Quoted 3 lines
I think I'm just confusing you with my terminology; shared lock == read lock == several readers might hold it at once (I'd say they share it)
Sorry for my late response.
Since
expand_stack()will modify vma, then why hold a read lock here?
Well, it'd be much nicer if we had a write lock, I think. But, we didn't know when taking the lock that we'd end up having to expand stacks.
What happens is that page faults don't generally modify vmas, so they get a read lock (just to know what vma the fault is happening in) and then fault in the page.
expand_stack() is the one exception to that - after getting the read lock as usual, we notice that the fault is not in any vma right now, but it's close enough to an expandable vma.
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 21 lines · Michel Lespinasse
Quoted 12 lines · Simon Jeons
Quoted 11 lines · Michel Lespinasse
Hi Simon,
Quoted 5 lines · Simon Jeons
One question.
I found that mainly callsite of
expand_stack()is #PF, but it holdsmmap_semeach time before callexpand_stack(), how can hold a *shared*mmap_semhappen?the #PF handler calls
down_read(&mm->mmap_sem)before callingexpand_stack.Quoted 7 lines
Quoted 3 lines
I think I'm just confusing you with my terminology; shared lock == read lock == several readers might hold it at once (I'd say they share it)
Sorry for my late response.
Since
expand_stack()will modify vma, then why hold a read lock here?
Quoted 8 lines
Well, it'd be much nicer if we had a write lock, I think. But, we didn't know when taking the lock that we'd end up having to expand stacks.
What happens is that page faults don't generally modify vmas, so they get a read lock (just to know what vma the fault is happening in) and then fault in the page.
Thanks for your quick explanation.
Quoted 3 lines
expand_stack()is the one exception to that - after getting the read lock as usual, we notice that the fault is not in any vma right now, but it's close enough to an expandable vma.
If this senario only occur for userspace stack?
Quoted 1 line
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 12 lines · Simon Jeons
Quoted 11 lines · Michel Lespinasse
Hi Simon,
Quoted 5 lines · Simon Jeons
One question.
I found that mainly callsite of
expand_stack()is #PF, but it holdsmmap_semeach time before callexpand_stack(), how can hold a *shared*mmap_semhappen?the #PF handler calls
down_read(&mm->mmap_sem)before callingexpand_stack.
Quoted 7 lines
Quoted 3 lines
I think I'm just confusing you with my terminology; shared lock == read lock == several readers might hold it at once (I'd say they share it)
Sorry for my late response.
Since
expand_stack()will modify vma, then why hold a read lock here?
To prevent that vma being ripped out.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>