Provide core API for NMIs
cover Provide core API for NMIs
Hi,
This patch series provides a way for irqchips to define some IRQs as NMIs.
For this to be possible, the irqchip must:
- be a root irqchip
- not require bus locking
- have the NMI support flag
Once these conditions are met, interrupt lines can be requested as NMIs. These lines must not be shared and the IRQ handling must not be threaded. Callbacks are added to the irqchip in order to set up (clean up) the necessary resources for NMIs before enabling (before releasing) the corresponding interrupt line.
For reference, see the examples of the support provided by GICv3 [1] and for the request and management of an NMI in the arm_pmu driver [2]. (These are not on the list yet as I'd like the focus of the discussion to be on the API for now)
This work was motivated by a discussion on Ricardo Neri's patch [3]
Patches 1 & 2 provide functions necessary to request and manage NMIs. Patches 3 & 4 provide functions for NMI handling.
[1] http://www.linux-arm.org/git?p=linux-jt.git;a=commitdiff;h=6401387983ce39a60a3cee12c8975dd18ac0b20e
[2] http://www.linux-arm.org/git?p=linux-jt.git;a=commitdiff;h=1e5105197ed20dc0f7061e4918e0fb6a84799908
[3] https://patchwork.kernel.org/patch/10461381/
Thanks,
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 | 19 +++ include/linux/irq.h | 10 ++ include/linux/irqdesc.h | 3 + kernel/irq/chip.c | 54 +++++++ kernel/irq/debugfs.c | 6 +- kernel/irq/internals.h | 1 + kernel/irq/irqdesc.c | 33 ++++ kernel/irq/manage.c | 399 ++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 523 insertions(+), 2 deletions(-)
--
1.9.1
RFC PATCH 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 to keep their state consistent.
To treat an 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 | 10 ++ include/linux/irq.h | 7 ++ kernel/irq/debugfs.c | 6 +- kernel/irq/internals.h | 1 + kernel/irq/manage.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 272 insertions(+), 2 deletions(-)
include/linux/interrupt.h +10 -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,10 @@ 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 disable_nmi (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
@@ -441,6 +441,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 {
@@ -489,6 +491,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; };
@@ -504,6 +509,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 ),
@@ -514,6 +520,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 +1 -0
@@ -60,6 +60,7 @@ enum {IRQS_PENDING =0x00000200 ,IRQS_SUSPENDED =0x00000800 ,IRQS_TIMINGS =0x00001000 , + IRQS_NMI = 0x00002000, };#include "debug.h"
kernel/irq/manage.c +250 -0
301 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 15 lines · Julien Thierry
kernel/irq/manage.c +15
-0+/** +* disable_nmi_nosync - disable an nmi without waiting +* @irq: Interrupt to disable +* +* Disable the selected interrupt line. Disables and enables are +* nested. +* The interrupt to disable must have been requested through request_nmi. +* Unlike disable_nmi(), this function does not ensure existing +* instances of the IRQ handler have completed before returning. +*/ +void disable_nmi_nosync (unsigned int irq) +{ +disable_irq_nosync (irq); +} +
kernel/irq/manage.c bool disable_hardirq(unsigned int irq) · line 577
Quoted 10 lines
kernel/irq/manage.c +10
-0+/** +* disable_nmi - disable an nmi and wait for completion +* @irq: Interrupt to disable +* +* Disable the selected interrupt line. Enables and disables are +* nested. +* The interrupt to disable must have been requested through request_nmi. +* This function waits for any pending IRQ handlers for this interrupt +* to complete before returning. If you use this function while +* holding a resource the IRQ handler may need you will deadlock.
I don't see how that wait would work with NMIs.
kernel/irq/manage.c const void *free_irq(unsigned int irq, void *dev_id) · line 1840
Quoted 11 lines
kernel/irq/manage.c +11
-0+/* This function must be called with desc->lock held */ +static const void *__cleanup_nmi (unsigned int irq,struct irq_desc *desc) +{ +const char *devname =NULL ; + + desc->istate &= ~IRQS_NMI ; + +#ifdef CONFIG_SMP +if (WARN_ON (desc->affinity_notify)) + desc->affinity_notify =NULL ; +#endif
We should not support that affinity notify horror for NMIs at all.
Quoted 7 lines
kernel/irq/manage.c +7
-0+if (!WARN_ON (desc->action ==NULL )) { +irq_pm_remove_action (desc, desc->action); + devname = desc->action->name; +unregister_handler_proc (irq, desc->action); + +kfree (desc->action); + desc->action =NULL ;
kernel/irq/manage.c const void *free_irq(unsigned int irq, void *dev_id) · line 1858
Quoted 10 lines
kernel/irq/manage.c +10
-0+ } + +irq_settings_clr_disable_unlazy (desc); +irq_shutdown (desc); + +#ifdef CONFIG_SMP +/* make sure affinity_hint is cleaned up */ +if (WARN_ON_ONCE (desc->affinity_hint)) + desc->affinity_hint =NULL ; +#endif
Especially not twice :)
Thanks,
tglx
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Hi Thomas,
Thanks for the review.
Quoted 20 lines · Thomas Gleixner
Quoted 19 lines · Julien Thierry
kernel/irq/manage.c +19
-0+/** +* disable_nmi_nosync - disable an nmi without waiting +* @irq: Interrupt to disable +* +* Disable the selected interrupt line. Disables and enables are +* nested. +* The interrupt to disable must have been requested through request_nmi. +* Unlike disable_nmi(), this function does not ensure existing +* instances of the IRQ handler have completed before returning. +*/ +void disable_nmi_nosync (unsigned int irq) +{ +disable_irq_nosync (irq); +} + +/** +* disable_nmi - disable an nmi and wait for completion +* @irq: Interrupt to disable +*
Quoted 9 lines
Quoted 6 lines
+ * Disable the selected interrupt line. Enables and disables are
+ * nested.
+ * The interrupt to disable must have been requested throughrequest_nmi.
+ * This function waits for any pending IRQ handlers for this interrupt
+ * to complete before returning. If you use this function while
+ * holding a resource the IRQ handler may need you will deadlock.I don't see how that wait would work with NMIs.
I'm a bit unsure about this one. In practice NMI handlers shouldn't take forever nor block the system for too long. And if we are able to disable interrupt line used for the NMI I'd think that the sequence:
disable_nmi_interrupt_line(irq); wait_for_remaining_handlers(irq);
should terminate if the NMI handler is not broken (unless I am missing something).
In any case, I don't mind getting rid of the waiting disable for NMIs, I doubt there is a strong need for it. So if the function is likely to be broken I'm happy to get rid of it.
Quoted 5 lines
Quoted 5 lines
kernel/irq/manage.c +5
-0+/* This function must be called with desc->lock held */ +static const void *__cleanup_nmi (unsigned int irq,struct irq_desc *desc) +{ +const char *devname =NULL ; +
Quoted 9 lines
kernel/irq/manage.c const void *free_irq(unsigned int irq, void *dev_id) · line 1840
Quoted 6 lines
kernel/irq/manage.c +6
-0+ desc->istate &= ~IRQS_NMI ; + +#ifdef CONFIG_SMP +if (WARN_ON (desc->affinity_notify)) + desc->affinity_notify =NULL ; +#endif We should not support that affinity notify horror for NMIs at all.
Makes sense, I'll add it to the constraints for NMIs and remove that part.
Quoted 11 lines
Quoted 11 lines
kernel/irq/manage.c +11
-0+if (!WARN_ON (desc->action ==NULL )) { +irq_pm_remove_action (desc, desc->action); + devname = desc->action->name; +unregister_handler_proc (irq, desc->action); + +kfree (desc->action); + desc->action =NULL ; + } + +irq_settings_clr_disable_unlazy (desc); +irq_shutdown (desc);
Quoted 9 lines
kernel/irq/manage.c const void *free_irq(unsigned int irq, void *dev_id) · line 1858
Quoted 6 lines
kernel/irq/manage.c +6
-0+ +#ifdef CONFIG_SMP +/* make sure affinity_hint is cleaned up */ +if (WARN_ON_ONCE (desc->affinity_hint)) + desc->affinity_hint =NULL ; +#endif Especially not twice :)
Oops... I'll remove that as well.
Thanks,
Signature · 1 line
Julien Thierry
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 2 lines · Julien Thierry
I'm a bit unsure about this one. In practice NMI handlers shouldn't take forever nor block the system for too long.
On x86 at least, kgdb can be in NMI context for extended periods. Not saying that's a good thing, but on occasion we get reports on stuff coming apart because of that.
(most times we tell people to not do that then)
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 11 lines · Peter Zijlstra
Quoted 2 lines · Julien Thierry
I'm a bit unsure about this one. In practice NMI handlers shouldn't take forever nor block the system for too long.
On x86 at least, kgdb can be in NMI context for extended periods. Not saying that's a good thing, but on occasion we get reports on stuff coming apart because of that.
(most times we tell people to not do that then)
Thanks for the explanation. I'll remove the function to avoid issues related to those scenario to arise.
Thanks,
Signature · 1 line
Julien Thierry
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
In reply to Julien ThierryQuoted 5 lines · Julien Thierry
Hi Thomas,
Thanks for the review.
On 17/07/18 10:22, Thomas Gleixner wrote:
[...]
Quoted 40 lines
Quoted 7 lines · Julien Thierry
Quoted 6 lines
(quoted diff) +2
-0+/* This function must be called with desc->lock held */ +static const void *__cleanup_nmi(unsigned int irq, struct irq_desc*desc)
kernel/irq/manage.c +3
-0+{ + const char *devname = NULL; +Quoted 9 lines
kernel/irq/manage.c const void *free_irq(unsigned int irq, void *dev_id) · line 1840
Quoted 6 lines
+
desc->istate&= ~IRQS_NMI;kernel/irq/manage.c +5
-0+ +#ifdef CONFIG_SMP + if (WARN_ON(desc->affinity_notify)) + desc->affinity_notify = NULL; +#endif We should not support that affinity notify horror for NMIs at all.
Makes sense, I'll add it to the constraints for NMIs and remove that part.
Quoted 11 lines
Quoted 11 lines
+ if (!WARN_ON(desc->action == NULL)) {
+irq_pm_remove_action(desc, desc->action); + devname =desc->action->name;
+unregister_handler_proc(irq, desc->action);kernel/irq/manage.c +7
-0+ + kfree(desc->action); + desc->action = NULL; + } + + irq_settings_clr_disable_unlazy(desc); + irq_shutdown(desc);Quoted 9 lines
kernel/irq/manage.c const void *free_irq(unsigned int irq, void *dev_id) · line 1858
Quoted 6 lines
kernel/irq/manage.c +6
-0+ +#ifdef CONFIG_SMP + /* make sure affinity_hint is cleaned up */ + if (WARN_ON_ONCE(desc->affinity_hint)) + desc->affinity_hint = NULL; +#endif Especially not twice :)
Quoted 1 line
Oops... I'll remove that as well.
Re-reading this I realized that second part was about desc->affinity_hint and not desc->affinity_notify.
Supporting desc->affinity_hint for NMIs shouldn't be an issue, right?
Thanks,
Signature · 1 line
Julien Thierry
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 1 line · Julien Thierry
Supporting
desc->affinity_hintfor NMIs shouldn't be an issue, right?
Shouldn't NMIs be strictly per cpu interrupt sources?
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 4 lines · Peter Zijlstra
Quoted 1 line · Julien Thierry
Supporting
desc->affinity_hintfor NMIs shouldn't be an issue, right?Shouldn't NMIs be strictly per cpu interrupt sources?
That's a good question, and I don't have a proper answer right now...
However, per cpu or not, the affinity still needs to be set for that interrupt. It seemed to me that the desc->affinity_hint was just a way to keep track of the interrupt affinity when it gets set through irq_set_affinity_hint.
Is that not the case?
Thanks,
Signature · 1 line
Julien Thierry
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 6 lines · Julien Thierry
Quoted 4 lines · Peter Zijlstra
Quoted 1 line · Julien Thierry
Supporting
desc->affinity_hintfor NMIs shouldn't be an issue, right?Shouldn't NMIs be strictly per cpu interrupt sources?
Quoted 8 lines
That's a good question, and I don't have a proper answer right now...
However, per cpu or not, the affinity still needs to be set for that interrupt. It seemed to me that the
desc->affinity_hintwas just a way to keep track of the interrupt affinity when it gets set throughirq_set_affinity_hint.Is that not the case?
No the affinity hint is a way to tell user space irqbalanced what the driver thinks is the best placement. Affinity settings are done by the kernel internal interfaces and those do not care about the hint at all.
Thanks,
tglx
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
Quoted 14 lines · Thomas Gleixner
Quoted 13 lines · Julien Thierry
Quoted 4 lines · Peter Zijlstra
Quoted 1 line · Julien Thierry
Supporting
desc->affinity_hintfor NMIs shouldn't be an issue, right?Shouldn't NMIs be strictly per cpu interrupt sources?
That's a good question, and I don't have a proper answer right now...
However, per cpu or not, the affinity still needs to be set for that interrupt. It seemed to me that the
desc->affinity_hintwas just a way to keep track of the interrupt affinity when it gets set throughirq_set_affinity_hint.
Quoted 6 lines
Quoted 1 line
Is that not the case?
No the affinity hint is a way to tell user space irqbalanced what the driver thinks is the best placement. Affinity settings are done by the kernel internal interfaces and those do not care about the hint at all.
Oh, I see. Thanks for explaining
I already do set the IRQF_NOBALANCING for NMIs, so fiddling with that should be already limited.
I'll consider setting the IRQF_PERCPU (or mandating it for the NMI requests).
Thanks,
Signature · 1 line
Julien Thierry
Re: [RFC PATCH 1/4] genirq: Provide basic NMI management for interrupt lines
In reply to Peter ZijlstraQuoted 4 lines · Peter Zijlstra
Quoted 1 line · Julien Thierry
Supporting
desc->affinity_hintfor NMIs shouldn't be an issue, right?Shouldn't NMIs be strictly per cpu interrupt sources?
Right. They are surely not subject to be fiddled with by irqbalanced or random admin affinity settings.
Thanks,
tglx
RFC PATCH 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 | 149 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 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 ;
@@ -225,7 +230,11 @@ struct irqaction {extern void disable_nmi_nosync (unsigned int irq);extern void disable_nmi (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 ready_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 +149 -0
179 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC PATCH 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
@@ -600,6 +600,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
RFC PATCH 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 | 3 +++ kernel/irq/irqdesc.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/irqdesc.h +3 -0
@@ -171,6 +171,9 @@ static inline int handle_domain_irq(struct irq_domain *domain, {return __handle_domain_irq (domain, hwirq, true, regs); } + +int handle_domain_nmi (struct irq_domain *domain,unsigned int hwirq, +struct pt_regs *regs);#endif /* Test to see if a driver has successfully requested an irq */
kernel/irq/irqdesc.c +33 -0
@@ -664,6 +664,39 @@ int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,set_irq_regs (old_regs); return ret; } + +/** +* 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 /* Dynamic interrupt handling */
Signature · 1 line
1.9.1