kmemleak.c 66 KB

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