Skip to content

Commit 618758e

Browse files
compudjPeter Zijlstra
authored andcommitted
sched: membarrier: cover kthread_use_mm (v4)
Add comments and memory barrier to kthread_use_mm and kthread_unuse_mm to allow the effect of membarrier(2) to apply to kthreads accessing user-space memory as well. Given that no prior kthread use this guarantee and that it only affects kthreads, adding this guarantee does not affect user-space ABI. Refine the check in membarrier_global_expedited to exclude runqueues running the idle thread rather than all kthreads from the IPI cpumask. Now that membarrier_global_expedited can IPI kthreads, the scheduler also needs to update the runqueue's membarrier_state when entering lazy TLB state. Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20201020134715.13909-3-mathieu.desnoyers@efficios.com
1 parent 5bc7850 commit 618758e

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎kernel/kthread.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,15 +1248,27 @@ void kthread_use_mm(struct mm_struct *mm)
12481248
tsk->active_mm = mm;
12491249
}
12501250
tsk->mm = mm;
1251+
membarrier_update_current_mm(mm);
12511252
switch_mm_irqs_off(active_mm, mm, tsk);
12521253
local_irq_enable();
12531254
task_unlock(tsk);
12541255
#ifdef finish_arch_post_lock_switch
12551256
finish_arch_post_lock_switch();
12561257
#endif
12571258

1259+
/*
1260+
* When a kthread starts operating on an address space, the loop
1261+
* in membarrier_{private,global}_expedited() may not observe
1262+
* that tsk->mm, and not issue an IPI. Membarrier requires a
1263+
* memory barrier after storing to tsk->mm, before accessing
1264+
* user-space memory. A full memory barrier for membarrier
1265+
* {PRIVATE,GLOBAL}_EXPEDITED is implicitly provided by
1266+
* mmdrop(), or explicitly with smp_mb().
1267+
*/
12581268
if (active_mm != mm)
12591269
mmdrop(active_mm);
1270+
else
1271+
smp_mb();
12601272

12611273
to_kthread(tsk)->oldfs = force_uaccess_begin();
12621274
}
@@ -1276,9 +1288,18 @@ void kthread_unuse_mm(struct mm_struct *mm)
12761288
force_uaccess_end(to_kthread(tsk)->oldfs);
12771289

12781290
task_lock(tsk);
1291+
/*
1292+
* When a kthread stops operating on an address space, the loop
1293+
* in membarrier_{private,global}_expedited() may not observe
1294+
* that tsk->mm, and not issue an IPI. Membarrier requires a
1295+
* memory barrier after accessing user-space memory, before
1296+
* clearing tsk->mm.
1297+
*/
1298+
smp_mb__after_spinlock();
12791299
sync_mm_rss(mm);
12801300
local_irq_disable();
12811301
tsk->mm = NULL;
1302+
membarrier_update_current_mm(NULL);
12821303
/* active_mm is still 'mm' */
12831304
enter_lazy_tlb(mm, tsk);
12841305
local_irq_enable();

‎kernel/sched/idle.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ void play_idle_precise(u64 duration_ns, u64 latency_ns)
338338
WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
339339
WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
340340
WARN_ON_ONCE(!duration_ns);
341+
WARN_ON_ONCE(current->mm);
341342

342343
rcu_sleep_check();
343344
preempt_disable();

‎kernel/sched/membarrier.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ static int membarrier_global_expedited(void)
126126
continue;
127127

128128
/*
129-
* Skip the CPU if it runs a kernel thread. The scheduler
130-
* leaves the prior task mm in place as an optimization when
131-
* scheduling a kthread.
129+
* Skip the CPU if it runs a kernel thread which is not using
130+
* a task mm.
132131
*/
133132
p = rcu_dereference(cpu_rq(cpu)->curr);
134-
if (p->flags & PF_KTHREAD)
133+
if (!p->mm)
135134
continue;
136135

137136
__cpumask_set_cpu(cpu, tmpmask);

0 commit comments

Comments
 (0)