debug-objects.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. .. kernel-doc:: lib/debugobjects.c
  45. :functions: debug_object_init
  46. This function is called whenever the initialization function of a real
  47. object is called.
  48. When the real object is already tracked by debugobjects it is checked,
  49. whether the object can be initialized. Initializing is not allowed for
  50. active and destroyed objects. When debugobjects detects an error, then
  51. it calls the fixup_init function of the object type description
  52. structure if provided by the caller. The fixup function can correct the
  53. problem before the real initialization of the object happens. E.g. it
  54. can deactivate an active object in order to prevent damage to the
  55. subsystem.
  56. When the real object is not yet tracked by debugobjects, debugobjects
  57. allocates a tracker object for the real object and sets the tracker
  58. object state to ODEBUG_STATE_INIT. It verifies that the object is not
  59. on the callers stack. If it is on the callers stack then a limited
  60. number of warnings including a full stack trace is printk'ed. The
  61. calling code must use debug_object_init_on_stack() and remove the
  62. object before leaving the function which allocated it. See next section.
  63. .. kernel-doc:: lib/debugobjects.c
  64. :functions: debug_object_init_on_stack
  65. This function is called whenever the initialization function of a real
  66. object which resides on the stack is called.
  67. When the real object is already tracked by debugobjects it is checked,
  68. whether the object can be initialized. Initializing is not allowed for
  69. active and destroyed objects. When debugobjects detects an error, then
  70. it calls the fixup_init function of the object type description
  71. structure if provided by the caller. The fixup function can correct the
  72. problem before the real initialization of the object happens. E.g. it
  73. can deactivate an active object in order to prevent damage to the
  74. subsystem.
  75. When the real object is not yet tracked by debugobjects debugobjects
  76. allocates a tracker object for the real object and sets the tracker
  77. object state to ODEBUG_STATE_INIT. It verifies that the object is on
  78. the callers stack.
  79. An object which is on the stack must be removed from the tracker by
  80. calling debug_object_free() before the function which allocates the
  81. object returns. Otherwise we keep track of stale objects.
  82. .. kernel-doc:: lib/debugobjects.c
  83. :functions: debug_object_activate
  84. This function is called whenever the activation function of a real
  85. object is called.
  86. When the real object is already tracked by debugobjects it is checked,
  87. whether the object can be activated. Activating is not allowed for
  88. active and destroyed objects. When debugobjects detects an error, then
  89. it calls the fixup_activate function of the object type description
  90. structure if provided by the caller. The fixup function can correct the
  91. problem before the real activation of the object happens. E.g. it can
  92. deactivate an active object in order to prevent damage to the subsystem.
  93. When the real object is not yet tracked by debugobjects then the
  94. fixup_activate function is called if available. This is necessary to
  95. allow the legitimate activation of statically allocated and initialized
  96. objects. The fixup function checks whether the object is valid and calls
  97. the debug_objects_init() function to initialize the tracking of this
  98. object.
  99. When the activation is legitimate, then the state of the associated
  100. tracker object is set to ODEBUG_STATE_ACTIVE.
  101. .. kernel-doc:: lib/debugobjects.c
  102. :functions: debug_object_deactivate
  103. This function is called whenever the deactivation function of a real
  104. object is called.
  105. When the real object is tracked by debugobjects it is checked, whether
  106. the object can be deactivated. Deactivating is not allowed for untracked
  107. or destroyed objects.
  108. When the deactivation is legitimate, then the state of the associated
  109. tracker object is set to ODEBUG_STATE_INACTIVE.
  110. .. kernel-doc:: lib/debugobjects.c
  111. :functions: debug_object_destroy
  112. This function is called to mark an object destroyed. This is useful to
  113. prevent the usage of invalid objects, which are still available in
  114. memory: either statically allocated objects or objects which are freed
  115. later.
  116. When the real object is tracked by debugobjects it is checked, whether
  117. the object can be destroyed. Destruction is not allowed for active and
  118. destroyed objects. When debugobjects detects an error, then it calls the
  119. fixup_destroy function of the object type description structure if
  120. provided by the caller. The fixup function can correct the problem
  121. before the real destruction of the object happens. E.g. it can
  122. deactivate an active object in order to prevent damage to the subsystem.
  123. When the destruction is legitimate, then the state of the associated
  124. tracker object is set to ODEBUG_STATE_DESTROYED.
  125. .. kernel-doc:: lib/debugobjects.c
  126. :functions: debug_object_free
  127. This function is called before an object is freed.
  128. When the real object is tracked by debugobjects it is checked, whether
  129. the object can be freed. Free is not allowed for active objects. When
  130. debugobjects detects an error, then it calls the fixup_free function of
  131. the object type description structure if provided by the caller. The
  132. fixup function can correct the problem before the real free of the
  133. object happens. E.g. it can deactivate an active object in order to
  134. prevent damage to the subsystem.
  135. Note that debug_object_free removes the object from the tracker. Later
  136. usage of the object is detected by the other debug checks.
  137. .. kernel-doc:: lib/debugobjects.c
  138. :functions: debug_object_assert_init
  139. This function is called to assert that an object has been initialized.
  140. When the real object is not tracked by debugobjects, it calls
  141. fixup_assert_init of the object type description structure provided by
  142. the caller, with the hardcoded object state ODEBUG_NOT_AVAILABLE. The
  143. fixup function can correct the problem by calling debug_object_init
  144. and other specific initializing functions.
  145. When the real object is already tracked by debugobjects it is ignored.
  146. Fixup functions
  147. ===============
  148. Debug object type description structure
  149. ---------------------------------------
  150. .. kernel-doc:: include/linux/debugobjects.h
  151. :internal:
  152. fixup_init
  153. -----------
  154. This function is called from the debug code whenever a problem in
  155. debug_object_init is detected. The function takes the address of the
  156. object and the state which is currently recorded in the tracker.
  157. Called from debug_object_init when the object state is:
  158. - ODEBUG_STATE_ACTIVE
  159. The function returns true when the fixup was successful, otherwise
  160. false. The return value is used to update the statistics.
  161. Note, that the function needs to call the debug_object_init() function
  162. again, after the damage has been repaired in order to keep the state
  163. consistent.
  164. fixup_activate
  165. ---------------
  166. This function is called from the debug code whenever a problem in
  167. debug_object_activate is detected.
  168. Called from debug_object_activate when the object state is:
  169. - ODEBUG_STATE_NOTAVAILABLE
  170. - ODEBUG_STATE_ACTIVE
  171. The function returns true when the fixup was successful, otherwise
  172. false. The return value is used to update the statistics.
  173. Note that the function needs to call the debug_object_activate()
  174. function again after the damage has been repaired in order to keep the
  175. state consistent.
  176. The activation of statically initialized objects is a special case. When
  177. debug_object_activate() has no tracked object for this object address
  178. then fixup_activate() is called with object state
  179. ODEBUG_STATE_NOTAVAILABLE. The fixup function needs to check whether
  180. this is a legitimate case of a statically initialized object or not. In
  181. case it is it calls debug_object_init() and debug_object_activate()
  182. to make the object known to the tracker and marked active. In this case
  183. the function should return false because this is not a real fixup.
  184. fixup_destroy
  185. --------------
  186. This function is called from the debug code whenever a problem in
  187. debug_object_destroy is detected.
  188. Called from debug_object_destroy when the object state is:
  189. - ODEBUG_STATE_ACTIVE
  190. The function returns true when the fixup was successful, otherwise
  191. false. The return value is used to update the statistics.
  192. fixup_free
  193. -----------
  194. This function is called from the debug code whenever a problem in
  195. debug_object_free is detected. Further it can be called from the debug
  196. checks in kfree/vfree, when an active object is detected from the
  197. debug_check_no_obj_freed() sanity checks.
  198. Called from debug_object_free() or debug_check_no_obj_freed() when
  199. the object state is:
  200. - ODEBUG_STATE_ACTIVE
  201. The function returns true when the fixup was successful, otherwise
  202. false. The return value is used to update the statistics.
  203. fixup_assert_init
  204. -------------------
  205. This function is called from the debug code whenever a problem in
  206. debug_object_assert_init is detected.
  207. Called from debug_object_assert_init() with a hardcoded state
  208. ODEBUG_STATE_NOTAVAILABLE when the object is not found in the debug
  209. bucket.
  210. The function returns true when the fixup was successful, otherwise
  211. false. The return value is used to update the statistics.
  212. Note, this function should make sure debug_object_init() is called
  213. before returning.
  214. The handling of statically initialized objects is a special case. The
  215. fixup function should check if this is a legitimate case of a statically
  216. initialized object or not. In this case only debug_object_init()
  217. should be called to make the object known to the tracker. Then the
  218. function should return false because this is not a real fixup.
  219. Known Bugs And Assumptions
  220. ==========================
  221. None (knock on wood).