fmr_ops.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2015 Oracle.  All rights reserved.
 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
 */

/* Lightweight memory registration using Fast Memory Regions (FMR).
 * Referred to sometimes as MTHCAFMR mode.
 *
 * FMR uses synchronous memory registration and deregistration.
 * FMR registration is known to be fast, but FMR deregistration
 * can take tens of usecs to complete.
 */

14 15 16 17 18 19 20 21
/* Normal operation
 *
 * A Memory Region is prepared for RDMA READ or WRITE using the
 * ib_map_phys_fmr verb (fmr_op_map). When the RDMA operation is
 * finished, the Memory Region is unmapped using the ib_unmap_fmr
 * verb (fmr_op_unmap).
 */

22 23 24 25 26 27
#include "xprt_rdma.h"

#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
# define RPCDBG_FACILITY	RPCDBG_TRANS
#endif

28 29 30
/* Maximum scatter/gather per FMR */
#define RPCRDMA_MAX_FMR_SGES	(64)

31 32 33 34 35 36
/* Access mode of externally registered pages */
enum {
	RPCRDMA_FMR_ACCESS_FLAGS	= IB_ACCESS_REMOTE_WRITE |
					  IB_ACCESS_REMOTE_READ,
};

37 38 39 40 41 42 43 44 45 46 47
bool
fmr_is_supported(struct rpcrdma_ia *ia)
{
	if (!ia->ri_device->alloc_fmr) {
		pr_info("rpcrdma: 'fmr' mode is not supported by device %s\n",
			ia->ri_device->name);
		return false;
	}
	return true;
}

48 49 50 51 52 53 54 55 56
static int
__fmr_init(struct rpcrdma_mw *mw, struct ib_pd *pd)
{
	static struct ib_fmr_attr fmr_attr = {
		.max_pages	= RPCRDMA_MAX_FMR_SGES,
		.max_maps	= 1,
		.page_shift	= PAGE_SHIFT
	};

57 58 59
	mw->fmr.fm_physaddrs = kcalloc(RPCRDMA_MAX_FMR_SGES,
				       sizeof(u64), GFP_KERNEL);
	if (!mw->fmr.fm_physaddrs)
60 61 62 63 64 65 66 67 68
		goto out_free;

	mw->mw_sg = kcalloc(RPCRDMA_MAX_FMR_SGES,
			    sizeof(*mw->mw_sg), GFP_KERNEL);
	if (!mw->mw_sg)
		goto out_free;

	sg_init_table(mw->mw_sg, RPCRDMA_MAX_FMR_SGES);

69 70 71
	mw->fmr.fm_mr = ib_alloc_fmr(pd, RPCRDMA_FMR_ACCESS_FLAGS,
				     &fmr_attr);
	if (IS_ERR(mw->fmr.fm_mr))
72 73 74 75 76 77
		goto out_fmr_err;

	return 0;

out_fmr_err:
	dprintk("RPC:       %s: ib_alloc_fmr returned %ld\n", __func__,
78
		PTR_ERR(mw->fmr.fm_mr));
79 80 81

out_free:
	kfree(mw->mw_sg);
82
	kfree(mw->fmr.fm_physaddrs);
83 84 85
	return -ENOMEM;
}

86 87 88 89
static int
__fmr_unmap(struct rpcrdma_mw *mw)
{
	LIST_HEAD(l);
90
	int rc;
91

92
	list_add(&mw->fmr.fm_mr->list, &l);
93
	rc = ib_unmap_fmr(&l);
94
	list_del_init(&mw->fmr.fm_mr->list);
95
	return rc;
96 97
}

98 99 100
static void
__fmr_release(struct rpcrdma_mw *r)
{
101
	LIST_HEAD(unmap_list);
102 103
	int rc;

104
	kfree(r->fmr.fm_physaddrs);
105 106
	kfree(r->mw_sg);

107 108 109 110 111 112 113 114
	/* In case this one was left mapped, try to unmap it
	 * to prevent dealloc_fmr from failing with EBUSY
	 */
	rc = __fmr_unmap(r);
	if (rc)
		pr_err("rpcrdma: final ib_unmap_fmr for %p failed %i\n",
		       r, rc);

115
	rc = ib_dealloc_fmr(r->fmr.fm_mr);
116 117 118 119 120
	if (rc)
		pr_err("rpcrdma: final ib_dealloc_fmr for %p returned %i\n",
		       r, rc);
}

121 122 123 124 125
/* Reset of a single FMR.
 *
 * There's no recovery if this fails. The FMR is abandoned, but
 * remains in rb_all. It will be cleaned up when the transport is
 * destroyed.
126 127
 */
static void
128
fmr_op_recover_mr(struct rpcrdma_mw *mw)
129
{
130 131
	struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
	int rc;
132

133 134
	/* ORDER: invalidate first */
	rc = __fmr_unmap(mw);
135

136 137 138 139 140 141 142 143 144 145 146 147
	/* ORDER: then DMA unmap */
	ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
			mw->mw_sg, mw->mw_nents, mw->mw_dir);
	if (rc) {
		pr_err("rpcrdma: FMR reset status %d, %p orphaned\n",
		       rc, mw);
		r_xprt->rx_stats.mrs_orphaned++;
		return;
	}

	rpcrdma_put_mw(r_xprt, mw);
	r_xprt->rx_stats.mrs_recovered++;
