qrwlock.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Queued read/write locks
  4. *
  5. * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
  6. *
  7. * Authors: Waiman Long <[email protected]>
  8. */
  9. #include <linux/smp.h>
  10. #include <linux/bug.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/percpu.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/spinlock.h>
  15. #include <trace/events/lock.h>
  16. /**
  17. * queued_read_lock_slowpath - acquire read lock of a queued rwlock
  18. * @lock: Pointer to queued rwlock structure
  19. */
  20. void __lockfunc queued_read_lock_slowpath(struct qrwlock *lock)
  21. {
  22. /*
  23. * Readers come here when they cannot get the lock without waiting
  24. */
  25. if (unlikely(in_interrupt())) {
  26. /*
  27. * Readers in interrupt context will get the lock immediately
  28. * if the writer is just waiting (not holding the lock yet),
  29. * so spin with ACQUIRE semantics until the lock is available
  30. * without waiting in the queue.
  31. */
  32. atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
  33. return;
  34. }
  35. atomic_sub(_QR_BIAS, &lock->cnts);
  36. trace_contention_begin(lock, LCB_F_SPIN | LCB_F_READ);
  37. /*
  38. * Put the reader into the wait queue
  39. */
  40. arch_spin_lock(&lock->wait_lock);
  41. atomic_add(_QR_BIAS, &lock->cnts);
  42. /*
  43. * The ACQUIRE semantics of the following spinning code ensure
  44. * that accesses can't leak upwards out of our subsequent critical
  45. * section in the case that the lock is currently held for write.
  46. */
  47. atomic_cond_read_acquire(&lock->cnts, !(VAL & _QW_LOCKED));
  48. /*
  49. * Signal the next one in queue to become queue head
  50. */
  51. arch_spin_unlock(&lock->wait_lock);
  52. trace_contention_end(lock, 0);
  53. }
  54. EXPORT_SYMBOL(queued_read_lock_slowpath);
  55. /**
  56. * queued_write_lock_slowpath - acquire write lock of a queued rwlock
  57. * @lock : Pointer to queued rwlock structure
  58. */
  59. void __lockfunc queued_write_lock_slowpath(struct qrwlock *lock)
  60. {
  61. int cnts;
  62. trace_contention_begin(lock, LCB_F_SPIN | LCB_F_WRITE);
  63. /* Put the writer into the wait queue */
  64. arch_spin_lock(&lock->wait_lock);
  65. /* Try to acquire the lock directly if no reader is present */
  66. if (!(cnts = atomic_read(&lock->cnts)) &&
  67. atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED))
  68. goto unlock;
  69. /* Set the waiting flag to notify readers that a writer is pending */
  70. atomic_or(_QW_WAITING, &lock->cnts);
  71. /* When no more readers or writers, set the locked flag */
  72. do {
  73. cnts = atomic_cond_read_relaxed(&lock->cnts, VAL == _QW_WAITING);
  74. } while (!atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED));
  75. unlock:
  76. arch_spin_unlock(&lock->wait_lock);
  77. trace_contention_end(lock, 0);
  78. }
  79. EXPORT_SYMBOL(queued_write_lock_slowpath);