Skip to contentSkip to conversation

uprobes: __replace_page() avoid BUG in munlock_vma_page()

PATCH Applied Hugh Dickins 4 participants 6 messages Aug 16, 2020 – Aug 17, 2020

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

PATCH 1/1 uprobes: __replace_page() avoid BUG in munlock_vma_page()

syzbot crashed on the VM_BUG_ON_PAGE(PageTail) in munlock_vma_page(), when called from uprobes __replace_page(). Which of many ways to fix it? Settled on not calling when PageCompound (since Head and Tail are equals in this context, PageCompound the usual check in uprobes.c, and the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already).

Fixes 5a52c9df62b4 ("uprobe: use FOLL_SPLIT_PMD instead of FOLL_SPLIT")

---
This one is not a 5.9-rc regression, but still good to fix.

 kernel/events/uprobes.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/uprobes.c +1 -1
@@ -205,7 +205,7 @@ static int __replace_page(struct vm_area
 		try_to_free_swap(old_page);
 	page_vma_mapped_walk_done(&pvmw);
 
-	if (vma->vm_flags & VM_LOCKED)
+	if ((vma->vm_flags & VM_LOCKED) && !PageCompound(old_page))
 		munlock_vma_page(old_page);
 	put_page(old_page);
 
#1 lore raw
Quoted 23 lines · Hugh Dickins

syzbot crashed on the VM_BUG_ON_PAGE(PageTail) in munlock_vma_page(), when called from uprobes __replace_page(). Which of many ways to fix it? Settled on not calling when PageCompound (since Head and Tail are equals in this context, PageCompound the usual check in uprobes.c, and the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already).

Fixes 5a52c9df62b4 ("uprobe: use FOLL_SPLIT_PMD instead of FOLL_SPLIT")

---
This one is not a 5.9-rc regression, but still good to fix.

kernel/events/uprobes.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)

