Skip to contentSkip to conversation

Switch x86 to generic get_user_pages_fast() implementation

PATCH Superseded Kirill A. Shutemov 6 participants 22 messages Mar 16, 2017 – Mar 18, 2017

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

cover Switch x86 to generic get_user_pages_fast() implementation

Hi,

The patcheset generalize mm/gup.c implementation of get_user_pages_fast() to be usable for x86 and switches x86 over.

Please review and consider applying.

Kirill A. Shutemov (7):
mm: Drop arch_pte_access_permitted() mmu hook
mm/gup: Move permission checks into helpers
mm/gup: Move page table entry dereference into helper mm/gup: Make pages referenced during generic get_user_pages_fast() mm/gup: Implement dev_pagemap logic in generic get_user_pages_fast() mm/gup: Provide hook to check if __GUP_fast() is allowed for the range x86/mm: Switch to generic get_user_page_fast() implementation

 arch/powerpc/include/asm/mmu_context.h   |   6 -
 arch/s390/include/asm/mmu_context.h      |   6 -
 arch/um/include/asm/mmu_context.h        |   6 -
 arch/unicore32/include/asm/mmu_context.h |   6 -
 arch/x86/Kconfig                         |   3 +
 arch/x86/include/asm/mmu_context.h       |  16 -
 arch/x86/include/asm/pgtable-3level.h    |  45 +++
 arch/x86/include/asm/pgtable.h           |  53 ++++
 arch/x86/include/asm/pgtable_64.h        |  16 +-
 arch/x86/mm/Makefile                     |   2 +-
 arch/x86/mm/gup.c                        | 496 -------------------------------
 include/asm-generic/mm_hooks.h           |   6 -
 include/asm-generic/pgtable.h            |  25 ++
 include/linux/mm.h                       |   4 +
 mm/gup.c                                 | 134 +++++++--
 15 files changed, 262 insertions(+), 562 deletions(-)
 delete mode 100644 arch/x86/mm/gup.c
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

PATCH 1/7 mm: Drop arch_pte_access_permitted() mmu hook

The only arch that defines it to something meaningful is x86. But x86 doesn't use generic GUP_fast() implementation -- the only place where the hook is called.

Let's drop it.

---
 arch/powerpc/include/asm/mmu_context.h   | 6 ------
 arch/s390/include/asm/mmu_context.h      | 6 ------
 arch/um/include/asm/mmu_context.h        | 6 ------
 arch/unicore32/include/asm/mmu_context.h | 6 ------
 arch/x86/include/asm/mmu_context.h       | 4 ----
 include/asm-generic/mm_hooks.h           | 6 ------
 mm/gup.c                                 | 3 ---
 7 files changed, 37 deletions(-)
arch/powerpc/include/asm/mmu_context.h +0 -6
index b9e3f0aca261..ecf9885ab660 100644
@@ -163,11 +163,5 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif /* __KERNEL__ */
 #endif /* __ASM_POWERPC_MMU_CONTEXT_H */
arch/s390/include/asm/mmu_context.h +0 -6
index 6e31d87fb669..fa2bf69be182 100644
@@ -156,10 +156,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif /* __S390_MMU_CONTEXT_H */
arch/um/include/asm/mmu_context.h +0 -6
index 94ac2739918c..b668e351fd6c 100644
@@ -37,12 +37,6 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	return true;
 }
 
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
-
 /*
  * end asm-generic/mm_hooks.h functions
  */
arch/unicore32/include/asm/mmu_context.h +0 -6
index 62dfc644c908..59b06b48f27d 100644
@@ -103,10 +103,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif
arch/x86/include/asm/mmu_context.h +0 -4
index 306c7e12af55..68b329d77b3a 100644
@@ -268,8 +268,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	return __pkru_allows_pkey(vma_pkey(vma), write);
 }
 
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	return __pkru_allows_pkey(pte_flags_pkey(pte_flags(pte)), write);
-}
 #endif /* _ASM_X86_MMU_CONTEXT_H */
include/asm-generic/mm_hooks.h +0 -6
index cc5d9a1405df..41e5b6784b97 100644
@@ -32,10 +32,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif	/* _ASM_GENERIC_MM_HOOKS_H */
mm/gup.c +0 -3
index 04aa405350dc..3f2338ba3402 100644
@@ -1216,9 +1216,6 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 			pte_protnone(pte) || (write && !pte_write(pte)))
 			goto pte_unmap;
 
-		if (!arch_pte_access_permitted(pte, write))
-			goto pte_unmap;
-
 		VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
 		page = pte_page(pte);
 		head = compound_head(page);
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>
#2 lore raw

[tip:x86/mm] mm/gup: Drop the arch_pte_access_permitted() MMU callback

mm/gup: Drop the arch_pte_access_permitted() MMU callback
Commit-ID:  9a804fecee232e71b47ac37d62fd3d5d66b08b91
Gitweb:     http://git.kernel.org/tip/9a804fecee232e71b47ac37d62fd3d5d66b08b91
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Thu, 16 Mar 2017 18:26:49 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:01 +0100

mm/gup: Drop the arch_pte_access_permitted() MMU callback

The only arch that defines it to something meaningful is x86. But x86 doesn't use the generic GUP_fast() implementation -- the only place where the callback is called.

Let's drop it.

Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 arch/powerpc/include/asm/mmu_context.h   | 6 ------
 arch/s390/include/asm/mmu_context.h      | 6 ------
 arch/um/include/asm/mmu_context.h        | 6 ------
 arch/unicore32/include/asm/mmu_context.h | 6 ------
 arch/x86/include/asm/mmu_context.h       | 4 ----
 include/asm-generic/mm_hooks.h           | 6 ------
 mm/gup.c                                 | 3 ---
 7 files changed, 37 deletions(-)
arch/powerpc/include/asm/mmu_context.h +0 -6
index b9e3f0a..ecf9885 100644
@@ -163,11 +163,5 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif /* __KERNEL__ */
 #endif /* __ASM_POWERPC_MMU_CONTEXT_H */
arch/s390/include/asm/mmu_context.h +0 -6
index 6e31d87..fa2bf69 100644
@@ -156,10 +156,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif /* __S390_MMU_CONTEXT_H */
arch/um/include/asm/mmu_context.h +0 -6
index 94ac273..b668e35 100644
@@ -37,12 +37,6 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	return true;
 }
 
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
-
 /*
  * end asm-generic/mm_hooks.h functions
  */
