2016-12-14 18:01:41

by Guilherme G. Piccoli

[permalink] [raw]
Subject: [PATCH] genirq/affinity: fix node generation from cpumask

Commit 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading
infrastructure") introduced a better IRQ spreading mechanism, taking
account of the available NUMA nodes in the machine.

Problem is that the algorithm of retrieving the nodemask iterates
"linearly" based on the number of online nodes - some architectures
present non-linear node distribution among the nodemask, like PowerPC.
If this is the case, the algorithm lead to a wrong node count number
and therefore to a bad/incomplete IRQ affinity distribution.

For example, this problem were found in a machine with 128 CPUs and two
nodes, namely nodes 0 and 8 (instead of 0 and 1, if it was linearly
distributed). This led to a wrong affinity distribution which then led to
a bad mq allocation for nvme driver.

Finally, we take the opportunity to fix a comment regarding the affinity
distribution when we have _more_ nodes than vectors.

Fixes: 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading infrastructure")
Reported-by: Gabriel Krisman Bertazi <[email protected]>
Signed-off-by: Guilherme G. Piccoli <[email protected]>
Cc: [email protected] # v4.9+
Cc: Christoph Hellwig <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
kernel/irq/affinity.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/irq/affinity.c b/kernel/irq/affinity.c
index 9be9bda..464eaf0 100644
--- a/kernel/irq/affinity.c
+++ b/kernel/irq/affinity.c
@@ -37,15 +37,15 @@ static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,

static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
{
- int n, nodes;
+ int n, nodes = 0;

/* Calculate the number of nodes in the supplied affinity mask */
- for (n = 0, nodes = 0; n < num_online_nodes(); n++) {
+ for_each_online_node(n)
if (cpumask_intersects(mask, cpumask_of_node(n))) {
node_set(n, *nodemsk);
nodes++;
}
- }
+
return nodes;
}

@@ -82,7 +82,7 @@ irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);

/*
- * If the number of nodes in the mask is less than or equal the
+ * If the number of nodes in the mask is greater than or equal the
* number of vectors we just spread the vectors across the nodes.
*/
if (affv <= nodes) {
--
2.1.0


2016-12-14 23:24:38

by Gavin Shan

[permalink] [raw]
Subject: Re: [PATCH] genirq/affinity: fix node generation from cpumask

On Wed, Dec 14, 2016 at 04:01:12PM -0200, Guilherme G. Piccoli wrote:
>Commit 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading
>infrastructure") introduced a better IRQ spreading mechanism, taking
>account of the available NUMA nodes in the machine.
>
>Problem is that the algorithm of retrieving the nodemask iterates
>"linearly" based on the number of online nodes - some architectures
>present non-linear node distribution among the nodemask, like PowerPC.
>If this is the case, the algorithm lead to a wrong node count number
>and therefore to a bad/incomplete IRQ affinity distribution.
>
>For example, this problem were found in a machine with 128 CPUs and two
>nodes, namely nodes 0 and 8 (instead of 0 and 1, if it was linearly
>distributed). This led to a wrong affinity distribution which then led to
>a bad mq allocation for nvme driver.
>
>Finally, we take the opportunity to fix a comment regarding the affinity
>distribution when we have _more_ nodes than vectors.
>
>Fixes: 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading infrastructure")
>Reported-by: Gabriel Krisman Bertazi <[email protected]>
>Signed-off-by: Guilherme G. Piccoli <[email protected]>
>Cc: [email protected] # v4.9+
>Cc: Christoph Hellwig <[email protected]>
>Cc: [email protected]
>Cc: [email protected]
>---

Reviewed-by: Gavin Shan <[email protected]>

There is one picky comment as below, but you don't have to fix it :)

> kernel/irq/affinity.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
>diff --git a/kernel/irq/affinity.c b/kernel/irq/affinity.c
>index 9be9bda..464eaf0 100644
>--- a/kernel/irq/affinity.c
>+++ b/kernel/irq/affinity.c
>@@ -37,15 +37,15 @@ static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
>
> static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
> {
>- int n, nodes;
>+ int n, nodes = 0;
>
> /* Calculate the number of nodes in the supplied affinity mask */
>- for (n = 0, nodes = 0; n < num_online_nodes(); n++) {
>+ for_each_online_node(n)
> if (cpumask_intersects(mask, cpumask_of_node(n))) {
> node_set(n, *nodemsk);
> nodes++;
> }
>- }
>+

It'd better to keep the brackets so that we needn't add them when adding
more code into the block next time.

> return nodes;
> }
>
>@@ -82,7 +82,7 @@ irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
> nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);
>
> /*
>- * If the number of nodes in the mask is less than or equal the
>+ * If the number of nodes in the mask is greater than or equal the
> * number of vectors we just spread the vectors across the nodes.
> */
> if (affv <= nodes) {

Thanks,
Gavin

2016-12-15 01:44:20

by Gabriel Krisman Bertazi

[permalink] [raw]
Subject: Re: [PATCH] genirq/affinity: fix node generation from cpumask

"Guilherme G. Piccoli" <[email protected]> writes:

> Commit 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading
> infrastructure") introduced a better IRQ spreading mechanism, taking
> account of the available NUMA nodes in the machine.
>
> Problem is that the algorithm of retrieving the nodemask iterates
> "linearly" based on the number of online nodes - some architectures
> present non-linear node distribution among the nodemask, like PowerPC.
> If this is the case, the algorithm lead to a wrong node count number
> and therefore to a bad/incomplete IRQ affinity distribution.
>
> For example, this problem were found in a machine with 128 CPUs and two
> nodes, namely nodes 0 and 8 (instead of 0 and 1, if it was linearly
> distributed). This led to a wrong affinity distribution which then led to
> a bad mq allocation for nvme driver.
>
> Finally, we take the opportunity to fix a comment regarding the affinity
> distribution when we have _more_ nodes than vectors.

Thanks for taking care of this so quickly, Guilherme.

Reviewed-by: Gabriel Krisman Bertazi <[email protected]>

--
Gabriel Krisman Bertazi

2016-12-15 08:55:01

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] genirq/affinity: fix node generation from cpumask

