Skip to contentSkip to conversation

powerpc/mm/cxl: Add barrier when setting mm cpumask

PATCH Superseded Aneesh Kumar K.V 3 participants 5 messages Aug 28, 2017 Char and misc drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/mm/cxl: Add barrier when setting mm cpumask

We need to add memory barrier so that the page table walk doesn't happen before the cpumask is set and made visible to the other cpus. We need to use a sync here instead of lwsync because lwsync is not sufficient for store/load ordering.

We also need to add an if (mm) check so that we do the right thing when called with a kernel context. For kernel context, we have mm = NULL. W.r.t kernel address we can skip setting the mm cpumask.

Fixes 0f4bc0932e ("powerpc/mm/cxl: Add the fault handling cpu to mm cpumask")
Cc Andrew Donnellan <[email protected]>
---
 drivers/misc/cxl/fault.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/cxl/fault.c +12 -2
index ab507e4ed69b..caed2a523bee 100644
@@ -141,9 +141,19 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
 	/*
 	 * Add the fault handling cpu to task mm cpumask so that we
 	 * can do a safe lockless page table walk when inserting the
-	 * hash page table entry.
+	 * hash page table entry. This function get called with a
+	 * valid mm for all user space applications. Hence using
+	 * if (mm) check is sufficient here.
 	 */
-	cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+	if (mm) {
+		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+		/*
+		 * We need to make sure we walk the table only after
+		 * we update the cpumask. The other side of the barrier is
+		 * explained * in serialize_against_pte_lookup()
+		 */
+		smp_mb();
+	}
 	if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
 		pr_devel("copro_handle_mm_fault failed: %#x\n", result);
 		return result;
Signature · 1 line
2.13.5
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Aneesh Kumar K.V

We need to add memory barrier so that the page table walk doesn't happen before the cpumask is set and made visible to the other cpus. We need to use a sync here instead of lwsync because lwsync is not sufficient for store/load ordering.

We also need to add an if (mm) check so that we do the right thing when called with a kernel context. For kernel context, we have mm = NULL. W.r.t kernel address we can skip setting the mm cpumask.

Quoted 5 lines
Fixes 0f4bc0932e ("powerpc/mm/cxl: Add the fault handling cpu to mm cpumask")
Cc Andrew Donnellan <[email protected]>
Quoted 31 lines
---
  drivers/misc/cxl/fault.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/cxl/fault.c +12 -2
index ab507e4ed69b..caed2a523bee 100644
@@ -141,9 +141,19 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
  	/*
  	 * Add the fault handling cpu to task mm cpumask so that we
  	 * can do a safe lockless page table walk when inserting the
-	 * hash page table entry.
+	 * hash page table entry. This function get called with a
+	 * valid mm for all user space applications. Hence using
+	 * if (mm) check is sufficient here.
  	 */
-	cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+	if (mm) {
+		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+		/*
+		 * We need to make sure we walk the table only after
+		 * we update the cpumask. The other side of the barrier is
+		 * explained * in serialize_against_pte_lookup()
+		 */
+		smp_mb();
+	}
  	if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
  		pr_devel("copro_handle_mm_fault failed: %#x\n", result);
  		return result;
Signature · 2 lines
Andrew Donnellan              OzLabs, ADL Canberra
[email protected]  IBM Australia Limited
#2 lore raw
In reply to Aneesh Kumar K.V
Quoted 24 lines · Aneesh Kumar K.V

We need to add memory barrier so that the page table walk doesn't happen before the cpumask is set and made visible to the other cpus. We need to use a sync here instead of lwsync because lwsync is not sufficient for store/load ordering.

We also need to add an if (mm) check so that we do the right thing when called with a kernel context. For kernel context, we have mm = NULL. W.r.t kernel address we can skip setting the mm cpumask.

Fixes 0f4bc0932e ("powerpc/mm/cxl: Add the fault handling cpu to mm cpumask")
Cc Andrew Donnellan <[email protected]>
---
 drivers/misc/cxl/fault.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/cxl/fault.c +0 -0
index ab507e4ed69b..caed2a523bee 100644
@@ -141,9 +141,19 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
 	/*

drivers/misc/cxl/fault.c int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar) · line 149

Quoted 10 lines
drivers/misc/cxl/fault.c +5 -2
 	 * Add the fault handling cpu to task mm cpumask so that we
 	 * can do a safe lockless page table walk when inserting the
-	 * hash page table entry.
+	 * hash page table entry. This function get called with a
+	 * valid mm for all user space applications. Hence using
+	 * if (mm) check is sufficient here.
 	 */
-	cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+	if (mm) {
+		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));

First test if it's already set as this should be quite common and the cost of setting is a full atomic.

Quoted 10 lines
drivers/misc/cxl/fault.c +7 -0
+		/*
+		 * We need to make sure we walk the table only after
+		 * we update the cpumask. The other side of the barrier is
+		 * explained * in serialize_against_pte_lookup()
+		 */
+		smp_mb();
+	}
 	if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
 		pr_devel("copro_handle_mm_fault failed: %#x\n", result);
 		return result;
