Skip to content

Commit d78f385

Browse files
sjp38torvalds
authored andcommitted
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: 75c1c2b ("mm/damon/dbgfs: support multiple contexts") Signed-off-by: SeongJae Park <sj@kernel.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent db7a347 commit d78f385

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

mm/damon/dbgfs.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,14 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
877877
return -EINVAL;
878878
}
879879

880+
mutex_lock(&damon_dbgfs_lock);
880881
if (!strncmp(kbuf, "on", count)) {
881882
int i;
882883

883884
for (i = 0; i < dbgfs_nr_ctxs; i++) {
884885
if (damon_targets_empty(dbgfs_ctxs[i])) {
885886
kfree(kbuf);
887+
mutex_unlock(&damon_dbgfs_lock);
886888
return -EINVAL;
887889
}
888890
}
@@ -892,6 +894,7 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
892894
} else {
893895
ret = -EINVAL;
894896
}
897+
mutex_unlock(&damon_dbgfs_lock);
895898

896899
if (!ret)
897900
ret = count;
@@ -944,15 +947,16 @@ static int __init __damon_dbgfs_init(void)
944947

945948
static int __init damon_dbgfs_init(void)
946949
{
947-
int rc;
950+
int rc = -ENOMEM;
948951

952+
mutex_lock(&damon_dbgfs_lock);
949953
dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
950954
if (!dbgfs_ctxs)
951-
return -ENOMEM;
955+
goto out;
952956
dbgfs_ctxs[0] = dbgfs_new_ctx();
953957
if (!dbgfs_ctxs[0]) {
954958
kfree(dbgfs_ctxs);
955-
return -ENOMEM;
959+
goto out;
956960
}
957961
dbgfs_nr_ctxs = 1;
958962

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

970+
out:
971+
mutex_unlock(&damon_dbgfs_lock);
966972
return rc;
967973
}
968974

0 commit comments

Comments
 (0)