arch/unicore32/include/asm/mmu_context.h +0 -6
index 62dfc64..59b06b4 100644
@@ -103,10 +103,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif
arch/x86/include/asm/mmu_context.h +0 -4
index 306c7e1..68b329d 100644
@@ -268,8 +268,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	return __pkru_allows_pkey(vma_pkey(vma), write);
 }
 
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	return __pkru_allows_pkey(pte_flags_pkey(pte_flags(pte)), write);
-}
 #endif /* _ASM_X86_MMU_CONTEXT_H */
include/asm-generic/mm_hooks.h +0 -6
index cc5d9a1..41e5b67 100644
@@ -32,10 +32,4 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
 	/* by default, allow everything */
 	return true;
 }
-
-static inline bool arch_pte_access_permitted(pte_t pte, bool write)
-{
-	/* by default, allow everything */
-	return true;
-}
 #endif	/* _ASM_GENERIC_MM_HOOKS_H */
mm/gup.c +0 -3
index 04aa405..3f2338b 100644
@@ -1216,9 +1216,6 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 			pte_protnone(pte) || (write && !pte_write(pte)))
 			goto pte_unmap;
 
-		if (!arch_pte_access_permitted(pte, write))
-			goto pte_unmap;
-
 		VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
 		page = pte_page(pte);
 		head = compound_head(page);
#3 lore raw

PATCH 2/7 mm/gup: Move permission checks into helpers

In reply to Kirill A. Shutemov

This is preparation patch for transition of x86 to generic GUP_fast() implementation.

On x86, we would need to do additional permission checks to determinate if access is allowed.

Let's abstract it out into separate helpers.

---
 include/asm-generic/pgtable.h | 25 +++++++++++++++++++++++++
 mm/gup.c                      | 15 ++++++++++-----
 2 files changed, 35 insertions(+), 5 deletions(-)
include/asm-generic/pgtable.h +25 -0
index 1fad160f35de..7dfa767dc680 100644
@@ -341,6 +341,31 @@ static inline int pte_unused(pte_t pte)
 }
 #endif
 
+#ifndef pte_access_permitted
+#define pte_access_permitted(pte, write) \
+	(pte_present(pte) && (!(write) || pte_write(pte)))
+#endif
+
+#ifndef pmd_access_permitted
+#define pmd_access_permitted(pmd, write) \
+	(pmd_present(pmd) && (!(write) || pmd_write(pmd)))
+#endif
+
+#ifndef pud_access_permitted
+#define pud_access_permitted(pud, write) \
+	(pud_present(pud) && (!(write) || pud_write(pud)))
+#endif
+
+#ifndef p4d_access_permitted
+#define p4d_access_permitted(p4d, write) \
+	(p4d_present(p4d) && (!(write) || p4d_write(p4d)))
+#endif
+
+#ifndef pgd_access_permitted
+#define pgd_access_permitted(pgd, write) \
+	(pgd_present(pgd) && (!(write) || pgd_write(pgd)))
+#endif
+
 #ifndef __HAVE_ARCH_PMD_SAME
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
mm/gup.c +10 -5
index 3f2338ba3402..a62a778ce4ec 100644
@@ -1212,8 +1212,13 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 		 * Similar to the PMD case below, NUMA hinting must take slow
 		 * path using the pte_protnone check.
 		 */
-		if (!pte_present(pte) || pte_special(pte) ||
-			pte_protnone(pte) || (write && !pte_write(pte)))
+		if (pte_protnone(pte))
+			goto pte_unmap;
+
+		if (!pte_access_permitted(pte, write))
+			goto pte_unmap;
+
+		if (pte_special(pte))
 			goto pte_unmap;
 
 		VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
@@ -1264,7 +1269,7 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
 	struct page *head, *page;
 	int refs;
 
-	if (write && !pmd_write(orig))
+	if (!pmd_access_permitted(orig, write))
 		return 0;
 
 	refs = 0;
@@ -1299,7 +1304,7 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
 	struct page *head, *page;
 	int refs;
 
-	if (write && !pud_write(orig))
+	if (!pud_access_permitted(orig, write))
 		return 0;
 
 	refs = 0;
@@ -1335,7 +1340,7 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
 	int refs;
 	struct page *head, *page;
 
-	if (write && !pgd_write(orig))
+	if (!pgd_access_permitted(orig, write))
 		return 0;
 
 	refs = 0;
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>
#4 lore raw

[tip:x86/mm] mm/gup: Move permission checks into helpers

mm/gup: Move permission checks into helpers
Commit-ID:  e7884f8ead4a301b04687a3238527b06feef8ea0
Gitweb:     http://git.kernel.org/tip/e7884f8ead4a301b04687a3238527b06feef8ea0
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Thu, 16 Mar 2017 18:26:50 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:01 +0100

mm/gup: Move permission checks into helpers

This is a preparation patch for the transition of x86 to the generic GUP_fast() implementation.

On x86, we would need to do additional permission checks to determine if access is allowed.

Let's abstract it out into separate helpers.

Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/asm-generic/pgtable.h | 25 +++++++++++++++++++++++++
 mm/gup.c                      | 15 ++++++++++-----
 2 files changed, 35 insertions(+), 5 deletions(-)
include/asm-generic/pgtable.h +25 -0
index 1fad160..7dfa767 100644
@@ -341,6 +341,31 @@ static inline int pte_unused(pte_t pte)
 }
 #endif
 
+#ifndef pte_access_permitted
+#define pte_access_permitted(pte, write) \
+	(pte_present(pte) && (!(write) || pte_write(pte)))
+#endif
+
+#ifndef pmd_access_permitted
+#define pmd_access_permitted(pmd, write) \
+	(pmd_present(pmd) && (!(write) || pmd_write(pmd)))
+#endif
+
+#ifndef pud_access_permitted
+#define pud_access_permitted(pud, write) \
+	(pud_present(pud) && (!(write) || pud_write(pud)))
+#endif
+
+#ifndef p4d_access_permitted
+#define p4d_access_permitted(p4d, write) \
+	(p4d_present(p4d) && (!(write) || p4d_write(p4d)))
+#endif
+
+#ifndef pgd_access_permitted
+#define pgd_access_permitted(pgd, write) \
+	(pgd_present(pgd) && (!(write) || pgd_write(pgd)))
+#endif
+
 #ifndef __HAVE_ARCH_PMD_SAME
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
mm/gup.c +10 -5
index 3f2338b..a62a778c 100644
@@ -1212,8 +1212,13 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 		 * Similar to the PMD case below, NUMA hinting must take slow
 		 * path using the pte_protnone check.
 		 */
