All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/kmemleak: fix the possible wrong memory scanning period
@ 2021-06-13 17:40 Yanfei Xu
  2021-06-15  9:35 ` Catalin Marinas
  0 siblings, 1 reply; 2+ messages in thread
From: Yanfei Xu @ 2021-06-13 17:40 UTC (permalink / raw)
  To: catalin.marinas, akpm; +Cc: linux-mm, linux-kernel

This commit contains 3 modifies:

1.Convert the type of jiffies_scan_wait to "unsigned long".

2.Use READ/WRITE_ONCE() for accessing "jiffies_scan_wait".

3.Fix the possible wrong memory scanning period. If you set a large
memory scanning period like blow, then the "secs" variable will be
non-zero, however the value of "jiffies_scan_wait" will be zero.

    echo "scan=0x10000000" > /sys/kernel/debug/kmemleak

It is because the type of the msecs_to_jiffies()'s parameter is
"unsigned int", and the "secs * 1000" is larger than its max value.
This in turn leads a unexpected jiffies_scan_wait, maybe zero. We
corret it by replacing kstrtoul() with kstrtouint(), and check the
msecs to prevent it larger than UINT_MAX.

Signed-off-by: Yanfei Xu <yanfei.xu@windriver.com>
---
 mm/kmemleak.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 92a2d4885808..228a2fbe0657 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -219,7 +219,7 @@ static struct task_struct *scan_thread;
 static unsigned long jiffies_min_age;
 static unsigned long jiffies_last_scan;
 /* delay between automatic memory scannings */
-static signed long jiffies_scan_wait;
+static unsigned long jiffies_scan_wait;
 /* enables or disables the task stacks scanning */
 static int kmemleak_stack_scan = 1;
 /* protects the memory scanning, parameters and debug/kmemleak file access */
@@ -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();
@@ -1807,14 +1807,20 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
 	else if (strncmp(buf, "scan=off", 8) == 0)
 		stop_scan_thread();
 	else if (strncmp(buf, "scan=", 5) == 0) {
-		unsigned long secs;
+		unsigned secs;
+		unsigned long msecs;
 
-		ret = kstrtoul(buf + 5, 0, &secs);
+		ret = kstrtouint(buf + 5, 0, &secs);
 		if (ret < 0)
 			goto out;
+
+		msecs = secs * MSEC_PER_SEC;
+		if (msecs > UINT_MAX)
+			msecs = UINT_MAX;
+
 		stop_scan_thread();
-		if (secs) {
-			jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
+		if (msecs) {
+			WRITE_ONCE(jiffies_scan_wait, msecs_to_jiffies(msecs));
 			start_scan_thread();
 		}
 	} else if (strncmp(buf, "scan", 4) == 0)
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm/kmemleak: fix the possible wrong memory scanning period
  2021-06-13 17:40 [PATCH] mm/kmemleak: fix the possible wrong memory scanning period Yanfei Xu
@ 2021-06-15  9:35 ` Catalin Marinas
  0 siblings, 0 replies; 2+ messages in thread
From: Catalin Marinas @ 2021-06-15  9:35 UTC (permalink / raw)
  To: Yanfei Xu; +Cc: akpm, linux-mm, linux-kernel

On Mon, Jun 14, 2021 at 01:40:22AM +0800, Yanfei Xu wrote:
> This commit contains 3 modifies:
> 
> 1.Convert the type of jiffies_scan_wait to "unsigned long".
> 
> 2.Use READ/WRITE_ONCE() for accessing "jiffies_scan_wait".
> 
> 3.Fix the possible wrong memory scanning period. If you set a large
> memory scanning period like blow, then the "secs" variable will be
> non-zero, however the value of "jiffies_scan_wait" will be zero.
> 
>     echo "scan=0x10000000" > /sys/kernel/debug/kmemleak
> 
> It is because the type of the msecs_to_jiffies()'s parameter is
> "unsigned int", and the "secs * 1000" is larger than its max value.
> This in turn leads a unexpected jiffies_scan_wait, maybe zero. We
> corret it by replacing kstrtoul() with kstrtouint(), and check the
> msecs to prevent it larger than UINT_MAX.
> 
> Signed-off-by: Yanfei Xu <yanfei.xu@windriver.com>

Acked-by: Catalin Marinas <catalin.marinas@arm.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-15  9:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-13 17:40 [PATCH] mm/kmemleak: fix the possible wrong memory scanning period Yanfei Xu
2021-06-15  9:35 ` Catalin Marinas

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.