mm/gup: track FOLL_PIN pages (follow on from v12)
Subsystems (12)
John Hubbard
reviewercover mm/gup: track FOLL_PIN pages (follow on from v12)
Leon Romanovsky:
If you get a chance, I'd love to have this short series (or even just the first patch; the others are just selftests) run through your test suite that was previously choking on my earlier v11 patchset. The huge page pincount limitations are removed, so I'm expecting a perfect test run this time!
Everyone:
This activates tracking of FOLL_PIN pages. This is in support of fixing the get_user_pages()+DMA problem described in [1]-[4].
It is based on today's (Jan 24) mmotm. There is a git repo and branch, for convenience in reviewing:
[email protected]:johnhubbard/linux.git track_user_pages_v1_mmotm_24Jan2020
FOLL_PIN support is (so far) in mmotm and linux-next. However, the patch to use FOLL_PIN to track pages was *not* submitted, because Leon saw an RDMA test suite failure that involved (I think) page refcount overflows when huge pages were used.
This patch definitively solves that kind of overflow problem, by adding an exact pincount, for compound pages (of order > 1), in the 3rd struct page of a compound page. If available, that form of pincounting is used, instead of the GUP_PIN_COUNTING_BIAS approach. Thanks again to Jan Kara for that idea.
Here's the last reviewed version of the tracking patch (v11):
https://lore.kernel.org/r/[email protected]
Jan Kara had provided a reviewed-by tag for that, but I've had to remove it (again) here, due to having changed the patch "a little bit", in order to add the feature described above.
Other interesting changes:
* dump_page(): added one, or two new things to report for compound
pages: head refcount (for all compound pages), and map_pincount (for compound pages of order > 1).
* Documentation/core-api/pin_user_pages.rst: removed the "TODO" for the huge page refcount upper limit problems, and added notes about how it works now. Also added a note about the dump_page() enhancements.
* Added some comments in gup.c and mm.h, to explain that there are two ways to count pinned pages: exact (for compound pages of order > 1) and fuzzy (GUP_PIN_COUNTING_BIAS: for all other pages).
============================================================
General notes about the tracking patch:
This is a prerequisite to solving the problem of proper interactions between file-backed pages, and [R]DMA activities, as discussed in [1], [2], [3], [4] and in a remarkable number of email threads since about 2017. :)
In contrast to earlier approaches, the page tracking can be incrementally applied to the kernel call sites that, until now, have been simply calling get_user_pages() ("gup"). In other words, opt-in by changing from this:
get_user_pages() (sets FOLL_GET)
put_page()
to this:
pin_user_pages() (sets FOLL_PIN)
unpin_user_page()
============================================================
Next steps:
* Convert more subsystems from get_user_pages() to pin_user_pages().
* Work with Ira and others to connect this all up with file system leases.
[1] Some slow progress on get_user_pages() (Apr 2, 2019): https://lwn.net/Articles/784574/
[2] DMA and get_user_pages() (LPC: Dec 12, 2018): https://lwn.net/Articles/774411/
[3] The trouble with get_user_pages() (Apr 30, 2018): https://lwn.net/Articles/753027/
[4] LWN kernel index: get_user_pages() https://lwn.net/Kernel/Index/#Memory_management-get_user_pages
John Hubbard (3):
mm/gup: track FOLL_PIN pages
mm/gup_benchmark: support pin_user_pages() and related calls selftests/vm: run_vmtests: invoke gup_benchmark with basic FOLL_PIN
coverage
Documentation/core-api/pin_user_pages.rst | 48 ++- include/linux/mm.h | 109 ++++- include/linux/mm_types.h | 7 +- include/linux/mmzone.h | 2 + include/linux/page_ref.h | 10 + mm/debug.c | 22 +- mm/gup.c | 467 ++++++++++++++++----- mm/gup_benchmark.c | 70 ++- mm/huge_memory.c | 29 +- mm/hugetlb.c | 44 +- mm/page_alloc.c | 2 + mm/rmap.c | 6 + mm/vmstat.c | 2 + tools/testing/selftests/vm/gup_benchmark.c | 15 +- tools/testing/selftests/vm/run_vmtests | 22 + 15 files changed, 678 insertions(+), 177 deletions(-)
Signature · 1 line
2.25.0
John Hubbard
reviewerPATCH 1/3 mm/gup: track FOLL_PIN pages
Add tracking of pages that were pinned via FOLL_PIN.
As mentioned in the FOLL_PIN documentation, callers who effectively set FOLL_PIN are required to ultimately free such pages via unpin_user_page(). The effect is similar to FOLL_GET, and may be thought of as "FOLL_GET for DIO and/or RDMA use".
Pages that have been pinned via FOLL_PIN are identifiable via a new function call:
bool page_dma_pinned(struct page *page);
What to do in response to encountering such a page, is left to later patchsets. There is discussion about this in [1], [2], and [3].
This also changes a BUG_ON(), to a WARN_ON(), in follow_page_mask().
It also adds a few critical diagnostics to dump_page().
[1] Some slow progress on get_user_pages() (Apr 2, 2019):
https://lwn.net/Articles/784574/ [2] DMA and get_user_pages() (LPC: Dec 12, 2018): https://lwn.net/Articles/774411/ [3] The trouble with get_user_pages() (Apr 30, 2018): https://lwn.net/Articles/753027/
--- Documentation/core-api/pin_user_pages.rst | 48 +-- include/linux/mm.h | 109 ++++- include/linux/mm_types.h | 7 +- include/linux/mmzone.h | 2 + include/linux/page_ref.h | 10 + mm/debug.c | 22 +- mm/gup.c | 467 +++++++++++++++++----- mm/huge_memory.c | 29 +- mm/hugetlb.c | 44 +- mm/page_alloc.c | 2 + mm/rmap.c | 6 + mm/vmstat.c | 2 + 12 files changed, 577 insertions(+), 171 deletions(-)
Documentation/core-api/pin_user_pages.rst +25 -23
@@ -52,8 +52,22 @@ Which flags are set by each wrapper For these pin_user_pages*() functions, FOLL_PIN is OR'd in with whatever gup flags the caller provides. The caller is required to pass in a non-null struct -pages* array, and the function then pin pages by incrementing each by a special -value. For now, that value is +1, just like get_user_pages*().:: +pages* array, and the function then pins pages by incrementing each by a special +value: GUP_PIN_COUNTING_BIAS. + +For huge pages (and in fact, any compound page of more than 2 pages), the +GUP_PIN_COUNTING_BIAS scheme is not used. Instead, an exact form of pin counting +is achieved, by using the 3rd struct page in the compound page. A new struct +page field, hpage_pinned_refcount, has been added in order to support this. + +This approach for compound pages avoids the counting upper limit problems that +are discussed below. Those limitations would have been aggravated severely by +huge pages, because each tail page adds a refcount to the head page. And in +fact, testing revealed that, without a separate hpage_pinned_refcount field, +page overflows were seen in some huge page stress tests. + +This also means that huge pages and compound pages (of order > 1) do not suffer +from the false positives problem that is mentioned below.:: Function --------
@@ -99,27 +113,6 @@ pages: This also leads to limitations: there are only 31-10==21 bits available for a counter that increments 10 bits at a time. -TODO: for 1GB and larger huge pages, this is cutting it close. That's because -when pin_user_pages() follows such pages, it increments the head page by "1" -(where "1" used to mean "+1" for get_user_pages(), but now means "+1024" for -pin_user_pages()) for each tail page. So if you have a 1GB huge page: - -* There are 256K (18 bits) worth of 4 KB tail pages. -* There are 21 bits available to count up via GUP_PIN_COUNTING_BIAS (that is, - 10 bits at a time) -* There are 21 - 18 == 3 bits available to count. Except that there aren't, - because you need to allow for a few normal get_page() calls on the head page, - as well. Fortunately, the approach of using addition, rather than "hard" - bitfields, within page->_refcount, allows for sharing these bits gracefully. - But we're still looking at about 8 references. - -This, however, is a missing feature more than anything else, because it's easily -solved by addressing an obvious inefficiency in the original get_user_pages() -approach of retrieving pages: stop treating all the pages as if they were -PAGE_SIZE. Retrieve huge pages as huge pages. The callers need to be aware of -this, so some work is required. Once that's in place, this limitation mostly -disappears from view, because there will be ample refcounting range available. - * Callers must specifically request "dma-pinned tracking of pages". In other words, just calling get_user_pages() will not suffice; a new set of functions, pin_user_page() and related, must be used.
@@ -222,11 +215,20 @@ Those are both going to show zero, unless CONFIG_DEBUG_VM is set. This is because there is a noticeable performance drop in unpin_user_page(), when they are activated. +Other diagnostics +================= + +dump_page() has been enhanced slightly, to handle these new counting fields, and +to better report on compound pages in general. Specifically, for compound pages, +the head page refcount is reported, and for compound pages with order > 1, the +exact (hpage_pinned_refcount) pincount is reported. + References ========== * `Some slow progress on get_user_pages() (Apr 2, 2019) <https://lwn.net/Articles/784574/>`_ * `DMA and get_user_pages() (LPC: Dec 12, 2018) <https://lwn.net/Articles/774411/>`_ * `The trouble with get_user_pages() (Apr 30, 2018) <https://lwn.net/Articles/753027/>`_ +* `LWN kernel index: get_user_pages() <https://lwn.net/Kernel/Index/#Memory_management-get_user_pages>` John Hubbard, October, 2019
include/linux/mm.h +94 -15
135 lines not on this page · raw message
include/linux/mm_types.h +6 -1
@@ -137,7 +137,7 @@ struct page { };struct {/* Second tail page of compound page */ unsigned long _compound_pad_1;/* compound_head */ -unsigned long _compound_pad_2; +atomic_t hpage_pinned_refcount;/* For both global and memcg */ struct list_head deferred_list; };
@@ -226,6 +226,11 @@ static inline atomic_t *compound_mapcount_ptr(struct page *page)return &page[1 ].compound_mapcount; } +static inline atomic_t *compound_pincount_ptr (struct page *page) +{ +return &page[2 ].hpage_pinned_refcount; +} +/* * Used for sizing the vmemmap region on some architectures */
include/linux/mmzone.h +2 -0
@@ -243,6 +243,8 @@ enum node_stat_item {NR_DIRTIED ,/* page dirtyings since bootup */ NR_WRITTEN ,/* page writings since bootup */ NR_KERNEL_MISC_RECLAIMABLE ,/* reclaimable non-slab kernel pages */ +NR_FOLL_PIN_REQUESTED ,/* via: pin_user_page(), gup flag: FOLL_PIN */ +NR_FOLL_PIN_RETURNED ,/* pages returned via unpin_user_page() */ NR_VM_NODE_STAT_ITEMS };
include/linux/page_ref.h +10 -0
@@ -102,6 +102,16 @@ static inline void page_ref_sub(struct page *page, int nr)__page_ref_mod (page, -nr); } +static inline int page_ref_sub_return (struct page *page,int nr) +{ +int ret =atomic_sub_return (nr, &page->_refcount); + +if (page_ref_tracepoint_active (__tracepoint_page_ref_mod)) +__page_ref_mod (page, -nr); + +return ret; +} +static inline void page_ref_inc (struct page *page) {atomic_inc (&page->_refcount);
mm/debug.c +17 -5
@@ -76,11 +76,23 @@ void __dump_page(struct page *page, const char *reason) mapcount =PageSlab (page) ?0 :page_mapcount (page);if (PageCompound (page)) -pr_warn ("page:%px refcount:%d mapcount:%d mapping:%px " -"index:%#lx compound_mapcount: %d\n" , - page,page_ref_count (page), mapcount, - page->mapping,page_to_pgoff (page), -compound_mapcount (page)); +if (hpage_pincount_available (page)) +pr_warn ("page:%px refcount:%d head refcount:%d " +"mapcount:%d mapping:%px index:%#lx " +"compound_mapcount:%d compound_pincount:%d\n" , + page,page_ref_count (page), +page_ref_count (compound_head (page)), mapcount, + page->mapping,page_to_pgoff (page), +compound_mapcount (page), +compound_pincount (page)); +else +pr_warn ("page:%px refcount:%d head refcount:%d " +"mapcount:%d mapping:%px index:%#lx " +"compound_mapcount:%d\n" , + page,page_ref_count (page), +page_ref_count (compound_head (page)), mapcount, + page->mapping,page_to_pgoff (page), +compound_mapcount (page));else pr_warn("page:%px refcount:%d mapcount:%d mapping:%px index:%#lx\n", page, page_ref_count(page), mapcount,
mm/gup.c +364 -103
727 lines not on this page · raw message
mm/huge_memory.c +23 -6
@@ -947,6 +947,11 @@ struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,*/ WARN_ONCE (flags &FOLL_COW ,"mm: In follow_devmap_pmd with FOLL_COW set" ); +/* FOLL_GET and FOLL_PIN are mutually exclusive. */ +if (WARN_ON_ONCE ((flags & (FOLL_PIN |FOLL_GET )) == + (FOLL_PIN |FOLL_GET ))) +return NULL ; +if (flags &FOLL_WRITE && !pmd_write (*pmd))return NULL ;
@@ -962,7 +967,7 @@ struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,* device mapped pages can only be returned if the * caller will manage the page reference count. */ -if (!(flags &FOLL_GET )) +if (!(flags & (FOLL_GET |FOLL_PIN )))return ERR_PTR (-EEXIST ); pfn += (addr & ~PMD_MASK ) >>PAGE_SHIFT ;
@@ -970,7 +975,8 @@ struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,if (!*pgmap)return ERR_PTR (-EFAULT ); page =pfn_to_page (pfn); -get_page (page); +if (!try_grab_page (page, flags)) + page =ERR_PTR (-ENOMEM );return page; }
@@ -1090,6 +1096,11 @@ struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,if (flags &FOLL_WRITE && !pud_write (*pud))return NULL ; +/* FOLL_GET and FOLL_PIN are mutually exclusive. */ +if (WARN_ON_ONCE ((flags & (FOLL_PIN |FOLL_GET )) == + (FOLL_PIN |FOLL_GET ))) +return NULL ; +if (pud_present (*pud) &&pud_devmap (*pud))/* pass */ ;else
@@ -1101,8 +1112,10 @@ struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,/* * device mapped pages can only be returned if the * caller will manage the page reference count. +* +* At least one of FOLL_GET | FOLL_PIN must be set, so assert that here: */ -if (!(flags &FOLL_GET )) +if (!(flags & (FOLL_GET |FOLL_PIN )))return ERR_PTR (-EEXIST ); pfn += (addr & ~PUD_MASK ) >>PAGE_SHIFT ;
@@ -1110,7 +1123,8 @@ struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,if (!*pgmap)return ERR_PTR (-EFAULT ); page =pfn_to_page (pfn); -get_page (page); +if (!try_grab_page (page, flags)) + page =ERR_PTR (-ENOMEM );return page; }
@@ -1486,8 +1500,13 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, page =pmd_page (*pmd);VM_BUG_ON_PAGE (!PageHead (page) && !is_zone_device_page (page), page); + +if (!try_grab_page (page, flags)) +return ERR_PTR (-ENOMEM ); +if (flags &FOLL_TOUCH )touch_pmd (vma, addr, pmd, flags); +if ((flags &FOLL_MLOCK ) && (vma->vm_flags &VM_LOCKED )) {/* * We don't mlock() pte-mapped THPs. This way we can avoid
@@ -1524,8 +1543,6 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, skip_mlock: page += (addr & ~HPAGE_PMD_MASK ) >>PAGE_SHIFT ;VM_BUG_ON_PAGE (!PageCompound (page) && !is_zone_device_page (page), page); -if (flags &FOLL_GET ) -get_page (page); out:return page;
mm/hugetlb.c +26 -18
92 lines not on this page · raw message
mm/page_alloc.c +2 -0
@@ -689,6 +689,8 @@ void prep_compound_page(struct page *page, unsigned int order)set_compound_head (p, page); }atomic_set (compound_mapcount_ptr (page),-1 ); +if (hpage_pincount_available (page)) +atomic_set (compound_pincount_ptr (page),0 ); }#ifdef CONFIG_DEBUG_PAGEALLOC
mm/rmap.c +6 -0
@@ -1178,6 +1178,9 @@ void page_add_new_anon_rmap(struct page *page,VM_BUG_ON_PAGE (!PageTransHuge (page), page);/* increment count (starts at -1) */ atomic_set (compound_mapcount_ptr (page),0 ); +if (hpage_pincount_available (page)) +atomic_set (compound_pincount_ptr (page),0 ); +__inc_node_page_state (page,NR_ANON_THPS ); }else {/* Anon THP always mapped first with PMD */
@@ -1974,6 +1977,9 @@ void hugepage_add_new_anon_rmap(struct page *page, {BUG_ON (address < vma->vm_start || address >= vma->vm_end);atomic_set (compound_mapcount_ptr (page),0 ); +if (hpage_pincount_available (page)) +atomic_set (compound_pincount_ptr (page),0 ); +__page_set_anon_rmap (page, vma, address,1 ); }#endif /* CONFIG_HUGETLB_PAGE */
mm/vmstat.c +2 -0
@@ -1168,6 +1168,8 @@ const char * const vmstat_text[] = {"nr_dirtied" ,"nr_written" ,"nr_kernel_misc_reclaimable" , +"nr_foll_pin_requested" , +"nr_foll_pin_returned" ,/* enum writeback_stat_item counters */ "nr_dirty_threshold",
Signature · 1 line
2.25.0
Jan Kara
reviewerRe: [PATCH 1/3] mm/gup: track FOLL_PIN pages
Quoted 16 lines · John Hubbard
mm/gup.c +16
-0+static __maybe_unused struct page *try_grab_compound_head (struct page *page, +int refs, +unsigned int flags) +{ +if (flags &FOLL_GET ) +return try_get_compound_head (page, refs); +else if (flags &FOLL_PIN ) { +int orig_refs = refs; + +/* +* When pinning a compound page of order > 1 (which is what +* hpage_pincount_available() checks for), use an exact count to +* track it, via hpage_pincount_inc/_dec(). +* +* However, be sure to *also* increment the normal page refcount +* field at least once, so that the page really is pinned.
mm/gup.c static inline struct page *try_get_compound_head(struct page *page, int refs) · line 116
Quoted 10 lines
mm/gup.c +10
-0+*/ +if (!hpage_pincount_available (page)) + refs *=GUP_PIN_COUNTING_BIAS ; + + page =try_get_compound_head (page, refs); +if (!page) +return NULL ; + +if (hpage_pincount_available (page)) +hpage_pincount_inc (page);
Umm, adding just 1 to pincount looks dangerous to me as try_grab_compound_head() would not compose - you could not release references acquired by try_grab_compound_head() with refs==2 by two calls to put_compound_head() with refs==1. So I'd rather have here:hpage_pincount_add(page, refs) and similarly in put_compound_head(). Otherwise the patch looks good to me from a quick look.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
John Hubbard
reviewerRe: [PATCH 1/3] mm/gup: track FOLL_PIN pages
Quoted 35 lines · Jan Kara
Quoted 16 lines · John Hubbard
mm/gup.c +16
-0+static __maybe_unused struct page *try_grab_compound_head (struct page *page, +int refs, +unsigned int flags) +{ +if (flags &FOLL_GET ) +return try_get_compound_head (page, refs); +else if (flags &FOLL_PIN ) { +int orig_refs = refs; + +/* +* When pinning a compound page of order > 1 (which is what +* hpage_pincount_available() checks for), use an exact count to +* track it, via hpage_pincount_inc/_dec(). +* +* However, be sure to *also* increment the normal page refcount +* field at least once, so that the page really is pinned. mm/gup.c static inline struct page *try_get_compound_head(struct page *page, int refs) · line 116
Quoted 10 lines
mm/gup.c +10
-0+*/ +if (!hpage_pincount_available (page)) + refs *=GUP_PIN_COUNTING_BIAS ; + + page =try_get_compound_head (page, refs); +if (!page) +return NULL ; + +if (hpage_pincount_available (page)) +hpage_pincount_inc (page);Umm, adding just 1 to pincount looks dangerous to me as
try_grab_compound_head()would not compose - you could not release references acquired bytry_grab_compound_head()with refs==2 by two calls toput_compound_head()with refs==1. So I'd rather have here:hpage_pincount_add(page, refs)and similarly input_compound_head(). Otherwise the patch looks good to me from a quick look.
Quoted 1 line
Honza
Yes, you are right. The hpage_pincount really should track refs. I'll fix it up.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [PATCH 1/3] mm/gup: track FOLL_PIN pages
In reply to John HubbardQuoted 1 line · John Hubbard
12 files changed, 577 insertions(+), 171 deletions(-)
Can we get it split? There's too much going on to give meaningful review.
Signature · 1 line
Kirill A. Shutemov
John Hubbard
reviewerRe: [PATCH 1/3] mm/gup: track FOLL_PIN pages
Quoted 5 lines · Kirill A. Shutemov
Quoted 1 line · John Hubbard
12 files changed, 577 insertions(+), 171 deletions(-)Can we get it split? There's too much going on to give meaningful review.
Sure, I'll split it up, it has gotten quite large for one patch.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
John Hubbard
reviewerPATCH 2/3 mm/gup_benchmark: support pin_user_pages() and related calls
In reply to John HubbardUp until now, gup_benchmark supported testing of the following kernel functions:
* get_user_pages(): via the '-U' command line option
* get_user_pages_longterm(): via the '-L' command line option
* get_user_pages_fast(): as the default (no options required)
Add test coverage for the new corresponding pin_*() functions:
* pin_user_pages_fast(): via the '-a' command line option
* pin_user_pages(): via the '-b' command line option
Also, add an option for clarity: '-u' for what is now (still) the default choice: get_user_pages_fast().
Also, for the commands that set FOLL_PIN, verify that the pages really are dma-pinned, via the new is_dma_pinned() routine. Those commands are:
PIN_FAST_BENCHMARK : calls pin_user_pages_fast()
PIN_BENCHMARK : calls pin_user_pages()
In between the calls to pin_*() and unpin_user_pages(), check each page: if page_dma_pinned() returns false, then WARN and return.
Do this outside of the benchmark timestamps, so that it doesn't affect reported times.
--- mm/gup_benchmark.c | 70 ++++++++++++++++++++-- tools/testing/selftests/vm/gup_benchmark.c | 15 ++++- 2 files changed, 79 insertions(+), 6 deletions(-)
mm/gup_benchmark.c +65 -5
@@ -8,6 +8,8 @@#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark) +#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark) +#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)struct gup_benchmark {__u64 get_delta_usec;
@@ -19,6 +21,47 @@ struct gup_benchmark {__u64 expansion[10 ];/* For future use */ }; +static void put_back_pages (int cmd,struct page **pages,unsigned long nr_pages) +{ +int i; + +switch (cmd) { +case GUP_FAST_BENCHMARK : +case GUP_LONGTERM_BENCHMARK : +case GUP_BENCHMARK : +for (i =0 ; i < nr_pages; i++) +put_page (pages[i]); +break ; + +case PIN_FAST_BENCHMARK : +case PIN_BENCHMARK : +unpin_user_pages (pages, nr_pages); +break ; + } +} + +static void verify_dma_pinned (int cmd,struct page **pages, +unsigned long nr_pages) +{ +int i; +struct page *page; + +switch (cmd) { +case PIN_FAST_BENCHMARK : +case PIN_BENCHMARK : +for (i =0 ; i < nr_pages; i++) { + page = pages[i]; +if (WARN (!page_dma_pinned (page), +"pages[%d] is NOT dma-pinned\n" , i)) { + +dump_page (page,"gup_benchmark failure" ); +break ; + } + } +break ; + } +} +static int __gup_benchmark_ioctl (unsigned int cmd,struct gup_benchmark *gup) {
@@ -66,6 +109,14 @@ static int __gup_benchmark_ioctl(unsigned int cmd, nr =get_user_pages (addr, nr, gup->flags, pages + i,NULL );break ; +case PIN_FAST_BENCHMARK : + nr =pin_user_pages_fast (addr, nr, gup->flags, + pages + i); +break ; +case PIN_BENCHMARK : + nr =pin_user_pages (addr, nr, gup->flags, pages + i, +NULL ); +break ;default :kvfree (pages); ret = -EINVAL ;
@@ -78,15 +129,22 @@ static int __gup_benchmark_ioctl(unsigned int cmd, } end_time =ktime_get (); +/* Shifting the meaning of nr_pages: now it is actual number pinned: */ + nr_pages = i; + gup->get_delta_usec =ktime_us_delta (end_time, start_time); gup->size = addr - gup->addr; +/* +* Take an un-benchmark-timed moment to verify DMA pinned +* state: print a warning if any non-dma-pinned pages are found: +*/ +verify_dma_pinned (cmd, pages, nr_pages); + start_time =ktime_get (); -for (i =0 ; i < nr_pages; i++) { -if (!pages[i]) -break ; -put_page (pages[i]); - } + +put_back_pages (cmd, pages, nr_pages); + end_time =ktime_get (); gup->put_delta_usec =ktime_us_delta (end_time, start_time);
@@ -105,6 +163,8 @@ static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,case GUP_FAST_BENCHMARK :case GUP_LONGTERM_BENCHMARK :case GUP_BENCHMARK : +case PIN_FAST_BENCHMARK : +case PIN_BENCHMARK :break ;default :return -EINVAL ;
tools/testing/selftests/vm/gup_benchmark.c +14 -1
@@ -18,6 +18,10 @@#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark) +/* Similar to above, but use FOLL_PIN instead of FOLL_GET. */ +#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark) +#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark) +/* Just the flags we need, copied from mm.h: */ #define FOLL_WRITE 0x01/* check pte is writable */
@@ -40,8 +44,14 @@ int main(int argc, char **argv)char *file ="/dev/zero" ;char *p; -while ((opt =getopt (argc, argv,"m:r:n:f:tTLUwSH" )) !=-1 ) { +while ((opt =getopt (argc, argv,"m:r:n:f:abtTLUuwSH" )) !=-1 ) {switch (opt) { +case 'a' : + cmd =PIN_FAST_BENCHMARK ; +break ; +case 'b' : + cmd =PIN_BENCHMARK ; +break ;case 'm' : size =atoi (optarg) *MB ;break ;
@@ -63,6 +73,9 @@ int main(int argc, char **argv)case 'U' : cmd =GUP_BENCHMARK ;break ; +case 'u' : + cmd =GUP_FAST_BENCHMARK ; +break ;case 'w' : write =1 ;break ;
Signature · 1 line
2.25.0
Re: [PATCH 2/3] mm/gup_benchmark: support pin_user_pages() and related calls
Hi John,
Quoted 50 lines · John Hubbard
Up until now,
gup_benchmarksupported testing of the following kernel functions:*
get_user_pages(): via the '-U' command line option
*get_user_pages_longterm(): via the '-L' command line option
*get_user_pages_fast(): as the default (no options required)Add test coverage for the new corresponding pin_*() functions:
*
pin_user_pages_fast(): via the '-a' command line option
*pin_user_pages(): via the '-b' command line optionAlso, add an option for clarity: '-u' for what is now (still) the default choice:
get_user_pages_fast().Also, for the commands that set
FOLL_PIN, verify that the pages really are dma-pinned, via the newis_dma_pinned()routine. Those commands are:
PIN_FAST_BENCHMARK: callspin_user_pages_fast()
PIN_BENCHMARK: callspin_user_pages()In between the calls to pin_*() and
unpin_user_pages(), check each page: ifpage_dma_pinned()returns false, then WARN and return.Do this outside of the benchmark timestamps, so that it doesn't affect reported times.
Reviewed-by Ira Weiny <[email protected]>Signed-off-by John Hubbard <[email protected]>--- mm/gup_benchmark.c | 70 ++++++++++++++++++++-- tools/testing/selftests/vm/gup_benchmark.c | 15 ++++- 2 files changed, 79 insertions(+), 6 deletions(-)mm/gup_benchmark.c +2
-0@@ -8,6 +8,8 @@#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark) +#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark) +#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)struct gup_benchmark {__u64 get_delta_usec;
mm/gup_benchmark.c struct gup_benchmark { · line 24
Quoted 4 lines
mm/gup_benchmark.c +1
-0@@ -19,6 +21,47 @@ struct gup_benchmark {__u64 expansion[10 ];/* For future use */ }; +static void put_back_pages (int cmd,struct page **pages,unsigned long nr_pages)
We received a Clang build report on this patch because the use of PIN_FAST_BENCHMARK and PIN_BENCHMARK in the switch statement below will overflow int; this should be unsigned int to match the cmd parameter in the ioctls.
The report can be read here if you care for it:
https://groups.google.com/d/msg/clang-built-linux/gyGayC_dnis/D1celSStEgAJ
Cheers,
Nathan
Quoted 139 lines
mm/gup_benchmark.c +62
-5+{ +int i; + +switch (cmd) { +case GUP_FAST_BENCHMARK : +case GUP_LONGTERM_BENCHMARK : +case GUP_BENCHMARK : +for (i =0 ; i < nr_pages; i++) +put_page (pages[i]); +break ; + +case PIN_FAST_BENCHMARK : +case PIN_BENCHMARK : +unpin_user_pages (pages, nr_pages); +break ; + } +} + +static void verify_dma_pinned (int cmd,struct page **pages, +unsigned long nr_pages) +{ +int i; +struct page *page; + +switch (cmd) { +case PIN_FAST_BENCHMARK : +case PIN_BENCHMARK : +for (i =0 ; i < nr_pages; i++) { + page = pages[i]; +if (WARN (!page_dma_pinned (page), +"pages[%d] is NOT dma-pinned\n" , i)) { + +dump_page (page,"gup_benchmark failure" ); +break ; + } + } +break ; + } +} +static int __gup_benchmark_ioctl (unsigned int cmd,struct gup_benchmark *gup) {@@ -66,6 +109,14 @@ static int __gup_benchmark_ioctl(unsigned int cmd, nr =get_user_pages (addr, nr, gup->flags, pages + i,NULL );break ; +case PIN_FAST_BENCHMARK : + nr =pin_user_pages_fast (addr, nr, gup->flags, + pages + i); +break ; +case PIN_BENCHMARK : + nr =pin_user_pages (addr, nr, gup->flags, pages + i, +NULL ); +break ;default :kvfree (pages); ret = -EINVAL ;@@ -78,15 +129,22 @@ static int __gup_benchmark_ioctl(unsigned int cmd, } end_time =ktime_get (); +/* Shifting the meaning of nr_pages: now it is actual number pinned: */ + nr_pages = i; + gup->get_delta_usec =ktime_us_delta (end_time, start_time); gup->size = addr - gup->addr; +/* +* Take an un-benchmark-timed moment to verify DMA pinned +* state: print a warning if any non-dma-pinned pages are found: +*/ +verify_dma_pinned (cmd, pages, nr_pages); + start_time =ktime_get (); -for (i =0 ; i < nr_pages; i++) { -if (!pages[i]) -break ; -put_page (pages[i]); - } + +put_back_pages (cmd, pages, nr_pages); + end_time =ktime_get (); gup->put_delta_usec =ktime_us_delta (end_time, start_time);@@ -105,6 +163,8 @@ static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,case GUP_FAST_BENCHMARK :case GUP_LONGTERM_BENCHMARK :case GUP_BENCHMARK : +case PIN_FAST_BENCHMARK : +case PIN_BENCHMARK :break ;default :return -EINVAL ;tools/testing/selftests/vm/gup_benchmark.c +14
-1@@ -18,6 +18,10 @@#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark) +/* Similar to above, but use FOLL_PIN instead of FOLL_GET. */ +#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark) +#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark) +/* Just the flags we need, copied from mm.h: */ #define FOLL_WRITE 0x01/* check pte is writable */ @@ -40,8 +44,14 @@ int main(int argc, char **argv)char *file ="/dev/zero" ;char *p; -while ((opt =getopt (argc, argv,"m:r:n:f:tTLUwSH" )) !=-1 ) { +while ((opt =getopt (argc, argv,"m:r:n:f:abtTLUuwSH" )) !=-1 ) {switch (opt) { +case 'a' : + cmd =PIN_FAST_BENCHMARK ; +break ; +case 'b' : + cmd =PIN_BENCHMARK ; +break ;case 'm' : size =atoi (optarg) *MB ;break ;@@ -63,6 +73,9 @@ int main(int argc, char **argv)case 'U' : cmd =GUP_BENCHMARK ;break ; +case 'u' : + cmd =GUP_FAST_BENCHMARK ; +break ;case 'w' : write =1 ;break ;Signature · 1 line
2.25.0
John Hubbard
reviewerRe: [PATCH 2/3] mm/gup_benchmark: support pin_user_pages() and related calls
On 1/27/20 12:52 PM, Nathan Chancellor wrote:
...
Quoted 15 lines
Quoted 15 lines
mm/gup_benchmark.c +2
-0@@ -8,6 +8,8 @@#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark) +#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark) +#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)struct gup_benchmark {__u64 get_delta_usec;@@ -19,6 +21,47 @@ struct gup_benchmark {__u64 expansion[10 ];/* For future use */ };
Quoted 6 lines
mm/gup_benchmark.c struct gup_benchmark { · line 24
Quoted 1 line
mm/gup_benchmark.c +1
-0+static void put_back_pages (int cmd,struct page **pages,unsigned long nr_pages)We received a Clang build report on this patch because the use of
PIN_FAST_BENCHMARKandPIN_BENCHMARKin the switch statement below will overflow int; this should be unsigned int to match the cmd parameter in the ioctls.
Thanks for the report! Yes, that should have been "unsigned int cmd", to match the one in the ioctls, just as you said.
I'll apply this diff, for the next version of the series:
mm/gup_benchmark.c +3 -2
@@ -21,7 +21,8 @@ struct gup_benchmark {__u64 expansion[10 ];/* For future use */ }; -static void put_back_pages (int cmd,struct page **pages,unsigned long nr_pages) +static void put_back_pages (unsigned int cmd,struct page **pages, +unsigned long nr_pages) {int i;
@@ -40,7 +41,7 @@ static void put_back_pages(int cmd, struct page **pages, unsigned long nr_pages) } } -static void verify_dma_pinned (int cmd,struct page **pages, +static void verify_dma_pinned (unsigned int cmd,struct page **pages,unsigned long nr_pages) {int i;
thanks,
Signature · 2 lines
John Hubbard NVIDIA
John Hubbard
reviewerPATCH 3/3 selftests/vm: run_vmtests: invoke gup_benchmark with basic FOLL_PIN coverage
In reply to John HubbardIt's good to have basic unit test coverage of the new FOLL_PIN behavior. Fortunately, the gup_benchmark unit test is extremely fast (a few milliseconds), so adding it the the run_vmtests suite is going to cause no noticeable change in running time.
So, add two new invocations to run_vmtests:
1) Run gup_benchmark with normal get_user_pages().
2) Run gup_benchmark with pin_user_pages(). This is much like the first call, except that it sets FOLL_PIN.
Running these two in quick succession also provide a visual comparison of the running times, which is convenient.
The new invocations are fairly early in the run_vmtests script, because with test suites, it's usually preferable to put the shorter, faster tests first, all other things being equal.
--- tools/testing/selftests/vm/run_vmtests | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
tools/testing/selftests/vm/run_vmtests +22 -0
@@ -112,6 +112,28 @@ echo "NOTE: The above hugetlb tests provide minimal coverage. Use" echo " https://github.com/libhugetlbfs/libhugetlbfs.git for" echo " hugetlb regression testing." +echo "--------------------------------------------" +echo "running 'gup_benchmark -U' (normal/slow gup)" +echo "--------------------------------------------" +./gup_benchmark -U +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + +echo "------------------------------------------" +echo "running gup_benchmark -b (pin_user_pages)" +echo "------------------------------------------" +./gup_benchmark -b +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi + echo "-------------------" echo "running userfaultfd" echo "-------------------"
Signature · 1 line
2.25.0
Quoted 8 lines · John Hubbard
Leon Romanovsky:
If you get a chance, I'd love to have this short series (or even just the first patch; the others are just selftests) run through your test suite that was previously choking on my earlier v11 patchset. The huge page pincount limitations are removed, so I'm expecting a perfect test run this time!
I added those patches to our regression and I will post the in the couple of days.
Thanks
Quoted 10 lines · Leon Romanovsky
Quoted 8 lines · John Hubbard
Leon Romanovsky:
If you get a chance, I'd love to have this short series (or even just the first patch; the others are just selftests) run through your test suite that was previously choking on my earlier v11 patchset. The huge page pincount limitations are removed, so I'm expecting a perfect test run this time!
Quoted 2 lines
I added those patches to our regression and I will post the in the couple of days.
Hi John,
The patches survived our RDMA verification night runs.
Thanks
Quoted 3 lines
Thanks
John Hubbard
reviewerQuoted 11 lines · Leon Romanovsky
Quoted 10 lines · Leon Romanovsky
Quoted 8 lines · John Hubbard
Leon Romanovsky:
If you get a chance, I'd love to have this short series (or even just the first patch; the others are just selftests) run through your test suite that was previously choking on my earlier v11 patchset. The huge page pincount limitations are removed, so I'm expecting a perfect test run this time!
Quoted 7 lines
Quoted 2 lines
I added those patches to our regression and I will post the in the couple of days.
Hi John,
The patches survived our RDMA verification night runs.
Great! Thanks very much for running those. That's a pretty solid confirmation that the earlier patch *did* allow a huge page refcount overflow, and that this approach avoids it.
thanks,
Signature · 2 lines
John Hubbard NVIDIA