x86/nmi: Make register_nmi_handler() more robust
Subsystems (2)
Sasha Levin
maintainerPATCH 1/4 x86/nmi: Make register_nmi_handler() more robust
From: Thomas Gleixner <[email protected]>
[ Upstream commit a7fed5c0431dbfa707037848830f980e0f93cfb3 ]
register_nmi_handler() has no sanity check whether a handler has been registered already. Such an unintended double-add leads to list corruption and hard to diagnose problems during the next NMI handling.
Init the list head in the static NMI action struct and check it for being empty in register_nmi_handler().
[ bp: Fixups. ]
--- arch/x86/include/asm/nmi.h | 1 + arch/x86/kernel/nmi.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/include/asm/nmi.h +1 -0
@@ -49,6 +49,7 @@ struct nmiaction { #define register_nmi_handler(t, fn, fg, n, init...) \ ({ \ static struct nmiaction init fn##_na = { \ + .list = LIST_HEAD_INIT(fn##_na.list), \ .handler = (fn), \ .name = (n), \ .flags = (fg), \
arch/x86/kernel/nmi.c +8 -4
@@ -159,7 +159,7 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)struct nmi_desc *desc =nmi_to_desc (type);unsigned long flags; -if (!action->handler) +if (WARN_ON_ONCE (!action->handler || !list_empty (&action->list)))return -EINVAL ;init_irq_work (&action->irq_work, nmi_max_handler);
@@ -181,7 +181,7 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)list_add_rcu (&action->list, &desc->head); else list_add_tail_rcu(&action->list, &desc->head); - +raw_spin_unlock_irqrestore (&desc->lock, flags);return 0 ; }
@@ -190,7 +190,7 @@ EXPORT_SYMBOL(__register_nmi_handler);void unregister_nmi_handler (unsigned int type,const char *name) {struct nmi_desc *desc =nmi_to_desc (type); -struct nmiaction *n; +struct nmiaction *n, *found =NULL ;unsigned long flags;raw_spin_lock_irqsave (&desc->lock, flags);
@@ -204,12 +204,16 @@ void unregister_nmi_handler(unsigned int type, const char *name)WARN (in_nmi (),"Trying to free NMI (%s) from NMI context!\n" , n->name);list_del_rcu (&n->list); + found = n;break ; } }raw_spin_unlock_irqrestore (&desc->lock, flags); -synchronize_rcu (); +if (found) { +synchronize_rcu (); +INIT_LIST_HEAD (&found->list); + } }EXPORT_SYMBOL_GPL (unregister_nmi_handler);
Signature · 1 line
2.35.1
Sasha Levin
maintainerPATCH 2/4 time/sched_clock: Round the frequency reported to nearest rather than down
From: "Maciej W. Rozycki" <[email protected]>
[ Upstream commit 92067440f1311dfa4d77b57a9da6b3706f5da32e ]
The frequency reported for clock sources are rounded down, which gives misleading figures, e.g.:
I/O ASIC clock frequency 24999480Hz
sched_clock: 32 bits at 24MHz, resolution 40ns, wraps every 85901132779ns MIPS counter frequency 59998512Hz
sched_clock: 32 bits at 59MHz, resolution 16ns, wraps every 35792281591ns
Rounding to nearest is more adequate:
I/O ASIC clock frequency 24999664Hz
sched_clock: 32 bits at 25MHz, resolution 40ns, wraps every 85900499947ns MIPS counter frequency 59999728Hz
sched_clock: 32 bits at 60MHz, resolution 16ns, wraps every 35791556599ns
--- kernel/time/sched_clock.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/time/sched_clock.c +3 -2
@@ -11,6 +11,7 @@#include <linux/jiffies.h> #include <linux/ktime.h> #include <linux/kernel.h> +#include <linux/math.h> #include <linux/moduleparam.h> #include <linux/sched.h> #include <linux/sched/clock.h>
@@ -213,11 +214,11 @@ sched_clock_register(u64 (*read)(void), int bits, unsigned long rate) r = rate;if (r >=4000000 ) { - r /=1000000 ; + r =DIV_ROUND_CLOSEST (r,1000000 ); r_unit ='M' ; }else {if (r >=1000 ) { - r /=1000 ; + r =DIV_ROUND_CLOSEST (r,1000 ); r_unit ='k' ; }else { r_unit =' ' ;
Signature · 1 line
2.35.1
Sasha Levin
maintainerPATCH 3/4 lib/irq_poll: Prevent softirq pending leak in irq_poll_cpu_dead()
In reply to Sasha LevinFrom: Sebastian Andrzej Siewior <[email protected]>
[ Upstream commit 75d8cce128c516fe6cf4b8683e8fe1a59e919902 ]
irq_poll_cpu_dead() pulls the blk_cpu_iopoll backlog from the dead CPU and raises the POLL softirq with __raise_softirq_irqoff() on the CPU it is running on. That just sets the bit in the pending softirq mask.
This means the handling of the softirq is delayed until the next interrupt or a local_bh_disable/enable() pair. As a consequence the CPU on which this code runs can reach idle with the POLL softirq pending, which triggers a warning in the NOHZ idle code.
Add a local_bh_disable/enable() pair around the interrupts disabled section in irq_poll_cpu_dead(). local_bh_enable will handle the pending softirq.
[tglx: Massaged changelog and comment]
--- lib/irq_poll.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/irq_poll.c +6 -2
@@ -188,14 +188,18 @@ EXPORT_SYMBOL(irq_poll_init);static int irq_poll_cpu_dead (unsigned int cpu) {/* -* If a CPU goes away, splice its entries to the current CPU -* and trigger a run of the softirq +* If a CPU goes away, splice its entries to the current CPU and +* set the POLL softirq bit. The local_bh_disable()/enable() pair +* ensures that it is handled. Otherwise the current CPU could +* reach idle with the POLL softirq pending. */ +local_bh_disable ();local_irq_disable ();list_splice_init (&per_cpu (blk_cpu_iopoll, cpu),this_cpu_ptr (&blk_cpu_iopoll));__raise_softirq_irqoff (IRQ_POLL_SOFTIRQ );local_irq_enable (); +local_bh_enable ();return 0 ; }
Signature · 1 line
2.35.1
Sasha Levin
maintainerPATCH 4/4 x86/PCI: Add PIRQ routing table range checks
In reply to Sasha LevinFrom: "Maciej W. Rozycki" <[email protected]>
[ Upstream commit 5d64089aa4a5bd3d7e00e3d6ddf4943dd34627b3 ]
Verify that the PCI IRQ Routing Table header as well as individual slot entries are all wholly contained within the BIOS memory area. Do not even call the checksum calculator if the header would overrun the area and then bail out early if any slot would.
--- arch/x86/pci/irq.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
arch/x86/pci/irq.c +12 -5
@@ -62,7 +62,8 @@ void (*pcibios_disable_irq)(struct pci_dev *dev) = pirq_disable_irq;* and perform checksum verification. */ -static inline struct irq_routing_table *pirq_check_routing_table (u8 *addr) +static inline struct irq_routing_table *pirq_check_routing_table (u8 *addr, +u8 *limit) {struct irq_routing_table *rt;int i;
@@ -72,7 +73,8 @@ static inline struct irq_routing_table *pirq_check_routing_table(u8 *addr)if (rt->signature !=PIRQ_SIGNATURE || rt->version !=PIRQ_VERSION || rt->size %16 || - rt->size <sizeof (struct irq_routing_table )) + rt->size <sizeof (struct irq_routing_table ) || + (limit && rt->size > limit - addr))return NULL ; sum =0 ;for (i =0 ; i < rt->size; i++)
@@ -93,17 +95,22 @@ static inline struct irq_routing_table *pirq_check_routing_table(u8 *addr)static struct irq_routing_table *__init pirq_find_routing_table (void ) { +u8 *const bios_start = (u8 *)__va (0xf0000 ); +u8 *const bios_end = (u8 *)__va (0x100000 );u8 *addr;struct irq_routing_table *rt;if (pirq_table_addr) { - rt =pirq_check_routing_table ((u8 *)__va (pirq_table_addr)); + rt =pirq_check_routing_table ((u8 *)__va (pirq_table_addr), +NULL );if (rt)return rt;printk (KERN_WARNING "PCI: PIRQ table NOT found at pirqaddr\n" ); } -for (addr = (u8 *)__va (0xf0000 ); addr < (u8 *)__va (0x100000 ); addr +=16 ) { - rt =pirq_check_routing_table (addr); +for (addr = bios_start; + addr < bios_end -sizeof (struct irq_routing_table ); + addr +=16 ) { + rt =pirq_check_routing_table (addr, bios_end);if (rt)return rt; }
Signature · 1 line
2.35.1