ACPI: APEI: GHES: drain per-CPU estatus irq_work before freeing ghes
PATCH v2 1/1 ACPI: APEI: GHES: drain per-CPU estatus irq_work before freeing ghes
NMI, SEA, and SDEI handlers enqueue estatus nodes holding a raw estatus_node->ghes pointer and schedule irq_work to run ghes_proc_in_irq().
During ghes_remove(), ghes_nmi_remove(), ghes_sea_remove(), and apei_sdei_unregister_ghes() stop further callbacks and synchronize RCU, but do not wait for queued ghes_proc_in_irq() work before calling ghes_fini() and kfree(ghes). In addition, ghes_in_nmi_spool_from_list() calls irq_work_queue() after rcu_read_unlock(), leaving a window where synchronize_rcu() can finish before the irq_work is queued.
Furthermore, a single global ghes_proc_irq_work and ghes_estatus_llist cannot be safely synchronized across CPUs with irq_work_sync(): once irq_work_single() clears IRQ_WORK_PENDING on one CPU, a notification on another CPU can reclaim and complete the same irq_work first, clearing IRQ_WORK_BUSY while the first CPU is still executing ghes_proc_in_irq() or allowing another CPU to dequeue nodes from the shared llist after its CPU has already been synced.
Convert the estatus llist and irq_work to a paired per-CPU ghes_estatus_work structure (all callers hold a raw_spinlock, keeping llist_add() and irq_work_queue() on the same CPU), move irq_work_queue() inside the RCU read-side critical section in ghes_in_nmi_spool_from_list(), and synchronize every possible CPU's irq_work in the NMI, SEA, and SDEI removal paths before freeing ghes.
Found by static code audit.
---
Changes in v2:
- Replace the single global ghes_estatus_llist and ghes_proc_irq_work with a paired per-CPU struct ghes_estatus_work and sync all possible CPUs via ghes_sync_irq_work(), eliminating the multi-CPU IRQ_WORK_BUSY / llist-stealing race flagged by Sashiko on v1.
- Update Fixes: tags to reference 21480547c8b8 and ee2eb3d4ee17.
Testing note:
Compile-tested on x86_64 with W=1 and KASAN (drivers/acpi/apei/ghes.o); not runtime-tested on APEI/NMI/SEA/SDEI hardware.
drivers/acpi/apei/ghes.c | 80 ++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 24 deletions(-)
drivers/acpi/apei/ghes.c +56 -24
@@ -33,8 +33,10 @@#include <linux/mutex.h> #include <linux/ratelimit.h> #include <linux/vmalloc.h> +#include <linux/cpumask.h> #include <linux/irq_work.h> #include <linux/llist.h> +#include <linux/percpu.h> #include <linux/genalloc.h> #include <linux/kfifo.h> #include <linux/pci.h>
@@ -1339,18 +1341,25 @@ static struct notifier_block ghes_notifier_hed = {* Memory from the ghes_estatus_pool is also used with the ghes_estatus_cache * to suppress frequent messages. */ -static struct llist_head ghes_estatus_llist; -static struct irq_work ghes_proc_irq_work; +struct ghes_estatus_work { +struct llist_head llist; +struct irq_work irq_work; +}; + +static DEFINE_PER_CPU(struct ghes_estatus_work, ghes_estatus_work);static void ghes_proc_in_irq (struct irq_work *irq_work) { +struct ghes_estatus_work *ew =container_of (irq_work, + struct ghes_estatus_work, + irq_work);struct llist_node *llnode, *next;struct ghes_estatus_node *estatus_node;struct acpi_hest_generic *generic;struct acpi_hest_generic_status *estatus;u32 len, node_len; - llnode =llist_del_all (&ghes_estatus_llist); + llnode =llist_del_all (&ew->llist);/* * Because the time order of estatus in list is reversed, * revert it back to proper order.
@@ -1384,23 +1393,36 @@ static void ghes_print_queued_estatus(void)struct ghes_estatus_node *estatus_node;struct acpi_hest_generic *generic;struct acpi_hest_generic_status *estatus; +int cpu; - llnode =llist_del_all (&ghes_estatus_llist); -/* -* Because the time order of estatus in list is reversed, -* revert it back to proper order. -*/ - llnode =llist_reverse_order (llnode); -while (llnode) { - estatus_node = llist_entry(llnode, struct ghes_estatus_node, - llnode); - estatus =GHES_ESTATUS_FROM_NODE (estatus_node); - generic = estatus_node->generic; -ghes_print_estatus (NULL , generic, estatus); - llnode = llnode->next; +for_each_possible_cpu (cpu) { + llnode =llist_del_all (per_cpu_ptr (&ghes_estatus_work.llist, + cpu)); +/* +* Because the time order of estatus in list is reversed, +* revert it back to proper order. +*/ + llnode =llist_reverse_order (llnode); +while (llnode) { + estatus_node =llist_entry (llnode, + struct ghes_estatus_node, + llnode); + estatus =GHES_ESTATUS_FROM_NODE (estatus_node); + generic = estatus_node->generic; +ghes_print_estatus (NULL , generic, estatus); + llnode = llnode->next; + } } } +static void ghes_sync_irq_work (void ) +{ +int cpu; + +for_each_possible_cpu (cpu) +irq_work_sync (per_cpu_ptr (&ghes_estatus_work.irq_work, cpu)); +} +static int ghes_in_nmi_queue_one_entry (struct ghes *ghes,enum fixed_addresses fixmap_idx) {
@@ -1461,7 +1483,7 @@ static int ghes_in_nmi_queue_one_entry(struct ghes *ghes, goto no_work; } -llist_add (&estatus_node->llnode, &ghes_estatus_llist); +llist_add (&estatus_node->llnode,this_cpu_ptr (&ghes_estatus_work.llist));return rc;
@@ -1483,10 +1505,9 @@ static int __maybe_unused ghes_in_nmi_spool_from_list(struct list_head *rcu_listif (!ghes_in_nmi_queue_one_entry (ghes, fixmap_idx)) ret =0 ; } -rcu_read_unlock (); -if (IS_ENABLED (CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG ) && !ret) -irq_work_queue (&ghes_proc_irq_work); +irq_work_queue (this_cpu_ptr (&ghes_estatus_work.irq_work)); +rcu_read_unlock ();return ret; }
@@ -1600,6 +1621,7 @@ static void ghes_sea_remove(struct ghes *ghes)mutex_unlock (&ghes_list_mutex);ghes_unmap_error_status (ghes);synchronize_rcu (); + ghes_sync_irq_work(); }#else /* CONFIG_ACPI_APEI_SEA */ static inline int ghes_sea_add (struct ghes *ghes) {return -EINVAL ; }
@@ -1663,10 +1685,11 @@ static void ghes_nmi_remove(struct ghes *ghes)ghes_unmap_error_status (ghes);/* -* To synchronize with NMI handler, ghes can only be -* freed after NMI handler finishes. +* To synchronize with NMI handler and any queued estatus work, +* ghes can only be freed after both complete. */ synchronize_rcu (); + ghes_sync_irq_work(); }#else /* CONFIG_HAVE_ACPI_APEI_NMI */ static inline int ghes_nmi_add (struct ghes *ghes) {return -EINVAL ; }
@@ -1675,14 +1698,22 @@ static inline void ghes_nmi_remove(struct ghes *ghes) { }static void ghes_nmi_init_cxt (void ) { -init_irq_work (&ghes_proc_irq_work, ghes_proc_in_irq); +int cpu; + +for_each_possible_cpu (cpu) { +struct ghes_estatus_work *ew = +per_cpu_ptr (&ghes_estatus_work, cpu); + +init_llist_head (&ew->llist); +init_irq_work (&ew->irq_work, ghes_proc_in_irq); + } }static int __ghes_sdei_callback (struct ghes *ghes,enum fixed_addresses fixmap_idx) {if (!ghes_in_nmi_queue_one_entry (ghes, fixmap_idx)) { -irq_work_queue (&ghes_proc_irq_work); +irq_work_queue (this_cpu_ptr (&ghes_estatus_work.irq_work));return 0 ; }
@@ -1921,6 +1952,7 @@ static void ghes_remove(struct platform_device *ghes_dev) ERR_PTR(rc));return ; } +ghes_sync_irq_work ();break ;default :BUG ();
Signature · 1 line
2.51.0.windows.1