18c2ecf20Sopenharmony_ci============================================ 28c2ecf20Sopenharmony_ciThe object-lifetime debugging infrastructure 38c2ecf20Sopenharmony_ci============================================ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci:Author: Thomas Gleixner 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ciIntroduction 88c2ecf20Sopenharmony_ci============ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_cidebugobjects is a generic infrastructure to track the life time of 118c2ecf20Sopenharmony_cikernel objects and validate the operations on those. 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cidebugobjects is useful to check for the following error patterns: 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci- Activation of uninitialized objects 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci- Initialization of active objects 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci- Usage of freed/destroyed objects 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cidebugobjects is not changing the data structure of the real object so it 228c2ecf20Sopenharmony_cican be compiled in with a minimal runtime impact and enabled on demand 238c2ecf20Sopenharmony_ciwith a kernel command line option. 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ciHowto use debugobjects 268c2ecf20Sopenharmony_ci====================== 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ciA kernel subsystem needs to provide a data structure which describes the 298c2ecf20Sopenharmony_ciobject type and add calls into the debug code at appropriate places. The 308c2ecf20Sopenharmony_cidata structure to describe the object type needs at minimum the name of 318c2ecf20Sopenharmony_cithe object type. Optional functions can and should be provided to fixup 328c2ecf20Sopenharmony_cidetected problems so the kernel can continue to work and the debug 338c2ecf20Sopenharmony_ciinformation can be retrieved from a live system instead of hard core 348c2ecf20Sopenharmony_cidebugging with serial consoles and stack trace transcripts from the 358c2ecf20Sopenharmony_cimonitor. 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciThe debug calls provided by debugobjects are: 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci- debug_object_init 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci- debug_object_init_on_stack 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci- debug_object_activate 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci- debug_object_deactivate 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci- debug_object_destroy 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci- debug_object_free 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci- debug_object_assert_init 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ciEach of these functions takes the address of the real object and a 548c2ecf20Sopenharmony_cipointer to the object type specific debug description structure. 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ciEach detected error is reported in the statistics and a limited number 578c2ecf20Sopenharmony_ciof errors are printk'ed including a full stack trace. 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciThe statistics are available via /sys/kernel/debug/debug_objects/stats. 608c2ecf20Sopenharmony_ciThey provide information about the number of warnings and the number of 618c2ecf20Sopenharmony_cisuccessful fixups along with information about the usage of the internal 628c2ecf20Sopenharmony_citracking objects and the state of the internal tracking objects pool. 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ciDebug functions 658c2ecf20Sopenharmony_ci=============== 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 688c2ecf20Sopenharmony_ci :functions: debug_object_init 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ciThis function is called whenever the initialization function of a real 718c2ecf20Sopenharmony_ciobject is called. 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ciWhen the real object is already tracked by debugobjects it is checked, 748c2ecf20Sopenharmony_ciwhether the object can be initialized. Initializing is not allowed for 758c2ecf20Sopenharmony_ciactive and destroyed objects. When debugobjects detects an error, then 768c2ecf20Sopenharmony_ciit calls the fixup_init function of the object type description 778c2ecf20Sopenharmony_cistructure if provided by the caller. The fixup function can correct the 788c2ecf20Sopenharmony_ciproblem before the real initialization of the object happens. E.g. it 798c2ecf20Sopenharmony_cican deactivate an active object in order to prevent damage to the 808c2ecf20Sopenharmony_cisubsystem. 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciWhen the real object is not yet tracked by debugobjects, debugobjects 838c2ecf20Sopenharmony_ciallocates a tracker object for the real object and sets the tracker 848c2ecf20Sopenharmony_ciobject state to ODEBUG_STATE_INIT. It verifies that the object is not 858c2ecf20Sopenharmony_cion the callers stack. If it is on the callers stack then a limited 868c2ecf20Sopenharmony_cinumber of warnings including a full stack trace is printk'ed. The 878c2ecf20Sopenharmony_cicalling code must use debug_object_init_on_stack() and remove the 888c2ecf20Sopenharmony_ciobject before leaving the function which allocated it. See next section. 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 918c2ecf20Sopenharmony_ci :functions: debug_object_init_on_stack 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ciThis function is called whenever the initialization function of a real 948c2ecf20Sopenharmony_ciobject which resides on the stack is called. 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciWhen the real object is already tracked by debugobjects it is checked, 978c2ecf20Sopenharmony_ciwhether the object can be initialized. Initializing is not allowed for 988c2ecf20Sopenharmony_ciactive and destroyed objects. When debugobjects detects an error, then 998c2ecf20Sopenharmony_ciit calls the fixup_init function of the object type description 1008c2ecf20Sopenharmony_cistructure if provided by the caller. The fixup function can correct the 1018c2ecf20Sopenharmony_ciproblem before the real initialization of the object happens. E.g. it 1028c2ecf20Sopenharmony_cican deactivate an active object in order to prevent damage to the 1038c2ecf20Sopenharmony_cisubsystem. 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ciWhen the real object is not yet tracked by debugobjects debugobjects 1068c2ecf20Sopenharmony_ciallocates a tracker object for the real object and sets the tracker 1078c2ecf20Sopenharmony_ciobject state to ODEBUG_STATE_INIT. It verifies that the object is on 1088c2ecf20Sopenharmony_cithe callers stack. 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ciAn object which is on the stack must be removed from the tracker by 1118c2ecf20Sopenharmony_cicalling debug_object_free() before the function which allocates the 1128c2ecf20Sopenharmony_ciobject returns. Otherwise we keep track of stale objects. 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 1158c2ecf20Sopenharmony_ci :functions: debug_object_activate 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ciThis function is called whenever the activation function of a real 1188c2ecf20Sopenharmony_ciobject is called. 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciWhen the real object is already tracked by debugobjects it is checked, 1218c2ecf20Sopenharmony_ciwhether the object can be activated. Activating is not allowed for 1228c2ecf20Sopenharmony_ciactive and destroyed objects. When debugobjects detects an error, then 1238c2ecf20Sopenharmony_ciit calls the fixup_activate function of the object type description 1248c2ecf20Sopenharmony_cistructure if provided by the caller. The fixup function can correct the 1258c2ecf20Sopenharmony_ciproblem before the real activation of the object happens. E.g. it can 1268c2ecf20Sopenharmony_cideactivate an active object in order to prevent damage to the subsystem. 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ciWhen the real object is not yet tracked by debugobjects then the 1298c2ecf20Sopenharmony_cifixup_activate function is called if available. This is necessary to 1308c2ecf20Sopenharmony_ciallow the legitimate activation of statically allocated and initialized 1318c2ecf20Sopenharmony_ciobjects. The fixup function checks whether the object is valid and calls 1328c2ecf20Sopenharmony_cithe debug_objects_init() function to initialize the tracking of this 1338c2ecf20Sopenharmony_ciobject. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ciWhen the activation is legitimate, then the state of the associated 1368c2ecf20Sopenharmony_citracker object is set to ODEBUG_STATE_ACTIVE. 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 1408c2ecf20Sopenharmony_ci :functions: debug_object_deactivate 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciThis function is called whenever the deactivation function of a real 1438c2ecf20Sopenharmony_ciobject is called. 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciWhen the real object is tracked by debugobjects it is checked, whether 1468c2ecf20Sopenharmony_cithe object can be deactivated. Deactivating is not allowed for untracked 1478c2ecf20Sopenharmony_cior destroyed objects. 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciWhen the deactivation is legitimate, then the state of the associated 1508c2ecf20Sopenharmony_citracker object is set to ODEBUG_STATE_INACTIVE. 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 1538c2ecf20Sopenharmony_ci :functions: debug_object_destroy 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ciThis function is called to mark an object destroyed. This is useful to 1568c2ecf20Sopenharmony_ciprevent the usage of invalid objects, which are still available in 1578c2ecf20Sopenharmony_cimemory: either statically allocated objects or objects which are freed 1588c2ecf20Sopenharmony_cilater. 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ciWhen the real object is tracked by debugobjects it is checked, whether 1618c2ecf20Sopenharmony_cithe object can be destroyed. Destruction is not allowed for active and 1628c2ecf20Sopenharmony_cidestroyed objects. When debugobjects detects an error, then it calls the 1638c2ecf20Sopenharmony_cifixup_destroy function of the object type description structure if 1648c2ecf20Sopenharmony_ciprovided by the caller. The fixup function can correct the problem 1658c2ecf20Sopenharmony_cibefore the real destruction of the object happens. E.g. it can 1668c2ecf20Sopenharmony_cideactivate an active object in order to prevent damage to the subsystem. 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ciWhen the destruction is legitimate, then the state of the associated 1698c2ecf20Sopenharmony_citracker object is set to ODEBUG_STATE_DESTROYED. 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 1728c2ecf20Sopenharmony_ci :functions: debug_object_free 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ciThis function is called before an object is freed. 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ciWhen the real object is tracked by debugobjects it is checked, whether 1778c2ecf20Sopenharmony_cithe object can be freed. Free is not allowed for active objects. When 1788c2ecf20Sopenharmony_cidebugobjects detects an error, then it calls the fixup_free function of 1798c2ecf20Sopenharmony_cithe object type description structure if provided by the caller. The 1808c2ecf20Sopenharmony_cifixup function can correct the problem before the real free of the 1818c2ecf20Sopenharmony_ciobject happens. E.g. it can deactivate an active object in order to 1828c2ecf20Sopenharmony_ciprevent damage to the subsystem. 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ciNote that debug_object_free removes the object from the tracker. Later 1858c2ecf20Sopenharmony_ciusage of the object is detected by the other debug checks. 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci.. kernel-doc:: lib/debugobjects.c 1898c2ecf20Sopenharmony_ci :functions: debug_object_assert_init 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ciThis function is called to assert that an object has been initialized. 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ciWhen the real object is not tracked by debugobjects, it calls 1948c2ecf20Sopenharmony_cifixup_assert_init of the object type description structure provided by 1958c2ecf20Sopenharmony_cithe caller, with the hardcoded object state ODEBUG_NOT_AVAILABLE. The 1968c2ecf20Sopenharmony_cifixup function can correct the problem by calling debug_object_init 1978c2ecf20Sopenharmony_ciand other specific initializing functions. 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ciWhen the real object is already tracked by debugobjects it is ignored. 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ciFixup functions 2028c2ecf20Sopenharmony_ci=============== 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ciDebug object type description structure 2058c2ecf20Sopenharmony_ci--------------------------------------- 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/debugobjects.h 2088c2ecf20Sopenharmony_ci :internal: 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cifixup_init 2118c2ecf20Sopenharmony_ci----------- 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciThis function is called from the debug code whenever a problem in 2148c2ecf20Sopenharmony_cidebug_object_init is detected. The function takes the address of the 2158c2ecf20Sopenharmony_ciobject and the state which is currently recorded in the tracker. 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ciCalled from debug_object_init when the object state is: 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci- ODEBUG_STATE_ACTIVE 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 2228c2ecf20Sopenharmony_cifalse. The return value is used to update the statistics. 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ciNote, that the function needs to call the debug_object_init() function 2258c2ecf20Sopenharmony_ciagain, after the damage has been repaired in order to keep the state 2268c2ecf20Sopenharmony_ciconsistent. 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cifixup_activate 2298c2ecf20Sopenharmony_ci--------------- 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ciThis function is called from the debug code whenever a problem in 2328c2ecf20Sopenharmony_cidebug_object_activate is detected. 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ciCalled from debug_object_activate when the object state is: 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci- ODEBUG_STATE_NOTAVAILABLE 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci- ODEBUG_STATE_ACTIVE 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 2418c2ecf20Sopenharmony_cifalse. The return value is used to update the statistics. 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ciNote that the function needs to call the debug_object_activate() 2448c2ecf20Sopenharmony_cifunction again after the damage has been repaired in order to keep the 2458c2ecf20Sopenharmony_cistate consistent. 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ciThe activation of statically initialized objects is a special case. When 2488c2ecf20Sopenharmony_cidebug_object_activate() has no tracked object for this object address 2498c2ecf20Sopenharmony_cithen fixup_activate() is called with object state 2508c2ecf20Sopenharmony_ciODEBUG_STATE_NOTAVAILABLE. The fixup function needs to check whether 2518c2ecf20Sopenharmony_cithis is a legitimate case of a statically initialized object or not. In 2528c2ecf20Sopenharmony_cicase it is it calls debug_object_init() and debug_object_activate() 2538c2ecf20Sopenharmony_cito make the object known to the tracker and marked active. In this case 2548c2ecf20Sopenharmony_cithe function should return false because this is not a real fixup. 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cifixup_destroy 2578c2ecf20Sopenharmony_ci-------------- 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ciThis function is called from the debug code whenever a problem in 2608c2ecf20Sopenharmony_cidebug_object_destroy is detected. 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ciCalled from debug_object_destroy when the object state is: 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci- ODEBUG_STATE_ACTIVE 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 2678c2ecf20Sopenharmony_cifalse. The return value is used to update the statistics. 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cifixup_free 2708c2ecf20Sopenharmony_ci----------- 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ciThis function is called from the debug code whenever a problem in 2738c2ecf20Sopenharmony_cidebug_object_free is detected. Further it can be called from the debug 2748c2ecf20Sopenharmony_cichecks in kfree/vfree, when an active object is detected from the 2758c2ecf20Sopenharmony_cidebug_check_no_obj_freed() sanity checks. 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ciCalled from debug_object_free() or debug_check_no_obj_freed() when 2788c2ecf20Sopenharmony_cithe object state is: 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci- ODEBUG_STATE_ACTIVE 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 2838c2ecf20Sopenharmony_cifalse. The return value is used to update the statistics. 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cifixup_assert_init 2868c2ecf20Sopenharmony_ci------------------- 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ciThis function is called from the debug code whenever a problem in 2898c2ecf20Sopenharmony_cidebug_object_assert_init is detected. 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ciCalled from debug_object_assert_init() with a hardcoded state 2928c2ecf20Sopenharmony_ciODEBUG_STATE_NOTAVAILABLE when the object is not found in the debug 2938c2ecf20Sopenharmony_cibucket. 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ciThe function returns true when the fixup was successful, otherwise 2968c2ecf20Sopenharmony_cifalse. The return value is used to update the statistics. 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ciNote, this function should make sure debug_object_init() is called 2998c2ecf20Sopenharmony_cibefore returning. 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ciThe handling of statically initialized objects is a special case. The 3028c2ecf20Sopenharmony_cifixup function should check if this is a legitimate case of a statically 3038c2ecf20Sopenharmony_ciinitialized object or not. In this case only debug_object_init() 3048c2ecf20Sopenharmony_cishould be called to make the object known to the tracker. Then the 3058c2ecf20Sopenharmony_cifunction should return false because this is not a real fixup. 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ciKnown Bugs And Assumptions 3088c2ecf20Sopenharmony_ci========================== 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ciNone (knock on wood). 311