-		if (!pte_present(pte) || pte_special(pte) ||
-			pte_protnone(pte) || (write && !pte_write(pte)))
+		if (pte_protnone(pte))
+			goto pte_unmap;
+
+		if (!pte_access_permitted(pte, write))
+			goto pte_unmap;
+
+		if (pte_special(pte))
 			goto pte_unmap;
 
 		VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
@@ -1264,7 +1269,7 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
 	struct page *head, *page;
 	int refs;
 
-	if (write && !pmd_write(orig))
+	if (!pmd_access_permitted(orig, write))
 		return 0;
 
 	refs = 0;
@@ -1299,7 +1304,7 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
 	struct page *head, *page;
 	int refs;
 
-	if (write && !pud_write(orig))
+	if (!pud_access_permitted(orig, write))
 		return 0;
 
 	refs = 0;
@@ -1335,7 +1340,7 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
 	int refs;
 	struct page *head, *page;
 
-	if (write && !pgd_write(orig))
+	if (!pgd_access_permitted(orig, write))
 		return 0;
 
 	refs = 0;
#5 lore raw

PATCH 3/7 mm/gup: Move page table entry dereference into helper

In reply to Kirill A. Shutemov

This is preparation patch for transition of x86 to generic GUP_fast() implementation.

On x86 PAE, page table entry is larger than sizeof(long) and we would need to provide helper that can read the entry atomically.

---
 mm/gup.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
mm/gup.c +12 -8
index a62a778ce4ec..ed2259dc4606 100644
@@ -1189,6 +1189,17 @@ struct page *get_dump_page(unsigned long addr)
  */
 #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
 
+#ifndef gup_get_pte
+/*
+ * We assume that the pte can be read atomically. If this is not the case for
+ * your architecture, please provide the helper.
+ */
+static inline pte_t gup_get_pte(pte_t *ptep)
+{
+	return READ_ONCE(*ptep);
+}
+#endif
+
 #ifdef __HAVE_ARCH_PTE_SPECIAL
 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 			 int write, struct page **pages, int *nr)
@@ -1198,14 +1209,7 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 
 	ptem = ptep = pte_offset_map(&pmd, addr);
 	do {
-		/*
-		 * In the line below we are assuming that the pte can be read
-		 * atomically. If this is not the case for your architecture,
-		 * please wrap this in a helper function!
-		 *
-		 * for an example see gup_get_pte in arch/x86/mm/gup.c
-		 */
-		pte_t pte = READ_ONCE(*ptep);
+		pte_t pte = gup_get_pte(ptep);
 		struct page *head, *page;
 
 		/*
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>
#6 lore raw

[tip:x86/mm] mm/gup: Move page table entry dereference into helper function

mm/gup: Move page table entry dereference into helper function
Commit-ID:  0005d20b2ff1e501e186b3b3bc587085ac305fdc
Gitweb:     http://git.kernel.org/tip/0005d20b2ff1e501e186b3b3bc587085ac305fdc
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Thu, 16 Mar 2017 18:26:51 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:02 +0100

mm/gup: Move page table entry dereference into helper function

This is a preparation patch for the transition of x86 to the generic GUP_fast() implementation.

On x86 PAE, page table entry is larger than sizeof(long) and we would need to provide a helper that can read the entry atomically.

Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 mm/gup.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
mm/gup.c +12 -8
index a62a778c..e83db38 100644
@@ -1189,6 +1189,17 @@ struct page *get_dump_page(unsigned long addr)
  */
 #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
 
+#ifndef gup_get_pte
+/*
+ * We assume that the PTE can be read atomically. If this is not the case for
+ * your architecture, please provide the helper.
+ */
+static inline pte_t gup_get_pte(pte_t *ptep)
+{
+	return READ_ONCE(*ptep);
+}
+#endif
+
 #ifdef __HAVE_ARCH_PTE_SPECIAL
 static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 			 int write, struct page **pages, int *nr)
