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