Skip to contentSkip to conversation

Fix: membarrier: racy access to p->mm in membarrier_global_expedited()

RFC PATCH In review Mathieu Desnoyers 4 participants 6 messages Jan 28, 2019

Subsystems (4)
lore
Share & tools
lore

Mathieu Desnoyers

author maintainer
#0 lore raw

RFC PATCH 1/1 Fix: membarrier: racy access to p->mm in membarrier_global_expedited()

Jann Horn identified a racy access to p->mm in the global expedited command of the membarrier system call.

The suggested fix is to hold the task_lock() around the accesses to p->mm and to the mm_struct membarrier_state field to guarantee the existence of the mm_struct.

CC Jann Horn <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Peter Zijlstra (Intel) <[email protected]>
CC Ingo Molnar <[email protected]>
CC Andrea Parri <[email protected]>
CC Andrew Hunter <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Avi Kivity <[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Boqun Feng <[email protected]>
CC Dave Watson <[email protected]>
CC David Sehr <[email protected]>
CC Greg Hackmann <[email protected]>
CC H. Peter Anvin <[email protected]>
CC Linus Torvalds <[email protected]>
CC Maged Michael <[email protected]>
CC Michael Ellerman <[email protected]>
CC Paul E. McKenney <[email protected]>
CC Paul Mackerras <[email protected]>
CC Russell King <[email protected]>
CC Will Deacon <[email protected]>
CC [email protected] # v4.16+
---
 kernel/sched/membarrier.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
kernel/sched/membarrier.c +21 -6
index 76e0eaf4654e..305fdcc4c5f7 100644
@@ -81,12 +81,27 @@ static int membarrier_global_expedited(void)
 
 		rcu_read_lock();
 		p = task_rcu_dereference(&cpu_rq(cpu)->curr);
-		if (p && p->mm && (atomic_read(&p->mm->membarrier_state) &
-				   MEMBARRIER_STATE_GLOBAL_EXPEDITED)) {
-			if (!fallback)
-				__cpumask_set_cpu(cpu, tmpmask);
-			else
-				smp_call_function_single(cpu, ipi_mb, NULL, 1);
+		/*
+		 * Skip this CPU if the runqueue's current task is NULL or if
+		 * it is a kernel thread.
+		 */
+		if (p && READ_ONCE(p->mm)) {
+			bool mm_match;
+
+			/*
+			 * Read p->mm and access membarrier_state while holding
+			 * the task lock to ensure existence of mm.
+			 */
+			task_lock(p);
+			mm_match = p->mm && (atomic_read(&p->mm->membarrier_state) &
+					     MEMBARRIER_STATE_GLOBAL_EXPEDITED);
+			task_unlock(p);
+			if (mm_match) {
+				if (!fallback)
+					__cpumask_set_cpu(cpu, tmpmask);
+				else
+					smp_call_function_single(cpu, ipi_mb, NULL, 1);
+			}
 		}
 		rcu_read_unlock();
 	}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 7 lines · Mathieu Desnoyers

Jann Horn identified a racy access to p->mm in the global expedited command of the membarrier system call.

The suggested fix is to hold the task_lock() around the accesses to p->mm and to the mm_struct membarrier_state field to guarantee the existence of the mm_struct.

Hmm. I think this is right. You shouldn't access another threads mm pointer without proper locking.

That said, we *could* make the mm_cachep be SLAB_TYPESAFE_BY_RCU, which would allow speculatively reading data off the mm pointer under RCU. It might not be the *right* mm if somebody just did an exit, but for things like this it shouldn't matter.

But if this is the only case that might care, it sounds like just doing the proper locking is the right approach.

Linus

#2 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 7 lines · Mathieu Desnoyers

Jann Horn identified a racy access to p->mm in the global expedited command of the membarrier system call.

The suggested fix is to hold the task_lock() around the accesses to p->mm and to the mm_struct membarrier_state field to guarantee the existence of the mm_struct.

Quoted 7 lines

Hmm. I think this is right. You shouldn't access another threads mm pointer without proper locking.

That said, we *could* make the mm_cachep be SLAB_TYPESAFE_BY_RCU, which would allow speculatively reading data off the mm pointer under RCU. It might not be the *right* mm if somebody just did an exit, but for things like this it shouldn't matter.

That sounds much simpler and more effective than the contention-reduction approach that I suggested. ;-)

