x86: fix cpu_mask_to_apicid_and to include cpu_online_mask
authorMike Travis <travis@sgi.com>
Wed, 17 Dec 2008 23:21:39 +0000 (15:21 -0800)
committerIngo Molnar <mingo@elte.hu>
Thu, 18 Dec 2008 10:59:24 +0000 (11:59 +0100)
Impact: fix potential APIC crash

In determining the destination apicid, there are usually three cpumasks
that are considered: the incoming cpumask arg, cfg->domain and the
cpu_online_mask.  Since we are just introducing the cpu_mask_to_apicid_and
function, make sure it includes the cpu_online_mask in it's evaluation.
[Added with this patch.]

There are two io_apic.c functions that did not previously use the
cpu_online_mask:  setup_IO_APIC_irq and msi_compose_msg.  Both of these
simply used cpu_mask_to_apicid(cfg->domain & TARGET_CPUS), and all but
one arch (NUMAQ[*]) returns only online cpus in the TARGET_CPUS mask,
so the behavior is identical for all cases.

[*: NUMAQ bug?]

Note that alloc_cpumask_var is only used for the 32-bit cases where
it's highly likely that the cpumask set size will be small and therefore
CPUMASK_OFFSTACK=n.  But if that's not the case, failing the allocate
will cause the same return value as the default.

Signed-off-by: Mike Travis <travis@sgi.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
arch/x86/include/asm/bigsmp/apic.h
arch/x86/include/asm/es7000/apic.h
arch/x86/include/asm/mach-default/mach_apic.h
arch/x86/include/asm/summit/apic.h
arch/x86/kernel/genapic_flat_64.c
arch/x86/kernel/genx2apic_cluster.c
arch/x86/kernel/genx2apic_phys.c
arch/x86/kernel/genx2apic_uv_x.c

index 976399debb3f1511a6b2c8faedf26c491fc97ce1..d8dd9f537911fb2a6e98f5c7d68cd2121cfd6ecc 100644 (file)
@@ -138,7 +138,9 @@ static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *cpumask,
         * We're using fixed IRQ delivery, can only return one phys APIC ID.
         * May as well be the first.
         */
-       cpu = cpumask_any_and(cpumask, andmask);
+       for_each_cpu_and(cpu, cpumask, andmask)
+               if (cpumask_test_cpu(cpu, cpu_online_mask))
+                       break;
        if (cpu < nr_cpu_ids)
                return cpu_to_logical_apicid(cpu);
 
index ba8423c5363faf5231ea1b01cc7757186b2bac18..51ac1230294e53561e176904a5d37d05bdfa712b 100644 (file)
@@ -214,51 +214,47 @@ static inline unsigned int cpu_mask_to_apicid(const cpumask_t *cpumask)
        return apicid;
 }
 
-static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *cpumask,
+
+static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *inmask,
                                                  const struct cpumask *andmask)
 {
        int num_bits_set;
-       int num_bits_set2;
        int cpus_found = 0;
        int cpu;
-       int apicid = 0;
+       int apicid = cpu_to_logical_apicid(0);
+       cpumask_var_t cpumask;
+
+       if (!alloc_cpumask_var(&cpumask, GFP_ATOMIC))
+               return apicid;
+
+       cpumask_and(cpumask, inmask, andmask);
+       cpumask_and(cpumask, cpumask, cpu_online_mask);
 
        num_bits_set = cpumask_weight(cpumask);
-       num_bits_set2 = cpumask_weight(andmask);
-       num_bits_set = min(num_bits_set, num_bits_set2);
        /* Return id to all */
-       if (num_bits_set >= nr_cpu_ids)
-#if defined CONFIG_ES7000_CLUSTERED_APIC
-               return 0xFF;
-#else
-               return cpu_to_logical_apicid(0);
-#endif
+       if (num_bits_set == NR_CPUS)
+               goto exit;
        /*
         * The cpus in the mask must all be on the apic cluster.  If are not
         * on the same apicid cluster return default value of TARGET_CPUS.
         */
-       cpu = cpumask_first_and(cpumask, andmask);
+       cpu = cpumask_first(cpumask);
        apicid = cpu_to_logical_apicid(cpu);
-
        while (cpus_found < num_bits_set) {
-               if (cpumask_test_cpu(cpu, cpumask) &&
-                   cpumask_test_cpu(cpu, andmask)) {
+               if (cpumask_test_cpu(cpu, cpumask)) {
                        int new_apicid = cpu_to_logical_apicid(cpu);
                        if (apicid_cluster(apicid) !=
-                                       apicid_cluster(new_apicid)) {
-                               printk(KERN_WARNING
-                                       "%s: Not a valid mask!\n", __func__);
-#if defined CONFIG_ES7000_CLUSTERED_APIC
-                               return 0xFF;
-#else
+                                       apicid_cluster(new_apicid)){
+                               printk ("%s: Not a valid mask!\n", __func__);
                                return cpu_to_logical_apicid(0);
-#endif
                        }
                        apicid = new_apicid;
                        cpus_found++;
                }
                cpu++;
        }
+exit:
+       free_cpumask_var(cpumask);
        return apicid;
 }
 
index 8863d978cb96f31a417d6abab49c6fcbe78232da..cc09cbbee27e9fc7d3e7f1adb339d812819818e5 100644 (file)
@@ -72,8 +72,9 @@ static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *cpumask,
 {
        unsigned long mask1 = cpumask_bits(cpumask)[0];
        unsigned long mask2 = cpumask_bits(andmask)[0];
+       unsigned long mask3 = cpumask_bits(cpu_online_mask)[0];
 
-       return (unsigned int)(mask1 & mask2);
+       return (unsigned int)(mask1 & mask2 & mask3);
 }
 
 static inline u32 phys_pkg_id(u32 cpuid_apic, int index_msb)
index 651a9384934128d7e24d85ffa51818687ef7b151..99327d1be49f0bd0fed4ecd5ca189af6bde63188 100644 (file)
@@ -170,35 +170,37 @@ static inline unsigned int cpu_mask_to_apicid(const cpumask_t *cpumask)
        return apicid;
 }
 
