1*4882a593Smuzhiyun============================================ 2*4882a593SmuzhiyunThe object-lifetime debugging infrastructure 3*4882a593Smuzhiyun============================================ 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Thomas Gleixner 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunIntroduction 8*4882a593Smuzhiyun============ 9*4882a593Smuzhiyun 10*4882a593Smuzhiyundebugobjects is a generic infrastructure to track the life time of 11*4882a593Smuzhiyunkernel objects and validate the operations on those. 12*4882a593Smuzhiyun 13*4882a593Smuzhiyundebugobjects is useful to check for the following error patterns: 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun- Activation of uninitialized objects 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun- Initialization of active objects 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun- Usage of freed/destroyed objects 20*4882a593Smuzhiyun 21*4882a593Smuzhiyundebugobjects is not changing the data structure of the real object so it 22*4882a593Smuzhiyuncan be compiled in with a minimal runtime impact and enabled on demand 23*4882a593Smuzhiyunwith a kernel command line option. 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunHowto use debugobjects 26*4882a593Smuzhiyun====================== 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunA kernel subsystem needs to provide a data structure which describes the 29*4882a593Smuzhiyunobject type and add calls into the debug code at appropriate places. The 30*4882a593Smuzhiyundata structure to describe the object type needs at minimum the name of 31*4882a593Smuzhiyunthe object type. Optional functions can and should be provided to fixup 32*4882a593Smuzhiyundetected problems so the kernel can continue to work and the debug 33*4882a593Smuzhiyuninformation can be retrieved from a live system instead of hard core 34*4882a593Smuzhiyundebugging with serial consoles and stack trace transcripts from the 35*4882a593Smuzhiyunmonitor. 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunThe debug calls provided by debugobjects are: 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun- debug_object_init 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun- debug_object_init_on_stack 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun- debug_object_activate 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun- debug_object_deactivate 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun- debug_object_destroy 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun- debug_object_free 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun- debug_object_assert_init 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunEach of these functions takes the address of the real object and a 54*4882a593Smuzhiyunpointer to the object type specific debug description structure. 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunEach detected error is reported in the statistics and a limited number 57*4882a593Smuzhiyunof errors are printk'ed including a full stack trace. 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunThe statistics are available via /sys/kernel/debug/debug_objects/stats. 60*4882a593SmuzhiyunThey provide information about the number of warnings and the number of 61*4882a593Smuzhiyunsuccessful fixups along with information about the usage of the internal 62*4882a593Smuzhiyuntracking objects and the state of the internal tracking objects pool. 63*4882a593Smuzhiyun 64*4882a593SmuzhiyunDebug functions 65*4882a593Smuzhiyun=============== 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 68*4882a593Smuzhiyun :functions: debug_object_init 69*4882a593Smuzhiyun 70*4882a593SmuzhiyunThis function is called whenever the initialization function of a real 71*4882a593Smuzhiyunobject is called. 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunWhen the real object is already tracked by debugobjects it is checked, 74*4882a593Smuzhiyunwhether the object can be initialized. Initializing is not allowed for 75*4882a593Smuzhiyunactive and destroyed objects. When debugobjects detects an error, then 76*4882a593Smuzhiyunit calls the fixup_init function of the object type description 77*4882a593Smuzhiyunstructure if provided by the caller. The fixup function can correct the 78*4882a593Smuzhiyunproblem before the real initialization of the object happens. E.g. it 79*4882a593Smuzhiyuncan deactivate an active object in order to prevent damage to the 80*4882a593Smuzhiyunsubsystem. 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunWhen the real object is not yet tracked by debugobjects, debugobjects 83*4882a593Smuzhiyunallocates a tracker object for the real object and sets the tracker 84*4882a593Smuzhiyunobject state to ODEBUG_STATE_INIT. It verifies that the object is not 85*4882a593Smuzhiyunon the callers stack. If it is on the callers stack then a limited 86*4882a593Smuzhiyunnumber of warnings including a full stack trace is printk'ed. The 87*4882a593Smuzhiyuncalling code must use debug_object_init_on_stack() and remove the 88*4882a593Smuzhiyunobject before leaving the function which allocated it. See next section. 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 91*4882a593Smuzhiyun :functions: debug_object_init_on_stack 92*4882a593Smuzhiyun 93*4882a593SmuzhiyunThis function is called whenever the initialization function of a real 94*4882a593Smuzhiyunobject which resides on the stack is called. 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunWhen the real object is already tracked by debugobjects it is checked, 97*4882a593Smuzhiyunwhether the object can be initialized. Initializing is not allowed for 98*4882a593Smuzhiyunactive and destroyed objects. When debugobjects detects an error, then 99*4882a593Smuzhiyunit calls the fixup_init function of the object type description 100*4882a593Smuzhiyunstructure if provided by the caller. The fixup function can correct the 101*4882a593Smuzhiyunproblem before the real initialization of the object happens. E.g. it 102*4882a593Smuzhiyuncan deactivate an active object in order to prevent damage to the 103*4882a593Smuzhiyunsubsystem. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunWhen the real object is not yet tracked by debugobjects debugobjects 106*4882a593Smuzhiyunallocates a tracker object for the real object and sets the tracker 107*4882a593Smuzhiyunobject state to ODEBUG_STATE_INIT. It verifies that the object is on 108*4882a593Smuzhiyunthe callers stack. 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunAn object which is on the stack must be removed from the tracker by 111*4882a593Smuzhiyuncalling debug_object_free() before the function which allocates the 112*4882a593Smuzhiyunobject returns. Otherwise we keep track of stale objects. 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 115*4882a593Smuzhiyun :functions: debug_object_activate 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunThis function is called whenever the activation function of a real 118*4882a593Smuzhiyunobject is called. 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunWhen the real object is already tracked by debugobjects it is checked, 121*4882a593Smuzhiyunwhether the object can be activated. Activating is not allowed for 122*4882a593Smuzhiyunactive and destroyed objects. When debugobjects detects an error, then 123*4882a593Smuzhiyunit calls the fixup_activate function of the object type description 124*4882a593Smuzhiyunstructure if provided by the caller. The fixup function can correct the 125*4882a593Smuzhiyunproblem before the real activation of the object happens. E.g. it can 126*4882a593Smuzhiyundeactivate an active object in order to prevent damage to the subsystem. 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunWhen the real object is not yet tracked by debugobjects then the 129*4882a593Smuzhiyunfixup_activate function is called if available. This is necessary to 130*4882a593Smuzhiyunallow the legitimate activation of statically allocated and initialized 131*4882a593Smuzhiyunobjects. The fixup function checks whether the object is valid and calls 132*4882a593Smuzhiyunthe debug_objects_init() function to initialize the tracking of this 133*4882a593Smuzhiyunobject. 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunWhen the activation is legitimate, then the state of the associated 136*4882a593Smuzhiyuntracker object is set to ODEBUG_STATE_ACTIVE. 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 140*4882a593Smuzhiyun :functions: debug_object_deactivate 141*4882a593Smuzhiyun 142*4882a593SmuzhiyunThis function is called whenever the deactivation function of a real 143*4882a593Smuzhiyunobject is called. 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunWhen the real object is tracked by debugobjects it is checked, whether 146*4882a593Smuzhiyunthe object can be deactivated. Deactivating is not allowed for untracked 147*4882a593Smuzhiyunor destroyed objects. 148*4882a593Smuzhiyun 149*4882a593SmuzhiyunWhen the deactivation is legitimate, then the state of the associated 150*4882a593Smuzhiyuntracker object is set to ODEBUG_STATE_INACTIVE. 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 153*4882a593Smuzhiyun :functions: debug_object_destroy 154*4882a593Smuzhiyun 155*4882a593SmuzhiyunThis function is called to mark an object destroyed. This is useful to 156*4882a593Smuzhiyunprevent the usage of invalid objects, which are still available in 157*4882a593Smuzhiyunmemory: either statically allocated objects or objects which are freed 158*4882a593Smuzhiyunlater. 159*4882a593Smuzhiyun 160*4882a593SmuzhiyunWhen the real object is tracked by debugobjects it is checked, whether 161*4882a593Smuzhiyunthe object can be destroyed. Destruction is not allowed for active and 162*4882a593Smuzhiyundestroyed objects. When debugobjects detects an error, then it calls the 163*4882a593Smuzhiyunfixup_destroy function of the object type description structure if 164*4882a593Smuzhiyunprovided by the caller. The fixup function can correct the problem 165*4882a593Smuzhiyunbefore the real destruction of the object happens. E.g. it can 166*4882a593Smuzhiyundeactivate an active object in order to prevent damage to the subsystem. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunWhen the destruction is legitimate, then the state of the associated 169*4882a593Smuzhiyuntracker object is set to ODEBUG_STATE_DESTROYED. 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 172*4882a593Smuzhiyun :functions: debug_object_free 173*4882a593Smuzhiyun 174*4882a593SmuzhiyunThis function is called before an object is freed. 175*4882a593Smuzhiyun 176*4882a593SmuzhiyunWhen the real object is tracked by debugobjects it is checked, whether 177*4882a593Smuzhiyunthe object can be freed. Free is not allowed for active objects. When 178*4882a593Smuzhiyundebugobjects detects an error, then it calls the fixup_free function of 179*4882a593Smuzhiyunthe object type description structure if provided by the caller. The 180*4882a593Smuzhiyunfixup function can correct the problem before the real free of the 181*4882a593Smuzhiyunobject happens. E.g. it can deactivate an active object in order to 182*4882a593Smuzhiyunprevent damage to the subsystem. 183*4882a593Smuzhiyun 184*4882a593SmuzhiyunNote that debug_object_free removes the object from the tracker. Later 185*4882a593Smuzhiyunusage of the object is detected by the other debug checks. 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun.. kernel-doc:: lib/debugobjects.c 189*4882a593Smuzhiyun :functions: debug_object_assert_init 190*4882a593Smuzhiyun 191*4882a593SmuzhiyunThis function is called to assert that an object has been initialized. 192*4882a593Smuzhiyun 193*4882a593SmuzhiyunWhen the real object is not tracked by debugobjects, it calls 194*4882a593Smuzhiyunfixup_assert_init of the object type description structure provided by 195*4882a593Smuzhiyunthe caller, with the hardcoded object state ODEBUG_NOT_AVAILABLE. The 196*4882a593Smuzhiyunfixup function can correct the problem by calling debug_object_init 197*4882a593Smuzhiyunand other specific initializing functions. 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunWhen the real object is already tracked by debugobjects it is ignored. 200*4882a593Smuzhiyun 201*4882a593SmuzhiyunFixup functions 202*4882a593Smuzhiyun=============== 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunDebug object type description structure 205*4882a593Smuzhiyun--------------------------------------- 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun.. kernel-doc:: include/linux/debugobjects.h 208*4882a593Smuzhiyun :internal: 209*4882a593Smuzhiyun 210*4882a593Smuzhiyunfixup_init 211*4882a593Smuzhiyun----------- 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunThis function is called from the debug code whenever a problem in 214*4882a593Smuzhiyundebug_object_init is detected. The function takes the address of the 215*4882a593Smuzhiyunobject and the state which is currently recorded in the tracker. 216*4882a593Smuzhiyun 217*4882a593SmuzhiyunCalled from debug_object_init when the object state is: 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun- ODEBUG_STATE_ACTIVE 220*4882a593Smuzhiyun 221*4882a593SmuzhiyunThe function returns true when the fixup was successful, otherwise 222*4882a593Smuzhiyunfalse. The return value is used to update the statistics. 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunNote, that the function needs to call the debug_object_init() function 225*4882a593Smuzhiyunagain, after the damage has been repaired in order to keep the state 226*4882a593Smuzhiyunconsistent. 227*4882a593Smuzhiyun 228*4882a593Smuzhiyunfixup_activate 229*4882a593Smuzhiyun--------------- 230*4882a593Smuzhiyun 231*4882a593SmuzhiyunThis function is called from the debug code whenever a problem in 232*4882a593Smuzhiyundebug_object_activate is detected. 233*4882a593Smuzhiyun 234*4882a593SmuzhiyunCalled from debug_object_activate when the object state is: 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun- ODEBUG_STATE_NOTAVAILABLE 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun- ODEBUG_STATE_ACTIVE 239*4882a593Smuzhiyun 240*4882a593SmuzhiyunThe function returns true when the fixup was successful, otherwise 241*4882a593Smuzhiyunfalse. The return value is used to update the statistics. 242*4882a593Smuzhiyun 243*4882a593SmuzhiyunNote that the function needs to call the debug_object_activate() 244*4882a593Smuzhiyunfunction again after the damage has been repaired in order to keep the 245*4882a593Smuzhiyunstate consistent. 246*4882a593Smuzhiyun 247*4882a593SmuzhiyunThe activation of statically initialized objects is a special case. When 248*4882a593Smuzhiyundebug_object_activate() has no tracked object for this object address 249*4882a593Smuzhiyunthen fixup_activate() is called with object state 250*4882a593SmuzhiyunODEBUG_STATE_NOTAVAILABLE. The fixup function needs to check whether 251*4882a593Smuzhiyunthis is a legitimate case of a statically initialized object or not. In 252*4882a593Smuzhiyuncase it is it calls debug_object_init() and debug_object_activate() 253*4882a593Smuzhiyunto make the object known to the tracker and marked active. In this case 254*4882a593Smuzhiyunthe function should return false because this is not a real fixup. 255*4882a593Smuzhiyun 256*4882a593Smuzhiyunfixup_destroy 257*4882a593Smuzhiyun-------------- 258*4882a593Smuzhiyun 259*4882a593SmuzhiyunThis function is called from the debug code whenever a problem in 260*4882a593Smuzhiyundebug_object_destroy is detected. 261*4882a593Smuzhiyun 262*4882a593SmuzhiyunCalled from debug_object_destroy when the object state is: 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun- ODEBUG_STATE_ACTIVE 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunThe function returns true when the fixup was successful, otherwise 267*4882a593Smuzhiyunfalse. The return value is used to update the statistics. 268*4882a593Smuzhiyun 269*4882a593Smuzhiyunfixup_free 270*4882a593Smuzhiyun----------- 271*4882a593Smuzhiyun 272*4882a593SmuzhiyunThis function is called from the debug code whenever a problem in 273*4882a593Smuzhiyundebug_object_free is detected. Further it can be called from the debug 274*4882a593Smuzhiyunchecks in kfree/vfree, when an active object is detected from the 275*4882a593Smuzhiyundebug_check_no_obj_freed() sanity checks. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunCalled from debug_object_free() or debug_check_no_obj_freed() when 278*4882a593Smuzhiyunthe object state is: 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun- ODEBUG_STATE_ACTIVE 281*4882a593Smuzhiyun 282*4882a593SmuzhiyunThe function returns true when the fixup was successful, otherwise 283*4882a593Smuzhiyunfalse. The return value is used to update the statistics. 284*4882a593Smuzhiyun 285*4882a593Smuzhiyunfixup_assert_init 286*4882a593Smuzhiyun------------------- 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunThis function is called from the debug code whenever a problem in 289*4882a593Smuzhiyundebug_object_assert_init is detected. 290*4882a593Smuzhiyun 291*4882a593SmuzhiyunCalled from debug_object_assert_init() with a hardcoded state 292*4882a593SmuzhiyunODEBUG_STATE_NOTAVAILABLE when the object is not found in the debug 293*4882a593Smuzhiyunbucket. 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunThe function returns true when the fixup was successful, otherwise 296*4882a593Smuzhiyunfalse. The return value is used to update the statistics. 297*4882a593Smuzhiyun 298*4882a593SmuzhiyunNote, this function should make sure debug_object_init() is called 299*4882a593Smuzhiyunbefore returning. 300*4882a593Smuzhiyun 301*4882a593SmuzhiyunThe handling of statically initialized objects is a special case. The 302*4882a593Smuzhiyunfixup function should check if this is a legitimate case of a statically 303*4882a593Smuzhiyuninitialized object or not. In this case only debug_object_init() 304*4882a593Smuzhiyunshould be called to make the object known to the tracker. Then the 305*4882a593Smuzhiyunfunction should return false because this is not a real fixup. 306*4882a593Smuzhiyun 307*4882a593SmuzhiyunKnown Bugs And Assumptions 308*4882a593Smuzhiyun========================== 309*4882a593Smuzhiyun 310*4882a593SmuzhiyunNone (knock on wood). 311