kernel/events/uprobes.c static int __replace_page(struct vm_area · line 208

Quoted 5 lines
kernel/events/uprobes.c +1 -1
@@ -205,7 +205,7 @@ static int __replace_page(struct vm_area
 		try_to_free_swap(old_page);
 	page_vma_mapped_walk_done(&pvmw);
 
-	if (vma->vm_flags & VM_LOCKED)
+	if ((vma->vm_flags & VM_LOCKED) && !PageCompound(old_page))

Do we need munlock_vma_page() for THP page head?

Thanks,
Song

Quoted 3 lines
kernel/events/uprobes.c +0 -0
 		munlock_vma_page(old_page);
 	put_page(old_page);
 
#2 lore raw
Quoted 24 lines · Song Liu
Quoted 24 lines · Hugh Dickins

syzbot crashed on the VM_BUG_ON_PAGE(PageTail) in munlock_vma_page(), when called from uprobes __replace_page(). Which of many ways to fix it? Settled on not calling when PageCompound (since Head and Tail are equals in this context, PageCompound the usual check in uprobes.c, and the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already).

Fixes 5a52c9df62b4 ("uprobe: use FOLL_SPLIT_PMD instead of FOLL_SPLIT")

---
This one is not a 5.9-rc regression, but still good to fix.

kernel/events/uprobes.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/uprobes.c +0 -0
@@ -205,7 +205,7 @@ static int __replace_page(struct vm_area
 		try_to_free_swap(old_page);
 	page_vma_mapped_walk_done(&pvmw);
 
Quoted 4 lines

kernel/events/uprobes.c static int __replace_page(struct vm_area · line 208

Quoted 2 lines
kernel/events/uprobes.c +1 -1
-	if (vma->vm_flags & VM_LOCKED)
+	if ((vma->vm_flags & VM_LOCKED) && !PageCompound(old_page))

Do we need munlock_vma_page() for THP page head?

No: as the commit message says "the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already" - our THP implementation has difficulty supporting Mlocked consistently when the huge page is somewhere mapped by ptes, so one of the things that __split_huge_pmd() does is clear_page_mlock(), then PageDoubleMap will prevent Mlocked being set again once GUP has brought the old_page pte back in.

But if you'd prefer us to munlock_vma_page(compound_head(old_page)) instead, I can certainly change the patch: it's one of the options I considered, but couldn't quite bring myself to do it that way, knowing that actually it would never find PageMlocked set. (If PageMlocked were allowed on tail pages, I'd have used a PageMlocked test instead of the PageCompound one: I spent nearly an hour bikeshedding the alternatives here!)

(One day I must remind myself of when munlock_vma_page() should be used, versus when clear_page_mlock() should be used: I think it comes down to a choice of which stats get incremented, but I may also be forgetting something more important: anyway, no obvious reason to depart from the munlock_vma_page() that's always been used here.)

Hugh

Acked-by
#3 lore raw
Quoted 49 lines · Hugh Dickins
Quoted 24 lines · Song Liu
Quoted 24 lines · Hugh Dickins

syzbot crashed on the VM_BUG_ON_PAGE(PageTail) in munlock_vma_page(), when called from uprobes __replace_page(). Which of many ways to fix it? Settled on not calling when PageCompound (since Head and Tail are equals in this context, PageCompound the usual check in uprobes.c, and the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already).

Fixes 5a52c9df62b4 ("uprobe: use FOLL_SPLIT_PMD instead of FOLL_SPLIT")

---
This one is not a 5.9-rc regression, but still good to fix.

kernel/events/uprobes.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/uprobes.c +0 -0
@@ -205,7 +205,7 @@ static int __replace_page(struct vm_area
 		try_to_free_swap(old_page);
 	page_vma_mapped_walk_done(&pvmw);
 
Quoted 4 lines

kernel/events/uprobes.c static int __replace_page(struct vm_area · line 208

Quoted 2 lines
kernel/events/uprobes.c +1 -1
-	if (vma->vm_flags & VM_LOCKED)
+	if ((vma->vm_flags & VM_LOCKED) && !PageCompound(old_page))

Do we need munlock_vma_page() for THP page head?

No: as the commit message says "the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already" - our THP implementation has difficulty supporting Mlocked consistently when the huge page is somewhere mapped by ptes, so one of the things that __split_huge_pmd() does is clear_page_mlock(), then PageDoubleMap will prevent Mlocked being set again once GUP has brought the old_page pte back in.

But if you'd prefer us to munlock_vma_page(compound_head(old_page)) instead, I can certainly change the patch: it's one of the options I considered, but couldn't quite bring myself to do it that way, knowing that actually it would never find PageMlocked set. (If PageMlocked were allowed on tail pages, I'd have used a PageMlocked test instead of the PageCompound one: I spent nearly an hour bikeshedding the alternatives here!)

Quoted 5 lines

(One day I must remind myself of when munlock_vma_page() should be used, versus when clear_page_mlock() should be used: I think it comes down to a choice of which stats get incremented, but I may also be forgetting something more important: anyway, no obvious reason to depart from the munlock_vma_page() that's always been used here.)

Thanks a lot for the explanation!

maintainer Acked-by on v1 1/1
#4 lore raw
In reply to Hugh Dickins

kernel/events/uprobes.c static int __replace_page(struct vm_area · line 210

Quoted 11 lines · Hugh Dickins
kernel/events/uprobes.c +1 -1
@@ -205,7 +205,7 @@ static int __replace_page(struct vm_area
 		try_to_free_swap(old_page);
 	page_vma_mapped_walk_done(&pvmw);
 
-	if (vma->vm_flags & VM_LOCKED)
+	if ((vma->vm_flags & VM_LOCKED) && !PageCompound(old_page))
 		munlock_vma_page(old_page);
 	put_page(old_page);

Thanks!

Reviewed-by on v1 1/1
#5 lore raw
In reply to Hugh Dickins
Quoted 19 lines · Hugh Dickins

syzbot crashed on the VM_BUG_ON_PAGE(PageTail) in munlock_vma_page(), when called from uprobes __replace_page(). Which of many ways to fix it? Settled on not calling when PageCompound (since Head and Tail are equals in this context, PageCompound the usual check in uprobes.c, and the prior use of FOLL_SPLIT_PMD will have cleared PageMlocked already).

Fixes 5a52c9df62b4 ("uprobe: use FOLL_SPLIT_PMD instead of FOLL_SPLIT")

---
This one is not a 5.9-rc regression, but still good to fix.

 kernel/events/uprobes.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

kernel/events/uprobes.c static int __replace_page(struct vm_area · line 211

Quoted 8 lines
kernel/events/uprobes.c +1 -1
@@ -205,7 +205,7 @@ static int __replace_page(struct vm_area
 		try_to_free_swap(old_page);
 	page_vma_mapped_walk_done(&pvmw);
 
-	if (vma->vm_flags & VM_LOCKED)
+	if ((vma->vm_flags & VM_LOCKED) && !PageCompound(old_page))
 		munlock_vma_page(old_page);
 	put_page(old_page);
 

Looks good to me.

Signature · 2 lines
Thanks and Regards
Srikar Dronamraju