Skip to content

Commit 41c45d3

Browse files
johnhubbardtorvalds
authored andcommitted
mm/gup_benchmark: support pin_user_pages() and related calls
Up 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_maybe_dma_pinned() returns false, then WARN and return. Do this outside of the benchmark timestamps, so that it doesn't affect reported times. Signed-off-by: John Hubbard <jhubbard@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Ira Weiny <ira.weiny@intel.com> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Jan Kara <jack@suse.cz> Cc: Jérôme Glisse <jglisse@redhat.com> Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Christoph Hellwig <hch@infradead.org> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dave Chinner <david@fromorbit.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Link: http://lkml.kernel.org/r/20200211001536.1027652-10-jhubbard@nvidia.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1970dc6 commit 41c45d3

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎mm/gup_benchmark.c‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)
99
#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
1010
#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
11+
#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark)
12+
#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)
1113

1214
struct gup_benchmark {
1315
__u64 get_delta_usec;
@@ -19,6 +21,48 @@ struct gup_benchmark {
1921
__u64 expansion[10]; /* For future use */
2022
};
2123

24+
static void put_back_pages(unsigned int cmd, struct page **pages,
25+
unsigned long nr_pages)
26+
{
27+
unsigned long i;
28+
29+
switch (cmd) {
30+
case GUP_FAST_BENCHMARK:
31+
case GUP_LONGTERM_BENCHMARK:
32+
case GUP_BENCHMARK:
33+
for (i = 0; i < nr_pages; i++)
34+
put_page(pages[i]);
35+
break;
36+
37+
case PIN_FAST_BENCHMARK:
38+
case PIN_BENCHMARK:
39+
unpin_user_pages(pages, nr_pages);
40+
break;
41+
}
42+
}
43+
44+
static void verify_dma_pinned(unsigned int cmd, struct page **pages,
45+
unsigned long nr_pages)
46+
{
47+
unsigned long i;
48+
struct page *page;
49+
50+
switch (cmd) {
51+
case PIN_FAST_BENCHMARK:
52+
case PIN_BENCHMARK:
53+
for (i = 0; i < nr_pages; i++) {
54+
page = pages[i];
55+
if (WARN(!page_maybe_dma_pinned(page),
56+
"pages[%lu] is NOT dma-pinned\n", i)) {
57+
58+
dump_page(page, "gup_benchmark failure");
59+
break;
60+
}
61+
}
62+
break;
63+
}
64+
}
65+
2266
static int __gup_benchmark_ioctl(unsigned int cmd,
2367
struct gup_benchmark *gup)
2468
{
@@ -66,6 +110,14 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
66110
nr = get_user_pages(addr, nr, gup->flags, pages + i,
67111
NULL);
68112
break;
113+
case PIN_FAST_BENCHMARK:
114+
nr = pin_user_pages_fast(addr, nr, gup->flags,
115+
pages + i);
116+
break;
117+
case PIN_BENCHMARK:
118+
nr = pin_user_pages(addr, nr, gup->flags, pages + i,
119+
NULL);
120+
break;
69121
default:
70122
kvfree(pages);
71123
ret = -EINVAL;
@@ -78,15 +130,22 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
78130
}
79131
end_time = ktime_get();
80132

133+
/* Shifting the meaning of nr_pages: now it is actual number pinned: */
134+
nr_pages = i;
135+
81136
gup->get_delta_usec = ktime_us_delta(end_time, start_time);
82137
gup->size = addr - gup->addr;
83138

139+
/*
140+
* Take an un-benchmark-timed moment to verify DMA pinned
141+
* state: print a warning if any non-dma-pinned pages are found:
142+
*/
143+
verify_dma_pinned(cmd, pages, nr_pages);
144+
84145
start_time = ktime_get();
85-
for (i = 0; i < nr_pages; i++) {
86-
if (!pages[i])
87-
break;
88-
put_page(pages[i]);
89-
}
146+
147+
put_back_pages(cmd, pages, nr_pages);
148+
90149
end_time = ktime_get();
91150
gup->put_delta_usec = ktime_us_delta(end_time, start_time);
92151

@@ -105,6 +164,8 @@ static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
105164
case GUP_FAST_BENCHMARK:
106165
case GUP_LONGTERM_BENCHMARK:
107166
case GUP_BENCHMARK:
167+
case PIN_FAST_BENCHMARK:
168+
case PIN_BENCHMARK:
108169
break;
109170
default:
110171
return -EINVAL;

‎tools/testing/selftests/vm/gup_benchmark.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
1919
#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
2020

21+
/* Similar to above, but use FOLL_PIN instead of FOLL_GET. */
22+
#define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark)
23+
#define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)
24+
2125
/* Just the flags we need, copied from mm.h: */
2226
#define FOLL_WRITE 0x01 /* check pte is writable */
2327

@@ -40,8 +44,14 @@ int main(int argc, char **argv)
4044
char *file = "/dev/zero";
4145
char *p;
4246

43-
while ((opt = getopt(argc, argv, "m:r:n:f:tTLUwSH")) != -1) {
47+
while ((opt = getopt(argc, argv, "m:r:n:f:abtTLUuwSH")) != -1) {
4448
switch (opt) {
49+
case 'a':
50+
cmd = PIN_FAST_BENCHMARK;
51+
break;
52+
case 'b':
53+
cmd = PIN_BENCHMARK;
54+
break;
4555
case 'm':
4656
size = atoi(optarg) * MB;
4757
break;
@@ -63,6 +73,9 @@ int main(int argc, char **argv)
6373
case 'U':
6474
cmd = GUP_BENCHMARK;
6575
break;
76+
case 'u':
77+
cmd = GUP_FAST_BENCHMARK;
78+
break;
6679
case 'w':
6780
write = 1;
6881
break;

0 commit comments

Comments
 (0)