Thanx, Paul

Quoted 5 lines

But if this is the only case that might care, it sounds like just doing the proper locking is the right approach.

Linus

author maintainer
#3 lore raw
Quoted 14 lines · ----- On Jan 28, 2019, at 3:46 PM, paulmck [email protected] wrote:
Quoted 13 lines · Linus Torvalds
Quoted 7 lines · Mathieu Desnoyers

Jann Horn identified a racy access to p->mm in the global expedited command of the membarrier system call.

The suggested fix is to hold the task_lock() around the accesses to p->mm and to the mm_struct membarrier_state field to guarantee the existence of the mm_struct.

Hmm. I think this is right. You shouldn't access another threads mm pointer without proper locking.

Quoted 7 lines
Quoted 4 lines

That said, we *could* make the mm_cachep be SLAB_TYPESAFE_BY_RCU, which would allow speculatively reading data off the mm pointer under RCU. It might not be the *right* mm if somebody just did an exit, but for things like this it shouldn't matter.

That sounds much simpler and more effective than the contention-reduction approach that I suggested. ;-)

I'd be tempted to stick to the locking approach for a fix, and implement Linus' type-safe mm_cachep idea if anyone complains about the overhead of membarrier GLOBAL_EXPEDITED (and submit for a future merge window).

I tested the KASAN splat reproducer from Jann locally, and confirmed that my patch fixes the issue it reproduces.

Please let me know if the task_lock() approach is OK as a fix for now.

I'm also awaiting a Tested-by from Jann before submitting this for real.

Thanks,

Mathieu

Quoted 7 lines

Thanx, Paul

Quoted 4 lines

But if this is the only case that might care, it sounds like just doing the proper locking is the right approach.

Linus

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#4 lore raw
Quoted 24 lines · Mathieu Desnoyers
Quoted 14 lines · ----- On Jan 28, 2019, at 3:46 PM, paulmck [email protected] wrote:
Quoted 13 lines · Linus Torvalds
Quoted 7 lines · Mathieu Desnoyers

Jann Horn identified a racy access to p->mm in the global expedited command of the membarrier system call.

The suggested fix is to hold the task_lock() around the accesses to p->mm and to the mm_struct membarrier_state field to guarantee the existence of the mm_struct.

Hmm. I think this is right. You shouldn't access another threads mm pointer without proper locking.

Quoted 7 lines
Quoted 4 lines

That said, we *could* make the mm_cachep be SLAB_TYPESAFE_BY_RCU, which would allow speculatively reading data off the mm pointer under RCU. It might not be the *right* mm if somebody just did an exit, but for things like this it shouldn't matter.

That sounds much simpler and more effective than the contention-reduction approach that I suggested. ;-)

Quoted 8 lines

I'd be tempted to stick to the locking approach for a fix, and implement Linus' type-safe mm_cachep idea if anyone complains about the overhead of membarrier GLOBAL_EXPEDITED (and submit for a future merge window).

I tested the KASAN splat reproducer from Jann locally, and confirmed that my patch fixes the issue it reproduces.

Please let me know if the task_lock() approach is OK as a fix for now.

Agreed, no need for added complexity until there is a clear need.

Quoted 1 line

I'm also awaiting a Tested-by from Jann before submitting this for real.

Makes sense to me!

Thanx, Paul

Quoted 17 lines

Thanks,

Mathieu

Quoted 7 lines

Thanx, Paul

Quoted 4 lines

But if this is the only case that might care, it sounds like just doing the proper locking is the right approach.

Linus

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Tested-by on v1 1/1
#5 lore raw
In reply to Mathieu Desnoyers
Quoted 9 lines · Mathieu Desnoyers

Jann Horn identified a racy access to p->mm in the global expedited command of the membarrier system call.

The suggested fix is to hold the task_lock() around the accesses to p->mm and to the mm_struct membarrier_state field to guarantee the existence of the mm_struct.

The patch looks good to me, and to be sure, I've also given it a spin
- I can't trigger a splat anymore. You can add: