Skip to contentSkip to conversation

mm: Prevent racy access to tlb_flush_pending

PATCH Superseded Nadav Amit 5 participants 10 messages Jul 18, 2017 – Jul 25, 2017

Subsystems (4)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH 1/1 mm: Prevent racy access to tlb_flush_pending

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Fixes 20841405940e ("mm: fix TLB flush race between migration, and

change_protection_range")

---
 include/linux/mm_types.h | 8 ++++----
 kernel/fork.c            | 2 +-
 mm/debug.c               | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mm_types.h +4 -4
index 45cdb27791a3..36f4ec589544 100644
@@ -493,7 +493,7 @@ struct mm_struct {
 	 * can move process memory needs to flush the TLB when moving a
 	 * PROT_NONE or PROT_NUMA mapped page.
 	 */
-	bool tlb_flush_pending;
+	atomic_t tlb_flush_pending;
 #endif
 	struct uprobes_state uprobes_state;
 #ifdef CONFIG_HUGETLB_PAGE
@@ -528,11 +528,11 @@ static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	return mm->tlb_flush_pending;
+	return atomic_read(&mm->tlb_flush_pending) > 0;
 }
 static inline void set_tlb_flush_pending(struct mm_struct *mm)
 {
-	mm->tlb_flush_pending = true;
+	atomic_inc(&mm->tlb_flush_pending);
 
 	/*
 	 * Guarantee that the tlb_flush_pending store does not leak into the
@@ -544,7 +544,7 @@ static inline void set_tlb_flush_pending(struct mm_struct *mm)
 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	mm->tlb_flush_pending = false;
+	atomic_dec(&mm->tlb_flush_pending);
 }
 #else
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
kernel/fork.c +1 -1
index e53770d2bf95..5a7ecfbb7420 100644
@@ -809,7 +809,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
 	mmu_notifier_mm_init(mm);
-	clear_tlb_flush_pending(mm);
+	atomic_set(&mm->tlb_flush_pending, 0);
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
 	mm->pmd_huge_pte = NULL;
 #endif
mm/debug.c +1 -1
index db1cd26d8752..d70103bb4731 100644
@@ -159,7 +159,7 @@ void dump_mm(const struct mm_struct *mm)
 		mm->numa_next_scan, mm->numa_scan_offset, mm->numa_scan_seq,
 #endif
 #if defined(CONFIG_NUMA_BALANCING) || defined(CONFIG_COMPACTION)
-		mm->tlb_flush_pending,
+		atomic_read(&mm->tlb_flush_pending),
 #endif
 		mm->def_flags, &mm->def_flags
 	);
Signature · 7 lines
2.11.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 10 lines · Nadav Amit

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

Quoted 4 lines

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

This thing is logically a refcount. Should it be refcount_t?

--Andy

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 11 lines · Andy Lutomirski
Quoted 10 lines · Nadav Amit

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

Quoted 6 lines
Quoted 4 lines

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

This thing is logically a refcount. Should it be refcount_t?

I don’t think so. refcount_inc() would WARN_ONCE if the counter is zero before the increase, although this is a valid scenario here.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 18 lines · Nadav Amit
Quoted 16 lines · Andy Lutomirski
Quoted 14 lines · Nadav Amit

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Quoted 5 lines
Quoted 1 line

This thing is logically a refcount. Should it be refcount_t?

I don’t think so. refcount_inc() would WARN_ONCE if the counter is zero before the increase, although this is a valid scenario here.

Hmm. Maybe a refcount that starts at 1? My point is that, if someone could force it to overflow, it would be bad. Maybe this isn't worth worrying about.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 19 lines · Andy Lutomirski
Quoted 18 lines · Nadav Amit
Quoted 16 lines · Andy Lutomirski
Quoted 14 lines · Nadav Amit

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This thing is logically a refcount. Should it be refcount_t?

I don’t think so. refcount_inc() would WARN_ONCE if the counter is zero before the increase, although this is a valid scenario here.

Hmm. Maybe a refcount that starts at 1? My point is that, if someone could force it to overflow, it would be bad. Maybe this isn't worth worrying about.

I don’t think it is a issue. At most you can have one task_numa_work() per core running in any given moment.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
In reply to Nadav Amit
Quoted 89 lines · Nadav Amit

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Fixes 20841405940e ("mm: fix TLB flush race between migration, and

change_protection_range")

---

include/linux/mm_types.h | 8 ++++----
kernel/fork.c            | 2 +-
mm/debug.c               | 2 +-
3 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mm_types.h +4 -4
index 45cdb27791a3..36f4ec589544 100644
@@ -493,7 +493,7 @@ struct mm_struct {
 	 * can move process memory needs to flush the TLB when moving a
 	 * PROT_NONE or PROT_NUMA mapped page.
 	 */
-	bool tlb_flush_pending;
+	atomic_t tlb_flush_pending;
 #endif
 	struct uprobes_state uprobes_state;
 #ifdef CONFIG_HUGETLB_PAGE
@@ -528,11 +528,11 @@ static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	return mm->tlb_flush_pending;
+	return atomic_read(&mm->tlb_flush_pending) > 0;
 }
 static inline void set_tlb_flush_pending(struct mm_struct *mm)
 {
-	mm->tlb_flush_pending = true;
+	atomic_inc(&mm->tlb_flush_pending);
 
 	/*
 	 * Guarantee that the tlb_flush_pending store does not leak into the
@@ -544,7 +544,7 @@ static inline void set_tlb_flush_pending(struct mm_struct *mm)
 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	mm->tlb_flush_pending = false;
+	atomic_dec(&mm->tlb_flush_pending);
 }
 #else
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
kernel/fork.c +1 -1
index e53770d2bf95..5a7ecfbb7420 100644
@@ -809,7 +809,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
 	mmu_notifier_mm_init(mm);
-	clear_tlb_flush_pending(mm);
+	atomic_set(&mm->tlb_flush_pending, 0);
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
 	mm->pmd_huge_pte = NULL;
 #endif
mm/debug.c +1 -1
index db1cd26d8752..d70103bb4731 100644
@@ -159,7 +159,7 @@ void dump_mm(const struct mm_struct *mm)
 		mm->numa_next_scan, mm->numa_scan_offset, mm->numa_scan_seq,
 #endif
 #if defined(CONFIG_NUMA_BALANCING) || defined(CONFIG_COMPACTION)
-		mm->tlb_flush_pending,
+		atomic_read(&mm->tlb_flush_pending),
 #endif
 		mm->def_flags, &mm->def_flags
 	);
Signature · 1 line
2.11.0

Andrew, are there any reservations regarding this patch (excluding those of Andy’s which I think I addressed)?

Thanks,
Nadav
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#6 lore raw
In reply to Nadav Amit
Quoted 10 lines · On Mon, 17 Jul 2017 11:02:46 -0700 Nadav Amit <[email protected]> wrote:

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

Quoted 8 lines

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Fixes 20841405940e ("mm: fix TLB flush race between migration, and

change_protection_range")

The changelog doesn't describe the user-visible effects of the bug (it should always do so, please). But it is presumably a data-corruption bug so I suggest that a -stable backport is warranted?

It has been there for 4 years so I'm thinking we can hold off a mainline (and hence -stable) merge until 4.13-rc1, yes?

One thought:

Quoted 20 lines
include/linux/mm_types.h +2 -2
 
...
@@ -528,11 +528,11 @@ static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	return mm->tlb_flush_pending;
+	return atomic_read(&mm->tlb_flush_pending) > 0;
 }
 static inline void set_tlb_flush_pending(struct mm_struct *mm)
 {
-	mm->tlb_flush_pending = true;
+	atomic_inc(&mm->tlb_flush_pending);
 
 	/*
 	 * Guarantee that the tlb_flush_pending store does not leak into the

include/linux/mm_types.h static inline void set_tlb_flush_pending(struct mm_struct *mm) · line 549

Quoted 7 lines
include/linux/mm_types.h +1 -1
@@ -544,7 +544,7 @@ static inline void set_tlb_flush_pending(struct mm_struct *mm)
 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	mm->tlb_flush_pending = false;
+	atomic_dec(&mm->tlb_flush_pending);
 }
 #else

Do we still need the barrier()s or is it OK to let the atomic op do that for us (with a suitable code comment).

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Mon, 17 Jul 2017 11:02:46 -0700 Nadav Amit <[email protected]> wrote:

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Quoted 6 lines
Quoted 2 lines
Fixes 20841405940e ("mm: fix TLB flush race between migration, and

change_protection_range")

The changelog doesn't describe the user-visible effects of the bug (it should always do so, please). But it is presumably a data-corruption bug so I suggest that a -stable backport is warranted?

Yes, although I did not encounter an actual memory corruption.

Quoted 24 lines

It has been there for 4 years so I'm thinking we can hold off a mainline (and hence -stable) merge until 4.13-rc1, yes?

One thought:

Quoted 17 lines
include/linux/mm_types.h +2 -2
 
...
@@ -528,11 +528,11 @@ static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	return mm->tlb_flush_pending;
+	return atomic_read(&mm->tlb_flush_pending) > 0;
 }
 static inline void set_tlb_flush_pending(struct mm_struct *mm)
 {
-	mm->tlb_flush_pending = true;
+	atomic_inc(&mm->tlb_flush_pending);
 
Quoted 13 lines

include/linux/mm_types.h static inline void set_tlb_flush_pending(struct mm_struct *mm) · line 549

Quoted 10 lines
include/linux/mm_types.h +1 -1
 	/*
 	 * Guarantee that the tlb_flush_pending store does not leak into the
@@ -544,7 +544,7 @@ static inline void set_tlb_flush_pending(struct mm_struct *mm)
 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	mm->tlb_flush_pending = false;
+	atomic_dec(&mm->tlb_flush_pending);
 }
 #else

Do we still need the barrier()s or is it OK to let the atomic op do that for us (with a suitable code comment).

I will submit v2. However, I really don’t understand the comment on mm_tlb_flush_pending():

/*
* Memory barriers to keep this state in sync are graciously provided by
* the page table locks, outside of which no page table modifications happen.
* The barriers below prevent the compiler from re-ordering the instructions
* around the memory barriers that are already present in the code. */

But IIUC migrate_misplaced_transhuge_page() does not call mm_tlb_flush_pending() while the ptl is taken.

Mel, can I bother you again? Should I move the flush in migrate_misplaced_transhuge_page() till after the ptl is taken?

Thanks,
Nadav

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 75 lines · Nadav Amit
Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Mon, 17 Jul 2017 11:02:46 -0700 Nadav Amit <[email protected]> wrote:

Setting and clearing mm->tlb_flush_pending can be performed by multiple threads, since mmap_sem may only be acquired for read in task_numa_work. If this happens, tlb_flush_pending may be cleared while one of the threads still changes PTEs and batches TLB flushes.

As a result, TLB flushes can be skipped because the indication of pending TLB flushes is lost, for instance due to race between migration and change_protection_range (just as in the scenario that caused the introduction of tlb_flush_pending).

The feasibility of such a scenario was confirmed by adding assertion to check tlb_flush_pending is not set by two threads, adding artificial latency in change_protection_range() and using sysctl to reduce kernel.numa_balancing_scan_delay_ms.

Quoted 6 lines
Quoted 2 lines
Fixes 20841405940e ("mm: fix TLB flush race between migration, and

change_protection_range")

The changelog doesn't describe the user-visible effects of the bug (it should always do so, please). But it is presumably a data-corruption bug so I suggest that a -stable backport is warranted?

Yes, although I did not encounter an actual memory corruption.

Quoted 23 lines

It has been there for 4 years so I'm thinking we can hold off a mainline (and hence -stable) merge until 4.13-rc1, yes?

One thought:

Quoted 17 lines
include/linux/mm_types.h +2 -2
 
...
@@ -528,11 +528,11 @@ static inline cpumask_t *mm_cpumask(struct mm_struct *mm)
 static inline bool mm_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	return mm->tlb_flush_pending;
+	return atomic_read(&mm->tlb_flush_pending) > 0;
 }
 static inline void set_tlb_flush_pending(struct mm_struct *mm)
 {
-	mm->tlb_flush_pending = true;
+	atomic_inc(&mm->tlb_flush_pending);
 
Quoted 13 lines

include/linux/mm_types.h static inline void set_tlb_flush_pending(struct mm_struct *mm) · line 549

Quoted 10 lines
include/linux/mm_types.h +1 -1
 	/*
 	 * Guarantee that the tlb_flush_pending store does not leak into the
@@ -544,7 +544,7 @@ static inline void set_tlb_flush_pending(struct mm_struct *mm)
 static inline void clear_tlb_flush_pending(struct mm_struct *mm)
 {
 	barrier();
-	mm->tlb_flush_pending = false;
+	atomic_dec(&mm->tlb_flush_pending);
 }
 #else

Do we still need the barrier()s or is it OK to let the atomic op do that for us (with a suitable code comment).

I will submit v2. However, I really don’t understand the comment on mm_tlb_flush_pending():

/*
* Memory barriers to keep this state in sync are graciously provided by
* the page table locks, outside of which no page table modifications happen.
* The barriers below prevent the compiler from re-ordering the instructions
* around the memory barriers that are already present in the code. */

Quoted 5 lines

But IIUC migrate_misplaced_transhuge_page() does not call mm_tlb_flush_pending() while the ptl is taken.

Mel, can I bother you again? Should I move the flush in migrate_misplaced_transhuge_page() till after the ptl is taken?

Oops: this would be obviously wrong since it would move it after

migrate_page_copy() is run. So I do need your advice whether the comment is wrong or the implementation.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

reviewer
#9 lore raw
In reply to Nadav Amit
Quoted 13 lines · Nadav Amit
Quoted 2 lines

Do we still need the barrier()s or is it OK to let the atomic op do that for us (with a suitable code comment).

I will submit v2. However, I really don???t understand the comment on mm_tlb_flush_pending():

/*
* Memory barriers to keep this state in sync are graciously provided by
* the page table locks, outside of which no page table modifications happen.
* The barriers below prevent the compiler from re-ordering the instructions
* around the memory barriers that are already present in the code. */

Quoted 6 lines

But IIUC migrate_misplaced_transhuge_page() does not call mm_tlb_flush_pending() while the ptl is taken.

Mel, can I bother you again? Should I move the flush in migrate_misplaced_transhuge_page() till after the ptl is taken?

The flush, if it's necessary, needs to happen before the copy. However, in this particular context it shouldn't matter. In this specific context, we must be dealing with a NUMA hinting fault which means the original PTE is PROT_NONE, flushed and no writes are possible. If a protection update happens during the copy in migrate_misplaced_transhuge_page then it'll be detected in migrate_misplaced_transhuge_page by the pmd_same check and the page copy was a waste of time but otherwise harmless.

Signature · 8 lines
Mel Gorman
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>