Skip to contentSkip to conversation

mm/mmu_notifier: mm/rmap.c: Fix a mmu_notifier range bug in try_to_unmap_one

PATCH In review Sean Christopherson 5 participants 5 messages Jan 10, 2019

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

PATCH 1/1 mm/mmu_notifier: mm/rmap.c: Fix a mmu_notifier range bug in try_to_unmap_one

The conversion to use a structure for mmu_notifier_invalidate_range_*() unintentionally changed the usage in try_to_unmap_one() to init the 'struct mmu_notifier_range' with vma->vm_start instead of @address, i.e. it invalidates the wrong address range. Revert to the correct address range.

Manifests as KVM use-after-free WARNINGs and subsequent "BUG: Bad page state in process X" errors when reclaiming from a KVM guest due to KVM removing the wrong pages from its own mappings.

Reported-by [email protected]
Cc Jérôme Glisse <[email protected]>
Cc Christian König <[email protected]>
Cc Jan Kara <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Ross Zwisler <[email protected]>
Cc Dan Williams <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc John Hubbard <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
Fixes ac46d4f3c432 ("mm/mmu_notifier: use structure for invalidate_range_start/end calls v2")

---

FWIW, I looked through all other calls to mmu_notifier_range_init() in the patch and didn't spot any other unintentional functional changes.

 mm/rmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/rmap.c +2 -2
index 68a1a5b869a5..0454ecc29537 100644
@@ -1371,8 +1371,8 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 	 * Note that the page can not be free in this function as call of
 	 * try_to_unmap() must hold a reference on the page.
 	 */
