xref: /linux/drivers/infiniband/core/umem.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2020 Intel Corporation. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35 
36 #include <linux/mm.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/sched/signal.h>
39 #include <linux/sched/mm.h>
40 #include <linux/export.h>
41 #include <linux/slab.h>
42 #include <linux/pagemap.h>
43 #include <linux/count_zeros.h>
44 #include <rdma/ib_umem_odp.h>
45 
46 #include "uverbs.h"
47 
48 #define RESCHED_LOOP_CNT_THRESHOLD 0x1000
49 
50 static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
51 {
52 	bool make_dirty = umem->writable && dirty;
53 	struct scatterlist *sg;
54 	unsigned int i;
55 
56 	if (dirty)
57 		ib_dma_unmap_sgtable_attrs(dev, &umem->sgt_append.sgt,
58 					   DMA_BIDIRECTIONAL, umem->dma_attrs);
59 
60 	for_each_sgtable_sg(&umem->sgt_append.sgt, sg, i) {
61 		unpin_user_page_range_dirty_lock(sg_page(sg),
62 			DIV_ROUND_UP(sg->length, PAGE_SIZE), make_dirty);
63 
64 		if (i && !(i % RESCHED_LOOP_CNT_THRESHOLD))
65 			cond_resched();
66 	}
67 
68 	sg_free_append_table(&umem->sgt_append);
69 }
70 
71 /**
72  * ib_umem_find_best_pgsz - Find best HW page size to use for this MR
73  *
74  * @umem: umem struct
75  * @pgsz_bitmap: bitmap of HW supported page sizes
76  * @virt: IOVA
77  *
78  * This helper is intended for HW that support multiple page
79  * sizes but can do only a single page size in an MR.
80  *
81  * Returns 0 if the umem requires page sizes not supported by
82  * the driver to be mapped. Drivers always supporting PAGE_SIZE
83  * or smaller will never see a 0 result.
84  */
85 unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
86 				     unsigned long pgsz_bitmap,
87 				     u64 virt)
88 {
89 	unsigned long curr_len = 0;
90 	dma_addr_t curr_base = ~0;
91 	unsigned long pgoff;
92 	struct scatterlist *sg;
93 	unsigned long mask = 0;
94 	unsigned int bits;
95 	dma_addr_t end;
96 	u64 last_va;
97 	u64 va;
98 	int i;
99 
100 	umem->iova = va = virt;
101 
102 	if (umem->is_odp) {
103 		unsigned int page_size = BIT(to_ib_umem_odp(umem)->page_shift);
104 
105 		/* ODP must always be self consistent. */
106 		if (!(pgsz_bitmap & page_size))
107 			return 0;
108 		return page_size;
109 	}
110 
111 	/* The best result is the smallest page size that results in the minimum
112 	 * number of required pages. Compute the largest page size that could
113 	 * work based on VA address bits that don't change.
114 	 */
115 	if (check_add_overflow(umem->length - 1, virt, &last_va))
116 		return 0;
117 	bits = bits_per(virt ^ last_va);
118 	if (bits < BITS_PER_LONG)
119 		mask = pgsz_bitmap & GENMASK(BITS_PER_LONG - 1, bits);
120 
121 	/* offset into first SGL */
122 	pgoff = umem->address & ~PAGE_MASK;
123 
124 	for_each_sgtable_dma_sg(&umem->sgt_append.sgt, sg, i) {
125 		/* If the current entry is physically contiguous with the previous
126 		 * one, no need to take its start addresses into consideration.
127 		 */
128 		if (check_add_overflow(curr_base, curr_len, &end) ||
129 		    end != sg_dma_address(sg)) {
130 
131 			curr_base = sg_dma_address(sg);
132 			curr_len = 0;
133 
134 			/* Reduce max page size if VA/PA bits differ */
135 			mask |= (curr_base + pgoff) ^ va;
136 
137 			/* The alignment of any VA matching a discontinuity point
138 			* in the physical memory sets the maximum possible page
139 			* size as this must be a starting point of a new page that
140 			* needs to be aligned.
141 			*/
142 			if (i != 0)
143 				mask |= va;
144 		}
145 
146 		curr_len += sg_dma_len(sg);
147 		va += sg_dma_len(sg) - pgoff;
148 
149 		pgoff = 0;
150 	}
151 
152 	/* The mask accumulates 1's in each position where the VA and physical
153 	 * address differ, thus the length of trailing 0 is the largest page
154 	 * size that can pass the VA through to the physical.
155 	 */
156 	if (mask)
157 		pgsz_bitmap &= GENMASK(count_trailing_zeros(mask), 0);
158 	return pgsz_bitmap ? rounddown_pow_of_two(pgsz_bitmap) : 0;
159 }
160 EXPORT_SYMBOL(ib_umem_find_best_pgsz);
161 
162 static struct ib_umem *__ib_umem_get_va(struct ib_device *device,
163 					unsigned long addr, size_t size,
164 					int access)
165 {
166 	struct ib_umem *umem;
167 	struct page **page_list;
168 	unsigned long lock_limit;
169 	unsigned long new_pinned;
170 	unsigned long cur_base;
171 	struct mm_struct *mm;
172 	unsigned long npages;
173 	int pinned, ret;
174 	unsigned int gup_flags = FOLL_LONGTERM;
175 
176 	if (device->cc_dma_bounce)
177 		return ERR_PTR(-EOPNOTSUPP);
178 
179 	/*
180 	 * If the combination of the addr and size requested for this memory
181 	 * region causes an integer overflow, return error.
182 	 */
183 	if (((addr + size) < addr) ||
184 	    PAGE_ALIGN(addr + size) < (addr + size))
185 		return ERR_PTR(-EINVAL);
186 
187 	if (!can_do_mlock())
188 		return ERR_PTR(-EPERM);
189 
190 	if (access & IB_ACCESS_ON_DEMAND)
191 		return ERR_PTR(-EOPNOTSUPP);
192 
193 	umem = kzalloc_obj(*umem);
194 	if (!umem)
195 		return ERR_PTR(-ENOMEM);
196 	umem->ibdev      = device;
197 	umem->length     = size;
198 	umem->address    = addr;
199 	/*
200 	 * Drivers should call ib_umem_find_best_pgsz() to set the iova
201 	 * correctly.
202 	 */
203 	umem->iova = addr;
204 	umem->writable   = ib_access_writable(access);
205 	umem->owning_mm = mm = current->mm;
206 	umem->dma_attrs = DMA_ATTR_REQUIRE_COHERENT;
207 	if (access & IB_ACCESS_RELAXED_ORDERING)
208 		umem->dma_attrs |= DMA_ATTR_WEAK_ORDERING;
209 
210 	mmgrab(mm);
211 
212 	/* TODO: switch to "fast and as large as possible" allocation helper */
213 	page_list = kmalloc(PAGE_SIZE, GFP_KERNEL);
214 	if (!page_list) {
215 		ret = -ENOMEM;
216 		goto umem_kfree;
217 	}
218 
219 	npages = ib_umem_num_pages(umem);
220 	if (npages == 0 || npages > UINT_MAX) {
221 		ret = -EINVAL;
222 		goto out;
223 	}
224 
225 	lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
226 
227 	new_pinned = atomic64_add_return(npages, &mm->pinned_vm);
228 	if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
229 		atomic64_sub(npages, &mm->pinned_vm);
230 		ret = -ENOMEM;
231 		goto out;
232 	}
233 
234 	cur_base = addr & PAGE_MASK;
235 
236 	if (umem->writable)
237 		gup_flags |= FOLL_WRITE;
238 
239 	while (npages) {
240 		cond_resched();
241 		pinned = pin_user_pages_fast(cur_base,
242 					  min_t(unsigned long, npages,
243 						PAGE_SIZE /
244 						sizeof(struct page *)),
245 					  gup_flags, page_list);
246 		if (pinned < 0) {
247 			ret = pinned;
248 			goto umem_release;
249 		}
250 
251 		cur_base += pinned * PAGE_SIZE;
252 		npages -= pinned;
253 		ret = sg_alloc_append_table_from_pages(
254 			&umem->sgt_append, page_list, pinned, 0,
255 			pinned << PAGE_SHIFT, ib_dma_max_seg_size(device),
256 			npages, GFP_KERNEL);
257 		if (ret) {
258 			unpin_user_pages_dirty_lock(page_list, pinned, 0);
259 			goto umem_release;
260 		}
261 	}
262 
263 	ret = ib_dma_map_sgtable_attrs(device, &umem->sgt_append.sgt,
264 				       DMA_BIDIRECTIONAL, umem->dma_attrs);
265 	if (ret)
266 		goto umem_release;
267 	goto out;
268 
269 umem_release:
270 	__ib_umem_release(device, umem, 0);
271 	atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm);
272 out:
273 	kfree(page_list);
274 umem_kfree:
275 	if (ret) {
276 		mmdrop(umem->owning_mm);
277 		kfree(umem);
278 	}
279 	return ret ? ERR_PTR(ret) : umem;
280 }
281 
282 /**
283  * ib_umem_get_desc - Pin a umem from a buffer descriptor.
284  * @device: IB device.
285  * @desc:   buffer descriptor (VA or DMABUF).
286  * @access: IB access flags.
287  *
288  * Return: caller-owned umem on success, ERR_PTR(...) on error.
289  */
290 struct ib_umem *ib_umem_get_desc(struct ib_device *device,
291 				 const struct ib_uverbs_buffer_desc *desc,
292 				 int access)
293 {
294 	struct ib_umem_dmabuf *umem_dmabuf;
295 
296 	if (desc->flags & ~IB_UVERBS_BUFFER_DESC_FLAGS_KNOWN_MASK)
297 		return ERR_PTR(-EINVAL);
298 
299 	if (overflows_type(desc->addr, unsigned long) ||
300 	    overflows_type(desc->length, size_t))
301 		return ERR_PTR(-EOVERFLOW);
302 
303 	switch (desc->type) {
304 	case IB_UVERBS_BUFFER_TYPE_DMABUF:
305 		umem_dmabuf = ib_umem_dmabuf_get_pinned(device, desc->addr,
306 							desc->length, desc->fd,
307 							access);
308 		if (IS_ERR(umem_dmabuf))
309 			return ERR_CAST(umem_dmabuf);
310 		return &umem_dmabuf->umem;
311 	case IB_UVERBS_BUFFER_TYPE_VA:
312 		return __ib_umem_get_va(device, desc->addr, desc->length,
313 					access);
314 	default:
315 		return ERR_PTR(-EINVAL);
316 	}
317 }
318 EXPORT_SYMBOL(ib_umem_get_desc);
319 
320 /*
321  * Per-command legacy buffer-desc filler.
322  * Returns 0 on success (desc filled), -ENODATA if no legacy attrs apply,
323  * negative errno on validation failure.
324  */
325 typedef int (*ib_umem_buf_desc_filler_t)(const struct uverbs_attr_bundle *attrs,
326 					 struct ib_uverbs_buffer_desc *desc);
327 
328 /*
329  * ib_umem_resolve_desc - Resolve a buffer descriptor from a per-command UMEM
330  *                        attribute and/or a legacy attr filler.
331  *
332  * Return:
333  *    0       @desc filled.
334  *   -ENOENT  no source produced a buffer.
335  *   -EINVAL  both the UMEM attribute and the legacy filler produced a buffer.
336  *   -errno   propagated from attr read / filler validation.
337  */
338 static int ib_umem_resolve_desc(const struct uverbs_attr_bundle *attrs,
339 				u16 attr_id,
340 				ib_umem_buf_desc_filler_t legacy_filler,
341 				struct ib_uverbs_buffer_desc *desc)
342 {
343 	bool have_desc = false;
344 	int ret;
345 
346 	if (!attrs)
347 		return -ENOENT;
348 
349 	ret = uverbs_get_buffer_desc(attrs, attr_id, desc);
350 	if (!ret)
351 		have_desc = true;
352 	else if (ret != -ENOENT)
353 		return ret;
354 
355 	if (legacy_filler) {
356 		struct ib_uverbs_buffer_desc legacy_desc = {};
357 
358 		ret = legacy_filler(attrs, &legacy_desc);
359 		if (!ret) {
360 			if (have_desc)
361 				return -EINVAL;
362 			*desc = legacy_desc;
363 			have_desc = true;
364 		} else if (ret != -ENODATA) {
365 			return ret;
366 		}
367 	}
368 
369 	return have_desc ? 0 : -ENOENT;
370 }
371 
372 /*
373  * ib_umem_get_desc_check - Pin a umem from @desc and verify it meets
374  *                          @min_size.
375  */
376 static struct ib_umem *
377 ib_umem_get_desc_check(struct ib_device *device,
378 		       const struct ib_uverbs_buffer_desc *desc,
379 		       size_t min_size, int access)
380 {
381 	struct ib_umem *umem;
382 
383 	umem = ib_umem_get_desc(device, desc, access);
384 	if (IS_ERR(umem))
385 		return umem;
386 	if (umem->length < min_size) {
387 		ib_umem_release(umem);
388 		return ERR_PTR(-EINVAL);
389 	}
390 	return umem;
391 }
392 
393 /*
394  * ib_umem_get_from_attrs - Pin a umem from a per-command UMEM attribute
395  *                          and/or a legacy attr filler.
396  *
397  * Return: caller-owned umem on success; NULL when no source supplied a
398  * buffer; ERR_PTR(...) on error.
399  */
400 static struct ib_umem *
401 ib_umem_get_from_attrs(struct ib_device *device,
402 		       const struct uverbs_attr_bundle *attrs,
403 		       u16 attr_id, ib_umem_buf_desc_filler_t legacy_filler,
404 		       size_t size, int access)
405 {
406 	struct ib_uverbs_buffer_desc desc = {};
407 	int ret;
408 
409 	ret = ib_umem_resolve_desc(attrs, attr_id, legacy_filler, &desc);
410 	if (ret == -ENOENT)
411 		return NULL;
412 	if (ret)
413 		return ERR_PTR(ret);
414 	return ib_umem_get_desc_check(device, &desc, size, access);
415 }
416 
417 /*
418  * ib_umem_get_from_attrs_or_va - Pin a umem from a per-command UMEM
419  *                                attribute and/or a legacy attr filler,
420  *                                falling back to a UHW VA when no source
421  *                                matched.
422  *
423  * @size is always consumed: it is the length to pin on the VA fallback
424  * path AND the post-pin minimum-length check on the attr / legacy paths.
425  * Callers must always pass a meaningful, validated value.
426  *
427  * Return: caller-owned umem on success, ERR_PTR(...) on error.
428  */
429 static struct ib_umem *
430 ib_umem_get_from_attrs_or_va(struct ib_device *device,
431 			     const struct uverbs_attr_bundle *attrs,
432 			     u16 attr_id,
433 			     ib_umem_buf_desc_filler_t legacy_filler,
434 			     u64 addr, size_t size, int access)
435 {
436 	struct ib_uverbs_buffer_desc desc = {};
437 	int ret;
438 
439 	ret = ib_umem_resolve_desc(attrs, attr_id, legacy_filler, &desc);
440 	if (ret == -ENOENT)
441 		desc = (struct ib_uverbs_buffer_desc){
442 			.type	= IB_UVERBS_BUFFER_TYPE_VA,
443 			.addr	= addr,
444 			.length	= size,
445 		};
446 	else if (ret)
447 		return ERR_PTR(ret);
448 	return ib_umem_get_desc_check(device, &desc, size, access);
449 }
450 
451 /**
452  * ib_umem_get_attr - Pin a umem from a per-command UMEM attribute.
453  * @device:  IB device.
454  * @attrs:   uverbs attribute bundle (may be NULL).
455  * @attr_id: per-command UMEM attribute id.
456  * @size:    minimum required umem length.
457  * @access:  IB access flags.
458  *
459  * Return: caller-owned umem on success; NULL when no source supplied
460  * a buffer; ERR_PTR(...) on error.
461  */
462 struct ib_umem *ib_umem_get_attr(struct ib_device *device,
463 				 const struct uverbs_attr_bundle *attrs,
464 				 u16 attr_id, size_t size, int access)
465 {
466 	return ib_umem_get_from_attrs(device, attrs, attr_id, NULL, size,
467 				      access);
468 }
469 EXPORT_SYMBOL(ib_umem_get_attr);
470 
471 /**
472  * ib_umem_get_attr_or_va - Pin a umem from a per-command UMEM attribute,
473  *                          falling back to a UHW VA.
474  * @device:  IB device.
475  * @attrs:   uverbs attribute bundle (may be NULL).
476  * @attr_id: per-command UMEM attribute id.
477  * @addr:    UHW user VA used when no per-command attribute matched.
478  * @size:    on the attr / legacy paths, the minimum required umem length
479  *           validated post-pin; on the VA fallback path, the length to pin.
480  * @access:  IB access flags.
481  *
482  * Like ib_umem_get_attr(), but pins @addr/@size when no per-command
483  * UMEM attribute is supplied.
484  *
485  * IMPORTANT: @size is always consumed. On the attr / legacy paths it is
486  * used as the post-pin minimum-length check; on the VA fallback path it
487  * is the length to pin. Callers MUST pass a meaningful, validated value
488  * even when they expect an attribute-supplied buffer to be used.
489  *
490  * Every in-tree caller passes the same value for the two roles of @size
491  * because no driver today distinguishes a user-passed buffer length from
492  * a driver-computed minimum. Drivers that currently accept a user-supplied
493  * length without cross-checking it against a driver minimum (vmw_pvrdma
494  * CQ/QP/SRQ, qedr CQ/QP/SRQ, mana WQ/QP, ionic CQ/QP), once tightened to
495  * compute and check a real minimum, will want to introduce a separate
496  * helper that passes these as distinct values.
497  *
498  * Return: caller-owned umem on success, ERR_PTR(...) on error.
499  */
500 struct ib_umem *ib_umem_get_attr_or_va(struct ib_device *device,
501 				       const struct uverbs_attr_bundle *attrs,
502 				       u16 attr_id, u64 addr, size_t size,
503 				       int access)
504 {
505 	return ib_umem_get_from_attrs_or_va(device, attrs, attr_id, NULL, addr,
506 					    size, access);
507 }
508 EXPORT_SYMBOL(ib_umem_get_attr_or_va);
509 
510 static int uverbs_create_cq_get_buffer_desc(const struct uverbs_attr_bundle *attrs,
511 					    struct ib_uverbs_buffer_desc *desc)
512 {
513 	struct ib_device *ib_dev = attrs->context->device;
514 	int ret;
515 
516 	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_VA)) {
517 		ret = uverbs_copy_from(&desc->addr, attrs,
518 				       UVERBS_ATTR_CREATE_CQ_BUFFER_VA);
519 		if (ret)
520 			return ret;
521 		ret = uverbs_copy_from(&desc->length, attrs,
522 				       UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH);
523 		if (ret)
524 			return ret;
525 		if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_FD) ||
526 		    uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET) ||
527 		    !ib_dev->ops.create_user_cq)
528 			return -EINVAL;
529 		desc->type = IB_UVERBS_BUFFER_TYPE_VA;
530 		return 0;
531 	}
532 
533 	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_FD)) {
534 		ret = uverbs_get_raw_fd(&desc->fd, attrs,
535 					UVERBS_ATTR_CREATE_CQ_BUFFER_FD);
536 		if (ret)
537 			return ret;
538 
539 		ret = uverbs_copy_from(&desc->addr, attrs,
540 				       UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET);
541 		if (ret)
542 			return ret;
543 		ret = uverbs_copy_from(&desc->length, attrs,
544 				       UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH);
545 		if (ret)
546 			return ret;
547 		if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_VA) ||
548 		    !ib_dev->ops.create_user_cq)
549 			return -EINVAL;
550 		desc->type = IB_UVERBS_BUFFER_TYPE_DMABUF;
551 		return 0;
552 	}
553 
554 	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET) ||
555 	    uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH))
556 		return -EINVAL;
557 	return -ENODATA;
558 }
559 
560 /**
561  * ib_umem_get_cq_buf - Pin a CQ buffer umem from per-command attributes.
562  * @device:  IB device.
563  * @attrs:   uverbs attribute bundle (may be NULL).
564  * @size:    minimum required CQ buffer length.
565  * @access:  IB access flags.
566  *
567  * Resolves the CQ buffer from the new UMEM attribute or the legacy
568  * CQ buffer attributes. There is no UHW VA fallback, so the caller
569  * must arrange its own backing (typically an in-kernel allocation)
570  * when no source is available.
571  *
572  * Return: caller-owned umem on success; NULL when no source supplied
573  * a buffer; ERR_PTR(...) on error.
574  */
575 struct ib_umem *ib_umem_get_cq_buf(struct ib_device *device,
576 				   const struct uverbs_attr_bundle *attrs,
577 				   size_t size, int access)
578 {
579 	return ib_umem_get_from_attrs(device, attrs,
580 				      UVERBS_ATTR_CREATE_CQ_BUF_UMEM,
581 				      uverbs_create_cq_get_buffer_desc,
582 				      size, access);
583 }
584 EXPORT_SYMBOL(ib_umem_get_cq_buf);
585 
586 /**
587  * ib_umem_get_cq_buf_or_va - Pin a CQ buffer umem with UHW VA fallback.
588  * @device:  IB device.
589  * @attrs:   uverbs attribute bundle (may be NULL).
590  * @addr:    UHW user VA used when no per-command attribute matched.
591  * @size:    on the attr / legacy paths, the minimum required umem length
592  *           validated post-pin; on the VA fallback path, the length to pin.
593  * @access:  IB access flags.
594  *
595  * Like ib_umem_get_cq_buf(), but pins @addr/@size when neither the
596  * UMEM attribute nor the legacy CQ buffer attributes are supplied.
597  *
598  * See ib_umem_get_attr_or_va() for the note on @size's dual role and
599  * the migration path for drivers that would distinguish a user-supplied
600  * length from a driver-computed minimum.
601  *
602  * Return: caller-owned umem on success, ERR_PTR(...) on error.
603  */
604 struct ib_umem *ib_umem_get_cq_buf_or_va(struct ib_device *device,
605 					 const struct uverbs_attr_bundle *attrs,
606 					 u64 addr, size_t size, int access)
607 {
608 	return ib_umem_get_from_attrs_or_va(device, attrs,
609 					    UVERBS_ATTR_CREATE_CQ_BUF_UMEM,
610 					    uverbs_create_cq_get_buffer_desc,
611 					    addr, size, access);
612 }
613 EXPORT_SYMBOL(ib_umem_get_cq_buf_or_va);
614 
615 /**
616  * ib_umem_release - release pinned memory
617  * @umem: umem struct to release
618  */
619 void ib_umem_release(struct ib_umem *umem)
620 {
621 	if (IS_ERR_OR_NULL(umem))
622 		return;
623 	if (umem->is_dmabuf)
624 		return ib_umem_dmabuf_release(to_ib_umem_dmabuf(umem));
625 	if (umem->is_odp)
626 		return ib_umem_odp_release(to_ib_umem_odp(umem));
627 
628 	__ib_umem_release(umem->ibdev, umem, 1);
629 
630 	atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm);
631 	mmdrop(umem->owning_mm);
632 	kfree(umem);
633 }
634 EXPORT_SYMBOL(ib_umem_release);
635 
636 /*
637  * Copy from the given ib_umem's pages to the given buffer.
638  *
639  * umem - the umem to copy from
640  * offset - offset to start copying from
641  * dst - destination buffer
642  * length - buffer length
643  *
644  * Returns 0 on success, or an error code.
645  */
646 int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
647 		      size_t length)
648 {
649 	size_t end = offset + length;
650 	int ret;
651 
652 	if (offset > umem->length || length > umem->length - offset) {
653 		pr_err("%s not in range. offset: %zd umem length: %zd end: %zd\n",
654 		       __func__, offset, umem->length, end);
655 		return -EINVAL;
656 	}
657 
658 	ret = sg_pcopy_to_buffer(umem->sgt_append.sgt.sgl,
659 				 umem->sgt_append.sgt.orig_nents, dst, length,
660 				 offset + ib_umem_offset(umem));
661 
662 	if (ret < 0)
663 		return ret;
664 	else if (ret != length)
665 		return -EINVAL;
666 	else
667 		return 0;
668 }
669 EXPORT_SYMBOL(ib_umem_copy_from);
670 
671 /*
672  * Called during rereg mr if the driver is able to re-use a umem for
673  * IB_MR_REREG_ACCESS.
674  */
675 int ib_umem_check_rereg(struct ib_umem *umem, int flags, int new_access_flags)
676 {
677 	if (!umem)
678 		return 0;
679 
680 	if (umem->is_dmabuf)
681 		return -EOPNOTSUPP;
682 
683 	if ((flags & IB_MR_REREG_ACCESS) && !(flags & IB_MR_REREG_TRANS))
684 		if (ib_access_writable(new_access_flags) && !umem->writable)
685 			return -EACCES;
686 	return 0;
687 }
688 EXPORT_SYMBOL(ib_umem_check_rereg);
689