generic-non-atomic.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H
  3. #define __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H
  4. #include <linux/bits.h>
  5. #include <asm/barrier.h>
  6. #ifndef _LINUX_BITOPS_H
  7. #error only <linux/bitops.h> can be included directly
  8. #endif
  9. /*
  10. * Generic definitions for bit operations, should not be used in regular code
  11. * directly.
  12. */
  13. /**
  14. * generic___set_bit - Set a bit in memory
  15. * @nr: the bit to set
  16. * @addr: the address to start counting from
  17. *
  18. * Unlike set_bit(), this function is non-atomic and may be reordered.
  19. * If it's called on the same region of memory simultaneously, the effect
  20. * may be that only one operation succeeds.
  21. */
  22. static __always_inline void
  23. generic___set_bit(unsigned long nr, volatile unsigned long *addr)
  24. {
  25. unsigned long mask = BIT_MASK(nr);
  26. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  27. *p |= mask;
  28. }
  29. static __always_inline void
  30. generic___clear_bit(unsigned long nr, volatile unsigned long *addr)
  31. {
  32. unsigned long mask = BIT_MASK(nr);
  33. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  34. *p &= ~mask;
  35. }
  36. /**
  37. * generic___change_bit - Toggle a bit in memory
  38. * @nr: the bit to change
  39. * @addr: the address to start counting from
  40. *
  41. * Unlike change_bit(), this function is non-atomic and may be reordered.
  42. * If it's called on the same region of memory simultaneously, the effect
  43. * may be that only one operation succeeds.
  44. */
  45. static __always_inline void
  46. generic___change_bit(unsigned long nr, volatile unsigned long *addr)
  47. {
  48. unsigned long mask = BIT_MASK(nr);
  49. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  50. *p ^= mask;
  51. }
  52. /**
  53. * generic___test_and_set_bit - Set a bit and return its old value
  54. * @nr: Bit to set
  55. * @addr: Address to count from
  56. *
  57. * This operation is non-atomic and can be reordered.
  58. * If two examples of this operation race, one can appear to succeed
  59. * but actually fail. You must protect multiple accesses with a lock.
  60. */
  61. static __always_inline bool
  62. generic___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
  63. {
  64. unsigned long mask = BIT_MASK(nr);
  65. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  66. unsigned long old = *p;
  67. *p = old | mask;
  68. return (old & mask) != 0;
  69. }
  70. /**
  71. * generic___test_and_clear_bit - Clear a bit and return its old value
  72. * @nr: Bit to clear
  73. * @addr: Address to count from
  74. *
  75. * This operation is non-atomic and can be reordered.
  76. * If two examples of this operation race, one can appear to succeed
  77. * but actually fail. You must protect multiple accesses with a lock.
  78. */
  79. static __always_inline bool
  80. generic___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  81. {
  82. unsigned long mask = BIT_MASK(nr);
  83. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  84. unsigned long old = *p;
  85. *p = old & ~mask;
  86. return (old & mask) != 0;
  87. }
  88. /* WARNING: non atomic and it can be reordered! */
  89. static __always_inline bool
  90. generic___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
  91. {
  92. unsigned long mask = BIT_MASK(nr);
  93. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  94. unsigned long old = *p;
  95. *p = old ^ mask;
  96. return (old & mask) != 0;
  97. }
  98. /**
  99. * generic_test_bit - Determine whether a bit is set
  100. * @nr: bit number to test
  101. * @addr: Address to start counting from
  102. */
  103. static __always_inline bool
  104. generic_test_bit(unsigned long nr, const volatile unsigned long *addr)
  105. {
  106. /*
  107. * Unlike the bitops with the '__' prefix above, this one *is* atomic,
  108. * so `volatile` must always stay here with no cast-aways. See
  109. * `Documentation/atomic_bitops.txt` for the details.
  110. */
  111. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  112. }
  113. /**
  114. * generic_test_bit_acquire - Determine, with acquire semantics, whether a bit is set
  115. * @nr: bit number to test
  116. * @addr: Address to start counting from
  117. */
  118. static __always_inline bool
  119. generic_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)
  120. {
  121. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  122. return 1UL & (smp_load_acquire(p) >> (nr & (BITS_PER_LONG-1)));
  123. }
  124. /*
  125. * const_*() definitions provide good compile-time optimizations when
  126. * the passed arguments can be resolved at compile time.
  127. */
  128. #define const___set_bit generic___set_bit
  129. #define const___clear_bit generic___clear_bit
  130. #define const___change_bit generic___change_bit
  131. #define const___test_and_set_bit generic___test_and_set_bit
  132. #define const___test_and_clear_bit generic___test_and_clear_bit
  133. #define const___test_and_change_bit generic___test_and_change_bit
  134. #define const_test_bit_acquire generic_test_bit_acquire
  135. /**
  136. * const_test_bit - Determine whether a bit is set
  137. * @nr: bit number to test
  138. * @addr: Address to start counting from
  139. *
  140. * A version of generic_test_bit() which discards the `volatile` qualifier to
  141. * allow a compiler to optimize code harder. Non-atomic and to be called only
  142. * for testing compile-time constants, e.g. by the corresponding macros, not
  143. * directly from "regular" code.
  144. */
  145. static __always_inline bool
  146. const_test_bit(unsigned long nr, const volatile unsigned long *addr)
  147. {
  148. const unsigned long *p = (const unsigned long *)addr + BIT_WORD(nr);
  149. unsigned long mask = BIT_MASK(nr);
  150. unsigned long val = *p;
  151. return !!(val & mask);
  152. }
  153. #endif /* __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H */