Skip to contentSkip to conversation

mm, vmstat: Make sure mutex is a global static

PATCH In review Kees Cook 4 participants 6 messages Nov 7, 2017 – Nov 9, 2017

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 mm, vmstat: Make sure mutex is a global static

The mutex in sysctl_vm_numa_stat_handler() needs to be a global static, not a stack variable, otherwise it doesn't serve any purpose. Also, reading the file with CONFIG_LOCKDEP=y will complain:

[   63.258593] INFO: trying to register non-static key.
[   63.259113] the code is fine but needs lockdep annotation.
[   63.259596] turning off the locking correctness validator.
[   63.260073] CPU: 1 PID: 4102 Comm: perl Not tainted 4.14.0-rc8-next-20171107+ #419
[   63.260769] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014
[   63.261570] Call Trace:
[   63.261783]  dump_stack+0x5f/0x86
[   63.262062]  register_lock_class+0xe4/0x550
[   63.262408]  ? __lock_acquire+0x308/0x1170
[   63.262746]  __lock_acquire+0x7e/0x1170
[   63.263063]  lock_acquire+0x9d/0x1d0
[   63.263363]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.263777]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264192]  __mutex_lock+0xb8/0x9a0
[   63.264488]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264942]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265398]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265840]  sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.266270]  proc_sys_call_handler+0xe3/0x100
[   63.266655]  __vfs_read+0x33/0x1b0
[   63.266957]  vfs_read+0xa6/0x150
[   63.267244]  SyS_read+0x55/0xc0
[   63.267525]  do_syscall_64+0x56/0x140
[   63.267850]  entry_SYSCALL64_slow_path+0x25/0x25
Fixes 920d5f77d1a25 ("mm, sysctl: make NUMA stats configurable")
Cc Jesper Dangaard Brouer <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ying Huang <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/vmstat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vmstat.c +2 -1
index e0593434fd58..40b2db6db6b1 100644
@@ -72,11 +72,12 @@ static void invalid_numa_statistics(void)
 	zero_global_numa_counters();
 }
 
+static DEFINE_MUTEX(vm_numa_stat_lock);
+
 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *length, loff_t *ppos)
 {
 	int ret, oldval;
-	DEFINE_MUTEX(vm_numa_stat_lock);
 
 	mutex_lock(&vm_numa_stat_lock);
 	if (write)
Signature · 6 lines
2.7.4


-- 
Kees Cook
Pixel Security
#1 lore raw
Quoted 3 lines · Kees Cook

The mutex in sysctl_vm_numa_stat_handler() needs to be a global static, not a stack variable, otherwise it doesn't serve any purpose. Also, reading the file with CONFIG_LOCKDEP=y will complain:

Oops, good catch.

Quoted 25 lines
[   63.258593] INFO: trying to register non-static key.
[   63.259113] the code is fine but needs lockdep annotation.
[   63.259596] turning off the locking correctness validator.
[   63.260073] CPU: 1 PID: 4102 Comm: perl Not tainted 4.14.0-rc8-next-20171107+ #419
[   63.260769] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014
[   63.261570] Call Trace:
[   63.261783]  dump_stack+0x5f/0x86
[   63.262062]  register_lock_class+0xe4/0x550
[   63.262408]  ? __lock_acquire+0x308/0x1170
[   63.262746]  __lock_acquire+0x7e/0x1170
[   63.263063]  lock_acquire+0x9d/0x1d0
[   63.263363]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.263777]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264192]  __mutex_lock+0xb8/0x9a0
[   63.264488]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264942]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265398]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265840]  sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.266270]  proc_sys_call_handler+0xe3/0x100
[   63.266655]  __vfs_read+0x33/0x1b0
[   63.266957]  vfs_read+0xa6/0x150
[   63.267244]  SyS_read+0x55/0xc0
[   63.267525]  do_syscall_64+0x56/0x140
[   63.267850]  entry_SYSCALL64_slow_path+0x25/0x25
Quoted 1 line
Fixes 920d5f77d1a25 ("mm, sysctl: make NUMA stats configurable")

Note that this hash is specific to particular next-$DATE as mmotm is reimported each day.

