lock.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BITOPS_LOCK_H_
  3. #define _ASM_GENERIC_BITOPS_LOCK_H_
  4. #include <linux/atomic.h>
  5. #include <linux/compiler.h>
  6. #include <asm/barrier.h>
  7. /**
  8. * test_and_set_bit_lock - Set a bit and return its old value, for lock
  9. * @nr: Bit to set
  10. * @addr: Address to count from
  11. *
  12. * This operation is atomic and provides acquire barrier semantics if
  13. * the returned value is 0.
  14. * It can be used to implement bit locks.
  15. */
  16. static inline int test_and_set_bit_lock(unsigned int nr,
  17. volatile unsigned long *p)
  18. {
  19. long old;
  20. unsigned long mask = BIT_MASK(nr);
  21. p += BIT_WORD(nr);
  22. if (READ_ONCE(*p) & mask)
  23. return 1;
  24. old = atomic_long_fetch_or_acquire(mask, (atomic_long_t *)p);
  25. return !!(old & mask);
  26. }
  27. /**
  28. * clear_bit_unlock - Clear a bit in memory, for unlock
  29. * @nr: the bit to set
  30. * @addr: the address to start counting from
  31. *
  32. * This operation is atomic and provides release barrier semantics.
  33. */
  34. static inline void clear_bit_unlock(unsigned int nr, volatile unsigned long *p)
  35. {
  36. p += BIT_WORD(nr);
  37. atomic_long_fetch_andnot_release(BIT_MASK(nr), (atomic_long_t *)p);
  38. }
  39. /**
  40. * __clear_bit_unlock - Clear a bit in memory, for unlock
  41. * @nr: the bit to set
  42. * @addr: the address to start counting from
  43. *
  44. * A weaker form of clear_bit_unlock() as used by __bit_lock_unlock(). If all
  45. * the bits in the word are protected by this lock some archs can use weaker
  46. * ops to safely unlock.
  47. *
  48. * See for example x86's implementation.
  49. */
  50. static inline void __clear_bit_unlock(unsigned int nr,
  51. volatile unsigned long *p)
  52. {
  53. unsigned long old;
  54. p += BIT_WORD(nr);
  55. old = READ_ONCE(*p);
  56. old &= ~BIT_MASK(nr);
  57. atomic_long_set_release((atomic_long_t *)p, old);
  58. }
  59. /**
  60. * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
  61. * byte is negative, for unlock.
  62. * @nr: the bit to clear
  63. * @addr: the address to start counting from
  64. *
  65. * This is a bit of a one-trick-pony for the filemap code, which clears
  66. * PG_locked and tests PG_waiters,
  67. */
  68. #ifndef clear_bit_unlock_is_negative_byte
  69. static inline bool clear_bit_unlock_is_negative_byte(unsigned int nr,
  70. volatile unsigned long *p)
  71. {
  72. long old;
  73. unsigned long mask = BIT_MASK(nr);
  74. p += BIT_WORD(nr);
  75. old = atomic_long_fetch_andnot_release(mask, (atomic_long_t *)p);
  76. return !!(old & BIT(7));
  77. }
  78. #define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
  79. #endif
  80. #endif /* _ASM_GENERIC_BITOPS_LOCK_H_ */