162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef __LINUX_SPINLOCK_H 362306a36Sopenharmony_ci#define __LINUX_SPINLOCK_H 462306a36Sopenharmony_ci#define __LINUX_INSIDE_SPINLOCK_H 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci/* 762306a36Sopenharmony_ci * include/linux/spinlock.h - generic spinlock/rwlock declarations 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * here's the role of the various spinlock/rwlock related include files: 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * on SMP builds: 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * asm/spinlock_types.h: contains the arch_spinlock_t/arch_rwlock_t and the 1462306a36Sopenharmony_ci * initializers 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * linux/spinlock_types_raw: 1762306a36Sopenharmony_ci * The raw types and initializers 1862306a36Sopenharmony_ci * linux/spinlock_types.h: 1962306a36Sopenharmony_ci * defines the generic type and initializers 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * asm/spinlock.h: contains the arch_spin_*()/etc. lowlevel 2262306a36Sopenharmony_ci * implementations, mostly inline assembly code 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * (also included on UP-debug builds:) 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * linux/spinlock_api_smp.h: 2762306a36Sopenharmony_ci * contains the prototypes for the _spin_*() APIs. 2862306a36Sopenharmony_ci * 2962306a36Sopenharmony_ci * linux/spinlock.h: builds the final spin_*() APIs. 3062306a36Sopenharmony_ci * 3162306a36Sopenharmony_ci * on UP builds: 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * linux/spinlock_type_up.h: 3462306a36Sopenharmony_ci * contains the generic, simplified UP spinlock type. 3562306a36Sopenharmony_ci * (which is an empty structure on non-debug builds) 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * linux/spinlock_types_raw: 3862306a36Sopenharmony_ci * The raw RT types and initializers 3962306a36Sopenharmony_ci * linux/spinlock_types.h: 4062306a36Sopenharmony_ci * defines the generic type and initializers 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * linux/spinlock_up.h: 4362306a36Sopenharmony_ci * contains the arch_spin_*()/etc. version of UP 4462306a36Sopenharmony_ci * builds. (which are NOPs on non-debug, non-preempt 4562306a36Sopenharmony_ci * builds) 4662306a36Sopenharmony_ci * 4762306a36Sopenharmony_ci * (included on UP-non-debug builds:) 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * linux/spinlock_api_up.h: 5062306a36Sopenharmony_ci * builds the _spin_*() APIs. 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * linux/spinlock.h: builds the final spin_*() APIs. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci#include <linux/typecheck.h> 5662306a36Sopenharmony_ci#include <linux/preempt.h> 5762306a36Sopenharmony_ci#include <linux/linkage.h> 5862306a36Sopenharmony_ci#include <linux/compiler.h> 5962306a36Sopenharmony_ci#include <linux/irqflags.h> 6062306a36Sopenharmony_ci#include <linux/thread_info.h> 6162306a36Sopenharmony_ci#include <linux/stringify.h> 6262306a36Sopenharmony_ci#include <linux/bottom_half.h> 6362306a36Sopenharmony_ci#include <linux/lockdep.h> 6462306a36Sopenharmony_ci#include <linux/cleanup.h> 6562306a36Sopenharmony_ci#include <asm/barrier.h> 6662306a36Sopenharmony_ci#include <asm/mmiowb.h> 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci/* 7062306a36Sopenharmony_ci * Must define these before including other files, inline functions need them 7162306a36Sopenharmony_ci */ 7262306a36Sopenharmony_ci#define LOCK_SECTION_NAME ".text..lock."KBUILD_BASENAME 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci#define LOCK_SECTION_START(extra) \ 7562306a36Sopenharmony_ci ".subsection 1\n\t" \ 7662306a36Sopenharmony_ci extra \ 7762306a36Sopenharmony_ci ".ifndef " LOCK_SECTION_NAME "\n\t" \ 7862306a36Sopenharmony_ci LOCK_SECTION_NAME ":\n\t" \ 7962306a36Sopenharmony_ci ".endif\n" 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci#define LOCK_SECTION_END \ 8262306a36Sopenharmony_ci ".previous\n\t" 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci#define __lockfunc __section(".spinlock.text") 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci/* 8762306a36Sopenharmony_ci * Pull the arch_spinlock_t and arch_rwlock_t definitions: 8862306a36Sopenharmony_ci */ 8962306a36Sopenharmony_ci#include <linux/spinlock_types.h> 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* 9262306a36Sopenharmony_ci * Pull the arch_spin*() functions/declarations (UP-nondebug doesn't need them): 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_ci#ifdef CONFIG_SMP 9562306a36Sopenharmony_ci# include <asm/spinlock.h> 9662306a36Sopenharmony_ci#else 9762306a36Sopenharmony_ci# include <linux/spinlock_up.h> 9862306a36Sopenharmony_ci#endif 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_SPINLOCK 10162306a36Sopenharmony_ci extern void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name, 10262306a36Sopenharmony_ci struct lock_class_key *key, short inner); 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci# define raw_spin_lock_init(lock) \ 10562306a36Sopenharmony_cido { \ 10662306a36Sopenharmony_ci static struct lock_class_key __key; \ 10762306a36Sopenharmony_ci \ 10862306a36Sopenharmony_ci __raw_spin_lock_init((lock), #lock, &__key, LD_WAIT_SPIN); \ 10962306a36Sopenharmony_ci} while (0) 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci#else 11262306a36Sopenharmony_ci# define raw_spin_lock_init(lock) \ 11362306a36Sopenharmony_ci do { *(lock) = __RAW_SPIN_LOCK_UNLOCKED(lock); } while (0) 11462306a36Sopenharmony_ci#endif 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci#define raw_spin_is_locked(lock) arch_spin_is_locked(&(lock)->raw_lock) 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci#ifdef arch_spin_is_contended 11962306a36Sopenharmony_ci#define raw_spin_is_contended(lock) arch_spin_is_contended(&(lock)->raw_lock) 12062306a36Sopenharmony_ci#else 12162306a36Sopenharmony_ci#define raw_spin_is_contended(lock) (((void)(lock), 0)) 12262306a36Sopenharmony_ci#endif /*arch_spin_is_contended*/ 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci/* 12562306a36Sopenharmony_ci * smp_mb__after_spinlock() provides the equivalent of a full memory barrier 12662306a36Sopenharmony_ci * between program-order earlier lock acquisitions and program-order later 12762306a36Sopenharmony_ci * memory accesses. 12862306a36Sopenharmony_ci * 12962306a36Sopenharmony_ci * This guarantees that the following two properties hold: 13062306a36Sopenharmony_ci * 13162306a36Sopenharmony_ci * 1) Given the snippet: 13262306a36Sopenharmony_ci * 13362306a36Sopenharmony_ci * { X = 0; Y = 0; } 13462306a36Sopenharmony_ci * 13562306a36Sopenharmony_ci * CPU0 CPU1 13662306a36Sopenharmony_ci * 13762306a36Sopenharmony_ci * WRITE_ONCE(X, 1); WRITE_ONCE(Y, 1); 13862306a36Sopenharmony_ci * spin_lock(S); smp_mb(); 13962306a36Sopenharmony_ci * smp_mb__after_spinlock(); r1 = READ_ONCE(X); 14062306a36Sopenharmony_ci * r0 = READ_ONCE(Y); 14162306a36Sopenharmony_ci * spin_unlock(S); 14262306a36Sopenharmony_ci * 14362306a36Sopenharmony_ci * it is forbidden that CPU0 does not observe CPU1's store to Y (r0 = 0) 14462306a36Sopenharmony_ci * and CPU1 does not observe CPU0's store to X (r1 = 0); see the comments 14562306a36Sopenharmony_ci * preceding the call to smp_mb__after_spinlock() in __schedule() and in 14662306a36Sopenharmony_ci * try_to_wake_up(). 14762306a36Sopenharmony_ci * 14862306a36Sopenharmony_ci * 2) Given the snippet: 14962306a36Sopenharmony_ci * 15062306a36Sopenharmony_ci * { X = 0; Y = 0; } 15162306a36Sopenharmony_ci * 15262306a36Sopenharmony_ci * CPU0 CPU1 CPU2 15362306a36Sopenharmony_ci * 15462306a36Sopenharmony_ci * spin_lock(S); spin_lock(S); r1 = READ_ONCE(Y); 15562306a36Sopenharmony_ci * WRITE_ONCE(X, 1); smp_mb__after_spinlock(); smp_rmb(); 15662306a36Sopenharmony_ci * spin_unlock(S); r0 = READ_ONCE(X); r2 = READ_ONCE(X); 15762306a36Sopenharmony_ci * WRITE_ONCE(Y, 1); 15862306a36Sopenharmony_ci * spin_unlock(S); 15962306a36Sopenharmony_ci * 16062306a36Sopenharmony_ci * it is forbidden that CPU0's critical section executes before CPU1's 16162306a36Sopenharmony_ci * critical section (r0 = 1), CPU2 observes CPU1's store to Y (r1 = 1) 16262306a36Sopenharmony_ci * and CPU2 does not observe CPU0's store to X (r2 = 0); see the comments 16362306a36Sopenharmony_ci * preceding the calls to smp_rmb() in try_to_wake_up() for similar 16462306a36Sopenharmony_ci * snippets but "projected" onto two CPUs. 16562306a36Sopenharmony_ci * 16662306a36Sopenharmony_ci * Property (2) upgrades the lock to an RCsc lock. 16762306a36Sopenharmony_ci * 16862306a36Sopenharmony_ci * Since most load-store architectures implement ACQUIRE with an smp_mb() after 16962306a36Sopenharmony_ci * the LL/SC loop, they need no further barriers. Similarly all our TSO 17062306a36Sopenharmony_ci * architectures imply an smp_mb() for each atomic instruction and equally don't 17162306a36Sopenharmony_ci * need more. 17262306a36Sopenharmony_ci * 17362306a36Sopenharmony_ci * Architectures that can implement ACQUIRE better need to take care. 17462306a36Sopenharmony_ci */ 17562306a36Sopenharmony_ci#ifndef smp_mb__after_spinlock 17662306a36Sopenharmony_ci#define smp_mb__after_spinlock() kcsan_mb() 17762306a36Sopenharmony_ci#endif 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_SPINLOCK 18062306a36Sopenharmony_ci extern void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock); 18162306a36Sopenharmony_ci extern int do_raw_spin_trylock(raw_spinlock_t *lock); 18262306a36Sopenharmony_ci extern void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock); 18362306a36Sopenharmony_ci#else 18462306a36Sopenharmony_cistatic inline void do_raw_spin_lock(raw_spinlock_t *lock) __acquires(lock) 18562306a36Sopenharmony_ci{ 18662306a36Sopenharmony_ci __acquire(lock); 18762306a36Sopenharmony_ci arch_spin_lock(&lock->raw_lock); 18862306a36Sopenharmony_ci mmiowb_spin_lock(); 18962306a36Sopenharmony_ci} 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_cistatic inline int do_raw_spin_trylock(raw_spinlock_t *lock) 19262306a36Sopenharmony_ci{ 19362306a36Sopenharmony_ci int ret = arch_spin_trylock(&(lock)->raw_lock); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci if (ret) 19662306a36Sopenharmony_ci mmiowb_spin_lock(); 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci return ret; 19962306a36Sopenharmony_ci} 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_cistatic inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock) 20262306a36Sopenharmony_ci{ 20362306a36Sopenharmony_ci mmiowb_spin_unlock(); 20462306a36Sopenharmony_ci arch_spin_unlock(&lock->raw_lock); 20562306a36Sopenharmony_ci __release(lock); 20662306a36Sopenharmony_ci} 20762306a36Sopenharmony_ci#endif 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci/* 21062306a36Sopenharmony_ci * Define the various spin_lock methods. Note we define these 21162306a36Sopenharmony_ci * regardless of whether CONFIG_SMP or CONFIG_PREEMPTION are set. The 21262306a36Sopenharmony_ci * various methods are defined as nops in the case they are not 21362306a36Sopenharmony_ci * required. 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_ci#define raw_spin_trylock(lock) __cond_lock(lock, _raw_spin_trylock(lock)) 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci#define raw_spin_lock(lock) _raw_spin_lock(lock) 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_LOCK_ALLOC 22062306a36Sopenharmony_ci# define raw_spin_lock_nested(lock, subclass) \ 22162306a36Sopenharmony_ci _raw_spin_lock_nested(lock, subclass) 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci# define raw_spin_lock_nest_lock(lock, nest_lock) \ 22462306a36Sopenharmony_ci do { \ 22562306a36Sopenharmony_ci typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\ 22662306a36Sopenharmony_ci _raw_spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \ 22762306a36Sopenharmony_ci } while (0) 22862306a36Sopenharmony_ci#else 22962306a36Sopenharmony_ci/* 23062306a36Sopenharmony_ci * Always evaluate the 'subclass' argument to avoid that the compiler 23162306a36Sopenharmony_ci * warns about set-but-not-used variables when building with 23262306a36Sopenharmony_ci * CONFIG_DEBUG_LOCK_ALLOC=n and with W=1. 23362306a36Sopenharmony_ci */ 23462306a36Sopenharmony_ci# define raw_spin_lock_nested(lock, subclass) \ 23562306a36Sopenharmony_ci _raw_spin_lock(((void)(subclass), (lock))) 23662306a36Sopenharmony_ci# define raw_spin_lock_nest_lock(lock, nest_lock) _raw_spin_lock(lock) 23762306a36Sopenharmony_ci#endif 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci#define raw_spin_lock_irqsave(lock, flags) \ 24262306a36Sopenharmony_ci do { \ 24362306a36Sopenharmony_ci typecheck(unsigned long, flags); \ 24462306a36Sopenharmony_ci flags = _raw_spin_lock_irqsave(lock); \ 24562306a36Sopenharmony_ci } while (0) 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_LOCK_ALLOC 24862306a36Sopenharmony_ci#define raw_spin_lock_irqsave_nested(lock, flags, subclass) \ 24962306a36Sopenharmony_ci do { \ 25062306a36Sopenharmony_ci typecheck(unsigned long, flags); \ 25162306a36Sopenharmony_ci flags = _raw_spin_lock_irqsave_nested(lock, subclass); \ 25262306a36Sopenharmony_ci } while (0) 25362306a36Sopenharmony_ci#else 25462306a36Sopenharmony_ci#define raw_spin_lock_irqsave_nested(lock, flags, subclass) \ 25562306a36Sopenharmony_ci do { \ 25662306a36Sopenharmony_ci typecheck(unsigned long, flags); \ 25762306a36Sopenharmony_ci flags = _raw_spin_lock_irqsave(lock); \ 25862306a36Sopenharmony_ci } while (0) 25962306a36Sopenharmony_ci#endif 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci#else 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci#define raw_spin_lock_irqsave(lock, flags) \ 26462306a36Sopenharmony_ci do { \ 26562306a36Sopenharmony_ci typecheck(unsigned long, flags); \ 26662306a36Sopenharmony_ci _raw_spin_lock_irqsave(lock, flags); \ 26762306a36Sopenharmony_ci } while (0) 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci#define raw_spin_lock_irqsave_nested(lock, flags, subclass) \ 27062306a36Sopenharmony_ci raw_spin_lock_irqsave(lock, flags) 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci#endif 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci#define raw_spin_lock_irq(lock) _raw_spin_lock_irq(lock) 27562306a36Sopenharmony_ci#define raw_spin_lock_bh(lock) _raw_spin_lock_bh(lock) 27662306a36Sopenharmony_ci#define raw_spin_unlock(lock) _raw_spin_unlock(lock) 27762306a36Sopenharmony_ci#define raw_spin_unlock_irq(lock) _raw_spin_unlock_irq(lock) 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci#define raw_spin_unlock_irqrestore(lock, flags) \ 28062306a36Sopenharmony_ci do { \ 28162306a36Sopenharmony_ci typecheck(unsigned long, flags); \ 28262306a36Sopenharmony_ci _raw_spin_unlock_irqrestore(lock, flags); \ 28362306a36Sopenharmony_ci } while (0) 28462306a36Sopenharmony_ci#define raw_spin_unlock_bh(lock) _raw_spin_unlock_bh(lock) 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci#define raw_spin_trylock_bh(lock) \ 28762306a36Sopenharmony_ci __cond_lock(lock, _raw_spin_trylock_bh(lock)) 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci#define raw_spin_trylock_irq(lock) \ 29062306a36Sopenharmony_ci({ \ 29162306a36Sopenharmony_ci local_irq_disable(); \ 29262306a36Sopenharmony_ci raw_spin_trylock(lock) ? \ 29362306a36Sopenharmony_ci 1 : ({ local_irq_enable(); 0; }); \ 29462306a36Sopenharmony_ci}) 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci#define raw_spin_trylock_irqsave(lock, flags) \ 29762306a36Sopenharmony_ci({ \ 29862306a36Sopenharmony_ci local_irq_save(flags); \ 29962306a36Sopenharmony_ci raw_spin_trylock(lock) ? \ 30062306a36Sopenharmony_ci 1 : ({ local_irq_restore(flags); 0; }); \ 30162306a36Sopenharmony_ci}) 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci#ifndef CONFIG_PREEMPT_RT 30462306a36Sopenharmony_ci/* Include rwlock functions for !RT */ 30562306a36Sopenharmony_ci#include <linux/rwlock.h> 30662306a36Sopenharmony_ci#endif 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci/* 30962306a36Sopenharmony_ci * Pull the _spin_*()/_read_*()/_write_*() functions/declarations: 31062306a36Sopenharmony_ci */ 31162306a36Sopenharmony_ci#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 31262306a36Sopenharmony_ci# include <linux/spinlock_api_smp.h> 31362306a36Sopenharmony_ci#else 31462306a36Sopenharmony_ci# include <linux/spinlock_api_up.h> 31562306a36Sopenharmony_ci#endif 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci/* Non PREEMPT_RT kernel, map to raw spinlocks: */ 31862306a36Sopenharmony_ci#ifndef CONFIG_PREEMPT_RT 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci/* 32162306a36Sopenharmony_ci * Map the spin_lock functions to the raw variants for PREEMPT_RT=n 32262306a36Sopenharmony_ci */ 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_cistatic __always_inline raw_spinlock_t *spinlock_check(spinlock_t *lock) 32562306a36Sopenharmony_ci{ 32662306a36Sopenharmony_ci return &lock->rlock; 32762306a36Sopenharmony_ci} 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_SPINLOCK 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci# define spin_lock_init(lock) \ 33262306a36Sopenharmony_cido { \ 33362306a36Sopenharmony_ci static struct lock_class_key __key; \ 33462306a36Sopenharmony_ci \ 33562306a36Sopenharmony_ci __raw_spin_lock_init(spinlock_check(lock), \ 33662306a36Sopenharmony_ci #lock, &__key, LD_WAIT_CONFIG); \ 33762306a36Sopenharmony_ci} while (0) 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci#else 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci# define spin_lock_init(_lock) \ 34262306a36Sopenharmony_cido { \ 34362306a36Sopenharmony_ci spinlock_check(_lock); \ 34462306a36Sopenharmony_ci *(_lock) = __SPIN_LOCK_UNLOCKED(_lock); \ 34562306a36Sopenharmony_ci} while (0) 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ci#endif 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_cistatic __always_inline void spin_lock(spinlock_t *lock) 35062306a36Sopenharmony_ci{ 35162306a36Sopenharmony_ci raw_spin_lock(&lock->rlock); 35262306a36Sopenharmony_ci} 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_cistatic __always_inline void spin_lock_bh(spinlock_t *lock) 35562306a36Sopenharmony_ci{ 35662306a36Sopenharmony_ci raw_spin_lock_bh(&lock->rlock); 35762306a36Sopenharmony_ci} 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_cistatic __always_inline int spin_trylock(spinlock_t *lock) 36062306a36Sopenharmony_ci{ 36162306a36Sopenharmony_ci return raw_spin_trylock(&lock->rlock); 36262306a36Sopenharmony_ci} 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci#define spin_lock_nested(lock, subclass) \ 36562306a36Sopenharmony_cido { \ 36662306a36Sopenharmony_ci raw_spin_lock_nested(spinlock_check(lock), subclass); \ 36762306a36Sopenharmony_ci} while (0) 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci#define spin_lock_nest_lock(lock, nest_lock) \ 37062306a36Sopenharmony_cido { \ 37162306a36Sopenharmony_ci raw_spin_lock_nest_lock(spinlock_check(lock), nest_lock); \ 37262306a36Sopenharmony_ci} while (0) 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_cistatic __always_inline void spin_lock_irq(spinlock_t *lock) 37562306a36Sopenharmony_ci{ 37662306a36Sopenharmony_ci raw_spin_lock_irq(&lock->rlock); 37762306a36Sopenharmony_ci} 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci#define spin_lock_irqsave(lock, flags) \ 38062306a36Sopenharmony_cido { \ 38162306a36Sopenharmony_ci raw_spin_lock_irqsave(spinlock_check(lock), flags); \ 38262306a36Sopenharmony_ci} while (0) 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci#define spin_lock_irqsave_nested(lock, flags, subclass) \ 38562306a36Sopenharmony_cido { \ 38662306a36Sopenharmony_ci raw_spin_lock_irqsave_nested(spinlock_check(lock), flags, subclass); \ 38762306a36Sopenharmony_ci} while (0) 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_cistatic __always_inline void spin_unlock(spinlock_t *lock) 39062306a36Sopenharmony_ci{ 39162306a36Sopenharmony_ci raw_spin_unlock(&lock->rlock); 39262306a36Sopenharmony_ci} 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_cistatic __always_inline void spin_unlock_bh(spinlock_t *lock) 39562306a36Sopenharmony_ci{ 39662306a36Sopenharmony_ci raw_spin_unlock_bh(&lock->rlock); 39762306a36Sopenharmony_ci} 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_cistatic __always_inline void spin_unlock_irq(spinlock_t *lock) 40062306a36Sopenharmony_ci{ 40162306a36Sopenharmony_ci raw_spin_unlock_irq(&lock->rlock); 40262306a36Sopenharmony_ci} 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_cistatic __always_inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags) 40562306a36Sopenharmony_ci{ 40662306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&lock->rlock, flags); 40762306a36Sopenharmony_ci} 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_cistatic __always_inline int spin_trylock_bh(spinlock_t *lock) 41062306a36Sopenharmony_ci{ 41162306a36Sopenharmony_ci return raw_spin_trylock_bh(&lock->rlock); 41262306a36Sopenharmony_ci} 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_cistatic __always_inline int spin_trylock_irq(spinlock_t *lock) 41562306a36Sopenharmony_ci{ 41662306a36Sopenharmony_ci return raw_spin_trylock_irq(&lock->rlock); 41762306a36Sopenharmony_ci} 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci#define spin_trylock_irqsave(lock, flags) \ 42062306a36Sopenharmony_ci({ \ 42162306a36Sopenharmony_ci raw_spin_trylock_irqsave(spinlock_check(lock), flags); \ 42262306a36Sopenharmony_ci}) 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci/** 42562306a36Sopenharmony_ci * spin_is_locked() - Check whether a spinlock is locked. 42662306a36Sopenharmony_ci * @lock: Pointer to the spinlock. 42762306a36Sopenharmony_ci * 42862306a36Sopenharmony_ci * This function is NOT required to provide any memory ordering 42962306a36Sopenharmony_ci * guarantees; it could be used for debugging purposes or, when 43062306a36Sopenharmony_ci * additional synchronization is needed, accompanied with other 43162306a36Sopenharmony_ci * constructs (memory barriers) enforcing the synchronization. 43262306a36Sopenharmony_ci * 43362306a36Sopenharmony_ci * Returns: 1 if @lock is locked, 0 otherwise. 43462306a36Sopenharmony_ci * 43562306a36Sopenharmony_ci * Note that the function only tells you that the spinlock is 43662306a36Sopenharmony_ci * seen to be locked, not that it is locked on your CPU. 43762306a36Sopenharmony_ci * 43862306a36Sopenharmony_ci * Further, on CONFIG_SMP=n builds with CONFIG_DEBUG_SPINLOCK=n, 43962306a36Sopenharmony_ci * the return value is always 0 (see include/linux/spinlock_up.h). 44062306a36Sopenharmony_ci * Therefore you should not rely heavily on the return value. 44162306a36Sopenharmony_ci */ 44262306a36Sopenharmony_cistatic __always_inline int spin_is_locked(spinlock_t *lock) 44362306a36Sopenharmony_ci{ 44462306a36Sopenharmony_ci return raw_spin_is_locked(&lock->rlock); 44562306a36Sopenharmony_ci} 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_cistatic __always_inline int spin_is_contended(spinlock_t *lock) 44862306a36Sopenharmony_ci{ 44962306a36Sopenharmony_ci return raw_spin_is_contended(&lock->rlock); 45062306a36Sopenharmony_ci} 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci#define assert_spin_locked(lock) assert_raw_spin_locked(&(lock)->rlock) 45362306a36Sopenharmony_ci 45462306a36Sopenharmony_ci#else /* !CONFIG_PREEMPT_RT */ 45562306a36Sopenharmony_ci# include <linux/spinlock_rt.h> 45662306a36Sopenharmony_ci#endif /* CONFIG_PREEMPT_RT */ 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci/* 45962306a36Sopenharmony_ci * Pull the atomic_t declaration: 46062306a36Sopenharmony_ci * (asm-mips/atomic.h needs above definitions) 46162306a36Sopenharmony_ci */ 46262306a36Sopenharmony_ci#include <linux/atomic.h> 46362306a36Sopenharmony_ci/** 46462306a36Sopenharmony_ci * atomic_dec_and_lock - lock on reaching reference count zero 46562306a36Sopenharmony_ci * @atomic: the atomic counter 46662306a36Sopenharmony_ci * @lock: the spinlock in question 46762306a36Sopenharmony_ci * 46862306a36Sopenharmony_ci * Decrements @atomic by 1. If the result is 0, returns true and locks 46962306a36Sopenharmony_ci * @lock. Returns false for all other cases. 47062306a36Sopenharmony_ci */ 47162306a36Sopenharmony_ciextern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock); 47262306a36Sopenharmony_ci#define atomic_dec_and_lock(atomic, lock) \ 47362306a36Sopenharmony_ci __cond_lock(lock, _atomic_dec_and_lock(atomic, lock)) 47462306a36Sopenharmony_ci 47562306a36Sopenharmony_ciextern int _atomic_dec_and_lock_irqsave(atomic_t *atomic, spinlock_t *lock, 47662306a36Sopenharmony_ci unsigned long *flags); 47762306a36Sopenharmony_ci#define atomic_dec_and_lock_irqsave(atomic, lock, flags) \ 47862306a36Sopenharmony_ci __cond_lock(lock, _atomic_dec_and_lock_irqsave(atomic, lock, &(flags))) 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ciextern int _atomic_dec_and_raw_lock(atomic_t *atomic, raw_spinlock_t *lock); 48162306a36Sopenharmony_ci#define atomic_dec_and_raw_lock(atomic, lock) \ 48262306a36Sopenharmony_ci __cond_lock(lock, _atomic_dec_and_raw_lock(atomic, lock)) 48362306a36Sopenharmony_ci 48462306a36Sopenharmony_ciextern int _atomic_dec_and_raw_lock_irqsave(atomic_t *atomic, raw_spinlock_t *lock, 48562306a36Sopenharmony_ci unsigned long *flags); 48662306a36Sopenharmony_ci#define atomic_dec_and_raw_lock_irqsave(atomic, lock, flags) \ 48762306a36Sopenharmony_ci __cond_lock(lock, _atomic_dec_and_raw_lock_irqsave(atomic, lock, &(flags))) 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ciint __alloc_bucket_spinlocks(spinlock_t **locks, unsigned int *lock_mask, 49062306a36Sopenharmony_ci size_t max_size, unsigned int cpu_mult, 49162306a36Sopenharmony_ci gfp_t gfp, const char *name, 49262306a36Sopenharmony_ci struct lock_class_key *key); 49362306a36Sopenharmony_ci 49462306a36Sopenharmony_ci#define alloc_bucket_spinlocks(locks, lock_mask, max_size, cpu_mult, gfp) \ 49562306a36Sopenharmony_ci ({ \ 49662306a36Sopenharmony_ci static struct lock_class_key key; \ 49762306a36Sopenharmony_ci int ret; \ 49862306a36Sopenharmony_ci \ 49962306a36Sopenharmony_ci ret = __alloc_bucket_spinlocks(locks, lock_mask, max_size, \ 50062306a36Sopenharmony_ci cpu_mult, gfp, #locks, &key); \ 50162306a36Sopenharmony_ci ret; \ 50262306a36Sopenharmony_ci }) 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_civoid free_bucket_spinlocks(spinlock_t *locks); 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(raw_spinlock, raw_spinlock_t, 50762306a36Sopenharmony_ci raw_spin_lock(_T->lock), 50862306a36Sopenharmony_ci raw_spin_unlock(_T->lock)) 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(raw_spinlock_nested, raw_spinlock_t, 51162306a36Sopenharmony_ci raw_spin_lock_nested(_T->lock, SINGLE_DEPTH_NESTING), 51262306a36Sopenharmony_ci raw_spin_unlock(_T->lock)) 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(raw_spinlock_irq, raw_spinlock_t, 51562306a36Sopenharmony_ci raw_spin_lock_irq(_T->lock), 51662306a36Sopenharmony_ci raw_spin_unlock_irq(_T->lock)) 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(raw_spinlock_irqsave, raw_spinlock_t, 51962306a36Sopenharmony_ci raw_spin_lock_irqsave(_T->lock, _T->flags), 52062306a36Sopenharmony_ci raw_spin_unlock_irqrestore(_T->lock, _T->flags), 52162306a36Sopenharmony_ci unsigned long flags) 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(spinlock, spinlock_t, 52462306a36Sopenharmony_ci spin_lock(_T->lock), 52562306a36Sopenharmony_ci spin_unlock(_T->lock)) 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(spinlock_irq, spinlock_t, 52862306a36Sopenharmony_ci spin_lock_irq(_T->lock), 52962306a36Sopenharmony_ci spin_unlock_irq(_T->lock)) 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_ciDEFINE_LOCK_GUARD_1(spinlock_irqsave, spinlock_t, 53262306a36Sopenharmony_ci spin_lock_irqsave(_T->lock, _T->flags), 53362306a36Sopenharmony_ci spin_unlock_irqrestore(_T->lock, _T->flags), 53462306a36Sopenharmony_ci unsigned long flags) 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci#undef __LINUX_INSIDE_SPINLOCK_H 53762306a36Sopenharmony_ci#endif /* __LINUX_SPINLOCK_H */ 538