debug-objects.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ============================================
  2. The object-lifetime debugging infrastructure
  3. ============================================
  4. :Author: Thomas Gleixner
  5. Introduction
  6. ============
  7. debugobjects is a generic infrastructure to track the life time of
  8. kernel objects and validate the operations on those.
  9. debugobjects is useful to check for the following error patterns:
  10. - Activation of uninitialized objects
  11. - Initialization of active objects
  12. - Usage of freed/destroyed objects
  13. debugobjects is not changing the data structure of the real object so it
  14. can be compiled in with a minimal runtime impact and enabled on demand
  15. with a kernel command line option.
  16. Howto use debugobjects
  17. ======================
  18. A kernel subsystem needs to provide a data structure which describes the
  19. object type and add calls into the debug code at appropriate places. The
  20. data structure to describe the object type needs at minimum the name of
  21. the object type. Optional functions can and should be provided to fixup
  22. detected problems so the kernel can continue to work and the debug
  23. information can be retrieved from a live system instead of hard core
  24. debugging with serial consoles and stack trace transcripts from the
  25. monitor.
  26. The debug calls provided by debugobjects are:
  27. - debug_object_init
  28. - debug_object_init_on_stack
  29. - debug_object_activate
  30. - debug_object_deactivate
  31. - debug_object_destroy
  32. - debug_object_free
  33. - debug_object_assert_init
  34. Each of these functions takes the address of the real object and a
  35. pointer to the object type specific debug description structure.
  36. Each detected error is reported in the statistics and a limited number
  37. of errors are printk'ed including a full stack trace.
  38. The statistics are available via /sys/kernel/debug/debug_objects/stats.
  39. They provide information about the number of warnings and the number of
  40. successful fixups along with information about the usage of the internal
  41. tracking objects and the state of the internal tracking objects pool.
  42. Debug functions
  43. ===============
  44. Debug object function reference
  45. -------------------------------
  46. .. kernel-doc:: lib/debugobjects.c
  47. :export:
  48. debug_object_init
  49. -------------------
  50. This function is called whenever the initialization function of a real
  51. object is called.
  52. When the real object is already tracked by debugobjects it is checked,
  53. whether the object can be initialized. Initializing is not allowed for
  54. active and destroyed objects. When debugobjects detects an error, then
  55. it calls the fixup_init function of the object type description
  56. structure if provided by the caller. The fixup function can correct the
  57. problem before the real initialization of the object happens. E.g. it
  58. can deactivate an active object in order to prevent damage to the
  59. subsystem.
  60. When the real object is not yet tracked by debugobjects, debugobjects
  61. allocates a tracker object for the real object and sets the tracker
  62. object state to ODEBUG_STATE_INIT. It verifies that the object is not
  63. on the callers stack. If it is on the callers stack then a limited
  64. number of warnings including a full stack trace is printk'ed. The
  65. calling code must use debug_object_init_on_stack() and remove the
  66. object before leaving the function which allocated it. See next section.
  67. debug_object_init_on_stack
  68. ------------------------------
  69. This function is called whenever the initialization function of a real
  70. object which resides on the stack is called.
  71. When the real object is already tracked by debugobjects it is checked,
  72. whether the object can be initialized. Initializing is not allowed for
  73. active and destroyed objects. When debugobjects detects an error, then
  74. it calls the fixup_init function of the object type description
  75. structure if provided by the caller. The fixup function can correct the
  76. problem before the real initialization of the object happens. E.g. it
  77. can deactivate an active object in order to prevent damage to the
  78. subsystem.
  79. When the real object is not yet tracked by debugobjects debugobjects
  80. allocates a tracker object for the real object and sets the tracker
  81. object state to ODEBUG_STATE_INIT. It verifies that the object is on
  82. the callers stack.
  83. An object which is on the stack must be removed from the tracker by
  84. calling debug_object_free() before the function which allocates the
  85. object returns. Otherwise we keep track of stale objects.
  86. debug_object_activate
  87. -----------------------
  88. This function is called whenever the activation function of a real
  89. object is called.
  90. When the real object is already tracked by debugobjects it is checked,
  91. whether the object can be activated. Activating is not allowed for
  92. active and destroyed objects. When debugobjects detects an error, then
  93. it calls the fixup_activate function of the object type description
  94. structure if provided by the caller. The fixup function can correct the
  95. problem before the real activation of the object happens. E.g. it can
  96. deactivate an active object in order to prevent damage to the subsystem.
  97. When the real object is not yet tracked by debugobjects then the
  98. fixup_activate function is called if available. This is necessary to
  99. allow the legitimate activation of statically allocated and initialized
  100. objects. The fixup function checks whether the object is valid and calls
  101. the debug_objects_init() function to initialize the tracking of this
  102. object.
  103. When the activation is legitimate, then the state of the associated
  104. tracker object is set to ODEBUG_STATE_ACTIVE.
  105. debug_object_deactivate
  106. -------------------------
  107. This function is called whenever the deactivation function of a real
  108. object is called.
  109. When the real object is tracked by debugobjects it is checked, whether
  110. the object can be deactivated. Deactivating is not allowed for untracked
  111. or destroyed objects.
  112. When the deactivation is legitimate, then the state of the associated
  113. tracker object is set to ODEBUG_STATE_INACTIVE.
  114. debug_object_destroy
  115. ----------------------
  116. This function is called to mark an object destroyed. This is useful to
  117. prevent the usage of invalid objects, which are still available in
  118. memory: either statically allocated objects or objects which are freed
  119. later.
  120. When the real object is tracked by debugobjects it is checked, whether
  121. the object can be destroyed. Destruction is not allowed for active and
  122. destroyed objects. When debugobjects detects an error, then it calls the
  123. fixup_destroy function of the object type description structure if
  124. provided by the caller. The fixup function can correct the problem
  125. before the real destruction of the object happens. E.g. it can
  126. deactivate an active object in order to prevent damage to the subsystem.
  127. When the destruction is legitimate, then the state of the associated
  128. tracker object is set to ODEBUG_STATE_DESTROYED.
  129. debug_object_free
  130. -------------------
  131. This function is called before an object is freed.
  132. When the real object is tracked by debugobjects it is checked, whether
  133. the object can be freed. Free is not allowed for active objects. When
  134. debugobjects detects an error, then it calls the fixup_free function of
  135. the object type description structure if provided by the caller. The
  136. fixup function can correct the problem before the real free of the
  137. object happens. E.g. it can deactivate an active object in order to
  138. prevent damage to the subsystem.
  139. Note that debug_object_free removes the object from the tracker. Later
  140. usage of the object is detected by the other debug checks.
  141. debug_object_assert_init
  142. ---------------------------
  143. This function is called to assert that an object has been initialized.
  144. When the real object is not tracked by debugobjects, it calls
  145. fixup_assert_init of the object type description structure provided by
  146. the caller, with the hardcoded object state ODEBUG_NOT_AVAILABLE. The
  147. fixup function can correct the problem by calling debug_object_init
  148. and other specific initializing functions.
  149. When the real object is already tracked by debugobjects it is ignored.
  150. Fixup functions
  151. ===============
  152. Debug object type description structure
  153. ---------------------------------------
  154. .. kernel-doc:: include/linux/debugobjects.h
  155. :internal:
  156. fixup_init
  157. -----------
  158. This function is called from the debug code whenever a problem in
  159. debug_object_init is detected. The function takes the address of the
  160. object and the state which is currently recorded in the tracker.
  161. Called from debug_object_init when the object state is:
  162. - ODEBUG_STATE_ACTIVE
  163. The function returns true when the fixup was successful, otherwise
  164. false. The return value is used to update the statistics.
  165. Note, that the function needs to call the debug_object_init() function
  166. again, after the damage has been repaired in order to keep the state
  167. consistent.
  168. fixup_activate
  169. ---------------
  170. This function is called from the debug code whenever a problem in
  171. debug_object_activate is detected.
  172. Called from debug_object_activate when the object state is:
  173. - ODEBUG_STATE_NOTAVAILABLE
  174. - ODEBUG_STATE_ACTIVE
  175. The function returns true when the fixup was successful, otherwise
  176. false. The return value is used to update the statistics.
  177. Note that the function needs to call the debug_object_activate()
  178. function again after the damage has been repaired in order to keep the
  179. state consistent.
  180. The activation of statically initialized objects is a special case. When
  181. debug_object_activate() has no tracked object for this object address
  182. then fixup_activate() is called with object state
  183. ODEBUG_STATE_NOTAVAILABLE. The fixup function needs to check whether
  184. this is a legitimate case of a statically initialized object or not. In
  185. case it is it calls debug_object_init() and debug_object_activate()
  186. to make the object known to the tracker and marked active. In this case
  187. the function should return false because this is not a real fixup.
  188. fixup_destroy
  189. --------------
  190. This function is called from the debug code whenever a problem in
  191. debug_object_destroy is detected.
  192. Called from debug_object_destroy when the object state is:
  193. - ODEBUG_STATE_ACTIVE
  194. The function returns true when the fixup was successful, otherwise
  195. false. The return value is used to update the statistics.
  196. fixup_free
  197. -----------
  198. This function is called from the debug code whenever a problem in
  199. debug_object_free is detected. Further it can be called from the debug
  200. checks in kfree/vfree, when an active object is detected from the
  201. debug_check_no_obj_freed() sanity checks.
  202. Called from debug_object_free() or debug_check_no_obj_freed() when
  203. the object state is:
  204. - ODEBUG_STATE_ACTIVE
  205. The function returns true when the fixup was successful, otherwise
  206. false. The return value is used to update the statistics.
  207. fixup_assert_init
  208. -------------------
  209. This function is called from the debug code whenever a problem in
  210. debug_object_assert_init is detected.
  211. Called from debug_object_assert_init() with a hardcoded state
  212. ODEBUG_STATE_NOTAVAILABLE when the object is not found in the debug
  213. bucket.
  214. The function returns true when the fixup was successful, otherwise
  215. false. The return value is used to update the statistics.
  216. Note, this function should make sure debug_object_init() is called
  217. before returning.
  218. The handling of statically initialized objects is a special case. The
  219. fixup function should check if this is a legitimate case of a statically
  220. initialized object or not. In this case only debug_object_init()
  221. should be called to make the object known to the tracker. Then the
  222. function should return false because this is not a real fixup.
  223. Known Bugs And Assumptions
  224. ==========================
  225. None (knock on wood).