Skip to contentSkip to conversation

Enhance soft hwpoison handling and injection

PATCH Superseded Jane Chu 3 participants 26 messages May 1, 2024 – May 10, 2024

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jane Chu

author
#0 lore raw

cover Enhance soft hwpoison handling and injection

This series aim at the following enhancement -
1. Let one hwpoison injector, that is, madvise(MADV_HWPOISON) to behave more like as if a real UE occurred. Because the other two injectors such as hwpoison-inject and the 'einj' on x86 can't, and it seems to me we need a better simulation to real UE scenario.
2. For years, if the kernel is unable to unmap a hwpoisoned page, it send a SIGKILL instead of SIGBUS to prevent user process from potentially accessing the page again. But in doing so, the user process also lose important information: vaddr, for recovery. Fortunately, the kernel already has code to kill process re-accessing a hwpoisoned page, so remove the '!unmap_success' check.
3. Right now, if a thp page under GUP longterm pin is hwpoisoned, and kernel cannot split the thp page, memory-failure simply ignores the UE and returns. That's not ideal, it could deliver a SIGBUS with useful information for userspace recovery.

Jane Chu (3):
mm/memory-failure: try to send SIGBUS even if unmap failed mm/madvise: Add MF_ACTION_REQUIRED to madvise(MADV_HWPOISON) mm/memory-failure: send SIGBUS in the event of thp split fail

 mm/madvise.c        |  2 +-
 mm/memory-failure.c | 49 ++++++++++++++++++++++++++++++++++++---------
 2 files changed, 41 insertions(+), 10 deletions(-)
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH 1/3 mm/memory-failure: try to send SIGBUS even if unmap failed

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

---
 mm/memory-failure.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/memory-failure.c +4 -9
