kmemleak: remove redundant code
[deliverable/linux.git] / mm / kmemleak.c
index 31f01c5011e59414e95b888b2b3d515940ab72ab..6f90d003830a91b3a368f01607cdfa0099eaff62 100644 (file)
@@ -218,7 +218,8 @@ static int kmemleak_stack_scan = 1;
 static DEFINE_MUTEX(scan_mutex);
 /* setting kmemleak=on, will set this var, skipping the disable */
 static int kmemleak_skip_disable;
-
+/* If there are leaks that can be reported */
+static bool kmemleak_found_leaks;
 
 /*
  * Early object allocation/freeing logging. Kmemleak is initialized after the
@@ -1382,9 +1383,12 @@ static void kmemleak_scan(void)
        }
        rcu_read_unlock();
 
-       if (new_leaks)
+       if (new_leaks) {
+               kmemleak_found_leaks = true;
+
                pr_info("%d new suspected memory leaks (see "
                        "/sys/kernel/debug/kmemleak)\n", new_leaks);
+       }
 
 }
 
@@ -1545,11 +1549,6 @@ static int kmemleak_open(struct inode *inode, struct file *file)
        return seq_open(file, &kmemleak_seq_ops);
 }
 
-static int kmemleak_release(struct inode *inode, struct file *file)
-{
-       return seq_release(inode, file);
-}
-
 static int dump_str_object_info(const char *str)
 {
        unsigned long flags;
@@ -1592,8 +1591,12 @@ static void kmemleak_clear(void)
                spin_unlock_irqrestore(&object->lock, flags);
        }
        rcu_read_unlock();
+
+       kmemleak_found_leaks = false;
 }
 
+static void __kmemleak_do_cleanup(void);
+
 /*
  * File write operation to configure kmemleak at run-time. The following
  * commands can be written to the /sys/kernel/debug/kmemleak file:
@@ -1606,7 +1609,8 @@ static void kmemleak_clear(void)
  *               disable it)
  *   scan      - trigger a memory scan
  *   clear     - mark all current reported unreferenced kmemleak objects as
- *               grey to ignore printing them
+ *               grey to ignore printing them, or free all kmemleak objects
+ *               if kmemleak has been disabled.
  *   dump=...  - dump information about the object found at the given address
  */
 static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
@@ -1616,9 +1620,6 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
        int buf_size;
        int ret;
 
-       if (!atomic_read(&kmemleak_enabled))
-               return -EBUSY;
-
        buf_size = min(size, (sizeof(buf) - 1));
        if (strncpy_from_user(buf, user_buf, buf_size) < 0)
                return -EFAULT;
@@ -1628,6 +1629,19 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
        if (ret < 0)
                return ret;
 
+       if (strncmp(buf, "clear", 5) == 0) {
+               if (atomic_read(&kmemleak_enabled))
+                       kmemleak_clear();
+               else
+                       __kmemleak_do_cleanup();
+               goto out;
+       }
+
+       if (!atomic_read(&kmemleak_enabled)) {
+               ret = -EBUSY;
+               goto out;
+       }
+
        if (strncmp(buf, "off", 3) == 0)
                kmemleak_disable();
        else if (strncmp(buf, "stack=on", 8) == 0)
@@ -1651,8 +1665,6 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
                }
        } else if (strncmp(buf, "scan", 4) == 0)
                kmemleak_scan();
-       else if (strncmp(buf, "clear", 5) == 0)
-               kmemleak_clear();
        else if (strncmp(buf, "dump=", 5) == 0)
                ret = dump_str_object_info(buf + 5);
        else
@@ -1674,9 +1686,19 @@ static const struct file_operations kmemleak_fops = {
        .read           = seq_read,
        .write          = kmemleak_write,
        .llseek         = seq_lseek,
-       .release        = kmemleak_release,
+       .release        = seq_release,
 };
 
+static void __kmemleak_do_cleanup(void)
+{
+       struct kmemleak_object *object;
+
+       rcu_read_lock();
+       list_for_each_entry_rcu(object, &object_list, object_list)
+               delete_object_full(object->pointer);
+       rcu_read_unlock();
+}
+
 /*
  * Stop the memory scanning thread and free the kmemleak internal objects if
  * no previous scan thread (otherwise, kmemleak may still have some useful
@@ -1684,18 +1706,14 @@ static const struct file_operations kmemleak_fops = {
  */
 static void kmemleak_do_cleanup(struct work_struct *work)
 {
-       struct kmemleak_object *object;
-       bool cleanup = scan_thread == NULL;
-
        mutex_lock(&scan_mutex);
        stop_scan_thread();
 
-       if (cleanup) {
-               rcu_read_lock();
-               list_for_each_entry_rcu(object, &object_list, object_list)
-                       delete_object_full(object->pointer);
-               rcu_read_unlock();
-       }
+       if (!kmemleak_found_leaks)
+               __kmemleak_do_cleanup();
+       else
+               pr_info("Kmemleak disabled without freeing internal data. "
+                       "Reclaim the memory with \"echo clear > /sys/kernel/debug/kmemleak\"\n");
        mutex_unlock(&scan_mutex);
 }
 
This page took 0.026997 seconds and 5 git commands to generate.