instrumented.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This header provides generic wrappers for memory access instrumentation that
  4. * the compiler cannot emit for: KASAN, KCSAN.
  5. */
  6. #ifndef _LINUX_INSTRUMENTED_H
  7. #define _LINUX_INSTRUMENTED_H
  8. #include <linux/compiler.h>
  9. #include <linux/kasan-checks.h>
  10. #include <linux/kcsan-checks.h>
  11. #include <linux/types.h>
  12. /**
  13. * instrument_read - instrument regular read access
  14. *
  15. * Instrument a regular read access. The instrumentation should be inserted
  16. * before the actual read happens.
  17. *
  18. * @ptr address of access
  19. * @size size of access
  20. */
  21. static __always_inline void instrument_read(const volatile void *v, size_t size)
  22. {
  23. kasan_check_read(v, size);
  24. kcsan_check_read(v, size);
  25. }
  26. /**
  27. * instrument_write - instrument regular write access
  28. *
  29. * Instrument a regular write access. The instrumentation should be inserted
  30. * before the actual write happens.
  31. *
  32. * @ptr address of access
  33. * @size size of access
  34. */
  35. static __always_inline void instrument_write(const volatile void *v, size_t size)
  36. {
  37. kasan_check_write(v, size);
  38. kcsan_check_write(v, size);
  39. }
  40. /**
  41. * instrument_read_write - instrument regular read-write access
  42. *
  43. * Instrument a regular write access. The instrumentation should be inserted
  44. * before the actual write happens.
  45. *
  46. * @ptr address of access
  47. * @size size of access
  48. */
  49. static __always_inline void instrument_read_write(const volatile void *v, size_t size)
  50. {
  51. kasan_check_write(v, size);
  52. kcsan_check_read_write(v, size);
  53. }
  54. /**
  55. * instrument_atomic_read - instrument atomic read access
  56. *
  57. * Instrument an atomic read access. The instrumentation should be inserted
  58. * before the actual read happens.
  59. *
  60. * @ptr address of access
  61. * @size size of access
  62. */
  63. static __always_inline void instrument_atomic_read(const volatile void *v, size_t size)
  64. {
  65. kasan_check_read(v, size);
  66. kcsan_check_atomic_read(v, size);
  67. }
  68. /**
  69. * instrument_atomic_write - instrument atomic write access
  70. *
  71. * Instrument an atomic write access. The instrumentation should be inserted
  72. * before the actual write happens.
  73. *
  74. * @ptr address of access
  75. * @size size of access
  76. */
  77. static __always_inline void instrument_atomic_write(const volatile void *v, size_t size)
  78. {
  79. kasan_check_write(v, size);
  80. kcsan_check_atomic_write(v, size);
  81. }
  82. /**
  83. * instrument_atomic_read_write - instrument atomic read-write access
  84. *
  85. * Instrument an atomic read-write access. The instrumentation should be
  86. * inserted before the actual write happens.
  87. *
  88. * @ptr address of access
  89. * @size size of access
  90. */
  91. static __always_inline void instrument_atomic_read_write(const volatile void *v, size_t size)
  92. {
  93. kasan_check_write(v, size);
  94. kcsan_check_atomic_read_write(v, size);
  95. }
  96. /**
  97. * instrument_copy_to_user - instrument reads of copy_to_user
  98. *
  99. * Instrument reads from kernel memory, that are due to copy_to_user (and
  100. * variants). The instrumentation must be inserted before the accesses.
  101. *
  102. * @to destination address
  103. * @from source address
  104. * @n number of bytes to copy
  105. */
  106. static __always_inline void
  107. instrument_copy_to_user(void __user *to, const void *from, unsigned long n)
  108. {
  109. kasan_check_read(from, n);
  110. kcsan_check_read(from, n);
  111. }
  112. /**
  113. * instrument_copy_from_user - instrument writes of copy_from_user
  114. *
  115. * Instrument writes to kernel memory, that are due to copy_from_user (and
  116. * variants). The instrumentation should be inserted before the accesses.
  117. *
  118. * @to destination address
  119. * @from source address
  120. * @n number of bytes to copy
  121. */
  122. static __always_inline void
  123. instrument_copy_from_user(const void *to, const void __user *from, unsigned long n)
  124. {
  125. kasan_check_write(to, n);
  126. kcsan_check_write(to, n);
  127. }
  128. #endif /* _LINUX_INSTRUMENTED_H */