Skip to content

Commit 0141978

Browse files
zhang-ruirafaeljw
authored andcommitted
x86/acpi: Fix LAPIC/x2APIC parsing order
On some systems, the same CPU (with the same APIC ID) is assigned a different logical CPU id after commit ec9aedb ("x86/acpi: Ignore invalid x2APIC entries"). This means that Linux enumerates the CPUs in a different order, which violates ACPI specification[1] that states: "OSPM should initialize processors in the order that they appear in the MADT" The problematic commit parses all LAPIC entries before any x2APIC entries, aiming to ignore x2APIC entries with APIC ID < 255 when valid LAPIC entries exist. However, it disrupts the CPU enumeration order on systems where x2APIC entries precede LAPIC entries in the MADT. Fix this problem by: 1) Parsing LAPIC entries first without registering them in the topology to evaluate whether valid LAPIC entries exist. 2) Restoring the MADT in order parser which invokes either the LAPIC or the X2APIC parser function depending on the entry type. The X2APIC parser still ignores entries < 0xff in case that #1 found valid LAPIC entries independent of their position in the MADT table. Link: https://uefi.org/specs/ACPI/6.5/05_ACPI_Software_Programming_Model.html#madt-processor-local-apic-sapic-structure-entry-order Cc: All applicable <[email protected]> Reported-by: Jim Mattson <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Fixes: ec9aedb ("x86/acpi: Ignore invalid x2APIC entries") Signed-off-by: Zhang Rui <[email protected]> Reviewed-by: Jim Mattson <[email protected]> Tested-by: Jim Mattson <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 8f62ca9 commit 0141978

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

arch/x86/kernel/acpi/boot.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,28 @@ acpi_parse_x2apic(union acpi_subtable_headers *header, const unsigned long end)
226226
return 0;
227227
}
228228

229+
static int __init
230+
acpi_check_lapic(union acpi_subtable_headers *header, const unsigned long end)
231+
{
232+
struct acpi_madt_local_apic *processor = NULL;
233+
234+
processor = (struct acpi_madt_local_apic *)header;
235+
236+
if (BAD_MADT_ENTRY(processor, end))
237+
return -EINVAL;
238+
239+
/* Ignore invalid ID */
240+
if (processor->id == 0xff)
241+
return 0;
242+
243+
/* Ignore processors that can not be onlined */
244+
if (!acpi_is_processor_usable(processor->lapic_flags))
245+
return 0;
246+
247+
has_lapic_cpus = true;
248+
return 0;
249+
}
250+
229251
static int __init
230252
acpi_parse_lapic(union acpi_subtable_headers * header, const unsigned long end)
231253
{
@@ -257,7 +279,6 @@ acpi_parse_lapic(union acpi_subtable_headers * header, const unsigned long end)
257279
processor->processor_id, /* ACPI ID */
258280
processor->lapic_flags & ACPI_MADT_ENABLED);
259281

260-
has_lapic_cpus = true;
261282
return 0;
262283
}
263284

@@ -1029,6 +1050,8 @@ static int __init early_acpi_parse_madt_lapic_addr_ovr(void)
10291050
static int __init acpi_parse_madt_lapic_entries(void)
10301051
{
10311052
int count, x2count = 0;
1053+
struct acpi_subtable_proc madt_proc[2];
1054+
int ret;
10321055

10331056
if (!boot_cpu_has(X86_FEATURE_APIC))
10341057
return -ENODEV;
@@ -1037,10 +1060,27 @@ static int __init acpi_parse_madt_lapic_entries(void)
10371060
acpi_parse_sapic, MAX_LOCAL_APIC);
10381061

10391062
if (!count) {
1040-
count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC,
1041-
acpi_parse_lapic, MAX_LOCAL_APIC);
1042-
x2count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_X2APIC,
1043-
acpi_parse_x2apic, MAX_LOCAL_APIC);
1063+
/* Check if there are valid LAPIC entries */
1064+
acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC, acpi_check_lapic, MAX_LOCAL_APIC);
1065+
1066+
/*
1067+
* Enumerate the APIC IDs in the order that they appear in the
1068+
* MADT, no matter LAPIC entry or x2APIC entry is used.
1069+
*/
1070+
memset(madt_proc, 0, sizeof(madt_proc));
1071+
madt_proc[0].id = ACPI_MADT_TYPE_LOCAL_APIC;
1072+
madt_proc[0].handler = acpi_parse_lapic;
1073+
madt_proc[1].id = ACPI_MADT_TYPE_LOCAL_X2APIC;
1074+
madt_proc[1].handler = acpi_parse_x2apic;
1075+
ret = acpi_table_parse_entries_array(ACPI_SIG_MADT,
1076+
sizeof(struct acpi_table_madt),
1077+
madt_proc, ARRAY_SIZE(madt_proc), MAX_LOCAL_APIC);
1078+
if (ret < 0) {
1079+
pr_err("Error parsing LAPIC/X2APIC entries\n");
1080+
return ret;
1081+
}
1082+
count = madt_proc[0].count;
1083+
x2count = madt_proc[1].count;
10441084
}
10451085
if (!count && !x2count) {
10461086
pr_err("No LAPIC entries present\n");

0 commit comments

Comments
 (0)