kmemcheck.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef LINUX_KMEMCHECK_H
  2. #define LINUX_KMEMCHECK_H
  3. #include <linux/mm_types.h>
  4. #include <linux/types.h>
  5. #ifdef CONFIG_KMEMCHECK
  6. extern int kmemcheck_enabled;
  7. /* The slab-related functions. */
  8. void kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node);
  9. void kmemcheck_free_shadow(struct page *page, int order);
  10. void kmemcheck_slab_alloc(struct kmem_cache *s, gfp_t gfpflags, void *object,
  11. size_t size);
  12. void kmemcheck_slab_free(struct kmem_cache *s, void *object, size_t size);
  13. void kmemcheck_pagealloc_alloc(struct page *p, unsigned int order,
  14. gfp_t gfpflags);
  15. void kmemcheck_show_pages(struct page *p, unsigned int n);
  16. void kmemcheck_hide_pages(struct page *p, unsigned int n);
  17. bool kmemcheck_page_is_tracked(struct page *p);
  18. void kmemcheck_mark_unallocated(void *address, unsigned int n);
  19. void kmemcheck_mark_uninitialized(void *address, unsigned int n);
  20. void kmemcheck_mark_initialized(void *address, unsigned int n);
  21. void kmemcheck_mark_freed(void *address, unsigned int n);
  22. void kmemcheck_mark_unallocated_pages(struct page *p, unsigned int n);
  23. void kmemcheck_mark_uninitialized_pages(struct page *p, unsigned int n);
  24. void kmemcheck_mark_initialized_pages(struct page *p, unsigned int n);
  25. int kmemcheck_show_addr(unsigned long address);
  26. int kmemcheck_hide_addr(unsigned long address);
  27. bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size);
  28. #else
  29. #define kmemcheck_enabled 0
  30. static inline void
  31. kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node)
  32. {
  33. }
  34. static inline void
  35. kmemcheck_free_shadow(struct page *page, int order)
  36. {
  37. }
  38. static inline void
  39. kmemcheck_slab_alloc(struct kmem_cache *s, gfp_t gfpflags, void *object,
  40. size_t size)
  41. {
  42. }
  43. static inline void kmemcheck_slab_free(struct kmem_cache *s, void *object,
  44. size_t size)
  45. {
  46. }
  47. static inline void kmemcheck_pagealloc_alloc(struct page *p,
  48. unsigned int order, gfp_t gfpflags)
  49. {
  50. }
  51. static inline bool kmemcheck_page_is_tracked(struct page *p)
  52. {
  53. return false;
  54. }
  55. static inline void kmemcheck_mark_unallocated(void *address, unsigned int n)
  56. {
  57. }
  58. static inline void kmemcheck_mark_uninitialized(void *address, unsigned int n)
  59. {
  60. }
  61. static inline void kmemcheck_mark_initialized(void *address, unsigned int n)
  62. {
  63. }
  64. static inline void kmemcheck_mark_freed(void *address, unsigned int n)
  65. {
  66. }
  67. static inline void kmemcheck_mark_unallocated_pages(struct page *p,
  68. unsigned int n)
  69. {
  70. }
  71. static inline void kmemcheck_mark_uninitialized_pages(struct page *p,
  72. unsigned int n)
  73. {
  74. }
  75. static inline void kmemcheck_mark_initialized_pages(struct page *p,
  76. unsigned int n)
  77. {
  78. }
  79. static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
  80. {
  81. return true;
  82. }
  83. #endif /* CONFIG_KMEMCHECK */
  84. /*
  85. * Bitfield annotations
  86. *
  87. * How to use: If you have a struct using bitfields, for example
  88. *
  89. * struct a {
  90. * int x:8, y:8;
  91. * };
  92. *
  93. * then this should be rewritten as
  94. *
  95. * struct a {
  96. * kmemcheck_bitfield_begin(flags);
  97. * int x:8, y:8;
  98. * kmemcheck_bitfield_end(flags);
  99. * };
  100. *
  101. * Now the "flags_begin" and "flags_end" members may be used to refer to the
  102. * beginning and end, respectively, of the bitfield (and things like
  103. * &x.flags_begin is allowed). As soon as the struct is allocated, the bit-
  104. * fields should be annotated:
  105. *
  106. * struct a *a = kmalloc(sizeof(struct a), GFP_KERNEL);
  107. * kmemcheck_annotate_bitfield(a, flags);
  108. *
  109. * Note: We provide the same definitions for both kmemcheck and non-
  110. * kmemcheck kernels. This makes it harder to introduce accidental errors. It
  111. * is also allowed to pass NULL pointers to kmemcheck_annotate_bitfield().
  112. */
  113. #define kmemcheck_bitfield_begin(name) \
  114. int name##_begin[0];
  115. #define kmemcheck_bitfield_end(name) \
  116. int name##_end[0];
  117. #define kmemcheck_annotate_bitfield(ptr, name) \
  118. do { \
  119. int _n; \
  120. \
  121. if (!ptr) \
  122. break; \
  123. \
  124. _n = (long) &((ptr)->name##_end) \
  125. - (long) &((ptr)->name##_begin); \
  126. BUILD_BUG_ON(_n < 0); \
  127. \
  128. kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \
  129. } while (0)
  130. #define kmemcheck_annotate_variable(var) \
  131. do { \
  132. kmemcheck_mark_initialized(&(var), sizeof(var)); \
  133. } while (0) \
  134. #endif /* LINUX_KMEMCHECK_H */