[PATCH] mm/kmemleak: use READ_ONCE() for accessing jiffies_scan_wait

From: Yanfei Xu
Date: Wed Jun 09 2021 - 11:57:25 EST


The stop_scan_thread() and start_scan_thread() cannot really solve
the problem of concurrent accessing the global jiffies_scan_wait.

kmemleak_write kmemleak_scan_thread
while (!kthread_should_stop())
stop_scan_thread
jiffies_scan_wait = xxx timeout = jiffies_scan_wait
start_scan_thread

We could replace these with a READ_ONCE() when reading
jiffies_scan_wait. It also can prevent compiler from reordering the
jiffies_scan_wait which is in while loop.

Signed-off-by: Yanfei Xu <yanfei.xu@xxxxxxxxxxxxx>
---
mm/kmemleak.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 92a2d4885808..5ccf3969b7fe 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1567,7 +1567,7 @@ static int kmemleak_scan_thread(void *arg)
}

while (!kthread_should_stop()) {
- signed long timeout = jiffies_scan_wait;
+ signed long timeout = READ_ONCE(jiffies_scan_wait);

mutex_lock(&scan_mutex);
kmemleak_scan();
@@ -1812,11 +1812,8 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
ret = kstrtoul(buf + 5, 0, &secs);
if (ret < 0)
goto out;
- stop_scan_thread();
- if (secs) {
+ if (secs)
jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
- start_scan_thread();
- }
} else if (strncmp(buf, "scan", 4) == 0)
kmemleak_scan();
else if (strncmp(buf, "dump=", 5) == 0)
--
2.27.0