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
383 unsigned long highest_vm_end; /* highest vma end address */383 unsigned long highest_vm_end; /* highest vma end address */
384 pgd_t * pgd;384 pgd_t * pgd;
385 385
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
386 /**396 /**
387 * @mm_users: The number of users including userspace.397 * @mm_users: The number of users including userspace.
388 *398 *
@@ -452,9 +462,7 @@ struct mm_struct { · 63 unmodified lines
452 unsigned long flags; /* Must use atomic bitops to access */462 unsigned long flags; /* Must use atomic bitops to access */
453 463
454 struct core_state *core_state; /* coredumping support */464 struct core_state *core_state; /* coredumping support */
455#ifdef CONFIG_MEMBARRIER465
456 atomic_t membarrier_state;
457#endif
458#ifdef CONFIG_AIO466#ifdef CONFIG_AIO
459 spinlock_t ioctx_lock;467 spinlock_t ioctx_lock;
460 struct kioctx_table __rcu *ioctx_table;468 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
370 sync_core_before_usermode();370 sync_core_before_usermode();
371}371}
372 372
373static inline void membarrier_execve(struct task_struct *t)373extern void membarrier_exec_mmap(struct mm_struct *mm);
374{374
375 atomic_set(&t->mm->membarrier_state, 0);
376}
377#else375#else
378#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS376#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
379static inline void membarrier_arch_switch_mm(struct mm_struct *prev,377static 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
382{380{
383}381}
384#endif382#endif
385static inline void membarrier_execve(struct task_struct *t)383static inline void membarrier_exec_mmap(struct mm_struct *mm)
386{384{
387}385}
388static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)386static 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