2 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. 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>
38 #include "rds_single_path.h"
42 struct workqueue_struct *rds_ib_mr_wq;
43 struct rds_ib_dereg_odp_mr {
44 struct work_struct work;
48 static void rds_ib_odp_mr_worker(struct work_struct *work);
50 static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
52 struct rds_ib_device *rds_ibdev;
53 struct rds_ib_ipaddr *i_ipaddr;
56 list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) {
57 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
58 if (i_ipaddr->ipaddr == ipaddr) {
59 refcount_inc(&rds_ibdev->refcount);
70 static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
72 struct rds_ib_ipaddr *i_ipaddr;
74 i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
78 i_ipaddr->ipaddr = ipaddr;
80 spin_lock_irq(&rds_ibdev->spinlock);
81 list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
82 spin_unlock_irq(&rds_ibdev->spinlock);
87 static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
89 struct rds_ib_ipaddr *i_ipaddr;
90 struct rds_ib_ipaddr *to_free = NULL;
93 spin_lock_irq(&rds_ibdev->spinlock);
94 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
95 if (i_ipaddr->ipaddr == ipaddr) {
96 list_del_rcu(&i_ipaddr->list);
101 spin_unlock_irq(&rds_ibdev->spinlock);
104 kfree_rcu(to_free, rcu);
107 int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev,
108 struct in6_addr *ipaddr)
110 struct rds_ib_device *rds_ibdev_old;
112 rds_ibdev_old = rds_ib_get_device(ipaddr->s6_addr32[3]);
114 return rds_ib_add_ipaddr(rds_ibdev, ipaddr->s6_addr32[3]);
116 if (rds_ibdev_old != rds_ibdev) {
117 rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr->s6_addr32[3]);
118 rds_ib_dev_put(rds_ibdev_old);
119 return rds_ib_add_ipaddr(rds_ibdev, ipaddr->s6_addr32[3]);
121 rds_ib_dev_put(rds_ibdev_old);
126 void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
128 struct rds_ib_connection *ic = conn->c_transport_data;
130 /* conn was previously on the nodev_conns_list */
131 spin_lock_irq(&ib_nodev_conns_lock);
132 BUG_ON(list_empty(&ib_nodev_conns));
133 BUG_ON(list_empty(&ic->ib_node));
134 list_del(&ic->ib_node);
136 spin_lock(&rds_ibdev->spinlock);
137 list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
138 spin_unlock(&rds_ibdev->spinlock);
139 spin_unlock_irq(&ib_nodev_conns_lock);
141 ic->rds_ibdev = rds_ibdev;
142 refcount_inc(&rds_ibdev->refcount);
145 void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
147 struct rds_ib_connection *ic = conn->c_transport_data;
149 /* place conn on nodev_conns_list */
150 spin_lock(&ib_nodev_conns_lock);
152 spin_lock_irq(&rds_ibdev->spinlock);
153 BUG_ON(list_empty(&ic->ib_node));
154 list_del(&ic->ib_node);
155 spin_unlock_irq(&rds_ibdev->spinlock);
157 list_add_tail(&ic->ib_node, &ib_nodev_conns);
159 spin_unlock(&ib_nodev_conns_lock);
161 ic->rds_ibdev = NULL;
162 rds_ib_dev_put(rds_ibdev);
165 void rds_ib_destroy_nodev_conns(void)
167 struct rds_ib_connection *ic, *_ic;
170 /* avoid calling conn_destroy with irqs off */
171 spin_lock_irq(&ib_nodev_conns_lock);
172 list_splice(&ib_nodev_conns, &tmp_list);
173 spin_unlock_irq(&ib_nodev_conns_lock);
175 list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
176 rds_conn_destroy(ic->conn);
179 void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
181 struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
183 iinfo->rdma_mr_max = pool_1m->max_items;
184 iinfo->rdma_mr_size = pool_1m->max_pages;
187 #if IS_ENABLED(CONFIG_IPV6)
188 void rds6_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
189 struct rds6_info_rdma_connection *iinfo6)
191 struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
193 iinfo6->rdma_mr_max = pool_1m->max_items;
194 iinfo6->rdma_mr_size = pool_1m->max_pages;
198 struct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *pool)
200 struct rds_ib_mr *ibmr = NULL;
201 struct llist_node *ret;
204 spin_lock_irqsave(&pool->clean_lock, flags);
205 ret = llist_del_first(&pool->clean_list);
206 spin_unlock_irqrestore(&pool->clean_lock, flags);
208 ibmr = llist_entry(ret, struct rds_ib_mr, llnode);
209 if (pool->pool_type == RDS_IB_MR_8K_POOL)
210 rds_ib_stats_inc(s_ib_rdma_mr_8k_reused);
212 rds_ib_stats_inc(s_ib_rdma_mr_1m_reused);
218 void rds_ib_sync_mr(void *trans_private, int direction)
220 struct rds_ib_mr *ibmr = trans_private;
221 struct rds_ib_device *rds_ibdev = ibmr->device;
227 case DMA_FROM_DEVICE:
228 ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
229 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
232 ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
233 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
238 void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
240 struct rds_ib_device *rds_ibdev = ibmr->device;
242 if (ibmr->sg_dma_len) {
243 ib_dma_unmap_sg(rds_ibdev->dev,
244 ibmr->sg, ibmr->sg_len,
246 ibmr->sg_dma_len = 0;
249 /* Release the s/g list */
253 for (i = 0; i < ibmr->sg_len; ++i) {
254 struct page *page = sg_page(&ibmr->sg[i]);
256 /* FIXME we need a way to tell a r/w MR
258 WARN_ON(!page->mapping && irqs_disabled());
259 set_page_dirty(page);
269 void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
271 unsigned int pinned = ibmr->sg_len;
273 __rds_ib_teardown_mr(ibmr);
275 struct rds_ib_mr_pool *pool = ibmr->pool;
277 atomic_sub(pinned, &pool->free_pinned);
281 static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
283 unsigned int item_count;
285 item_count = atomic_read(&pool->item_count);
293 * given an llist of mrs, put them all into the list_head for more processing
295 static unsigned int llist_append_to_list(struct llist_head *llist,
296 struct list_head *list)
298 struct rds_ib_mr *ibmr;
299 struct llist_node *node;
300 struct llist_node *next;
301 unsigned int count = 0;
303 node = llist_del_all(llist);
306 ibmr = llist_entry(node, struct rds_ib_mr, llnode);
307 list_add_tail(&ibmr->unmap_list, list);
315 * this takes a list head of mrs and turns it into linked llist nodes
316 * of clusters. Each cluster has linked llist nodes of
317 * MR_CLUSTER_SIZE mrs that are ready for reuse.
319 static void list_to_llist_nodes(struct list_head *list,
320 struct llist_node **nodes_head,
321 struct llist_node **nodes_tail)
323 struct rds_ib_mr *ibmr;
324 struct llist_node *cur = NULL;
325 struct llist_node **next = nodes_head;
327 list_for_each_entry(ibmr, list, unmap_list) {
337 * Flush our pool of MRs.
338 * At a minimum, all currently unused MRs are unmapped.
339 * If the number of MRs allocated exceeds the limit, we also try
340 * to free as many MRs as needed to get back to this limit.
342 int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
343 int free_all, struct rds_ib_mr **ibmr_ret)
345 struct rds_ib_mr *ibmr;
346 struct llist_node *clean_nodes;
347 struct llist_node *clean_tail;
348 LIST_HEAD(unmap_list);
349 unsigned long unpinned = 0;
350 unsigned int nfreed = 0, dirty_to_clean = 0, free_goal;
352 if (pool->pool_type == RDS_IB_MR_8K_POOL)
353 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush);
355 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush);
359 while (!mutex_trylock(&pool->flush_lock)) {
360 ibmr = rds_ib_reuse_mr(pool);
363 finish_wait(&pool->flush_wait, &wait);
367 prepare_to_wait(&pool->flush_wait, &wait,
368 TASK_UNINTERRUPTIBLE);
369 if (llist_empty(&pool->clean_list))
372 ibmr = rds_ib_reuse_mr(pool);
375 finish_wait(&pool->flush_wait, &wait);
379 finish_wait(&pool->flush_wait, &wait);
381 mutex_lock(&pool->flush_lock);
384 ibmr = rds_ib_reuse_mr(pool);
391 /* Get the list of all MRs to be dropped. Ordering matters -
392 * we want to put drop_list ahead of free_list.
394 dirty_to_clean = llist_append_to_list(&pool->drop_list, &unmap_list);
395 dirty_to_clean += llist_append_to_list(&pool->free_list, &unmap_list);
399 spin_lock_irqsave(&pool->clean_lock, flags);
400 llist_append_to_list(&pool->clean_list, &unmap_list);
401 spin_unlock_irqrestore(&pool->clean_lock, flags);
404 free_goal = rds_ib_flush_goal(pool, free_all);
406 if (list_empty(&unmap_list))
409 rds_ib_unreg_frmr(&unmap_list, &nfreed, &unpinned, free_goal);
411 if (!list_empty(&unmap_list)) {
414 list_to_llist_nodes(&unmap_list, &clean_nodes, &clean_tail);
416 *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode);
417 clean_nodes = clean_nodes->next;
419 /* more than one entry in llist nodes */
421 spin_lock_irqsave(&pool->clean_lock, flags);
422 llist_add_batch(clean_nodes, clean_tail,
424 spin_unlock_irqrestore(&pool->clean_lock, flags);
428 atomic_sub(unpinned, &pool->free_pinned);
429 atomic_sub(dirty_to_clean, &pool->dirty_count);
430 atomic_sub(nfreed, &pool->item_count);
433 mutex_unlock(&pool->flush_lock);
434 if (waitqueue_active(&pool->flush_wait))
435 wake_up(&pool->flush_wait);
440 struct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *pool)
442 struct rds_ib_mr *ibmr = NULL;
446 ibmr = rds_ib_reuse_mr(pool);
450 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
453 atomic_dec(&pool->item_count);
456 if (pool->pool_type == RDS_IB_MR_8K_POOL)
457 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted);
459 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted);
463 /* We do have some empty MRs. Flush them out. */
464 if (pool->pool_type == RDS_IB_MR_8K_POOL)
465 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait);
467 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait);
469 rds_ib_flush_mr_pool(pool, 0, &ibmr);
477 static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
479 struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
481 rds_ib_flush_mr_pool(pool, 0, NULL);
484 void rds_ib_free_mr(void *trans_private, int invalidate)
486 struct rds_ib_mr *ibmr = trans_private;
487 struct rds_ib_mr_pool *pool = ibmr->pool;
488 struct rds_ib_device *rds_ibdev = ibmr->device;
490 rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
493 /* A MR created and marked as use_once. We use delayed work,
494 * because there is a change that we are in interrupt and can't
495 * call to ib_dereg_mr() directly.
497 INIT_DELAYED_WORK(&ibmr->work, rds_ib_odp_mr_worker);
498 queue_delayed_work(rds_ib_mr_wq, &ibmr->work, 0);
502 /* Return it to the pool's free list */
503 rds_ib_free_frmr_list(ibmr);
505 atomic_add(ibmr->sg_len, &pool->free_pinned);
506 atomic_inc(&pool->dirty_count);
508 /* If we've pinned too many pages, request a flush */
509 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
510 atomic_read(&pool->dirty_count) >= pool->max_items / 5)
511 queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
514 if (likely(!in_interrupt())) {
515 rds_ib_flush_mr_pool(pool, 0, NULL);
517 /* We get here if the user created a MR marked
518 * as use_once and invalidate at the same time.
520 queue_delayed_work(rds_ib_mr_wq,
521 &pool->flush_worker, 10);
525 rds_ib_dev_put(rds_ibdev);
528 void rds_ib_flush_mrs(void)
530 struct rds_ib_device *rds_ibdev;
532 down_read(&rds_ib_devices_lock);
533 list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
534 if (rds_ibdev->mr_8k_pool)
535 rds_ib_flush_mr_pool(rds_ibdev->mr_8k_pool, 0, NULL);
537 if (rds_ibdev->mr_1m_pool)
538 rds_ib_flush_mr_pool(rds_ibdev->mr_1m_pool, 0, NULL);
540 up_read(&rds_ib_devices_lock);
543 u32 rds_ib_get_lkey(void *trans_private)
545 struct rds_ib_mr *ibmr = trans_private;
547 return ibmr->u.mr->lkey;
550 void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
551 struct rds_sock *rs, u32 *key_ret,
552 struct rds_connection *conn,
553 u64 start, u64 length, int need_odp)
555 struct rds_ib_device *rds_ibdev;
556 struct rds_ib_mr *ibmr = NULL;
557 struct rds_ib_connection *ic = NULL;
560 rds_ibdev = rds_ib_get_device(rs->rs_bound_addr.s6_addr32[3]);
566 if (need_odp == ODP_ZEROBASED || need_odp == ODP_VIRTUAL) {
567 u64 virt_addr = need_odp == ODP_ZEROBASED ? 0 : start;
569 (IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
570 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_ATOMIC |
571 IB_ACCESS_ON_DEMAND);
572 struct ib_sge sge = {};
575 if (!rds_ibdev->odp_capable) {
580 ib_mr = ib_reg_user_mr(rds_ibdev->pd, start, length, virt_addr,
584 rdsdebug("rds_ib_get_user_mr returned %d\n",
586 ret = PTR_ERR(ib_mr);
590 *key_ret = ib_mr->rkey;
592 ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL);
601 sge.addr = virt_addr;
603 sge.lkey = ib_mr->lkey;
605 ib_advise_mr(rds_ibdev->pd,
606 IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH_WRITE,
607 IB_UVERBS_ADVISE_MR_FLAG_FLUSH, &sge, 1);
612 ic = conn->c_transport_data;
614 if (!rds_ibdev->mr_8k_pool || !rds_ibdev->mr_1m_pool) {
619 ibmr = rds_ib_reg_frmr(rds_ibdev, ic, sg, nents, key_ret);
622 pr_warn("RDS/IB: rds_ib_get_mr failed (errno=%d)\n", ret);
629 rds_ib_dev_put(rds_ibdev);
634 void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
636 cancel_delayed_work_sync(&pool->flush_worker);
637 rds_ib_flush_mr_pool(pool, 1, NULL);
638 WARN_ON(atomic_read(&pool->item_count));
639 WARN_ON(atomic_read(&pool->free_pinned));
643 struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev,
646 struct rds_ib_mr_pool *pool;
648 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
650 return ERR_PTR(-ENOMEM);
652 pool->pool_type = pool_type;
653 init_llist_head(&pool->free_list);
654 init_llist_head(&pool->drop_list);
655 init_llist_head(&pool->clean_list);
656 spin_lock_init(&pool->clean_lock);
657 mutex_init(&pool->flush_lock);
658 init_waitqueue_head(&pool->flush_wait);
659 INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
661 if (pool_type == RDS_IB_MR_1M_POOL) {
662 /* +1 allows for unaligned MRs */
663 pool->max_pages = RDS_MR_1M_MSG_SIZE + 1;
664 pool->max_items = rds_ibdev->max_1m_mrs;
666 /* pool_type == RDS_IB_MR_8K_POOL */
667 pool->max_pages = RDS_MR_8K_MSG_SIZE + 1;
668 pool->max_items = rds_ibdev->max_8k_mrs;
671 pool->max_free_pinned = pool->max_items * pool->max_pages / 4;
672 pool->max_items_soft = rds_ibdev->max_mrs * 3 / 4;
677 int rds_ib_mr_init(void)
679 rds_ib_mr_wq = alloc_workqueue("rds_mr_flushd", WQ_MEM_RECLAIM, 0);
685 /* By the time this is called all the IB devices should have been torn down and
686 * had their pools freed. As each pool is freed its work struct is waited on,
687 * so the pool flushing work queue should be idle by the time we get here.
689 void rds_ib_mr_exit(void)
691 destroy_workqueue(rds_ib_mr_wq);
694 static void rds_ib_odp_mr_worker(struct work_struct *work)
696 struct rds_ib_mr *ibmr;
698 ibmr = container_of(work, struct rds_ib_mr, work.work);
699 ib_dereg_mr(ibmr->u.mr);