Skip to contentSkip to conversation

Don't touch single threaded PTEs which are on the right node

PATCH In review Andi Kleen 3 participants 4 messages Oct 12, 2016 – Oct 13, 2016

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 Don't touch single threaded PTEs which are on the right node

From: Andi Kleen <[email protected]>

We had some problems with pages getting unmapped in single threaded affinitized processes. It was tracked down to NUMA scanning.

In this case it doesn't make any sense to unmap pages if the process is single threaded and the page is already on the node the process is running on.

Add a check for this case into the numa protection code, and skip unmapping if true.

In theory the process could be migrated later, but we will eventually rescan and unmap and migrate then.

In theory this could be made more fancy: remembering this state per process or even whole mm. However that would need extra tracking and be more complicated, and the simple check seems to work fine so far.

v2: Only do it for private VMAs. Move most of check out of loop.

---
 mm/mprotect.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
mm/mprotect.c +13 -0
index a4830f0325fe..e9473e7e1468 100644
@@ -68,11 +68,17 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 	pte_t *pte, oldpte;
 	spinlock_t *ptl;
 	unsigned long pages = 0;
+	int target_node = -1;
 
 	pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
 	if (!pte)
 		return 0;
 
+	if (prot_numa &&
+	    !(vma->vm_flags & VM_SHARED) &&
+	    atomic_read(&vma->vm_mm->mm_users) == 1)
+	    target_node = cpu_to_node(raw_smp_processor_id());
+
 	arch_enter_lazy_mmu_mode();
 	do {
 		oldpte = *pte;
@@ -94,6 +100,13 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 				/* Avoid TLB flush if possible */
 				if (pte_protnone(oldpte))
 					continue;
+
+				/*
+				 * Don't mess with PTEs if page is already on the node
+				 * a single-threaded process is running on.
+				 */
+				if (target_node == page_to_nid(page))
+					continue;
 			}
 
 			ptent = ptep_modify_prot_start(mm, addr, pte);
Signature · 7 lines
2.5.5

--
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>
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Andi Kleen

From: Andi Kleen <[email protected]>

We had some problems with pages getting unmapped in single threaded affinitized processes. It was tracked down to NUMA scanning.

In this case it doesn't make any sense to unmap pages if the process is single threaded and the page is already on the node the process is running on.

Add a check for this case into the numa protection code, and skip unmapping if true.

In theory the process could be migrated later, but we will eventually rescan and unmap and migrate then.

Quoted 8 lines

In theory this could be made more fancy: remembering this state per process or even whole mm. However that would need extra tracking and be more complicated, and the simple check seems to work fine so far.

v2: Only do it for private VMAs. Move most of check out of loop.

Minor comments

Quoted 9 lines
---
 mm/mprotect.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
mm/mprotect.c +0 -0
index a4830f0325fe..e9473e7e1468 100644

mm/mprotect.c static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 72

Quoted 5 lines
mm/mprotect.c +1 -0
@@ -68,11 +68,17 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 	pte_t *pte, oldpte;
 	spinlock_t *ptl;
 	unsigned long pages = 0;
+	int target_node = -1;
 

Proper convention is to use NUMA_NO_NODE instead of -1 although it's not always adhered to.

mm/mprotect.c static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 81

Quoted 9 lines
mm/mprotect.c +5 -0
 	pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
 	if (!pte)
 		return 0;
 
+	if (prot_numa &&
+	    !(vma->vm_flags & VM_SHARED) &&
+	    atomic_read(&vma->vm_mm->mm_users) == 1)
+	    target_node = cpu_to_node(raw_smp_processor_id());
+

Use numa_node_id() instead of open-coding this. A short comment probably would not hurt even if git blame should make it obvious.

Quoted 6 lines
mm/mprotect.c +0 -0
 	arch_enter_lazy_mmu_mode();
 	do {
 		oldpte = *pte;
@@ -94,6 +100,13 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 				/* Avoid TLB flush if possible */
 				if (pte_protnone(oldpte))

mm/mprotect.c static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 111

Quoted 10 lines
mm/mprotect.c +7 -0
 					continue;
+
+				/*
+				 * Don't mess with PTEs if page is already on the node
+				 * a single-threaded process is running on.
+				 */
+				if (target_node == page_to_nid(page))
+					continue;
 			}
 

Check target_node != NUMA_NODE && target_node == page_to_nid(page) to avoid unnecessary page->flag masking and shifts?

The last one will be fairly marginal, the others are taste so whether you spin a v3 with the corrections or not;

Thanks.

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>
#2 lore raw
Quoted 10 lines
Quoted 10 lines
mm/mprotect.c +4 -0
 	do {
 		oldpte = *pte;
@@ -94,6 +100,13 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 				/* Avoid TLB flush if possible */
 				if (pte_protnone(oldpte))
 					continue;
+
+				/*
+				 * Don't mess with PTEs if page is already on the node
+				 * a single-threaded process is running on.
Quoted 8 lines

mm/mprotect.c static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 111

Quoted 5 lines
mm/mprotect.c +3 -0
+				 */
+				if (target_node == page_to_nid(page))
+					continue;
 			}
 

Check target_node != NUMA_NODE && target_node == page_to_nid(page) to avoid unnecessary page->flag masking and shifts?

I didn't do this last change because I expect a potentially mispredicted check is more expensive than some shifting/masking.

-Andi

--
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 19 lines · Andi Kleen
Quoted 10 lines
Quoted 10 lines
mm/mprotect.c +4 -0
 	do {
 		oldpte = *pte;
@@ -94,6 +100,13 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 				/* Avoid TLB flush if possible */
 				if (pte_protnone(oldpte))
 					continue;
+
+				/*
+				 * Don't mess with PTEs if page is already on the node
+				 * a single-threaded process is running on.
Quoted 8 lines

mm/mprotect.c static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 111

Quoted 5 lines
mm/mprotect.c +3 -0
+				 */
+				if (target_node == page_to_nid(page))
+					continue;
 			}
 

Check target_node != NUMA_NODE && target_node == page_to_nid(page) to avoid unnecessary page->flag masking and shifts?

Quoted 3 lines

I didn't do this last change because I expect a potentially mispredicted check is more expensive than some shifting/masking.

Ok, that's fair enough. For something that minor I expect it to be a case of "you win some you lose some" depending on workload, CPU and phase of the moon.

Thanks.

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>