Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull locking updates from Ingo Molnar:
"The tree got pretty big in this development cycle, but the net effect
is pretty good:

115 files changed, 673 insertions(+), 1522 deletions(-)

The main changes were:

- Rework and generalize the mutex code to remove per arch mutex
primitives. (Peter Zijlstra)

- Add vCPU preemption support: add an interface to query the
preemption status of vCPUs and use it in locking primitives - this
optimizes paravirt performance. (Pan Xinhui, Juergen Gross,
Christian Borntraeger)

- Introduce cpu_relax_yield() and remov cpu_relax_lowlatency() to
clean up and improve the s390 lock yielding machinery and its core
kernel impact. (Christian Borntraeger)

- Micro-optimize mutexes some more. (Waiman Long)

- Reluctantly add the to-be-deprecated mutex_trylock_recursive()
interface on a temporary basis, to give the DRM code more time to
get rid of its locking hacks. Any other users will be NAK-ed on
sight. (We turned off the deprecation warning for the time being to
not pollute the build log.) (Peter Zijlstra)

- Improve the rtmutex code a bit, in light of recent long lived
bugs/races. (Thomas Gleixner)

- Misc fixes, cleanups"

* 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (36 commits)
x86/paravirt: Fix bool return type for PVOP_CALL()
x86/paravirt: Fix native_patch()
locking/ww_mutex: Use relaxed atomics
locking/rtmutex: Explain locking rules for rt_mutex_proxy_unlock()/init_proxy_locked()
locking/rtmutex: Get rid of RT_MUTEX_OWNER_MASKALL
x86/paravirt: Optimize native pv_lock_ops.vcpu_is_preempted()
locking/mutex: Break out of expensive busy-loop on {mutex,rwsem}_spin_on_owner() when owner vCPU is preempted
locking/osq: Break out of spin-wait busy waiting loop for a preempted vCPU in osq_lock()
Documentation/virtual/kvm: Support the vCPU preemption check
x86/xen: Support the vCPU preemption check
x86/kvm: Support the vCPU preemption check
x86/kvm: Support the vCPU preemption check
kvm: Introduce kvm_write_guest_offset_cached()
locking/core, x86/paravirt: Implement vcpu_is_preempted(cpu) for KVM and Xen guests
locking/spinlocks, s390: Implement vcpu_is_preempted(cpu)
locking/core, powerpc: Implement vcpu_is_preempted(cpu)
sched/core: Introduce the vcpu_is_preempted(cpu) interface
sched/wake_q: Rename WAKE_Q to DEFINE_WAKE_Q
locking/core: Provide common cpu_relax_yield() definition
locking/mutex: Don't mark mutex_trylock_recursive() as deprecated, temporarily
...