-static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *cpumask,
+static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *inmask,
                                                  const struct cpumask *andmask)
 {
        int num_bits_set;
-       int num_bits_set2;
        int cpus_found = 0;
        int cpu;
-       int apicid = 0;
+       int apicid = 0xFF;
+       cpumask_var_t cpumask;
+
+       if (!alloc_cpumask_var(&cpumask, GFP_ATOMIC))
+               return (int) 0xFF;
+
+       cpumask_and(cpumask, inmask, andmask);
+       cpumask_and(cpumask, cpumask, cpu_online_mask);
 
        num_bits_set = cpumask_weight(cpumask);
-       num_bits_set2 = cpumask_weight(andmask);
-       num_bits_set = min(num_bits_set, num_bits_set2);
        /* Return id to all */
-       if (num_bits_set >= nr_cpu_ids)
-               return 0xFF;
+       if (num_bits_set == nr_cpu_ids)
+               goto exit;
        /*
         * The cpus in the mask must all be on the apic cluster.  If are not
         * on the same apicid cluster return default value of TARGET_CPUS.
         */
-       cpu = cpumask_first_and(cpumask, andmask);
+       cpu = cpumask_first(cpumask);
        apicid = cpu_to_logical_apicid(cpu);
        while (cpus_found < num_bits_set) {
-               if (cpumask_test_cpu(cpu, cpumask)
-                   && cpumask_test_cpu(cpu, andmask)) {
+               if (cpumask_test_cpu(cpu, cpumask)) {
                        int new_apicid = cpu_to_logical_apicid(cpu);
                        if (apicid_cluster(apicid) !=
-                                       apicid_cluster(new_apicid)) {
-                               printk(KERN_WARNING
-                                       "%s: Not a valid mask!\n", __func__);
+                                       apicid_cluster(new_apicid)){
+                               printk ("%s: Not a valid mask!\n", __func__);
                                return 0xFF;
                        }
                        apicid = apicid | new_apicid;
@@ -206,6 +208,8 @@ static inline unsigned int cpu_mask_to_apicid_and(const struct cpumask *cpumask,
                }
                cpu++;
        }
+exit:
+       free_cpumask_var(cpumask);
        return apicid;
 }
 
index 7fa5f49c2ddac015b8aa7bc4f132b60793efba2a..34185488e4fb47e41aed797bcd8cd6b1a5c9d33d 100644 (file)
@@ -276,7 +276,9 @@ physflat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
         * We're using fixed IRQ delivery, can only return one phys APIC ID.
         * May as well be the first.
         */
-       cpu = cpumask_any_and(cpumask, andmask);
+       for_each_cpu_and(cpu, cpumask, andmask)
+               if (cpumask_test_cpu(cpu, cpu_online_mask))
+                       break;
        if (cpu < nr_cpu_ids)
                return per_cpu(x86_cpu_to_apicid, cpu);
        return BAD_APICID;
index 4716a0c9f9369a3c721a147aadb423850d80035e..d451c9b9fdff22a3d397b2f0de7aa0054cdbe509 100644 (file)
@@ -133,7 +133,9 @@ static unsigned int x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
         * We're using fixed IRQ delivery, can only return one phys APIC ID.
         * May as well be the first.
         */
-       cpu = cpumask_any_and(cpumask, andmask);
+       for_each_cpu_and(cpu, cpumask, andmask)
+               if (cpumask_test_cpu(cpu, cpu_online_mask))
+                       break;
        if (cpu < nr_cpu_ids)
                return per_cpu(x86_cpu_to_apicid, cpu);
        return BAD_APICID;
index b255507884f219487368b067034e4b5fc01d3871..62895cf315ffb1041e57364cd35d385a62826b25 100644 (file)
@@ -132,7 +132,9 @@ static unsigned int x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
         * We're using fixed IRQ delivery, can only return one phys APIC ID.
         * May as well be the first.
         */
-       cpu = cpumask_any_and(cpumask, andmask);
+       for_each_cpu_and(cpu, cpumask, andmask)
+               if (cpumask_test_cpu(cpu, cpu_online_mask))
+                       break;
        if (cpu < nr_cpu_ids)
                return per_cpu(x86_cpu_to_apicid, cpu);
        return BAD_APICID;
index 3984682cd84976b06a15c695f199116b130ed33c..0e88be11227df8cc0e9b72f4eb74af2928fffb16 100644 (file)
@@ -188,7 +188,9 @@ static unsigned int uv_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
         * We're using fixed IRQ delivery, can only return one phys APIC ID.
         * May as well be the first.
         */
-       cpu = cpumask_any_and(cpumask, andmask);
+       for_each_cpu_and(cpu, cpumask, andmask)
+               if (cpumask_test_cpu(cpu, cpu_online_mask))
+                       break;
        if (cpu < nr_cpu_ids)
                return per_cpu(x86_cpu_to_apicid, cpu);
        return BAD_APICID;