2 * Copyright (c) 2006 Oracle. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/rculist.h>
36 #include <linux/llist.h>
41 static DEFINE_PER_CPU(unsigned long, clean_list_grace);
42 #define CLEAN_LIST_BUSY_BIT 0
45 * This is stored as mr->r_trans_private.
48 struct rds_ib_device *device;
49 struct rds_ib_mr_pool *pool;
52 struct llist_node llnode;
54 /* unmap_list is for freeing */
55 struct list_head unmap_list;
56 unsigned int remap_count;
58 struct scatterlist *sg;
65 * Our own little FMR pool
67 struct rds_ib_mr_pool {
68 struct mutex flush_lock; /* serialize fmr invalidate */
69 struct delayed_work flush_worker; /* flush worker */
71 atomic_t item_count; /* total # of MRs */
72 atomic_t dirty_count; /* # dirty of MRs */
74 struct llist_head drop_list; /* MRs that have reached their max_maps limit */
75 struct llist_head free_list; /* unused MRs */
76 struct llist_head clean_list; /* global unused & unamapped MRs */
77 wait_queue_head_t flush_wait;
79 atomic_t free_pinned; /* memory pinned by free MRs */
80 unsigned long max_items;
81 unsigned long max_items_soft;
82 unsigned long max_free_pinned;
83 struct ib_fmr_attr fmr_attr;
86 static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool, int free_all, struct rds_ib_mr **);
87 static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr);
88 static void rds_ib_mr_pool_flush_worker(struct work_struct *work);
90 static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
92 struct rds_ib_device *rds_ibdev;
93 struct rds_ib_ipaddr *i_ipaddr;
96 list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) {
97 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
98 if (i_ipaddr->ipaddr == ipaddr) {
99 atomic_inc(&rds_ibdev->refcount);
110 static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
112 struct rds_ib_ipaddr *i_ipaddr;
114 i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
118 i_ipaddr->ipaddr = ipaddr;
120 spin_lock_irq(&rds_ibdev->spinlock);
121 list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
122 spin_unlock_irq(&rds_ibdev->spinlock);
127 static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
129 struct rds_ib_ipaddr *i_ipaddr;
130 struct rds_ib_ipaddr *to_free = NULL;
133 spin_lock_irq(&rds_ibdev->spinlock);
134 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
135 if (i_ipaddr->ipaddr == ipaddr) {
136 list_del_rcu(&i_ipaddr->list);
141 spin_unlock_irq(&rds_ibdev->spinlock);
149 int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
151 struct rds_ib_device *rds_ibdev_old;
153 rds_ibdev_old = rds_ib_get_device(ipaddr);
155 rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr);
156 rds_ib_dev_put(rds_ibdev_old);
159 return rds_ib_add_ipaddr(rds_ibdev, ipaddr);
162 void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
164 struct rds_ib_connection *ic = conn->c_transport_data;
166 /* conn was previously on the nodev_conns_list */
167 spin_lock_irq(&ib_nodev_conns_lock);
168 BUG_ON(list_empty(&ib_nodev_conns));
169 BUG_ON(list_empty(&ic->ib_node));
170 list_del(&ic->ib_node);
172 spin_lock(&rds_ibdev->spinlock);
173 list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
174 spin_unlock(&rds_ibdev->spinlock);
175 spin_unlock_irq(&ib_nodev_conns_lock);
177 ic->rds_ibdev = rds_ibdev;
178 atomic_inc(&rds_ibdev->refcount);
181 void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
183 struct rds_ib_connection *ic = conn->c_transport_data;
185 /* place conn on nodev_conns_list */
186 spin_lock(&ib_nodev_conns_lock);
188 spin_lock_irq(&rds_ibdev->spinlock);
189 BUG_ON(list_empty(&ic->ib_node));
190 list_del(&ic->ib_node);
191 spin_unlock_irq(&rds_ibdev->spinlock);
193 list_add_tail(&ic->ib_node, &ib_nodev_conns);
195 spin_unlock(&ib_nodev_conns_lock);
197 ic->rds_ibdev = NULL;
198 rds_ib_dev_put(rds_ibdev);
201 void rds_ib_destroy_nodev_conns(void)
203 struct rds_ib_connection *ic, *_ic;
206 /* avoid calling conn_destroy with irqs off */
207 spin_lock_irq(&ib_nodev_conns_lock);
208 list_splice(&ib_nodev_conns, &tmp_list);
209 spin_unlock_irq(&ib_nodev_conns_lock);
211 list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
212 rds_conn_destroy(ic->conn);
215 struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev)
217 struct rds_ib_mr_pool *pool;
219 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
221 return ERR_PTR(-ENOMEM);
223 init_llist_head(&pool->free_list);
224 init_llist_head(&pool->drop_list);
225 init_llist_head(&pool->clean_list);
226 mutex_init(&pool->flush_lock);
227 init_waitqueue_head(&pool->flush_wait);
228 INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
230 pool->fmr_attr.max_pages = fmr_message_size;
231 pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps;
232 pool->fmr_attr.page_shift = PAGE_SHIFT;
233 pool->max_free_pinned = rds_ibdev->max_fmrs * fmr_message_size / 4;
235 /* We never allow more than max_items MRs to be allocated.
236 * When we exceed more than max_items_soft, we start freeing
237 * items more aggressively.
238 * Make sure that max_items > max_items_soft > max_items / 2
240 pool->max_items_soft = rds_ibdev->max_fmrs * 3 / 4;
241 pool->max_items = rds_ibdev->max_fmrs;
246 void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
248 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
250 iinfo->rdma_mr_max = pool->max_items;
251 iinfo->rdma_mr_size = pool->fmr_attr.max_pages;
254 void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
256 cancel_delayed_work_sync(&pool->flush_worker);
257 rds_ib_flush_mr_pool(pool, 1, NULL);
258 WARN_ON(atomic_read(&pool->item_count));
259 WARN_ON(atomic_read(&pool->free_pinned));
263 static inline struct rds_ib_mr *rds_ib_reuse_fmr(struct rds_ib_mr_pool *pool)
265 struct rds_ib_mr *ibmr = NULL;
266 struct llist_node *ret;
270 flag = &__get_cpu_var(clean_list_grace);
271 set_bit(CLEAN_LIST_BUSY_BIT, flag);
272 ret = llist_del_first(&pool->clean_list);
274 ibmr = llist_entry(ret, struct rds_ib_mr, llnode);
276 clear_bit(CLEAN_LIST_BUSY_BIT, flag);
281 static inline void wait_clean_list_grace(void)
286 for_each_online_cpu(cpu) {
287 flag = &per_cpu(clean_list_grace, cpu);
288 while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
293 static struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev)
295 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
296 struct rds_ib_mr *ibmr = NULL;
297 int err = 0, iter = 0;
299 if (atomic_read(&pool->dirty_count) >= pool->max_items / 10)
300 schedule_delayed_work(&pool->flush_worker, 10);
303 ibmr = rds_ib_reuse_fmr(pool);
307 /* No clean MRs - now we have the choice of either
308 * allocating a fresh MR up to the limit imposed by the
309 * driver, or flush any dirty unused MRs.
310 * We try to avoid stalling in the send path if possible,
311 * so we allocate as long as we're allowed to.
313 * We're fussy with enforcing the FMR limit, though. If the driver
314 * tells us we can't use more than N fmrs, we shouldn't start
316 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
319 atomic_dec(&pool->item_count);
322 rds_ib_stats_inc(s_ib_rdma_mr_pool_depleted);
323 return ERR_PTR(-EAGAIN);
326 /* We do have some empty MRs. Flush them out. */
327 rds_ib_stats_inc(s_ib_rdma_mr_pool_wait);
328 rds_ib_flush_mr_pool(pool, 0, &ibmr);
333 ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL, rdsibdev_to_node(rds_ibdev));
339 memset(ibmr, 0, sizeof(*ibmr));
341 ibmr->fmr = ib_alloc_fmr(rds_ibdev->pd,
342 (IB_ACCESS_LOCAL_WRITE |
343 IB_ACCESS_REMOTE_READ |
344 IB_ACCESS_REMOTE_WRITE|
345 IB_ACCESS_REMOTE_ATOMIC),
347 if (IS_ERR(ibmr->fmr)) {
348 err = PTR_ERR(ibmr->fmr);
350 printk(KERN_WARNING "RDS/IB: ib_alloc_fmr failed (err=%d)\n", err);
354 rds_ib_stats_inc(s_ib_rdma_mr_alloc);
360 ib_dealloc_fmr(ibmr->fmr);
363 atomic_dec(&pool->item_count);
367 static int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr,
368 struct scatterlist *sg, unsigned int nents)
370 struct ib_device *dev = rds_ibdev->dev;
371 struct scatterlist *scat = sg;
375 int page_cnt, sg_dma_len;
379 sg_dma_len = ib_dma_map_sg(dev, sg, nents,
381 if (unlikely(!sg_dma_len)) {
382 printk(KERN_WARNING "RDS/IB: dma_map_sg failed!\n");
389 for (i = 0; i < sg_dma_len; ++i) {
390 unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
391 u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
393 if (dma_addr & ~PAGE_MASK) {
399 if ((dma_addr + dma_len) & ~PAGE_MASK) {
400 if (i < sg_dma_len - 1)
409 page_cnt += len >> PAGE_SHIFT;
410 if (page_cnt > fmr_message_size)
413 dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC,
414 rdsibdev_to_node(rds_ibdev));
419 for (i = 0; i < sg_dma_len; ++i) {
420 unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
421 u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
423 for (j = 0; j < dma_len; j += PAGE_SIZE)
424 dma_pages[page_cnt++] =
425 (dma_addr & PAGE_MASK) + j;
428 ret = ib_map_phys_fmr(ibmr->fmr,
429 dma_pages, page_cnt, io_addr);
433 /* Success - we successfully remapped the MR, so we can
434 * safely tear down the old mapping. */
435 rds_ib_teardown_mr(ibmr);
438 ibmr->sg_len = nents;
439 ibmr->sg_dma_len = sg_dma_len;
442 rds_ib_stats_inc(s_ib_rdma_mr_used);
451 void rds_ib_sync_mr(void *trans_private, int direction)
453 struct rds_ib_mr *ibmr = trans_private;
454 struct rds_ib_device *rds_ibdev = ibmr->device;
457 case DMA_FROM_DEVICE:
458 ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
459 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
462 ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
463 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
468 static void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
470 struct rds_ib_device *rds_ibdev = ibmr->device;
472 if (ibmr->sg_dma_len) {
473 ib_dma_unmap_sg(rds_ibdev->dev,
474 ibmr->sg, ibmr->sg_len,
476 ibmr->sg_dma_len = 0;
479 /* Release the s/g list */
483 for (i = 0; i < ibmr->sg_len; ++i) {
484 struct page *page = sg_page(&ibmr->sg[i]);
486 /* FIXME we need a way to tell a r/w MR
488 BUG_ON(irqs_disabled());
489 set_page_dirty(page);
499 static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
501 unsigned int pinned = ibmr->sg_len;
503 __rds_ib_teardown_mr(ibmr);
505 struct rds_ib_device *rds_ibdev = ibmr->device;
506 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
508 atomic_sub(pinned, &pool->free_pinned);
512 static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
514 unsigned int item_count;
516 item_count = atomic_read(&pool->item_count);
524 * given an llist of mrs, put them all into the list_head for more processing
526 static void llist_append_to_list(struct llist_head *llist, struct list_head *list)
528 struct rds_ib_mr *ibmr;
529 struct llist_node *node;
530 struct llist_node *next;
532 node = llist_del_all(llist);
535 ibmr = llist_entry(node, struct rds_ib_mr, llnode);
536 list_add_tail(&ibmr->unmap_list, list);
542 * this takes a list head of mrs and turns it into linked llist nodes
543 * of clusters. Each cluster has linked llist nodes of
544 * MR_CLUSTER_SIZE mrs that are ready for reuse.
546 static void list_to_llist_nodes(struct rds_ib_mr_pool *pool,
547 struct list_head *list,
548 struct llist_node **nodes_head,
549 struct llist_node **nodes_tail)
551 struct rds_ib_mr *ibmr;
552 struct llist_node *cur = NULL;
553 struct llist_node **next = nodes_head;
555 list_for_each_entry(ibmr, list, unmap_list) {
565 * Flush our pool of MRs.
566 * At a minimum, all currently unused MRs are unmapped.
567 * If the number of MRs allocated exceeds the limit, we also try
568 * to free as many MRs as needed to get back to this limit.
570 static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
571 int free_all, struct rds_ib_mr **ibmr_ret)
573 struct rds_ib_mr *ibmr, *next;
574 struct llist_node *clean_nodes;
575 struct llist_node *clean_tail;
576 LIST_HEAD(unmap_list);
578 unsigned long unpinned = 0;
579 unsigned int nfreed = 0, ncleaned = 0, free_goal;
582 rds_ib_stats_inc(s_ib_rdma_mr_pool_flush);
586 while(!mutex_trylock(&pool->flush_lock)) {
587 ibmr = rds_ib_reuse_fmr(pool);
590 finish_wait(&pool->flush_wait, &wait);
594 prepare_to_wait(&pool->flush_wait, &wait,
595 TASK_UNINTERRUPTIBLE);
596 if (llist_empty(&pool->clean_list))
599 ibmr = rds_ib_reuse_fmr(pool);
602 finish_wait(&pool->flush_wait, &wait);
606 finish_wait(&pool->flush_wait, &wait);
608 mutex_lock(&pool->flush_lock);
611 ibmr = rds_ib_reuse_fmr(pool);
618 /* Get the list of all MRs to be dropped. Ordering matters -
619 * we want to put drop_list ahead of free_list.
621 llist_append_to_list(&pool->drop_list, &unmap_list);
622 llist_append_to_list(&pool->free_list, &unmap_list);
624 llist_append_to_list(&pool->clean_list, &unmap_list);
626 free_goal = rds_ib_flush_goal(pool, free_all);
628 if (list_empty(&unmap_list))
631 /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
632 list_for_each_entry(ibmr, &unmap_list, unmap_list)
633 list_add(&ibmr->fmr->list, &fmr_list);
635 ret = ib_unmap_fmr(&fmr_list);
637 printk(KERN_WARNING "RDS/IB: ib_unmap_fmr failed (err=%d)\n", ret);
639 /* Now we can destroy the DMA mapping and unpin any pages */
640 list_for_each_entry_safe(ibmr, next, &unmap_list, unmap_list) {
641 unpinned += ibmr->sg_len;
642 __rds_ib_teardown_mr(ibmr);
643 if (nfreed < free_goal || ibmr->remap_count >= pool->fmr_attr.max_maps) {
644 rds_ib_stats_inc(s_ib_rdma_mr_free);
645 list_del(&ibmr->unmap_list);
646 ib_dealloc_fmr(ibmr->fmr);
653 if (!list_empty(&unmap_list)) {
654 /* we have to make sure that none of the things we're about
655 * to put on the clean list would race with other cpus trying
656 * to pull items off. The llist would explode if we managed to
657 * remove something from the clean list and then add it back again
658 * while another CPU was spinning on that same item in llist_del_first.
660 * This is pretty unlikely, but just in case wait for an llist grace period
661 * here before adding anything back into the clean list.
663 wait_clean_list_grace();
665 list_to_llist_nodes(pool, &unmap_list, &clean_nodes, &clean_tail);
667 *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode);
669 /* more than one entry in llist nodes */
670 if (clean_nodes->next)
671 llist_add_batch(clean_nodes->next, clean_tail, &pool->clean_list);
675 atomic_sub(unpinned, &pool->free_pinned);
676 atomic_sub(ncleaned, &pool->dirty_count);
677 atomic_sub(nfreed, &pool->item_count);
680 mutex_unlock(&pool->flush_lock);
681 if (waitqueue_active(&pool->flush_wait))
682 wake_up(&pool->flush_wait);
687 static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
689 struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
691 rds_ib_flush_mr_pool(pool, 0, NULL);
694 void rds_ib_free_mr(void *trans_private, int invalidate)
696 struct rds_ib_mr *ibmr = trans_private;
697 struct rds_ib_device *rds_ibdev = ibmr->device;
698 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
700 rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
702 /* Return it to the pool's free list */
703 if (ibmr->remap_count >= pool->fmr_attr.max_maps)
704 llist_add(&ibmr->llnode, &pool->drop_list);
706 llist_add(&ibmr->llnode, &pool->free_list);
708 atomic_add(ibmr->sg_len, &pool->free_pinned);
709 atomic_inc(&pool->dirty_count);
711 /* If we've pinned too many pages, request a flush */
712 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
713 atomic_read(&pool->dirty_count) >= pool->max_items / 10)
714 schedule_delayed_work(&pool->flush_worker, 10);
717 if (likely(!in_interrupt())) {
718 rds_ib_flush_mr_pool(pool, 0, NULL);
720 /* We get here if the user created a MR marked
721 * as use_once and invalidate at the same time. */
722 schedule_delayed_work(&pool->flush_worker, 10);
726 rds_ib_dev_put(rds_ibdev);
729 void rds_ib_flush_mrs(void)
731 struct rds_ib_device *rds_ibdev;
733 down_read(&rds_ib_devices_lock);
734 list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
735 struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool;
738 rds_ib_flush_mr_pool(pool, 0, NULL);
740 up_read(&rds_ib_devices_lock);
743 void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
744 struct rds_sock *rs, u32 *key_ret)
746 struct rds_ib_device *rds_ibdev;
747 struct rds_ib_mr *ibmr = NULL;
750 rds_ibdev = rds_ib_get_device(rs->rs_bound_addr);
756 if (!rds_ibdev->mr_pool) {
761 ibmr = rds_ib_alloc_fmr(rds_ibdev);
765 ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents);
767 *key_ret = ibmr->fmr->rkey;
769 printk(KERN_WARNING "RDS/IB: map_fmr failed (errno=%d)\n", ret);
771 ibmr->device = rds_ibdev;
777 rds_ib_free_mr(ibmr, 0);
781 rds_ib_dev_put(rds_ibdev);