kmemleak.c 53 KB

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