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 | |
| 38 | #include "rds.h" |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 39 | #include "ib.h" |
| 40 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 41 | static DEFINE_PER_CPU(unsigned long, clean_list_grace); |
| 42 | #define CLEAN_LIST_BUSY_BIT 0 |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * This is stored as mr->r_trans_private. |
| 46 | */ |
| 47 | struct rds_ib_mr { |
| 48 | struct rds_ib_device *device; |
| 49 | struct rds_ib_mr_pool *pool; |
| 50 | struct ib_fmr *fmr; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 51 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 52 | struct llist_node llnode; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 53 | |
| 54 | /* unmap_list is for freeing */ |
| 55 | struct list_head unmap_list; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 56 | unsigned int remap_count; |
| 57 | |
| 58 | struct scatterlist *sg; |
| 59 | unsigned int sg_len; |
| 60 | u64 *dma; |
| 61 | int sg_dma_len; |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * Our own little FMR pool |
| 66 | */ |
| 67 | struct rds_ib_mr_pool { |
| 68 | struct mutex flush_lock; /* serialize fmr invalidate */ |
Chris Mason | 7a0ff5d | 2010-06-11 11:26:02 -0700 | [diff] [blame] | 69 | struct delayed_work flush_worker; /* flush worker */ |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 70 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 71 | atomic_t item_count; /* total # of MRs */ |
| 72 | atomic_t dirty_count; /* # dirty of MRs */ |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 73 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 74 | struct llist_head drop_list; /* MRs that have reached their max_maps limit */ |
| 75 | struct llist_head free_list; /* unused MRs */ |
| 76 | struct llist_head clean_list; /* global unused & unamapped MRs */ |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 77 | wait_queue_head_t flush_wait; |
| 78 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 79 | atomic_t free_pinned; /* memory pinned by free MRs */ |
| 80 | unsigned long max_items; |
| 81 | unsigned long max_items_soft; |
| 82 | unsigned long max_free_pinned; |
| 83 | struct ib_fmr_attr fmr_attr; |
| 84 | }; |
| 85 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 86 | static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool, int free_all, struct rds_ib_mr **); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 87 | static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr); |
| 88 | static void rds_ib_mr_pool_flush_worker(struct work_struct *work); |
| 89 | |
| 90 | static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr) |
| 91 | { |
| 92 | struct rds_ib_device *rds_ibdev; |
| 93 | struct rds_ib_ipaddr *i_ipaddr; |
| 94 | |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 95 | rcu_read_lock(); |
| 96 | list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) { |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 97 | list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 98 | if (i_ipaddr->ipaddr == ipaddr) { |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 99 | atomic_inc(&rds_ibdev->refcount); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 100 | rcu_read_unlock(); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 101 | return rds_ibdev; |
| 102 | } |
| 103 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 104 | } |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 105 | rcu_read_unlock(); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 106 | |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr) |
| 111 | { |
| 112 | struct rds_ib_ipaddr *i_ipaddr; |
| 113 | |
| 114 | i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL); |
| 115 | if (!i_ipaddr) |
| 116 | return -ENOMEM; |
| 117 | |
| 118 | i_ipaddr->ipaddr = ipaddr; |
| 119 | |
| 120 | spin_lock_irq(&rds_ibdev->spinlock); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 121 | list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 122 | spin_unlock_irq(&rds_ibdev->spinlock); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr) |
| 128 | { |
Andy Grover | 4a81802 | 2010-04-23 11:04:21 -0700 | [diff] [blame] | 129 | struct rds_ib_ipaddr *i_ipaddr; |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 130 | struct rds_ib_ipaddr *to_free = NULL; |
| 131 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 132 | |
| 133 | spin_lock_irq(&rds_ibdev->spinlock); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 134 | list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 135 | if (i_ipaddr->ipaddr == ipaddr) { |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 136 | list_del_rcu(&i_ipaddr->list); |
| 137 | to_free = i_ipaddr; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | spin_unlock_irq(&rds_ibdev->spinlock); |
Chris Mason | 764f2dd | 2010-04-22 21:59:15 -0400 | [diff] [blame] | 142 | |
| 143 | if (to_free) { |
| 144 | synchronize_rcu(); |
| 145 | kfree(to_free); |
| 146 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr) |
| 150 | { |
| 151 | struct rds_ib_device *rds_ibdev_old; |
| 152 | |
| 153 | rds_ibdev_old = rds_ib_get_device(ipaddr); |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame^] | 154 | if (!rds_ibdev_old) |
| 155 | return rds_ib_add_ipaddr(rds_ibdev, ipaddr); |
| 156 | |
| 157 | if (rds_ibdev_old != rds_ibdev) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 158 | rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr); |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 159 | rds_ib_dev_put(rds_ibdev_old); |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame^] | 160 | return rds_ib_add_ipaddr(rds_ibdev, ipaddr); |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 161 | } |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame^] | 162 | rds_ib_dev_put(rds_ibdev_old); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 163 | |
santosh.shilimkar@oracle.com | e1f475a | 2015-08-22 15:45:25 -0700 | [diff] [blame^] | 164 | return 0; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 167 | 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] | 168 | { |
| 169 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 170 | |
| 171 | /* conn was previously on the nodev_conns_list */ |
| 172 | spin_lock_irq(&ib_nodev_conns_lock); |
| 173 | BUG_ON(list_empty(&ib_nodev_conns)); |
| 174 | BUG_ON(list_empty(&ic->ib_node)); |
| 175 | list_del(&ic->ib_node); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 176 | |
Dan Carpenter | aef3ea3 | 2010-09-18 13:44:14 +0000 | [diff] [blame] | 177 | spin_lock(&rds_ibdev->spinlock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 178 | list_add_tail(&ic->ib_node, &rds_ibdev->conn_list); |
Dan Carpenter | aef3ea3 | 2010-09-18 13:44:14 +0000 | [diff] [blame] | 179 | spin_unlock(&rds_ibdev->spinlock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 180 | spin_unlock_irq(&ib_nodev_conns_lock); |
| 181 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 182 | ic->rds_ibdev = rds_ibdev; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 183 | atomic_inc(&rds_ibdev->refcount); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 186 | void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn) |
| 187 | { |
| 188 | struct rds_ib_connection *ic = conn->c_transport_data; |
| 189 | |
| 190 | /* place conn on nodev_conns_list */ |
| 191 | spin_lock(&ib_nodev_conns_lock); |
| 192 | |
| 193 | spin_lock_irq(&rds_ibdev->spinlock); |
| 194 | BUG_ON(list_empty(&ic->ib_node)); |
| 195 | list_del(&ic->ib_node); |
| 196 | spin_unlock_irq(&rds_ibdev->spinlock); |
| 197 | |
| 198 | list_add_tail(&ic->ib_node, &ib_nodev_conns); |
| 199 | |
| 200 | spin_unlock(&ib_nodev_conns_lock); |
| 201 | |
| 202 | ic->rds_ibdev = NULL; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 203 | rds_ib_dev_put(rds_ibdev); |
Andy Grover | 745cbcc | 2009-04-01 08:20:19 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Zach Brown | 8aeb1ba | 2010-06-25 14:58:16 -0700 | [diff] [blame] | 206 | void rds_ib_destroy_nodev_conns(void) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 207 | { |
| 208 | struct rds_ib_connection *ic, *_ic; |
| 209 | LIST_HEAD(tmp_list); |
| 210 | |
| 211 | /* avoid calling conn_destroy with irqs off */ |
Zach Brown | 8aeb1ba | 2010-06-25 14:58:16 -0700 | [diff] [blame] | 212 | spin_lock_irq(&ib_nodev_conns_lock); |
| 213 | list_splice(&ib_nodev_conns, &tmp_list); |
| 214 | spin_unlock_irq(&ib_nodev_conns_lock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 215 | |
Andy Grover | 433d308 | 2009-10-30 08:51:55 +0000 | [diff] [blame] | 216 | list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 217 | rds_conn_destroy(ic->conn); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev) |
| 221 | { |
| 222 | struct rds_ib_mr_pool *pool; |
| 223 | |
| 224 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
| 225 | if (!pool) |
| 226 | return ERR_PTR(-ENOMEM); |
| 227 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 228 | init_llist_head(&pool->free_list); |
| 229 | init_llist_head(&pool->drop_list); |
| 230 | init_llist_head(&pool->clean_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 231 | mutex_init(&pool->flush_lock); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 232 | init_waitqueue_head(&pool->flush_wait); |
Chris Mason | 7a0ff5d | 2010-06-11 11:26:02 -0700 | [diff] [blame] | 233 | INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 234 | |
| 235 | pool->fmr_attr.max_pages = fmr_message_size; |
| 236 | pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps; |
Andy Grover | a870d62 | 2009-07-17 13:13:33 +0000 | [diff] [blame] | 237 | pool->fmr_attr.page_shift = PAGE_SHIFT; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 238 | pool->max_free_pinned = rds_ibdev->max_fmrs * fmr_message_size / 4; |
| 239 | |
| 240 | /* We never allow more than max_items MRs to be allocated. |
| 241 | * When we exceed more than max_items_soft, we start freeing |
| 242 | * items more aggressively. |
| 243 | * Make sure that max_items > max_items_soft > max_items / 2 |
| 244 | */ |
| 245 | pool->max_items_soft = rds_ibdev->max_fmrs * 3 / 4; |
| 246 | pool->max_items = rds_ibdev->max_fmrs; |
| 247 | |
| 248 | return pool; |
| 249 | } |
| 250 | |
| 251 | void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo) |
| 252 | { |
| 253 | struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool; |
| 254 | |
| 255 | iinfo->rdma_mr_max = pool->max_items; |
| 256 | iinfo->rdma_mr_size = pool->fmr_attr.max_pages; |
| 257 | } |
| 258 | |
| 259 | void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool) |
| 260 | { |
Chris Mason | 7a0ff5d | 2010-06-11 11:26:02 -0700 | [diff] [blame] | 261 | cancel_delayed_work_sync(&pool->flush_worker); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 262 | rds_ib_flush_mr_pool(pool, 1, NULL); |
Andy Grover | 571c02f | 2010-03-11 13:50:01 +0000 | [diff] [blame] | 263 | WARN_ON(atomic_read(&pool->item_count)); |
| 264 | WARN_ON(atomic_read(&pool->free_pinned)); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 265 | kfree(pool); |
| 266 | } |
| 267 | |
| 268 | static inline struct rds_ib_mr *rds_ib_reuse_fmr(struct rds_ib_mr_pool *pool) |
| 269 | { |
| 270 | struct rds_ib_mr *ibmr = NULL; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 271 | struct llist_node *ret; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 272 | unsigned long *flag; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 273 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 274 | preempt_disable(); |
Christoph Lameter | 903ceff | 2014-08-17 12:30:35 -0500 | [diff] [blame] | 275 | flag = this_cpu_ptr(&clean_list_grace); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 276 | set_bit(CLEAN_LIST_BUSY_BIT, flag); |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 277 | ret = llist_del_first(&pool->clean_list); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 278 | if (ret) |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 279 | ibmr = llist_entry(ret, struct rds_ib_mr, llnode); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 280 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 281 | clear_bit(CLEAN_LIST_BUSY_BIT, flag); |
| 282 | preempt_enable(); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 283 | return ibmr; |
| 284 | } |
| 285 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 286 | static inline void wait_clean_list_grace(void) |
| 287 | { |
| 288 | int cpu; |
| 289 | unsigned long *flag; |
| 290 | |
| 291 | for_each_online_cpu(cpu) { |
| 292 | flag = &per_cpu(clean_list_grace, cpu); |
| 293 | while (test_bit(CLEAN_LIST_BUSY_BIT, flag)) |
| 294 | cpu_relax(); |
| 295 | } |
| 296 | } |
| 297 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 298 | static struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev) |
| 299 | { |
| 300 | struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool; |
| 301 | struct rds_ib_mr *ibmr = NULL; |
| 302 | int err = 0, iter = 0; |
| 303 | |
Chris Mason | 8576f37 | 2010-07-19 17:06:46 -0700 | [diff] [blame] | 304 | if (atomic_read(&pool->dirty_count) >= pool->max_items / 10) |
Tejun Heo | c534a10 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 305 | schedule_delayed_work(&pool->flush_worker, 10); |
Chris Mason | 8576f37 | 2010-07-19 17:06:46 -0700 | [diff] [blame] | 306 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 307 | while (1) { |
| 308 | ibmr = rds_ib_reuse_fmr(pool); |
| 309 | if (ibmr) |
| 310 | return ibmr; |
| 311 | |
| 312 | /* No clean MRs - now we have the choice of either |
| 313 | * allocating a fresh MR up to the limit imposed by the |
| 314 | * driver, or flush any dirty unused MRs. |
| 315 | * We try to avoid stalling in the send path if possible, |
| 316 | * so we allocate as long as we're allowed to. |
| 317 | * |
| 318 | * We're fussy with enforcing the FMR limit, though. If the driver |
| 319 | * tells us we can't use more than N fmrs, we shouldn't start |
| 320 | * arguing with it */ |
| 321 | if (atomic_inc_return(&pool->item_count) <= pool->max_items) |
| 322 | break; |
| 323 | |
| 324 | atomic_dec(&pool->item_count); |
| 325 | |
| 326 | if (++iter > 2) { |
| 327 | rds_ib_stats_inc(s_ib_rdma_mr_pool_depleted); |
| 328 | return ERR_PTR(-EAGAIN); |
| 329 | } |
| 330 | |
| 331 | /* We do have some empty MRs. Flush them out. */ |
| 332 | rds_ib_stats_inc(s_ib_rdma_mr_pool_wait); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 333 | rds_ib_flush_mr_pool(pool, 0, &ibmr); |
| 334 | if (ibmr) |
| 335 | return ibmr; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Andy Grover | e4c52c9 | 2010-04-23 10:49:53 -0700 | [diff] [blame] | 338 | ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL, rdsibdev_to_node(rds_ibdev)); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 339 | if (!ibmr) { |
| 340 | err = -ENOMEM; |
| 341 | goto out_no_cigar; |
| 342 | } |
| 343 | |
Chris Mason | 38a4e5e | 2010-05-11 15:09:45 -0700 | [diff] [blame] | 344 | memset(ibmr, 0, sizeof(*ibmr)); |
| 345 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 346 | ibmr->fmr = ib_alloc_fmr(rds_ibdev->pd, |
| 347 | (IB_ACCESS_LOCAL_WRITE | |
| 348 | IB_ACCESS_REMOTE_READ | |
Andy Grover | 15133f6 | 2010-01-12 14:33:38 -0800 | [diff] [blame] | 349 | IB_ACCESS_REMOTE_WRITE| |
| 350 | IB_ACCESS_REMOTE_ATOMIC), |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 351 | &pool->fmr_attr); |
| 352 | if (IS_ERR(ibmr->fmr)) { |
| 353 | err = PTR_ERR(ibmr->fmr); |
| 354 | ibmr->fmr = NULL; |
| 355 | printk(KERN_WARNING "RDS/IB: ib_alloc_fmr failed (err=%d)\n", err); |
| 356 | goto out_no_cigar; |
| 357 | } |
| 358 | |
| 359 | rds_ib_stats_inc(s_ib_rdma_mr_alloc); |
| 360 | return ibmr; |
| 361 | |
| 362 | out_no_cigar: |
| 363 | if (ibmr) { |
| 364 | if (ibmr->fmr) |
| 365 | ib_dealloc_fmr(ibmr->fmr); |
| 366 | kfree(ibmr); |
| 367 | } |
| 368 | atomic_dec(&pool->item_count); |
| 369 | return ERR_PTR(err); |
| 370 | } |
| 371 | |
| 372 | static int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr, |
| 373 | struct scatterlist *sg, unsigned int nents) |
| 374 | { |
| 375 | struct ib_device *dev = rds_ibdev->dev; |
| 376 | struct scatterlist *scat = sg; |
| 377 | u64 io_addr = 0; |
| 378 | u64 *dma_pages; |
| 379 | u32 len; |
| 380 | int page_cnt, sg_dma_len; |
| 381 | int i, j; |
| 382 | int ret; |
| 383 | |
| 384 | sg_dma_len = ib_dma_map_sg(dev, sg, nents, |
| 385 | DMA_BIDIRECTIONAL); |
| 386 | if (unlikely(!sg_dma_len)) { |
| 387 | printk(KERN_WARNING "RDS/IB: dma_map_sg failed!\n"); |
| 388 | return -EBUSY; |
| 389 | } |
| 390 | |
| 391 | len = 0; |
| 392 | page_cnt = 0; |
| 393 | |
| 394 | for (i = 0; i < sg_dma_len; ++i) { |
| 395 | unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]); |
| 396 | u64 dma_addr = ib_sg_dma_address(dev, &scat[i]); |
| 397 | |
Andy Grover | a870d62 | 2009-07-17 13:13:33 +0000 | [diff] [blame] | 398 | if (dma_addr & ~PAGE_MASK) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 399 | if (i > 0) |
| 400 | return -EINVAL; |
| 401 | else |
| 402 | ++page_cnt; |
| 403 | } |
Andy Grover | a870d62 | 2009-07-17 13:13:33 +0000 | [diff] [blame] | 404 | if ((dma_addr + dma_len) & ~PAGE_MASK) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 405 | if (i < sg_dma_len - 1) |
| 406 | return -EINVAL; |
| 407 | else |
| 408 | ++page_cnt; |
| 409 | } |
| 410 | |
| 411 | len += dma_len; |
| 412 | } |
| 413 | |
Andy Grover | a870d62 | 2009-07-17 13:13:33 +0000 | [diff] [blame] | 414 | page_cnt += len >> PAGE_SHIFT; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 415 | if (page_cnt > fmr_message_size) |
| 416 | return -EINVAL; |
| 417 | |
Andy Grover | e4c52c9 | 2010-04-23 10:49:53 -0700 | [diff] [blame] | 418 | dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC, |
| 419 | rdsibdev_to_node(rds_ibdev)); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 420 | if (!dma_pages) |
| 421 | return -ENOMEM; |
| 422 | |
| 423 | page_cnt = 0; |
| 424 | for (i = 0; i < sg_dma_len; ++i) { |
| 425 | unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]); |
| 426 | u64 dma_addr = ib_sg_dma_address(dev, &scat[i]); |
| 427 | |
Andy Grover | a870d62 | 2009-07-17 13:13:33 +0000 | [diff] [blame] | 428 | for (j = 0; j < dma_len; j += PAGE_SIZE) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 429 | dma_pages[page_cnt++] = |
Andy Grover | a870d62 | 2009-07-17 13:13:33 +0000 | [diff] [blame] | 430 | (dma_addr & PAGE_MASK) + j; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | ret = ib_map_phys_fmr(ibmr->fmr, |
| 434 | dma_pages, page_cnt, io_addr); |
| 435 | if (ret) |
| 436 | goto out; |
| 437 | |
| 438 | /* Success - we successfully remapped the MR, so we can |
| 439 | * safely tear down the old mapping. */ |
| 440 | rds_ib_teardown_mr(ibmr); |
| 441 | |
| 442 | ibmr->sg = scat; |
| 443 | ibmr->sg_len = nents; |
| 444 | ibmr->sg_dma_len = sg_dma_len; |
| 445 | ibmr->remap_count++; |
| 446 | |
| 447 | rds_ib_stats_inc(s_ib_rdma_mr_used); |
| 448 | ret = 0; |
| 449 | |
| 450 | out: |
| 451 | kfree(dma_pages); |
| 452 | |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | void rds_ib_sync_mr(void *trans_private, int direction) |
| 457 | { |
| 458 | struct rds_ib_mr *ibmr = trans_private; |
| 459 | struct rds_ib_device *rds_ibdev = ibmr->device; |
| 460 | |
| 461 | switch (direction) { |
| 462 | case DMA_FROM_DEVICE: |
| 463 | ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg, |
| 464 | ibmr->sg_dma_len, DMA_BIDIRECTIONAL); |
| 465 | break; |
| 466 | case DMA_TO_DEVICE: |
| 467 | ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg, |
| 468 | ibmr->sg_dma_len, DMA_BIDIRECTIONAL); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr) |
| 474 | { |
| 475 | struct rds_ib_device *rds_ibdev = ibmr->device; |
| 476 | |
| 477 | if (ibmr->sg_dma_len) { |
| 478 | ib_dma_unmap_sg(rds_ibdev->dev, |
| 479 | ibmr->sg, ibmr->sg_len, |
| 480 | DMA_BIDIRECTIONAL); |
| 481 | ibmr->sg_dma_len = 0; |
| 482 | } |
| 483 | |
| 484 | /* Release the s/g list */ |
| 485 | if (ibmr->sg_len) { |
| 486 | unsigned int i; |
| 487 | |
| 488 | for (i = 0; i < ibmr->sg_len; ++i) { |
| 489 | struct page *page = sg_page(&ibmr->sg[i]); |
| 490 | |
| 491 | /* FIXME we need a way to tell a r/w MR |
| 492 | * from a r/o MR */ |
Andy Grover | 9e2effb | 2010-03-12 16:22:32 -0800 | [diff] [blame] | 493 | BUG_ON(irqs_disabled()); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 494 | set_page_dirty(page); |
| 495 | put_page(page); |
| 496 | } |
| 497 | kfree(ibmr->sg); |
| 498 | |
| 499 | ibmr->sg = NULL; |
| 500 | ibmr->sg_len = 0; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static void rds_ib_teardown_mr(struct rds_ib_mr *ibmr) |
| 505 | { |
| 506 | unsigned int pinned = ibmr->sg_len; |
| 507 | |
| 508 | __rds_ib_teardown_mr(ibmr); |
| 509 | if (pinned) { |
| 510 | struct rds_ib_device *rds_ibdev = ibmr->device; |
| 511 | struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool; |
| 512 | |
| 513 | atomic_sub(pinned, &pool->free_pinned); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all) |
| 518 | { |
| 519 | unsigned int item_count; |
| 520 | |
| 521 | item_count = atomic_read(&pool->item_count); |
| 522 | if (free_all) |
| 523 | return item_count; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 529 | * 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] | 530 | */ |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 531 | static void llist_append_to_list(struct llist_head *llist, struct list_head *list) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 532 | { |
| 533 | struct rds_ib_mr *ibmr; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 534 | struct llist_node *node; |
| 535 | struct llist_node *next; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 536 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 537 | node = llist_del_all(llist); |
| 538 | while (node) { |
| 539 | next = node->next; |
| 540 | ibmr = llist_entry(node, struct rds_ib_mr, llnode); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 541 | list_add_tail(&ibmr->unmap_list, list); |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 542 | node = next; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | /* |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 547 | * this takes a list head of mrs and turns it into linked llist nodes |
| 548 | * of clusters. Each cluster has linked llist nodes of |
| 549 | * MR_CLUSTER_SIZE mrs that are ready for reuse. |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 550 | */ |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 551 | static void list_to_llist_nodes(struct rds_ib_mr_pool *pool, |
| 552 | struct list_head *list, |
| 553 | struct llist_node **nodes_head, |
| 554 | struct llist_node **nodes_tail) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 555 | { |
| 556 | struct rds_ib_mr *ibmr; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 557 | struct llist_node *cur = NULL; |
| 558 | struct llist_node **next = nodes_head; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 559 | |
| 560 | list_for_each_entry(ibmr, list, unmap_list) { |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 561 | cur = &ibmr->llnode; |
| 562 | *next = cur; |
| 563 | next = &cur->next; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 564 | } |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 565 | *next = NULL; |
| 566 | *nodes_tail = cur; |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 570 | * Flush our pool of MRs. |
| 571 | * At a minimum, all currently unused MRs are unmapped. |
| 572 | * If the number of MRs allocated exceeds the limit, we also try |
| 573 | * to free as many MRs as needed to get back to this limit. |
| 574 | */ |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 575 | static int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool, |
| 576 | int free_all, struct rds_ib_mr **ibmr_ret) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 577 | { |
| 578 | struct rds_ib_mr *ibmr, *next; |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 579 | struct llist_node *clean_nodes; |
| 580 | struct llist_node *clean_tail; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 581 | LIST_HEAD(unmap_list); |
| 582 | LIST_HEAD(fmr_list); |
| 583 | unsigned long unpinned = 0; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 584 | unsigned int nfreed = 0, ncleaned = 0, free_goal; |
| 585 | int ret = 0; |
| 586 | |
| 587 | rds_ib_stats_inc(s_ib_rdma_mr_pool_flush); |
| 588 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 589 | if (ibmr_ret) { |
| 590 | DEFINE_WAIT(wait); |
| 591 | while(!mutex_trylock(&pool->flush_lock)) { |
| 592 | ibmr = rds_ib_reuse_fmr(pool); |
| 593 | if (ibmr) { |
| 594 | *ibmr_ret = ibmr; |
| 595 | finish_wait(&pool->flush_wait, &wait); |
| 596 | goto out_nolock; |
| 597 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 598 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 599 | prepare_to_wait(&pool->flush_wait, &wait, |
| 600 | TASK_UNINTERRUPTIBLE); |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 601 | if (llist_empty(&pool->clean_list)) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 602 | schedule(); |
| 603 | |
| 604 | ibmr = rds_ib_reuse_fmr(pool); |
| 605 | if (ibmr) { |
| 606 | *ibmr_ret = ibmr; |
| 607 | finish_wait(&pool->flush_wait, &wait); |
| 608 | goto out_nolock; |
| 609 | } |
| 610 | } |
| 611 | finish_wait(&pool->flush_wait, &wait); |
| 612 | } else |
| 613 | mutex_lock(&pool->flush_lock); |
| 614 | |
| 615 | if (ibmr_ret) { |
| 616 | ibmr = rds_ib_reuse_fmr(pool); |
| 617 | if (ibmr) { |
| 618 | *ibmr_ret = ibmr; |
| 619 | goto out; |
| 620 | } |
| 621 | } |
| 622 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 623 | /* Get the list of all MRs to be dropped. Ordering matters - |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 624 | * we want to put drop_list ahead of free_list. |
| 625 | */ |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 626 | llist_append_to_list(&pool->drop_list, &unmap_list); |
| 627 | llist_append_to_list(&pool->free_list, &unmap_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 628 | if (free_all) |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 629 | llist_append_to_list(&pool->clean_list, &unmap_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 630 | |
| 631 | free_goal = rds_ib_flush_goal(pool, free_all); |
| 632 | |
| 633 | if (list_empty(&unmap_list)) |
| 634 | goto out; |
| 635 | |
| 636 | /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */ |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 637 | list_for_each_entry(ibmr, &unmap_list, unmap_list) |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 638 | list_add(&ibmr->fmr->list, &fmr_list); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 639 | |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 640 | ret = ib_unmap_fmr(&fmr_list); |
| 641 | if (ret) |
| 642 | printk(KERN_WARNING "RDS/IB: ib_unmap_fmr failed (err=%d)\n", ret); |
| 643 | |
| 644 | /* Now we can destroy the DMA mapping and unpin any pages */ |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 645 | list_for_each_entry_safe(ibmr, next, &unmap_list, unmap_list) { |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 646 | unpinned += ibmr->sg_len; |
| 647 | __rds_ib_teardown_mr(ibmr); |
| 648 | if (nfreed < free_goal || ibmr->remap_count >= pool->fmr_attr.max_maps) { |
| 649 | rds_ib_stats_inc(s_ib_rdma_mr_free); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 650 | list_del(&ibmr->unmap_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 651 | ib_dealloc_fmr(ibmr->fmr); |
| 652 | kfree(ibmr); |
| 653 | nfreed++; |
| 654 | } |
| 655 | ncleaned++; |
| 656 | } |
| 657 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 658 | if (!list_empty(&unmap_list)) { |
| 659 | /* we have to make sure that none of the things we're about |
| 660 | * to put on the clean list would race with other cpus trying |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 661 | * to pull items off. The llist would explode if we managed to |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 662 | * remove something from the clean list and then add it back again |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 663 | * 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] | 664 | * |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 665 | * 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] | 666 | * here before adding anything back into the clean list. |
| 667 | */ |
| 668 | wait_clean_list_grace(); |
| 669 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 670 | list_to_llist_nodes(pool, &unmap_list, &clean_nodes, &clean_tail); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 671 | if (ibmr_ret) |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 672 | *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 673 | |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 674 | /* more than one entry in llist nodes */ |
| 675 | if (clean_nodes->next) |
| 676 | llist_add_batch(clean_nodes->next, clean_tail, &pool->clean_list); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 677 | |
| 678 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 679 | |
| 680 | atomic_sub(unpinned, &pool->free_pinned); |
| 681 | atomic_sub(ncleaned, &pool->dirty_count); |
| 682 | atomic_sub(nfreed, &pool->item_count); |
| 683 | |
| 684 | out: |
| 685 | mutex_unlock(&pool->flush_lock); |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 686 | if (waitqueue_active(&pool->flush_wait)) |
| 687 | wake_up(&pool->flush_wait); |
| 688 | out_nolock: |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | static void rds_ib_mr_pool_flush_worker(struct work_struct *work) |
| 693 | { |
Chris Mason | 7a0ff5d | 2010-06-11 11:26:02 -0700 | [diff] [blame] | 694 | 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] | 695 | |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 696 | rds_ib_flush_mr_pool(pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void rds_ib_free_mr(void *trans_private, int invalidate) |
| 700 | { |
| 701 | struct rds_ib_mr *ibmr = trans_private; |
| 702 | struct rds_ib_device *rds_ibdev = ibmr->device; |
| 703 | struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 704 | |
| 705 | rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len); |
| 706 | |
| 707 | /* Return it to the pool's free list */ |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 708 | if (ibmr->remap_count >= pool->fmr_attr.max_maps) |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 709 | llist_add(&ibmr->llnode, &pool->drop_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 710 | else |
Huang Ying | 1bc144b | 2011-08-30 15:21:30 +0000 | [diff] [blame] | 711 | llist_add(&ibmr->llnode, &pool->free_list); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 712 | |
| 713 | atomic_add(ibmr->sg_len, &pool->free_pinned); |
| 714 | atomic_inc(&pool->dirty_count); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 715 | |
| 716 | /* If we've pinned too many pages, request a flush */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 717 | if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned || |
| 718 | atomic_read(&pool->dirty_count) >= pool->max_items / 10) |
Tejun Heo | c534a10 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 719 | schedule_delayed_work(&pool->flush_worker, 10); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 720 | |
| 721 | if (invalidate) { |
| 722 | if (likely(!in_interrupt())) { |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 723 | rds_ib_flush_mr_pool(pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 724 | } else { |
| 725 | /* We get here if the user created a MR marked |
| 726 | * as use_once and invalidate at the same time. */ |
Tejun Heo | c534a10 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 727 | schedule_delayed_work(&pool->flush_worker, 10); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 730 | |
| 731 | rds_ib_dev_put(rds_ibdev); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | void rds_ib_flush_mrs(void) |
| 735 | { |
| 736 | struct rds_ib_device *rds_ibdev; |
| 737 | |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 738 | down_read(&rds_ib_devices_lock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 739 | list_for_each_entry(rds_ibdev, &rds_ib_devices, list) { |
| 740 | struct rds_ib_mr_pool *pool = rds_ibdev->mr_pool; |
| 741 | |
| 742 | if (pool) |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 743 | rds_ib_flush_mr_pool(pool, 0, NULL); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 744 | } |
Zach Brown | ea81986 | 2010-07-15 12:34:33 -0700 | [diff] [blame] | 745 | up_read(&rds_ib_devices_lock); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents, |
| 749 | struct rds_sock *rs, u32 *key_ret) |
| 750 | { |
| 751 | struct rds_ib_device *rds_ibdev; |
| 752 | struct rds_ib_mr *ibmr = NULL; |
| 753 | int ret; |
| 754 | |
| 755 | rds_ibdev = rds_ib_get_device(rs->rs_bound_addr); |
| 756 | if (!rds_ibdev) { |
| 757 | ret = -ENODEV; |
| 758 | goto out; |
| 759 | } |
| 760 | |
| 761 | if (!rds_ibdev->mr_pool) { |
| 762 | ret = -ENODEV; |
| 763 | goto out; |
| 764 | } |
| 765 | |
| 766 | ibmr = rds_ib_alloc_fmr(rds_ibdev); |
Wengang Wang | 4fabb59 | 2015-07-06 14:35:11 +0800 | [diff] [blame] | 767 | if (IS_ERR(ibmr)) { |
| 768 | rds_ib_dev_put(rds_ibdev); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 769 | return ibmr; |
Wengang Wang | 4fabb59 | 2015-07-06 14:35:11 +0800 | [diff] [blame] | 770 | } |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 771 | |
| 772 | ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents); |
| 773 | if (ret == 0) |
| 774 | *key_ret = ibmr->fmr->rkey; |
| 775 | else |
| 776 | printk(KERN_WARNING "RDS/IB: map_fmr failed (errno=%d)\n", ret); |
| 777 | |
| 778 | ibmr->device = rds_ibdev; |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 779 | rds_ibdev = NULL; |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 780 | |
| 781 | out: |
| 782 | if (ret) { |
| 783 | if (ibmr) |
| 784 | rds_ib_free_mr(ibmr, 0); |
| 785 | ibmr = ERR_PTR(ret); |
| 786 | } |
Zach Brown | 3e0249f | 2010-05-18 15:48:51 -0700 | [diff] [blame] | 787 | if (rds_ibdev) |
| 788 | rds_ib_dev_put(rds_ibdev); |
Andy Grover | 08b48a1 | 2009-02-24 15:30:32 +0000 | [diff] [blame] | 789 | return ibmr; |
| 790 | } |
Chris Mason | 6fa70da | 2010-06-11 11:17:59 -0700 | [diff] [blame] | 791 | |