Skip to contentSkip to conversation

powerpc/xive: Don't call cpu_online() on an invalid CPU number

PATCH Superseded Benjamin Herrenschmidt 1 participant 3 messages Apr 10, 2017 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 powerpc/xive: Don't call cpu_online() on an invalid CPU number

If the interrupt didn't have a selected target yet, we could call cpu_online() and do other cpumask tests with cpu #-1 which would result in random outcomes.

---
 arch/powerpc/sysdev/xive/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/sysdev/xive/common.c +2 -1
index 006a53e..dbbe446 100644
@@ -662,7 +662,8 @@ static int xive_irq_set_affinity(struct irq_data *d,
 	 * If existing target is already in the new mask, and is
 	 * online then do nothing.
 	 */
-	if (cpu_online(xd->target) &&
+	if (xd->target != XIVE_INVALID_TARGET &&
+	    cpu_online(xd->target) &&
 	    cpumask_test_cpu(xd->target, cpumask))
 		return IRQ_SET_MASK_OK;
 
Signature · 1 line
2.9.3
#2 lore raw

PATCH 3/3 powerpc/xive: Extra sanity checks on cpu numbers

When targetting interrupts we do various manipulations of cpu numbers and CPU masks. This adds some sanity checking to ensure we don't break assumptions and manpulate cpu numbers that are out of bounds of the various cpumasks.

---
 arch/powerpc/sysdev/xive/common.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
arch/powerpc/sysdev/xive/common.c +17 -1
index abda9b2..496036c 100644
@@ -452,8 +452,14 @@ static int xive_find_target_in_mask(const struct cpumask *mask,
 
 	/* Locate it */
 	cpu = cpumask_first(mask);
-	for (i = 0; i < first; i++)
+	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;
 
 	/*
@@ -545,6 +551,12 @@ static unsigned int xive_irq_startup(struct irq_data *d)
 			return -ENXIO;
 		pr_warn("irq %d started with broken affinity\n", d->irq);
 	}
+
+	/* Sanity check */
+	if (WARN_ON(target == XIVE_INVALID_TARGET ||
+		    target >= nr_cpu_ids))
+		target = smp_processor_id();
+
 	xd->target = target;
 
 	/*
@@ -676,6 +688,10 @@ static int xive_irq_set_affinity(struct irq_data *d,
 	if (target == XIVE_INVALID_TARGET)
 		return -ENXIO;
 
+	/* Sanity check */
+	if (WARN_ON(target >= nr_cpu_ids))
+		target = smp_processor_id();
+
 	old_target = xd->target;
 
 	/*
Signature · 1 line
2.9.3
#1 lore raw

PATCH 2/3 powerpx/xive: Fix irq target selection returning out of bounds cpu#

In reply to Benjamin Herrenschmidt

xive_pick_irq_target() first tries to construct a mask that is the intersection of the requested affinity, online CPUs, and the group of CPUs that are on the same chip as the interrupt source.

If that resulting mask is empty, we were incorrectly returning nr_cpu_ids as a target.

---
 arch/powerpc/sysdev/xive/common.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/sysdev/xive/common.c +4 -2
index dbbe446..abda9b2 100644
@@ -492,7 +492,7 @@ static int xive_pick_irq_target(struct irq_data *d,
 
 	/*
 	 * If we have chip IDs, first we try to build a mask of
-	 * CPUs matching ther CPU and find a target in there
+	 * CPUs matching the CPU and find a target in there
 	 */
 	if (xd->src_chip != XIVE_INVALID_CHIP_ID &&
 		zalloc_cpumask_var(&mask, GFP_ATOMIC)) {
@@ -503,7 +503,9 @@ static int xive_pick_irq_target(struct irq_data *d,
 				cpumask_set_cpu(cpu, mask);
 		}
 		/* Try to find a target */
-		if (!cpumask_empty(mask))
+		if (cpumask_empty(mask))
+			cpu = -1;
+		else
 			cpu = xive_find_target_in_mask(mask, fuzz++);
 		free_cpumask_var(mask);
 		if (cpu >= 0)
Signature · 1 line
2.9.3