148 149
}

C
Chuck Lever 已提交
150 151 152 153
static int
fmr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
	    struct rpcrdma_create_data_internal *cdata)
{
C
Chuck Lever 已提交
154 155 156
	rpcrdma_set_max_header_sizes(ia, cdata, max_t(unsigned int, 1,
						      RPCRDMA_MAX_DATA_SEGS /
						      RPCRDMA_MAX_FMR_SGES));
C
Chuck Lever 已提交
157 158 159
	return 0;
}

160 161 162 163 164 165
/* FMR mode conveys up to 64 pages of payload per chunk segment.
 */
static size_t
fmr_op_maxpages(struct rpcrdma_xprt *r_xprt)
{
	return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
166
		     RPCRDMA_MAX_HDR_SEGS * RPCRDMA_MAX_FMR_SGES);
167 168
}

C
Chuck Lever 已提交
169 170 171 172 173 174 175 176
static int
fmr_op_init(struct rpcrdma_xprt *r_xprt)
{
	struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
	struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
	struct rpcrdma_mw *r;
	int i, rc;

C
Chuck Lever 已提交
177
	spin_lock_init(&buf->rb_mwlock);
C
Chuck Lever 已提交
178 179 180
	INIT_LIST_HEAD(&buf->rb_mws);
	INIT_LIST_HEAD(&buf->rb_all);

181 182 183 184
	i = max_t(int, RPCRDMA_MAX_DATA_SEGS / RPCRDMA_MAX_FMR_SGES, 1);
	i += 2;				/* head + tail */
	i *= buf->rb_max_requests;	/* one set for each RPC slot */
	dprintk("RPC:       %s: initalizing %d FMRs\n", __func__, i);
C
Chuck Lever 已提交
185 186 187 188

	while (i--) {
		r = kzalloc(sizeof(*r), GFP_KERNEL);
		if (!r)
189
			return -ENOMEM;
C
Chuck Lever 已提交
190

191 192 193 194 195
		rc = __fmr_init(r, pd);
		if (rc) {
			kfree(r);
			return rc;
		}
C
Chuck Lever 已提交
196

197
		r->mw_xprt = r_xprt;
C
Chuck Lever 已提交
198 199 200 201 202 203
		list_add(&r->mw_list, &buf->rb_mws);
		list_add(&r->mw_all, &buf->rb_all);
	}
	return 0;
}

204 205 206 207 208 209 210 211 212
/* Use the ib_map_phys_fmr() verb to register a memory region
 * for remote access via RDMA READ or RDMA WRITE.
 */