index 9e62a00b46dd..7fcf182abb96 100644
@@ -519,19 +519,14 @@ void add_to_kill_ksm(struct task_struct *tsk, struct page *p,
  * Also when FAIL is set do a force kill because something went
  * wrong earlier.
  */
-static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
+static void kill_procs(struct list_head *to_kill, int forcekill,
 		unsigned long pfn, int flags)
 {
 	struct to_kill *tk, *next;
 
 	list_for_each_entry_safe(tk, next, to_kill, nd) {
 		if (forcekill) {
-			/*
-			 * In case something went wrong with munmapping
-			 * make sure the process doesn't catch the
-			 * signal and then access the memory. Just kill it.
-			 */
-			if (fail || tk->addr == -EFAULT) {
+			if (tk->addr == -EFAULT) {
 				pr_err("%#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
 				       pfn, tk->tsk->comm, tk->tsk->pid);
 				do_send_sig_info(SIGKILL, SEND_SIG_PRIV,
@@ -1666,7 +1661,7 @@ static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
 	 */
 	forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL) ||
 		    !unmap_success;
-	kill_procs(&tokill, forcekill, !unmap_success, pfn, flags);
+	kill_procs(&tokill, forcekill, pfn, flags);
 
 	return unmap_success;
 }
@@ -1730,7 +1725,7 @@ static void unmap_and_kill(struct list_head *to_kill, unsigned long pfn,
 		unmap_mapping_range(mapping, start, size, 0);
 	}
 
-	kill_procs(to_kill, flags & MF_MUST_KILL, false, pfn, flags);
+	kill_procs(to_kill, flags & MF_MUST_KILL, pfn, flags);
 }
 
 /*
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

Quoted 10 lines · Jane Chu

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

I am missing some details here.
An unmapped hwpoison page will trigger a fault and will return VM_FAULT_HWPOISON all the way down and then deliver SIGBUS, but if the page was not unmapped, how will this be catch upon re-accessing? Will the system deliver a MCE event?

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#3 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

Quoted 11 lines · Oscar Salvador
Quoted 10 lines · Jane Chu

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

Quoted 6 lines

I am missing some details here.
An unmapped hwpoison page will trigger a fault and will return VM_FAULT_HWPOISON all the way down and then deliver SIGBUS, but if the page was not unmapped, how will this be catch upon re-accessing? Will the system deliver a MCE event?

I actually managed to hit the re-access case with an older version of Linux -

MCE occurred, but unmap failed,  no SIGBUS and test process re-access

the same address over and over (hence MCE after MCE), as the CPU

was unable to make forward progress.   In reality, this issue is fixed with

kill_accessing_processes().  The comment for this patch refers to comment made

about '!unmap_access' long time ago.

thanks,

-jane

#4 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

Quoted 11 lines · Jane Chu

I actually managed to hit the re-access case with an older version of Linux -

MCE occurred, but unmap failed,  no SIGBUS and test process re-access

the same address over and over (hence MCE after MCE), as the CPU

was unable to make forward progress.   In reality, this issue is fixed with

kill_accessing_processes().  The comment for this patch refers to comment made

So we get a faulty page and we try to unmap it from all processes that might have it mapped in their pgtables.
Prior to this patch we would kill the processes right away and now we deliver a SIGBUS.

Seems safe as upon-reaccesing kill_accessing_process() will be called for already hwpoisoned pages.

I think the changelog could be made more explicit about this scenario and state the role of kill_accessing_process more clear.

With that: Reviewed-by: Oscar Salvador <[email protected]>

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#5 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

Quoted 17 lines · Oscar Salvador
Quoted 11 lines · Jane Chu

I actually managed to hit the re-access case with an older version of Linux -

MCE occurred, but unmap failed,  no SIGBUS and test process re-access

the same address over and over (hence MCE after MCE), as the CPU

was unable to make forward progress.   In reality, this issue is fixed with

kill_accessing_processes().  The comment for this patch refers to comment made

So we get a faulty page and we try to unmap it from all processes that might have it mapped in their pgtables.
Prior to this patch we would kill the processes right away and now we deliver a SIGBUS.

Quoted 8 lines

Seems safe as upon-reaccesing kill_accessing_process() will be called for already hwpoisoned pages.

I think the changelog could be made more explicit about this scenario and state the role of kill_accessing_process more clear.

With that: Reviewed-by: Oscar Salvador <[email protected]>

I will revise the changelog and mention kill_accessing_process().

Thanks!

-jane

Quoted 1 line
maintainer
#6 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

In reply to Jane Chu
Quoted 21 lines · Jane Chu

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

---
 mm/memory-failure.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/memory-failure.c +0 -0
index 9e62a00b46dd..7fcf182abb96 100644

mm/memory-failure.c void add_to_kill_ksm(struct task_struct *tsk, struct page *p, · line 520

Quoted 2 lines
mm/memory-failure.c +0 -0
@@ -519,19 +519,14 @@ void add_to_kill_ksm(struct task_struct *tsk, struct page *p,
  * Also when FAIL is set do a force kill because something went
  * wrong earlier.

Since @fail is removed, above comment should be removed too. Thanks.
.

author
#7 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

Quoted 17 lines · Miaohe Lin
Quoted 16 lines · Jane Chu

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

---
  mm/memory-failure.c | 13 ++++---------
  1 file changed, 4 insertions(+), 9 deletions(-)
Quoted 10 lines

mm/memory-failure.c void add_to_kill_ksm(struct task_struct *tsk, struct page *p, · line 520

Quoted 7 lines
mm/memory-failure.c +0 -0
index 9e62a00b46dd..7fcf182abb96 100644
@@ -519,19 +519,14 @@ void add_to_kill_ksm(struct task_struct *tsk, struct page *p,
   * Also when FAIL is set do a force kill because something went
   * wrong earlier.

Since @fail is removed, above comment should be removed too. Thanks.
.

Good catch, will do.

Thanks!

-jane

maintainer
#8 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

In reply to Jane Chu
Quoted 43 lines · Jane Chu

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

---
 mm/memory-failure.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
mm/memory-failure.c +2 -7
index 9e62a00b46dd..7fcf182abb96 100644
@@ -519,19 +519,14 @@ void add_to_kill_ksm(struct task_struct *tsk, struct page *p,
  * Also when FAIL is set do a force kill because something went
  * wrong earlier.
  */
-static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
+static void kill_procs(struct list_head *to_kill, int forcekill,
 		unsigned long pfn, int flags)
 {
 	struct to_kill *tk, *next;
 
 	list_for_each_entry_safe(tk, next, to_kill, nd) {
 		if (forcekill) {
-			/*
-			 * In case something went wrong with munmapping
-			 * make sure the process doesn't catch the
-			 * signal and then access the memory. Just kill it.
-			 */
-			if (fail || tk->addr == -EFAULT) {
+			if (tk->addr == -EFAULT) {
 				pr_err("%#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
 				       pfn, tk->tsk->comm, tk->tsk->pid);
 				do_send_sig_info(SIGKILL, SEND_SIG_PRIV,

mm/memory-failure.c static bool hwpoison_user_mappings(struct page *p, unsigned long pfn, · line 1661

Quoted 1 line
mm/memory-failure.c +0 -0
@@ -1666,7 +1661,7 @@ static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
 	 */

There is comment above the forcekill saying:

When there was a problem unmapping earlier use a more force-full
uncatchable kill to prevent any accesses to the poisoned memory.

This might need to be changed too.
Thanks.
.

Quoted 17 lines
mm/memory-failure.c +2 -2
 	forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL) ||
 		    !unmap_success;
-	kill_procs(&tokill, forcekill, !unmap_success, pfn, flags);
+	kill_procs(&tokill, forcekill, pfn, flags);
 
 	return unmap_success;
 }
@@ -1730,7 +1725,7 @@ static void unmap_and_kill(struct list_head *to_kill, unsigned long pfn,
 		unmap_mapping_range(mapping, start, size, 0);
 	}
 
-	kill_procs(to_kill, flags & MF_MUST_KILL, false, pfn, flags);
+	kill_procs(to_kill, flags & MF_MUST_KILL, pfn, flags);
 }
 
 /*
author
#9 lore raw

Re: [PATCH 1/3] mm/memory-failure: try to send SIGBUS even if unmap failed

Quoted 47 lines · Miaohe Lin
Quoted 43 lines · Jane Chu

For years when it comes down to kill a process due to hwpoison, a SIGBUS is delivered only if unmap has been successful. Otherwise, a SIGKILL is delivered. And the reason for that is to prevent the involved process from accessing the hwpoisoned page again.

Since then a lot has changed, a hwpoisoned page is marked and upon being re-accessed, the process will be killed immediately. So let's take out the '!unmap_success' factor and try to deliver SIGBUS if possible.

---
  mm/memory-failure.c | 13 ++++---------
  1 file changed, 4 insertions(+), 9 deletions(-)
mm/memory-failure.c +2 -7
index 9e62a00b46dd..7fcf182abb96 100644
@@ -519,19 +519,14 @@ void add_to_kill_ksm(struct task_struct *tsk, struct page *p,
   * Also when FAIL is set do a force kill because something went
   * wrong earlier.
   */
-static void kill_procs(struct list_head *to_kill, int forcekill, bool fail,
+static void kill_procs(struct list_head *to_kill, int forcekill,
  		unsigned long pfn, int flags)
  {
  	struct to_kill *tk, *next;
 
  	list_for_each_entry_safe(tk, next, to_kill, nd) {
  		if (forcekill) {
-			/*
-			 * In case something went wrong with munmapping
-			 * make sure the process doesn't catch the
-			 * signal and then access the memory. Just kill it.
-			 */
-			if (fail || tk->addr == -EFAULT) {
+			if (tk->addr == -EFAULT) {
  				pr_err("%#lx: forcibly killing %s:%d because of failure to unmap corrupted page\n",
  				       pfn, tk->tsk->comm, tk->tsk->pid);
  				do_send_sig_info(SIGKILL, SEND_SIG_PRIV,

mm/memory-failure.c static bool hwpoison_user_mappings(struct page *p, unsigned long pfn, · line 1661

Quoted 1 line
mm/memory-failure.c +0 -0
@@ -1666,7 +1661,7 @@ static bool hwpoison_user_mappings(struct page *p, unsigned long pfn,
  	 */

There is comment above the forcekill saying:

Quoted 4 lines

When there was a problem unmapping earlier use a more force-full
uncatchable kill to prevent any accesses to the poisoned memory.

This might need to be changed too.

Yes, will do.

thanks!

-jane

Quoted 21 lines

Thanks.
.

Quoted 17 lines
mm/memory-failure.c +2 -2
  	forcekill = PageDirty(hpage) || (flags & MF_MUST_KILL) ||
  		    !unmap_success;
-	kill_procs(&tokill, forcekill, !unmap_success, pfn, flags);
+	kill_procs(&tokill, forcekill, pfn, flags);
 
  	return unmap_success;
  }
@@ -1730,7 +1725,7 @@ static void unmap_and_kill(struct list_head *to_kill, unsigned long pfn,
  		unmap_mapping_range(mapping, start, size, 0);
  	}
 
-	kill_procs(to_kill, flags & MF_MUST_KILL, false, pfn, flags);
+	kill_procs(to_kill, flags & MF_MUST_KILL, pfn, flags);
  }
 
  /*
author
#10 lore raw

PATCH 2/3 mm/madvise: Add MF_ACTION_REQUIRED to madvise(MADV_HWPOISON)

In reply to Jane Chu

The soft hwpoison injector via madvise(MADV_HWPOISON) operates in a synchrous way in a sense, the injector is also a process under test, and should it have the poisoned page mapped in its address space, it should legitimately get killed as much as in a real UE situation.

---
 mm/madvise.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/madvise.c +1 -1
index 1a073fcc4c0c..eaeae5252c02 100644
@@ -1127,7 +1127,7 @@ static int madvise_inject_error(int behavior,
 		} else {
 			pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
 				 pfn, start);
-			ret = memory_failure(pfn, MF_COUNT_INCREASED | MF_SW_SIMULATED);
+			ret = memory_failure(pfn, MF_ACTION_REQUIRED | MF_COUNT_INCREASED | MF_SW_SIMULATED);
 			if (ret == -EOPNOTSUPP)
 				ret = 0;
 		}
Signature · 1 line
2.39.3
maintainer
#11 lore raw

Re: [PATCH 2/3] mm/madvise: Add MF_ACTION_REQUIRED to madvise(MADV_HWPOISON)

Quoted 5 lines · Jane Chu

The soft hwpoison injector via madvise(MADV_HWPOISON) operates in a synchrous way in a sense, the injector is also a process under test, and should it have the poisoned page mapped in its address space, it should legitimately get killed as much as in a real UE situation.

Will it be better to add a method to set MF_ACTION_REQUIRED explicitly when inject soft hwpoison? Thanks.
.

Quoted 20 lines
---
 mm/madvise.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/madvise.c +1 -1
index 1a073fcc4c0c..eaeae5252c02 100644
@@ -1127,7 +1127,7 @@ static int madvise_inject_error(int behavior,
 		} else {
 			pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
 				 pfn, start);
-			ret = memory_failure(pfn, MF_COUNT_INCREASED | MF_SW_SIMULATED);
+			ret = memory_failure(pfn, MF_ACTION_REQUIRED | MF_COUNT_INCREASED | MF_SW_SIMULATED);
 			if (ret == -EOPNOTSUPP)
 				ret = 0;
 		}
author
#12 lore raw

Re: [PATCH 2/3] mm/madvise: Add MF_ACTION_REQUIRED to madvise(MADV_HWPOISON)

Quoted 8 lines · Miaohe Lin
Quoted 5 lines · Jane Chu

The soft hwpoison injector via madvise(MADV_HWPOISON) operates in a synchrous way in a sense, the injector is also a process under test, and should it have the poisoned page mapped in its address space, it should legitimately get killed as much as in a real UE situation.

Will it be better to add a method to set MF_ACTION_REQUIRED explicitly when inject soft hwpoison? Thanks.

So the first question is: Is there a need to preserve the existing behavior of  madvise(MADV_HWPOISON)?

The madvise(2) man page says -

*MADV_HWPOISON *(since Linux 2.6.32)
Poison the pages in the range specified by/addr/ and/length/ and handle subsequent references to those pages like a hardware memory corruption. This operation is available only for privileged (*CAP_SYS_ADMIN*) processes. This operation may result in the calling process receiving a *SIGBUS *and the page being unmapped.

This feature is intended for testing of memory error- handling code; it is available only if the kernel was configured with*CONFIG_MEMORY_FAILURE*.

And the impression from my reading is that: there doesn't seem to be a need.

A couple observations -
- The man page states that the calling process may receive a SIGBUS and the page being unmapped. But the existing behavior is no SIGBUS unless MCE early kill is elected, so it doesn't quite match the man page.
- There is 'hwpoison-inject' which behaves similar to the existing madvise(MADV_HWPOISON), that is, soft inject without MF_ACTION_REQUIRED flag.

thanks,
-jane

Quoted 21 lines

.

Quoted 19 lines
---
  mm/madvise.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/madvise.c +1 -1
index 1a073fcc4c0c..eaeae5252c02 100644
@@ -1127,7 +1127,7 @@ static int madvise_inject_error(int behavior,
  		} else {
  			pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
  				 pfn, start);
-			ret = memory_failure(pfn, MF_COUNT_INCREASED | MF_SW_SIMULATED);
+			ret = memory_failure(pfn, MF_ACTION_REQUIRED | MF_COUNT_INCREASED | MF_SW_SIMULATED);
  			if (ret == -EOPNOTSUPP)
  				ret = 0;
  		}
maintainer
#13 lore raw

Re: [PATCH 2/3] mm/madvise: Add MF_ACTION_REQUIRED to madvise(MADV_HWPOISON)

Quoted 27 lines · Jane Chu
Quoted 8 lines · Miaohe Lin
Quoted 5 lines · Jane Chu

The soft hwpoison injector via madvise(MADV_HWPOISON) operates in a synchrous way in a sense, the injector is also a process under test, and should it have the poisoned page mapped in its address space, it should legitimately get killed as much as in a real UE situation.

Will it be better to add a method to set MF_ACTION_REQUIRED explicitly when inject soft hwpoison? Thanks.

So the first question is: Is there a need to preserve the existing behavior of  madvise(MADV_HWPOISON)?

The madvise(2) man page says -

       *MADV_HWPOISON *(since Linux 2.6.32)
              Poison the pages in the range specified by/addr/  and/length/ and handle subsequent references to those pages like a hardware memory corruption.  This operation is available only for privileged (*CAP_SYS_ADMIN*) processes.  This operation may result in the calling process receiving a *SIGBUS *and the page being unmapped.

              This feature is intended for testing of memory error- handling code; it is available only if the kernel was configured with*CONFIG_MEMORY_FAILURE*.

Quoted 9 lines

And the impression from my reading is that: there doesn't seem to be a need.

A couple observations -
- The man page states that the calling process may receive a SIGBUS and the page being unmapped. But the existing behavior is no SIGBUS unless MCE early kill is elected, so it doesn't quite match the man page.
- There is 'hwpoison-inject' which behaves similar to the existing madvise(MADV_HWPOISON), that is, soft inject without MF_ACTION_REQUIRED flag.

I tend to agree with you. It might be a good idea to add MF_ACTION_REQUIRED to madvise(MADV_HWPOISON). Thanks.
.

author
#14 lore raw

PATCH 3/3 mm/memory-failure: send SIGBUS in the event of thp split fail

In reply to Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

---
 mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
mm/memory-failure.c +36 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
 	return rc;
 }
 
+/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+			struct page *hpage)
+{
+	struct folio *folio = page_folio(hpage);
+	LIST_HEAD(tokill);
+	int res = -EHWPOISON;
+
+	/* deal with user pages only */
+	if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p))
+		res = -EBUSY;
+	if (!(PageLRU(hpage) || PageHuge(p)))
+		res = -EBUSY;
+
+	if (res == -EHWPOISON) {
+		collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+		kill_procs(&tokill, true, pfn, flags);
+	}
+
+	if (flags & MF_COUNT_INCREASED)
+		put_page(p);
+
+	return res;
+}
+
 /**
  * memory_failure - Handle memory failure of a page.
  * @pfn: Page Number of the corrupted page
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
 		 */
 		SetPageHasHWPoisoned(hpage);
 		if (try_to_split_thp_page(p) < 0) {
+			if (flags & MF_ACTION_REQUIRED) {
+				pr_err("%#lx: thp split failed\n", pfn);
+				res = kill_procs_now(p, pfn, flags, hpage);
+				goto unlock_mutex;
+			}
 			res = action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
 			goto unlock_mutex;
 		}
Signature · 1 line
2.39.3
maintainer
#15 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 21 lines
---
 mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
mm/memory-failure.c +9 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
 	return rc;
 }
 
+/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+			struct page *hpage)

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2189

Quoted 10 lines
mm/memory-failure.c +10 -0
+{
+	struct folio *folio = page_folio(hpage);
+	LIST_HEAD(tokill);
+	int res = -EHWPOISON;
+
+	/* deal with user pages only */
+	if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p))
+		res = -EBUSY;
+	if (!(PageLRU(hpage) || PageHuge(p)))
+		res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+	if (res == -EHWPOISON) {
+		collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+		kill_procs(&tokill, true, pfn, flags);
+	}
+
+	if (flags & MF_COUNT_INCREASED)
+		put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2198

Quoted 1 line
mm/memory-failure.c +1 -0
+

action_result is missing?

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2330

Quoted 10 lines
mm/memory-failure.c +3 -0
+	return res;
+}
+
 /**
  * memory_failure - Handle memory failure of a page.
  * @pfn: Page Number of the corrupted page
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
 		 */
 		SetPageHasHWPoisoned(hpage);
 		if (try_to_split_thp_page(p) < 0) {

Should hwpoison_filter() be called in this case?

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2333

Quoted 3 lines
mm/memory-failure.c +3 -0
+			if (flags & MF_ACTION_REQUIRED) {
+				pr_err("%#lx: thp split failed\n", pfn);
+				res = kill_procs_now(p, pfn, flags, hpage);

Can we use hwpoison_user_mappings() directly here?

Thanks.
.

Quoted 6 lines
mm/memory-failure.c +2 -0
+				goto unlock_mutex;
+			}
 			res = action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
 			goto unlock_mutex;
 		}
author
#16 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 33 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 24 lines
---
  mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
  1 file changed, 36 insertions(+)
mm/memory-failure.c +12 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
  	return rc;
  }
 
+/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+			struct page *hpage)
+{
+	struct folio *folio = page_folio(hpage);
+	LIST_HEAD(tokill);
Quoted 8 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2189

Quoted 7 lines
mm/memory-failure.c +7 -0
+	int res = -EHWPOISON;
+
+	/* deal with user pages only */
+	if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p))
+		res = -EBUSY;
+	if (!(PageLRU(hpage) || PageHuge(p)))
+		res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 10 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 9 lines
mm/memory-failure.c +8 -0
+
+	if (res == -EHWPOISON) {
+		collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+		kill_procs(&tokill, true, pfn, flags);
+	}
+
+	if (flags & MF_COUNT_INCREASED)
+		put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

I will revise the code so that put_page() is called regardless MF_ACTION_REQUIRED is set or not.

Quoted 3 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2198

Quoted 2 lines
mm/memory-failure.c +1 -0
+

action_result is missing?

Indeed,  action_result() isn't always called, referring to the re-accessing hwpoison scenarios.

In this case, I think the reason  is that, we just killed the process and there is nothing

else to do or to report.

Quoted 12 lines

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2330

Quoted 11 lines
mm/memory-failure.c +3 -0
+	return res;
+}
+
  /**
   * memory_failure - Handle memory failure of a page.
   * @pfn: Page Number of the corrupted page
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
  		 */
  		SetPageHasHWPoisoned(hpage);
  		if (try_to_split_thp_page(p) < 0) {

Should hwpoison_filter() be called in this case?

Yes, it should. I will add the hwpoison_filter check.

Quoted 5 lines

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2333

Quoted 4 lines
mm/memory-failure.c +3 -0
+			if (flags & MF_ACTION_REQUIRED) {
+				pr_err("%#lx: thp split failed\n", pfn);
+				res = kill_procs_now(p, pfn, flags, hpage);

Can we use hwpoison_user_mappings() directly here?

I thought about using hwpoison_user_mappings() with an extra flag, but gave up in the end.

Because most of the code there are not needed, such as the checks you mentioned above,

and umapping etc.

thanks!

-jane

Quoted 10 lines

Thanks.
.

Quoted 6 lines
mm/memory-failure.c +2 -0
+				goto unlock_mutex;
+			}
  			res = action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
  			goto unlock_mutex;
  		}
maintainer
#17 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 62 lines · Jane Chu
Quoted 32 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 23 lines

---
  mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++

  1 file changed, 36 insertions(+)
mm/memory-failure.c +0 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
       return rc;
   }

  +/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */

mm/memory-failure.c +5 -0
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+            struct page *hpage)
+{
+    struct folio *folio = page_folio(hpage);
+    LIST_HEAD(tokill);
Quoted 8 lines
Quoted 7 lines

+    int res = -EHWPOISON;
+
+    /* deal with user pages only */
+    if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p)) +        res = -EBUSY;
+    if (!(PageLRU(hpage) || PageHuge(p)))
+        res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 10 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 9 lines
mm/memory-failure.c +8 -0
+
+    if (res == -EHWPOISON) {
+        collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+        kill_procs(&tokill, true, pfn, flags);
+    }
+
+    if (flags & MF_COUNT_INCREASED)
+        put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

Quoted 1 line

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

In try_to_split_thp_page(), if split_huge_page fails, i.e. ret != 0, put_page() is called. See below:

static int try_to_split_thp_page(struct page *page)

{
	int ret;

	lock_page(page);
	ret = split_huge_page(page);
	unlock_page(page);

	if (unlikely(ret))
		put_page(page);
	^^^^^^^^^^^^^^^^^^^^^^^
	return ret;
}

Or am I miss something?

Quoted 25 lines

I will revise the code so that put_page() is called regardless MF_ACTION_REQUIRED is set or not.

Quoted 3 lines
Quoted 2 lines

+

action_result is missing?

Indeed,  action_result() isn't always called, referring to the re-accessing hwpoison scenarios.

In this case, I think the reason  is that, we just killed the process and there is nothing

else to do or to report.

Quoted 12 lines

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2330

Quoted 11 lines

+    return res;

mm/memory-failure.c +2 -0
+}
+

  /**
   * memory_failure - Handle memory failure of a page.
   * @pfn: Page Number of the corrupted page

mm/memory-failure.c +0 -0
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
            */
           SetPageHasHWPoisoned(hpage);
           if (try_to_split_thp_page(p) < 0) {

Should hwpoison_filter() be called in this case?

Quoted 3 lines

Yes, it should. I will add the hwpoison_filter check.

Quoted 2 lines
Quoted 2 lines
mm/memory-failure.c +1 -0
+            if (flags & MF_ACTION_REQUIRED) {

Only in MF_ACTION_REQUIRED case, SIGBUS is sent to processes when thp split failed. Any reson under it?

Thanks.
.

author
#18 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 77 lines · Miaohe Lin
Quoted 59 lines · Jane Chu
Quoted 32 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 23 lines
---
   mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
   1 file changed, 36 insertions(+)
mm/memory-failure.c +11 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
       return rc;
   }
   +/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+            struct page *hpage)
