kmemleak: Simplify the reports logged by the scanning thread

Because of false positives, the memory scanning thread may print too
much information. This patch changes the scanning thread to only print
the number of newly suspected leaks. Further information can be read
from the /sys/kernel/debug/kmemleak file.

Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
This commit is contained in:
Catalin Marinas 2009-06-26 17:38:26 +01:00
parent e0a2a1601b
commit bab4a34afc
2 changed files with 19 additions and 48 deletions

View file

@ -16,9 +16,9 @@ Usage
----- -----
CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel
thread scans the memory every 10 minutes (by default) and prints any new thread scans the memory every 10 minutes (by default) and prints the
unreferenced objects found. To trigger an intermediate scan and display number of new unreferenced objects found. To trigger an intermediate
all the possible memory leaks: scan and display the details of all the possible memory leaks:
# mount -t debugfs nodev /sys/kernel/debug/ # mount -t debugfs nodev /sys/kernel/debug/
# cat /sys/kernel/debug/kmemleak # cat /sys/kernel/debug/kmemleak

View file

@ -278,15 +278,6 @@ static int color_gray(const struct kmemleak_object *object)
return object->min_count != -1 && object->count >= object->min_count; return object->min_count != -1 && object->count >= object->min_count;
} }
/*
* Objects are considered referenced if their color is gray and they have not
* been deleted.
*/
static int referenced_object(struct kmemleak_object *object)
{
return (object->flags & OBJECT_ALLOCATED) && color_gray(object);
}
/* /*
* Objects are considered unreferenced only if their color is white, they have * Objects are considered unreferenced only if their color is white, they have
* not be deleted and have a minimum age to avoid false positives caused by * not be deleted and have a minimum age to avoid false positives caused by
@ -299,38 +290,23 @@ static int unreferenced_object(struct kmemleak_object *object)
} }
/* /*
* Printing of the (un)referenced objects information, either to the seq file * Printing of the unreferenced objects information to the seq file. The
* or to the kernel log. The print_referenced/print_unreferenced functions * print_unreferenced function must be called with the object->lock held.
* must be called with the object->lock held.
*/ */
#define print_helper(seq, x...) do { \
struct seq_file *s = (seq); \
if (s) \
seq_printf(s, x); \
else \
pr_info(x); \
} while (0)
static void print_referenced(struct kmemleak_object *object)
{
pr_info("referenced object 0x%08lx (size %zu)\n",
object->pointer, object->size);
}
static void print_unreferenced(struct seq_file *seq, static void print_unreferenced(struct seq_file *seq,
struct kmemleak_object *object) struct kmemleak_object *object)
{ {
int i; int i;
print_helper(seq, "unreferenced object 0x%08lx (size %zu):\n", seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
object->pointer, object->size); object->pointer, object->size);
print_helper(seq, " comm \"%s\", pid %d, jiffies %lu\n", seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n",
object->comm, object->pid, object->jiffies); object->comm, object->pid, object->jiffies);
print_helper(seq, " backtrace:\n"); seq_printf(seq, " backtrace:\n");
for (i = 0; i < object->trace_len; i++) { for (i = 0; i < object->trace_len; i++) {
void *ptr = (void *)object->trace[i]; void *ptr = (void *)object->trace[i];
print_helper(seq, " [<%p>] %pS\n", ptr, ptr); seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
} }
} }
@ -571,8 +547,6 @@ static void delete_object(unsigned long ptr)
* cannot be freed when it is being scanned. * cannot be freed when it is being scanned.
*/ */
spin_lock_irqsave(&object->lock, flags); spin_lock_irqsave(&object->lock, flags);
if (object->flags & OBJECT_REPORTED)
print_referenced(object);
object->flags &= ~OBJECT_ALLOCATED; object->flags &= ~OBJECT_ALLOCATED;
spin_unlock_irqrestore(&object->lock, flags); spin_unlock_irqrestore(&object->lock, flags);
put_object(object); put_object(object);
@ -1073,33 +1047,30 @@ static int kmemleak_scan_thread(void *arg)
while (!kthread_should_stop()) { while (!kthread_should_stop()) {
struct kmemleak_object *object; struct kmemleak_object *object;
signed long timeout = jiffies_scan_wait; signed long timeout = jiffies_scan_wait;
int new_leaks = 0;
mutex_lock(&scan_mutex); mutex_lock(&scan_mutex);
kmemleak_scan(); kmemleak_scan();
reported_leaks = 0;
rcu_read_lock(); rcu_read_lock();
list_for_each_entry_rcu(object, &object_list, object_list) { list_for_each_entry_rcu(object, &object_list, object_list) {
unsigned long flags; unsigned long flags;
if (reported_leaks >= REPORTS_NR)
break;
spin_lock_irqsave(&object->lock, flags); spin_lock_irqsave(&object->lock, flags);
if (!(object->flags & OBJECT_REPORTED) && if (unreferenced_object(object) &&
unreferenced_object(object)) { !(object->flags & OBJECT_REPORTED)) {
print_unreferenced(NULL, object);
object->flags |= OBJECT_REPORTED; object->flags |= OBJECT_REPORTED;
reported_leaks++; new_leaks++;
} else if ((object->flags & OBJECT_REPORTED) &&
referenced_object(object)) {
print_referenced(object);
object->flags &= ~OBJECT_REPORTED;
} }
spin_unlock_irqrestore(&object->lock, flags); spin_unlock_irqrestore(&object->lock, flags);
} }
rcu_read_unlock(); rcu_read_unlock();
if (new_leaks)
pr_info("%d new suspected memory leaks (see "
"/sys/kernel/debug/kmemleak)\n", new_leaks);
mutex_unlock(&scan_mutex); mutex_unlock(&scan_mutex);
/* wait before the next scan */ /* wait before the next scan */
while (timeout && !kthread_should_stop()) while (timeout && !kthread_should_stop())