instrumented-lock.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #if defined(arch_clear_bit_unlock_is_negative_byte)
  56. /**
  57. * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom
  58. * byte is negative, for unlock.
  59. * @nr: the bit to clear
  60. * @addr: the address to start counting from
  61. *
  62. * This operation is atomic and provides release barrier semantics.
  63. *
  64. * This is a bit of a one-trick-pony for the filemap code, which clears
  65. * PG_locked and tests PG_waiters,
  66. */
  67. static inline bool
  68. clear_bit_unlock_is_negative_byte(long nr, volatile unsigned long *addr)
  69. {
  70. kcsan_release();
  71. instrument_atomic_write(addr + BIT_WORD(nr), sizeof(long));
  72. return arch_clear_bit_unlock_is_negative_byte(nr, addr);
  73. }
  74. /* Let everybody know we have it. */
  75. #define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
  76. #endif
  77. #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_LOCK_H */