Looks fine:

Reviewed-by: Christoph Hellwig <[email protected]>

(but I agree with the bracing nitpick from Gavin)

2016-12-15 09:39:39

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [PATCH] genirq/affinity: fix node generation from cpumask

On Thu, 15 Dec 2016, Gavin Shan wrote:
> > static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
> > {
> >- int n, nodes;
> >+ int n, nodes = 0;
> >
> > /* Calculate the number of nodes in the supplied affinity mask */
> >- for (n = 0, nodes = 0; n < num_online_nodes(); n++) {
> >+ for_each_online_node(n)
> > if (cpumask_intersects(mask, cpumask_of_node(n))) {
> > node_set(n, *nodemsk);
> > nodes++;
> > }
> >- }
> >+
>
> It'd better to keep the brackets so that we needn't add them when adding
> more code into the block next time.

Removing the brackets is outright wrong. See:
https://marc.info/?l=linux-kernel&m=147351236615103

I'll fix that up when applying the patch.

Thanks,

tglx

Subject: [tip:irq/urgent] genirq/affinity: Fix node generation from cpumask

Commit-ID: c0af52437254fda8b0cdbaae5a9b6d9327f1fcd5
Gitweb: http://git.kernel.org/tip/c0af52437254fda8b0cdbaae5a9b6d9327f1fcd5
Author: Guilherme G. Piccoli <[email protected]>
AuthorDate: Wed, 14 Dec 2016 16:01:12 -0200
Committer: Thomas Gleixner <[email protected]>
CommitDate: Thu, 15 Dec 2016 12:32:35 +0100

genirq/affinity: Fix node generation from cpumask

Commit 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading
infrastructure") introduced a better IRQ spreading mechanism, taking
account of the available NUMA nodes in the machine.

Problem is that the algorithm of retrieving the nodemask iterates
"linearly" based on the number of online nodes - some architectures
present non-linear node distribution among the nodemask, like PowerPC.
If this is the case, the algorithm lead to a wrong node count number
and therefore to a bad/incomplete IRQ affinity distribution.

For example, this problem were found in a machine with 128 CPUs and two
nodes, namely nodes 0 and 8 (instead of 0 and 1, if it was linearly
distributed). This led to a wrong affinity distribution which then led to
a bad mq allocation for nvme driver.

Finally, we take the opportunity to fix a comment regarding the affinity
distribution when we have _more_ nodes than vectors.

Fixes: 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading infrastructure")
Reported-by: Gabriel Krisman Bertazi <[email protected]>
Signed-off-by: Guilherme G. Piccoli <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Reviewed-by: Gabriel Krisman Bertazi <[email protected]>
Reviewed-by: Gavin Shan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Thomas Gleixner <[email protected]>

---
kernel/irq/affinity.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/irq/affinity.c b/kernel/irq/affinity.c
index 9be9bda..4544b11 100644
--- a/kernel/irq/affinity.c
+++ b/kernel/irq/affinity.c
@@ -37,10 +37,10 @@ static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,

static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
{
- int n, nodes;
+ int n, nodes = 0;

/* Calculate the number of nodes in the supplied affinity mask */
- for (n = 0, nodes = 0; n < num_online_nodes(); n++) {
+ for_each_online_node(n) {
if (cpumask_intersects(mask, cpumask_of_node(n))) {
node_set(n, *nodemsk);
nodes++;
@@ -82,7 +82,7 @@ irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);

/*
- * If the number of nodes in the mask is less than or equal the
+ * If the number of nodes in the mask is greater than or equal the
* number of vectors we just spread the vectors across the nodes.
*/
if (affv <= nodes) {

2016-12-15 12:34:37

by Balbir Singh

[permalink] [raw]
Subject: Re: [PATCH] genirq/affinity: fix node generation from cpumask



On 15/12/16 05:01, Guilherme G. Piccoli wrote:
> Commit 34c3d9819fda ("genirq/affinity: Provide smarter irq spreading
> infrastructure") introduced a better IRQ spreading mechanism, taking
> account of the available NUMA nodes in the machine.
>
> Problem is that the algorithm of retrieving the nodemask iterates
> "linearly" based on the number of online nodes - some architectures
> present non-linear node distribution among the nodemask, like PowerPC.
> If this is the case, the algorithm lead to a wrong node count number
> and therefore to a bad/incomplete IRQ affinity distribution.
>
> For example, this problem were found in a machine with 128 CPUs and two
> nodes, namely nodes 0 and 8 (instead of 0 and 1, if it was linearly
> distributed). This led to a wrong affinity distribution which then led to
> a bad mq allocation for nvme driver.
>
> Finally, we take the opportunity to fix a comment regarding the affinity
> distribution when we have _more_ nodes than vectors.

Very good catch!

Acked-by: Balbir Singh <[email protected]>

2016-12-15 13:57:24

by Guilherme G. Piccoli

[permalink] [raw]
Subject: Re: [PATCH] genirq/affinity: fix node generation from cpumask

On 12/15/2016 07:36 AM, Thomas Gleixner wrote:
> On Thu, 15 Dec 2016, Gavin Shan wrote:
>>> static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
>>> {
>>> - int n, nodes;
>>> + int n, nodes = 0;
>>>
>>> /* Calculate the number of nodes in the supplied affinity mask */
>>> - for (n = 0, nodes = 0; n < num_online_nodes(); n++) {
>>> + for_each_online_node(n)
>>> if (cpumask_intersects(mask, cpumask_of_node(n))) {
>>> node_set(n, *nodemsk);
>>> nodes++;
>>> }
>>> - }
>>> +
>>
>> It'd better to keep the brackets so that we needn't add them when adding
>> more code into the block next time.
>
> Removing the brackets is outright wrong. See:
> https://marc.info/?l=linux-kernel&m=147351236615103
>
> I'll fix that up when applying the patch.
>
> Thanks,
>
> tglx
>

Thanks you all very much for the reviews and comments - lesson learned
about the brackets in multi-line if/for statements!

Thanks for fixing it Thomas.
Cheers,


Guilherme