Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.1-rc3 879 lines 25 kB view raw
1/* 2 * Copyright (c) 2006 Oracle. All rights reserved. 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/kernel.h> 34#include <linux/slab.h> 35#include <linux/ratelimit.h> 36 37#include "rds.h" 38#include "iw.h" 39 40 41/* 42 * This is stored as mr->r_trans_private. 43 */ 44struct rds_iw_mr { 45 struct rds_iw_device *device; 46 struct rds_iw_mr_pool *pool; 47 struct rdma_cm_id *cm_id; 48 49 struct ib_mr *mr; 50 struct ib_fast_reg_page_list *page_list; 51 52 struct rds_iw_mapping mapping; 53 unsigned char remap_count; 54}; 55 56/* 57 * Our own little MR pool 58 */ 59struct rds_iw_mr_pool { 60 struct rds_iw_device *device; /* back ptr to the device that owns us */ 61 62 struct mutex flush_lock; /* serialize fmr invalidate */ 63 struct work_struct flush_worker; /* flush worker */ 64 65 spinlock_t list_lock; /* protect variables below */ 66 atomic_t item_count; /* total # of MRs */ 67 atomic_t dirty_count; /* # dirty of MRs */ 68 struct list_head dirty_list; /* dirty mappings */ 69 struct list_head clean_list; /* unused & unamapped MRs */ 70 atomic_t free_pinned; /* memory pinned by free MRs */ 71 unsigned long max_message_size; /* in pages */ 72 unsigned long max_items; 73 unsigned long max_items_soft; 74 unsigned long max_free_pinned; 75 int max_pages; 76}; 77 78static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all); 79static void rds_iw_mr_pool_flush_worker(struct work_struct *work); 80static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr); 81static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool, 82 struct rds_iw_mr *ibmr, 83 struct scatterlist *sg, unsigned int nents); 84static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr); 85static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool, 86 struct list_head *unmap_list, 87 struct list_head *kill_list); 88static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr); 89 90static int rds_iw_get_device(struct rds_sock *rs, struct rds_iw_device **rds_iwdev, struct rdma_cm_id **cm_id) 91{ 92 struct rds_iw_device *iwdev; 93 struct rds_iw_cm_id *i_cm_id; 94 95 *rds_iwdev = NULL; 96 *cm_id = NULL; 97 98 list_for_each_entry(iwdev, &rds_iw_devices, list) { 99 spin_lock_irq(&iwdev->spinlock); 100 list_for_each_entry(i_cm_id, &iwdev->cm_id_list, list) { 101 struct sockaddr_in *src_addr, *dst_addr; 102 103 src_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.src_addr; 104 dst_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.dst_addr; 105 106 rdsdebug("local ipaddr = %x port %d, " 107 "remote ipaddr = %x port %d" 108 "..looking for %x port %d, " 109 "remote ipaddr = %x port %d\n", 110 src_addr->sin_addr.s_addr, 111 src_addr->sin_port, 112 dst_addr->sin_addr.s_addr, 113 dst_addr->sin_port, 114 rs->rs_bound_addr, 115 rs->rs_bound_port, 116 rs->rs_conn_addr, 117 rs->rs_conn_port); 118#ifdef WORKING_TUPLE_DETECTION 119 if (src_addr->sin_addr.s_addr == rs->rs_bound_addr && 120 src_addr->sin_port == rs->rs_bound_port && 121 dst_addr->sin_addr.s_addr == rs->rs_conn_addr && 122 dst_addr->sin_port == rs->rs_conn_port) { 123#else 124 /* FIXME - needs to compare the local and remote 125 * ipaddr/port tuple, but the ipaddr is the only 126 * available information in the rds_sock (as the rest are 127 * zero'ed. It doesn't appear to be properly populated 128 * during connection setup... 129 */ 130 if (src_addr->sin_addr.s_addr == rs->rs_bound_addr) { 131#endif 132 spin_unlock_irq(&iwdev->spinlock); 133 *rds_iwdev = iwdev; 134 *cm_id = i_cm_id->cm_id; 135 return 0; 136 } 137 } 138 spin_unlock_irq(&iwdev->spinlock); 139 } 140 141 return 1; 142} 143 144static int rds_iw_add_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id) 145{ 146 struct rds_iw_cm_id *i_cm_id; 147 148 i_cm_id = kmalloc(sizeof *i_cm_id, GFP_KERNEL); 149 if (!i_cm_id) 150 return -ENOMEM; 151 152 i_cm_id->cm_id = cm_id; 153 154 spin_lock_irq(&rds_iwdev->spinlock); 155 list_add_tail(&i_cm_id->list, &rds_iwdev->cm_id_list); 156 spin_unlock_irq(&rds_iwdev->spinlock); 157 158 return 0; 159} 160 161static void rds_iw_remove_cm_id(struct rds_iw_device *rds_iwdev, 162 struct rdma_cm_id *cm_id) 163{ 164 struct rds_iw_cm_id *i_cm_id; 165 166 spin_lock_irq(&rds_iwdev->spinlock); 167 list_for_each_entry(i_cm_id, &rds_iwdev->cm_id_list, list) { 168 if (i_cm_id->cm_id == cm_id) { 169 list_del(&i_cm_id->list); 170 kfree(i_cm_id); 171 break; 172 } 173 } 174 spin_unlock_irq(&rds_iwdev->spinlock); 175} 176 177 178int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id) 179{ 180 struct sockaddr_in *src_addr, *dst_addr; 181 struct rds_iw_device *rds_iwdev_old; 182 struct rds_sock rs; 183 struct rdma_cm_id *pcm_id; 184 int rc; 185 186 src_addr = (struct sockaddr_in *)&cm_id->route.addr.src_addr; 187 dst_addr = (struct sockaddr_in *)&cm_id->route.addr.dst_addr; 188 189 rs.rs_bound_addr = src_addr->sin_addr.s_addr; 190 rs.rs_bound_port = src_addr->sin_port; 191 rs.rs_conn_addr = dst_addr->sin_addr.s_addr; 192 rs.rs_conn_port = dst_addr->sin_port; 193 194 rc = rds_iw_get_device(&rs, &rds_iwdev_old, &pcm_id); 195 if (rc) 196 rds_iw_remove_cm_id(rds_iwdev, cm_id); 197 198 return rds_iw_add_cm_id(rds_iwdev, cm_id); 199} 200 201void rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn) 202{ 203 struct rds_iw_connection *ic = conn->c_transport_data; 204 205 /* conn was previously on the nodev_conns_list */ 206 spin_lock_irq(&iw_nodev_conns_lock); 207 BUG_ON(list_empty(&iw_nodev_conns)); 208 BUG_ON(list_empty(&ic->iw_node)); 209 list_del(&ic->iw_node); 210 211 spin_lock(&rds_iwdev->spinlock); 212 list_add_tail(&ic->iw_node, &rds_iwdev->conn_list); 213 spin_unlock(&rds_iwdev->spinlock); 214 spin_unlock_irq(&iw_nodev_conns_lock); 215 216 ic->rds_iwdev = rds_iwdev; 217} 218 219void rds_iw_remove_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn) 220{ 221 struct rds_iw_connection *ic = conn->c_transport_data; 222 223 /* place conn on nodev_conns_list */ 224 spin_lock(&iw_nodev_conns_lock); 225 226 spin_lock_irq(&rds_iwdev->spinlock); 227 BUG_ON(list_empty(&ic->iw_node)); 228 list_del(&ic->iw_node); 229 spin_unlock_irq(&rds_iwdev->spinlock); 230 231 list_add_tail(&ic->iw_node, &iw_nodev_conns); 232 233 spin_unlock(&iw_nodev_conns_lock); 234 235 rds_iw_remove_cm_id(ic->rds_iwdev, ic->i_cm_id); 236 ic->rds_iwdev = NULL; 237} 238 239void __rds_iw_destroy_conns(struct list_head *list, spinlock_t *list_lock) 240{ 241 struct rds_iw_connection *ic, *_ic; 242 LIST_HEAD(tmp_list); 243 244 /* avoid calling conn_destroy with irqs off */ 245 spin_lock_irq(list_lock); 246 list_splice(list, &tmp_list); 247 INIT_LIST_HEAD(list); 248 spin_unlock_irq(list_lock); 249 250 list_for_each_entry_safe(ic, _ic, &tmp_list, iw_node) 251 rds_conn_destroy(ic->conn); 252} 253 254static void rds_iw_set_scatterlist(struct rds_iw_scatterlist *sg, 255 struct scatterlist *list, unsigned int sg_len) 256{ 257 sg->list = list; 258 sg->len = sg_len; 259 sg->dma_len = 0; 260 sg->dma_npages = 0; 261 sg->bytes = 0; 262} 263 264static u64 *rds_iw_map_scatterlist(struct rds_iw_device *rds_iwdev, 265 struct rds_iw_scatterlist *sg) 266{ 267 struct ib_device *dev = rds_iwdev->dev; 268 u64 *dma_pages = NULL; 269 int i, j, ret; 270 271 WARN_ON(sg->dma_len); 272 273 sg->dma_len = ib_dma_map_sg(dev, sg->list, sg->len, DMA_BIDIRECTIONAL); 274 if (unlikely(!sg->dma_len)) { 275 printk(KERN_WARNING "RDS/IW: dma_map_sg failed!\n"); 276 return ERR_PTR(-EBUSY); 277 } 278 279 sg->bytes = 0; 280 sg->dma_npages = 0; 281 282 ret = -EINVAL; 283 for (i = 0; i < sg->dma_len; ++i) { 284 unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]); 285 u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]); 286 u64 end_addr; 287 288 sg->bytes += dma_len; 289 290 end_addr = dma_addr + dma_len; 291 if (dma_addr & PAGE_MASK) { 292 if (i > 0) 293 goto out_unmap; 294 dma_addr &= ~PAGE_MASK; 295 } 296 if (end_addr & PAGE_MASK) { 297 if (i < sg->dma_len - 1) 298 goto out_unmap; 299 end_addr = (end_addr + PAGE_MASK) & ~PAGE_MASK; 300 } 301 302 sg->dma_npages += (end_addr - dma_addr) >> PAGE_SHIFT; 303 } 304 305 /* Now gather the dma addrs into one list */ 306 if (sg->dma_npages > fastreg_message_size) 307 goto out_unmap; 308 309 dma_pages = kmalloc(sizeof(u64) * sg->dma_npages, GFP_ATOMIC); 310 if (!dma_pages) { 311 ret = -ENOMEM; 312 goto out_unmap; 313 } 314 315 for (i = j = 0; i < sg->dma_len; ++i) { 316 unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]); 317 u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]); 318 u64 end_addr; 319 320 end_addr = dma_addr + dma_len; 321 dma_addr &= ~PAGE_MASK; 322 for (; dma_addr < end_addr; dma_addr += PAGE_SIZE) 323 dma_pages[j++] = dma_addr; 324 BUG_ON(j > sg->dma_npages); 325 } 326 327 return dma_pages; 328 329out_unmap: 330 ib_dma_unmap_sg(rds_iwdev->dev, sg->list, sg->len, DMA_BIDIRECTIONAL); 331 sg->dma_len = 0; 332 kfree(dma_pages); 333 return ERR_PTR(ret); 334} 335 336 337struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *rds_iwdev) 338{ 339 struct rds_iw_mr_pool *pool; 340 341 pool = kzalloc(sizeof(*pool), GFP_KERNEL); 342 if (!pool) { 343 printk(KERN_WARNING "RDS/IW: rds_iw_create_mr_pool alloc error\n"); 344 return ERR_PTR(-ENOMEM); 345 } 346 347 pool->device = rds_iwdev; 348 INIT_LIST_HEAD(&pool->dirty_list); 349 INIT_LIST_HEAD(&pool->clean_list); 350 mutex_init(&pool->flush_lock); 351 spin_lock_init(&pool->list_lock); 352 INIT_WORK(&pool->flush_worker, rds_iw_mr_pool_flush_worker); 353 354 pool->max_message_size = fastreg_message_size; 355 pool->max_items = fastreg_pool_size; 356 pool->max_free_pinned = pool->max_items * pool->max_message_size / 4; 357 pool->max_pages = fastreg_message_size; 358 359 /* We never allow more than max_items MRs to be allocated. 360 * When we exceed more than max_items_soft, we start freeing 361 * items more aggressively. 362 * Make sure that max_items > max_items_soft > max_items / 2 363 */ 364 pool->max_items_soft = pool->max_items * 3 / 4; 365 366 return pool; 367} 368 369void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo) 370{ 371 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool; 372 373 iinfo->rdma_mr_max = pool->max_items; 374 iinfo->rdma_mr_size = pool->max_pages; 375} 376 377void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *pool) 378{ 379 flush_workqueue(rds_wq); 380 rds_iw_flush_mr_pool(pool, 1); 381 BUG_ON(atomic_read(&pool->item_count)); 382 BUG_ON(atomic_read(&pool->free_pinned)); 383 kfree(pool); 384} 385 386static inline struct rds_iw_mr *rds_iw_reuse_fmr(struct rds_iw_mr_pool *pool) 387{ 388 struct rds_iw_mr *ibmr = NULL; 389 unsigned long flags; 390 391 spin_lock_irqsave(&pool->list_lock, flags); 392 if (!list_empty(&pool->clean_list)) { 393 ibmr = list_entry(pool->clean_list.next, struct rds_iw_mr, mapping.m_list); 394 list_del_init(&ibmr->mapping.m_list); 395 } 396 spin_unlock_irqrestore(&pool->list_lock, flags); 397 398 return ibmr; 399} 400 401static struct rds_iw_mr *rds_iw_alloc_mr(struct rds_iw_device *rds_iwdev) 402{ 403 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool; 404 struct rds_iw_mr *ibmr = NULL; 405 int err = 0, iter = 0; 406 407 while (1) { 408 ibmr = rds_iw_reuse_fmr(pool); 409 if (ibmr) 410 return ibmr; 411 412 /* No clean MRs - now we have the choice of either 413 * allocating a fresh MR up to the limit imposed by the 414 * driver, or flush any dirty unused MRs. 415 * We try to avoid stalling in the send path if possible, 416 * so we allocate as long as we're allowed to. 417 * 418 * We're fussy with enforcing the FMR limit, though. If the driver 419 * tells us we can't use more than N fmrs, we shouldn't start 420 * arguing with it */ 421 if (atomic_inc_return(&pool->item_count) <= pool->max_items) 422 break; 423 424 atomic_dec(&pool->item_count); 425 426 if (++iter > 2) { 427 rds_iw_stats_inc(s_iw_rdma_mr_pool_depleted); 428 return ERR_PTR(-EAGAIN); 429 } 430 431 /* We do have some empty MRs. Flush them out. */ 432 rds_iw_stats_inc(s_iw_rdma_mr_pool_wait); 433 rds_iw_flush_mr_pool(pool, 0); 434 } 435 436 ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL); 437 if (!ibmr) { 438 err = -ENOMEM; 439 goto out_no_cigar; 440 } 441 442 spin_lock_init(&ibmr->mapping.m_lock); 443 INIT_LIST_HEAD(&ibmr->mapping.m_list); 444 ibmr->mapping.m_mr = ibmr; 445 446 err = rds_iw_init_fastreg(pool, ibmr); 447 if (err) 448 goto out_no_cigar; 449 450 rds_iw_stats_inc(s_iw_rdma_mr_alloc); 451 return ibmr; 452 453out_no_cigar: 454 if (ibmr) { 455 rds_iw_destroy_fastreg(pool, ibmr); 456 kfree(ibmr); 457 } 458 atomic_dec(&pool->item_count); 459 return ERR_PTR(err); 460} 461 462void rds_iw_sync_mr(void *trans_private, int direction) 463{ 464 struct rds_iw_mr *ibmr = trans_private; 465 struct rds_iw_device *rds_iwdev = ibmr->device; 466 467 switch (direction) { 468 case DMA_FROM_DEVICE: 469 ib_dma_sync_sg_for_cpu(rds_iwdev->dev, ibmr->mapping.m_sg.list, 470 ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL); 471 break; 472 case DMA_TO_DEVICE: 473 ib_dma_sync_sg_for_device(rds_iwdev->dev, ibmr->mapping.m_sg.list, 474 ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL); 475 break; 476 } 477} 478 479static inline unsigned int rds_iw_flush_goal(struct rds_iw_mr_pool *pool, int free_all) 480{ 481 unsigned int item_count; 482 483 item_count = atomic_read(&pool->item_count); 484 if (free_all) 485 return item_count; 486 487 return 0; 488} 489 490/* 491 * Flush our pool of MRs. 492 * At a minimum, all currently unused MRs are unmapped. 493 * If the number of MRs allocated exceeds the limit, we also try 494 * to free as many MRs as needed to get back to this limit. 495 */ 496static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all) 497{ 498 struct rds_iw_mr *ibmr, *next; 499 LIST_HEAD(unmap_list); 500 LIST_HEAD(kill_list); 501 unsigned long flags; 502 unsigned int nfreed = 0, ncleaned = 0, free_goal; 503 int ret = 0; 504 505 rds_iw_stats_inc(s_iw_rdma_mr_pool_flush); 506 507 mutex_lock(&pool->flush_lock); 508 509 spin_lock_irqsave(&pool->list_lock, flags); 510 /* Get the list of all mappings to be destroyed */ 511 list_splice_init(&pool->dirty_list, &unmap_list); 512 if (free_all) 513 list_splice_init(&pool->clean_list, &kill_list); 514 spin_unlock_irqrestore(&pool->list_lock, flags); 515 516 free_goal = rds_iw_flush_goal(pool, free_all); 517 518 /* Batched invalidate of dirty MRs. 519 * For FMR based MRs, the mappings on the unmap list are 520 * actually members of an ibmr (ibmr->mapping). They either 521 * migrate to the kill_list, or have been cleaned and should be 522 * moved to the clean_list. 523 * For fastregs, they will be dynamically allocated, and 524 * will be destroyed by the unmap function. 525 */ 526 if (!list_empty(&unmap_list)) { 527 ncleaned = rds_iw_unmap_fastreg_list(pool, &unmap_list, &kill_list); 528 /* If we've been asked to destroy all MRs, move those 529 * that were simply cleaned to the kill list */ 530 if (free_all) 531 list_splice_init(&unmap_list, &kill_list); 532 } 533 534 /* Destroy any MRs that are past their best before date */ 535 list_for_each_entry_safe(ibmr, next, &kill_list, mapping.m_list) { 536 rds_iw_stats_inc(s_iw_rdma_mr_free); 537 list_del(&ibmr->mapping.m_list); 538 rds_iw_destroy_fastreg(pool, ibmr); 539 kfree(ibmr); 540 nfreed++; 541 } 542 543 /* Anything that remains are laundered ibmrs, which we can add 544 * back to the clean list. */ 545 if (!list_empty(&unmap_list)) { 546 spin_lock_irqsave(&pool->list_lock, flags); 547 list_splice(&unmap_list, &pool->clean_list); 548 spin_unlock_irqrestore(&pool->list_lock, flags); 549 } 550 551 atomic_sub(ncleaned, &pool->dirty_count); 552 atomic_sub(nfreed, &pool->item_count); 553 554 mutex_unlock(&pool->flush_lock); 555 return ret; 556} 557 558static void rds_iw_mr_pool_flush_worker(struct work_struct *work) 559{ 560 struct rds_iw_mr_pool *pool = container_of(work, struct rds_iw_mr_pool, flush_worker); 561 562 rds_iw_flush_mr_pool(pool, 0); 563} 564 565void rds_iw_free_mr(void *trans_private, int invalidate) 566{ 567 struct rds_iw_mr *ibmr = trans_private; 568 struct rds_iw_mr_pool *pool = ibmr->device->mr_pool; 569 570 rdsdebug("RDS/IW: free_mr nents %u\n", ibmr->mapping.m_sg.len); 571 if (!pool) 572 return; 573 574 /* Return it to the pool's free list */ 575 rds_iw_free_fastreg(pool, ibmr); 576 577 /* If we've pinned too many pages, request a flush */ 578 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned || 579 atomic_read(&pool->dirty_count) >= pool->max_items / 10) 580 queue_work(rds_wq, &pool->flush_worker); 581 582 if (invalidate) { 583 if (likely(!in_interrupt())) { 584 rds_iw_flush_mr_pool(pool, 0); 585 } else { 586 /* We get here if the user created a MR marked 587 * as use_once and invalidate at the same time. */ 588 queue_work(rds_wq, &pool->flush_worker); 589 } 590 } 591} 592 593void rds_iw_flush_mrs(void) 594{ 595 struct rds_iw_device *rds_iwdev; 596 597 list_for_each_entry(rds_iwdev, &rds_iw_devices, list) { 598 struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool; 599 600 if (pool) 601 rds_iw_flush_mr_pool(pool, 0); 602 } 603} 604 605void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents, 606 struct rds_sock *rs, u32 *key_ret) 607{ 608 struct rds_iw_device *rds_iwdev; 609 struct rds_iw_mr *ibmr = NULL; 610 struct rdma_cm_id *cm_id; 611 int ret; 612 613 ret = rds_iw_get_device(rs, &rds_iwdev, &cm_id); 614 if (ret || !cm_id) { 615 ret = -ENODEV; 616 goto out; 617 } 618 619 if (!rds_iwdev->mr_pool) { 620 ret = -ENODEV; 621 goto out; 622 } 623 624 ibmr = rds_iw_alloc_mr(rds_iwdev); 625 if (IS_ERR(ibmr)) 626 return ibmr; 627 628 ibmr->cm_id = cm_id; 629 ibmr->device = rds_iwdev; 630 631 ret = rds_iw_map_fastreg(rds_iwdev->mr_pool, ibmr, sg, nents); 632 if (ret == 0) 633 *key_ret = ibmr->mr->rkey; 634 else 635 printk(KERN_WARNING "RDS/IW: failed to map mr (errno=%d)\n", ret); 636 637out: 638 if (ret) { 639 if (ibmr) 640 rds_iw_free_mr(ibmr, 0); 641 ibmr = ERR_PTR(ret); 642 } 643 return ibmr; 644} 645 646/* 647 * iWARP fastreg handling 648 * 649 * The life cycle of a fastreg registration is a bit different from 650 * FMRs. 651 * The idea behind fastreg is to have one MR, to which we bind different 652 * mappings over time. To avoid stalling on the expensive map and invalidate 653 * operations, these operations are pipelined on the same send queue on 654 * which we want to send the message containing the r_key. 655 * 656 * This creates a bit of a problem for us, as we do not have the destination 657 * IP in GET_MR, so the connection must be setup prior to the GET_MR call for 658 * RDMA to be correctly setup. If a fastreg request is present, rds_iw_xmit 659 * will try to queue a LOCAL_INV (if needed) and a FAST_REG_MR work request 660 * before queuing the SEND. When completions for these arrive, they are 661 * dispatched to the MR has a bit set showing that RDMa can be performed. 662 * 663 * There is another interesting aspect that's related to invalidation. 664 * The application can request that a mapping is invalidated in FREE_MR. 665 * The expectation there is that this invalidation step includes ALL 666 * PREVIOUSLY FREED MRs. 667 */ 668static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, 669 struct rds_iw_mr *ibmr) 670{ 671 struct rds_iw_device *rds_iwdev = pool->device; 672 struct ib_fast_reg_page_list *page_list = NULL; 673 struct ib_mr *mr; 674 int err; 675 676 mr = ib_alloc_fast_reg_mr(rds_iwdev->pd, pool->max_message_size); 677 if (IS_ERR(mr)) { 678 err = PTR_ERR(mr); 679 680 printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_mr failed (err=%d)\n", err); 681 return err; 682 } 683 684 /* FIXME - this is overkill, but mapping->m_sg.dma_len/mapping->m_sg.dma_npages 685 * is not filled in. 686 */ 687 page_list = ib_alloc_fast_reg_page_list(rds_iwdev->dev, pool->max_message_size); 688 if (IS_ERR(page_list)) { 689 err = PTR_ERR(page_list); 690 691 printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed (err=%d)\n", err); 692 ib_dereg_mr(mr); 693 return err; 694 } 695 696 ibmr->page_list = page_list; 697 ibmr->mr = mr; 698 return 0; 699} 700 701static int rds_iw_rdma_build_fastreg(struct rds_iw_mapping *mapping) 702{ 703 struct rds_iw_mr *ibmr = mapping->m_mr; 704 struct ib_send_wr f_wr, *failed_wr; 705 int ret; 706 707 /* 708 * Perform a WR for the fast_reg_mr. Each individual page 709 * in the sg list is added to the fast reg page list and placed 710 * inside the fast_reg_mr WR. The key used is a rolling 8bit 711 * counter, which should guarantee uniqueness. 712 */ 713 ib_update_fast_reg_key(ibmr->mr, ibmr->remap_count++); 714 mapping->m_rkey = ibmr->mr->rkey; 715 716 memset(&f_wr, 0, sizeof(f_wr)); 717 f_wr.wr_id = RDS_IW_FAST_REG_WR_ID; 718 f_wr.opcode = IB_WR_FAST_REG_MR; 719 f_wr.wr.fast_reg.length = mapping->m_sg.bytes; 720 f_wr.wr.fast_reg.rkey = mapping->m_rkey; 721 f_wr.wr.fast_reg.page_list = ibmr->page_list; 722 f_wr.wr.fast_reg.page_list_len = mapping->m_sg.dma_len; 723 f_wr.wr.fast_reg.page_shift = PAGE_SHIFT; 724 f_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE | 725 IB_ACCESS_REMOTE_READ | 726 IB_ACCESS_REMOTE_WRITE; 727 f_wr.wr.fast_reg.iova_start = 0; 728 f_wr.send_flags = IB_SEND_SIGNALED; 729 730 failed_wr = &f_wr; 731 ret = ib_post_send(ibmr->cm_id->qp, &f_wr, &failed_wr); 732 BUG_ON(failed_wr != &f_wr); 733 if (ret) 734 printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n", 735 __func__, __LINE__, ret); 736 return ret; 737} 738 739static int rds_iw_rdma_fastreg_inv(struct rds_iw_mr *ibmr) 740{ 741 struct ib_send_wr s_wr, *failed_wr; 742 int ret = 0; 743 744 if (!ibmr->cm_id->qp || !ibmr->mr) 745 goto out; 746 747 memset(&s_wr, 0, sizeof(s_wr)); 748 s_wr.wr_id = RDS_IW_LOCAL_INV_WR_ID; 749 s_wr.opcode = IB_WR_LOCAL_INV; 750 s_wr.ex.invalidate_rkey = ibmr->mr->rkey; 751 s_wr.send_flags = IB_SEND_SIGNALED; 752 753 failed_wr = &s_wr; 754 ret = ib_post_send(ibmr->cm_id->qp, &s_wr, &failed_wr); 755 if (ret) { 756 printk_ratelimited(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n", 757 __func__, __LINE__, ret); 758 goto out; 759 } 760out: 761 return ret; 762} 763 764static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool, 765 struct rds_iw_mr *ibmr, 766 struct scatterlist *sg, 767 unsigned int sg_len) 768{ 769 struct rds_iw_device *rds_iwdev = pool->device; 770 struct rds_iw_mapping *mapping = &ibmr->mapping; 771 u64 *dma_pages; 772 int i, ret = 0; 773 774 rds_iw_set_scatterlist(&mapping->m_sg, sg, sg_len); 775 776 dma_pages = rds_iw_map_scatterlist(rds_iwdev, &mapping->m_sg); 777 if (IS_ERR(dma_pages)) { 778 ret = PTR_ERR(dma_pages); 779 dma_pages = NULL; 780 goto out; 781 } 782 783 if (mapping->m_sg.dma_len > pool->max_message_size) { 784 ret = -EMSGSIZE; 785 goto out; 786 } 787 788 for (i = 0; i < mapping->m_sg.dma_npages; ++i) 789 ibmr->page_list->page_list[i] = dma_pages[i]; 790 791 ret = rds_iw_rdma_build_fastreg(mapping); 792 if (ret) 793 goto out; 794 795 rds_iw_stats_inc(s_iw_rdma_mr_used); 796 797out: 798 kfree(dma_pages); 799 800 return ret; 801} 802 803/* 804 * "Free" a fastreg MR. 805 */ 806static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, 807 struct rds_iw_mr *ibmr) 808{ 809 unsigned long flags; 810 int ret; 811 812 if (!ibmr->mapping.m_sg.dma_len) 813 return; 814 815 ret = rds_iw_rdma_fastreg_inv(ibmr); 816 if (ret) 817 return; 818 819 /* Try to post the LOCAL_INV WR to the queue. */ 820 spin_lock_irqsave(&pool->list_lock, flags); 821 822 list_add_tail(&ibmr->mapping.m_list, &pool->dirty_list); 823 atomic_add(ibmr->mapping.m_sg.len, &pool->free_pinned); 824 atomic_inc(&pool->dirty_count); 825 826 spin_unlock_irqrestore(&pool->list_lock, flags); 827} 828 829static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool, 830 struct list_head *unmap_list, 831 struct list_head *kill_list) 832{ 833 struct rds_iw_mapping *mapping, *next; 834 unsigned int ncleaned = 0; 835 LIST_HEAD(laundered); 836 837 /* Batched invalidation of fastreg MRs. 838 * Why do we do it this way, even though we could pipeline unmap 839 * and remap? The reason is the application semantics - when the 840 * application requests an invalidation of MRs, it expects all 841 * previously released R_Keys to become invalid. 842 * 843 * If we implement MR reuse naively, we risk memory corruption 844 * (this has actually been observed). So the default behavior 845 * requires that a MR goes through an explicit unmap operation before 846 * we can reuse it again. 847 * 848 * We could probably improve on this a little, by allowing immediate 849 * reuse of a MR on the same socket (eg you could add small 850 * cache of unused MRs to strct rds_socket - GET_MR could grab one 851 * of these without requiring an explicit invalidate). 852 */ 853 while (!list_empty(unmap_list)) { 854 unsigned long flags; 855 856 spin_lock_irqsave(&pool->list_lock, flags); 857 list_for_each_entry_safe(mapping, next, unmap_list, m_list) { 858 list_move(&mapping->m_list, &laundered); 859 ncleaned++; 860 } 861 spin_unlock_irqrestore(&pool->list_lock, flags); 862 } 863 864 /* Move all laundered mappings back to the unmap list. 865 * We do not kill any WRs right now - it doesn't seem the 866 * fastreg API has a max_remap limit. */ 867 list_splice_init(&laundered, unmap_list); 868 869 return ncleaned; 870} 871 872static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, 873 struct rds_iw_mr *ibmr) 874{ 875 if (ibmr->page_list) 876 ib_free_fast_reg_page_list(ibmr->page_list); 877 if (ibmr->mr) 878 ib_dereg_mr(ibmr->mr); 879}