-	mmu_notifier_range_init(&range, vma->vm_mm, vma->vm_start,
-				min(vma->vm_end, vma->vm_start +
+	mmu_notifier_range_init(&range, vma->vm_mm, address,
+				min(vma->vm_end, address +
 				    (PAGE_SIZE << compound_order(page))));
 	if (PageHuge(page)) {
 		/*
Signature · 1 line
2.19.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Sean Christopherson

The conversion to use a structure for mmu_notifier_invalidate_range_*() unintentionally changed the usage in try_to_unmap_one() to init the 'struct mmu_notifier_range' with vma->vm_start instead of @address, i.e. it invalidates the wrong address range. Revert to the correct address range.

Quoted 7 lines

Manifests as KVM use-after-free WARNINGs and subsequent "BUG: Bad page state in process X" errors when reclaiming from a KVM guest due to KVM removing the wrong pages from its own mappings.

Quoted 41 lines
Cc Christian König <[email protected]>
Cc Jan Kara <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Ross Zwisler <[email protected]>
Cc Dan Williams <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc John Hubbard <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
Fixes ac46d4f3c432 ("mm/mmu_notifier: use structure for invalidate_range_start/end calls v2")

---

FWIW, I looked through all other calls to mmu_notifier_range_init() in the patch and didn't spot any other unintentional functional changes.

 mm/rmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/rmap.c +2 -2
index 68a1a5b869a5..0454ecc29537 100644
@@ -1371,8 +1371,8 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 	 * Note that the page can not be free in this function as call of
 	 * try_to_unmap() must hold a reference on the page.
 	 */
-	mmu_notifier_range_init(&range, vma->vm_mm, vma->vm_start,
-				min(vma->vm_end, vma->vm_start +
+	mmu_notifier_range_init(&range, vma->vm_mm, address,
+				min(vma->vm_end, address +
 				    (PAGE_SIZE << compound_order(page))));
 	if (PageHuge(page)) {
 		/*
Signature · 1 line
2.19.2
Reviewed-by on v1 1/1
#2 lore raw
In reply to Sean Christopherson
Quoted 54 lines

The conversion to use a structure for mmu_notifier_invalidate_range_*() unintentionally changed the usage in try_to_unmap_one() to init the 'struct mmu_notifier_range' with vma->vm_start instead of @address, i.e. it invalidates the wrong address range. Revert to the correct address range.

Manifests as KVM use-after-free WARNINGs and subsequent "BUG: Bad page state in process X" errors when reclaiming from a KVM guest due to KVM removing the wrong pages from its own mappings.

Reported-by [email protected]
Cc Jérôme Glisse <[email protected]>
Cc Christian König <[email protected]>
Cc Jan Kara <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Ross Zwisler <[email protected]>
Cc Dan Williams <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc John Hubbard <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
Fixes ac46d4f3c432 ("mm/mmu_notifier: use structure for

invalidate_range_start/end calls v2")

---

FWIW, I looked through all other calls to mmu_notifier_range_init() in the patch and didn't spot any other unintentional functional changes.

 mm/rmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/rmap.c +2 -2
index 68a1a5b869a5..0454ecc29537 100644
@@ -1371,8 +1371,8 @@ static bool try_to_unmap_one(struct page *page, struct
 vm_area_struct *vma,
 	 * Note that the page can not be free in this function as call of
 	 * try_to_unmap() must hold a reference on the page.
 	 */
-	mmu_notifier_range_init(&range, vma->vm_mm, vma->vm_start,
-				min(vma->vm_end, vma->vm_start +
+	mmu_notifier_range_init(&range, vma->vm_mm, address,
+				min(vma->vm_end, address +
 				    (PAGE_SIZE << compound_order(page))));
 	if (PageHuge(page)) {
Quoted 2 lines

/*
--

I was suspecting this patch for some other issue. But could not spot this after in depth analyzing the changed "invalidate_range_start/end calls".

Its indeed a good catch.

Quoted 3 lines

2.19.2

#3 lore raw
In reply to Sean Christopherson
Quoted 3 lines · Sean Christopherson

Manifests as KVM use-after-free WARNINGs and subsequent "BUG: Bad page state in process X" errors when reclaiming from a KVM guest due to KVM removing the wrong pages from its own mappings.

With your patch, no badness happened so far. Thanks!

mm/rmap.c static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, · line 1375

Quoted 9 lines
Fixes ac46d4f3c432 ("mm/mmu_notifier: use structure for invalidate_range_start/end calls v2")
mm/rmap.c +2 -2
-	mmu_notifier_range_init(&range, vma->vm_mm, vma->vm_start,
-				min(vma->vm_end, vma->vm_start +
+	mmu_notifier_range_init(&range, vma->vm_mm, address,
+				min(vma->vm_end, address +

Meow.

Signature · 4 lines
⢀⣴⠾⠻⢶⣦⠀ Hans 1 was born and raised in Johannesburg, then moved to Boston,
⣾⠁⢠⠒⠀⣿⡁ and has just became a naturalized citizen.  Hans 2's grandparents
⢿⡄⠘⠷⠚⠋⠀ came from Melanesia to Düsseldorf, and he hasn't ever been outside
⠈⠳⣄⠀⠀⠀⠀ Germany until yesterday.  Which one is an African-American?
#4 lore raw
In reply to Sean Christopherson

After applying this patch, I'm no longer seeing dmesg messages with WARNINGs and BUGs, and both host and guests are stable.

Thank you.

Às 22:51 de 09/01/2019, Sean Christopherson escreveu:

Quoted 53 lines

The conversion to use a structure for mmu_notifier_invalidate_range_*() unintentionally changed the usage in try_to_unmap_one() to init the 'struct mmu_notifier_range' with vma->vm_start instead of @address, i.e. it invalidates the wrong address range. Revert to the correct address range.

Manifests as KVM use-after-free WARNINGs and subsequent "BUG: Bad page state in process X" errors when reclaiming from a KVM guest due to KVM removing the wrong pages from its own mappings.

Reported-by [email protected]
Cc Jérôme Glisse <[email protected]>
Cc Christian König <[email protected]>
Cc Jan Kara <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Ross Zwisler <[email protected]>
Cc Dan Williams <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Radim Krčmář <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc John Hubbard <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>
Fixes ac46d4f3c432 ("mm/mmu_notifier: use structure for invalidate_range_start/end calls v2")

---

FWIW, I looked through all other calls to mmu_notifier_range_init() in the patch and didn't spot any other unintentional functional changes.

 mm/rmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/rmap.c +2 -2
index 68a1a5b869a5..0454ecc29537 100644
@@ -1371,8 +1371,8 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 	 * Note that the page can not be free in this function as call of
 	 * try_to_unmap() must hold a reference on the page.
 	 */
-	mmu_notifier_range_init(&range, vma->vm_mm, vma->vm_start,
-				min(vma->vm_end, vma->vm_start +
+	mmu_notifier_range_init(&range, vma->vm_mm, address,
+				min(vma->vm_end, address +
 				    (PAGE_SIZE << compound_order(page))));
 	if (PageHuge(page)) {
 		/*