]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
e172800e WD |
2 | #ifndef _ASM_GENERIC_RWSEM_H |
3 | #define _ASM_GENERIC_RWSEM_H | |
dd472da3 RK |
4 | |
5 | #ifndef _LINUX_RWSEM_H | |
6 | #error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead." | |
7 | #endif | |
8 | ||
9 | #ifdef __KERNEL__ | |
10 | ||
11 | /* | |
e172800e | 12 | * R/W semaphores originally for PPC using the stuff in lib/rwsem.c. |
dd472da3 RK |
13 | * Adapted largely from include/asm-i386/rwsem.h |
14 | * by Paul Mackerras <paulus@samba.org>. | |
15 | */ | |
16 | ||
17 | /* | |
18 | * the semaphore definition | |
19 | */ | |
e172800e | 20 | #ifdef CONFIG_64BIT |
dd472da3 RK |
21 | # define RWSEM_ACTIVE_MASK 0xffffffffL |
22 | #else | |
23 | # define RWSEM_ACTIVE_MASK 0x0000ffffL | |
24 | #endif | |
25 | ||
26 | #define RWSEM_UNLOCKED_VALUE 0x00000000L | |
27 | #define RWSEM_ACTIVE_BIAS 0x00000001L | |
28 | #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1) | |
29 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS | |
30 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) | |
31 | ||
32 | /* | |
33 | * lock for reading | |
34 | */ | |
35 | static inline void __down_read(struct rw_semaphore *sem) | |
36 | { | |
06321dd2 | 37 | if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) |
dd472da3 RK |
38 | rwsem_down_read_failed(sem); |
39 | } | |
40 | ||
41 | static inline int __down_read_trylock(struct rw_semaphore *sem) | |
42 | { | |
43 | long tmp; | |
44 | ||
8ee62b18 JL |
45 | while ((tmp = atomic_long_read(&sem->count)) >= 0) { |
46 | if (tmp == atomic_long_cmpxchg_acquire(&sem->count, tmp, | |
dd472da3 RK |
47 | tmp + RWSEM_ACTIVE_READ_BIAS)) { |
48 | return 1; | |
49 | } | |
50 | } | |
51 | return 0; | |
52 | } | |
53 | ||
54 | /* | |
55 | * lock for writing | |
56 | */ | |
f8e04d85 | 57 | static inline void __down_write(struct rw_semaphore *sem) |
dd472da3 RK |
58 | { |
59 | long tmp; | |
60 | ||
00eb4bab | 61 | tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS, |
06321dd2 | 62 | &sem->count); |
dd472da3 RK |
63 | if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS)) |
64 | rwsem_down_write_failed(sem); | |
65 | } | |
66 | ||
d4799608 MH |
67 | static inline int __down_write_killable(struct rw_semaphore *sem) |
68 | { | |
69 | long tmp; | |
70 | ||
71 | tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS, | |
06321dd2 | 72 | &sem->count); |
d4799608 MH |
73 | if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS)) |
74 | if (IS_ERR(rwsem_down_write_failed_killable(sem))) | |
75 | return -EINTR; | |
76 | return 0; | |
77 | } | |
78 | ||
dd472da3 RK |
79 | static inline int __down_write_trylock(struct rw_semaphore *sem) |
80 | { | |
81 | long tmp; | |
82 | ||
8ee62b18 | 83 | tmp = atomic_long_cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE, |
dd472da3 RK |
84 | RWSEM_ACTIVE_WRITE_BIAS); |
85 | return tmp == RWSEM_UNLOCKED_VALUE; | |
86 | } | |
87 | ||
88 | /* | |
89 | * unlock after reading | |
90 | */ | |
91 | static inline void __up_read(struct rw_semaphore *sem) | |
92 | { | |
93 | long tmp; | |
94 | ||
06321dd2 | 95 | tmp = atomic_long_dec_return_release(&sem->count); |
dd472da3 RK |
96 | if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)) |
97 | rwsem_wake(sem); | |
98 | } | |
99 | ||
100 | /* | |
101 | * unlock after writing | |
102 | */ | |
103 | static inline void __up_write(struct rw_semaphore *sem) | |
104 | { | |
00eb4bab | 105 | if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS, |
06321dd2 | 106 | &sem->count) < 0)) |
dd472da3 RK |
107 | rwsem_wake(sem); |
108 | } | |
109 | ||
dd472da3 RK |
110 | /* |
111 | * downgrade write lock to read lock | |
112 | */ | |
113 | static inline void __downgrade_write(struct rw_semaphore *sem) | |
114 | { | |
115 | long tmp; | |
116 | ||
00eb4bab DB |
117 | /* |
118 | * When downgrading from exclusive to shared ownership, | |
119 | * anything inside the write-locked region cannot leak | |
120 | * into the read side. In contrast, anything in the | |
121 | * read-locked region is ok to be re-ordered into the | |
122 | * write side. As such, rely on RELEASE semantics. | |
123 | */ | |
06321dd2 | 124 | tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS, &sem->count); |
dd472da3 RK |
125 | if (tmp < 0) |
126 | rwsem_downgrade_wake(sem); | |
127 | } | |
128 | ||
dd472da3 | 129 | #endif /* __KERNEL__ */ |
e172800e | 130 | #endif /* _ASM_GENERIC_RWSEM_H */ |