Quoted 15 lines
Cc Jesper Dangaard Brouer <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ying Huang <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/vmstat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vmstat.c +0 -0
index e0593434fd58..40b2db6db6b1 100644

mm/vmstat.c static void invalid_numa_statistics(void) · line 79

Quoted 10 lines
mm/vmstat.c +2 -1
@@ -72,11 +72,12 @@ static void invalid_numa_statistics(void)
 	zero_global_numa_counters();
 }
 
+static DEFINE_MUTEX(vm_numa_stat_lock);
+
 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *length, loff_t *ppos)
 {
 	int ret, oldval;
-	DEFINE_MUTEX(vm_numa_stat_lock);

Yeah it was Michal who suggested scoping the mutex here instead of global scope, but I think he didn't mean to remove the 'static' qualifier, and we both missed that in the review :(
So the scope under sysctl_vm_numa_stat_handler() should be okay, just with the 'static' added.

Quoted 4 lines
mm/vmstat.c +0 -0
 	mutex_lock(&vm_numa_stat_lock);
 	if (write)
 
#2 lore raw
Quoted 1 line · Vlastimil Babka

On 11/07/2017 10:38 PM, Kees Cook wrote:

[...]

Quoted 5 lines
Quoted 5 lines
mm/vmstat.c +2 -0
+static DEFINE_MUTEX(vm_numa_stat_lock);
+
 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *length, loff_t *ppos)
 {
Quoted 8 lines

mm/vmstat.c static void invalid_numa_statistics(void) · line 79

Quoted 2 lines
mm/vmstat.c +0 -1
 	int ret, oldval;
-	DEFINE_MUTEX(vm_numa_stat_lock);

Yeah it was Michal who suggested scoping the mutex here instead of global scope, but I think he didn't mean to remove the 'static' qualifier, and we both missed that in the review :(
So the scope under sysctl_vm_numa_stat_handler() should be okay, just with the 'static' added.

exactly! Sorry that I've missed the missing static and thanks for catching that Kees.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#3 lore raw
In reply to Vlastimil Babka
Quoted 32 lines · Vlastimil Babka
Quoted 3 lines · Kees Cook

The mutex in sysctl_vm_numa_stat_handler() needs to be a global static, not a stack variable, otherwise it doesn't serve any purpose. Also, reading the file with CONFIG_LOCKDEP=y will complain:

Oops, good catch.

Quoted 25 lines
[   63.258593] INFO: trying to register non-static key.
[   63.259113] the code is fine but needs lockdep annotation.
[   63.259596] turning off the locking correctness validator.
[   63.260073] CPU: 1 PID: 4102 Comm: perl Not tainted 4.14.0-rc8-next-20171107+ #419
[   63.260769] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014
[   63.261570] Call Trace:
[   63.261783]  dump_stack+0x5f/0x86
[   63.262062]  register_lock_class+0xe4/0x550
[   63.262408]  ? __lock_acquire+0x308/0x1170
[   63.262746]  __lock_acquire+0x7e/0x1170
[   63.263063]  lock_acquire+0x9d/0x1d0
[   63.263363]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.263777]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264192]  __mutex_lock+0xb8/0x9a0
[   63.264488]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264942]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265398]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265840]  sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.266270]  proc_sys_call_handler+0xe3/0x100
[   63.266655]  __vfs_read+0x33/0x1b0
[   63.266957]  vfs_read+0xa6/0x150
[   63.267244]  SyS_read+0x55/0xc0
[   63.267525]  do_syscall_64+0x56/0x140
[   63.267850]  entry_SYSCALL64_slow_path+0x25/0x25
Quoted 4 lines
Quoted 1 line
Fixes 920d5f77d1a25 ("mm, sysctl: make NUMA stats configurable")

Note that this hash is specific to particular next-$DATE as mmotm is reimported each day.

Ah yes, duh. :)

Quoted 19 lines
Quoted 19 lines
Cc Jesper Dangaard Brouer <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ying Huang <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/vmstat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vmstat.c +0 -0
index e0593434fd58..40b2db6db6b1 100644
@@ -72,11 +72,12 @@ static void invalid_numa_statistics(void)
      zero_global_numa_counters();
 }
 
Quoted 13 lines

