Skip to contentSkip to conversation

thp: add tlb_remove_pmd_tlb_entry

PATCH Superseded Shaohua Li 2 participants 5 messages Oct 25, 2011 – Nov 11, 2011

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

PATCH 3/5 thp: add tlb_remove_pmd_tlb_entry

We have tlb_remove_tlb_entry to indicate a pte tlb flush entry should be flushed, but not a corresponding API for pmd entry. This isn't a problem so far because THP is only for x86 currently and tlb_flush() under x86 will flush entire TLB. But thsi is confusion and could be missed if thp is ported to other arch.

---
 arch/x86/include/asm/tlb.h |    1 +
 include/asm-generic/tlb.h  |   10 ++++++++++
 include/linux/huge_mm.h    |    2 +-
 mm/huge_memory.c           |    3 ++-
 mm/memory.c                |    2 +-
 5 files changed, 15 insertions(+), 3 deletions(-)
arch/x86/include/asm/tlb.h +1 -0
Index: linux/arch/x86/include/asm/tlb.h
===================================================================
@@ -4,6 +4,7 @@
 #define tlb_start_vma(tlb, vma) do { } while (0)
 #define tlb_end_vma(tlb, vma) do { } while (0)
 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do { } while (0)
 #define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)
 
 #include <asm-generic/tlb.h>
include/asm-generic/tlb.h +10 -0
Index: linux/include/asm-generic/tlb.h
===================================================================
@@ -139,6 +139,16 @@ static inline void tlb_remove_page(struc
 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
 	} while (0)
 
+#ifndef __tlb_remove_pmd_tlb_entry
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while(0)
+#endif
+
+#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)		\
+	do {							\
+		tlb->need_flush = 1;				\
+		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);	\
+	} while (0)
+
 #define pte_free_tlb(tlb, ptep, address)			\
 	do {							\
 		tlb->need_flush = 1;				\
include/linux/huge_mm.h +1 -1
Index: linux/include/linux/huge_mm.h
===================================================================
@@ -18,7 +18,7 @@ extern struct page *follow_trans_huge_pm
 					  unsigned int flags);
 extern int zap_huge_pmd(struct mmu_gather *tlb,
 			struct vm_area_struct *vma,
-			pmd_t *pmd);
+			pmd_t *pmd, unsigned long addr);
 extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 			unsigned long addr, unsigned long end,
 			unsigned char *vec);
mm/huge_memory.c +2 -1
Index: linux/mm/huge_memory.c
===================================================================
@@ -1005,7 +1005,7 @@ out:
 }
 
 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
-		 pmd_t *pmd)
+		 pmd_t *pmd, unsigned long addr)
 {
 	int ret = 0;
 
@@ -1021,6 +1021,7 @@ int zap_huge_pmd(struct mmu_gather *tlb,
 			pgtable = get_pmd_huge_pte(tlb->mm);
 			page = pmd_page(*pmd);
 			pmd_clear(pmd);
+			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
 			page_remove_rmap(page);
 			VM_BUG_ON(page_mapcount(page) < 0);
 			add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
mm/memory.c +1 -1
Index: linux/mm/memory.c
===================================================================
@@ -1231,7 +1231,7 @@ static inline unsigned long zap_pmd_rang
 			if (next-addr != HPAGE_PMD_SIZE) {
 				VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
 				split_huge_page_pmd(vma->vm_mm, pmd);
-			} else if (zap_huge_pmd(tlb, vma, pmd))
+			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
 				continue;
 			/* fall through */
 		}

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

arch/x86/include/asm/tlb.h line 8

Quoted 10 lines · Shaohua Li

Index: linux/arch/x86/include/asm/tlb.h
===================================================================

arch/x86/include/asm/tlb.h +1 -0
@@ -4,6 +4,7 @@
 #define tlb_start_vma(tlb, vma) do { } while (0)
 #define tlb_end_vma(tlb, vma) do { } while (0)
 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do { } while (0)
 #define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)

This is superfluous, it's already define below as noop.

Quoted 9 lines
arch/x86/include/asm/tlb.h +0 -0
 #include <asm-generic/tlb.h>

Index: linux/include/asm-generic/tlb.h
===================================================================

include/asm-generic/tlb.h +0 -0
@@ -139,6 +139,16 @@ static inline void tlb_remove_page(struc
 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
 	} while (0)

include/asm-generic/tlb.h static inline void tlb_remove_page(struc · line 150

Quoted 10 lines
include/asm-generic/tlb.h +9 -0
 
+#ifndef __tlb_remove_pmd_tlb_entry
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while(0)
+#endif
+
+#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)		\
+	do {							\
+		tlb->need_flush = 1;				\
+		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);	\
+	} while (0)

this looks weird, why do we set need_flush = 1 again, considering that we're doing tlb_remove_page() just a few lines later (which also sets tlb->need_flush = 1).

Ok that other archs may need the __tlb_remove_pmd_tlb_entry to be called (and I've no idea why), but the need_flush = 1 seems unnecessary.

Why other archs need the __tlb_remove_pmd_tlb_entry to be called?

One way to go would be to change the tlb->need_flush = 1 in __tlb_remove_page to a VM_BUG_ON(!tlb->need_flush) and then we keep it above and we add the __tlb_remove_pmd_tlb_entry call.

Or is there any place where __tlb_remove_page is called without a tlb_remove_*tlb_entry being called before it?

In any case the VM_BUG_ON will verify this.

Quoted 39 lines

Index: linux/include/linux/huge_mm.h
===================================================================

include/linux/huge_mm.h +1 -1
@@ -18,7 +18,7 @@ extern struct page *follow_trans_huge_pm
 					  unsigned int flags);
 extern int zap_huge_pmd(struct mmu_gather *tlb,
 			struct vm_area_struct *vma,
-			pmd_t *pmd);
+			pmd_t *pmd, unsigned long addr);
 extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 			unsigned long addr, unsigned long end,
 			unsigned char *vec);

