xref: /openbmc/linux/net/rds/rdma.c (revision 78560d41)
1 /*
2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. 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/pagemap.h>
34 #include <linux/slab.h>
35 #include <linux/rbtree.h>
36 #include <linux/dma-mapping.h> /* for DMA_*_DEVICE */
37 
38 #include "rds.h"
39 
40 /*
41  * XXX
42  *  - build with sparse
43  *  - should we detect duplicate keys on a socket?  hmm.
44  *  - an rdma is an mlock, apply rlimit?
45  */
46 
47 /*
48  * get the number of pages by looking at the page indices that the start and
49  * end addresses fall in.
50  *
51  * Returns 0 if the vec is invalid.  It is invalid if the number of bytes
52  * causes the address to wrap or overflows an unsigned int.  This comes
53  * from being stored in the 'length' member of 'struct scatterlist'.
54  */
55 static unsigned int rds_pages_in_vec(struct rds_iovec *vec)
56 {
57 	if ((vec->addr + vec->bytes <= vec->addr) ||
58 	    (vec->bytes > (u64)UINT_MAX))
59 		return 0;
60 
61 	return ((vec->addr + vec->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT) -
62 		(vec->addr >> PAGE_SHIFT);
63 }
64 
65 static struct rds_mr *rds_mr_tree_walk(struct rb_root *root, u64 key,
66 				       struct rds_mr *insert)
67 {
68 	struct rb_node **p = &root->rb_node;
69 	struct rb_node *parent = NULL;
70 	struct rds_mr *mr;
71 
72 	while (*p) {
73 		parent = *p;
74 		mr = rb_entry(parent, struct rds_mr, r_rb_node);
75 
76 		if (key < mr->r_key)
77 			p = &(*p)->rb_left;
78 		else if (key > mr->r_key)
79 			p = &(*p)->rb_right;
80 		else
81 			return mr;
82 	}
83 
84 	if (insert) {
85 		rb_link_node(&insert->r_rb_node, parent, p);
86 		rb_insert_color(&insert->r_rb_node, root);
87 		refcount_inc(&insert->r_refcount);
88 	}
89 	return NULL;
90 }
91 
92 /*
93  * Destroy the transport-specific part of a MR.
94  */
95 static void rds_destroy_mr(struct rds_mr *mr)
96 {
97 	struct rds_sock *rs = mr->r_sock;
98 	void *trans_private = NULL;
99 	unsigned long flags;
100 
101 	rdsdebug("RDS: destroy mr key is %x refcnt %u\n",
102 			mr->r_key, refcount_read(&mr->r_refcount));
103 
104 	if (test_and_set_bit(RDS_MR_DEAD, &mr->r_state))
105 		return;
106 
107 	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
108 	if (!RB_EMPTY_NODE(&mr->r_rb_node))
109 		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
110 	trans_private = mr->r_trans_private;
111 	mr->r_trans_private = NULL;
112 	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
113 
114 	if (trans_private)
115 		mr->r_trans->free_mr(trans_private, mr->r_invalidate);
116 }
117 
118 void __rds_put_mr_final(struct rds_mr *mr)
119 {
120 	rds_destroy_mr(mr);
121 	kfree(mr);
122 }
123 
124 /*
125  * By the time this is called we can't have any more ioctls called on
126  * the socket so we don't need to worry about racing with others.
127  */
128 void rds_rdma_drop_keys(struct rds_sock *rs)
129 {
130 	struct rds_mr *mr;
131 	struct rb_node *node;
132 	unsigned long flags;
133 
134 	/* Release any MRs associated with this socket */
135 	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
136 	while ((node = rb_first(&rs->rs_rdma_keys))) {
137 		mr = rb_entry(node, struct rds_mr, r_rb_node);
138 		if (mr->r_trans == rs->rs_transport)
139 			mr->r_invalidate = 0;
140 		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
141 		RB_CLEAR_NODE(&mr->r_rb_node);
142 		spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
143 		rds_destroy_mr(mr);
144 		rds_mr_put(mr);
145 		spin_lock_irqsave(&rs->rs_rdma_lock, flags);
146 	}
147 	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
148 
149 	if (rs->rs_transport && rs->rs_transport->flush_mrs)
150 		rs->rs_transport->flush_mrs();
151 }
152 
153 /*
154  * Helper function to pin user pages.
155  */
156 static int rds_pin_pages(unsigned long user_addr, unsigned int nr_pages,
157 			struct page **pages, int write)
158 {
159 	unsigned int gup_flags = FOLL_LONGTERM;
160 	int ret;
161 
162 	if (write)
163 		gup_flags |= FOLL_WRITE;
164 
165 	ret = get_user_pages_fast(user_addr, nr_pages, gup_flags, pages);
166 	if (ret >= 0 && ret < nr_pages) {
167 		while (ret--)
168 			put_page(pages[ret]);
169 		ret = -EFAULT;
170 	}
171 
172 	return ret;
173 }
174 
175 static int __rds_rdma_map(struct rds_sock *rs, struct rds_get_mr_args *args,
176 			  u64 *cookie_ret, struct rds_mr **mr_ret,
177 			  struct rds_conn_path *cp)
178 {
179 	struct rds_mr *mr = NULL, *found;
180 	struct scatterlist *sg = NULL;
181 	unsigned int nr_pages;
182 	struct page **pages = NULL;
183 	void *trans_private;
184 	unsigned long flags;
185 	rds_rdma_cookie_t cookie;
186 	unsigned int nents = 0;
187 	int need_odp = 0;
188 	long i;
189 	int ret;
190 
191 	if (ipv6_addr_any(&rs->rs_bound_addr) || !rs->rs_transport) {
192 		ret = -ENOTCONN; /* XXX not a great errno */
193 		goto out;
194 	}
195 
196 	if (!rs->rs_transport->get_mr) {
197 		ret = -EOPNOTSUPP;
198 		goto out;
199 	}
200 
201 	/* If the combination of the addr and size requested for this memory
202 	 * region causes an integer overflow, return error.
203 	 */
204 	if (((args->vec.addr + args->vec.bytes) < args->vec.addr) ||
205 	    PAGE_ALIGN(args->vec.addr + args->vec.bytes) <
206 		    (args->vec.addr + args->vec.bytes)) {
207 		ret = -EINVAL;
208 		goto out;
209 	}
210 
211 	if (!can_do_mlock()) {
212 		ret = -EPERM;
213 		goto out;
214 	}
215 
216 	nr_pages = rds_pages_in_vec(&args->vec);
217 	if (nr_pages == 0) {
218 		ret = -EINVAL;
219 		goto out;
220 	}
221 
222 	/* Restrict the size of mr irrespective of underlying transport
223 	 * To account for unaligned mr regions, subtract one from nr_pages
224 	 */
225 	if ((nr_pages - 1) > (RDS_MAX_MSG_SIZE >> PAGE_SHIFT)) {
226 		ret = -EMSGSIZE;
227 		goto out;
228 	}
229 
230 	rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n",
231 		args->vec.addr, args->vec.bytes, nr_pages);
232 
233 	/* XXX clamp nr_pages to limit the size of this alloc? */
234 	pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
235 	if (!pages) {
236 		ret = -ENOMEM;
237 		goto out;
238 	}
239 
240 	mr = kzalloc(sizeof(struct rds_mr), GFP_KERNEL);
241 	if (!mr) {
242 		ret = -ENOMEM;
243 		goto out;
244 	}
245 
246 	refcount_set(&mr->r_refcount, 1);
247 	RB_CLEAR_NODE(&mr->r_rb_node);
248 	mr->r_trans = rs->rs_transport;
249 	mr->r_sock = rs;
250 
251 	if (args->flags & RDS_RDMA_USE_ONCE)
252 		mr->r_use_once = 1;
253 	if (args->flags & RDS_RDMA_INVALIDATE)
254 		mr->r_invalidate = 1;
255 	if (args->flags & RDS_RDMA_READWRITE)
256 		mr->r_write = 1;
257 
258 	/*
259 	 * Pin the pages that make up the user buffer and transfer the page
260 	 * pointers to the mr's sg array.  We check to see if we've mapped
261 	 * the whole region after transferring the partial page references
262 	 * to the sg array so that we can have one page ref cleanup path.
263 	 *
264 	 * For now we have no flag that tells us whether the mapping is
265 	 * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to
266 	 * the zero page.
267 	 */
268 	ret = rds_pin_pages(args->vec.addr, nr_pages, pages, 1);
269 	if (ret == -EOPNOTSUPP) {
270 		need_odp = 1;
271 	} else if (ret <= 0) {
272 		goto out;
273 	} else {
274 		nents = ret;
275 		sg = kcalloc(nents, sizeof(*sg), GFP_KERNEL);
276 		if (!sg) {
277 			ret = -ENOMEM;
278 			goto out;
279 		}
280 		WARN_ON(!nents);
281 		sg_init_table(sg, nents);
282 
283 		/* Stick all pages into the scatterlist */
284 		for (i = 0 ; i < nents; i++)
285 			sg_set_page(&sg[i], pages[i], PAGE_SIZE, 0);
286 
287 		rdsdebug("RDS: trans_private nents is %u\n", nents);
288 	}
289 	/* Obtain a transport specific MR. If this succeeds, the
290 	 * s/g list is now owned by the MR.
291 	 * Note that dma_map() implies that pending writes are
292 	 * flushed to RAM, so no dma_sync is needed here. */
293 	trans_private = rs->rs_transport->get_mr(
294 		sg, nents, rs, &mr->r_key, cp ? cp->cp_conn : NULL,
295 		args->vec.addr, args->vec.bytes,
296 		need_odp ? ODP_ZEROBASED : ODP_NOT_NEEDED);
297 
298 	if (IS_ERR(trans_private)) {
299 		/* In ODP case, we don't GUP pages, so don't need
300 		 * to release anything.
301 		 */
302 		if (!need_odp) {
303 			for (i = 0 ; i < nents; i++)
304 				put_page(sg_page(&sg[i]));
305 			kfree(sg);
306 		}
307 		ret = PTR_ERR(trans_private);
308 		goto out;
309 	}
310 
311 	mr->r_trans_private = trans_private;
312 
313 	rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n",
314 	       mr->r_key, (void *)(unsigned long) args->cookie_addr);
315 
316 	/* The user may pass us an unaligned address, but we can only
317 	 * map page aligned regions. So we keep the offset, and build
318 	 * a 64bit cookie containing <R_Key, offset> and pass that
319 	 * around. */
320 	if (need_odp)
321 		cookie = rds_rdma_make_cookie(mr->r_key, 0);
322 	else
323 		cookie = rds_rdma_make_cookie(mr->r_key,
324 					      args->vec.addr & ~PAGE_MASK);
325 	if (cookie_ret)
326 		*cookie_ret = cookie;
327 
328 	if (args->cookie_addr && put_user(cookie, (u64 __user *)(unsigned long) args->cookie_addr)) {
329 		ret = -EFAULT;
330 		goto out;
331 	}
332 
333 	/* Inserting the new MR into the rbtree bumps its
334 	 * reference count. */
335 	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
336 	found = rds_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr);
337 	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
338 
339 	BUG_ON(found && found != mr);
340 
341 	rdsdebug("RDS: get_mr key is %x\n", mr->r_key);
342 	if (mr_ret) {
343 		refcount_inc(&mr->r_refcount);
344 		*mr_ret = mr;
345 	}
346 
347 	ret = 0;
348 out:
349 	kfree(pages);
350 	if (mr)
351 		rds_mr_put(mr);
352 	return ret;
353 }
354 
355 int rds_get_mr(struct rds_sock *rs, char __user *optval, int optlen)
356 {
357 	struct rds_get_mr_args args;
358 
359 	if (optlen != sizeof(struct rds_get_mr_args))
360 		return -EINVAL;
361 
362 	if (copy_from_user(&args, (struct rds_get_mr_args __user *)optval,
363 			   sizeof(struct rds_get_mr_args)))
364 		return -EFAULT;
365 
366 	return __rds_rdma_map(rs, &args, NULL, NULL, NULL);
367 }
368 
369 int rds_get_mr_for_dest(struct rds_sock *rs, char __user *optval, int optlen)
370 {
371 	struct rds_get_mr_for_dest_args args;
372 	struct rds_get_mr_args new_args;
373 
374 	if (optlen != sizeof(struct rds_get_mr_for_dest_args))
375 		return -EINVAL;
376 
377 	if (copy_from_user(&args, (struct rds_get_mr_for_dest_args __user *)optval,
378 			   sizeof(struct rds_get_mr_for_dest_args)))
379 		return -EFAULT;
380 
381 	/*
382 	 * Initially, just behave like get_mr().
383 	 * TODO: Implement get_mr as wrapper around this
384 	 *	 and deprecate it.
385 	 */
386 	new_args.vec = args.vec;
387 	new_args.cookie_addr = args.cookie_addr;
388 	new_args.flags = args.flags;
389 
390 	return __rds_rdma_map(rs, &new_args, NULL, NULL, NULL);
391 }
392 
393 /*
394  * Free the MR indicated by the given R_Key
395  */
396 int rds_free_mr(struct rds_sock *rs, char __user *optval, int optlen)
397 {
398 	struct rds_free_mr_args args;
399 	struct rds_mr *mr;
400 	unsigned long flags;
401 
402 	if (optlen != sizeof(struct rds_free_mr_args))
403 		return -EINVAL;
404 
405 	if (copy_from_user(&args, (struct rds_free_mr_args __user *)optval,
406 			   sizeof(struct rds_free_mr_args)))
407 		return -EFAULT;
408 
409 	/* Special case - a null cookie means flush all unused MRs */
410 	if (args.cookie == 0) {
411 		if (!rs->rs_transport || !rs->rs_transport->flush_mrs)
412 			return -EINVAL;
413 		rs->rs_transport->flush_mrs();
414 		return 0;
415 	}
416 
417 	/* Look up the MR given its R_key and remove it from the rbtree
418 	 * so nobody else finds it.
419 	 * This should also prevent races with rds_rdma_unuse.
420 	 */
421 	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
422 	mr = rds_mr_tree_walk(&rs->rs_rdma_keys, rds_rdma_cookie_key(args.cookie), NULL);
423 	if (mr) {
424 		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
425 		RB_CLEAR_NODE(&mr->r_rb_node);
426 		if (args.flags & RDS_RDMA_INVALIDATE)
427 			mr->r_invalidate = 1;
428 	}
429 	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
430 
431 	if (!mr)
432 		return -EINVAL;
433 
434 	/*
435 	 * call rds_destroy_mr() ourselves so that we're sure it's done by the time
436 	 * we return.  If we let rds_mr_put() do it it might not happen until
437 	 * someone else drops their ref.
438 	 */
439 	rds_destroy_mr(mr);
440 	rds_mr_put(mr);
441 	return 0;
442 }
443 
444 /*
445  * This is called when we receive an extension header that
446  * tells us this MR was used. It allows us to implement
447  * use_once semantics
448  */
449 void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force)
450 {
451 	struct rds_mr *mr;
452 	unsigned long flags;
453 	int zot_me = 0;
454 
455 	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
456 	mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
457 	if (!mr) {
458 		pr_debug("rds: trying to unuse MR with unknown r_key %u!\n",
459 			 r_key);
460 		spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
461 		return;
462 	}
463 
464 	if (mr->r_use_once || force) {
465 		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
466 		RB_CLEAR_NODE(&mr->r_rb_node);
467 		zot_me = 1;
468 	}
469 	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
470 
471 	/* May have to issue a dma_sync on this memory region.
472 	 * Note we could avoid this if the operation was a RDMA READ,
473 	 * but at this point we can't tell. */
474 	if (mr->r_trans->sync_mr)
475 		mr->r_trans->sync_mr(mr->r_trans_private, DMA_FROM_DEVICE);
476 
477 	/* If the MR was marked as invalidate, this will
478 	 * trigger an async flush. */
479 	if (zot_me) {
480 		rds_destroy_mr(mr);
481 		rds_mr_put(mr);
482 	}
483 }
484 
485 void rds_rdma_free_op(struct rm_rdma_op *ro)
486 {
487 	unsigned int i;
488 
489 	if (ro->op_odp_mr) {
490 		rds_mr_put(ro->op_odp_mr);
491 	} else {
492 		for (i = 0; i < ro->op_nents; i++) {
493 			struct page *page = sg_page(&ro->op_sg[i]);
494 
495 			/* Mark page dirty if it was possibly modified, which
496 			 * is the case for a RDMA_READ which copies from remote
497 			 * to local memory
498 			 */
499 			if (!ro->op_write)
500 				set_page_dirty(page);
501 			put_page(page);
502 		}
503 	}
504 
505 	kfree(ro->op_notifier);
506 	ro->op_notifier = NULL;
507 	ro->op_active = 0;
508 	ro->op_odp_mr = NULL;
509 }
510 
511 void rds_atomic_free_op(struct rm_atomic_op *ao)
512 {
513 	struct page *page = sg_page(ao->op_sg);
514 
515 	/* Mark page dirty if it was possibly modified, which
516 	 * is the case for a RDMA_READ which copies from remote
517 	 * to local memory */
518 	set_page_dirty(page);
519 	put_page(page);
520 
521 	kfree(ao->op_notifier);
522 	ao->op_notifier = NULL;
523 	ao->op_active = 0;
524 }
525 
526 
527 /*
528  * Count the number of pages needed to describe an incoming iovec array.
529  */
530 static int rds_rdma_pages(struct rds_iovec iov[], int nr_iovecs)
531 {
532 	int tot_pages = 0;
533 	unsigned int nr_pages;
534 	unsigned int i;
535 
536 	/* figure out the number of pages in the vector */
537 	for (i = 0; i < nr_iovecs; i++) {
538 		nr_pages = rds_pages_in_vec(&iov[i]);
539 		if (nr_pages == 0)
540 			return -EINVAL;
541 
542 		tot_pages += nr_pages;
543 
544 		/*
545 		 * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
546 		 * so tot_pages cannot overflow without first going negative.
547 		 */
548 		if (tot_pages < 0)
549 			return -EINVAL;
550 	}
551 
552 	return tot_pages;
553 }
554 
555 int rds_rdma_extra_size(struct rds_rdma_args *args,
556 			struct rds_iov_vector *iov)
557 {
558 	struct rds_iovec *vec;
559 	struct rds_iovec __user *local_vec;
560 	int tot_pages = 0;
561 	unsigned int nr_pages;
562 	unsigned int i;
563 
564 	local_vec = (struct rds_iovec __user *)(unsigned long) args->local_vec_addr;
565 
566 	if (args->nr_local == 0)
567 		return -EINVAL;
568 
569 	iov->iov = kcalloc(args->nr_local,
570 			   sizeof(struct rds_iovec),
571 			   GFP_KERNEL);
572 	if (!iov->iov)
573 		return -ENOMEM;
574 
575 	vec = &iov->iov[0];
576 
577 	if (copy_from_user(vec, local_vec, args->nr_local *
578 			   sizeof(struct rds_iovec)))
579 		return -EFAULT;
580 	iov->len = args->nr_local;
581 
582 	/* figure out the number of pages in the vector */
583 	for (i = 0; i < args->nr_local; i++, vec++) {
584 
585 		nr_pages = rds_pages_in_vec(vec);
586 		if (nr_pages == 0)
587 			return -EINVAL;
588 
589 		tot_pages += nr_pages;
590 
591 		/*
592 		 * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
593 		 * so tot_pages cannot overflow without first going negative.
594 		 */
595 		if (tot_pages < 0)
596 			return -EINVAL;
597 	}
598 
599 	return tot_pages * sizeof(struct scatterlist);
600 }
601 
602 /*
603  * The application asks for a RDMA transfer.
604  * Extract all arguments and set up the rdma_op
605  */
606 int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
607 		       struct cmsghdr *cmsg,
608 		       struct rds_iov_vector *vec)
609 {
610 	struct rds_rdma_args *args;
611 	struct rm_rdma_op *op = &rm->rdma;
612 	int nr_pages;
613 	unsigned int nr_bytes;
614 	struct page **pages = NULL;
615 	struct rds_iovec *iovs;
616 	unsigned int i, j;
617 	int ret = 0;
618 	bool odp_supported = true;
619 
620 	if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args))
621 	    || rm->rdma.op_active)
622 		return -EINVAL;
623 
624 	args = CMSG_DATA(cmsg);
625 
626 	if (ipv6_addr_any(&rs->rs_bound_addr)) {
627 		ret = -ENOTCONN; /* XXX not a great errno */
628 		goto out_ret;
629 	}
630 
631 	if (args->nr_local > UIO_MAXIOV) {
632 		ret = -EMSGSIZE;
633 		goto out_ret;
634 	}
635 
636 	if (vec->len != args->nr_local) {
637 		ret = -EINVAL;
638 		goto out_ret;
639 	}
640 	/* odp-mr is not supported for multiple requests within one message */
641 	if (args->nr_local != 1)
642 		odp_supported = false;
643 
644 	iovs = vec->iov;
645 
646 	nr_pages = rds_rdma_pages(iovs, args->nr_local);
647 	if (nr_pages < 0) {
648 		ret = -EINVAL;
649 		goto out_ret;
650 	}
651 
652 	pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
653 	if (!pages) {
654 		ret = -ENOMEM;
655 		goto out_ret;
656 	}
657 
658 	op->op_write = !!(args->flags & RDS_RDMA_READWRITE);
659 	op->op_fence = !!(args->flags & RDS_RDMA_FENCE);
660 	op->op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
661 	op->op_silent = !!(args->flags & RDS_RDMA_SILENT);
662 	op->op_active = 1;
663 	op->op_recverr = rs->rs_recverr;
664 	op->op_odp_mr = NULL;
665 
666 	WARN_ON(!nr_pages);
667 	op->op_sg = rds_message_alloc_sgs(rm, nr_pages, &ret);
668 	if (!op->op_sg)
669 		goto out_pages;
670 
671 	if (op->op_notify || op->op_recverr) {
672 		/* We allocate an uninitialized notifier here, because
673 		 * we don't want to do that in the completion handler. We
674 		 * would have to use GFP_ATOMIC there, and don't want to deal
675 		 * with failed allocations.
676 		 */
677 		op->op_notifier = kmalloc(sizeof(struct rds_notifier), GFP_KERNEL);
678 		if (!op->op_notifier) {
679 			ret = -ENOMEM;
680 			goto out_pages;
681 		}
682 		op->op_notifier->n_user_token = args->user_token;
683 		op->op_notifier->n_status = RDS_RDMA_SUCCESS;
684 	}
685 
686 	/* The cookie contains the R_Key of the remote memory region, and
687 	 * optionally an offset into it. This is how we implement RDMA into
688 	 * unaligned memory.
689 	 * When setting up the RDMA, we need to add that offset to the
690 	 * destination address (which is really an offset into the MR)
691 	 * FIXME: We may want to move this into ib_rdma.c
692 	 */
693 	op->op_rkey = rds_rdma_cookie_key(args->cookie);
694 	op->op_remote_addr = args->remote_vec.addr + rds_rdma_cookie_offset(args->cookie);
695 
696 	nr_bytes = 0;
697 
698 	rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n",
699 	       (unsigned long long)args->nr_local,
700 	       (unsigned long long)args->remote_vec.addr,
701 	       op->op_rkey);
702 
703 	for (i = 0; i < args->nr_local; i++) {
704 		struct rds_iovec *iov = &iovs[i];
705 		/* don't need to check, rds_rdma_pages() verified nr will be +nonzero */
706 		unsigned int nr = rds_pages_in_vec(iov);
707 
708 		rs->rs_user_addr = iov->addr;
709 		rs->rs_user_bytes = iov->bytes;
710 
711 		/* If it's a WRITE operation, we want to pin the pages for reading.
712 		 * If it's a READ operation, we need to pin the pages for writing.
713 		 */
714 		ret = rds_pin_pages(iov->addr, nr, pages, !op->op_write);
715 		if ((!odp_supported && ret <= 0) ||
716 		    (odp_supported && ret <= 0 && ret != -EOPNOTSUPP))
717 			goto out_pages;
718 
719 		if (ret == -EOPNOTSUPP) {
720 			struct rds_mr *local_odp_mr;
721 
722 			if (!rs->rs_transport->get_mr) {
723 				ret = -EOPNOTSUPP;
724 				goto out_pages;
725 			}
726 			local_odp_mr =
727 				kzalloc(sizeof(*local_odp_mr), GFP_KERNEL);
728 			if (!local_odp_mr) {
729 				ret = -ENOMEM;
730 				goto out_pages;
731 			}
732 			RB_CLEAR_NODE(&local_odp_mr->r_rb_node);
733 			refcount_set(&local_odp_mr->r_refcount, 1);
734 			local_odp_mr->r_trans = rs->rs_transport;
735 			local_odp_mr->r_sock = rs;
736 			local_odp_mr->r_trans_private =
737 				rs->rs_transport->get_mr(
738 					NULL, 0, rs, &local_odp_mr->r_key, NULL,
739 					iov->addr, iov->bytes, ODP_VIRTUAL);
740 			if (IS_ERR(local_odp_mr->r_trans_private)) {
741 				ret = IS_ERR(local_odp_mr->r_trans_private);
742 				rdsdebug("get_mr ret %d %p\"", ret,
743 					 local_odp_mr->r_trans_private);
744 				kfree(local_odp_mr);
745 				ret = -EOPNOTSUPP;
746 				goto out_pages;
747 			}
748 			rdsdebug("Need odp; local_odp_mr %p trans_private %p\n",
749 				 local_odp_mr, local_odp_mr->r_trans_private);
750 			op->op_odp_mr = local_odp_mr;
751 			op->op_odp_addr = iov->addr;
752 		}
753 
754 		rdsdebug("RDS: nr_bytes %u nr %u iov->bytes %llu iov->addr %llx\n",
755 			 nr_bytes, nr, iov->bytes, iov->addr);
756 
757 		nr_bytes += iov->bytes;
758 
759 		for (j = 0; j < nr; j++) {
760 			unsigned int offset = iov->addr & ~PAGE_MASK;
761 			struct scatterlist *sg;
762 
763 			sg = &op->op_sg[op->op_nents + j];
764 			sg_set_page(sg, pages[j],
765 					min_t(unsigned int, iov->bytes, PAGE_SIZE - offset),
766 					offset);
767 
768 			sg_dma_len(sg) = sg->length;
769 			rdsdebug("RDS: sg->offset %x sg->len %x iov->addr %llx iov->bytes %llu\n",
770 			       sg->offset, sg->length, iov->addr, iov->bytes);
771 
772 			iov->addr += sg->length;
773 			iov->bytes -= sg->length;
774 		}
775 
776 		op->op_nents += nr;
777 	}
778 
779 	if (nr_bytes > args->remote_vec.bytes) {
780 		rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n",
781 				nr_bytes,
782 				(unsigned int) args->remote_vec.bytes);
783 		ret = -EINVAL;
784 		goto out_pages;
785 	}
786 	op->op_bytes = nr_bytes;
787 	ret = 0;
788 
789 out_pages:
790 	kfree(pages);
791 out_ret:
792 	if (ret)
793 		rds_rdma_free_op(op);
794 	else
795 		rds_stats_inc(s_send_rdma);
796 
797 	return ret;
798 }
799 
800 /*
801  * The application wants us to pass an RDMA destination (aka MR)
802  * to the remote
803  */
804 int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
805 			  struct cmsghdr *cmsg)
806 {
807 	unsigned long flags;
808 	struct rds_mr *mr;
809 	u32 r_key;
810 	int err = 0;
811 
812 	if (cmsg->cmsg_len < CMSG_LEN(sizeof(rds_rdma_cookie_t)) ||
813 	    rm->m_rdma_cookie != 0)
814 		return -EINVAL;
815 
816 	memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg), sizeof(rm->m_rdma_cookie));
817 
818 	/* We are reusing a previously mapped MR here. Most likely, the
819 	 * application has written to the buffer, so we need to explicitly
820 	 * flush those writes to RAM. Otherwise the HCA may not see them
821 	 * when doing a DMA from that buffer.
822 	 */
823 	r_key = rds_rdma_cookie_key(rm->m_rdma_cookie);
824 
825 	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
826 	mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
827 	if (!mr)
828 		err = -EINVAL;	/* invalid r_key */
829 	else
830 		refcount_inc(&mr->r_refcount);
831 	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
832 
833 	if (mr) {
834 		mr->r_trans->sync_mr(mr->r_trans_private,
835 				     DMA_TO_DEVICE);
836 		rm->rdma.op_rdma_mr = mr;
837 	}
838 	return err;
839 }
840 
841 /*
842  * The application passes us an address range it wants to enable RDMA
843  * to/from. We map the area, and save the <R_Key,offset> pair
844  * in rm->m_rdma_cookie. This causes it to be sent along to the peer
845  * in an extension header.
846  */
847 int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
848 			  struct cmsghdr *cmsg)
849 {
850 	if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_get_mr_args)) ||
851 	    rm->m_rdma_cookie != 0)
852 		return -EINVAL;
853 
854 	return __rds_rdma_map(rs, CMSG_DATA(cmsg), &rm->m_rdma_cookie,
855 			      &rm->rdma.op_rdma_mr, rm->m_conn_path);
856 }
857 
858 /*
859  * Fill in rds_message for an atomic request.
860  */
861 int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
862 		    struct cmsghdr *cmsg)
863 {
864 	struct page *page = NULL;
865 	struct rds_atomic_args *args;
866 	int ret = 0;
867 
868 	if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_atomic_args))
869 	 || rm->atomic.op_active)
870 		return -EINVAL;
871 
872 	args = CMSG_DATA(cmsg);
873 
874 	/* Nonmasked & masked cmsg ops converted to masked hw ops */
875 	switch (cmsg->cmsg_type) {
876 	case RDS_CMSG_ATOMIC_FADD:
877 		rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
878 		rm->atomic.op_m_fadd.add = args->fadd.add;
879 		rm->atomic.op_m_fadd.nocarry_mask = 0;
880 		break;
881 	case RDS_CMSG_MASKED_ATOMIC_FADD:
882 		rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
883 		rm->atomic.op_m_fadd.add = args->m_fadd.add;
884 		rm->atomic.op_m_fadd.nocarry_mask = args->m_fadd.nocarry_mask;
885 		break;
886 	case RDS_CMSG_ATOMIC_CSWP:
887 		rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
888 		rm->atomic.op_m_cswp.compare = args->cswp.compare;
889 		rm->atomic.op_m_cswp.swap = args->cswp.swap;
890 		rm->atomic.op_m_cswp.compare_mask = ~0;
891 		rm->atomic.op_m_cswp.swap_mask = ~0;
892 		break;
893 	case RDS_CMSG_MASKED_ATOMIC_CSWP:
894 		rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
895 		rm->atomic.op_m_cswp.compare = args->m_cswp.compare;
896 		rm->atomic.op_m_cswp.swap = args->m_cswp.swap;
897 		rm->atomic.op_m_cswp.compare_mask = args->m_cswp.compare_mask;
898 		rm->atomic.op_m_cswp.swap_mask = args->m_cswp.swap_mask;
899 		break;
900 	default:
901 		BUG(); /* should never happen */
902 	}
903 
904 	rm->atomic.op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
905 	rm->atomic.op_silent = !!(args->flags & RDS_RDMA_SILENT);
906 	rm->atomic.op_active = 1;
907 	rm->atomic.op_recverr = rs->rs_recverr;
908 	rm->atomic.op_sg = rds_message_alloc_sgs(rm, 1, &ret);
909 	if (!rm->atomic.op_sg)
910 		goto err;
911 
912 	/* verify 8 byte-aligned */
913 	if (args->local_addr & 0x7) {
914 		ret = -EFAULT;
915 		goto err;
916 	}
917 
918 	ret = rds_pin_pages(args->local_addr, 1, &page, 1);
919 	if (ret != 1)
920 		goto err;
921 	ret = 0;
922 
923 	sg_set_page(rm->atomic.op_sg, page, 8, offset_in_page(args->local_addr));
924 
925 	if (rm->atomic.op_notify || rm->atomic.op_recverr) {
926 		/* We allocate an uninitialized notifier here, because
927 		 * we don't want to do that in the completion handler. We
928 		 * would have to use GFP_ATOMIC there, and don't want to deal
929 		 * with failed allocations.
930 		 */
931 		rm->atomic.op_notifier = kmalloc(sizeof(*rm->atomic.op_notifier), GFP_KERNEL);
932 		if (!rm->atomic.op_notifier) {
933 			ret = -ENOMEM;
934 			goto err;
935 		}
936 
937 		rm->atomic.op_notifier->n_user_token = args->user_token;
938 		rm->atomic.op_notifier->n_status = RDS_RDMA_SUCCESS;
939 	}
940 
941 	rm->atomic.op_rkey = rds_rdma_cookie_key(args->cookie);
942 	rm->atomic.op_remote_addr = args->remote_addr + rds_rdma_cookie_offset(args->cookie);
943 
944 	return ret;
945 err:
946 	if (page)
947 		put_page(page);
948 	rm->atomic.op_active = 0;
949 	kfree(rm->atomic.op_notifier);
950 
951 	return ret;
952 }
953