kmemleak.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. /*
  2. * mm/kmemleak.c
  3. *
  4. * Copyright (C) 2008 ARM Limited
  5. * Written by Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. *
  21. * For more information on the algorithm and kmemleak usage, please see
  22. * Documentation/kmemleak.txt.
  23. *
  24. * Notes on locking
  25. * ----------------
  26. *
  27. * The following locks and mutexes are used by kmemleak:
  28. *
  29. * - kmemleak_lock (rwlock): protects the object_list modifications and
  30. * accesses to the object_tree_root. The object_list is the main list
  31. * holding the metadata (struct kmemleak_object) for the allocated memory
  32. * blocks. The object_tree_root is a priority search tree used to look-up
  33. * metadata based on a pointer to the corresponding memory block. The
  34. * kmemleak_object structures are added to the object_list and
  35. * object_tree_root in the create_object() function called from the
  36. * kmemleak_alloc() callback and removed in delete_object() called from the
  37. * kmemleak_free() callback
  38. * - kmemleak_object.lock (spinlock): protects a kmemleak_object. Accesses to
  39. * the metadata (e.g. count) are protected by this lock. Note that some
  40. * members of this structure may be protected by other means (atomic or
  41. * kmemleak_lock). This lock is also held when scanning the corresponding
  42. * memory block to avoid the kernel freeing it via the kmemleak_free()
  43. * callback. This is less heavyweight than holding a global lock like
  44. * kmemleak_lock during scanning
  45. * - scan_mutex (mutex): ensures that only one thread may scan the memory for
  46. * unreferenced objects at a time. The gray_list contains the objects which
  47. * are already referenced or marked as false positives and need to be
  48. * scanned. This list is only modified during a scanning episode when the
  49. * scan_mutex is held. At the end of a scan, the gray_list is always empty.
  50. * Note that the kmemleak_object.use_count is incremented when an object is
  51. * added to the gray_list and therefore cannot be freed. This mutex also
  52. * prevents multiple users of the "kmemleak" debugfs file together with
  53. * modifications to the memory scanning parameters including the scan_thread
  54. * pointer
  55. *
  56. * The kmemleak_object structures have a use_count incremented or decremented
  57. * using the get_object()/put_object() functions. When the use_count becomes
  58. * 0, this count can no longer be incremented and put_object() schedules the
  59. * kmemleak_object freeing via an RCU callback. All calls to the get_object()
  60. * function must be protected by rcu_read_lock() to avoid accessing a freed
  61. * structure.
  62. */
  63. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  64. #include <linux/init.h>
  65. #include <linux/kernel.h>
  66. #include <linux/list.h>
  67. #include <linux/sched.h>
  68. #include <linux/jiffies.h>
  69. #include <linux/delay.h>
  70. #include <linux/export.h>
  71. #include <linux/kthread.h>
  72. #include <linux/prio_tree.h>
  73. #include <linux/fs.h>
  74. #include <linux/debugfs.h>
  75. #include <linux/seq_file.h>
  76. #include <linux/cpumask.h>
  77. #include <linux/spinlock.h>
  78. #include <linux/mutex.h>
  79. #include <linux/rcupdate.h>
  80. #include <linux/stacktrace.h>
  81. #include <linux/cache.h>
  82. #include <linux/percpu.h>
  83. #include <linux/hardirq.h>
  84. #include <linux/mmzone.h>
  85. #include <linux/slab.h>
  86. #include <linux/thread_info.h>
  87. #include <linux/err.h>
  88. #include <linux/uaccess.h>
  89. #include <linux/string.h>
  90. #include <linux/nodemask.h>
  91. #include <linux/mm.h>
  92. #include <linux/workqueue.h>
  93. #include <linux/crc32.h>
  94. #include <asm/sections.h>
  95. #include <asm/processor.h>
  96. #include <linux/atomic.h>
  97. #include <linux/kmemcheck.h>
  98. #include <linux/kmemleak.h>
  99. #include <linux/memory_hotplug.h>
  100. /*
  101. * Kmemleak configuration and common defines.
  102. */
  103. #define MAX_TRACE 16 /* stack trace length */
  104. #define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
  105. #define SECS_FIRST_SCAN 60 /* delay before the first scan */
  106. #define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */
  107. #define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */
  108. #define BYTES_PER_POINTER sizeof(void *)
  109. /* GFP bitmask for kmemleak internal allocations */
  110. #define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC)) | \
  111. __GFP_NORETRY | __GFP_NOMEMALLOC | \
  112. __GFP_NOWARN)
  113. /* scanning area inside a memory block */
  114. struct kmemleak_scan_area {
  115. struct hlist_node node;
  116. unsigned long start;
  117. size_t size;
  118. };
  119. #define KMEMLEAK_GREY 0
  120. #define KMEMLEAK_BLACK -1
  121. /*
  122. * Structure holding the metadata for each allocated memory block.
  123. * Modifications to such objects should be made while holding the
  124. * object->lock. Insertions or deletions from object_list, gray_list or
  125. * tree_node are already protected by the corresponding locks or mutex (see
  126. * the notes on locking above). These objects are reference-counted
  127. * (use_count) and freed using the RCU mechanism.
  128. */
  129. struct kmemleak_object {
  130. spinlock_t lock;
  131. unsigned long flags; /* object status flags */
  132. struct list_head object_list;
  133. struct list_head gray_list;
  134. struct prio_tree_node tree_node;
  135. struct rcu_head rcu; /* object_list lockless traversal */
  136. /* object usage count; object freed when use_count == 0 */
  137. atomic_t use_count;
  138. unsigned long pointer;
  139. size_t size;
  140. /* minimum number of a pointers found before it is considered leak */
  141. int min_count;
  142. /* the total number of pointers found pointing to this object */
  143. int count;
  144. /* checksum for detecting modified objects */
  145. u32 checksum;
  146. /* memory ranges to be scanned inside an object (empty for all) */
  147. struct hlist_head area_list;
  148. unsigned long trace[MAX_TRACE];
  149. unsigned int trace_len;
  150. unsigned long jiffies; /* creation timestamp */
  151. pid_t pid; /* pid of the current task */
  152. char comm[TASK_COMM_LEN]; /* executable name */
  153. };
  154. /* flag representing the memory block allocation status */
  155. #define OBJECT_ALLOCATED (1 << 0)
  156. /* flag set after the first reporting of an unreference object */
  157. #define OBJECT_REPORTED (1 << 1)
  158. /* flag set to not scan the object */
  159. #define OBJECT_NO_SCAN (1 << 2)
  160. /* number of bytes to print per line; must be 16 or 32 */
  161. #define HEX_ROW_SIZE 16
  162. /* number of bytes to print at a time (1, 2, 4, 8) */
  163. #define HEX_GROUP_SIZE 1
  164. /* include ASCII after the hex output */
  165. #define HEX_ASCII 1
  166. /* max number of lines to be printed */
  167. #define HEX_MAX_LINES 2
  168. /* the list of all allocated objects */
  169. static LIST_HEAD(object_list);
  170. /* the list of gray-colored objects (see color_gray comment below) */
  171. static LIST_HEAD(gray_list);
  172. /* prio search tree for object boundaries */
  173. static struct prio_tree_root object_tree_root;
  174. /* rw_lock protecting the access to object_list and prio_tree_root */
  175. static DEFINE_RWLOCK(kmemleak_lock);
  176. /* allocation caches for kmemleak internal data */
  177. static struct kmem_cache *object_cache;
  178. static struct kmem_cache *scan_area_cache;
  179. /* set if tracing memory operations is enabled */
  180. static atomic_t kmemleak_enabled = ATOMIC_INIT(0);
  181. /* set in the late_initcall if there were no errors */
  182. static atomic_t kmemleak_initialized = ATOMIC_INIT(0);
  183. /* enables or disables early logging of the memory operations */
  184. static atomic_t kmemleak_early_log = ATOMIC_INIT(1);
  185. /* set if a kmemleak warning was issued */
  186. static atomic_t kmemleak_warning = ATOMIC_INIT(0);
  187. /* set if a fatal kmemleak error has occurred */
  188. static atomic_t kmemleak_error = ATOMIC_INIT(0);
  189. /* minimum and maximum address that may be valid pointers */
  190. static unsigned long min_addr = ULONG_MAX;
  191. static unsigned long max_addr;
  192. static struct task_struct *scan_thread;
  193. /* used to avoid reporting of recently allocated objects */
  194. static unsigned long jiffies_min_age;
  195. static unsigned long jiffies_last_scan;
  196. /* delay between automatic memory scannings */
  197. static signed long jiffies_scan_wait;
  198. /* enables or disables the task stacks scanning */
  199. static int kmemleak_stack_scan = 1;
  200. /* protects the memory scanning, parameters and debug/kmemleak file access */
  201. static DEFINE_MUTEX(scan_mutex);
  202. /* setting kmemleak=on, will set this var, skipping the disable */
  203. static int kmemleak_skip_disable;
  204. /*
  205. * Early object allocation/freeing logging. Kmemleak is initialized after the
  206. * kernel allocator. However, both the kernel allocator and kmemleak may
  207. * allocate memory blocks which need to be tracked. Kmemleak defines an
  208. * arbitrary buffer to hold the allocation/freeing information before it is
  209. * fully initialized.
  210. */
  211. /* kmemleak operation type for early logging */
  212. enum {
  213. KMEMLEAK_ALLOC,
  214. KMEMLEAK_ALLOC_PERCPU,
  215. KMEMLEAK_FREE,
  216. KMEMLEAK_FREE_PART,
  217. KMEMLEAK_FREE_PERCPU,
  218. KMEMLEAK_NOT_LEAK,
  219. KMEMLEAK_IGNORE,
  220. KMEMLEAK_SCAN_AREA,
  221. KMEMLEAK_NO_SCAN
  222. };
  223. /*
  224. * Structure holding the information passed to kmemleak callbacks during the
  225. * early logging.
  226. */
  227. struct early_log {
  228. int op_type; /* kmemleak operation type */
  229. const void *ptr; /* allocated/freed memory block */
  230. size_t size; /* memory block size */
  231. int min_count; /* minimum reference count */
  232. unsigned long trace[MAX_TRACE]; /* stack trace */
  233. unsigned int trace_len; /* stack trace length */
  234. };
  235. /* early logging buffer and current position */
  236. static struct early_log
  237. early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE] __initdata;
  238. static int crt_early_log __initdata;
  239. static void kmemleak_disable(void);
  240. /*
  241. * Print a warning and dump the stack trace.
  242. */
  243. #define kmemleak_warn(x...) do { \
  244. pr_warning(x); \
  245. dump_stack(); \
  246. atomic_set(&kmemleak_warning, 1); \
  247. } while (0)
  248. /*
  249. * Macro invoked when a serious kmemleak condition occurred and cannot be
  250. * recovered from. Kmemleak will be disabled and further allocation/freeing
  251. * tracing no longer available.
  252. */
  253. #define kmemleak_stop(x...) do { \
  254. kmemleak_warn(x); \
  255. kmemleak_disable(); \
  256. } while (0)
  257. /*
  258. * Printing of the objects hex dump to the seq file. The number of lines to be
  259. * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The
  260. * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called
  261. * with the object->lock held.
  262. */
  263. static void hex_dump_object(struct seq_file *seq,
  264. struct kmemleak_object *object)
  265. {
  266. const u8 *ptr = (const u8 *)object->pointer;
  267. int i, len, remaining;
  268. unsigned char linebuf[HEX_ROW_SIZE * 5];
  269. /* limit the number of lines to HEX_MAX_LINES */
  270. remaining = len =
  271. min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE));
  272. seq_printf(seq, " hex dump (first %d bytes):\n", len);
  273. for (i = 0; i < len; i += HEX_ROW_SIZE) {
  274. int linelen = min(remaining, HEX_ROW_SIZE);
  275. remaining -= HEX_ROW_SIZE;
  276. hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE,
  277. HEX_GROUP_SIZE, linebuf, sizeof(linebuf),
  278. HEX_ASCII);
  279. seq_printf(seq, " %s\n", linebuf);
  280. }
  281. }
  282. /*
  283. * Object colors, encoded with count and min_count:
  284. * - white - orphan object, not enough references to it (count < min_count)
  285. * - gray - not orphan, not marked as false positive (min_count == 0) or
  286. * sufficient references to it (count >= min_count)
  287. * - black - ignore, it doesn't contain references (e.g. text section)
  288. * (min_count == -1). No function defined for this color.
  289. * Newly created objects don't have any color assigned (object->count == -1)
  290. * before the next memory scan when they become white.
  291. */
  292. static bool color_white(const struct kmemleak_object *object)
  293. {
  294. return object->count != KMEMLEAK_BLACK &&
  295. object->count < object->min_count;
  296. }
  297. static bool color_gray(const struct kmemleak_object *object)
  298. {
  299. return object->min_count != KMEMLEAK_BLACK &&
  300. object->count >= object->min_count;
  301. }
  302. /*
  303. * Objects are considered unreferenced only if their color is white, they have
  304. * not be deleted and have a minimum age to avoid false positives caused by
  305. * pointers temporarily stored in CPU registers.
  306. */
  307. static bool unreferenced_object(struct kmemleak_object *object)
  308. {
  309. return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
  310. time_before_eq(object->jiffies + jiffies_min_age,
  311. jiffies_last_scan);
  312. }
  313. /*
  314. * Printing of the unreferenced objects information to the seq file. The
  315. * print_unreferenced function must be called with the object->lock held.
  316. */
  317. static void print_unreferenced(struct seq_file *seq,
  318. struct kmemleak_object *object)
  319. {
  320. int i;
  321. unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
  322. seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
  323. object->pointer, object->size);
  324. seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n",
  325. object->comm, object->pid, object->jiffies,
  326. msecs_age / 1000, msecs_age % 1000);
  327. hex_dump_object(seq, object);
  328. seq_printf(seq, " backtrace:\n");
  329. for (i = 0; i < object->trace_len; i++) {
  330. void *ptr = (void *)object->trace[i];
  331. seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
  332. }
  333. }
  334. /*
  335. * Print the kmemleak_object information. This function is used mainly for
  336. * debugging special cases when kmemleak operations. It must be called with
  337. * the object->lock held.
  338. */
  339. static void dump_object_info(struct kmemleak_object *object)
  340. {
  341. struct stack_trace trace;
  342. trace.nr_entries = object->trace_len;
  343. trace.entries = object->trace;
  344. pr_notice("Object 0x%08lx (size %zu):\n",
  345. object->tree_node.start, object->size);
  346. pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
  347. object->comm, object->pid, object->jiffies);
  348. pr_notice(" min_count = %d\n", object->min_count);
  349. pr_notice(" count = %d\n", object->count);
  350. pr_notice(" flags = 0x%lx\n", object->flags);
  351. pr_notice(" checksum = %d\n", object->checksum);
  352. pr_notice(" backtrace:\n");
  353. print_stack_trace(&trace, 4);
  354. }
  355. /*
  356. * Look-up a memory block metadata (kmemleak_object) in the priority search
  357. * tree based on a pointer value. If alias is 0, only values pointing to the
  358. * beginning of the memory block are allowed. The kmemleak_lock must be held
  359. * when calling this function.
  360. */
  361. static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
  362. {
  363. struct prio_tree_node *node;
  364. struct prio_tree_iter iter;
  365. struct kmemleak_object *object;
  366. prio_tree_iter_init(&iter, &object_tree_root, ptr, ptr);
  367. node = prio_tree_next(&iter);
  368. if (node) {
  369. object = prio_tree_entry(node, struct kmemleak_object,
  370. tree_node);
  371. if (!alias && object->pointer != ptr) {
  372. kmemleak_warn("Found object by alias at 0x%08lx\n",
  373. ptr);
  374. dump_object_info(object);
  375. object = NULL;
  376. }
  377. } else
  378. object = NULL;
  379. return object;
  380. }
  381. /*
  382. * Increment the object use_count. Return 1 if successful or 0 otherwise. Note
  383. * that once an object's use_count reached 0, the RCU freeing was already
  384. * registered and the object should no longer be used. This function must be
  385. * called under the protection of rcu_read_lock().
  386. */
  387. static int get_object(struct kmemleak_object *object)
  388. {
  389. return atomic_inc_not_zero(&object->use_count);
  390. }
  391. /*
  392. * RCU callback to free a kmemleak_object.
  393. */
  394. static void free_object_rcu(struct rcu_head *rcu)
  395. {
  396. struct hlist_node *elem, *tmp;
  397. struct kmemleak_scan_area *area;
  398. struct kmemleak_object *object =
  399. container_of(rcu, struct kmemleak_object, rcu);
  400. /*
  401. * Once use_count is 0 (guaranteed by put_object), there is no other
  402. * code accessing this object, hence no need for locking.
  403. */
  404. hlist_for_each_entry_safe(area, elem, tmp, &object->area_list, node) {
  405. hlist_del(elem);
  406. kmem_cache_free(scan_area_cache, area);
  407. }
  408. kmem_cache_free(object_cache, object);
  409. }
  410. /*
  411. * Decrement the object use_count. Once the count is 0, free the object using
  412. * an RCU callback. Since put_object() may be called via the kmemleak_free() ->
  413. * delete_object() path, the delayed RCU freeing ensures that there is no
  414. * recursive call to the kernel allocator. Lock-less RCU object_list traversal
  415. * is also possible.
  416. */
  417. static void put_object(struct kmemleak_object *object)
  418. {
  419. if (!atomic_dec_and_test(&object->use_count))
  420. return;
  421. /* should only get here after delete_object was called */
  422. WARN_ON(object->flags & OBJECT_ALLOCATED);
  423. call_rcu(&object->rcu, free_object_rcu);
  424. }
  425. /*
  426. * Look up an object in the prio search tree and increase its use_count.
  427. */
  428. static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias)
  429. {
  430. unsigned long flags;
  431. struct kmemleak_object *object = NULL;
  432. rcu_read_lock();
  433. read_lock_irqsave(&kmemleak_lock, flags);
  434. if (ptr >= min_addr && ptr < max_addr)
  435. object = lookup_object(ptr, alias);
  436. read_unlock_irqrestore(&kmemleak_lock, flags);
  437. /* check whether the object is still available */
  438. if (object && !get_object(object))
  439. object = NULL;
  440. rcu_read_unlock();
  441. return object;
  442. }
  443. /*
  444. * Save stack trace to the given array of MAX_TRACE size.
  445. */
  446. static int __save_stack_trace(unsigned long *trace)
  447. {
  448. struct stack_trace stack_trace;
  449. stack_trace.max_entries = MAX_TRACE;
  450. stack_trace.nr_entries = 0;
  451. stack_trace.entries = trace;
  452. stack_trace.skip = 2;
  453. save_stack_trace(&stack_trace);
  454. return stack_trace.nr_entries;
  455. }
  456. /*
  457. * Create the metadata (struct kmemleak_object) corresponding to an allocated
  458. * memory block and add it to the object_list and object_tree_root.
  459. */
  460. static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
  461. int min_count, gfp_t gfp)
  462. {
  463. unsigned long flags;
  464. struct kmemleak_object *object;
  465. struct prio_tree_node *node;
  466. object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
  467. if (!object) {
  468. pr_warning("Cannot allocate a kmemleak_object structure\n");
  469. kmemleak_disable();
  470. return NULL;
  471. }
  472. INIT_LIST_HEAD(&object->object_list);
  473. INIT_LIST_HEAD(&object->gray_list);
  474. INIT_HLIST_HEAD(&object->area_list);
  475. spin_lock_init(&object->lock);
  476. atomic_set(&object->use_count, 1);
  477. object->flags = OBJECT_ALLOCATED;
  478. object->pointer = ptr;
  479. object->size = size;
  480. object->min_count = min_count;
  481. object->count = 0; /* white color initially */
  482. object->jiffies = jiffies;
  483. object->checksum = 0;
  484. /* task information */
  485. if (in_irq()) {
  486. object->pid = 0;
  487. strncpy(object->comm, "hardirq", sizeof(object->comm));
  488. } else if (in_softirq()) {
  489. object->pid = 0;
  490. strncpy(object->comm, "softirq", sizeof(object->comm));
  491. } else {
  492. object->pid = current->pid;
  493. /*
  494. * There is a small chance of a race with set_task_comm(),
  495. * however using get_task_comm() here may cause locking
  496. * dependency issues with current->alloc_lock. In the worst
  497. * case, the command line is not correct.
  498. */
  499. strncpy(object->comm, current->comm, sizeof(object->comm));
  500. }
  501. /* kernel backtrace */
  502. object->trace_len = __save_stack_trace(object->trace);
  503. INIT_PRIO_TREE_NODE(&object->tree_node);
  504. object->tree_node.start = ptr;
  505. object->tree_node.last = ptr + size - 1;
  506. write_lock_irqsave(&kmemleak_lock, flags);
  507. min_addr = min(min_addr, ptr);
  508. max_addr = max(max_addr, ptr + size);
  509. node = prio_tree_insert(&object_tree_root, &object->tree_node);
  510. /*
  511. * The code calling the kernel does not yet have the pointer to the
  512. * memory block to be able to free it. However, we still hold the
  513. * kmemleak_lock here in case parts of the kernel started freeing
  514. * random memory blocks.
  515. */
  516. if (node != &object->tree_node) {
  517. kmemleak_stop("Cannot insert 0x%lx into the object search tree "
  518. "(already existing)\n", ptr);
  519. object = lookup_object(ptr, 1);
  520. spin_lock(&object->lock);
  521. dump_object_info(object);
  522. spin_unlock(&object->lock);
  523. goto out;
  524. }
  525. list_add_tail_rcu(&object->object_list, &object_list);
  526. out:
  527. write_unlock_irqrestore(&kmemleak_lock, flags);
  528. return object;
  529. }
  530. /*
  531. * Remove the metadata (struct kmemleak_object) for a memory block from the
  532. * object_list and object_tree_root and decrement its use_count.
  533. */
  534. static void __delete_object(struct kmemleak_object *object)
  535. {
  536. unsigned long flags;
  537. write_lock_irqsave(&kmemleak_lock, flags);
  538. prio_tree_remove(&object_tree_root, &object->tree_node);
  539. list_del_rcu(&object->object_list);
  540. write_unlock_irqrestore(&kmemleak_lock, flags);
  541. WARN_ON(!(object->flags & OBJECT_ALLOCATED));
  542. WARN_ON(atomic_read(&object->use_count) < 2);
  543. /*
  544. * Locking here also ensures that the corresponding memory block
  545. * cannot be freed when it is being scanned.
  546. */
  547. spin_lock_irqsave(&object->lock, flags);
  548. object->flags &= ~OBJECT_ALLOCATED;
  549. spin_unlock_irqrestore(&object->lock, flags);
  550. put_object(object);
  551. }
  552. /*
  553. * Look up the metadata (struct kmemleak_object) corresponding to ptr and
  554. * delete it.
  555. */
  556. static void delete_object_full(unsigned long ptr)
  557. {
  558. struct kmemleak_object *object;
  559. object = find_and_get_object(ptr, 0);
  560. if (!object) {
  561. #ifdef DEBUG
  562. kmemleak_warn("Freeing unknown object at 0x%08lx\n",
  563. ptr);
  564. #endif
  565. return;
  566. }
  567. __delete_object(object);
  568. put_object(object);
  569. }
  570. /*
  571. * Look up the metadata (struct kmemleak_object) corresponding to ptr and
  572. * delete it. If the memory block is partially freed, the function may create
  573. * additional metadata for the remaining parts of the block.
  574. */
  575. static void delete_object_part(unsigned long ptr, size_t size)
  576. {
  577. struct kmemleak_object *object;
  578. unsigned long start, end;
  579. object = find_and_get_object(ptr, 1);
  580. if (!object) {
  581. #ifdef DEBUG
  582. kmemleak_warn("Partially freeing unknown object at 0x%08lx "
  583. "(size %zu)\n", ptr, size);
  584. #endif
  585. return;
  586. }
  587. __delete_object(object);
  588. /*
  589. * Create one or two objects that may result from the memory block
  590. * split. Note that partial freeing is only done by free_bootmem() and
  591. * this happens before kmemleak_init() is called. The path below is
  592. * only executed during early log recording in kmemleak_init(), so
  593. * GFP_KERNEL is enough.
  594. */
  595. start = object->pointer;
  596. end = object->pointer + object->size;
  597. if (ptr > start)
  598. create_object(start, ptr - start, object->min_count,
  599. GFP_KERNEL);
  600. if (ptr + size < end)
  601. create_object(ptr + size, end - ptr - size, object->min_count,
  602. GFP_KERNEL);
  603. put_object(object);
  604. }
  605. static void __paint_it(struct kmemleak_object *object, int color)
  606. {
  607. object->min_count = color;
  608. if (color == KMEMLEAK_BLACK)
  609. object->flags |= OBJECT_NO_SCAN;
  610. }
  611. static void paint_it(struct kmemleak_object *object, int color)
  612. {
  613. unsigned long flags;
  614. spin_lock_irqsave(&object->lock, flags);
  615. __paint_it(object, color);
  616. spin_unlock_irqrestore(&object->lock, flags);
  617. }
  618. static void paint_ptr(unsigned long ptr, int color)
  619. {
  620. struct kmemleak_object *object;
  621. object = find_and_get_object(ptr, 0);
  622. if (!object) {
  623. kmemleak_warn("Trying to color unknown object "
  624. "at 0x%08lx as %s\n", ptr,
  625. (color == KMEMLEAK_GREY) ? "Grey" :
  626. (color == KMEMLEAK_BLACK) ? "Black" : "Unknown");
  627. return;
  628. }
  629. paint_it(object, color);
  630. put_object(object);
  631. }
  632. /*
  633. * Mark an object permanently as gray-colored so that it can no longer be
  634. * reported as a leak. This is used in general to mark a false positive.
  635. */
  636. static void make_gray_object(unsigned long ptr)
  637. {
  638. paint_ptr(ptr, KMEMLEAK_GREY);
  639. }
  640. /*
  641. * Mark the object as black-colored so that it is ignored from scans and
  642. * reporting.
  643. */
  644. static void make_black_object(unsigned long ptr)
  645. {
  646. paint_ptr(ptr, KMEMLEAK_BLACK);
  647. }
  648. /*
  649. * Add a scanning area to the object. If at least one such area is added,
  650. * kmemleak will only scan these ranges rather than the whole memory block.
  651. */
  652. static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
  653. {
  654. unsigned long flags;
  655. struct kmemleak_object *object;
  656. struct kmemleak_scan_area *area;
  657. object = find_and_get_object(ptr, 1);
  658. if (!object) {
  659. kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
  660. ptr);
  661. return;
  662. }
  663. area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
  664. if (!area) {
  665. pr_warning("Cannot allocate a scan area\n");
  666. goto out;
  667. }
  668. spin_lock_irqsave(&object->lock, flags);
  669. if (ptr + size > object->pointer + object->size) {
  670. kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
  671. dump_object_info(object);
  672. kmem_cache_free(scan_area_cache, area);
  673. goto out_unlock;
  674. }
  675. INIT_HLIST_NODE(&area->node);
  676. area->start = ptr;
  677. area->size = size;
  678. hlist_add_head(&area->node, &object->area_list);
  679. out_unlock:
  680. spin_unlock_irqrestore(&object->lock, flags);
  681. out:
  682. put_object(object);
  683. }
  684. /*
  685. * Set the OBJECT_NO_SCAN flag for the object corresponding to the give
  686. * pointer. Such object will not be scanned by kmemleak but references to it
  687. * are searched.
  688. */
  689. static void object_no_scan(unsigned long ptr)
  690. {
  691. unsigned long flags;
  692. struct kmemleak_object *object;
  693. object = find_and_get_object(ptr, 0);
  694. if (!object) {
  695. kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
  696. return;
  697. }
  698. spin_lock_irqsave(&object->lock, flags);
  699. object->flags |= OBJECT_NO_SCAN;
  700. spin_unlock_irqrestore(&object->lock, flags);
  701. put_object(object);
  702. }
  703. /*
  704. * Log an early kmemleak_* call to the early_log buffer. These calls will be
  705. * processed later once kmemleak is fully initialized.
  706. */
  707. static void __init log_early(int op_type, const void *ptr, size_t size,
  708. int min_count)
  709. {
  710. unsigned long flags;
  711. struct early_log *log;
  712. if (atomic_read(&kmemleak_error)) {
  713. /* kmemleak stopped recording, just count the requests */
  714. crt_early_log++;
  715. return;
  716. }
  717. if (crt_early_log >= ARRAY_SIZE(early_log)) {
  718. kmemleak_disable();
  719. return;
  720. }
  721. /*
  722. * There is no need for locking since the kernel is still in UP mode
  723. * at this stage. Disabling the IRQs is enough.
  724. */
  725. local_irq_save(flags);
  726. log = &early_log[crt_early_log];
  727. log->op_type = op_type;
  728. log->ptr = ptr;
  729. log->size = size;
  730. log->min_count = min_count;
  731. log->trace_len = __save_stack_trace(log->trace);
  732. crt_early_log++;
  733. local_irq_restore(flags);
  734. }
  735. /*
  736. * Log an early allocated block and populate the stack trace.
  737. */
  738. static void early_alloc(struct early_log *log)
  739. {
  740. struct kmemleak_object *object;
  741. unsigned long flags;
  742. int i;
  743. if (!atomic_read(&kmemleak_enabled) || !log->ptr || IS_ERR(log->ptr))
  744. return;
  745. /*
  746. * RCU locking needed to ensure object is not freed via put_object().
  747. */
  748. rcu_read_lock();
  749. object = create_object((unsigned long)log->ptr, log->size,
  750. log->min_count, GFP_ATOMIC);
  751. if (!object)
  752. goto out;
  753. spin_lock_irqsave(&object->lock, flags);
  754. for (i = 0; i < log->trace_len; i++)
  755. object->trace[i] = log->trace[i];
  756. object->trace_len = log->trace_len;
  757. spin_unlock_irqrestore(&object->lock, flags);
  758. out:
  759. rcu_read_unlock();
  760. }
  761. /*
  762. * Log an early allocated block and populate the stack trace.
  763. */
  764. static void early_alloc_percpu(struct early_log *log)
  765. {
  766. unsigned int cpu;
  767. const void __percpu *ptr = log->ptr;
  768. for_each_possible_cpu(cpu) {
  769. log->ptr = per_cpu_ptr(ptr, cpu);
  770. early_alloc(log);
  771. }
  772. }
  773. /**
  774. * kmemleak_alloc - register a newly allocated object
  775. * @ptr: pointer to beginning of the object
  776. * @size: size of the object
  777. * @min_count: minimum number of references to this object. If during memory
  778. * scanning a number of references less than @min_count is found,
  779. * the object is reported as a memory leak. If @min_count is 0,
  780. * the object is never reported as a leak. If @min_count is -1,
  781. * the object is ignored (not scanned and not reported as a leak)
  782. * @gfp: kmalloc() flags used for kmemleak internal memory allocations
  783. *
  784. * This function is called from the kernel allocators when a new object
  785. * (memory block) is allocated (kmem_cache_alloc, kmalloc, vmalloc etc.).
  786. */
  787. void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
  788. gfp_t gfp)
  789. {
  790. pr_debug("%s(0x%p, %zu, %d)\n", __func__, ptr, size, min_count);
  791. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  792. create_object((unsigned long)ptr, size, min_count, gfp);
  793. else if (atomic_read(&kmemleak_early_log))
  794. log_early(KMEMLEAK_ALLOC, ptr, size, min_count);
  795. }
  796. EXPORT_SYMBOL_GPL(kmemleak_alloc);
  797. /**
  798. * kmemleak_alloc_percpu - register a newly allocated __percpu object
  799. * @ptr: __percpu pointer to beginning of the object
  800. * @size: size of the object
  801. *
  802. * This function is called from the kernel percpu allocator when a new object
  803. * (memory block) is allocated (alloc_percpu). It assumes GFP_KERNEL
  804. * allocation.
  805. */
  806. void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size)
  807. {
  808. unsigned int cpu;
  809. pr_debug("%s(0x%p, %zu)\n", __func__, ptr, size);
  810. /*
  811. * Percpu allocations are only scanned and not reported as leaks
  812. * (min_count is set to 0).
  813. */
  814. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  815. for_each_possible_cpu(cpu)
  816. create_object((unsigned long)per_cpu_ptr(ptr, cpu),
  817. size, 0, GFP_KERNEL);
  818. else if (atomic_read(&kmemleak_early_log))
  819. log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0);
  820. }
  821. EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
  822. /**
  823. * kmemleak_free - unregister a previously registered object
  824. * @ptr: pointer to beginning of the object
  825. *
  826. * This function is called from the kernel allocators when an object (memory
  827. * block) is freed (kmem_cache_free, kfree, vfree etc.).
  828. */
  829. void __ref kmemleak_free(const void *ptr)
  830. {
  831. pr_debug("%s(0x%p)\n", __func__, ptr);
  832. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  833. delete_object_full((unsigned long)ptr);
  834. else if (atomic_read(&kmemleak_early_log))
  835. log_early(KMEMLEAK_FREE, ptr, 0, 0);
  836. }
  837. EXPORT_SYMBOL_GPL(kmemleak_free);
  838. /**
  839. * kmemleak_free_part - partially unregister a previously registered object
  840. * @ptr: pointer to the beginning or inside the object. This also
  841. * represents the start of the range to be freed
  842. * @size: size to be unregistered
  843. *
  844. * This function is called when only a part of a memory block is freed
  845. * (usually from the bootmem allocator).
  846. */
  847. void __ref kmemleak_free_part(const void *ptr, size_t size)
  848. {
  849. pr_debug("%s(0x%p)\n", __func__, ptr);
  850. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  851. delete_object_part((unsigned long)ptr, size);
  852. else if (atomic_read(&kmemleak_early_log))
  853. log_early(KMEMLEAK_FREE_PART, ptr, size, 0);
  854. }
  855. EXPORT_SYMBOL_GPL(kmemleak_free_part);
  856. /**
  857. * kmemleak_free_percpu - unregister a previously registered __percpu object
  858. * @ptr: __percpu pointer to beginning of the object
  859. *
  860. * This function is called from the kernel percpu allocator when an object
  861. * (memory block) is freed (free_percpu).
  862. */
  863. void __ref kmemleak_free_percpu(const void __percpu *ptr)
  864. {
  865. unsigned int cpu;
  866. pr_debug("%s(0x%p)\n", __func__, ptr);
  867. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  868. for_each_possible_cpu(cpu)
  869. delete_object_full((unsigned long)per_cpu_ptr(ptr,
  870. cpu));
  871. else if (atomic_read(&kmemleak_early_log))
  872. log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0);
  873. }
  874. EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
  875. /**
  876. * kmemleak_not_leak - mark an allocated object as false positive
  877. * @ptr: pointer to beginning of the object
  878. *
  879. * Calling this function on an object will cause the memory block to no longer
  880. * be reported as leak and always be scanned.
  881. */
  882. void __ref kmemleak_not_leak(const void *ptr)
  883. {
  884. pr_debug("%s(0x%p)\n", __func__, ptr);
  885. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  886. make_gray_object((unsigned long)ptr);
  887. else if (atomic_read(&kmemleak_early_log))
  888. log_early(KMEMLEAK_NOT_LEAK, ptr, 0, 0);
  889. }
  890. EXPORT_SYMBOL(kmemleak_not_leak);
  891. /**
  892. * kmemleak_ignore - ignore an allocated object
  893. * @ptr: pointer to beginning of the object
  894. *
  895. * Calling this function on an object will cause the memory block to be
  896. * ignored (not scanned and not reported as a leak). This is usually done when
  897. * it is known that the corresponding block is not a leak and does not contain
  898. * any references to other allocated memory blocks.
  899. */
  900. void __ref kmemleak_ignore(const void *ptr)
  901. {
  902. pr_debug("%s(0x%p)\n", __func__, ptr);
  903. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  904. make_black_object((unsigned long)ptr);
  905. else if (atomic_read(&kmemleak_early_log))
  906. log_early(KMEMLEAK_IGNORE, ptr, 0, 0);
  907. }
  908. EXPORT_SYMBOL(kmemleak_ignore);
  909. /**
  910. * kmemleak_scan_area - limit the range to be scanned in an allocated object
  911. * @ptr: pointer to beginning or inside the object. This also
  912. * represents the start of the scan area
  913. * @size: size of the scan area
  914. * @gfp: kmalloc() flags used for kmemleak internal memory allocations
  915. *
  916. * This function is used when it is known that only certain parts of an object
  917. * contain references to other objects. Kmemleak will only scan these areas
  918. * reducing the number false negatives.
  919. */
  920. void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
  921. {
  922. pr_debug("%s(0x%p)\n", __func__, ptr);
  923. if (atomic_read(&kmemleak_enabled) && ptr && size && !IS_ERR(ptr))
  924. add_scan_area((unsigned long)ptr, size, gfp);
  925. else if (atomic_read(&kmemleak_early_log))
  926. log_early(KMEMLEAK_SCAN_AREA, ptr, size, 0);
  927. }
  928. EXPORT_SYMBOL(kmemleak_scan_area);
  929. /**
  930. * kmemleak_no_scan - do not scan an allocated object
  931. * @ptr: pointer to beginning of the object
  932. *
  933. * This function notifies kmemleak not to scan the given memory block. Useful
  934. * in situations where it is known that the given object does not contain any
  935. * references to other objects. Kmemleak will not scan such objects reducing
  936. * the number of false negatives.
  937. */
  938. void __ref kmemleak_no_scan(const void *ptr)
  939. {
  940. pr_debug("%s(0x%p)\n", __func__, ptr);
  941. if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
  942. object_no_scan((unsigned long)ptr);
  943. else if (atomic_read(&kmemleak_early_log))
  944. log_early(KMEMLEAK_NO_SCAN, ptr, 0, 0);
  945. }
  946. EXPORT_SYMBOL(kmemleak_no_scan);
  947. /*
  948. * Update an object's checksum and return true if it was modified.
  949. */
  950. static bool update_checksum(struct kmemleak_object *object)
  951. {
  952. u32 old_csum = object->checksum;
  953. if (!kmemcheck_is_obj_initialized(object->pointer, object->size))
  954. return false;
  955. object->checksum = crc32(0, (void *)object->pointer, object->size);
  956. return object->checksum != old_csum;
  957. }
  958. /*
  959. * Memory scanning is a long process and it needs to be interruptable. This
  960. * function checks whether such interrupt condition occurred.
  961. */
  962. static int scan_should_stop(void)
  963. {
  964. if (!atomic_read(&kmemleak_enabled))
  965. return 1;
  966. /*
  967. * This function may be called from either process or kthread context,
  968. * hence the need to check for both stop conditions.
  969. */
  970. if (current->mm)
  971. return signal_pending(current);
  972. else
  973. return kthread_should_stop();
  974. return 0;
  975. }
  976. /*
  977. * Scan a memory block (exclusive range) for valid pointers and add those
  978. * found to the gray list.
  979. */
  980. static void scan_block(void *_start, void *_end,
  981. struct kmemleak_object *scanned, int allow_resched)
  982. {
  983. unsigned long *ptr;
  984. unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER);
  985. unsigned long *end = _end - (BYTES_PER_POINTER - 1);
  986. for (ptr = start; ptr < end; ptr++) {
  987. struct kmemleak_object *object;
  988. unsigned long flags;
  989. unsigned long pointer;
  990. if (allow_resched)
  991. cond_resched();
  992. if (scan_should_stop())
  993. break;
  994. /* don't scan uninitialized memory */
  995. if (!kmemcheck_is_obj_initialized((unsigned long)ptr,
  996. BYTES_PER_POINTER))
  997. continue;
  998. pointer = *ptr;
  999. object = find_and_get_object(pointer, 1);
  1000. if (!object)
  1001. continue;
  1002. if (object == scanned) {
  1003. /* self referenced, ignore */
  1004. put_object(object);
  1005. continue;
  1006. }
  1007. /*
  1008. * Avoid the lockdep recursive warning on object->lock being
  1009. * previously acquired in scan_object(). These locks are
  1010. * enclosed by scan_mutex.
  1011. */
  1012. spin_lock_irqsave_nested(&object->lock, flags,
  1013. SINGLE_DEPTH_NESTING);
  1014. if (!color_white(object)) {
  1015. /* non-orphan, ignored or new */
  1016. spin_unlock_irqrestore(&object->lock, flags);
  1017. put_object(object);
  1018. continue;
  1019. }
  1020. /*
  1021. * Increase the object's reference count (number of pointers
  1022. * to the memory block). If this count reaches the required
  1023. * minimum, the object's color will become gray and it will be
  1024. * added to the gray_list.
  1025. */
  1026. object->count++;
  1027. if (color_gray(object)) {
  1028. list_add_tail(&object->gray_list, &gray_list);
  1029. spin_unlock_irqrestore(&object->lock, flags);
  1030. continue;
  1031. }
  1032. spin_unlock_irqrestore(&object->lock, flags);
  1033. put_object(object);
  1034. }
  1035. }
  1036. /*
  1037. * Scan a memory block corresponding to a kmemleak_object. A condition is
  1038. * that object->use_count >= 1.
  1039. */
  1040. static void scan_object(struct kmemleak_object *object)
  1041. {
  1042. struct kmemleak_scan_area *area;
  1043. struct hlist_node *elem;
  1044. unsigned long flags;
  1045. /*
  1046. * Once the object->lock is acquired, the corresponding memory block
  1047. * cannot be freed (the same lock is acquired in delete_object).
  1048. */
  1049. spin_lock_irqsave(&object->lock, flags);
  1050. if (object->flags & OBJECT_NO_SCAN)
  1051. goto out;
  1052. if (!(object->flags & OBJECT_ALLOCATED))
  1053. /* already freed object */
  1054. goto out;
  1055. if (hlist_empty(&object->area_list)) {
  1056. void *start = (void *)object->pointer;
  1057. void *end = (void *)(object->pointer + object->size);
  1058. while (start < end && (object->flags & OBJECT_ALLOCATED) &&
  1059. !(object->flags & OBJECT_NO_SCAN)) {
  1060. scan_block(start, min(start + MAX_SCAN_SIZE, end),
  1061. object, 0);
  1062. start += MAX_SCAN_SIZE;
  1063. spin_unlock_irqrestore(&object->lock, flags);
  1064. cond_resched();
  1065. spin_lock_irqsave(&object->lock, flags);
  1066. }
  1067. } else
  1068. hlist_for_each_entry(area, elem, &object->area_list, node)
  1069. scan_block((void *)area->start,
  1070. (void *)(area->start + area->size),
  1071. object, 0);
  1072. out:
  1073. spin_unlock_irqrestore(&object->lock, flags);
  1074. }
  1075. /*
  1076. * Scan the objects already referenced (gray objects). More objects will be
  1077. * referenced and, if there are no memory leaks, all the objects are scanned.
  1078. */
  1079. static void scan_gray_list(void)
  1080. {
  1081. struct kmemleak_object *object, *tmp;
  1082. /*
  1083. * The list traversal is safe for both tail additions and removals
  1084. * from inside the loop. The kmemleak objects cannot be freed from
  1085. * outside the loop because their use_count was incremented.
  1086. */
  1087. object = list_entry(gray_list.next, typeof(*object), gray_list);
  1088. while (&object->gray_list != &gray_list) {
  1089. cond_resched();
  1090. /* may add new objects to the list */
  1091. if (!scan_should_stop())
  1092. scan_object(object);
  1093. tmp = list_entry(object->gray_list.next, typeof(*object),
  1094. gray_list);
  1095. /* remove the object from the list and release it */
  1096. list_del(&object->gray_list);
  1097. put_object(object);
  1098. object = tmp;
  1099. }
  1100. WARN_ON(!list_empty(&gray_list));
  1101. }
  1102. /*
  1103. * Scan data sections and all the referenced memory blocks allocated via the
  1104. * kernel's standard allocators. This function must be called with the
  1105. * scan_mutex held.
  1106. */
  1107. static void kmemleak_scan(void)
  1108. {
  1109. unsigned long flags;
  1110. struct kmemleak_object *object;
  1111. int i;
  1112. int new_leaks = 0;
  1113. jiffies_last_scan = jiffies;
  1114. /* prepare the kmemleak_object's */
  1115. rcu_read_lock();
  1116. list_for_each_entry_rcu(object, &object_list, object_list) {
  1117. spin_lock_irqsave(&object->lock, flags);
  1118. #ifdef DEBUG
  1119. /*
  1120. * With a few exceptions there should be a maximum of
  1121. * 1 reference to any object at this point.
  1122. */
  1123. if (atomic_read(&object->use_count) > 1) {
  1124. pr_debug("object->use_count = %d\n",
  1125. atomic_read(&object->use_count));
  1126. dump_object_info(object);
  1127. }
  1128. #endif
  1129. /* reset the reference count (whiten the object) */
  1130. object->count = 0;
  1131. if (color_gray(object) && get_object(object))
  1132. list_add_tail(&object->gray_list, &gray_list);
  1133. spin_unlock_irqrestore(&object->lock, flags);
  1134. }
  1135. rcu_read_unlock();
  1136. /* data/bss scanning */
  1137. scan_block(_sdata, _edata, NULL, 1);
  1138. scan_block(__bss_start, __bss_stop, NULL, 1);
  1139. #ifdef CONFIG_SMP
  1140. /* per-cpu sections scanning */
  1141. for_each_possible_cpu(i)
  1142. scan_block(__per_cpu_start + per_cpu_offset(i),
  1143. __per_cpu_end + per_cpu_offset(i), NULL, 1);
  1144. #endif
  1145. /*
  1146. * Struct page scanning for each node.
  1147. */
  1148. lock_memory_hotplug();
  1149. for_each_online_node(i) {
  1150. pg_data_t *pgdat = NODE_DATA(i);
  1151. unsigned long start_pfn = pgdat->node_start_pfn;
  1152. unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
  1153. unsigned long pfn;
  1154. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1155. struct page *page;
  1156. if (!pfn_valid(pfn))
  1157. continue;
  1158. page = pfn_to_page(pfn);
  1159. /* only scan if page is in use */
  1160. if (page_count(page) == 0)
  1161. continue;
  1162. scan_block(page, page + 1, NULL, 1);
  1163. }
  1164. }
  1165. unlock_memory_hotplug();
  1166. /*
  1167. * Scanning the task stacks (may introduce false negatives).
  1168. */
  1169. if (kmemleak_stack_scan) {
  1170. struct task_struct *p, *g;
  1171. read_lock(&tasklist_lock);
  1172. do_each_thread(g, p) {
  1173. scan_block(task_stack_page(p), task_stack_page(p) +
  1174. THREAD_SIZE, NULL, 0);
  1175. } while_each_thread(g, p);
  1176. read_unlock(&tasklist_lock);
  1177. }
  1178. /*
  1179. * Scan the objects already referenced from the sections scanned
  1180. * above.
  1181. */
  1182. scan_gray_list();
  1183. /*
  1184. * Check for new or unreferenced objects modified since the previous
  1185. * scan and color them gray until the next scan.
  1186. */
  1187. rcu_read_lock();
  1188. list_for_each_entry_rcu(object, &object_list, object_list) {
  1189. spin_lock_irqsave(&object->lock, flags);
  1190. if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
  1191. && update_checksum(object) && get_object(object)) {
  1192. /* color it gray temporarily */
  1193. object->count = object->min_count;
  1194. list_add_tail(&object->gray_list, &gray_list);
  1195. }
  1196. spin_unlock_irqrestore(&object->lock, flags);
  1197. }
  1198. rcu_read_unlock();
  1199. /*
  1200. * Re-scan the gray list for modified unreferenced objects.
  1201. */
  1202. scan_gray_list();
  1203. /*
  1204. * If scanning was stopped do not report any new unreferenced objects.
  1205. */
  1206. if (scan_should_stop())
  1207. return;
  1208. /*
  1209. * Scanning result reporting.
  1210. */
  1211. rcu_read_lock();
  1212. list_for_each_entry_rcu(object, &object_list, object_list) {
  1213. spin_lock_irqsave(&object->lock, flags);
  1214. if (unreferenced_object(object) &&
  1215. !(object->flags & OBJECT_REPORTED)) {
  1216. object->flags |= OBJECT_REPORTED;
  1217. new_leaks++;
  1218. }
  1219. spin_unlock_irqrestore(&object->lock, flags);
  1220. }
  1221. rcu_read_unlock();
  1222. if (new_leaks)
  1223. pr_info("%d new suspected memory leaks (see "
  1224. "/sys/kernel/debug/kmemleak)\n", new_leaks);
  1225. }
  1226. /*
  1227. * Thread function performing automatic memory scanning. Unreferenced objects
  1228. * at the end of a memory scan are reported but only the first time.
  1229. */
  1230. static int kmemleak_scan_thread(void *arg)
  1231. {
  1232. static int first_run = 1;
  1233. pr_info("Automatic memory scanning thread started\n");
  1234. set_user_nice(current, 10);
  1235. /*
  1236. * Wait before the first scan to allow the system to fully initialize.
  1237. */
  1238. if (first_run) {
  1239. first_run = 0;
  1240. ssleep(SECS_FIRST_SCAN);
  1241. }
  1242. while (!kthread_should_stop()) {
  1243. signed long timeout = jiffies_scan_wait;
  1244. mutex_lock(&scan_mutex);
  1245. kmemleak_scan();
  1246. mutex_unlock(&scan_mutex);
  1247. /* wait before the next scan */
  1248. while (timeout && !kthread_should_stop())
  1249. timeout = schedule_timeout_interruptible(timeout);
  1250. }
  1251. pr_info("Automatic memory scanning thread ended\n");
  1252. return 0;
  1253. }
  1254. /*
  1255. * Start the automatic memory scanning thread. This function must be called
  1256. * with the scan_mutex held.
  1257. */
  1258. static void start_scan_thread(void)
  1259. {
  1260. if (scan_thread)
  1261. return;
  1262. scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
  1263. if (IS_ERR(scan_thread)) {
  1264. pr_warning("Failed to create the scan thread\n");
  1265. scan_thread = NULL;
  1266. }
  1267. }
  1268. /*
  1269. * Stop the automatic memory scanning thread. This function must be called
  1270. * with the scan_mutex held.
  1271. */
  1272. static void stop_scan_thread(void)
  1273. {
  1274. if (scan_thread) {
  1275. kthread_stop(scan_thread);
  1276. scan_thread = NULL;
  1277. }
  1278. }
  1279. /*
  1280. * Iterate over the object_list and return the first valid object at or after
  1281. * the required position with its use_count incremented. The function triggers
  1282. * a memory scanning when the pos argument points to the first position.
  1283. */
  1284. static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
  1285. {
  1286. struct kmemleak_object *object;
  1287. loff_t n = *pos;
  1288. int err;
  1289. err = mutex_lock_interruptible(&scan_mutex);
  1290. if (err < 0)
  1291. return ERR_PTR(err);
  1292. rcu_read_lock();
  1293. list_for_each_entry_rcu(object, &object_list, object_list) {
  1294. if (n-- > 0)
  1295. continue;
  1296. if (get_object(object))
  1297. goto out;
  1298. }
  1299. object = NULL;
  1300. out:
  1301. return object;
  1302. }
  1303. /*
  1304. * Return the next object in the object_list. The function decrements the
  1305. * use_count of the previous object and increases that of the next one.
  1306. */
  1307. static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1308. {
  1309. struct kmemleak_object *prev_obj = v;
  1310. struct kmemleak_object *next_obj = NULL;
  1311. struct list_head *n = &prev_obj->object_list;
  1312. ++(*pos);
  1313. list_for_each_continue_rcu(n, &object_list) {
  1314. struct kmemleak_object *obj =
  1315. list_entry(n, struct kmemleak_object, object_list);
  1316. if (get_object(obj)) {
  1317. next_obj = obj;
  1318. break;
  1319. }
  1320. }
  1321. put_object(prev_obj);
  1322. return next_obj;
  1323. }
  1324. /*
  1325. * Decrement the use_count of the last object required, if any.
  1326. */
  1327. static void kmemleak_seq_stop(struct seq_file *seq, void *v)
  1328. {
  1329. if (!IS_ERR(v)) {
  1330. /*
  1331. * kmemleak_seq_start may return ERR_PTR if the scan_mutex
  1332. * waiting was interrupted, so only release it if !IS_ERR.
  1333. */
  1334. rcu_read_unlock();
  1335. mutex_unlock(&scan_mutex);
  1336. if (v)
  1337. put_object(v);
  1338. }
  1339. }
  1340. /*
  1341. * Print the information for an unreferenced object to the seq file.
  1342. */
  1343. static int kmemleak_seq_show(struct seq_file *seq, void *v)
  1344. {
  1345. struct kmemleak_object *object = v;
  1346. unsigned long flags;
  1347. spin_lock_irqsave(&object->lock, flags);
  1348. if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
  1349. print_unreferenced(seq, object);
  1350. spin_unlock_irqrestore(&object->lock, flags);
  1351. return 0;
  1352. }
  1353. static const struct seq_operations kmemleak_seq_ops = {
  1354. .start = kmemleak_seq_start,
  1355. .next = kmemleak_seq_next,
  1356. .stop = kmemleak_seq_stop,
  1357. .show = kmemleak_seq_show,
  1358. };
  1359. static int kmemleak_open(struct inode *inode, struct file *file)
  1360. {
  1361. return seq_open(file, &kmemleak_seq_ops);
  1362. }
  1363. static int kmemleak_release(struct inode *inode, struct file *file)
  1364. {
  1365. return seq_release(inode, file);
  1366. }
  1367. static int dump_str_object_info(const char *str)
  1368. {
  1369. unsigned long flags;
  1370. struct kmemleak_object *object;
  1371. unsigned long addr;
  1372. addr= simple_strtoul(str, NULL, 0);
  1373. object = find_and_get_object(addr, 0);
  1374. if (!object) {
  1375. pr_info("Unknown object at 0x%08lx\n", addr);
  1376. return -EINVAL;
  1377. }
  1378. spin_lock_irqsave(&object->lock, flags);
  1379. dump_object_info(object);
  1380. spin_unlock_irqrestore(&object->lock, flags);
  1381. put_object(object);
  1382. return 0;
  1383. }
  1384. /*
  1385. * We use grey instead of black to ensure we can do future scans on the same
  1386. * objects. If we did not do future scans these black objects could
  1387. * potentially contain references to newly allocated objects in the future and
  1388. * we'd end up with false positives.
  1389. */
  1390. static void kmemleak_clear(void)
  1391. {
  1392. struct kmemleak_object *object;
  1393. unsigned long flags;
  1394. rcu_read_lock();
  1395. list_for_each_entry_rcu(object, &object_list, object_list) {
  1396. spin_lock_irqsave(&object->lock, flags);
  1397. if ((object->flags & OBJECT_REPORTED) &&
  1398. unreferenced_object(object))
  1399. __paint_it(object, KMEMLEAK_GREY);
  1400. spin_unlock_irqrestore(&object->lock, flags);
  1401. }
  1402. rcu_read_unlock();
  1403. }
  1404. /*
  1405. * File write operation to configure kmemleak at run-time. The following
  1406. * commands can be written to the /sys/kernel/debug/kmemleak file:
  1407. * off - disable kmemleak (irreversible)
  1408. * stack=on - enable the task stacks scanning
  1409. * stack=off - disable the tasks stacks scanning
  1410. * scan=on - start the automatic memory scanning thread
  1411. * scan=off - stop the automatic memory scanning thread
  1412. * scan=... - set the automatic memory scanning period in seconds (0 to
  1413. * disable it)
  1414. * scan - trigger a memory scan
  1415. * clear - mark all current reported unreferenced kmemleak objects as
  1416. * grey to ignore printing them
  1417. * dump=... - dump information about the object found at the given address
  1418. */
  1419. static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
  1420. size_t size, loff_t *ppos)
  1421. {
  1422. char buf[64];
  1423. int buf_size;
  1424. int ret;
  1425. if (!atomic_read(&kmemleak_enabled))
  1426. return -EBUSY;
  1427. buf_size = min(size, (sizeof(buf) - 1));
  1428. if (strncpy_from_user(buf, user_buf, buf_size) < 0)
  1429. return -EFAULT;
  1430. buf[buf_size] = 0;
  1431. ret = mutex_lock_interruptible(&scan_mutex);
  1432. if (ret < 0)
  1433. return ret;
  1434. if (strncmp(buf, "off", 3) == 0)
  1435. kmemleak_disable();
  1436. else if (strncmp(buf, "stack=on", 8) == 0)
  1437. kmemleak_stack_scan = 1;
  1438. else if (strncmp(buf, "stack=off", 9) == 0)
  1439. kmemleak_stack_scan = 0;
  1440. else if (strncmp(buf, "scan=on", 7) == 0)
  1441. start_scan_thread();
  1442. else if (strncmp(buf, "scan=off", 8) == 0)
  1443. stop_scan_thread();
  1444. else if (strncmp(buf, "scan=", 5) == 0) {
  1445. unsigned long secs;
  1446. ret = strict_strtoul(buf + 5, 0, &secs);
  1447. if (ret < 0)
  1448. goto out;
  1449. stop_scan_thread();
  1450. if (secs) {
  1451. jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
  1452. start_scan_thread();
  1453. }
  1454. } else if (strncmp(buf, "scan", 4) == 0)
  1455. kmemleak_scan();
  1456. else if (strncmp(buf, "clear", 5) == 0)
  1457. kmemleak_clear();
  1458. else if (strncmp(buf, "dump=", 5) == 0)
  1459. ret = dump_str_object_info(buf + 5);
  1460. else
  1461. ret = -EINVAL;
  1462. out:
  1463. mutex_unlock(&scan_mutex);
  1464. if (ret < 0)
  1465. return ret;
  1466. /* ignore the rest of the buffer, only one command at a time */
  1467. *ppos += size;
  1468. return size;
  1469. }
  1470. static const struct file_operations kmemleak_fops = {
  1471. .owner = THIS_MODULE,
  1472. .open = kmemleak_open,
  1473. .read = seq_read,
  1474. .write = kmemleak_write,
  1475. .llseek = seq_lseek,
  1476. .release = kmemleak_release,
  1477. };
  1478. /*
  1479. * Stop the memory scanning thread and free the kmemleak internal objects if
  1480. * no previous scan thread (otherwise, kmemleak may still have some useful
  1481. * information on memory leaks).
  1482. */
  1483. static void kmemleak_do_cleanup(struct work_struct *work)
  1484. {
  1485. struct kmemleak_object *object;
  1486. bool cleanup = scan_thread == NULL;
  1487. mutex_lock(&scan_mutex);
  1488. stop_scan_thread();
  1489. if (cleanup) {
  1490. rcu_read_lock();
  1491. list_for_each_entry_rcu(object, &object_list, object_list)
  1492. delete_object_full(object->pointer);
  1493. rcu_read_unlock();
  1494. }
  1495. mutex_unlock(&scan_mutex);
  1496. }
  1497. static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
  1498. /*
  1499. * Disable kmemleak. No memory allocation/freeing will be traced once this
  1500. * function is called. Disabling kmemleak is an irreversible operation.
  1501. */
  1502. static void kmemleak_disable(void)
  1503. {
  1504. /* atomically check whether it was already invoked */
  1505. if (atomic_cmpxchg(&kmemleak_error, 0, 1))
  1506. return;
  1507. /* stop any memory operation tracing */
  1508. atomic_set(&kmemleak_enabled, 0);
  1509. /* check whether it is too early for a kernel thread */
  1510. if (atomic_read(&kmemleak_initialized))
  1511. schedule_work(&cleanup_work);
  1512. pr_info("Kernel memory leak detector disabled\n");
  1513. }
  1514. /*
  1515. * Allow boot-time kmemleak disabling (enabled by default).
  1516. */
  1517. static int kmemleak_boot_config(char *str)
  1518. {
  1519. if (!str)
  1520. return -EINVAL;
  1521. if (strcmp(str, "off") == 0)
  1522. kmemleak_disable();
  1523. else if (strcmp(str, "on") == 0)
  1524. kmemleak_skip_disable = 1;
  1525. else
  1526. return -EINVAL;
  1527. return 0;
  1528. }
  1529. early_param("kmemleak", kmemleak_boot_config);
  1530. static void __init print_log_trace(struct early_log *log)
  1531. {
  1532. struct stack_trace trace;
  1533. trace.nr_entries = log->trace_len;
  1534. trace.entries = log->trace;
  1535. pr_notice("Early log backtrace:\n");
  1536. print_stack_trace(&trace, 2);
  1537. }
  1538. /*
  1539. * Kmemleak initialization.
  1540. */
  1541. void __init kmemleak_init(void)
  1542. {
  1543. int i;
  1544. unsigned long flags;
  1545. #ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF
  1546. if (!kmemleak_skip_disable) {
  1547. atomic_set(&kmemleak_early_log, 0);
  1548. kmemleak_disable();
  1549. return;
  1550. }
  1551. #endif
  1552. jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
  1553. jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
  1554. object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE);
  1555. scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);
  1556. INIT_PRIO_TREE_ROOT(&object_tree_root);
  1557. if (crt_early_log >= ARRAY_SIZE(early_log))
  1558. pr_warning("Early log buffer exceeded (%d), please increase "
  1559. "DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n", crt_early_log);
  1560. /* the kernel is still in UP mode, so disabling the IRQs is enough */
  1561. local_irq_save(flags);
  1562. atomic_set(&kmemleak_early_log, 0);
  1563. if (atomic_read(&kmemleak_error)) {
  1564. local_irq_restore(flags);
  1565. return;
  1566. } else
  1567. atomic_set(&kmemleak_enabled, 1);
  1568. local_irq_restore(flags);
  1569. /*
  1570. * This is the point where tracking allocations is safe. Automatic
  1571. * scanning is started during the late initcall. Add the early logged
  1572. * callbacks to the kmemleak infrastructure.
  1573. */
  1574. for (i = 0; i < crt_early_log; i++) {
  1575. struct early_log *log = &early_log[i];
  1576. switch (log->op_type) {
  1577. case KMEMLEAK_ALLOC:
  1578. early_alloc(log);
  1579. break;
  1580. case KMEMLEAK_ALLOC_PERCPU:
  1581. early_alloc_percpu(log);
  1582. break;
  1583. case KMEMLEAK_FREE:
  1584. kmemleak_free(log->ptr);
  1585. break;
  1586. case KMEMLEAK_FREE_PART:
  1587. kmemleak_free_part(log->ptr, log->size);
  1588. break;
  1589. case KMEMLEAK_FREE_PERCPU:
  1590. kmemleak_free_percpu(log->ptr);
  1591. break;
  1592. case KMEMLEAK_NOT_LEAK:
  1593. kmemleak_not_leak(log->ptr);
  1594. break;
  1595. case KMEMLEAK_IGNORE:
  1596. kmemleak_ignore(log->ptr);
  1597. break;
  1598. case KMEMLEAK_SCAN_AREA:
  1599. kmemleak_scan_area(log->ptr, log->size, GFP_KERNEL);
  1600. break;
  1601. case KMEMLEAK_NO_SCAN:
  1602. kmemleak_no_scan(log->ptr);
  1603. break;
  1604. default:
  1605. kmemleak_warn("Unknown early log operation: %d\n",
  1606. log->op_type);
  1607. }
  1608. if (atomic_read(&kmemleak_warning)) {
  1609. print_log_trace(log);
  1610. atomic_set(&kmemleak_warning, 0);
  1611. }
  1612. }
  1613. }
  1614. /*
  1615. * Late initialization function.
  1616. */
  1617. static int __init kmemleak_late_init(void)
  1618. {
  1619. struct dentry *dentry;
  1620. atomic_set(&kmemleak_initialized, 1);
  1621. if (atomic_read(&kmemleak_error)) {
  1622. /*
  1623. * Some error occurred and kmemleak was disabled. There is a
  1624. * small chance that kmemleak_disable() was called immediately
  1625. * after setting kmemleak_initialized and we may end up with
  1626. * two clean-up threads but serialized by scan_mutex.
  1627. */
  1628. schedule_work(&cleanup_work);
  1629. return -ENOMEM;
  1630. }
  1631. dentry = debugfs_create_file("kmemleak", S_IRUGO, NULL, NULL,
  1632. &kmemleak_fops);
  1633. if (!dentry)
  1634. pr_warning("Failed to create the debugfs kmemleak file\n");
  1635. mutex_lock(&scan_mutex);
  1636. start_scan_thread();
  1637. mutex_unlock(&scan_mutex);
  1638. pr_info("Kernel memory leak detector initialized\n");
  1639. return 0;
  1640. }
  1641. late_initcall(kmemleak_late_init);