tty_ldsem.c 11 KB

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