1 /* 2 * Copyright (c) 2007, 2020 Oracle and/or its affiliates. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 */ 33 #include <linux/pagemap.h> 34 #include <linux/slab.h> 35 #include <linux/rbtree.h> 36 #include <linux/dma-mapping.h> /* for DMA_*_DEVICE */ 37 38 #include "rds.h" 39 40 /* 41 * XXX 42 * - build with sparse 43 * - should we detect duplicate keys on a socket? hmm. 44 * - an rdma is an mlock, apply rlimit? 45 */ 46 47 /* 48 * get the number of pages by looking at the page indices that the start and 49 * end addresses fall in. 50 * 51 * Returns 0 if the vec is invalid. It is invalid if the number of bytes 52 * causes the address to wrap or overflows an unsigned int. This comes 53 * from being stored in the 'length' member of 'struct scatterlist'. 54 */ 55 static unsigned int rds_pages_in_vec(struct rds_iovec *vec) 56 { 57 if ((vec->addr + vec->bytes <= vec->addr) || 58 (vec->bytes > (u64)UINT_MAX)) 59 return 0; 60 61 return ((vec->addr + vec->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT) - 62 (vec->addr >> PAGE_SHIFT); 63 } 64 65 static struct rds_mr *rds_mr_tree_walk(struct rb_root *root, u64 key, 66 struct rds_mr *insert) 67 { 68 struct rb_node **p = &root->rb_node; 69 struct rb_node *parent = NULL; 70 struct rds_mr *mr; 71 72 while (*p) { 73 parent = *p; 74 mr = rb_entry(parent, struct rds_mr, r_rb_node); 75 76 if (key < mr->r_key) 77 p = &(*p)->rb_left; 78 else if (key > mr->r_key) 79 p = &(*p)->rb_right; 80 else 81 return mr; 82 } 83 84 if (insert) { 85 rb_link_node(&insert->r_rb_node, parent, p); 86 rb_insert_color(&insert->r_rb_node, root); 87 kref_get(&insert->r_kref); 88 } 89 return NULL; 90 } 91 92 /* 93 * Destroy the transport-specific part of a MR. 94 */ 95 static void rds_destroy_mr(struct rds_mr *mr) 96 { 97 struct rds_sock *rs = mr->r_sock; 98 void *trans_private = NULL; 99 unsigned long flags; 100 101 rdsdebug("RDS: destroy mr key is %x refcnt %u\n", 102 mr->r_key, kref_read(&mr->r_kref)); 103 104 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 105 if (!RB_EMPTY_NODE(&mr->r_rb_node)) 106 rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys); 107 trans_private = mr->r_trans_private; 108 mr->r_trans_private = NULL; 109 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 110 111 if (trans_private) 112 mr->r_trans->free_mr(trans_private, mr->r_invalidate); 113 } 114 115 void __rds_put_mr_final(struct kref *kref) 116 { 117 struct rds_mr *mr = container_of(kref, struct rds_mr, r_kref); 118 119 rds_destroy_mr(mr); 120 sock_put(rds_rs_to_sk(mr->r_sock)); 121 kfree(mr); 122 } 123 124 /* 125 * By the time this is called we can't have any more ioctls called on 126 * the socket so we don't need to worry about racing with others. 127 */ 128 void rds_rdma_drop_keys(struct rds_sock *rs) 129 { 130 struct rds_mr *mr; 131 struct rb_node *node; 132 unsigned long flags; 133 134 /* Release any MRs associated with this socket */ 135 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 136 while ((node = rb_first(&rs->rs_rdma_keys))) { 137 mr = rb_entry(node, struct rds_mr, r_rb_node); 138 if (mr->r_trans == rs->rs_transport) 139 mr->r_invalidate = 0; 140 rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys); 141 RB_CLEAR_NODE(&mr->r_rb_node); 142 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 143 kref_put(&mr->r_kref, __rds_put_mr_final); 144 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 145 } 146 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 147 148 if (rs->rs_transport && rs->rs_transport->flush_mrs) 149 rs->rs_transport->flush_mrs(); 150 } 151 152 /* 153 * Helper function to pin user pages. 154 */ 155 static int rds_pin_pages(unsigned long user_addr, unsigned int nr_pages, 156 struct page **pages, int write) 157 { 158 unsigned int gup_flags = FOLL_LONGTERM; 159 int ret; 160 161 if (write) 162 gup_flags |= FOLL_WRITE; 163 164 ret = pin_user_pages_fast(user_addr, nr_pages, gup_flags, pages); 165 if (ret >= 0 && ret < nr_pages) { 166 unpin_user_pages(pages, ret); 167 ret = -EFAULT; 168 } 169 170 return ret; 171 } 172 173 static int __rds_rdma_map(struct rds_sock *rs, struct rds_get_mr_args *args, 174 u64 *cookie_ret, struct rds_mr **mr_ret, 175 struct rds_conn_path *cp) 176 { 177 struct rds_mr *mr = NULL, *found; 178 struct scatterlist *sg = NULL; 179 unsigned int nr_pages; 180 struct page **pages = NULL; 181 void *trans_private; 182 unsigned long flags; 183 rds_rdma_cookie_t cookie; 184 unsigned int nents = 0; 185 int need_odp = 0; 186 long i; 187 int ret; 188 189 if (ipv6_addr_any(&rs->rs_bound_addr) || !rs->rs_transport) { 190 ret = -ENOTCONN; /* XXX not a great errno */ 191 goto out; 192 } 193 194 if (!rs->rs_transport->get_mr) { 195 ret = -EOPNOTSUPP; 196 goto out; 197 } 198 199 /* If the combination of the addr and size requested for this memory 200 * region causes an integer overflow, return error. 201 */ 202 if (((args->vec.addr + args->vec.bytes) < args->vec.addr) || 203 PAGE_ALIGN(args->vec.addr + args->vec.bytes) < 204 (args->vec.addr + args->vec.bytes)) { 205 ret = -EINVAL; 206 goto out; 207 } 208 209 if (!can_do_mlock()) { 210 ret = -EPERM; 211 goto out; 212 } 213 214 nr_pages = rds_pages_in_vec(&args->vec); 215 if (nr_pages == 0) { 216 ret = -EINVAL; 217 goto out; 218 } 219 220 /* Restrict the size of mr irrespective of underlying transport 221 * To account for unaligned mr regions, subtract one from nr_pages 222 */ 223 if ((nr_pages - 1) > (RDS_MAX_MSG_SIZE >> PAGE_SHIFT)) { 224 ret = -EMSGSIZE; 225 goto out; 226 } 227 228 rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n", 229 args->vec.addr, args->vec.bytes, nr_pages); 230 231 /* XXX clamp nr_pages to limit the size of this alloc? */ 232 pages = kzalloc_objs(struct page *, nr_pages); 233 if (!pages) { 234 ret = -ENOMEM; 235 goto out; 236 } 237 238 mr = kzalloc_obj(struct rds_mr); 239 if (!mr) { 240 ret = -ENOMEM; 241 goto out; 242 } 243 244 kref_init(&mr->r_kref); 245 RB_CLEAR_NODE(&mr->r_rb_node); 246 mr->r_trans = rs->rs_transport; 247 /* The MR can outlive its socket: a socket reference is held 248 * until the final kref is dropped in __rds_put_mr_final(). 249 */ 250 mr->r_sock = rs; 251 sock_hold(rds_rs_to_sk(rs)); 252 253 if (args->flags & RDS_RDMA_USE_ONCE) 254 mr->r_use_once = 1; 255 if (args->flags & RDS_RDMA_INVALIDATE) 256 mr->r_invalidate = 1; 257 if (args->flags & RDS_RDMA_READWRITE) 258 mr->r_write = 1; 259 260 /* 261 * Pin the pages that make up the user buffer and transfer the page 262 * pointers to the mr's sg array. We check to see if we've mapped 263 * the whole region after transferring the partial page references 264 * to the sg array so that we can have one page ref cleanup path. 265 * 266 * For now we have no flag that tells us whether the mapping is 267 * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to 268 * the zero page. 269 */ 270 ret = rds_pin_pages(args->vec.addr, nr_pages, pages, 1); 271 if (ret == -EOPNOTSUPP) { 272 need_odp = 1; 273 } else if (ret <= 0) { 274 goto out; 275 } else { 276 nents = ret; 277 sg = kmalloc_objs(*sg, nents); 278 if (!sg) { 279 ret = -ENOMEM; 280 goto out; 281 } 282 WARN_ON(!nents); 283 sg_init_table(sg, nents); 284 285 /* Stick all pages into the scatterlist */ 286 for (i = 0 ; i < nents; i++) 287 sg_set_page(&sg[i], pages[i], PAGE_SIZE, 0); 288 289 rdsdebug("RDS: trans_private nents is %u\n", nents); 290 } 291 /* Obtain a transport specific MR. If this succeeds, the 292 * s/g list is now owned by the MR. 293 * Note that dma_map() implies that pending writes are 294 * flushed to RAM, so no dma_sync is needed here. */ 295 trans_private = rs->rs_transport->get_mr( 296 sg, nents, rs, &mr->r_key, cp ? cp->cp_conn : NULL, 297 args->vec.addr, args->vec.bytes, 298 need_odp ? ODP_ZEROBASED : ODP_NOT_NEEDED); 299 300 if (IS_ERR(trans_private)) { 301 /* In ODP case, we don't GUP pages, so don't need 302 * to release anything. 303 */ 304 if (!need_odp) { 305 unpin_user_pages(pages, nr_pages); 306 kfree(sg); 307 } 308 ret = PTR_ERR(trans_private); 309 /* Trigger connection so that its ready for the next retry */ 310 if (ret == -ENODEV && cp) 311 rds_conn_connect_if_down(cp->cp_conn); 312 goto out; 313 } 314 315 mr->r_trans_private = trans_private; 316 317 rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n", 318 mr->r_key, (void *)(unsigned long) args->cookie_addr); 319 320 /* The user may pass us an unaligned address, but we can only 321 * map page aligned regions. So we keep the offset, and build 322 * a 64bit cookie containing <R_Key, offset> and pass that 323 * around. */ 324 if (need_odp) 325 cookie = rds_rdma_make_cookie(mr->r_key, 0); 326 else 327 cookie = rds_rdma_make_cookie(mr->r_key, 328 args->vec.addr & ~PAGE_MASK); 329 if (cookie_ret) 330 *cookie_ret = cookie; 331 332 if (args->cookie_addr && 333 put_user(cookie, (u64 __user *)(unsigned long)args->cookie_addr)) { 334 ret = -EFAULT; 335 goto out; 336 } 337 338 /* Inserting the new MR into the rbtree bumps its 339 * reference count. */ 340 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 341 found = rds_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr); 342 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 343 344 BUG_ON(found && found != mr); 345 346 rdsdebug("RDS: get_mr key is %x\n", mr->r_key); 347 if (mr_ret) { 348 kref_get(&mr->r_kref); 349 *mr_ret = mr; 350 } 351 352 ret = 0; 353 out: 354 kfree(pages); 355 if (mr) 356 kref_put(&mr->r_kref, __rds_put_mr_final); 357 return ret; 358 } 359 360 int rds_get_mr(struct rds_sock *rs, sockptr_t optval, int optlen) 361 { 362 struct rds_get_mr_args args; 363 364 if (optlen != sizeof(struct rds_get_mr_args)) 365 return -EINVAL; 366 367 if (copy_from_sockptr(&args, optval, sizeof(struct rds_get_mr_args))) 368 return -EFAULT; 369 370 return __rds_rdma_map(rs, &args, NULL, NULL, NULL); 371 } 372 373 int rds_get_mr_for_dest(struct rds_sock *rs, sockptr_t optval, int optlen) 374 { 375 struct rds_get_mr_for_dest_args args; 376 struct rds_get_mr_args new_args; 377 378 if (optlen != sizeof(struct rds_get_mr_for_dest_args)) 379 return -EINVAL; 380 381 if (copy_from_sockptr(&args, optval, 382 sizeof(struct rds_get_mr_for_dest_args))) 383 return -EFAULT; 384 385 /* 386 * Initially, just behave like get_mr(). 387 * TODO: Implement get_mr as wrapper around this 388 * and deprecate it. 389 */ 390 new_args.vec = args.vec; 391 new_args.cookie_addr = args.cookie_addr; 392 new_args.flags = args.flags; 393 394 return __rds_rdma_map(rs, &new_args, NULL, NULL, NULL); 395 } 396 397 /* 398 * Free the MR indicated by the given R_Key 399 */ 400 int rds_free_mr(struct rds_sock *rs, sockptr_t optval, int optlen) 401 { 402 struct rds_free_mr_args args; 403 struct rds_mr *mr; 404 unsigned long flags; 405 406 if (optlen != sizeof(struct rds_free_mr_args)) 407 return -EINVAL; 408 409 if (copy_from_sockptr(&args, optval, sizeof(struct rds_free_mr_args))) 410 return -EFAULT; 411 412 /* Special case - a null cookie means flush all unused MRs */ 413 if (args.cookie == 0) { 414 if (!rs->rs_transport || !rs->rs_transport->flush_mrs) 415 return -EINVAL; 416 rs->rs_transport->flush_mrs(); 417 return 0; 418 } 419 420 /* Look up the MR given its R_key and remove it from the rbtree 421 * so nobody else finds it. 422 * This should also prevent races with rds_rdma_unuse. 423 */ 424 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 425 mr = rds_mr_tree_walk(&rs->rs_rdma_keys, rds_rdma_cookie_key(args.cookie), NULL); 426 if (mr) { 427 rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys); 428 RB_CLEAR_NODE(&mr->r_rb_node); 429 if (args.flags & RDS_RDMA_INVALIDATE) 430 mr->r_invalidate = 1; 431 } 432 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 433 434 if (!mr) 435 return -EINVAL; 436 437 kref_put(&mr->r_kref, __rds_put_mr_final); 438 return 0; 439 } 440 441 /* 442 * This is called when we receive an extension header that 443 * tells us this MR was used. It allows us to implement 444 * use_once semantics 445 */ 446 void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force) 447 { 448 struct rds_mr *mr; 449 unsigned long flags; 450 int zot_me = 0; 451 452 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 453 mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL); 454 if (!mr) { 455 pr_debug("rds: trying to unuse MR with unknown r_key %u!\n", 456 r_key); 457 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 458 return; 459 } 460 461 /* Get a reference so that the MR won't go away before calling 462 * sync_mr() below. 463 */ 464 kref_get(&mr->r_kref); 465 466 /* If it is going to be freed, remove it from the tree now so 467 * that no other thread can find it and free it. 468 */ 469 if (mr->r_use_once || force) { 470 rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys); 471 RB_CLEAR_NODE(&mr->r_rb_node); 472 zot_me = 1; 473 } 474 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 475 476 /* May have to issue a dma_sync on this memory region. 477 * Note we could avoid this if the operation was a RDMA READ, 478 * but at this point we can't tell. */ 479 if (mr->r_trans->sync_mr) 480 mr->r_trans->sync_mr(mr->r_trans_private, DMA_FROM_DEVICE); 481 482 /* Release the reference held above. */ 483 kref_put(&mr->r_kref, __rds_put_mr_final); 484 485 /* If the MR was marked as invalidate, this will 486 * trigger an async flush. */ 487 if (zot_me) 488 kref_put(&mr->r_kref, __rds_put_mr_final); 489 } 490 491 void rds_rdma_op_unpin_pages(struct rm_rdma_op *ro) 492 { 493 unsigned int i; 494 495 for (i = 0; i < ro->op_nents; i++) { 496 struct page *page = sg_page(&ro->op_sg[i]); 497 498 /* Mark page dirty if it was possibly modified, which 499 * is the case for a RDMA_READ which copies from remote 500 * to local memory 501 */ 502 unpin_user_pages_dirty_lock(&page, 1, !ro->op_write); 503 } 504 } 505 506 void rds_rdma_free_op(struct rm_rdma_op *ro) 507 { 508 if (ro->op_odp_mr) { 509 kref_put(&ro->op_odp_mr->r_kref, __rds_put_mr_final); 510 } else if (in_task() || ro->op_write) { 511 /* An RDMA write's pages are only read by the remote 512 * side; unpinning without dirtying does not sleep. 513 */ 514 rds_rdma_op_unpin_pages(ro); 515 } else { 516 /* Dirtying the pages on unpin can sleep; leave them 517 * pinned and have rds_message_put() finish the unpin 518 * from process context. 519 */ 520 ro->op_unpin_deferred = 1; 521 } 522 523 kfree(ro->op_notifier); 524 ro->op_notifier = NULL; 525 ro->op_active = 0; 526 ro->op_odp_mr = NULL; 527 } 528 529 void rds_atomic_op_unpin_page(struct rm_atomic_op *ao) 530 { 531 struct page *page = sg_page(ao->op_sg); 532 533 /* Mark page dirty if it was possibly modified, which 534 * is the case for a RDMA_READ which copies from remote 535 * to local memory */ 536 unpin_user_pages_dirty_lock(&page, 1, true); 537 } 538 539 void rds_atomic_free_op(struct rm_atomic_op *ao) 540 { 541 if (in_task()) { 542 rds_atomic_op_unpin_page(ao); 543 } else { 544 /* Dirtying the page on unpin can sleep; leave it 545 * pinned and have rds_message_put() finish the unpin 546 * from process context. 547 */ 548 ao->op_unpin_deferred = 1; 549 } 550 551 kfree(ao->op_notifier); 552 ao->op_notifier = NULL; 553 ao->op_active = 0; 554 } 555 556 557 /* 558 * Count the number of pages needed to describe an incoming iovec array. 559 */ 560 static int rds_rdma_pages(struct rds_iovec iov[], int nr_iovecs) 561 { 562 int tot_pages = 0; 563 unsigned int nr_pages; 564 unsigned int i; 565 566 /* figure out the number of pages in the vector */ 567 for (i = 0; i < nr_iovecs; i++) { 568 nr_pages = rds_pages_in_vec(&iov[i]); 569 if (nr_pages == 0) 570 return -EINVAL; 571 572 tot_pages += nr_pages; 573 574 /* 575 * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1, 576 * so tot_pages cannot overflow without first going negative. 577 */ 578 if (tot_pages < 0) 579 return -EINVAL; 580 } 581 582 return tot_pages; 583 } 584 585 int rds_rdma_extra_size(struct rds_rdma_args *args, 586 struct rds_iov_vector *iov) 587 { 588 struct rds_iovec *vec; 589 struct rds_iovec __user *local_vec; 590 int tot_pages = 0; 591 unsigned int nr_pages; 592 unsigned int i; 593 594 local_vec = (struct rds_iovec __user *)(unsigned long) args->local_vec_addr; 595 596 if (args->nr_local == 0) 597 return -EINVAL; 598 599 if (args->nr_local > UIO_MAXIOV) 600 return -EMSGSIZE; 601 602 iov->iov = kzalloc_objs(struct rds_iovec, args->nr_local); 603 if (!iov->iov) 604 return -ENOMEM; 605 606 vec = &iov->iov[0]; 607 608 if (copy_from_user(vec, local_vec, args->nr_local * 609 sizeof(struct rds_iovec))) 610 return -EFAULT; 611 iov->len = args->nr_local; 612 613 /* figure out the number of pages in the vector */ 614 for (i = 0; i < args->nr_local; i++, vec++) { 615 616 nr_pages = rds_pages_in_vec(vec); 617 if (nr_pages == 0) 618 return -EINVAL; 619 620 tot_pages += nr_pages; 621 622 /* 623 * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1, 624 * so tot_pages cannot overflow without first going negative. 625 */ 626 if (tot_pages < 0) 627 return -EINVAL; 628 } 629 630 return tot_pages * sizeof(struct scatterlist); 631 } 632 633 /* 634 * The application asks for a RDMA transfer. 635 * Extract all arguments and set up the rdma_op 636 */ 637 int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm, 638 struct cmsghdr *cmsg, 639 struct rds_iov_vector *vec) 640 { 641 struct rds_rdma_args *args; 642 struct rm_rdma_op *op = &rm->rdma; 643 int nr_pages; 644 unsigned int nr_bytes; 645 struct page **pages = NULL; 646 struct rds_iovec *iovs; 647 unsigned int i, j; 648 int ret = 0; 649 bool odp_supported = true; 650 651 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args)) 652 || rm->rdma.op_active) 653 return -EINVAL; 654 655 args = CMSG_DATA(cmsg); 656 657 if (ipv6_addr_any(&rs->rs_bound_addr)) { 658 ret = -ENOTCONN; /* XXX not a great errno */ 659 goto out_ret; 660 } 661 662 if (args->nr_local > UIO_MAXIOV) { 663 ret = -EMSGSIZE; 664 goto out_ret; 665 } 666 667 if (vec->len != args->nr_local) { 668 ret = -EINVAL; 669 goto out_ret; 670 } 671 /* odp-mr is not supported for multiple requests within one message */ 672 if (args->nr_local != 1) 673 odp_supported = false; 674 675 iovs = vec->iov; 676 677 nr_pages = rds_rdma_pages(iovs, args->nr_local); 678 if (nr_pages < 0) { 679 ret = -EINVAL; 680 goto out_ret; 681 } 682 683 pages = kzalloc_objs(struct page *, nr_pages); 684 if (!pages) { 685 ret = -ENOMEM; 686 goto out_ret; 687 } 688 689 op->op_write = !!(args->flags & RDS_RDMA_READWRITE); 690 op->op_fence = !!(args->flags & RDS_RDMA_FENCE); 691 op->op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME); 692 op->op_silent = !!(args->flags & RDS_RDMA_SILENT); 693 op->op_active = 1; 694 op->op_recverr = rs->rs_recverr; 695 op->op_odp_mr = NULL; 696 697 WARN_ON(!nr_pages); 698 op->op_sg = rds_message_alloc_sgs(rm, nr_pages); 699 if (IS_ERR(op->op_sg)) { 700 ret = PTR_ERR(op->op_sg); 701 goto out_pages; 702 } 703 704 if (op->op_notify || op->op_recverr) { 705 /* We allocate an uninitialized notifier here, because 706 * we don't want to do that in the completion handler. We 707 * would have to use GFP_ATOMIC there, and don't want to deal 708 * with failed allocations. 709 */ 710 op->op_notifier = kmalloc_obj(struct rds_notifier); 711 if (!op->op_notifier) { 712 ret = -ENOMEM; 713 goto out_pages; 714 } 715 op->op_notifier->n_user_token = args->user_token; 716 op->op_notifier->n_status = RDS_RDMA_SUCCESS; 717 } 718 719 /* The cookie contains the R_Key of the remote memory region, and 720 * optionally an offset into it. This is how we implement RDMA into 721 * unaligned memory. 722 * When setting up the RDMA, we need to add that offset to the 723 * destination address (which is really an offset into the MR) 724 * FIXME: We may want to move this into ib_rdma.c 725 */ 726 op->op_rkey = rds_rdma_cookie_key(args->cookie); 727 op->op_remote_addr = args->remote_vec.addr + rds_rdma_cookie_offset(args->cookie); 728 729 nr_bytes = 0; 730 731 rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n", 732 (unsigned long long)args->nr_local, 733 (unsigned long long)args->remote_vec.addr, 734 op->op_rkey); 735 736 for (i = 0; i < args->nr_local; i++) { 737 struct rds_iovec *iov = &iovs[i]; 738 /* don't need to check, rds_rdma_pages() verified nr will be +nonzero */ 739 unsigned int nr = rds_pages_in_vec(iov); 740 741 rs->rs_user_addr = iov->addr; 742 rs->rs_user_bytes = iov->bytes; 743 744 /* If it's a WRITE operation, we want to pin the pages for reading. 745 * If it's a READ operation, we need to pin the pages for writing. 746 */ 747 ret = rds_pin_pages(iov->addr, nr, pages, !op->op_write); 748 if ((!odp_supported && ret <= 0) || 749 (odp_supported && ret <= 0 && ret != -EOPNOTSUPP)) 750 goto out_pages; 751 752 if (ret == -EOPNOTSUPP) { 753 struct rds_mr *local_odp_mr; 754 755 if (!rs->rs_transport->get_mr) { 756 ret = -EOPNOTSUPP; 757 goto out_pages; 758 } 759 local_odp_mr = kzalloc_obj(*local_odp_mr); 760 if (!local_odp_mr) { 761 ret = -ENOMEM; 762 goto out_pages; 763 } 764 RB_CLEAR_NODE(&local_odp_mr->r_rb_node); 765 kref_init(&local_odp_mr->r_kref); 766 local_odp_mr->r_trans = rs->rs_transport; 767 /* The MR can outlive its socket: a socket 768 * reference is held until the final kref is 769 * dropped in __rds_put_mr_final(). 770 */ 771 local_odp_mr->r_sock = rs; 772 sock_hold(rds_rs_to_sk(rs)); 773 local_odp_mr->r_trans_private = 774 rs->rs_transport->get_mr( 775 NULL, 0, rs, &local_odp_mr->r_key, NULL, 776 iov->addr, iov->bytes, ODP_VIRTUAL); 777 if (IS_ERR(local_odp_mr->r_trans_private)) { 778 ret = PTR_ERR(local_odp_mr->r_trans_private); 779 rdsdebug("get_mr ret %d %p\"", ret, 780 local_odp_mr->r_trans_private); 781 local_odp_mr->r_trans_private = NULL; 782 kref_put(&local_odp_mr->r_kref, 783 __rds_put_mr_final); 784 ret = -EOPNOTSUPP; 785 goto out_pages; 786 } 787 rdsdebug("Need odp; local_odp_mr %p trans_private %p\n", 788 local_odp_mr, local_odp_mr->r_trans_private); 789 op->op_odp_mr = local_odp_mr; 790 op->op_odp_addr = iov->addr; 791 } 792 793 rdsdebug("RDS: nr_bytes %u nr %u iov->bytes %llu iov->addr %llx\n", 794 nr_bytes, nr, iov->bytes, iov->addr); 795 796 nr_bytes += iov->bytes; 797 798 for (j = 0; j < nr; j++) { 799 unsigned int offset = iov->addr & ~PAGE_MASK; 800 struct scatterlist *sg; 801 802 sg = &op->op_sg[op->op_nents + j]; 803 sg_set_page(sg, pages[j], 804 min_t(unsigned int, iov->bytes, PAGE_SIZE - offset), 805 offset); 806 807 sg_dma_len(sg) = sg->length; 808 rdsdebug("RDS: sg->offset %x sg->len %x iov->addr %llx iov->bytes %llu\n", 809 sg->offset, sg->length, iov->addr, iov->bytes); 810 811 iov->addr += sg->length; 812 iov->bytes -= sg->length; 813 } 814 815 op->op_nents += nr; 816 } 817 818 if (nr_bytes > args->remote_vec.bytes) { 819 rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n", 820 nr_bytes, 821 (unsigned int) args->remote_vec.bytes); 822 ret = -EINVAL; 823 goto out_pages; 824 } 825 op->op_bytes = nr_bytes; 826 ret = 0; 827 828 out_pages: 829 kfree(pages); 830 out_ret: 831 if (ret) 832 rds_rdma_free_op(op); 833 else 834 rds_stats_inc(s_send_rdma); 835 836 return ret; 837 } 838 839 /* 840 * The application wants us to pass an RDMA destination (aka MR) 841 * to the remote 842 */ 843 int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm, 844 struct cmsghdr *cmsg) 845 { 846 unsigned long flags; 847 struct rds_mr *mr; 848 u32 r_key; 849 int err = 0; 850 851 if (cmsg->cmsg_len < CMSG_LEN(sizeof(rds_rdma_cookie_t)) || 852 rm->m_rdma_cookie != 0) 853 return -EINVAL; 854 855 memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg), sizeof(rm->m_rdma_cookie)); 856 857 /* We are reusing a previously mapped MR here. Most likely, the 858 * application has written to the buffer, so we need to explicitly 859 * flush those writes to RAM. Otherwise the HCA may not see them 860 * when doing a DMA from that buffer. 861 */ 862 r_key = rds_rdma_cookie_key(rm->m_rdma_cookie); 863 864 spin_lock_irqsave(&rs->rs_rdma_lock, flags); 865 mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL); 866 if (!mr) 867 err = -EINVAL; /* invalid r_key */ 868 else 869 kref_get(&mr->r_kref); 870 spin_unlock_irqrestore(&rs->rs_rdma_lock, flags); 871 872 if (mr) { 873 mr->r_trans->sync_mr(mr->r_trans_private, 874 DMA_TO_DEVICE); 875 rm->rdma.op_rdma_mr = mr; 876 } 877 return err; 878 } 879 880 /* 881 * The application passes us an address range it wants to enable RDMA 882 * to/from. We map the area, and save the <R_Key,offset> pair 883 * in rm->m_rdma_cookie. This causes it to be sent along to the peer 884 * in an extension header. 885 */ 886 int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm, 887 struct cmsghdr *cmsg) 888 { 889 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_get_mr_args)) || 890 rm->m_rdma_cookie != 0) 891 return -EINVAL; 892 893 return __rds_rdma_map(rs, CMSG_DATA(cmsg), &rm->m_rdma_cookie, 894 &rm->rdma.op_rdma_mr, rm->m_conn_path); 895 } 896 897 /* 898 * Fill in rds_message for an atomic request. 899 */ 900 int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm, 901 struct cmsghdr *cmsg) 902 { 903 struct page *page = NULL; 904 struct rds_atomic_args *args; 905 int ret = 0; 906 907 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_atomic_args)) 908 || rm->atomic.op_active) 909 return -EINVAL; 910 911 args = CMSG_DATA(cmsg); 912 913 /* Nonmasked & masked cmsg ops converted to masked hw ops */ 914 switch (cmsg->cmsg_type) { 915 case RDS_CMSG_ATOMIC_FADD: 916 rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD; 917 rm->atomic.op_m_fadd.add = args->fadd.add; 918 rm->atomic.op_m_fadd.nocarry_mask = 0; 919 break; 920 case RDS_CMSG_MASKED_ATOMIC_FADD: 921 rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD; 922 rm->atomic.op_m_fadd.add = args->m_fadd.add; 923 rm->atomic.op_m_fadd.nocarry_mask = args->m_fadd.nocarry_mask; 924 break; 925 case RDS_CMSG_ATOMIC_CSWP: 926 rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP; 927 rm->atomic.op_m_cswp.compare = args->cswp.compare; 928 rm->atomic.op_m_cswp.swap = args->cswp.swap; 929 rm->atomic.op_m_cswp.compare_mask = ~0; 930 rm->atomic.op_m_cswp.swap_mask = ~0; 931 break; 932 case RDS_CMSG_MASKED_ATOMIC_CSWP: 933 rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP; 934 rm->atomic.op_m_cswp.compare = args->m_cswp.compare; 935 rm->atomic.op_m_cswp.swap = args->m_cswp.swap; 936 rm->atomic.op_m_cswp.compare_mask = args->m_cswp.compare_mask; 937 rm->atomic.op_m_cswp.swap_mask = args->m_cswp.swap_mask; 938 break; 939 default: 940 BUG(); /* should never happen */ 941 } 942 943 rm->atomic.op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME); 944 rm->atomic.op_silent = !!(args->flags & RDS_RDMA_SILENT); 945 rm->atomic.op_active = 1; 946 rm->atomic.op_recverr = rs->rs_recverr; 947 rm->atomic.op_sg = rds_message_alloc_sgs(rm, 1); 948 if (IS_ERR(rm->atomic.op_sg)) { 949 ret = PTR_ERR(rm->atomic.op_sg); 950 goto err; 951 } 952 953 /* verify 8 byte-aligned */ 954 if (args->local_addr & 0x7) { 955 ret = -EFAULT; 956 goto err; 957 } 958 959 ret = rds_pin_pages(args->local_addr, 1, &page, 1); 960 if (ret != 1) 961 goto err; 962 ret = 0; 963 964 sg_set_page(rm->atomic.op_sg, page, 8, offset_in_page(args->local_addr)); 965 966 if (rm->atomic.op_notify || rm->atomic.op_recverr) { 967 /* We allocate an uninitialized notifier here, because 968 * we don't want to do that in the completion handler. We 969 * would have to use GFP_ATOMIC there, and don't want to deal 970 * with failed allocations. 971 */ 972 rm->atomic.op_notifier = kmalloc_obj(*rm->atomic.op_notifier); 973 if (!rm->atomic.op_notifier) { 974 ret = -ENOMEM; 975 goto err; 976 } 977 978 rm->atomic.op_notifier->n_user_token = args->user_token; 979 rm->atomic.op_notifier->n_status = RDS_RDMA_SUCCESS; 980 } 981 982 rm->atomic.op_rkey = rds_rdma_cookie_key(args->cookie); 983 rm->atomic.op_remote_addr = args->remote_addr + rds_rdma_cookie_offset(args->cookie); 984 985 return ret; 986 err: 987 if (page) 988 unpin_user_page(page); 989 rm->atomic.op_active = 0; 990 kfree(rm->atomic.op_notifier); 991 992 return ret; 993 } 994