Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 35 | #include <linux/rculist.h> |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 36 | #include <linux/llist.h> |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 37 | |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 38 | #include "ib_mr.h" |
| 39 | |
| 40 | struct workqueue_struct *rds_ib_mr_wq; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 41 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 42 | static DEFINE_PER_CPU(unsigned long, clean_list_grace); |
| 43 | #define CLEAN_LIST_BUSY_BIT 0 |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 44 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 45 | static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr) |
| 46 | { |
| 47 | struct rds_ib_device *rds_ibdev; |
| 48 | struct rds_ib_ipaddr *i_ipaddr; |
| 49 | |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 50 | rcu_read_lock(); |
| 51 | list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) { |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 52 | list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 53 | if (i_ipaddr->ipaddr == ipaddr) { |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 54 | atomic_inc(&rds_ibdev->refcount); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 55 | rcu_read_unlock(); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 56 | return rds_ibdev; |
| 57 | } |
| 58 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 59 | } |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 60 | rcu_read_unlock(); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr) |
| 66 | { |
| 67 | struct rds_ib_ipaddr *i_ipaddr; |
| 68 | |
| 69 | i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL); |
| 70 | if (!i_ipaddr) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | i_ipaddr->ipaddr = ipaddr; |
| 74 | |
| 75 | spin_lock_irq(&rds_ibdev->spinlock); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 76 | list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 77 | spin_unlock_irq(&rds_ibdev->spinlock); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr) |
| 83 | { |
Andy Grover | 4a81802 | 2010-04-23 11:04:21 -0700 | [diff] [blame] | 84 | struct rds_ib_ipaddr *i_ipaddr; |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 85 | struct rds_ib_ipaddr *to_free = NULL; |
| 86 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 87 | |
| 88 | spin_lock_irq(&rds_ibdev->spinlock); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 89 | list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 90 | if (i_ipaddr->ipaddr == ipaddr) { |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 91 | list_del_rcu(&i_ipaddr->list); |
| 92 | to_free = i_ipaddr; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | } |
| 96 | spin_unlock_irq(&rds_ibdev->spinlock); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 97 | |
Santosh Shilimkar | 59fe460 | 2012-02-03 11:09:23 -0500 | [diff] [blame] | 98 | if (to_free) |
| 99 | kfree_rcu(to_free, rcu); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr) |
| 103 | { |
| 104 | struct rds_ib_device *rds_ibdev_old; |
| 105 | |
| 106 | rds_ibdev_old = rds_ib_get_device(ipaddr); |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame] | 107 | if (!rds_ibdev_old) |
| 108 | return rds_ib_add_ipaddr(rds_ibdev, ipaddr); |
| 109 | |
| 110 | if (rds_ibdev_old != rds_ibdev) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 111 | rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr); |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 112 | rds_ib_dev_put(rds_ibdev_old); |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame] | 113 | return rds_ib_add_ipaddr(rds_ibdev, ipaddr); |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 114 | } |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame] | 115 | rds_ib_dev_put(rds_ibdev_old); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 116 | |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame] | 117 | return 0; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 120 | void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 121 | { |
| 122 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 123 | |
| 124 | /* conn was previously on the nodev_conns_list */ |
| 125 | spin_lock_irq(&ib_nodev_conns_lock); |
| 126 | BUG_ON(list_empty(&ib_nodev_conns)); |
| 127 | BUG_ON(list_empty(&ic->ib_node)); |
| 128 | list_del(&ic->ib_node); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 129 | |
Dan Carpenter | aef3ea3 | 2010-09-18 13:44:14 +0000 | [diff] [blame] | 130 | spin_lock(&rds_ibdev->spinlock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 131 | list_add_tail(&ic->ib_node, &rds_ibdev->conn_list); |
Dan Carpenter | aef3ea3 | 2010-09-18 13:44:14 +0000 | [diff] [blame] | 132 | spin_unlock(&rds_ibdev->spinlock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 133 | spin_unlock_irq(&ib_nodev_conns_lock); |
| 134 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 135 | ic->rds_ibdev = rds_ibdev; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 136 | atomic_inc(&rds_ibdev->refcount); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 139 | void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn) |
| 140 | { |
| 141 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 142 | |
| 143 | /* place conn on nodev_conns_list */ |
| 144 | spin_lock(&ib_nodev_conns_lock); |
| 145 | |
| 146 | spin_lock_irq(&rds_ibdev->spinlock); |
| 147 | BUG_ON(list_empty(&ic->ib_node)); |
| 148 | list_del(&ic->ib_node); |
| 149 | spin_unlock_irq(&rds_ibdev->spinlock); |
| 150 | |
| 151 | list_add_tail(&ic->ib_node, &ib_nodev_conns); |
| 152 | |
| 153 | spin_unlock(&ib_nodev_conns_lock); |
| 154 | |
| 155 | ic->rds_ibdev = NULL; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 156 | rds_ib_dev_put(rds_ibdev); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Zach Brown | 8aeb1ba | 2010-06-25 14:58:16 -0700 | [diff] [blame] | 159 | void rds_ib_destroy_nodev_conns(void) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 160 | { |
| 161 | struct rds_ib_connection *ic, *_ic; |
| 162 | LIST_HEAD(tmp_list); |
| 163 | |
| 164 | /* avoid calling conn_destroy with irqs off */ |
Zach Brown | 8aeb1ba | 2010-06-25 14:58:16 -0700 | [diff] [blame] | 165 | spin_lock_irq(&ib_nodev_conns_lock); |
| 166 | list_splice(&ib_nodev_conns, &tmp_list); |
| 167 | spin_unlock_irq(&ib_nodev_conns_lock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 168 | |
Andy Grover | 433d308 | 2009-10-30 08:51:55 +0000 | [diff] [blame] | 169 | list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 170 | rds_conn_destroy(ic->conn); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 173 | void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo) |
| 174 | { |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 175 | struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 176 | |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 177 | iinfo->rdma_mr_max = pool_1m->max_items; |
| 178 | iinfo->rdma_mr_size = pool_1m->fmr_attr.max_pages; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 181 | struct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *pool) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 182 | { |
| 183 | struct rds_ib_mr *ibmr = NULL; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 184 | struct llist_node *ret; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 185 | unsigned long *flag; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 186 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 187 | preempt_disable(); |
Christoph Lameter | 903ceff | 2014-08-17 12:30:35 -0500 | [diff] [blame] | 188 | flag = this_cpu_ptr(&clean_list_grace); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 189 | set_bit(CLEAN_LIST_BUSY_BIT, flag); |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 190 | ret = llist_del_first(&pool->clean_list); |
santosh.shilimkar@oracle.com | db42753 | 2016-03-01 15:20:51 -0800 | [diff] [blame] | 191 | if (ret) { |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 192 | ibmr = llist_entry(ret, struct rds_ib_mr, llnode); |
santosh.shilimkar@oracle.com | db42753 | 2016-03-01 15:20:51 -0800 | [diff] [blame] | 193 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 194 | rds_ib_stats_inc(s_ib_rdma_mr_8k_reused); |
| 195 | else |
| 196 | rds_ib_stats_inc(s_ib_rdma_mr_1m_reused); |
| 197 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 198 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 199 | clear_bit(CLEAN_LIST_BUSY_BIT, flag); |
| 200 | preempt_enable(); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 201 | return ibmr; |
| 202 | } |
| 203 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 204 | static inline void wait_clean_list_grace(void) |
| 205 | { |
| 206 | int cpu; |
| 207 | unsigned long *flag; |
| 208 | |
| 209 | for_each_online_cpu(cpu) { |
| 210 | flag = &per_cpu(clean_list_grace, cpu); |
| 211 | while (test_bit(CLEAN_LIST_BUSY_BIT, flag)) |
| 212 | cpu_relax(); |
| 213 | } |
| 214 | } |
| 215 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 216 | void rds_ib_sync_mr(void *trans_private, int direction) |
| 217 | { |
| 218 | struct rds_ib_mr *ibmr = trans_private; |
| 219 | struct rds_ib_device *rds_ibdev = ibmr->device; |
| 220 | |
| 221 | switch (direction) { |
| 222 | case DMA_FROM_DEVICE: |
| 223 | ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg, |
| 224 | ibmr->sg_dma_len, DMA_BIDIRECTIONAL); |
| 225 | break; |
| 226 | case DMA_TO_DEVICE: |
| 227 | ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg, |
| 228 | ibmr->sg_dma_len, DMA_BIDIRECTIONAL); |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 233 | void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 234 | { |
| 235 | struct rds_ib_device *rds_ibdev = ibmr->device; |
| 236 | |
| 237 | if (ibmr->sg_dma_len) { |
| 238 | ib_dma_unmap_sg(rds_ibdev->dev, |
| 239 | ibmr->sg, ibmr->sg_len, |
| 240 | DMA_BIDIRECTIONAL); |
| 241 | ibmr->sg_dma_len = 0; |
| 242 | } |
| 243 | |
| 244 | /* Release the s/g list */ |
| 245 | if (ibmr->sg_len) { |
| 246 | unsigned int i; |
| 247 | |
| 248 | for (i = 0; i < ibmr->sg_len; ++i) { |
| 249 | struct page *page = sg_page(&ibmr->sg[i]); |
| 250 | |
| 251 | /* FIXME we need a way to tell a r/w MR |
| 252 | * from a r/o MR */ |
santosh.shilimkar@oracle.com | 5c240fa | 2015-08-22 15:45:31 -0700 | [diff] [blame] | 253 | WARN_ON(!page->mapping && irqs_disabled()); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 254 | set_page_dirty(page); |
| 255 | put_page(page); |
| 256 | } |
| 257 | kfree(ibmr->sg); |
| 258 | |
| 259 | ibmr->sg = NULL; |
| 260 | ibmr->sg_len = 0; |
| 261 | } |
| 262 | } |
| 263 | |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 264 | void rds_ib_teardown_mr(struct rds_ib_mr *ibmr) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 265 | { |
| 266 | unsigned int pinned = ibmr->sg_len; |
| 267 | |
| 268 | __rds_ib_teardown_mr(ibmr); |
| 269 | if (pinned) { |
Santosh Shilimkar | 26139dc | 2015-09-15 18:20:35 -0700 | [diff] [blame] | 270 | struct rds_ib_mr_pool *pool = ibmr->pool; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 271 | |
| 272 | atomic_sub(pinned, &pool->free_pinned); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all) |
| 277 | { |
| 278 | unsigned int item_count; |
| 279 | |
| 280 | item_count = atomic_read(&pool->item_count); |
| 281 | if (free_all) |
| 282 | return item_count; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 288 | * given an llist of mrs, put them all into the list_head for more processing |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 289 | */ |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 290 | static unsigned int llist_append_to_list(struct llist_head *llist, |
| 291 | struct list_head *list) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 292 | { |
| 293 | struct rds_ib_mr *ibmr; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 294 | struct llist_node *node; |
| 295 | struct llist_node *next; |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 296 | unsigned int count = 0; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 297 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 298 | node = llist_del_all(llist); |
| 299 | while (node) { |
| 300 | next = node->next; |
| 301 | ibmr = llist_entry(node, struct rds_ib_mr, llnode); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 302 | list_add_tail(&ibmr->unmap_list, list); |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 303 | node = next; |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 304 | count++; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 305 | } |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 306 | return count; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 310 | * this takes a list head of mrs and turns it into linked llist nodes |
| 311 | * of clusters. Each cluster has linked llist nodes of |
| 312 | * MR_CLUSTER_SIZE mrs that are ready for reuse. |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 313 | */ |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 314 | static void list_to_llist_nodes(struct rds_ib_mr_pool *pool, |
| 315 | struct list_head *list, |
| 316 | struct llist_node **nodes_head, |
| 317 | struct llist_node **nodes_tail) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 318 | { |
| 319 | struct rds_ib_mr *ibmr; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 320 | struct llist_node *cur = NULL; |
| 321 | struct llist_node **next = nodes_head; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 322 | |
| 323 | list_for_each_entry(ibmr, list, unmap_list) { |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 324 | cur = &ibmr->llnode; |
| 325 | *next = cur; |
| 326 | next = &cur->next; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 327 | } |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 328 | *next = NULL; |
| 329 | *nodes_tail = cur; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 333 | * Flush our pool of MRs. |
| 334 | * At a minimum, all currently unused MRs are unmapped. |
| 335 | * If the number of MRs allocated exceeds the limit, we also try |
| 336 | * to free as many MRs as needed to get back to this limit. |
| 337 | */ |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 338 | int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool, |
| 339 | int free_all, struct rds_ib_mr **ibmr_ret) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 340 | { |
santosh.shilimkar@oracle.com | 490ea59 | 2016-03-01 15:20:48 -0800 | [diff] [blame] | 341 | struct rds_ib_mr *ibmr; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 342 | struct llist_node *clean_nodes; |
| 343 | struct llist_node *clean_tail; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 344 | LIST_HEAD(unmap_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 345 | unsigned long unpinned = 0; |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 346 | unsigned int nfreed = 0, dirty_to_clean = 0, free_goal; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 347 | |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 348 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 349 | rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush); |
| 350 | else |
| 351 | rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 352 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 353 | if (ibmr_ret) { |
| 354 | DEFINE_WAIT(wait); |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 355 | while (!mutex_trylock(&pool->flush_lock)) { |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 356 | ibmr = rds_ib_reuse_mr(pool); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 357 | if (ibmr) { |
| 358 | *ibmr_ret = ibmr; |
| 359 | finish_wait(&pool->flush_wait, &wait); |
| 360 | goto out_nolock; |
| 361 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 362 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 363 | prepare_to_wait(&pool->flush_wait, &wait, |
| 364 | TASK_UNINTERRUPTIBLE); |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 365 | if (llist_empty(&pool->clean_list)) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 366 | schedule(); |
| 367 | |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 368 | ibmr = rds_ib_reuse_mr(pool); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 369 | if (ibmr) { |
| 370 | *ibmr_ret = ibmr; |
| 371 | finish_wait(&pool->flush_wait, &wait); |
| 372 | goto out_nolock; |
| 373 | } |
| 374 | } |
| 375 | finish_wait(&pool->flush_wait, &wait); |
| 376 | } else |
| 377 | mutex_lock(&pool->flush_lock); |
| 378 | |
| 379 | if (ibmr_ret) { |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 380 | ibmr = rds_ib_reuse_mr(pool); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 381 | if (ibmr) { |
| 382 | *ibmr_ret = ibmr; |
| 383 | goto out; |
| 384 | } |
| 385 | } |
| 386 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 387 | /* Get the list of all MRs to be dropped. Ordering matters - |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 388 | * we want to put drop_list ahead of free_list. |
| 389 | */ |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 390 | dirty_to_clean = llist_append_to_list(&pool->drop_list, &unmap_list); |
| 391 | dirty_to_clean += llist_append_to_list(&pool->free_list, &unmap_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 392 | if (free_all) |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 393 | llist_append_to_list(&pool->clean_list, &unmap_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 394 | |
| 395 | free_goal = rds_ib_flush_goal(pool, free_all); |
| 396 | |
| 397 | if (list_empty(&unmap_list)) |
| 398 | goto out; |
| 399 | |
Avinash Repaka | 1659185 | 2016-03-01 15:20:54 -0800 | [diff] [blame] | 400 | if (pool->use_fastreg) |
| 401 | rds_ib_unreg_frmr(&unmap_list, &nfreed, &unpinned, free_goal); |
| 402 | else |
| 403 | rds_ib_unreg_fmr(&unmap_list, &nfreed, &unpinned, free_goal); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 404 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 405 | if (!list_empty(&unmap_list)) { |
| 406 | /* we have to make sure that none of the things we're about |
| 407 | * to put on the clean list would race with other cpus trying |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 408 | * to pull items off. The llist would explode if we managed to |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 409 | * remove something from the clean list and then add it back again |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 410 | * while another CPU was spinning on that same item in llist_del_first. |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 411 | * |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 412 | * This is pretty unlikely, but just in case wait for an llist grace period |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 413 | * here before adding anything back into the clean list. |
| 414 | */ |
| 415 | wait_clean_list_grace(); |
| 416 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 417 | list_to_llist_nodes(pool, &unmap_list, &clean_nodes, &clean_tail); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 418 | if (ibmr_ret) |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 419 | *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 420 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 421 | /* more than one entry in llist nodes */ |
| 422 | if (clean_nodes->next) |
| 423 | llist_add_batch(clean_nodes->next, clean_tail, &pool->clean_list); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 424 | |
| 425 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 426 | |
| 427 | atomic_sub(unpinned, &pool->free_pinned); |
Wengang Wang | 6116c20 | 2015-08-25 12:02:00 -0700 | [diff] [blame] | 428 | atomic_sub(dirty_to_clean, &pool->dirty_count); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 429 | atomic_sub(nfreed, &pool->item_count); |
| 430 | |
| 431 | out: |
| 432 | mutex_unlock(&pool->flush_lock); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 433 | if (waitqueue_active(&pool->flush_wait)) |
| 434 | wake_up(&pool->flush_wait); |
| 435 | out_nolock: |
santosh.shilimkar@oracle.com | 490ea59 | 2016-03-01 15:20:48 -0800 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | struct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *pool) |
| 440 | { |
| 441 | struct rds_ib_mr *ibmr = NULL; |
| 442 | int iter = 0; |
| 443 | |
| 444 | if (atomic_read(&pool->dirty_count) >= pool->max_items_soft / 10) |
| 445 | queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10); |
| 446 | |
| 447 | while (1) { |
| 448 | ibmr = rds_ib_reuse_mr(pool); |
| 449 | if (ibmr) |
| 450 | return ibmr; |
| 451 | |
| 452 | if (atomic_inc_return(&pool->item_count) <= pool->max_items) |
| 453 | break; |
| 454 | |
| 455 | atomic_dec(&pool->item_count); |
| 456 | |
| 457 | if (++iter > 2) { |
| 458 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 459 | rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted); |
| 460 | else |
| 461 | rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted); |
| 462 | return ERR_PTR(-EAGAIN); |
| 463 | } |
| 464 | |
| 465 | /* We do have some empty MRs. Flush them out. */ |
| 466 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 467 | rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait); |
| 468 | else |
| 469 | rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait); |
| 470 | |
| 471 | rds_ib_flush_mr_pool(pool, 0, &ibmr); |
| 472 | if (ibmr) |
| 473 | return ibmr; |
| 474 | } |
| 475 | |
| 476 | return ibmr; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void rds_ib_mr_pool_flush_worker(struct work_struct *work) |
| 480 | { |
Chris Mason | 7a0ff5d | 2010-06-11 11:26:02 -0700 | [diff] [blame] | 481 | struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 482 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 483 | rds_ib_flush_mr_pool(pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void rds_ib_free_mr(void *trans_private, int invalidate) |
| 487 | { |
| 488 | struct rds_ib_mr *ibmr = trans_private; |
Santosh Shilimkar | 26139dc | 2015-09-15 18:20:35 -0700 | [diff] [blame] | 489 | struct rds_ib_mr_pool *pool = ibmr->pool; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 490 | struct rds_ib_device *rds_ibdev = ibmr->device; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 491 | |
| 492 | rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len); |
| 493 | |
| 494 | /* Return it to the pool's free list */ |
Avinash Repaka | 1659185 | 2016-03-01 15:20:54 -0800 | [diff] [blame] | 495 | if (rds_ibdev->use_fastreg) |
| 496 | rds_ib_free_frmr_list(ibmr); |
| 497 | else |
| 498 | rds_ib_free_fmr_list(ibmr); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 499 | |
| 500 | atomic_add(ibmr->sg_len, &pool->free_pinned); |
| 501 | atomic_inc(&pool->dirty_count); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 502 | |
| 503 | /* If we've pinned too many pages, request a flush */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 504 | if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned || |
santosh.shilimkar@oracle.com | ef5217a | 2015-08-25 12:02:02 -0700 | [diff] [blame] | 505 | atomic_read(&pool->dirty_count) >= pool->max_items / 5) |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 506 | queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 507 | |
| 508 | if (invalidate) { |
| 509 | if (likely(!in_interrupt())) { |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 510 | rds_ib_flush_mr_pool(pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 511 | } else { |
| 512 | /* We get here if the user created a MR marked |
santosh.shilimkar@oracle.com | ad1d7dc | 2015-08-25 12:02:01 -0700 | [diff] [blame] | 513 | * as use_once and invalidate at the same time. |
| 514 | */ |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 515 | queue_delayed_work(rds_ib_mr_wq, |
santosh.shilimkar@oracle.com | ad1d7dc | 2015-08-25 12:02:01 -0700 | [diff] [blame] | 516 | &pool->flush_worker, 10); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 519 | |
| 520 | rds_ib_dev_put(rds_ibdev); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void rds_ib_flush_mrs(void) |
| 524 | { |
| 525 | struct rds_ib_device *rds_ibdev; |
| 526 | |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 527 | down_read(&rds_ib_devices_lock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 528 | list_for_each_entry(rds_ibdev, &rds_ib_devices, list) { |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 529 | if (rds_ibdev->mr_8k_pool) |
| 530 | rds_ib_flush_mr_pool(rds_ibdev->mr_8k_pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 531 | |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 532 | if (rds_ibdev->mr_1m_pool) |
| 533 | rds_ib_flush_mr_pool(rds_ibdev->mr_1m_pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 534 | } |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 535 | up_read(&rds_ib_devices_lock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents, |
| 539 | struct rds_sock *rs, u32 *key_ret) |
| 540 | { |
| 541 | struct rds_ib_device *rds_ibdev; |
| 542 | struct rds_ib_mr *ibmr = NULL; |
Avinash Repaka | 1659185 | 2016-03-01 15:20:54 -0800 | [diff] [blame] | 543 | struct rds_ib_connection *ic = rs->rs_conn->c_transport_data; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 544 | int ret; |
| 545 | |
| 546 | rds_ibdev = rds_ib_get_device(rs->rs_bound_addr); |
| 547 | if (!rds_ibdev) { |
| 548 | ret = -ENODEV; |
| 549 | goto out; |
| 550 | } |
| 551 | |
Santosh Shilimkar | 0676651 | 2015-09-10 21:20:57 -0700 | [diff] [blame] | 552 | if (!rds_ibdev->mr_8k_pool || !rds_ibdev->mr_1m_pool) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 553 | ret = -ENODEV; |
| 554 | goto out; |
| 555 | } |
| 556 | |
Avinash Repaka | 1659185 | 2016-03-01 15:20:54 -0800 | [diff] [blame] | 557 | if (rds_ibdev->use_fastreg) |
| 558 | ibmr = rds_ib_reg_frmr(rds_ibdev, ic, sg, nents, key_ret); |
| 559 | else |
| 560 | ibmr = rds_ib_reg_fmr(rds_ibdev, sg, nents, key_ret); |
santosh.shilimkar@oracle.com | 490ea59 | 2016-03-01 15:20:48 -0800 | [diff] [blame] | 561 | if (ibmr) |
| 562 | rds_ibdev = NULL; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 563 | |
| 564 | out: |
santosh.shilimkar@oracle.com | 490ea59 | 2016-03-01 15:20:48 -0800 | [diff] [blame] | 565 | if (!ibmr) |
| 566 | pr_warn("RDS/IB: rds_ib_get_mr failed (errno=%d)\n", ret); |
| 567 | |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 568 | if (rds_ibdev) |
| 569 | rds_ib_dev_put(rds_ibdev); |
santosh.shilimkar@oracle.com | 490ea59 | 2016-03-01 15:20:48 -0800 | [diff] [blame] | 570 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 571 | return ibmr; |
| 572 | } |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 573 | |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 574 | void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool) |
| 575 | { |
| 576 | cancel_delayed_work_sync(&pool->flush_worker); |
| 577 | rds_ib_flush_mr_pool(pool, 1, NULL); |
| 578 | WARN_ON(atomic_read(&pool->item_count)); |
| 579 | WARN_ON(atomic_read(&pool->free_pinned)); |
| 580 | kfree(pool); |
| 581 | } |
| 582 | |
| 583 | struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev, |
| 584 | int pool_type) |
| 585 | { |
| 586 | struct rds_ib_mr_pool *pool; |
| 587 | |
| 588 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
| 589 | if (!pool) |
| 590 | return ERR_PTR(-ENOMEM); |
| 591 | |
| 592 | pool->pool_type = pool_type; |
| 593 | init_llist_head(&pool->free_list); |
| 594 | init_llist_head(&pool->drop_list); |
| 595 | init_llist_head(&pool->clean_list); |
| 596 | mutex_init(&pool->flush_lock); |
| 597 | init_waitqueue_head(&pool->flush_wait); |
| 598 | INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker); |
| 599 | |
| 600 | if (pool_type == RDS_IB_MR_1M_POOL) { |
| 601 | /* +1 allows for unaligned MRs */ |
| 602 | pool->fmr_attr.max_pages = RDS_MR_1M_MSG_SIZE + 1; |
| 603 | pool->max_items = RDS_MR_1M_POOL_SIZE; |
| 604 | } else { |
| 605 | /* pool_type == RDS_IB_MR_8K_POOL */ |
| 606 | pool->fmr_attr.max_pages = RDS_MR_8K_MSG_SIZE + 1; |
| 607 | pool->max_items = RDS_MR_8K_POOL_SIZE; |
| 608 | } |
| 609 | |
| 610 | pool->max_free_pinned = pool->max_items * pool->fmr_attr.max_pages / 4; |
| 611 | pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps; |
| 612 | pool->fmr_attr.page_shift = PAGE_SHIFT; |
| 613 | pool->max_items_soft = rds_ibdev->max_mrs * 3 / 4; |
Avinash Repaka | 1659185 | 2016-03-01 15:20:54 -0800 | [diff] [blame] | 614 | pool->use_fastreg = rds_ibdev->use_fastreg; |
santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame] | 615 | |
| 616 | return pool; |
| 617 | } |
| 618 | |
| 619 | int rds_ib_mr_init(void) |
| 620 | { |
| 621 | rds_ib_mr_wq = create_workqueue("rds_mr_flushd"); |
| 622 | if (!rds_ib_mr_wq) |
| 623 | return -ENOMEM; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* By the time this is called all the IB devices should have been torn down and |
| 628 | * had their pools freed. As each pool is freed its work struct is waited on, |
| 629 | * so the pool flushing work queue should be idle by the time we get here. |
| 630 | */ |
| 631 | void rds_ib_mr_exit(void) |
| 632 | { |
| 633 | destroy_workqueue(rds_ib_mr_wq); |
| 634 | } |