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