tty_ldsem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Ldisc rw semaphore
  3. *
  4. * The ldisc semaphore is semantically a rw_semaphore but which enforces
  5. * an alternate policy, namely:
  6. * 1) Supports lock wait timeouts
  7. * 2) Write waiter has priority
  8. * 3) Downgrading is not supported
  9. *
  10. * Implementation notes:
  11. * 1) Upper half of semaphore count is a wait count (differs from rwsem
  12. * in that rwsem normalizes the upper half to the wait bias)
  13. * 2) Lacks overflow checking
  14. *
  15. * The generic counting was copied and modified from include/asm-generic/rwsem.h
  16. * by Paul Mackerras <paulus@samba.org>.
  17. *
  18. * The scheduling policy was copied and modified from lib/rwsem.c
  19. * Written by David Howells (dhowells@redhat.com).
  20. *
  21. * This implementation incorporates the write lock stealing work of
  22. * Michel Lespinasse <walken@google.com>.
  23. *
  24. * Copyright (C) 2013 Peter Hurley <peter@hurleysoftware.com>
  25. *
  26. * This file may be redistributed under the terms of the GNU General Public
  27. * License v2.
  28. */
  29. #include <linux/list.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/atomic.h>
  32. #include <linux/tty.h>
  33. #include <linux/sched.h>
  34. #include <linux/sched/debug.h>
  35. #include <linux/sched/task.h>
  36. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  37. # define __acq(l, s, t, r, c, n, i) \
  38. lock_acquire(&(l)->dep_map, s, t, r, c, n, i)
  39. # define __rel(l, n, i) \
  40. lock_release(&(l)->dep_map, n, i)
  41. #define lockdep_acquire(l, s, t, i) __acq(l, s, t, 0, 1, NULL, i)
  42. #define lockdep_acquire_nest(l, s, t, n, i) __acq(l, s, t, 0, 1, n, i)
  43. #define lockdep_acquire_read(l, s, t, i) __acq(l, s, t, 1, 1, NULL, i)
  44. #define lockdep_release(l, n, i) __rel(l, n, i)
  45. #else
  46. # define lockdep_acquire(l, s, t, i) do { } while (0)
  47. # define lockdep_acquire_nest(l, s, t, n, i) do { } while (0)
  48. # define lockdep_acquire_read(l, s, t, i) do { } while (0)
  49. # define lockdep_release(l, n, i) do { } while (0)
  50. #endif
  51. #ifdef CONFIG_LOCK_STAT
  52. # define lock_stat(_lock, stat) lock_##stat(&(_lock)->dep_map, _RET_IP_)
  53. #else
  54. # define lock_stat(_lock, stat) do { } while (0)
  55. #endif
  56. #if BITS_PER_LONG == 64
  57. # define LDSEM_ACTIVE_MASK 0xffffffffL
  58. #else
  59. # define LDSEM_ACTIVE_MASK 0x0000ffffL
  60. #endif
  61. #define LDSEM_UNLOCKED 0L
  62. #define LDSEM_ACTIVE_BIAS 1L
  63. #define LDSEM_WAIT_BIAS (-LDSEM_ACTIVE_MASK-1)
  64. #define LDSEM_READ_BIAS LDSEM_ACTIVE_BIAS
  65. #define LDSEM_WRITE_BIAS (LDSEM_WAIT_BIAS + LDSEM_ACTIVE_BIAS)
  66. struct ldsem_waiter {
  67. struct list_head list;
  68. struct task_struct *task;
  69. };
  70. static inline long ldsem_atomic_update(long delta, struct ld_semaphore *sem)
  71. {
  72. return atomic_long_add_return(delta, (atomic_long_t *)&sem->count);
  73. }
  74. /*
  75. * ldsem_cmpxchg() updates @*old with the last-known sem->count value.
  76. * Returns 1 if count was successfully changed; @*old will have @new value.
  77. * Returns 0 if count was not changed; @*old will have most recent sem->count
  78. */
  79. static inline int ldsem_cmpxchg(long *old, long new, struct ld_semaphore *sem)
  80. {
  81. long tmp = atomic_long_cmpxchg(&sem->count, *old, new);
  82. if (tmp == *old) {
  83. *old = new;
  84. return 1;
  85. } else {
  86. *old = tmp;
  87. return 0;
  88. }
  89. }
  90. /*
  91. * Initialize an ldsem:
  92. */
  93. void __init_ldsem(struct ld_semaphore *sem, const char *name,
  94. struct lock_class_key *key)
  95. {
  96. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  97. /*
  98. * Make sure we are not reinitializing a held semaphore:
  99. */
  100. debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  101. lockdep_init_map(&sem->dep_map, name, key, 0);
  102. #endif
  103. sem->count = LDSEM_UNLOCKED;
  104. sem->wait_readers = 0;
  105. raw_spin_lock_init(&sem->wait_lock);
  106. INIT_LIST_HEAD(&sem->read_wait);
  107. INIT_LIST_HEAD(&sem->write_wait);
  108. }
  109. static void __ldsem_wake_readers(struct ld_semaphore *sem)
  110. {
  111. struct ldsem_waiter *waiter, *next;
  112. struct task_struct *tsk;
  113. long adjust, count;
  114. /* Try to grant read locks to all readers on the read wait list.
  115. * Note the 'active part' of the count is incremented by
  116. * the number of readers before waking any processes up.
  117. */
  118. adjust = sem->wait_readers * (LDSEM_ACTIVE_BIAS - LDSEM_WAIT_BIAS);
  119. count = ldsem_atomic_update(adjust, sem);
  120. do {
  121. if (count > 0)
  122. break;
  123. if (ldsem_cmpxchg(&count, count - adjust, sem))
  124. return;
  125. } while (1);
  126. list_for_each_entry_safe(waiter, next, &sem->read_wait, list) {
  127. tsk = waiter->task;
  128. smp_mb();
  129. waiter->task = NULL;
  130. wake_up_process(tsk);
  131. put_task_struct(tsk);
  132. }
  133. INIT_LIST_HEAD(&sem->read_wait);
  134. sem->wait_readers = 0;
  135. }
  136. static inline int writer_trylock(struct ld_semaphore *sem)
  137. {
  138. /* only wake this writer if the active part of the count can be
  139. * transitioned from 0 -> 1
  140. */
  141. long count = ldsem_atomic_update(LDSEM_ACTIVE_BIAS, sem);
  142. do {
  143. if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS)
  144. return 1;
  145. if (ldsem_cmpxchg(&count, count - LDSEM_ACTIVE_BIAS, sem))
  146. return 0;
  147. } while (1);
  148. }
  149. static void __ldsem_wake_writer(struct ld_semaphore *sem)
  150. {
  151. struct ldsem_waiter *waiter;
  152. waiter = list_entry(sem->write_wait.next, struct ldsem_waiter, list);
  153. wake_up_process(waiter->task);
  154. }
  155. /*
  156. * handle the lock release when processes blocked on it that can now run
  157. * - if we come here from up_xxxx(), then:
  158. * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
  159. * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
  160. * - the spinlock must be held by the caller
  161. * - woken process blocks are discarded from the list after having task zeroed
  162. */
  163. static void __ldsem_wake(struct ld_semaphore *sem)
  164. {
  165. if (!list_empty(&sem->write_wait))
  166. __ldsem_wake_writer(sem);
  167. else if (!list_empty(&sem->read_wait))
  168. __ldsem_wake_readers(sem);
  169. }
  170. static void ldsem_wake(struct ld_semaphore *sem)
  171. {
  172. unsigned long flags;
  173. raw_spin_lock_irqsave(&sem->wait_lock, flags);
  174. __ldsem_wake(sem);
  175. raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
  176. }
  177. /*
  178. * wait for the read lock to be granted
  179. */
  180. static struct ld_semaphore __sched *
  181. down_read_failed(struct ld_semaphore *sem, long count, long timeout)
  182. {
  183. struct ldsem_waiter waiter;
  184. long adjust = -LDSEM_ACTIVE_BIAS + LDSEM_WAIT_BIAS;
  185. /* set up my own style of waitqueue */
  186. raw_spin_lock_irq(&sem->wait_lock);
  187. /* Try to reverse the lock attempt but if the count has changed
  188. * so that reversing fails, check if there are are no waiters,
  189. * and early-out if not */
  190. do {
  191. if (ldsem_cmpxchg(&count, count + adjust, sem))
  192. break;
  193. if (count > 0) {
  194. raw_spin_unlock_irq(&sem->wait_lock);
  195. return sem;
  196. }
  197. } while (1);
  198. list_add_tail(&waiter.list, &sem->read_wait);
  199. sem->wait_readers++;
  200. waiter.task = current;
  201. get_task_struct(current);
  202. /* if there are no active locks, wake the new lock owner(s) */
  203. if ((count & LDSEM_ACTIVE_MASK) == 0)
  204. __ldsem_wake(sem);
  205. raw_spin_unlock_irq(&sem->wait_lock);
  206. /* wait to be given the lock */
  207. for (;;) {
  208. set_current_state(TASK_UNINTERRUPTIBLE);
  209. if (!waiter.task)
  210. break;
  211. if (!timeout)
  212. break;
  213. timeout = schedule_timeout(timeout);
  214. }
  215. __set_current_state(TASK_RUNNING);
  216. if (!timeout) {
  217. /* lock timed out but check if this task was just
  218. * granted lock ownership - if so, pretend there
  219. * was no timeout; otherwise, cleanup lock wait */
  220. raw_spin_lock_irq(&sem->wait_lock);
  221. if (waiter.task) {
  222. ldsem_atomic_update(-LDSEM_WAIT_BIAS, sem);
  223. list_del(&waiter.list);
  224. raw_spin_unlock_irq(&sem->wait_lock);
  225. put_task_struct(waiter.task);
  226. return NULL;
  227. }
  228. raw_spin_unlock_irq(&sem->wait_lock);
  229. }
  230. return sem;
  231. }
  232. /*
  233. * wait for the write lock to be granted
  234. */
  235. static struct ld_semaphore __sched *
  236. down_write_failed(struct ld_semaphore *sem, long count, long timeout)
  237. {
  238. struct ldsem_waiter waiter;
  239. long adjust = -LDSEM_ACTIVE_BIAS;
  240. int locked = 0;
  241. /* set up my own style of waitqueue */
  242. raw_spin_lock_irq(&sem->wait_lock);
  243. /* Try to reverse the lock attempt but if the count has changed
  244. * so that reversing fails, check if the lock is now owned,
  245. * and early-out if so */
  246. do {
  247. if (ldsem_cmpxchg(&count, count + adjust, sem))
  248. break;
  249. if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS) {
  250. raw_spin_unlock_irq(&sem->wait_lock);
  251. return sem;
  252. }
  253. } while (1);
  254. list_add_tail(&waiter.list, &sem->write_wait);
  255. waiter.task = current;
  256. set_current_state(TASK_UNINTERRUPTIBLE);
  257. for (;;) {
  258. if (!timeout)
  259. break;
  260. raw_spin_unlock_irq(&sem->wait_lock);
  261. timeout = schedule_timeout(timeout);
  262. raw_spin_lock_irq(&sem->wait_lock);
  263. set_current_state(TASK_UNINTERRUPTIBLE);
  264. locked = writer_trylock(sem);
  265. if (locked)
  266. break;
  267. }
  268. if (!locked)
  269. ldsem_atomic_update(-LDSEM_WAIT_BIAS, sem);
  270. list_del(&waiter.list);
  271. raw_spin_unlock_irq(&sem->wait_lock);
  272. __set_current_state(TASK_RUNNING);
  273. /* lock wait may have timed out */
  274. if (!locked)
  275. return NULL;
  276. return sem;
  277. }
  278. static int __ldsem_down_read_nested(struct ld_semaphore *sem,
  279. int subclass, long timeout)
  280. {
  281. long count;
  282. lockdep_acquire_read(sem, subclass, 0, _RET_IP_);
  283. count = ldsem_atomic_update(LDSEM_READ_BIAS, sem);
  284. if (count <= 0) {
  285. lock_stat(sem, contended);
  286. if (!down_read_failed(sem, count, timeout)) {
  287. lockdep_release(sem, 1, _RET_IP_);
  288. return 0;
  289. }
  290. }
  291. lock_stat(sem, acquired);
  292. return 1;
  293. }
  294. static int __ldsem_down_write_nested(struct ld_semaphore *sem,
  295. int subclass, long timeout)
  296. {
  297. long count;
  298. lockdep_acquire(sem, subclass, 0, _RET_IP_);
  299. count = ldsem_atomic_update(LDSEM_WRITE_BIAS, sem);
  300. if ((count & LDSEM_ACTIVE_MASK) != LDSEM_ACTIVE_BIAS) {
  301. lock_stat(sem, contended);
  302. if (!down_write_failed(sem, count, timeout)) {
  303. lockdep_release(sem, 1, _RET_IP_);
  304. return 0;
  305. }
  306. }
  307. lock_stat(sem, acquired);
  308. return 1;
  309. }
  310. /*
  311. * lock for reading -- returns 1 if successful, 0 if timed out
  312. */
  313. int __sched ldsem_down_read(struct ld_semaphore *sem, long timeout)
  314. {
  315. might_sleep();
  316. return __ldsem_down_read_nested(sem, 0, timeout);
  317. }
  318. /*
  319. * trylock for reading -- returns 1 if successful, 0 if contention
  320. */
  321. int ldsem_down_read_trylock(struct ld_semaphore *sem)
  322. {
  323. long count = sem->count;
  324. while (count >= 0) {
  325. if (ldsem_cmpxchg(&count, count + LDSEM_READ_BIAS, sem)) {
  326. lockdep_acquire_read(sem, 0, 1, _RET_IP_);
  327. lock_stat(sem, acquired);
  328. return 1;
  329. }
  330. }
  331. return 0;
  332. }
  333. /*
  334. * lock for writing -- returns 1 if successful, 0 if timed out
  335. */
  336. int __sched ldsem_down_write(struct ld_semaphore *sem, long timeout)
  337. {
  338. might_sleep();
  339. return __ldsem_down_write_nested(sem, 0, timeout);
  340. }
  341. /*
  342. * trylock for writing -- returns 1 if successful, 0 if contention
  343. */
  344. int ldsem_down_write_trylock(struct ld_semaphore *sem)
  345. {
  346. long count = sem->count;
  347. while ((count & LDSEM_ACTIVE_MASK) == 0) {
  348. if (ldsem_cmpxchg(&count, count + LDSEM_WRITE_BIAS, sem)) {
  349. lockdep_acquire(sem, 0, 1, _RET_IP_);
  350. lock_stat(sem, acquired);
  351. return 1;
  352. }
  353. }
  354. return 0;
  355. }
  356. /*
  357. * release a read lock
  358. */
  359. void ldsem_up_read(struct ld_semaphore *sem)
  360. {
  361. long count;
  362. lockdep_release(sem, 1, _RET_IP_);
  363. count = ldsem_atomic_update(-LDSEM_READ_BIAS, sem);
  364. if (count < 0 && (count & LDSEM_ACTIVE_MASK) == 0)
  365. ldsem_wake(sem);
  366. }
  367. /*
  368. * release a write lock
  369. */
  370. void ldsem_up_write(struct ld_semaphore *sem)
  371. {
  372. long count;
  373. lockdep_release(sem, 1, _RET_IP_);
  374. count = ldsem_atomic_update(-LDSEM_WRITE_BIAS, sem);
  375. if (count < 0)
  376. ldsem_wake(sem);
  377. }
  378. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  379. int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass, long timeout)
  380. {
  381. might_sleep();
  382. return __ldsem_down_read_nested(sem, subclass, timeout);
  383. }
  384. int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
  385. long timeout)
  386. {
  387. might_sleep();
  388. return __ldsem_down_write_nested(sem, subclass, timeout);
  389. }
  390. #endif