Skip to contentSkip to conversation

locktorture: Fix memory leak in param_set_cpumask()

PATCH Superseded Wang Liang 3 participants 11 messages Sep 12, 2025 – Sep 23, 2025 Torture-test modules

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wang Liang

author
#0 lore raw

PATCH 1/1 locktorture: Fix memory leak in param_set_cpumask()

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):
comm "bash", pid 323, jiffies 4295059233
hex dump (first 8 bytes):

    07 00 00 00 00 00 00 00                          ........
  backtrace (crc ac50919):
    __kmalloc_node_noprof+0x2e5/0x420
    alloc_cpumask_var_node+0x1f/0x30
    param_set_cpumask+0x26/0xb0 [locktorture]
    param_attr_store+0x93/0x100
    module_attr_store+0x1b/0x30
    kernfs_fop_write_iter+0x114/0x1b0
    vfs_write+0x300/0x410
    ksys_write+0x60/0xd0
    do_syscall_64+0xa4/0x260
    entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
 kernel/locking/locktorture.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +9 -0
index ce0362f0a871..cad80c050502 100644
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
 	int ret;
 	char *s;
 
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;
+
 	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
 		s = "Out of memory";
 		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
 			cxt.cur_ops->exit();
 		cxt.init_called = false;
 	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;
+
 	torture_cleanup_end();
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 46 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):
comm "bash", pid 323, jiffies 4295059233
hex dump (first 8 bytes):

    07 00 00 00 00 00 00 00                          ........
  backtrace (crc ac50919):
    __kmalloc_node_noprof+0x2e5/0x420
    alloc_cpumask_var_node+0x1f/0x30
    param_set_cpumask+0x26/0xb0 [locktorture]
    param_attr_store+0x93/0x100
    module_attr_store+0x1b/0x30
    kernfs_fop_write_iter+0x114/0x1b0
    vfs_write+0x300/0x410
    ksys_write+0x60/0xd0
    do_syscall_64+0xa4/0x260
    entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
 kernel/locking/locktorture.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +0 -0
index ce0362f0a871..cad80c050502 100644

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 74

Quoted 5 lines
kernel/locking/locktorture.c +2 -0
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
 	int ret;
 	char *s;
 
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 13 lines
kernel/locking/locktorture.c +6 -0
+
 	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
 		s = "Out of memory";
 		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
 			cxt.cur_ops->exit();
 		cxt.init_called = false;
 	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

Quoted 4 lines
kernel/locking/locktorture.c +1 -0
+
 	torture_cleanup_end();
 }
 
author
#2 lore raw
Quoted 51 lines · Zhang Changzhong
Quoted 49 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

   comm "bash", pid 323, jiffies 4295059233
   hex dump (first 8 bytes):
     07 00 00 00 00 00 00 00                          ........
   backtrace (crc ac50919):
     __kmalloc_node_noprof+0x2e5/0x420
     alloc_cpumask_var_node+0x1f/0x30
     param_set_cpumask+0x26/0xb0 [locktorture]
     param_attr_store+0x93/0x100
     module_attr_store+0x1b/0x30
     kernfs_fop_write_iter+0x114/0x1b0
     vfs_write+0x300/0x410
     ksys_write+0x60/0xd0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
  kernel/locking/locktorture.c | 9 +++++++++
  1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +0 -0
index ce0362f0a871..cad80c050502 100644
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  	int ret;
  	char *s;
 
Quoted 3 lines

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 74

Quoted 2 lines
kernel/locking/locktorture.c +2 -0
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Setting global pointer to NULL after free may be more safe. ^-^

Quoted 19 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 13 lines
kernel/locking/locktorture.c +6 -0
+
  	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  		s = "Out of memory";
  		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
  			cxt.cur_ops->exit();
  		cxt.init_called = false;
  	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

Quoted 4 lines
kernel/locking/locktorture.c +1 -0
+
  	torture_cleanup_end();
  }
 
