mm/gup: Cache dev_pagemap while pinning pages
Subsystems (5)
PATCH v2 1/1 mm/gup: Cache dev_pagemap while pinning pages
Getting pages from ZONE_DEVICE memory needs to check the backing device's live-ness, which is tracked in the device's dev_pagemap metadata. This metadata is stored in a radix tree and looking it up adds measurable software overhead.
This patch avoids repeating this relatively costly operation when dev_pagemap is used by caching the last dev_pagemap while getting user pages. The gup_benchmark kernel self test reports this reduces time to get user pages to as low as 1/3 of the previous time.
---
Changes from previous version:
Fixed CONFIG_NOMMU compile error from bad copy-paste
include/linux/huge_mm.h | 8 ++-- include/linux/mm.h | 12 +---- mm/gup.c | 113 +++++++++++++++++++++++++++++------------------- mm/huge_memory.c | 16 +++---- mm/nommu.c | 6 +-- 5 files changed, 82 insertions(+), 73 deletions(-)
include/linux/huge_mm.h +4 -4
@@ -213,9 +213,9 @@ static inline int hpage_nr_pages(struct page *page) }struct page *follow_devmap_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,int flags); +pmd_t *pmd,int flags,struct dev_pagemap **pgmap);struct page *follow_devmap_pud (struct vm_area_struct *vma,unsigned long addr, -pud_t *pud,int flags); +pud_t *pud,int flags,struct dev_pagemap **pgmap);extern vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf,pmd_t orig_pmd);
@@ -344,13 +344,13 @@ static inline void mm_put_huge_zero_page(struct mm_struct *mm) }static inline struct page *follow_devmap_pmd (struct vm_area_struct *vma, -unsigned long addr,pmd_t *pmd,int flags) +unsigned long addr,pmd_t *pmd,int flags,struct dev_pagemap **pgmap) {return NULL ; }static inline struct page *follow_devmap_pud (struct vm_area_struct *vma, -unsigned long addr,pud_t *pud,int flags) +unsigned long addr,pud_t *pud,int flags,struct dev_pagemap **pgmap) {return NULL ; }
include/linux/mm.h +2 -10
@@ -2534,16 +2534,8 @@ static inline vm_fault_t vmf_error(int err)return VM_FAULT_SIGBUS ; } -struct page *follow_page_mask (struct vm_area_struct *vma, -unsigned long address,unsigned int foll_flags, -unsigned int *page_mask); - -static inline struct page *follow_page (struct vm_area_struct *vma, -unsigned long address,unsigned int foll_flags) -{ -unsigned int unused_page_mask; -return follow_page_mask (vma, address, foll_flags, &unused_page_mask); -} +struct page *follow_page (struct vm_area_struct *vma,unsigned long address, +unsigned int foll_flags);#define FOLL_WRITE 0x01/* check pte is writable */ #define FOLL_TOUCH 0x02/* mark page accessed */
mm/gup.c +68 -45
270 lines not on this page · raw message
mm/huge_memory.c +6 -10
@@ -852,11 +852,10 @@ static void touch_pmd(struct vm_area_struct *vma, unsigned long addr, }struct page *follow_devmap_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,int flags) +pmd_t *pmd,int flags,struct dev_pagemap **pgmap) {unsigned long pfn =pmd_pfn (*pmd);struct mm_struct *mm = vma->vm_mm; -struct dev_pagemap *pgmap;struct page *page;assert_spin_locked (pmd_lockptr (mm, pmd));
@@ -886,12 +885,11 @@ struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,return ERR_PTR (-EEXIST ); pfn += (addr & ~PMD_MASK ) >>PAGE_SHIFT ; - pgmap =get_dev_pagemap (pfn,NULL ); -if (!pgmap) + *pgmap =get_dev_pagemap (pfn, *pgmap); +if (!*pgmap)return ERR_PTR (-EFAULT ); page =pfn_to_page (pfn);get_page (page); -put_dev_pagemap (pgmap);return page; }
@@ -1000,11 +998,10 @@ static void touch_pud(struct vm_area_struct *vma, unsigned long addr, }struct page *follow_devmap_pud (struct vm_area_struct *vma,unsigned long addr, -pud_t *pud,int flags) +pud_t *pud,int flags,struct dev_pagemap **pgmap) {unsigned long pfn =pud_pfn (*pud);struct mm_struct *mm = vma->vm_mm; -struct dev_pagemap *pgmap;struct page *page;assert_spin_locked (pud_lockptr (mm, pud));
@@ -1028,12 +1025,11 @@ struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,return ERR_PTR (-EEXIST ); pfn += (addr & ~PUD_MASK ) >>PAGE_SHIFT ; - pgmap =get_dev_pagemap (pfn,NULL ); -if (!pgmap) + *pgmap =get_dev_pagemap (pfn, *pgmap); +if (!*pgmap)return ERR_PTR (-EFAULT ); page =pfn_to_page (pfn);get_page (page); -put_dev_pagemap (pgmap);return page; }
mm/nommu.c +2 -4
@@ -1709,11 +1709,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,return ret; } -struct page *follow_page_mask (struct vm_area_struct *vma, -unsigned long address,unsigned int flags, -unsigned int *page_mask) +struct page *follow_page (struct vm_area_struct *vma,unsigned long address, +unsigned int foll_flags) { - *page_mask =0 ;return NULL ; }
Signature · 1 line
2.14.4
Quoted 11 lines · Keith Busch
Getting pages from
ZONE_DEVICEmemory needs to check the backing device's live-ness, which is tracked in the device'sdev_pagemapmetadata. This metadata is stored in a radix tree and looking it up adds measurable software overhead.This patch avoids repeating this relatively costly operation when
dev_pagemapis used by caching the lastdev_pagemapwhile getting user pages. Thegup_benchmarkkernel self test reports this reduces time to get user pages to as low as 1/3 of the previous time.
Quoted 4 lines
Cc Kirill Shutemov <[email protected]>Cc Dave Hansen <[email protected]>Cc Dan Williams <[email protected]>Signed-off-by Keith Busch <[email protected]>
Other than the 2 comments below, this looks good to me:
[..]
Quoted 4 lines
mm/gup.c +0
-0
[..]
Quoted 6 lines
mm/gup.c +1
-1@@ -431,7 +430,22 @@ struct page *follow_page_mask(struct vm_area_struct *vma,return no_page_table (vma, flags); } -return follow_p4d_mask (vma, address, pgd, flags, page_mask); +return follow_p4d_mask (vma, address, pgd, flags, ctx);
mm/gup.c struct page *follow_page_mask(struct vm_area_struct *vma, · line 443
Quoted 10 lines
mm/gup.c +10
-0+} + +struct page *follow_page (struct vm_area_struct *vma,unsigned long address, +unsigned int foll_flags) +{ +struct page *page; +struct follow_page_context ctx = { + .pgmap =NULL , + .page_mask =0 , + };
You don't need to init all members. It is defined that if you init at least one member then all non initialized members are set to zero, so you should be able to do " = { 0 }".
Quoted 9 lines
mm/gup.c +5
-0+ + page =follow_page_mask (vma, address, foll_flags, &ctx); +if (ctx.pgmap) +put_dev_pagemap (ctx.pgmap); +return page; } static int get_gate_page(struct mm_struct *mm, unsigned long address,
mm/gup.c static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, · line 678
Quoted 8 lines
mm/gup.c +2
-2@@ -659,9 +673,9 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned int gup_flags, struct page **pages, struct vm_area_struct **vmas, int *nonblocking) { - long i = 0; -unsigned int page_mask; + long ret = 0, i = 0;struct vm_area_struct *vma =NULL ; +struct follow_page_context ctx = {};
Does this have defined behavior? I would feel better with " = { 0 }" to be explicit.
Quoted 173 lines
mm/gup.c +24
-15if (!nr_pages)return 0 ;@@ -691,12 +705,14 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, pages ? &pages[i] : NULL);if (ret)return i ? : ret; - page_mask =0 ; + ctx.page_mask =0 ; goto next_page; } -if (!vma ||check_vma_flags (vma, gup_flags)) -return i ? : -EFAULT ; +if (!vma ||check_vma_flags (vma, gup_flags)) { + ret = -EFAULT ; + goto out; + }if (is_vm_hugetlb_page (vma)) { i = follow_hugetlb_page(mm, vma, pages, vmas, &start, &nr_pages, i,@@ -709,23 +725,26 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,* If we have a pending SIGKILL, don't keep faulting pages and * potentially allocating memory. */ -if (unlikely (fatal_signal_pending (current))) -return i ? i : -ERESTARTSYS ; +if (unlikely (fatal_signal_pending (current))) { + ret = -ERESTARTSYS ; + goto out; + }cond_resched (); - page =follow_page_mask (vma, start, foll_flags, &page_mask); + + page =follow_page_mask (vma, start, foll_flags, &ctx);if (!page) { -int ret; ret =faultin_page (tsk, vma, start, &foll_flags, nonblocking);switch (ret) {case 0 : goto retry; +case -EBUSY : + ret =0 ; +/* FALLTHRU */ case -EFAULT :case -ENOMEM :case -EHWPOISON : -return i ? i : ret; -case -EBUSY : -return i; + goto out;case -ENOENT : goto next_page; }@@ -737,27 +756,31 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,*/ goto next_page; }else if (IS_ERR (page)) { -return i ? i :PTR_ERR (page); + ret =PTR_ERR (page); + goto out; }if (pages) { pages[i] = page;flush_anon_page (vma, page, start);flush_dcache_page (page); - page_mask =0 ; + ctx.page_mask =0 ; } next_page:if (vmas) { vmas[i] = vma; - page_mask =0 ; + ctx.page_mask =0 ; } - page_increm =1 + (~(start >>PAGE_SHIFT ) & page_mask); + page_increm =1 + (~(start >>PAGE_SHIFT ) & ctx.page_mask);if (page_increm > nr_pages) page_increm = nr_pages; i += page_increm; start += page_increm *PAGE_SIZE ; nr_pages -= page_increm; }while (nr_pages); -return i; +out: +if (ctx.pgmap) +put_dev_pagemap (ctx.pgmap); +return i ? i : ret; } static bool vma_permits_fault(struct vm_area_struct *vma,mm/huge_memory.c +6
-10@@ -852,11 +852,10 @@ static void touch_pmd(struct vm_area_struct *vma, unsigned long addr, }struct page *follow_devmap_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,int flags) +pmd_t *pmd,int flags,struct dev_pagemap **pgmap) {unsigned long pfn =pmd_pfn (*pmd);struct mm_struct *mm = vma->vm_mm; -struct dev_pagemap *pgmap;struct page *page;assert_spin_locked (pmd_lockptr (mm, pmd));@@ -886,12 +885,11 @@ struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,return ERR_PTR (-EEXIST ); pfn += (addr & ~PMD_MASK ) >>PAGE_SHIFT ; - pgmap =get_dev_pagemap (pfn,NULL ); -if (!pgmap) + *pgmap =get_dev_pagemap (pfn, *pgmap); +if (!*pgmap)return ERR_PTR (-EFAULT ); page =pfn_to_page (pfn);get_page (page); -put_dev_pagemap (pgmap);return page; }@@ -1000,11 +998,10 @@ static void touch_pud(struct vm_area_struct *vma, unsigned long addr, }struct page *follow_devmap_pud (struct vm_area_struct *vma,unsigned long addr, -pud_t *pud,int flags) +pud_t *pud,int flags,struct dev_pagemap **pgmap) {unsigned long pfn =pud_pfn (*pud);struct mm_struct *mm = vma->vm_mm; -struct dev_pagemap *pgmap;struct page *page;assert_spin_locked (pud_lockptr (mm, pud));@@ -1028,12 +1025,11 @@ struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,return ERR_PTR (-EEXIST ); pfn += (addr & ~PUD_MASK ) >>PAGE_SHIFT ; - pgmap =get_dev_pagemap (pfn,NULL ); -if (!pgmap) + *pgmap =get_dev_pagemap (pfn, *pgmap); +if (!*pgmap)return ERR_PTR (-EFAULT ); page =pfn_to_page (pfn);get_page (page); -put_dev_pagemap (pgmap);return page; }mm/nommu.c +2
-5@@ -1709,11 +1709,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,return ret; } -struct page *follow_page_mask (struct vm_area_struct *vma, -unsigned long address,unsigned int flags, -unsigned int *page_mask) +struct page *follow_page (struct vm_area_struct *vma,unsigned long address, +unsigned int foll_flags) { - *page_mask =0 ;return NULL ; } --2.14.4
Kirill A. Shutemov
Acked-byQuoted 12 lines · Dan Williams
Quoted 11 lines · Keith Busch
Getting pages from
ZONE_DEVICEmemory needs to check the backing device's live-ness, which is tracked in the device'sdev_pagemapmetadata. This metadata is stored in a radix tree and looking it up adds measurable software overhead.This patch avoids repeating this relatively costly operation when
dev_pagemapis used by caching the lastdev_pagemapwhile getting user pages. Thegup_benchmarkkernel self test reports this reduces time to get user pages to as low as 1/3 of the previous time.
Quoted 8 lines
Quoted 4 lines
Cc Kirill Shutemov <[email protected]>Cc Dave Hansen <[email protected]>Cc Dan Williams <[email protected]>Signed-off-by Keith Busch <[email protected]>Other than the 2 comments below, this looks good to me:
Reviewed-by Dan Williams <[email protected]>
Looks good to me too:
Quoted 35 lines
[..]
Quoted 4 lines
mm/gup.c +0
-0[..]
Quoted 6 lines
mm/gup.c +1
-1@@ -431,7 +430,22 @@ struct page *follow_page_mask(struct vm_area_struct *vma,return no_page_table (vma, flags); } -return follow_p4d_mask (vma, address, pgd, flags, page_mask); +return follow_p4d_mask (vma, address, pgd, flags, ctx);mm/gup.c struct page *follow_page_mask(struct vm_area_struct *vma, · line 443
Quoted 10 lines
mm/gup.c +10
-0+} + +struct page *follow_page (struct vm_area_struct *vma,unsigned long address, +unsigned int foll_flags) +{ +struct page *page; +struct follow_page_context ctx = { + .pgmap =NULL , + .page_mask =0 , + };You don't need to init all members. It is defined that if you init at least one member then all non initialized members are set to zero, so you should be able to do " = { 0 }".
Quoted 7 lines
mm/gup.c +5
-0+ + page =follow_page_mask (vma, address, foll_flags, &ctx); +if (ctx.pgmap) +put_dev_pagemap (ctx.pgmap); +return page; }
Quoted 13 lines
mm/gup.c static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, · line 678
Quoted 10 lines
mm/gup.c +2
-2static int get_gate_page(struct mm_struct *mm, unsigned long address,@@ -659,9 +673,9 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned int gup_flags, struct page **pages, struct vm_area_struct **vmas, int *nonblocking) { - long i = 0; -unsigned int page_mask; + long ret = 0, i = 0;struct vm_area_struct *vma =NULL ; +struct follow_page_context ctx = {};Does this have defined behavior? I would feel better with " = { 0 }" to be explicit.
Well, it's not allowed by the standart, but GCC allows this. You can see a warning with -pedantic.
We use empty-list initializers a lot in the kernel:
$ git grep 'struct .*= {};' | wc -l
997It should be fine.
Signature · 1 line
Kirill A. Shutemov
On Fri, Oct 12, 2018 at 4:00 AM Kirill A. Shutemov <[email protected]> wrote:
[..]
Quoted 11 lines
Quoted 2 lines
Does this have defined behavior? I would feel better with " = { 0 }" to be explicit.
Well, it's not allowed by the standart, but GCC allows this. You can see a warning with -pedantic.
We use empty-list initializers a lot in the kernel:
$ git grep 'struct .*= {};' | wc -l 997It should be fine.
Ah, ok. I would still say we should be consistent between the init syntax for 'ctx' in follow_page() and __get_user_pages(), and why not go with '= { 0 }', one less unnecessary gcc'ism.
Quoted 12 lines · Dan Williams
On Fri, Oct 12, 2018 at 4:00 AM Kirill A. Shutemov <[email protected]> wrote:
[..]Quoted 10 lines
Quoted 2 lines
Does this have defined behavior? I would feel better with " = { 0 }" to be explicit.
Well, it's not allowed by the standart, but GCC allows this. You can see a warning with -pedantic.
We use empty-list initializers a lot in the kernel:
$ git grep 'struct .*= {};' | wc -l 997
Quoted 5 lines
Quoted 1 line
It should be fine.
Ah, ok. I would still say we should be consistent between the init syntax for 'ctx' in
follow_page()and__get_user_pages(), and why not go with '= { 0 }', one less unnecessary gcc'ism.
No problem, I'll make that happen and copy your reviews/acks into the next patch.