lock.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * arch_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 __always_inline int
  17. arch_test_and_set_bit_lock(unsigned int nr, 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 = arch_atomic_long_fetch_or_acquire(mask, (atomic_long_t *)p);
  25. return !!(old & mask);
  26. }
  27. /**
  28. * arch_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 __always_inline void
  35. arch_clear_bit_unlock(unsigned int nr, volatile unsigned long *p)
  36. {
  37. p += BIT_WORD(nr);
  38. arch_atomic_long_fetch_andnot_release(BIT_MASK(nr), (atomic_long_t *)p);
  39. }
  40. /**
  41. * arch___clear_bit_unlock - Clear a bit in memory, for unlock
  42. * @nr: the bit to set
  43. * @addr: the address to start counting from
  44. *
  45. * A weaker form of clear_bit_unlock() as used by __bit_lock_unlock(). If all
  46. * the bits in the word are protected by this lock some archs can use weaker
  47. * ops to safely unlock.
  48. *
  49. * See for example x86's implementation.
  50. */
  51. static inline void
  52. arch___clear_bit_unlock(unsigned int nr, volatile unsigned long *p)
  53. {
  54. unsigned long old;
  55. p += BIT_WORD(nr);
  56. old = READ_ONCE(*p);
  57. old &= ~BIT_MASK(nr);
  58. arch_atomic_long_set_release((atomic_long_t *)p, old);
  59. }
  60. /**
  61. * arch_clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
  62. * byte is negative, for unlock.
  63. * @nr: the bit to clear
  64. * @addr: the address to start counting from
  65. *
  66. * This is a bit of a one-trick-pony for the filemap code, which clears
  67. * PG_locked and tests PG_waiters,
  68. */
  69. #ifndef arch_clear_bit_unlock_is_negative_byte
  70. static inline bool arch_clear_bit_unlock_is_negative_byte(unsigned int nr,
  71. volatile unsigned long *p)
  72. {
  73. long old;
  74. unsigned long mask = BIT_MASK(nr);
  75. p += BIT_WORD(nr);
  76. old = arch_atomic_long_fetch_andnot_release(mask, (atomic_long_t *)p);
  77. return !!(old & BIT(7));
  78. }
  79. #define arch_clear_bit_unlock_is_negative_byte arch_clear_bit_unlock_is_negative_byte
  80. #endif
  81. #include <asm-generic/bitops/instrumented-lock.h>
  82. #endif /* _ASM_GENERIC_BITOPS_LOCK_H_ */