Runtime IRQ isolation has to distinguish an automatically placed interrupt from one which was pinned explicitly: only the former may be redirected to other housekeeping CPUs when its requested targets become isolated. Record the provenance in a private descriptor state bit, IRQS_AFFINITY_AUTO, in desc->istate next to IRQS_SYSFS. istate is genirq-private, so the public IRQ_* space in include/linux/irq.h and the status_use_accessors word stay untouched, irq_modify_status() cannot reach the bit, and it survives free_irq()/request_irq(). The bit is set or cleared by the descriptor setup, by the managed affinity descriptor update and by irq_setup_affinity(). In-kernel irq_set_affinity_and_hint() describes automatic placement: drivers use it to bind a queue, not to exclude other CPUs. A bare irq_set_affinity() is an explicit pin, the safe default for a per-CPU PMU or an architecture timer which needs the interrupt on one CPU. Core code which manages an affinity on a driver's behalf marks it automatic with irq_set_affinity_auto(); the helper is not exported, so a module cannot opt in by accident. A /proc/irq/N/smp_affinity write, irq_force_affinity() and a bare irq_set_affinity() clear the bit; subsequent filtering does not redirect them to unrelated housekeeping CPUs, so only their cross-mask fallback is lost. No behaviour change: the bit is recorded, not read. Signed-off-by: Kris Pan <kris…@intel.com> --- include/linux/interrupt.h | 8 ++++ kernel/irq/debugfs.c | 1 + kernel/irq/internals.h | 17 ++++++++ kernel/irq/irqdesc.c | 9 +++++ kernel/irq/manage.c | 85 +++++++++++++++++++++++++++++++++++---- 5 files changed, 112 insertions(+), 8 deletions(-) diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h index ee2dd5349697d..58ec0acf82772 100644 --- a/include/linux/interrupt.h +++ b/include/linux/interrupt.h @@ -318,6 +318,8 @@ struct irq_affinity_desc { extern cpumask_var_t irq_default_affinity; extern int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask); +/* Core-internal only; drivers must use irq_set_affinity_and_hint() */ +int irq_set_affinity_auto(unsigned int irq, const struct cpumask *cpumask); extern int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask); extern int irq_can_set_affinity(unsigned int irq); @@ -382,6 +384,12 @@ static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m) return -EINVAL; } +static inline int irq_set_affinity_auto(unsigned int irq, + const struct cpumask *m) +{ + return -EINVAL; +} + static inline int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask) { return 0; diff --git a/kernel/irq/debugfs.c b/kernel/irq/debugfs.c index 3bdc5a388ea5f..515c01754f1f0 100644 --- a/kernel/irq/debugfs.c +++ b/kernel/irq/debugfs.c @@ -154,6 +154,7 @@ static const struct irq_bit_descr irqdesc_istates[] = { BIT_MASK_DESCR(IRQS_PENDING), BIT_MASK_DESCR(IRQS_SUSPENDED), BIT_MASK_DESCR(IRQS_NMI), + BIT_MASK_DESCR(IRQS_AFFINITY_AUTO), }; diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h index 703a062be8783..06cfa77409022 100644 --- a/kernel/irq/internals.h +++ b/kernel/irq/internals.h @@ -64,6 +64,7 @@ enum { * IRQS_SUSPENDED - irq is suspended * IRQS_NMI - irq line is used to deliver NMIs * IRQS_SYSFS - descriptor has been added to sysfs + * IRQS_AFFINITY_AUTO - affinity placement was chosen automatically */ enum { IRQS_AUTODETECT = 0x00000001, @@ -77,8 +78,24 @@ enum { IRQS_TIMINGS = 0x00001000, IRQS_NMI = 0x00002000, IRQS_SYSFS = 0x00004000, + IRQS_AFFINITY_AUTO = 0x00008000, }; +static inline bool irq_affinity_is_auto(struct irq_desc *desc) +{ + return desc->istate & IRQS_AFFINITY_AUTO; +} + +static inline void irq_affinity_set_auto(struct irq_desc *desc) +{ + desc->istate |= IRQS_AFFINITY_AUTO; +} + +static inline void irq_affinity_clr_auto(struct irq_desc *desc) +{ + desc->istate &= ~IRQS_AFFINITY_AUTO; +} + #include "debug.h" #include "settings.h" diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c index 7cbcc3fb9e6d4..4f578f8d497fd 100644 --- a/kernel/irq/irqdesc.c +++ b/kernel/irq/irqdesc.c @@ -138,6 +138,7 @@ static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node, irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS); irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED); irqd_set(&desc->irq_data, IRQD_IRQ_MASKED); + irq_affinity_set_auto(desc); desc->handle_irq = handle_bad_irq; desc->depth = 1; desc->irq_count = 0; @@ -519,6 +520,7 @@ static int alloc_descs(unsigned int start, unsigned int cnt, int node, } for (i = 0; i < cnt; i++) { + bool managed = affinity && affinity->is_managed; const struct cpumask *mask = NULL; unsigned int flags = 0; @@ -536,6 +538,13 @@ static int alloc_descs(unsigned int start, unsigned int cnt, int node, desc = alloc_desc(start + i, node, flags, mask, owner); if (!desc) goto err; + /* + * A managed interrupt is placed by the block layer and does + * not take part in the runtime isolation filter. It must + * not be reported as automatically placed. + */ + if (managed) + irq_affinity_clr_auto(desc); irq_insert_desc(start + i, desc); irq_sysfs_add(start + i, desc); irq_add_debugfs_entry(start + i, desc); diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c index ec3ecb0e48cec..4f5c776e8c5de 100644 --- a/kernel/irq/manage.c +++ b/kernel/irq/manage.c @@ -452,6 +452,10 @@ int irq_update_affinity_desc(unsigned int irq, struct irq_affinity_desc *affinit } cpumask_copy(desc->irq_common_data.affinity, &affinity->mask); + if (!affinity->is_managed) + irq_affinity_set_auto(desc); + else + irq_affinity_clr_auto(desc); /* Restore the activation state */ if (activated) @@ -461,16 +465,40 @@ int irq_update_affinity_desc(unsigned int irq, struct irq_affinity_desc *affinit return -EINVAL; } +enum irq_affinity_origin { + IRQ_AFFINITY_SET_AUTO, + IRQ_AFFINITY_CLEAR_AUTO, +}; + static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, - bool force) + bool force, enum irq_affinity_origin origin) { struct irq_desc *desc = irq_to_desc(irq); + bool old_auto; + int ret; if (!desc) return -EINVAL; guard(raw_spinlock_irqsave)(&desc->lock); - return irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force); + old_auto = irq_affinity_is_auto(desc); + switch (origin) { + case IRQ_AFFINITY_SET_AUTO: + irq_affinity_set_auto(desc); + break; + case IRQ_AFFINITY_CLEAR_AUTO: + irq_affinity_clr_auto(desc); + break; + } + + ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force); + if (ret) { + if (old_auto) + irq_affinity_set_auto(desc); + else + irq_affinity_clr_auto(desc); + } + return ret; } /** @@ -479,13 +507,38 @@ static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, * @cpumask: cpumask * * Fails if cpumask does not contain an online CPU + * + * The affinity is recorded as an explicit request and is not redirected + * by runtime IRQ isolation. This is the safe default for a driver which + * needs the interrupt on a specific CPU, for example a per-CPU PMU or an + * architecture timer. A driver which binds a queue for load spreading + * uses irq_set_affinity_and_hint() instead, which keeps the placement + * automatic. */ int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask) { - return __irq_set_affinity(irq, cpumask, false); + return __irq_set_affinity(irq, cpumask, false, IRQ_AFFINITY_CLEAR_AUTO); } EXPORT_SYMBOL_GPL(irq_set_affinity); +/** + * irq_set_affinity_auto - Program an automatically placed irq affinity + * @irq: Interrupt to set affinity + * @cpumask: cpumask + * + * Same as irq_set_affinity(), but records the request as automatic + * placement, so runtime IRQ isolation may move the interrupt off a CPU + * which later becomes isolated. + * + * Not exported: it is for core code which manages a device's affinity on + * the driver's behalf, not for drivers themselves. A driver which wants + * automatic placement uses irq_set_affinity_and_hint(). + */ +int irq_set_affinity_auto(unsigned int irq, const struct cpumask *cpumask) +{ + return __irq_set_affinity(irq, cpumask, false, IRQ_AFFINITY_SET_AUTO); +} + /** * irq_force_affinity - Force the irq affinity of a given irq * @irq: Interrupt to set affinity @@ -499,11 +552,12 @@ EXPORT_SYMBOL_GPL(irq_set_affinity); */ int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask) { - return __irq_set_affinity(irq, cpumask, true); + return __irq_set_affinity(irq, cpumask, true, IRQ_AFFINITY_CLEAR_AUTO); } EXPORT_SYMBOL_GPL(irq_force_affinity); -int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool setaffinity) +int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, + bool setaffinity) { int ret = -EINVAL; @@ -512,8 +566,8 @@ int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool se ret = 0; } - if (!ret && m && setaffinity) - __irq_set_affinity(irq, m, false); + if (!ret && setaffinity && m) + __irq_set_affinity(irq, m, false, IRQ_AFFINITY_SET_AUTO); return ret; } EXPORT_SYMBOL_GPL(__irq_apply_affinity_hint); @@ -595,6 +649,8 @@ int irq_setup_affinity(struct irq_desc *desc) { struct cpumask *set = irq_default_affinity; int node = irq_desc_get_node(desc); + bool old_auto, set_auto; + int ret; static DEFINE_RAW_SPINLOCK(mask_lock); static struct cpumask mask; @@ -628,7 +684,20 @@ int irq_setup_affinity(struct irq_desc *desc) if (cpumask_intersects(&mask, nodemask)) cpumask_and(&mask, &mask, nodemask); } - return irq_do_set_affinity(&desc->irq_data, &mask, false); + /* + * Read IRQD_AFFINITY_SET after the block above: it may have been + * cleared because the requested targets are offline, in which case + * this placement is automatic again. + */ + old_auto = irq_affinity_is_auto(desc); + set_auto = !irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET) && + !irqd_affinity_is_managed(&desc->irq_data); + if (set_auto) + irq_affinity_set_auto(desc); + ret = irq_do_set_affinity(&desc->irq_data, &mask, false); + if (ret && set_auto && !old_auto) + irq_affinity_clr_auto(desc); + return ret; } #else /* Wrapper for ALPHA specific affinity selector magic */
-- 2.43.0