ACPI: APEI: set memory failure flags as MF_ACTION_REQUIRED on action required events
Subsystems (10)
Shuai Xue
reviewerPATCH v11 3/3 ACPI: APEI: handle synchronous exceptions in task work to send correct SIGBUS si_code
In reply to Shuai XueHardware errors could be signaled by asynchronous interrupt, e.g. when an error is detected by a background scrubber, or signaled by synchronous exception, e.g. when a CPU tries to access a poisoned cache line. 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 should terminate the current process which accessing the 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, the memory failure recovery is incorrectly sending a SIGBUS with wrong error code BUS_MCEERR_AO for synchronous errors in early kill mode, even MF_ACTION_REQUIRED is set. The main problem is that synchronous errors are queued as a memory_failure() work, and are executed within a kernel thread context, not the user-space process that encountered the corrupted memory on ARM64 platform. As a result, when kill_proc() is called to terminate the process, it sends the incorrect SIGBUS error code because the context in which it operates is not the one where the error was triggered.
To this end, queue memory_failure() as a task_work so that it runs in the context of the process that is actually consuming the poisoned data, and it will send SIBBUS with si_code BUS_MCEERR_AR.
--- drivers/acpi/apei/ghes.c | 77 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- mm/memory-failure.c | 13 ------- 3 files changed, 44 insertions(+), 49 deletions(-)
drivers/acpi/apei/ghes.c +44 -33
@@ -465,28 +465,41 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 sync_task_work - for synchronous RAS event +* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: fine tune action taken +* +* Structure to pass task work to be handled before +* ret_to_user via task_work_add(). */ -static void ghes_kick_task_work (struct callback_head *head) +struct sync_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; +int ret; +struct sync_task_work *twcb = + container_of(twork, struct sync_task_work, twork); - 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 ("Sending SIGBUS to current task due to memory error not recovered" ); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct sync_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;
@@ -499,6 +512,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; }
@@ -746,7 +771,7 @@ int cxl_cper_unregister_callback(cxl_cper_callback callback) }EXPORT_SYMBOL_NS_GPL (cxl_cper_unregister_callback,CXL ); -static bool ghes_do_proc (struct ghes *ghes, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;
@@ -814,8 +839,6 @@ static bool ghes_do_proc(struct ghes *ghes,pr_err ("Sending SIGBUS to current task due to memory error not recovered" );force_sig (SIGBUS ); } - -return queued; } static void __ghes_print_estatus(const char *pfx,
@@ -1117,9 +1140,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);/*
@@ -1134,25 +1155,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; }
@@ -1213,7 +1225,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 {
mm/memory-failure.c +0 -13
@@ -2462,19 +2462,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
reviewerRe: [PATCH v11 3/3] ACPI: APEI: handle synchronous exceptions in task work to send correct SIGBUS si_code
Quoted 209 lines · Shuai Xue
Hardware errors could be signaled by asynchronous interrupt, e.g. when an error is detected by a background scrubber, or signaled by synchronous exception, e.g. when a CPU tries to access a poisoned cache line. Since 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 should terminate the current process which accessing the 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, the memory failure recovery is incorrectly sending a SIGBUS with wrong error code
BUS_MCEERR_AOfor synchronous errors in early kill mode, evenMF_ACTION_REQUIREDis set. The main problem is that synchronous errors are queued as amemory_failure()work, and are executed within a kernel thread context, not the user-space process that encountered the corrupted memory on ARM64 platform. As a result, whenkill_proc()is called to terminate the process, it sends the incorrect SIGBUS error code because the context in which it operates is not the one where the error was triggered.To this end, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data, and it will send SIBBUS withsi_codeBUS_MCEERR_AR.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 77 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- mm/memory-failure.c | 13 ------- 3 files changed, 44 insertions(+), 49 deletions(-)drivers/acpi/apei/ghes.c +44
-33@@ -465,28 +465,41 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 sync_task_work - for synchronous RAS event +* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: fine tune action taken +* +* Structure to pass task work to be handled before +* ret_to_user via task_work_add(). */ -static void ghes_kick_task_work (struct callback_head *head) +struct sync_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; +int ret; +struct sync_task_work *twcb = + container_of(twork, struct sync_task_work, twork); - 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 ("Sending SIGBUS to current task due to memory error not recovered" ); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct sync_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;@@ -499,6 +512,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; }@@ -746,7 +771,7 @@ int cxl_cper_unregister_callback(cxl_cper_callback callback) }EXPORT_SYMBOL_NS_GPL (cxl_cper_unregister_callback,CXL ); -static bool ghes_do_proc (struct ghes *ghes, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;@@ -814,8 +839,6 @@ static bool ghes_do_proc(struct ghes *ghes,pr_err ("Sending SIGBUS to current task due to memory error not recovered" );force_sig (SIGBUS ); } - -return queued; } static void __ghes_print_estatus(const char *pfx,@@ -1117,9 +1140,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);/* @@ -1134,25 +1155,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; }@@ -1213,7 +1225,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 {mm/memory-failure.c +0
-6@@ -2462,19 +2462,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) -{
mm/memory-failure.c static void memory_failure_work_func(struct work_struct *work) · line 2467
Quoted 10 lines
mm/memory-failure.c +0
-7-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;
Hi, Tony, Borislav, and James:
Any comments to this patch?
Looking forward to hear from you.
Best Regards,
Shuai
Borislav Petkov
maintainerRe: [PATCH v11 3/3] ACPI: APEI: handle synchronous exceptions in task work to send correct SIGBUS si_code
In reply to Shuai XueQuoted 8 lines · Shuai Xue
Hardware errors could be signaled by asynchronous interrupt, e.g. when an error is detected by a background scrubber, or signaled by synchronous exception, e.g. when a CPU tries to access a poisoned cache line. Since 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 should terminate the current process which accessing the poisoned page. This is
"which has accessed poison data"
Quoted 6 lines
done by sending a SIGBUS signal with an error code
BUS_MCEERR_AR, indicating an action-required machine check error on read.However, the memory failure recovery is incorrectly sending a SIGBUS with wrong error code
BUS_MCEERR_AOfor synchronous errors in early kill mode, evenMF_ACTION_REQUIREDis set. The main problem is that
"even if"
Quoted 10 lines
synchronous errors are queued as a
memory_failure()work, and are executed within a kernel thread context, not the user-space process that encountered the corrupted memory on ARM64 platform. As a result, whenkill_proc()is called to terminate the process, it sends the incorrect SIGBUS error code because the context in which it operates is not the one where the error was triggered.To this end, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data, and it will send SIBBUS withsi_codeBUS_MCEERR_AR.
SIGBUS
Quoted 17 lines
Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 77 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- mm/memory-failure.c | 13 ------- 3 files changed, 44 insertions(+), 49 deletions(-)drivers/acpi/apei/ghes.c +0
-0
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 468
Quoted 7 lines
drivers/acpi/apei/ghes.c +1
-3@@ -465,28 +465,41 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 sync_task_work - for synchronous RAS event
What's so special about it being a "sync_"?
task_work is just fine and something else could use it too.
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 472
Quoted 4 lines
drivers/acpi/apei/ghes.c +4
-0+* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: fine tune action taken
s/fine tune action taken/work control flags/
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 475
Quoted 3 lines
drivers/acpi/apei/ghes.c +3
-0+* +* Structure to pass task work to be handled before +* ret_to_user via task_work_add().
What is "ret_to_user"?
If this is an ARM thing, then make sure you explain stuff properly and detailed. This driver is used by multiple architectures.
Quoted 6 lines
drivers/acpi/apei/ghes.c +4
-1*/ -static void ghes_kick_task_work (struct callback_head *head) +struct sync_task_work { +struct callback_head twork; +u64 pfn; +int flags;
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 487
Quoted 10 lines
drivers/acpi/apei/ghes.c +6
-3+}; + +static void memory_failure_cb (struct callback_head *twork) { -struct acpi_hest_generic_status *estatus; -struct ghes_estatus_node *estatus_node; -u32 node_len; +int ret; +struct sync_task_work *twcb = + container_of(twork, struct sync_task_work, twork);
Ugly linebreak - no need for it.
Quoted 86 lines
drivers/acpi/apei/ghes.c +24
-15- 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 ("Sending SIGBUS to current task due to memory error not recovered" ); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct sync_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;@@ -499,6 +512,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; }@@ -746,7 +771,7 @@ int cxl_cper_unregister_callback(cxl_cper_callback callback) }EXPORT_SYMBOL_NS_GPL (cxl_cper_unregister_callback,CXL ); -static bool ghes_do_proc (struct ghes *ghes, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;@@ -814,8 +839,6 @@ static bool ghes_do_proc(struct ghes *ghes,pr_err ("Sending SIGBUS to current task due to memory error not recovered" );force_sig (SIGBUS ); } - -return queued; } static void __ghes_print_estatus(const char *pfx,@@ -1117,9 +1140,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);/* @@ -1134,25 +1155,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;
drivers/acpi/apei/ghes.c static void ghes_proc_in_irq(struct irq_work *irq_work) · line 1155
Quoted 10 lines
drivers/acpi/apei/ghes.c +0
-10- 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);
I have no clue why this is being removed.
Why doesn't a synchronous exception on ARM call into ghes_proc_in_irq()?
That SDEI thing certainly does.
Well looka here:
7f17b4a121d0 ("ACPI: APEI: Kick the memory_failure() queue for synchronous errors")
that thing does exactly what you're trying to "fix". So why doesn't that work for you?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteShuai Xue
reviewerRe: [PATCH v11 3/3] ACPI: APEI: handle synchronous exceptions in task work to send correct SIGBUS si_code
Quoted 11 lines · Borislav Petkov
Quoted 8 lines · Shuai Xue
Hardware errors could be signaled by asynchronous interrupt, e.g. when an error is detected by a background scrubber, or signaled by synchronous exception, e.g. when a CPU tries to access a poisoned cache line. Since 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 should terminate the current process which accessing the poisoned page. This is"which has accessed poison data"
Thank you. Will fix the grammer.
Quoted 9 lines
Quoted 7 lines
done by sending a SIGBUS signal with an error code
BUS_MCEERR_AR, indicating an action-required machine check error on read.However, the memory failure recovery is incorrectly sending a SIGBUS with wrong error code
BUS_MCEERR_AOfor synchronous errors in early kill mode, evenMF_ACTION_REQUIREDis set. The main problem is that"even if"
Thank you. Will fix the grammer.
Quoted 8 lines
Quoted 8 lines
synchronous errors are queued as a
memory_failure()work, and are executed within a kernel thread context, not the user-space process that encountered the corrupted memory on ARM64 platform. As a result, whenkill_proc()is called to terminate the process, it sends the incorrect SIGBUS error code because the context in which it operates is not the one where the error was triggered.
Quoted 5 lines
Quoted 3 lines
To this end, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data, and it will send SIBBUS withsi_codeBUS_MCEERR_AR.SIGBUS
Sorry, will fix the typo.
Quoted 28 lines
Quoted 18 lines
Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 77 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- mm/memory-failure.c | 13 ------- 3 files changed, 44 insertions(+), 49 deletions(-)drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 468
Quoted 7 lines
drivers/acpi/apei/ghes.c +1
-3@@ -465,28 +465,41 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 sync_task_work - for synchronous RAS event What's so special about it being a "
sync_"?
Quoted 1 line
task_workis just fine and something else could use it too.
You are right, the sync_task_work is only use for synchronous RAS event right, but it could be also use for other purpose in the future. The purpose can be specified through flags.
I will remove the sync_ prefix.
Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 472
Quoted 5 lines
drivers/acpi/apei/ghes.c +4
-0+* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: fine tune action taken s/fine tune action taken/work control flags/
Will fix it.
Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 475
Quoted 3 lines
drivers/acpi/apei/ghes.c +3
-0+* +* Structure to pass task work to be handled before +* ret_to_user via task_work_add(). What is "
ret_to_user"?If this is an ARM thing, then make sure you explain stuff properly and detailed. This driver is used by multiple architectures.
It is not ARM specific thing. I mean it is used by task_work before returning to user-space.
+ * Structure to pass task work to be handled before
+ * returning to user-space via task_work_add().
Quoted 11 lines
Quoted 11 lines
drivers/acpi/apei/ghes.c +7
-1*/ -static void ghes_kick_task_work (struct callback_head *head) +struct sync_task_work { +struct callback_head twork; +u64 pfn; +int flags; +}; + +static void memory_failure_cb (struct callback_head *twork) {
Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 487
Quoted 6 lines
drivers/acpi/apei/ghes.c +3
-3-struct acpi_hest_generic_status *estatus; -struct ghes_estatus_node *estatus_node; -u32 node_len; +int ret; +struct sync_task_work *twcb = + container_of(twork, struct sync_task_work, twork);Ugly linebreak - no need for it.
Will fix it.
Quoted 91 lines
Quoted 91 lines
drivers/acpi/apei/ghes.c +24
-19- 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 ("Sending SIGBUS to current task due to memory error not recovered" ); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct sync_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;@@ -499,6 +512,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; }@@ -746,7 +771,7 @@ int cxl_cper_unregister_callback(cxl_cper_callback callback) }EXPORT_SYMBOL_NS_GPL (cxl_cper_unregister_callback,CXL ); -static bool ghes_do_proc (struct ghes *ghes, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;@@ -814,8 +839,6 @@ static bool ghes_do_proc(struct ghes *ghes,pr_err ("Sending SIGBUS to current task due to memory error not recovered" );force_sig (SIGBUS ); } - -return queued; } static void __ghes_print_estatus(const char *pfx,@@ -1117,9 +1140,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);/* @@ -1134,25 +1155,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)
Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_proc_in_irq(struct irq_work *irq_work) · line 1155
Quoted 6 lines
drivers/acpi/apei/ghes.c +0
-6- estatus_node->task_work.func =NULL ; - } - -if (!estatus_node->task_work.func) -gen_pool_free (ghes_estatus_pool, - (unsigned long )estatus_node, node_len);I have no clue why this is being removed.
Before this patch, a memory_failure() work is queued into workqueue for both the asynchronous interrupt and synchronous exception. So memory_failure() will be executed asynchronously. For NMIlike notifications, commit 7f17b4a121d0 ("ACPI: APEI: Kick the memory_failure() queue for synchronous errors") keeps track of whether memory_failure() work was queued, and makes task_work pending to flush out the queue. It ensures any queued work has been done before we return to the context that triggered the notification.
In this patch:
- a memory_failure() work is queued into workqueue for asynchronous interrupt
- a memory_failure() task_work is queued by task_work_add for synchronous exception
The memory_failure() task_work will be handled before returning to user space, so we does not need to queue a flushing task_work any anymore.
Quoted 2 lines
Why doesn't a synchronous exception on ARM call into
ghes_proc_in_irq()?
/* * SEA can interrupt SError, mask it and describe this as an NMI so * that APEI defers the handling. */ local_daif_restore(DAIF_ERRCTX); nmi_enter(); => ghes_notify_sea => ghes_in_nmi_spool_from_list => ghes_in_nmi_queue_one_entry // also called in __ghes_sdei_callback => irq_work_queue(&ghes_proc_irq_work); nmi_exit();
Quoted 10 lines
That SDEI thing certainly does.
Well looka here:
7f17b4a121d0("ACPI: APEI: Kick thememory_failure()queue for synchronous errors")that thing does exactly what you're trying to "fix". So why doesn't that work for you?
Commit a70297d22132 (ACPI: APEI: set memory failure flags as MF_ACTION_REQUIRED on synchronous events) set MF_ACTION_REQUIRED for synchronous events.
/*
* Send all the processes who have the page mapped a signal.
* ``action optional'' if they are not immediately affected by the error
* ``action required'' if error happened in current execution context
*/
static int kill_proc(struct to_kill *tk, unsigned long pfn, int flags)
{
...
if ((flags & MF_ACTION_REQUIRED) && (t == current))
ret = force_sig_mceerr(BUS_MCEERR_AR,
(void __user *)tk->addr, addr_lsb);
else
ret = send_sig_mceerr(BUS_MCEERR_AO, (void __user *)tk->addr,
addr_lsb, t);
...
}Because the memory_failure() running in a kthread context, the false branch in kill_proc() will send SIGBUS with BUS_MCEERR_AO. But we except it as a BUS_MCEERR_AR.
Thank you for valuable comments :)
Best Regards,
Shuai
Shuai Xue
reviewerv12 cover ACPI: APEI: handle synchronous errors in task work
In reply to Shuai Xue## Changes Log
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 | 86 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 22 +++------ 5 files changed, 60 insertions(+), 59 deletions(-)
Signature · 1 line
2.39.3
Shuai Xue
reviewerRe: [PATCH v12 0/3] ACPI: APEI: handle synchronous errors in task work
Hi, all,
Gentle ping.
Best Regards,
Shuai
Quoted 204 lines · Shuai Xue
## Changes Log
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 | 86 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 22 +++------ 5 files changed, 60 insertions(+), 59 deletions(-)
Shuai Xue
reviewerPATCH v12 1/3 ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
In reply to Shuai XueSynchronous 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 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()
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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/acpi/apei/ghes.c +10 -0
@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Signature · 1 line
2.39.3
Re: [PATCH v12 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 34 lines · Shuai Xue
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 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()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.Suggested-by Xiaofei Tan <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current));
Hmm... doest this need "hardware" or would "memory corruption" be enough?
Also, does this need to say that it is sending SIGBUS when the signal itself tells that already?
I.e. could "%s:%d has memory corruption" be enough information?
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 816
Quoted 6 lines
drivers/acpi/apei/ghes.c +3
-0+force_sig (SIGBUS ); + } +return queued; }
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v12 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 49 lines · Jarkko Sakkinen
Quoted 34 lines · Shuai Xue
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 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()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.Suggested-by Xiaofei Tan <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current));Hmm... doest this need "hardware" or would "memory corruption" be enough?
Quoted 4 lines
Also, does this need to say that it is sending SIGBUS when the signal itself tells that already?
I.e. could "%s:%d has memory corruption" be enough information?
Hi, Jarkko,
Thank you for your suggestion. Maybe it could.
There are some similar error info which use "hardware memory error", e.g.
static void kill_me_maybe(struct callback_head *cb)
{
pr_err("Uncorrected hardware memory error in user-access at %llx",
p->mce_addr);
...
pr_err("Memory error not recovered");
kill_me_now(cb);
}
static int kill_proc(struct to_kill *tk, unsigned long pfn, int flags)
{
pr_err("%#lx: Sending SIGBUS to %s:%d due to hardware memory
corruption\n",
pfn, t->comm, task_pid_nr(t));
...
ret = force_sig_mceerr(BUS_MCEERR_AR,
(void __user *)tk->addr, addr_lsb);
} So, personally, I prefer this info to be consistent with them.
Best Regards,
Shuai
Re: [PATCH v12 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 55 lines · Shuai Xue
Quoted 52 lines · Jarkko Sakkinen
Quoted 34 lines · Shuai Xue
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 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()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.Suggested-by Xiaofei Tan <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current));Hmm... doest this need "hardware" or would "memory corruption" be enough?
Also, does this need to say that it is sending SIGBUS when the signal itself tells that already?
Quoted 7 lines
Quoted 1 line
I.e. could "%s:%d has memory corruption" be enough information?
Hi, Jarkko,
Thank you for your suggestion. Maybe it could.
There are some similar error info which use "hardware memory error", e.g.
By tweaking my original suggestion just a bit:
"%s:%d: hardware memory corruption"
Can't get clearer than that, right?
BR, Jarkko
Re: [PATCH v12 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 62 lines · Jarkko Sakkinen
Quoted 61 lines · Shuai Xue
Quoted 49 lines · Jarkko Sakkinen
Quoted 34 lines · Shuai Xue
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 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()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.Suggested-by Xiaofei Tan <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current));Hmm... doest this need "hardware" or would "memory corruption" be enough?
Quoted 4 lines
Also, does this need to say that it is sending SIGBUS when the signal itself tells that already?
I.e. could "%s:%d has memory corruption" be enough information?
Hi, Jarkko,
Thank you for your suggestion. Maybe it could.
Quoted 7 lines
Quoted 1 line
There are some similar error info which use "hardware memory error", e.g.
By tweaking my original suggestion just a bit:
"%s:%d: hardware memory corruption"
Can't get clearer than that, right?
And obvious reason that shorter and more consistent klog message is easy to spot and grep. It is simply less convoluted.
If you want also SIGBUS, I'd just put it as "%s:%d: hardware memory corruption (SIGBUS)"
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v12 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 71 lines · Jarkko Sakkinen
Quoted 62 lines · Jarkko Sakkinen
Quoted 61 lines · Shuai Xue
Quoted 49 lines · Jarkko Sakkinen
Quoted 34 lines · Shuai Xue
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 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()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.Suggested-by Xiaofei Tan <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current));Hmm... doest this need "hardware" or would "memory corruption" be enough?
Quoted 4 lines
Also, does this need to say that it is sending SIGBUS when the signal itself tells that already?
I.e. could "%s:%d has memory corruption" be enough information?
Hi, Jarkko,
Thank you for your suggestion. Maybe it could.
Quoted 7 lines
Quoted 1 line
There are some similar error info which use "hardware memory error", e.g.
By tweaking my original suggestion just a bit:
"%s:%d: hardware memory corruption"
Can't get clearer than that, right?
Quoted 7 lines
And obvious reason that shorter and more consistent klog message is easy to spot and grep. It is simply less convoluted.
If you want also SIGBUS, I'd just put it as "%s:%d: hardware memory corruption (SIGBUS)"
BR, Jarkko
Hi, Jarkko,
I will change it to "%s:%d: hardware memory corruption (SIGBUS)".
Thank you for valuable suggestion.
Best Regards,
Shuai
Re: [PATCH v12 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 80 lines · Shuai Xue
Quoted 77 lines · Jarkko Sakkinen
Quoted 62 lines · Jarkko Sakkinen
Quoted 61 lines · Shuai Xue
Quoted 49 lines · Jarkko Sakkinen
Quoted 34 lines · Shuai Xue
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 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()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.Suggested-by Xiaofei Tan <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + current->comm,task_pid_nr (current));Hmm... doest this need "hardware" or would "memory corruption" be enough?
Quoted 4 lines
Also, does this need to say that it is sending SIGBUS when the signal itself tells that already?
I.e. could "%s:%d has memory corruption" be enough information?
Hi, Jarkko,
Thank you for your suggestion. Maybe it could.
Quoted 7 lines
Quoted 1 line
There are some similar error info which use "hardware memory error", e.g.
By tweaking my original suggestion just a bit:
"%s:%d: hardware memory corruption"
Can't get clearer than that, right?
And obvious reason that shorter and more consistent klog message is easy to spot and grep. It is simply less convoluted.
If you want also SIGBUS, I'd just put it as "%s:%d: hardware memory corruption (SIGBUS)"
Quoted 7 lines
Quoted 1 line
BR, Jarkko
Hi, Jarkko,
I will change it to "%s:%d: hardware memory corruption (SIGBUS)".
Thank you for valuable suggestion.
Yeah, no intention nitpick, it has a practical value when debugging issues :-)
Quoted 3 lines
Best Regards,
Shuai
BR, Jarkko
Shuai Xue
reviewerPATCH v12 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 | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/mce/core.c +0 -7
@@ -1361,13 +1361,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 +6 -3
@@ -2199,9 +2199,12 @@ 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 values: +* 0 - success, +* -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 v12 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 34 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 immediate access to function usage and return information.Signed-off-by Shuai Xue <[email protected]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1361,13 +1361,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
-0
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2202
Quoted 7 lines
mm/memory-failure.c +1
-3@@ -2199,9 +2199,12 @@ 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 values:
s/Return values/Return:/
https://www.kernel.org/doc/Documentation/kernel-doc-nano-HOWTO.txt
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2210
Quoted 8 lines
mm/memory-failure.c +5
-0+* 0 - success, +* -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) {
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v12 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 45 lines · Jarkko Sakkinen
Quoted 34 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 immediate access to function usage and return information.Signed-off-by Shuai Xue <[email protected]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1361,13 +1361,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
-0mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2202
Quoted 7 lines
mm/memory-failure.c +1
-3@@ -2199,9 +2199,12 @@ 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 values: s/Return values/Return:/
Hi, Jarkko,
Thank you for the reminder. Will fix it in next version.
Best Regards,
Shuai
Shuai Xue
reviewerPATCH v12 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 excepction 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() 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.
--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)
drivers/acpi/apei/ghes.c +45 -33
@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 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 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 task_work *twcb = container_of(twork, struct task_work, twork); +unsigned long pfn = twcb->pfn; +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 ("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + pfn, current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;
@@ -501,6 +515,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,7 +771,7 @@ 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, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;
@@ -810,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,
@@ -1113,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);/*
@@ -1130,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; }
@@ -1209,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
@@ -3981,7 +3981,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
@@ -2486,19 +2486,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
Re: [PATCH v12 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 47 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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 passed
Quoted 7 lines
The
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:
s/patch set/patch/
Quoted 242 lines
# 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.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +45
-33@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 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 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 task_work *twcb = container_of(twork, struct task_work, twork); +unsigned long pfn = twcb->pfn; +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 ("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + pfn, current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;@@ -501,6 +515,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,7 +771,7 @@ 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, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;@@ -810,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,@@ -1113,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);/* @@ -1130,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; }@@ -1209,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@@ -3981,7 +3981,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
-6@@ -2486,19 +2486,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) -{
mm/memory-failure.c static void memory_failure_work_func(struct work_struct *work) · line 2491
Quoted 10 lines
mm/memory-failure.c +0
-7-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;
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v12 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 51 lines · Jarkko Sakkinen
Quoted 50 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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.
Quoted 7 lines
Quoted 4 lines
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:
s/patch set/patch/
Hi, Jarkko,
Will fix the typo in next version.
Thank you.
Best Regards,
Shuai
Shuai Xue
reviewerPATCH v13 1/3 ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
In reply to Shuai XueSynchronous 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/acpi/apei/ghes.c +10 -0
@@ -801,6 +801,16 @@ 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) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Signature · 1 line
2.39.3
Re: [PATCH v13 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 33 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +3
-0@@ -801,6 +801,16 @@ static bool ghes_do_proc(struct ghes *ghes, } } +/* +* If no memory failure work is queued for abnormal synchronous +* errors, do a force kill.
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 816
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0+*/ +if (sync && !queued) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Looks good to me!
BR, Jarkko
Shuai Xue
reviewerv13 cover ACPI: APEI: handle synchronous errors in task work
In reply to Shuai Xue## Changes Log
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 | 86 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 22 +++------ 5 files changed, 60 insertions(+), 59 deletions(-)
Signature · 1 line
2.39.3
Shuai Xue
reviewerPATCH v13 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 | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/mce/core.c +0 -7
@@ -1373,13 +1373,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 +6 -3
@@ -2199,9 +2199,12 @@ 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, +* -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 v13 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 39 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 immediate access to function usage and return information.Signed-off-by Shuai Xue <[email protected]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1373,13 +1373,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
-2@@ -2199,9 +2199,12 @@ 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,
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2210
Quoted 10 lines
mm/memory-failure.c +6
-1-* < 0(except -EOPNOTSUPP) on failure. +* Return: +* 0 - success, +* -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) {
BR, Jarkko
Shuai Xue
reviewerPATCH v13 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 excepction 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() 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:
#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.
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 BUS_MCEERR_AR error as we expected.
Issue 2: a synchronous error infinite loop
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 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 | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)
drivers/acpi/apei/ghes.c +45 -33
@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 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 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 task_work *twcb = container_of(twork, struct task_work, twork); +unsigned long pfn = twcb->pfn; +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 ("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + pfn, current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;
@@ -501,6 +515,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,7 +771,7 @@ 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, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;
@@ -810,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,
@@ -1113,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);/*
@@ -1130,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; }
@@ -1209,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
@@ -3992,7 +3992,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
@@ -2486,19 +2486,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
Re: [PATCH v13 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 122 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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:
#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.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 =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
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 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 above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +5
-3@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 task_work - for synchronous RAS event +* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: work control flags
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 483
Quoted 10 lines
drivers/acpi/apei/ghes.c +8
-1+* +* 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 task_work { +struct callback_head twork; +u64 pfn; +int flags; +};
I'd rename this as ghes_task_work. It is too generic name IMHO, easily confused with task_work.h definitions.
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v13 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 137 lines · Jarkko Sakkinen
Quoted 122 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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:
#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.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 =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
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 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 above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +5
-3@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 task_work - for synchronous RAS event +* +* @twork: callback_head for task work +* @pfn: page frame number of corrupted page +* @flags: work control flags drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 483
Quoted 10 lines
drivers/acpi/apei/ghes.c +8
-1+* +* 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 task_work { +struct callback_head twork; +u64 pfn; +int flags; +};I'd rename this as
ghes_task_work. It is too generic name IMHO, easily confused withtask_work.hdefinitions.
Quoted 1 line
BR, Jarkko
Agreed, I will rename it in next version.
Thank you.
Best Regards,
Shuai
Shuai Xue
reviewerv14 cover ACPI: APEI: handle synchronous errors in task work
In reply to Shuai Xue## Changes Log
changes since v12:
- 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 | 86 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 22 +++------ 5 files changed, 60 insertions(+), 59 deletions(-)
Signature · 1 line
2.39.3
Shuai Xue
reviewerPATCH v14 1/3 ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
In reply to Shuai XueSynchronous 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/acpi/apei/ghes.c +10 -0
@@ -801,6 +801,16 @@ 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) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Signature · 1 line
2.39.3
Re: [PATCH v14 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 14 lines · On Mon, 14 Oct 2024 16:42:38 +0800 Shuai Xue <[email protected]> wrote:
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.
Quoted 5 lines
Fix it by performing a force kill if no
memory_failure()work is queued for synchronous errors.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>
The subtle cases in here are the various other forms of delayed handling buried in some of the record handling that don't set queued. I've been through them all and have convinced myself that either hey should never be synchronous or that there is no attempt to recover in kernel today (non memory things such as CXL protocol collapse, which might I guess be detected synchronously on a read
- though I'd expect poison and a memory error first) so the correct thing to do is what you have here.
Fiddly code though with a lot of paths, so more eyes welcome!
+CC linux-cxl for info.
Quoted 25 lines
--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +10
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Shuai Xue
reviewerRe: [PATCH v14 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 32 lines · Jonathan Cameron
Quoted 14 lines · On Mon, 14 Oct 2024 16:42:38 +0800 Shuai Xue <[email protected]> wrote:
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.Quoted 5 lines
Fix it by performing a force kill if no
memory_failure()work is queued for synchronous errors.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>The subtle cases in here are the various other forms of delayed handling buried in some of the record handling that don't set queued. I've been through them all and have convinced myself that either hey should never be synchronous or that there is no attempt to recover in kernel today (non memory things such as CXL protocol collapse, which might I guess be detected synchronously on a read
- though I'd expect poison and a memory error first) so the correct thing to do is what you have here.
Quoted 5 lines
Fiddly code though with a lot of paths, so more eyes welcome!
Reviewed-by Jonathan Cameron <[email protected]>+CC linux-cxl for info.
Thanks :)
Best Regards,
Shuai
Shuai Xue
reviewerPATCH v14 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 | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/mce/core.c +0 -7
@@ -1373,13 +1373,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 +6 -3
@@ -2209,9 +2209,12 @@ 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, +* -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 v14 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 7 lines · On Mon, 14 Oct 2024 16:42:39 +0800 Shuai Xue <[email protected]> wrote:
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 immediate access to function usage and return information.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>
Seems sensible.
Quoted 43 lines
--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1373,13 +1373,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 +6
-3@@ -2209,9 +2209,12 @@ 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, +* -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) {
Shuai Xue
reviewerRe: [PATCH v14 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 14 Oct 2024 16:42:39 +0800 Shuai Xue <[email protected]> wrote:
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 immediate access to function usage and return information.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Seems sensible.
Reviewed-by Jonathan Cameron <[email protected]>
Thanks.
Best Regards,
Shuai
Shuai Xue
reviewerPATCH v14 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 excepction 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() 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:
#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.
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 BUS_MCEERR_AR error as we expected.
Issue 2: a synchronous error infinite loop
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 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 | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)
drivers/acpi/apei/ghes.c +45 -33
@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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); +unsigned long pfn = twcb->pfn; +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 ("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + pfn, current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); }static bool ghes_do_memory_failure (u64 physical_addr,int flags) {unsigned long pfn; +struct ghes_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;
@@ -501,6 +515,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,7 +771,7 @@ 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, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;
@@ -810,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,
@@ -1113,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);/*
@@ -1130,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; }
@@ -1209,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
@@ -3923,7 +3923,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
@@ -2496,19 +2496,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
Re: [PATCH v14 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 4 lines · On Mon, 14 Oct 2024 16:42:40 +0800 Shuai Xue <[email protected]> wrote:
The 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 excepction in arm64 platform), e.g. when a CPU
exception
Quoted 3 lines
tries to access a poisoned cache line. Currently, both synchronous and asynchronous error use
memory_failure_queue()to schedulememory_failure()exectute in kworker context.
memory_failure() to execute in kworker context.
(spell check in general)
Quoted 4 lines
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:
context, memory_failure():
//No subject of the following two bullets otherwise.
Quoted 52 lines
- 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:
#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.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 =0xffffb0d75400paddr =4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr0xffffb0d75000
page not present
Test passed
Quoted 6 lines
The
si_code(code 4) fromeinj_mem_ucindicates that it isBUS_MCEERR_ARerror as we expected.Issue 2: a synchronous error infinite loop
If a user-space process, e.g. devmem, a poisoned page which has been set
devmem accesses a poisoned page for which the HWPoison flag is set
Quoted 3 lines
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
context
Quoted 2 lines
EFAULT. So, devmem will access the posioned page and trigger an excepction again, resulting in a synchronous error infinite loop. Such
exception
Quoted 17 lines
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
0x4092d55b400
Quoted 8 lines
To fix above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>
One other trivial comment inline.
Whilst this also looks fine to me, there are others who (hopefully) understand these paths better than me. With that said.
Quoted 31 lines
--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +11
-4@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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;
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 488
Quoted 10 lines
drivers/acpi/apei/ghes.c +6
-3+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); +unsigned long pfn = twcb->pfn;
This local variable is not used consistently. I'd just drop it in favor of always accessing via twcb->pfn
Quoted 19 lines
drivers/acpi/apei/ghes.c +9
-6+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 ("%#lx: Sending SIGBUS to %s:%d due to hardware memory corruption\n" , + pfn, current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); }
Shuai Xue
reviewerRe: [PATCH v14 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 8 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 14 Oct 2024 16:42:40 +0800 Shuai Xue <[email protected]> wrote:
The 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 excepction in arm64 platform), e.g. when a CPU
Quoted 8 lines
exception
Quoted 3 lines
tries to access a poisoned cache line. Currently, both synchronous and asynchronous error use
memory_failure_queue()to schedulememory_failure()exectute in kworker context.
memory_failure()to execute in kworker context.(spell check in general)
Sorry for typos. Will fix it in next version. Thanks.
Quoted 6 lines
Quoted 4 lines
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:context,
memory_failure():
//No subject of the following two bullets otherwise.
I see, will fix it.
Quoted 65 lines
Quoted 52 lines
- 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:
#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.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 =0xffffb0d75400paddr =4092d55b400
injecting ...
triggering ...
signal 7 code 4 addr0xffffb0d75000
page not present
Test passedQuoted 6 lines
The
si_code(code 4) fromeinj_mem_ucindicates that it isBUS_MCEERR_ARerror as we expected.Issue 2: a synchronous error infinite loop
If a user-space process, e.g. devmem, a poisoned page which has been set
devmem accesses a poisoned page for which the HWPoison flag is set
Quoted 3 lines
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
Quoted 6 lines
context
Quoted 2 lines
EFAULT. So, devmem will access the posioned page and trigger an excepction again, resulting in a synchronous error infinite loop. Such
exception
I see, will fix it.
Quoted 26 lines
Quoted 18 lines
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
0x4092d55b400Quoted 8 lines
To fix above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>
Quoted 6 lines
One other trivial comment inline.
Whilst this also looks fine to me, there are others who (hopefully) understand these paths better than me. With that said.
Reviewed-by Jonathan Cameron <[email protected]>
Thank you for valuable comments.
Quoted 37 lines
Quoted 37 lines
--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +15
-4@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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) {
Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 488
Quoted 5 lines
drivers/acpi/apei/ghes.c +2
-3-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); +unsigned long pfn = twcb->pfn;This local variable is not used consistently. I'd just drop it in favor of always accessing via
twcb->pfn
Agreed, will remove local pfn.
Best Regards,
Shuai
Shuai Xue
reviewerRe: [PATCH v14 3/3] ACPI: APEI: handle synchronous exceptions in task work
In reply to Shuai XueHi, Jarkko,
Quoted 116 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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:
#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.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 =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
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 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 above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +0
-2@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* Called as task_work before returning to user-space. -* Ensure any queued work has been done before we return to the context that
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 478
Quoted 10 lines
drivers/acpi/apei/ghes.c +8
-1-* 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(). */
Do you have any futer comments about this patch? Any comments are welcomed. If not, are you happy to explictly give the reveiwed-by tag?
Best Regard,
Shuai
Jarkko Sakkinen
Reviewed-byRe: [PATCH v14 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 126 lines · Shuai Xue
Hi, Jarkko,
Quoted 122 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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:
#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.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 =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
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 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 above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +5
-3@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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
Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 478
Quoted 4 lines
drivers/acpi/apei/ghes.c +3
-0+* +* Structure to pass task work to be handled before +* returning to user-space via task_work_add(). */ Do you have any futer comments about this patch? Any comments are welcomed. If not, are you happy to explictly give the reveiwed-by tag?
Sorry I've been busy switching to a new job.
I read this now through and both commit messages and the code changes look sane to me so I guess I don't have any problem with that:
Quoted 3 lines
Best Regard,
Shuai
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v14 3/3] ACPI: APEI: handle synchronous exceptions in task work
Quoted 141 lines · Jarkko Sakkinen
Quoted 126 lines · Shuai Xue
Hi, Jarkko,
Quoted 122 lines · Shuai Xue
The 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 excepction 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 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 the
memory_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:
#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.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 =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
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 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 above two issues, queue
memory_failure()as atask_workso that it runs in the context of the process that is actually consuming the poisoned data.Signed-off-by Shuai Xue <[email protected]>Tested-by Ma Wupeng <[email protected]>Reviewed-by Kefeng Wang <[email protected]>Reviewed-by Xiaofei Tan <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- drivers/acpi/apei/ghes.c | 78 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 45 insertions(+), 50 deletions(-)drivers/acpi/apei/ghes.c +5
-3@@ -467,28 +467,42 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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 Quoted 8 lines
drivers/acpi/apei/ghes.c static void ghes_clear_estatus(struct ghes *ghes, · line 478
Quoted 4 lines
drivers/acpi/apei/ghes.c +3
-0+* +* Structure to pass task work to be handled before +* returning to user-space via task_work_add(). */ Do you have any futer comments about this patch? Any comments are welcomed. If not, are you happy to explictly give the reveiwed-by tag?
Sorry I've been busy switching to a new job.
I read this now through and both commit messages and the code changes look sane to me so I guess I don't have any problem with that:
Quoted 7 lines
Reviewed-by Jarkko Sakkinen <[email protected]>Quoted 3 lines
Best Regard,
ShuaiBR, Jarkko
Thank you.
BR. Shuai
Shuai Xue
reviewerv15 cover ACPI: APEI: handle synchronous errors in task work
In reply to Shuai Xue## Changes Log
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 | 85 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 22 +++------ 5 files changed, 59 insertions(+), 59 deletions(-)
Signature · 1 line
2.39.3
Shuai Xue
reviewerPATCH v15 1/3 ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
In reply to Shuai XueSynchronous 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/acpi/apei/ghes.c +10 -0
@@ -801,6 +801,16 @@ 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) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current)); +force_sig (SIGBUS ); + } +return queued; }
Signature · 1 line
2.39.3
Re: [PATCH v15 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 29 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 10 lines
drivers/acpi/apei/ghes.c +7
-0@@ -801,6 +801,16 @@ 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) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current));
I think it would help to include the GHES_PFX to indicate where this message is coming from. The pr_fmt() macro could also be introduced instead.
Also, you may want to include the HW_ERR prefix. Not all kernel messages related to hardware errors have this prefix today. But maybe that should be changed so there is more consistent messaging.
Thanks,
Yazen
Shuai Xue
reviewerRe: [PATCH v15 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 33 lines · Yazen Ghannam
Quoted 32 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0@@ -801,6 +801,16 @@ static bool ghes_do_proc(struct ghes *ghes, } }
Quoted 11 lines
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 7 lines
drivers/acpi/apei/ghes.c +7
-0+/* +* If no memory failure work is queued for abnormal synchronous +* errors, do a force kill. +*/ +if (sync && !queued) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current));I think it would help to include the
GHES_PFXto indicate where this message is coming from. Thepr_fmt()macro could also be introduced instead.
Yes, GHES_PFX is a effective prefix and will be consistent to other message in GHES driver. Will add it in next version.
What do you mean about pr_fmt()?
Quoted 5 lines
Also, you may want to include the
HW_ERRprefix. Not all kernel messages related to hardware errors have this prefix today. But maybe that should be changed so there is more consistent messaging.
Do we really need a HW_ERR prefix? The other case which use HW_ERR prefix are for hardware registers. The messages which send SIGBUS does not include HW_ERR, e.g. in kill_proc(), kill_procs().
pr_err("%#lx: Sending SIGBUS to %s:%d due to hardware memory
corruption\n",...
pr_err("%#lx: forcibly killing %s:%d because of failure to unmap
corrupted page\n",...
Quoted 2 lines
Thanks,
Yazen
Thanks for valuable comments.
Best Regards,
Shuai
Re: [PATCH v15 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 47 lines · Shuai Xue
Quoted 35 lines · Yazen Ghannam
Quoted 34 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +3
-0@@ -801,6 +801,16 @@ static bool ghes_do_proc(struct ghes *ghes, } } +/* +* If no memory failure work is queued for abnormal synchronous +* errors, do a force kill. Quoted 8 lines
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 4 lines
drivers/acpi/apei/ghes.c +4
-0+*/ +if (sync && !queued) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current));I think it would help to include the
GHES_PFXto indicate where this message is coming from. Thepr_fmt()macro could also be introduced instead.
Quoted 4 lines
Yes,
GHES_PFXis a effective prefix and will be consistent to other message in GHES driver. Will add it in next version.What do you mean about
pr_fmt()?
This can be used to set a prefix for an entire section of code. The pr_*() macros will pick it up without needing to include a prefix for each call.
This is described in "Documentation/core-api/printk-basics.rst".
Quoted 7 lines
Quoted 6 lines
Also, you may want to include the
HW_ERRprefix. Not all kernel messages related to hardware errors have this prefix today. But maybe that should be changed so there is more consistent messaging.
Quoted 10 lines
Do we really need a
HW_ERRprefix? The other case which useHW_ERRprefix are for hardware registers. The messages which send SIGBUS does not includeHW_ERR, e.g. inkill_proc(),kill_procs().pr_err("%#lx: Sending SIGBUS to %s:%d due to hardware memory
corruption\n",...
pr_err("%#lx: forcibly killing %s:%d because of failure to unmap
corrupted page\n",...
Correct, HW_ERR isn't used there. My interpretation is that it can be used whenever an event is due to a hardware error (real or simulated). This is a very clear message to a user.
It may be redundant in some cases (like here where the message already says "hardware memory corruption"). But I think it would be go to use it anyway for consistency.
I think other relevant places in the kernel should also be updated. But that is beyond this patch, and I don't expect it to be done here and now.
Thanks,
Yazen
Shuai Xue
reviewerRe: [PATCH v15 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 51 lines · Yazen Ghannam
Quoted 50 lines · Shuai Xue
Quoted 35 lines · Yazen Ghannam
Quoted 34 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +3
-0@@ -801,6 +801,16 @@ static bool ghes_do_proc(struct ghes *ghes, } } +/* +* If no memory failure work is queued for abnormal synchronous +* errors, do a force kill. Quoted 8 lines
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 810
Quoted 4 lines
drivers/acpi/apei/ghes.c +4
-0+*/ +if (sync && !queued) { +pr_err ("%s:%d: hardware memory corruption (SIGBUS)\n" , + current->comm,task_pid_nr (current));I think it would help to include the
GHES_PFXto indicate where this message is coming from. Thepr_fmt()macro could also be introduced instead.Yes,
GHES_PFXis a effective prefix and will be consistent to other message in GHES driver. Will add it in next version.
Quoted 7 lines
Quoted 1 line
What do you mean about
pr_fmt()?This can be used to set a prefix for an entire section of code. The pr_*() macros will pick it up without needing to include a prefix for each call.
This is described in "
Documentation/core-api/printk-basics.rst".
Got you point, a pr_fmt is much more convenient, but it is beyond this patch. I would like to send a patch to add pr_fmt and then replace each call after this patch merged.
Quoted 23 lines
Quoted 8 lines
Quoted 7 lines
Also, you may want to include the
HW_ERRprefix. Not all kernel messages related to hardware errors have this prefix today. But maybe that should be changed so there is more consistent messaging.Quoted 10 lines
Do we really need a
HW_ERRprefix? The other case which useHW_ERRprefix are for hardware registers. The messages which send SIGBUS does not includeHW_ERR, e.g. inkill_proc(),kill_procs().pr_err("%#lx: Sending SIGBUS to %s:%d due to hardware memory
corruption\n",...
pr_err("%#lx: forcibly killing %s:%d because of failure to unmap
corrupted page\n",...Correct,
HW_ERRisn't used there. My interpretation is that it can be used whenever an event is due to a hardware error (real or simulated). This is a very clear message to a user.
Quoted 7 lines
It may be redundant in some cases (like here where the message already says "hardware memory corruption"). But I think it would be go to use it anyway for consistency.
I think other relevant places in the kernel should also be updated. But that is beyond this patch, and I don't expect it to be done here and now.
Ok, I will add the HW_ERR in next version, if no one else objects.
Quoted 3 lines
Thanks,
Yazen
Thank you.
Best Regards,
Shuai
Shuai Xue
reviewerPATCH v15 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 | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/mce/core.c +0 -7
@@ -1373,13 +1373,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 +6 -3
@@ -2209,9 +2209,12 @@ 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, +* -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
Shuai Xue
reviewerPATCH v15 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 | 77 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 44 insertions(+), 50 deletions(-)
drivers/acpi/apei/ghes.c +44 -33
@@ -467,28 +467,41 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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) {unsigned long pfn; +struct ghes_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;
@@ -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,7 +770,7 @@ 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, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;
@@ -810,8 +835,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,
@@ -1113,9 +1136,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);/*
@@ -1130,25 +1151,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; }
@@ -1209,7 +1221,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
@@ -3923,7 +3923,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
@@ -2496,19 +2496,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
reviewerv16 cover ACPI: APEI: handle synchronous errors in task work
In reply to Shuai Xue## Changes Log
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 | 85 +++++++++++++++++++++------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 22 +++------ 5 files changed, 59 insertions(+), 59 deletions(-)
Signature · 1 line
2.39.3
Shuai Xue
reviewerPATCH v16 1/3 ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
In reply to Shuai XueSynchronous 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 | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/acpi/apei/ghes.c +10 -0
@@ -801,6 +801,16 @@ 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) { + pr_err(HW_ERR GHES_PFX "%s:%d: hardware memory corruption (SIGBUS)\n", + current->comm, task_pid_nr(current)); +force_sig (SIGBUS ); + } +return queued; }
Signature · 1 line
2.39.3
Re: [PATCH v16 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 29 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 809
Quoted 9 lines
drivers/acpi/apei/ghes.c +6
-0@@ -801,6 +801,16 @@ 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) { + pr_err(HW_ERR GHES_PFX "%s:%d: hardware memory corruption (SIGBUS)\n",
Is this always a memory error? The code flow above implies that an unrecoverable ARM processor error can all be !queued. So should the message be more generic like "synchronous unrecoverable error" or similar?
In any case, this is just a minor nit if this is not an issue in practice.
Thanks,
Yazen
Re: [PATCH v16 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
Quoted 20 lines · Yazen Ghannam
Quoted 5 lines
drivers/acpi/apei/ghes.c +0
-0drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 809
Quoted 9 lines
drivers/acpi/apei/ghes.c +6
-0@@ -801,6 +801,16 @@ 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) { + pr_err(HW_ERR GHES_PFX "%s:%d: hardware memory corruption (SIGBUS)\n",Is this always a memory error? The code flow above implies that an unrecoverable ARM processor error can all be !queued. So should the message be more generic like "synchronous unrecoverable error" or similar?
Quoted 2 lines
In any case, this is just a minor nit if this is not an issue in practice.
One minor thing that came to mind after reading your response: wouldn't it be a better idea to use dev_err() against ghes->dev, rather than raw pr_err()?
That would better context information and also I just (re-)checked the file and also ghes_remove() is using dev_err().
Quoted 5 lines
Reviewed-by Yazen Ghannam <[email protected]>Thanks,
Yazen
BR, Jarkko
Shuai Xue
reviewerRe: [PATCH v16 1/3] ACPI: APEI: send SIGBUS to current task if synchronous memory error not recovered
In reply to Yazen GhannamQuoted 33 lines · Yazen Ghannam
Quoted 32 lines · Shuai Xue
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.Signed-off-by Shuai Xue <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>--- drivers/acpi/apei/ghes.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/acpi/apei/ghes.c +0
-0@@ -801,6 +801,16 @@ static bool ghes_do_proc(struct ghes *ghes, } }
Quoted 11 lines
drivers/acpi/apei/ghes.c static bool ghes_do_proc(struct ghes *ghes, · line 809
Quoted 6 lines
drivers/acpi/apei/ghes.c +6
-0+/* +* If no memory failure work is queued for abnormal synchronous +* errors, do a force kill. +*/ +if (sync && !queued) { + pr_err(HW_ERR GHES_PFX "%s:%d: hardware memory corruption (SIGBUS)\n",Is this always a memory error? The code flow above implies that an unrecoverable ARM processor error can all be !queued. So should the message be more generic like "synchronous unrecoverable error" or similar?
Yes, you are right. Will fix it.
Quoted 8 lines
In any case, this is just a minor nit if this is not an issue in practice.
Reviewed-by Yazen Ghannam <[email protected]>Thanks,
Yazen
Thank you for valuable comments.
Shuai
Shuai Xue
reviewerPATCH v16 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 | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/mce/core.c +0 -7
@@ -1373,13 +1373,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 +6 -3
@@ -2209,9 +2209,12 @@ 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, +* -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 v16 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 36 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 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]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1373,13 +1373,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
-0
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2213
Quoted 8 lines
mm/memory-failure.c +2
-3@@ -2209,9 +2209,12 @@ 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,
One more obvious one from this function:
-ENXIO - memory not managed by the kernel
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2220
Quoted 8 lines
mm/memory-failure.c +4
-0+* -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) {
Thanks,
Yazen
Shuai Xue
reviewerRe: [PATCH v16 2/3] mm: memory-failure: move return value documentation to function declaration
Quoted 48 lines · Yazen Ghannam
Quoted 36 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 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]>--- arch/x86/kernel/cpu/mce/core.c | 7 ------- mm/memory-failure.c | 9 ++++++--- 2 files changed, 6 insertions(+), 10 deletions(-)arch/x86/kernel/cpu/mce/core.c +0
-7@@ -1373,13 +1373,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
-0mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2213
Quoted 8 lines
mm/memory-failure.c +2
-3@@ -2209,9 +2209,12 @@ 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, One more obvious one from this function:
Quoted 1 line
-ENXIO- memory not managed by the kernel
Yes, will fix it.
Quoted 10 lines
mm/memory-failure.c static void kill_procs_now(struct page *p, unsigned long pfn, int flags, · line 2220
Quoted 9 lines
mm/memory-failure.c +4
-0+* -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) {
Quoted 4 lines
Reviewed-by Yazen Ghannam <[email protected]>Thanks,
Yazen
Thank you for valuable comments.
Shuai
Shuai Xue
reviewerPATCH v16 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 | 77 +++++++++++++++++++++++----------------- include/acpi/ghes.h | 3 -- include/linux/mm.h | 1 - mm/memory-failure.c | 13 ------- 4 files changed, 44 insertions(+), 50 deletions(-)
drivers/acpi/apei/ghes.c +44 -33
@@ -467,28 +467,41 @@ static void ghes_clear_estatus(struct ghes *ghes, }/* -* 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) {unsigned long pfn; +struct ghes_task_work *twcb;if (!IS_ENABLED (CONFIG_ACPI_APEI_MEMORY_FAILURE ))return false;
@@ -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,7 +770,7 @@ 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, +static void ghes_do_proc (struct ghes *ghes,const struct acpi_hest_generic_status *estatus) {int sev, sec_sev;
@@ -810,8 +835,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,
@@ -1113,9 +1136,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);/*
@@ -1130,25 +1151,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; }
@@ -1209,7 +1221,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
@@ -3924,7 +3924,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
@@ -2496,19 +2496,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