kmemleak.c 54 KB

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