blob: c8c1e3ae8d84740d240f38b2f1ef64965f073598 [file] [log] [blame]
Andy Grover08b48a12009-02-24 15:30:32 +00001/*
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -07002 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
Andy Grover08b48a12009-02-24 15:30:32 +00003 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Chris Mason764f2dd2010-04-22 21:59:15 -040035#include <linux/rculist.h>
Huang Ying1bc144b2011-08-30 15:21:30 +000036#include <linux/llist.h>
Andy Grover08b48a12009-02-24 15:30:32 +000037
Sowmini Varadhan0cb43962016-06-13 09:44:26 -070038#include "rds_single_path.h"
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -080039#include "ib_mr.h"
40
41struct workqueue_struct *rds_ib_mr_wq;
Andy Grover08b48a12009-02-24 15:30:32 +000042
Andy Grover08b48a12009-02-24 15:30:32 +000043static struct rds_ib_device *rds_ib_get_device(__be32 ipaddr)
44{
45 struct rds_ib_device *rds_ibdev;
46 struct rds_ib_ipaddr *i_ipaddr;
47
Zach Brownea819862010-07-15 12:34:33 -070048 rcu_read_lock();
49 list_for_each_entry_rcu(rds_ibdev, &rds_ib_devices, list) {
Chris Mason764f2dd2010-04-22 21:59:15 -040050 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
Andy Grover08b48a12009-02-24 15:30:32 +000051 if (i_ipaddr->ipaddr == ipaddr) {
Reshetova, Elena50d61ff2017-07-04 15:53:15 +030052 refcount_inc(&rds_ibdev->refcount);
Chris Mason764f2dd2010-04-22 21:59:15 -040053 rcu_read_unlock();
Andy Grover08b48a12009-02-24 15:30:32 +000054 return rds_ibdev;
55 }
56 }
Andy Grover08b48a12009-02-24 15:30:32 +000057 }
Zach Brownea819862010-07-15 12:34:33 -070058 rcu_read_unlock();
Andy Grover08b48a12009-02-24 15:30:32 +000059
60 return NULL;
61}
62
63static int rds_ib_add_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
64{
65 struct rds_ib_ipaddr *i_ipaddr;
66
67 i_ipaddr = kmalloc(sizeof *i_ipaddr, GFP_KERNEL);
68 if (!i_ipaddr)
69 return -ENOMEM;
70
71 i_ipaddr->ipaddr = ipaddr;
72
73 spin_lock_irq(&rds_ibdev->spinlock);
Chris Mason764f2dd2010-04-22 21:59:15 -040074 list_add_tail_rcu(&i_ipaddr->list, &rds_ibdev->ipaddr_list);
Andy Grover08b48a12009-02-24 15:30:32 +000075 spin_unlock_irq(&rds_ibdev->spinlock);
76
77 return 0;
78}
79
80static void rds_ib_remove_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr)
81{
Andy Grover4a818022010-04-23 11:04:21 -070082 struct rds_ib_ipaddr *i_ipaddr;
Chris Mason764f2dd2010-04-22 21:59:15 -040083 struct rds_ib_ipaddr *to_free = NULL;
84
Andy Grover08b48a12009-02-24 15:30:32 +000085
86 spin_lock_irq(&rds_ibdev->spinlock);
Chris Mason764f2dd2010-04-22 21:59:15 -040087 list_for_each_entry_rcu(i_ipaddr, &rds_ibdev->ipaddr_list, list) {
Andy Grover08b48a12009-02-24 15:30:32 +000088 if (i_ipaddr->ipaddr == ipaddr) {
Chris Mason764f2dd2010-04-22 21:59:15 -040089 list_del_rcu(&i_ipaddr->list);
90 to_free = i_ipaddr;
Andy Grover08b48a12009-02-24 15:30:32 +000091 break;
92 }
93 }
94 spin_unlock_irq(&rds_ibdev->spinlock);
Chris Mason764f2dd2010-04-22 21:59:15 -040095
Santosh Shilimkar59fe4602012-02-03 11:09:23 -050096 if (to_free)
97 kfree_rcu(to_free, rcu);
Andy Grover08b48a12009-02-24 15:30:32 +000098}
99
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700100int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev,
101 struct in6_addr *ipaddr)
Andy Grover08b48a12009-02-24 15:30:32 +0000102{
103 struct rds_ib_device *rds_ibdev_old;
104
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700105 rds_ibdev_old = rds_ib_get_device(ipaddr->s6_addr32[3]);
santosh.shilimkar@oracle.come1f475a2015-08-22 15:45:25 -0700106 if (!rds_ibdev_old)
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700107 return rds_ib_add_ipaddr(rds_ibdev, ipaddr->s6_addr32[3]);
santosh.shilimkar@oracle.come1f475a2015-08-22 15:45:25 -0700108
109 if (rds_ibdev_old != rds_ibdev) {
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700110 rds_ib_remove_ipaddr(rds_ibdev_old, ipaddr->s6_addr32[3]);
Zach Brown3e0249f2010-05-18 15:48:51 -0700111 rds_ib_dev_put(rds_ibdev_old);
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700112 return rds_ib_add_ipaddr(rds_ibdev, ipaddr->s6_addr32[3]);
Zach Brown3e0249f2010-05-18 15:48:51 -0700113 }
santosh.shilimkar@oracle.come1f475a2015-08-22 15:45:25 -0700114 rds_ib_dev_put(rds_ibdev_old);
Andy Grover08b48a12009-02-24 15:30:32 +0000115
santosh.shilimkar@oracle.come1f475a2015-08-22 15:45:25 -0700116 return 0;
Andy Grover08b48a12009-02-24 15:30:32 +0000117}
118
Andy Grover745cbcc2009-04-01 08:20:19 +0000119void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
Andy Grover08b48a12009-02-24 15:30:32 +0000120{
121 struct rds_ib_connection *ic = conn->c_transport_data;
122
123 /* conn was previously on the nodev_conns_list */
124 spin_lock_irq(&ib_nodev_conns_lock);
125 BUG_ON(list_empty(&ib_nodev_conns));
126 BUG_ON(list_empty(&ic->ib_node));
127 list_del(&ic->ib_node);
Andy Grover08b48a12009-02-24 15:30:32 +0000128
Dan Carpenteraef3ea32010-09-18 13:44:14 +0000129 spin_lock(&rds_ibdev->spinlock);
Andy Grover08b48a12009-02-24 15:30:32 +0000130 list_add_tail(&ic->ib_node, &rds_ibdev->conn_list);
Dan Carpenteraef3ea32010-09-18 13:44:14 +0000131 spin_unlock(&rds_ibdev->spinlock);
Andy Grover08b48a12009-02-24 15:30:32 +0000132 spin_unlock_irq(&ib_nodev_conns_lock);
133
Andy Grover745cbcc2009-04-01 08:20:19 +0000134 ic->rds_ibdev = rds_ibdev;
Reshetova, Elena50d61ff2017-07-04 15:53:15 +0300135 refcount_inc(&rds_ibdev->refcount);
Andy Grover08b48a12009-02-24 15:30:32 +0000136}
137
Andy Grover745cbcc2009-04-01 08:20:19 +0000138void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn)
139{
140 struct rds_ib_connection *ic = conn->c_transport_data;
141
142 /* place conn on nodev_conns_list */
143 spin_lock(&ib_nodev_conns_lock);
144
145 spin_lock_irq(&rds_ibdev->spinlock);
146 BUG_ON(list_empty(&ic->ib_node));
147 list_del(&ic->ib_node);
148 spin_unlock_irq(&rds_ibdev->spinlock);
149
150 list_add_tail(&ic->ib_node, &ib_nodev_conns);
151
152 spin_unlock(&ib_nodev_conns_lock);
153
154 ic->rds_ibdev = NULL;
Zach Brown3e0249f2010-05-18 15:48:51 -0700155 rds_ib_dev_put(rds_ibdev);
Andy Grover745cbcc2009-04-01 08:20:19 +0000156}
157
Zach Brown8aeb1ba2010-06-25 14:58:16 -0700158void rds_ib_destroy_nodev_conns(void)
Andy Grover08b48a12009-02-24 15:30:32 +0000159{
160 struct rds_ib_connection *ic, *_ic;
161 LIST_HEAD(tmp_list);
162
163 /* avoid calling conn_destroy with irqs off */
Zach Brown8aeb1ba2010-06-25 14:58:16 -0700164 spin_lock_irq(&ib_nodev_conns_lock);
165 list_splice(&ib_nodev_conns, &tmp_list);
166 spin_unlock_irq(&ib_nodev_conns_lock);
Andy Grover08b48a12009-02-24 15:30:32 +0000167
Andy Grover433d3082009-10-30 08:51:55 +0000168 list_for_each_entry_safe(ic, _ic, &tmp_list, ib_node)
Andy Grover08b48a12009-02-24 15:30:32 +0000169 rds_conn_destroy(ic->conn);
Andy Grover08b48a12009-02-24 15:30:32 +0000170}
171
Andy Grover08b48a12009-02-24 15:30:32 +0000172void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo)
173{
Santosh Shilimkar06766512015-09-10 21:20:57 -0700174 struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
Andy Grover08b48a12009-02-24 15:30:32 +0000175
Santosh Shilimkar06766512015-09-10 21:20:57 -0700176 iinfo->rdma_mr_max = pool_1m->max_items;
177 iinfo->rdma_mr_size = pool_1m->fmr_attr.max_pages;
Andy Grover08b48a12009-02-24 15:30:32 +0000178}
179
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700180#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700181void rds6_ib_get_mr_info(struct rds_ib_device *rds_ibdev,
182 struct rds6_info_rdma_connection *iinfo6)
183{
184 struct rds_ib_mr_pool *pool_1m = rds_ibdev->mr_1m_pool;
185
186 iinfo6->rdma_mr_max = pool_1m->max_items;
187 iinfo6->rdma_mr_size = pool_1m->fmr_attr.max_pages;
188}
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700189#endif
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700190
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800191struct rds_ib_mr *rds_ib_reuse_mr(struct rds_ib_mr_pool *pool)
Andy Grover08b48a12009-02-24 15:30:32 +0000192{
193 struct rds_ib_mr *ibmr = NULL;
Huang Ying1bc144b2011-08-30 15:21:30 +0000194 struct llist_node *ret;
Gerd Rauschc9467442019-07-16 15:28:57 -0700195 unsigned long flags;
Andy Grover08b48a12009-02-24 15:30:32 +0000196
Gerd Rauschc9467442019-07-16 15:28:57 -0700197 spin_lock_irqsave(&pool->clean_lock, flags);
Huang Ying1bc144b2011-08-30 15:21:30 +0000198 ret = llist_del_first(&pool->clean_list);
Gerd Rauschc9467442019-07-16 15:28:57 -0700199 spin_unlock_irqrestore(&pool->clean_lock, flags);
santosh.shilimkar@oracle.comdb427532016-03-01 15:20:51 -0800200 if (ret) {
Huang Ying1bc144b2011-08-30 15:21:30 +0000201 ibmr = llist_entry(ret, struct rds_ib_mr, llnode);
santosh.shilimkar@oracle.comdb427532016-03-01 15:20:51 -0800202 if (pool->pool_type == RDS_IB_MR_8K_POOL)
203 rds_ib_stats_inc(s_ib_rdma_mr_8k_reused);
204 else
205 rds_ib_stats_inc(s_ib_rdma_mr_1m_reused);
206 }
Andy Grover08b48a12009-02-24 15:30:32 +0000207
208 return ibmr;
209}
210
Andy Grover08b48a12009-02-24 15:30:32 +0000211void rds_ib_sync_mr(void *trans_private, int direction)
212{
213 struct rds_ib_mr *ibmr = trans_private;
214 struct rds_ib_device *rds_ibdev = ibmr->device;
215
216 switch (direction) {
217 case DMA_FROM_DEVICE:
218 ib_dma_sync_sg_for_cpu(rds_ibdev->dev, ibmr->sg,
219 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
220 break;
221 case DMA_TO_DEVICE:
222 ib_dma_sync_sg_for_device(rds_ibdev->dev, ibmr->sg,
223 ibmr->sg_dma_len, DMA_BIDIRECTIONAL);
224 break;
225 }
226}
227
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800228void __rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
Andy Grover08b48a12009-02-24 15:30:32 +0000229{
230 struct rds_ib_device *rds_ibdev = ibmr->device;
231
232 if (ibmr->sg_dma_len) {
233 ib_dma_unmap_sg(rds_ibdev->dev,
234 ibmr->sg, ibmr->sg_len,
235 DMA_BIDIRECTIONAL);
236 ibmr->sg_dma_len = 0;
237 }
238
239 /* Release the s/g list */
240 if (ibmr->sg_len) {
241 unsigned int i;
242
243 for (i = 0; i < ibmr->sg_len; ++i) {
244 struct page *page = sg_page(&ibmr->sg[i]);
245
246 /* FIXME we need a way to tell a r/w MR
247 * from a r/o MR */
santosh.shilimkar@oracle.com5c240fa2015-08-22 15:45:31 -0700248 WARN_ON(!page->mapping && irqs_disabled());
Andy Grover08b48a12009-02-24 15:30:32 +0000249 set_page_dirty(page);
250 put_page(page);
251 }
252 kfree(ibmr->sg);
253
254 ibmr->sg = NULL;
255 ibmr->sg_len = 0;
256 }
257}
258
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800259void rds_ib_teardown_mr(struct rds_ib_mr *ibmr)
Andy Grover08b48a12009-02-24 15:30:32 +0000260{
261 unsigned int pinned = ibmr->sg_len;
262
263 __rds_ib_teardown_mr(ibmr);
264 if (pinned) {
Santosh Shilimkar26139dc2015-09-15 18:20:35 -0700265 struct rds_ib_mr_pool *pool = ibmr->pool;
Andy Grover08b48a12009-02-24 15:30:32 +0000266
267 atomic_sub(pinned, &pool->free_pinned);
268 }
269}
270
271static inline unsigned int rds_ib_flush_goal(struct rds_ib_mr_pool *pool, int free_all)
272{
273 unsigned int item_count;
274
275 item_count = atomic_read(&pool->item_count);
276 if (free_all)
277 return item_count;
278
279 return 0;
280}
281
282/*
Huang Ying1bc144b2011-08-30 15:21:30 +0000283 * given an llist of mrs, put them all into the list_head for more processing
Chris Mason6fa70da2010-06-11 11:17:59 -0700284 */
Wengang Wang6116c202015-08-25 12:02:00 -0700285static unsigned int llist_append_to_list(struct llist_head *llist,
286 struct list_head *list)
Chris Mason6fa70da2010-06-11 11:17:59 -0700287{
288 struct rds_ib_mr *ibmr;
Huang Ying1bc144b2011-08-30 15:21:30 +0000289 struct llist_node *node;
290 struct llist_node *next;
Wengang Wang6116c202015-08-25 12:02:00 -0700291 unsigned int count = 0;
Chris Mason6fa70da2010-06-11 11:17:59 -0700292
Huang Ying1bc144b2011-08-30 15:21:30 +0000293 node = llist_del_all(llist);
294 while (node) {
295 next = node->next;
296 ibmr = llist_entry(node, struct rds_ib_mr, llnode);
Chris Mason6fa70da2010-06-11 11:17:59 -0700297 list_add_tail(&ibmr->unmap_list, list);
Huang Ying1bc144b2011-08-30 15:21:30 +0000298 node = next;
Wengang Wang6116c202015-08-25 12:02:00 -0700299 count++;
Chris Mason6fa70da2010-06-11 11:17:59 -0700300 }
Wengang Wang6116c202015-08-25 12:02:00 -0700301 return count;
Chris Mason6fa70da2010-06-11 11:17:59 -0700302}
303
304/*
Huang Ying1bc144b2011-08-30 15:21:30 +0000305 * this takes a list head of mrs and turns it into linked llist nodes
306 * of clusters. Each cluster has linked llist nodes of
307 * MR_CLUSTER_SIZE mrs that are ready for reuse.
Chris Mason6fa70da2010-06-11 11:17:59 -0700308 */
Gerd Rauschc9467442019-07-16 15:28:57 -0700309static void list_to_llist_nodes(struct list_head *list,
Huang Ying1bc144b2011-08-30 15:21:30 +0000310 struct llist_node **nodes_head,
311 struct llist_node **nodes_tail)
Chris Mason6fa70da2010-06-11 11:17:59 -0700312{
313 struct rds_ib_mr *ibmr;
Huang Ying1bc144b2011-08-30 15:21:30 +0000314 struct llist_node *cur = NULL;
315 struct llist_node **next = nodes_head;
Chris Mason6fa70da2010-06-11 11:17:59 -0700316
317 list_for_each_entry(ibmr, list, unmap_list) {
Huang Ying1bc144b2011-08-30 15:21:30 +0000318 cur = &ibmr->llnode;
319 *next = cur;
320 next = &cur->next;
Chris Mason6fa70da2010-06-11 11:17:59 -0700321 }
Huang Ying1bc144b2011-08-30 15:21:30 +0000322 *next = NULL;
323 *nodes_tail = cur;
Chris Mason6fa70da2010-06-11 11:17:59 -0700324}
325
326/*
Andy Grover08b48a12009-02-24 15:30:32 +0000327 * Flush our pool of MRs.
328 * At a minimum, all currently unused MRs are unmapped.
329 * If the number of MRs allocated exceeds the limit, we also try
330 * to free as many MRs as needed to get back to this limit.
331 */
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800332int rds_ib_flush_mr_pool(struct rds_ib_mr_pool *pool,
333 int free_all, struct rds_ib_mr **ibmr_ret)
Andy Grover08b48a12009-02-24 15:30:32 +0000334{
santosh.shilimkar@oracle.com490ea592016-03-01 15:20:48 -0800335 struct rds_ib_mr *ibmr;
Huang Ying1bc144b2011-08-30 15:21:30 +0000336 struct llist_node *clean_nodes;
337 struct llist_node *clean_tail;
Andy Grover08b48a12009-02-24 15:30:32 +0000338 LIST_HEAD(unmap_list);
Andy Grover08b48a12009-02-24 15:30:32 +0000339 unsigned long unpinned = 0;
Wengang Wang6116c202015-08-25 12:02:00 -0700340 unsigned int nfreed = 0, dirty_to_clean = 0, free_goal;
Andy Grover08b48a12009-02-24 15:30:32 +0000341
Santosh Shilimkar06766512015-09-10 21:20:57 -0700342 if (pool->pool_type == RDS_IB_MR_8K_POOL)
343 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_flush);
344 else
345 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_flush);
Andy Grover08b48a12009-02-24 15:30:32 +0000346
Chris Mason6fa70da2010-06-11 11:17:59 -0700347 if (ibmr_ret) {
348 DEFINE_WAIT(wait);
Santosh Shilimkar06766512015-09-10 21:20:57 -0700349 while (!mutex_trylock(&pool->flush_lock)) {
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800350 ibmr = rds_ib_reuse_mr(pool);
Chris Mason6fa70da2010-06-11 11:17:59 -0700351 if (ibmr) {
352 *ibmr_ret = ibmr;
353 finish_wait(&pool->flush_wait, &wait);
354 goto out_nolock;
355 }
Andy Grover08b48a12009-02-24 15:30:32 +0000356
Chris Mason6fa70da2010-06-11 11:17:59 -0700357 prepare_to_wait(&pool->flush_wait, &wait,
358 TASK_UNINTERRUPTIBLE);
Huang Ying1bc144b2011-08-30 15:21:30 +0000359 if (llist_empty(&pool->clean_list))
Chris Mason6fa70da2010-06-11 11:17:59 -0700360 schedule();
361
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800362 ibmr = rds_ib_reuse_mr(pool);
Chris Mason6fa70da2010-06-11 11:17:59 -0700363 if (ibmr) {
364 *ibmr_ret = ibmr;
365 finish_wait(&pool->flush_wait, &wait);
366 goto out_nolock;
367 }
368 }
369 finish_wait(&pool->flush_wait, &wait);
370 } else
371 mutex_lock(&pool->flush_lock);
372
373 if (ibmr_ret) {
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800374 ibmr = rds_ib_reuse_mr(pool);
Chris Mason6fa70da2010-06-11 11:17:59 -0700375 if (ibmr) {
376 *ibmr_ret = ibmr;
377 goto out;
378 }
379 }
380
Andy Grover08b48a12009-02-24 15:30:32 +0000381 /* Get the list of all MRs to be dropped. Ordering matters -
Chris Mason6fa70da2010-06-11 11:17:59 -0700382 * we want to put drop_list ahead of free_list.
383 */
Wengang Wang6116c202015-08-25 12:02:00 -0700384 dirty_to_clean = llist_append_to_list(&pool->drop_list, &unmap_list);
385 dirty_to_clean += llist_append_to_list(&pool->free_list, &unmap_list);
Gerd Rauschc9467442019-07-16 15:28:57 -0700386 if (free_all) {
387 unsigned long flags;
388
389 spin_lock_irqsave(&pool->clean_lock, flags);
Huang Ying1bc144b2011-08-30 15:21:30 +0000390 llist_append_to_list(&pool->clean_list, &unmap_list);
Gerd Rauschc9467442019-07-16 15:28:57 -0700391 spin_unlock_irqrestore(&pool->clean_lock, flags);
392 }
Andy Grover08b48a12009-02-24 15:30:32 +0000393
394 free_goal = rds_ib_flush_goal(pool, free_all);
395
396 if (list_empty(&unmap_list))
397 goto out;
398
Avinash Repaka16591852016-03-01 15:20:54 -0800399 if (pool->use_fastreg)
400 rds_ib_unreg_frmr(&unmap_list, &nfreed, &unpinned, free_goal);
401 else
402 rds_ib_unreg_fmr(&unmap_list, &nfreed, &unpinned, free_goal);
Andy Grover08b48a12009-02-24 15:30:32 +0000403
Chris Mason6fa70da2010-06-11 11:17:59 -0700404 if (!list_empty(&unmap_list)) {
Gerd Rauschc9467442019-07-16 15:28:57 -0700405 unsigned long flags;
Chris Mason6fa70da2010-06-11 11:17:59 -0700406
Gerd Rauschc9467442019-07-16 15:28:57 -0700407 list_to_llist_nodes(&unmap_list, &clean_nodes, &clean_tail);
Zhu Yanjun85cb9282019-06-06 04:00:03 -0400408 if (ibmr_ret) {
Huang Ying1bc144b2011-08-30 15:21:30 +0000409 *ibmr_ret = llist_entry(clean_nodes, struct rds_ib_mr, llnode);
Zhu Yanjun85cb9282019-06-06 04:00:03 -0400410 clean_nodes = clean_nodes->next;
411 }
Huang Ying1bc144b2011-08-30 15:21:30 +0000412 /* more than one entry in llist nodes */
Gerd Rauschc9467442019-07-16 15:28:57 -0700413 if (clean_nodes) {
414 spin_lock_irqsave(&pool->clean_lock, flags);
Zhu Yanjun85cb9282019-06-06 04:00:03 -0400415 llist_add_batch(clean_nodes, clean_tail,
416 &pool->clean_list);
Gerd Rauschc9467442019-07-16 15:28:57 -0700417 spin_unlock_irqrestore(&pool->clean_lock, flags);
418 }
Chris Mason6fa70da2010-06-11 11:17:59 -0700419 }
Andy Grover08b48a12009-02-24 15:30:32 +0000420
421 atomic_sub(unpinned, &pool->free_pinned);
Wengang Wang6116c202015-08-25 12:02:00 -0700422 atomic_sub(dirty_to_clean, &pool->dirty_count);
Andy Grover08b48a12009-02-24 15:30:32 +0000423 atomic_sub(nfreed, &pool->item_count);
424
425out:
426 mutex_unlock(&pool->flush_lock);
Chris Mason6fa70da2010-06-11 11:17:59 -0700427 if (waitqueue_active(&pool->flush_wait))
428 wake_up(&pool->flush_wait);
429out_nolock:
santosh.shilimkar@oracle.com490ea592016-03-01 15:20:48 -0800430 return 0;
431}
432
433struct rds_ib_mr *rds_ib_try_reuse_ibmr(struct rds_ib_mr_pool *pool)
434{
435 struct rds_ib_mr *ibmr = NULL;
436 int iter = 0;
437
santosh.shilimkar@oracle.com490ea592016-03-01 15:20:48 -0800438 while (1) {
439 ibmr = rds_ib_reuse_mr(pool);
440 if (ibmr)
441 return ibmr;
442
443 if (atomic_inc_return(&pool->item_count) <= pool->max_items)
444 break;
445
446 atomic_dec(&pool->item_count);
447
448 if (++iter > 2) {
449 if (pool->pool_type == RDS_IB_MR_8K_POOL)
450 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted);
451 else
452 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted);
Gerd Rauschaea01a22019-07-16 15:29:07 -0700453 break;
santosh.shilimkar@oracle.com490ea592016-03-01 15:20:48 -0800454 }
455
456 /* We do have some empty MRs. Flush them out. */
457 if (pool->pool_type == RDS_IB_MR_8K_POOL)
458 rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait);
459 else
460 rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait);
461
462 rds_ib_flush_mr_pool(pool, 0, &ibmr);
463 if (ibmr)
464 return ibmr;
465 }
466
Gerd Rauschaea01a22019-07-16 15:29:07 -0700467 return NULL;
Andy Grover08b48a12009-02-24 15:30:32 +0000468}
469
470static void rds_ib_mr_pool_flush_worker(struct work_struct *work)
471{
Chris Mason7a0ff5d2010-06-11 11:26:02 -0700472 struct rds_ib_mr_pool *pool = container_of(work, struct rds_ib_mr_pool, flush_worker.work);
Andy Grover08b48a12009-02-24 15:30:32 +0000473
Chris Mason6fa70da2010-06-11 11:17:59 -0700474 rds_ib_flush_mr_pool(pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000475}
476
477void rds_ib_free_mr(void *trans_private, int invalidate)
478{
479 struct rds_ib_mr *ibmr = trans_private;
Santosh Shilimkar26139dc2015-09-15 18:20:35 -0700480 struct rds_ib_mr_pool *pool = ibmr->pool;
Andy Grover08b48a12009-02-24 15:30:32 +0000481 struct rds_ib_device *rds_ibdev = ibmr->device;
Andy Grover08b48a12009-02-24 15:30:32 +0000482
483 rdsdebug("RDS/IB: free_mr nents %u\n", ibmr->sg_len);
484
485 /* Return it to the pool's free list */
Avinash Repaka16591852016-03-01 15:20:54 -0800486 if (rds_ibdev->use_fastreg)
487 rds_ib_free_frmr_list(ibmr);
488 else
489 rds_ib_free_fmr_list(ibmr);
Andy Grover08b48a12009-02-24 15:30:32 +0000490
491 atomic_add(ibmr->sg_len, &pool->free_pinned);
492 atomic_inc(&pool->dirty_count);
Andy Grover08b48a12009-02-24 15:30:32 +0000493
494 /* If we've pinned too many pages, request a flush */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800495 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
santosh.shilimkar@oracle.comef5217a2015-08-25 12:02:02 -0700496 atomic_read(&pool->dirty_count) >= pool->max_items / 5)
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800497 queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
Andy Grover08b48a12009-02-24 15:30:32 +0000498
499 if (invalidate) {
500 if (likely(!in_interrupt())) {
Chris Mason6fa70da2010-06-11 11:17:59 -0700501 rds_ib_flush_mr_pool(pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000502 } else {
503 /* We get here if the user created a MR marked
santosh.shilimkar@oracle.comad1d7dc2015-08-25 12:02:01 -0700504 * as use_once and invalidate at the same time.
505 */
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800506 queue_delayed_work(rds_ib_mr_wq,
santosh.shilimkar@oracle.comad1d7dc2015-08-25 12:02:01 -0700507 &pool->flush_worker, 10);
Andy Grover08b48a12009-02-24 15:30:32 +0000508 }
509 }
Zach Brown3e0249f2010-05-18 15:48:51 -0700510
511 rds_ib_dev_put(rds_ibdev);
Andy Grover08b48a12009-02-24 15:30:32 +0000512}
513
514void rds_ib_flush_mrs(void)
515{
516 struct rds_ib_device *rds_ibdev;
517
Zach Brownea819862010-07-15 12:34:33 -0700518 down_read(&rds_ib_devices_lock);
Andy Grover08b48a12009-02-24 15:30:32 +0000519 list_for_each_entry(rds_ibdev, &rds_ib_devices, list) {
Santosh Shilimkar06766512015-09-10 21:20:57 -0700520 if (rds_ibdev->mr_8k_pool)
521 rds_ib_flush_mr_pool(rds_ibdev->mr_8k_pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000522
Santosh Shilimkar06766512015-09-10 21:20:57 -0700523 if (rds_ibdev->mr_1m_pool)
524 rds_ib_flush_mr_pool(rds_ibdev->mr_1m_pool, 0, NULL);
Andy Grover08b48a12009-02-24 15:30:32 +0000525 }
Zach Brownea819862010-07-15 12:34:33 -0700526 up_read(&rds_ib_devices_lock);
Andy Grover08b48a12009-02-24 15:30:32 +0000527}
528
529void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
Avinash Repaka9e630bc2018-07-24 20:31:58 -0700530 struct rds_sock *rs, u32 *key_ret,
531 struct rds_connection *conn)
Andy Grover08b48a12009-02-24 15:30:32 +0000532{
533 struct rds_ib_device *rds_ibdev;
534 struct rds_ib_mr *ibmr = NULL;
Avinash Repaka9e630bc2018-07-24 20:31:58 -0700535 struct rds_ib_connection *ic = NULL;
Andy Grover08b48a12009-02-24 15:30:32 +0000536 int ret;
537
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700538 rds_ibdev = rds_ib_get_device(rs->rs_bound_addr.s6_addr32[3]);
Andy Grover08b48a12009-02-24 15:30:32 +0000539 if (!rds_ibdev) {
540 ret = -ENODEV;
541 goto out;
542 }
543
Avinash Repaka9e630bc2018-07-24 20:31:58 -0700544 if (conn)
545 ic = conn->c_transport_data;
546
Santosh Shilimkar06766512015-09-10 21:20:57 -0700547 if (!rds_ibdev->mr_8k_pool || !rds_ibdev->mr_1m_pool) {
Andy Grover08b48a12009-02-24 15:30:32 +0000548 ret = -ENODEV;
549 goto out;
550 }
551
Avinash Repaka16591852016-03-01 15:20:54 -0800552 if (rds_ibdev->use_fastreg)
553 ibmr = rds_ib_reg_frmr(rds_ibdev, ic, sg, nents, key_ret);
554 else
555 ibmr = rds_ib_reg_fmr(rds_ibdev, sg, nents, key_ret);
Avinash Repaka9e630bc2018-07-24 20:31:58 -0700556 if (IS_ERR(ibmr)) {
557 ret = PTR_ERR(ibmr);
558 pr_warn("RDS/IB: rds_ib_get_mr failed (errno=%d)\n", ret);
559 } else {
560 return ibmr;
561 }
Andy Grover08b48a12009-02-24 15:30:32 +0000562
563 out:
Zach Brown3e0249f2010-05-18 15:48:51 -0700564 if (rds_ibdev)
565 rds_ib_dev_put(rds_ibdev);
santosh.shilimkar@oracle.com490ea592016-03-01 15:20:48 -0800566
Avinash Repaka9e630bc2018-07-24 20:31:58 -0700567 return ERR_PTR(ret);
Andy Grover08b48a12009-02-24 15:30:32 +0000568}
Chris Mason6fa70da2010-06-11 11:17:59 -0700569
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800570void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *pool)
571{
572 cancel_delayed_work_sync(&pool->flush_worker);
573 rds_ib_flush_mr_pool(pool, 1, NULL);
574 WARN_ON(atomic_read(&pool->item_count));
575 WARN_ON(atomic_read(&pool->free_pinned));
576 kfree(pool);
577}
578
579struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *rds_ibdev,
580 int pool_type)
581{
582 struct rds_ib_mr_pool *pool;
583
584 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
585 if (!pool)
586 return ERR_PTR(-ENOMEM);
587
588 pool->pool_type = pool_type;
589 init_llist_head(&pool->free_list);
590 init_llist_head(&pool->drop_list);
591 init_llist_head(&pool->clean_list);
Gerd Rauschc9467442019-07-16 15:28:57 -0700592 spin_lock_init(&pool->clean_lock);
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800593 mutex_init(&pool->flush_lock);
594 init_waitqueue_head(&pool->flush_wait);
595 INIT_DELAYED_WORK(&pool->flush_worker, rds_ib_mr_pool_flush_worker);
596
597 if (pool_type == RDS_IB_MR_1M_POOL) {
598 /* +1 allows for unaligned MRs */
599 pool->fmr_attr.max_pages = RDS_MR_1M_MSG_SIZE + 1;
Avinash Repakab1fb67f2017-10-04 12:11:29 -0700600 pool->max_items = rds_ibdev->max_1m_mrs;
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800601 } else {
602 /* pool_type == RDS_IB_MR_8K_POOL */
603 pool->fmr_attr.max_pages = RDS_MR_8K_MSG_SIZE + 1;
Avinash Repakab1fb67f2017-10-04 12:11:29 -0700604 pool->max_items = rds_ibdev->max_8k_mrs;
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800605 }
606
607 pool->max_free_pinned = pool->max_items * pool->fmr_attr.max_pages / 4;
608 pool->fmr_attr.max_maps = rds_ibdev->fmr_max_remaps;
609 pool->fmr_attr.page_shift = PAGE_SHIFT;
610 pool->max_items_soft = rds_ibdev->max_mrs * 3 / 4;
Avinash Repaka16591852016-03-01 15:20:54 -0800611 pool->use_fastreg = rds_ibdev->use_fastreg;
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800612
613 return pool;
614}
615
616int rds_ib_mr_init(void)
617{
Bhaktipriya Shridhar231edca2016-06-08 01:03:45 +0530618 rds_ib_mr_wq = alloc_workqueue("rds_mr_flushd", WQ_MEM_RECLAIM, 0);
santosh.shilimkar@oracle.comf6df6832016-03-01 15:20:46 -0800619 if (!rds_ib_mr_wq)
620 return -ENOMEM;
621 return 0;
622}
623
624/* By the time this is called all the IB devices should have been torn down and
625 * had their pools freed. As each pool is freed its work struct is waited on,
626 * so the pool flushing work queue should be idle by the time we get here.
627 */
628void rds_ib_mr_exit(void)
629{
630 destroy_workqueue(rds_ib_mr_wq);
631}