#3 lore raw
Quoted 30 lines · Benjamin Herrenschmidt
Quoted 29 lines · Aneesh Kumar K.V

We need to add memory barrier so that the page table walk doesn't happen before the cpumask is set and made visible to the other cpus. We need to use a sync here instead of lwsync because lwsync is not sufficient for store/load ordering.

We also need to add an if (mm) check so that we do the right thing when called with a kernel context. For kernel context, we have mm = NULL. W.r.t kernel address we can skip setting the mm cpumask.

Fixes 0f4bc0932e ("powerpc/mm/cxl: Add the fault handling cpu to mm cpumask")
Cc Andrew Donnellan <[email protected]>
---
 drivers/misc/cxl/fault.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/cxl/fault.c +2 -1
index ab507e4ed69b..caed2a523bee 100644
@@ -141,9 +141,19 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
 	/*
 	 * Add the fault handling cpu to task mm cpumask so that we
 	 * can do a safe lockless page table walk when inserting the
-	 * hash page table entry.
+	 * hash page table entry. This function get called with a
+	 * valid mm for all user space applications. Hence using
Quoted 9 lines

drivers/misc/cxl/fault.c int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar) · line 149

Quoted 5 lines
drivers/misc/cxl/fault.c +3 -1
+	 * if (mm) check is sufficient here.
 	 */
-	cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+	if (mm) {
+		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));

First test if it's already set as this should be quite common and the cost of setting is a full atomic.

Something like below ?

drivers/misc/cxl/fault.c +7 -7
index caed2a523bee..ccf8568262e4 100644
@@ -146,13 +146,13 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
 	 * if (mm) check is sufficient here.
 	 */
 	if (mm) {
-		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
-		/*
-		 * We need to make sure we walk the table only after
-		 * we update the cpumask. The other side of the barrier is
-		 * explained * in serialize_against_pte_lookup()
-		 */
-		smp_mb();
+		if (!cpumask_test_and_set_cpu(smp_processor_id(), mm_cpumask(mm)))
+			/*
+			 * We need to make sure we walk the table only after
+			 * we update the cpumask. The other side of the barrier
+			 * is explained in serialize_against_pte_lookup()
+			 */
+			smp_mb();
 	}
 	if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
 		pr_devel("copro_handle_mm_fault failed: %#x\n", result);
#4 lore raw
Quoted 42 lines · Aneesh Kumar K.V
Quoted 30 lines · Benjamin Herrenschmidt
Quoted 29 lines · Aneesh Kumar K.V

We need to add memory barrier so that the page table walk doesn't happen before the cpumask is set and made visible to the other cpus. We need to use a sync here instead of lwsync because lwsync is not sufficient for store/load ordering.

We also need to add an if (mm) check so that we do the right thing when called with a kernel context. For kernel context, we have mm = NULL. W.r.t kernel address we can skip setting the mm cpumask.

Fixes 0f4bc0932e ("powerpc/mm/cxl: Add the fault handling cpu to mm cpumask")
Cc Andrew Donnellan <[email protected]>
---
 drivers/misc/cxl/fault.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/misc/cxl/fault.c +2 -1
index ab507e4ed69b..caed2a523bee 100644
@@ -141,9 +141,19 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
 	/*
 	 * Add the fault handling cpu to task mm cpumask so that we
 	 * can do a safe lockless page table walk when inserting the
-	 * hash page table entry.
+	 * hash page table entry. This function get called with a
+	 * valid mm for all user space applications. Hence using
Quoted 9 lines

drivers/misc/cxl/fault.c int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar) · line 149

Quoted 5 lines
drivers/misc/cxl/fault.c +3 -1
+	 * if (mm) check is sufficient here.
 	 */
-	cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
+	if (mm) {
+		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));

First test if it's already set as this should be quite common and the cost of setting is a full atomic.

Quoted 1 line

Something like below ?

No, don't use cpumask_test_and_set_cpu, that has extra barriers you don't want.

Do an

	if (!cpumask_test(...)) {
		cpumask_set(...);
		smp_mb();
	}

Like we do in context switch.

Quoted 25 lines
drivers/misc/cxl/fault.c +7 -7
index caed2a523bee..ccf8568262e4 100644
@@ -146,13 +146,13 @@ int cxl_handle_mm_fault(struct mm_struct *mm, u64 dsisr, u64 dar)
 	 * if (mm) check is sufficient here.
 	 */
 	if (mm) {
-		cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
-		/*
-		 * We need to make sure we walk the table only after
-		 * we update the cpumask. The other side of the barrier is
-		 * explained * in serialize_against_pte_lookup()
-		 */
-		smp_mb();
+		if (!cpumask_test_and_set_cpu(smp_processor_id(), mm_cpumask(mm)))
+			/*
+			 * We need to make sure we walk the table only after
+			 * we update the cpumask. The other side of the barrier
+			 * is explained in serialize_against_pte_lookup()
+			 */
+			smp_mb();
 	}
 	if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
 		pr_devel("copro_handle_mm_fault failed: %#x\n", result);