Index: linux/mm/huge_memory.c
===================================================================

mm/huge_memory.c +2 -1
@@ -1005,7 +1005,7 @@ out:
 }
 
 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
-		 pmd_t *pmd)
+		 pmd_t *pmd, unsigned long addr)
 {
 	int ret = 0;
 
@@ -1021,6 +1021,7 @@ int zap_huge_pmd(struct mmu_gather *tlb,
 			pgtable = get_pmd_huge_pte(tlb->mm);
 			page = pmd_page(*pmd);
 			pmd_clear(pmd);
+			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
 			page_remove_rmap(page);
 			VM_BUG_ON(page_mapcount(page) < 0);
 			add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);

Index: linux/mm/memory.c
===================================================================

mm/memory.c static inline unsigned long zap_pmd_rang · line 1237

Quoted 8 lines
mm/memory.c +1 -1
@@ -1231,7 +1231,7 @@ static inline unsigned long zap_pmd_rang
 			if (next-addr != HPAGE_PMD_SIZE) {
 				VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
 				split_huge_page_pmd(vma->vm_mm, pmd);
-			} else if (zap_huge_pmd(tlb, vma, pmd))
+			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
 				continue;
 			/* fall through */
 		}

Rest looks ok.

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#2 lore raw
Quoted 44 lines · Andrea Arcangeli

arch/x86/include/asm/tlb.h line 8

Quoted 10 lines · Shaohua Li

Index: linux/arch/x86/include/asm/tlb.h
===================================================================

arch/x86/include/asm/tlb.h +1 -0
@@ -4,6 +4,7 @@
 #define tlb_start_vma(tlb, vma) do { } while (0)
 #define tlb_end_vma(tlb, vma) do { } while (0)
 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do { } while (0)
 #define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)

This is superfluous, it's already define below as noop.

Quoted 9 lines
arch/x86/include/asm/tlb.h +0 -0
 #include <asm-generic/tlb.h>

Index: linux/include/asm-generic/tlb.h
===================================================================

include/asm-generic/tlb.h +0 -0
@@ -139,6 +139,16 @@ static inline void tlb_remove_page(struc
 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
 	} while (0)

include/asm-generic/tlb.h static inline void tlb_remove_page(struc · line 150

Quoted 10 lines
include/asm-generic/tlb.h +9 -0
 
+#ifndef __tlb_remove_pmd_tlb_entry
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while(0)
+#endif
+
+#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)		\
+	do {							\
+		tlb->need_flush = 1;				\
+		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);	\
+	} while (0)

this looks weird, why do we set need_flush = 1 again, considering that we're doing tlb_remove_page() just a few lines later (which also sets tlb->need_flush = 1).

