1
0
Fork 0

kmemleak: Do not acquire scan_mutex in kmemleak_open()

Initially, the scan_mutex was acquired in kmemleak_open() and released
in kmemleak_release() (corresponding to /sys/kernel/debug/kmemleak
operations). This was causing some lockdep reports when the file was
closed from a different task than the one opening it. This patch moves
the scan_mutex acquiring in kmemleak_write() or kmemleak_seq_start()
with releasing in kmemleak_seq_stop().

Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
hifive-unleashed-5.1
Catalin Marinas 2009-07-07 10:32:58 +01:00
parent 288c857d66
commit b87324d082
1 changed files with 30 additions and 33 deletions

View File

@ -1101,6 +1101,11 @@ static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
{ {
struct kmemleak_object *object; struct kmemleak_object *object;
loff_t n = *pos; loff_t n = *pos;
int err;
err = mutex_lock_interruptible(&scan_mutex);
if (err < 0)
return ERR_PTR(err);
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) {
@ -1144,8 +1149,15 @@ static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
*/ */
static void kmemleak_seq_stop(struct seq_file *seq, void *v) static void kmemleak_seq_stop(struct seq_file *seq, void *v)
{ {
if (v) if (!IS_ERR(v)) {
put_object(v); /*
* kmemleak_seq_start may return ERR_PTR if the scan_mutex
* waiting was interrupted, so only release it if !IS_ERR.
*/
mutex_unlock(&scan_mutex);
if (v)
put_object(v);
}
} }
/* /*
@ -1172,36 +1184,15 @@ static const struct seq_operations kmemleak_seq_ops = {
static int kmemleak_open(struct inode *inode, struct file *file) static int kmemleak_open(struct inode *inode, struct file *file)
{ {
int ret = 0;
if (!atomic_read(&kmemleak_enabled)) if (!atomic_read(&kmemleak_enabled))
return -EBUSY; return -EBUSY;
ret = mutex_lock_interruptible(&scan_mutex); return seq_open(file, &kmemleak_seq_ops);
if (ret < 0)
goto out;
if (file->f_mode & FMODE_READ) {
ret = seq_open(file, &kmemleak_seq_ops);
if (ret < 0)
goto scan_unlock;
}
return ret;
scan_unlock:
mutex_unlock(&scan_mutex);
out:
return ret;
} }
static int kmemleak_release(struct inode *inode, struct file *file) static int kmemleak_release(struct inode *inode, struct file *file)
{ {
int ret = 0; return seq_release(inode, file);
if (file->f_mode & FMODE_READ)
seq_release(inode, file);
mutex_unlock(&scan_mutex);
return ret;
} }
/* /*
@ -1221,15 +1212,17 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
{ {
char buf[64]; char buf[64];
int buf_size; int buf_size;
int ret;
if (!atomic_read(&kmemleak_enabled))
return -EBUSY;
buf_size = min(size, (sizeof(buf) - 1)); buf_size = min(size, (sizeof(buf) - 1));
if (strncpy_from_user(buf, user_buf, buf_size) < 0) if (strncpy_from_user(buf, user_buf, buf_size) < 0)
return -EFAULT; return -EFAULT;
buf[buf_size] = 0; buf[buf_size] = 0;
ret = mutex_lock_interruptible(&scan_mutex);
if (ret < 0)
return ret;
if (strncmp(buf, "off", 3) == 0) if (strncmp(buf, "off", 3) == 0)
kmemleak_disable(); kmemleak_disable();
else if (strncmp(buf, "stack=on", 8) == 0) else if (strncmp(buf, "stack=on", 8) == 0)
@ -1242,11 +1235,10 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
stop_scan_thread(); stop_scan_thread();
else if (strncmp(buf, "scan=", 5) == 0) { else if (strncmp(buf, "scan=", 5) == 0) {
unsigned long secs; unsigned long secs;
int err;
err = strict_strtoul(buf + 5, 0, &secs); ret = strict_strtoul(buf + 5, 0, &secs);
if (err < 0) if (ret < 0)
return err; goto out;
stop_scan_thread(); stop_scan_thread();
if (secs) { if (secs) {
jiffies_scan_wait = msecs_to_jiffies(secs * 1000); jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
@ -1255,7 +1247,12 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
} else if (strncmp(buf, "scan", 4) == 0) } else if (strncmp(buf, "scan", 4) == 0)
kmemleak_scan(); kmemleak_scan();
else else
return -EINVAL; ret = -EINVAL;
out:
mutex_unlock(&scan_mutex);
if (ret < 0)
return ret;
/* ignore the rest of the buffer, only one command at a time */ /* ignore the rest of the buffer, only one command at a time */
*ppos += size; *ppos += size;