+{
+    struct folio *folio = page_folio(hpage);
+    LIST_HEAD(tokill);
Quoted 8 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2189

Quoted 7 lines
mm/memory-failure.c +7 -0
+    int res = -EHWPOISON;
+
+    /* deal with user pages only */
+    if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p))
+        res = -EBUSY;
+    if (!(PageLRU(hpage) || PageHuge(p)))
+        res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 9 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+    if (res == -EHWPOISON) {
+        collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+        kill_procs(&tokill, true, pfn, flags);
+    }
+
+    if (flags & MF_COUNT_INCREASED)
+        put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

Quoted 1 line

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

In try_to_split_thp_page(), if split_huge_page fails, i.e. ret != 0, put_page() is called. See below:

static int try_to_split_thp_page(struct page *page)

{
	int ret;

	lock_page(page);
	ret = split_huge_page(page);
	unlock_page(page);

	if (unlikely(ret))
		put_page(page);
	^^^^^^^^^^^^^^^^^^^^^^^
	return ret;
}
Quoted 1 line

Or am I miss something?

I think you caught a bug in my code, thanks!

How about moving put_page() outside try_to_split_thp_page() ?

Quoted 22 lines
Quoted 22 lines

I will revise the code so that put_page() is called regardless MF_ACTION_REQUIRED is set or not.

