Skip to contentSkip to conversation

x86/mm: Change barriers before TLB flushes to smp_mb__after_atomic

PATCH In review Nadav Amit 3 participants 3 messages Jun 6, 2016 – Jul 15, 2016

Subsystems (4)
lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH 1/1 x86/mm: Change barriers before TLB flushes to smp_mb__after_atomic

When (current->active_mm != mm), flush_tlb_page() does not perform a memory barrier. In practice, this memory barrier is not needed since in the existing call-sites the PTE is modified using atomic-operations. This patch therefore modifies the existing smp_mb in flush_tlb_page to smp_mb__after_atomic and adds the missing one, while documenting the new assumption of flush_tlb_page.

In addition smp_mb__after_atomic is also added to set_tlb_ubc_flush_pending, since it makes a similar implicit assumption and omits the memory barrier.

---
 arch/x86/mm/tlb.c | 9 ++++++++-
 mm/rmap.c         | 3 +++
 2 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +8 -1
index fe9b9f7..2534333 100644
@@ -242,6 +242,10 @@ out:
 	preempt_enable();
 }
 
+/*
+ * Calls to flush_tlb_page must be preceded by atomic PTE change or
+ * explicit memory-barrier.
+ */
 void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
 {
 	struct mm_struct *mm = vma->vm_mm;
@@ -259,8 +263,11 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
 			leave_mm(smp_processor_id());
 
 			/* Synchronize with switch_mm. */
-			smp_mb();
+			smp_mb__after_atomic();
 		}
+	} else {
+		/* Synchronize with switch_mm. */
+		smp_mb__after_atomic();
 	}
 
 	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
mm/rmap.c +3 -0
index 307b555..60ab0fe 100644
@@ -613,6 +613,9 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
 {
 	struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
 
+	/* Synchronize with switch_mm. */
+	smp_mb__after_atomic();
+
 	cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm));
 	tlb_ubc->flush_required = true;
 
Signature · 7 lines
2.7.4

--
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 60 lines · Nadav Amit

When (current->active_mm != mm), flush_tlb_page() does not perform a memory barrier. In practice, this memory barrier is not needed since in the existing call-sites the PTE is modified using atomic-operations. This patch therefore modifies the existing smp_mb in flush_tlb_page to smp_mb__after_atomic and adds the missing one, while documenting the new assumption of flush_tlb_page.

In addition smp_mb__after_atomic is also added to set_tlb_ubc_flush_pending, since it makes a similar implicit assumption and omits the memory barrier.

---
 arch/x86/mm/tlb.c | 9 ++++++++-
 mm/rmap.c         | 3 +++
 2 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +8 -1
index fe9b9f7..2534333 100644
@@ -242,6 +242,10 @@ out:
        preempt_enable();
 }
 
+/*
+ * Calls to flush_tlb_page must be preceded by atomic PTE change or
+ * explicit memory-barrier.
+ */
 void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
 {
        struct mm_struct *mm = vma->vm_mm;
@@ -259,8 +263,11 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
                        leave_mm(smp_processor_id());
 
                        /* Synchronize with switch_mm. */
-                       smp_mb();
+                       smp_mb__after_atomic();
                }
+       } else {
+               /* Synchronize with switch_mm. */
+               smp_mb__after_atomic();
        }
 
        if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
mm/rmap.c +3 -1
index 307b555..60ab0fe 100644
@@ -613,6 +613,9 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
 {
        struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
 
+       /* Synchronize with switch_mm. */
+       smp_mb__after_atomic();
+
        cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm));
        tlb_ubc->flush_required = true;
 
--
Quoted 2 lines

2.7.4

This looks fine for x86, but I have no idea whether other architectures are okay with it. akpm? mm folks?

--
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 60 lines · Andy Lutomirski
Quoted 59 lines · Nadav Amit

When (current->active_mm != mm), flush_tlb_page() does not perform a memory barrier. In practice, this memory barrier is not needed since in the existing call-sites the PTE is modified using atomic-operations. This patch therefore modifies the existing smp_mb in flush_tlb_page to smp_mb__after_atomic and adds the missing one, while documenting the new assumption of flush_tlb_page.

In addition smp_mb__after_atomic is also added to set_tlb_ubc_flush_pending, since it makes a similar implicit assumption and omits the memory barrier.

---
arch/x86/mm/tlb.c | 9 ++++++++-
mm/rmap.c | 3 +++

2 files changed, 11 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +8 -1
index fe9b9f7..2534333 100644
@@ -242,6 +242,10 @@ out:
       preempt_enable();
 }
 
+/*
+ * Calls to flush_tlb_page must be preceded by atomic PTE change or
+ * explicit memory-barrier.
+ */
 void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
 {
       struct mm_struct *mm = vma->vm_mm;
@@ -259,8 +263,11 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
                       leave_mm(smp_processor_id());
 
                       /* Synchronize with switch_mm. */
-                       smp_mb();
+                       smp_mb__after_atomic();
               }
+       } else {
+               /* Synchronize with switch_mm. */
+               smp_mb__after_atomic();
       }
 
       if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
mm/rmap.c +3 -0
index 307b555..60ab0fe 100644
@@ -613,6 +613,9 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm,
 {
       struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
 
+       /* Synchronize with switch_mm. */
+       smp_mb__after_atomic();
+
       cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm));
       tlb_ubc->flush_required = true;
 
Quoted 5 lines
Quoted 2 lines

--
2.7.4

This looks fine for x86, but I have no idea whether other architectures are okay with it. akpm? mm folks?

Ping?

Note that this patch adds two missing barriers.

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>