maintainer
#3 lore raw
Quoted 45 lines · Wang Liang
Quoted 43 lines · Zhang Changzhong
Quoted 41 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

   comm "bash", pid 323, jiffies 4295059233
   hex dump (first 8 bytes):
     07 00 00 00 00 00 00 00                          ........
   backtrace (crc ac50919):
     __kmalloc_node_noprof+0x2e5/0x420
     alloc_cpumask_var_node+0x1f/0x30
     param_set_cpumask+0x26/0xb0 [locktorture]
     param_attr_store+0x93/0x100
     module_attr_store+0x1b/0x30
     kernfs_fop_write_iter+0x114/0x1b0
     vfs_write+0x300/0x410
     ksys_write+0x60/0xd0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
  kernel/locking/locktorture.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 10 lines
Quoted 10 lines

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 9 lines
kernel/locking/locktorture.c +2 -0
index ce0362f0a871..cad80c050502 100644
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  	int ret;
  	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
  	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  		s = "Out of memory";
  		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
  			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
  		cxt.init_called = false;
  	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
  	torture_cleanup_end();
  }
author
#4 lore raw
Quoted 54 lines · Paul E. McKenney
Quoted 43 lines · Wang Liang
Quoted 42 lines · Zhang Changzhong
Quoted 41 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

    comm "bash", pid 323, jiffies 4295059233
    hex dump (first 8 bytes):
      07 00 00 00 00 00 00 00                          ........
    backtrace (crc ac50919):
      __kmalloc_node_noprof+0x2e5/0x420
      alloc_cpumask_var_node+0x1f/0x30
      param_set_cpumask+0x26/0xb0 [locktorture]
      param_attr_store+0x93/0x100
      module_attr_store+0x1b/0x30
      kernfs_fop_write_iter+0x114/0x1b0
      vfs_write+0x300/0x410
      ksys_write+0x60/0xd0
      do_syscall_64+0xa4/0x260
      entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
   kernel/locking/locktorture.c | 9 +++++++++
   1 file changed, 9 insertions(+)
Quoted 10 lines
Quoted 10 lines

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 9 lines
kernel/locking/locktorture.c +2 -0
index ce0362f0a871..cad80c050502 100644
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
   	int ret;
   	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 3 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Thanks for your replies!

The free_cpumask_var() in param_set_cpumask() may be necessary(). If user set the parameter for two times, the pointer will point to a new memory, and no one hold the old memory, which trigger a memory leak.

Quoted 17 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
   	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
   		s = "Out of memory";
   		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
   			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
   		cxt.init_called = false;
   	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

Quoted 7 lines

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

For CONFIG_CPUMASK_OFFSTACK=n, the NULLing may be not appropriate. I will remove it.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
   	torture_cleanup_end();
   }
maintainer
#5 lore raw
Quoted 60 lines · Wang Liang
Quoted 54 lines · Paul E. McKenney
Quoted 43 lines · Wang Liang
Quoted 42 lines · Zhang Changzhong
Quoted 41 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

    comm "bash", pid 323, jiffies 4295059233
    hex dump (first 8 bytes):
      07 00 00 00 00 00 00 00                          ........
    backtrace (crc ac50919):
      __kmalloc_node_noprof+0x2e5/0x420
      alloc_cpumask_var_node+0x1f/0x30
      param_set_cpumask+0x26/0xb0 [locktorture]
      param_attr_store+0x93/0x100
      module_attr_store+0x1b/0x30
      kernfs_fop_write_iter+0x114/0x1b0
      vfs_write+0x300/0x410
      ksys_write+0x60/0xd0
      do_syscall_64+0xa4/0x260
      entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
   kernel/locking/locktorture.c | 9 +++++++++
   1 file changed, 9 insertions(+)
Quoted 10 lines
Quoted 10 lines

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 9 lines
kernel/locking/locktorture.c +2 -0
index ce0362f0a871..cad80c050502 100644
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
   	int ret;
   	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 3 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Quoted 5 lines

Thanks for your replies!

The free_cpumask_var() in param_set_cpumask() may be necessary(). If user set the parameter for two times, the pointer will point to a new memory, and no one hold the old memory, which trigger a memory leak.

Why wouldn't the free_cpumask_var() you are adding to lock_torture_cleanup() cover that case?

Quoted 17 lines
Quoted 17 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 14 lines
Quoted 14 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 13 lines
kernel/locking/locktorture.c +6 -0
+
   	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
   		s = "Out of memory";
   		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
   			cxt.cur_ops->exit();
   		cxt.init_called = false;
   	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

