mm/kmemleak: use READ_ONCE() for accessing jiffies_scan_wait
Subsystems (2)
PATCH 1/1 mm/kmemleak: use READ_ONCE() for accessing jiffies_scan_wait
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.
--- mm/kmemleak.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
mm/kmemleak.c +2 -5
@@ -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 )
Signature · 1 line
2.27.0
Catalin Marinas
maintainerQuoted 9 lines · Yanfei Xu
The
stop_scan_thread()andstart_scan_thread()cannot really solve the problem of concurrent accessing the globaljiffies_scan_wait.
kmemleak_writekmemleak_scan_thread
while (!kthread_should_stop())
stop_scan_thread
jiffies_scan_wait= xxx timeout =jiffies_scan_wait
start_scan_thread
Quoted 3 lines
We could replace these with a
READ_ONCE()when readingjiffies_scan_wait. It also can prevent compiler from reordering thejiffies_scan_waitwhich is in while loop.
I'm ok with READ_ONCE but your patch introduces functional changes.
Quoted 14 lines
mm/kmemleak.c +1
-1@@ -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 ();
mm/kmemleak.c static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, · line 1816
Quoted 7 lines
mm/kmemleak.c +1
-2@@ -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 );
For symmetry, I'd add a WRITE_ONCE here as well.
mm/kmemleak.c static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, · line 1819
Quoted 2 lines
mm/kmemleak.c +0
-2-start_scan_thread (); - }
The reason for stop/start_scan_thread() wasn't to protect against jiffies_scan_wait access but rather to force a new delay. Let's say you start by default with a 10min delay between scans (default) but you want to lower it to 1min. With the above removal of stop/start, you'd still have to wait for 10min until the scanning thread will notice the change. Also, with secs=0, the expectations is that the thread won't be restarted but this is removed by your patch.
Signature · 1 line
Catalin
Quoted 45 lines · Catalin Marinas
[Please note: This e-mail is from an EXTERNAL e-mail address]
Quoted 9 lines · Yanfei Xu
The
stop_scan_thread()andstart_scan_thread()cannot really solve the problem of concurrent accessing the globaljiffies_scan_wait.
kmemleak_writekmemleak_scan_thread
while (!kthread_should_stop())
stop_scan_thread
jiffies_scan_wait= xxx timeout =jiffies_scan_wait
start_scan_threadQuoted 3 lines
We could replace these with a
READ_ONCE()when readingjiffies_scan_wait. It also can prevent compiler from reordering thejiffies_scan_waitwhich is in while loop.I'm ok with
READ_ONCEbut your patch introduces functional changes.Quoted 14 lines
mm/kmemleak.c +1
-1@@ -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 ();mm/kmemleak.c static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, · line 1816
Quoted 7 lines
mm/kmemleak.c +1
-2@@ -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 );For symmetry, I'd add a
WRITE_ONCEhere as well.mm/kmemleak.c static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, · line 1819
Quoted 2 lines
mm/kmemleak.c +0
-2-start_scan_thread (); - }
Quoted 8 lines
The reason for stop/start_scan_thread() wasn't to protect against
jiffies_scan_waitaccess but rather to force a new delay. Let's say you start by default with a 10min delay between scans (default) but you want to lower it to 1min. With the above removal of stop/start, you'd still have to wait for 10min until the scanning thread will notice the change. Also, with secs=0, the expectations is that the thread won't be restarted but this is removed by your patch.
I see.
Thanks for your explain and sorry for my bad introduction. Will send a v2.
Thanks,
Yanfei
Quoted 3 lines
--
Catalin
Quoted 58 lines · Yanfei
Quoted 46 lines · Catalin Marinas
[Please note: This e-mail is from an EXTERNAL e-mail address]
Quoted 9 lines · Yanfei Xu
The
stop_scan_thread()andstart_scan_thread()cannot really solve the problem of concurrent accessing the globaljiffies_scan_wait.kmemleak_write
kmemleak_scan_threadwhile (!kthread_should_stop())stop_scan_thread
jiffies_scan_wait= xxx timeout =jiffies_scan_waitstart_scan_threadQuoted 3 lines
We could replace these with a
READ_ONCE()when readingjiffies_scan_wait. It also can prevent compiler from reordering thejiffies_scan_waitwhich is in while loop.I'm ok with
READ_ONCEbut your patch introduces functional changes.Quoted 14 lines
mm/kmemleak.c +1
-1@@ -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();mm/kmemleak.c static ssize_t kmemleak_write(struct file · line 1817
Quoted 8 lines
mm/kmemleak.c +1
-2@@ -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);For symmetry, I'd add a
WRITE_ONCEhere as well.mm/kmemleak.c static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, · line 1819
Quoted 2 lines
mm/kmemleak.c +0
-2- start_scan_thread(); - }Quoted 8 lines
The reason for stop/start_scan_thread() wasn't to protect against
jiffies_scan_waitaccess but rather to force a new delay. Let's say you start by default with a 10min delay between scans (default) but you want to lower it to 1min. With the above removal of stop/start, you'd still have to wait for 10min until the scanning thread will notice the change. Also, with secs=0, the expectations is that the thread won't be restarted but this is removed by your patch.
Quoted 3 lines
I see.
Thanks for your explain and sorry for my bad introduction. Will send a v2.
Hi Catalin and Andrew,
I sent the v2 patch which is renamed to:
[PATCH] mm/kmemleak: fix the possible wrong memory scanning period
I have tested it on qemux86, and hope you can help to review. Thanks.
--Yanfei
Quoted 6 lines
Thanks,
YanfeiQuoted 3 lines
Signature · 1 line
Catalin