Quoted 2 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 1 line
mm/memory-failure.c +1 -0
+

action_result is missing?

Indeed,  action_result() isn't always called, referring to the re-accessing hwpoison scenarios.

In this case, I think the reason  is that, we just killed the process and there is nothing

else to do or to report.

Quoted 11 lines

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2330

Quoted 10 lines
mm/memory-failure.c +3 -0
+    return res;
+}
+
   /**
    * memory_failure - Handle memory failure of a page.
    * @pfn: Page Number of the corrupted page
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
            */
           SetPageHasHWPoisoned(hpage);
           if (try_to_split_thp_page(p) < 0) {

Should hwpoison_filter() be called in this case?

Quoted 3 lines
Quoted 2 lines

Yes, it should. I will add the hwpoison_filter check.

Quoted 1 line
Quoted 1 line
mm/memory-failure.c +1 -0
+            if (flags & MF_ACTION_REQUIRED) {

Only in MF_ACTION_REQUIRED case, SIGBUS is sent to processes when thp split failed. Any reson under it?

I took a clue from kill_accessing_process() which is invoked only if MF_ACTION_REQUIRED is set.

The usual code path for delivery signal is

if page-is-dirty or MF_MUST_KILL-is-set or umap-failed, then

- send SIGKILL if vaddr is -EFAULT

- send SIGBUS with BUS_MCEERR_AR if MF_ACTION_REQUIRED is set

- send SIGBUS with BUS_MCEERR_AO if MF_ACTION_REQUIRED is not set and process elected for MCE-early-kill

So, if kill_procs_now() is invoked only if MF_ACTION_REQUIRED (as it is in the patch), one can argue that

the MCE-early-kill request is not honored which deviates from the existing behavior.

Perhaps I should remove the

+ if (flags & MF_ACTION_REQUIRED) {

check.

thanks!

-jane

Quoted 3 lines

Thanks.
.

maintainer
#19 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 79 lines · Jane Chu
Quoted 77 lines · Miaohe Lin
Quoted 59 lines · Jane Chu
Quoted 32 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 23 lines

---
   mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++

   1 file changed, 36 insertions(+)
mm/memory-failure.c +0 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
        return rc;
    }

   +/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */

mm/memory-failure.c +5 -0
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+            struct page *hpage)
+{
+    struct folio *folio = page_folio(hpage);
+    LIST_HEAD(tokill);
Quoted 8 lines
Quoted 7 lines

+    int res = -EHWPOISON;
+
+    /* deal with user pages only */
+    if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p)) +        res = -EBUSY;
+    if (!(PageLRU(hpage) || PageHuge(p)))
+        res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 9 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+    if (res == -EHWPOISON) {
+        collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+        kill_procs(&tokill, true, pfn, flags);
+    }
+
+    if (flags & MF_COUNT_INCREASED)
+        put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