@@ -1198,14 +1209,7 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 
 	ptem = ptep = pte_offset_map(&pmd, addr);
 	do {
-		/*
-		 * In the line below we are assuming that the pte can be read
-		 * atomically. If this is not the case for your architecture,
-		 * please wrap this in a helper function!
-		 *
-		 * for an example see gup_get_pte in arch/x86/mm/gup.c
-		 */
-		pte_t pte = READ_ONCE(*ptep);
+		pte_t pte = gup_get_pte(ptep);
 		struct page *head, *page;
 
 		/*
#7 lore raw

PATCH 4/7 mm/gup: Make pages referenced during generic get_user_pages_fast()

In reply to Kirill A. Shutemov

This is preparation patch for transition of x86 to generic GUP_fast() implementation.

Unlike generic GUP_fast(), x86 version makes all pages it touches referenced. It seems required for GRU and EPT.

See commit 8ee53820edfd ("thp: mmu_notifier_test_young").

---
 mm/gup.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/gup.c +5 -0
index ed2259dc4606..f0cfe225c08f 100644
@@ -1238,6 +1238,8 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 		}
 
 		VM_BUG_ON_PAGE(compound_head(page) != head, page);
+
+		SetPageReferenced(page);
 		pages[*nr] = page;
 		(*nr)++;
 
@@ -1299,6 +1301,7 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
 		return 0;
 	}
 
+	SetPageReferenced(head);
 	return 1;
 }
 
@@ -1334,6 +1337,7 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
 		return 0;
 	}
 
+	SetPageReferenced(head);
 	return 1;
 }
 
@@ -1370,6 +1374,7 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
 		return 0;
 	}
 
+	SetPageReferenced(head);
 	return 1;
 }
 
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>
#8 lore raw

[tip:x86/mm] mm/gup: Mark all pages PageReferenced in generic get_user_pages_fast()

mm/gup: Mark all pages PageReferenced in generic get_user_pages_fast()
Commit-ID:  e93480537fd7ecaf5ed1a662a979376f6fee50e3
Gitweb:     http://git.kernel.org/tip/e93480537fd7ecaf5ed1a662a979376f6fee50e3
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Thu, 16 Mar 2017 18:26:52 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:02 +0100

mm/gup: Mark all pages PageReferenced in generic get_user_pages_fast()

This is a preparation patch for the transition of x86 to the generic GUP_fast() implementation.

Unlike generic GUP_fast(), the x86 version makes all pages it touches referenced. It seems required for GRU and EPT.

See the following commit:

8ee53820edfd ("thp: mmu_notifier_test_young")

Cc Andrea Arcangeli <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 mm/gup.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/gup.c +5 -0
index e83db38..2b6cd357 100644
@@ -1238,6 +1238,8 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 		}
 
 		VM_BUG_ON_PAGE(compound_head(page) != head, page);
+
+		SetPageReferenced(page);
 		pages[*nr] = page;
 		(*nr)++;
 
@@ -1299,6 +1301,7 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
 		return 0;
 	}
 
+	SetPageReferenced(head);
 	return 1;
 }
 
@@ -1334,6 +1337,7 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
 		return 0;
 	}
 
+	SetPageReferenced(head);
 	return 1;
 }
 
@@ -1370,6 +1374,7 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
 		return 0;
 	}
 
+	SetPageReferenced(head);
 	return 1;
 }
 
#9 lore raw

PATCH 5/7 mm/gup: Implement dev_pagemap logic in generic get_user_pages_fast()

In reply to Kirill A. Shutemov

This is preparation patch for transition of x86 to generic GUP_fast() implementation.

Prepare generic GUP_fast() to handle dev_mapmap. At the moment, it's only implemented on x86. On non-x86, the new code will be compiled out.

---
 include/linux/mm.h |  4 +++
 mm/gup.c           | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 75 insertions(+), 2 deletions(-)
include/linux/mm.h +4 -0
index 5f01c88f0800..e197d3ca3e8a 100644
@@ -430,6 +430,10 @@ static inline int pud_devmap(pud_t pud)
 {
 	return 0;
 }
+static inline int pgd_devmap(pgd_t pgd)
+{
+	return 0;
+}
 #endif
 
 /*
mm/gup.c +71 -2

120 lines not on this page · raw message

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>
#10 lore raw

Re: [PATCH 5/7] mm/gup: Implement dev_pagemap logic in generic get_user_pages_fast()

Quoted 8 lines · Kirill A. Shutemov
(quoted diff) +8 -0
+static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
+		unsigned long end, struct page **pages, int *nr)
+{
+	unsigned long fault_pfn;
+
+	fault_pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
+	return __gup_device_huge(fault_pfn, addr, end, pages, nr);
+}

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+static int __gup_device_huge_pud(pud_t pud, unsigned long addr,
+		unsigned long end, struct page **pages, int *nr)
+{
+	unsigned long fault_pfn;
+
+	fault_pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
+	return __gup_device_huge(fault_pfn, addr, end, pages, nr);
+}
+

PowerPC doesn't [always] provide pmd_pfn() and pud_pfn().

Fixup:

mm/gup.c +16 -2
index 5cc489d98562..6f36cbc294cf 100644
@@ -1287,6 +1287,7 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 }
 #endif /* __HAVE_ARCH_PTE_SPECIAL */
 
+#ifdef __HAVE_ARCH_PTE_DEVMAP
 static int __gup_device_huge(unsigned long pfn, unsigned long addr,
 		unsigned long end, struct page **pages, int *nr)
 {
@@ -1328,6 +1329,21 @@ static int __gup_device_huge_pud(pud_t pud, unsigned long addr,
 	fault_pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
 	return __gup_device_huge(fault_pfn, addr, end, pages, nr);
 }
+#else
+static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
+		unsigned long end, struct page **pages, int *nr)
+{
+	BUILD_BUG();
+	return 0;
+}
+
+static int __gup_device_huge_pud(pud_t pud, unsigned long addr,
+		unsigned long end, struct page **pages, int *nr)
+{
+	BUILD_BUG();
+	return 0;
+}
+#endif
 
 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
 		unsigned long end, int write, struct page **pages, int *nr)
@@ -1338,7 +1354,6 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
 	if (!pmd_access_permitted(orig, write))
 		return 0;
 
-	VM_BUG_ON(!pfn_valid(pmd_pfn(orig)));
 	if (pmd_devmap(orig))
 		return __gup_device_huge_pmd(orig, addr, end, pages, nr);
 
@@ -1378,7 +1393,6 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
 	if (!pud_access_permitted(orig, write))
 		return 0;
 
-	VM_BUG_ON(!pfn_valid(pud_pfn(orig)));
 	if (pud_devmap(orig))
 		return __gup_device_huge_pud(orig, addr, end, pages, nr);
 
Signature · 7 lines
 Kirill A. Shutemov

--
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>
#11 lore raw

[tip:x86/mm] mm/gup: Implement the dev_pagemap() logic in the generic get_user_pages_fast() function

In reply to Kirill A. Shutemov
mm/gup: Implement the dev_pagemap() logic in the generic get_user_pages_fast() function
Commit-ID:  b59f65fa076a8eac2ff3a8ab7f8e1705b9fa86cb
Gitweb:     http://git.kernel.org/tip/b59f65fa076a8eac2ff3a8ab7f8e1705b9fa86cb
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Thu, 16 Mar 2017 18:26:53 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:02 +0100

mm/gup: Implement the dev_pagemap() logic in the generic get_user_pages_fast() function

This is a preparation patch for the transition of x86 to the generic GUP_fast() implementation.

Prepare generic GUP_fast() to handle dev_pagemap(). At the moment, it's only implemented on x86. On non-x86, the new code will be compiled out.

Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/linux/mm.h |  4 +++
 mm/gup.c           | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 89 insertions(+), 2 deletions(-)
include/linux/mm.h +4 -0
index 5f01c88..e197d3c 100644
@@ -430,6 +430,10 @@ static inline int pud_devmap(pud_t pud)
 {
 	return 0;
 }
+static inline int pgd_devmap(pgd_t pgd)
+{
+	return 0;
+}
 #endif
 
 /*
mm/gup.c +85 -2

134 lines not on this page · raw message

#12 lore raw

PATCH 6/7 mm/gup: Provide hook to check if __GUP_fast() is allowed for the range

In reply to Kirill A. Shutemov

This is preparation patch for transition of x86 to generic GUP_fast() implementation.

On x86, get_user_pages_fast() does few sanity checks to see if we can call __get_user_pages_fast() for the range.

Wrapping protection should be useful for generic code too.

---
 mm/gup.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
mm/gup.c +21 -3
index e04f76594eb9..a6e21380c8b0 100644
@@ -1600,6 +1600,21 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 	return nr;
 }
 
+#ifndef gup_fast_permitted
+/*
+ * Check if it's allowed to use __get_user_pages_fast() for the range or
+ * need to fallback to non-fast version.
+ */
+bool gup_fast_permitted(unsigned long start, int nr_pages, int write)
+{
+	unsigned long len, end;
+
+	len = (unsigned long) nr_pages << PAGE_SHIFT;
+	end = start + len;
+	return end >= start;
+}
+#endif
+
 /**
  * get_user_pages_fast() - pin user pages in memory
  * @start:	starting user address
@@ -1619,11 +1634,14 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
 			struct page **pages)
 {
-	int nr, ret;
+	int nr = 0, ret = 0;
 
 	start &= PAGE_MASK;
-	nr = __get_user_pages_fast(start, nr_pages, write, pages);
-	ret = nr;
+
+	if (gup_fast_permitted(start, nr_pages, write)) {
+		nr = __get_user_pages_fast(start, nr_pages, write, pages);
+		ret = nr;
+	}
 
 	if (nr < nr_pages) {
 		/* Try to get the remaining pages with get_user_pages */
