• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 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 "ib_mr.h"
34 
rds_ib_alloc_frmr(struct rds_ib_device * rds_ibdev,int npages)35 static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
36 					   int npages)
37 {
38 	struct rds_ib_mr_pool *pool;
39 	struct rds_ib_mr *ibmr = NULL;
40 	struct rds_ib_frmr *frmr;
41 	int err = 0;
42 
43 	if (npages <= RDS_MR_8K_MSG_SIZE)
44 		pool = rds_ibdev->mr_8k_pool;
45 	else
46 		pool = rds_ibdev->mr_1m_pool;
47 
48 	ibmr = rds_ib_try_reuse_ibmr(pool);
49 	if (ibmr)
50 		return ibmr;
51 
52 	ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL,
53 			    rdsibdev_to_node(rds_ibdev));
54 	if (!ibmr) {
55 		err = -ENOMEM;
56 		goto out_no_cigar;
57 	}
58 
59 	frmr = &ibmr->u.frmr;
60 	frmr->mr = ib_alloc_mr(rds_ibdev->pd, IB_MR_TYPE_MEM_REG,
61 			 pool->fmr_attr.max_pages);
62 	if (IS_ERR(frmr->mr)) {
63 		pr_warn("RDS/IB: %s failed to allocate MR", __func__);
64 		err = PTR_ERR(frmr->mr);
65 		goto out_no_cigar;
66 	}
67 
68 	ibmr->pool = pool;
69 	if (pool->pool_type == RDS_IB_MR_8K_POOL)
70 		rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
71 	else
72 		rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
73 
74 	if (atomic_read(&pool->item_count) > pool->max_items_soft)
75 		pool->max_items_soft = pool->max_items;
76 
77 	frmr->fr_state = FRMR_IS_FREE;
78 	return ibmr;
79 
80 out_no_cigar:
81 	kfree(ibmr);
82 	atomic_dec(&pool->item_count);
83 	return ERR_PTR(err);
84 }
85 
rds_ib_free_frmr(struct rds_ib_mr * ibmr,bool drop)86 static void rds_ib_free_frmr(struct rds_ib_mr *ibmr, bool drop)
87 {
88 	struct rds_ib_mr_pool *pool = ibmr->pool;
89 
90 	if (drop)
91 		llist_add(&ibmr->llnode, &pool->drop_list);
92 	else
93 		llist_add(&ibmr->llnode, &pool->free_list);
94 	atomic_add(ibmr->sg_len, &pool->free_pinned);
95 	atomic_inc(&pool->dirty_count);
96 
97 	/* If we've pinned too many pages, request a flush */
98 	if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
99 	    atomic_read(&pool->dirty_count) >= pool->max_items / 5)
100 		queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
101 }
102 
rds_ib_post_reg_frmr(struct rds_ib_mr * ibmr)103 static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
104 {
105 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
106 	struct ib_send_wr *failed_wr;
107 	struct ib_reg_wr reg_wr;
108 	int ret, off = 0;
109 
110 	while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
111 		atomic_inc(&ibmr->ic->i_fastreg_wrs);
112 		cpu_relax();
113 	}
114 
115 	ret = ib_map_mr_sg_zbva(frmr->mr, ibmr->sg, ibmr->sg_len,
116 				&off, PAGE_SIZE);
117 	if (unlikely(ret != ibmr->sg_len))
118 		return ret < 0 ? ret : -EINVAL;
119 
120 	/* Perform a WR for the fast_reg_mr. Each individual page
121 	 * in the sg list is added to the fast reg page list and placed
122 	 * inside the fast_reg_mr WR.  The key used is a rolling 8bit
123 	 * counter, which should guarantee uniqueness.
124 	 */
125 	ib_update_fast_reg_key(frmr->mr, ibmr->remap_count++);
126 	frmr->fr_state = FRMR_IS_INUSE;
127 
128 	memset(&reg_wr, 0, sizeof(reg_wr));
129 	reg_wr.wr.wr_id = (unsigned long)(void *)ibmr;
130 	reg_wr.wr.opcode = IB_WR_REG_MR;
131 	reg_wr.wr.num_sge = 0;
132 	reg_wr.mr = frmr->mr;
133 	reg_wr.key = frmr->mr->rkey;
134 	reg_wr.access = IB_ACCESS_LOCAL_WRITE |
135 			IB_ACCESS_REMOTE_READ |
136 			IB_ACCESS_REMOTE_WRITE;
137 	reg_wr.wr.send_flags = IB_SEND_SIGNALED;
138 
139 	failed_wr = &reg_wr.wr;
140 	ret = ib_post_send(ibmr->ic->i_cm_id->qp, &reg_wr.wr, &failed_wr);
141 	WARN_ON(failed_wr != &reg_wr.wr);
142 	if (unlikely(ret)) {
143 		/* Failure here can be because of -ENOMEM as well */
144 		frmr->fr_state = FRMR_IS_STALE;
145 		atomic_inc(&ibmr->ic->i_fastreg_wrs);
146 		if (printk_ratelimit())
147 			pr_warn("RDS/IB: %s returned error(%d)\n",
148 				__func__, ret);
149 	}
150 	return ret;
151 }
152 
rds_ib_map_frmr(struct rds_ib_device * rds_ibdev,struct rds_ib_mr_pool * pool,struct rds_ib_mr * ibmr,struct scatterlist * sg,unsigned int sg_len)153 static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev,
154 			   struct rds_ib_mr_pool *pool,
155 			   struct rds_ib_mr *ibmr,
156 			   struct scatterlist *sg, unsigned int sg_len)
157 {
158 	struct ib_device *dev = rds_ibdev->dev;
159 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
160 	int i;
161 	u32 len;
162 	int ret = 0;
163 
164 	/* We want to teardown old ibmr values here and fill it up with
165 	 * new sg values
166 	 */
167 	rds_ib_teardown_mr(ibmr);
168 
169 	ibmr->sg = sg;
170 	ibmr->sg_len = sg_len;
171 	ibmr->sg_dma_len = 0;
172 	frmr->sg_byte_len = 0;
173 	WARN_ON(ibmr->sg_dma_len);
174 	ibmr->sg_dma_len = ib_dma_map_sg(dev, ibmr->sg, ibmr->sg_len,
175 					 DMA_BIDIRECTIONAL);
176 	if (unlikely(!ibmr->sg_dma_len)) {
177 		pr_warn("RDS/IB: %s failed!\n", __func__);
178 		return -EBUSY;
179 	}
180 
181 	frmr->sg_byte_len = 0;
182 	frmr->dma_npages = 0;
183 	len = 0;
184 
185 	ret = -EINVAL;
186 	for (i = 0; i < ibmr->sg_dma_len; ++i) {
187 		unsigned int dma_len = ib_sg_dma_len(dev, &ibmr->sg[i]);
188 		u64 dma_addr = ib_sg_dma_address(dev, &ibmr->sg[i]);
189 
190 		frmr->sg_byte_len += dma_len;
191 		if (dma_addr & ~PAGE_MASK) {
192 			if (i > 0)
193 				goto out_unmap;
194 			else
195 				++frmr->dma_npages;
196 		}
197 
198 		if ((dma_addr + dma_len) & ~PAGE_MASK) {
199 			if (i < ibmr->sg_dma_len - 1)
200 				goto out_unmap;
201 			else
202 				++frmr->dma_npages;
203 		}
204 
205 		len += dma_len;
206 	}
207 	frmr->dma_npages += len >> PAGE_SHIFT;
208 
209 	if (frmr->dma_npages > ibmr->pool->fmr_attr.max_pages) {
210 		ret = -EMSGSIZE;
211 		goto out_unmap;
212 	}
213 
214 	ret = rds_ib_post_reg_frmr(ibmr);
215 	if (ret)
216 		goto out_unmap;
217 
218 	if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
219 		rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
220 	else
221 		rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
222 
223 	return ret;
224 
225 out_unmap:
226 	ib_dma_unmap_sg(rds_ibdev->dev, ibmr->sg, ibmr->sg_len,
227 			DMA_BIDIRECTIONAL);
228 	ibmr->sg_dma_len = 0;
229 	return ret;
230 }
231 
rds_ib_post_inv(struct rds_ib_mr * ibmr)232 static int rds_ib_post_inv(struct rds_ib_mr *ibmr)
233 {
234 	struct ib_send_wr *s_wr, *failed_wr;
235 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
236 	struct rdma_cm_id *i_cm_id = ibmr->ic->i_cm_id;
237 	int ret = -EINVAL;
238 
239 	if (!i_cm_id || !i_cm_id->qp || !frmr->mr)
240 		goto out;
241 
242 	if (frmr->fr_state != FRMR_IS_INUSE)
243 		goto out;
244 
245 	while (atomic_dec_return(&ibmr->ic->i_fastunreg_wrs) <= 0) {
246 		atomic_inc(&ibmr->ic->i_fastunreg_wrs);
247 		cpu_relax();
248 	}
249 
250 	frmr->fr_inv = true;
251 	s_wr = &frmr->fr_wr;
252 
253 	memset(s_wr, 0, sizeof(*s_wr));
254 	s_wr->wr_id = (unsigned long)(void *)ibmr;
255 	s_wr->opcode = IB_WR_LOCAL_INV;
256 	s_wr->ex.invalidate_rkey = frmr->mr->rkey;
257 	s_wr->send_flags = IB_SEND_SIGNALED;
258 
259 	failed_wr = s_wr;
260 	ret = ib_post_send(i_cm_id->qp, s_wr, &failed_wr);
261 	WARN_ON(failed_wr != s_wr);
262 	if (unlikely(ret)) {
263 		frmr->fr_state = FRMR_IS_STALE;
264 		frmr->fr_inv = false;
265 		atomic_inc(&ibmr->ic->i_fastunreg_wrs);
266 		pr_err("RDS/IB: %s returned error(%d)\n", __func__, ret);
267 		goto out;
268 	}
269 out:
270 	return ret;
271 }
272 
rds_ib_mr_cqe_handler(struct rds_ib_connection * ic,struct ib_wc * wc)273 void rds_ib_mr_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc)
274 {
275 	struct rds_ib_mr *ibmr = (void *)(unsigned long)wc->wr_id;
276 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
277 
278 	if (wc->status != IB_WC_SUCCESS) {
279 		frmr->fr_state = FRMR_IS_STALE;
280 		if (rds_conn_up(ic->conn))
281 			rds_ib_conn_error(ic->conn,
282 					  "frmr completion <%pI4,%pI4> status %u(%s), vendor_err 0x%x, disconnecting and reconnecting\n",
283 					  &ic->conn->c_laddr,
284 					  &ic->conn->c_faddr,
285 					  wc->status,
286 					  ib_wc_status_msg(wc->status),
287 					  wc->vendor_err);
288 	}
289 
290 	if (frmr->fr_inv) {
291 		frmr->fr_state = FRMR_IS_FREE;
292 		frmr->fr_inv = false;
293 		atomic_inc(&ic->i_fastreg_wrs);
294 	} else {
295 		atomic_inc(&ic->i_fastunreg_wrs);
296 	}
297 }
298 
rds_ib_unreg_frmr(struct list_head * list,unsigned int * nfreed,unsigned long * unpinned,unsigned int goal)299 void rds_ib_unreg_frmr(struct list_head *list, unsigned int *nfreed,
300 		       unsigned long *unpinned, unsigned int goal)
301 {
302 	struct rds_ib_mr *ibmr, *next;
303 	struct rds_ib_frmr *frmr;
304 	int ret = 0;
305 	unsigned int freed = *nfreed;
306 
307 	/* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
308 	list_for_each_entry(ibmr, list, unmap_list) {
309 		if (ibmr->sg_dma_len)
310 			ret |= rds_ib_post_inv(ibmr);
311 	}
312 	if (ret)
313 		pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, ret);
314 
315 	/* Now we can destroy the DMA mapping and unpin any pages */
316 	list_for_each_entry_safe(ibmr, next, list, unmap_list) {
317 		*unpinned += ibmr->sg_len;
318 		frmr = &ibmr->u.frmr;
319 		__rds_ib_teardown_mr(ibmr);
320 		if (freed < goal || frmr->fr_state == FRMR_IS_STALE) {
321 			/* Don't de-allocate if the MR is not free yet */
322 			if (frmr->fr_state == FRMR_IS_INUSE)
323 				continue;
324 
325 			if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
326 				rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
327 			else
328 				rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
329 			list_del(&ibmr->unmap_list);
330 			if (frmr->mr)
331 				ib_dereg_mr(frmr->mr);
332 			kfree(ibmr);
333 			freed++;
334 		}
335 	}
336 	*nfreed = freed;
337 }
338 
rds_ib_reg_frmr(struct rds_ib_device * rds_ibdev,struct rds_ib_connection * ic,struct scatterlist * sg,unsigned long nents,u32 * key)339 struct rds_ib_mr *rds_ib_reg_frmr(struct rds_ib_device *rds_ibdev,
340 				  struct rds_ib_connection *ic,
341 				  struct scatterlist *sg,
342 				  unsigned long nents, u32 *key)
343 {
344 	struct rds_ib_mr *ibmr = NULL;
345 	struct rds_ib_frmr *frmr;
346 	int ret;
347 
348 	do {
349 		if (ibmr)
350 			rds_ib_free_frmr(ibmr, true);
351 		ibmr = rds_ib_alloc_frmr(rds_ibdev, nents);
352 		if (IS_ERR(ibmr))
353 			return ibmr;
354 		frmr = &ibmr->u.frmr;
355 	} while (frmr->fr_state != FRMR_IS_FREE);
356 
357 	ibmr->ic = ic;
358 	ibmr->device = rds_ibdev;
359 	ret = rds_ib_map_frmr(rds_ibdev, ibmr->pool, ibmr, sg, nents);
360 	if (ret == 0) {
361 		*key = frmr->mr->rkey;
362 	} else {
363 		rds_ib_free_frmr(ibmr, false);
364 		ibmr = ERR_PTR(ret);
365 	}
366 
367 	return ibmr;
368 }
369 
rds_ib_free_frmr_list(struct rds_ib_mr * ibmr)370 void rds_ib_free_frmr_list(struct rds_ib_mr *ibmr)
371 {
372 	struct rds_ib_mr_pool *pool = ibmr->pool;
373 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
374 
375 	if (frmr->fr_state == FRMR_IS_STALE)
376 		llist_add(&ibmr->llnode, &pool->drop_list);
377 	else
378 		llist_add(&ibmr->llnode, &pool->free_list);
379 }
380