Skip to contentSkip to conversation

mm,hwpoison: unmap poisoned page before invalidation

PATCH In review Rik van Riel 4 participants 11 messages Mar 25, 2022 – Mar 29, 2022

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

PATCH 1/1 mm,hwpoison: unmap poisoned page before invalidation

In some cases it appears the invalidation of a hwpoisoned page fails because the page is still mapped in another process. This can cause a program to be continuously restarted and die when it page faults on the page that was not invalidated. Avoid that problem by unmapping the hwpoisoned page when we find it.

Another issue is that sometimes we end up oopsing in finish_fault, if the code tries to do something with the now-NULL vmf->page. I did not hit this error when submitting the previous patch because there are several opportunities for alloc_set_pte to bail out before accessing vmf->page, and that apparently happened on those systems, and most of the time on other systems, too.

However, across several million systems that error does occur a handful of times a day. It can be avoided by returning VM_FAULT_NOPAGE which will cause do_read_fault to return before calling finish_fault.

Fixes e53ac7374e64 ("mm: invalidate hwpoison page cache page in fault path")
Cc Oscar Salvador <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +8 -4
index be44d0b36b18..76e3af9639d9 100644
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
 		return ret;
 
 	if (unlikely(PageHWPoison(vmf->page))) {
+		struct page *page = vmf->page;
 		vm_fault_t poisonret = VM_FAULT_HWPOISON;
 		if (ret & VM_FAULT_LOCKED) {
+			if (page_mapped(page))
+				unmap_mapping_pages(page_mapping(page),
+						    page->index, 1, false);
 			/* Retry if a clean page was removed from the cache. */
-			if (invalidate_inode_page(vmf->page))
-				poisonret = 0;
-			unlock_page(vmf->page);
+			if (invalidate_inode_page(page))
+				poisonret = VM_FAULT_NOPAGE;
+			unlock_page(page);
 		}
-		put_page(vmf->page);
+		put_page(page);
 		vmf->page = NULL;
 		return poisonret;
 	}
Signature · 1 line
2.35.1
#1 lore raw
Quoted 34 lines · Rik van Riel

In some cases it appears the invalidation of a hwpoisoned page fails because the page is still mapped in another process. This can cause a program to be continuously restarted and die when it page faults on the page that was not invalidated. Avoid that problem by unmapping the hwpoisoned page when we find it.

Another issue is that sometimes we end up oopsing in finish_fault, if the code tries to do something with the now-NULL vmf->page. I did not hit this error when submitting the previous patch because there are several opportunities for alloc_set_pte to bail out before accessing vmf->page, and that apparently happened on those systems, and most of the time on other systems, too.

However, across several million systems that error does occur a handful of times a day. It can be avoided by returning VM_FAULT_NOPAGE which will cause do_read_fault to return before calling finish_fault.

Fixes e53ac7374e64 ("mm: invalidate hwpoison page cache page in fault path")
Cc Oscar Salvador <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +0 -0
index be44d0b36b18..76e3af9639d9 100644

mm/memory.c static vm_fault_t __do_fault(struct vm_fault *vmf) · line 3926

Quoted 9 lines
mm/memory.c +4 -0
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
 		return ret;
 
 	if (unlikely(PageHWPoison(vmf->page))) {
+		struct page *page = vmf->page;
 		vm_fault_t poisonret = VM_FAULT_HWPOISON;
 		if (ret & VM_FAULT_LOCKED) {
+			if (page_mapped(page))
+				unmap_mapping_pages(page_mapping(page),
+						    page->index, 1, false);

It seems this unmap_mapping_pages also helps the success rate of the below invalidate_inode_page.

mm/memory.c static vm_fault_t __do_fault(struct vm_fault *vmf) · line 3932

Quoted 10 lines
mm/memory.c +4 -4
 			/* Retry if a clean page was removed from the cache. */
-			if (invalidate_inode_page(vmf->page))
-				poisonret = 0;
-			unlock_page(vmf->page);
+			if (invalidate_inode_page(page))
+				poisonret = VM_FAULT_NOPAGE;
+			unlock_page(page);
 		}
-		put_page(vmf->page);
+		put_page(page);

Do we use page instead of vmf->page just for simplicity? Or there is some other concern?

mm/memory.c static vm_fault_t __do_fault(struct vm_fault *vmf) · line 3933

Quoted 1 line
mm/memory.c +0 -0
 		vmf->page = NULL;

We return either VM_FAULT_NOPAGE or VM_FAULT_HWPOISON with vmf->page = NULL. If any case, finish_fault won't be called later. So I think your fix is right.

mm/memory.c static vm_fault_t __do_fault(struct vm_fault *vmf) · line 3935

Quoted 3 lines
mm/memory.c +0 -0
 		return poisonret;
 	}

