Skip to contentSkip to conversation

powerpc/xive: rework xive_find_target_in_mask()

PATCH Applied Yury Norov 4 participants 10 messages Mar 19 – Apr 8 PowerPC

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

cover powerpc/xive: rework xive_find_target_in_mask()

Simplify the function by switching to using a modern cpumask API.

Yury Norov (2):
Revert "powerpc/xive: Fix the size of the cpumask used in

    xive_find_target_in_mask()"

powerpc/xive: rework xive_find_target_in_mask()

 arch/powerpc/sysdev/xive/common.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 Revert "powerpc/xive: Fix the size of the cpumask used in xive_find_target_in_mask()"

This reverts commit a9dadc1c512807f955f0799e85830b420da47932.

The commit message states:

When called from xive_irq_startup(), the size of the cpumask can be larger than nr_cpu_ids. This can result in a WARN_ON. [...]
This happens because we're being called with our affinity mask set to irq_default_affinity. That in turn was populated using cpumask_setall(), which sets NR_CPUs worth of bits, not nr_cpu_ids worth. Finally cpumask_weight() will return > nr_cpu_ids when passed a mask which has > nr_cpu_ids bits set.

In modern kernel, cpumask_weight() can't return > nr_cpu_ids.

In inline case, cpumask_setall() explicitly clears all bits above nr_cpu_ids, see commit 63355b9884b3 ("cpumask: be more careful with 'cpumask_setall()'"). So, despite that cpumask_weight() is passed with small_cpumask_bits, which is NR_CPUS in this case, it can't count over the nr_cpu_ids.

In outline case, cpumask_setall() may set bits beyond the limit up to the next byte alignment, but in this case small_cpumask_bits is wired to nr_cpu_ids, thus making overcounting impossible.

---

I've added some explicit tests for cpumask_weight() to make sure:
https://lore.kernel.org/all/[email protected]/

 arch/powerpc/sysdev/xive/common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/sysdev/xive/common.c +1 -1
index e1a4f8a97393..e91ec9036ad8 100644
@@ -551,7 +551,7 @@ static int xive_find_target_in_mask(const struct cpumask *mask,
 	int cpu, first, num, i;
 
 	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
+	num = cpumask_weight(mask);
 	first = fuzz % num;
 
 	/* Locate it */
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] Revert "powerpc/xive: Fix the size of the cpumask used in xive_find_target_in_mask()"

Quoted 50 lines · Yury Norov

This reverts commit a9dadc1c512807f955f0799e85830b420da47932.

The commit message states:

When called from xive_irq_startup(), the size of the cpumask can be larger than nr_cpu_ids. This can result in a WARN_ON. [...]
This happens because we're being called with our affinity mask set to irq_default_affinity. That in turn was populated using cpumask_setall(), which sets NR_CPUs worth of bits, not nr_cpu_ids worth. Finally cpumask_weight() will return > nr_cpu_ids when passed a mask which has > nr_cpu_ids bits set.

In modern kernel, cpumask_weight() can't return > nr_cpu_ids.

In inline case, cpumask_setall() explicitly clears all bits above nr_cpu_ids, see commit 63355b9884b3 ("cpumask: be more careful with 'cpumask_setall()'"). So, despite that cpumask_weight() is passed with small_cpumask_bits, which is NR_CPUS in this case, it can't count over the nr_cpu_ids.

In outline case, cpumask_setall() may set bits beyond the limit up to the next byte alignment, but in this case small_cpumask_bits is wired to nr_cpu_ids, thus making overcounting impossible.

---

I've added some explicit tests for cpumask_weight() to make sure:
https://lore.kernel.org/all/[email protected]/

 arch/powerpc/sysdev/xive/common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/sysdev/xive/common.c +1 -1
