instrumented-lock.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This file provides wrappers with sanitizer instrumentation for bit
  4. * locking operations.
  5. *
  6. * To use this functionality, an arch's bitops.h file needs to define each of
  7. * the below bit operations with an arch_ prefix (e.g. arch_set_bit(),
  8. * arch___set_bit(), etc.).
  9. */
  10. #ifndef _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H
  11. #define _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H
  12. #include <linux/instrumented.h>
  13. /**
  14. * clear_bit_unlock - Clear a bit in memory, for unlock
  15. * @nr: the bit to set
  16. * @addr: the address to start counting from
  17. *
  18. * This operation is atomic and provides release barrier semantics.
  19. */
  20. static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
  21. {
  22. kcsan_release();
  23. instrument_atomic_write(addr + BIT_WORD(nr), sizeof(long));
  24. arch_clear_bit_unlock(nr, addr);
  25. }
  26. /**
  27. * __clear_bit_unlock - Clears a bit in memory
  28. * @nr: Bit to clear
  29. * @addr: Address to start counting from
  30. *
  31. * This is a non-atomic operation but implies a release barrier before the
  32. * memory operation. It can be used for an unlock if no other CPUs can
  33. * concurrently modify other bits in the word.
  34. */
  35. static inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
  36. {
  37. kcsan_release();
  38. instrument_write(addr + BIT_WORD(nr), sizeof(long));
  39. arch___clear_bit_unlock(nr, addr);
  40. }
  41. /**
  42. * test_and_set_bit_lock - Set a bit and return its old value, for lock
  43. * @nr: Bit to set
  44. * @addr: Address to count from
  45. *
  46. * This operation is atomic and provides acquire barrier semantics if
  47. * the returned value is 0.
  48. * It can be used to implement bit locks.
  49. */
  50. static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
  51. {
  52. instrument_atomic_read_write(addr + BIT_WORD(nr), sizeof(long));
  53. return arch_test_and_set_bit_lock(nr, addr);
  54. }
  55. /**
  56. * xor_unlock_is_negative_byte - XOR a single byte in memory and test if
  57. * it is negative, for unlock.
  58. * @mask: Change the bits which are set in this mask.
  59. * @addr: The address of the word containing the byte to change.
  60. *
  61. * Changes some of bits 0-6 in the word pointed to by @addr.
  62. * This operation is atomic and provides release barrier semantics.
  63. * Used to optimise some folio operations which are commonly paired
  64. * with an unlock or end of writeback. Bit 7 is used as PG_waiters to
  65. * indicate whether anybody is waiting for the unlock.
  66. *
  67. * Return: Whether the top bit of the byte is set.
  68. */
  69. static inline bool xor_unlock_is_negative_byte(unsigned long mask,
  70. volatile unsigned long *addr)
  71. {
  72. kcsan_release();
  73. instrument_atomic_write(addr, sizeof(long));
  74. return arch_xor_unlock_is_negative_byte(mask, addr);
  75. }
  76. #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H */