2005-07-08 07:57:14 +07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005 Topspin Communications. All rights reserved.
|
|
|
|
* Copyright (c) 2005 Cisco Systems. All rights reserved.
|
2005-08-11 13:03:10 +07:00
|
|
|
* Copyright (c) 2005 Mellanox Technologies. All rights reserved.
|
2005-07-08 07:57:14 +07: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 00:51:30 +07:00
|
|
|
#include <linux/sched/signal.h>
|
2017-02-09 00:51:29 +07:00
|
|
|
#include <linux/sched/mm.h>
|
2011-05-28 02:29:33 +07: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 15:04:11 +07:00
|
|
|
#include <linux/slab.h>
|
2019-04-03 02:52:52 +07:00
|
|
|
#include <linux/pagemap.h>
|
2014-12-11 22:04:17 +07:00
|
|
|
#include <rdma/ib_umem_odp.h>
|
2005-07-08 07:57:14 +07:00
|
|
|
|
|
|
|
#include "uverbs.h"
|
|
|
|
|
|
|
|
static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
|
|
|
|
{
|
2019-04-03 02:52:52 +07:00
|
|
|
struct sg_page_iter sg_iter;
|
2014-01-28 18:40:15 +07:00
|
|
|
struct page *page;
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2014-01-28 18:40:15 +07:00
|
|
|
if (umem->nmap > 0)
|
2019-04-03 02:52:52 +07:00
|
|
|
ib_dma_unmap_sg(dev, umem->sg_head.sgl, umem->sg_nents,
|
2014-01-28 18:40:15 +07:00
|
|
|
DMA_BIDIRECTIONAL);
|
2007-10-23 02:19:53 +07:00
|
|
|
|
2019-04-03 02:52:52 +07:00
|
|
|
for_each_sg_page(umem->sg_head.sgl, &sg_iter, umem->sg_nents, 0) {
|
|
|
|
page = sg_page_iter_page(&sg_iter);
|
2020-01-31 13:13:35 +07:00
|
|
|
unpin_user_pages_dirty_lock(&page, 1, umem->writable && dirty);
|
2005-07-08 07:57:14 +07:00
|
|
|
}
|
2014-01-28 18:40:15 +07:00
|
|
|
|
|
|
|
sg_free_table(&umem->sg_head);
|
2005-07-08 07:57:14 +07:00
|
|
|
}
|
|
|
|
|
2019-04-03 02:52:52 +07: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 04:32:04 +07:00
|
|
|
first_pfn + len == page_to_pfn(page_list[i]) &&
|
|
|
|
len < (max_seg_sz >> PAGE_SHIFT);
|
2019-04-03 02:52:52 +07:00
|
|
|
len++)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
/* Squash N contiguous pages from page_list into current sge */
|
2019-04-30 04:32:04 +07: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 02:52:52 +07: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 20:53:32 +07: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;
|
|
|
|
va += sg_dma_len(sg) - pgoff;
|
2020-01-28 20:56:12 +07:00
|
|
|
/* Except for the last entry, the ending iova alignment sets
|
|
|
|
* the maximum possible page size as the low bits of the iova
|
|
|
|
* must be zero when starting the next chunk.
|
|
|
|
*/
|
|
|
|
if (i != (umem->nmap - 1))
|
|
|
|
mask |= va;
|
2019-05-06 20:53:32 +07:00
|
|
|
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 07:15:11 +07:00
|
|
|
/**
|
|
|
|
* ib_umem_get - Pin and DMA map userspace memory.
|
2014-12-11 22:04:17 +07:00
|
|
|
*
|
2020-01-15 19:43:31 +07:00
|
|
|
* @device: IB device to connect UMEM
|
2007-03-05 07:15:11 +07: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 19:43:31 +07:00
|
|
|
struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr,
|
2019-11-13 14:32:14 +07:00
|
|
|
size_t size, int access)
|
2005-07-08 07:57:14 +07:00
|
|
|
{
|
2007-03-05 07:15:11 +07:00
|
|
|
struct ib_umem *umem;
|
2005-07-08 07:57:14 +07:00
|
|
|
struct page **page_list;
|
|
|
|
unsigned long lock_limit;
|
2018-09-21 22:30:13 +07:00
|
|
|
unsigned long new_pinned;
|
2005-07-08 07:57:14 +07:00
|
|
|
unsigned long cur_base;
|
2020-02-12 14:35:59 +07:00
|
|
|
unsigned long dma_attr = 0;
|
2018-09-17 00:44:45 +07:00
|
|
|
struct mm_struct *mm;
|
2005-07-08 07:57:14 +07:00
|
|
|
unsigned long npages;
|
2007-03-05 07:15:11 +07:00
|
|
|
int ret;
|
2019-04-03 02:52:52 +07:00
|
|
|
struct scatterlist *sg;
|
2016-10-13 07:20:16 +07:00
|
|
|
unsigned int gup_flags = FOLL_WRITE;
|
2008-04-29 15:00:34 +07:00
|
|
|
|
2015-03-19 00:39:08 +07:00
|
|
|
/*
|
|
|
|
* If the combination of the addr and size requested for this memory
|
|
|
|
* region causes an integer overflow, return error.
|
|
|
|
*/
|
2015-04-13 19:56:23 +07:00
|
|
|
if (((addr + size) < addr) ||
|
|
|
|
PAGE_ALIGN(addr + size) < (addr + size))
|
2015-03-19 00:39:08 +07:00
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2005-07-08 07:57:14 +07:00
|
|
|
if (!can_do_mlock())
|
2007-03-05 07:15:11 +07:00
|
|
|
return ERR_PTR(-EPERM);
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2019-08-19 18:17:04 +07:00
|
|
|
if (access & IB_ACCESS_ON_DEMAND)
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2019-08-19 18:17:04 +07:00
|
|
|
umem = kzalloc(sizeof(*umem), GFP_KERNEL);
|
|
|
|
if (!umem)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2020-01-15 19:43:31 +07:00
|
|
|
umem->ibdev = device;
|
2017-04-05 13:23:50 +07:00
|
|
|
umem->length = size;
|
|
|
|
umem->address = addr;
|
2018-05-23 19:30:30 +07:00
|
|
|
umem->writable = ib_access_writable(access);
|
2018-09-17 00:48:05 +07:00
|
|
|
umem->owning_mm = mm = current->mm;
|
|
|
|
mmgrab(mm);
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2007-03-05 07:15:11 +07:00
|
|
|
page_list = (struct page **) __get_free_page(GFP_KERNEL);
|
|
|
|
if (!page_list) {
|
2018-07-10 17:31:49 +07:00
|
|
|
ret = -ENOMEM;
|
2018-09-17 00:48:05 +07:00
|
|
|
goto umem_kfree;
|
2007-03-05 07:15:11 +07:00
|
|
|
}
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2014-12-11 22:04:12 +07:00
|
|
|
npages = ib_umem_num_pages(umem);
|
2018-09-21 22:30:12 +07:00
|
|
|
if (npages == 0 || npages > UINT_MAX) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2010-02-12 06:40:48 +07:00
|
|
|
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2019-02-07 00:59:20 +07:00
|
|
|
new_pinned = atomic64_add_return(npages, &mm->pinned_vm);
|
2019-02-07 00:59:15 +07:00
|
|
|
if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
|
2019-02-07 00:59:20 +07:00
|
|
|
atomic64_sub(npages, &mm->pinned_vm);
|
2005-07-08 07:57:14 +07:00
|
|
|
ret = -ENOMEM;
|
2018-09-21 22:30:13 +07:00
|
|
|
goto out;
|
2005-07-08 07:57:14 +07:00
|
|
|
}
|
|
|
|
|
2007-03-05 07:15:11 +07:00
|
|
|
cur_base = addr & PAGE_MASK;
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2014-01-28 18:40:15 +07:00
|
|
|
ret = sg_alloc_table(&umem->sg_head, npages, GFP_KERNEL);
|
|
|
|
if (ret)
|
2018-07-10 17:31:49 +07:00
|
|
|
goto vma;
|
2014-01-28 18:40:15 +07:00
|
|
|
|
2016-10-13 07:20:16 +07:00
|
|
|
if (!umem->writable)
|
|
|
|
gup_flags |= FOLL_FORCE;
|
|
|
|
|
2019-04-03 02:52:52 +07:00
|
|
|
sg = umem->sg_head.sgl;
|
2014-01-28 18:40:15 +07:00
|
|
|
|
2005-07-08 07:57:14 +07:00
|
|
|
while (npages) {
|
2020-01-31 13:13:02 +07:00
|
|
|
ret = pin_user_pages_fast(cur_base,
|
2020-01-31 13:12:47 +07:00
|
|
|
min_t(unsigned long, npages,
|
|
|
|
PAGE_SIZE /
|
|
|
|
sizeof(struct page *)),
|
|
|
|
gup_flags | FOLL_LONGTERM, page_list);
|
|
|
|
if (ret < 0)
|
2018-07-10 17:31:49 +07:00
|
|
|
goto umem_release;
|
2005-07-08 07:57:14 +07:00
|
|
|
|
|
|
|
cur_base += ret * PAGE_SIZE;
|
|
|
|
npages -= ret;
|
|
|
|
|
2019-04-03 02:52:52 +07:00
|
|
|
sg = ib_umem_add_sg_table(sg, page_list, ret,
|
2020-01-15 19:43:31 +07:00
|
|
|
dma_get_max_seg_size(device->dma_device),
|
2019-04-03 02:52:52 +07:00
|
|
|
&umem->sg_nents);
|
2005-07-08 07:57:14 +07:00
|
|
|
}
|
|
|
|
|
2019-04-03 02:52:52 +07:00
|
|
|
sg_mark_end(sg);
|
|
|
|
|
2020-02-12 14:35:59 +07:00
|
|
|
if (access & IB_ACCESS_RELAXED_ORDERING)
|
|
|
|
dma_attr |= DMA_ATTR_WEAK_ORDERING;
|
|
|
|
|
|
|
|
umem->nmap =
|
|
|
|
ib_dma_map_sg_attrs(device, umem->sg_head.sgl, umem->sg_nents,
|
|
|
|
DMA_BIDIRECTIONAL, dma_attr);
|
2014-01-28 18:40:15 +07:00
|
|
|
|
2018-06-24 15:23:48 +07:00
|
|
|
if (!umem->nmap) {
|
2014-01-28 18:40:15 +07:00
|
|
|
ret = -ENOMEM;
|
2018-07-10 17:31:49 +07:00
|
|
|
goto umem_release;
|
2014-01-28 18:40:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2018-07-10 17:31:49 +07:00
|
|
|
goto out;
|
2014-01-28 18:40:15 +07:00
|
|
|
|
2018-07-10 17:31:49 +07:00
|
|
|
umem_release:
|
2020-01-15 19:43:31 +07:00
|
|
|
__ib_umem_release(device, umem, 0);
|
2018-07-10 17:31:49 +07:00
|
|
|
vma:
|
2019-02-07 00:59:15 +07:00
|
|
|
atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm);
|
2005-07-08 07:57:14 +07:00
|
|
|
out:
|
|
|
|
free_page((unsigned long) page_list);
|
2018-07-10 17:31:49 +07:00
|
|
|
umem_kfree:
|
2018-09-17 00:48:05 +07:00
|
|
|
if (ret) {
|
|
|
|
mmdrop(umem->owning_mm);
|
2018-07-10 17:31:49 +07:00
|
|
|
kfree(umem);
|
2018-09-17 00:48:05 +07:00
|
|
|
}
|
2018-07-10 17:31:49 +07:00
|
|
|
return ret ? ERR_PTR(ret) : umem;
|
2005-07-08 07:57:14 +07:00
|
|
|
}
|
2007-03-05 07:15:11 +07:00
|
|
|
EXPORT_SYMBOL(ib_umem_get);
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2007-03-05 07:15:11 +07: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 07:57:14 +07:00
|
|
|
{
|
2019-06-16 19:05:20 +07:00
|
|
|
if (!umem)
|
|
|
|
return;
|
2019-08-19 18:17:05 +07:00
|
|
|
if (umem->is_odp)
|
|
|
|
return ib_umem_odp_release(to_ib_umem_odp(umem));
|
2014-12-11 22:04:17 +07:00
|
|
|
|
2019-08-07 06:15:44 +07:00
|
|
|
__ib_umem_release(umem->ibdev, umem, 1);
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2019-02-07 00:59:15 +07:00
|
|
|
atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm);
|
2019-08-19 18:17:05 +07:00
|
|
|
mmdrop(umem->owning_mm);
|
|
|
|
kfree(umem);
|
2007-03-05 07:15:11 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ib_umem_release);
|
|
|
|
|
|
|
|
int ib_umem_page_count(struct ib_umem *umem)
|
|
|
|
{
|
2019-08-15 15:38:30 +07:00
|
|
|
int i, n = 0;
|
2014-01-28 18:40:15 +07:00
|
|
|
struct scatterlist *sg;
|
2007-03-05 07:15:11 +07:00
|
|
|
|
2014-01-28 18:40:15 +07:00
|
|
|
for_each_sg(umem->sg_head.sgl, sg, umem->nmap, i)
|
2019-05-20 13:05:25 +07:00
|
|
|
n += sg_dma_len(sg) >> PAGE_SHIFT;
|
2005-07-08 07:57:14 +07:00
|
|
|
|
2007-03-05 07:15:11 +07:00
|
|
|
return n;
|
2005-07-08 07:57:14 +07:00
|
|
|
}
|
2007-03-05 07:15:11 +07:00
|
|
|
EXPORT_SYMBOL(ib_umem_page_count);
|
2014-12-11 22:04:13 +07: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 22:22:47 +07:00
|
|
|
ret = sg_pcopy_to_buffer(umem->sg_head.sgl, umem->sg_nents, dst, length,
|
|
|
|
offset + ib_umem_offset(umem));
|
2014-12-11 22:04:13 +07:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret != length)
|
|
|
|
return -EINVAL;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ib_umem_copy_from);
|