xref: /linux/Documentation/core-api/debug-objects.rst (revision 9095bf25ea08135a5b74875dd0e3eeaddc4218a0)
193dc3a11SJonathan Corbet============================================
293dc3a11SJonathan CorbetThe object-lifetime debugging infrastructure
393dc3a11SJonathan Corbet============================================
493dc3a11SJonathan Corbet
593dc3a11SJonathan Corbet:Author: Thomas Gleixner
693dc3a11SJonathan Corbet
793dc3a11SJonathan CorbetIntroduction
893dc3a11SJonathan Corbet============
993dc3a11SJonathan Corbet
1093dc3a11SJonathan Corbetdebugobjects is a generic infrastructure to track the life time of
1193dc3a11SJonathan Corbetkernel objects and validate the operations on those.
1293dc3a11SJonathan Corbet
1393dc3a11SJonathan Corbetdebugobjects is useful to check for the following error patterns:
1493dc3a11SJonathan Corbet
1593dc3a11SJonathan Corbet-  Activation of uninitialized objects
1693dc3a11SJonathan Corbet
1793dc3a11SJonathan Corbet-  Initialization of active objects
1893dc3a11SJonathan Corbet
1993dc3a11SJonathan Corbet-  Usage of freed/destroyed objects
2093dc3a11SJonathan Corbet
2193dc3a11SJonathan Corbetdebugobjects is not changing the data structure of the real object so it
2293dc3a11SJonathan Corbetcan be compiled in with a minimal runtime impact and enabled on demand
2393dc3a11SJonathan Corbetwith a kernel command line option.
2493dc3a11SJonathan Corbet
2593dc3a11SJonathan CorbetHowto use debugobjects
2693dc3a11SJonathan Corbet======================
2793dc3a11SJonathan Corbet
2893dc3a11SJonathan CorbetA kernel subsystem needs to provide a data structure which describes the
2993dc3a11SJonathan Corbetobject type and add calls into the debug code at appropriate places. The
3093dc3a11SJonathan Corbetdata structure to describe the object type needs at minimum the name of
3193dc3a11SJonathan Corbetthe object type. Optional functions can and should be provided to fixup
3293dc3a11SJonathan Corbetdetected problems so the kernel can continue to work and the debug
3393dc3a11SJonathan Corbetinformation can be retrieved from a live system instead of hard core
3493dc3a11SJonathan Corbetdebugging with serial consoles and stack trace transcripts from the
3593dc3a11SJonathan Corbetmonitor.
3693dc3a11SJonathan Corbet
3793dc3a11SJonathan CorbetThe debug calls provided by debugobjects are:
3893dc3a11SJonathan Corbet
3993dc3a11SJonathan Corbet-  debug_object_init
4093dc3a11SJonathan Corbet
4193dc3a11SJonathan Corbet-  debug_object_init_on_stack
4293dc3a11SJonathan Corbet
4393dc3a11SJonathan Corbet-  debug_object_activate
4493dc3a11SJonathan Corbet
4593dc3a11SJonathan Corbet-  debug_object_deactivate
4693dc3a11SJonathan Corbet
4793dc3a11SJonathan Corbet-  debug_object_destroy
4893dc3a11SJonathan Corbet
4993dc3a11SJonathan Corbet-  debug_object_free
5093dc3a11SJonathan Corbet
5193dc3a11SJonathan Corbet-  debug_object_assert_init
5293dc3a11SJonathan Corbet
5393dc3a11SJonathan CorbetEach of these functions takes the address of the real object and a
5493dc3a11SJonathan Corbetpointer to the object type specific debug description structure.
5593dc3a11SJonathan Corbet
5693dc3a11SJonathan CorbetEach detected error is reported in the statistics and a limited number
5793dc3a11SJonathan Corbetof errors are printk'ed including a full stack trace.
5893dc3a11SJonathan Corbet
5993dc3a11SJonathan CorbetThe statistics are available via /sys/kernel/debug/debug_objects/stats.
6093dc3a11SJonathan CorbetThey provide information about the number of warnings and the number of
6193dc3a11SJonathan Corbetsuccessful fixups along with information about the usage of the internal
6293dc3a11SJonathan Corbettracking objects and the state of the internal tracking objects pool.
6393dc3a11SJonathan Corbet
6493dc3a11SJonathan CorbetDebug functions
6593dc3a11SJonathan Corbet===============
6693dc3a11SJonathan Corbet
6793dc3a11SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
68*8da3dc53SJonathan Corbet   :functions: debug_object_init
6993dc3a11SJonathan Corbet
7093dc3a11SJonathan CorbetThis function is called whenever the initialization function of a real
7193dc3a11SJonathan Corbetobject is called.
7293dc3a11SJonathan Corbet
7393dc3a11SJonathan CorbetWhen the real object is already tracked by debugobjects it is checked,
7493dc3a11SJonathan Corbetwhether the object can be initialized. Initializing is not allowed for
7593dc3a11SJonathan Corbetactive and destroyed objects. When debugobjects detects an error, then
7693dc3a11SJonathan Corbetit calls the fixup_init function of the object type description
7793dc3a11SJonathan Corbetstructure if provided by the caller. The fixup function can correct the
7893dc3a11SJonathan Corbetproblem before the real initialization of the object happens. E.g. it
7993dc3a11SJonathan Corbetcan deactivate an active object in order to prevent damage to the
8093dc3a11SJonathan Corbetsubsystem.
8193dc3a11SJonathan Corbet
8293dc3a11SJonathan CorbetWhen the real object is not yet tracked by debugobjects, debugobjects
8393dc3a11SJonathan Corbetallocates a tracker object for the real object and sets the tracker
8493dc3a11SJonathan Corbetobject state to ODEBUG_STATE_INIT. It verifies that the object is not
8593dc3a11SJonathan Corbeton the callers stack. If it is on the callers stack then a limited
8693dc3a11SJonathan Corbetnumber of warnings including a full stack trace is printk'ed. The
8793dc3a11SJonathan Corbetcalling code must use debug_object_init_on_stack() and remove the
8893dc3a11SJonathan Corbetobject before leaving the function which allocated it. See next section.
8993dc3a11SJonathan Corbet
90*8da3dc53SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
91*8da3dc53SJonathan Corbet   :functions: debug_object_init_on_stack
9293dc3a11SJonathan Corbet
9393dc3a11SJonathan CorbetThis function is called whenever the initialization function of a real
9493dc3a11SJonathan Corbetobject which resides on the stack is called.
9593dc3a11SJonathan Corbet
9693dc3a11SJonathan CorbetWhen the real object is already tracked by debugobjects it is checked,
9793dc3a11SJonathan Corbetwhether the object can be initialized. Initializing is not allowed for
9893dc3a11SJonathan Corbetactive and destroyed objects. When debugobjects detects an error, then
9993dc3a11SJonathan Corbetit calls the fixup_init function of the object type description
10093dc3a11SJonathan Corbetstructure if provided by the caller. The fixup function can correct the
10193dc3a11SJonathan Corbetproblem before the real initialization of the object happens. E.g. it
10293dc3a11SJonathan Corbetcan deactivate an active object in order to prevent damage to the
10393dc3a11SJonathan Corbetsubsystem.
10493dc3a11SJonathan Corbet
10593dc3a11SJonathan CorbetWhen the real object is not yet tracked by debugobjects debugobjects
10693dc3a11SJonathan Corbetallocates a tracker object for the real object and sets the tracker
10793dc3a11SJonathan Corbetobject state to ODEBUG_STATE_INIT. It verifies that the object is on
10893dc3a11SJonathan Corbetthe callers stack.
10993dc3a11SJonathan Corbet
11093dc3a11SJonathan CorbetAn object which is on the stack must be removed from the tracker by
11193dc3a11SJonathan Corbetcalling debug_object_free() before the function which allocates the
11293dc3a11SJonathan Corbetobject returns. Otherwise we keep track of stale objects.
11393dc3a11SJonathan Corbet
114*8da3dc53SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
115*8da3dc53SJonathan Corbet   :functions: debug_object_activate
11693dc3a11SJonathan Corbet
11793dc3a11SJonathan CorbetThis function is called whenever the activation function of a real
11893dc3a11SJonathan Corbetobject is called.
11993dc3a11SJonathan Corbet
12093dc3a11SJonathan CorbetWhen the real object is already tracked by debugobjects it is checked,
12193dc3a11SJonathan Corbetwhether the object can be activated. Activating is not allowed for
12293dc3a11SJonathan Corbetactive and destroyed objects. When debugobjects detects an error, then
12393dc3a11SJonathan Corbetit calls the fixup_activate function of the object type description
12493dc3a11SJonathan Corbetstructure if provided by the caller. The fixup function can correct the
12593dc3a11SJonathan Corbetproblem before the real activation of the object happens. E.g. it can
12693dc3a11SJonathan Corbetdeactivate an active object in order to prevent damage to the subsystem.
12793dc3a11SJonathan Corbet
12893dc3a11SJonathan CorbetWhen the real object is not yet tracked by debugobjects then the
12993dc3a11SJonathan Corbetfixup_activate function is called if available. This is necessary to
13093dc3a11SJonathan Corbetallow the legitimate activation of statically allocated and initialized
13193dc3a11SJonathan Corbetobjects. The fixup function checks whether the object is valid and calls
13293dc3a11SJonathan Corbetthe debug_objects_init() function to initialize the tracking of this
13393dc3a11SJonathan Corbetobject.
13493dc3a11SJonathan Corbet
13593dc3a11SJonathan CorbetWhen the activation is legitimate, then the state of the associated
13693dc3a11SJonathan Corbettracker object is set to ODEBUG_STATE_ACTIVE.
13793dc3a11SJonathan Corbet
138*8da3dc53SJonathan Corbet
139*8da3dc53SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
140*8da3dc53SJonathan Corbet   :functions: debug_object_deactivate
14193dc3a11SJonathan Corbet
14293dc3a11SJonathan CorbetThis function is called whenever the deactivation function of a real
14393dc3a11SJonathan Corbetobject is called.
14493dc3a11SJonathan Corbet
14593dc3a11SJonathan CorbetWhen the real object is tracked by debugobjects it is checked, whether
14693dc3a11SJonathan Corbetthe object can be deactivated. Deactivating is not allowed for untracked
14793dc3a11SJonathan Corbetor destroyed objects.
14893dc3a11SJonathan Corbet
14993dc3a11SJonathan CorbetWhen the deactivation is legitimate, then the state of the associated
15093dc3a11SJonathan Corbettracker object is set to ODEBUG_STATE_INACTIVE.
15193dc3a11SJonathan Corbet
152*8da3dc53SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
153*8da3dc53SJonathan Corbet   :functions: debug_object_destroy
15493dc3a11SJonathan Corbet
15593dc3a11SJonathan CorbetThis function is called to mark an object destroyed. This is useful to
15693dc3a11SJonathan Corbetprevent the usage of invalid objects, which are still available in
15793dc3a11SJonathan Corbetmemory: either statically allocated objects or objects which are freed
15893dc3a11SJonathan Corbetlater.
15993dc3a11SJonathan Corbet
16093dc3a11SJonathan CorbetWhen the real object is tracked by debugobjects it is checked, whether
16193dc3a11SJonathan Corbetthe object can be destroyed. Destruction is not allowed for active and
16293dc3a11SJonathan Corbetdestroyed objects. When debugobjects detects an error, then it calls the
16393dc3a11SJonathan Corbetfixup_destroy function of the object type description structure if
16493dc3a11SJonathan Corbetprovided by the caller. The fixup function can correct the problem
16593dc3a11SJonathan Corbetbefore the real destruction of the object happens. E.g. it can
16693dc3a11SJonathan Corbetdeactivate an active object in order to prevent damage to the subsystem.
16793dc3a11SJonathan Corbet
16893dc3a11SJonathan CorbetWhen the destruction is legitimate, then the state of the associated
16993dc3a11SJonathan Corbettracker object is set to ODEBUG_STATE_DESTROYED.
17093dc3a11SJonathan Corbet
171*8da3dc53SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
172*8da3dc53SJonathan Corbet   :functions: debug_object_free
17393dc3a11SJonathan Corbet
17493dc3a11SJonathan CorbetThis function is called before an object is freed.
17593dc3a11SJonathan Corbet
17693dc3a11SJonathan CorbetWhen the real object is tracked by debugobjects it is checked, whether
17793dc3a11SJonathan Corbetthe object can be freed. Free is not allowed for active objects. When
17893dc3a11SJonathan Corbetdebugobjects detects an error, then it calls the fixup_free function of
17993dc3a11SJonathan Corbetthe object type description structure if provided by the caller. The
18093dc3a11SJonathan Corbetfixup function can correct the problem before the real free of the
18193dc3a11SJonathan Corbetobject happens. E.g. it can deactivate an active object in order to
18293dc3a11SJonathan Corbetprevent damage to the subsystem.
18393dc3a11SJonathan Corbet
18493dc3a11SJonathan CorbetNote that debug_object_free removes the object from the tracker. Later
18593dc3a11SJonathan Corbetusage of the object is detected by the other debug checks.
18693dc3a11SJonathan Corbet
187*8da3dc53SJonathan Corbet
188*8da3dc53SJonathan Corbet.. kernel-doc:: lib/debugobjects.c
189*8da3dc53SJonathan Corbet   :functions: debug_object_assert_init
19093dc3a11SJonathan Corbet
19193dc3a11SJonathan CorbetThis function is called to assert that an object has been initialized.
19293dc3a11SJonathan Corbet
19393dc3a11SJonathan CorbetWhen the real object is not tracked by debugobjects, it calls
19493dc3a11SJonathan Corbetfixup_assert_init of the object type description structure provided by
19593dc3a11SJonathan Corbetthe caller, with the hardcoded object state ODEBUG_NOT_AVAILABLE. The
19693dc3a11SJonathan Corbetfixup function can correct the problem by calling debug_object_init
19793dc3a11SJonathan Corbetand other specific initializing functions.
19893dc3a11SJonathan Corbet
19993dc3a11SJonathan CorbetWhen the real object is already tracked by debugobjects it is ignored.
20093dc3a11SJonathan Corbet
20193dc3a11SJonathan CorbetFixup functions
20293dc3a11SJonathan Corbet===============
20393dc3a11SJonathan Corbet
20493dc3a11SJonathan CorbetDebug object type description structure
20593dc3a11SJonathan Corbet---------------------------------------
20693dc3a11SJonathan Corbet
20793dc3a11SJonathan Corbet.. kernel-doc:: include/linux/debugobjects.h
20893dc3a11SJonathan Corbet   :internal:
20993dc3a11SJonathan Corbet
21093dc3a11SJonathan Corbetfixup_init
21193dc3a11SJonathan Corbet-----------
21293dc3a11SJonathan Corbet
21393dc3a11SJonathan CorbetThis function is called from the debug code whenever a problem in
21493dc3a11SJonathan Corbetdebug_object_init is detected. The function takes the address of the
21593dc3a11SJonathan Corbetobject and the state which is currently recorded in the tracker.
21693dc3a11SJonathan Corbet
21793dc3a11SJonathan CorbetCalled from debug_object_init when the object state is:
21893dc3a11SJonathan Corbet
21993dc3a11SJonathan Corbet-  ODEBUG_STATE_ACTIVE
22093dc3a11SJonathan Corbet
22193dc3a11SJonathan CorbetThe function returns true when the fixup was successful, otherwise
22293dc3a11SJonathan Corbetfalse. The return value is used to update the statistics.
22393dc3a11SJonathan Corbet
22493dc3a11SJonathan CorbetNote, that the function needs to call the debug_object_init() function
22593dc3a11SJonathan Corbetagain, after the damage has been repaired in order to keep the state
22693dc3a11SJonathan Corbetconsistent.
22793dc3a11SJonathan Corbet
22893dc3a11SJonathan Corbetfixup_activate
22993dc3a11SJonathan Corbet---------------
23093dc3a11SJonathan Corbet
23193dc3a11SJonathan CorbetThis function is called from the debug code whenever a problem in
23293dc3a11SJonathan Corbetdebug_object_activate is detected.
23393dc3a11SJonathan Corbet
23493dc3a11SJonathan CorbetCalled from debug_object_activate when the object state is:
23593dc3a11SJonathan Corbet
23693dc3a11SJonathan Corbet-  ODEBUG_STATE_NOTAVAILABLE
23793dc3a11SJonathan Corbet
23893dc3a11SJonathan Corbet-  ODEBUG_STATE_ACTIVE
23993dc3a11SJonathan Corbet
24093dc3a11SJonathan CorbetThe function returns true when the fixup was successful, otherwise
24193dc3a11SJonathan Corbetfalse. The return value is used to update the statistics.
24293dc3a11SJonathan Corbet
24393dc3a11SJonathan CorbetNote that the function needs to call the debug_object_activate()
24493dc3a11SJonathan Corbetfunction again after the damage has been repaired in order to keep the
24593dc3a11SJonathan Corbetstate consistent.
24693dc3a11SJonathan Corbet
24793dc3a11SJonathan CorbetThe activation of statically initialized objects is a special case. When
24893dc3a11SJonathan Corbetdebug_object_activate() has no tracked object for this object address
24993dc3a11SJonathan Corbetthen fixup_activate() is called with object state
25093dc3a11SJonathan CorbetODEBUG_STATE_NOTAVAILABLE. The fixup function needs to check whether
25193dc3a11SJonathan Corbetthis is a legitimate case of a statically initialized object or not. In
25293dc3a11SJonathan Corbetcase it is it calls debug_object_init() and debug_object_activate()
25393dc3a11SJonathan Corbetto make the object known to the tracker and marked active. In this case
25493dc3a11SJonathan Corbetthe function should return false because this is not a real fixup.
25593dc3a11SJonathan Corbet
25693dc3a11SJonathan Corbetfixup_destroy
25793dc3a11SJonathan Corbet--------------
25893dc3a11SJonathan Corbet
25993dc3a11SJonathan CorbetThis function is called from the debug code whenever a problem in
26093dc3a11SJonathan Corbetdebug_object_destroy is detected.
26193dc3a11SJonathan Corbet
26293dc3a11SJonathan CorbetCalled from debug_object_destroy when the object state is:
26393dc3a11SJonathan Corbet
26493dc3a11SJonathan Corbet-  ODEBUG_STATE_ACTIVE
26593dc3a11SJonathan Corbet
26693dc3a11SJonathan CorbetThe function returns true when the fixup was successful, otherwise
26793dc3a11SJonathan Corbetfalse. The return value is used to update the statistics.
26893dc3a11SJonathan Corbet
26993dc3a11SJonathan Corbetfixup_free
27093dc3a11SJonathan Corbet-----------
27193dc3a11SJonathan Corbet
27293dc3a11SJonathan CorbetThis function is called from the debug code whenever a problem in
27393dc3a11SJonathan Corbetdebug_object_free is detected. Further it can be called from the debug
27493dc3a11SJonathan Corbetchecks in kfree/vfree, when an active object is detected from the
27593dc3a11SJonathan Corbetdebug_check_no_obj_freed() sanity checks.
27693dc3a11SJonathan Corbet
27793dc3a11SJonathan CorbetCalled from debug_object_free() or debug_check_no_obj_freed() when
27893dc3a11SJonathan Corbetthe object state is:
27993dc3a11SJonathan Corbet
28093dc3a11SJonathan Corbet-  ODEBUG_STATE_ACTIVE
28193dc3a11SJonathan Corbet
28293dc3a11SJonathan CorbetThe function returns true when the fixup was successful, otherwise
28393dc3a11SJonathan Corbetfalse. The return value is used to update the statistics.
28493dc3a11SJonathan Corbet
28593dc3a11SJonathan Corbetfixup_assert_init
28693dc3a11SJonathan Corbet-------------------
28793dc3a11SJonathan Corbet
28893dc3a11SJonathan CorbetThis function is called from the debug code whenever a problem in
28993dc3a11SJonathan Corbetdebug_object_assert_init is detected.
29093dc3a11SJonathan Corbet
29193dc3a11SJonathan CorbetCalled from debug_object_assert_init() with a hardcoded state
29293dc3a11SJonathan CorbetODEBUG_STATE_NOTAVAILABLE when the object is not found in the debug
29393dc3a11SJonathan Corbetbucket.
29493dc3a11SJonathan Corbet
29593dc3a11SJonathan CorbetThe function returns true when the fixup was successful, otherwise
29693dc3a11SJonathan Corbetfalse. The return value is used to update the statistics.
29793dc3a11SJonathan Corbet
29893dc3a11SJonathan CorbetNote, this function should make sure debug_object_init() is called
29993dc3a11SJonathan Corbetbefore returning.
30093dc3a11SJonathan Corbet
30193dc3a11SJonathan CorbetThe handling of statically initialized objects is a special case. The
30293dc3a11SJonathan Corbetfixup function should check if this is a legitimate case of a statically
30393dc3a11SJonathan Corbetinitialized object or not. In this case only debug_object_init()
30493dc3a11SJonathan Corbetshould be called to make the object known to the tracker. Then the
30593dc3a11SJonathan Corbetfunction should return false because this is not a real fixup.
30693dc3a11SJonathan Corbet
30793dc3a11SJonathan CorbetKnown Bugs And Assumptions
30893dc3a11SJonathan Corbet==========================
30993dc3a11SJonathan Corbet
31093dc3a11SJonathan CorbetNone (knock on wood).
311