aboutsummaryrefslogtreecommitdiffstats
path: root/mm/kmemleak.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/kmemleak.c')
-rw-r--r--mm/kmemleak.c197
1 files changed, 71 insertions, 126 deletions
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index c96f2c8700aa..e766e1da09d2 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -48,10 +48,10 @@
48 * scanned. This list is only modified during a scanning episode when the 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. 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 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 51 * added to the gray_list and therefore cannot be freed. This mutex also
52 * - kmemleak_mutex (mutex): prevents multiple users of the "kmemleak" debugfs 52 * prevents multiple users of the "kmemleak" debugfs file together with
53 * file together with modifications to the memory scanning parameters 53 * modifications to the memory scanning parameters including the scan_thread
54 * including the scan_thread pointer 54 * pointer
55 * 55 *
56 * The kmemleak_object structures have a use_count incremented or decremented 56 * The kmemleak_object structures have a use_count incremented or decremented
57 * using the get_object()/put_object() functions. When the use_count becomes 57 * using the get_object()/put_object() functions. When the use_count becomes
@@ -105,7 +105,6 @@
105#define MAX_TRACE 16 /* stack trace length */ 105#define MAX_TRACE 16 /* stack trace length */
106#define REPORTS_NR 50 /* maximum number of reported leaks */ 106#define REPORTS_NR 50 /* maximum number of reported leaks */
107#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */ 107#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
108#define MSECS_SCAN_YIELD 10 /* CPU yielding period */
109#define SECS_FIRST_SCAN 60 /* delay before the first scan */ 108#define SECS_FIRST_SCAN 60 /* delay before the first scan */
110#define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */ 109#define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */
111 110
@@ -186,19 +185,16 @@ static atomic_t kmemleak_error = ATOMIC_INIT(0);
186static unsigned long min_addr = ULONG_MAX; 185static unsigned long min_addr = ULONG_MAX;
187static unsigned long max_addr; 186static unsigned long max_addr;
188 187
189/* used for yielding the CPU to other tasks during scanning */
190static unsigned long next_scan_yield;
191static struct task_struct *scan_thread; 188static struct task_struct *scan_thread;
192static unsigned long jiffies_scan_yield; 189/* used to avoid reporting of recently allocated objects */
193static unsigned long jiffies_min_age; 190static unsigned long jiffies_min_age;
191static unsigned long jiffies_last_scan;
194/* delay between automatic memory scannings */ 192/* delay between automatic memory scannings */
195static signed long jiffies_scan_wait; 193static signed long jiffies_scan_wait;
196/* enables or disables the task stacks scanning */ 194/* enables or disables the task stacks scanning */
197static int kmemleak_stack_scan; 195static int kmemleak_stack_scan = 1;
198/* mutex protecting the memory scanning */ 196/* protects the memory scanning, parameters and debug/kmemleak file access */
199static DEFINE_MUTEX(scan_mutex); 197static DEFINE_MUTEX(scan_mutex);
200/* mutex protecting the access to the /sys/kernel/debug/kmemleak file */
201static DEFINE_MUTEX(kmemleak_mutex);
202 198
203/* number of leaks reported (for limitation purposes) */ 199/* number of leaks reported (for limitation purposes) */
204static int reported_leaks; 200static int reported_leaks;
@@ -235,7 +231,7 @@ struct early_log {
235}; 231};
236 232
237/* early logging buffer and current position */ 233/* early logging buffer and current position */
238static struct early_log early_log[200]; 234static struct early_log early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE];
239static int crt_early_log; 235static int crt_early_log;
240 236
241static void kmemleak_disable(void); 237static void kmemleak_disable(void);
@@ -279,15 +275,6 @@ static int color_gray(const struct kmemleak_object *object)
279} 275}
280 276
281/* 277/*
282 * Objects are considered referenced if their color is gray and they have not
283 * been deleted.
284 */
285static int referenced_object(struct kmemleak_object *object)
286{
287 return (object->flags & OBJECT_ALLOCATED) && color_gray(object);
288}
289
290/*
291 * Objects are considered unreferenced only if their color is white, they have 278 * Objects are considered unreferenced only if their color is white, they have
292 * not be deleted and have a minimum age to avoid false positives caused by 279 * not be deleted and have a minimum age to avoid false positives caused by
293 * pointers temporarily stored in CPU registers. 280 * pointers temporarily stored in CPU registers.
@@ -295,42 +282,28 @@ static int referenced_object(struct kmemleak_object *object)
295static int unreferenced_object(struct kmemleak_object *object) 282static int unreferenced_object(struct kmemleak_object *object)
296{ 283{
297 return (object->flags & OBJECT_ALLOCATED) && color_white(object) && 284 return (object->flags & OBJECT_ALLOCATED) && color_white(object) &&
298 time_is_before_eq_jiffies(object->jiffies + jiffies_min_age); 285 time_before_eq(object->jiffies + jiffies_min_age,
286 jiffies_last_scan);
299} 287}
300 288
301/* 289/*
302 * Printing of the (un)referenced objects information, either to the seq file 290 * Printing of the unreferenced objects information to the seq file. The
303 * or to the kernel log. The print_referenced/print_unreferenced functions 291 * print_unreferenced function must be called with the object->lock held.
304 * must be called with the object->lock held.
305 */ 292 */
306#define print_helper(seq, x...) do { \
307 struct seq_file *s = (seq); \
308 if (s) \
309 seq_printf(s, x); \
310 else \
311 pr_info(x); \
312} while (0)
313
314static void print_referenced(struct kmemleak_object *object)
315{
316 pr_info("referenced object 0x%08lx (size %zu)\n",
317 object->pointer, object->size);
318}
319
320static void print_unreferenced(struct seq_file *seq, 293static void print_unreferenced(struct seq_file *seq,
321 struct kmemleak_object *object) 294 struct kmemleak_object *object)
322{ 295{
323 int i; 296 int i;
324 297
325 print_helper(seq, "unreferenced object 0x%08lx (size %zu):\n", 298 seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
326 object->pointer, object->size); 299 object->pointer, object->size);
327 print_helper(seq, " comm \"%s\", pid %d, jiffies %lu\n", 300 seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n",
328 object->comm, object->pid, object->jiffies); 301 object->comm, object->pid, object->jiffies);
329 print_helper(seq, " backtrace:\n"); 302 seq_printf(seq, " backtrace:\n");
330 303
331 for (i = 0; i < object->trace_len; i++) { 304 for (i = 0; i < object->trace_len; i++) {
332 void *ptr = (void *)object->trace[i]; 305 void *ptr = (void *)object->trace[i];
333 print_helper(seq, " [<%p>] %pS\n", ptr, ptr); 306 seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
334 } 307 }
335} 308}
336 309
@@ -554,8 +527,10 @@ static void delete_object(unsigned long ptr)
554 write_lock_irqsave(&kmemleak_lock, flags); 527 write_lock_irqsave(&kmemleak_lock, flags);
555 object = lookup_object(ptr, 0); 528 object = lookup_object(ptr, 0);
556 if (!object) { 529 if (!object) {
530#ifdef DEBUG
557 kmemleak_warn("Freeing unknown object at 0x%08lx\n", 531 kmemleak_warn("Freeing unknown object at 0x%08lx\n",
558 ptr); 532 ptr);
533#endif
559 write_unlock_irqrestore(&kmemleak_lock, flags); 534 write_unlock_irqrestore(&kmemleak_lock, flags);
560 return; 535 return;
561 } 536 }
@@ -571,8 +546,6 @@ static void delete_object(unsigned long ptr)
571 * cannot be freed when it is being scanned. 546 * cannot be freed when it is being scanned.
572 */ 547 */
573 spin_lock_irqsave(&object->lock, flags); 548 spin_lock_irqsave(&object->lock, flags);
574 if (object->flags & OBJECT_REPORTED)
575 print_referenced(object);
576 object->flags &= ~OBJECT_ALLOCATED; 549 object->flags &= ~OBJECT_ALLOCATED;
577 spin_unlock_irqrestore(&object->lock, flags); 550 spin_unlock_irqrestore(&object->lock, flags);
578 put_object(object); 551 put_object(object);
@@ -696,7 +669,8 @@ static void log_early(int op_type, const void *ptr, size_t size,
696 struct early_log *log; 669 struct early_log *log;
697 670
698 if (crt_early_log >= ARRAY_SIZE(early_log)) { 671 if (crt_early_log >= ARRAY_SIZE(early_log)) {
699 kmemleak_stop("Early log buffer exceeded\n"); 672 pr_warning("Early log buffer exceeded\n");
673 kmemleak_disable();
700 return; 674 return;
701 } 675 }
702 676
@@ -808,21 +782,6 @@ void kmemleak_no_scan(const void *ptr)
808EXPORT_SYMBOL(kmemleak_no_scan); 782EXPORT_SYMBOL(kmemleak_no_scan);
809 783
810/* 784/*
811 * Yield the CPU so that other tasks get a chance to run. The yielding is
812 * rate-limited to avoid excessive number of calls to the schedule() function
813 * during memory scanning.
814 */
815static void scan_yield(void)
816{
817 might_sleep();
818
819 if (time_is_before_eq_jiffies(next_scan_yield)) {
820 schedule();
821 next_scan_yield = jiffies + jiffies_scan_yield;
822 }
823}
824
825/*
826 * Memory scanning is a long process and it needs to be interruptable. This 785 * Memory scanning is a long process and it needs to be interruptable. This
827 * function checks whether such interrupt condition occured. 786 * function checks whether such interrupt condition occured.
828 */ 787 */
@@ -862,15 +821,6 @@ static void scan_block(void *_start, void *_end,
862 if (scan_should_stop()) 821 if (scan_should_stop())
863 break; 822 break;
864 823
865 /*
866 * When scanning a memory block with a corresponding
867 * kmemleak_object, the CPU yielding is handled in the calling
868 * code since it holds the object->lock to avoid the block
869 * freeing.
870 */
871 if (!scanned)
872 scan_yield();
873
874 object = find_and_get_object(pointer, 1); 824 object = find_and_get_object(pointer, 1);
875 if (!object) 825 if (!object)
876 continue; 826 continue;
@@ -952,6 +902,9 @@ static void kmemleak_scan(void)
952 struct kmemleak_object *object, *tmp; 902 struct kmemleak_object *object, *tmp;
953 struct task_struct *task; 903 struct task_struct *task;
954 int i; 904 int i;
905 int new_leaks = 0;
906
907 jiffies_last_scan = jiffies;
955 908
956 /* prepare the kmemleak_object's */ 909 /* prepare the kmemleak_object's */
957 rcu_read_lock(); 910 rcu_read_lock();
@@ -1033,7 +986,7 @@ static void kmemleak_scan(void)
1033 */ 986 */
1034 object = list_entry(gray_list.next, typeof(*object), gray_list); 987 object = list_entry(gray_list.next, typeof(*object), gray_list);
1035 while (&object->gray_list != &gray_list) { 988 while (&object->gray_list != &gray_list) {
1036 scan_yield(); 989 cond_resched();
1037 990
1038 /* may add new objects to the list */ 991 /* may add new objects to the list */
1039 if (!scan_should_stop()) 992 if (!scan_should_stop())
@@ -1049,6 +1002,32 @@ static void kmemleak_scan(void)
1049 object = tmp; 1002 object = tmp;
1050 } 1003 }
1051 WARN_ON(!list_empty(&gray_list)); 1004 WARN_ON(!list_empty(&gray_list));
1005
1006 /*
1007 * If scanning was stopped do not report any new unreferenced objects.
1008 */
1009 if (scan_should_stop())
1010 return;
1011
1012 /*
1013 * Scanning result reporting.
1014 */
1015 rcu_read_lock();
1016 list_for_each_entry_rcu(object, &object_list, object_list) {
1017 spin_lock_irqsave(&object->lock, flags);
1018 if (unreferenced_object(object) &&
1019 !(object->flags & OBJECT_REPORTED)) {
1020 object->flags |= OBJECT_REPORTED;
1021 new_leaks++;
1022 }
1023 spin_unlock_irqrestore(&object->lock, flags);
1024 }
1025 rcu_read_unlock();
1026
1027 if (new_leaks)
1028 pr_info("%d new suspected memory leaks (see "
1029 "/sys/kernel/debug/kmemleak)\n", new_leaks);
1030
1052} 1031}
1053 1032
1054/* 1033/*
@@ -1070,36 +1049,12 @@ static int kmemleak_scan_thread(void *arg)
1070 } 1049 }
1071 1050
1072 while (!kthread_should_stop()) { 1051 while (!kthread_should_stop()) {
1073 struct kmemleak_object *object;
1074 signed long timeout = jiffies_scan_wait; 1052 signed long timeout = jiffies_scan_wait;
1075 1053
1076 mutex_lock(&scan_mutex); 1054 mutex_lock(&scan_mutex);
1077
1078 kmemleak_scan(); 1055 kmemleak_scan();
1079 reported_leaks = 0;
1080
1081 rcu_read_lock();
1082 list_for_each_entry_rcu(object, &object_list, object_list) {
1083 unsigned long flags;
1084
1085 if (reported_leaks >= REPORTS_NR)
1086 break;
1087 spin_lock_irqsave(&object->lock, flags);
1088 if (!(object->flags & OBJECT_REPORTED) &&
1089 unreferenced_object(object)) {
1090 print_unreferenced(NULL, object);
1091 object->flags |= OBJECT_REPORTED;
1092 reported_leaks++;
1093 } else if ((object->flags & OBJECT_REPORTED) &&
1094 referenced_object(object)) {
1095 print_referenced(object);
1096 object->flags &= ~OBJECT_REPORTED;
1097 }
1098 spin_unlock_irqrestore(&object->lock, flags);
1099 }
1100 rcu_read_unlock();
1101
1102 mutex_unlock(&scan_mutex); 1056 mutex_unlock(&scan_mutex);
1057
1103 /* wait before the next scan */ 1058 /* wait before the next scan */
1104 while (timeout && !kthread_should_stop()) 1059 while (timeout && !kthread_should_stop())
1105 timeout = schedule_timeout_interruptible(timeout); 1060 timeout = schedule_timeout_interruptible(timeout);
@@ -1112,7 +1067,7 @@ static int kmemleak_scan_thread(void *arg)
1112 1067
1113/* 1068/*
1114 * Start the automatic memory scanning thread. This function must be called 1069 * Start the automatic memory scanning thread. This function must be called
1115 * with the kmemleak_mutex held. 1070 * with the scan_mutex held.
1116 */ 1071 */
1117void start_scan_thread(void) 1072void start_scan_thread(void)
1118{ 1073{
@@ -1127,7 +1082,7 @@ void start_scan_thread(void)
1127 1082
1128/* 1083/*
1129 * Stop the automatic memory scanning thread. This function must be called 1084 * Stop the automatic memory scanning thread. This function must be called
1130 * with the kmemleak_mutex held. 1085 * with the scan_mutex held.
1131 */ 1086 */
1132void stop_scan_thread(void) 1087void stop_scan_thread(void)
1133{ 1088{
@@ -1147,10 +1102,8 @@ static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
1147 struct kmemleak_object *object; 1102 struct kmemleak_object *object;
1148 loff_t n = *pos; 1103 loff_t n = *pos;
1149 1104
1150 if (!n) { 1105 if (!n)
1151 kmemleak_scan();
1152 reported_leaks = 0; 1106 reported_leaks = 0;
1153 }
1154 if (reported_leaks >= REPORTS_NR) 1107 if (reported_leaks >= REPORTS_NR)
1155 return NULL; 1108 return NULL;
1156 1109
@@ -1211,11 +1164,10 @@ static int kmemleak_seq_show(struct seq_file *seq, void *v)
1211 unsigned long flags; 1164 unsigned long flags;
1212 1165
1213 spin_lock_irqsave(&object->lock, flags); 1166 spin_lock_irqsave(&object->lock, flags);
1214 if (!unreferenced_object(object)) 1167 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object)) {
1215 goto out; 1168 print_unreferenced(seq, object);
1216 print_unreferenced(seq, object); 1169 reported_leaks++;
1217 reported_leaks++; 1170 }
1218out:
1219 spin_unlock_irqrestore(&object->lock, flags); 1171 spin_unlock_irqrestore(&object->lock, flags);
1220 return 0; 1172 return 0;
1221} 1173}
@@ -1234,13 +1186,10 @@ static int kmemleak_open(struct inode *inode, struct file *file)
1234 if (!atomic_read(&kmemleak_enabled)) 1186 if (!atomic_read(&kmemleak_enabled))
1235 return -EBUSY; 1187 return -EBUSY;
1236 1188
1237 ret = mutex_lock_interruptible(&kmemleak_mutex); 1189 ret = mutex_lock_interruptible(&scan_mutex);
1238 if (ret < 0) 1190 if (ret < 0)
1239 goto out; 1191 goto out;
1240 if (file->f_mode & FMODE_READ) { 1192 if (file->f_mode & FMODE_READ) {
1241 ret = mutex_lock_interruptible(&scan_mutex);
1242 if (ret < 0)
1243 goto kmemleak_unlock;
1244 ret = seq_open(file, &kmemleak_seq_ops); 1193 ret = seq_open(file, &kmemleak_seq_ops);
1245 if (ret < 0) 1194 if (ret < 0)
1246 goto scan_unlock; 1195 goto scan_unlock;
@@ -1249,8 +1198,6 @@ static int kmemleak_open(struct inode *inode, struct file *file)
1249 1198
1250scan_unlock: 1199scan_unlock:
1251 mutex_unlock(&scan_mutex); 1200 mutex_unlock(&scan_mutex);
1252kmemleak_unlock:
1253 mutex_unlock(&kmemleak_mutex);
1254out: 1201out:
1255 return ret; 1202 return ret;
1256} 1203}
@@ -1259,11 +1206,9 @@ static int kmemleak_release(struct inode *inode, struct file *file)
1259{ 1206{
1260 int ret = 0; 1207 int ret = 0;
1261 1208
1262 if (file->f_mode & FMODE_READ) { 1209 if (file->f_mode & FMODE_READ)
1263 seq_release(inode, file); 1210 seq_release(inode, file);
1264 mutex_unlock(&scan_mutex); 1211 mutex_unlock(&scan_mutex);
1265 }
1266 mutex_unlock(&kmemleak_mutex);
1267 1212
1268 return ret; 1213 return ret;
1269} 1214}
@@ -1278,6 +1223,7 @@ static int kmemleak_release(struct inode *inode, struct file *file)
1278 * scan=off - stop the automatic memory scanning thread 1223 * scan=off - stop the automatic memory scanning thread
1279 * scan=... - set the automatic memory scanning period in seconds (0 to 1224 * scan=... - set the automatic memory scanning period in seconds (0 to
1280 * disable it) 1225 * disable it)
1226 * scan - trigger a memory scan
1281 */ 1227 */
1282static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, 1228static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
1283 size_t size, loff_t *ppos) 1229 size_t size, loff_t *ppos)
@@ -1315,7 +1261,9 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
1315 jiffies_scan_wait = msecs_to_jiffies(secs * 1000); 1261 jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
1316 start_scan_thread(); 1262 start_scan_thread();
1317 } 1263 }
1318 } else 1264 } else if (strncmp(buf, "scan", 4) == 0)
1265 kmemleak_scan();
1266 else
1319 return -EINVAL; 1267 return -EINVAL;
1320 1268
1321 /* ignore the rest of the buffer, only one command at a time */ 1269 /* ignore the rest of the buffer, only one command at a time */
@@ -1340,11 +1288,9 @@ static int kmemleak_cleanup_thread(void *arg)
1340{ 1288{
1341 struct kmemleak_object *object; 1289 struct kmemleak_object *object;
1342 1290
1343 mutex_lock(&kmemleak_mutex); 1291 mutex_lock(&scan_mutex);
1344 stop_scan_thread(); 1292 stop_scan_thread();
1345 mutex_unlock(&kmemleak_mutex);
1346 1293
1347 mutex_lock(&scan_mutex);
1348 rcu_read_lock(); 1294 rcu_read_lock();
1349 list_for_each_entry_rcu(object, &object_list, object_list) 1295 list_for_each_entry_rcu(object, &object_list, object_list)
1350 delete_object(object->pointer); 1296 delete_object(object->pointer);
@@ -1411,7 +1357,6 @@ void __init kmemleak_init(void)
1411 int i; 1357 int i;
1412 unsigned long flags; 1358 unsigned long flags;
1413 1359
1414 jiffies_scan_yield = msecs_to_jiffies(MSECS_SCAN_YIELD);
1415 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE); 1360 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
1416 jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000); 1361 jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
1417 1362
@@ -1486,9 +1431,9 @@ static int __init kmemleak_late_init(void)
1486 &kmemleak_fops); 1431 &kmemleak_fops);
1487 if (!dentry) 1432 if (!dentry)
1488 pr_warning("Failed to create the debugfs kmemleak file\n"); 1433 pr_warning("Failed to create the debugfs kmemleak file\n");
1489 mutex_lock(&kmemleak_mutex); 1434 mutex_lock(&scan_mutex);
1490 start_scan_thread(); 1435 start_scan_thread();
1491 mutex_unlock(&kmemleak_mutex); 1436 mutex_unlock(&scan_mutex);
1492 1437
1493 pr_info("Kernel memory leak detector initialized\n"); 1438 pr_info("Kernel memory leak detector initialized\n");
1494 1439