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"
41 struct workqueue_struct
*rds_ib_mr_wq
;
43 static DEFINE_PER_CPU(unsigned long, clean_list_grace
);
44 #define CLEAN_LIST_BUSY_BIT 0
46 static struct rds_ib_device
*rds_ib_get_device(__be32 ipaddr
)
48 struct rds_ib_device
*rds_ibdev
;
49 struct rds_ib_ipaddr
*i_ipaddr
;
52 list_for_each_entry_rcu(rds_ibdev
, &rds_ib_devices
, list
) {
53 list_for_each_entry_rcu(i_ipaddr
, &rds_ibdev
->ipaddr_list
, list
) {
54 if (i_ipaddr
->ipaddr
== ipaddr
) {
55 refcount_inc(&rds_ibdev
->refcount
);
66 static int rds_ib_add_ipaddr(struct rds_ib_device
*rds_ibdev
, __be32 ipaddr
)
68 struct rds_ib_ipaddr
*i_ipaddr
;
70 i_ipaddr
= kmalloc(sizeof *i_ipaddr
, GFP_KERNEL
);
74 i_ipaddr
->ipaddr
= ipaddr
;
76 spin_lock_irq(&rds_ibdev
->spinlock
);
77 list_add_tail_rcu(&i_ipaddr
->list
, &rds_ibdev
->ipaddr_list
);
78 spin_unlock_irq(&rds_ibdev
->spinlock
);
83 static void rds_ib_remove_ipaddr(struct rds_ib_device
*rds_ibdev
, __be32 ipaddr
)
85 struct rds_ib_ipaddr
*i_ipaddr
;
86 struct rds_ib_ipaddr
*to_free
= NULL
;
89 spin_lock_irq(&rds_ibdev
->spinlock
);
90 list_for_each_entry_rcu(i_ipaddr
, &rds_ibdev
->ipaddr_list
, list
) {
91 if (i_ipaddr
->ipaddr
== ipaddr
) {
92 list_del_rcu(&i_ipaddr
->list
);
97 spin_unlock_irq(&rds_ibdev
->spinlock
);
100 kfree_rcu(to_free
, rcu
);
103 int rds_ib_update_ipaddr(struct rds_ib_device
*rds_ibdev
,
104 struct in6_addr
*ipaddr
)
106 struct rds_ib_device
*rds_ibdev_old
;
108 rds_ibdev_old
= rds_ib_get_device(ipaddr
->s6_addr32
[3]);
110 return rds_ib_add_ipaddr(rds_ibdev
, ipaddr
->s6_addr32
[3]);
112 if (rds_ibdev_old
!= rds_ibdev
) {
113 rds_ib_remove_ipaddr(rds_ibdev_old
, ipaddr
->s6_addr32
[3]);
114 rds_ib_dev_put(rds_ibdev_old
);
115 return rds_ib_add_ipaddr(rds_ibdev
, ipaddr
->s6_addr32
[3]);
117 rds_ib_dev_put(rds_ibdev_old
);
122 void rds_ib_add_conn(struct rds_ib_device
*rds_ibdev
, struct rds_connection
*conn
)
124 struct rds_ib_connection
*ic
= conn
->c_transport_data
;
126 /* conn was previously on the nodev_conns_list */
127 spin_lock_irq(&ib_nodev_conns_lock
);
128 BUG_ON(list_empty(&ib_nodev_conns
));
129 BUG_ON(list_empty(&ic
->ib_node
));
130 list_del(&ic
->ib_node
);
132 spin_lock(&rds_ibdev
->spinlock
);
133 list_add_tail(&ic
->ib_node
, &rds_ibdev
->conn_list
);
134 spin_unlock(&rds_ibdev
->spinlock
);
135 spin_unlock_irq(&ib_nodev_conns_lock
);
137 ic
->rds_ibdev
= rds_ibdev
;
138 refcount_inc(&rds_ibdev
->refcount
);
141 void rds_ib_remove_conn(struct rds_ib_device
*rds_ibdev
, struct rds_connection
*conn
)
143 struct rds_ib_connection
*ic
= conn
->c_transport_data
;
145 /* place conn on nodev_conns_list */
146 spin_lock(&ib_nodev_conns_lock
);
148 spin_lock_irq(&rds_ibdev
->spinlock
);
149 BUG_ON(list_empty(&ic
->ib_node
));
150 list_del(&ic
->ib_node
);
151 spin_unlock_irq(&rds_ibdev
->spinlock
);
153 list_add_tail(&ic
->ib_node
, &ib_nodev_conns
);
155 spin_unlock(&ib_nodev_conns_lock
);
157 ic
->rds_ibdev
= NULL
;
158 rds_ib_dev_put(rds_ibdev
);
161 void rds_ib_destroy_nodev_conns(void)
163 struct rds_ib_connection
*ic
, *_ic
;
166 /* avoid calling conn_destroy with irqs off */
167 spin_lock_irq(&ib_nodev_conns_lock
);
168 list_splice(&ib_nodev_conns
, &tmp_list
);
169 spin_unlock_irq(&ib_nodev_conns_lock
);
171 list_for_each_entry_safe(ic
, _ic
, &tmp_list
, ib_node
)
172 rds_conn_destroy(ic
->conn
);
175 void rds_ib_get_mr_info(struct rds_ib_device
*rds_ibdev
, struct rds_info_rdma_connection
*iinfo
)
177 struct rds_ib_mr_pool
*pool_1m
= rds_ibdev
->mr_1m_pool
;
179 iinfo
->rdma_mr_max
= pool_1m
->max_items
;
180 iinfo
->rdma_mr_size
= pool_1m
->fmr_attr
.max_pages
;
183 #if IS_ENABLED(CONFIG_IPV6)
184 void rds6_ib_get_mr_info(struct rds_ib_device
*rds_ibdev
,
185 struct rds6_info_rdma_connection
*iinfo6
)
187 struct rds_ib_mr_pool
*pool_1m
= rds_ibdev
->mr_1m_pool
;
189 iinfo6
->rdma_mr_max
= pool_1m
->max_items
;
190 iinfo6
->rdma_mr_size
= pool_1m
->fmr_attr
.max_pages
;
194 struct rds_ib_mr
*rds_ib_reuse_mr(struct rds_ib_mr_pool
*pool
)
196 struct rds_ib_mr
*ibmr
= NULL
;
197 struct llist_node
*ret
;
201 flag
= this_cpu_ptr(&clean_list_grace
);
202 set_bit(CLEAN_LIST_BUSY_BIT
, flag
);
203 ret
= llist_del_first(&pool
->clean_list
);
205 ibmr
= llist_entry(ret
, struct rds_ib_mr
, llnode
);
206 if (pool
->pool_type
== RDS_IB_MR_8K_POOL
)
207 rds_ib_stats_inc(s_ib_rdma_mr_8k_reused
);
209 rds_ib_stats_inc(s_ib_rdma_mr_1m_reused
);
212 clear_bit(CLEAN_LIST_BUSY_BIT
, flag
);
217 static inline void wait_clean_list_grace(void)
222 for_each_online_cpu(cpu
) {
223 flag
= &per_cpu(clean_list_grace
, cpu
);
224 while (test_bit(CLEAN_LIST_BUSY_BIT
, flag
))
229 void rds_ib_sync_mr(void *trans_private
, int direction
)
231 struct rds_ib_mr
*ibmr
= trans_private
;
232 struct rds_ib_device
*rds_ibdev
= ibmr
->device
;
235 case DMA_FROM_DEVICE
:
236 ib_dma_sync_sg_for_cpu(rds_ibdev
->dev
, ibmr
->sg
,
237 ibmr
->sg_dma_len
, DMA_BIDIRECTIONAL
);
240 ib_dma_sync_sg_for_device(rds_ibdev
->dev
, ibmr
->sg
,
241 ibmr
->sg_dma_len
, DMA_BIDIRECTIONAL
);
246 void __rds_ib_teardown_mr(struct rds_ib_mr
*ibmr
)
248 struct rds_ib_device
*rds_ibdev
= ibmr
->device
;
250 if (ibmr
->sg_dma_len
) {
251 ib_dma_unmap_sg(rds_ibdev
->dev
,
252 ibmr
->sg
, ibmr
->sg_len
,
254 ibmr
->sg_dma_len
= 0;
257 /* Release the s/g list */
261 for (i
= 0; i
< ibmr
->sg_len
; ++i
) {
262 struct page
*page
= sg_page(&ibmr
->sg
[i
]);
264 /* FIXME we need a way to tell a r/w MR
266 WARN_ON(!page
->mapping
&& irqs_disabled());
267 set_page_dirty(page
);
277 void rds_ib_teardown_mr(struct rds_ib_mr
*ibmr
)
279 unsigned int pinned
= ibmr
->sg_len
;
281 __rds_ib_teardown_mr(ibmr
);
283 struct rds_ib_mr_pool
*pool
= ibmr
->pool
;
285 atomic_sub(pinned
, &pool
->free_pinned
);
289 static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool
*pool
, int free_all
)
291 unsigned int item_count
;
293 item_count
= atomic_read(&pool
->item_count
);
301 * given an llist of mrs, put them all into the list_head for more processing
303 static unsigned int llist_append_to_list(struct llist_head
*llist
,
304 struct list_head
*list
)
306 struct rds_ib_mr
*ibmr
;
307 struct llist_node
*node
;
308 struct llist_node
*next
;
309 unsigned int count
= 0;
311 node
= llist_del_all(llist
);
314 ibmr
= llist_entry(node
, struct rds_ib_mr
, llnode
);
315 list_add_tail(&ibmr
->unmap_list
, list
);
323 * this takes a list head of mrs and turns it into linked llist nodes
324 * of clusters. Each cluster has linked llist nodes of
325 * MR_CLUSTER_SIZE mrs that are ready for reuse.
327 static void list_to_llist_nodes(struct rds_ib_mr_pool
*pool
,
328 struct list_head
*list
,
329 struct llist_node
**nodes_head
,
330 struct llist_node
**nodes_tail
)
332 struct rds_ib_mr
*ibmr
;
333 struct llist_node
*cur
= NULL
;
334 struct llist_node
**next
= nodes_head
;
336 list_for_each_entry(ibmr
, list
, unmap_list
) {
346 * Flush our pool of MRs.
347 * At a minimum, all currently unused MRs are unmapped.
348 * If the number of MRs allocated exceeds the limit, we also try
349 * to free as many MRs as needed to get back to this limit.
351 int rds_ib_flush_mr_pool(struct rds_ib_mr_pool
*pool
,
352 int free_all
, struct rds_ib_mr
**ibmr_ret
)
354 struct rds_ib_mr
*ibmr
;
355 struct llist_node
*clean_nodes
;
356 struct llist_node
*clean_tail
;
357 LIST_HEAD(unmap_list
);
358 unsigned long unpinned
= 0;
359 unsigned int nfreed
= 0, dirty_to_clean
= 0, free_goal
;
361 if (pool
->pool_type
== RDS_IB_MR_8K_POOL
)
362 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush
);
364 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush
);
368 while (!mutex_trylock(&pool
->flush_lock
)) {
369 ibmr
= rds_ib_reuse_mr(pool
);
372 finish_wait(&pool
->flush_wait
, &wait
);
376 prepare_to_wait(&pool
->flush_wait
, &wait
,
377 TASK_UNINTERRUPTIBLE
);
378 if (llist_empty(&pool
->clean_list
))
381 ibmr
= rds_ib_reuse_mr(pool
);
384 finish_wait(&pool
->flush_wait
, &wait
);
388 finish_wait(&pool
->flush_wait
, &wait
);
390 mutex_lock(&pool
->flush_lock
);
393 ibmr
= rds_ib_reuse_mr(pool
);
400 /* Get the list of all MRs to be dropped. Ordering matters -
401 * we want to put drop_list ahead of free_list.
403 dirty_to_clean
= llist_append_to_list(&pool
->drop_list
, &unmap_list
);
404 dirty_to_clean
+= llist_append_to_list(&pool
->free_list
, &unmap_list
);
406 llist_append_to_list(&pool
->clean_list
, &unmap_list
);
408 free_goal
= rds_ib_flush_goal(pool
, free_all
);
410 if (list_empty(&unmap_list
))
413 if (pool
->use_fastreg
)
414 rds_ib_unreg_frmr(&unmap_list
, &nfreed
, &unpinned
, free_goal
);
416 rds_ib_unreg_fmr(&unmap_list
, &nfreed
, &unpinned
, free_goal
);
418 if (!list_empty(&unmap_list
)) {
419 /* we have to make sure that none of the things we're about
420 * to put on the clean list would race with other cpus trying
421 * to pull items off. The llist would explode if we managed to
422 * remove something from the clean list and then add it back again
423 * while another CPU was spinning on that same item in llist_del_first.
425 * This is pretty unlikely, but just in case wait for an llist grace period
426 * here before adding anything back into the clean list.
428 wait_clean_list_grace();
430 list_to_llist_nodes(pool
, &unmap_list
, &clean_nodes
, &clean_tail
);
432 *ibmr_ret
= llist_entry(clean_nodes
, struct rds_ib_mr
, llnode
);
433 clean_nodes
= clean_nodes
->next
;
435 /* more than one entry in llist nodes */
437 llist_add_batch(clean_nodes
, clean_tail
,
442 atomic_sub(unpinned
, &pool
->free_pinned
);
443 atomic_sub(dirty_to_clean
, &pool
->dirty_count
);
444 atomic_sub(nfreed
, &pool
->item_count
);
447 mutex_unlock(&pool
->flush_lock
);
448 if (waitqueue_active(&pool
->flush_wait
))
449 wake_up(&pool
->flush_wait
);
454 struct rds_ib_mr
*rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool
*pool
)
456 struct rds_ib_mr
*ibmr
= NULL
;
460 ibmr
= rds_ib_reuse_mr(pool
);
464 if (atomic_inc_return(&pool
->item_count
) <= pool
->max_items
)
467 atomic_dec(&pool
->item_count
);
470 if (pool
->pool_type
== RDS_IB_MR_8K_POOL
)
471 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted
);
473 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted
);
474 return ERR_PTR(-EAGAIN
);
477 /* We do have some empty MRs. Flush them out. */
478 if (pool
->pool_type
== RDS_IB_MR_8K_POOL
)
479 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait
);
481 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait
);
483 rds_ib_flush_mr_pool(pool
, 0, &ibmr
);
491 static void rds_ib_mr_pool_flush_worker(struct work_struct
*work
)
493 struct rds_ib_mr_pool
*pool
= container_of(work
, struct rds_ib_mr_pool
, flush_worker
.work
);
495 rds_ib_flush_mr_pool(pool
, 0, NULL
);
498 void rds_ib_free_mr(void *trans_private
, int invalidate
)
500 struct rds_ib_mr
*ibmr
= trans_private
;
501 struct rds_ib_mr_pool
*pool
= ibmr
->pool
;
502 struct rds_ib_device
*rds_ibdev
= ibmr
->device
;
504 rdsdebug("RDS/IB: free_mr nents %u\n", ibmr
->sg_len
);
506 /* Return it to the pool's free list */
507 if (rds_ibdev
->use_fastreg
)
508 rds_ib_free_frmr_list(ibmr
);
510 rds_ib_free_fmr_list(ibmr
);
512 atomic_add(ibmr
->sg_len
, &pool
->free_pinned
);
513 atomic_inc(&pool
->dirty_count
);
515 /* If we've pinned too many pages, request a flush */
516 if (atomic_read(&pool
->free_pinned
) >= pool
->max_free_pinned
||
517 atomic_read(&pool
->dirty_count
) >= pool
->max_items
/ 5)
518 queue_delayed_work(rds_ib_mr_wq
, &pool
->flush_worker
, 10);
521 if (likely(!in_interrupt())) {
522 rds_ib_flush_mr_pool(pool
, 0, NULL
);
524 /* We get here if the user created a MR marked
525 * as use_once and invalidate at the same time.
527 queue_delayed_work(rds_ib_mr_wq
,
528 &pool
->flush_worker
, 10);
532 rds_ib_dev_put(rds_ibdev
);
535 void rds_ib_flush_mrs(void)
537 struct rds_ib_device
*rds_ibdev
;
539 down_read(&rds_ib_devices_lock
);
540 list_for_each_entry(rds_ibdev
, &rds_ib_devices
, list
) {
541 if (rds_ibdev
->mr_8k_pool
)
542 rds_ib_flush_mr_pool(rds_ibdev
->mr_8k_pool
, 0, NULL
);
544 if (rds_ibdev
->mr_1m_pool
)
545 rds_ib_flush_mr_pool(rds_ibdev
->mr_1m_pool
, 0, NULL
);
547 up_read(&rds_ib_devices_lock
);
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
)
554 struct rds_ib_device
*rds_ibdev
;
555 struct rds_ib_mr
*ibmr
= NULL
;
556 struct rds_ib_connection
*ic
= NULL
;
559 rds_ibdev
= rds_ib_get_device(rs
->rs_bound_addr
.s6_addr32
[3]);
566 ic
= conn
->c_transport_data
;
568 if (!rds_ibdev
->mr_8k_pool
|| !rds_ibdev
->mr_1m_pool
) {
573 if (rds_ibdev
->use_fastreg
)
574 ibmr
= rds_ib_reg_frmr(rds_ibdev
, ic
, sg
, nents
, key_ret
);
576 ibmr
= rds_ib_reg_fmr(rds_ibdev
, sg
, nents
, key_ret
);
579 pr_warn("RDS/IB: rds_ib_get_mr failed (errno=%d)\n", ret
);
586 rds_ib_dev_put(rds_ibdev
);
591 void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool
*pool
)
593 cancel_delayed_work_sync(&pool
->flush_worker
);
594 rds_ib_flush_mr_pool(pool
, 1, NULL
);
595 WARN_ON(atomic_read(&pool
->item_count
));
596 WARN_ON(atomic_read(&pool
->free_pinned
));
600 struct rds_ib_mr_pool
*rds_ib_create_mr_pool(struct rds_ib_device
*rds_ibdev
,
603 struct rds_ib_mr_pool
*pool
;
605 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
607 return ERR_PTR(-ENOMEM
);
609 pool
->pool_type
= pool_type
;
610 init_llist_head(&pool
->free_list
);
611 init_llist_head(&pool
->drop_list
);
612 init_llist_head(&pool
->clean_list
);
613 mutex_init(&pool
->flush_lock
);
614 init_waitqueue_head(&pool
->flush_wait
);
615 INIT_DELAYED_WORK(&pool
->flush_worker
, rds_ib_mr_pool_flush_worker
);
617 if (pool_type
== RDS_IB_MR_1M_POOL
) {
618 /* +1 allows for unaligned MRs */
619 pool
->fmr_attr
.max_pages
= RDS_MR_1M_MSG_SIZE
+ 1;
620 pool
->max_items
= rds_ibdev
->max_1m_mrs
;
622 /* pool_type == RDS_IB_MR_8K_POOL */
623 pool
->fmr_attr
.max_pages
= RDS_MR_8K_MSG_SIZE
+ 1;
624 pool
->max_items
= rds_ibdev
->max_8k_mrs
;
627 pool
->max_free_pinned
= pool
->max_items
* pool
->fmr_attr
.max_pages
/ 4;
628 pool
->fmr_attr
.max_maps
= rds_ibdev
->fmr_max_remaps
;
629 pool
->fmr_attr
.page_shift
= PAGE_SHIFT
;
630 pool
->max_items_soft
= rds_ibdev
->max_mrs
* 3 / 4;
631 pool
->use_fastreg
= rds_ibdev
->use_fastreg
;
636 int rds_ib_mr_init(void)
638 rds_ib_mr_wq
= alloc_workqueue("rds_mr_flushd", WQ_MEM_RECLAIM
, 0);
644 /* By the time this is called all the IB devices should have been torn down and
645 * had their pools freed. As each pool is freed its work struct is waited on,
646 * so the pool flushing work queue should be idle by the time we get here.
648 void rds_ib_mr_exit(void)
650 destroy_workqueue(rds_ib_mr_wq
);