+678 -1527
+8 -1
Documentation/virtual/kvm/msr.txt
··· 208 208 __u64 steal; 209 209 __u32 version; 210 210 __u32 flags; 211 - __u32 pad[12]; 211 + __u8 preempted; 212 + __u8 u8_pad[3]; 213 + __u32 pad[11]; 212 214 } 213 215 214 216 whose data will be filled in by the hypervisor periodically. Only one ··· 233 231 steal: the amount of time in which this vCPU did not run, in 234 232 nanoseconds. Time during which the vcpu is idle, will not be 235 233 reported as steal time. 234 + 235 + preempted: indicate the vCPU who owns this struct is running or 236 + not. Non-zero values mean the vCPU has been preempted. Zero 237 + means the vCPU is not preempted. NOTE, it is always zero if the 238 + the hypervisor doesn't support this field. 236 239 237 240 MSR_KVM_EOI_EN: 0x4b564d04 238 241 data: Bit 0 is 1 when PV end of interrupt is enabled on the vcpu; 0
-9
arch/alpha/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/alpha/include/asm/processor.h
··· 58 58 ((tsk) == current ? rdusp() : task_thread_info(tsk)->pcb.usp) 59 59 60 60 #define cpu_relax() barrier() 61 - #define cpu_relax_lowlatency() cpu_relax() 62 61 63 62 #define ARCH_HAS_PREFETCH 64 63 #define ARCH_HAS_PREFETCHW
-18
arch/arc/include/asm/mutex.h
··· 1 - /* 2 - * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 3 - * 4 - * This program is free software; you can redistribute it and/or modify 5 - * it under the terms of the GNU General Public License version 2 as 6 - * published by the Free Software Foundation. 7 - */ 8 - 9 - /* 10 - * xchg() based mutex fast path maintains a state of 0 or 1, as opposed to 11 - * atomic dec based which can "count" any number of lock contenders. 12 - * This ideally needs to be fixed in core, but for now switching to dec ver. 13 - */ 14 - #if defined(CONFIG_SMP) && (CONFIG_NR_CPUS > 2) 15 - #include <asm-generic/mutex-dec.h> 16 - #else 17 - #include <asm-generic/mutex-xchg.h> 18 - #endif
-3
arch/arc/include/asm/processor.h
··· 60 60 #ifndef CONFIG_EZNPS_MTM_EXT 61 61 62 62 #define cpu_relax() barrier() 63 - #define cpu_relax_lowlatency() cpu_relax() 64 63 65 64 #else 66 65 67 66 #define cpu_relax() \ 68 67 __asm__ __volatile__ (".word %0" : : "i"(CTOP_INST_SCHD_RW) : "memory") 69 - 70 - #define cpu_relax_lowlatency() barrier() 71 68 72 69 #endif 73 70
-21
arch/arm/include/asm/mutex.h
··· 1 - /* 2 - * arch/arm/include/asm/mutex.h 3 - * 4 - * ARM optimized mutex locking primitives 5 - * 6 - * Please look into asm-generic/mutex-xchg.h for a formal definition. 7 - */ 8 - #ifndef _ASM_MUTEX_H 9 - #define _ASM_MUTEX_H 10 - /* 11 - * On pre-ARMv6 hardware this results in a swp-based implementation, 12 - * which is the most efficient. For ARMv6+, we have exclusive memory 13 - * accessors and use atomic_dec to avoid the extra xchg operations 14 - * on the locking slowpaths. 15 - */ 16 - #if __LINUX_ARM_ARCH__ < 6 17 - #include <asm-generic/mutex-xchg.h> 18 - #else 19 - #include <asm-generic/mutex-dec.h> 20 - #endif 21 - #endif /* _ASM_MUTEX_H */
-2
arch/arm/include/asm/processor.h
··· 82 82 #define cpu_relax() barrier() 83 83 #endif 84 84 85 - #define cpu_relax_lowlatency() cpu_relax() 86 - 87 85 #define task_pt_regs(p) \ 88 86 ((struct pt_regs *)(THREAD_START_SP + task_stack_page(p)) - 1) 89 87
-1
arch/arm64/include/asm/Kbuild
··· 24 24 generic-y += mman.h 25 25 generic-y += msgbuf.h 26 26 generic-y += msi.h 27 - generic-y += mutex.h 28 27 generic-y += poll.h 29 28 generic-y += preempt.h 30 29 generic-y += resource.h
-2
arch/arm64/include/asm/processor.h
··· 149 149 asm volatile("yield" ::: "memory"); 150 150 } 151 151 152 - #define cpu_relax_lowlatency() cpu_relax() 153 - 154 152 /* Thread switching */ 155 153 extern struct task_struct *cpu_switch_to(struct task_struct *prev, 156 154 struct task_struct *next);
-9
arch/avr32/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/avr32/include/asm/processor.h
··· 92 92 #define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3)) 93 93 94 94 #define cpu_relax() barrier() 95 - #define cpu_relax_lowlatency() cpu_relax() 96 95 #define cpu_sync_pipeline() asm volatile("sub pc, -2" : : : "memory") 97 96 98 97 struct cpu_context {
-1
arch/blackfin/include/asm/Kbuild
··· 24 24 generic-y += mm-arch-hooks.h 25 25 generic-y += mman.h 26 26 generic-y += msgbuf.h 27 - generic-y += mutex.h 28 27 generic-y += param.h 29 28 generic-y += percpu.h 30 29 generic-y += pgalloc.h
-1
arch/blackfin/include/asm/processor.h
··· 92 92 #define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->thread.usp) 93 93 94 94 #define cpu_relax() smp_mb() 95 - #define cpu_relax_lowlatency() cpu_relax() 96 95 97 96 /* Get the Silicon Revision of the chip */ 98 97 static inline uint32_t __pure bfin_revid(void)
-6
arch/c6x/include/asm/mutex.h
··· 1 - #ifndef _ASM_C6X_MUTEX_H 2 - #define _ASM_C6X_MUTEX_H 3 - 4 - #include <asm-generic/mutex-null.h> 5 - 6 - #endif /* _ASM_C6X_MUTEX_H */
-1
arch/c6x/include/asm/processor.h
··· 121 121 #define KSTK_ESP(task) (task_pt_regs(task)->sp) 122 122 123 123 #define cpu_relax() do { } while (0) 124 - #define cpu_relax_lowlatency() cpu_relax() 125 124 126 125 extern const struct seq_operations cpuinfo_op; 127 126
-9
arch/cris/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/cris/include/asm/processor.h
··· 63 63 #define init_stack (init_thread_union.stack) 64 64 65 65 #define cpu_relax() barrier() 66 - #define cpu_relax_lowlatency() cpu_relax() 67 66 68 67 void default_idle(void); 69 68
-9
arch/frv/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/frv/include/asm/processor.h
··· 107 107 #define KSTK_ESP(tsk) ((tsk)->thread.frame0->sp) 108 108 109 109 #define cpu_relax() barrier() 110 - #define cpu_relax_lowlatency() cpu_relax() 111 110 112 111 /* data cache prefetch */ 113 112 #define ARCH_HAS_PREFETCH
-9
arch/h8300/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/h8300/include/asm/processor.h
··· 127 127 #define KSTK_ESP(tsk) ((tsk) == current ? rdusp() : (tsk)->thread.usp) 128 128 129 129 #define cpu_relax() barrier() 130 - #define cpu_relax_lowlatency() cpu_relax() 131 130 132 131 #define HARD_RESET_NOW() ({ \ 133 132 local_irq_disable(); \
-8
arch/hexagon/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - #include <asm-generic/mutex-xchg.h>
-1
arch/hexagon/include/asm/processor.h
··· 56 56 } 57 57 58 58 #define cpu_relax() __vmyield() 59 - #define cpu_relax_lowlatency() cpu_relax() 60 59 61 60 /* 62 61 * Decides where the kernel will search for a free chunk of vm space during
-90
arch/ia64/include/asm/mutex.h
··· 1 - /* 2 - * ia64 implementation of the mutex fastpath. 3 - * 4 - * Copyright (C) 2006 Ken Chen <kenneth.w.chen@intel.com> 5 - * 6 - */ 7 - 8 - #ifndef _ASM_MUTEX_H 9 - #define _ASM_MUTEX_H 10 - 11 - /** 12 - * __mutex_fastpath_lock - try to take the lock by moving the count 13 - * from 1 to a 0 value 14 - * @count: pointer of type atomic_t 15 - * @fail_fn: function to call if the original value was not 1 16 - * 17 - * Change the count from 1 to a value lower than 1, and call <fail_fn> if 18 - * it wasn't 1 originally. This function MUST leave the value lower than 19 - * 1 even when the "1" assertion wasn't true. 20 - */ 21 - static inline void 22 - __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *)) 23 - { 24 - if (unlikely(ia64_fetchadd4_acq(count, -1) != 1)) 25 - fail_fn(count); 26 - } 27 - 28 - /** 29 - * __mutex_fastpath_lock_retval - try to take the lock by moving the count 30 - * from 1 to a 0 value 31 - * @count: pointer of type atomic_t 32 - * 33 - * Change the count from 1 to a value lower than 1. This function returns 0 34 - * if the fastpath succeeds, or -1 otherwise. 35 - */ 36 - static inline int 37 - __mutex_fastpath_lock_retval(atomic_t *count) 38 - { 39 - if (unlikely(ia64_fetchadd4_acq(count, -1) != 1)) 40 - return -1; 41 - return 0; 42 - } 43 - 44 - /** 45 - * __mutex_fastpath_unlock - try to promote the count from 0 to 1 46 - * @count: pointer of type atomic_t 47 - * @fail_fn: function to call if the original value was not 0 48 - * 49 - * Try to promote the count from 0 to 1. If it wasn't 0, call <fail_fn>. 50 - * In the failure case, this function is allowed to either set the value to 51 - * 1, or to set it to a value lower than 1. 52 - * 53 - * If the implementation sets it to a value of lower than 1, then the 54 - * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs 55 - * to return 0 otherwise. 56 - */ 57 - static inline void 58 - __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *)) 59 - { 60 - int ret = ia64_fetchadd4_rel(count, 1); 61 - if (unlikely(ret < 0)) 62 - fail_fn(count); 63 - } 64 - 65 - #define __mutex_slowpath_needs_to_unlock() 1 66 - 67 - /** 68 - * __mutex_fastpath_trylock - try to acquire the mutex, without waiting 69 - * 70 - * @count: pointer of type atomic_t 71 - * @fail_fn: fallback function 72 - * 73 - * Change the count from 1 to a value lower than 1, and return 0 (failure) 74 - * if it wasn't 1 originally, or return 1 (success) otherwise. This function 75 - * MUST leave the value lower than 1 even when the "1" assertion wasn't true. 76 - * Additionally, if the value was < 0 originally, this function must not leave 77 - * it to 0 on failure. 78 - * 79 - * If the architecture has no effective trylock variant, it should call the 80 - * <fail_fn> spinlock-based trylock variant unconditionally. 81 - */ 82 - static inline int 83 - __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *)) 84 - { 85 - if (atomic_read(count) == 1 && cmpxchg_acq(count, 1, 0) == 1) 86 - return 1; 87 - return 0; 88 - } 89 - 90 - #endif
-1
arch/ia64/include/asm/processor.h
··· 547 547 } 548 548 549 549 #define cpu_relax() ia64_hint(ia64_hint_pause) 550 - #define cpu_relax_lowlatency() cpu_relax() 551 550 552 551 static inline int 553 552 ia64_get_irr(unsigned int vector)
-9
arch/m32r/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/m32r/include/asm/processor.h
··· 133 133 #define KSTK_ESP(tsk) ((tsk)->thread.sp) 134 134 135 135 #define cpu_relax() barrier() 136 - #define cpu_relax_lowlatency() cpu_relax() 137 136 138 137 #endif /* _ASM_M32R_PROCESSOR_H */
-1
arch/m68k/include/asm/Kbuild
··· 20 20 generic-y += mcs_spinlock.h 21 21 generic-y += mm-arch-hooks.h 22 22 generic-y += mman.h 23 - generic-y += mutex.h 24 23 generic-y += percpu.h 25 24 generic-y += preempt.h 26 25 generic-y += resource.h
-1
arch/m68k/include/asm/processor.h
··· 156 156 #define task_pt_regs(tsk) ((struct pt_regs *) ((tsk)->thread.esp0)) 157 157 158 158 #define cpu_relax() barrier() 159 - #define cpu_relax_lowlatency() cpu_relax() 160 159 161 160 #endif
-1
arch/metag/include/asm/Kbuild
··· 27 27 generic-y += mcs_spinlock.h 28 28 generic-y += mm-arch-hooks.h 29 29 generic-y += msgbuf.h 30 - generic-y += mutex.h 31 30 generic-y += param.h 32 31 generic-y += pci.h 33 32 generic-y += percpu.h
-1
arch/metag/include/asm/processor.h
··· 152 152 #define user_stack_pointer(regs) ((regs)->ctx.AX[0].U0) 153 153 154 154 #define cpu_relax() barrier() 155 - #define cpu_relax_lowlatency() cpu_relax() 156 155 157 156 extern void setup_priv(void); 158 157
-1
arch/microblaze/include/asm/mutex.h
··· 1 - #include <asm-generic/mutex-dec.h>
-1
arch/microblaze/include/asm/processor.h
··· 22 22 extern const struct seq_operations cpuinfo_op; 23 23 24 24 # define cpu_relax() barrier() 25 - # define cpu_relax_lowlatency() cpu_relax() 26 25 27 26 #define task_pt_regs(tsk) \ 28 27 (((struct pt_regs *)(THREAD_SIZE + task_stack_page(tsk))) - 1)
-1
arch/mips/include/asm/Kbuild
··· 9 9 generic-y += local64.h 10 10 generic-y += mcs_spinlock.h 11 11 generic-y += mm-arch-hooks.h 12 - generic-y += mutex.h 13 12 generic-y += parport.h 14 13 generic-y += percpu.h 15 14 generic-y += preempt.h
-1
arch/mips/include/asm/processor.h
··· 389 389 #define KSTK_STATUS(tsk) (task_pt_regs(tsk)->cp0_status) 390 390 391 391 #define cpu_relax() barrier() 392 - #define cpu_relax_lowlatency() cpu_relax() 393 392 394 393 /* 395 394 * Return_address is a replacement for __builtin_return_address(count)
-16
arch/mn10300/include/asm/mutex.h
··· 1 - /* MN10300 Mutex fastpath 2 - * 3 - * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 4 - * Written by David Howells (dhowells@redhat.com) 5 - * 6 - * This program is free software; you can redistribute it and/or 7 - * modify it under the terms of the GNU General Public Licence 8 - * as published by the Free Software Foundation; either version 9 - * 2 of the Licence, or (at your option) any later version. 10 - * 11 - * 12 - * TODO: implement optimized primitives instead, or leave the generic 13 - * implementation in place, or pick the atomic_xchg() based generic 14 - * implementation. (see asm-generic/mutex-xchg.h for details) 15 - */ 16 - #include <asm-generic/mutex-null.h>
-1
arch/mn10300/include/asm/processor.h
··· 69 69 extern void dodgy_tsc(void); 70 70 71 71 #define cpu_relax() barrier() 72 - #define cpu_relax_lowlatency() cpu_relax() 73 72 74 73 /* 75 74 * User space process size: 1.75GB (default).
-1
arch/nios2/include/asm/mutex.h
··· 1 - #include <asm-generic/mutex-dec.h>
-1
arch/nios2/include/asm/processor.h
··· 88 88 #define KSTK_ESP(tsk) ((tsk)->thread.kregs->sp) 89 89 90 90 #define cpu_relax() barrier() 91 - #define cpu_relax_lowlatency() cpu_relax() 92 91 93 92 #endif /* __ASSEMBLY__ */ 94 93
-27
arch/openrisc/include/asm/mutex.h
··· 1 - /* 2 - * OpenRISC Linux 3 - * 4 - * Linux architectural port borrowing liberally from similar works of 5 - * others. All original copyrights apply as per the original source 6 - * declaration. 7 - * 8 - * OpenRISC implementation: 9 - * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com> 10 - * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se> 11 - * et al. 12 - * 13 - * This program is free software; you can redistribute it and/or modify 14 - * it under the terms of the GNU General Public License as published by 15 - * the Free Software Foundation; either version 2 of the License, or 16 - * (at your option) any later version. 17 - */ 18 - 19 - /* 20 - * Pull in the generic implementation for the mutex fastpath. 21 - * 22 - * TODO: implement optimized primitives instead, or leave the generic 23 - * implementation in place, or pick the atomic_xchg() based generic 24 - * implementation. (see asm-generic/mutex-xchg.h for details) 25 - */ 26 - 27 - #include <asm-generic/mutex-dec.h>
-1
arch/openrisc/include/asm/processor.h
··· 92 92 #define init_stack (init_thread_union.stack) 93 93 94 94 #define cpu_relax() barrier() 95 - #define cpu_relax_lowlatency() cpu_relax() 96 95 97 96 #endif /* __ASSEMBLY__ */ 98 97 #endif /* __ASM_OPENRISC_PROCESSOR_H */
-1
arch/parisc/include/asm/Kbuild
··· 16 16 generic-y += local64.h 17 17 generic-y += mcs_spinlock.h 18 18 generic-y += mm-arch-hooks.h 19 - generic-y += mutex.h 20 19 generic-y += param.h 21 20 generic-y += percpu.h 22 21 generic-y += poll.h
-1
arch/parisc/include/asm/processor.h
··· 309 309 #define KSTK_ESP(tsk) ((tsk)->thread.regs.gr[30]) 310 310 311 311 #define cpu_relax() barrier() 312 - #define cpu_relax_lowlatency() cpu_relax() 313 312 314 313 /* 315 314 * parisc_requires_coherency() is used to identify the combined VIPT/PIPT
-132
arch/powerpc/include/asm/mutex.h
··· 1 - /* 2 - * Optimised mutex implementation of include/asm-generic/mutex-dec.h algorithm 3 - */ 4 - #ifndef _ASM_POWERPC_MUTEX_H 5 - #define _ASM_POWERPC_MUTEX_H 6 - 7 - static inline int __mutex_cmpxchg_lock(atomic_t *v, int old, int new) 8 - { 9 - int t; 10 - 11 - __asm__ __volatile__ ( 12 - "1: lwarx %0,0,%1 # mutex trylock\n\ 13 - cmpw 0,%0,%2\n\ 14 - bne- 2f\n" 15 - PPC405_ERR77(0,%1) 16 - " stwcx. %3,0,%1\n\ 17 - bne- 1b" 18 - PPC_ACQUIRE_BARRIER 19 - "\n\ 20 - 2:" 21 - : "=&r" (t) 22 - : "r" (&v->counter), "r" (old), "r" (new) 23 - : "cc", "memory"); 24 - 25 - return t; 26 - } 27 - 28 - static inline int __mutex_dec_return_lock(atomic_t *v) 29 - { 30 - int t; 31 - 32 - __asm__ __volatile__( 33 - "1: lwarx %0,0,%1 # mutex lock\n\ 34 - addic %0,%0,-1\n" 35 - PPC405_ERR77(0,%1) 36 - " stwcx. %0,0,%1\n\ 37 - bne- 1b" 38 - PPC_ACQUIRE_BARRIER 39 - : "=&r" (t) 40 - : "r" (&v->counter) 41 - : "cc", "memory"); 42 - 43 - return t; 44 - } 45 - 46 - static inline int __mutex_inc_return_unlock(atomic_t *v) 47 - { 48 - int t; 49 - 50 - __asm__ __volatile__( 51 - PPC_RELEASE_BARRIER 52 - "1: lwarx %0,0,%1 # mutex unlock\n\ 53 - addic %0,%0,1\n" 54 - PPC405_ERR77(0,%1) 55 - " stwcx. %0,0,%1 \n\ 56 - bne- 1b" 57 - : "=&r" (t) 58 - : "r" (&v->counter) 59 - : "cc", "memory"); 60 - 61 - return t; 62 - } 63 - 64 - /** 65 - * __mutex_fastpath_lock - try to take the lock by moving the count 66 - * from 1 to a 0 value 67 - * @count: pointer of type atomic_t 68 - * @fail_fn: function to call if the original value was not 1 69 - * 70 - * Change the count from 1 to a value lower than 1, and call <fail_fn> if 71 - * it wasn't 1 originally. This function MUST leave the value lower than 72 - * 1 even when the "1" assertion wasn't true. 73 - */ 74 - static inline void 75 - __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *)) 76 - { 77 - if (unlikely(__mutex_dec_return_lock(count) < 0)) 78 - fail_fn(count); 79 - } 80 - 81 - /** 82 - * __mutex_fastpath_lock_retval - try to take the lock by moving the count 83 - * from 1 to a 0 value 84 - * @count: pointer of type atomic_t 85 - * 86 - * Change the count from 1 to a value lower than 1. This function returns 0 87 - * if the fastpath succeeds, or -1 otherwise. 88 - */ 89 - static inline int 90 - __mutex_fastpath_lock_retval(atomic_t *count) 91 - { 92 - if (unlikely(__mutex_dec_return_lock(count) < 0)) 93 - return -1; 94 - return 0; 95 - } 96 - 97 - /** 98 - * __mutex_fastpath_unlock - try to promote the count from 0 to 1 99 - * @count: pointer of type atomic_t 100 - * @fail_fn: function to call if the original value was not 0 101 - * 102 - * Try to promote the count from 0 to 1. If it wasn't 0, call <fail_fn>. 103 - * In the failure case, this function is allowed to either set the value to 104 - * 1, or to set it to a value lower than 1. 105 - */ 106 - static inline void 107 - __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *)) 108 - { 109 - if (unlikely(__mutex_inc_return_unlock(count) <= 0)) 110 - fail_fn(count); 111 - } 112 - 113 - #define __mutex_slowpath_needs_to_unlock() 1 114 - 115 - /** 116 - * __mutex_fastpath_trylock - try to acquire the mutex, without waiting 117 - * 118 - * @count: pointer of type atomic_t 119 - * @fail_fn: fallback function 120 - * 121 - * Change the count from 1 to 0, and return 1 (success), or if the count 122 - * was not 1, then return 0 (failure). 123 - */ 124 - static inline int 125 - __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *)) 126 - { 127 - if (likely(atomic_read(count) == 1 && __mutex_cmpxchg_lock(count, 1, 0) == 1)) 128 - return 1; 129 - return 0; 130 - } 131 - 132 - #endif
-2
arch/powerpc/include/asm/processor.h
··· 404 404 #define cpu_relax() barrier() 405 405 #endif 406 406 407 - #define cpu_relax_lowlatency() cpu_relax() 408 - 409 407 /* Check that a certain kernel stack pointer is valid in task_struct p */ 410 408 int validate_sp(unsigned long sp, struct task_struct *p, 411 409 unsigned long nbytes);
+8
arch/powerpc/include/asm/spinlock.h
··· 52 52 #define SYNC_IO 53 53 #endif 54 54 55 + #ifdef CONFIG_PPC_PSERIES 56 + #define vcpu_is_preempted vcpu_is_preempted 57 + static inline bool vcpu_is_preempted(int cpu) 58 + { 59 + return !!(be32_to_cpu(lppaca_of(cpu).yield_count) & 1); 60 + } 61 + #endif 62 + 55 63 static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) 56 64 { 57 65 return lock.slock == 0;
-9
arch/s390/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
+3 -2
arch/s390/include/asm/processor.h
··· 234 234 /* 235 235 * Give up the time slice of the virtual PU. 236 236 */ 237 - void cpu_relax(void); 237 + #define cpu_relax_yield cpu_relax_yield 238 + void cpu_relax_yield(void); 238 239 239 - #define cpu_relax_lowlatency() barrier() 240 + #define cpu_relax() barrier() 240 241 241 242 #define ECAG_CACHE_ATTRIBUTE 0 242 243 #define ECAG_CPU_ATTRIBUTE 1
+8
arch/s390/include/asm/spinlock.h
··· 23 23 return __sync_bool_compare_and_swap(lock, old, new); 24 24 } 25 25 26 + #ifndef CONFIG_SMP 27 + static inline bool arch_vcpu_is_preempted(int cpu) { return false; } 28 + #else 29 + bool arch_vcpu_is_preempted(int cpu); 30 + #endif 31 + 32 + #define vcpu_is_preempted arch_vcpu_is_preempted 33 + 26 34 /* 27 35 * Simple spin lock operations. There are two variants, one clears IRQ's 28 36 * on the local processor, one does not.
+2 -2
arch/s390/kernel/processor.c
··· 53 53 on_each_cpu(update_cpu_mhz, NULL, 0); 54 54 } 55 55 56 - void notrace cpu_relax(void) 56 + void notrace cpu_relax_yield(void) 57 57 { 58 58 if (!smp_cpu_mtid && MACHINE_HAS_DIAG44) { 59 59 diag_stat_inc(DIAG_STAT_X044); ··· 61 61 } 62 62 barrier(); 63 63 } 64 - EXPORT_SYMBOL(cpu_relax); 64 + EXPORT_SYMBOL(cpu_relax_yield); 65 65 66 66 /* 67 67 * cpu_init - initializes state that is per-CPU.
+7 -2
arch/s390/kernel/smp.c
··· 368 368 return -1; 369 369 } 370 370 371 - int smp_vcpu_scheduled(int cpu) 371 + bool arch_vcpu_is_preempted(int cpu) 372 372 { 373 - return pcpu_running(pcpu_devices + cpu); 373 + if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu)) 374 + return false; 375 + if (pcpu_running(pcpu_devices + cpu)) 376 + return false; 377 + return true; 374 378 } 379 + EXPORT_SYMBOL(arch_vcpu_is_preempted); 375 380 376 381 void smp_yield_cpu(int cpu) 377 382 {
+8 -17
arch/s390/lib/spinlock.c
··· 37 37 asm(".insn rsy,0xeb0000000022,%0,0,%1" : : "d" (old), "Q" (*lock)); 38 38 } 39 39 40 - static inline int cpu_is_preempted(int cpu) 41 - { 42 - if (test_cpu_flag_of(CIF_ENABLED_WAIT, cpu)) 43 - return 0; 44 - if (smp_vcpu_scheduled(cpu)) 45 - return 0; 46 - return 1; 47 - } 48 - 49 40 void arch_spin_lock_wait(arch_spinlock_t *lp) 50 41 { 51 42 unsigned int cpu = SPINLOCK_LOCKVAL; ··· 53 62 continue; 54 63 } 55 64 /* First iteration: check if the lock owner is running. */ 56 - if (first_diag && cpu_is_preempted(~owner)) { 65 + if (first_diag && arch_vcpu_is_preempted(~owner)) { 57 66 smp_yield_cpu(~owner); 58 67 first_diag = 0; 59 68 continue; ··· 72 81 * yield the CPU unconditionally. For LPAR rely on the 73 82 * sense running status. 74 83 */ 75 - if (!MACHINE_IS_LPAR || cpu_is_preempted(~owner)) { 84 + if (!MACHINE_IS_LPAR || arch_vcpu_is_preempted(~owner)) { 76 85 smp_yield_cpu(~owner); 77 86 first_diag = 0; 78 87 } ··· 99 108 continue; 100 109 } 101 110 /* Check if the lock owner is running. */ 102 - if (first_diag && cpu_is_preempted(~owner)) { 111 + if (first_diag && arch_vcpu_is_preempted(~owner)) { 103 112 smp_yield_cpu(~owner); 104 113 first_diag = 0; 105 114 continue; ··· 118 127 * yield the CPU unconditionally. For LPAR rely on the 119 128 * sense running status. 120 129 */ 121 - if (!MACHINE_IS_LPAR || cpu_is_preempted(~owner)) { 130 + if (!MACHINE_IS_LPAR || arch_vcpu_is_preempted(~owner)) { 122 131 smp_yield_cpu(~owner); 123 132 first_diag = 0; 124 133 } ··· 156 165 owner = 0; 157 166 while (1) { 158 167 if (count-- <= 0) { 159 - if (owner && cpu_is_preempted(~owner)) 168 + if (owner && arch_vcpu_is_preempted(~owner)) 160 169 smp_yield_cpu(~owner); 161 170 count = spin_retry; 162 171 } ··· 202 211 owner = 0; 203 212 while (1) { 204 213 if (count-- <= 0) { 205 - if (owner && cpu_is_preempted(~owner)) 214 + if (owner && arch_vcpu_is_preempted(~owner)) 206 215 smp_yield_cpu(~owner); 207 216 count = spin_retry; 208 217 } ··· 232 241 owner = 0; 233 242 while (1) { 234 243 if (count-- <= 0) { 235 - if (owner && cpu_is_preempted(~owner)) 244 + if (owner && arch_vcpu_is_preempted(~owner)) 236 245 smp_yield_cpu(~owner); 237 246 count = spin_retry; 238 247 } ··· 276 285 { 277 286 if (!cpu) 278 287 return; 279 - if (MACHINE_IS_LPAR && !cpu_is_preempted(~cpu)) 288 + if (MACHINE_IS_LPAR && !arch_vcpu_is_preempted(~cpu)) 280 289 return; 281 290 smp_yield_cpu(~cpu); 282 291 }
-6
arch/score/include/asm/mutex.h
··· 1 - #ifndef _ASM_SCORE_MUTEX_H 2 - #define _ASM_SCORE_MUTEX_H 3 - 4 - #include <asm-generic/mutex-dec.h> 5 - 6 - #endif /* _ASM_SCORE_MUTEX_H */
-1
arch/score/include/asm/processor.h
··· 24 24 #define current_text_addr() ({ __label__ _l; _l: &&_l; }) 25 25 26 26 #define cpu_relax() barrier() 27 - #define cpu_relax_lowlatency() cpu_relax() 28 27 #define release_thread(thread) do {} while (0) 29 28 30 29 /*
-109
arch/sh/include/asm/mutex-llsc.h
··· 1 - /* 2 - * arch/sh/include/asm/mutex-llsc.h 3 - * 4 - * SH-4A optimized mutex locking primitives 5 - * 6 - * Please look into asm-generic/mutex-xchg.h for a formal definition. 7 - */ 8 - #ifndef __ASM_SH_MUTEX_LLSC_H 9 - #define __ASM_SH_MUTEX_LLSC_H 10 - 11 - /* 12 - * Attempting to lock a mutex on SH4A is done like in ARMv6+ architecure. 13 - * with a bastardized atomic decrement (it is not a reliable atomic decrement 14 - * but it satisfies the defined semantics for our purpose, while being 15 - * smaller and faster than a real atomic decrement or atomic swap. 16 - * The idea is to attempt decrementing the lock value only once. If once 17 - * decremented it isn't zero, or if its store-back fails due to a dispute 18 - * on the exclusive store, we simply bail out immediately through the slow 19 - * path where the lock will be reattempted until it succeeds. 20 - */ 21 - static inline void 22 - __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *)) 23 - { 24 - int __done, __res; 25 - 26 - __asm__ __volatile__ ( 27 - "movli.l @%2, %0 \n" 28 - "add #-1, %0 \n" 29 - "movco.l %0, @%2 \n" 30 - "movt %1 \n" 31 - : "=&z" (__res), "=&r" (__done) 32 - : "r" (&(count)->counter) 33 - : "t"); 34 - 35 - if (unlikely(!__done || __res != 0)) 36 - fail_fn(count); 37 - } 38 - 39 - static inline int 40 - __mutex_fastpath_lock_retval(atomic_t *count) 41 - { 42 - int __done, __res; 43 - 44 - __asm__ __volatile__ ( 45 - "movli.l @%2, %0 \n" 46 - "add #-1, %0 \n" 47 - "movco.l %0, @%2 \n" 48 - "movt %1 \n" 49 - : "=&z" (__res), "=&r" (__done) 50 - : "r" (&(count)->counter) 51 - : "t"); 52 - 53 - if (unlikely(!__done || __res != 0)) 54 - __res = -1; 55 - 56 - return __res; 57 - } 58 - 59 - static inline void 60 - __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *)) 61 - { 62 - int __done, __res; 63 - 64 - __asm__ __volatile__ ( 65 - "movli.l @%2, %0 \n\t" 66 - "add #1, %0 \n\t" 67 - "movco.l %0, @%2 \n\t" 68 - "movt %1 \n\t" 69 - : "=&z" (__res), "=&r" (__done) 70 - : "r" (&(count)->counter) 71 - : "t"); 72 - 73 - if (unlikely(!__done || __res <= 0)) 74 - fail_fn(count); 75 - } 76 - 77 - /* 78 - * If the unlock was done on a contended lock, or if the unlock simply fails 79 - * then the mutex remains locked. 80 - */ 81 - #define __mutex_slowpath_needs_to_unlock() 1 82 - 83 - /* 84 - * For __mutex_fastpath_trylock we do an atomic decrement and check the 85 - * result and put it in the __res variable. 86 - */ 87 - static inline int 88 - __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *)) 89 - { 90 - int __res, __orig; 91 - 92 - __asm__ __volatile__ ( 93 - "1: movli.l @%2, %0 \n\t" 94 - "dt %0 \n\t" 95 - "movco.l %0,@%2 \n\t" 96 - "bf 1b \n\t" 97 - "cmp/eq #0,%0 \n\t" 98 - "bt 2f \n\t" 99 - "mov #0, %1 \n\t" 100 - "bf 3f \n\t" 101 - "2: mov #1, %1 \n\t" 102 - "3: " 103 - : "=&z" (__orig), "=&r" (__res) 104 - : "r" (&count->counter) 105 - : "t"); 106 - 107 - return __res; 108 - } 109 - #endif /* __ASM_SH_MUTEX_LLSC_H */
-12
arch/sh/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - #if defined(CONFIG_CPU_SH4A) 9 - #include <asm/mutex-llsc.h> 10 - #else 11 - #include <asm-generic/mutex-dec.h> 12 - #endif
-1
arch/sh/include/asm/processor.h
··· 97 97 98 98 #define cpu_sleep() __asm__ __volatile__ ("sleep" : : : "memory") 99 99 #define cpu_relax() barrier() 100 - #define cpu_relax_lowlatency() cpu_relax() 101 100 102 101 void default_idle(void); 103 102 void stop_this_cpu(void *);
-1
arch/sparc/include/asm/Kbuild
··· 15 15 generic-y += mcs_spinlock.h 16 16 generic-y += mm-arch-hooks.h 17 17 generic-y += module.h 18 - generic-y += mutex.h 19 18 generic-y += preempt.h 20 19 generic-y += rwsem.h 21 20 generic-y += serial.h
-1
arch/sparc/include/asm/processor_32.h
··· 119 119 int do_mathemu(struct pt_regs *regs, struct task_struct *fpt); 120 120 121 121 #define cpu_relax() barrier() 122 - #define cpu_relax_lowlatency() cpu_relax() 123 122 124 123 extern void (*sparc_idle)(void); 125 124
-1
arch/sparc/include/asm/processor_64.h
··· 216 216 "nop\n\t" \ 217 217 ".previous" \ 218 218 ::: "memory") 219 - #define cpu_relax_lowlatency() cpu_relax() 220 219 221 220 /* Prefetch support. This is tuned for UltraSPARC-III and later. 222 221 * UltraSPARC-I will treat these as nops, and UltraSPARC-II has
-1
arch/tile/include/asm/Kbuild
··· 21 21 generic-y += mcs_spinlock.h 22 22 generic-y += mm-arch-hooks.h 23 23 generic-y += msgbuf.h 24 - generic-y += mutex.h 25 24 generic-y += param.h 26 25 generic-y += parport.h 27 26 generic-y += poll.h
-2
arch/tile/include/asm/processor.h
··· 264 264 barrier(); 265 265 } 266 266 267 - #define cpu_relax_lowlatency() cpu_relax() 268 - 269 267 /* Info on this processor (see fs/proc/cpuinfo.c) */ 270 268 struct seq_operations; 271 269 extern const struct seq_operations cpuinfo_op;
-1
arch/um/include/asm/Kbuild
··· 17 17 generic-y += kdebug.h 18 18 generic-y += mcs_spinlock.h 19 19 generic-y += mm-arch-hooks.h 20 - generic-y += mutex.h 21 20 generic-y += param.h 22 21 generic-y += pci.h 23 22 generic-y += percpu.h
-20
arch/unicore32/include/asm/mutex.h
··· 1 - /* 2 - * linux/arch/unicore32/include/asm/mutex.h 3 - * 4 - * Code specific to PKUnity SoC and UniCore ISA 5 - * 6 - * Copyright (C) 2001-2010 GUAN Xue-tao 7 - * 8 - * This program is free software; you can redistribute it and/or modify 9 - * it under the terms of the GNU General Public License version 2 as 10 - * published by the Free Software Foundation. 11 - * 12 - * UniCore optimized mutex locking primitives 13 - * 14 - * Please look into asm-generic/mutex-xchg.h for a formal definition. 15 - */ 16 - #ifndef __UNICORE_MUTEX_H__ 17 - #define __UNICORE_MUTEX_H__ 18 - 19 - # include <asm-generic/mutex-xchg.h> 20 - #endif
-1
arch/unicore32/include/asm/processor.h
··· 71 71 unsigned long get_wchan(struct task_struct *p); 72 72 73 73 #define cpu_relax() barrier() 74 - #define cpu_relax_lowlatency() cpu_relax() 75 74 76 75 #define task_pt_regs(p) \ 77 76 ((struct pt_regs *)(THREAD_START_SP + task_stack_page(p)) - 1)
-5
arch/x86/include/asm/mutex.h
··· 1 - #ifdef CONFIG_X86_32 2 - # include <asm/mutex_32.h> 3 - #else 4 - # include <asm/mutex_64.h> 5 - #endif
-110
arch/x86/include/asm/mutex_32.h
··· 1 - /* 2 - * Assembly implementation of the mutex fastpath, based on atomic 3 - * decrement/increment. 4 - * 5 - * started by Ingo Molnar: 6 - * 7 - * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 8 - */ 9 - #ifndef _ASM_X86_MUTEX_32_H 10 - #define _ASM_X86_MUTEX_32_H 11 - 12 - #include <asm/alternative.h> 13 - 14 - /** 15 - * __mutex_fastpath_lock - try to take the lock by moving the count 16 - * from 1 to a 0 value 17 - * @count: pointer of type atomic_t 18 - * @fn: function to call if the original value was not 1 19 - * 20 - * Change the count from 1 to a value lower than 1, and call <fn> if it 21 - * wasn't 1 originally. This function MUST leave the value lower than 1 22 - * even when the "1" assertion wasn't true. 23 - */ 24 - #define __mutex_fastpath_lock(count, fail_fn) \ 25 - do { \ 26 - unsigned int dummy; \ 27 - \ 28 - typecheck(atomic_t *, count); \ 29 - typecheck_fn(void (*)(atomic_t *), fail_fn); \ 30 - \ 31 - asm volatile(LOCK_PREFIX " decl (%%eax)\n" \ 32 - " jns 1f \n" \ 33 - " call " #fail_fn "\n" \ 34 - "1:\n" \ 35 - : "=a" (dummy) \ 36 - : "a" (count) \ 37 - : "memory", "ecx", "edx"); \ 38 - } while (0) 39 - 40 - 41 - /** 42 - * __mutex_fastpath_lock_retval - try to take the lock by moving the count 43 - * from 1 to a 0 value 44 - * @count: pointer of type atomic_t 45 - * 46 - * Change the count from 1 to a value lower than 1. This function returns 0 47 - * if the fastpath succeeds, or -1 otherwise. 48 - */ 49 - static inline int __mutex_fastpath_lock_retval(atomic_t *count) 50 - { 51 - if (unlikely(atomic_dec_return(count) < 0)) 52 - return -1; 53 - else 54 - return 0; 55 - } 56 - 57 - /** 58 - * __mutex_fastpath_unlock - try to promote the mutex from 0 to 1 59 - * @count: pointer of type atomic_t 60 - * @fail_fn: function to call if the original value was not 0 61 - * 62 - * try to promote the mutex from 0 to 1. if it wasn't 0, call <fail_fn>. 63 - * In the failure case, this function is allowed to either set the value 64 - * to 1, or to set it to a value lower than 1. 65 - * 66 - * If the implementation sets it to a value of lower than 1, the 67 - * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs 68 - * to return 0 otherwise. 69 - */ 70 - #define __mutex_fastpath_unlock(count, fail_fn) \ 71 - do { \ 72 - unsigned int dummy; \ 73 - \ 74 - typecheck(atomic_t *, count); \ 75 - typecheck_fn(void (*)(atomic_t *), fail_fn); \ 76 - \ 77 - asm volatile(LOCK_PREFIX " incl (%%eax)\n" \ 78 - " jg 1f\n" \ 79 - " call " #fail_fn "\n" \ 80 - "1:\n" \ 81 - : "=a" (dummy) \ 82 - : "a" (count) \ 83 - : "memory", "ecx", "edx"); \ 84 - } while (0) 85 - 86 - #define __mutex_slowpath_needs_to_unlock() 1 87 - 88 - /** 89 - * __mutex_fastpath_trylock - try to acquire the mutex, without waiting 90 - * 91 - * @count: pointer of type atomic_t 92 - * @fail_fn: fallback function 93 - * 94 - * Change the count from 1 to a value lower than 1, and return 0 (failure) 95 - * if it wasn't 1 originally, or return 1 (success) otherwise. This function 96 - * MUST leave the value lower than 1 even when the "1" assertion wasn't true. 97 - * Additionally, if the value was < 0 originally, this function must not leave 98 - * it to 0 on failure. 99 - */ 100 - static inline int __mutex_fastpath_trylock(atomic_t *count, 101 - int (*fail_fn)(atomic_t *)) 102 - { 103 - /* cmpxchg because it never induces a false contention state. */ 104 - if (likely(atomic_read(count) == 1 && atomic_cmpxchg(count, 1, 0) == 1)) 105 - return 1; 106 - 107 - return 0; 108 - } 109 - 110 - #endif /* _ASM_X86_MUTEX_32_H */
-127
arch/x86/include/asm/mutex_64.h
··· 1 - /* 2 - * Assembly implementation of the mutex fastpath, based on atomic 3 - * decrement/increment. 4 - * 5 - * started by Ingo Molnar: 6 - * 7 - * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 8 - */ 9 - #ifndef _ASM_X86_MUTEX_64_H 10 - #define _ASM_X86_MUTEX_64_H 11 - 12 - /** 13 - * __mutex_fastpath_lock - decrement and call function if negative 14 - * @v: pointer of type atomic_t 15 - * @fail_fn: function to call if the result is negative 16 - * 17 - * Atomically decrements @v and calls <fail_fn> if the result is negative. 18 - */ 19 - #ifdef CC_HAVE_ASM_GOTO 20 - static inline void __mutex_fastpath_lock(atomic_t *v, 21 - void (*fail_fn)(atomic_t *)) 22 - { 23 - asm_volatile_goto(LOCK_PREFIX " decl %0\n" 24 - " jns %l[exit]\n" 25 - : : "m" (v->counter) 26 - : "memory", "cc" 27 - : exit); 28 - fail_fn(v); 29 - exit: 30 - return; 31 - } 32 - #else 33 - #define __mutex_fastpath_lock(v, fail_fn) \ 34 - do { \ 35 - unsigned long dummy; \ 36 - \ 37 - typecheck(atomic_t *, v); \ 38 - typecheck_fn(void (*)(atomic_t *), fail_fn); \ 39 - \ 40 - asm volatile(LOCK_PREFIX " decl (%%rdi)\n" \ 41 - " jns 1f \n" \ 42 - " call " #fail_fn "\n" \ 43 - "1:" \ 44 - : "=D" (dummy) \ 45 - : "D" (v) \ 46 - : "rax", "rsi", "rdx", "rcx", \ 47 - "r8", "r9", "r10", "r11", "memory"); \ 48 - } while (0) 49 - #endif 50 - 51 - /** 52 - * __mutex_fastpath_lock_retval - try to take the lock by moving the count 53 - * from 1 to a 0 value 54 - * @count: pointer of type atomic_t 55 - * 56 - * Change the count from 1 to a value lower than 1. This function returns 0 57 - * if the fastpath succeeds, or -1 otherwise. 58 - */ 59 - static inline int __mutex_fastpath_lock_retval(atomic_t *count) 60 - { 61 - if (unlikely(atomic_dec_return(count) < 0)) 62 - return -1; 63 - else 64 - return 0; 65 - } 66 - 67 - /** 68 - * __mutex_fastpath_unlock - increment and call function if nonpositive 69 - * @v: pointer of type atomic_t 70 - * @fail_fn: function to call if the result is nonpositive 71 - * 72 - * Atomically increments @v and calls <fail_fn> if the result is nonpositive. 73 - */ 74 - #ifdef CC_HAVE_ASM_GOTO 75 - static inline void __mutex_fastpath_unlock(atomic_t *v, 76 - void (*fail_fn)(atomic_t *)) 77 - { 78 - asm_volatile_goto(LOCK_PREFIX " incl %0\n" 79 - " jg %l[exit]\n" 80 - : : "m" (v->counter) 81 - : "memory", "cc" 82 - : exit); 83 - fail_fn(v); 84 - exit: 85 - return; 86 - } 87 - #else 88 - #define __mutex_fastpath_unlock(v, fail_fn) \ 89 - do { \ 90 - unsigned long dummy; \ 91 - \ 92 - typecheck(atomic_t *, v); \ 93 - typecheck_fn(void (*)(atomic_t *), fail_fn); \ 94 - \ 95 - asm volatile(LOCK_PREFIX " incl (%%rdi)\n" \ 96 - " jg 1f\n" \ 97 - " call " #fail_fn "\n" \ 98 - "1:" \ 99 - : "=D" (dummy) \ 100 - : "D" (v) \ 101 - : "rax", "rsi", "rdx", "rcx", \ 102 - "r8", "r9", "r10", "r11", "memory"); \ 103 - } while (0) 104 - #endif 105 - 106 - #define __mutex_slowpath_needs_to_unlock() 1 107 - 108 - /** 109 - * __mutex_fastpath_trylock - try to acquire the mutex, without waiting 110 - * 111 - * @count: pointer of type atomic_t 112 - * @fail_fn: fallback function 113 - * 114 - * Change the count from 1 to 0 and return 1 (success), or return 0 (failure) 115 - * if it wasn't 1 originally. [the fallback function is never used on 116 - * x86_64, because all x86_64 CPUs have a CMPXCHG instruction.] 117 - */ 118 - static inline int __mutex_fastpath_trylock(atomic_t *count, 119 - int (*fail_fn)(atomic_t *)) 120 - { 121 - if (likely(atomic_read(count) == 1 && atomic_cmpxchg(count, 1, 0) == 1)) 122 - return 1; 123 - 124 - return 0; 125 - } 126 - 127 - #endif /* _ASM_X86_MUTEX_64_H */
+5
arch/x86/include/asm/paravirt.h
··· 678 678 PVOP_VCALL1(pv_lock_ops.kick, cpu); 679 679 } 680 680 681 + static __always_inline bool pv_vcpu_is_preempted(int cpu) 682 + { 683 + return PVOP_CALLEE1(bool, pv_lock_ops.vcpu_is_preempted, cpu); 684 + } 685 + 681 686 #endif /* SMP && PARAVIRT_SPINLOCKS */ 682 687 683 688 #ifdef CONFIG_X86_32
+15 -1
arch/x86/include/asm/paravirt_types.h
··· 310 310 311 311 void (*wait)(u8 *ptr, u8 val); 312 312 void (*kick)(int cpu); 313 + 314 + struct paravirt_callee_save vcpu_is_preempted; 313 315 }; 314 316 315 317 /* This contains all the paravirt structures: we get a convenient ··· 510 508 #define PVOP_TEST_NULL(op) ((void)op) 511 509 #endif 512 510 511 + #define PVOP_RETMASK(rettype) \ 512 + ({ unsigned long __mask = ~0UL; \ 513 + switch (sizeof(rettype)) { \ 514 + case 1: __mask = 0xffUL; break; \ 515 + case 2: __mask = 0xffffUL; break; \ 516 + case 4: __mask = 0xffffffffUL; break; \ 517 + default: break; \ 518 + } \ 519 + __mask; \ 520 + }) 521 + 522 + 513 523 #define ____PVOP_CALL(rettype, op, clbr, call_clbr, extra_clbr, \ 514 524 pre, post, ...) \ 515 525 ({ \ ··· 549 535 paravirt_clobber(clbr), \ 550 536 ##__VA_ARGS__ \ 551 537 : "memory", "cc" extra_clbr); \ 552 - __ret = (rettype)__eax; \ 538 + __ret = (rettype)(__eax & PVOP_RETMASK(rettype)); \ 553 539 } \ 554 540 __ret; \ 555 541 })
-2
arch/x86/include/asm/processor.h
··· 588 588 rep_nop(); 589 589 } 590 590 591 - #define cpu_relax_lowlatency() cpu_relax() 592 - 593 591 /* Stop speculative execution and prefetching of modified code. */ 594 592 static inline void sync_core(void) 595 593 {
+6
arch/x86/include/asm/qspinlock.h
··· 32 32 { 33 33 pv_queued_spin_unlock(lock); 34 34 } 35 + 36 + #define vcpu_is_preempted vcpu_is_preempted 37 + static inline bool vcpu_is_preempted(int cpu) 38 + { 39 + return pv_vcpu_is_preempted(cpu); 40 + } 35 41 #else 36 42 static inline void queued_spin_unlock(struct qspinlock *lock) 37 43 {
+3 -1
arch/x86/include/uapi/asm/kvm_para.h
··· 45 45 __u64 steal; 46 46 __u32 version; 47 47 __u32 flags; 48 - __u32 pad[12]; 48 + __u8 preempted; 49 + __u8 u8_pad[3]; 50 + __u32 pad[11]; 49 51 }; 50 52 51 53 #define KVM_STEAL_ALIGNMENT_BITS 5
+13
arch/x86/kernel/kvm.c
··· 592 592 local_irq_restore(flags); 593 593 } 594 594 595 + __visible bool __kvm_vcpu_is_preempted(int cpu) 596 + { 597 + struct kvm_steal_time *src = &per_cpu(steal_time, cpu); 598 + 599 + return !!src->preempted; 600 + } 601 + PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted); 602 + 595 603 /* 596 604 * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present. 597 605 */ ··· 616 608 pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock); 617 609 pv_lock_ops.wait = kvm_wait; 618 610 pv_lock_ops.kick = kvm_kick_cpu; 611 + 612 + if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) { 613 + pv_lock_ops.vcpu_is_preempted = 614 + PV_CALLEE_SAVE(__kvm_vcpu_is_preempted); 615 + } 619 616 } 620 617 621 618 static __init int kvm_spinlock_init_jump(void)
+13 -1
arch/x86/kernel/paravirt-spinlocks.c
··· 12 12 { 13 13 native_queued_spin_unlock(lock); 14 14 } 15 - 16 15 PV_CALLEE_SAVE_REGS_THUNK(__native_queued_spin_unlock); 17 16 18 17 bool pv_is_native_spin_unlock(void) ··· 20 21 __raw_callee_save___native_queued_spin_unlock; 21 22 } 22 23 24 + __visible bool __native_vcpu_is_preempted(int cpu) 25 + { 26 + return false; 27 + } 28 + PV_CALLEE_SAVE_REGS_THUNK(__native_vcpu_is_preempted); 29 + 30 + bool pv_is_native_vcpu_is_preempted(void) 31 + { 32 + return pv_lock_ops.vcpu_is_preempted.func == 33 + __raw_callee_save___native_vcpu_is_preempted; 34 + } 35 + 23 36 struct pv_lock_ops pv_lock_ops = { 24 37 #ifdef CONFIG_SMP 25 38 .queued_spin_lock_slowpath = native_queued_spin_lock_slowpath, 26 39 .queued_spin_unlock = PV_CALLEE_SAVE(__native_queued_spin_unlock), 27 40 .wait = paravirt_nop, 28 41 .kick = paravirt_nop, 42 + .vcpu_is_preempted = PV_CALLEE_SAVE(__native_vcpu_is_preempted), 29 43 #endif /* SMP */ 30 44 }; 31 45 EXPORT_SYMBOL(pv_lock_ops);
+12
arch/x86/kernel/paravirt_patch_32.c
··· 12 12 13 13 #if defined(CONFIG_PARAVIRT_SPINLOCKS) 14 14 DEF_NATIVE(pv_lock_ops, queued_spin_unlock, "movb $0, (%eax)"); 15 + DEF_NATIVE(pv_lock_ops, vcpu_is_preempted, "xor %eax, %eax"); 15 16 #endif 16 17 17 18 unsigned paravirt_patch_ident_32(void *insnbuf, unsigned len) ··· 28 27 } 29 28 30 29 extern bool pv_is_native_spin_unlock(void); 30 + extern bool pv_is_native_vcpu_is_preempted(void); 31 31 32 32 unsigned native_patch(u8 type, u16 clobbers, void *ibuf, 33 33 unsigned long addr, unsigned len) ··· 58 56 end = end_pv_lock_ops_queued_spin_unlock; 59 57 goto patch_site; 60 58 } 59 + goto patch_default; 60 + 61 + case PARAVIRT_PATCH(pv_lock_ops.vcpu_is_preempted): 62 + if (pv_is_native_vcpu_is_preempted()) { 63 + start = start_pv_lock_ops_vcpu_is_preempted; 64 + end = end_pv_lock_ops_vcpu_is_preempted; 65 + goto patch_site; 66 + } 67 + goto patch_default; 61 68 #endif 62 69 63 70 default: 71 + patch_default: 64 72 ret = paravirt_patch_default(type, clobbers, ibuf, addr, len); 65 73 break; 66 74
+12
arch/x86/kernel/paravirt_patch_64.c
··· 21 21 22 22 #if defined(CONFIG_PARAVIRT_SPINLOCKS) 23 23 DEF_NATIVE(pv_lock_ops, queued_spin_unlock, "movb $0, (%rdi)"); 24 + DEF_NATIVE(pv_lock_ops, vcpu_is_preempted, "xor %rax, %rax"); 24 25 #endif 25 26 26 27 unsigned paravirt_patch_ident_32(void *insnbuf, unsigned len) ··· 37 36 } 38 37 39 38 extern bool pv_is_native_spin_unlock(void); 39 + extern bool pv_is_native_vcpu_is_preempted(void); 40 40 41 41 unsigned native_patch(u8 type, u16 clobbers, void *ibuf, 42 42 unsigned long addr, unsigned len) ··· 70 68 end = end_pv_lock_ops_queued_spin_unlock; 71 69 goto patch_site; 72 70 } 71 + goto patch_default; 72 + 73 + case PARAVIRT_PATCH(pv_lock_ops.vcpu_is_preempted): 74 + if (pv_is_native_vcpu_is_preempted()) { 75 + start = start_pv_lock_ops_vcpu_is_preempted; 76 + end = end_pv_lock_ops_vcpu_is_preempted; 77 + goto patch_site; 78 + } 79 + goto patch_default; 73 80 #endif 74 81 75 82 default: 83 + patch_default: 76 84 ret = paravirt_patch_default(type, clobbers, ibuf, addr, len); 77 85 break; 78 86
+16
arch/x86/kvm/x86.c
··· 2071 2071 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) 2072 2072 return; 2073 2073 2074 + vcpu->arch.st.steal.preempted = 0; 2075 + 2074 2076 if (vcpu->arch.st.steal.version & 1) 2075 2077 vcpu->arch.st.steal.version += 1; /* first time write, random junk */ 2076 2078 ··· 2828 2826 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); 2829 2827 } 2830 2828 2829 + static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) 2830 + { 2831 + if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) 2832 + return; 2833 + 2834 + vcpu->arch.st.steal.preempted = 1; 2835 + 2836 + kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime, 2837 + &vcpu->arch.st.steal.preempted, 2838 + offsetof(struct kvm_steal_time, preempted), 2839 + sizeof(vcpu->arch.st.steal.preempted)); 2840 + } 2841 + 2831 2842 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 2832 2843 { 2844 + kvm_steal_time_set_preempted(vcpu); 2833 2845 kvm_x86_ops->vcpu_put(vcpu); 2834 2846 kvm_put_guest_fpu(vcpu); 2835 2847 vcpu->arch.last_host_tsc = rdtsc();
-1
arch/x86/um/asm/processor.h
··· 26 26 } 27 27 28 28 #define cpu_relax() rep_nop() 29 - #define cpu_relax_lowlatency() cpu_relax() 30 29 31 30 #define task_pt_regs(t) (&(t)->thread.regs) 32 31
+2
arch/x86/xen/spinlock.c
··· 114 114 per_cpu(irq_name, cpu) = NULL; 115 115 } 116 116 117 + PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen); 117 118 118 119 /* 119 120 * Our init of PV spinlocks is split in two init functions due to us ··· 138 137 pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock); 139 138 pv_lock_ops.wait = xen_qlock_wait; 140 139 pv_lock_ops.kick = xen_qlock_kick; 140 + pv_lock_ops.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen); 141 141 } 142 142 143 143 /*
-9
arch/xtensa/include/asm/mutex.h
··· 1 - /* 2 - * Pull in the generic implementation for the mutex fastpath. 3 - * 4 - * TODO: implement optimized primitives instead, or leave the generic 5 - * implementation in place, or pick the atomic_xchg() based generic 6 - * implementation. (see asm-generic/mutex-xchg.h for details) 7 - */ 8 - 9 - #include <asm-generic/mutex-dec.h>
-1
arch/xtensa/include/asm/processor.h
··· 206 206 #define KSTK_ESP(tsk) (task_pt_regs(tsk)->areg[1]) 207 207 208 208 #define cpu_relax() barrier() 209 - #define cpu_relax_lowlatency() cpu_relax() 210 209 211 210 /* Special register access. */ 212 211
+1 -1
drivers/gpu/drm/i915/i915_gem_request.c
··· 723 723 if (busywait_stop(timeout_us, cpu)) 724 724 break; 725 725 726 - cpu_relax_lowlatency(); 726 + cpu_relax(); 727 727 } while (!need_resched()); 728 728 729 729 return false;
+11 -19
drivers/gpu/drm/i915/i915_gem_shrinker.c
··· 35 35 #include "i915_drv.h" 36 36 #include "i915_trace.h" 37 37 38 - static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task) 39 - { 40 - if (!mutex_is_locked(mutex)) 41 - return false; 42 - 43 - #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER) 44 - return mutex->owner == task; 45 - #else 46 - /* Since UP may be pre-empted, we cannot assume that we own the lock */ 47 - return false; 48 - #endif 49 - } 50 - 51 38 static bool any_vma_pinned(struct drm_i915_gem_object *obj) 52 39 { 53 40 struct i915_vma *vma; ··· 227 240 228 241 static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock) 229 242 { 230 - if (!mutex_trylock(&dev->struct_mutex)) { 231 - if (!mutex_is_locked_by(&dev->struct_mutex, current)) 232 - return false; 243 + switch (mutex_trylock_recursive(&dev->struct_mutex)) { 244 + case MUTEX_TRYLOCK_FAILED: 245 + return false; 233 246 234 - *unlock = false; 235 - } else 247 + case MUTEX_TRYLOCK_SUCCESS: 236 248 *unlock = true; 249 + return true; 237 250 238 - return true; 251 + case MUTEX_TRYLOCK_RECURSIVE: 252 + *unlock = false; 253 + return true; 254 + } 255 + 256 + BUG(); 239 257 } 240 258 241 259 static unsigned long
+11 -20
drivers/gpu/drm/msm/msm_gem_shrinker.c
··· 18 18 #include "msm_drv.h" 19 19 #include "msm_gem.h" 20 20 21 - static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task) 22 - { 23 - if (!mutex_is_locked(mutex)) 24 - return false; 25 - 26 - #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES) 27 - return mutex->owner == task; 28 - #else 29 - /* Since UP may be pre-empted, we cannot assume that we own the lock */ 30 - return false; 31 - #endif 32 - } 33 - 34 21 static bool msm_gem_shrinker_lock(struct drm_device *dev, bool *unlock) 35 22 { 36 - if (!mutex_trylock(&dev->struct_mutex)) { 37 - if (!mutex_is_locked_by(&dev->struct_mutex, current)) 38 - return false; 39 - *unlock = false; 40 - } else { 23 + switch (mutex_trylock_recursive(&dev->struct_mutex)) { 24 + case MUTEX_TRYLOCK_FAILED: 25 + return false; 26 + 27 + case MUTEX_TRYLOCK_SUCCESS: 41 28 *unlock = true; 29 + return true; 30 + 31 + case MUTEX_TRYLOCK_RECURSIVE: 32 + *unlock = false; 33 + return true; 42 34 } 43 35 44 - return true; 36 + BUG(); 45 37 } 46 - 47 38 48 39 static unsigned long 49 40 msm_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
+2 -2
drivers/vhost/net.c
··· 342 342 endtime = busy_clock() + vq->busyloop_timeout; 343 343 while (vhost_can_busy_poll(vq->dev, endtime) && 344 344 vhost_vq_avail_empty(vq->dev, vq)) 345 - cpu_relax_lowlatency(); 345 + cpu_relax(); 346 346 preempt_enable(); 347 347 r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov), 348 348 out_num, in_num, NULL, NULL); ··· 533 533 while (vhost_can_busy_poll(&net->dev, endtime) && 534 534 !sk_has_rx_data(sk) && 535 535 vhost_vq_avail_empty(&net->dev, vq)) 536 - cpu_relax_lowlatency(); 536 + cpu_relax(); 537 537 538 538 preempt_enable(); 539 539
-88
include/asm-generic/mutex-dec.h
··· 1 - /* 2 - * include/asm-generic/mutex-dec.h 3 - * 4 - * Generic implementation of the mutex fastpath, based on atomic 5 - * decrement/increment. 6 - */ 7 - #ifndef _ASM_GENERIC_MUTEX_DEC_H 8 - #define _ASM_GENERIC_MUTEX_DEC_H 9 - 10 - /** 11 - * __mutex_fastpath_lock - try to take the lock by moving the count 12 - * from 1 to a 0 value 13 - * @count: pointer of type atomic_t 14 - * @fail_fn: function to call if the original value was not 1 15 - * 16 - * Change the count from 1 to a value lower than 1, and call <fail_fn> if 17 - * it wasn't 1 originally. This function MUST leave the value lower than 18 - * 1 even when the "1" assertion wasn't true. 19 - */ 20 - static inline void 21 - __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *)) 22 - { 23 - if (unlikely(atomic_dec_return_acquire(count) < 0)) 24 - fail_fn(count); 25 - } 26 - 27 - /** 28 - * __mutex_fastpath_lock_retval - try to take the lock by moving the count 29 - * from 1 to a 0 value 30 - * @count: pointer of type atomic_t 31 - * 32 - * Change the count from 1 to a value lower than 1. This function returns 0 33 - * if the fastpath succeeds, or -1 otherwise. 34 - */ 35 - static inline int 36 - __mutex_fastpath_lock_retval(atomic_t *count) 37 - { 38 - if (unlikely(atomic_dec_return_acquire(count) < 0)) 39 - return -1; 40 - return 0; 41 - } 42 - 43 - /** 44 - * __mutex_fastpath_unlock - try to promote the count from 0 to 1 45 - * @count: pointer of type atomic_t 46 - * @fail_fn: function to call if the original value was not 0 47 - * 48 - * Try to promote the count from 0 to 1. If it wasn't 0, call <fail_fn>. 49 - * In the failure case, this function is allowed to either set the value to 50 - * 1, or to set it to a value lower than 1. 51 - * 52 - * If the implementation sets it to a value of lower than 1, then the 53 - * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs 54 - * to return 0 otherwise. 55 - */ 56 - static inline void 57 - __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *)) 58 - { 59 - if (unlikely(atomic_inc_return_release(count) <= 0)) 60 - fail_fn(count); 61 - } 62 - 63 - #define __mutex_slowpath_needs_to_unlock() 1 64 - 65 - /** 66 - * __mutex_fastpath_trylock - try to acquire the mutex, without waiting 67 - * 68 - * @count: pointer of type atomic_t 69 - * @fail_fn: fallback function 70 - * 71 - * Change the count from 1 to a value lower than 1, and return 0 (failure) 72 - * if it wasn't 1 originally, or return 1 (success) otherwise. This function 73 - * MUST leave the value lower than 1 even when the "1" assertion wasn't true. 74 - * Additionally, if the value was < 0 originally, this function must not leave 75 - * it to 0 on failure. 76 - * 77 - * If the architecture has no effective trylock variant, it should call the 78 - * <fail_fn> spinlock-based trylock variant unconditionally. 79 - */ 80 - static inline int 81 - __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *)) 82 - { 83 - if (likely(atomic_read(count) == 1 && atomic_cmpxchg_acquire(count, 1, 0) == 1)) 84 - return 1; 85 - return 0; 86 - } 87 - 88 - #endif
-19
include/asm-generic/mutex-null.h
··· 1 - /* 2 - * include/asm-generic/mutex-null.h 3 - * 4 - * Generic implementation of the mutex fastpath, based on NOP :-) 5 - * 6 - * This is used by the mutex-debugging infrastructure, but it can also 7 - * be used by architectures that (for whatever reason) want to use the 8 - * spinlock based slowpath. 9 - */ 10 - #ifndef _ASM_GENERIC_MUTEX_NULL_H 11 - #define _ASM_GENERIC_MUTEX_NULL_H 12 - 13 - #define __mutex_fastpath_lock(count, fail_fn) fail_fn(count) 14 - #define __mutex_fastpath_lock_retval(count) (-1) 15 - #define __mutex_fastpath_unlock(count, fail_fn) fail_fn(count) 16 - #define __mutex_fastpath_trylock(count, fail_fn) fail_fn(count) 17 - #define __mutex_slowpath_needs_to_unlock() 1 18 - 19 - #endif
-120
include/asm-generic/mutex-xchg.h
··· 1 - /* 2 - * include/asm-generic/mutex-xchg.h 3 - * 4 - * Generic implementation of the mutex fastpath, based on xchg(). 5 - * 6 - * NOTE: An xchg based implementation might be less optimal than an atomic 7 - * decrement/increment based implementation. If your architecture 8 - * has a reasonable atomic dec/inc then you should probably use 9 - * asm-generic/mutex-dec.h instead, or you could open-code an 10 - * optimized version in asm/mutex.h. 11 - */ 12 - #ifndef _ASM_GENERIC_MUTEX_XCHG_H 13 - #define _ASM_GENERIC_MUTEX_XCHG_H 14 - 15 - /** 16 - * __mutex_fastpath_lock - try to take the lock by moving the count 17 - * from 1 to a 0 value 18 - * @count: pointer of type atomic_t 19 - * @fail_fn: function to call if the original value was not 1 20 - * 21 - * Change the count from 1 to a value lower than 1, and call <fail_fn> if it 22 - * wasn't 1 originally. This function MUST leave the value lower than 1 23 - * even when the "1" assertion wasn't true. 24 - */ 25 - static inline void 26 - __mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *)) 27 - { 28 - if (unlikely(atomic_xchg(count, 0) != 1)) 29 - /* 30 - * We failed to acquire the lock, so mark it contended 31 - * to ensure that any waiting tasks are woken up by the 32 - * unlock slow path. 33 - */ 34 - if (likely(atomic_xchg_acquire(count, -1) != 1)) 35 - fail_fn(count); 36 - } 37 - 38 - /** 39 - * __mutex_fastpath_lock_retval - try to take the lock by moving the count 40 - * from 1 to a 0 value 41 - * @count: pointer of type atomic_t 42 - * 43 - * Change the count from 1 to a value lower than 1. This function returns 0 44 - * if the fastpath succeeds, or -1 otherwise. 45 - */ 46 - static inline int 47 - __mutex_fastpath_lock_retval(atomic_t *count) 48 - { 49 - if (unlikely(atomic_xchg_acquire(count, 0) != 1)) 50 - if (likely(atomic_xchg(count, -1) != 1)) 51 - return -1; 52 - return 0; 53 - } 54 - 55 - /** 56 - * __mutex_fastpath_unlock - try to promote the mutex from 0 to 1 57 - * @count: pointer of type atomic_t 58 - * @fail_fn: function to call if the original value was not 0 59 - * 60 - * try to promote the mutex from 0 to 1. if it wasn't 0, call <function> 61 - * In the failure case, this function is allowed to either set the value to 62 - * 1, or to set it to a value lower than one. 63 - * If the implementation sets it to a value of lower than one, the 64 - * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs 65 - * to return 0 otherwise. 66 - */ 67 - static inline void 68 - __mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *)) 69 - { 70 - if (unlikely(atomic_xchg_release(count, 1) != 0)) 71 - fail_fn(count); 72 - } 73 - 74 - #define __mutex_slowpath_needs_to_unlock() 0 75 - 76 - /** 77 - * __mutex_fastpath_trylock - try to acquire the mutex, without waiting 78 - * 79 - * @count: pointer of type atomic_t 80 - * @fail_fn: spinlock based trylock implementation 81 - * 82 - * Change the count from 1 to a value lower than 1, and return 0 (failure) 83 - * if it wasn't 1 originally, or return 1 (success) otherwise. This function 84 - * MUST leave the value lower than 1 even when the "1" assertion wasn't true. 85 - * Additionally, if the value was < 0 originally, this function must not leave 86 - * it to 0 on failure. 87 - * 88 - * If the architecture has no effective trylock variant, it should call the 89 - * <fail_fn> spinlock-based trylock variant unconditionally. 90 - */ 91 - static inline int 92 - __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *)) 93 - { 94 - int prev; 95 - 96 - if (atomic_read(count) != 1) 97 - return 0; 98 - 99 - prev = atomic_xchg_acquire(count, 0); 100 - if (unlikely(prev < 0)) { 101 - /* 102 - * The lock was marked contended so we must restore that 103 - * state. If while doing so we get back a prev value of 1 104 - * then we just own it. 105 - * 106 - * [ In the rare case of the mutex going to 1, to 0, to -1 107 - * and then back to 0 in this few-instructions window, 108 - * this has the potential to trigger the slowpath for the 109 - * owner's unlock path needlessly, but that's not a problem 110 - * in practice. ] 111 - */ 112 - prev = atomic_xchg_acquire(count, prev); 113 - if (prev < 0) 114 - prev = 0; 115 - } 116 - 117 - return prev; 118 - } 119 - 120 - #endif
-9
include/asm-generic/mutex.h
··· 1 - #ifndef __ASM_GENERIC_MUTEX_H 2 - #define __ASM_GENERIC_MUTEX_H 3 - /* 4 - * Pull in the generic implementation for the mutex fastpath, 5 - * which is a reasonable default on many architectures. 6 - */ 7 - 8 - #include <asm-generic/mutex-dec.h> 9 - #endif /* __ASM_GENERIC_MUTEX_H */
+2
include/linux/kvm_host.h
··· 645 645 unsigned long len); 646 646 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 647 647 void *data, unsigned long len); 648 + int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 649 + void *data, int offset, unsigned long len); 648 650 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 649 651 gpa_t gpa, unsigned long len); 650 652 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
-24
include/linux/mutex-debug.h
··· 1 - #ifndef __LINUX_MUTEX_DEBUG_H 2 - #define __LINUX_MUTEX_DEBUG_H 3 - 4 - #include <linux/linkage.h> 5 - #include <linux/lockdep.h> 6 - #include <linux/debug_locks.h> 7 - 8 - /* 9 - * Mutexes - debugging helpers: 10 - */ 11 - 12 - #define __DEBUG_MUTEX_INITIALIZER(lockname) \ 13 - , .magic = &lockname 14 - 15 - #define mutex_init(mutex) \ 16 - do { \ 17 - static struct lock_class_key __key; \ 18 - \ 19 - __mutex_init((mutex), #mutex, &__key); \ 20 - } while (0) 21 - 22 - extern void mutex_destroy(struct mutex *lock); 23 - 24 - #endif
+61 -16
include/linux/mutex.h
··· 18 18 #include <linux/atomic.h> 19 19 #include <asm/processor.h> 20 20 #include <linux/osq_lock.h> 21 + #include <linux/debug_locks.h> 21 22 22 23 /* 23 24 * Simple, straightforward mutexes with strict semantics: ··· 49 48 * locks and tasks (and only those tasks) 50 49 */ 51 50 struct mutex { 52 - /* 1: unlocked, 0: locked, negative: locked, possible waiters */ 53 - atomic_t count; 51 + atomic_long_t owner; 54 52 spinlock_t wait_lock; 55 - struct list_head wait_list; 56 - #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER) 57 - struct task_struct *owner; 58 - #endif 59 53 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 60 54 struct optimistic_spin_queue osq; /* Spinner MCS lock */ 61 55 #endif 56 + struct list_head wait_list; 62 57 #ifdef CONFIG_DEBUG_MUTEXES 63 58 void *magic; 64 59 #endif ··· 62 65 struct lockdep_map dep_map; 63 66 #endif 64 67 }; 68 + 69 + static inline struct task_struct *__mutex_owner(struct mutex *lock) 70 + { 71 + return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x03); 72 + } 65 73 66 74 /* 67 75 * This is the control structure for tasks blocked on mutex, ··· 81 79 }; 82 80 83 81 #ifdef CONFIG_DEBUG_MUTEXES 84 - # include <linux/mutex-debug.h> 82 + 83 + #define __DEBUG_MUTEX_INITIALIZER(lockname) \ 84 + , .magic = &lockname 85 + 86 + extern void mutex_destroy(struct mutex *lock); 87 + 85 88 #else 89 + 86 90 # define __DEBUG_MUTEX_INITIALIZER(lockname) 91 + 92 + static inline void mutex_destroy(struct mutex *lock) {} 93 + 94 + #endif 95 + 87 96 /** 88 97 * mutex_init - initialize the mutex 89 98 * @mutex: the mutex to be initialized ··· 103 90 * 104 91 * It is not allowed to initialize an already locked mutex. 105 92 */ 106 - # define mutex_init(mutex) \ 107 - do { \ 108 - static struct lock_class_key __key; \ 109 - \ 110 - __mutex_init((mutex), #mutex, &__key); \ 93 + #define mutex_init(mutex) \ 94 + do { \ 95 + static struct lock_class_key __key; \ 96 + \ 97 + __mutex_init((mutex), #mutex, &__key); \ 111 98 } while (0) 112 - static inline void mutex_destroy(struct mutex *lock) {} 113 - #endif 114 99 115 100 #ifdef CONFIG_DEBUG_LOCK_ALLOC 116 101 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \ ··· 118 107 #endif 119 108 120 109 #define __MUTEX_INITIALIZER(lockname) \ 121 - { .count = ATOMIC_INIT(1) \ 110 + { .owner = ATOMIC_LONG_INIT(0) \ 122 111 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \ 123 112 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \ 124 113 __DEBUG_MUTEX_INITIALIZER(lockname) \ ··· 138 127 */ 139 128 static inline int mutex_is_locked(struct mutex *lock) 140 129 { 141 - return atomic_read(&lock->count) != 1; 130 + /* 131 + * XXX think about spin_is_locked 132 + */ 133 + return __mutex_owner(lock) != NULL; 142 134 } 143 135 144 136 /* ··· 188 174 extern void mutex_unlock(struct mutex *lock); 189 175 190 176 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); 177 + 178 + /* 179 + * These values are chosen such that FAIL and SUCCESS match the 180 + * values of the regular mutex_trylock(). 181 + */ 182 + enum mutex_trylock_recursive_enum { 183 + MUTEX_TRYLOCK_FAILED = 0, 184 + MUTEX_TRYLOCK_SUCCESS = 1, 185 + MUTEX_TRYLOCK_RECURSIVE, 186 + }; 187 + 188 + /** 189 + * mutex_trylock_recursive - trylock variant that allows recursive locking 190 + * @lock: mutex to be locked 191 + * 192 + * This function should not be used, _ever_. It is purely for hysterical GEM 193 + * raisins, and once those are gone this will be removed. 194 + * 195 + * Returns: 196 + * MUTEX_TRYLOCK_FAILED - trylock failed, 197 + * MUTEX_TRYLOCK_SUCCESS - lock acquired, 198 + * MUTEX_TRYLOCK_RECURSIVE - we already owned the lock. 199 + */ 200 + static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum 201 + mutex_trylock_recursive(struct mutex *lock) 202 + { 203 + if (unlikely(__mutex_owner(lock) == current)) 204 + return MUTEX_TRYLOCK_RECURSIVE; 205 + 206 + return mutex_trylock(lock); 207 + } 191 208 192 209 #endif /* __LINUX_MUTEX_H */
+18 -2
include/linux/sched.h
··· 989 989 * already in a wake queue, the wakeup will happen soon and the second 990 990 * waker can just skip it. 991 991 * 992 - * The WAKE_Q macro declares and initializes the list head. 992 + * The DEFINE_WAKE_Q macro declares and initializes the list head. 993 993 * wake_up_q() does NOT reinitialize the list; it's expected to be 994 994 * called near the end of a function, where the fact that the queue is 995 995 * not used again will be easy to see by inspection. ··· 1009 1009 1010 1010 #define WAKE_Q_TAIL ((struct wake_q_node *) 0x01) 1011 1011 1012 - #define WAKE_Q(name) \ 1012 + #define DEFINE_WAKE_Q(name) \ 1013 1013 struct wake_q_head name = { WAKE_Q_TAIL, &name.first } 1014 1014 1015 1015 extern void wake_q_add(struct wake_q_head *head, ··· 2444 2444 static inline void calc_load_exit_idle(void) { } 2445 2445 #endif /* CONFIG_NO_HZ_COMMON */ 2446 2446 2447 + #ifndef cpu_relax_yield 2448 + #define cpu_relax_yield() cpu_relax() 2449 + #endif 2450 + 2447 2451 /* 2448 2452 * Do not use outside of architecture code which knows its limitations. 2449 2453 * ··· 3511 3507 } 3512 3508 3513 3509 #endif /* CONFIG_SMP */ 3510 + 3511 + /* 3512 + * In order to reduce various lock holder preemption latencies provide an 3513 + * interface to see if a vCPU is currently running or not. 3514 + * 3515 + * This allows us to terminate optimistic spin loops and block, analogous to 3516 + * the native optimistic spin heuristic of testing if the lock owner task is 3517 + * running or not. 3518 + */ 3519 + #ifndef vcpu_is_preempted 3520 + # define vcpu_is_preempted(cpu) false 3521 + #endif 3514 3522 3515 3523 extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask); 3516 3524 extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
+1 -1
include/linux/ww_mutex.h
··· 120 120 struct ww_class *ww_class) 121 121 { 122 122 ctx->task = current; 123 - ctx->stamp = atomic_long_inc_return(&ww_class->stamp); 123 + ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp); 124 124 ctx->acquired = 0; 125 125 #ifdef CONFIG_DEBUG_MUTEXES 126 126 ctx->ww_class = ww_class;
+2 -2
ipc/mqueue.c
··· 967 967 struct timespec ts; 968 968 struct posix_msg_tree_node *new_leaf = NULL; 969 969 int ret = 0; 970 - WAKE_Q(wake_q); 970 + DEFINE_WAKE_Q(wake_q); 971 971 972 972 if (u_abs_timeout) { 973 973 int res = prepare_timeout(u_abs_timeout, &expires, &ts); ··· 1151 1151 msg_ptr = wait.msg; 1152 1152 } 1153 1153 } else { 1154 - WAKE_Q(wake_q); 1154 + DEFINE_WAKE_Q(wake_q); 1155 1155 1156 1156 msg_ptr = msg_get(info); 1157 1157
+4 -4
ipc/msg.c
··· 235 235 { 236 236 struct msg_msg *msg, *t; 237 237 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm); 238 - WAKE_Q(wake_q); 238 + DEFINE_WAKE_Q(wake_q); 239 239 240 240 expunge_all(msq, -EIDRM, &wake_q); 241 241 ss_wakeup(msq, &wake_q, true); ··· 397 397 goto out_up; 398 398 case IPC_SET: 399 399 { 400 - WAKE_Q(wake_q); 400 + DEFINE_WAKE_Q(wake_q); 401 401 402 402 if (msqid64.msg_qbytes > ns->msg_ctlmnb && 403 403 !capable(CAP_SYS_RESOURCE)) { ··· 634 634 struct msg_msg *msg; 635 635 int err; 636 636 struct ipc_namespace *ns; 637 - WAKE_Q(wake_q); 637 + DEFINE_WAKE_Q(wake_q); 638 638 639 639 ns = current->nsproxy->ipc_ns; 640 640 ··· 850 850 struct msg_queue *msq; 851 851 struct ipc_namespace *ns; 852 852 struct msg_msg *msg, *copy = NULL; 853 - WAKE_Q(wake_q); 853 + DEFINE_WAKE_Q(wake_q); 854 854 855 855 ns = current->nsproxy->ipc_ns; 856 856
+1 -1
kernel/Kconfig.locks
··· 225 225 226 226 config MUTEX_SPIN_ON_OWNER 227 227 def_bool y 228 - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW 228 + depends on SMP && ARCH_SUPPORTS_ATOMIC_RMW 229 229 230 230 config RWSEM_SPIN_ON_OWNER 231 231 def_bool y
+4 -4
kernel/futex.c
··· 1298 1298 struct task_struct *new_owner; 1299 1299 struct futex_pi_state *pi_state = this->pi_state; 1300 1300 u32 uninitialized_var(curval), newval; 1301 - WAKE_Q(wake_q); 1301 + DEFINE_WAKE_Q(wake_q); 1302 1302 bool deboost; 1303 1303 int ret = 0; 1304 1304 ··· 1415 1415 struct futex_q *this, *next; 1416 1416 union futex_key key = FUTEX_KEY_INIT; 1417 1417 int ret; 1418 - WAKE_Q(wake_q); 1418 + DEFINE_WAKE_Q(wake_q); 1419 1419 1420 1420 if (!bitset) 1421 1421 return -EINVAL; ··· 1469 1469 struct futex_hash_bucket *hb1, *hb2; 1470 1470 struct futex_q *this, *next; 1471 1471 int ret, op_ret; 1472 - WAKE_Q(wake_q); 1472 + DEFINE_WAKE_Q(wake_q); 1473 1473 1474 1474 retry: 1475 1475 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ); ··· 1708 1708 struct futex_pi_state *pi_state = NULL; 1709 1709 struct futex_hash_bucket *hb1, *hb2; 1710 1710 struct futex_q *this, *next; 1711 - WAKE_Q(wake_q); 1711 + DEFINE_WAKE_Q(wake_q); 1712 1712 1713 1713 if (requeue_pi) { 1714 1714 /*
+5 -5
kernel/locking/lockdep.c
··· 840 840 /* 841 841 * Add a new dependency to the head of the list: 842 842 */ 843 - static int add_lock_to_list(struct lock_class *class, struct lock_class *this, 844 - struct list_head *head, unsigned long ip, 845 - int distance, struct stack_trace *trace) 843 + static int add_lock_to_list(struct lock_class *this, struct list_head *head, 844 + unsigned long ip, int distance, 845 + struct stack_trace *trace) 846 846 { 847 847 struct lock_list *entry; 848 848 /* ··· 1868 1868 * Ok, all validations passed, add the new lock 1869 1869 * to the previous lock's dependency list: 1870 1870 */ 1871 - ret = add_lock_to_list(hlock_class(prev), hlock_class(next), 1871 + ret = add_lock_to_list(hlock_class(next), 1872 1872 &hlock_class(prev)->locks_after, 1873 1873 next->acquire_ip, distance, &trace); 1874 1874 1875 1875 if (!ret) 1876 1876 return 0; 1877 1877 1878 - ret = add_lock_to_list(hlock_class(next), hlock_class(prev), 1878 + ret = add_lock_to_list(hlock_class(prev), 1879 1879 &hlock_class(next)->locks_before, 1880 1880 next->acquire_ip, distance, &trace); 1881 1881 if (!ret)
+2 -2
kernel/locking/mcs_spinlock.h
··· 28 28 #define arch_mcs_spin_lock_contended(l) \ 29 29 do { \ 30 30 while (!(smp_load_acquire(l))) \ 31 - cpu_relax_lowlatency(); \ 31 + cpu_relax(); \ 32 32 } while (0) 33 33 #endif 34 34 ··· 108 108 return; 109 109 /* Wait until the next pointer is set */ 110 110 while (!(next = READ_ONCE(node->next))) 111 - cpu_relax_lowlatency(); 111 + cpu_relax(); 112 112 } 113 113 114 114 /* Pass lock to next waiter. */
-13
kernel/locking/mutex-debug.c
··· 73 73 { 74 74 if (likely(debug_locks)) { 75 75 DEBUG_LOCKS_WARN_ON(lock->magic != lock); 76 - 77 - if (!lock->owner) 78 - DEBUG_LOCKS_WARN_ON(!lock->owner); 79 - else 80 - DEBUG_LOCKS_WARN_ON(lock->owner != current); 81 - 82 76 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next); 83 77 } 84 - 85 - /* 86 - * __mutex_slowpath_needs_to_unlock() is explicitly 0 for debug 87 - * mutexes so that we can do it here after we've verified state. 88 - */ 89 - mutex_clear_owner(lock); 90 - atomic_set(&lock->count, 1); 91 78 } 92 79 93 80 void debug_mutex_init(struct mutex *lock, const char *name,
-10
kernel/locking/mutex-debug.h
··· 27 27 extern void debug_mutex_init(struct mutex *lock, const char *name, 28 28 struct lock_class_key *key); 29 29 30 - static inline void mutex_set_owner(struct mutex *lock) 31 - { 32 - WRITE_ONCE(lock->owner, current); 33 - } 34 - 35 - static inline void mutex_clear_owner(struct mutex *lock) 36 - { 37 - WRITE_ONCE(lock->owner, NULL); 38 - } 39 - 40 30 #define spin_lock_mutex(lock, flags) \ 41 31 do { \ 42 32 struct mutex *l = container_of(lock, struct mutex, wait_lock); \
+342 -250
kernel/locking/mutex.c
··· 27 27 #include <linux/debug_locks.h> 28 28 #include <linux/osq_lock.h> 29 29 30 - /* 31 - * In the DEBUG case we are using the "NULL fastpath" for mutexes, 32 - * which forces all calls into the slowpath: 33 - */ 34 30 #ifdef CONFIG_DEBUG_MUTEXES 35 31 # include "mutex-debug.h" 36 - # include <asm-generic/mutex-null.h> 37 - /* 38 - * Must be 0 for the debug case so we do not do the unlock outside of the 39 - * wait_lock region. debug_mutex_unlock() will do the actual unlock in this 40 - * case. 41 - */ 42 - # undef __mutex_slowpath_needs_to_unlock 43 - # define __mutex_slowpath_needs_to_unlock() 0 44 32 #else 45 33 # include "mutex.h" 46 - # include <asm/mutex.h> 47 34 #endif 48 35 49 36 void 50 37 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 51 38 { 52 - atomic_set(&lock->count, 1); 39 + atomic_long_set(&lock->owner, 0); 53 40 spin_lock_init(&lock->wait_lock); 54 41 INIT_LIST_HEAD(&lock->wait_list); 55 - mutex_clear_owner(lock); 56 42 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 57 43 osq_lock_init(&lock->osq); 58 44 #endif 59 45 60 46 debug_mutex_init(lock, name, key); 61 47 } 62 - 63 48 EXPORT_SYMBOL(__mutex_init); 49 + 50 + /* 51 + * @owner: contains: 'struct task_struct *' to the current lock owner, 52 + * NULL means not owned. Since task_struct pointers are aligned at 53 + * ARCH_MIN_TASKALIGN (which is at least sizeof(void *)), we have low 54 + * bits to store extra state. 55 + * 56 + * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. 57 + * Bit1 indicates unlock needs to hand the lock to the top-waiter 58 + */ 59 + #define MUTEX_FLAG_WAITERS 0x01 60 + #define MUTEX_FLAG_HANDOFF 0x02 61 + 62 + #define MUTEX_FLAGS 0x03 63 + 64 + static inline struct task_struct *__owner_task(unsigned long owner) 65 + { 66 + return (struct task_struct *)(owner & ~MUTEX_FLAGS); 67 + } 68 + 69 + static inline unsigned long __owner_flags(unsigned long owner) 70 + { 71 + return owner & MUTEX_FLAGS; 72 + } 73 + 74 + /* 75 + * Actual trylock that will work on any unlocked state. 76 + * 77 + * When setting the owner field, we must preserve the low flag bits. 78 + * 79 + * Be careful with @handoff, only set that in a wait-loop (where you set 80 + * HANDOFF) to avoid recursive lock attempts. 81 + */ 82 + static inline bool __mutex_trylock(struct mutex *lock, const bool handoff) 83 + { 84 + unsigned long owner, curr = (unsigned long)current; 85 + 86 + owner = atomic_long_read(&lock->owner); 87 + for (;;) { /* must loop, can race against a flag */ 88 + unsigned long old, flags = __owner_flags(owner); 89 + 90 + if (__owner_task(owner)) { 91 + if (handoff && unlikely(__owner_task(owner) == current)) { 92 + /* 93 + * Provide ACQUIRE semantics for the lock-handoff. 94 + * 95 + * We cannot easily use load-acquire here, since 96 + * the actual load is a failed cmpxchg, which 97 + * doesn't imply any barriers. 98 + * 99 + * Also, this is a fairly unlikely scenario, and 100 + * this contains the cost. 101 + */ 102 + smp_mb(); /* ACQUIRE */ 103 + return true; 104 + } 105 + 106 + return false; 107 + } 108 + 109 + /* 110 + * We set the HANDOFF bit, we must make sure it doesn't live 111 + * past the point where we acquire it. This would be possible 112 + * if we (accidentally) set the bit on an unlocked mutex. 113 + */ 114 + if (handoff) 115 + flags &= ~MUTEX_FLAG_HANDOFF; 116 + 117 + old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags); 118 + if (old == owner) 119 + return true; 120 + 121 + owner = old; 122 + } 123 + } 124 + 125 + #ifndef CONFIG_DEBUG_LOCK_ALLOC 126 + /* 127 + * Lockdep annotations are contained to the slow paths for simplicity. 128 + * There is nothing that would stop spreading the lockdep annotations outwards 129 + * except more code. 130 + */ 131 + 132 + /* 133 + * Optimistic trylock that only works in the uncontended case. Make sure to 134 + * follow with a __mutex_trylock() before failing. 135 + */ 136 + static __always_inline bool __mutex_trylock_fast(struct mutex *lock) 137 + { 138 + unsigned long curr = (unsigned long)current; 139 + 140 + if (!atomic_long_cmpxchg_acquire(&lock->owner, 0UL, curr)) 141 + return true; 142 + 143 + return false; 144 + } 145 + 146 + static __always_inline bool __mutex_unlock_fast(struct mutex *lock) 147 + { 148 + unsigned long curr = (unsigned long)current; 149 + 150 + if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr) 151 + return true; 152 + 153 + return false; 154 + } 155 + #endif 156 + 157 + static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 158 + { 159 + atomic_long_or(flag, &lock->owner); 160 + } 161 + 162 + static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) 163 + { 164 + atomic_long_andnot(flag, &lock->owner); 165 + } 166 + 167 + static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) 168 + { 169 + return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; 170 + } 171 + 172 + /* 173 + * Give up ownership to a specific task, when @task = NULL, this is equivalent 174 + * to a regular unlock. Clears HANDOFF, preserves WAITERS. Provides RELEASE 175 + * semantics like a regular unlock, the __mutex_trylock() provides matching 176 + * ACQUIRE semantics for the handoff. 177 + */ 178 + static void __mutex_handoff(struct mutex *lock, struct task_struct *task) 179 + { 180 + unsigned long owner = atomic_long_read(&lock->owner); 181 + 182 + for (;;) { 183 + unsigned long old, new; 184 + 185 + #ifdef CONFIG_DEBUG_MUTEXES 186 + DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current); 187 + #endif 188 + 189 + new = (owner & MUTEX_FLAG_WAITERS); 190 + new |= (unsigned long)task; 191 + 192 + old = atomic_long_cmpxchg_release(&lock->owner, owner, new); 193 + if (old == owner) 194 + break; 195 + 196 + owner = old; 197 + } 198 + } 64 199 65 200 #ifndef CONFIG_DEBUG_LOCK_ALLOC 66 201 /* ··· 204 69 * We also put the fastpath first in the kernel image, to make sure the 205 70 * branch is predicted by the CPU as default-untaken. 206 71 */ 207 - __visible void __sched __mutex_lock_slowpath(atomic_t *lock_count); 72 + static void __sched __mutex_lock_slowpath(struct mutex *lock); 208 73 209 74 /** 210 75 * mutex_lock - acquire the mutex ··· 230 95 void __sched mutex_lock(struct mutex *lock) 231 96 { 232 97 might_sleep(); 233 - /* 234 - * The locking fastpath is the 1->0 transition from 235 - * 'unlocked' into 'locked' state. 236 - */ 237 - __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath); 238 - mutex_set_owner(lock); 239 - } 240 98 99 + if (!__mutex_trylock_fast(lock)) 100 + __mutex_lock_slowpath(lock); 101 + } 241 102 EXPORT_SYMBOL(mutex_lock); 242 103 #endif 243 104 ··· 280 149 /* 281 150 * After acquiring lock with fastpath or when we lost out in contested 282 151 * slowpath, set ctx and wake up any waiters so they can recheck. 283 - * 284 - * This function is never called when CONFIG_DEBUG_LOCK_ALLOC is set, 285 - * as the fastpath and opportunistic spinning are disabled in that case. 286 152 */ 287 153 static __always_inline void 288 154 ww_mutex_set_context_fastpath(struct ww_mutex *lock, ··· 304 176 /* 305 177 * Check if lock is contended, if not there is nobody to wake up 306 178 */ 307 - if (likely(atomic_read(&lock->base.count) == 0)) 179 + if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS))) 308 180 return; 309 181 310 182 /* ··· 355 227 bool ret = true; 356 228 357 229 rcu_read_lock(); 358 - while (lock->owner == owner) { 230 + while (__mutex_owner(lock) == owner) { 359 231 /* 360 232 * Ensure we emit the owner->on_cpu, dereference _after_ 361 233 * checking lock->owner still matches owner. If that fails, ··· 364 236 */ 365 237 barrier(); 366 238 367 - if (!owner->on_cpu || need_resched()) { 239 + /* 240 + * Use vcpu_is_preempted to detect lock holder preemption issue. 241 + */ 242 + if (!owner->on_cpu || need_resched() || 243 + vcpu_is_preempted(task_cpu(owner))) { 368 244 ret = false; 369 245 break; 370 246 } 371 247 372 - cpu_relax_lowlatency(); 248 + cpu_relax(); 373 249 } 374 250 rcu_read_unlock(); 375 251 ··· 392 260 return 0; 393 261 394 262 rcu_read_lock(); 395 - owner = READ_ONCE(lock->owner); 396 - if (owner) 397 - retval = owner->on_cpu; 398 - rcu_read_unlock(); 263 + owner = __mutex_owner(lock); 264 + 399 265 /* 400 - * if lock->owner is not set, the mutex owner may have just acquired 401 - * it and not set the owner yet or the mutex has been released. 266 + * As lock holder preemption issue, we both skip spinning if task is not 267 + * on cpu or its cpu is preempted 268 + */ 269 + if (owner) 270 + retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner)); 271 + rcu_read_unlock(); 272 + 273 + /* 274 + * If lock->owner is not set, the mutex has been released. Return true 275 + * such that we'll trylock in the spin path, which is a faster option 276 + * than the blocking slow path. 402 277 */ 403 278 return retval; 404 - } 405 - 406 - /* 407 - * Atomically try to take the lock when it is available 408 - */ 409 - static inline bool mutex_try_to_acquire(struct mutex *lock) 410 - { 411 - return !mutex_is_locked(lock) && 412 - (atomic_cmpxchg_acquire(&lock->count, 1, 0) == 1); 413 279 } 414 280 415 281 /* ··· 418 288 * need to reschedule. The rationale is that if the lock owner is 419 289 * running, it is likely to release the lock soon. 420 290 * 421 - * Since this needs the lock owner, and this mutex implementation 422 - * doesn't track the owner atomically in the lock field, we need to 423 - * track it non-atomically. 424 - * 425 - * We can't do this for DEBUG_MUTEXES because that relies on wait_lock 426 - * to serialize everything. 427 - * 428 291 * The mutex spinners are queued up using MCS lock so that only one 429 292 * spinner can compete for the mutex. However, if mutex spinning isn't 430 293 * going to happen, there is no point in going through the lock/unlock ··· 425 302 * 426 303 * Returns true when the lock was taken, otherwise false, indicating 427 304 * that we need to jump to the slowpath and sleep. 305 + * 306 + * The waiter flag is set to true if the spinner is a waiter in the wait 307 + * queue. The waiter-spinner will spin on the lock directly and concurrently 308 + * with the spinner at the head of the OSQ, if present, until the owner is 309 + * changed to itself. 428 310 */ 429 311 static bool mutex_optimistic_spin(struct mutex *lock, 430 - struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 312 + struct ww_acquire_ctx *ww_ctx, 313 + const bool use_ww_ctx, const bool waiter) 431 314 { 432 315 struct task_struct *task = current; 433 316 434 - if (!mutex_can_spin_on_owner(lock)) 435 - goto done; 317 + if (!waiter) { 318 + /* 319 + * The purpose of the mutex_can_spin_on_owner() function is 320 + * to eliminate the overhead of osq_lock() and osq_unlock() 321 + * in case spinning isn't possible. As a waiter-spinner 322 + * is not going to take OSQ lock anyway, there is no need 323 + * to call mutex_can_spin_on_owner(). 324 + */ 325 + if (!mutex_can_spin_on_owner(lock)) 326 + goto fail; 436 327 437 - /* 438 - * In order to avoid a stampede of mutex spinners trying to 439 - * acquire the mutex all at once, the spinners need to take a 440 - * MCS (queued) lock first before spinning on the owner field. 441 - */ 442 - if (!osq_lock(&lock->osq)) 443 - goto done; 328 + /* 329 + * In order to avoid a stampede of mutex spinners trying to 330 + * acquire the mutex all at once, the spinners need to take a 331 + * MCS (queued) lock first before spinning on the owner field. 332 + */ 333 + if (!osq_lock(&lock->osq)) 334 + goto fail; 335 + } 444 336 445 - while (true) { 337 + for (;;) { 446 338 struct task_struct *owner; 447 339 448 340 if (use_ww_ctx && ww_ctx->acquired > 0) { ··· 473 335 * performed the optimistic spinning cannot be done. 474 336 */ 475 337 if (READ_ONCE(ww->ctx)) 476 - break; 338 + goto fail_unlock; 477 339 } 478 340 479 341 /* 480 342 * If there's an owner, wait for it to either 481 343 * release the lock or go to sleep. 482 344 */ 483 - owner = READ_ONCE(lock->owner); 484 - if (owner && !mutex_spin_on_owner(lock, owner)) 485 - break; 486 - 487 - /* Try to acquire the mutex if it is unlocked. */ 488 - if (mutex_try_to_acquire(lock)) { 489 - lock_acquired(&lock->dep_map, ip); 490 - 491 - if (use_ww_ctx) { 492 - struct ww_mutex *ww; 493 - ww = container_of(lock, struct ww_mutex, base); 494 - 495 - ww_mutex_set_context_fastpath(ww, ww_ctx); 345 + owner = __mutex_owner(lock); 346 + if (owner) { 347 + if (waiter && owner == task) { 348 + smp_mb(); /* ACQUIRE */ 349 + break; 496 350 } 497 351 498 - mutex_set_owner(lock); 499 - osq_unlock(&lock->osq); 500 - return true; 352 + if (!mutex_spin_on_owner(lock, owner)) 353 + goto fail_unlock; 501 354 } 502 355 503 - /* 504 - * When there's no owner, we might have preempted between the 505 - * owner acquiring the lock and setting the owner field. If 506 - * we're an RT task that will live-lock because we won't let 507 - * the owner complete. 508 - */ 509 - if (!owner && (need_resched() || rt_task(task))) 356 + /* Try to acquire the mutex if it is unlocked. */ 357 + if (__mutex_trylock(lock, waiter)) 510 358 break; 511 359 512 360 /* ··· 501 377 * memory barriers as we'll eventually observe the right 502 378 * values at the cost of a few extra spins. 503 379 */ 504 - cpu_relax_lowlatency(); 380 + cpu_relax(); 505 381 } 506 382 507 - osq_unlock(&lock->osq); 508 - done: 383 + if (!waiter) 384 + osq_unlock(&lock->osq); 385 + 386 + return true; 387 + 388 + 389 + fail_unlock: 390 + if (!waiter) 391 + osq_unlock(&lock->osq); 392 + 393 + fail: 509 394 /* 510 395 * If we fell out of the spin path because of need_resched(), 511 396 * reschedule now, before we try-lock the mutex. This avoids getting ··· 533 400 } 534 401 #else 535 402 static bool mutex_optimistic_spin(struct mutex *lock, 536 - struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 403 + struct ww_acquire_ctx *ww_ctx, 404 + const bool use_ww_ctx, const bool waiter) 537 405 { 538 406 return false; 539 407 } 540 408 #endif 541 409 542 - __visible __used noinline 543 - void __sched __mutex_unlock_slowpath(atomic_t *lock_count); 410 + static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip); 544 411 545 412 /** 546 413 * mutex_unlock - release the mutex ··· 555 422 */ 556 423 void __sched mutex_unlock(struct mutex *lock) 557 424 { 558 - /* 559 - * The unlocking fastpath is the 0->1 transition from 'locked' 560 - * into 'unlocked' state: 561 - */ 562 - #ifndef CONFIG_DEBUG_MUTEXES 563 - /* 564 - * When debugging is enabled we must not clear the owner before time, 565 - * the slow path will always be taken, and that clears the owner field 566 - * after verifying that it was indeed current. 567 - */ 568 - mutex_clear_owner(lock); 425 + #ifndef CONFIG_DEBUG_LOCK_ALLOC 426 + if (__mutex_unlock_fast(lock)) 427 + return; 569 428 #endif 570 - __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath); 429 + __mutex_unlock_slowpath(lock, _RET_IP_); 571 430 } 572 - 573 431 EXPORT_SYMBOL(mutex_unlock); 574 432 575 433 /** ··· 589 465 lock->ctx = NULL; 590 466 } 591 467 592 - #ifndef CONFIG_DEBUG_MUTEXES 593 - /* 594 - * When debugging is enabled we must not clear the owner before time, 595 - * the slow path will always be taken, and that clears the owner field 596 - * after verifying that it was indeed current. 597 - */ 598 - mutex_clear_owner(&lock->base); 599 - #endif 600 - __mutex_fastpath_unlock(&lock->base.count, __mutex_unlock_slowpath); 468 + mutex_unlock(&lock->base); 601 469 } 602 470 EXPORT_SYMBOL(ww_mutex_unlock); 603 471 ··· 625 509 struct task_struct *task = current; 626 510 struct mutex_waiter waiter; 627 511 unsigned long flags; 512 + bool first = false; 513 + struct ww_mutex *ww; 628 514 int ret; 629 515 630 516 if (use_ww_ctx) { 631 - struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 517 + ww = container_of(lock, struct ww_mutex, base); 632 518 if (unlikely(ww_ctx == READ_ONCE(ww->ctx))) 633 519 return -EALREADY; 634 520 } ··· 638 520 preempt_disable(); 639 521 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 640 522 641 - if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) { 523 + if (__mutex_trylock(lock, false) || 524 + mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, false)) { 642 525 /* got the lock, yay! */ 526 + lock_acquired(&lock->dep_map, ip); 527 + if (use_ww_ctx) 528 + ww_mutex_set_context_fastpath(ww, ww_ctx); 643 529 preempt_enable(); 644 530 return 0; 645 531 } 646 532 647 533 spin_lock_mutex(&lock->wait_lock, flags); 648 - 649 534 /* 650 - * Once more, try to acquire the lock. Only try-lock the mutex if 651 - * it is unlocked to reduce unnecessary xchg() operations. 535 + * After waiting to acquire the wait_lock, try again. 652 536 */ 653 - if (!mutex_is_locked(lock) && 654 - (atomic_xchg_acquire(&lock->count, 0) == 1)) 537 + if (__mutex_trylock(lock, false)) 655 538 goto skip_wait; 656 539 657 540 debug_mutex_lock_common(lock, &waiter); ··· 662 543 list_add_tail(&waiter.list, &lock->wait_list); 663 544 waiter.task = task; 664 545 546 + if (__mutex_waiter_is_first(lock, &waiter)) 547 + __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); 548 + 665 549 lock_contended(&lock->dep_map, ip); 666 550 551 + set_task_state(task, state); 667 552 for (;;) { 668 553 /* 669 - * Lets try to take the lock again - this is needed even if 670 - * we get here for the first time (shortly after failing to 671 - * acquire the lock), to make sure that we get a wakeup once 672 - * it's unlocked. Later on, if we sleep, this is the 673 - * operation that gives us the lock. We xchg it to -1, so 674 - * that when we release the lock, we properly wake up the 675 - * other waiters. We only attempt the xchg if the count is 676 - * non-negative in order to avoid unnecessary xchg operations: 554 + * Once we hold wait_lock, we're serialized against 555 + * mutex_unlock() handing the lock off to us, do a trylock 556 + * before testing the error conditions to make sure we pick up 557 + * the handoff. 677 558 */ 678 - if (atomic_read(&lock->count) >= 0 && 679 - (atomic_xchg_acquire(&lock->count, -1) == 1)) 680 - break; 559 + if (__mutex_trylock(lock, first)) 560 + goto acquired; 681 561 682 562 /* 683 - * got a signal? (This code gets eliminated in the 684 - * TASK_UNINTERRUPTIBLE case.) 563 + * Check for signals and wound conditions while holding 564 + * wait_lock. This ensures the lock cancellation is ordered 565 + * against mutex_unlock() and wake-ups do not go missing. 685 566 */ 686 567 if (unlikely(signal_pending_state(state, task))) { 687 568 ret = -EINTR; ··· 694 575 goto err; 695 576 } 696 577 697 - __set_task_state(task, state); 698 - 699 - /* didn't get the lock, go to sleep: */ 700 578 spin_unlock_mutex(&lock->wait_lock, flags); 701 579 schedule_preempt_disabled(); 580 + 581 + if (!first && __mutex_waiter_is_first(lock, &waiter)) { 582 + first = true; 583 + __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); 584 + } 585 + 586 + set_task_state(task, state); 587 + /* 588 + * Here we order against unlock; we must either see it change 589 + * state back to RUNNING and fall through the next schedule(), 590 + * or we must see its unlock and acquire. 591 + */ 592 + if ((first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, true)) || 593 + __mutex_trylock(lock, first)) 594 + break; 595 + 702 596 spin_lock_mutex(&lock->wait_lock, flags); 703 597 } 598 + spin_lock_mutex(&lock->wait_lock, flags); 599 + acquired: 704 600 __set_task_state(task, TASK_RUNNING); 705 601 706 602 mutex_remove_waiter(lock, &waiter, task); 707 - /* set it to 0 if there are no waiters left: */ 708 603 if (likely(list_empty(&lock->wait_list))) 709 - atomic_set(&lock->count, 0); 604 + __mutex_clear_flag(lock, MUTEX_FLAGS); 605 + 710 606 debug_mutex_free_waiter(&waiter); 711 607 712 608 skip_wait: 713 609 /* got the lock - cleanup and rejoice! */ 714 610 lock_acquired(&lock->dep_map, ip); 715 - mutex_set_owner(lock); 716 611 717 - if (use_ww_ctx) { 718 - struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 612 + if (use_ww_ctx) 719 613 ww_mutex_set_context_slowpath(ww, ww_ctx); 720 - } 721 614 722 615 spin_unlock_mutex(&lock->wait_lock, flags); 723 616 preempt_enable(); 724 617 return 0; 725 618 726 619 err: 620 + __set_task_state(task, TASK_RUNNING); 727 621 mutex_remove_waiter(lock, &waiter, task); 728 622 spin_unlock_mutex(&lock->wait_lock, flags); 729 623 debug_mutex_free_waiter(&waiter); ··· 763 631 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 764 632 0, nest, _RET_IP_, NULL, 0); 765 633 } 766 - 767 634 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 768 635 769 636 int __sched ··· 781 650 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 782 651 subclass, NULL, _RET_IP_, NULL, 0); 783 652 } 784 - 785 653 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 786 654 787 655 static inline int ··· 845 715 /* 846 716 * Release the lock, slowpath: 847 717 */ 848 - static inline void 849 - __mutex_unlock_common_slowpath(struct mutex *lock, int nested) 718 + static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) 850 719 { 851 - unsigned long flags; 852 - WAKE_Q(wake_q); 720 + struct task_struct *next = NULL; 721 + unsigned long owner, flags; 722 + DEFINE_WAKE_Q(wake_q); 723 + 724 + mutex_release(&lock->dep_map, 1, ip); 853 725 854 726 /* 855 - * As a performance measurement, release the lock before doing other 856 - * wakeup related duties to follow. This allows other tasks to acquire 857 - * the lock sooner, while still handling cleanups in past unlock calls. 858 - * This can be done as we do not enforce strict equivalence between the 859 - * mutex counter and wait_list. 727 + * Release the lock before (potentially) taking the spinlock such that 728 + * other contenders can get on with things ASAP. 860 729 * 861 - * 862 - * Some architectures leave the lock unlocked in the fastpath failure 863 - * case, others need to leave it locked. In the later case we have to 864 - * unlock it here - as the lock counter is currently 0 or negative. 730 + * Except when HANDOFF, in that case we must not clear the owner field, 731 + * but instead set it to the top waiter. 865 732 */ 866 - if (__mutex_slowpath_needs_to_unlock()) 867 - atomic_set(&lock->count, 1); 733 + owner = atomic_long_read(&lock->owner); 734 + for (;;) { 735 + unsigned long old; 736 + 737 + #ifdef CONFIG_DEBUG_MUTEXES 738 + DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current); 739 + #endif 740 + 741 + if (owner & MUTEX_FLAG_HANDOFF) 742 + break; 743 + 744 + old = atomic_long_cmpxchg_release(&lock->owner, owner, 745 + __owner_flags(owner)); 746 + if (old == owner) { 747 + if (owner & MUTEX_FLAG_WAITERS) 748 + break; 749 + 750 + return; 751 + } 752 + 753 + owner = old; 754 + } 868 755 869 756 spin_lock_mutex(&lock->wait_lock, flags); 870 - mutex_release(&lock->dep_map, nested, _RET_IP_); 871 757 debug_mutex_unlock(lock); 872 - 873 758 if (!list_empty(&lock->wait_list)) { 874 759 /* get the first entry from the wait-list: */ 875 760 struct mutex_waiter *waiter = 876 - list_entry(lock->wait_list.next, 877 - struct mutex_waiter, list); 761 + list_first_entry(&lock->wait_list, 762 + struct mutex_waiter, list); 763 + 764 + next = waiter->task; 878 765 879 766 debug_mutex_wake_waiter(lock, waiter); 880 - wake_q_add(&wake_q, waiter->task); 767 + wake_q_add(&wake_q, next); 881 768 } 882 769 770 + if (owner & MUTEX_FLAG_HANDOFF) 771 + __mutex_handoff(lock, next); 772 + 883 773 spin_unlock_mutex(&lock->wait_lock, flags); 774 + 884 775 wake_up_q(&wake_q); 885 - } 886 - 887 - /* 888 - * Release the lock, slowpath: 889 - */ 890 - __visible void 891 - __mutex_unlock_slowpath(atomic_t *lock_count) 892 - { 893 - struct mutex *lock = container_of(lock_count, struct mutex, count); 894 - 895 - __mutex_unlock_common_slowpath(lock, 1); 896 776 } 897 777 898 778 #ifndef CONFIG_DEBUG_LOCK_ALLOC ··· 929 789 */ 930 790 int __sched mutex_lock_interruptible(struct mutex *lock) 931 791 { 932 - int ret; 933 - 934 792 might_sleep(); 935 - ret = __mutex_fastpath_lock_retval(&lock->count); 936 - if (likely(!ret)) { 937 - mutex_set_owner(lock); 793 + 794 + if (__mutex_trylock_fast(lock)) 938 795 return 0; 939 - } else 940 - return __mutex_lock_interruptible_slowpath(lock); 796 + 797 + return __mutex_lock_interruptible_slowpath(lock); 941 798 } 942 799 943 800 EXPORT_SYMBOL(mutex_lock_interruptible); 944 801 945 802 int __sched mutex_lock_killable(struct mutex *lock) 946 803 { 947 - int ret; 948 - 949 804 might_sleep(); 950 - ret = __mutex_fastpath_lock_retval(&lock->count); 951 - if (likely(!ret)) { 952 - mutex_set_owner(lock); 805 + 806 + if (__mutex_trylock_fast(lock)) 953 807 return 0; 954 - } else 955 - return __mutex_lock_killable_slowpath(lock); 808 + 809 + return __mutex_lock_killable_slowpath(lock); 956 810 } 957 811 EXPORT_SYMBOL(mutex_lock_killable); 958 812 959 - __visible void __sched 960 - __mutex_lock_slowpath(atomic_t *lock_count) 813 + static noinline void __sched 814 + __mutex_lock_slowpath(struct mutex *lock) 961 815 { 962 - struct mutex *lock = container_of(lock_count, struct mutex, count); 963 - 964 816 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, 965 817 NULL, _RET_IP_, NULL, 0); 966 818 } ··· 988 856 989 857 #endif 990 858 991 - /* 992 - * Spinlock based trylock, we take the spinlock and check whether we 993 - * can get the lock: 994 - */ 995 - static inline int __mutex_trylock_slowpath(atomic_t *lock_count) 996 - { 997 - struct mutex *lock = container_of(lock_count, struct mutex, count); 998 - unsigned long flags; 999 - int prev; 1000 - 1001 - /* No need to trylock if the mutex is locked. */ 1002 - if (mutex_is_locked(lock)) 1003 - return 0; 1004 - 1005 - spin_lock_mutex(&lock->wait_lock, flags); 1006 - 1007 - prev = atomic_xchg_acquire(&lock->count, -1); 1008 - if (likely(prev == 1)) { 1009 - mutex_set_owner(lock); 1010 - mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 1011 - } 1012 - 1013 - /* Set it back to 0 if there are no waiters: */ 1014 - if (likely(list_empty(&lock->wait_list))) 1015 - atomic_set(&lock->count, 0); 1016 - 1017 - spin_unlock_mutex(&lock->wait_lock, flags); 1018 - 1019 - return prev == 1; 1020 - } 1021 - 1022 859 /** 1023 860 * mutex_trylock - try to acquire the mutex, without waiting 1024 861 * @lock: the mutex to be acquired ··· 1004 903 */ 1005 904 int __sched mutex_trylock(struct mutex *lock) 1006 905 { 1007 - int ret; 906 + bool locked = __mutex_trylock(lock, false); 1008 907 1009 - ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath); 1010 - if (ret) 1011 - mutex_set_owner(lock); 908 + if (locked) 909 + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 1012 910 1013 - return ret; 911 + return locked; 1014 912 } 1015 913 EXPORT_SYMBOL(mutex_trylock); 1016 914 ··· 1017 917 int __sched 1018 918 __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 1019 919 { 1020 - int ret; 1021 - 1022 920 might_sleep(); 1023 921 1024 - ret = __mutex_fastpath_lock_retval(&lock->base.count); 1025 - 1026 - if (likely(!ret)) { 922 + if (__mutex_trylock_fast(&lock->base)) { 1027 923 ww_mutex_set_context_fastpath(lock, ctx); 1028 - mutex_set_owner(&lock->base); 1029 - } else 1030 - ret = __ww_mutex_lock_slowpath(lock, ctx); 1031 - return ret; 924 + return 0; 925 + } 926 + 927 + return __ww_mutex_lock_slowpath(lock, ctx); 1032 928 } 1033 929 EXPORT_SYMBOL(__ww_mutex_lock); 1034 930 1035 931 int __sched 1036 932 __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 1037 933 { 1038 - int ret; 1039 - 1040 934 might_sleep(); 1041 935 1042 - ret = __mutex_fastpath_lock_retval(&lock->base.count); 1043 - 1044 - if (likely(!ret)) { 936 + if (__mutex_trylock_fast(&lock->base)) { 1045 937 ww_mutex_set_context_fastpath(lock, ctx); 1046 - mutex_set_owner(&lock->base); 1047 - } else 1048 - ret = __ww_mutex_lock_interruptible_slowpath(lock, ctx); 1049 - return ret; 938 + return 0; 939 + } 940 + 941 + return __ww_mutex_lock_interruptible_slowpath(lock, ctx); 1050 942 } 1051 943 EXPORT_SYMBOL(__ww_mutex_lock_interruptible); 1052 944
-26
kernel/locking/mutex.h
··· 16 16 #define mutex_remove_waiter(lock, waiter, task) \ 17 17 __list_del((waiter)->list.prev, (waiter)->list.next) 18 18 19 - #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 20 - /* 21 - * The mutex owner can get read and written to locklessly. 22 - * We should use WRITE_ONCE when writing the owner value to 23 - * avoid store tearing, otherwise, a thread could potentially 24 - * read a partially written and incomplete owner value. 25 - */ 26 - static inline void mutex_set_owner(struct mutex *lock) 27 - { 28 - WRITE_ONCE(lock->owner, current); 29 - } 30 - 31 - static inline void mutex_clear_owner(struct mutex *lock) 32 - { 33 - WRITE_ONCE(lock->owner, NULL); 34 - } 35 - #else 36 - static inline void mutex_set_owner(struct mutex *lock) 37 - { 38 - } 39 - 40 - static inline void mutex_clear_owner(struct mutex *lock) 41 - { 42 - } 43 - #endif 44 - 45 19 #define debug_mutex_wake_waiter(lock, waiter) do { } while (0) 46 20 #define debug_mutex_free_waiter(waiter) do { } while (0) 47 21 #define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0)
+11 -4
kernel/locking/osq_lock.c
··· 21 21 return cpu_nr + 1; 22 22 } 23 23 24 + static inline int node_cpu(struct optimistic_spin_node *node) 25 + { 26 + return node->cpu - 1; 27 + } 28 + 24 29 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) 25 30 { 26 31 int cpu_nr = encoded_cpu_val - 1; ··· 80 75 break; 81 76 } 82 77 83 - cpu_relax_lowlatency(); 78 + cpu_relax(); 84 79 } 85 80 86 81 return next; ··· 123 118 while (!READ_ONCE(node->locked)) { 124 119 /* 125 120 * If we need to reschedule bail... so we can block. 121 + * Use vcpu_is_preempted() to avoid waiting for a preempted 122 + * lock holder: 126 123 */ 127 - if (need_resched()) 124 + if (need_resched() || vcpu_is_preempted(node_cpu(node->prev))) 128 125 goto unqueue; 129 126 130 - cpu_relax_lowlatency(); 127 + cpu_relax(); 131 128 } 132 129 return true; 133 130 ··· 155 148 if (smp_load_acquire(&node->locked)) 156 149 return true; 157 150 158 - cpu_relax_lowlatency(); 151 + cpu_relax(); 159 152 160 153 /* 161 154 * Or we race against a concurrent unqueue()'s step-B, in which
+3 -3
kernel/locking/qrwlock.c
··· 54 54 rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts) 55 55 { 56 56 while ((cnts & _QW_WMASK) == _QW_LOCKED) { 57 - cpu_relax_lowlatency(); 57 + cpu_relax(); 58 58 cnts = atomic_read_acquire(&lock->cnts); 59 59 } 60 60 } ··· 130 130 (cmpxchg_relaxed(&l->wmode, 0, _QW_WAITING) == 0)) 131 131 break; 132 132 133 - cpu_relax_lowlatency(); 133 + cpu_relax(); 134 134 } 135 135 136 136 /* When no more readers, set the locked flag */ ··· 141 141 _QW_LOCKED) == _QW_WAITING)) 142 142 break; 143 143 144 - cpu_relax_lowlatency(); 144 + cpu_relax(); 145 145 } 146 146 unlock: 147 147 arch_spin_unlock(&lock->wait_lock);
+13 -5
kernel/locking/rtmutex.c
··· 1446 1446 bool (*slowfn)(struct rt_mutex *lock, 1447 1447 struct wake_q_head *wqh)) 1448 1448 { 1449 - WAKE_Q(wake_q); 1449 + DEFINE_WAKE_Q(wake_q); 1450 1450 1451 1451 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) { 1452 1452 rt_mutex_deadlock_account_unlock(current); ··· 1619 1619 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a 1620 1620 * proxy owner 1621 1621 * 1622 - * @lock: the rt_mutex to be locked 1622 + * @lock: the rt_mutex to be locked 1623 1623 * @proxy_owner:the task to set as owner 1624 1624 * 1625 1625 * No locking. Caller has to do serializing itself 1626 - * Special API call for PI-futex support 1626 + * 1627 + * Special API call for PI-futex support. This initializes the rtmutex and 1628 + * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not 1629 + * possible at this point because the pi_state which contains the rtmutex 1630 + * is not yet visible to other tasks. 1627 1631 */ 1628 1632 void rt_mutex_init_proxy_locked(struct rt_mutex *lock, 1629 1633 struct task_struct *proxy_owner) ··· 1641 1637 /** 1642 1638 * rt_mutex_proxy_unlock - release a lock on behalf of owner 1643 1639 * 1644 - * @lock: the rt_mutex to be locked 1640 + * @lock: the rt_mutex to be locked 1645 1641 * 1646 1642 * No locking. Caller has to do serializing itself 1647 - * Special API call for PI-futex support 1643 + * 1644 + * Special API call for PI-futex support. This merrily cleans up the rtmutex 1645 + * (debugging) state. Concurrent operations on this rt_mutex are not 1646 + * possible because it belongs to the pi_state which is about to be freed 1647 + * and it is not longer visible to other tasks. 1648 1648 */ 1649 1649 void rt_mutex_proxy_unlock(struct rt_mutex *lock, 1650 1650 struct task_struct *proxy_owner)
+1 -2
kernel/locking/rtmutex_common.h
··· 71 71 * lock->owner state tracking: 72 72 */ 73 73 #define RT_MUTEX_HAS_WAITERS 1UL 74 - #define RT_MUTEX_OWNER_MASKALL 1UL 75 74 76 75 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock) 77 76 { 78 77 unsigned long owner = (unsigned long) READ_ONCE(lock->owner); 79 78 80 - return (struct task_struct *) (owner & ~RT_MUTEX_OWNER_MASKALL); 79 + return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS); 81 80 } 82 81 83 82 /*
+18 -10
kernel/locking/rwsem-xadd.c
··· 225 225 long count, adjustment = -RWSEM_ACTIVE_READ_BIAS; 226 226 struct rwsem_waiter waiter; 227 227 struct task_struct *tsk = current; 228 - WAKE_Q(wake_q); 228 + DEFINE_WAKE_Q(wake_q); 229 229 230 230 waiter.task = tsk; 231 231 waiter.type = RWSEM_WAITING_FOR_READ; ··· 336 336 goto done; 337 337 } 338 338 339 - ret = owner->on_cpu; 339 + /* 340 + * As lock holder preemption issue, we both skip spinning if task is not 341 + * on cpu or its cpu is preempted 342 + */ 343 + ret = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner)); 340 344 done: 341 345 rcu_read_unlock(); 342 346 return ret; ··· 366 362 */ 367 363 barrier(); 368 364 369 - /* abort spinning when need_resched or owner is not running */ 370 - if (!owner->on_cpu || need_resched()) { 365 + /* 366 + * abort spinning when need_resched or owner is not running or 367 + * owner's cpu is preempted. 368 + */ 369 + if (!owner->on_cpu || need_resched() || 370 + vcpu_is_preempted(task_cpu(owner))) { 371 371 rcu_read_unlock(); 372 372 return false; 373 373 } 374 374 375 - cpu_relax_lowlatency(); 375 + cpu_relax(); 376 376 } 377 377 rcu_read_unlock(); 378 378 out: ··· 431 423 * memory barriers as we'll eventually observe the right 432 424 * values at the cost of a few extra spins. 433 425 */ 434 - cpu_relax_lowlatency(); 426 + cpu_relax(); 435 427 } 436 428 osq_unlock(&sem->osq); 437 429 done: ··· 469 461 bool waiting = true; /* any queued threads before us */ 470 462 struct rwsem_waiter waiter; 471 463 struct rw_semaphore *ret = sem; 472 - WAKE_Q(wake_q); 464 + DEFINE_WAKE_Q(wake_q); 473 465 474 466 /* undo write bias from down_write operation, stop active locking */ 475 467 count = atomic_long_sub_return(RWSEM_ACTIVE_WRITE_BIAS, &sem->count); ··· 503 495 * wake any read locks that were queued ahead of us. 504 496 */ 505 497 if (count > RWSEM_WAITING_BIAS) { 506 - WAKE_Q(wake_q); 498 + DEFINE_WAKE_Q(wake_q); 507 499 508 500 __rwsem_mark_wake(sem, RWSEM_WAKE_READERS, &wake_q); 509 501 /* ··· 579 571 struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) 580 572 { 581 573 unsigned long flags; 582 - WAKE_Q(wake_q); 574 + DEFINE_WAKE_Q(wake_q); 583 575 584 576 /* 585 577 * If a spinner is present, it is not necessary to do the wakeup. ··· 633 625 struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) 634 626 { 635 627 unsigned long flags; 636 - WAKE_Q(wake_q); 628 + DEFINE_WAKE_Q(wake_q); 637 629 638 630 raw_spin_lock_irqsave(&sem->wait_lock, flags); 639 631
+1 -1
kernel/sched/core.c
··· 75 75 #include <linux/compiler.h> 76 76 #include <linux/frame.h> 77 77 #include <linux/prefetch.h> 78 + #include <linux/mutex.h> 78 79 79 80 #include <asm/switch_to.h> 80 81 #include <asm/tlb.h> 81 82 #include <asm/irq_regs.h> 82 - #include <asm/mutex.h> 83 83 #ifdef CONFIG_PARAVIRT 84 84 #include <asm/paravirt.h> 85 85 #endif
+1 -1
kernel/stop_machine.c
··· 194 194 /* Simple state machine */ 195 195 do { 196 196 /* Chill out and ensure we re-read multi_stop_state. */ 197 - cpu_relax(); 197 + cpu_relax_yield(); 198 198 if (msdata->state != curstate) { 199 199 curstate = msdata->state; 200 200 switch (curstate) {
+1 -1
lib/lockref.c
··· 20 20 if (likely(old.lock_count == prev.lock_count)) { \ 21 21 SUCCESS; \ 22 22 } \ 23 - cpu_relax_lowlatency(); \ 23 + cpu_relax(); \ 24 24 } \ 25 25 } while (0) 26 26
+1 -1
net/core/dev.c
··· 5064 5064 return rc; 5065 5065 goto restart; 5066 5066 } 5067 - cpu_relax_lowlatency(); 5067 + cpu_relax(); 5068 5068 } 5069 5069 if (napi_poll) 5070 5070 busy_poll_stop(napi, have_poll_lock);
+6
scripts/checkpatch.pl
··· 6076 6076 } 6077 6077 } 6078 6078 6079 + # check for mutex_trylock_recursive usage 6080 + if ($line =~ /mutex_trylock_recursive/) { 6081 + ERROR("LOCKING", 6082 + "recursive locking is bad, do not use this ever.\n" . $herecurr); 6083 + } 6084 + 6079 6085 # check for lockdep_set_novalidate_class 6080 6086 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 6081 6087 $line =~ /__lockdep_no_validate__\s*\)/ ) {
+14 -6
virt/kvm/kvm_main.c
··· 1972 1972 } 1973 1973 EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init); 1974 1974 1975 - int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1976 - void *data, unsigned long len) 1975 + int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 1976 + void *data, int offset, unsigned long len) 1977 1977 { 1978 1978 struct kvm_memslots *slots = kvm_memslots(kvm); 1979 1979 int r; 1980 + gpa_t gpa = ghc->gpa + offset; 1980 1981 1981 - BUG_ON(len > ghc->len); 1982 + BUG_ON(len + offset > ghc->len); 1982 1983 1983 1984 if (slots->generation != ghc->generation) 1984 1985 kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len); 1985 1986 1986 1987 if (unlikely(!ghc->memslot)) 1987 - return kvm_write_guest(kvm, ghc->gpa, data, len); 1988 + return kvm_write_guest(kvm, gpa, data, len); 1988 1989 1989 1990 if (kvm_is_error_hva(ghc->hva)) 1990 1991 return -EFAULT; 1991 1992 1992 - r = __copy_to_user((void __user *)ghc->hva, data, len); 1993 + r = __copy_to_user((void __user *)ghc->hva + offset, data, len); 1993 1994 if (r) 1994 1995 return -EFAULT; 1995 - mark_page_dirty_in_slot(ghc->memslot, ghc->gpa >> PAGE_SHIFT); 1996 + mark_page_dirty_in_slot(ghc->memslot, gpa >> PAGE_SHIFT); 1996 1997 1997 1998 return 0; 1999 + } 2000 + EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached); 2001 + 2002 + int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, 2003 + void *data, unsigned long len) 2004 + { 2005 + return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len); 1998 2006 } 1999 2007 EXPORT_SYMBOL_GPL(kvm_write_guest_cached); 2000 2008