kmemleak.c 59 KB

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