mm/vmstat.c static void invalid_numa_statistics(void) · line 79

Quoted 7 lines
mm/vmstat.c +2 -1
+static DEFINE_MUTEX(vm_numa_stat_lock);
+
 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
              void __user *buffer, size_t *length, loff_t *ppos)
 {
      int ret, oldval;
-     DEFINE_MUTEX(vm_numa_stat_lock);

Yeah it was Michal who suggested scoping the mutex here instead of global scope, but I think he didn't mean to remove the 'static' qualifier, and we both missed that in the review :(
So the scope under sysctl_vm_numa_stat_handler() should be okay, just with the 'static' added.

That part is a matter of taste, I guess. :) But yes, static is important.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw
In reply to Kees Cook
Quoted 4 lines · Kees Cook

The mutex in sysctl_vm_numa_stat_handler() needs to be a global static, not a stack variable, otherwise it doesn't serve any purpose. Also, reading the file with CONFIG_LOCKDEP=y will complain:

It's my mistake. Kees, thanks for catching it.

Quoted 55 lines
[   63.258593] INFO: trying to register non-static key.
[   63.259113] the code is fine but needs lockdep annotation.
[   63.259596] turning off the locking correctness validator.
[   63.260073] CPU: 1 PID: 4102 Comm: perl Not tainted 4.14.0-rc8-next-20171107+ #419
[   63.260769] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014
[   63.261570] Call Trace:
[   63.261783]  dump_stack+0x5f/0x86
[   63.262062]  register_lock_class+0xe4/0x550
[   63.262408]  ? __lock_acquire+0x308/0x1170
[   63.262746]  __lock_acquire+0x7e/0x1170
[   63.263063]  lock_acquire+0x9d/0x1d0
[   63.263363]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.263777]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264192]  __mutex_lock+0xb8/0x9a0
[   63.264488]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.264942]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265398]  ? sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.265840]  sysctl_vm_numa_stat_handler+0x8f/0x2d0
[   63.266270]  proc_sys_call_handler+0xe3/0x100
[   63.266655]  __vfs_read+0x33/0x1b0
[   63.266957]  vfs_read+0xa6/0x150
[   63.267244]  SyS_read+0x55/0xc0
[   63.267525]  do_syscall_64+0x56/0x140
[   63.267850]  entry_SYSCALL64_slow_path+0x25/0x25
Fixes 920d5f77d1a25 ("mm, sysctl: make NUMA stats configurable")
Cc Jesper Dangaard Brouer <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ying Huang <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/vmstat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vmstat.c +2 -1
index e0593434fd58..40b2db6db6b1 100644
@@ -72,11 +72,12 @@ static void invalid_numa_statistics(void)
 	zero_global_numa_counters();
 }
 
+static DEFINE_MUTEX(vm_numa_stat_lock);
+
 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
 		void __user *buffer, size_t *length, loff_t *ppos)
 {
 	int ret, oldval;
-	DEFINE_MUTEX(vm_numa_stat_lock);
 
 	mutex_lock(&vm_numa_stat_lock);
 	if (write)
#4 lore raw
In reply to Kees Cook
Quoted 2 lines · Kees Cook
Quoted 1 line · Vlastimil Babka

On 11/07/2017 10:38 PM, Kees Cook wrote:

[...]

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
mm/vmstat.c +2 -0
+static DEFINE_MUTEX(vm_numa_stat_lock);
+
 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
              void __user *buffer, size_t *length, loff_t *ppos)
 {
Quoted 8 lines
Quoted 2 lines
      int ret, oldval;

- DEFINE_MUTEX(vm_numa_stat_lock);

Yeah it was Michal who suggested scoping the mutex here instead of global scope, but I think he didn't mean to remove the 'static' qualifier, and we both missed that in the review :(
So the scope under sysctl_vm_numa_stat_handler() should be okay, just with the 'static' added.

Quoted 1 line

That part is a matter of taste, I guess. :) But yes, static is important.

The primary reason I prefer function scope lock is that it is less tempting to reuse the lock for something else that way. But this is hardly something to insist on. So I am ok with the file scope as well.

Signature · 2 lines
Michal Hocko
SUSE Labs