static int
fmr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
	   int nsegs, bool writing)
{
	struct rpcrdma_mr_seg *seg1 = seg;
	int len, pageoff, i, rc;
213
	struct rpcrdma_mw *mw;
214
	u64 *dma_pages;
215 216 217

	mw = seg1->rl_mw;
	seg1->rl_mw = NULL;
218 219 220 221
	if (mw)
		rpcrdma_defer_mr_recovery(mw);
	mw = rpcrdma_get_mw(r_xprt);
	if (!mw)
222
		return -ENOBUFS;
223 224 225 226 227 228 229 230

	pageoff = offset_in_page(seg1->mr_offset);
	seg1->mr_offset -= pageoff;	/* start of page */
	seg1->mr_len += pageoff;
	len = -pageoff;
	if (nsegs > RPCRDMA_MAX_FMR_SGES)
		nsegs = RPCRDMA_MAX_FMR_SGES;
	for (i = 0; i < nsegs;) {
231 232 233 234 235 236 237 238
		if (seg->mr_page)
			sg_set_page(&mw->mw_sg[i],
				    seg->mr_page,
				    seg->mr_len,
				    offset_in_page(seg->mr_offset));
		else
			sg_set_buf(&mw->mw_sg[i], seg->mr_offset,
				   seg->mr_len);
239 240 241 242 243 244 245 246
		len += seg->mr_len;
		++seg;
		++i;
		/* Check for holes */
		if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
		    offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
			break;
	}
247 248 249 250 251 252
	mw->mw_nents = i;
	mw->mw_dir = rpcrdma_data_dir(writing);

	if (!ib_dma_map_sg(r_xprt->rx_ia.ri_device,
			   mw->mw_sg, mw->mw_nents, mw->mw_dir))
		goto out_dmamap_err;
253

254 255 256 257
	for (i = 0, dma_pages = mw->fmr.fm_physaddrs; i < mw->mw_nents; i++)
		dma_pages[i] = sg_dma_address(&mw->mw_sg[i]);
	rc = ib_map_phys_fmr(mw->fmr.fm_mr, dma_pages, mw->mw_nents,
			     dma_pages[0]);
258 259 260
	if (rc)
		goto out_maperr;

261
	seg1->rl_mw = mw;
262
	seg1->mr_rkey = mw->fmr.fm_mr->rkey;
263 264
	seg1->mr_base = dma_pages[0] + pageoff;
	seg1->mr_nsegs = mw->mw_nents;
265
	seg1->mr_len = len;
266 267 268 269 270
	return mw->mw_nents;

out_dmamap_err:
	pr_err("rpcrdma: failed to dma map sg %p sg_nents %u\n",
	       mw->mw_sg, mw->mw_nents);
271
	rpcrdma_defer_mr_recovery(mw);
272
	return -EIO;
273 274

out_maperr:
275 276 277
	pr_err("rpcrdma: ib_map_phys_fmr %u@0x%llx+%i (%d) status %i\n",
	       len, (unsigned long long)dma_pages[0],
	       pageoff, mw->mw_nents, rc);
278
	rpcrdma_defer_mr_recovery(mw);
279
	return -EIO;
280 281
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
/* Invalidate all memory regions that were registered for "req".
 *
 * Sleeps until it is safe for the host CPU to access the
 * previously mapped memory regions.
 */
static void
fmr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
{
	struct rpcrdma_mr_seg *seg;
	unsigned int i, nchunks;
	struct rpcrdma_mw *mw;
	LIST_HEAD(unmap_list);
	int rc;

	dprintk("RPC:       %s: req %p\n", __func__, req);

	/* ORDER: Invalidate all of the req's MRs first
	 *
	 * ib_unmap_fmr() is slow, so use a single call instead
301
	 * of one call per mapped FMR.
302 303 304 305 306
	 */
	for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) {
		seg = &req->rl_segments[i];
		mw = seg->rl_mw;

307
		list_add_tail(&mw->fmr.fm_mr->list, &unmap_list);
308 309 310 311 312

		i += seg->mr_nsegs;
	}
	rc = ib_unmap_fmr(&unmap_list);
	if (rc)
313
		goto out_reset;
314 315 316 317 318 319

	/* ORDER: Now DMA unmap all of the req's MRs, and return
	 * them to the free MW list.
	 */
	for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) {
		seg = &req->rl_segments[i];
320
		mw = seg->rl_mw;
321

322
		list_del_init(&mw->fmr.fm_mr->list);
323 324 325
		ib_dma_unmap_sg(r_xprt->rx_ia.ri_device,
				mw->mw_sg, mw->mw_nents, mw->mw_dir);
		rpcrdma_put_mw(r_xprt, mw);
326 327 328

		i += seg->mr_nsegs;
		seg->mr_nsegs = 0;
329
		seg->rl_mw = NULL;
330 331 332
	}

	req->rl_nchunks = 0;
333 334 335 336 337 338 339 340 341 342 343 344 345 346
	return;

out_reset:
	pr_err("rpcrdma: ib_unmap_fmr failed (%i)\n", rc);

	for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) {
		seg = &req->rl_segments[i];
		mw = seg->rl_mw;

		list_del_init(&mw->fmr.fm_mr->list);
		fmr_op_recover_mr(mw);

		i += seg->mr_nsegs;
	}
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/* Use a slow, safe mechanism to invalidate all memory regions
 * that were registered for "req".
 */
static void
fmr_op_unmap_safe(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
		  bool sync)
{
	struct rpcrdma_mr_seg *seg;
	struct rpcrdma_mw *mw;
	unsigned int i;

	for (i = 0; req->rl_nchunks; req->rl_nchunks--) {
		seg = &req->rl_segments[i];
		mw = seg->rl_mw;

364
		if (sync)
365
			fmr_op_recover_mr(mw);
366
		else
367
			rpcrdma_defer_mr_recovery(mw);
368 369 370 371 372 373 374

		i += seg->mr_nsegs;
		seg->mr_nsegs = 0;
		seg->rl_mw = NULL;
	}
}

375 376 377 378 379 380 381 382
static void
fmr_op_destroy(struct rpcrdma_buffer *buf)
{
	struct rpcrdma_mw *r;

	while (!list_empty(&buf->rb_all)) {
		r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
		list_del(&r->mw_all);
383
		__fmr_release(r);
384 385 386 387
		kfree(r);
	}
}

388
const struct rpcrdma_memreg_ops rpcrdma_fmr_memreg_ops = {
389
	.ro_map				= fmr_op_map,
390
	.ro_unmap_sync			= fmr_op_unmap_sync,
391
	.ro_unmap_safe			= fmr_op_unmap_safe,
392
	.ro_recover_mr			= fmr_op_recover_mr,
C
Chuck Lever 已提交
393
	.ro_open			= fmr_op_open,
394
	.ro_maxpages			= fmr_op_maxpages,
C
Chuck Lever 已提交
395
	.ro_init			= fmr_op_init,
396
	.ro_destroy			= fmr_op_destroy,
397 398
	.ro_displayname			= "fmr",
};