THP mlock fix
Subsystems (3)
cover THP mlock fix
Hi Andrew,
There are two patches below. I believe either of the would fix the bug reported by Sasha, but it worth applying both.
Sasha, as I cannot trigger the bug, I would like to have your Tested-by.
Kirill A. Shutemov (2):
mm, oom: skip mlocked VMAs in __oom_reap_vmas() mm, thp: clear PG_mlocked when last mapping gone
mm/oom_kill.c | 7 +++++++ mm/rmap.c | 3 +++ 2 files changed, 10 insertions(+)
Signature · 7 lines
2.6.4 -- 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>
PATCH 1/2 mm, oom: skip mlocked VMAs in __oom_reap_vmas()
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
I think this patch would fix this crash:
http://lkml.kernel.org/r/[email protected]
--- mm/oom_kill.c | 7 +++++++ 1 file changed, 7 insertions(+)
mm/oom_kill.c +7 -0
@@ -447,6 +447,13 @@ static bool __oom_reap_vmas(struct mm_struct *mm)continue ;/* +* mlocked VMAs require explicit munlocking before unmap. +* Let's keep it simple here and skip such VMAs. +*/ +if (vma->vm_flags &VM_LOCKED ) +continue ; + +/* * Only anonymous pages have a good chance to be dropped * without additional steps which we cannot afford as we * are OOM already.
Signature · 7 lines
2.6.4 -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 2 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
Very well spotted!
Quoted 2 lines
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
Why cannot we simply munlock them here for the private mappings?
mm/oom_kill.c +4 -1
@@ -456,9 +456,12 @@ static bool __oom_reap_vmas(struct mm_struct *mm)* we do not want to block exit_mmap by keeping mm ref * count elevated without a good reason. */ -if (vma_is_anonymous (vma) || !(vma->vm_flags &VM_SHARED )) +if (vma_is_anonymous (vma) || !(vma->vm_flags &VM_SHARED )) { +if (vma->vm_flags &VM_LOCKED ) +munlock_vma_pages_all (vma);unmap_page_range (&tlb, vma, vma->vm_start, vma->vm_end, &details); + } }tlb_finish_mmu (&tlb,0 ,-1 );up_read (&mm->mmap_sem);
Signature · 8 lines
Michal Hocko SUSE Labs -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 10 lines · Michal Hocko
Quoted 2 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
Very well spotted!
Quoted 2 lines
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
Why cannot we simply munlock them here for the private mappings?
It's probably right think to do, but I wanted to fix the bug first. And I wasn't ready to investigate context the reaper working in to check if it's safe to munlock there. For instance, munlock would take page lock and I'm not sure at the moment if it can or cannot lead to deadlock in some scenario. So I choose safer fix.
If calling munlock is always safe where unmap happens, why not move inside unmap?
Quoted 19 lines
mm/oom_kill.c +4
-1@@ -456,9 +456,12 @@ static bool __oom_reap_vmas(struct mm_struct *mm)* we do not want to block exit_mmap by keeping mm ref * count elevated without a good reason. */ -if (vma_is_anonymous (vma) || !(vma->vm_flags &VM_SHARED )) +if (vma_is_anonymous (vma) || !(vma->vm_flags &VM_SHARED )) { +if (vma->vm_flags &VM_LOCKED ) +munlock_vma_pages_all (vma);unmap_page_range (&tlb, vma, vma->vm_start, vma->vm_end, &details); + } }tlb_finish_mmu (&tlb,0 ,-1 );up_read (&mm->mmap_sem);
Signature · 7 lines
Kirill A. Shutemov -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 5 lines · Kirill A. Shutemov
Quoted 4 lines · Michal Hocko
Quoted 2 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
Quoted 8 lines
Quoted 6 lines
Very well spotted!
Quoted 2 lines
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
Why cannot we simply munlock them here for the private mappings?
It's probably right think to do, but I wanted to fix the bug first.
Fair enough. It is surely simpler, although I think we should tear private mappings down even when mlocked. I can cook up a separate patch on top of yours which is obviously correct and can be folded into the original one.
Quoted 4 lines
And I wasn't ready to investigate context the reaper working in to check if it's safe to munlock there. For instance, munlock would take page lock and I'm not sure at the moment if it can or cannot lead to deadlock in some scenario. So I choose safer fix.
repear is a flat kernel thread context which doesn't sit on any locks (except for mmap sem for read taken on the way) so I do not immediately see any potential for the dead lock. If the original context which wakes it up depend on the page lock to move on then we would be screwed already because we can end up doing exit_mmap in that context already and so end up doing munlock as well.
Quoted 2 lines
If calling munlock is always safe where unmap happens, why not move inside unmap?
This would be less error prone for sure. I would rather see it as a separate patch which explains why it is safe in all cases though.
Signature · 8 lines
Michal Hocko SUSE Labs -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 11 lines · Michal Hocko
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Michal Hocko
Quoted 2 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
Very well spotted!
Quoted 2 lines
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Why cannot we simply munlock them here for the private mappings?
It's probably right think to do, but I wanted to fix the bug first.
Fair enough. It is surely simpler, although I think we should tear private mappings down even when mlocked. I can cook up a separate patch on top of yours which is obviously correct and can be folded into the original one.
I prefer it not to be folded. To be able to revert in something go wrong.
Quoted 11 lines
Quoted 4 lines
And I wasn't ready to investigate context the reaper working in to check if it's safe to munlock there. For instance, munlock would take page lock and I'm not sure at the moment if it can or cannot lead to deadlock in some scenario. So I choose safer fix.
repear is a flat kernel thread context which doesn't sit on any locks (except for mmap sem for read taken on the way) so I do not immediately see any potential for the dead lock. If the original context which wakes it up depend on the page lock to move on then we would be screwed already because we can end up doing
exit_mmapin that context already and so end up doing munlock as well.
Can target process hold page lock? Or a process in direct replaim? Basically, I don't know what I'm talking about ;-P
Quoted 5 lines
Quoted 2 lines
If calling munlock is always safe where unmap happens, why not move inside unmap?
This would be less error prone for sure. I would rather see it as a separate patch which explains why it is safe in all cases though.
I haven't subscribed to implementing this just yet :)
Signature · 7 lines
Kirill A. Shutemov -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 14 lines · Kirill A. Shutemov
Quoted 13 lines · Michal Hocko
Quoted 12 lines · Kirill A. Shutemov
Quoted 10 lines · Michal Hocko
Quoted 2 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
Very well spotted!
Quoted 2 lines
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
Why cannot we simply munlock them here for the private mappings?
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
It's probably right think to do, but I wanted to fix the bug first.
Fair enough. It is surely simpler, although I think we should tear private mappings down even when mlocked. I can cook up a separate patch on top of yours which is obviously correct and can be folded into the original one.
I prefer it not to be folded. To be able to revert in something go wrong.
Sorry, I meant your fixup should be folded. The one to allow munlock as a separate fix.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
And I wasn't ready to investigate context the reaper working in to check if it's safe to munlock there. For instance, munlock would take page lock and I'm not sure at the moment if it can or cannot lead to deadlock in some scenario. So I choose safer fix.
Quoted 8 lines
Quoted 6 lines
repear is a flat kernel thread context which doesn't sit on any locks (except for mmap sem for read taken on the way) so I do not immediately see any potential for the dead lock. If the original context which wakes it up depend on the page lock to move on then we would be screwed already because we can end up doing
exit_mmapin that context already and so end up doing munlock as well.Can target process hold page lock? Or a process in direct replaim?
The target process is the OOM victim. It can be doing anything. Including holding the page lock. But it cannot be holding page lock while doing the allocation or invoking the OOM killer because that would be a deadlock already even without oom reaper in the picture.
Signature · 8 lines
Michal Hocko SUSE Labs -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
In reply to Kirill A. ShutemovQuoted 8 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
I think this patch would fix this crash:
http://lkml.kernel.org/r/[email protected]
Btw, do you happen to have the full log here. OOM reaper can only interfere if there was an OOM killer invoked.
Signature · 8 lines
Michal Hocko SUSE Labs -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 4 lines · Michal Hocko
Quoted 3 lines · Kirill A. Shutemov
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
Quoted 8 lines
Quoted 5 lines
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
I think this patch would fix this crash:
http://lkml.kernel.org/r/[email protected]Btw, do you happen to have the full log here. OOM reaper can only interfere if there was an OOM killer invoked.
No, I don't. Sasha?
Signature · 7 lines
Kirill A. Shutemov -- 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>
Re: [PATCH 1/2] mm, oom: skip mlocked VMAs in __oom_reap_vmas()
Quoted 8 lines · Kirill A. Shutemov
Quoted 7 lines · Michal Hocko
Quoted 7 lines · Kirill A. Shutemov
Quoted 6 lines
Quoted 6 lines
As far as I can see we explicitly munlock pages everywhere before unmap them. The only case when we don't to that is OOM-reaper.
I don't think we should bother with munlocking in this case, we can just skip the locked VMA.
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
I think this patch would fix this crash:
http://lkml.kernel.org/r/[email protected]Btw, do you happen to have the full log here. OOM reaper can only interfere if there was an OOM killer invoked.
No, I don't. Sasha?
I don't have the log, but my setup does invoke the OOM killer occasionally.
Thanks,
Sasha
--
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>
PATCH 2/2 mm, thp: clear PG_mlocked when last mapping gone
In reply to Kirill A. ShutemovI missed clear_page_mlock() in page_remove_anon_compound_rmap(). It usually shouldn't cause any problems since we munlock pages explicitly, but in conjunction with missed munlock in __oom_reap_vmas() it causes problems:
http://lkml.kernel.org/r/[email protected]
Let's put it in place an mirror behaviour for small pages.
NOTE: I'm not entirely sure why we ever need clear_page_mlock() in page_remove_rmap() codepath. It looks redundant to me as we munlock pages anyway. But this is out of scope of the patch.
The patch can be folded into
"thp: allow mlocked THP again"
--- mm/rmap.c | 3 +++ 1 file changed, 3 insertions(+)
mm/rmap.c +3 -0
@@ -1356,6 +1356,9 @@ static void page_remove_anon_compound_rmap(struct page *page) nr =HPAGE_PMD_NR ; } +if (unlikely (PageMlocked (page))) +clear_page_mlock (page); +if (nr) {__mod_zone_page_state (page_zone (page),NR_ANON_PAGES , -nr);deferred_split_huge_page (page);
Signature · 7 lines
2.6.4 -- 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>
Re: [PATCH 2/2] mm, thp: clear PG_mlocked when last mapping gone
Quoted 11 lines · Kirill A. Shutemov
I missed
clear_page_mlock()inpage_remove_anon_compound_rmap(). It usually shouldn't cause any problems since we munlock pages explicitly, but in conjunction with missed munlock in__oom_reap_vmas()it causes problems:
http://lkml.kernel.org/r/[email protected]Let's put it in place an mirror behaviour for small pages.
NOTE: I'm not entirely sure why we ever need
clear_page_mlock()inpage_remove_rmap()codepath. It looks redundant to me as we munlock pages anyway. But this is out of scope of the patch.
Git blame actually quickly points to commit e6c509f854550 which explains it :)
Quoted 5 lines
The patch can be folded into
"thp: allow mlocked THP again"Signed-off-by Kirill A. Shutemov <[email protected]>
Ack.
Quoted 11 lines
Reported-by Sasha Levin <[email protected]>--- mm/rmap.c | 3 +++ 1 file changed, 3 insertions(+)mm/rmap.c +0
-0
mm/rmap.c static void page_remove_anon_compound_rmap(struct page *page) · line 1364
Quoted 9 lines
mm/rmap.c +3
-0@@ -1356,6 +1356,9 @@ static void page_remove_anon_compound_rmap(struct page *page) nr =HPAGE_PMD_NR ; } +if (unlikely (PageMlocked (page))) +clear_page_mlock (page); +if (nr) {__mod_zone_page_state (page_zone (page),NR_ANON_PAGES , -nr);deferred_split_huge_page (page);
--
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>
Re: [PATCH 2/2] mm, thp: clear PG_mlocked when last mapping gone
Quoted 7 lines · Vlastimil Babka
Quoted 6 lines · Kirill A. Shutemov
I missed
clear_page_mlock()inpage_remove_anon_compound_rmap(). It usually shouldn't cause any problems since we munlock pages explicitly, but in conjunction with missed munlock in__oom_reap_vmas()it causes problems:
http://lkml.kernel.org/r/[email protected]
Quoted 8 lines
Quoted 5 lines
Let's put it in place an mirror behaviour for small pages.
NOTE: I'm not entirely sure why we ever need
clear_page_mlock()inpage_remove_rmap()codepath. It looks redundant to me as we munlock pages anyway. But this is out of scope of the patch.Git blame actually quickly points to commit
e6c509f854550which explains it :)
Okay, it explains situation somewhat.
The thing which still makes me a bit uncomfortable with the situation is that we remove PG_mlocked only when the last mapping of the page gone. It's not necessary the mapping which was VM_LOCKED. It means we can rely on the clear_page_mlock() inside page_remove_rmap() only when remove all page mappings at once (like in truncate case).
The clear_page_mlock() also helps hide real mlock leak bugs, like fixed by patch 1/2: we saliently munlock page when last mapping gone, even if the VMA was never been mlocked in the first place.
That's kinda suboptimal.
Quoted 35 lines
Quoted 6 lines
The patch can be folded into
"thp: allow mlocked THP again"Signed-off-by Kirill A. Shutemov <[email protected]>Ack.
Quoted 11 lines
Reported-by Sasha Levin <[email protected]>--- mm/rmap.c | 3 +++ 1 file changed, 3 insertions(+)mm/rmap.c +0
-0mm/rmap.c static void page_remove_anon_compound_rmap(struct page *page) · line 1364
Quoted 9 lines
mm/rmap.c +3
-0@@ -1356,6 +1356,9 @@ static void page_remove_anon_compound_rmap(struct page *page) nr =HPAGE_PMD_NR ; } +if (unlikely (PageMlocked (page))) +clear_page_mlock (page); +if (nr) {__mod_zone_page_state (page_zone (page),NR_ANON_PAGES , -nr);deferred_split_huge_page (page);--
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>
Signature · 7 lines
Kirill A. Shutemov -- 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 7 lines · Kirill A. Shutemov
Hi Andrew,
There are two patches below. I believe either of the would fix the bug reported by Sasha, but it worth applying both.
Sasha, as I cannot trigger the bug, I would like to have your Tested-by.
Quoted 8 lines
Kirill A. Shutemov (2):
mm, oom: skip mlocked VMAs in__oom_reap_vmas()mm, thp: clearPG_mlockedwhen last mapping gonemm/oom_kill.c | 7 +++++++ mm/rmap.c | 3 +++ 2 files changed, 10 insertions(+)
Fixed for me.
Thanks,
Sasha
--
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>