Signature · 1 line
2.11.0
#13 lore raw

[tip:x86/mm] mm/gup: Provide callback to check if __GUP_fast() is allowed for the range

mm/gup: Provide callback to check if __GUP_fast() is allowed for the range
Commit-ID:  73e10a61817dfc97fe7418bfad1f608e562d7348
Gitweb:     http://git.kernel.org/tip/73e10a61817dfc97fe7418bfad1f608e562d7348
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Thu, 16 Mar 2017 18:26:54 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:03 +0100

mm/gup: Provide callback to check if __GUP_fast() is allowed for the range

This is a preparation patch for the transition of x86 to the generic GUP_fast() implementation.

On x86, get_user_pages_fast() does a couple of sanity checks to see if we can call __get_user_pages_fast() for the range.

This kind of wrapping protection should be useful for the generic code too.

Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
[ Small readability edits. ]
---
 mm/gup.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
mm/gup.c +21 -3
index e3d1e80..527ec2c 100644
@@ -1614,6 +1614,21 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 	return nr;
 }
 
+#ifndef gup_fast_permitted
+/*
+ * Check if it's allowed to use __get_user_pages_fast() for the range, or
+ * we need to fall back to the slow version:
+ */
+bool gup_fast_permitted(unsigned long start, int nr_pages, int write)
+{
+	unsigned long len, end;
+
+	len = (unsigned long) nr_pages << PAGE_SHIFT;
+	end = start + len;
+	return end >= start;
+}
+#endif
+
 /**
  * get_user_pages_fast() - pin user pages in memory
  * @start:	starting user address
@@ -1633,11 +1648,14 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
 			struct page **pages)
 {
-	int nr, ret;
+	int nr = 0, ret = 0;
 
 	start &= PAGE_MASK;
-	nr = __get_user_pages_fast(start, nr_pages, write, pages);
-	ret = nr;
+
+	if (gup_fast_permitted(start, nr_pages, write)) {
+		nr = __get_user_pages_fast(start, nr_pages, write, pages);
+		ret = nr;
+	}
 
 	if (nr < nr_pages) {
 		/* Try to get the remaining pages with get_user_pages */
#14 lore raw

PATCH 7/7 x86/mm: Switch to generic get_user_page_fast() implementation

In reply to Kirill A. Shutemov

The patch provides all required hooks to match generic get_user_pages_fast() behaviour to x86 and switch x86 over.

---
 arch/x86/Kconfig                      |   3 +
 arch/x86/include/asm/mmu_context.h    |  12 -
 arch/x86/include/asm/pgtable-3level.h |  45 +++
 arch/x86/include/asm/pgtable.h        |  53 ++++
 arch/x86/include/asm/pgtable_64.h     |  16 +-
 arch/x86/mm/Makefile                  |   2 +-
 arch/x86/mm/gup.c                     | 496 ----------------------------------
 7 files changed, 117 insertions(+), 510 deletions(-)
 delete mode 100644 arch/x86/mm/gup.c
arch/x86/Kconfig +3 -0
index 2bab9d093b51..f9d859180803 100644
@@ -2788,6 +2788,9 @@ config X86_DMA_REMAP
 	bool
 	depends on STA2X11
 
+config HAVE_GENERIC_RCU_GUP
+	def_bool y
+
 source "net/Kconfig"
 
 source "drivers/Kconfig"
arch/x86/include/asm/mmu_context.h +0 -12
index 68b329d77b3a..6e933d2d88d9 100644
@@ -220,18 +220,6 @@ static inline int vma_pkey(struct vm_area_struct *vma)
 }
 #endif
 
-static inline bool __pkru_allows_pkey(u16 pkey, bool write)
-{
-	u32 pkru = read_pkru();
-
-	if (!__pkru_allows_read(pkru, pkey))
-		return false;
-	if (write && !__pkru_allows_write(pkru, pkey))
-		return false;
-
-	return true;
-}
-
 /*
  * We only want to enforce protection keys on the current process
  * because we effectively have no access to PKRU for other
arch/x86/include/asm/pgtable-3level.h +45 -0

49 lines not on this page · raw message

arch/x86/include/asm/pgtable.h +53 -0

65 lines not on this page · raw message

arch/x86/include/asm/pgtable_64.h +15 -1
index 73c7ccc38912..1a4bc71534d4 100644
@@ -206,6 +206,20 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
-#endif /* !__ASSEMBLY__ */
+#define gup_fast_permitted gup_fast_permitted
+static inline bool gup_fast_permitted(unsigned long start, int nr_pages,
+		int write)
+{
+	unsigned long len, end;
+
+	len = (unsigned long)nr_pages << PAGE_SHIFT;
+	end = start + len;
+	if (end < start)
+		return false;
+	if (end >> __VIRTUAL_MASK_SHIFT)
+		return false;
+	return true;
+}
 
+#endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/mm/Makefile +1 -1
index 96d2b847e09e..0fbdcb64f9f8 100644
@@ -2,7 +2,7 @@
 KCOV_INSTRUMENT_tlb.o	:= n
 
 obj-y	:=  init.o init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \
-	    pat.o pgtable.o physaddr.o gup.o setup_nx.o tlb.o
+	    pat.o pgtable.o physaddr.o setup_nx.o tlb.o
 
 # Make sure __phys_addr has no stackprotector
 nostackp := $(call cc-option, -fno-stack-protector)
arch/x86/mm/gup.c deleted +0 -496

496 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer
#15 lore raw

Re: [PATCH 7/7] x86/mm: Switch to generic get_user_page_fast() implementation

arch/x86/Kconfig config X86_DMA_REMAP · line 2793

Quoted 3 lines · Kirill A. Shutemov
arch/x86/Kconfig +3 -0
+config HAVE_GENERIC_RCU_GUP
+	def_bool y
+

Nothing immediately jumped out to me; except that this option might be misnamed.

AFAICT that code does not in fact rely on HAVE_RCU_TABLE_FREE; it will happily work with the (x86) broadcast IPI invalidate model, as you show here.

Architectures that do not do that obviously need HAVE_RCU_TABLE_FREE, but that is not the point I feel.

Also, this code hard relies on IRQ-disable delaying grace periods, which is mostly true I think, but has always been something Paul didn't really want to commit too firmly to.