Quoted 8 lines
Quoted 5 lines

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

For CONFIG_CPUMASK_OFFSTACK=n, the NULLing may be not appropriate. I will remove it.

But if you remove the NULLing entirely, mightn't that inconvenience people debugging?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
   	torture_cleanup_end();
   }
#6 lore raw
In reply to Paul E. McKenney
Quoted 57 lines · Paul E. McKenney
Quoted 45 lines · Wang Liang
Quoted 43 lines · Zhang Changzhong
Quoted 41 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

   comm "bash", pid 323, jiffies 4295059233
   hex dump (first 8 bytes):
     07 00 00 00 00 00 00 00                          ........
   backtrace (crc ac50919):
     __kmalloc_node_noprof+0x2e5/0x420
     alloc_cpumask_var_node+0x1f/0x30
     param_set_cpumask+0x26/0xb0 [locktorture]
     param_attr_store+0x93/0x100
     module_attr_store+0x1b/0x30
     kernfs_fop_write_iter+0x114/0x1b0
     vfs_write+0x300/0x410
     ksys_write+0x60/0xd0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
  kernel/locking/locktorture.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 10 lines
Quoted 10 lines

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 9 lines
kernel/locking/locktorture.c +2 -0
index ce0362f0a871..cad80c050502 100644
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  	int ret;
  	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 4 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Sorry, I used Chinese by mistake—I didn't notice this was a public thread.

With CONFIG_CPUMASK_OFFSTACK=y, the free_cpumask_var() here seems necessary, when param_set_cpumask() called multiple times, 'cm_bind' gets overwritten, and the free_cpumask_var() in lock_torture_cleanup() cannot free the old memory.

Quoted 31 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
  	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  		s = "Out of memory";
  		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
  			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
  		cxt.init_called = false;
  	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
  	torture_cleanup_end();
  }
maintainer
#7 lore raw
Quoted 56 lines · Zhang Changzhong
Quoted 55 lines · Paul E. McKenney
Quoted 53 lines · Wang Liang
Quoted 52 lines · Zhang Changzhong
Quoted 46 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

   comm "bash", pid 323, jiffies 4295059233
   hex dump (first 8 bytes):
     07 00 00 00 00 00 00 00                          ........
   backtrace (crc ac50919):
     __kmalloc_node_noprof+0x2e5/0x420
     alloc_cpumask_var_node+0x1f/0x30
     param_set_cpumask+0x26/0xb0 [locktorture]
     param_attr_store+0x93/0x100
     module_attr_store+0x1b/0x30
     kernfs_fop_write_iter+0x114/0x1b0
     vfs_write+0x300/0x410
     ksys_write+0x60/0xd0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
  kernel/locking/locktorture.c | 9 +++++++++
  1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +0 -0
index ce0362f0a871..cad80c050502 100644

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 4 lines
kernel/locking/locktorture.c +2 -0
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  	int ret;
  	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 5 lines
Quoted 3 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Sorry, I used Chinese by mistake—I didn't notice this was a public thread.

Not a problem! There is always translation software, not that I ever will completely trust it. ;-)

Quoted 3 lines

With CONFIG_CPUMASK_OFFSTACK=y, the free_cpumask_var() here seems necessary, when param_set_cpumask() called multiple times, 'cm_bind' gets overwritten, and the free_cpumask_var() in lock_torture_cleanup() cannot free the old memory.

So the situation you are worried about is when the user mistakenly puts multiple copies of one of the locktorture.bind_{readers,writers} module parameters on the kernel boot command line or as a modprobe parameter?

If so, what do we really want to happen in that case? Do we want the last (say) locktorture.bind_readers value to win? Or do we want to OR together all such values?

Thanx, Paul

Quoted 32 lines
Quoted 31 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
  	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  		s = "Out of memory";
  		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
  			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
  		cxt.init_called = false;
  	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
  	torture_cleanup_end();
  }