Quoted 1 line

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

In try_to_split_thp_page(), if split_huge_page fails, i.e. ret != 0, put_page() is called. See below:

static int try_to_split_thp_page(struct page *page) {
    int ret;

    lock_page(page);
    ret = split_huge_page(page);
    unlock_page(page);

    if (unlikely(ret))
        put_page(page);
    ^^^^^^^^^^^^^^^^^^^^^^^
    return ret;
}

Quoted 5 lines
Quoted 1 line

Or am I miss something?

I think you caught a bug in my code, thanks!

How about moving put_page() outside try_to_split_thp_page() ?

If you want to send SIGBUS in the event of thp split fail, it might be required to do so. I think kill_procs_now() needs extra thp refcnt to do its work.

Quoted 39 lines
Quoted 23 lines
Quoted 23 lines

I will revise the code so that put_page() is called regardless MF_ACTION_REQUIRED is set or not.

Quoted 2 lines
Quoted 1 line

+

action_result is missing?

Indeed,  action_result() isn't always called, referring to the re-accessing hwpoison scenarios.

In this case, I think the reason  is that, we just killed the process and there is nothing

else to do or to report.

Quoted 11 lines

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2330

Quoted 10 lines

+    return res;

mm/memory-failure.c +2 -0
+}
+

   /**
    * memory_failure - Handle memory failure of a page.
    * @pfn: Page Number of the corrupted page

mm/memory-failure.c +0 -0
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
             */
            SetPageHasHWPoisoned(hpage);
            if (try_to_split_thp_page(p) < 0) {

Should hwpoison_filter() be called in this case?

Quoted 3 lines
Quoted 2 lines

Yes, it should. I will add the hwpoison_filter check.

Quoted 1 line
Quoted 1 line
mm/memory-failure.c +1 -0
+            if (flags & MF_ACTION_REQUIRED) {

Only in MF_ACTION_REQUIRED case, SIGBUS is sent to processes when thp split failed. Any reson under it?

I took a clue from kill_accessing_process() which is invoked only if MF_ACTION_REQUIRED is set.

The usual code path for delivery signal is

if page-is-dirty or MF_MUST_KILL-is-set or umap-failed, then

- send SIGKILL if vaddr is -EFAULT

- send SIGBUS with BUS_MCEERR_AR if MF_ACTION_REQUIRED is set

- send SIGBUS with BUS_MCEERR_AO if MF_ACTION_REQUIRED is not set and process elected for MCE-early-kill

Quoted 7 lines

So, if kill_procs_now() is invoked only if MF_ACTION_REQUIRED (as it is in the patch), one can argue that

the MCE-early-kill request is not honored which deviates from the existing behavior.

Perhaps I should remove the

+ if (flags & MF_ACTION_REQUIRED) {

I tend to agree MCE-early-kill request should be honored when try to kill process. Thanks.
.

author
#20 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 80 lines · Miaohe Lin
Quoted 79 lines · Jane Chu
Quoted 77 lines · Miaohe Lin
Quoted 59 lines · Jane Chu
Quoted 32 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 23 lines
---
    mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
    1 file changed, 36 insertions(+)
mm/memory-failure.c +11 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
        return rc;
    }
    +/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+            struct page *hpage)
+{
+    struct folio *folio = page_folio(hpage);
+    LIST_HEAD(tokill);
Quoted 8 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2189

Quoted 7 lines
mm/memory-failure.c +7 -0
+    int res = -EHWPOISON;
+
+    /* deal with user pages only */
+    if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p))
+        res = -EBUSY;
+    if (!(PageLRU(hpage) || PageHuge(p)))
+        res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 9 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+    if (res == -EHWPOISON) {
+        collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+        kill_procs(&tokill, true, pfn, flags);
+    }
+
+    if (flags & MF_COUNT_INCREASED)
+        put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