--
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>

#16 lore raw

Re: [PATCH 7/7] x86/mm: Switch to generic get_user_page_fast() implementation

Quoted 12 lines · Peter Zijlstra

arch/x86/Kconfig config X86_DMA_REMAP · line 2793

Quoted 3 lines · Kirill A. Shutemov
arch/x86/Kconfig +3 -0
+config HAVE_GENERIC_RCU_GUP
+	def_bool y
+

Nothing immediately jumped out to me; except that this option might be misnamed.

AFAICT that code does not in fact rely on HAVE_RCU_TABLE_FREE; it will happily work with the (x86) broadcast IPI invalidate model, as you show here.

Quoted 6 lines

Architectures that do not do that obviously need HAVE_RCU_TABLE_FREE, but that is not the point I feel.

Also, this code hard relies on IRQ-disable delaying grace periods, which is mostly true I think, but has always been something Paul didn't really want to commit too firmly to.

That is quite true!

The only case where IRQ-disable is guaranteed to delay grace periods is when you are using RCU-sched, in other words synchronize_sched() and call_rcu_sched(). And even then, the CPU cannot be in the idle loop, cannot be offline, and cannot be a nohz_full CPU on its way to/from userspace execution.

Thanx, Paul

--
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>

#17 lore raw

PATCH v2 7/7 x86/mm: Switch to generic get_user_page_fast() implementation

In reply to Kirill A. Shutemov

The patch provides all required hooks to match generic get_user_pages_fast() behaviour to x86 and switch x86 over.

---
v2:
- Fix build on non-PAE 32-bit x86;
- Rename HAVE_GENERIC_RCU_GUP to HAVE_GENERIC_GUP and update comment;

---
 arch/arm/Kconfig                      |   2 +-
 arch/arm64/Kconfig                    |   2 +-
 arch/powerpc/Kconfig                  |   2 +-
 arch/x86/Kconfig                      |   3 +
 arch/x86/include/asm/mmu_context.h    |  12 -
 arch/x86/include/asm/pgtable-3level.h |  45 +++
 arch/x86/include/asm/pgtable.h        |  53 ++++
 arch/x86/include/asm/pgtable_64.h     |  16 +-
 arch/x86/mm/Makefile                  |   2 +-
 arch/x86/mm/gup.c                     | 496 ----------------------------------
 mm/Kconfig                            |   2 +-
 mm/gup.c                              |  10 +-
 12 files changed, 126 insertions(+), 519 deletions(-)
 delete mode 100644 arch/x86/mm/gup.c
arch/arm/Kconfig +1 -1
index 0d4e71b42c77..454fadd077ad 100644
@@ -1666,7 +1666,7 @@ config ARCH_SELECT_MEMORY_MODEL
 config HAVE_ARCH_PFN_VALID
 	def_bool ARCH_HAS_HOLES_MEMORYMODEL || !SPARSEMEM
 
-config HAVE_GENERIC_RCU_GUP
+config HAVE_GENERIC_GUP
 	def_bool y
 	depends on ARM_LPAE
 
arch/arm64/Kconfig +1 -1
index 8c7c244247b6..e0306e4b6cc8 100644
@@ -205,7 +205,7 @@ config GENERIC_CALIBRATE_DELAY
 config ZONE_DMA
 	def_bool y
 
-config HAVE_GENERIC_RCU_GUP
+config HAVE_GENERIC_GUP
 	def_bool y
 
 config ARCH_DMA_ADDR_T_64BIT
arch/powerpc/Kconfig +1 -1
index 97a8bc8a095c..3a716b2dcde9 100644
@@ -135,7 +135,7 @@ config PPC
 	select HAVE_FUNCTION_GRAPH_TRACER
 	select HAVE_FUNCTION_TRACER
 	select HAVE_GCC_PLUGINS
-	select HAVE_GENERIC_RCU_GUP
+	select HAVE_GENERIC_GUP
 	select HAVE_HW_BREAKPOINT		if PERF_EVENTS && (PPC_BOOK3S || PPC_8xx)
 	select HAVE_IDE
 	select HAVE_IOREMAP_PROT
arch/x86/Kconfig +3 -0
index 2bab9d093b51..8977d9c77373 100644
@@ -2788,6 +2788,9 @@ config X86_DMA_REMAP
 	bool
 	depends on STA2X11
 
+config HAVE_GENERIC_GUP
+	def_bool y
+
 source "net/Kconfig"
 
 source "drivers/Kconfig"
arch/x86/include/asm/mmu_context.h +0 -12
index 68b329d77b3a..6e933d2d88d9 100644
@@ -220,18 +220,6 @@ static inline int vma_pkey(struct vm_area_struct *vma)
 }
 #endif
 
-static inline bool __pkru_allows_pkey(u16 pkey, bool write)
-{
-	u32 pkru = read_pkru();
-
-	if (!__pkru_allows_read(pkru, pkey))
-		return false;
-	if (write && !__pkru_allows_write(pkru, pkey))
-		return false;
-
-	return true;
-}
-
 /*
  * We only want to enforce protection keys on the current process
  * because we effectively have no access to PKRU for other
arch/x86/include/asm/pgtable-3level.h +45 -0

49 lines not on this page · raw message

arch/x86/include/asm/pgtable.h +53 -0

65 lines not on this page · raw message

arch/x86/include/asm/pgtable_64.h +15 -1
index 73c7ccc38912..1a4bc71534d4 100644
@@ -206,6 +206,20 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
-#endif /* !__ASSEMBLY__ */
+#define gup_fast_permitted gup_fast_permitted
+static inline bool gup_fast_permitted(unsigned long start, int nr_pages,
+		int write)
+{
+	unsigned long len, end;
+
+	len = (unsigned long)nr_pages << PAGE_SHIFT;
+	end = start + len;
+	if (end < start)
+		return false;
+	if (end >> __VIRTUAL_MASK_SHIFT)
+		return false;
+	return true;
+}
 
+#endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/mm/Makefile +1 -1
index 96d2b847e09e..0fbdcb64f9f8 100644
@@ -2,7 +2,7 @@
 KCOV_INSTRUMENT_tlb.o	:= n
 
 obj-y	:=  init.o init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \
-	    pat.o pgtable.o physaddr.o gup.o setup_nx.o tlb.o
+	    pat.o pgtable.o physaddr.o setup_nx.o tlb.o
 
 # Make sure __phys_addr has no stackprotector
 nostackp := $(call cc-option, -fno-stack-protector)
arch/x86/mm/gup.c deleted +0 -496

496 lines not on this page · raw message

