The per-cpu preempt count of x86 contains two values, the actual preempt
count and the inverted PREEMPT_NEED_RESCHED bit. If a corrupted preempt
count is detected the preempt_count_set function is used to reset the
preempt count.

In case the inverted PREEMPT_NEED_RESCHED bit is zero at the time of the
reset, the preemption indication is lost. Use raw_cpu_cmpxchg_4 to reset
only the count part and leave the PREEMPT_NEED_RESCHED bit as it is.

Signed-off-by: Martin Schwidefsky <[email protected]>
---
 arch/x86/include/asm/preempt.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/preempt.h b/arch/x86/include/asm/preempt.h
index 17f2186..ec1f3c6 100644
--- a/arch/x86/include/asm/preempt.h
+++ b/arch/x86/include/asm/preempt.h
@@ -24,7 +24,13 @@ static __always_inline int preempt_count(void)
 
 static __always_inline void preempt_count_set(int pc)
 {
-       raw_cpu_write_4(__preempt_count, pc);
+       int old, new;
+
+       do {
+               old = raw_cpu_read_4(__preempt_count);
+               new = (old & PREEMPT_NEED_RESCHED) |
+                       (pc & ~PREEMPT_NEED_RESCHED);
+       } while (raw_cpu_cmpxchg_4(__preempt_count, old, new) != old);
 }
 
 /*
-- 
1.9.1

Reply via email to