Many drivers call cpumask_local_spread() to spread IRQs to several CPUs, mainly to get best performance and balance CPU load. For realtime (PREEMPT_RT) and other cpu-isolating workloads the old implementation was triggering an IRQ placement problem. IRQs were targeting CPUs that were isolated for those sensitive workloads. Userland will tell the kernel about the desired IRQ configuration for new interrupts by writing a proper cpumask to /proc/irq/default_smp_affinity. This cpu mask has to be honored to avoid IRQ noise on isolated cores. The default for irq_default_affinity is "all CPUs". So all CPUs will be taken into account for spreading when userland did not configure something special. --- lib/cpumask.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/cpumask.c b/lib/cpumask.c index 5adb9874fbd0f5a42ea8cd9e6c3729a70599781f..73e7b60a9201174f83df6067effbe7d1889dcdb9 100644 --- a/lib/cpumask.c +++ b/lib/cpumask.c @@ -6,6 +6,7 @@ #include #include #include +#include /* These are not inline because of header tangles. */ #ifdef CONFIG_CPUMASK_OFFSTACK @@ -81,8 +82,9 @@ void __init free_bootmem_cpumask_var(cpumask_var_t mask) * @i: index number * @node: local numa_node * - * Return: online CPU according to a numa aware policy; local cpus are returned - * first, followed by non-local ones, then it wraps around. + * Return: online CPU according to the default IRQ affinity and a numa aware + * policy; local cpus are returned first, followed by non-local ones, then it + * wraps around. * * For those who wants to enumerate all CPUs based on their NUMA distances, * i.e. call this function in a loop, like: @@ -110,9 +112,9 @@ unsigned int cpumask_local_spread(unsigned int i, int node) unsigned int cpu; /* Wrap: we always want a cpu. */ - i %= num_online_cpus(); + i %= cpumask_weight(irq_default_affinity); - cpu = sched_numa_find_nth_cpu(cpu_online_mask, i, node); + cpu = sched_numa_find_nth_cpu(irq_default_affinity, i, node); WARN_ON(cpu >= nr_cpu_ids); return cpu; -- 2.55.0 Some device drivers implement an IRQ balancing / spreading mechanism based on cpumask_local_spread() and irq_set_affinity_hint() (deprecated) or irq_set_affinity_and_hint(). __irq_apply_affinity_hint() was overwriting already configured affinities and with that violating user defined affinities. This was especially a problem on systems with isolated CPUs, a common pattern on PREEMPT_RT enabled systems. Device IRQs were balanced over CPUs that were isolated for RT workloads. Note that only the first call of irq_set_affinity_hint() and irq_set_affinity_and_hint() - for each IRQ - will have any effect. Afterward affinities might be controlled from userspace using the /proc/irq//smp_affinity{_list} interface. Signed-off-by: Florian Bezdeka --- kernel/irq/manage.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c index 7eb07e3bdb4c2419e0a58a75c29d88b4b00b8287..3937ba24df6d63375dda88164cf92fd49e70a3eb 100644 --- a/kernel/irq/manage.c +++ b/kernel/irq/manage.c @@ -503,11 +503,21 @@ EXPORT_SYMBOL_GPL(irq_force_affinity); int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool setaffinity) { + struct irq_data *irq_data; int ret = -EINVAL; scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { scoped_irqdesc->affinity_hint = m; + irq_data = irq_desc_get_irq_data(scoped_irqdesc); ret = 0; + + /* + * Do not overwrite the affinity in case an affinity for this + * IRQ has already been requested, e.g. by writing to + * /proc/irq//smp_affinity + */ + if (irqd_affinity_was_set(irq_data)) + setaffinity = false; } if (!ret && m && setaffinity) -- 2.55.0 The previous balancing mechanism was based on num_online_cpus(), which is a problem for systems cpu-isolating workloads. IRQs were targeting CPUs that were isolated for those sensitive workloads. With a migration to cpumask_local_spread() we - get NUMA locality - honor the default SMP affinity mask, which avoids targeting isolated CPUs. This also aligns with the pattern used by most network drivers dealing with IRQ affinities / affinity hints. Signed-off-by: Florian Bezdeka --- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c index a71f0df263785dd8badc45292ca3067ab33bda05..949ced7e46d2814b57c6bd86b4886ac3bf33996c 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c @@ -3826,11 +3826,16 @@ static int stmmac_request_irq_multi_msi(struct net_device *dev) struct stmmac_priv *priv = netdev_priv(dev); struct stmmac_msi *msi = priv->msi; enum request_irq_err irq_err; + cpumask_var_t affinity; int irq_idx = 0; char *int_name; + int node; int ret; int i; + if (!zalloc_cpumask_var(&affinity, GFP_KERNEL)) + return -ENOMEM; + /* For common interrupt */ int_name = msi->int_name_mac; sprintf(int_name, "%s:%s", dev->name, "mac"); @@ -3916,6 +3921,7 @@ static int stmmac_request_irq_multi_msi(struct net_device *dev) } /* Request Rx MSI irq */ + node = dev_to_node(&priv->dev->dev); for (i = 0; i < priv->plat->rx_queues_to_use; i++) { if (i >= MTL_MAX_RX_QUEUES) break; @@ -3935,8 +3941,10 @@ static int stmmac_request_irq_multi_msi(struct net_device *dev) irq_idx = i; goto irq_error; } - irq_set_affinity_hint(msi->rx_irq[i], - cpumask_of(i % num_online_cpus())); + + cpumask_clear(affinity); + cpumask_set_cpu(cpumask_local_spread(i, node), affinity); + irq_set_affinity_and_hint(msi->rx_irq[i], affinity); } /* Request Tx MSI irq */ @@ -3959,13 +3967,18 @@ static int stmmac_request_irq_multi_msi(struct net_device *dev) irq_idx = i; goto irq_error; } - irq_set_affinity_hint(msi->tx_irq[i], - cpumask_of(i % num_online_cpus())); + + cpumask_clear(affinity); + cpumask_set_cpu(cpumask_local_spread(i, node), affinity); + irq_set_affinity_and_hint(msi->tx_irq[i], affinity); } + free_cpumask_var(affinity); + return 0; irq_error: + free_cpumask_var(affinity); stmmac_free_irq(dev, irq_err, irq_idx); return ret; } -- 2.55.0