kmemleak.c 53 KB

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