x86: implement pte_special
Subsystems (6)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/5 x86: implement pte_special
Implement the pte_special bit for x86. This is required to support lockless get_user_pages, because we need to know whether or not we can refcount a particular page given only its pte (and no vma).
--- include/asm-x86/pgtable.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
include/asm-x86/pgtable.h +5 -2
@@ -17,6 +17,7 @@#define _PAGE_BIT_UNUSED1 9/* available for programmer */ #define _PAGE_BIT_UNUSED2 10#define _PAGE_BIT_UNUSED3 11 +#define _PAGE_BIT_SPECIAL _PAGE_BIT_UNUSED1#define _PAGE_BIT_PAT_LARGE 12/* On 2MB or 1GB pages */ #define _PAGE_BIT_NX 63/* No execute: only valid after cpuid check */
@@ -39,6 +40,8 @@#define _PAGE_UNUSED3 (_AC(1, L)<<_PAGE_BIT_UNUSED3)#define _PAGE_PAT (_AC(1, L)<<_PAGE_BIT_PAT)#define _PAGE_PAT_LARGE (_AC(1, L)<<_PAGE_BIT_PAT_LARGE) +#define _PAGE_SPECIAL (_AC(1, L)<<_PAGE_BIT_SPECIAL) +#define __HAVE_ARCH_PTE_SPECIAL#if defined(CONFIG_X86_64 ) || defined(CONFIG_X86_PAE )#define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)
@@ -199,7 +202,7 @@ static inline int pte_exec(pte_t pte)static inline int pte_special (pte_t pte) { -return 0 ; +return pte_val (pte) & _PAGE_SPECIAL; }static inline int pmd_large (pmd_t pte)
@@ -265,7 +268,7 @@ static inline pte_t pte_clrglobal(pte_tstatic inline pte_t pte_mkspecial (pte_t pte) { -return pte; +return __pte (pte_val (pte) | _PAGE_SPECIAL); }extern pteval_t __supported_pte_mask;
Signature · 5 lines
-- 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>
Andrew Morton
maintainerQuoted 3 lines · On Thu, 29 May 2008 22:20:51 +1000 [email protected] wrote:
Implement the
pte_specialbit for x86. This is required to support locklessget_user_pages, because we need to know whether or not we can refcount a particular page given only its pte (and no vma).
Spits this reject:
***************
*** 39,44 ****
#define _PAGE_UNUSED3 (_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define _PAGE_PAT (_AC(1, L)<<_PAGE_BIT_PAT)
#define _PAGE_PAT_LARGE (_AC(1, L)<<_PAGE_BIT_PAT_LARGE)
#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
#define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)
--- 40,47 ----
#define _PAGE_UNUSED3 (_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define _PAGE_PAT (_AC(1, L)<<_PAGE_BIT_PAT)
#define _PAGE_PAT_LARGE (_AC(1, L)<<_PAGE_BIT_PAT_LARGE)
+ #define _PAGE_SPECIAL (_AC(1, L)<<_PAGE_BIT_SPECIAL)
+ #define __HAVE_ARCH_PTE_SPECIAL
#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) #define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)
Which I fixed thusly:
#define _PAGE_PAT (_AT(pteval_t, 1) << _PAGE_BIT_PAT)
#define _PAGE_PAT_LARGE (_AT(pteval_t, 1) << _PAGE_BIT_PAT_LARGE)
#define _PAGE_SPECIAL (_AT(pteval_t, 1) << _PAGE_BIT_SPECIAL)
#define __HAVE_ARCH_PTE_SPECIAL
OK?
(Also please check the bunch of checkpatch fixes, a warning fix and a compile fix).
--
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>
Quoted 29 lines · Andrew Morton
Quoted 3 lines · On Thu, 29 May 2008 22:20:51 +1000 [email protected] wrote:
Implement the
pte_specialbit for x86. This is required to support locklessget_user_pages, because we need to know whether or not we can refcount a particular page given only its pte (and no vma).Spits this reject:
***************
*** 39,44 ****
#define_PAGE_UNUSED3(_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define_PAGE_PAT(_AC(1, L)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AC(1, L)<<_PAGE_BIT_PAT_LARGE)#if
defined(CONFIG_X86_64)||defined(CONFIG_X86_PAE)
#define_PAGE_NX(_AC(1, ULL)<<_PAGE_BIT_NX)
--- 40,47 ----
#define_PAGE_UNUSED3(_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define_PAGE_PAT(_AC(1, L)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AC(1, L)<<_PAGE_BIT_PAT_LARGE)
+ #define_PAGE_SPECIAL(_AC(1, L)<<_PAGE_BIT_SPECIAL)
+ #define__HAVE_ARCH_PTE_SPECIAL#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) #define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)Which I fixed thusly:
Quoted 7 lines
#define
_PAGE_PAT(_AT(pteval_t, 1)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AT(pteval_t, 1)<<_PAGE_BIT_PAT_LARGE)
#define_PAGE_SPECIAL(_AT(pteval_t, 1)<<_PAGE_BIT_SPECIAL)
#define__HAVE_ARCH_PTE_SPECIALOK?
I think so, thanks.
Quoted 2 lines
(Also please check the bunch of checkpatch fixes, a warning fix and a compile fix).
Ah, I forgot to rerun checkpatch after renaming it from fast_gup. Missed the compile bug though... perhaps I was getting the definition pulled in some other way... hmm, will investigate, but it looks good.
--
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>
Quoted 35 lines · Andrew Morton
Quoted 3 lines · On Thu, 29 May 2008 22:20:51 +1000 [email protected] wrote:
Implement the
pte_specialbit for x86. This is required to support locklessget_user_pages, because we need to know whether or not we can refcount a particular page given only its pte (and no vma).Spits this reject:
***************
*** 39,44 ****
#define_PAGE_UNUSED3(_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define_PAGE_PAT(_AC(1, L)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AC(1, L)<<_PAGE_BIT_PAT_LARGE)#if
defined(CONFIG_X86_64)||defined(CONFIG_X86_PAE)
#define_PAGE_NX(_AC(1, ULL)<<_PAGE_BIT_NX)
--- 40,47 ----
#define_PAGE_UNUSED3(_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define_PAGE_PAT(_AC(1, L)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AC(1, L)<<_PAGE_BIT_PAT_LARGE)
+ #define_PAGE_SPECIAL(_AC(1, L)<<_PAGE_BIT_SPECIAL)
+ #define__HAVE_ARCH_PTE_SPECIAL#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) #define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)Which I fixed thusly:
#define
_PAGE_PAT(_AT(pteval_t, 1)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AT(pteval_t, 1)<<_PAGE_BIT_PAT_LARGE)
#define_PAGE_SPECIAL(_AT(pteval_t, 1)<<_PAGE_BIT_SPECIAL)
#define__HAVE_ARCH_PTE_SPECIAL
Quoted 5 lines
OK?
(Also please check the bunch of checkpatch fixes, a warning fix and a compile fix).
That looks a sane merge to me. I had a quick look over the various fixes and they all look fine to me.
-apw
--
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>
Quoted 36 lines · Andy Whitcroft
Quoted 35 lines · Andrew Morton
Quoted 3 lines · On Thu, 29 May 2008 22:20:51 +1000 [email protected] wrote:
Implement the
pte_specialbit for x86. This is required to support locklessget_user_pages, because we need to know whether or not we can refcount a particular page given only its pte (and no vma).Spits this reject:
***************
*** 39,44 ****
#define_PAGE_UNUSED3(_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define_PAGE_PAT(_AC(1, L)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AC(1, L)<<_PAGE_BIT_PAT_LARGE)#if
defined(CONFIG_X86_64)||defined(CONFIG_X86_PAE)
#define_PAGE_NX(_AC(1, ULL)<<_PAGE_BIT_NX)
--- 40,47 ----
#define_PAGE_UNUSED3(_AC(1, L)<<_PAGE_BIT_UNUSED3)
#define_PAGE_PAT(_AC(1, L)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AC(1, L)<<_PAGE_BIT_PAT_LARGE)
+ #define_PAGE_SPECIAL(_AC(1, L)<<_PAGE_BIT_SPECIAL)
+ #define__HAVE_ARCH_PTE_SPECIAL#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) #define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)Which I fixed thusly:
#define
_PAGE_PAT(_AT(pteval_t, 1)<<_PAGE_BIT_PAT)
#define_PAGE_PAT_LARGE(_AT(pteval_t, 1)<<_PAGE_BIT_PAT_LARGE)
#define_PAGE_SPECIAL(_AT(pteval_t, 1)<<_PAGE_BIT_SPECIAL)
#define__HAVE_ARCH_PTE_SPECIAL
Quoted 8 lines
Quoted 5 lines
OK?
(Also please check the bunch of checkpatch fixes, a warning fix and a compile fix).
That looks a sane merge to me. I had a quick look over the various fixes and they all look fine to me.
That means we can put your reviewed-by: back? ;)
--
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>
Quoted 10 lines · On Thu, 2008-05-29 at 22:20 +1000, [email protected] wrote:
plain text document attachment (x86-implement-pte_special.patch) Implement the
pte_specialbit for x86. This is required to support locklessget_user_pages, because we need to know whether or not we can refcount a particular page given only its pte (and no vma).Signed-off-by Nick Piggin <[email protected]>
Full series:
--
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>
PATCH 2/5 mm: introduce get_user_pages_fast
In reply to a message not in the archiveIntroduce a new get_user_pages_fast mm API, which is basically a get_user_pages with a less general API (but still tends to be suited to the common case):
- task and mm are always current and current->mm
- force is always 0
- pages is always non-NULL
- don't pass back vmas
This restricted API can be implemented in a much more scalable way on many architectures when the ptes are present, by walking the page tables locklessly (no mmap_sem or page table locks). When the ptes are not populated, get_user_pages_fast() could be slower.
This is implemented locklessly on x86, and used in some key direct IO call sites, in later patches, which provides nearly 10% performance improvement on a threaded database workload.
Lots of other code could use this too, depending on use cases (eg. grep drivers/). And it might inspire some new and clever ways to use it.
--- include/linux/mm.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
include/linux/mm.h +33 -0
@@ -12,6 +12,7 @@#include <linux/prio_tree.h> #include <linux/debug_locks.h> #include <linux/mm_types.h> +#include <linux/uaccess.h> /* for __HAVE_ARCH_GET_USER_PAGES_FAST */ struct mempolicy ;struct anon_vma ;
@@ -830,6 +831,38 @@ extern int mprotect_fixup(struct vm_area struct vm_area_struct **pprev, unsigned long start, unsigned long end, unsigned long newflags); +#ifdef __HAVE_ARCH_GET_USER_PAGES_FAST +/* +* get_user_pages_fast provides equivalent functionality to get_user_pages, +* operating on current and current->mm (force=0 and doesn't return any vmas). +* +* get_user_pages_fast may take mmap_sem and page tables, so no assumptions +* can be made about locking. get_user_pages_fast is to be implemented in a +* way that is advantageous (vs get_user_pages()) when the user memory area is +* already faulted in and present in ptes. However if the pages have to be +* faulted in, it may turn out to be slightly slower). +*/ +int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages); + +#else +/* +* Should probably be moved to asm-generic, and architectures can include it if +* they don't implement their own get_user_pages_fast. +*/ +#define get_user_pages_fast (start, nr_pages, write, pages) \ +({ \ + struct mm_struct *mm = current->mm; \ + int ret; \ + \ + down_read(&mm->mmap_sem); \ + ret = get_user_pages(current, mm, start, nr_pages, \ + write, 0, pages, NULL); \ + up_read(&mm->mmap_sem); \ + \ + ret; \ +}) +#endif +/* * A callback you can register to apply pressure to ageable caches. *
Signature · 5 lines
-- 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>
Andrew Morton
maintainerRe: [patch 2/5] mm: introduce get_user_pages_fast
Quoted 25 lines · On Thu, 29 May 2008 22:20:52 +1000 [email protected] wrote:
Introduce a new
get_user_pages_fastmm API, which is basically aget_user_pageswith a less general API (but still tends to be suited to the common case):- task and mm are always current and
current->mm
- force is always 0
- pages is always non-NULL
- don't pass back vmasThis restricted API can be implemented in a much more scalable way on many architectures when the ptes are present, by walking the page tables locklessly (no
mmap_semor page table locks). When the ptes are not populated,get_user_pages_fast()could be slower.This is implemented locklessly on x86, and used in some key direct IO call sites, in later patches, which provides nearly 10% performance improvement on a threaded database workload.
Lots of other code could use this too, depending on use cases (eg. grep
drivers/). And it might inspire some new and clever ways to use it....
include/linux/mm.h line 16
Quoted 5 lines
include/linux/mm.h +1
-0@@ -12,6 +12,7 @@#include <linux/prio_tree.h> #include <linux/debug_locks.h> #include <linux/mm_types.h> +#include <linux/uaccess.h> /* for __HAVE_ARCH_GET_USER_PAGES_FAST */
That breaks ia64:
In file included from include/linux/mm.h:15,
from include/asm/uaccess.h:39,
from include/linux/poll.h:13,
from include/linux/rtc.h:113,
from include/linux/efi.h:19,
from include/asm/sal.h:40,
from include/asm-ia64/mca.h:20,
from arch/ia64/kernel/asm-offsets.c:17:include/linux/uaccess.h: In function `__copy_from_user_inatomic_nocache':include/linux/uaccess.h:46: error: implicit declaration of function __copy_from_user_inatomic' include/linux/uaccess.h: In function __copy_from_user_nocache':include/linux/uaccess.h:52: error: implicit declaration of function `__copy_from_user'
It shouldn't have been a __HAVE_ARCH_whatever anyway - it should have been a CONFIG_whatever.
I'll fix it.
--
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>
PATCH 3/5 x86: lockless get_user_pages_fast
In reply to a message not in the archiveImplement get_user_pages_fast without locking in the fastpath on x86.
Do an optimistic lockless pagetable walk, without taking mmap_sem or any page table locks or even mmap_sem. Page table existence is guaranteed by turning interrupts off (combined with the fact that we're always looking up the current mm, means we can do the lockless page table walk within the constraints of the TLB shootdown design). Basically we can do this lockless pagetable walk in a similar manner to the way the CPU's pagetable walker does not have to take any locks to find present ptes.
This patch (combined with the subsequent ones to convert direct IO to use it) was found to give about 10% performance improvement on a 2 socket 8 core Intel Xeon system running an OLTP workload on DB2 v9.5
"To test the effects of the patch, an OLTP workload was run on an IBM x3850 M2 server with 2 processors (quad-core Intel Xeon processors at 2.93 GHz) using IBM DB2 v9.5 running Linux 2.6.24rc7 kernel. Comparing runs with and without the patch resulted in an overall performance benefit of ~9.8%. Correspondingly, oprofiles showed that samples from __up_read and __down_read routines that is seen during thread contention for system resources was reduced from 2.8% down to .05%. Monitoring the /proc/vmstat output from the patched run showed that the counter for fast_gup contained a very high number while the fast_gup_slow value was zero."
(fast_gup is the old name for get_user_pages_fast, fast_gup_slow is a counter we had for the number of times the slowpath was invoked).
The main reason for the improvement is that DB2 has multiple threads each issuing direct-IO. Direct-IO uses get_user_pages, and thus the threads contend the mmap_sem cacheline, and can also contend on page table locks.
I would anticipate larger performance gains on larger systems, however I think DB2 uses an adaptive mix of threads and processes, so it could be that thread contention remains pretty constant as machine size increases. In which case, we stuck with "only" a 10% gain.
The downside of using get_user_pages_fast is that if there is not a pte with the correct permissions for the access, we end up falling back to get_user_pages and so the get_user_pages_fast is a bit of extra work. However this should not be the common case in most performance critical code.
--- arch/x86/mm/Makefile | 2 arch/x86/mm/gup.c | 254 ++++++++++++++++++++++++++++++++++++++++++++++ include/asm-x86/uaccess.h | 3 3 files changed, 258 insertions(+), 1 deletion(-)
arch/x86/mm/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y := init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \ - pat.o pgtable.o + pat.o pgtable.o gup.o obj-$(CONFIG_X86_32) += pgtable_32.o
arch/x86/mm/gup.c new file +254 -0
254 lines not on this page · raw message
include/asm-x86/uaccess.h +3 -0
@@ -3,3 +3,6 @@#else # include "uaccess_64.h" #endif + +#define __HAVE_ARCH_GET_USER_PAGES_FAST +
Signature · 5 lines
-- 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>
Re: [patch 3/5] x86: lockless get_user_pages_fast
Quoted 46 lines · On Thu, 2008-05-29 at 22:20 +1000, [email protected] wrote:
arch/x86/mm/gup.c new file +46
-0+int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages) +{ +struct mm_struct *mm = current->mm; +unsigned long end = start + (nr_pages <<PAGE_SHIFT ); +unsigned long addr = start; +unsigned long next; +pgd_t *pgdp; +int nr =0 ; + +if (unlikely (!access_ok (write ?VERIFY_WRITE :VERIFY_READ , + start, nr_pages*PAGE_SIZE ))) + goto slow_irqon; + +/* +* XXX: batch / limit 'nr', to avoid large irq off latency +* needs some instrumenting to determine the common sizes used by +* important workloads (eg. DB2), and whether limiting the batch size +* will decrease performance. +* +* It seems like we're in the clear for the moment. Direct-IO is +* the main guy that batches up lots of get_user_pages, and even +* they are limited to 64-at-a-time which is not so many. +*/ +/* +* This doesn't prevent pagetable teardown, but does prevent +* the pagetables and pages from being freed on x86. +* +* So long as we atomically load page table pointers versus teardown +* (which we do on x86, with the above PAE exception), we can follow the +* address down to the the page and take a ref on it. +*/ +local_irq_disable (); + pgdp =pgd_offset (mm, addr); +do { +pgd_t pgd = *pgdp; + + next =pgd_addr_end (addr, end); +if (pgd_none (pgd)) + goto slow; +if (!gup_pud_range (pgd, addr, next, write, pages, &nr)) + goto slow; + }while (pgdp++, addr = next, addr != end); +local_irq_enable (); + +VM_BUG_ON (nr != (end - start) >>PAGE_SHIFT ); +return nr;
arch/x86/mm/gup.c line 237
Quoted 10 lines
arch/x86/mm/gup.c new file +10
-0+ + { +int i, ret; + +slow: +local_irq_enable (); +slow_irqon: +/* Try to get the remaining pages with get_user_pages */ + start += nr <<PAGE_SHIFT ; + pgaes += nr;
Typo: s/pgaes/pages/
Quoted 17 lines
arch/x86/mm/gup.c new file +17
-0+ +down_read (&mm->mmap_sem); + ret =get_user_pages (current, mm, start, + (end - start) >>PAGE_SHIFT , write,0 , pages,NULL ); +up_read (&mm->mmap_sem); + +/* Have to be a bit careful with return values */ +if (nr >0 ) { +if (ret <0 ) + ret = nr; +else + ret += nr; + } + +return ret; + } +}
Signature · 8 lines
David Kleikamp IBM Linux Technology Center -- 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>
Re: [patch 3/5] x86: lockless get_user_pages_fast
Quoted 52 lines · Dave Kleikamp
Quoted 50 lines · On Thu, 2008-05-29 at 22:20 +1000, [email protected] wrote:
(quoted diff) +50
-0+int get_user_pages_fast(unsigned long start, int nr_pages, int write, struct page **pages) +{ + struct mm_struct *mm = current->mm; + unsigned long end = start + (nr_pages << PAGE_SHIFT); + unsigned long addr = start; + unsigned long next; + pgd_t *pgdp; + int nr = 0; + + if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ, + start, nr_pages*PAGE_SIZE))) + goto slow_irqon; + + /* + * XXX: batch / limit 'nr', to avoid large irq off latency + * needs some instrumenting to determine the common sizes used by + * important workloads (eg. DB2), and whether limiting the batch size + * will decrease performance. + * + * It seems like we're in the clear for the moment. Direct-IO is + * the main guy that batches up lots of get_user_pages, and even + * they are limited to 64-at-a-time which is not so many. + */ + /* + * This doesn't prevent pagetable teardown, but does prevent + * the pagetables and pages from being freed on x86. + * + * So long as we atomically load page table pointers versus teardown + * (which we do on x86, with the above PAE exception), we can follow the + * address down to the the page and take a ref on it. + */ + local_irq_disable(); + pgdp = pgd_offset(mm, addr); + do { + pgd_t pgd = *pgdp; + + next = pgd_addr_end(addr, end); + if (pgd_none(pgd)) + goto slow; + if (!gup_pud_range(pgd, addr, next, write, pages, &nr)) + goto slow; + } while (pgdp++, addr = next, addr != end); + local_irq_enable(); + + VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT); + return nr; + + { + int i, ret; +
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+slow: + local_irq_enable(); +slow_irqon: + /* Try to get the remaining pages with get_user_pages */ + start += nr << PAGE_SHIFT; + pgaes += nr;Typo: s/pgaes/pages/
Gah, missing quilt refresh. Sorry.
I actually did stick a printk in here and manage to hit this path with a constructed test case (and with nr ! always = 0 to boot). It seemed to work fine.
BTW. Andy, I dropped your Reviewed-by: Andy Whitcroft <[email protected]> because I did make a couple of these little changes that technically you hadn't reviewed. I don't know what the exact protocol is regarding the fluidity of RB/AB...
---
x86: lockless get_user_pages_fast
Implement get_user_pages_fast without locking in the fastpath on x86.
Do an optimistic lockless pagetable walk, without taking mmap_sem or any page table locks or even mmap_sem. Page table existence is guaranteed by turning interrupts off (combined with the fact that we're always looking up the current mm, means we can do the lockless page table walk within the constraints of the TLB shootdown design). Basically we can do this lockless pagetable walk in a similar manner to the way the CPU's pagetable walker does not have to take any locks to find present ptes.
This patch (combined with the subsequent ones to convert direct IO to use it) was found to give about 10% performance improvement on a 2 socket 8 core Intel Xeon system running an OLTP workload on DB2 v9.5
"To test the effects of the patch, an OLTP workload was run on an IBM x3850 M2 server with 2 processors (quad-core Intel Xeon processors at 2.93 GHz) using IBM DB2 v9.5 running Linux 2.6.24rc7 kernel. Comparing runs with and without the patch resulted in an overall performance benefit of ~9.8%. Correspondingly, oprofiles showed that samples from __up_read and __down_read routines that is seen during thread contention for system resources was reduced from 2.8% down to .05%. Monitoring the /proc/vmstat output from the patched run showed that the counter for fast_gup contained a very high number while the fast_gup_slow value was zero."
(fast_gup is the old name for get_user_pages_fast, fast_gup_slow is a counter we had for the number of times the slowpath was invoked).
The main reason for the improvement is that DB2 has multiple threads each issuing direct-IO. Direct-IO uses get_user_pages, and thus the threads contend the mmap_sem cacheline, and can also contend on page table locks.
I would anticipate larger performance gains on larger systems, however I think DB2 uses an adaptive mix of threads and processes, so it could be that thread contention remains pretty constant as machine size increases. In which case, we stuck with "only" a 10% gain.
The downside of using get_user_pages_fast is that if there is not a pte with the correct permissions for the access, we end up falling back to get_user_pages and so the get_user_pages_fast is a bit of extra work. However this should not be the common case in most performance critical code.
--- arch/x86/mm/Makefile | 2 arch/x86/mm/gup.c | 254 ++++++++++++++++++++++++++++++++++++++++++++++ include/asm-x86/uaccess.h | 3 3 files changed, 258 insertions(+), 1 deletion(-)
arch/x86/mm/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y := init_$(BITS).o fault.o ioremap.o extable.o pageattr.o mmap.o \ - pat.o pgtable.o + pat.o pgtable.o gup.o obj-$(CONFIG_X86_32) += pgtable_32.o
arch/x86/mm/gup.c new file +254 -0
254 lines not on this page · raw message
include/asm-x86/uaccess.h +3 -0
@@ -3,3 +3,6 @@#else # include "uaccess_64.h" #endif + +#define __HAVE_ARCH_GET_USER_PAGES_FAST +
--
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>
Re: [patch 3/5] x86: lockless get_user_pages_fast
In reply to Dave KleikampBTW. I do plan to ask Linus to merge this as soon as 2.6.27 opens. Hope nobody objects (or if they do please speak up before then)
Quoted 3 lines · Dave Kleikamp
Quoted 1 line · On Thu, 2008-05-29 at 22:20 +1000, [email protected] wrote:
+int
get_user_pages_fast(unsigned long start, int nr_pages, int write, struct page **pages)
--
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>
Re: [patch 3/5] x86: lockless get_user_pages_fast
Hi Nick,
Quoted 3 lines · On Mon, 2 Jun 2008 12:15:30 +0200 Nick Piggin <[email protected]> wrote:
BTW. I do plan to ask Linus to merge this as soon as 2.6.27 opens. Hope nobody objects (or if they do please speak up before then)
Any chance of getting this into linux-next then to see if it conflicts with/kills anything else?
If this is posted/reviewed/tested enough to be "finished" then put it in a tree (or quilt series) and submit it.
Thanks.
Signature · 3 lines
Cheers, Stephen Rothwell [email protected] http://www.canb.auug.org.au/~sfr/
Re: [patch 3/5] x86: lockless get_user_pages_fast
Quoted 4 lines · Stephen Rothwell
Hi Nick,
Quoted 1 line · On Mon, 2 Jun 2008 12:15:30 +0200 Nick Piggin <[email protected]> wrote:
Quoted 8 lines
Quoted 2 lines
BTW. I do plan to ask Linus to merge this as soon as 2.6.27 opens. Hope nobody objects (or if they do please speak up before then)
Any chance of getting this into linux-next then to see if it conflicts with/kills anything else?
If this is posted/reviewed/tested enough to be "finished" then put it in a tree (or quilt series) and submit it.
Hi Stephen,
Thanks for the offer... I was hoping for Andrew to pick it up (which he now has).
I'm not sure how best to do mm/ related stuff, but I suspect we have gone as smoothly as we are in large part due to Andrew's reviewing and martialling mm patches so well.
Not saying that wouldn't happen if the patches went to linux-next, but I'm quite happy with how -mm works for mm development, so I will prefer to submit to -mm unless Andrew asks otherwise.
For other developments I'll keep linux-next in mind. I guess it will be useful for me eg in the case where I change an arch defined prototype that requires a big sweep of the tree.
Thanks,
Nick
--
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>
Re: [patch 3/5] x86: lockless get_user_pages_fast
Hi Nick,
Quoted 7 lines · On Tue, 3 Jun 2008 04:34:20 +0200 Nick Piggin <[email protected]> wrote:
Thanks for the offer... I was hoping for Andrew to pick it up (which he now has).
I'm not sure how best to do
mm/related stuff, but I suspect we have gone as smoothly as we are in large part due to Andrew's reviewing and martialling mm patches so well.
Yeah, that is the correct and best way to go.
Quoted 3 lines
For other developments I'll keep linux-next in mind. I guess it will be useful for me eg in the case where I change an arch defined prototype that requires a big sweep of the tree.
Yep, linux-next is idea for that because you find out all the places you step on other people's toes :-)
Signature · 3 lines
Cheers, Stephen Rothwell [email protected] http://www.canb.auug.org.au/~sfr/
PATCH 4/5 dio: use get_user_pages_fast
In reply to a message not in the archiveUse get_user_pages_fast in the common/generic block and fs direct IO paths.
--- fs/bio.c | 8 ++------ fs/direct-io.c | 10 ++-------- 2 files changed, 4 insertions(+), 14 deletions(-)
fs/bio.c +2 -6
@@ -713,12 +713,8 @@ static struct bio *__bio_map_user_iov(stconst int local_nr_pages = end - start;const int page_limit = cur_page + local_nr_pages; -down_read (¤t->mm->mmap_sem); - ret =get_user_pages (current, current->mm, uaddr, - local_nr_pages, - write_to_vm,0 , &pages[cur_page],NULL ); -up_read (¤t->mm->mmap_sem); - + ret =get_user_pages_fast (uaddr, local_nr_pages, + write_to_vm, &pages[cur_page]);if (ret < local_nr_pages) { ret = -EFAULT ; goto out_unmap;
fs/direct-io.c +2 -8
@@ -150,17 +150,11 @@ static int dio_refill_pages(struct dio *int nr_pages; nr_pages =min (dio->total_pages - dio->curr_page,DIO_PAGES ); -down_read (¤t->mm->mmap_sem); - ret =get_user_pages ( - current,/* Task for fault acounting */ - current->mm,/* whose pages? */ + ret =get_user_pages_fast ( dio->curr_user_address,/* Where from? */ nr_pages,/* How many pages? */ dio->rw ==READ ,/* Write to memory? */ -0 ,/* force (?) */ - &dio->pages[0 ], -NULL );/* vmas */ -up_read (¤t->mm->mmap_sem); + &dio->pages[0 ]);/* Put results here */ if (ret <0 && dio->blocks_available && (dio->rw &WRITE )) {struct page *page =ZERO_PAGE (0 );
Signature · 5 lines
-- 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>
PATCH 5/5 splice: use get_user_pages_fast
In reply to a message not in the archiveUse get_user_pages_fast in splice. This reverts some mmap_sem batching there, however the biggest problem with mmap_sem tends to be hold times blocking out other threads rather than cacheline bouncing. Further: on architectures that implement get_user_pages_fast without locks, mmap_sem can be avoided completely anyway.
--- fs/splice.c | 41 +++-------------------------------------- 1 file changed, 3 insertions(+), 38 deletions(-)
fs/splice.c +3 -38
@@ -1147,36 +1147,6 @@ static long do_splice(struct file *in, l }/* -* Do a copy-from-user while holding the mmap_semaphore for reading, in a -* manner safe from deadlocking with simultaneous mmap() (grabbing mmap_sem -* for writing) and page faulting on the user memory pointed to by src. -* This assumes that we will very rarely hit the partial != 0 path, or this -* will not be a win. -*/ -static int copy_from_user_mmap_sem (void *dst,const void __user *src,size_t n) -{ -int partial; - -if (!access_ok (VERIFY_READ , src, n)) -return -EFAULT ; - -pagefault_disable (); - partial =__copy_from_user_inatomic (dst, src, n); -pagefault_enable (); - -/* -* Didn't copy everything, drop the mmap_sem and do a faulting copy -*/ -if (unlikely (partial)) { -up_read (¤t->mm->mmap_sem); - partial =copy_from_user (dst, src, n); -down_read (¤t->mm->mmap_sem); - } - -return partial; -} - -/* * Map an iov into an array of pages and offset/length tupples. With the * partial_page structure, we can map several non-contiguous ranges into * our ones pages[] map instead of splitting that operation into pieces.
@@ -1189,8 +1159,6 @@ static int get_iovec_page_array(const st {int buffers =0 , error =0 ; -down_read (¤t->mm->mmap_sem); -while (nr_vecs) {unsigned long off, npages;struct iovec entry;
@@ -1199,7 +1167,7 @@ static int get_iovec_page_array(const stint i; error = -EFAULT ; -if (copy_from_user_mmap_sem (&entry, iov,sizeof (entry))) +if (copy_from_user (&entry, iov,sizeof (entry)))break ; base = entry.iov_base;
@@ -1233,9 +1201,8 @@ static int get_iovec_page_array(const stif (npages >PIPE_BUFFERS - buffers) npages =PIPE_BUFFERS - buffers; - error =get_user_pages (current, current->mm, - (unsigned long ) base, npages,0 ,0 , - &pages[buffers],NULL ); + error =get_user_pages_fast ((unsigned long )base, npages, +0 , &pages[buffers]);if (unlikely (error <=0 ))break ;
@@ -1274,8 +1241,6 @@ static int get_iovec_page_array(const st iov++; } -up_read (¤t->mm->mmap_sem); -if (buffers)return buffers;
Signature · 5 lines
-- 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>