mm/Kconfig +1 -1
index 9b8fccb969dc..c89f472b658c 100644
@@ -137,7 +137,7 @@ config HAVE_MEMBLOCK_NODE_MAP
 config HAVE_MEMBLOCK_PHYS_MAP
 	bool
 
-config HAVE_GENERIC_RCU_GUP
+config HAVE_GENERIC_GUP
 	bool
 
 config ARCH_DISCARD_MEMBLOCK
mm/gup.c +5 -5
index a6e21380c8b0..5cc489d98562 100644
@@ -1155,7 +1155,7 @@ struct page *get_dump_page(unsigned long addr)
 #endif /* CONFIG_ELF_CORE */
 
 /*
- * Generic RCU Fast GUP
+ * Generic Fast GUP
  *
  * get_user_pages_fast attempts to pin user pages by walking the page
  * tables directly and avoids taking locks. Thus the walker needs to be
@@ -1176,8 +1176,8 @@ struct page *get_dump_page(unsigned long addr)
  * Before activating this code, please be aware that the following assumptions
  * are currently made:
  *
- *  *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
- *      pages containing page tables.
+ *  *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to
+ *  free pages containing page tables or TLB flushing requires IPI broadcast.
  *
  *  *) ptes can be read atomically by the architecture.
  *
@@ -1187,7 +1187,7 @@ struct page *get_dump_page(unsigned long addr)
  *
  * This code is based heavily on the PowerPC implementation by Nick Piggin.
  */
-#ifdef CONFIG_HAVE_GENERIC_RCU_GUP
+#ifdef CONFIG_HAVE_GENERIC_GUP
 
 #ifndef gup_get_pte
 /*
@@ -1663,4 +1663,4 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int write,
 	return ret;
 }
 
-#endif /* CONFIG_HAVE_GENERIC_RCU_GUP */
+#endif /* CONFIG_HAVE_GENERIC_GUP */
Signature · 1 line
2.11.0
#18 lore raw

Re: [PATCHv2 7/7] x86/mm: Switch to generic get_user_page_fast() implementation

Quoted 7 lines · Kirill A. Shutemov

The patch provides all required hooks to match generic get_user_pages_fast() behaviour to x86 and switch x86 over.

---
v2:
- Fix build on non-PAE 32-bit x86;

Fixup for allmodconfig on x86-64:

arch/x86/include/asm/pgtable.h +1 -1
index e55fe9475979..e1a0a1fb5971 100644
@@ -230,13 +230,13 @@ static inline int pud_devmap(pud_t pud)
 {
 	return 0;
 }
+#endif
 
 static inline int pgd_devmap(pgd_t pgd)
 {
 	return 0;
 }
 #endif
-#endif
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
Signature · 7 lines
 Kirill A. Shutemov

--
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>
#19 lore raw

[tip:x86/mm] x86/mm/gup: Switch GUP to the generic get_user_page_fast() implementation

In reply to Kirill A. Shutemov
x86/mm/gup: Switch GUP to the generic get_user_page_fast() implementation
Commit-ID:  2947ba054a4dabbd82848728d765346886050029
Gitweb:     http://git.kernel.org/tip/2947ba054a4dabbd82848728d765346886050029
Author:     Kirill A. Shutemov <[email protected]>
AuthorDate: Fri, 17 Mar 2017 00:39:06 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 18 Mar 2017 09:48:03 +0100

x86/mm/gup: Switch GUP to the generic get_user_page_fast() implementation

This patch provides all required callbacks required by the generic get_user_pages_fast() code and switches x86 over - and removes the platform specific implementation.

Cc Andrew Morton <[email protected]>
Cc Aneesh Kumar K . V <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dann Frazier <[email protected]>
Cc Dave Hansen <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Steve Capper <[email protected]>
Cc Thomas Gleixner <[email protected]>
[ Minor readability edits. ]
---
 arch/arm/Kconfig                      |   2 +-
 arch/arm64/Kconfig                    |   2 +-
 arch/powerpc/Kconfig                  |   2 +-
 arch/x86/Kconfig                      |   3 +
 arch/x86/include/asm/mmu_context.h    |  12 -
 arch/x86/include/asm/pgtable-3level.h |  47 ++++
 arch/x86/include/asm/pgtable.h        |  53 ++++
 arch/x86/include/asm/pgtable_64.h     |  16 +-
 arch/x86/mm/Makefile                  |   2 +-
 arch/x86/mm/gup.c                     | 496 ----------------------------------
 mm/Kconfig                            |   2 +-
 mm/gup.c                              |  10 +-
 12 files changed, 128 insertions(+), 519 deletions(-)
arch/arm/Kconfig +1 -1
index 0d4e71b..454fadd 100644
@@ -1666,7 +1666,7 @@ config ARCH_SELECT_MEMORY_MODEL
 config HAVE_ARCH_PFN_VALID
 	def_bool ARCH_HAS_HOLES_MEMORYMODEL || !SPARSEMEM
 
-config HAVE_GENERIC_RCU_GUP
+config HAVE_GENERIC_GUP
 	def_bool y
 	depends on ARM_LPAE
 
arch/arm64/Kconfig +1 -1
index 3741859..af62bf7 100644
@@ -205,7 +205,7 @@ config GENERIC_CALIBRATE_DELAY
 config ZONE_DMA
 	def_bool y
 
-config HAVE_GENERIC_RCU_GUP
+config HAVE_GENERIC_GUP
 	def_bool y
 
 config ARCH_DMA_ADDR_T_64BIT
arch/powerpc/Kconfig +1 -1
index 97a8bc8..3a716b2 100644
@@ -135,7 +135,7 @@ config PPC
 	select HAVE_FUNCTION_GRAPH_TRACER
 	select HAVE_FUNCTION_TRACER
 	select HAVE_GCC_PLUGINS
-	select HAVE_GENERIC_RCU_GUP
+	select HAVE_GENERIC_GUP
 	select HAVE_HW_BREAKPOINT		if PERF_EVENTS && (PPC_BOOK3S || PPC_8xx)
 	select HAVE_IDE
 	select HAVE_IOREMAP_PROT
arch/x86/Kconfig +3 -0
index 2bab9d0..8977d9c 100644
@@ -2788,6 +2788,9 @@ config X86_DMA_REMAP
 	bool
 	depends on STA2X11
 
+config HAVE_GENERIC_GUP
+	def_bool y
+
 source "net/Kconfig"
 
 source "drivers/Kconfig"
arch/x86/include/asm/mmu_context.h +0 -12
index 68b329d..6e933d2 100644
@@ -220,18 +220,6 @@ static inline int vma_pkey(struct vm_area_struct *vma)
 }
 #endif
 
