Skip to content

Commit 890a079

Browse files
ubizjakIngo Molnar
authored andcommitted
x86/ACPI/boot: Use try_cmpxchg() in __acpi_{acquire,release}_global_lock()
Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in __acpi_{acquire,release}_global_lock(). x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after CMPXCHG (and related MOV instruction in front of CMPXCHG). Also, try_cmpxchg() implicitly assigns old *ptr value to "old" when CMPXCHG fails. There is no need to re-read the value in the loop. Note that the value from *ptr should be read using READ_ONCE() to prevent the compiler from merging, refetching or reordering the read. No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Rafael J. Wysocki <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 50fd4d5 commit 890a079

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

arch/x86/kernel/acpi/boot.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,23 +1840,23 @@ early_param("acpi_sci", setup_acpi_sci);
18401840

18411841
int __acpi_acquire_global_lock(unsigned int *lock)
18421842
{
1843-
unsigned int old, new, val;
1843+
unsigned int old, new;
1844+
1845+
old = READ_ONCE(*lock);
18441846
do {
1845-
old = *lock;
18461847
new = (((old & ~0x3) + 2) + ((old >> 1) & 0x1));
1847-
val = cmpxchg(lock, old, new);
1848-
} while (unlikely (val != old));
1848+
} while (!try_cmpxchg(lock, &old, new));
18491849
return ((new & 0x3) < 3) ? -1 : 0;
18501850
}
18511851

18521852
int __acpi_release_global_lock(unsigned int *lock)
18531853
{
1854-
unsigned int old, new, val;
1854+
unsigned int old, new;
1855+
1856+
old = READ_ONCE(*lock);
18551857
do {
1856-
old = *lock;
18571858
new = old & ~0x3;
1858-
val = cmpxchg(lock, old, new);
1859-
} while (unlikely (val != old));
1859+
} while (!try_cmpxchg(lock, &old, new));
18601860
return old & 0x1;
18611861
}
18621862

0 commit comments

Comments
 (0)