Commit d78f3853 authored by SeongJae Park's avatar SeongJae Park Committed by Linus Torvalds

mm/damon/dbgfs: fix missed use of damon_dbgfs_lock

DAMON debugfs is supposed to protect dbgfs_ctxs, dbgfs_nr_ctxs, and
dbgfs_dirs using damon_dbgfs_lock.  However, some of the code is
accessing the variables without the protection.  This fixes it by
protecting all such accesses.

Link: https://lkml.kernel.org/r/20211110145758.16558-3-sj@kernel.org
Fixes: 75c1c2b5 ("mm/damon/dbgfs: support multiple contexts")
Signed-off-by: default avatarSeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent db7a347b
...@@ -877,12 +877,14 @@ static ssize_t dbgfs_monitor_on_write(struct file *file, ...@@ -877,12 +877,14 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
return -EINVAL; return -EINVAL;
} }
mutex_lock(&damon_dbgfs_lock);
if (!strncmp(kbuf, "on", count)) { if (!strncmp(kbuf, "on", count)) {
int i; int i;
for (i = 0; i < dbgfs_nr_ctxs; i++) { for (i = 0; i < dbgfs_nr_ctxs; i++) {
if (damon_targets_empty(dbgfs_ctxs[i])) { if (damon_targets_empty(dbgfs_ctxs[i])) {
kfree(kbuf); kfree(kbuf);
mutex_unlock(&damon_dbgfs_lock);
return -EINVAL; return -EINVAL;
} }
} }
...@@ -892,6 +894,7 @@ static ssize_t dbgfs_monitor_on_write(struct file *file, ...@@ -892,6 +894,7 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
} else { } else {
ret = -EINVAL; ret = -EINVAL;
} }
mutex_unlock(&damon_dbgfs_lock);
if (!ret) if (!ret)
ret = count; ret = count;
...@@ -944,15 +947,16 @@ static int __init __damon_dbgfs_init(void) ...@@ -944,15 +947,16 @@ static int __init __damon_dbgfs_init(void)
static int __init damon_dbgfs_init(void) static int __init damon_dbgfs_init(void)
{ {
int rc; int rc = -ENOMEM;
mutex_lock(&damon_dbgfs_lock);
dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL); dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
if (!dbgfs_ctxs) if (!dbgfs_ctxs)
return -ENOMEM; goto out;
dbgfs_ctxs[0] = dbgfs_new_ctx(); dbgfs_ctxs[0] = dbgfs_new_ctx();
if (!dbgfs_ctxs[0]) { if (!dbgfs_ctxs[0]) {
kfree(dbgfs_ctxs); kfree(dbgfs_ctxs);
return -ENOMEM; goto out;
} }
dbgfs_nr_ctxs = 1; dbgfs_nr_ctxs = 1;
...@@ -963,6 +967,8 @@ static int __init damon_dbgfs_init(void) ...@@ -963,6 +967,8 @@ static int __init damon_dbgfs_init(void)
pr_err("%s: dbgfs init failed\n", __func__); pr_err("%s: dbgfs init failed\n", __func__);
} }
out:
mutex_unlock(&damon_dbgfs_lock);
return rc; return rc;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment