tty_ldsem.c 11 KB

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