ACPI: APEI: handle synchronous errors in task work
Subsystems (9)
Shuai Xue
reviewerv18 cover ACPI: APEI: handle synchronous errors in task work
changes singce v17:
- rebase to Linux 6.13-rc7 with no functional changes
- add reviewed-by tag for patch 1-3 from Jane Chu
- add reviewed-by tag for patch 3 from Yazen
changes singce v16:
- add reviewed-by tag for patch 1 and patch 2 from Yazen
- rewrite warning message for force kill (per Yazen)
- warn with dev_err in ghes (per Jarkko)
- add return value -ENXIO in memory_failure comments (per Yazen)
- Link: https://lore.kernel.org/lkml/[email protected]/
changes singce v15:
- add HW_ERR and GHES_PFX prefix per Yazen
changes since v14:
- add reviewed-by tags from Jarkko and Jonathan
- remove local variable and use twcb->pfn
changes since v13:
- add reviewed-by tag from Jarkko
- rename task_work to ghes_task_work (per Jarkko)
changes since v12:
- tweak error message for force kill (per Jarkko)
- fix comments style (per Jarkko)
- fix commit log typo (per Jarko)
changes since v11:
- rebase to Linux 6.11-rc6
- fix grammer and typo in commit log (per Borislav)
- remove sync_ perfix of sync_task_work (per Borislav)
- comments flags and description of task_work (per Borislav)
changes since v10:
- rebase to v6.8-rc2
changes since v9:
- split patch 2 to address exactly one issue in one patch (per Borislav)
- rewrite commit log according to template (per Borislav)
- pickup reviewed-by tag of patch 1 from James Morse
- alloc and free twcb through gen_pool_{alloc, free) (Per James)
- rewrite cover letter
changes since v8:
- remove the bug fix tag of patch 2 (per Jarkko Sakkinen)
- remove the declaration of memory_failure_queue_kick (per Naoya Horiguchi)
- rewrite the return value comments of memory_failure (per Naoya Horiguchi)
changes since v7:
- rebase to Linux v6.6-rc2 (no code changed)
- rewritten the cover letter to explain the motivation of this patchset
changes since v6:
- add more explicty error message suggested by Xiaofei
- pick up reviewed-by tag from Xiaofei
- pick up internal reviewed-by tag from Baolin
changes since v5 by addressing comments from Kefeng:
- document return value of memory_failure()
- drop redundant comments in call site of memory_failure()
- make ghes_do_proc void and handle abnormal case within it
- pick up reviewed-by tag from Kefeng Wang
changes since v4 by addressing comments from Xiaofei:
- do a force kill only for abnormal sync errors
changes since v3 by addressing comments from Xiaofei:
- do a force kill for abnormal memory failure error such as invalid PA, unexpected severity, OOM, etc
- pcik up tested-by tag from Ma Wupeng
changes since v2 by addressing comments from Naoya:
- rename mce_task_work to sync_task_work
- drop ACPI_HEST_NOTIFY_MCE case in is_hest_sync_notify()
- add steps to reproduce this problem in cover letter
changes since v1:
- synchronous events by notify type
- Link: https://lore.kernel.org/lkml/[email protected]/
## Cover Letter
There are two major types of uncorrected recoverable (UCR) errors :
- Synchronous error: The error is detected and raised at the point of the consumption in the execution flow, e.g. when a CPU tries to access a poisoned cache line. The CPU will take a synchronous error exception such as Synchronous External Abort (SEA) on Arm64 and Machine Check Exception (MCE) on X86. OS requires to take action (for example, offline failure page/kill failure thread) to recover this uncorrectable error.
- Asynchronous error: The error is detected out of processor execution context, e.g. when an error is detected by a background scrubber. Some data in the memory are corrupted. But the data have not been consumed. OS is optional to take action to recover this uncorrectable error.
Currently, both synchronous and asynchronous error use memory_failure_queue() to schedule memory_failure() exectute in kworker context. As a result, when a user-space process is accessing a poisoned data, a data abort is taken and the memory_failure() is executed in the kworker context:
- will send wrong si_code by SIGBUS signal in early_kill mode, and
- can not kill the user-space in some cases resulting a synchronous error infinite loop
Issue 1: send wrong si_code in early_kill mode
Since commit a70297d22132 ("ACPI: APEI: set memory failure flags as MF_ACTION_REQUIRED on synchronous events")', the flag MF_ACTION_REQUIRED could be used to determine whether a synchronous exception occurs on ARM64 platform. When a synchronous exception is detected, the kernel is expected to terminate the current process which has accessed poisoned page. This is done by sending a SIGBUS signal with an error code BUS_MCEERR_AR, indicating an action-required machine check error on read.
However, when kill_proc() is called to terminate the processes who have the poisoned page mapped, it sends the incorrect SIGBUS error code BUS_MCEERR_AO because the context in which it operates is not the one where the error was triggered.
To reproduce this problem:
# STEP1: enable early kill mode
#sysctl -w vm.memory_failure_early_kill=1
vm.memory_failure_early_kill = 1
# STEP2: inject an UCE error and consume it to trigger a synchronous error #einj_mem_uc single
0: single vaddr = 0xffffb0d75400 paddr = 4092d55b400
injecting ...
triggering ...
signal 7 code 5 addr 0xffffb0d75000
page not present
Test passed
The si_code (code 5) from einj_mem_uc indicates that it is BUS_MCEERR_AO error and it is not fact.
To fix it, queue memory_failure() as a task_work so that it runs in the context of the process that is actually consuming the poisoned data.
After this patch set:
# STEP1: enable early kill mode
#sysctl -w vm.memory_failure_early_kill=1
vm.memory_failure_early_kill = 1
# STEP2: inject an UCE error and consume it to trigger a synchronous error #einj_mem_uc single
0: single vaddr = 0xffffb0d75400 paddr = 4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr 0xffffb0d75000
page not present
Test passed
The si_code (code 4) from einj_mem_uc indicates that it is BUS_MCEERR_AR error as we expected.
Issue 2: a synchronous error infinite loop due to memory_failure() failed
If a user-space process, e.g. devmem, a poisoned page which has been set HWPosion flag, kill_accessing_process() is called to send SIGBUS to the current processs with error info. Because the memory_failure() is executed in the kworker contex, it will just do nothing but return EFAULT. So, devmem will access the posioned page and trigger an excepction again, resulting in a synchronous error infinite loop. Such loop may cause platform firmware to exceed some threshold and reboot when Linux could have recovered from this error.
To reproduce this problem:
# STEP 1: inject an UCE error, and kernel will set HWPosion flag for related page #einj_mem_uc single
0: single vaddr = 0xffffb0d75400 paddr = 4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr 0xffffb0d75000
page not present
Test passed
# STEP 2: access the same page and it will trigger a synchronous error infinite loop devmem 0x4092d55b400
To fix it, if memory_failure() failed, perform a force kill to current process.
Issue 3: a synchronous error infinite loop due to no memory_failure() queued
No memory_failure() work is queued unless all bellow preconditions check passed:
- if (!(mem_err->validation_bits & CPER_MEM_VALID_PA)) in ghes_handle_memory_failure()
- if (flags == -1) in ghes_handle_memory_failure()
- if (!IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE)) in ghes_do_memory_failure()
- if (!pfn_valid(pfn) && !arch_is_platform_page(physical_addr)) in ghes_do_memory_failure()
If the preconditions are not passed, the user-space process will trigger SEA again. This loop can potentially exceed the platform firmware threshold or even trigger a kernel hard lockup, leading to a system reboot.
To fix it, if no memory_failure() queued, perform a force kill to current process.
And the the memory errors triggered in kernel-mode[5], also relies on this patchset to kill the failure thread.
Lv Ying and XiuQi from Huawei also proposed to address similar problem[2][4]. Acknowledge to discussion with them.
[1] Add ARMv8 RAS virtualization support in QEMU https://patchew.org/QEMU/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lkml.kernel.org/r/[email protected]
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://patchwork.kernel.org/project/linux-arm-kernel/cover/[email protected]/
Shuai Xue (3):
ACPI: APEI: send SIGBUS to current task if synchronous memory error
not recovered
mm: memory-failure: move return value documentation to function
declaration
ACPI: APEI: handle synchronous exceptions in task work
arch/x86/kernel/cpu/mce/core.c | 7 --- drivers/acpi/apei/ghes.c | 90 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 23 +++------ 5 files changed, 63 insertions(+), 61 deletions(-)
Signature · 1 line
2.39.3
Shuai Xue
reviewerPATCH v18 1/3 ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Synchronous error was detected as a result of user-space process accessing a 2-bit uncorrected error. The CPU will take a synchronous error exception such as Synchronous External Abort (SEA) on Arm64. The kernel will queue a memory_failure() work which poisons the related page, unmaps the page, and then sends a SIGBUS to the process, so that a system wide panic can be avoided.
However, no memory_failure() work will be queued when abnormal synchronous errors occur. These errors can include situations such as invalid PA, unexpected severity, no memory failure config support, invalid GUID section, etc. In such case, the user-space process will trigger SEA again. This loop can potentially exceed the platform firmware threshold or even trigger a kernel hard lockup, leading to a system reboot.
Fix it by performing a force kill if no memory_failure() work is queued for synchronous errors.
--- drivers/acpi/apei/ghes.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/acpi/apei/ghes.c +11 -0
@@ -801,6 +801,17 @@ static bool ghes_do_proc(struct ghes *ghes, } } +/* +* If no memory failure work is queued for abnormal synchronous +* errors, do a force kill. +*/ +if (sync && !queued) { +dev_err (ghes->dev, + HW_ERR GHES_PFX "%s:%d: synchronous unrecoverable error (SIGBUS)\n", + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Signature · 1 line
2.39.3
Shuai Xue
reviewerPATCH v18 2/3 mm: memory-failure: move return value documentation to function declaration
In reply to Shuai XuePart of return value comments for memory_failure() were originally documented at the call site. Move those comments to the function declaration to improve code readability and to provide developers with immediate access to function usage and return information.
--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 10 +++++++--- 2 files changed, 7 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/mce/core.c +0 -7
@@ -1398,13 +1398,6 @@ static void kill_me_maybe(struct callback_head *cb)return ; } -/* -* -EHWPOISON from memory_failure() means that it already sent SIGBUS -* to the current process with the proper error info, -* -EOPNOTSUPP means hwpoison_filter() filtered the error event, -* -* In both cases, no further processing is required. -*/ if (ret == -EHWPOISON || ret == -EOPNOTSUPP )return ;
mm/memory-failure.c +7 -3
@@ -2211,9 +2211,13 @@ static void kill_procs_now(struct page *p, unsigned long pfn, int flags,* Must run in process context (e.g. a work queue) with interrupts * enabled and no spinlocks held. * -* Return: 0 for successfully handled the memory error, -* -EOPNOTSUPP for hwpoison_filter() filtered the error event, -* < 0(except -EOPNOTSUPP) on failure. +* Return: +* 0 - success, +* -ENXIO - memory not managed by the kernel +* -EOPNOTSUPP - hwpoison_filter() filtered the error event, +* -EHWPOISON - the page was already poisoned, potentially +* kill process, +* other negative values - failure. */ int memory_failure (unsigned long pfn,int flags) {
Signature · 1 line
2.39.3
Re: [PATCH v18 2/3] mm: memory-failure: move return value documentation to function declaration
(going through patches in my inbox)
Quoted 3 lines · Shuai Xue
Part of return value comments for
memory_failure()were originally documented at the call site. Move those comments to the function declaration to improve code readability and to provide developers with
s/declaration/definition/
Quoted 41 lines
immediate access to function usage and return information.
Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>Reviewed-by Yazen Ghannam <[email protected]>Reviewed-by Jane Chu <[email protected]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 10 +++++++--- 2 files changed, 7 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1398,13 +1398,6 @@ static void kill_me_maybe(struct callback_head *cb)return ; } -/* -* -EHWPOISON from memory_failure() means that it already sent SIGBUS -* to the current process with the proper error info, -* -EOPNOTSUPP means hwpoison_filter() filtered the error event, -* -* In both cases, no further processing is required. -*/ if (ret == -EHWPOISON || ret == -EOPNOTSUPP )return ;mm/memory-failure.c +0
-3@@ -2211,9 +2211,13 @@ static void kill_procs_now(struct page *p, unsigned long pfn, int flags,* Must run in process context (e.g. a work queue) with interrupts * enabled and no spinlocks held. * -* Return: 0 for successfully handled the memory error, -* -EOPNOTSUPP for hwpoison_filter() filtered the error event, -* < 0(except -EOPNOTSUPP) on failure.
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2223
Quoted 10 lines
mm/memory-failure.c +7
-0+* Return: +* 0 - success, +* -ENXIO - memory not managed by the kernel +* -EOPNOTSUPP - hwpoison_filter() filtered the error event, +* -EHWPOISON - the page was already poisoned, potentially +* kill process, +* other negative values - failure. */ int memory_failure (unsigned long pfn,int flags) {
Why not keep the comment in both places? One is about the x86 decisions, the other is about what memory_failure() can return.
Signature · 1 line
Catalin
Shuai Xue
reviewerRe: [PATCH v18 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 8 lines · Catalin Marinas
(going through patches in my inbox)
Quoted 3 lines · Shuai Xue
Part of return value comments for
memory_failure()were originally documented at the call site. Move those comments to the function declaration to improve code readability and to provide developers withs/declaration/definition/
Thanks, will fix.
Quoted 47 lines
Quoted 47 lines
immediate access to function usage and return information.
Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>Reviewed-by Yazen Ghannam <[email protected]>Reviewed-by Jane Chu <[email protected]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 10 +++++++--- 2 files changed, 7 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1398,13 +1398,6 @@ static void kill_me_maybe(struct callback_head *cb)return ; } -/* -* -EHWPOISON from memory_failure() means that it already sent SIGBUS -* to the current process with the proper error info, -* -EOPNOTSUPP means hwpoison_filter() filtered the error event, -* -* In both cases, no further processing is required. -*/ if (ret == -EHWPOISON || ret == -EOPNOTSUPP )return ;mm/memory-failure.c +5
-3@@ -2211,9 +2211,13 @@ static void kill_procs_now(struct page *p, unsigned long pfn, int flags,* Must run in process context (e.g. a work queue) with interrupts * enabled and no spinlocks held. * -* Return: 0 for successfully handled the memory error, -* -EOPNOTSUPP for hwpoison_filter() filtered the error event, -* < 0(except -EOPNOTSUPP) on failure. +* Return: +* 0 - success, +* -ENXIO - memory not managed by the kernel +* -EOPNOTSUPP - hwpoison_filter() filtered the error event, +* -EHWPOISON - the page was already poisoned, potentially
Quoted 9 lines
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2223
Quoted 5 lines
mm/memory-failure.c +2
-0+* kill process, +* other negative values - failure. */ int memory_failure (unsigned long pfn,int flags) {Why not keep the comment in both places? One is about the x86 decisions, the other is about what
memory_failure()can return.
Ok, will keep x86 path.
By the way, could arm maintainers help to ack patch 1 and 3 if there is no objection?
Thanks.
Shuai
Re: [PATCH v18 2/3] mm: memory-failure: move return value documentation to function declaration
On Fri, Feb 28, 2025 at 09:46:30AM +0800, Shuai Xue wrote:
[...]
Quoted 4 lines
Quoted 4 lines · Shuai Xue
Quoted 3 lines
Part of return value comments for
memory_failure()were originally documented at the call site. Move those comments to the function declaration to improve code readability and to provide developers with
[...]
Quoted 2 lines
By the way, could arm maintainers help to ack patch 1 and 3 if there is no objection?
James Morse is listed as reviewer of the ACPI APEI code but he's busy with resctrl/MPAM. Adding Lorenzo, Sudeep and Hanjun as arm64 ACPI maintainers, hopefully they can help. Here's the full series:
https://lore.kernel.org/r/[email protected]/
Signature · 1 line
Catalin
Shuai Xue
reviewerRe: [PATCH v18 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 7 lines · Catalin Marinas
On Fri, Feb 28, 2025 at 09:46:30AM +0800, Shuai Xue wrote:
[...]Quoted 4 lines
Quoted 4 lines · Shuai Xue
Quoted 3 lines
Part of return value comments for
memory_failure()were originally documented at the call site. Move those comments to the function declaration to improve code readability and to provide developers with[...]
Quoted 9 lines
Quoted 2 lines
By the way, could arm maintainers help to ack patch 1 and 3 if there is no objection?
James Morse is listed as reviewer of the ACPI APEI code but he's busy with resctrl/MPAM. Adding Lorenzo, Sudeep and Hanjun as arm64 ACPI maintainers, hopefully they can help. Here's the full series:
Thank you for your assistance.
Looking forward to any updates.
Best regards,
Shuai
Shuai Xue
reviewerPATCH v18 3/3 ACPI: APEI: handle synchronous exceptions in task work
In reply to Shuai XueThe memory uncorrected error could be signaled by asynchronous interrupt (specifically, SPI in arm64 platform), e.g. when an error is detected by a background scrubber, or signaled by synchronous exception (specifically, data abort exception in arm64 platform), e.g. when a CPU tries to access a poisoned cache line. Currently, both synchronous and asynchronous error use memory_failure_queue() to schedule memory_failure() to exectute in a kworker context.
As a result, when a user-space process is accessing a poisoned data, a data abort is taken and the memory_failure() is executed in the kworker context, memory_failure():
- will send wrong si_code by SIGBUS signal in early_kill mode, and
- can not kill the user-space in some cases resulting a synchronous error infinite loop
Issue 1: send wrong si_code in early_kill mode
Since commit a70297d22132 ("ACPI: APEI: set memory failure flags as MF_ACTION_REQUIRED on synchronous events")', the flag MF_ACTION_REQUIRED could be used to determine whether a synchronous exception occurs on ARM64 platform. When a synchronous exception is detected, the kernel is expected to terminate the current process which has accessed poisoned page. This is done by sending a SIGBUS signal with an error code BUS_MCEERR_AR, indicating an action-required machine check error on read.
However, when kill_proc() is called to terminate the processes who have the poisoned page mapped, it sends the incorrect SIGBUS error code BUS_MCEERR_AO because the context in which it operates is not the one where the error was triggered.
To reproduce this problem:
#sysctl -w vm.memory_failure_early_kill=1
vm.memory_failure_early_kill = 1
# STEP2: inject an UCE error and consume it to trigger a synchronous error #einj_mem_uc single
0: single vaddr = 0xffffb0d75400 paddr = 4092d55b400
injecting ...
triggering ...
signal 7 code 5 addr 0xffffb0d75000
page not present
Test passed
The si_code (code 5) from einj_mem_uc indicates that it is BUS_MCEERR_AO error and it is not the fact.
After this patch:
# STEP1: enable early kill mode
#sysctl -w vm.memory_failure_early_kill=1 vm.memory_failure_early_kill = 1
# STEP2: inject an UCE error and consume it to trigger a synchronous error #einj_mem_uc single
0: single vaddr = 0xffffb0d75400 paddr = 4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr 0xffffb0d75000
page not present
Test passed
The si_code (code 4) from einj_mem_uc indicates that it is a BUS_MCEERR_AR error as we expected.
Issue 2: a synchronous error infinite loop
If a user-space process, e.g. devmem, accesses a poisoned page for which the HWPoison flag is set, kill_accessing_process() is called to send SIGBUS to current processs with error info. Because the memory_failure() is executed in the kworker context, it will just do nothing but return EFAULT. So, devmem will access the posioned page and trigger an exception again, resulting in a synchronous error infinite loop. Such exception loop may cause platform firmware to exceed some threshold and reboot when Linux could have recovered from this error.
To reproduce this problem:
# STEP 1: inject an UCE error, and kernel will set HWPosion flag for related page #einj_mem_uc single
0: single vaddr = 0xffffb0d75400 paddr = 4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr 0xffffb0d75000
page not present
Test passed
# STEP 2: access the same page and it will trigger a synchronous error infinite loop devmem 0x4092d55b400
To fix above two issues, queue memory_failure() as a task_work so that it runs in the context of the process that is actually consuming the poisoned data.
--- drivers/acpi/apei/ghes.c | 81 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 46 insertions(+), 52 deletions(-)
drivers/acpi/apei/ghes.c +46 -35
@@ -466,28 +466,41 @@ static void ghes_clear_estatus(struct ghes *ghes,ghes_ack_error (ghes->generic_v2); } -/* -* Called as task_work before returning to user-space. -* Ensure any queued work has been done before we return to the context that -* triggered the notification. +/** +* struct ghes_task_work - for synchronous RAS event +* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: work control flags +* +* Structure to pass task work to be handled before +* returning to user-space via task_work_add(). */ -static void ghes_kick_task_work (struct callback_head *head) +struct ghes_task_work { +struct callback_head twork; +u64 pfn; +int flags; +}; + +static void memory_failure_cb (struct callback_head *twork) { -struct acpi_hest_generic_status *estatus; -struct ghes_estatus_node *estatus_node; -u32 node_len; + struct ghes_task_work *twcb = container_of(twork, struct ghes_task_work, twork); +int ret; - estatus_node = container_of(head, struct ghes_estatus_node, task_work); -if (IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE )) -memory_failure_queue_kick (estatus_node->task_work_cpu); + ret =memory_failure (twcb->pfn, twcb->flags); +gen_pool_free (ghes_estatus_pool, (unsigned long )twcb,sizeof (*twcb)); - estatus =GHES_ESTATUS_FROM_NODE (estatus_node); - node_len =GHES_ESTATUS_NODE_LEN (cper_estatus_len (estatus)); -gen_pool_free (ghes_estatus_pool, (unsigned long )estatus_node, node_len); +if (!ret || ret == -EHWPOISON || ret == -EOPNOTSUPP ) +return ; + +pr_err ("%#llx: Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + twcb->pfn, current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) { +struct ghes_task_work *twcb;unsigned long pfn;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))
@@ -501,6 +514,18 @@ static bool ghes_do_memory_failure(u64 physical_addr, int flags)return false; } +if (flags ==MF_ACTION_REQUIRED && current->mm) { + twcb = (void *)gen_pool_alloc (ghes_estatus_pool,sizeof (*twcb)); +if (!twcb) +return false; + + twcb->pfn = pfn; + twcb->flags = flags; +init_task_work (&twcb->twork, memory_failure_cb); +task_work_add (current, &twcb->twork,TWA_RESUME ); +return true; + } +memory_failure_queue (pfn, flags);return true; }
@@ -745,8 +770,8 @@ int cxl_cper_kfifo_get(struct cxl_cper_work_data *wd) }EXPORT_SYMBOL_NS_GPL (cxl_cper_kfifo_get,"CXL" ); -static bool ghes_do_proc (struct ghes *ghes, -const struct acpi_hest_generic_status *estatus) +static void ghes_do_proc (struct ghes *ghes, +const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;struct acpi_hest_generic_data *gdata;
@@ -811,8 +836,6 @@ static bool ghes_do_proc(struct ghes *ghes, current->comm, task_pid_nr(current));force_sig (SIGBUS ); } - -return queued; } static void __ghes_print_estatus(const char *pfx,
@@ -1114,9 +1137,7 @@ static void ghes_proc_in_irq(struct irq_work *irq_work)struct ghes_estatus_node *estatus_node;struct acpi_hest_generic *generic;struct acpi_hest_generic_status *estatus; -bool task_work_pending;u32 len, node_len; -int ret; llnode =llist_del_all (&ghes_estatus_llist);/*
@@ -1131,25 +1152,16 @@ static void ghes_proc_in_irq(struct irq_work *irq_work) estatus =GHES_ESTATUS_FROM_NODE (estatus_node); len =cper_estatus_len (estatus); node_len =GHES_ESTATUS_NODE_LEN (len); - task_work_pending =ghes_do_proc (estatus_node->ghes, estatus); + +ghes_do_proc (estatus_node->ghes, estatus); +if (!ghes_estatus_cached (estatus)) { generic = estatus_node->generic;if (ghes_print_estatus (NULL , generic, estatus))ghes_estatus_cache_add (generic, estatus); } - -if (task_work_pending && current->mm) { - estatus_node->task_work.func = ghes_kick_task_work; - estatus_node->task_work_cpu =smp_processor_id (); - ret =task_work_add (current, &estatus_node->task_work, -TWA_RESUME ); -if (ret) - estatus_node->task_work.func =NULL ; - } - -if (!estatus_node->task_work.func) -gen_pool_free (ghes_estatus_pool, - (unsigned long )estatus_node, node_len); +gen_pool_free (ghes_estatus_pool, (unsigned long )estatus_node, + node_len); llnode = next; }
@@ -1210,7 +1222,6 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes, estatus_node->ghes = ghes; estatus_node->generic = ghes->generic; - estatus_node->task_work.func =NULL ; estatus =GHES_ESTATUS_FROM_NODE (estatus_node);if (__ghes_read_estatus (estatus, buf_paddr, fixmap_idx, len)) {
include/acpi/ghes.h +0 -3
@@ -35,9 +35,6 @@ struct ghes_estatus_node {struct llist_node llnode;struct acpi_hest_generic *generic;struct ghes *ghes; - -int task_work_cpu; -struct callback_head task_work; };struct ghes_estatus_cache {
include/linux/mm.h +0 -1
@@ -3947,7 +3947,6 @@ enum mf_flags {int mf_dax_kill_procs (struct address_space *mapping,pgoff_t index,unsigned long count,int mf_flags);extern int memory_failure (unsigned long pfn,int flags); -extern void memory_failure_queue_kick (int cpu);extern int unpoison_memory (unsigned long pfn);extern atomic_long_t num_poisoned_pages__read_mostly ;extern int soft_offline_page (unsigned long pfn,int flags);
mm/memory-failure.c +0 -13
@@ -2499,19 +2499,6 @@ static void memory_failure_work_func(struct work_struct *work) } } -/* -* Process memory_failure work queued on the specified CPU. -* Used to avoid return-to-userspace racing with the memory_failure workqueue. -*/ -void memory_failure_queue_kick (int cpu) -{ -struct memory_failure_cpu *mf_cpu; - - mf_cpu = &per_cpu (memory_failure_cpu, cpu); -cancel_work_sync (&mf_cpu->work); -memory_failure_work_func (&mf_cpu->work); -} -static int __init memory_failure_init (void ) {struct memory_failure_cpu *mf_cpu;
Signature · 1 line
2.39.3
Shuai Xue
reviewerHi, Rafael,
Gentle ping.
Thanks.
Best Regards,
Shuai
Quoted 230 lines · Shuai Xue
changes singce v17:
- rebase to Linux 6.13-rc7 with no functional changes
- add reviewed-by tag for patch 1-3 from Jane Chu
- add reviewed-by tag for patch 3 from Yazenchanges singce v16:
- add reviewed-by tag for patch 1 and patch 2 from Yazen
- rewrite warning message for force kill (per Yazen)
- warn withdev_errin ghes (per Jarkko)
- add return value-ENXIOinmemory_failurecomments (per Yazen)
- Link: https://lore.kernel.org/lkml/[email protected]/changes singce v15:
- addHW_ERRandGHES_PFXprefix per Yazenchanges since v14:
- add reviewed-by tags from Jarkko and Jonathan
- remove local variable and usetwcb->pfnchanges since v13:
- add reviewed-by tag from Jarkko
- renametask_worktoghes_task_work(per Jarkko)changes since v12:
- tweak error message for force kill (per Jarkko)
- fix comments style (per Jarkko)
- fix commit log typo (per Jarko)changes since v11:
- rebase to Linux 6.11-rc6
- fix grammer and typo in commit log (per Borislav)
- removesync_perfix ofsync_task_work(per Borislav)
- comments flags and description oftask_work(per Borislav)changes since v10:
- rebase to v6.8-rc2changes since v9:
- split patch 2 to address exactly one issue in one patch (per Borislav)
- rewrite commit log according to template (per Borislav)
- pickup reviewed-by tag of patch 1 from James Morse
- alloc and free twcb through gen_pool_{alloc, free) (Per James)
- rewrite cover letterchanges since v8:
- remove the bug fix tag of patch 2 (per Jarkko Sakkinen)
- remove the declaration ofmemory_failure_queue_kick(per Naoya Horiguchi)
- rewrite the return value comments ofmemory_failure(per Naoya Horiguchi)changes since v7:
- rebase to Linux v6.6-rc2 (no code changed)
- rewritten the cover letter to explain the motivation of this patchsetchanges since v6:
- add more explicty error message suggested by Xiaofei
- pick up reviewed-by tag from Xiaofei
- pick up internal reviewed-by tag from Baolinchanges since v5 by addressing comments from Kefeng:
- document return value ofmemory_failure()
- drop redundant comments in call site ofmemory_failure()
- makeghes_do_procvoid and handle abnormal case within it
- pick up reviewed-by tag from Kefeng Wangchanges since v4 by addressing comments from Xiaofei:
- do a force kill only for abnormal sync errorschanges since v3 by addressing comments from Xiaofei:
- do a force kill for abnormal memory failure error such as invalid PA, unexpected severity, OOM, etc
- pcik up tested-by tag from Ma Wupengchanges since v2 by addressing comments from Naoya:
- renamemce_task_worktosync_task_work
- dropACPI_HEST_NOTIFY_MCEcase inis_hest_sync_notify()
- add steps to reproduce this problem in cover letterchanges since v1:
- synchronous events by notify type
- Link: https://lore.kernel.org/lkml/[email protected]/## Cover Letter
There are two major types of uncorrected recoverable (UCR) errors :
- Synchronous error: The error is detected and raised at the point of the consumption in the execution flow, e.g. when a CPU tries to access a poisoned cache line. The CPU will take a synchronous error exception such as Synchronous External Abort (SEA) on Arm64 and Machine Check Exception (MCE) on X86. OS requires to take action (for example, offline failure page/kill failure thread) to recover this uncorrectable error.
- Asynchronous error: The error is detected out of processor execution context, e.g. when an error is detected by a background scrubber. Some data in the memory are corrupted. But the data have not been consumed. OS is optional to take action to recover this uncorrectable error.
Currently, both synchronous and asynchronous error use
memory_failure_queue()to schedulememory_failure()exectute in kworker context. As a result, when a user-space process is accessing a poisoned data, a data abort is taken and thememory_failure()is executed in the kworker context:- will send wrong
si_codeby SIGBUS signal inearly_killmode, and
- can not kill the user-space in some cases resulting a synchronous error infinite loopIssue 1: send wrong
si_codeinearly_killmodeSince commit
a70297d22132("ACPI: APEI: set memory failure flags asMF_ACTION_REQUIREDon synchronous events")', the flagMF_ACTION_REQUIREDcould be used to determine whether a synchronous exception occurs on ARM64 platform. When a synchronous exception is detected, the kernel is expected to terminate the current process which has accessed poisoned page. This is done by sending a SIGBUS signal with an error codeBUS_MCEERR_AR, indicating an action-required machine check error on read.However, when
kill_proc()is called to terminate the processes who have the poisoned page mapped, it sends the incorrect SIGBUS error codeBUS_MCEERR_AObecause the context in which it operates is not the one where the error was triggered.To reproduce this problem:
# STEP1: enable early kill mode
#sysctl -w vm.memory_failure_early_kill=1
vm.memory_failure_early_kill = 1# STEP2: inject an UCE error and consume it to trigger a synchronous error #einj_mem_uc single
0: single vaddr =0xffffb0d75400paddr =4092d55b400
injecting ...
triggering ...
signal 7 code 5 addr0xffffb0d75000
page not present
Test passedThe
si_code(code 5) fromeinj_mem_ucindicates that it isBUS_MCEERR_AOerror and it is not fact.To fix it, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.After this patch set:
# STEP1: enable early kill mode
#sysctl -w vm.memory_failure_early_kill=1
vm.memory_failure_early_kill = 1# STEP2: inject an UCE error and consume it to trigger a synchronous error #einj_mem_uc single
0: single vaddr =0xffffb0d75400paddr =4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr0xffffb0d75000
page not present
Test passedThe
si_code(code 4) fromeinj_mem_ucindicates that it isBUS_MCEERR_ARerror as we expected.Issue 2: a synchronous error infinite loop due to
memory_failure()failedIf a user-space process, e.g. devmem, a poisoned page which has been set HWPosion flag,
kill_accessing_process()is called to send SIGBUS to the current processs with error info. Because thememory_failure()is executed in the kworker contex, it will just do nothing but return EFAULT. So, devmem will access the posioned page and trigger an excepction again, resulting in a synchronous error infinite loop. Such loop may cause platform firmware to exceed some threshold and reboot when Linux could have recovered from this error.To reproduce this problem:
# STEP 1: inject an UCE error, and kernel will set HWPosion flag for related page #einj_mem_uc single
0: single vaddr =0xffffb0d75400paddr =4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr0xffffb0d75000
page not present
Test passed# STEP 2: access the same page and it will trigger a synchronous error infinite loop devmem
0x4092d55b400To fix it, if
memory_failure()failed, perform a force kill to current process.Issue 3: a synchronous error infinite loop due to no
memory_failure()queuedNo
memory_failure()work is queued unless all bellow preconditions check passed:-
if (!(mem_err->validation_bits & CPER_MEM_VALID_PA))inghes_handle_memory_failure()
-if (flags == -1)inghes_handle_memory_failure()
-if (!IS_ENABLED(CONFIG_ACPI_APEI_MEMORY_FAILURE))inghes_do_memory_failure()
-if (!pfn_valid(pfn) && !arch_is_platform_page(physical_addr))inghes_do_memory_failure()If the preconditions are not passed, the user-space process will trigger SEA again. This loop can potentially exceed the platform firmware threshold or even trigger a kernel hard lockup, leading to a system reboot.
To fix it, if no
memory_failure()queued, perform a force kill to current process.And the the memory errors triggered in kernel-mode[5], also relies on this patchset to kill the failure thread.
Lv Ying and XiuQi from Huawei also proposed to address similar problem[2][4]. Acknowledge to discussion with them.
[1] Add ARMv8 RAS virtualization support in QEMU https://patchew.org/QEMU/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lkml.kernel.org/r/[email protected]
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://patchwork.kernel.org/project/linux-arm-kernel/cover/[email protected]/Shuai Xue (3):
ACPI: APEI: send SIGBUS to current task if synchronous memory error
not recovered
mm: memory-failure: move return value documentation to function
declaration
ACPI: APEI: handle synchronous exceptions in task workarch/x86/kernel/cpu/mce/core.c | 7 --- drivers/acpi/apei/ghes.c | 90 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 23 +++------ 5 files changed, 63 insertions(+), 61 deletions(-)
Shuai Xue
reviewerQuoted 29 lines · Shuai Xue
Hi, Rafael,
Gentle ping.
Thanks.
Best Regards,
ShuaiQuoted 19 lines · Shuai Xue
changes singce v17:
- rebase to Linux 6.13-rc7 with no functional changes
- add reviewed-by tag for patch 1-3 from Jane Chu
- add reviewed-by tag for patch 3 from Yazenchanges singce v16:
- add reviewed-by tag for patch 1 and patch 2 from Yazen
- rewrite warning message for force kill (per Yazen)
- warn withdev_errin ghes (per Jarkko)
- add return value-ENXIOinmemory_failurecomments (per Yazen)
- Link: https://lore.kernel.org/lkml/[email protected]/changes singce v15:
- addHW_ERRandGHES_PFXprefix per Yazenchanges since v14:
- add reviewed-by tags from Jarkko and Jonathan
- remove local variable and usetwcb->pfn
Hi, all,
Gentle ping.
Thanks.
Shuai