Many thanks for your patch.

#2 lore raw
Quoted 11 lines · Miaohe Lin
Quoted 10 lines · Rik van Riel

+++ b/mm/memory.c

(quoted diff) +0 -0
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct
 vm_fault *vmf)
                 return ret;


        if (unlikely(PageHWPoison(vmf->page))) { +               struct page *page = vmf->page; vm_fault_t poisonret = VM_FAULT_HWPOISON; if (ret & VM_FAULT_LOCKED) {

Quoted 9 lines
Quoted 5 lines

+                       if (page_mapped(page))
+                               unmap_mapping_pages(page_mapping(pa ge),
+                                                   page->index, 1, false);

It seems this unmap_mapping_pages also helps the success rate of the below invalidate_inode_page.

That is indeed what it is supposed to do.

It isn't fool proof, since you can still end up with dirty pages that don't get cleaned immediately, but it seems to turn infinite loops of a program being killed every time it's started into a more manageable situation where the task succeeds again pretty quickly.

Quoted 12 lines
Quoted 11 lines

                        /* Retry if a clean page was removed from the cache. */
-                       if (invalidate_inode_page(vmf->page)) -                               poisonret = 0;
-                       unlock_page(vmf->page);
+                       if (invalidate_inode_page(page)) +                               poisonret = VM_FAULT_NOPAGE; +                       unlock_page(page);
                }
-               put_page(vmf->page);
+               put_page(page);

Quoted 3 lines

Do we use page instead of vmf->page just for simplicity? Or there is some other concern?

Just a simplification, and not dereferencing the same thing 6 times.

Quoted 5 lines
Quoted 1 line

                vmf->page = NULL;

We return either VM_FAULT_NOPAGE or VM_FAULT_HWPOISON with vmf->page = NULL. If any case,
finish_fault won't be called later. So I think your fix is right.

Want to send in a Reviewed-by or Acked-by? :)

Signature · 1 line
All Rights Reversed.
#3 lore raw
Quoted 22 lines · Rik van Riel
Quoted 11 lines · Miaohe Lin
Quoted 10 lines · Rik van Riel

+++ b/mm/memory.c

(quoted diff) +0 -0
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct
 vm_fault *vmf)
                 return ret;


        if (unlikely(PageHWPoison(vmf->page))) { +               struct page *page = vmf->page; vm_fault_t poisonret = VM_FAULT_HWPOISON; if (ret & VM_FAULT_LOCKED) {

Quoted 9 lines
Quoted 5 lines

+                       if (page_mapped(page))
+                               unmap_mapping_pages(page_mapping(pa ge),
+                                                   page->index, 1, false);

It seems this unmap_mapping_pages also helps the success rate of the below invalidate_inode_page.

Quoted 8 lines

That is indeed what it is supposed to do.

It isn't fool proof, since you can still end up with dirty pages that don't get cleaned immediately, but it seems to turn infinite loops of a program being killed every time it's started into a more manageable situation where the task succeeds again pretty quickly.

Looks convincing to me.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

                        /* Retry if a clean page was removed from the cache. */
-                       if (invalidate_inode_page(vmf->page)) -                               poisonret = 0;
-                       unlock_page(vmf->page);
+                       if (invalidate_inode_page(page)) +                               poisonret = VM_FAULT_NOPAGE; +                       unlock_page(page);
                }
-               put_page(vmf->page);
+               put_page(page);

Quoted 7 lines
Quoted 3 lines

Do we use page instead of vmf->page just for simplicity? Or there is some other concern?

Just a simplification, and not dereferencing the same thing 6 times.

I see. :)

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

                vmf->page = NULL;

We return either VM_FAULT_NOPAGE or VM_FAULT_HWPOISON with vmf->page = NULL. If any case,
finish_fault won't be called later. So I think your fix is right.

Want to send in a Reviewed-by or Acked-by? :)

Sure, but when I think more about this, it seems this fix isn't ideal:
If VM_FAULT_NOPAGE is returned with page table unset, the process will re-trigger page fault again and again until invalidate_inode_page succeeds to evict the inode page. This might hang the process a really long time. Or am I miss something?

Thanks.

#4 lore raw
Quoted 15 lines · Miaohe Lin
Quoted 11 lines · Rik van Riel
Quoted 10 lines
Quoted 10 lines

                        /* Retry if a clean page was removed from
the cache. */
-                       if (invalidate_inode_page(vmf->page)) -                               poisonret = 0;
-                       unlock_page(vmf->page);
+                       if (invalidate_inode_page(page)) +                               poisonret = VM_FAULT_NOPAGE; +                       unlock_page(page);

