kmemleak.c 53 KB

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