Ok that other archs may need the __tlb_remove_pmd_tlb_entry to be called (and I've no idea why), but the need_flush = 1 seems unnecessary.

Why other archs need the __tlb_remove_pmd_tlb_entry to be called?

Quoted 8 lines

One way to go would be to change the tlb->need_flush = 1 in __tlb_remove_page to a VM_BUG_ON(!tlb->need_flush) and then we keep it above and we add the __tlb_remove_pmd_tlb_entry call.

Or is there any place where __tlb_remove_page is called without a tlb_remove_*tlb_entry being called before it?

In any case the VM_BUG_ON will verify this.

ok, I made the whole tlb_remove_pmd_tlb_entry() noop now. we don't need add anything on it for x86 currently. We can change it later if necessary.

We have tlb_remove_tlb_entry to indicate a pte tlb flush entry should be flushed, but not a corresponding API for pmd entry. This isn't a problem so far because THP is only for x86 currently and tlb_flush() under x86 will flush entire TLB. But this is confusion and could be missed if thp is ported to other arch.

---
 include/asm-generic/tlb.h |    6 ++++++
 include/linux/huge_mm.h   |    2 +-
 mm/huge_memory.c          |    3 ++-
 mm/memory.c               |    2 +-
 4 files changed, 10 insertions(+), 3 deletions(-)
include/asm-generic/tlb.h +6 -0
Index: linux/include/asm-generic/tlb.h
===================================================================
@@ -139,6 +139,12 @@ static inline void tlb_remove_page(struc
 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
 	} while (0)
 
+/**
+ * tlb_remove_pmd_tlb_entry - remember a pmd mapping for later tlb invalidation
+ * This is a nop so far, because only x86 needs it.
+ */
+#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while (0)
+
 #define pte_free_tlb(tlb, ptep, address)			\
 	do {							\
 		tlb->need_flush = 1;				\
include/linux/huge_mm.h +1 -1
Index: linux/include/linux/huge_mm.h
===================================================================
@@ -18,7 +18,7 @@ extern struct page *follow_trans_huge_pm
 					  unsigned int flags);
 extern int zap_huge_pmd(struct mmu_gather *tlb,
 			struct vm_area_struct *vma,
-			pmd_t *pmd);
+			pmd_t *pmd, unsigned long addr);
 extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 			unsigned long addr, unsigned long end,
 			unsigned char *vec);
mm/huge_memory.c +2 -1
Index: linux/mm/huge_memory.c
===================================================================
@@ -1026,7 +1026,7 @@ out:
 }
 
 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
-		 pmd_t *pmd)
+		 pmd_t *pmd, unsigned long addr)
 {
 	int ret = 0;
 
@@ -1042,6 +1042,7 @@ int zap_huge_pmd(struct mmu_gather *tlb,
 			pgtable = get_pmd_huge_pte(tlb->mm);
 			page = pmd_page(*pmd);
 			pmd_clear(pmd);
+			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
 			page_remove_rmap(page);
 			VM_BUG_ON(page_mapcount(page) < 0);
 			add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
mm/memory.c +1 -1
Index: linux/mm/memory.c
===================================================================
@@ -1231,7 +1231,7 @@ static inline unsigned long zap_pmd_rang
 			if (next-addr != HPAGE_PMD_SIZE) {
 				VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
 				split_huge_page_pmd(vma->vm_mm, pmd);
-			} else if (zap_huge_pmd(tlb, vma, pmd))
+			} else if (zap_huge_pmd(tlb, vma, pmd, addr))
 				continue;
 			/* fall through */
 		}

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 49 lines · Shaohua Li
Quoted 48 lines · Andrea Arcangeli

arch/x86/include/asm/tlb.h line 8

Quoted 10 lines · Shaohua Li

Index: linux/arch/x86/include/asm/tlb.h
===================================================================

arch/x86/include/asm/tlb.h +1 -0
@@ -4,6 +4,7 @@
 #define tlb_start_vma(tlb, vma) do { } while (0)
 #define tlb_end_vma(tlb, vma) do { } while (0)
 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do { } while (0)
 #define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)

This is superfluous, it's already define below as noop.

Quoted 9 lines
arch/x86/include/asm/tlb.h +0 -0
 #include <asm-generic/tlb.h>

Index: linux/include/asm-generic/tlb.h
===================================================================

include/asm-generic/tlb.h +0 -0
@@ -139,6 +139,16 @@ static inline void tlb_remove_page(struc
 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
 	} while (0)

include/asm-generic/tlb.h static inline void tlb_remove_page(struc · line 150

Quoted 10 lines
include/asm-generic/tlb.h +9 -0
 
+#ifndef __tlb_remove_pmd_tlb_entry
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while(0)
+#endif
+
+#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)		\
+	do {							\
+		tlb->need_flush = 1;				\
+		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);	\
+	} while (0)

this looks weird, why do we set need_flush = 1 again, considering that we're doing tlb_remove_page() just a few lines later (which also sets tlb->need_flush = 1).

