2017-11-18 07:31:22 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/ktime.h>
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
|
|
|
|
#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)
|
2020-10-14 07:51:54 +08:00
|
|
|
#define GUP_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
|
|
|
|
#define PIN_FAST_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
|
|
|
|
#define PIN_BENCHMARK _IOWR('g', 4, struct gup_benchmark)
|
|
|
|
#define PIN_LONGTERM_BENCHMARK _IOWR('g', 5, struct gup_benchmark)
|
2017-11-18 07:31:22 +08:00
|
|
|
|
|
|
|
struct gup_benchmark {
|
2018-10-27 06:09:52 +08:00
|
|
|
__u64 get_delta_usec;
|
|
|
|
__u64 put_delta_usec;
|
2017-11-18 07:31:22 +08:00
|
|
|
__u64 addr;
|
|
|
|
__u64 size;
|
|
|
|
__u32 nr_pages_per_call;
|
|
|
|
__u32 flags;
|
2018-10-27 06:09:52 +08:00
|
|
|
__u64 expansion[10]; /* For future use */
|
2017-11-18 07:31:22 +08:00
|
|
|
};
|
|
|
|
|
2020-04-02 12:05:41 +08:00
|
|
|
static void put_back_pages(unsigned int cmd, struct page **pages,
|
|
|
|
unsigned long nr_pages)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case GUP_FAST_BENCHMARK:
|
|
|
|
case GUP_BENCHMARK:
|
|
|
|
for (i = 0; i < nr_pages; i++)
|
|
|
|
put_page(pages[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PIN_FAST_BENCHMARK:
|
|
|
|
case PIN_BENCHMARK:
|
2020-10-14 07:51:54 +08:00
|
|
|
case PIN_LONGTERM_BENCHMARK:
|
2020-04-02 12:05:41 +08:00
|
|
|
unpin_user_pages(pages, nr_pages);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void verify_dma_pinned(unsigned int cmd, struct page **pages,
|
|
|
|
unsigned long nr_pages)
|
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case PIN_FAST_BENCHMARK:
|
|
|
|
case PIN_BENCHMARK:
|
2020-10-14 07:51:54 +08:00
|
|
|
case PIN_LONGTERM_BENCHMARK:
|
2020-04-02 12:05:41 +08:00
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
page = pages[i];
|
|
|
|
if (WARN(!page_maybe_dma_pinned(page),
|
|
|
|
"pages[%lu] is NOT dma-pinned\n", i)) {
|
|
|
|
|
|
|
|
dump_page(page, "gup_benchmark failure");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 07:31:22 +08:00
|
|
|
static int __gup_benchmark_ioctl(unsigned int cmd,
|
|
|
|
struct gup_benchmark *gup)
|
|
|
|
{
|
|
|
|
ktime_t start_time, end_time;
|
2018-10-06 06:51:44 +08:00
|
|
|
unsigned long i, nr_pages, addr, next;
|
|
|
|
int nr;
|
2017-11-18 07:31:22 +08:00
|
|
|
struct page **pages;
|
2020-01-05 05:00:12 +08:00
|
|
|
int ret = 0;
|
2020-10-18 07:14:12 +08:00
|
|
|
bool needs_mmap_lock =
|
|
|
|
cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK;
|
2017-11-18 07:31:22 +08:00
|
|
|
|
2018-10-31 06:04:32 +08:00
|
|
|
if (gup->size > ULONG_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-11-18 07:31:22 +08:00
|
|
|
nr_pages = gup->size / PAGE_SIZE;
|
treewide: kvzalloc() -> kvcalloc()
The kvzalloc() function has a 2-factor argument form, kvcalloc(). This
patch replaces cases of:
kvzalloc(a * b, gfp)
with:
kvcalloc(a * b, gfp)
as well as handling cases of:
kvzalloc(a * b * c, gfp)
with:
kvzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kvcalloc(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kvzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kvzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kvzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kvzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kvzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kvzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kvzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kvzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kvzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kvzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kvzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kvzalloc
+ kvcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kvzalloc
+ kvcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kvzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kvzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kvzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kvzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kvzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kvzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kvzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kvzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kvzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kvzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kvzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kvzalloc(C1 * C2 * C3, ...)
|
kvzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kvzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kvzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kvzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kvzalloc(sizeof(THING) * C2, ...)
|
kvzalloc(sizeof(TYPE) * C2, ...)
|
kvzalloc(C1 * C2 * C3, ...)
|
kvzalloc(C1 * C2, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kvzalloc
+ kvcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kvzalloc
+ kvcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kvzalloc
+ kvcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kvzalloc
+ kvcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:04:48 +08:00
|
|
|
pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
|
2017-11-18 07:31:22 +08:00
|
|
|
if (!pages)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-10-18 07:14:12 +08:00
|
|
|
if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) {
|
|
|
|
ret = -EINTR;
|
|
|
|
goto free_pages;
|
|
|
|
}
|
|
|
|
|
2017-11-18 07:31:22 +08:00
|
|
|
i = 0;
|
|
|
|
nr = gup->nr_pages_per_call;
|
|
|
|
start_time = ktime_get();
|
|
|
|
for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
|
|
|
|
if (nr != gup->nr_pages_per_call)
|
|
|
|
break;
|
|
|
|
|
|
|
|
next = addr + nr * PAGE_SIZE;
|
|
|
|
if (next > gup->addr + gup->size) {
|
|
|
|
next = gup->addr + gup->size;
|
|
|
|
nr = (next - addr) / PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:13:32 +08:00
|
|
|
/* Filter out most gup flags: only allow a tiny subset here: */
|
|
|
|
gup->flags &= FOLL_WRITE;
|
|
|
|
|
2018-10-27 06:09:56 +08:00
|
|
|
switch (cmd) {
|
|
|
|
case GUP_FAST_BENCHMARK:
|
2020-01-31 14:13:32 +08:00
|
|
|
nr = get_user_pages_fast(addr, nr, gup->flags,
|
2018-10-27 06:09:56 +08:00
|
|
|
pages + i);
|
|
|
|
break;
|
|
|
|
case GUP_BENCHMARK:
|
2020-01-31 14:13:32 +08:00
|
|
|
nr = get_user_pages(addr, nr, gup->flags, pages + i,
|
2018-10-27 06:09:56 +08:00
|
|
|
NULL);
|
|
|
|
break;
|
2020-04-02 12:05:41 +08:00
|
|
|
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;
|
2020-10-14 07:51:54 +08:00
|
|
|
case PIN_LONGTERM_BENCHMARK:
|
|
|
|
nr = pin_user_pages(addr, nr,
|
|
|
|
gup->flags | FOLL_LONGTERM,
|
|
|
|
pages + i, NULL);
|
|
|
|
break;
|
2018-10-27 06:09:56 +08:00
|
|
|
default:
|
2020-01-05 05:00:12 +08:00
|
|
|
ret = -EINVAL;
|
2020-10-18 07:14:12 +08:00
|
|
|
goto unlock;
|
2018-10-27 06:09:56 +08:00
|
|
|
}
|
|
|
|
|
2018-04-14 06:35:16 +08:00
|
|
|
if (nr <= 0)
|
|
|
|
break;
|
2017-11-18 07:31:22 +08:00
|
|
|
i += nr;
|
|
|
|
}
|
|
|
|
end_time = ktime_get();
|
|
|
|
|
2020-04-02 12:05:41 +08:00
|
|
|
/* Shifting the meaning of nr_pages: now it is actual number pinned: */
|
|
|
|
nr_pages = i;
|
|
|
|
|
2018-10-27 06:09:52 +08:00
|
|
|
gup->get_delta_usec = ktime_us_delta(end_time, start_time);
|
2017-11-18 07:31:22 +08:00
|
|
|
gup->size = addr - gup->addr;
|
|
|
|
|
2020-04-02 12:05:41 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2018-10-27 06:09:52 +08:00
|
|
|
start_time = ktime_get();
|
2020-04-02 12:05:41 +08:00
|
|
|
|
|
|
|
put_back_pages(cmd, pages, nr_pages);
|
|
|
|
|
2018-10-27 06:09:52 +08:00
|
|
|
end_time = ktime_get();
|
|
|
|
gup->put_delta_usec = ktime_us_delta(end_time, start_time);
|
2017-11-18 07:31:22 +08:00
|
|
|
|
2020-10-18 07:14:12 +08:00
|
|
|
unlock:
|
|
|
|
if (needs_mmap_lock)
|
|
|
|
mmap_read_unlock(current->mm);
|
|
|
|
free_pages:
|
2017-11-18 07:31:22 +08:00
|
|
|
kvfree(pages);
|
2020-01-05 05:00:12 +08:00
|
|
|
return ret;
|
2017-11-18 07:31:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
struct gup_benchmark gup;
|
|
|
|
int ret;
|
|
|
|
|
2018-10-27 06:09:56 +08:00
|
|
|
switch (cmd) {
|
|
|
|
case GUP_FAST_BENCHMARK:
|
|
|
|
case GUP_BENCHMARK:
|
2020-04-02 12:05:41 +08:00
|
|
|
case PIN_FAST_BENCHMARK:
|
|
|
|
case PIN_BENCHMARK:
|
2020-10-14 07:51:54 +08:00
|
|
|
case PIN_LONGTERM_BENCHMARK:
|
2018-10-27 06:09:56 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-11-18 07:31:22 +08:00
|
|
|
return -EINVAL;
|
2018-10-27 06:09:56 +08:00
|
|
|
}
|
2017-11-18 07:31:22 +08:00
|
|
|
|
|
|
|
if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
ret = __gup_benchmark_ioctl(cmd, &gup);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations gup_benchmark_fops = {
|
|
|
|
.open = nonseekable_open,
|
|
|
|
.unlocked_ioctl = gup_benchmark_ioctl,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gup_benchmark_init(void)
|
|
|
|
{
|
2019-03-06 07:46:09 +08:00
|
|
|
debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
|
|
|
|
&gup_benchmark_fops);
|
2017-11-18 07:31:22 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
late_initcall(gup_benchmark_init);
|