Skip to content

Commit

Permalink
mm/damon/dbgfs: fix missed use of damon_dbgfs_lock
Browse files Browse the repository at this point in the history
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/[email protected]
Fixes: 75c1c2b ("mm/damon/dbgfs: support multiple contexts")
Signed-off-by: SeongJae Park <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
sjp38 authored and torvalds committed Nov 20, 2021
1 parent db7a347 commit d78f385
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mm/damon/dbgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,12 +877,14 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
return -EINVAL;
}

mutex_lock(&damon_dbgfs_lock);
if (!strncmp(kbuf, "on", count)) {
int i;

for (i = 0; i < dbgfs_nr_ctxs; i++) {
if (damon_targets_empty(dbgfs_ctxs[i])) {
kfree(kbuf);
mutex_unlock(&damon_dbgfs_lock);
return -EINVAL;
}
}
Expand All @@ -892,6 +894,7 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
} else {
ret = -EINVAL;
}
mutex_unlock(&damon_dbgfs_lock);

if (!ret)
ret = count;
Expand Down Expand Up @@ -944,15 +947,16 @@ 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);
if (!dbgfs_ctxs)
return -ENOMEM;
goto out;
dbgfs_ctxs[0] = dbgfs_new_ctx();
if (!dbgfs_ctxs[0]) {
kfree(dbgfs_ctxs);
return -ENOMEM;
goto out;
}
dbgfs_nr_ctxs = 1;

Expand All @@ -963,6 +967,8 @@ static int __init damon_dbgfs_init(void)
pr_err("%s: dbgfs init failed\n", __func__);
}

out:
mutex_unlock(&damon_dbgfs_lock);
return rc;
}

Expand Down

0 comments on commit d78f385

Please sign in to comment.