Ok that other archs may need the __tlb_remove_pmd_tlb_entry to be called (and I've no idea why), but the need_flush = 1 seems unnecessary.

Why other archs need the __tlb_remove_pmd_tlb_entry to be called?

One way to go would be to change the tlb->need_flush = 1 in __tlb_remove_page to a VM_BUG_ON(!tlb->need_flush) and then we keep it above and we add the __tlb_remove_pmd_tlb_entry call.

Quoted 7 lines
Quoted 4 lines

Or is there any place where __tlb_remove_page is called without a tlb_remove_*tlb_entry being called before it?

In any case the VM_BUG_ON will verify this.

ok, I made the whole tlb_remove_pmd_tlb_entry() noop now. we don't need add anything on it for x86 currently. We can change it later if necessary.

I thought it'd be cleaner to have only the __tlb_remove_*tlb_entry variants set need_flush=1 and have __tlb_remove_page just check that is set under a VM_BUG_ON. That would also avoid a second unnecessary need_flush = 1 for the pte case which is repeated now (it's not the repeated in the pmd case in your patch because it's a noop, but the pte case it's not a noop). Maybe it's not possible but if it's possible it looks better.

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#4 lore raw
Quoted 58 lines · Andrea Arcangeli
Quoted 49 lines · Shaohua Li
Quoted 48 lines · Andrea Arcangeli

arch/x86/include/asm/tlb.h line 8

Quoted 10 lines · Shaohua Li

Index: linux/arch/x86/include/asm/tlb.h
===================================================================

arch/x86/include/asm/tlb.h +1 -0
@@ -4,6 +4,7 @@
 #define tlb_start_vma(tlb, vma) do { } while (0)
 #define tlb_end_vma(tlb, vma) do { } while (0)
 #define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do { } while (0)
 #define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)

This is superfluous, it's already define below as noop.

Quoted 9 lines
arch/x86/include/asm/tlb.h +0 -0
 #include <asm-generic/tlb.h>

Index: linux/include/asm-generic/tlb.h
===================================================================

include/asm-generic/tlb.h +0 -0
@@ -139,6 +139,16 @@ static inline void tlb_remove_page(struc
 		__tlb_remove_tlb_entry(tlb, ptep, address);	\
 	} while (0)

include/asm-generic/tlb.h static inline void tlb_remove_page(struc · line 150

Quoted 10 lines
include/asm-generic/tlb.h +9 -0
 
+#ifndef __tlb_remove_pmd_tlb_entry
+#define __tlb_remove_pmd_tlb_entry(tlb, pmdp, address) do {} while(0)
+#endif
+
+#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address)		\
+	do {							\
+		tlb->need_flush = 1;				\
+		__tlb_remove_pmd_tlb_entry(tlb, pmdp, address);	\
+	} while (0)

this looks weird, why do we set need_flush = 1 again, considering that we're doing tlb_remove_page() just a few lines later (which also sets tlb->need_flush = 1).

Ok that other archs may need the __tlb_remove_pmd_tlb_entry to be called (and I've no idea why), but the need_flush = 1 seems unnecessary.

Why other archs need the __tlb_remove_pmd_tlb_entry to be called?

One way to go would be to change the tlb->need_flush = 1 in __tlb_remove_page to a VM_BUG_ON(!tlb->need_flush) and then we keep it above and we add the __tlb_remove_pmd_tlb_entry call.

Quoted 7 lines
Quoted 4 lines

Or is there any place where __tlb_remove_page is called without a tlb_remove_*tlb_entry being called before it?

In any case the VM_BUG_ON will verify this.

ok, I made the whole tlb_remove_pmd_tlb_entry() noop now. we don't need add anything on it for x86 currently. We can change it later if necessary.

Quoted 7 lines

I thought it'd be cleaner to have only the __tlb_remove_*tlb_entry variants set need_flush=1 and have __tlb_remove_page just check that is set under a VM_BUG_ON. That would also avoid a second unnecessary need_flush = 1 for the pte case which is repeated now (it's not the repeated in the pmd case in your patch because it's a noop, but the pte case it's not a noop). Maybe it's not possible but if it's possible it looks better.

makes sense to me. __tlb_remove_page should always follow after __tlb_remove_*tlb_entry. if we set need_flush=1 in __tlb_remove_*tlb_entry, tlb_remove_*tlb_entry == __tlb_remove_*tlb entry then, we can delete __xxx. But this sounds not related to the issue, I'd prefer another patch to clean it up. how do you think?

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>