Quoted 10 lines

Sure, but when I think more about this, it seems this fix isn't ideal:
If VM_FAULT_NOPAGE is returned with page table unset, the process will
re-trigger page fault again and again until invalidate_inode_page succeeds
to evict the inode page. This might hang the process a really long time.
Or am I miss something?

If invalidate_inode_page fails, we will return
VM_FAULT_HWPOISON, and kill the task, instead
of looping indefinitely.

Signature · 1 line
All Rights Reversed.
Reviewed-by
#5 lore raw
Quoted 26 lines · Rik van Riel
Quoted 15 lines · Miaohe Lin
Quoted 11 lines · Rik van Riel
Quoted 10 lines
Quoted 10 lines

                        /* Retry if a clean page was removed from
the cache. */
-                       if (invalidate_inode_page(vmf->page)) -                               poisonret = 0;
-                       unlock_page(vmf->page);
+                       if (invalidate_inode_page(page)) +                               poisonret = VM_FAULT_NOPAGE; +                       unlock_page(page);

Quoted 10 lines

Sure, but when I think more about this, it seems this fix isn't ideal:
If VM_FAULT_NOPAGE is returned with page table unset, the process will
re-trigger page fault again and again until invalidate_inode_page succeeds
to evict the inode page. This might hang the process a really long time.
Or am I miss something?

Quoted 3 lines

If invalidate_inode_page fails, we will return
VM_FAULT_HWPOISON, and kill the task, instead
of looping indefinitely.

Oh, really sorry! It's a drowsy Monday morning. :)

This patch looks good to me. Thanks!

Quoted 1 line
#6 lore raw
In reply to Rik van Riel
Quoted 39 lines · Rik van Riel

In some cases it appears the invalidation of a hwpoisoned page fails because the page is still mapped in another process. This can cause a program to be continuously restarted and die when it page faults on the page that was not invalidated. Avoid that problem by unmapping the hwpoisoned page when we find it.

Another issue is that sometimes we end up oopsing in finish_fault, if the code tries to do something with the now-NULL vmf->page. I did not hit this error when submitting the previous patch because there are several opportunities for alloc_set_pte to bail out before accessing vmf->page, and that apparently happened on those systems, and most of the time on other systems, too.

However, across several million systems that error does occur a handful of times a day. It can be avoided by returning VM_FAULT_NOPAGE which will cause do_read_fault to return before calling finish_fault.

