x86/apic: Fix signedness bug in APIC ID validity checks

The APIC ID as parsed from ACPI MADT is validity checked with the
apic->apic_id_valid() callback, which depends on the selected APIC type.

For non X2APIC types APIC IDs >= 0xFF are invalid, but values > 0x7FFFFFFF
are detected as valid. This happens because the 'apicid' argument of the
apic_id_valid() callback is type 'int'. So the resulting comparison

apicid < 0xFF

evaluates to true for all unsigned int values > 0x7FFFFFFF which are handed
to default_apic_id_valid(). As a consequence, invalid APIC IDs in !X2APIC
mode are considered valid and accounted as possible CPUs.

Change the apicid argument type of the apic_id_valid() callback to u32 so
the evaluation is unsigned and returns the correct result.

[ tglx: Massaged changelog ]

Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
Cc: jgross@suse.com
Cc: Dou Liyang <douly.fnst@cn.fujitsu.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: hpa@zytor.com
Link: https://lkml.kernel.org/r/1523322966-10296-1-git-send-email-lirongqing@baidu.com

authored by Li RongQing and committed by Thomas Gleixner a774635d d94a155c

+16 -13
+2 -2
arch/x86/include/asm/apic.h
··· 319 319 /* Probe, setup and smpboot functions */ 320 320 int (*probe)(void); 321 321 int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id); 322 - int (*apic_id_valid)(int apicid); 322 + int (*apic_id_valid)(u32 apicid); 323 323 int (*apic_id_registered)(void); 324 324 325 325 bool (*check_apicid_used)(physid_mask_t *map, int apicid); ··· 492 492 return apic->get_apic_id(reg); 493 493 } 494 494 495 - extern int default_apic_id_valid(int apicid); 495 + extern int default_apic_id_valid(u32 apicid); 496 496 extern int default_acpi_madt_oem_check(char *, char *); 497 497 extern void default_setup_apic_routing(void); 498 498
+8 -5
arch/x86/kernel/acpi/boot.c
··· 200 200 { 201 201 struct acpi_madt_local_x2apic *processor = NULL; 202 202 #ifdef CONFIG_X86_X2APIC 203 - int apic_id; 203 + u32 apic_id; 204 204 u8 enabled; 205 205 #endif 206 206 ··· 222 222 * to not preallocating memory for all NR_CPUS 223 223 * when we use CPU hotplug. 224 224 */ 225 - if (!apic->apic_id_valid(apic_id) && enabled) 226 - printk(KERN_WARNING PREFIX "x2apic entry ignored\n"); 227 - else 228 - acpi_register_lapic(apic_id, processor->uid, enabled); 225 + if (!apic->apic_id_valid(apic_id)) { 226 + if (enabled) 227 + pr_warn(PREFIX "x2apic entry ignored\n"); 228 + return 0; 229 + } 230 + 231 + acpi_register_lapic(apic_id, processor->uid, enabled); 229 232 #else 230 233 printk(KERN_WARNING PREFIX "x2apic entry ignored\n"); 231 234 #endif
+1 -1
arch/x86/kernel/apic/apic_common.c
··· 40 40 return physid_isset(phys_apicid, phys_cpu_present_map); 41 41 } 42 42 43 - int default_apic_id_valid(int apicid) 43 + int default_apic_id_valid(u32 apicid) 44 44 { 45 45 return (apicid < 255); 46 46 }
+1 -1
arch/x86/kernel/apic/apic_numachip.c
··· 56 56 return id << 24; 57 57 } 58 58 59 - static int numachip_apic_id_valid(int apicid) 59 + static int numachip_apic_id_valid(u32 apicid) 60 60 { 61 61 /* Trust what bootloader passes in MADT */ 62 62 return 1;
+1 -1
arch/x86/kernel/apic/x2apic.h
··· 1 1 /* Common bits for X2APIC cluster/physical modes. */ 2 2 3 - int x2apic_apic_id_valid(int apicid); 3 + int x2apic_apic_id_valid(u32 apicid); 4 4 int x2apic_apic_id_registered(void); 5 5 void __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest); 6 6 unsigned int x2apic_get_apic_id(unsigned long id);
+1 -1
arch/x86/kernel/apic/x2apic_phys.c
··· 101 101 } 102 102 103 103 /* Common x2apic functions, also used by x2apic_cluster */ 104 - int x2apic_apic_id_valid(int apicid) 104 + int x2apic_apic_id_valid(u32 apicid) 105 105 { 106 106 return 1; 107 107 }
+1 -1
arch/x86/kernel/apic/x2apic_uv_x.c
··· 557 557 uv_send_IPI_mask(cpu_online_mask, vector); 558 558 } 559 559 560 - static int uv_apic_id_valid(int apicid) 560 + static int uv_apic_id_valid(u32 apicid) 561 561 { 562 562 return 1; 563 563 }
+1 -1
arch/x86/xen/apic.c
··· 112 112 return xen_pv_domain(); 113 113 } 114 114 115 - static int xen_id_always_valid(int apicid) 115 + static int xen_id_always_valid(u32 apicid) 116 116 { 117 117 return 1; 118 118 }