#8 lore raw
Quoted 66 lines · Paul E. McKenney
Quoted 56 lines · Zhang Changzhong
Quoted 55 lines · Paul E. McKenney
Quoted 53 lines · Wang Liang
Quoted 52 lines · Zhang Changzhong
Quoted 46 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

   comm "bash", pid 323, jiffies 4295059233
   hex dump (first 8 bytes):
     07 00 00 00 00 00 00 00                          ........
   backtrace (crc ac50919):
     __kmalloc_node_noprof+0x2e5/0x420
     alloc_cpumask_var_node+0x1f/0x30
     param_set_cpumask+0x26/0xb0 [locktorture]
     param_attr_store+0x93/0x100
     module_attr_store+0x1b/0x30
     kernfs_fop_write_iter+0x114/0x1b0
     vfs_write+0x300/0x410
     ksys_write+0x60/0xd0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
  kernel/locking/locktorture.c | 9 +++++++++
  1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +0 -0
index ce0362f0a871..cad80c050502 100644

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 4 lines
kernel/locking/locktorture.c +2 -0
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  	int ret;
  	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 5 lines
Quoted 3 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Sorry, I used Chinese by mistake—I didn't notice this was a public thread.

Not a problem! There is always translation software, not that I ever will completely trust it. ;-)

Quoted 8 lines
Quoted 3 lines

With CONFIG_CPUMASK_OFFSTACK=y, the free_cpumask_var() here seems necessary, when param_set_cpumask() called multiple times, 'cm_bind' gets overwritten, and the free_cpumask_var() in lock_torture_cleanup() cannot free the old memory.

So the situation you are worried about is when the user mistakenly puts multiple copies of one of the locktorture.bind_{readers,writers} module parameters on the kernel boot command line or as a modprobe parameter?

I didn't consider this situation. What I noticed is that bind_{readers,writers} are writable interface, and fuzz testing tools like syzkaller can easily write to /sys/module/locktorture/parameters/bind_{readers,writers} and trigger memory leak.

In this case, the patch fixes the memory leak issue, but the old parameters remain in effect instead of the newly written ones. Considering that writing to this interface after modprobe has no real effect, how about set the permissions to 0444?

Quoted 3 lines

If so, what do we really want to happen in that case? Do we want the last (say) locktorture.bind_readers value to win? Or do we want to OR together all such values?

In the case you mentioned, it seems more reasonable that the last locktorture.bind_readers wins, which is also the current behavior.

Quoted 36 lines

Thanx, Paul

Quoted 32 lines
Quoted 31 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
  	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  		s = "Out of memory";
  		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
  			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
  		cxt.init_called = false;
  	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
  	torture_cleanup_end();
  }
maintainer
#9 lore raw
Quoted 81 lines · Zhang Changzhong
Quoted 66 lines · Paul E. McKenney
Quoted 56 lines · Zhang Changzhong
Quoted 55 lines · Paul E. McKenney
Quoted 53 lines · Wang Liang
Quoted 52 lines · Zhang Changzhong
Quoted 46 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

   comm "bash", pid 323, jiffies 4295059233
   hex dump (first 8 bytes):
     07 00 00 00 00 00 00 00                          ........
   backtrace (crc ac50919):
     __kmalloc_node_noprof+0x2e5/0x420
     alloc_cpumask_var_node+0x1f/0x30
     param_set_cpumask+0x26/0xb0 [locktorture]
     param_attr_store+0x93/0x100
     module_attr_store+0x1b/0x30
     kernfs_fop_write_iter+0x114/0x1b0
     vfs_write+0x300/0x410
     ksys_write+0x60/0xd0
     do_syscall_64+0xa4/0x260
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
  kernel/locking/locktorture.c | 9 +++++++++
  1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +0 -0
index ce0362f0a871..cad80c050502 100644

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 4 lines
kernel/locking/locktorture.c +2 -0
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  	int ret;
  	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 5 lines
Quoted 3 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Sorry, I used Chinese by mistake—I didn't notice this was a public thread.

Not a problem! There is always translation software, not that I ever will completely trust it. ;-)

Quoted 8 lines
Quoted 3 lines

With CONFIG_CPUMASK_OFFSTACK=y, the free_cpumask_var() here seems necessary, when param_set_cpumask() called multiple times, 'cm_bind' gets overwritten, and the free_cpumask_var() in lock_torture_cleanup() cannot free the old memory.

So the situation you are worried about is when the user mistakenly puts multiple copies of one of the locktorture.bind_{readers,writers} module parameters on the kernel boot command line or as a modprobe parameter?