Fixes e53ac7374e64 ("mm: invalidate hwpoison page cache page in fault path")
Cc Oscar Salvador <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +1 -0
index be44d0b36b18..76e3af9639d9 100644
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
 		return ret;
 
 	if (unlikely(PageHWPoison(vmf->page))) {
+		struct page *page = vmf->page;
 		vm_fault_t poisonret = VM_FAULT_HWPOISON;

mm/memory.c static vm_fault_t __do_fault(struct vm_fault *vmf) · line 3929

Quoted 10 lines
mm/memory.c +5 -3
 		if (ret & VM_FAULT_LOCKED) {
+			if (page_mapped(page))
+				unmap_mapping_pages(page_mapping(page),
+						    page->index, 1, false);
 			/* Retry if a clean page was removed from the cache. */
-			if (invalidate_inode_page(vmf->page))
-				poisonret = 0;
-			unlock_page(vmf->page);
+			if (invalidate_inode_page(page))
+				poisonret = VM_FAULT_NOPAGE;

What is the effect of returning VM_FAULT_NOPAGE?
I take that we are cool because the pte has been installed and points to a new page? (I could not find where that is being done).

Signature · 2 lines
Oscar Salvador
SUSE Labs
#7 lore raw
Quoted 9 lines · Oscar Salvador
Quoted 2 lines · Rik van Riel

+                       if (invalidate_inode_page(page)) +                               poisonret = VM_FAULT_NOPAGE;

What is the effect of returning VM_FAULT_NOPAGE?
I take that we are cool because the pte has been installed and points to
a new page? (I could not find where that is being done).

It results in us returning to userspace as if the page fault had been handled, resulting in a second fault on the same address.

However, now the page is no longer in the page cache, and we can read it in from disk, to a page that is not hardware poisoned, and we can then use that second page without issues.

Signature · 1 line
All Rights Reversed.
#8 lore raw
Quoted 8 lines · Rik van Riel

It results in us returning to userspace as if the page fault had been handled, resulting in a second fault on the same address.

However, now the page is no longer in the page cache, and we can read it in from disk, to a page that is not hardware poisoned, and we can then use that second page without issues.

Ok, I see, thanks a lot for the explanation Rik.

Signature · 2 lines
Oscar Salvador
SUSE Labs
Tested-by on v1 1/1
#9 lore raw
In reply to Rik van Riel
Quoted 13 lines · Rik van Riel

In some cases it appears the invalidation of a hwpoisoned page fails because the page is still mapped in another process. This can cause a program to be continuously restarted and die when it page faults on the page that was not invalidated. Avoid that problem by unmapping the hwpoisoned page when we find it.

Another issue is that sometimes we end up oopsing in finish_fault, if the code tries to do something with the now-NULL vmf->page. I did not hit this error when submitting the previous patch because there are several opportunities for alloc_set_pte to bail out before accessing vmf->page, and that apparently happened on those systems, and most of the time on other systems, too.

Quoted 3 lines

However, across several million systems that error does occur a handful of times a day. It can be avoided by returning VM_FAULT_NOPAGE which will cause do_read_fault to return before calling finish_fault.

I artificially created clean/dirty page cache pages with PageHWPoison flag (with SystemTap), then reproduced NULL pointer dereference by page fault on current mainline branch (with e53ac7374e64). And confirmed that the bug was fixed with this patch, so the fix seems to work.

(Maybe I should've done this kind of testing before merging e53ac7374e64, sorry..)

Anyway, thank you very much.

Quoted 43 lines
Fixes e53ac7374e64 ("mm: invalidate hwpoison page cache page in fault path")
Cc Oscar Salvador <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +8 -4
index be44d0b36b18..76e3af9639d9 100644
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
 		return ret;
 
 	if (unlikely(PageHWPoison(vmf->page))) {
+		struct page *page = vmf->page;
 		vm_fault_t poisonret = VM_FAULT_HWPOISON;
 		if (ret & VM_FAULT_LOCKED) {
+			if (page_mapped(page))
+				unmap_mapping_pages(page_mapping(page),
+						    page->index, 1, false);
 			/* Retry if a clean page was removed from the cache. */
-			if (invalidate_inode_page(vmf->page))
-				poisonret = 0;
-			unlock_page(vmf->page);
+			if (invalidate_inode_page(page))
+				poisonret = VM_FAULT_NOPAGE;
+			unlock_page(page);
 		}
-		put_page(vmf->page);
+		put_page(page);
 		vmf->page = NULL;
 		return poisonret;
 	}
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#10 lore raw
In reply to Rik van Riel
Quoted 17 lines · Rik van Riel

In some cases it appears the invalidation of a hwpoisoned page fails because the page is still mapped in another process. This can cause a program to be continuously restarted and die when it page faults on the page that was not invalidated. Avoid that problem by unmapping the hwpoisoned page when we find it.

Another issue is that sometimes we end up oopsing in finish_fault, if the code tries to do something with the now-NULL vmf->page. I did not hit this error when submitting the previous patch because there are several opportunities for alloc_set_pte to bail out before accessing vmf->page, and that apparently happened on those systems, and most of the time on other systems, too.

However, across several million systems that error does occur a handful of times a day. It can be avoided by returning VM_FAULT_NOPAGE which will cause do_read_fault to return before calling finish_fault.

Quoted 8 lines
Fixes e53ac7374e64 ("mm: invalidate hwpoison page cache page in fault path")
Cc Oscar Salvador <[email protected]>
Cc Miaohe Lin <[email protected]>
Cc Naoya Horiguchi <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Andrew Morton <[email protected]>
Quoted 36 lines
---
 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +8 -4
index be44d0b36b18..76e3af9639d9 100644
@@ -3918,14 +3918,18 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)
 		return ret;
 
 	if (unlikely(PageHWPoison(vmf->page))) {
+		struct page *page = vmf->page;
 		vm_fault_t poisonret = VM_FAULT_HWPOISON;
 		if (ret & VM_FAULT_LOCKED) {
+			if (page_mapped(page))
+				unmap_mapping_pages(page_mapping(page),
+						    page->index, 1, false);
 			/* Retry if a clean page was removed from the cache. */
-			if (invalidate_inode_page(vmf->page))
-				poisonret = 0;
-			unlock_page(vmf->page);
+			if (invalidate_inode_page(page))
+				poisonret = VM_FAULT_NOPAGE;
+			unlock_page(page);
 		}
-		put_page(vmf->page);
+		put_page(page);
 		vmf->page = NULL;
 		return poisonret;
 	}
Signature · 1 line
2.35.1
Signature · 2 lines
Oscar Salvador
SUSE Labs