Quoted 1 line

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

In try_to_split_thp_page(), if split_huge_page fails, i.e. ret != 0, put_page() is called. See below:

static int try_to_split_thp_page(struct page *page) {
    int ret;

    lock_page(page);
    ret = split_huge_page(page);
    unlock_page(page);

    if (unlikely(ret))
        put_page(page);
    ^^^^^^^^^^^^^^^^^^^^^^^
    return ret;
}

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Or am I miss something?

I think you caught a bug in my code, thanks!

How about moving put_page() outside try_to_split_thp_page() ?

If you want to send SIGBUS in the event of thp split fail, it might be required to do so. I think kill_procs_now() needs extra thp refcnt to do its work.

Agreed.  I added an boolean to try_to_split_thp_page(),the boolean indicates whether to put_page().

In case of kill_procs_now(), put_page() is called afterwards.

Quoted 39 lines
Quoted 39 lines
Quoted 22 lines
Quoted 22 lines

I will revise the code so that put_page() is called regardless MF_ACTION_REQUIRED is set or not.

Quoted 2 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2197

Quoted 1 line
mm/memory-failure.c +1 -0
+

action_result is missing?

Indeed,  action_result() isn't always called, referring to the re-accessing hwpoison scenarios.

In this case, I think the reason  is that, we just killed the process and there is nothing

