Provide core API for NMIs
v6 cover Provide core API for NMIs
Hi,
This patch series provides a way for irqchips to define some IRQs as NMIs.
Updating this series as it is needed for the arm64 pseudo-NMI which we are considering to merge (I'll post a new version shortly).
Changes since v5[1]:
- Added Marc's Acked-by, Reviewed-by tags
- Fixed locking bug in ready_percpu_nmi()
- Renamed ready_percpu_nmi() to prepare_percpu_nmi()
- Added documentation prepare_percpu_nmi() and teardown_percpu_nmi()
Changes since v4[2]:
- Rebased on v5.0-rc4
- Modified comment for request_percpu_nmi() to detail the use of per CPU NMIs a bit more
Changes since v3[3]:
- Rebased on v4.20-rc2
Changes since v2[4]:
- Fix ARM build error reported by kbuild bot
- Fix documentation warnings reported by kbuild bot
Changes since v1[5]:
- Rebased to v4.19-rc1
- Fix some coding style issues
Changes since RFC[6]:
- Rebased to v4.18-rc6
- Remove disable_nmi, one should call disable_nmi_nosync instead
- Remove code related to affinity balancing from NMI functions
- Require PERCPU configuration for NMIs
[1] https://lkml.org/lkml/2019/1/28/541
[2] https://lkml.org/lkml/2018/11/12/2113
[3] https://lkml.org/lkml/2018/8/31/173
[4] https://lkml.org/lkml/2018/8/28/661
[5] https://lkml.org/lkml/2018/7/24/321
[6] https://lkml.org/lkml/2018/7/9/768
Cheers,
Julien
-->
Julien Thierry (4):
genirq: Provide basic NMI management for interrupt lines
genirq: Provide NMI management for percpu_devid interrupts
genirq: Provide NMI handlers
irqdesc: Add domain handler for NMIs
include/linux/interrupt.h | 18 +++ include/linux/irq.h | 10 ++ include/linux/irqdesc.h | 5 + kernel/irq/chip.c | 54 +++++++ kernel/irq/debugfs.c | 6 +- kernel/irq/internals.h | 2 + kernel/irq/irqdesc.c | 35 ++++ kernel/irq/manage.c | 405 +++++++++++++++++++++++++++++++++++++++++++++- 8 files changed, 532 insertions(+), 3 deletions(-)
--
1.9.1
PATCH v6 1/4 genirq: Provide basic NMI management for interrupt lines
Add functionality to allocate interrupt lines that will deliver IRQs as Non-Maskable Interrupts. These allocations are only successful if the irqchip provides the necessary support and allows NMI delivery for the interrupt line.
Interrupt lines allocated for NMI delivery must be enabled/disabled through enable_nmi/disable_nmi_nosync to keep their state consistent.
To treat a PERCPU IRQ as NMI, the interrupt must not be shared nor threaded, the irqchip directly managing the IRQ must be the root irqchip and the irqchip cannot be behind a slow bus.
--- include/linux/interrupt.h | 9 ++ include/linux/irq.h | 7 ++ kernel/irq/debugfs.c | 6 +- kernel/irq/internals.h | 2 + kernel/irq/manage.c | 228 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 249 insertions(+), 3 deletions(-)
include/linux/interrupt.h +9 -0
@@ -156,6 +156,10 @@ struct irqaction { unsigned long flags, const char *devname, void __percpu *percpu_dev_id); +extern int __must_check +request_nmi (unsigned int irq,irq_handler_t handler,unsigned long flags, +const char *name,void *dev); +static inline int __must_check request_percpu_irq (unsigned int irq,irq_handler_t handler,const char *devname,void __percpu *percpu_dev_id)
@@ -167,6 +171,8 @@ struct irqaction {extern const void *free_irq (unsigned int ,void *);extern void free_percpu_irq (unsigned int ,void __percpu *); +extern const void *free_nmi (unsigned int irq,void *dev_id); +struct device ; extern int __must_check
@@ -217,6 +223,9 @@ struct irqaction {extern bool irq_percpu_is_enabled (unsigned int irq);extern void irq_wake_thread (unsigned int irq,void *dev_id); +extern void disable_nmi_nosync (unsigned int irq); +extern void enable_nmi (unsigned int irq); +/* The following three functions are for the core kernel use only. */ extern void suspend_device_irqs (void );extern void resume_device_irqs (void );
include/linux/irq.h +7 -0
@@ -442,6 +442,8 @@ static inline irq_hw_number_t irqd_to_hwirq(struct irq_data *d)* @irq_set_vcpu_affinity: optional to target a vCPU in a virtual machine * @ipi_send_single: send a single IPI to destination cpus * @ipi_send_mask: send an IPI to destination cpus in cpumask +* @irq_nmi_setup: function called from core code before enabling an NMI +* @irq_nmi_teardown: function called from core code after disabling an NMI * @flags: chip specific flags */ struct irq_chip {
@@ -490,6 +492,9 @@ struct irq_chip {void (*ipi_send_single)(struct irq_data *data,unsigned int cpu);void (*ipi_send_mask)(struct irq_data *data,const struct cpumask *dest); +int (*irq_nmi_setup)(struct irq_data *data); +void (*irq_nmi_teardown)(struct irq_data *data); +unsigned long flags; };
@@ -505,6 +510,7 @@ struct irq_chip {* IRQCHIP_ONESHOT_SAFE: One shot does not require mask/unmask * IRQCHIP_EOI_THREADED: Chip requires eoi() on unmask in threaded mode * IRQCHIP_SUPPORTS_LEVEL_MSI Chip can provide two doorbells for Level MSIs +* IRQCHIP_SUPPORTS_NMI: Chip can deliver NMIs, only for root irqchips */ enum {IRQCHIP_SET_TYPE_MASKED = (1 <<0 ),
@@ -515,6 +521,7 @@ enum {IRQCHIP_ONESHOT_SAFE = (1 <<5 ),IRQCHIP_EOI_THREADED = (1 <<6 ),IRQCHIP_SUPPORTS_LEVEL_MSI = (1 <<7 ), + IRQCHIP_SUPPORTS_NMI = (1 << 8), };#include <linux/irqdesc.h>
kernel/irq/debugfs.c +4 -2
@@ -56,6 +56,7 @@ static void irq_debug_show_masks(struct seq_file *m, struct irq_desc *desc) { }BIT_MASK_DESCR (IRQCHIP_ONESHOT_SAFE ),BIT_MASK_DESCR (IRQCHIP_EOI_THREADED ),BIT_MASK_DESCR (IRQCHIP_SUPPORTS_LEVEL_MSI ), + BIT_MASK_DESCR(IRQCHIP_SUPPORTS_NMI), }; static void
@@ -140,6 +141,7 @@ static void irq_debug_show_masks(struct seq_file *m, struct irq_desc *desc) { }BIT_MASK_DESCR (IRQS_WAITING ),BIT_MASK_DESCR (IRQS_PENDING ),BIT_MASK_DESCR (IRQS_SUSPENDED ), + BIT_MASK_DESCR(IRQS_NMI), };
@@ -203,8 +205,8 @@ static ssize_t irq_debug_write(struct file *file, const char __user *user_buf,chip_bus_lock (desc);raw_spin_lock_irqsave (&desc->lock, flags); -if (irq_settings_is_level (desc)) { -/* Can't do level, sorry */ +if (irq_settings_is_level (desc) || desc->istate &IRQS_NMI ) { +/* Can't do level nor NMIs, sorry */ err = -EINVAL ; }else { desc->istate |=IRQS_PENDING ;
kernel/irq/internals.h +2 -0
@@ -49,6 +49,7 @@ enum {* IRQS_WAITING - irq is waiting * IRQS_PENDING - irq is pending and replayed later * IRQS_SUSPENDED - irq is suspended +* IRQS_NMI - irq line is used to deliver NMIs */ enum {IRQS_AUTODETECT =0x00000001 ,
@@ -60,6 +61,7 @@ enum {IRQS_PENDING =0x00000200 ,IRQS_SUSPENDED =0x00000800 ,IRQS_TIMINGS =0x00001000 , + IRQS_NMI = 0x00002000, };#include "debug.h"
kernel/irq/manage.c +227 -1
285 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v6 2/4 genirq: Provide NMI management for percpu_devid interrupts
In reply to Julien ThierryAdd support for percpu_devid interrupts treated as NMIs.
Percpu_devid NMIs need to be setup/torn down on each CPU they target.
The same restrictions as for global NMIs still apply for percpu_devid NMIs.
--- include/linux/interrupt.h | 9 +++ kernel/irq/manage.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+)
include/linux/interrupt.h +9 -0
@@ -168,10 +168,15 @@ struct irqaction { devname, percpu_dev_id); } +extern int __must_check +request_percpu_nmi (unsigned int irq,irq_handler_t handler, +const char *devname,void __percpu *dev); +extern const void *free_irq (unsigned int ,void *);extern void free_percpu_irq (unsigned int ,void __percpu *);extern const void *free_nmi (unsigned int irq,void *dev_id); +extern void free_percpu_nmi (unsigned int irq,void __percpu *percpu_dev_id);struct device ;
@@ -224,7 +229,11 @@ struct irqaction {extern void irq_wake_thread (unsigned int irq,void *dev_id);extern void disable_nmi_nosync (unsigned int irq); +extern void disable_percpu_nmi (unsigned int irq);extern void enable_nmi (unsigned int irq); +extern void enable_percpu_nmi (unsigned int irq,unsigned int type); +extern int prepare_percpu_nmi (unsigned int irq); +extern void teardown_percpu_nmi (unsigned int irq);/* The following three functions are for the core kernel use only. */ extern void suspend_device_irqs (void );
kernel/irq/manage.c +177 -0
207 lines not on this page · raw message
Signature · 1 line
1.9.1
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v6 2/4] genirq: Provide NMI management for percpu_devid interrupts
Hi Li Wei,
[Adding back Cc that were dropped]
Quoted 30 lines · liwei (GF)
Hi Julien,
On 2019/1/31 22:53, Julien Thierry Wrote:
Quoted 16 lines
Add support for
percpu_devidinterrupts treated as NMIs.
Percpu_devidNMIs need to be setup/torn down on each CPU they target.The same restrictions as for global NMIs still apply for
percpu_devidNMIs.Signed-off-by Julien Thierry <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Marc Zyngier <[email protected]>--- include/linux/interrupt.h | 9 +++ kernel/irq/manage.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+)(SNIP)
Quoted 10 lines
/**
+ *request_percpu_nmi- allocate a percpu interrupt line for NMI delivery
+ * @irq: Interrupt line to allocate
+ * @handler: Function to be called when the IRQ occurs.
+ * @name: An ascii name for the claiming device
+ * @dev_id: A percpu cookie passed back to the handler function
+ *
+ * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
+ * have to be setup on each CPU by callingready_percpu_nmi()before being
Quoted 2 lines
ready_percpu_nmineed to be renamed here.
Thanks for spotting this. I'll send a fix for next.
Quoted 20 lines
Quoted 20 lines
+ * enabled on the same CPU by using
enable_percpu_nmi().
+ *
+ *Dev_idmust be globally unique. It is a per-cpu variable, and
+ * the handler gets called with the interrupted CPU's instance of
+ * that variable.
+ *
+ * Interrupt lines requested for NMI delivering should have auto enabling
+ * setting disabled.
+ *
+ * If the interrupt line cannot be used to deliver NMIs, function
+ * will fail returning a negative value.
+ */kernel/irq/manage.c +8
-0+int request_percpu_nmi (unsigned int irq,irq_handler_t handler, +const char *name,void __percpu *dev_id) +{ +struct irqaction *action; +struct irq_desc *desc; +unsigned long flags; +int retval; +
Quoted 8 lines
kernel/irq/manage.c int __request_percpu_irq(unsigned int irq, irq_handler_t handler, · line 2455
Quoted 5 lines
kernel/irq/manage.c +5
-0+if (!handler) +return -EINVAL ; + + desc =irq_to_desc (irq); +and i have an other question, i found that
kstat_incr_irqs_this_cpu()was omitted inhandle_fasteoi_nmi()andhandle_percpu_devid_fasteoi_nmi(), are there some worrys here? It is a little odd since we can get NMI counters on the x86 machine in/proc/interrputs.
I don't think x86 uses the IRQ framework for NMIs and probably have their own thing to count NMIs.
The issue is that we cannot take the desc lock so I'm not sure it would be safe to increment the desc->tot_count in NMI context (although technically NMI IRQ lines cannot be shared, so I think it should be fine for non-percpu_nmis). The other concern is incrementing the percpu kstat.irqs_sum, which I guess is a definite *no* in an NMI context as we could have interrupted and interrupt trying to increment that same counter.
So we might be able to establish some kstat_incr_nmi to update some of the counters but it might require a bit more reflexion on the matter, so I'd like to avoid adding that as a "quick fix" in next which might end up breaking things.
Thanks,
Signature · 1 line
Julien Thierry
PATCH v6 3/4 genirq: Provide NMI handlers
In reply to Julien ThierryProvide flow handlers that are NMI safe for interrupts and percpu_devid interrupts.
--- include/linux/irq.h | 3 +++ kernel/irq/chip.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)
include/linux/irq.h +3 -0
@@ -601,6 +601,9 @@ static inline int irq_set_parent(int irq, int parent_irq)extern void handle_bad_irq (struct irq_desc *desc);extern void handle_nested_irq (unsigned int irq); +extern void handle_fasteoi_nmi (struct irq_desc *desc); +extern void handle_percpu_devid_fasteoi_nmi (struct irq_desc *desc); +extern int irq_chip_compose_msi_msg (struct irq_data *data,struct msi_msg *msg);extern int irq_chip_pm_get (struct irq_data *data);extern int irq_chip_pm_put (struct irq_data *data);
kernel/irq/chip.c +54 -0
66 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v6 4/4 irqdesc: Add domain handler for NMIs
In reply to Julien ThierryNMI handling code should be executed between calls to nmi_enter and nmi_exit.
Add a separate domain handler to properly setup NMI context when handling an interrupt requested as NMI.
--- include/linux/irqdesc.h | 5 +++++ kernel/irq/irqdesc.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+)
include/linux/irqdesc.h +5 -0
@@ -171,6 +171,11 @@ static inline int handle_domain_irq(struct irq_domain *domain, {return __handle_domain_irq (domain, hwirq, true, regs); } + +#ifdef CONFIG_IRQ_DOMAIN +int handle_domain_nmi (struct irq_domain *domain,unsigned int hwirq, +struct pt_regs *regs); +#endif #endif /* Test to see if a driver has successfully requested an irq */
kernel/irq/irqdesc.c +35 -0
@@ -669,6 +669,41 @@ int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,set_irq_regs (old_regs); return ret; } + +#ifdef CONFIG_IRQ_DOMAIN +/** +* handle_domain_nmi - Invoke the handler for a HW irq belonging to a domain +* @domain: The domain where to perform the lookup +* @hwirq: The HW irq number to convert to a logical one +* @regs: Register file coming from the low-level handling code +* +* Returns: 0 on success, or -EINVAL if conversion has failed +*/ +int handle_domain_nmi (struct irq_domain *domain,unsigned int hwirq, +struct pt_regs *regs) +{ +struct pt_regs *old_regs =set_irq_regs (regs); +unsigned int irq; +int ret =0 ; + +nmi_enter (); + + irq =irq_find_mapping (domain, hwirq); + +/* +* ack_bad_irq is not NMI-safe, just report +* an invalid interrupt. +*/ +if (likely (irq)) +generic_handle_irq (irq); +else + ret = -EINVAL ; + +nmi_exit (); +set_irq_regs (old_regs); +return ret; +} +#endif #endif /* Dynamic interrupt handling */
Signature · 1 line
1.9.1
Quoted 7 lines · Julien Thierry
Hi,
This patch series provides a way for irqchips to define some IRQs as NMIs.
Updating this series as it is needed for the arm64 pseudo-NMI which we are considering to merge (I'll post a new version shortly).
Latest version of the arm64 pseudo-NMI can be found here:
https://lkml.org/lkml/2019/1/31/535
Cheers,
Signature · 1 line
Julien Thierry
[ + Catalin, Will ]
Hi Julien,
Quoted 7 lines · Julien Thierry
Hi,
This patch series provides a way for irqchips to define some IRQs as NMIs.
Updating this series as it is needed for the arm64 pseudo-NMI which we are considering to merge (I'll post a new version shortly).
For what it is worth, and after having talked to Thomas, I've put these 4 patches in a separate irq/generic-nmi branch which I plan to put into -next shortly.
This is a stable branch that can be pulled into the arm64 tree for the rest of the pseudo-NMI series to be taken into the 5.1 merge window.
Thanks a lot for sticking with this!
M.
Signature · 1 line
Jazz is not dead, it just smell funny.