qdf_tracker.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2021, 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for
  7. * any purpose with or without fee is hereby granted, provided that the
  8. * above copyright notice and this permission notice appear in all
  9. * copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  12. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  13. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  14. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  15. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  16. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  17. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  18. * PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. #ifndef __QDF_TRACKER_H
  21. #define __QDF_TRACKER_H
  22. #include "qdf_lock.h"
  23. #include "qdf_ptr_hash.h"
  24. #include "qdf_status.h"
  25. #include "qdf_types.h"
  26. #define QDF_TRACKER_FUNC_SIZE 48
  27. /**
  28. * struct qdf_tracker - a generic type for tracking resources
  29. * @leak_title: the string title to use when logging leaks
  30. * @track_title: the string title to use when logging double tracking issues
  31. * @untrack_title: the string title to use when logging double untracking issues
  32. * @lock: lock for simultaneous access to @ht
  33. * @ht: the hashtable used for storing tracking information
  34. */
  35. struct qdf_tracker {
  36. const char *leak_title;
  37. const char *track_title;
  38. const char *untrack_title;
  39. struct qdf_spinlock lock;
  40. struct qdf_ptr_hash *ht;
  41. };
  42. /**
  43. * qdf_tracker_declare() - statically declare a qdf_tacker instance
  44. * @name: C identifier to use for the new qdf_tracker
  45. * @bits: the number of bits to use for hashing the resource pointers
  46. * @_leak_title: the string title to use when logging leaks
  47. * @_track_title: the string title to use when logging double tracking issues
  48. * @_untrack_title: the string title to use when logging double untracking issues
  49. */
  50. #define qdf_tracker_declare(name, bits, _leak_title, \
  51. _track_title, _untrack_title) \
  52. qdf_ptr_hash_declare(name ## _ht, bits); \
  53. struct qdf_tracker name = { \
  54. .leak_title = _leak_title, \
  55. .track_title = _track_title, \
  56. .untrack_title = _untrack_title, \
  57. .ht = qdf_ptr_hash_ptr(name ## _ht), \
  58. }
  59. #ifdef CONFIG_LEAK_DETECTION
  60. /**
  61. * qdf_tracker_init() - initialize a qdf_tracker
  62. * @tracker: the qdf_tracker to initialize
  63. *
  64. * Return: None
  65. */
  66. void qdf_tracker_init(struct qdf_tracker *tracker);
  67. /**
  68. * qdf_tracker_deinit() - de-initialize a qdf_tracker
  69. * @tracker: the qdf_tracker to de-initialize
  70. *
  71. * Return: None
  72. */
  73. void qdf_tracker_deinit(struct qdf_tracker *tracker);
  74. /**
  75. * qdf_tracker_track() - track a resource with @tracker
  76. * @tracker: the qdf_tracker to track with
  77. * @ptr: an opaque pointer to the resource to track
  78. * @func: name of the caller function operating on @ptr
  79. * @line: line number of the call site operating on @ptr
  80. *
  81. * Return: QDF_STATUS
  82. */
  83. qdf_must_check QDF_STATUS
  84. qdf_tracker_track(struct qdf_tracker *tracker, void *ptr,
  85. const char *func, uint32_t line);
  86. /**
  87. * qdf_tracker_untrack() - untrack a resource with @tracker
  88. * @tracker: the qdf_tracker used to track @ptr
  89. * @ptr: an opaque pointer to the resource to untrack
  90. * @func: name of the caller function operating on @ptr
  91. * @line: line number of the call site operating on @ptr
  92. *
  93. * Return: None
  94. */
  95. void qdf_tracker_untrack(struct qdf_tracker *tracker, void *ptr,
  96. const char *func, uint32_t line);
  97. /**
  98. * qdf_tracker_check_for_leaks() - assert @tracker has no tracked resources
  99. * for the current debug domain
  100. * @tracker: the qdf_tracker to check
  101. *
  102. * Return: None
  103. */
  104. void qdf_tracker_check_for_leaks(struct qdf_tracker *tracker);
  105. /**
  106. * qdf_tracker_lookup() - query tracking information for @ptr
  107. * @tracker: the qdf_tracker to check
  108. * @ptr: the opaque pointer of the resource to lookup
  109. * @out_func: function name provided when @ptr was tracked, populated on success
  110. * @out_line: line number provided when @ptr was tracked, populated on success
  111. *
  112. * Note: @out_func is assumed to be sizeof(QDF_TRACKER_FUNC_SIZE).
  113. *
  114. * Return: true if @tracker is tracking @ptr
  115. */
  116. qdf_must_check bool
  117. qdf_tracker_lookup(struct qdf_tracker *tracker, void *ptr,
  118. char (*out_func)[QDF_TRACKER_FUNC_SIZE],
  119. uint32_t *out_line);
  120. #else
  121. static inline
  122. void qdf_tracker_init(struct qdf_tracker *tracker)
  123. {
  124. }
  125. static inline
  126. void qdf_tracker_deinit(struct qdf_tracker *tracker)
  127. {
  128. }
  129. static inline qdf_must_check QDF_STATUS
  130. qdf_tracker_track(struct qdf_tracker *tracker, void *ptr,
  131. const char *func, uint32_t line)
  132. {
  133. return QDF_STATUS_SUCCESS;
  134. }
  135. static inline
  136. void qdf_tracker_untrack(struct qdf_tracker *tracker, void *ptr,
  137. const char *func, uint32_t line)
  138. {
  139. }
  140. static inline
  141. void qdf_tracker_check_for_leaks(struct qdf_tracker *tracker)
  142. {
  143. }
  144. static inline qdf_must_check bool
  145. qdf_tracker_lookup(struct qdf_tracker *tracker, void *ptr,
  146. char (*out_func)[QDF_TRACKER_FUNC_SIZE],
  147. uint32_t *out_line)
  148. {
  149. return false;
  150. }
  151. #endif
  152. #endif /* __QDF_TRACKER_H */