else to do or to report.

Quoted 11 lines

mm/memory-failure.c int memory_failure(unsigned long pfn, int flags) · line 2330

Quoted 10 lines
mm/memory-failure.c +3 -0
+    return res;
+}
+
    /**
     * memory_failure - Handle memory failure of a page.
     * @pfn: Page Number of the corrupted page
@@ -2297,6 +2328,11 @@ int memory_failure(unsigned long pfn, int flags)
             */
            SetPageHasHWPoisoned(hpage);
            if (try_to_split_thp_page(p) < 0) {

Should hwpoison_filter() be called in this case?

Quoted 3 lines
Quoted 2 lines

Yes, it should. I will add the hwpoison_filter check.

Quoted 1 line
Quoted 1 line
mm/memory-failure.c +1 -0
+            if (flags & MF_ACTION_REQUIRED) {

Only in MF_ACTION_REQUIRED case, SIGBUS is sent to processes when thp split failed. Any reson under it?

I took a clue from kill_accessing_process() which is invoked only if MF_ACTION_REQUIRED is set.

The usual code path for delivery signal is

if page-is-dirty or MF_MUST_KILL-is-set or umap-failed, then

- send SIGKILL if vaddr is -EFAULT

- send SIGBUS with BUS_MCEERR_AR if MF_ACTION_REQUIRED is set

- send SIGBUS with BUS_MCEERR_AO if MF_ACTION_REQUIRED is not set and process elected for MCE-early-kill

So, if kill_procs_now() is invoked only if MF_ACTION_REQUIRED (as it is in the patch), one can argue that

Quoted 8 lines
Quoted 5 lines

the MCE-early-kill request is not honored which deviates from the existing behavior.

Perhaps I should remove the

+ if (flags & MF_ACTION_REQUIRED) {

I tend to agree MCE-early-kill request should be honored when try to kill process. Thanks.
.

Thanks,

-jane

Quoted 1 line
maintainer
#21 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 82 lines · Jane Chu
Quoted 80 lines · Miaohe Lin
Quoted 79 lines · Jane Chu
Quoted 77 lines · Miaohe Lin
Quoted 59 lines · Jane Chu
Quoted 32 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 23 lines

---
    mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++

    1 file changed, 36 insertions(+)
mm/memory-failure.c +0 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
         return rc;
     }

    +/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */

mm/memory-failure.c +5 -0
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+            struct page *hpage)
+{
+    struct folio *folio = page_folio(hpage);
+    LIST_HEAD(tokill);
Quoted 8 lines
Quoted 7 lines

+    int res = -EHWPOISON;
+
+    /* deal with user pages only */
+    if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p)) +        res = -EBUSY;
+    if (!(PageLRU(hpage) || PageHuge(p)))
+        res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 9 lines

mm/memory-failure.c

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+    if (res == -EHWPOISON) {
+        collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+        kill_procs(&tokill, true, pfn, flags);
+    }
+
+    if (flags & MF_COUNT_INCREASED)
+        put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

Quoted 1 line

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

In try_to_split_thp_page(), if split_huge_page fails, i.e. ret != 0, put_page() is called. See below:

static int try_to_split_thp_page(struct page *page) {
     int ret;

     lock_page(page);
     ret = split_huge_page(page);
     unlock_page(page);

     if (unlikely(ret))
         put_page(page);
     ^^^^^^^^^^^^^^^^^^^^^^^
     return ret;
}

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Or am I miss something?

I think you caught a bug in my code, thanks!