I didn't consider this situation. What I noticed is that bind_{readers,writers} are writable interface, and fuzz testing tools like syzkaller can easily write to /sys/module/locktorture/parameters/bind_{readers,writers} and trigger memory leak.

Quoted 4 lines

In this case, the patch fixes the memory leak issue, but the old parameters remain in effect instead of the newly written ones. Considering that writing to this interface after modprobe has no real effect, how about set the permissions to 0444?

That sounds like a most excellent approach.

Quoted 6 lines
Quoted 3 lines

If so, what do we really want to happen in that case? Do we want the last (say) locktorture.bind_readers value to win? Or do we want to OR together all such values?

In the case you mentioned, it seems more reasonable that the last locktorture.bind_readers wins, which is also the current behavior.

In which case, what has to happen to handle something like this?

modprobe locktorture bind_readers=8
rmmod locktorture
modprobe locktorture bind_readers=5

Doesn't this require careful handling of the bind_readers variable in lock_torture_cleanup(), for both possible settings of the CPUMASK_OFFSTACK Kconfig option?

Thanx, Paul

Quoted 34 lines
Quoted 33 lines
Quoted 32 lines
Quoted 31 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
  	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  		s = "Out of memory";
  		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
  			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
  		cxt.init_called = false;
  	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
  	torture_cleanup_end();
  }
author
#10 lore raw
Quoted 94 lines · Paul E. McKenney
Quoted 76 lines · Zhang Changzhong
Quoted 63 lines · Paul E. McKenney
Quoted 55 lines · Zhang Changzhong
Quoted 54 lines · Paul E. McKenney
Quoted 53 lines · Wang Liang
Quoted 52 lines · Zhang Changzhong
Quoted 46 lines · Wang Liang

When setting the locktorture module parameter 'bind_writers', the variable 'cpumask_var_t bind_writers' is allocated in param_set_cpumask(). But it is not freed, when removing module or setting the parameter again.

Below kmemleak trace is seen for this issue:

unreferenced object 0xffff888100aabff8 (size 8):

    comm "bash", pid 323, jiffies 4295059233
    hex dump (first 8 bytes):
      07 00 00 00 00 00 00 00                          ........
    backtrace (crc ac50919):
      __kmalloc_node_noprof+0x2e5/0x420
      alloc_cpumask_var_node+0x1f/0x30
      param_set_cpumask+0x26/0xb0 [locktorture]
      param_attr_store+0x93/0x100
      module_attr_store+0x1b/0x30
      kernfs_fop_write_iter+0x114/0x1b0
      vfs_write+0x300/0x410
      ksys_write+0x60/0xd0
      do_syscall_64+0xa4/0x260
      entry_SYSCALL_64_after_hwframe+0x77/0x7f

This issue can be reproduced by:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers rmmod locktorture

or:
insmod locktorture.ko
echo 0-2 > /sys/module/locktorture/parameters/bind_writers echo 0-2 > /sys/module/locktorture/parameters/bind_writers

The parameter 'bind_readers' also has the same problem. Free the memory when removing module or setting the parameter.

Fixes 73e341242483 ("locktorture: Add readers_bind and writers_bind module parameters")
---
   kernel/locking/locktorture.c | 9 +++++++++
   1 file changed, 9 insertions(+)
kernel/locking/locktorture.c +0 -0
index ce0362f0a871..cad80c050502 100644

kernel/locking/locktorture.c static int param_set_cpumask(const char *val, const struct kernel_param *kp) · line 73

Quoted 4 lines
kernel/locking/locktorture.c +2 -0
@@ -70,6 +70,9 @@ static int param_set_cpumask(const char *val, const struct kernel_param *kp)
   	int ret;
   	char *s;
+	free_cpumask_var(*cm_bind);
+	*cm_bind = NULL;

这个NULL没必要吧

Quoted 4 lines
Quoted 3 lines

Assuming this translates to "This NULL is unnecessary", I have to agree with Zhang Changzhong. I would go further and argue that the free_cpumask_var() is also unnecessary here.

Sorry, I used Chinese by mistake—I didn't notice this was a public thread.

Not a problem! There is always translation software, not that I ever will completely trust it. ;-)

Quoted 7 lines
Quoted 3 lines

