2005-07-08 08:57:14 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005 Topspin Communications. All rights reserved.
|
|
|
|
* Copyright (c) 2005 Cisco Systems. All rights reserved.
|
2005-08-11 14:03:10 +08:00
|
|
|
* Copyright (c) 2005 Mellanox Technologies. All rights reserved.
|
2005-07-08 08:57:14 +08:00
|
|
|
*
|
|
|
|
* This software is available to you under a choice of one of two
|
|
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
|
|
* General Public License (GPL) Version 2, available from the file
|
|
|
|
* COPYING in the main directory of this source tree, or the
|
|
|
|
* OpenIB.org BSD license below:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2017-02-09 01:51:30 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2017-02-09 01:51:29 +08:00
|
|
|
#include <linux/sched/mm.h>
|
2011-05-28 03:29:33 +08:00
|
|
|
#include <linux/export.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2019-04-03 03:52:52 +08:00
|
|
|
#include <linux/pagemap.h>
|
2014-12-11 23:04:17 +08:00
|
|
|
#include <rdma/ib_umem_odp.h>
|
2005-07-08 08:57:14 +08:00
|
|
|
|
|
|
|
#include "uverbs.h"
|
|
|
|
|
|
|
|
static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
|
|
|
|
{
|
2019-04-03 03:52:52 +08:00
|
|
|
struct sg_page_iter sg_iter;
|
2014-01-28 19:40:15 +08:00
|
|
|
struct page *page;
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2014-01-28 19:40:15 +08:00
|
|
|
if (umem->nmap > 0)
|
2019-04-03 03:52:52 +08:00
|
|
|
ib_dma_unmap_sg(dev, umem->sg_head.sgl, umem->sg_nents,
|
2014-01-28 19:40:15 +08:00
|
|
|
DMA_BIDIRECTIONAL);
|
2007-10-23 03:19:53 +08:00
|
|
|
|
2019-04-03 03:52:52 +08:00
|
|
|
for_each_sg_page(umem->sg_head.sgl, &sg_iter, umem->sg_nents, 0) {
|
|
|
|
page = sg_page_iter_page(&sg_iter);
|
mm/gup: add make_dirty arg to put_user_pages_dirty_lock()
[11~From: John Hubbard <jhubbard@nvidia.com>
Subject: mm/gup: add make_dirty arg to put_user_pages_dirty_lock()
Patch series "mm/gup: add make_dirty arg to put_user_pages_dirty_lock()",
v3.
There are about 50+ patches in my tree [2], and I'll be sending out the
remaining ones in a few more groups:
* The block/bio related changes (Jerome mostly wrote those, but I've had
to move stuff around extensively, and add a little code)
* mm/ changes
* other subsystem patches
* an RFC that shows the current state of the tracking patch set. That
can only be applied after all call sites are converted, but it's good to
get an early look at it.
This is part a tree-wide conversion, as described in fc1d8e7cca2d ("mm:
introduce put_user_page*(), placeholder versions").
This patch (of 3):
Provide more capable variation of put_user_pages_dirty_lock(), and delete
put_user_pages_dirty(). This is based on the following:
1. Lots of call sites become simpler if a bool is passed into
put_user_page*(), instead of making the call site choose which
put_user_page*() variant to call.
2. Christoph Hellwig's observation that set_page_dirty_lock() is
usually correct, and set_page_dirty() is usually a bug, or at least
questionable, within a put_user_page*() calling chain.
This leads to the following API choices:
* put_user_pages_dirty_lock(page, npages, make_dirty)
* There is no put_user_pages_dirty(). You have to
hand code that, in the rare case that it's
required.
[jhubbard@nvidia.com: remove unused variable in siw_free_plist()]
Link: http://lkml.kernel.org/r/20190729074306.10368-1-jhubbard@nvidia.com
Link: http://lkml.kernel.org/r/20190724044537.10458-2-jhubbard@nvidia.com
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ira Weiny <ira.weiny@intel.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-09-24 06:35:04 +08:00
|
|
|
put_user_pages_dirty_lock(&page, 1, umem->writable && dirty);
|
2005-07-08 08:57:14 +08:00
|
|
|
}
|
2014-01-28 19:40:15 +08:00
|
|
|
|
|
|
|
sg_free_table(&umem->sg_head);
|
2005-07-08 08:57:14 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 03:52:52 +08:00
|
|
|
/* ib_umem_add_sg_table - Add N contiguous pages to scatter table
|
|
|
|
*
|
|
|
|
* sg: current scatterlist entry
|
|
|
|
* page_list: array of npage struct page pointers
|
|
|
|
* npages: number of pages in page_list
|
|
|
|
* max_seg_sz: maximum segment size in bytes
|
|
|
|
* nents: [out] number of entries in the scatterlist
|
|
|
|
*
|
|
|
|
* Return new end of scatterlist
|
|
|
|
*/
|
|
|
|
static struct scatterlist *ib_umem_add_sg_table(struct scatterlist *sg,
|
|
|
|
struct page **page_list,
|
|
|
|
unsigned long npages,
|
|
|
|
unsigned int max_seg_sz,
|
|
|
|
int *nents)
|
|
|
|
{
|
|
|
|
unsigned long first_pfn;
|
|
|
|
unsigned long i = 0;
|
|
|
|
bool update_cur_sg = false;
|
|
|
|
bool first = !sg_page(sg);
|
|
|
|
|
|
|
|
/* Check if new page_list is contiguous with end of previous page_list.
|
|
|
|
* sg->length here is a multiple of PAGE_SIZE and sg->offset is 0.
|
|
|
|
*/
|
|
|
|
if (!first && (page_to_pfn(sg_page(sg)) + (sg->length >> PAGE_SHIFT) ==
|
|
|
|
page_to_pfn(page_list[0])))
|
|
|
|
update_cur_sg = true;
|
|
|
|
|
|
|
|
while (i != npages) {
|
|
|
|
unsigned long len;
|
|
|
|
struct page *first_page = page_list[i];
|
|
|
|
|
|
|
|
first_pfn = page_to_pfn(first_page);
|
|
|
|
|
|
|
|
/* Compute the number of contiguous pages we have starting
|
|
|
|
* at i
|
|
|
|
*/
|
|
|
|
for (len = 0; i != npages &&
|
2019-04-30 05:32:04 +08:00
|
|
|
first_pfn + len == page_to_pfn(page_list[i]) &&
|
|
|
|
len < (max_seg_sz >> PAGE_SHIFT);
|
2019-04-03 03:52:52 +08:00
|
|
|
len++)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
/* Squash N contiguous pages from page_list into current sge */
|
2019-04-30 05:32:04 +08:00
|
|
|
if (update_cur_sg) {
|
|
|
|
if ((max_seg_sz - sg->length) >= (len << PAGE_SHIFT)) {
|
|
|
|
sg_set_page(sg, sg_page(sg),
|
|
|
|
sg->length + (len << PAGE_SHIFT),
|
|
|
|
0);
|
|
|
|
update_cur_sg = false;
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-03 03:52:52 +08:00
|
|
|
update_cur_sg = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Squash N contiguous pages into next sge or first sge */
|
|
|
|
if (!first)
|
|
|
|
sg = sg_next(sg);
|
|
|
|
|
|
|
|
(*nents)++;
|
|
|
|
sg_set_page(sg, first_page, len << PAGE_SHIFT, 0);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sg;
|
|
|
|
}
|
|
|
|
|
2019-05-06 21:53:32 +08:00
|
|
|
/**
|
|
|
|
* ib_umem_find_best_pgsz - Find best HW page size to use for this MR
|
|
|
|
*
|
|
|
|
* @umem: umem struct
|
|
|
|
* @pgsz_bitmap: bitmap of HW supported page sizes
|
|
|
|
* @virt: IOVA
|
|
|
|
*
|
|
|
|
* This helper is intended for HW that support multiple page
|
|
|
|
* sizes but can do only a single page size in an MR.
|
|
|
|
*
|
|
|
|
* Returns 0 if the umem requires page sizes not supported by
|
|
|
|
* the driver to be mapped. Drivers always supporting PAGE_SIZE
|
|
|
|
* or smaller will never see a 0 result.
|
|
|
|
*/
|
|
|
|
unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
|
|
|
|
unsigned long pgsz_bitmap,
|
|
|
|
unsigned long virt)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
unsigned int best_pg_bit;
|
|
|
|
unsigned long va, pgoff;
|
|
|
|
dma_addr_t mask;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* At minimum, drivers must support PAGE_SIZE or smaller */
|
|
|
|
if (WARN_ON(!(pgsz_bitmap & GENMASK(PAGE_SHIFT, 0))))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va = virt;
|
|
|
|
/* max page size not to exceed MR length */
|
|
|
|
mask = roundup_pow_of_two(umem->length);
|
|
|
|
/* offset into first SGL */
|
|
|
|
pgoff = umem->address & ~PAGE_MASK;
|
|
|
|
|
|
|
|
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, i) {
|
|
|
|
/* Walk SGL and reduce max page size if VA/PA bits differ
|
|
|
|
* for any address.
|
|
|
|
*/
|
|
|
|
mask |= (sg_dma_address(sg) + pgoff) ^ va;
|
|
|
|
if (i && i != (umem->nmap - 1))
|
|
|
|
/* restrict by length as well for interior SGEs */
|
|
|
|
mask |= sg_dma_len(sg);
|
|
|
|
va += sg_dma_len(sg) - pgoff;
|
|
|
|
pgoff = 0;
|
|
|
|
}
|
|
|
|
best_pg_bit = rdma_find_pg_bit(mask, pgsz_bitmap);
|
|
|
|
|
|
|
|
return BIT_ULL(best_pg_bit);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ib_umem_find_best_pgsz);
|
|
|
|
|
2007-03-05 08:15:11 +08:00
|
|
|
/**
|
|
|
|
* ib_umem_get - Pin and DMA map userspace memory.
|
2014-12-11 23:04:17 +08:00
|
|
|
*
|
2020-01-15 20:43:31 +08:00
|
|
|
* @device: IB device to connect UMEM
|
2007-03-05 08:15:11 +08:00
|
|
|
* @addr: userspace virtual address to start at
|
|
|
|
* @size: length of region to pin
|
|
|
|
* @access: IB_ACCESS_xxx flags for memory being pinned
|
|
|
|
*/
|
2020-01-15 20:43:31 +08:00
|
|
|
struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr,
|
2019-11-13 15:32:14 +08:00
|
|
|
size_t size, int access)
|
2005-07-08 08:57:14 +08:00
|
|
|
{
|
2007-03-05 08:15:11 +08:00
|
|
|
struct ib_umem *umem;
|
2005-07-08 08:57:14 +08:00
|
|
|
struct page **page_list;
|
|
|
|
unsigned long lock_limit;
|
2018-09-21 23:30:13 +08:00
|
|
|
unsigned long new_pinned;
|
2005-07-08 08:57:14 +08:00
|
|
|
unsigned long cur_base;
|
2018-09-17 01:44:45 +08:00
|
|
|
struct mm_struct *mm;
|
2005-07-08 08:57:14 +08:00
|
|
|
unsigned long npages;
|
2007-03-05 08:15:11 +08:00
|
|
|
int ret;
|
2019-04-03 03:52:52 +08:00
|
|
|
struct scatterlist *sg;
|
2016-10-13 08:20:16 +08:00
|
|
|
unsigned int gup_flags = FOLL_WRITE;
|
2008-04-29 16:00:34 +08:00
|
|
|
|
2015-03-19 01:39:08 +08:00
|
|
|
/*
|
|
|
|
* If the combination of the addr and size requested for this memory
|
|
|
|
* region causes an integer overflow, return error.
|
|
|
|
*/
|
2015-04-13 20:56:23 +08:00
|
|
|
if (((addr + size) < addr) ||
|
|
|
|
PAGE_ALIGN(addr + size) < (addr + size))
|
2015-03-19 01:39:08 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2005-07-08 08:57:14 +08:00
|
|
|
if (!can_do_mlock())
|
2007-03-05 08:15:11 +08:00
|
|
|
return ERR_PTR(-EPERM);
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2019-08-19 19:17:04 +08:00
|
|
|
if (access & IB_ACCESS_ON_DEMAND)
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2019-08-19 19:17:04 +08:00
|
|
|
umem = kzalloc(sizeof(*umem), GFP_KERNEL);
|
|
|
|
if (!umem)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2020-01-15 20:43:31 +08:00
|
|
|
umem->ibdev = device;
|
2017-04-05 14:23:50 +08:00
|
|
|
umem->length = size;
|
|
|
|
umem->address = addr;
|
2018-05-23 20:30:30 +08:00
|
|
|
umem->writable = ib_access_writable(access);
|
2018-09-17 01:48:05 +08:00
|
|
|
umem->owning_mm = mm = current->mm;
|
|
|
|
mmgrab(mm);
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2007-03-05 08:15:11 +08:00
|
|
|
page_list = (struct page **) __get_free_page(GFP_KERNEL);
|
|
|
|
if (!page_list) {
|
2018-07-10 18:31:49 +08:00
|
|
|
ret = -ENOMEM;
|
2018-09-17 01:48:05 +08:00
|
|
|
goto umem_kfree;
|
2007-03-05 08:15:11 +08:00
|
|
|
}
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2014-12-11 23:04:12 +08:00
|
|
|
npages = ib_umem_num_pages(umem);
|
2018-09-21 23:30:12 +08:00
|
|
|
if (npages == 0 || npages > UINT_MAX) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2010-02-12 07:40:48 +08:00
|
|
|
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2019-02-07 01:59:20 +08:00
|
|
|
new_pinned = atomic64_add_return(npages, &mm->pinned_vm);
|
2019-02-07 01:59:15 +08:00
|
|
|
if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
|
2019-02-07 01:59:20 +08:00
|
|
|
atomic64_sub(npages, &mm->pinned_vm);
|
2005-07-08 08:57:14 +08:00
|
|
|
ret = -ENOMEM;
|
2018-09-21 23:30:13 +08:00
|
|
|
goto out;
|
2005-07-08 08:57:14 +08:00
|
|
|
}
|
|
|
|
|
2007-03-05 08:15:11 +08:00
|
|
|
cur_base = addr & PAGE_MASK;
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2014-01-28 19:40:15 +08:00
|
|
|
ret = sg_alloc_table(&umem->sg_head, npages, GFP_KERNEL);
|
|
|
|
if (ret)
|
2018-07-10 18:31:49 +08:00
|
|
|
goto vma;
|
2014-01-28 19:40:15 +08:00
|
|
|
|
2016-10-13 08:20:16 +08:00
|
|
|
if (!umem->writable)
|
|
|
|
gup_flags |= FOLL_FORCE;
|
|
|
|
|
2019-04-03 03:52:52 +08:00
|
|
|
sg = umem->sg_head.sgl;
|
2014-01-28 19:40:15 +08:00
|
|
|
|
2005-07-08 08:57:14 +08:00
|
|
|
while (npages) {
|
2018-09-25 17:04:04 +08:00
|
|
|
down_read(&mm->mmap_sem);
|
mm/gup: replace get_user_pages_longterm() with FOLL_LONGTERM
Pach series "Add FOLL_LONGTERM to GUP fast and use it".
HFI1, qib, and mthca, use get_user_pages_fast() due to its performance
advantages. These pages can be held for a significant time. But
get_user_pages_fast() does not protect against mapping FS DAX pages.
Introduce FOLL_LONGTERM and use this flag in get_user_pages_fast() which
retains the performance while also adding the FS DAX checks. XDP has also
shown interest in using this functionality.[1]
In addition we change get_user_pages() to use the new FOLL_LONGTERM flag
and remove the specialized get_user_pages_longterm call.
[1] https://lkml.org/lkml/2019/3/19/939
"longterm" is a relative thing and at this point is probably a misnomer.
This is really flagging a pin which is going to be given to hardware and
can't move. I've thought of a couple of alternative names but I think we
have to settle on if we are going to use FL_LAYOUT or something else to
solve the "longterm" problem. Then I think we can change the flag to a
better name.
Secondly, it depends on how often you are registering memory. I have
spoken with some RDMA users who consider MR in the performance path...
For the overall application performance. I don't have the numbers as the
tests for HFI1 were done a long time ago. But there was a significant
advantage. Some of which is probably due to the fact that you don't have
to hold mmap_sem.
Finally, architecturally I think it would be good for everyone to use
*_fast. There are patches submitted to the RDMA list which would allow
the use of *_fast (they reworking the use of mmap_sem) and as soon as they
are accepted I'll submit a patch to convert the RDMA core as well. Also
to this point others are looking to use *_fast.
As an aside, Jasons pointed out in my previous submission that *_fast and
*_unlocked look very much the same. I agree and I think further cleanup
will be coming. But I'm focused on getting the final solution for DAX at
the moment.
This patch (of 7):
This patch starts a series which aims to support FOLL_LONGTERM in
get_user_pages_fast(). Some callers who would like to do a longterm (user
controlled pin) of pages with the fast variant of GUP for performance
purposes.
Rather than have a separate get_user_pages_longterm() call, introduce
FOLL_LONGTERM and change the longterm callers to use it.
This patch does not change any functionality. In the short term
"longterm" or user controlled pins are unsafe for Filesystems and FS DAX
in particular has been blocked. However, callers of get_user_pages_fast()
were not "protected".
FOLL_LONGTERM can _only_ be supported with get_user_pages[_fast]() as it
requires vmas to determine if DAX is in use.
NOTE: In merging with the CMA changes we opt to change the
get_user_pages() call in check_and_migrate_cma_pages() to a call of
__get_user_pages_locked() on the newly migrated pages. This makes the
code read better in that we are calling __get_user_pages_locked() on the
pages before and after a potential migration.
As a side affect some of the interfaces are cleaned up but this is not the
primary purpose of the series.
In review[1] it was asked:
<quote>
> This I don't get - if you do lock down long term mappings performance
> of the actual get_user_pages call shouldn't matter to start with.
>
> What do I miss?
A couple of points.
First "longterm" is a relative thing and at this point is probably a
misnomer. This is really flagging a pin which is going to be given to
hardware and can't move. I've thought of a couple of alternative names
but I think we have to settle on if we are going to use FL_LAYOUT or
something else to solve the "longterm" problem. Then I think we can
change the flag to a better name.
Second, It depends on how often you are registering memory. I have spoken
with some RDMA users who consider MR in the performance path... For the
overall application performance. I don't have the numbers as the tests
for HFI1 were done a long time ago. But there was a significant
advantage. Some of which is probably due to the fact that you don't have
to hold mmap_sem.
Finally, architecturally I think it would be good for everyone to use
*_fast. There are patches submitted to the RDMA list which would allow
the use of *_fast (they reworking the use of mmap_sem) and as soon as they
are accepted I'll submit a patch to convert the RDMA core as well. Also
to this point others are looking to use *_fast.
As an asside, Jasons pointed out in my previous submission that *_fast and
*_unlocked look very much the same. I agree and I think further cleanup
will be coming. But I'm focused on getting the final solution for DAX at
the moment.
</quote>
[1] https://lore.kernel.org/lkml/20190220180255.GA12020@iweiny-DESK2.sc.intel.com/T/#md6abad2569f3bf6c1f03686c8097ab6563e94965
[ira.weiny@intel.com: v3]
Link: http://lkml.kernel.org/r/20190328084422.29911-2-ira.weiny@intel.com
Link: http://lkml.kernel.org/r/20190328084422.29911-2-ira.weiny@intel.com
Link: http://lkml.kernel.org/r/20190317183438.2057-2-ira.weiny@intel.com
Signed-off-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Rich Felker <dalias@libc.org>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: James Hogan <jhogan@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Mike Marshall <hubcap@omnibond.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-14 08:17:03 +08:00
|
|
|
ret = get_user_pages(cur_base,
|
IB/umem: Avoid sign problems when demoting npages to integer
On a 64-bit architecture, if ib_umem_get() is called with a size value
that is so big that npages is negative when cast to int, then the
length of the page list passed to get_user_pages(), namely
min_t(int, npages, PAGE_SIZE / sizeof (struct page *))
will be negative, and get_user_pages() will immediately return 0 (at
least since 900cf086, "Be more robust about bad arguments in
get_user_pages()"). This leads to an infinite loop in ib_umem_get(),
since the code boils down to:
while (npages) {
ret = get_user_pages(...);
npages -= ret;
}
Fix this by taking the minimum as unsigned longs, so that the value of
npages is never truncated.
The impact of this bug isn't too severe, since the value of npages is
checked against RLIMIT_MEMLOCK, so a process would need to have an
astronomical limit or have CAP_IPC_LOCK to be able to trigger this,
and such a process could already cause lots of mischief. But it does
let buggy userspace code cause a kernel lock-up; for example I hit
this with code that passes a negative value into a memory registartion
function where it is promoted to a huge u64 value.
Cc: <stable@kernel.org>
Signed-off-by: Roland Dreier <rolandd@cisco.com>
2008-06-07 12:38:37 +08:00
|
|
|
min_t(unsigned long, npages,
|
2005-07-08 08:57:14 +08:00
|
|
|
PAGE_SIZE / sizeof (struct page *)),
|
mm/gup: replace get_user_pages_longterm() with FOLL_LONGTERM
Pach series "Add FOLL_LONGTERM to GUP fast and use it".
HFI1, qib, and mthca, use get_user_pages_fast() due to its performance
advantages. These pages can be held for a significant time. But
get_user_pages_fast() does not protect against mapping FS DAX pages.
Introduce FOLL_LONGTERM and use this flag in get_user_pages_fast() which
retains the performance while also adding the FS DAX checks. XDP has also
shown interest in using this functionality.[1]
In addition we change get_user_pages() to use the new FOLL_LONGTERM flag
and remove the specialized get_user_pages_longterm call.
[1] https://lkml.org/lkml/2019/3/19/939
"longterm" is a relative thing and at this point is probably a misnomer.
This is really flagging a pin which is going to be given to hardware and
can't move. I've thought of a couple of alternative names but I think we
have to settle on if we are going to use FL_LAYOUT or something else to
solve the "longterm" problem. Then I think we can change the flag to a
better name.
Secondly, it depends on how often you are registering memory. I have
spoken with some RDMA users who consider MR in the performance path...
For the overall application performance. I don't have the numbers as the
tests for HFI1 were done a long time ago. But there was a significant
advantage. Some of which is probably due to the fact that you don't have
to hold mmap_sem.
Finally, architecturally I think it would be good for everyone to use
*_fast. There are patches submitted to the RDMA list which would allow
the use of *_fast (they reworking the use of mmap_sem) and as soon as they
are accepted I'll submit a patch to convert the RDMA core as well. Also
to this point others are looking to use *_fast.
As an aside, Jasons pointed out in my previous submission that *_fast and
*_unlocked look very much the same. I agree and I think further cleanup
will be coming. But I'm focused on getting the final solution for DAX at
the moment.
This patch (of 7):
This patch starts a series which aims to support FOLL_LONGTERM in
get_user_pages_fast(). Some callers who would like to do a longterm (user
controlled pin) of pages with the fast variant of GUP for performance
purposes.
Rather than have a separate get_user_pages_longterm() call, introduce
FOLL_LONGTERM and change the longterm callers to use it.
This patch does not change any functionality. In the short term
"longterm" or user controlled pins are unsafe for Filesystems and FS DAX
in particular has been blocked. However, callers of get_user_pages_fast()
were not "protected".
FOLL_LONGTERM can _only_ be supported with get_user_pages[_fast]() as it
requires vmas to determine if DAX is in use.
NOTE: In merging with the CMA changes we opt to change the
get_user_pages() call in check_and_migrate_cma_pages() to a call of
__get_user_pages_locked() on the newly migrated pages. This makes the
code read better in that we are calling __get_user_pages_locked() on the
pages before and after a potential migration.
As a side affect some of the interfaces are cleaned up but this is not the
primary purpose of the series.
In review[1] it was asked:
<quote>
> This I don't get - if you do lock down long term mappings performance
> of the actual get_user_pages call shouldn't matter to start with.
>
> What do I miss?
A couple of points.
First "longterm" is a relative thing and at this point is probably a
misnomer. This is really flagging a pin which is going to be given to
hardware and can't move. I've thought of a couple of alternative names
but I think we have to settle on if we are going to use FL_LAYOUT or
something else to solve the "longterm" problem. Then I think we can
change the flag to a better name.
Second, It depends on how often you are registering memory. I have spoken
with some RDMA users who consider MR in the performance path... For the
overall application performance. I don't have the numbers as the tests
for HFI1 were done a long time ago. But there was a significant
advantage. Some of which is probably due to the fact that you don't have
to hold mmap_sem.
Finally, architecturally I think it would be good for everyone to use
*_fast. There are patches submitted to the RDMA list which would allow
the use of *_fast (they reworking the use of mmap_sem) and as soon as they
are accepted I'll submit a patch to convert the RDMA core as well. Also
to this point others are looking to use *_fast.
As an asside, Jasons pointed out in my previous submission that *_fast and
*_unlocked look very much the same. I agree and I think further cleanup
will be coming. But I'm focused on getting the final solution for DAX at
the moment.
</quote>
[1] https://lore.kernel.org/lkml/20190220180255.GA12020@iweiny-DESK2.sc.intel.com/T/#md6abad2569f3bf6c1f03686c8097ab6563e94965
[ira.weiny@intel.com: v3]
Link: http://lkml.kernel.org/r/20190328084422.29911-2-ira.weiny@intel.com
Link: http://lkml.kernel.org/r/20190328084422.29911-2-ira.weiny@intel.com
Link: http://lkml.kernel.org/r/20190317183438.2057-2-ira.weiny@intel.com
Signed-off-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Rich Felker <dalias@libc.org>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: James Hogan <jhogan@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Mike Marshall <hubcap@omnibond.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-14 08:17:03 +08:00
|
|
|
gup_flags | FOLL_LONGTERM,
|
|
|
|
page_list, NULL);
|
2018-07-10 18:31:48 +08:00
|
|
|
if (ret < 0) {
|
2018-09-17 01:44:45 +08:00
|
|
|
up_read(&mm->mmap_sem);
|
2018-07-10 18:31:49 +08:00
|
|
|
goto umem_release;
|
2018-07-10 18:31:48 +08:00
|
|
|
}
|
2005-07-08 08:57:14 +08:00
|
|
|
|
|
|
|
cur_base += ret * PAGE_SIZE;
|
|
|
|
npages -= ret;
|
|
|
|
|
2019-04-03 03:52:52 +08:00
|
|
|
sg = ib_umem_add_sg_table(sg, page_list, ret,
|
2020-01-15 20:43:31 +08:00
|
|
|
dma_get_max_seg_size(device->dma_device),
|
2019-04-03 03:52:52 +08:00
|
|
|
&umem->sg_nents);
|
|
|
|
|
|
|
|
up_read(&mm->mmap_sem);
|
2005-07-08 08:57:14 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 03:52:52 +08:00
|
|
|
sg_mark_end(sg);
|
|
|
|
|
2020-01-15 20:43:31 +08:00
|
|
|
umem->nmap = ib_dma_map_sg(device,
|
|
|
|
umem->sg_head.sgl,
|
|
|
|
umem->sg_nents,
|
|
|
|
DMA_BIDIRECTIONAL);
|
2014-01-28 19:40:15 +08:00
|
|
|
|
2018-06-24 16:23:48 +08:00
|
|
|
if (!umem->nmap) {
|
2014-01-28 19:40:15 +08:00
|
|
|
ret = -ENOMEM;
|
2018-07-10 18:31:49 +08:00
|
|
|
goto umem_release;
|
2014-01-28 19:40:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2018-07-10 18:31:49 +08:00
|
|
|
goto out;
|
2014-01-28 19:40:15 +08:00
|
|
|
|
2018-07-10 18:31:49 +08:00
|
|
|
umem_release:
|
2020-01-15 20:43:31 +08:00
|
|
|
__ib_umem_release(device, umem, 0);
|
2018-07-10 18:31:49 +08:00
|
|
|
vma:
|
2019-02-07 01:59:15 +08:00
|
|
|
atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm);
|
2005-07-08 08:57:14 +08:00
|
|
|
out:
|
|
|
|
free_page((unsigned long) page_list);
|
2018-07-10 18:31:49 +08:00
|
|
|
umem_kfree:
|
2018-09-17 01:48:05 +08:00
|
|
|
if (ret) {
|
|
|
|
mmdrop(umem->owning_mm);
|
2018-07-10 18:31:49 +08:00
|
|
|
kfree(umem);
|
2018-09-17 01:48:05 +08:00
|
|
|
}
|
2018-07-10 18:31:49 +08:00
|
|
|
return ret ? ERR_PTR(ret) : umem;
|
2005-07-08 08:57:14 +08:00
|
|
|
}
|
2007-03-05 08:15:11 +08:00
|
|
|
EXPORT_SYMBOL(ib_umem_get);
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2007-03-05 08:15:11 +08:00
|
|
|
/**
|
|
|
|
* ib_umem_release - release memory pinned with ib_umem_get
|
|
|
|
* @umem: umem struct to release
|
|
|
|
*/
|
|
|
|
void ib_umem_release(struct ib_umem *umem)
|
2005-07-08 08:57:14 +08:00
|
|
|
{
|
2019-06-16 20:05:20 +08:00
|
|
|
if (!umem)
|
|
|
|
return;
|
2019-08-19 19:17:05 +08:00
|
|
|
if (umem->is_odp)
|
|
|
|
return ib_umem_odp_release(to_ib_umem_odp(umem));
|
2014-12-11 23:04:17 +08:00
|
|
|
|
2019-08-07 07:15:44 +08:00
|
|
|
__ib_umem_release(umem->ibdev, umem, 1);
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2019-02-07 01:59:15 +08:00
|
|
|
atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm);
|
2019-08-19 19:17:05 +08:00
|
|
|
mmdrop(umem->owning_mm);
|
|
|
|
kfree(umem);
|
2007-03-05 08:15:11 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ib_umem_release);
|
|
|
|
|
|
|
|
int ib_umem_page_count(struct ib_umem *umem)
|
|
|
|
{
|
2019-08-15 16:38:30 +08:00
|
|
|
int i, n = 0;
|
2014-01-28 19:40:15 +08:00
|
|
|
struct scatterlist *sg;
|
2007-03-05 08:15:11 +08:00
|
|
|
|
2014-01-28 19:40:15 +08:00
|
|
|
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, i)
|
2019-05-20 14:05:25 +08:00
|
|
|
n += sg_dma_len(sg) >> PAGE_SHIFT;
|
2005-07-08 08:57:14 +08:00
|
|
|
|
2007-03-05 08:15:11 +08:00
|
|
|
return n;
|
2005-07-08 08:57:14 +08:00
|
|
|
}
|
2007-03-05 08:15:11 +08:00
|
|
|
EXPORT_SYMBOL(ib_umem_page_count);
|
2014-12-11 23:04:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy from the given ib_umem's pages to the given buffer.
|
|
|
|
*
|
|
|
|
* umem - the umem to copy from
|
|
|
|
* offset - offset to start copying from
|
|
|
|
* dst - destination buffer
|
|
|
|
* length - buffer length
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or an error code.
|
|
|
|
*/
|
|
|
|
int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
size_t end = offset + length;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (offset > umem->length || length > umem->length - offset) {
|
|
|
|
pr_err("ib_umem_copy_from not in range. offset: %zd umem length: %zd end: %zd\n",
|
|
|
|
offset, umem->length, end);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-04-04 23:22:47 +08:00
|
|
|
ret = sg_pcopy_to_buffer(umem->sg_head.sgl, umem->sg_nents, dst, length,
|
|
|
|
offset + ib_umem_offset(umem));
|
2014-12-11 23:04:13 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret != length)
|
|
|
|
return -EINVAL;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ib_umem_copy_from);
|