index e1a4f8a97393..e91ec9036ad8 100644
@@ -551,7 +551,7 @@ static int xive_find_target_in_mask(const struct cpumask *mask,
 	int cpu, first, num, i;
 
 	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
+	num = cpumask_weight(mask);
 	first = fuzz % num;
 
 	/* Locate it */
Signature · 1 line
2.43.0

LGTM

author
#3 lore raw

PATCH 2/2 powerpc/xive: rework xive_find_target_in_mask()

In reply to Yury Norov

Switch the function to using modern cpumask API and drop most of the housekeeping code.

Notice, if first >= nr_cpu_ids, for_each_cpu_wrap() iterator behaves just like for_each_cpu(), i.e. begins from 0. So even if WARN_ON() is triggered, no special handling is needed.

---
 arch/powerpc/sysdev/xive/common.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
arch/powerpc/sysdev/xive/common.c +6 -25
index e91ec9036ad8..4e05f678e171 100644
@@ -548,40 +548,21 @@ static void xive_dec_target_count(int cpu)
 static int xive_find_target_in_mask(const struct cpumask *mask,
 				    unsigned int fuzz)
 {
-	int cpu, first, num, i;
+	int cpu, first;
 
 	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = cpumask_weight(mask);
-	first = fuzz % num;
-
-	/* Locate it */
-	cpu = cpumask_first(mask);
-	for (i = 0; i < first && cpu < nr_cpu_ids; i++)
-		cpu = cpumask_next(cpu, mask);
-
-	/* Sanity check */
-	if (WARN_ON(cpu >= nr_cpu_ids))
-		cpu = cpumask_first(cpu_online_mask);
-
-	/* Remember first one to handle wrap-around */
-	first = cpu;
+	fuzz %= cpumask_weight(mask);
+	first = cpumask_nth(fuzz, mask);
+	WARN_ON(first >= nr_cpu_ids);
 
 	/*
 	 * Now go through the entire mask until we find a valid
 	 * target.
 	 */
-	do {
-		/*
-		 * We re-check online as the fallback case passes us
-		 * an untested affinity mask
-		 */
+	for_each_cpu_wrap(cpu, mask, first) {
 		if (cpu_online(cpu) && xive_try_pick_target(cpu))
 			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
-	} while (cpu != first);
+	}
 
 	return -1;
 }
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 2/2
#4 lore raw

Hi Yury.

Quoted 53 lines · Yury Norov

Switch the function to using modern cpumask API and drop most of the housekeeping code.

Notice, if first >= nr_cpu_ids, for_each_cpu_wrap() iterator behaves just like for_each_cpu(), i.e. begins from 0. So even if WARN_ON() is triggered, no special handling is needed.

---
  arch/powerpc/sysdev/xive/common.c | 31 ++++++-------------------------
  1 file changed, 6 insertions(+), 25 deletions(-)
arch/powerpc/sysdev/xive/common.c +5 -20
index e91ec9036ad8..4e05f678e171 100644
@@ -548,40 +548,21 @@ static void xive_dec_target_count(int cpu)
  static int xive_find_target_in_mask(const struct cpumask *mask,
  				    unsigned int fuzz)
  {
-	int cpu, first, num, i;
+	int cpu, first;
 
  	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = cpumask_weight(mask);
-	first = fuzz % num;
-
-	/* Locate it */
-	cpu = cpumask_first(mask);
-	for (i = 0; i < first && cpu < nr_cpu_ids; i++)
-		cpu = cpumask_next(cpu, mask);
-
-	/* Sanity check */
-	if (WARN_ON(cpu >= nr_cpu_ids))
-		cpu = cpumask_first(cpu_online_mask);
-
-	/* Remember first one to handle wrap-around */
-	first = cpu;
+	fuzz %= cpumask_weight(mask);
+	first = cpumask_nth(fuzz, mask);
+	WARN_ON(first >= nr_cpu_ids);
 
  	/*
  	 * Now go through the entire mask until we find a valid
  	 * target.
  	 */
-	do {
-		/*
-		 * We re-check online as the fallback case passes us
-		 * an untested affinity mask
-		 */
+	for_each_cpu_wrap(cpu, mask, first) {
  		if (cpu_online(cpu) && xive_try_pick_target(cpu))

arch/powerpc/sysdev/xive/common.c static void xive_dec_target_count(int cpu) · line 568

Quoted 10 lines
arch/powerpc/sysdev/xive/common.c +1 -5
  			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
-	} while (cpu != first);
+	}
 
  	return -1;
  }

Only concern i have, (which could potentially leads to while(1) loop today if it), is if mask is empty. atleast for_each_cpu_wrap will not be a while(1) loop.

So, IMO this is better than what we have today.

nit: maybe a good to add a WARN_ON(cpu >= nr_cpu_ids) in the end.

author
#5 lore raw
Quoted 68 lines · Shrikanth Hegde

Hi Yury.

Quoted 50 lines · Yury Norov

Switch the function to using modern cpumask API and drop most of the housekeeping code.

Notice, if first >= nr_cpu_ids, for_each_cpu_wrap() iterator behaves just like for_each_cpu(), i.e. begins from 0. So even if WARN_ON() is triggered, no special handling is needed.

---
  arch/powerpc/sysdev/xive/common.c | 31 ++++++-------------------------
  1 file changed, 6 insertions(+), 25 deletions(-)
arch/powerpc/sysdev/xive/common.c +5 -20
index e91ec9036ad8..4e05f678e171 100644
@@ -548,40 +548,21 @@ static void xive_dec_target_count(int cpu)
  static int xive_find_target_in_mask(const struct cpumask *mask,
  				    unsigned int fuzz)
  {
-	int cpu, first, num, i;
+	int cpu, first;
  	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = cpumask_weight(mask);
-	first = fuzz % num;
-
-	/* Locate it */
-	cpu = cpumask_first(mask);
-	for (i = 0; i < first && cpu < nr_cpu_ids; i++)
-		cpu = cpumask_next(cpu, mask);
-
-	/* Sanity check */
-	if (WARN_ON(cpu >= nr_cpu_ids))
-		cpu = cpumask_first(cpu_online_mask);
-
-	/* Remember first one to handle wrap-around */
-	first = cpu;
+	fuzz %= cpumask_weight(mask);
+	first = cpumask_nth(fuzz, mask);
+	WARN_ON(first >= nr_cpu_ids);
  	/*
  	 * Now go through the entire mask until we find a valid
  	 * target.
  	 */
-	do {
-		/*
-		 * We re-check online as the fallback case passes us
-		 * an untested affinity mask
-		 */
+	for_each_cpu_wrap(cpu, mask, first) {

arch/powerpc/sysdev/xive/common.c static void xive_dec_target_count(int cpu) · line 565

Quoted 10 lines
arch/powerpc/sysdev/xive/common.c +1 -5
  		if (cpu_online(cpu) && xive_try_pick_target(cpu))
  			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
-	} while (cpu != first);
+	}
  	return -1;
  }

Only concern i have, (which could potentially leads to while(1) loop today if it), is if mask is empty. atleast for_each_cpu_wrap will not be a while(1) loop.

Quoted 5 lines

So, IMO this is better than what we have today.

nit: maybe a good to add a WARN_ON(cpu >= nr_cpu_ids) in the end.

The iterator of for_each_cpu() is always >= nr_cpu_ids when it exits the loop because it's the exit condition. If you want to warn user before returning -1, just do __WARN().

#6 lore raw

Hi Yury,

Quoted 71 lines · Yury Norov
Quoted 70 lines · Shrikanth Hegde

Hi Yury.

Quoted 50 lines · Yury Norov

Switch the function to using modern cpumask API and drop most of the housekeeping code.

Notice, if first >= nr_cpu_ids, for_each_cpu_wrap() iterator behaves just like for_each_cpu(), i.e. begins from 0. So even if WARN_ON() is triggered, no special handling is needed.

---
   arch/powerpc/sysdev/xive/common.c | 31 ++++++-------------------------
   1 file changed, 6 insertions(+), 25 deletions(-)
arch/powerpc/sysdev/xive/common.c +5 -20
index e91ec9036ad8..4e05f678e171 100644
@@ -548,40 +548,21 @@ static void xive_dec_target_count(int cpu)
   static int xive_find_target_in_mask(const struct cpumask *mask,
   				    unsigned int fuzz)
   {
-	int cpu, first, num, i;
+	int cpu, first;
   	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = cpumask_weight(mask);
-	first = fuzz % num;
-
-	/* Locate it */
-	cpu = cpumask_first(mask);
-	for (i = 0; i < first && cpu < nr_cpu_ids; i++)
-		cpu = cpumask_next(cpu, mask);
-
-	/* Sanity check */
-	if (WARN_ON(cpu >= nr_cpu_ids))
-		cpu = cpumask_first(cpu_online_mask);
-
-	/* Remember first one to handle wrap-around */
-	first = cpu;
+	fuzz %= cpumask_weight(mask);
+	first = cpumask_nth(fuzz, mask);
+	WARN_ON(first >= nr_cpu_ids);
   	/*
   	 * Now go through the entire mask until we find a valid
   	 * target.
   	 */
-	do {
-		/*
-		 * We re-check online as the fallback case passes us
-		 * an untested affinity mask
-		 */
+	for_each_cpu_wrap(cpu, mask, first) {

arch/powerpc/sysdev/xive/common.c static void xive_dec_target_count(int cpu) · line 565

Quoted 10 lines
arch/powerpc/sysdev/xive/common.c +1 -5
   		if (cpu_online(cpu) && xive_try_pick_target(cpu))
   			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
-	} while (cpu != first);
+	}
   	return -1;
   }

Only concern i have, (which could potentially leads to while(1) loop today if it), is if mask is empty. atleast for_each_cpu_wrap will not be a while(1) loop.

So, IMO this is better than what we have today.

Quoted 7 lines
Quoted 3 lines

nit: maybe a good to add a WARN_ON(cpu >= nr_cpu_ids) in the end.

The iterator of for_each_cpu() is always >= nr_cpu_ids when it exits the loop because it's the exit condition. If you want to warn user before returning -1, just do __WARN().

Ok.

It might take a while a while to send the next version out since i am working on getting the next version of that paravirt series out soon.

#7 lore raw
Quoted 78 lines · Shrikanth Hegde

Hi Yury,

Quoted 75 lines · Yury Norov
Quoted 74 lines · Shrikanth Hegde

Hi Yury.

Quoted 51 lines · Yury Norov

Switch the function to using modern cpumask API and drop most of the housekeeping code.

Notice, if first >= nr_cpu_ids, for_each_cpu_wrap() iterator behaves just like for_each_cpu(), i.e. begins from 0. So even if WARN_ON() is triggered, no special handling is needed.

---
   arch/powerpc/sysdev/xive/common.c | 31
++++++-------------------------

   1 file changed, 6 insertions(+), 25 deletions(-)
arch/powerpc/sysdev/xive/common.c +5 -20
index e91ec9036ad8..4e05f678e171 100644
@@ -548,40 +548,21 @@ static void xive_dec_target_count(int cpu)
    static int xive_find_target_in_mask(const struct cpumask *mask,
                        unsigned int fuzz)
    {
-    int cpu, first, num, i;
+    int cpu, first;
        /* Pick up a starting point CPU in the mask based on fuzz */
-    num = cpumask_weight(mask);
-    first = fuzz % num;
-
-    /* Locate it */
-    cpu = cpumask_first(mask);
-    for (i = 0; i < first && cpu < nr_cpu_ids; i++)
-        cpu = cpumask_next(cpu, mask);
-
-    /* Sanity check */
-    if (WARN_ON(cpu >= nr_cpu_ids))
-        cpu = cpumask_first(cpu_online_mask);
-
-    /* Remember first one to handle wrap-around */
-    first = cpu;
+    fuzz %= cpumask_weight(mask);
+    first = cpumask_nth(fuzz, mask);
+    WARN_ON(first >= nr_cpu_ids);
        /*
         * Now go through the entire mask until we find a valid
         * target.
         */
-    do {
-        /*
-         * We re-check online as the fallback case passes us
-         * an untested affinity mask
-         */
+    for_each_cpu_wrap(cpu, mask, first) {

arch/powerpc/sysdev/xive/common.c static void xive_dec_target_count(int cpu) · line 565

Quoted 10 lines
arch/powerpc/sysdev/xive/common.c +1 -5
            if (cpu_online(cpu) && xive_try_pick_target(cpu))
                return cpu;
-        cpu = cpumask_next(cpu, mask);
-        /* Wrap around */
-        if (cpu >= nr_cpu_ids)
-            cpu = cpumask_first(mask);
-    } while (cpu != first);
+    }
        return -1;
    }

Only concern i have, (which could potentially leads to while(1) loop today if it), is if mask is empty. atleast for_each_cpu_wrap will not be a while(1) loop.

So, IMO this is better than what we have today.

nit: maybe a good to add a WARN_ON(cpu >= nr_cpu_ids) in the end.

Quoted 8 lines
Quoted 3 lines

  The iterator of for_each_cpu() is always >= nr_cpu_ids when it exits the loop because it's the exit condition. If you want to warn user before returning -1, just do __WARN().

Ok.

It might take a while a while to send the next version out since i am working on getting the next version of that paravirt series out soon.

OK, I am pulling this patchset as-is for next merge. I will expect a followup patch  to add a __WARN().

Maddy

Tested-by Reviewed-by on the v1 series
#8 lore raw
In reply to Yury Norov
Quoted 13 lines · Yury Norov

Simplify the function by switching to using a modern cpumask API.

Yury Norov (2):
Revert "powerpc/xive: Fix the size of the cpumask used in

    xive_find_target_in_mask()"

powerpc/xive: rework xive_find_target_in_mask()

 arch/powerpc/sysdev/xive/common.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
Signature · 1 line
2.43.0

Tested the whole series with
https://lore.kernel.org/all/[email protected]/

maintainer Applied
#9 lore raw
In reply to Yury Norov
Quoted 8 lines · Yury Norov

Simplify the function by switching to using a modern cpumask API.

Yury Norov (2):
Revert "powerpc/xive: Fix the size of the cpumask used in

    xive_find_target_in_mask()"

powerpc/xive: rework xive_find_target_in_mask()

[...]

Applied to powerpc/next.

[1/2] Revert "powerpc/xive: Fix the size of the cpumask used in xive_find_target_in_mask()"

      https://git.kernel.org/powerpc/c/cad2a72c29e037f1ade0079f7e4b925508680e20
[2/2] powerpc/xive: rework xive_find_target_in_mask()
      https://git.kernel.org/powerpc/c/ce7c43b0871989b4c665cceb9720d79b933c1818

cheers