-static inline bool __pkru_allows_pkey(u16 pkey, bool write)
-{
-	u32 pkru = read_pkru();
-
-	if (!__pkru_allows_read(pkru, pkey))
-		return false;
-	if (write && !__pkru_allows_write(pkru, pkey))
-		return false;
-
-	return true;
-}
-
 /*
  * We only want to enforce protection keys on the current process
  * because we effectively have no access to PKRU for other
arch/x86/include/asm/pgtable-3level.h +47 -0

51 lines not on this page · raw message

arch/x86/include/asm/pgtable.h +53 -0

65 lines not on this page · raw message

arch/x86/include/asm/pgtable_64.h +15 -1
index 67608d4..13709cf 100644
@@ -207,6 +207,20 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
-#endif /* !__ASSEMBLY__ */
+#define gup_fast_permitted gup_fast_permitted
+static inline bool gup_fast_permitted(unsigned long start, int nr_pages,
+		int write)
+{
+	unsigned long len, end;
+
+	len = (unsigned long)nr_pages << PAGE_SHIFT;
+	end = start + len;
+	if (end < start)
+		return false;
+	if (end >> __VIRTUAL_MASK_SHIFT)
+		return false;
+	return true;
+}
 
+#endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/mm/Makefile +1 -1
index 96d2b84..0fbdcb6 100644
@@ -2,7 +2,7 @@
 KCOV_INSTRUMENT_tlb.o	:= n
 
 obj-y	:=  init.o init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \
-	    pat.o pgtable.o physaddr.o gup.o setup_nx.o tlb.o
+	    pat.o pgtable.o physaddr.o setup_nx.o tlb.o
 
 # Make sure __phys_addr has no stackprotector
 nostackp := $(call cc-option, -fno-stack-protector)
arch/x86/mm/gup.c deleted +0 -496

496 lines not on this page · raw message

mm/Kconfig +1 -1
index 9b8fccb..c89f472 100644
@@ -137,7 +137,7 @@ config HAVE_MEMBLOCK_NODE_MAP
 config HAVE_MEMBLOCK_PHYS_MAP
 	bool
 
-config HAVE_GENERIC_RCU_GUP
+config HAVE_GENERIC_GUP
 	bool
 
 config ARCH_DISCARD_MEMBLOCK
mm/gup.c +5 -5
index 527ec2c..2559a39 100644
@@ -1155,7 +1155,7 @@ struct page *get_dump_page(unsigned long addr)
 #endif /* CONFIG_ELF_CORE */
 
 /*
- * Generic RCU Fast GUP
+ * Generic Fast GUP
  *
  * get_user_pages_fast attempts to pin user pages by walking the page
  * tables directly and avoids taking locks. Thus the walker needs to be
@@ -1176,8 +1176,8 @@ struct page *get_dump_page(unsigned long addr)
  * Before activating this code, please be aware that the following assumptions
  * are currently made:
  *
- *  *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
- *      pages containing page tables.
+ *  *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to
+ *  free pages containing page tables or TLB flushing requires IPI broadcast.
  *
  *  *) ptes can be read atomically by the architecture.
  *
@@ -1187,7 +1187,7 @@ struct page *get_dump_page(unsigned long addr)
  *
  * This code is based heavily on the PowerPC implementation by Nick Piggin.
  */
-#ifdef CONFIG_HAVE_GENERIC_RCU_GUP
+#ifdef CONFIG_HAVE_GENERIC_GUP
 
 #ifndef gup_get_pte
 /*
@@ -1677,4 +1677,4 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int write,
 	return ret;
 }
 
-#endif /* CONFIG_HAVE_GENERIC_RCU_GUP */
+#endif /* CONFIG_HAVE_GENERIC_GUP */
#20 lore raw
In reply to Kirill A. Shutemov
Quoted 3 lines · Kirill A. Shutemov

The patcheset generalize mm/gup.c implementation of get_user_pages_fast() to be usable for x86 and switches x86 over.

Thanks for doing this, it looks good and removes more lines than it adds.

And despite removing lines, it should make it easier for other architectures to support devmap if they ever want to. I hadn't even noticed that difference in the GUP implementations.

Linus

#21 lore raw
In reply to Kirill A. Shutemov
Quoted 25 lines · Kirill A. Shutemov

Hi,

The patcheset generalize mm/gup.c implementation of get_user_pages_fast() to be usable for x86 and switches x86 over.

Please review and consider applying.

Kirill A. Shutemov (7):
mm: Drop arch_pte_access_permitted() mmu hook
mm/gup: Move permission checks into helpers
mm/gup: Move page table entry dereference into helper mm/gup: Make pages referenced during generic get_user_pages_fast() mm/gup: Implement dev_pagemap logic in generic get_user_pages_fast() mm/gup: Provide hook to check if __GUP_fast() is allowed for the range x86/mm: Switch to generic get_user_page_fast() implementation

 arch/powerpc/include/asm/mmu_context.h   |   6 -
 arch/s390/include/asm/mmu_context.h      |   6 -
 arch/um/include/asm/mmu_context.h        |   6 -
 arch/unicore32/include/asm/mmu_context.h |   6 -
 arch/x86/Kconfig                         |   3 +
 arch/x86/include/asm/mmu_context.h       |  16 -
 arch/x86/include/asm/pgtable-3level.h    |  45 +++
 arch/x86/include/asm/pgtable.h           |  53 ++++
 arch/x86/include/asm/pgtable_64.h        |  16 +-
Quoted 8 lines
 arch/x86/mm/Makefile                     |   2 +-
 arch/x86/mm/gup.c                        | 496 -------------------------------
 include/asm-generic/mm_hooks.h           |   6 -
 include/asm-generic/pgtable.h            |  25 ++
 include/linux/mm.h                       |   4 +
 mm/gup.c                                 | 134 +++++++--
 15 files changed, 262 insertions(+), 562 deletions(-)
 delete mode 100644 arch/x86/mm/gup.c

It fails to build on x86-64 defconfig/allyesconfig/allmodconfig:

mm/gup.c:1422:15: error: implicit declaration of function ‘pgd_devmap’ [-Werror=implicit-function-declaration]

The PowerPC allnoconfig build broke as well:

/home/mingo/tip/mm/gup.c: In function '__gup_device_huge_pmd':
/home/mingo/tip/mm/gup.c:1319:2: error: implicit declaration of function 'pmd_pfn' [-Werror=implicit-function-declaration]

Please send delta fixes, because I've already done many small readability edits to the patches.

Thanks,

Ingo

--
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>