With CONFIG_CPUMASK_OFFSTACK=y, the free_cpumask_var() here seems necessary, when param_set_cpumask() called multiple times, 'cm_bind' gets overwritten, and the free_cpumask_var() in lock_torture_cleanup() cannot free the old memory.

So the situation you are worried about is when the user mistakenly puts multiple copies of one of the locktorture.bind_{readers,writers} module parameters on the kernel boot command line or as a modprobe parameter?

I didn't consider this situation. What I noticed is that bind_{readers,writers} are writable interface, and fuzz testing tools like syzkaller can easily write to /sys/module/locktorture/parameters/bind_{readers,writers} and trigger memory leak.

Quoted 4 lines

In this case, the patch fixes the memory leak issue, but the old parameters remain in effect instead of the newly written ones. Considering that writing to this interface after modprobe has no real effect, how about set the permissions to 0444?

That sounds like a most excellent approach.

Quoted 5 lines
Quoted 3 lines

If so, what do we really want to happen in that case? Do we want the last (say) locktorture.bind_readers value to win? Or do we want to OR together all such values?

In the case you mentioned, it seems more reasonable that the last locktorture.bind_readers wins, which is also the current behavior.

In which case, what has to happen to handle something like this?

modprobe locktorture bind_readers=8
rmmod locktorture
modprobe locktorture bind_readers=5

Quoted 5 lines

Doesn't this require careful handling of the bind_readers variable in lock_torture_cleanup(), for both possible settings of the CPUMASK_OFFSTACK Kconfig option?

Thanx, Paul

@Paul @Zhang thanks for all your suggestions!

Setting the parameter permissions to 0444 is a good approach, so user can't change the parameters and not need to call free_cpumask_var() in param_set_cpumask().

The free_cpumask_var() is still need to free bind_readers/bind_writers.

In below case, the 'bind_readers' is freed by rmmod. When run modprobe at the second time, the 'bind_readers' is reinitialized to NULL before allocated in param_set_cpumask().

    modprobe locktorture bind_readers=8
    rmmod locktorture
    modprobe locktorture bind_readers=5

So no need to NULLing bind_readers, the final patch like this:

kernel/locking/locktorture.c +0 -0
index ce0362f0a871..6567e5eeacc0 100644
@@ -103,8 +103,8 @@ static const struct kernel_param_ops lt_bind_ops = {

        .get = param_get_cpumask,
 };

-module_param_cb(bind_readers, &lt_bind_ops, &bind_readers, 0644); -module_param_cb(bind_writers, &lt_bind_ops, &bind_writers, 0644); +module_param_cb(bind_readers, &lt_bind_ops, &bind_readers, 0444); +module_param_cb(bind_writers, &lt_bind_ops, &bind_writers, 0444);

 long torture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask, bool dowarn);

@@ -1211,6 +1211,10 @@ static void lock_torture_cleanup(void) cxt.cur_ops->exit(); cxt.init_called = false;
        }
+
+       free_cpumask_var(bind_readers);
+       free_cpumask_var(bind_writers);
+
        torture_cleanup_end();
 }

------
Best regards
Wang Liang

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 1 line

Setting global pointer to NULL after free may be more safe. ^-^

In lock_torture_cleanup(), you mean? I would agree with that.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/locking/locktorture.c +1 -0
+
   	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
   		s = "Out of memory";
   		ret = -ENOMEM;
@@ -1211,6 +1214,12 @@ static void lock_torture_cleanup(void)
   			cxt.cur_ops->exit();
Quoted 8 lines
Quoted 8 lines

kernel/locking/locktorture.c static void lock_torture_cleanup(void) · line 1221

Quoted 7 lines
kernel/locking/locktorture.c +5 -0
   		cxt.init_called = false;
   	}
+
+	free_cpumask_var(bind_readers);
+	free_cpumask_var(bind_writers);
+	bind_readers = NULL;
+	bind_writers = NULL;

同上

But here I agree with Wang Liang, as it helps people running debuggers on the kernel. Instead of a dangling pointer, they see a NULL pointer.

Except... Is this NULLing really the right thing to do for CONFIG_CPUMASK_OFFSTACK=n kernels?

Thanx, Paul

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
kernel/locking/locktorture.c +1 -0
+
   	torture_cleanup_end();
   }