Merge tag 'pstore-v4.19-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[linux-block.git] / net / rds / ib_rdma.c
CommitLineData
08b48a1e 1/*
b7ff8b10 2 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
08b48a1e
AG
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>
5a0e3ad6 34#include <linux/slab.h>
764f2dd9 35#include <linux/rculist.h>
1bc144b6 36#include <linux/llist.h>
08b48a1e 37
0cb43965 38#include "rds_single_path.h"
f6df683f 39#include "ib_mr.h"
40
41struct workqueue_struct *rds_ib_mr_wq;
08b48a1e 42
6fa70da6
CM
43static DEFINE_PER_CPU(unsigned long, clean_list_grace);
44#define CLEAN_LIST_BUSY_BIT 0
08b48a1e 45
08b48a1e
AG
46static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
47{
48 struct rds_ib_device *rds_ibdev;
49 struct rds_ib_ipaddr *i_ipaddr;
50
ea819867
ZB
51 rcu_read_lock();
52 list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) {
764f2dd9 53 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
08b48a1e 54 if (i_ipaddr->ipaddr == ipaddr) {
50d61ff7 55 refcount_inc(&rds_ibdev->refcount);
764f2dd9 56 rcu_read_unlock();
08b48a1e
AG
57 return rds_ibdev;
58 }
59 }
08b48a1e 60 }
ea819867 61 rcu_read_unlock();
08b48a1e
AG
62
63 return NULL;
64}
65
66static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
67{
68 struct rds_ib_ipaddr *i_ipaddr;
69
70 i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
71 if (!i_ipaddr)
72 return -ENOMEM;
73
74 i_ipaddr->ipaddr = ipaddr;
75
76 spin_lock_irq(&rds_ibdev->spinlock);
764f2dd9 77 list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
08b48a1e
AG
78 spin_unlock_irq(&rds_ibdev->spinlock);
79
80 return 0;
81}
82
83static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
84{
4a81802b 85 struct rds_ib_ipaddr *i_ipaddr;
764f2dd9
CM
86 struct rds_ib_ipaddr *to_free = NULL;
87
08b48a1e
AG
88
89 spin_lock_irq(&rds_ibdev->spinlock);
764f2dd9 90 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
08b48a1e 91 if (i_ipaddr->ipaddr == ipaddr) {
764f2dd9
CM
92 list_del_rcu(&i_ipaddr->list);
93 to_free = i_ipaddr;
08b48a1e
AG
94 break;
95 }
96 }
97 spin_unlock_irq(&rds_ibdev->spinlock);
764f2dd9 98
59fe4606
SS
99 if (to_free)
100 kfree_rcu(to_free, rcu);
08b48a1e
AG
101}
102
eee2fa6a
KCP
103int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev,
104 struct in6_addr *ipaddr)
08b48a1e
AG
105{
106 struct rds_ib_device *rds_ibdev_old;
107
eee2fa6a 108 rds_ibdev_old = rds_ib_get_device(ipaddr->s6_addr32[3]);
e1f475a7 109 if (!rds_ibdev_old)
eee2fa6a 110 return rds_ib_add_ipaddr(rds_ibdev, ipaddr->s6_addr32[3]);
e1f475a7 111
112 if (rds_ibdev_old != rds_ibdev) {
eee2fa6a 113 rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr->s6_addr32[3]);
3e0249f9 114 rds_ib_dev_put(rds_ibdev_old);
eee2fa6a 115 return rds_ib_add_ipaddr(rds_ibdev, ipaddr->s6_addr32[3]);
3e0249f9 116 }
e1f475a7 117 rds_ib_dev_put(rds_ibdev_old);
08b48a1e 118
e1f475a7 119 return 0;
08b48a1e
AG
120}
121
745cbcca 122void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
08b48a1e
AG
123{
124 struct rds_ib_connection *ic = conn->c_transport_data;
125
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);
08b48a1e 131
aef3ea33 132 spin_lock(&rds_ibdev->spinlock);
08b48a1e 133 list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
aef3ea33 134 spin_unlock(&rds_ibdev->spinlock);
745cbcca 135 spin_unlock_irq(&ib_nodev_conns_lock);
08b48a1e
AG
136
137 ic->rds_ibdev = rds_ibdev;
50d61ff7 138 refcount_inc(&rds_ibdev->refcount);
08b48a1e
AG
139}
140
745cbcca 141void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
08b48a1e 142{
745cbcca 143 struct rds_ib_connection *ic = conn->c_transport_data;
08b48a1e 144
745cbcca
AG
145 /* place conn on nodev_conns_list */
146 spin_lock(&ib_nodev_conns_lock);
08b48a1e 147
745cbcca
AG
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);
152
153 list_add_tail(&ic->ib_node, &ib_nodev_conns);
154
155 spin_unlock(&ib_nodev_conns_lock);
156
157 ic->rds_ibdev = NULL;
3e0249f9 158 rds_ib_dev_put(rds_ibdev);
08b48a1e
AG
159}
160
8aeb1ba6 161void rds_ib_destroy_nodev_conns(void)
08b48a1e
AG
162{
163 struct rds_ib_connection *ic, *_ic;
164 LIST_HEAD(tmp_list);
165
166 /* avoid calling conn_destroy with irqs off */
8aeb1ba6
ZB
167 spin_lock_irq(&ib_nodev_conns_lock);
168 list_splice(&ib_nodev_conns, &tmp_list);
169 spin_unlock_irq(&ib_nodev_conns_lock);
08b48a1e 170
433d308d 171 list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
08b48a1e 172 rds_conn_destroy(ic->conn);
08b48a1e
AG
173}
174
08b48a1e
AG
175void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
176{
06766513 177 struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
08b48a1e 178
06766513
SS
179 iinfo->rdma_mr_max = pool_1m->max_items;
180 iinfo->rdma_mr_size = pool_1m->fmr_attr.max_pages;
08b48a1e
AG
181}
182
e65d4d96 183#if IS_ENABLED(CONFIG_IPV6)
b7ff8b10
KCP
184void rds6_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
185 struct rds6_info_rdma_connection *iinfo6)
186{
187 struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
188
189 iinfo6->rdma_mr_max = pool_1m->max_items;
190 iinfo6->rdma_mr_size = pool_1m->fmr_attr.max_pages;
191}
e65d4d96 192#endif
b7ff8b10 193
f6df683f 194struct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *pool)
08b48a1e
AG
195{
196 struct rds_ib_mr *ibmr = NULL;
1bc144b6 197 struct llist_node *ret;
6fa70da6 198 unsigned long *flag;
08b48a1e 199
6fa70da6 200 preempt_disable();
903ceff7 201 flag = this_cpu_ptr(&clean_list_grace);
6fa70da6 202 set_bit(CLEAN_LIST_BUSY_BIT, flag);
1bc144b6 203 ret = llist_del_first(&pool->clean_list);
db42753a 204 if (ret) {
1bc144b6 205 ibmr = llist_entry(ret, struct rds_ib_mr, llnode);
db42753a 206 if (pool->pool_type == RDS_IB_MR_8K_POOL)
207 rds_ib_stats_inc(s_ib_rdma_mr_8k_reused);
208 else
209 rds_ib_stats_inc(s_ib_rdma_mr_1m_reused);
210 }
08b48a1e 211
6fa70da6
CM
212 clear_bit(CLEAN_LIST_BUSY_BIT, flag);
213 preempt_enable();
08b48a1e
AG
214 return ibmr;
215}
216
6fa70da6
CM
217static inline void wait_clean_list_grace(void)
218{
219 int cpu;
220 unsigned long *flag;
221
222 for_each_online_cpu(cpu) {
223 flag = &per_cpu(clean_list_grace, cpu);
224 while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
225 cpu_relax();
226 }
227}
228
08b48a1e
AG
229void rds_ib_sync_mr(void *trans_private, int direction)
230{
231 struct rds_ib_mr *ibmr = trans_private;
232 struct rds_ib_device *rds_ibdev = ibmr->device;
233
234 switch (direction) {
235 case DMA_FROM_DEVICE:
236 ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
237 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
238 break;
239 case DMA_TO_DEVICE:
240 ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
241 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
242 break;
243 }
244}
245
f6df683f 246void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
08b48a1e
AG
247{
248 struct rds_ib_device *rds_ibdev = ibmr->device;
249
250 if (ibmr->sg_dma_len) {
251 ib_dma_unmap_sg(rds_ibdev->dev,
252 ibmr->sg, ibmr->sg_len,
253 DMA_BIDIRECTIONAL);
254 ibmr->sg_dma_len = 0;
255 }
256
257 /* Release the s/g list */
258 if (ibmr->sg_len) {
259 unsigned int i;
260
261 for (i = 0; i < ibmr->sg_len; ++i) {
262 struct page *page = sg_page(&ibmr->sg[i]);
263
264 /* FIXME we need a way to tell a r/w MR
265 * from a r/o MR */
5c240fa2 266 WARN_ON(!page->mapping && irqs_disabled());
08b48a1e
AG
267 set_page_dirty(page);
268 put_page(page);
269 }
270 kfree(ibmr->sg);
271
272 ibmr->sg = NULL;
273 ibmr->sg_len = 0;
274 }
275}
276
f6df683f 277void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
08b48a1e
AG
278{
279 unsigned int pinned = ibmr->sg_len;
280
281 __rds_ib_teardown_mr(ibmr);
282 if (pinned) {
26139dc1 283 struct rds_ib_mr_pool *pool = ibmr->pool;
08b48a1e
AG
284
285 atomic_sub(pinned, &pool->free_pinned);
286 }
287}
288
289static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
290{
291 unsigned int item_count;
292
293 item_count = atomic_read(&pool->item_count);
294 if (free_all)
295 return item_count;
296
297 return 0;
298}
299
6fa70da6 300/*
1bc144b6 301 * given an llist of mrs, put them all into the list_head for more processing
6fa70da6 302 */
6116c203
WW
303static unsigned int llist_append_to_list(struct llist_head *llist,
304 struct list_head *list)
6fa70da6
CM
305{
306 struct rds_ib_mr *ibmr;
1bc144b6
HY
307 struct llist_node *node;
308 struct llist_node *next;
6116c203 309 unsigned int count = 0;
1bc144b6
HY
310
311 node = llist_del_all(llist);
312 while (node) {
313 next = node->next;
314 ibmr = llist_entry(node, struct rds_ib_mr, llnode);
6fa70da6 315 list_add_tail(&ibmr->unmap_list, list);
1bc144b6 316 node = next;
6116c203 317 count++;
6fa70da6 318 }
6116c203 319 return count;
6fa70da6
CM
320}
321
322/*
1bc144b6
HY
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.
6fa70da6 326 */
1bc144b6
HY
327static 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)
6fa70da6
CM
331{
332 struct rds_ib_mr *ibmr;
1bc144b6
HY
333 struct llist_node *cur = NULL;
334 struct llist_node **next = nodes_head;
6fa70da6
CM
335
336 list_for_each_entry(ibmr, list, unmap_list) {
1bc144b6
HY
337 cur = &ibmr->llnode;
338 *next = cur;
339 next = &cur->next;
6fa70da6 340 }
1bc144b6
HY
341 *next = NULL;
342 *nodes_tail = cur;
6fa70da6
CM
343}
344
08b48a1e
AG
345/*
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.
350 */
f6df683f 351int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
352 int free_all, struct rds_ib_mr **ibmr_ret)
08b48a1e 353{
490ea596 354 struct rds_ib_mr *ibmr;
1bc144b6
HY
355 struct llist_node *clean_nodes;
356 struct llist_node *clean_tail;
08b48a1e 357 LIST_HEAD(unmap_list);
08b48a1e 358 unsigned long unpinned = 0;
6116c203 359 unsigned int nfreed = 0, dirty_to_clean = 0, free_goal;
08b48a1e 360
06766513
SS
361 if (pool->pool_type == RDS_IB_MR_8K_POOL)
362 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush);
363 else
364 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush);
08b48a1e 365
6fa70da6
CM
366 if (ibmr_ret) {
367 DEFINE_WAIT(wait);
06766513 368 while (!mutex_trylock(&pool->flush_lock)) {
f6df683f 369 ibmr = rds_ib_reuse_mr(pool);
6fa70da6
CM
370 if (ibmr) {
371 *ibmr_ret = ibmr;
372 finish_wait(&pool->flush_wait, &wait);
373 goto out_nolock;
374 }
375
376 prepare_to_wait(&pool->flush_wait, &wait,
377 TASK_UNINTERRUPTIBLE);
1bc144b6 378 if (llist_empty(&pool->clean_list))
6fa70da6
CM
379 schedule();
380
f6df683f 381 ibmr = rds_ib_reuse_mr(pool);
6fa70da6
CM
382 if (ibmr) {
383 *ibmr_ret = ibmr;
384 finish_wait(&pool->flush_wait, &wait);
385 goto out_nolock;
386 }
387 }
388 finish_wait(&pool->flush_wait, &wait);
389 } else
390 mutex_lock(&pool->flush_lock);
391
392 if (ibmr_ret) {
f6df683f 393 ibmr = rds_ib_reuse_mr(pool);
6fa70da6
CM
394 if (ibmr) {
395 *ibmr_ret = ibmr;
396 goto out;
397 }
398 }
08b48a1e 399
08b48a1e 400 /* Get the list of all MRs to be dropped. Ordering matters -
6fa70da6
CM
401 * we want to put drop_list ahead of free_list.
402 */
6116c203
WW
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);
08b48a1e 405 if (free_all)
1bc144b6 406 llist_append_to_list(&pool->clean_list, &unmap_list);
08b48a1e
AG
407
408 free_goal = rds_ib_flush_goal(pool, free_all);
409
410 if (list_empty(&unmap_list))
411 goto out;
412
1659185f
AR
413 if (pool->use_fastreg)
414 rds_ib_unreg_frmr(&unmap_list, &nfreed, &unpinned, free_goal);
415 else
416 rds_ib_unreg_fmr(&unmap_list, &nfreed, &unpinned, free_goal);
08b48a1e 417
6fa70da6
CM
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
1bc144b6 421 * to pull items off. The llist would explode if we managed to
6fa70da6 422 * remove something from the clean list and then add it back again
1bc144b6 423 * while another CPU was spinning on that same item in llist_del_first.
6fa70da6 424 *
1bc144b6 425 * This is pretty unlikely, but just in case wait for an llist grace period
6fa70da6
CM
426 * here before adding anything back into the clean list.
427 */
428 wait_clean_list_grace();
429
1bc144b6 430 list_to_llist_nodes(pool, &unmap_list, &clean_nodes, &clean_tail);
6fa70da6 431 if (ibmr_ret)
1bc144b6 432 *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode);
6fa70da6 433
1bc144b6
HY
434 /* more than one entry in llist nodes */
435 if (clean_nodes->next)
436 llist_add_batch(clean_nodes->next, clean_tail, &pool->clean_list);
6fa70da6
CM
437
438 }
08b48a1e
AG
439
440 atomic_sub(unpinned, &pool->free_pinned);
6116c203 441 atomic_sub(dirty_to_clean, &pool->dirty_count);
08b48a1e
AG
442 atomic_sub(nfreed, &pool->item_count);
443
444out:
445 mutex_unlock(&pool->flush_lock);
6fa70da6
CM
446 if (waitqueue_active(&pool->flush_wait))
447 wake_up(&pool->flush_wait);
448out_nolock:
490ea596 449 return 0;
450}
451
452struct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *pool)
453{
454 struct rds_ib_mr *ibmr = NULL;
455 int iter = 0;
456
457 if (atomic_read(&pool->dirty_count) >= pool->max_items_soft / 10)
458 queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
459
460 while (1) {
461 ibmr = rds_ib_reuse_mr(pool);
462 if (ibmr)
463 return ibmr;
464
465 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
466 break;
467
468 atomic_dec(&pool->item_count);
469
470 if (++iter > 2) {
471 if (pool->pool_type == RDS_IB_MR_8K_POOL)
472 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted);
473 else
474 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted);
475 return ERR_PTR(-EAGAIN);
476 }
477
478 /* We do have some empty MRs. Flush them out. */
479 if (pool->pool_type == RDS_IB_MR_8K_POOL)
480 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait);
481 else
482 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait);
483
484 rds_ib_flush_mr_pool(pool, 0, &ibmr);
485 if (ibmr)
486 return ibmr;
487 }
488
489 return ibmr;
08b48a1e
AG
490}
491
492static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
493{
7a0ff5db 494 struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
08b48a1e 495
6fa70da6 496 rds_ib_flush_mr_pool(pool, 0, NULL);
08b48a1e
AG
497}
498
499void rds_ib_free_mr(void *trans_private, int invalidate)
500{
501 struct rds_ib_mr *ibmr = trans_private;
26139dc1 502 struct rds_ib_mr_pool *pool = ibmr->pool;
08b48a1e 503 struct rds_ib_device *rds_ibdev = ibmr->device;
08b48a1e
AG
504
505 rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
506
507 /* Return it to the pool's free list */
1659185f
AR
508 if (rds_ibdev->use_fastreg)
509 rds_ib_free_frmr_list(ibmr);
510 else
511 rds_ib_free_fmr_list(ibmr);
08b48a1e
AG
512
513 atomic_add(ibmr->sg_len, &pool->free_pinned);
514 atomic_inc(&pool->dirty_count);
08b48a1e
AG
515
516 /* If we've pinned too many pages, request a flush */
f64f9e71 517 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
ef5217a6 518 atomic_read(&pool->dirty_count) >= pool->max_items / 5)
f6df683f 519 queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
08b48a1e
AG
520
521 if (invalidate) {
522 if (likely(!in_interrupt())) {
6fa70da6 523 rds_ib_flush_mr_pool(pool, 0, NULL);
08b48a1e
AG
524 } else {
525 /* We get here if the user created a MR marked
ad1d7dc0 526 * as use_once and invalidate at the same time.
527 */
f6df683f 528 queue_delayed_work(rds_ib_mr_wq,
ad1d7dc0 529 &pool->flush_worker, 10);
08b48a1e
AG
530 }
531 }
3e0249f9
ZB
532
533 rds_ib_dev_put(rds_ibdev);
08b48a1e
AG
534}
535
536void rds_ib_flush_mrs(void)
537{
538 struct rds_ib_device *rds_ibdev;
539
ea819867 540 down_read(&rds_ib_devices_lock);
08b48a1e 541 list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
06766513
SS
542 if (rds_ibdev->mr_8k_pool)
543 rds_ib_flush_mr_pool(rds_ibdev->mr_8k_pool, 0, NULL);
08b48a1e 544
06766513
SS
545 if (rds_ibdev->mr_1m_pool)
546 rds_ib_flush_mr_pool(rds_ibdev->mr_1m_pool, 0, NULL);
08b48a1e 547 }
ea819867 548 up_read(&rds_ib_devices_lock);
08b48a1e
AG
549}
550
551void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
9e630bcb
AR
552 struct rds_sock *rs, u32 *key_ret,
553 struct rds_connection *conn)
08b48a1e
AG
554{
555 struct rds_ib_device *rds_ibdev;
556 struct rds_ib_mr *ibmr = NULL;
9e630bcb 557 struct rds_ib_connection *ic = NULL;
08b48a1e
AG
558 int ret;
559
eee2fa6a 560 rds_ibdev = rds_ib_get_device(rs->rs_bound_addr.s6_addr32[3]);
08b48a1e
AG
561 if (!rds_ibdev) {
562 ret = -ENODEV;
563 goto out;
564 }
565
9e630bcb
AR
566 if (conn)
567 ic = conn->c_transport_data;
568
06766513 569 if (!rds_ibdev->mr_8k_pool || !rds_ibdev->mr_1m_pool) {
08b48a1e
AG
570 ret = -ENODEV;
571 goto out;
572 }
573
1659185f
AR
574 if (rds_ibdev->use_fastreg)
575 ibmr = rds_ib_reg_frmr(rds_ibdev, ic, sg, nents, key_ret);
576 else
577 ibmr = rds_ib_reg_fmr(rds_ibdev, sg, nents, key_ret);
9e630bcb
AR
578 if (IS_ERR(ibmr)) {
579 ret = PTR_ERR(ibmr);
490ea596 580 pr_warn("RDS/IB: rds_ib_get_mr failed (errno=%d)\n", ret);
9e630bcb
AR
581 } else {
582 return ibmr;
583 }
490ea596 584
9e630bcb 585 out:
3e0249f9
ZB
586 if (rds_ibdev)
587 rds_ib_dev_put(rds_ibdev);
490ea596 588
9e630bcb 589 return ERR_PTR(ret);
08b48a1e 590}
6fa70da6 591
f6df683f 592void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
593{
594 cancel_delayed_work_sync(&pool->flush_worker);
595 rds_ib_flush_mr_pool(pool, 1, NULL);
596 WARN_ON(atomic_read(&pool->item_count));
597 WARN_ON(atomic_read(&pool->free_pinned));
598 kfree(pool);
599}
600
601struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev,
602 int pool_type)
603{
604 struct rds_ib_mr_pool *pool;
605
606 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
607 if (!pool)
608 return ERR_PTR(-ENOMEM);
609
610 pool->pool_type = pool_type;
611 init_llist_head(&pool->free_list);
612 init_llist_head(&pool->drop_list);
613 init_llist_head(&pool->clean_list);
614 mutex_init(&pool->flush_lock);
615 init_waitqueue_head(&pool->flush_wait);
616 INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
617
618 if (pool_type == RDS_IB_MR_1M_POOL) {
619 /* +1 allows for unaligned MRs */
620 pool->fmr_attr.max_pages = RDS_MR_1M_MSG_SIZE + 1;
b1fb67fa 621 pool->max_items = rds_ibdev->max_1m_mrs;
f6df683f 622 } else {
623 /* pool_type == RDS_IB_MR_8K_POOL */
624 pool->fmr_attr.max_pages = RDS_MR_8K_MSG_SIZE + 1;
b1fb67fa 625 pool->max_items = rds_ibdev->max_8k_mrs;
f6df683f 626 }
627
628 pool->max_free_pinned = pool->max_items * pool->fmr_attr.max_pages / 4;
629 pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps;
630 pool->fmr_attr.page_shift = PAGE_SHIFT;
631 pool->max_items_soft = rds_ibdev->max_mrs * 3 / 4;
1659185f 632 pool->use_fastreg = rds_ibdev->use_fastreg;
f6df683f 633
634 return pool;
635}
636
637int rds_ib_mr_init(void)
638{
231edca9 639 rds_ib_mr_wq = alloc_workqueue("rds_mr_flushd", WQ_MEM_RECLAIM, 0);
f6df683f 640 if (!rds_ib_mr_wq)
641 return -ENOMEM;
642 return 0;
643}
644
645/* By the time this is called all the IB devices should have been torn down and
646 * had their pools freed. As each pool is freed its work struct is waited on,
647 * so the pool flushing work queue should be idle by the time we get here.
648 */
649void rds_ib_mr_exit(void)
650{
651 destroy_workqueue(rds_ib_mr_wq);
652}