History

sched/membarrier: Fix p->mm->membarrier_state racy load

The membarrier_state field is located within the mm_struct, which is not guaranteed to exist when used from runqueue-lock-free iteration on runqueues by the membarrier system call. Copy the membarrier_state from the mm_struct into the scheduler runqueue when the scheduler switches between mm. When registering membarrier for mm, after setting the registration bit in the mm membarrier state, issue a synchronize_rcu() to ensure the scheduler observes the change. In order to take care of the case where a runqueue keeps executing the target mm without swapping to other mm, iterate over each runqueue and issue an IPI to copy the membarrier_state from the mm_struct into each runqueue which have the same mm which state has just been modified. Move the mm membarrier_state field closer to pgd in mm_struct to use a cache line already touched by the scheduler switch_mm. The membarrier_execve() (now membarrier_exec_mmap) hook now needs to clear the runqueue's membarrier state in addition to clear the mm membarrier state, so move its implementation into the scheduler membarrier code so it can access the runqueue structure. Add memory barrier in membarrier_exec_mmap() prior to clearing the membarrier state, ensuring memory accesses executed prior to exec are not reordered with the stores clearing the membarrier state. As suggested by Linus, move all membarrier.c RCU read-side locks outside of the for each cpu loops.

Mathieu Desnoyers committed 7 years ago

Commit 227a4aa1 parent 2840cf06 files · +183 −54Browse files

Showing the changes to include/linux (2 of 6 files). Show all

Unified · Split

2 files
  1. include/linux/mm_types.h +11 −3
  2. include/linux/sched/mm.h +3 −5
include/linux/mm_types.h+11−3
@@ -383,6 +383,16 @@ struct mm_struct { · 382 unmodified lines
383383 unsigned long highest_vm_end; /* highest vma end address */
384384 pgd_t * pgd;
385385
386+#ifdef CONFIG_MEMBARRIER
387+ /**
388+ * @membarrier_state: Flags controlling membarrier behavior.
389+ *
390+ * This field is close to @pgd to hopefully fit in the same
391+ * cache-line, which needs to be touched by switch_mm().
392+ */
393+ atomic_t membarrier_state;
394+#endif
395+
386396 /**
387397 * @mm_users: The number of users including userspace.
388398 *
@@ -452,9 +462,7 @@ struct mm_struct { · 63 unmodified lines
452462 unsigned long flags; /* Must use atomic bitops to access */
453463
454464 struct core_state *core_state; /* coredumping support */
455−#ifdef CONFIG_MEMBARRIER
456− atomic_t membarrier_state;
457−#endif
465+
458466 #ifdef CONFIG_AIO
459467 spinlock_t ioctx_lock;
460468 struct kioctx_table __rcu *ioctx_table;
include/linux/sched/mm.h+3−5
@@ -370,10 +370,8 @@ static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm) · 369 unmodified lines
370370 sync_core_before_usermode();
371371 }
372372
373−static inline void membarrier_execve(struct task_struct *t)
374−{
375− atomic_set(&t->mm->membarrier_state, 0);
376−}
373+extern void membarrier_exec_mmap(struct mm_struct *mm);
374+
377375 #else
378376 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
379377 static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
@@ -382,7 +380,7 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev, · 2 unmodified lines
382380 {
383381 }
384382 #endif
385−static inline void membarrier_execve(struct task_struct *t)
383+static inline void membarrier_exec_mmap(struct mm_struct *mm)
386384 {
387385 }
388386 static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t