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