162306a36Sopenharmony_ci============================================ 262306a36Sopenharmony_ciThe object-lifetime debugging infrastructure 362306a36Sopenharmony_ci============================================ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci:Author: Thomas Gleixner 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciIntroduction 862306a36Sopenharmony_ci============ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_cidebugobjects is a generic infrastructure to track the life time of 1162306a36Sopenharmony_cikernel objects and validate the operations on those. 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_cidebugobjects is useful to check for the following error patterns: 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci- Activation of uninitialized objects 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci- Initialization of active objects 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci- Usage of freed/destroyed objects 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_cidebugobjects is not changing the data structure of the real object so it 2262306a36Sopenharmony_cican be compiled in with a minimal runtime impact and enabled on demand 2362306a36Sopenharmony_ciwith a kernel command line option. 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciHowto use debugobjects 2662306a36Sopenharmony_ci====================== 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciA kernel subsystem needs to provide a data structure which describes the 2962306a36Sopenharmony_ciobject type and add calls into the debug code at appropriate places. The 3062306a36Sopenharmony_cidata structure to describe the object type needs at minimum the name of 3162306a36Sopenharmony_cithe object type. Optional functions can and should be provided to fixup 3262306a36Sopenharmony_cidetected problems so the kernel can continue to work and the debug 3362306a36Sopenharmony_ciinformation can be retrieved from a live system instead of hard core 3462306a36Sopenharmony_cidebugging with serial consoles and stack trace transcripts from the 3562306a36Sopenharmony_cimonitor. 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ciThe debug calls provided by debugobjects are: 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci- debug_object_init 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci- debug_object_init_on_stack 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci- debug_object_activate 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci- debug_object_deactivate 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci- debug_object_destroy 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci- debug_object_free 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci- debug_object_assert_init 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ciEach of these functions takes the address of the real object and a 5462306a36Sopenharmony_cipointer to the object type specific debug description structure. 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciEach detected error is reported in the statistics and a limited number 5762306a36Sopenharmony_ciof errors are printk'ed including a full stack trace. 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciThe statistics are available via /sys/kernel/debug/debug_objects/stats. 6062306a36Sopenharmony_ciThey provide information about the number of warnings and the number of 6162306a36Sopenharmony_cisuccessful fixups along with information about the usage of the internal 6262306a36Sopenharmony_citracking objects and the state of the internal tracking objects pool. 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ciDebug functions 6562306a36Sopenharmony_ci=============== 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 6862306a36Sopenharmony_ci :functions: debug_object_init 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciThis function is called whenever the initialization function of a real 7162306a36Sopenharmony_ciobject is called. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciWhen the real object is already tracked by debugobjects it is checked, 7462306a36Sopenharmony_ciwhether the object can be initialized. Initializing is not allowed for 7562306a36Sopenharmony_ciactive and destroyed objects. When debugobjects detects an error, then 7662306a36Sopenharmony_ciit calls the fixup_init function of the object type description 7762306a36Sopenharmony_cistructure if provided by the caller. The fixup function can correct the 7862306a36Sopenharmony_ciproblem before the real initialization of the object happens. E.g. it 7962306a36Sopenharmony_cican deactivate an active object in order to prevent damage to the 8062306a36Sopenharmony_cisubsystem. 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ciWhen the real object is not yet tracked by debugobjects, debugobjects 8362306a36Sopenharmony_ciallocates a tracker object for the real object and sets the tracker 8462306a36Sopenharmony_ciobject state to ODEBUG_STATE_INIT. It verifies that the object is not 8562306a36Sopenharmony_cion the callers stack. If it is on the callers stack then a limited 8662306a36Sopenharmony_cinumber of warnings including a full stack trace is printk'ed. The 8762306a36Sopenharmony_cicalling code must use debug_object_init_on_stack() and remove the 8862306a36Sopenharmony_ciobject before leaving the function which allocated it. See next section. 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 9162306a36Sopenharmony_ci :functions: debug_object_init_on_stack 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ciThis function is called whenever the initialization function of a real 9462306a36Sopenharmony_ciobject which resides on the stack is called. 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ciWhen the real object is already tracked by debugobjects it is checked, 9762306a36Sopenharmony_ciwhether the object can be initialized. Initializing is not allowed for 9862306a36Sopenharmony_ciactive and destroyed objects. When debugobjects detects an error, then 9962306a36Sopenharmony_ciit calls the fixup_init function of the object type description 10062306a36Sopenharmony_cistructure if provided by the caller. The fixup function can correct the 10162306a36Sopenharmony_ciproblem before the real initialization of the object happens. E.g. it 10262306a36Sopenharmony_cican deactivate an active object in order to prevent damage to the 10362306a36Sopenharmony_cisubsystem. 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciWhen the real object is not yet tracked by debugobjects debugobjects 10662306a36Sopenharmony_ciallocates a tracker object for the real object and sets the tracker 10762306a36Sopenharmony_ciobject state to ODEBUG_STATE_INIT. It verifies that the object is on 10862306a36Sopenharmony_cithe callers stack. 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciAn object which is on the stack must be removed from the tracker by 11162306a36Sopenharmony_cicalling debug_object_free() before the function which allocates the 11262306a36Sopenharmony_ciobject returns. Otherwise we keep track of stale objects. 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 11562306a36Sopenharmony_ci :functions: debug_object_activate 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ciThis function is called whenever the activation function of a real 11862306a36Sopenharmony_ciobject is called. 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ciWhen the real object is already tracked by debugobjects it is checked, 12162306a36Sopenharmony_ciwhether the object can be activated. Activating is not allowed for 12262306a36Sopenharmony_ciactive and destroyed objects. When debugobjects detects an error, then 12362306a36Sopenharmony_ciit calls the fixup_activate function of the object type description 12462306a36Sopenharmony_cistructure if provided by the caller. The fixup function can correct the 12562306a36Sopenharmony_ciproblem before the real activation of the object happens. E.g. it can 12662306a36Sopenharmony_cideactivate an active object in order to prevent damage to the subsystem. 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ciWhen the real object is not yet tracked by debugobjects then the 12962306a36Sopenharmony_cifixup_activate function is called if available. This is necessary to 13062306a36Sopenharmony_ciallow the legitimate activation of statically allocated and initialized 13162306a36Sopenharmony_ciobjects. The fixup function checks whether the object is valid and calls 13262306a36Sopenharmony_cithe debug_objects_init() function to initialize the tracking of this 13362306a36Sopenharmony_ciobject. 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciWhen the activation is legitimate, then the state of the associated 13662306a36Sopenharmony_citracker object is set to ODEBUG_STATE_ACTIVE. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 14062306a36Sopenharmony_ci :functions: debug_object_deactivate 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ciThis function is called whenever the deactivation function of a real 14362306a36Sopenharmony_ciobject is called. 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciWhen the real object is tracked by debugobjects it is checked, whether 14662306a36Sopenharmony_cithe object can be deactivated. Deactivating is not allowed for untracked 14762306a36Sopenharmony_cior destroyed objects. 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ciWhen the deactivation is legitimate, then the state of the associated 15062306a36Sopenharmony_citracker object is set to ODEBUG_STATE_INACTIVE. 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 15362306a36Sopenharmony_ci :functions: debug_object_destroy 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ciThis function is called to mark an object destroyed. This is useful to 15662306a36Sopenharmony_ciprevent the usage of invalid objects, which are still available in 15762306a36Sopenharmony_cimemory: either statically allocated objects or objects which are freed 15862306a36Sopenharmony_cilater. 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ciWhen the real object is tracked by debugobjects it is checked, whether 16162306a36Sopenharmony_cithe object can be destroyed. Destruction is not allowed for active and 16262306a36Sopenharmony_cidestroyed objects. When debugobjects detects an error, then it calls the 16362306a36Sopenharmony_cifixup_destroy function of the object type description structure if 16462306a36Sopenharmony_ciprovided by the caller. The fixup function can correct the problem 16562306a36Sopenharmony_cibefore the real destruction of the object happens. E.g. it can 16662306a36Sopenharmony_cideactivate an active object in order to prevent damage to the subsystem. 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ciWhen the destruction is legitimate, then the state of the associated 16962306a36Sopenharmony_citracker object is set to ODEBUG_STATE_DESTROYED. 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 17262306a36Sopenharmony_ci :functions: debug_object_free 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ciThis function is called before an object is freed. 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ciWhen the real object is tracked by debugobjects it is checked, whether 17762306a36Sopenharmony_cithe object can be freed. Free is not allowed for active objects. When 17862306a36Sopenharmony_cidebugobjects detects an error, then it calls the fixup_free function of 17962306a36Sopenharmony_cithe object type description structure if provided by the caller. The 18062306a36Sopenharmony_cifixup function can correct the problem before the real free of the 18162306a36Sopenharmony_ciobject happens. E.g. it can deactivate an active object in order to 18262306a36Sopenharmony_ciprevent damage to the subsystem. 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ciNote that debug_object_free removes the object from the tracker. Later 18562306a36Sopenharmony_ciusage of the object is detected by the other debug checks. 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 18962306a36Sopenharmony_ci :functions: debug_object_assert_init 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ciThis function is called to assert that an object has been initialized. 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ciWhen the real object is not tracked by debugobjects, it calls 19462306a36Sopenharmony_cifixup_assert_init of the object type description structure provided by 19562306a36Sopenharmony_cithe caller, with the hardcoded object state ODEBUG_NOT_AVAILABLE. The 19662306a36Sopenharmony_cifixup function can correct the problem by calling debug_object_init 19762306a36Sopenharmony_ciand other specific initializing functions. 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ciWhen the real object is already tracked by debugobjects it is ignored. 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ciFixup functions 20262306a36Sopenharmony_ci=============== 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ciDebug object type description structure 20562306a36Sopenharmony_ci--------------------------------------- 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci.. kernel-doc:: include/linux/debugobjects.h 20862306a36Sopenharmony_ci :internal: 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_cifixup_init 21162306a36Sopenharmony_ci----------- 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ciThis function is called from the debug code whenever a problem in 21462306a36Sopenharmony_cidebug_object_init is detected. The function takes the address of the 21562306a36Sopenharmony_ciobject and the state which is currently recorded in the tracker. 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ciCalled from debug_object_init when the object state is: 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci- ODEBUG_STATE_ACTIVE 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 22262306a36Sopenharmony_cifalse. The return value is used to update the statistics. 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ciNote, that the function needs to call the debug_object_init() function 22562306a36Sopenharmony_ciagain, after the damage has been repaired in order to keep the state 22662306a36Sopenharmony_ciconsistent. 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_cifixup_activate 22962306a36Sopenharmony_ci--------------- 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ciThis function is called from the debug code whenever a problem in 23262306a36Sopenharmony_cidebug_object_activate is detected. 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ciCalled from debug_object_activate when the object state is: 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci- ODEBUG_STATE_NOTAVAILABLE 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci- ODEBUG_STATE_ACTIVE 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 24162306a36Sopenharmony_cifalse. The return value is used to update the statistics. 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ciNote that the function needs to call the debug_object_activate() 24462306a36Sopenharmony_cifunction again after the damage has been repaired in order to keep the 24562306a36Sopenharmony_cistate consistent. 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ciThe activation of statically initialized objects is a special case. When 24862306a36Sopenharmony_cidebug_object_activate() has no tracked object for this object address 24962306a36Sopenharmony_cithen fixup_activate() is called with object state 25062306a36Sopenharmony_ciODEBUG_STATE_NOTAVAILABLE. The fixup function needs to check whether 25162306a36Sopenharmony_cithis is a legitimate case of a statically initialized object or not. In 25262306a36Sopenharmony_cicase it is it calls debug_object_init() and debug_object_activate() 25362306a36Sopenharmony_cito make the object known to the tracker and marked active. In this case 25462306a36Sopenharmony_cithe function should return false because this is not a real fixup. 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_cifixup_destroy 25762306a36Sopenharmony_ci-------------- 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ciThis function is called from the debug code whenever a problem in 26062306a36Sopenharmony_cidebug_object_destroy is detected. 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ciCalled from debug_object_destroy when the object state is: 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci- ODEBUG_STATE_ACTIVE 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 26762306a36Sopenharmony_cifalse. The return value is used to update the statistics. 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_cifixup_free 27062306a36Sopenharmony_ci----------- 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ciThis function is called from the debug code whenever a problem in 27362306a36Sopenharmony_cidebug_object_free is detected. Further it can be called from the debug 27462306a36Sopenharmony_cichecks in kfree/vfree, when an active object is detected from the 27562306a36Sopenharmony_cidebug_check_no_obj_freed() sanity checks. 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ciCalled from debug_object_free() or debug_check_no_obj_freed() when 27862306a36Sopenharmony_cithe object state is: 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci- ODEBUG_STATE_ACTIVE 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 28362306a36Sopenharmony_cifalse. The return value is used to update the statistics. 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_cifixup_assert_init 28662306a36Sopenharmony_ci------------------- 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ciThis function is called from the debug code whenever a problem in 28962306a36Sopenharmony_cidebug_object_assert_init is detected. 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ciCalled from debug_object_assert_init() with a hardcoded state 29262306a36Sopenharmony_ciODEBUG_STATE_NOTAVAILABLE when the object is not found in the debug 29362306a36Sopenharmony_cibucket. 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 29662306a36Sopenharmony_cifalse. The return value is used to update the statistics. 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ciNote, this function should make sure debug_object_init() is called 29962306a36Sopenharmony_cibefore returning. 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ciThe handling of statically initialized objects is a special case. The 30262306a36Sopenharmony_cifixup function should check if this is a legitimate case of a statically 30362306a36Sopenharmony_ciinitialized object or not. In this case only debug_object_init() 30462306a36Sopenharmony_cishould be called to make the object known to the tracker. Then the 30562306a36Sopenharmony_cifunction should return false because this is not a real fixup. 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ciKnown Bugs And Assumptions 30862306a36Sopenharmony_ci========================== 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ciNone (knock on wood). 311