How about moving put_page() outside try_to_split_thp_page() ?

If you want to send SIGBUS in the event of thp split fail, it might be required to do so. I think kill_procs_now() needs extra thp refcnt to do its work.

Agreed.  I added an boolean to try_to_split_thp_page(),the boolean indicates whether to put_page().

IMHO, it might be too complicated to add an extra boolean to indicate whether to put_page(). It might be more straightforward to always put_page outside try_to_split_thp_page?
Thanks.
.

author
#22 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 83 lines · Miaohe Lin
Quoted 82 lines · Jane Chu
Quoted 81 lines · Miaohe Lin
Quoted 80 lines · Jane Chu
Quoted 78 lines · Miaohe Lin
Quoted 59 lines · Jane Chu
Quoted 32 lines · Miaohe Lin
Quoted 6 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Thanks for your patch. Some comments below.

Quoted 23 lines
---
     mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
     1 file changed, 36 insertions(+)
mm/memory-failure.c +11 -0
index 7fcf182abb96..67f4d24a98e7 100644
@@ -2168,6 +2168,37 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
         return rc;
     }
     +/*
+ * The calling condition is as such: thp split failed, page might have
+ * been GUP longterm pinned, not much can be done for recovery.
+ * But a SIGBUS should be delivered with vaddr provided so that the user
+ * application has a chance to recover. Also, application processes'
+ * election for MCE early killed will be honored.
+ */
+static int kill_procs_now(struct page *p, unsigned long pfn, int flags,
+            struct page *hpage)
+{
+    struct folio *folio = page_folio(hpage);
+    LIST_HEAD(tokill);
Quoted 8 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2188

Quoted 7 lines
mm/memory-failure.c +7 -0
+    int res = -EHWPOISON;
+
+    /* deal with user pages only */
+    if (PageReserved(p) || PageSlab(p) || PageTable(p) || PageOffline(p))
+        res = -EBUSY;
+    if (!(PageLRU(hpage) || PageHuge(p)))
+        res = -EBUSY;

Above checks seems unneeded. We already know it's thp?

Agreed.

I  lifted these checks from hwpoison_user_mapping() with a hope to make kill_procs_now() more generic,

such as, potentially replacing kill_accessing_processes() for re-accessing hwpoisoned page.

But I backed out at last, due to concerns that my tests might not have covered sufficient number of scenarios.

Quoted 9 lines

mm/memory-failure.c static int memory_failure_dev_pagemap(unsigned long pfn, int flags, · line 2196

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+    if (res == -EHWPOISON) {
+        collect_procs(folio, p, &tokill, flags & MF_ACTION_REQUIRED);
+        kill_procs(&tokill, true, pfn, flags);
+    }
+
+    if (flags & MF_COUNT_INCREASED)
+        put_page(p);

This if block is broken. put_page() has been done when try_to_split_thp_page() fails?

Quoted 1 line

put_page() has not been done if try_to_split_thp_page() fails, and I think it should.

In try_to_split_thp_page(), if split_huge_page fails, i.e. ret != 0, put_page() is called. See below:

static int try_to_split_thp_page(struct page *page) {
     int ret;

     lock_page(page);
     ret = split_huge_page(page);
     unlock_page(page);

     if (unlikely(ret))
         put_page(page);
     ^^^^^^^^^^^^^^^^^^^^^^^
     return ret;
}

Or am I miss something?

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

I think you caught a bug in my code, thanks!

How about moving put_page() outside try_to_split_thp_page() ?

If you want to send SIGBUS in the event of thp split fail, it might be required to do so. I think kill_procs_now() needs extra thp refcnt to do its work.

Agreed.  I added an boolean to try_to_split_thp_page(),the boolean indicates whether to put_page().

IMHO, it might be too complicated to add an extra boolean to indicate whether to put_page(). It might be more straightforward to always put_page outside try_to_split_thp_page?

Looks okay to me, let's see.  Will send out v2 in a while.

thanks,

-jane

Quoted 3 lines

Thanks.
.

maintainer
#23 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

In reply to Jane Chu
Quoted 4 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

It seems the user process will still receive SIGBUS via kill_accessing_process() when (re-)access thp later. So they should have a chance to recover already. Or am I miss something?

Thanks.
.

author
#24 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 8 lines · Miaohe Lin
Quoted 4 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

It seems the user process will still receive SIGBUS via kill_accessing_process() when (re-)access thp later. So they should have a chance to recover already. Or am I miss something?

The concern is about real UE consumption in which case, it's desirable to kill the process ASAP without having to relying on subsequent access.  Also to honor processes' MCE-early-kill request. kill_accessing_process() is very conservative in that, it doesn't check other processes that have the poisoned page mapped.

thanks,

-jane

Quoted 5 lines

Thanks.
.

maintainer
#25 lore raw

Re: [PATCH 3/3] mm/memory-failure: send SIGBUS in the event of thp split fail

Quoted 7 lines · Jane Chu
Quoted 5 lines · Miaohe Lin
Quoted 4 lines · Jane Chu

When handle hwpoison in a GUP longterm pin'ed thp page, try_to_split_thp_page() will fail. And at this point, there is little else the kernel could do except sending a SIGBUS to the user process, thus give it a chance to recover.

Quoted 5 lines
Quoted 3 lines

It seems the user process will still receive SIGBUS via kill_accessing_process() when (re-)access thp later. So they should have a chance to recover already. Or am I miss something?

The concern is about real UE consumption in which case, it's desirable to kill the process ASAP without having to relying on subsequent access.  Also to honor processes' MCE-early-kill request. kill_accessing_process() is very conservative in that, it doesn't check other processes that have the poisoned page mapped.

I see. Thanks for your explanation.
Thanks.
.

Quoted 11 lines

thanks,

-jane

Quoted 5 lines

Thanks.
.

.