Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 233323f

Browse files
visitorckwrafaeljw
authored andcommitted
ACPI: processor_idle: Fix invalid comparison with insertion sort for latency
The acpi_cst_latency_cmp() comparison function currently used for sorting C-state latencies does not satisfy transitivity, causing incorrect sorting results. Specifically, if there are two valid acpi_processor_cx elements A and B and one invalid element C, it may occur that A < B, A = C, and B = C. Sorting algorithms assume that if A < B and A = C, then C < B, leading to incorrect ordering. Given the small size of the array (<=8), we replace the library sort function with a simple insertion sort that properly ignores invalid elements and sorts valid ones based on latency. This change ensures correct ordering of the C-state latencies. Fixes: 65ea8f2 ("ACPI: processor idle: Fix up C-state latency if not ordered") Reported-by: Julian Sikorski <belegdol@gmail.com> Closes: https://lore.kernel.org/lkml/70674dc7-5586-4183-8953-8095567e73df@gmail.com Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> Tested-by: Julian Sikorski <belegdol@gmail.com> Cc: All applicable <stable@vger.kernel.org> Link: https://patch.msgid.link/20240701205639.117194-1-visitorckw@gmail.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 22a40d1 commit 233323f

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

drivers/acpi/processor_idle.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <linux/acpi.h>
1717
#include <linux/dmi.h>
1818
#include <linux/sched.h> /* need_resched() */
19-
#include <linux/sort.h>
2019
#include <linux/tick.h>
2120
#include <linux/cpuidle.h>
2221
#include <linux/cpu.h>
@@ -386,25 +385,24 @@ static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
386385
acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
387386
}
388387

389-
static int acpi_cst_latency_cmp(const void *a, const void *b)
388+
static void acpi_cst_latency_sort(struct acpi_processor_cx *states, size_t length)
390389
{
391-
const struct acpi_processor_cx *x = a, *y = b;
390+
int i, j, k;
392391

393-
if (!(x->valid && y->valid))
394-
return 0;
395-
if (x->latency > y->latency)
396-
return 1;
397-
if (x->latency < y->latency)
398-
return -1;
399-
return 0;
400-
}
401-
static void acpi_cst_latency_swap(void *a, void *b, int n)
402-
{
403-
struct acpi_processor_cx *x = a, *y = b;
392+
for (i = 1; i < length; i++) {
393+
if (!states[i].valid)
394+
continue;
404395

405-
if (!(x->valid && y->valid))
406-
return;
407-
swap(x->latency, y->latency);
396+
for (j = i - 1, k = i; j >= 0; j--) {
397+
if (!states[j].valid)
398+
continue;
399+
400+
if (states[j].latency > states[k].latency)
401+
swap(states[j].latency, states[k].latency);
402+
403+
k = j;
404+
}
405+
}
408406
}
409407

410408
static int acpi_processor_power_verify(struct acpi_processor *pr)
@@ -449,10 +447,7 @@ static int acpi_processor_power_verify(struct acpi_processor *pr)
449447

450448
if (buggy_latency) {
451449
pr_notice("FW issue: working around C-state latencies out of order\n");
452-
sort(&pr->power.states[1], max_cstate,
453-
sizeof(struct acpi_processor_cx),
454-
acpi_cst_latency_cmp,
455-
acpi_cst_latency_swap);
450+
acpi_cst_latency_sort(&pr->power.states[1], max_cstate);
456451
}
457452

458453
lapic_timer_propagate_broadcast(pr);

0 commit comments

Comments
 (0)