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 umem->dma_dir, 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 inline enum dma_data_direction ib_access_dma_dir(int access) 163 { 164 return ib_access_writable(access) ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE; 165 } 166 167 static struct ib_umem *__ib_umem_get_va(struct ib_device *device, 168 unsigned long addr, size_t size, 169 int access, 170 enum dma_data_direction dir) 171 { 172 struct ib_umem *umem; 173 struct page **page_list; 174 unsigned long lock_limit; 175 unsigned long new_pinned; 176 unsigned long cur_base; 177 struct mm_struct *mm; 178 unsigned long npages; 179 int pinned, ret; 180 unsigned int gup_flags = FOLL_LONGTERM; 181 182 if (device->cc_dma_bounce) 183 return ERR_PTR(-EOPNOTSUPP); 184 185 /* 186 * If the combination of the addr and size requested for this memory 187 * region causes an integer overflow, return error. 188 */ 189 if (((addr + size) < addr) || 190 PAGE_ALIGN(addr + size) < (addr + size)) 191 return ERR_PTR(-EINVAL); 192 193 if (!can_do_mlock()) 194 return ERR_PTR(-EPERM); 195 196 if (access & IB_ACCESS_ON_DEMAND) 197 return ERR_PTR(-EOPNOTSUPP); 198 199 umem = kzalloc_obj(*umem); 200 if (!umem) 201 return ERR_PTR(-ENOMEM); 202 umem->ibdev = device; 203 umem->length = size; 204 umem->address = addr; 205 /* 206 * Drivers should call ib_umem_find_best_pgsz() to set the iova 207 * correctly. 208 */ 209 umem->iova = addr; 210 umem->writable = ib_access_writable(access); 211 umem->dma_dir = dir; 212 umem->owning_mm = mm = current->mm; 213 umem->dma_attrs = DMA_ATTR_REQUIRE_COHERENT; 214 if (access & IB_ACCESS_RELAXED_ORDERING) 215 umem->dma_attrs |= DMA_ATTR_WEAK_ORDERING; 216 217 mmgrab(mm); 218 219 /* TODO: switch to "fast and as large as possible" allocation helper */ 220 page_list = kmalloc(PAGE_SIZE, GFP_KERNEL); 221 if (!page_list) { 222 ret = -ENOMEM; 223 goto umem_kfree; 224 } 225 226 npages = ib_umem_num_pages(umem); 227 if (npages == 0 || npages > UINT_MAX) { 228 ret = -EINVAL; 229 goto out; 230 } 231 232 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 233 234 new_pinned = atomic64_add_return(npages, &mm->pinned_vm); 235 if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) { 236 atomic64_sub(npages, &mm->pinned_vm); 237 ret = -ENOMEM; 238 goto out; 239 } 240 241 cur_base = addr & PAGE_MASK; 242 243 if (umem->writable) 244 gup_flags |= FOLL_WRITE; 245 246 while (npages) { 247 cond_resched(); 248 pinned = pin_user_pages_fast(cur_base, 249 min_t(unsigned long, npages, 250 PAGE_SIZE / 251 sizeof(struct page *)), 252 gup_flags, page_list); 253 if (pinned < 0) { 254 ret = pinned; 255 goto umem_release; 256 } 257 258 cur_base += pinned * PAGE_SIZE; 259 npages -= pinned; 260 ret = sg_alloc_append_table_from_pages( 261 &umem->sgt_append, page_list, pinned, 0, 262 pinned << PAGE_SHIFT, ib_dma_max_seg_size(device), 263 npages, GFP_KERNEL); 264 if (ret) { 265 unpin_user_pages_dirty_lock(page_list, pinned, 0); 266 goto umem_release; 267 } 268 } 269 270 ret = ib_dma_map_sgtable_attrs(device, &umem->sgt_append.sgt, 271 dir, umem->dma_attrs); 272 if (ret) 273 goto umem_release; 274 goto out; 275 276 umem_release: 277 __ib_umem_release(device, umem, 0); 278 atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm); 279 out: 280 kfree(page_list); 281 umem_kfree: 282 if (ret) { 283 mmdrop(umem->owning_mm); 284 kfree(umem); 285 } 286 return ret ? ERR_PTR(ret) : umem; 287 } 288 289 /* 290 * __ib_umem_get_desc_dir - core implementation for ib_umem_get_desc(). 291 * 292 * @dir applies to VA buffers only; dmabuf direction is managed by the 293 * dmabuf subsystem and this argument is ignored for that type. 294 */ 295 static struct ib_umem * 296 __ib_umem_get_desc_dir(struct ib_device *device, 297 const struct ib_uverbs_buffer_desc *desc, 298 int access, enum dma_data_direction dir) 299 { 300 struct ib_umem_dmabuf *umem_dmabuf; 301 302 if (desc->flags & ~IB_UVERBS_BUFFER_DESC_FLAGS_KNOWN_MASK) 303 return ERR_PTR(-EINVAL); 304 305 if (overflows_type(desc->addr, unsigned long) || 306 overflows_type(desc->length, size_t)) 307 return ERR_PTR(-EOVERFLOW); 308 309 switch (desc->type) { 310 case IB_UVERBS_BUFFER_TYPE_DMABUF: 311 umem_dmabuf = ib_umem_dmabuf_get_pinned(device, desc->addr, 312 desc->length, desc->fd, 313 access); 314 if (IS_ERR(umem_dmabuf)) 315 return ERR_CAST(umem_dmabuf); 316 return &umem_dmabuf->umem; 317 case IB_UVERBS_BUFFER_TYPE_VA: 318 return __ib_umem_get_va(device, desc->addr, desc->length, 319 access, dir); 320 default: 321 return ERR_PTR(-EINVAL); 322 } 323 } 324 325 /** 326 * ib_umem_get_desc - Pin a umem from a buffer descriptor. 327 * @device: IB device. 328 * @desc: buffer descriptor (VA or DMABUF). 329 * @access: IB access flags. 330 * 331 * Return: caller-owned umem on success, ERR_PTR(...) on error. 332 */ 333 struct ib_umem *ib_umem_get_desc(struct ib_device *device, 334 const struct ib_uverbs_buffer_desc *desc, 335 int access) 336 { 337 return __ib_umem_get_desc_dir(device, desc, access, 338 ib_access_dma_dir(access)); 339 } 340 EXPORT_SYMBOL(ib_umem_get_desc); 341 342 /* 343 * Per-command legacy buffer-desc filler. 344 * Returns 0 on success (desc filled), -ENODATA if no legacy attrs apply, 345 * negative errno on validation failure. 346 */ 347 typedef int (*ib_umem_buf_desc_filler_t)(const struct uverbs_attr_bundle *attrs, 348 struct ib_uverbs_buffer_desc *desc); 349 350 /* 351 * ib_umem_resolve_desc - Resolve a buffer descriptor from a per-command UMEM 352 * attribute and/or a legacy attr filler. 353 * 354 * Return: 355 * 0 @desc filled. 356 * -ENOENT no source produced a buffer. 357 * -EINVAL both the UMEM attribute and the legacy filler produced a buffer. 358 * -errno propagated from attr read / filler validation. 359 */ 360 static int ib_umem_resolve_desc(const struct uverbs_attr_bundle *attrs, 361 u16 attr_id, 362 ib_umem_buf_desc_filler_t legacy_filler, 363 struct ib_uverbs_buffer_desc *desc) 364 { 365 bool have_desc = false; 366 int ret; 367 368 if (!attrs) 369 return -ENOENT; 370 371 ret = uverbs_get_buffer_desc(attrs, attr_id, desc); 372 if (!ret) 373 have_desc = true; 374 else if (ret != -ENOENT) 375 return ret; 376 377 if (legacy_filler) { 378 struct ib_uverbs_buffer_desc legacy_desc = {}; 379 380 ret = legacy_filler(attrs, &legacy_desc); 381 if (!ret) { 382 if (have_desc) 383 return -EINVAL; 384 *desc = legacy_desc; 385 have_desc = true; 386 } else if (ret != -ENODATA) { 387 return ret; 388 } 389 } 390 391 return have_desc ? 0 : -ENOENT; 392 } 393 394 /* 395 * ib_umem_get_desc_check - Pin a umem from @desc and verify it meets 396 * @min_size. 397 */ 398 static struct ib_umem * 399 ib_umem_get_desc_check(struct ib_device *device, 400 const struct ib_uverbs_buffer_desc *desc, 401 size_t min_size, int access, 402 enum dma_data_direction dir) 403 { 404 struct ib_umem *umem; 405 406 umem = __ib_umem_get_desc_dir(device, desc, access, dir); 407 if (IS_ERR(umem)) 408 return umem; 409 if (umem->length < min_size) { 410 ib_umem_release(umem); 411 return ERR_PTR(-EINVAL); 412 } 413 return umem; 414 } 415 416 /* 417 * ib_umem_get_from_attrs - Pin a umem from a per-command UMEM attribute 418 * and/or a legacy attr filler. 419 * 420 * Return: caller-owned umem on success; NULL when no source supplied a 421 * buffer; ERR_PTR(...) on error. 422 */ 423 static struct ib_umem * 424 ib_umem_get_from_attrs(struct ib_device *device, 425 const struct uverbs_attr_bundle *attrs, 426 u16 attr_id, ib_umem_buf_desc_filler_t legacy_filler, 427 size_t size, int access, enum dma_data_direction dir) 428 { 429 struct ib_uverbs_buffer_desc desc = {}; 430 int ret; 431 432 ret = ib_umem_resolve_desc(attrs, attr_id, legacy_filler, &desc); 433 if (ret == -ENOENT) 434 return NULL; 435 if (ret) 436 return ERR_PTR(ret); 437 return ib_umem_get_desc_check(device, &desc, size, access, dir); 438 } 439 440 /* 441 * ib_umem_get_from_attrs_or_va - Pin a umem from a per-command UMEM 442 * attribute and/or a legacy attr filler, 443 * falling back to a UHW VA when no source 444 * matched. 445 * 446 * @size is always consumed: it is the length to pin on the VA fallback 447 * path AND the post-pin minimum-length check on the attr / legacy paths. 448 * Callers must always pass a meaningful, validated value. 449 * 450 * Return: caller-owned umem on success, ERR_PTR(...) on error. 451 */ 452 static struct ib_umem * 453 ib_umem_get_from_attrs_or_va(struct ib_device *device, 454 const struct uverbs_attr_bundle *attrs, 455 u16 attr_id, 456 ib_umem_buf_desc_filler_t legacy_filler, 457 u64 addr, size_t size, int access, 458 enum dma_data_direction dir) 459 { 460 struct ib_uverbs_buffer_desc desc = {}; 461 int ret; 462 463 ret = ib_umem_resolve_desc(attrs, attr_id, legacy_filler, &desc); 464 if (ret == -ENOENT) 465 desc = (struct ib_uverbs_buffer_desc){ 466 .type = IB_UVERBS_BUFFER_TYPE_VA, 467 .addr = addr, 468 .length = size, 469 }; 470 else if (ret) 471 return ERR_PTR(ret); 472 return ib_umem_get_desc_check(device, &desc, size, access, dir); 473 } 474 475 /** 476 * ib_umem_get_attr - Pin a umem from a per-command UMEM attribute. 477 * @device: IB device. 478 * @attrs: uverbs attribute bundle (may be NULL). 479 * @attr_id: per-command UMEM attribute id. 480 * @size: minimum required umem length. 481 * @access: IB access flags. 482 * 483 * Return: caller-owned umem on success; NULL when no source supplied 484 * a buffer; ERR_PTR(...) on error. 485 */ 486 struct ib_umem *ib_umem_get_attr(struct ib_device *device, 487 const struct uverbs_attr_bundle *attrs, 488 u16 attr_id, size_t size, int access) 489 { 490 return ib_umem_get_from_attrs(device, attrs, attr_id, NULL, size, 491 access, ib_access_dma_dir(access)); 492 } 493 EXPORT_SYMBOL(ib_umem_get_attr); 494 495 /** 496 * ib_umem_get_attr_or_va - Pin a umem from a per-command UMEM attribute, 497 * falling back to a UHW VA. 498 * @device: IB device. 499 * @attrs: uverbs attribute bundle (may be NULL). 500 * @attr_id: per-command UMEM attribute id. 501 * @addr: UHW user VA used when no per-command attribute matched. 502 * @size: on the attr / legacy paths, the minimum required umem length 503 * validated post-pin; on the VA fallback path, the length to pin. 504 * @access: IB access flags. 505 * 506 * Like ib_umem_get_attr(), but pins @addr/@size when no per-command 507 * UMEM attribute is supplied. 508 * 509 * IMPORTANT: @size is always consumed. On the attr / legacy paths it is 510 * used as the post-pin minimum-length check; on the VA fallback path it 511 * is the length to pin. Callers MUST pass a meaningful, validated value 512 * even when they expect an attribute-supplied buffer to be used. 513 * 514 * Every in-tree caller passes the same value for the two roles of @size 515 * because no driver today distinguishes a user-passed buffer length from 516 * a driver-computed minimum. Drivers that currently accept a user-supplied 517 * length without cross-checking it against a driver minimum (vmw_pvrdma 518 * CQ/QP/SRQ, qedr CQ/QP/SRQ, mana WQ/QP, ionic CQ/QP), once tightened to 519 * compute and check a real minimum, will want to introduce a separate 520 * helper that passes these as distinct values. 521 * 522 * Return: caller-owned umem on success, ERR_PTR(...) on error. 523 */ 524 struct ib_umem *ib_umem_get_attr_or_va(struct ib_device *device, 525 const struct uverbs_attr_bundle *attrs, 526 u16 attr_id, u64 addr, size_t size, 527 int access) 528 { 529 return ib_umem_get_from_attrs_or_va(device, attrs, attr_id, NULL, addr, 530 size, access, 531 ib_access_dma_dir(access)); 532 } 533 EXPORT_SYMBOL(ib_umem_get_attr_or_va); 534 535 static int uverbs_create_cq_get_buffer_desc(const struct uverbs_attr_bundle *attrs, 536 struct ib_uverbs_buffer_desc *desc) 537 { 538 struct ib_device *ib_dev = attrs->context->device; 539 int ret; 540 541 if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_VA)) { 542 ret = uverbs_copy_from(&desc->addr, attrs, 543 UVERBS_ATTR_CREATE_CQ_BUFFER_VA); 544 if (ret) 545 return ret; 546 ret = uverbs_copy_from(&desc->length, attrs, 547 UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH); 548 if (ret) 549 return ret; 550 if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_FD) || 551 uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET) || 552 !ib_dev->ops.create_user_cq) 553 return -EINVAL; 554 desc->type = IB_UVERBS_BUFFER_TYPE_VA; 555 return 0; 556 } 557 558 if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_FD)) { 559 ret = uverbs_get_raw_fd(&desc->fd, attrs, 560 UVERBS_ATTR_CREATE_CQ_BUFFER_FD); 561 if (ret) 562 return ret; 563 564 ret = uverbs_copy_from(&desc->addr, attrs, 565 UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET); 566 if (ret) 567 return ret; 568 ret = uverbs_copy_from(&desc->length, attrs, 569 UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH); 570 if (ret) 571 return ret; 572 if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_VA) || 573 !ib_dev->ops.create_user_cq) 574 return -EINVAL; 575 desc->type = IB_UVERBS_BUFFER_TYPE_DMABUF; 576 return 0; 577 } 578 579 if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_OFFSET) || 580 uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_CQ_BUFFER_LENGTH)) 581 return -EINVAL; 582 return -ENODATA; 583 } 584 585 /** 586 * ib_umem_get_cq_buf - Pin a CQ buffer umem from per-command attributes. 587 * @device: IB device. 588 * @attrs: uverbs attribute bundle (may be NULL). 589 * @size: minimum required CQ buffer length. 590 * @access: IB access flags. 591 * 592 * Resolves the CQ buffer from the new UMEM attribute or the legacy 593 * CQ buffer attributes. There is no UHW VA fallback, so the caller 594 * must arrange its own backing (typically an in-kernel allocation) 595 * when no source is available. 596 * 597 * The buffer is mapped DMA_FROM_DEVICE: the NIC writes CQEs into it 598 * and the CPU only reads. 599 * 600 * Return: caller-owned umem on success; NULL when no source supplied 601 * a buffer; ERR_PTR(...) on error. 602 */ 603 struct ib_umem *ib_umem_get_cq_buf(struct ib_device *device, 604 const struct uverbs_attr_bundle *attrs, 605 size_t size, int access) 606 { 607 return ib_umem_get_from_attrs(device, attrs, 608 UVERBS_ATTR_CREATE_CQ_BUF_UMEM, 609 uverbs_create_cq_get_buffer_desc, 610 size, access, DMA_FROM_DEVICE); 611 } 612 EXPORT_SYMBOL(ib_umem_get_cq_buf); 613 614 /** 615 * ib_umem_get_cq_buf_or_va - Pin a CQ buffer umem with UHW VA fallback. 616 * @device: IB device. 617 * @attrs: uverbs attribute bundle (may be NULL). 618 * @addr: UHW user VA used when no per-command attribute matched. 619 * @size: on the attr / legacy paths, the minimum required umem length 620 * validated post-pin; on the VA fallback path, the length to pin. 621 * @access: IB access flags. 622 * 623 * Like ib_umem_get_cq_buf(), but pins @addr/@size when neither the 624 * UMEM attribute nor the legacy CQ buffer attributes are supplied. 625 * 626 * The buffer is mapped DMA_FROM_DEVICE: the NIC writes CQEs into it 627 * and the CPU only reads. 628 * 629 * See ib_umem_get_attr_or_va() for the note on @size's dual role and 630 * the migration path for drivers that would distinguish a user-supplied 631 * length from a driver-computed minimum. 632 * 633 * Return: caller-owned umem on success, ERR_PTR(...) on error. 634 */ 635 struct ib_umem *ib_umem_get_cq_buf_or_va(struct ib_device *device, 636 const struct uverbs_attr_bundle *attrs, 637 u64 addr, size_t size, int access) 638 { 639 return ib_umem_get_from_attrs_or_va(device, attrs, 640 UVERBS_ATTR_CREATE_CQ_BUF_UMEM, 641 uverbs_create_cq_get_buffer_desc, 642 addr, size, access, DMA_FROM_DEVICE); 643 } 644 EXPORT_SYMBOL(ib_umem_get_cq_buf_or_va); 645 646 /** 647 * ib_umem_release - release pinned memory 648 * @umem: umem struct to release 649 */ 650 void ib_umem_release(struct ib_umem *umem) 651 { 652 if (IS_ERR_OR_NULL(umem)) 653 return; 654 if (umem->is_dmabuf) 655 return ib_umem_dmabuf_release(to_ib_umem_dmabuf(umem)); 656 if (umem->is_odp) 657 return ib_umem_odp_release(to_ib_umem_odp(umem)); 658 659 __ib_umem_release(umem->ibdev, umem, 1); 660 661 atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm); 662 mmdrop(umem->owning_mm); 663 kfree(umem); 664 } 665 EXPORT_SYMBOL(ib_umem_release); 666 667 /* 668 * Copy from the given ib_umem's pages to the given buffer. 669 * 670 * umem - the umem to copy from 671 * offset - offset to start copying from 672 * dst - destination buffer 673 * length - buffer length 674 * 675 * Returns 0 on success, or an error code. 676 */ 677 int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset, 678 size_t length) 679 { 680 size_t end = offset + length; 681 int ret; 682 683 if (offset > umem->length || length > umem->length - offset) { 684 pr_err("%s not in range. offset: %zd umem length: %zd end: %zd\n", 685 __func__, offset, umem->length, end); 686 return -EINVAL; 687 } 688 689 ret = sg_pcopy_to_buffer(umem->sgt_append.sgt.sgl, 690 umem->sgt_append.sgt.orig_nents, dst, length, 691 offset + ib_umem_offset(umem)); 692 693 if (ret < 0) 694 return ret; 695 else if (ret != length) 696 return -EINVAL; 697 else 698 return 0; 699 } 700 EXPORT_SYMBOL(ib_umem_copy_from); 701 702 /* 703 * Called during rereg mr if the driver is able to re-use a umem for 704 * IB_MR_REREG_ACCESS. 705 */ 706 int ib_umem_check_rereg(struct ib_umem *umem, int flags, int new_access_flags) 707 { 708 if (!umem) 709 return 0; 710 711 if (umem->is_dmabuf) 712 return -EOPNOTSUPP; 713 714 if ((flags & IB_MR_REREG_ACCESS) && !(flags & IB_MR_REREG_TRANS)) 715 if (ib_access_writable(new_access_flags) && !umem->writable) 716 return -EACCES; 717 return 0; 718 } 719 EXPORT_SYMBOL(ib_umem_check_rereg); 720