18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2007, 2020 Oracle and/or its affiliates.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
348c2ecf20Sopenharmony_ci#include <linux/slab.h>
358c2ecf20Sopenharmony_ci#include <linux/rbtree.h>
368c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> /* for DMA_*_DEVICE */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include "rds.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * XXX
428c2ecf20Sopenharmony_ci *  - build with sparse
438c2ecf20Sopenharmony_ci *  - should we detect duplicate keys on a socket?  hmm.
448c2ecf20Sopenharmony_ci *  - an rdma is an mlock, apply rlimit?
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/*
488c2ecf20Sopenharmony_ci * get the number of pages by looking at the page indices that the start and
498c2ecf20Sopenharmony_ci * end addresses fall in.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * Returns 0 if the vec is invalid.  It is invalid if the number of bytes
528c2ecf20Sopenharmony_ci * causes the address to wrap or overflows an unsigned int.  This comes
538c2ecf20Sopenharmony_ci * from being stored in the 'length' member of 'struct scatterlist'.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_cistatic unsigned int rds_pages_in_vec(struct rds_iovec *vec)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	if ((vec->addr + vec->bytes <= vec->addr) ||
588c2ecf20Sopenharmony_ci	    (vec->bytes > (u64)UINT_MAX))
598c2ecf20Sopenharmony_ci		return 0;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return ((vec->addr + vec->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT) -
628c2ecf20Sopenharmony_ci		(vec->addr >> PAGE_SHIFT);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic struct rds_mr *rds_mr_tree_walk(struct rb_root *root, u64 key,
668c2ecf20Sopenharmony_ci				       struct rds_mr *insert)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct rb_node **p = &root->rb_node;
698c2ecf20Sopenharmony_ci	struct rb_node *parent = NULL;
708c2ecf20Sopenharmony_ci	struct rds_mr *mr;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	while (*p) {
738c2ecf20Sopenharmony_ci		parent = *p;
748c2ecf20Sopenharmony_ci		mr = rb_entry(parent, struct rds_mr, r_rb_node);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		if (key < mr->r_key)
778c2ecf20Sopenharmony_ci			p = &(*p)->rb_left;
788c2ecf20Sopenharmony_ci		else if (key > mr->r_key)
798c2ecf20Sopenharmony_ci			p = &(*p)->rb_right;
808c2ecf20Sopenharmony_ci		else
818c2ecf20Sopenharmony_ci			return mr;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (insert) {
858c2ecf20Sopenharmony_ci		rb_link_node(&insert->r_rb_node, parent, p);
868c2ecf20Sopenharmony_ci		rb_insert_color(&insert->r_rb_node, root);
878c2ecf20Sopenharmony_ci		kref_get(&insert->r_kref);
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci	return NULL;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * Destroy the transport-specific part of a MR.
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_cistatic void rds_destroy_mr(struct rds_mr *mr)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct rds_sock *rs = mr->r_sock;
988c2ecf20Sopenharmony_ci	void *trans_private = NULL;
998c2ecf20Sopenharmony_ci	unsigned long flags;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	rdsdebug("RDS: destroy mr key is %x refcnt %u\n",
1028c2ecf20Sopenharmony_ci		 mr->r_key, kref_read(&mr->r_kref));
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
1058c2ecf20Sopenharmony_ci	if (!RB_EMPTY_NODE(&mr->r_rb_node))
1068c2ecf20Sopenharmony_ci		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
1078c2ecf20Sopenharmony_ci	trans_private = mr->r_trans_private;
1088c2ecf20Sopenharmony_ci	mr->r_trans_private = NULL;
1098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (trans_private)
1128c2ecf20Sopenharmony_ci		mr->r_trans->free_mr(trans_private, mr->r_invalidate);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_civoid __rds_put_mr_final(struct kref *kref)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct rds_mr *mr = container_of(kref, struct rds_mr, r_kref);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	rds_destroy_mr(mr);
1208c2ecf20Sopenharmony_ci	kfree(mr);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/*
1248c2ecf20Sopenharmony_ci * By the time this is called we can't have any more ioctls called on
1258c2ecf20Sopenharmony_ci * the socket so we don't need to worry about racing with others.
1268c2ecf20Sopenharmony_ci */
1278c2ecf20Sopenharmony_civoid rds_rdma_drop_keys(struct rds_sock *rs)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct rds_mr *mr;
1308c2ecf20Sopenharmony_ci	struct rb_node *node;
1318c2ecf20Sopenharmony_ci	unsigned long flags;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* Release any MRs associated with this socket */
1348c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
1358c2ecf20Sopenharmony_ci	while ((node = rb_first(&rs->rs_rdma_keys))) {
1368c2ecf20Sopenharmony_ci		mr = rb_entry(node, struct rds_mr, r_rb_node);
1378c2ecf20Sopenharmony_ci		if (mr->r_trans == rs->rs_transport)
1388c2ecf20Sopenharmony_ci			mr->r_invalidate = 0;
1398c2ecf20Sopenharmony_ci		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
1408c2ecf20Sopenharmony_ci		RB_CLEAR_NODE(&mr->r_rb_node);
1418c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
1428c2ecf20Sopenharmony_ci		kref_put(&mr->r_kref, __rds_put_mr_final);
1438c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rs->rs_rdma_lock, flags);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (rs->rs_transport && rs->rs_transport->flush_mrs)
1488c2ecf20Sopenharmony_ci		rs->rs_transport->flush_mrs();
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/*
1528c2ecf20Sopenharmony_ci * Helper function to pin user pages.
1538c2ecf20Sopenharmony_ci */
1548c2ecf20Sopenharmony_cistatic int rds_pin_pages(unsigned long user_addr, unsigned int nr_pages,
1558c2ecf20Sopenharmony_ci			struct page **pages, int write)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	unsigned int gup_flags = FOLL_LONGTERM;
1588c2ecf20Sopenharmony_ci	int ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	if (write)
1618c2ecf20Sopenharmony_ci		gup_flags |= FOLL_WRITE;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ret = pin_user_pages_fast(user_addr, nr_pages, gup_flags, pages);
1648c2ecf20Sopenharmony_ci	if (ret >= 0 && ret < nr_pages) {
1658c2ecf20Sopenharmony_ci		unpin_user_pages(pages, ret);
1668c2ecf20Sopenharmony_ci		ret = -EFAULT;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return ret;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic int __rds_rdma_map(struct rds_sock *rs, struct rds_get_mr_args *args,
1738c2ecf20Sopenharmony_ci			  u64 *cookie_ret, struct rds_mr **mr_ret,
1748c2ecf20Sopenharmony_ci			  struct rds_conn_path *cp)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct rds_mr *mr = NULL, *found;
1778c2ecf20Sopenharmony_ci	struct scatterlist *sg = NULL;
1788c2ecf20Sopenharmony_ci	unsigned int nr_pages;
1798c2ecf20Sopenharmony_ci	struct page **pages = NULL;
1808c2ecf20Sopenharmony_ci	void *trans_private;
1818c2ecf20Sopenharmony_ci	unsigned long flags;
1828c2ecf20Sopenharmony_ci	rds_rdma_cookie_t cookie;
1838c2ecf20Sopenharmony_ci	unsigned int nents = 0;
1848c2ecf20Sopenharmony_ci	int need_odp = 0;
1858c2ecf20Sopenharmony_ci	long i;
1868c2ecf20Sopenharmony_ci	int ret;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (ipv6_addr_any(&rs->rs_bound_addr) || !rs->rs_transport) {
1898c2ecf20Sopenharmony_ci		ret = -ENOTCONN; /* XXX not a great errno */
1908c2ecf20Sopenharmony_ci		goto out;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (!rs->rs_transport->get_mr) {
1948c2ecf20Sopenharmony_ci		ret = -EOPNOTSUPP;
1958c2ecf20Sopenharmony_ci		goto out;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* If the combination of the addr and size requested for this memory
1998c2ecf20Sopenharmony_ci	 * region causes an integer overflow, return error.
2008c2ecf20Sopenharmony_ci	 */
2018c2ecf20Sopenharmony_ci	if (((args->vec.addr + args->vec.bytes) < args->vec.addr) ||
2028c2ecf20Sopenharmony_ci	    PAGE_ALIGN(args->vec.addr + args->vec.bytes) <
2038c2ecf20Sopenharmony_ci		    (args->vec.addr + args->vec.bytes)) {
2048c2ecf20Sopenharmony_ci		ret = -EINVAL;
2058c2ecf20Sopenharmony_ci		goto out;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (!can_do_mlock()) {
2098c2ecf20Sopenharmony_ci		ret = -EPERM;
2108c2ecf20Sopenharmony_ci		goto out;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	nr_pages = rds_pages_in_vec(&args->vec);
2148c2ecf20Sopenharmony_ci	if (nr_pages == 0) {
2158c2ecf20Sopenharmony_ci		ret = -EINVAL;
2168c2ecf20Sopenharmony_ci		goto out;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* Restrict the size of mr irrespective of underlying transport
2208c2ecf20Sopenharmony_ci	 * To account for unaligned mr regions, subtract one from nr_pages
2218c2ecf20Sopenharmony_ci	 */
2228c2ecf20Sopenharmony_ci	if ((nr_pages - 1) > (RDS_MAX_MSG_SIZE >> PAGE_SHIFT)) {
2238c2ecf20Sopenharmony_ci		ret = -EMSGSIZE;
2248c2ecf20Sopenharmony_ci		goto out;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n",
2288c2ecf20Sopenharmony_ci		args->vec.addr, args->vec.bytes, nr_pages);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* XXX clamp nr_pages to limit the size of this alloc? */
2318c2ecf20Sopenharmony_ci	pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
2328c2ecf20Sopenharmony_ci	if (!pages) {
2338c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2348c2ecf20Sopenharmony_ci		goto out;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	mr = kzalloc(sizeof(struct rds_mr), GFP_KERNEL);
2388c2ecf20Sopenharmony_ci	if (!mr) {
2398c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2408c2ecf20Sopenharmony_ci		goto out;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	kref_init(&mr->r_kref);
2448c2ecf20Sopenharmony_ci	RB_CLEAR_NODE(&mr->r_rb_node);
2458c2ecf20Sopenharmony_ci	mr->r_trans = rs->rs_transport;
2468c2ecf20Sopenharmony_ci	mr->r_sock = rs;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (args->flags & RDS_RDMA_USE_ONCE)
2498c2ecf20Sopenharmony_ci		mr->r_use_once = 1;
2508c2ecf20Sopenharmony_ci	if (args->flags & RDS_RDMA_INVALIDATE)
2518c2ecf20Sopenharmony_ci		mr->r_invalidate = 1;
2528c2ecf20Sopenharmony_ci	if (args->flags & RDS_RDMA_READWRITE)
2538c2ecf20Sopenharmony_ci		mr->r_write = 1;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/*
2568c2ecf20Sopenharmony_ci	 * Pin the pages that make up the user buffer and transfer the page
2578c2ecf20Sopenharmony_ci	 * pointers to the mr's sg array.  We check to see if we've mapped
2588c2ecf20Sopenharmony_ci	 * the whole region after transferring the partial page references
2598c2ecf20Sopenharmony_ci	 * to the sg array so that we can have one page ref cleanup path.
2608c2ecf20Sopenharmony_ci	 *
2618c2ecf20Sopenharmony_ci	 * For now we have no flag that tells us whether the mapping is
2628c2ecf20Sopenharmony_ci	 * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to
2638c2ecf20Sopenharmony_ci	 * the zero page.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	ret = rds_pin_pages(args->vec.addr, nr_pages, pages, 1);
2668c2ecf20Sopenharmony_ci	if (ret == -EOPNOTSUPP) {
2678c2ecf20Sopenharmony_ci		need_odp = 1;
2688c2ecf20Sopenharmony_ci	} else if (ret <= 0) {
2698c2ecf20Sopenharmony_ci		goto out;
2708c2ecf20Sopenharmony_ci	} else {
2718c2ecf20Sopenharmony_ci		nents = ret;
2728c2ecf20Sopenharmony_ci		sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
2738c2ecf20Sopenharmony_ci		if (!sg) {
2748c2ecf20Sopenharmony_ci			ret = -ENOMEM;
2758c2ecf20Sopenharmony_ci			goto out;
2768c2ecf20Sopenharmony_ci		}
2778c2ecf20Sopenharmony_ci		WARN_ON(!nents);
2788c2ecf20Sopenharmony_ci		sg_init_table(sg, nents);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci		/* Stick all pages into the scatterlist */
2818c2ecf20Sopenharmony_ci		for (i = 0 ; i < nents; i++)
2828c2ecf20Sopenharmony_ci			sg_set_page(&sg[i], pages[i], PAGE_SIZE, 0);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci		rdsdebug("RDS: trans_private nents is %u\n", nents);
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci	/* Obtain a transport specific MR. If this succeeds, the
2878c2ecf20Sopenharmony_ci	 * s/g list is now owned by the MR.
2888c2ecf20Sopenharmony_ci	 * Note that dma_map() implies that pending writes are
2898c2ecf20Sopenharmony_ci	 * flushed to RAM, so no dma_sync is needed here. */
2908c2ecf20Sopenharmony_ci	trans_private = rs->rs_transport->get_mr(
2918c2ecf20Sopenharmony_ci		sg, nents, rs, &mr->r_key, cp ? cp->cp_conn : NULL,
2928c2ecf20Sopenharmony_ci		args->vec.addr, args->vec.bytes,
2938c2ecf20Sopenharmony_ci		need_odp ? ODP_ZEROBASED : ODP_NOT_NEEDED);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (IS_ERR(trans_private)) {
2968c2ecf20Sopenharmony_ci		/* In ODP case, we don't GUP pages, so don't need
2978c2ecf20Sopenharmony_ci		 * to release anything.
2988c2ecf20Sopenharmony_ci		 */
2998c2ecf20Sopenharmony_ci		if (!need_odp) {
3008c2ecf20Sopenharmony_ci			unpin_user_pages(pages, nr_pages);
3018c2ecf20Sopenharmony_ci			kfree(sg);
3028c2ecf20Sopenharmony_ci		}
3038c2ecf20Sopenharmony_ci		ret = PTR_ERR(trans_private);
3048c2ecf20Sopenharmony_ci		goto out;
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	mr->r_trans_private = trans_private;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n",
3108c2ecf20Sopenharmony_ci	       mr->r_key, (void *)(unsigned long) args->cookie_addr);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* The user may pass us an unaligned address, but we can only
3138c2ecf20Sopenharmony_ci	 * map page aligned regions. So we keep the offset, and build
3148c2ecf20Sopenharmony_ci	 * a 64bit cookie containing <R_Key, offset> and pass that
3158c2ecf20Sopenharmony_ci	 * around. */
3168c2ecf20Sopenharmony_ci	if (need_odp)
3178c2ecf20Sopenharmony_ci		cookie = rds_rdma_make_cookie(mr->r_key, 0);
3188c2ecf20Sopenharmony_ci	else
3198c2ecf20Sopenharmony_ci		cookie = rds_rdma_make_cookie(mr->r_key,
3208c2ecf20Sopenharmony_ci					      args->vec.addr & ~PAGE_MASK);
3218c2ecf20Sopenharmony_ci	if (cookie_ret)
3228c2ecf20Sopenharmony_ci		*cookie_ret = cookie;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (args->cookie_addr &&
3258c2ecf20Sopenharmony_ci	    put_user(cookie, (u64 __user *)(unsigned long)args->cookie_addr)) {
3268c2ecf20Sopenharmony_ci		if (!need_odp) {
3278c2ecf20Sopenharmony_ci			unpin_user_pages(pages, nr_pages);
3288c2ecf20Sopenharmony_ci			kfree(sg);
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci		ret = -EFAULT;
3318c2ecf20Sopenharmony_ci		goto out;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/* Inserting the new MR into the rbtree bumps its
3358c2ecf20Sopenharmony_ci	 * reference count. */
3368c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
3378c2ecf20Sopenharmony_ci	found = rds_mr_tree_walk(&rs->rs_rdma_keys, mr->r_key, mr);
3388c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	BUG_ON(found && found != mr);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	rdsdebug("RDS: get_mr key is %x\n", mr->r_key);
3438c2ecf20Sopenharmony_ci	if (mr_ret) {
3448c2ecf20Sopenharmony_ci		kref_get(&mr->r_kref);
3458c2ecf20Sopenharmony_ci		*mr_ret = mr;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	ret = 0;
3498c2ecf20Sopenharmony_ciout:
3508c2ecf20Sopenharmony_ci	kfree(pages);
3518c2ecf20Sopenharmony_ci	if (mr)
3528c2ecf20Sopenharmony_ci		kref_put(&mr->r_kref, __rds_put_mr_final);
3538c2ecf20Sopenharmony_ci	return ret;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ciint rds_get_mr(struct rds_sock *rs, sockptr_t optval, int optlen)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct rds_get_mr_args args;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	if (optlen != sizeof(struct rds_get_mr_args))
3618c2ecf20Sopenharmony_ci		return -EINVAL;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (copy_from_sockptr(&args, optval, sizeof(struct rds_get_mr_args)))
3648c2ecf20Sopenharmony_ci		return -EFAULT;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return __rds_rdma_map(rs, &args, NULL, NULL, NULL);
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ciint rds_get_mr_for_dest(struct rds_sock *rs, sockptr_t optval, int optlen)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct rds_get_mr_for_dest_args args;
3728c2ecf20Sopenharmony_ci	struct rds_get_mr_args new_args;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (optlen != sizeof(struct rds_get_mr_for_dest_args))
3758c2ecf20Sopenharmony_ci		return -EINVAL;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	if (copy_from_sockptr(&args, optval,
3788c2ecf20Sopenharmony_ci			   sizeof(struct rds_get_mr_for_dest_args)))
3798c2ecf20Sopenharmony_ci		return -EFAULT;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/*
3828c2ecf20Sopenharmony_ci	 * Initially, just behave like get_mr().
3838c2ecf20Sopenharmony_ci	 * TODO: Implement get_mr as wrapper around this
3848c2ecf20Sopenharmony_ci	 *	 and deprecate it.
3858c2ecf20Sopenharmony_ci	 */
3868c2ecf20Sopenharmony_ci	new_args.vec = args.vec;
3878c2ecf20Sopenharmony_ci	new_args.cookie_addr = args.cookie_addr;
3888c2ecf20Sopenharmony_ci	new_args.flags = args.flags;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	return __rds_rdma_map(rs, &new_args, NULL, NULL, NULL);
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci/*
3948c2ecf20Sopenharmony_ci * Free the MR indicated by the given R_Key
3958c2ecf20Sopenharmony_ci */
3968c2ecf20Sopenharmony_ciint rds_free_mr(struct rds_sock *rs, sockptr_t optval, int optlen)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	struct rds_free_mr_args args;
3998c2ecf20Sopenharmony_ci	struct rds_mr *mr;
4008c2ecf20Sopenharmony_ci	unsigned long flags;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	if (optlen != sizeof(struct rds_free_mr_args))
4038c2ecf20Sopenharmony_ci		return -EINVAL;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if (copy_from_sockptr(&args, optval, sizeof(struct rds_free_mr_args)))
4068c2ecf20Sopenharmony_ci		return -EFAULT;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* Special case - a null cookie means flush all unused MRs */
4098c2ecf20Sopenharmony_ci	if (args.cookie == 0) {
4108c2ecf20Sopenharmony_ci		if (!rs->rs_transport || !rs->rs_transport->flush_mrs)
4118c2ecf20Sopenharmony_ci			return -EINVAL;
4128c2ecf20Sopenharmony_ci		rs->rs_transport->flush_mrs();
4138c2ecf20Sopenharmony_ci		return 0;
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	/* Look up the MR given its R_key and remove it from the rbtree
4178c2ecf20Sopenharmony_ci	 * so nobody else finds it.
4188c2ecf20Sopenharmony_ci	 * This should also prevent races with rds_rdma_unuse.
4198c2ecf20Sopenharmony_ci	 */
4208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
4218c2ecf20Sopenharmony_ci	mr = rds_mr_tree_walk(&rs->rs_rdma_keys, rds_rdma_cookie_key(args.cookie), NULL);
4228c2ecf20Sopenharmony_ci	if (mr) {
4238c2ecf20Sopenharmony_ci		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
4248c2ecf20Sopenharmony_ci		RB_CLEAR_NODE(&mr->r_rb_node);
4258c2ecf20Sopenharmony_ci		if (args.flags & RDS_RDMA_INVALIDATE)
4268c2ecf20Sopenharmony_ci			mr->r_invalidate = 1;
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if (!mr)
4318c2ecf20Sopenharmony_ci		return -EINVAL;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	kref_put(&mr->r_kref, __rds_put_mr_final);
4348c2ecf20Sopenharmony_ci	return 0;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci/*
4388c2ecf20Sopenharmony_ci * This is called when we receive an extension header that
4398c2ecf20Sopenharmony_ci * tells us this MR was used. It allows us to implement
4408c2ecf20Sopenharmony_ci * use_once semantics
4418c2ecf20Sopenharmony_ci */
4428c2ecf20Sopenharmony_civoid rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	struct rds_mr *mr;
4458c2ecf20Sopenharmony_ci	unsigned long flags;
4468c2ecf20Sopenharmony_ci	int zot_me = 0;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
4498c2ecf20Sopenharmony_ci	mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
4508c2ecf20Sopenharmony_ci	if (!mr) {
4518c2ecf20Sopenharmony_ci		pr_debug("rds: trying to unuse MR with unknown r_key %u!\n",
4528c2ecf20Sopenharmony_ci			 r_key);
4538c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
4548c2ecf20Sopenharmony_ci		return;
4558c2ecf20Sopenharmony_ci	}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* Get a reference so that the MR won't go away before calling
4588c2ecf20Sopenharmony_ci	 * sync_mr() below.
4598c2ecf20Sopenharmony_ci	 */
4608c2ecf20Sopenharmony_ci	kref_get(&mr->r_kref);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/* If it is going to be freed, remove it from the tree now so
4638c2ecf20Sopenharmony_ci	 * that no other thread can find it and free it.
4648c2ecf20Sopenharmony_ci	 */
4658c2ecf20Sopenharmony_ci	if (mr->r_use_once || force) {
4668c2ecf20Sopenharmony_ci		rb_erase(&mr->r_rb_node, &rs->rs_rdma_keys);
4678c2ecf20Sopenharmony_ci		RB_CLEAR_NODE(&mr->r_rb_node);
4688c2ecf20Sopenharmony_ci		zot_me = 1;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* May have to issue a dma_sync on this memory region.
4738c2ecf20Sopenharmony_ci	 * Note we could avoid this if the operation was a RDMA READ,
4748c2ecf20Sopenharmony_ci	 * but at this point we can't tell. */
4758c2ecf20Sopenharmony_ci	if (mr->r_trans->sync_mr)
4768c2ecf20Sopenharmony_ci		mr->r_trans->sync_mr(mr->r_trans_private, DMA_FROM_DEVICE);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/* Release the reference held above. */
4798c2ecf20Sopenharmony_ci	kref_put(&mr->r_kref, __rds_put_mr_final);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* If the MR was marked as invalidate, this will
4828c2ecf20Sopenharmony_ci	 * trigger an async flush. */
4838c2ecf20Sopenharmony_ci	if (zot_me)
4848c2ecf20Sopenharmony_ci		kref_put(&mr->r_kref, __rds_put_mr_final);
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_civoid rds_rdma_free_op(struct rm_rdma_op *ro)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	unsigned int i;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	if (ro->op_odp_mr) {
4928c2ecf20Sopenharmony_ci		kref_put(&ro->op_odp_mr->r_kref, __rds_put_mr_final);
4938c2ecf20Sopenharmony_ci	} else {
4948c2ecf20Sopenharmony_ci		for (i = 0; i < ro->op_nents; i++) {
4958c2ecf20Sopenharmony_ci			struct page *page = sg_page(&ro->op_sg[i]);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci			/* Mark page dirty if it was possibly modified, which
4988c2ecf20Sopenharmony_ci			 * is the case for a RDMA_READ which copies from remote
4998c2ecf20Sopenharmony_ci			 * to local memory
5008c2ecf20Sopenharmony_ci			 */
5018c2ecf20Sopenharmony_ci			unpin_user_pages_dirty_lock(&page, 1, !ro->op_write);
5028c2ecf20Sopenharmony_ci		}
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	kfree(ro->op_notifier);
5068c2ecf20Sopenharmony_ci	ro->op_notifier = NULL;
5078c2ecf20Sopenharmony_ci	ro->op_active = 0;
5088c2ecf20Sopenharmony_ci	ro->op_odp_mr = NULL;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_civoid rds_atomic_free_op(struct rm_atomic_op *ao)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	struct page *page = sg_page(ao->op_sg);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/* Mark page dirty if it was possibly modified, which
5168c2ecf20Sopenharmony_ci	 * is the case for a RDMA_READ which copies from remote
5178c2ecf20Sopenharmony_ci	 * to local memory */
5188c2ecf20Sopenharmony_ci	unpin_user_pages_dirty_lock(&page, 1, true);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	kfree(ao->op_notifier);
5218c2ecf20Sopenharmony_ci	ao->op_notifier = NULL;
5228c2ecf20Sopenharmony_ci	ao->op_active = 0;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci/*
5278c2ecf20Sopenharmony_ci * Count the number of pages needed to describe an incoming iovec array.
5288c2ecf20Sopenharmony_ci */
5298c2ecf20Sopenharmony_cistatic int rds_rdma_pages(struct rds_iovec iov[], int nr_iovecs)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	int tot_pages = 0;
5328c2ecf20Sopenharmony_ci	unsigned int nr_pages;
5338c2ecf20Sopenharmony_ci	unsigned int i;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* figure out the number of pages in the vector */
5368c2ecf20Sopenharmony_ci	for (i = 0; i < nr_iovecs; i++) {
5378c2ecf20Sopenharmony_ci		nr_pages = rds_pages_in_vec(&iov[i]);
5388c2ecf20Sopenharmony_ci		if (nr_pages == 0)
5398c2ecf20Sopenharmony_ci			return -EINVAL;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		tot_pages += nr_pages;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		/*
5448c2ecf20Sopenharmony_ci		 * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
5458c2ecf20Sopenharmony_ci		 * so tot_pages cannot overflow without first going negative.
5468c2ecf20Sopenharmony_ci		 */
5478c2ecf20Sopenharmony_ci		if (tot_pages < 0)
5488c2ecf20Sopenharmony_ci			return -EINVAL;
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	return tot_pages;
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ciint rds_rdma_extra_size(struct rds_rdma_args *args,
5558c2ecf20Sopenharmony_ci			struct rds_iov_vector *iov)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	struct rds_iovec *vec;
5588c2ecf20Sopenharmony_ci	struct rds_iovec __user *local_vec;
5598c2ecf20Sopenharmony_ci	int tot_pages = 0;
5608c2ecf20Sopenharmony_ci	unsigned int nr_pages;
5618c2ecf20Sopenharmony_ci	unsigned int i;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	local_vec = (struct rds_iovec __user *)(unsigned long) args->local_vec_addr;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (args->nr_local == 0)
5668c2ecf20Sopenharmony_ci		return -EINVAL;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	if (args->nr_local > UIO_MAXIOV)
5698c2ecf20Sopenharmony_ci		return -EMSGSIZE;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	iov->iov = kcalloc(args->nr_local,
5728c2ecf20Sopenharmony_ci			   sizeof(struct rds_iovec),
5738c2ecf20Sopenharmony_ci			   GFP_KERNEL);
5748c2ecf20Sopenharmony_ci	if (!iov->iov)
5758c2ecf20Sopenharmony_ci		return -ENOMEM;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	vec = &iov->iov[0];
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	if (copy_from_user(vec, local_vec, args->nr_local *
5808c2ecf20Sopenharmony_ci			   sizeof(struct rds_iovec)))
5818c2ecf20Sopenharmony_ci		return -EFAULT;
5828c2ecf20Sopenharmony_ci	iov->len = args->nr_local;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	/* figure out the number of pages in the vector */
5858c2ecf20Sopenharmony_ci	for (i = 0; i < args->nr_local; i++, vec++) {
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci		nr_pages = rds_pages_in_vec(vec);
5888c2ecf20Sopenharmony_ci		if (nr_pages == 0)
5898c2ecf20Sopenharmony_ci			return -EINVAL;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci		tot_pages += nr_pages;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		/*
5948c2ecf20Sopenharmony_ci		 * nr_pages for one entry is limited to (UINT_MAX>>PAGE_SHIFT)+1,
5958c2ecf20Sopenharmony_ci		 * so tot_pages cannot overflow without first going negative.
5968c2ecf20Sopenharmony_ci		 */
5978c2ecf20Sopenharmony_ci		if (tot_pages < 0)
5988c2ecf20Sopenharmony_ci			return -EINVAL;
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	return tot_pages * sizeof(struct scatterlist);
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci/*
6058c2ecf20Sopenharmony_ci * The application asks for a RDMA transfer.
6068c2ecf20Sopenharmony_ci * Extract all arguments and set up the rdma_op
6078c2ecf20Sopenharmony_ci */
6088c2ecf20Sopenharmony_ciint rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
6098c2ecf20Sopenharmony_ci		       struct cmsghdr *cmsg,
6108c2ecf20Sopenharmony_ci		       struct rds_iov_vector *vec)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct rds_rdma_args *args;
6138c2ecf20Sopenharmony_ci	struct rm_rdma_op *op = &rm->rdma;
6148c2ecf20Sopenharmony_ci	int nr_pages;
6158c2ecf20Sopenharmony_ci	unsigned int nr_bytes;
6168c2ecf20Sopenharmony_ci	struct page **pages = NULL;
6178c2ecf20Sopenharmony_ci	struct rds_iovec *iovs;
6188c2ecf20Sopenharmony_ci	unsigned int i, j;
6198c2ecf20Sopenharmony_ci	int ret = 0;
6208c2ecf20Sopenharmony_ci	bool odp_supported = true;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args))
6238c2ecf20Sopenharmony_ci	    || rm->rdma.op_active)
6248c2ecf20Sopenharmony_ci		return -EINVAL;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	args = CMSG_DATA(cmsg);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (ipv6_addr_any(&rs->rs_bound_addr)) {
6298c2ecf20Sopenharmony_ci		ret = -ENOTCONN; /* XXX not a great errno */
6308c2ecf20Sopenharmony_ci		goto out_ret;
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	if (args->nr_local > UIO_MAXIOV) {
6348c2ecf20Sopenharmony_ci		ret = -EMSGSIZE;
6358c2ecf20Sopenharmony_ci		goto out_ret;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (vec->len != args->nr_local) {
6398c2ecf20Sopenharmony_ci		ret = -EINVAL;
6408c2ecf20Sopenharmony_ci		goto out_ret;
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci	/* odp-mr is not supported for multiple requests within one message */
6438c2ecf20Sopenharmony_ci	if (args->nr_local != 1)
6448c2ecf20Sopenharmony_ci		odp_supported = false;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	iovs = vec->iov;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	nr_pages = rds_rdma_pages(iovs, args->nr_local);
6498c2ecf20Sopenharmony_ci	if (nr_pages < 0) {
6508c2ecf20Sopenharmony_ci		ret = -EINVAL;
6518c2ecf20Sopenharmony_ci		goto out_ret;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
6558c2ecf20Sopenharmony_ci	if (!pages) {
6568c2ecf20Sopenharmony_ci		ret = -ENOMEM;
6578c2ecf20Sopenharmony_ci		goto out_ret;
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	op->op_write = !!(args->flags & RDS_RDMA_READWRITE);
6618c2ecf20Sopenharmony_ci	op->op_fence = !!(args->flags & RDS_RDMA_FENCE);
6628c2ecf20Sopenharmony_ci	op->op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
6638c2ecf20Sopenharmony_ci	op->op_silent = !!(args->flags & RDS_RDMA_SILENT);
6648c2ecf20Sopenharmony_ci	op->op_active = 1;
6658c2ecf20Sopenharmony_ci	op->op_recverr = rs->rs_recverr;
6668c2ecf20Sopenharmony_ci	op->op_odp_mr = NULL;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	WARN_ON(!nr_pages);
6698c2ecf20Sopenharmony_ci	op->op_sg = rds_message_alloc_sgs(rm, nr_pages);
6708c2ecf20Sopenharmony_ci	if (IS_ERR(op->op_sg)) {
6718c2ecf20Sopenharmony_ci		ret = PTR_ERR(op->op_sg);
6728c2ecf20Sopenharmony_ci		goto out_pages;
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	if (op->op_notify || op->op_recverr) {
6768c2ecf20Sopenharmony_ci		/* We allocate an uninitialized notifier here, because
6778c2ecf20Sopenharmony_ci		 * we don't want to do that in the completion handler. We
6788c2ecf20Sopenharmony_ci		 * would have to use GFP_ATOMIC there, and don't want to deal
6798c2ecf20Sopenharmony_ci		 * with failed allocations.
6808c2ecf20Sopenharmony_ci		 */
6818c2ecf20Sopenharmony_ci		op->op_notifier = kmalloc(sizeof(struct rds_notifier), GFP_KERNEL);
6828c2ecf20Sopenharmony_ci		if (!op->op_notifier) {
6838c2ecf20Sopenharmony_ci			ret = -ENOMEM;
6848c2ecf20Sopenharmony_ci			goto out_pages;
6858c2ecf20Sopenharmony_ci		}
6868c2ecf20Sopenharmony_ci		op->op_notifier->n_user_token = args->user_token;
6878c2ecf20Sopenharmony_ci		op->op_notifier->n_status = RDS_RDMA_SUCCESS;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/* The cookie contains the R_Key of the remote memory region, and
6918c2ecf20Sopenharmony_ci	 * optionally an offset into it. This is how we implement RDMA into
6928c2ecf20Sopenharmony_ci	 * unaligned memory.
6938c2ecf20Sopenharmony_ci	 * When setting up the RDMA, we need to add that offset to the
6948c2ecf20Sopenharmony_ci	 * destination address (which is really an offset into the MR)
6958c2ecf20Sopenharmony_ci	 * FIXME: We may want to move this into ib_rdma.c
6968c2ecf20Sopenharmony_ci	 */
6978c2ecf20Sopenharmony_ci	op->op_rkey = rds_rdma_cookie_key(args->cookie);
6988c2ecf20Sopenharmony_ci	op->op_remote_addr = args->remote_vec.addr + rds_rdma_cookie_offset(args->cookie);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	nr_bytes = 0;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n",
7038c2ecf20Sopenharmony_ci	       (unsigned long long)args->nr_local,
7048c2ecf20Sopenharmony_ci	       (unsigned long long)args->remote_vec.addr,
7058c2ecf20Sopenharmony_ci	       op->op_rkey);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	for (i = 0; i < args->nr_local; i++) {
7088c2ecf20Sopenharmony_ci		struct rds_iovec *iov = &iovs[i];
7098c2ecf20Sopenharmony_ci		/* don't need to check, rds_rdma_pages() verified nr will be +nonzero */
7108c2ecf20Sopenharmony_ci		unsigned int nr = rds_pages_in_vec(iov);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci		rs->rs_user_addr = iov->addr;
7138c2ecf20Sopenharmony_ci		rs->rs_user_bytes = iov->bytes;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci		/* If it's a WRITE operation, we want to pin the pages for reading.
7168c2ecf20Sopenharmony_ci		 * If it's a READ operation, we need to pin the pages for writing.
7178c2ecf20Sopenharmony_ci		 */
7188c2ecf20Sopenharmony_ci		ret = rds_pin_pages(iov->addr, nr, pages, !op->op_write);
7198c2ecf20Sopenharmony_ci		if ((!odp_supported && ret <= 0) ||
7208c2ecf20Sopenharmony_ci		    (odp_supported && ret <= 0 && ret != -EOPNOTSUPP))
7218c2ecf20Sopenharmony_ci			goto out_pages;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci		if (ret == -EOPNOTSUPP) {
7248c2ecf20Sopenharmony_ci			struct rds_mr *local_odp_mr;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci			if (!rs->rs_transport->get_mr) {
7278c2ecf20Sopenharmony_ci				ret = -EOPNOTSUPP;
7288c2ecf20Sopenharmony_ci				goto out_pages;
7298c2ecf20Sopenharmony_ci			}
7308c2ecf20Sopenharmony_ci			local_odp_mr =
7318c2ecf20Sopenharmony_ci				kzalloc(sizeof(*local_odp_mr), GFP_KERNEL);
7328c2ecf20Sopenharmony_ci			if (!local_odp_mr) {
7338c2ecf20Sopenharmony_ci				ret = -ENOMEM;
7348c2ecf20Sopenharmony_ci				goto out_pages;
7358c2ecf20Sopenharmony_ci			}
7368c2ecf20Sopenharmony_ci			RB_CLEAR_NODE(&local_odp_mr->r_rb_node);
7378c2ecf20Sopenharmony_ci			kref_init(&local_odp_mr->r_kref);
7388c2ecf20Sopenharmony_ci			local_odp_mr->r_trans = rs->rs_transport;
7398c2ecf20Sopenharmony_ci			local_odp_mr->r_sock = rs;
7408c2ecf20Sopenharmony_ci			local_odp_mr->r_trans_private =
7418c2ecf20Sopenharmony_ci				rs->rs_transport->get_mr(
7428c2ecf20Sopenharmony_ci					NULL, 0, rs, &local_odp_mr->r_key, NULL,
7438c2ecf20Sopenharmony_ci					iov->addr, iov->bytes, ODP_VIRTUAL);
7448c2ecf20Sopenharmony_ci			if (IS_ERR(local_odp_mr->r_trans_private)) {
7458c2ecf20Sopenharmony_ci				ret = IS_ERR(local_odp_mr->r_trans_private);
7468c2ecf20Sopenharmony_ci				rdsdebug("get_mr ret %d %p\"", ret,
7478c2ecf20Sopenharmony_ci					 local_odp_mr->r_trans_private);
7488c2ecf20Sopenharmony_ci				kfree(local_odp_mr);
7498c2ecf20Sopenharmony_ci				ret = -EOPNOTSUPP;
7508c2ecf20Sopenharmony_ci				goto out_pages;
7518c2ecf20Sopenharmony_ci			}
7528c2ecf20Sopenharmony_ci			rdsdebug("Need odp; local_odp_mr %p trans_private %p\n",
7538c2ecf20Sopenharmony_ci				 local_odp_mr, local_odp_mr->r_trans_private);
7548c2ecf20Sopenharmony_ci			op->op_odp_mr = local_odp_mr;
7558c2ecf20Sopenharmony_ci			op->op_odp_addr = iov->addr;
7568c2ecf20Sopenharmony_ci		}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		rdsdebug("RDS: nr_bytes %u nr %u iov->bytes %llu iov->addr %llx\n",
7598c2ecf20Sopenharmony_ci			 nr_bytes, nr, iov->bytes, iov->addr);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci		nr_bytes += iov->bytes;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		for (j = 0; j < nr; j++) {
7648c2ecf20Sopenharmony_ci			unsigned int offset = iov->addr & ~PAGE_MASK;
7658c2ecf20Sopenharmony_ci			struct scatterlist *sg;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci			sg = &op->op_sg[op->op_nents + j];
7688c2ecf20Sopenharmony_ci			sg_set_page(sg, pages[j],
7698c2ecf20Sopenharmony_ci					min_t(unsigned int, iov->bytes, PAGE_SIZE - offset),
7708c2ecf20Sopenharmony_ci					offset);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci			sg_dma_len(sg) = sg->length;
7738c2ecf20Sopenharmony_ci			rdsdebug("RDS: sg->offset %x sg->len %x iov->addr %llx iov->bytes %llu\n",
7748c2ecf20Sopenharmony_ci			       sg->offset, sg->length, iov->addr, iov->bytes);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci			iov->addr += sg->length;
7778c2ecf20Sopenharmony_ci			iov->bytes -= sg->length;
7788c2ecf20Sopenharmony_ci		}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci		op->op_nents += nr;
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (nr_bytes > args->remote_vec.bytes) {
7848c2ecf20Sopenharmony_ci		rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n",
7858c2ecf20Sopenharmony_ci				nr_bytes,
7868c2ecf20Sopenharmony_ci				(unsigned int) args->remote_vec.bytes);
7878c2ecf20Sopenharmony_ci		ret = -EINVAL;
7888c2ecf20Sopenharmony_ci		goto out_pages;
7898c2ecf20Sopenharmony_ci	}
7908c2ecf20Sopenharmony_ci	op->op_bytes = nr_bytes;
7918c2ecf20Sopenharmony_ci	ret = 0;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ciout_pages:
7948c2ecf20Sopenharmony_ci	kfree(pages);
7958c2ecf20Sopenharmony_ciout_ret:
7968c2ecf20Sopenharmony_ci	if (ret)
7978c2ecf20Sopenharmony_ci		rds_rdma_free_op(op);
7988c2ecf20Sopenharmony_ci	else
7998c2ecf20Sopenharmony_ci		rds_stats_inc(s_send_rdma);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	return ret;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci/*
8058c2ecf20Sopenharmony_ci * The application wants us to pass an RDMA destination (aka MR)
8068c2ecf20Sopenharmony_ci * to the remote
8078c2ecf20Sopenharmony_ci */
8088c2ecf20Sopenharmony_ciint rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
8098c2ecf20Sopenharmony_ci			  struct cmsghdr *cmsg)
8108c2ecf20Sopenharmony_ci{
8118c2ecf20Sopenharmony_ci	unsigned long flags;
8128c2ecf20Sopenharmony_ci	struct rds_mr *mr;
8138c2ecf20Sopenharmony_ci	u32 r_key;
8148c2ecf20Sopenharmony_ci	int err = 0;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	if (cmsg->cmsg_len < CMSG_LEN(sizeof(rds_rdma_cookie_t)) ||
8178c2ecf20Sopenharmony_ci	    rm->m_rdma_cookie != 0)
8188c2ecf20Sopenharmony_ci		return -EINVAL;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	memcpy(&rm->m_rdma_cookie, CMSG_DATA(cmsg), sizeof(rm->m_rdma_cookie));
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	/* We are reusing a previously mapped MR here. Most likely, the
8238c2ecf20Sopenharmony_ci	 * application has written to the buffer, so we need to explicitly
8248c2ecf20Sopenharmony_ci	 * flush those writes to RAM. Otherwise the HCA may not see them
8258c2ecf20Sopenharmony_ci	 * when doing a DMA from that buffer.
8268c2ecf20Sopenharmony_ci	 */
8278c2ecf20Sopenharmony_ci	r_key = rds_rdma_cookie_key(rm->m_rdma_cookie);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rs->rs_rdma_lock, flags);
8308c2ecf20Sopenharmony_ci	mr = rds_mr_tree_walk(&rs->rs_rdma_keys, r_key, NULL);
8318c2ecf20Sopenharmony_ci	if (!mr)
8328c2ecf20Sopenharmony_ci		err = -EINVAL;	/* invalid r_key */
8338c2ecf20Sopenharmony_ci	else
8348c2ecf20Sopenharmony_ci		kref_get(&mr->r_kref);
8358c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rs->rs_rdma_lock, flags);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	if (mr) {
8388c2ecf20Sopenharmony_ci		mr->r_trans->sync_mr(mr->r_trans_private,
8398c2ecf20Sopenharmony_ci				     DMA_TO_DEVICE);
8408c2ecf20Sopenharmony_ci		rm->rdma.op_rdma_mr = mr;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci	return err;
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci/*
8468c2ecf20Sopenharmony_ci * The application passes us an address range it wants to enable RDMA
8478c2ecf20Sopenharmony_ci * to/from. We map the area, and save the <R_Key,offset> pair
8488c2ecf20Sopenharmony_ci * in rm->m_rdma_cookie. This causes it to be sent along to the peer
8498c2ecf20Sopenharmony_ci * in an extension header.
8508c2ecf20Sopenharmony_ci */
8518c2ecf20Sopenharmony_ciint rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
8528c2ecf20Sopenharmony_ci			  struct cmsghdr *cmsg)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_get_mr_args)) ||
8558c2ecf20Sopenharmony_ci	    rm->m_rdma_cookie != 0)
8568c2ecf20Sopenharmony_ci		return -EINVAL;
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	return __rds_rdma_map(rs, CMSG_DATA(cmsg), &rm->m_rdma_cookie,
8598c2ecf20Sopenharmony_ci			      &rm->rdma.op_rdma_mr, rm->m_conn_path);
8608c2ecf20Sopenharmony_ci}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci/*
8638c2ecf20Sopenharmony_ci * Fill in rds_message for an atomic request.
8648c2ecf20Sopenharmony_ci */
8658c2ecf20Sopenharmony_ciint rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
8668c2ecf20Sopenharmony_ci		    struct cmsghdr *cmsg)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	struct page *page = NULL;
8698c2ecf20Sopenharmony_ci	struct rds_atomic_args *args;
8708c2ecf20Sopenharmony_ci	int ret = 0;
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_atomic_args))
8738c2ecf20Sopenharmony_ci	 || rm->atomic.op_active)
8748c2ecf20Sopenharmony_ci		return -EINVAL;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	args = CMSG_DATA(cmsg);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	/* Nonmasked & masked cmsg ops converted to masked hw ops */
8798c2ecf20Sopenharmony_ci	switch (cmsg->cmsg_type) {
8808c2ecf20Sopenharmony_ci	case RDS_CMSG_ATOMIC_FADD:
8818c2ecf20Sopenharmony_ci		rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
8828c2ecf20Sopenharmony_ci		rm->atomic.op_m_fadd.add = args->fadd.add;
8838c2ecf20Sopenharmony_ci		rm->atomic.op_m_fadd.nocarry_mask = 0;
8848c2ecf20Sopenharmony_ci		break;
8858c2ecf20Sopenharmony_ci	case RDS_CMSG_MASKED_ATOMIC_FADD:
8868c2ecf20Sopenharmony_ci		rm->atomic.op_type = RDS_ATOMIC_TYPE_FADD;
8878c2ecf20Sopenharmony_ci		rm->atomic.op_m_fadd.add = args->m_fadd.add;
8888c2ecf20Sopenharmony_ci		rm->atomic.op_m_fadd.nocarry_mask = args->m_fadd.nocarry_mask;
8898c2ecf20Sopenharmony_ci		break;
8908c2ecf20Sopenharmony_ci	case RDS_CMSG_ATOMIC_CSWP:
8918c2ecf20Sopenharmony_ci		rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
8928c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.compare = args->cswp.compare;
8938c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.swap = args->cswp.swap;
8948c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.compare_mask = ~0;
8958c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.swap_mask = ~0;
8968c2ecf20Sopenharmony_ci		break;
8978c2ecf20Sopenharmony_ci	case RDS_CMSG_MASKED_ATOMIC_CSWP:
8988c2ecf20Sopenharmony_ci		rm->atomic.op_type = RDS_ATOMIC_TYPE_CSWP;
8998c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.compare = args->m_cswp.compare;
9008c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.swap = args->m_cswp.swap;
9018c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.compare_mask = args->m_cswp.compare_mask;
9028c2ecf20Sopenharmony_ci		rm->atomic.op_m_cswp.swap_mask = args->m_cswp.swap_mask;
9038c2ecf20Sopenharmony_ci		break;
9048c2ecf20Sopenharmony_ci	default:
9058c2ecf20Sopenharmony_ci		BUG(); /* should never happen */
9068c2ecf20Sopenharmony_ci	}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	rm->atomic.op_notify = !!(args->flags & RDS_RDMA_NOTIFY_ME);
9098c2ecf20Sopenharmony_ci	rm->atomic.op_silent = !!(args->flags & RDS_RDMA_SILENT);
9108c2ecf20Sopenharmony_ci	rm->atomic.op_active = 1;
9118c2ecf20Sopenharmony_ci	rm->atomic.op_recverr = rs->rs_recverr;
9128c2ecf20Sopenharmony_ci	rm->atomic.op_sg = rds_message_alloc_sgs(rm, 1);
9138c2ecf20Sopenharmony_ci	if (IS_ERR(rm->atomic.op_sg)) {
9148c2ecf20Sopenharmony_ci		ret = PTR_ERR(rm->atomic.op_sg);
9158c2ecf20Sopenharmony_ci		goto err;
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	/* verify 8 byte-aligned */
9198c2ecf20Sopenharmony_ci	if (args->local_addr & 0x7) {
9208c2ecf20Sopenharmony_ci		ret = -EFAULT;
9218c2ecf20Sopenharmony_ci		goto err;
9228c2ecf20Sopenharmony_ci	}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	ret = rds_pin_pages(args->local_addr, 1, &page, 1);
9258c2ecf20Sopenharmony_ci	if (ret != 1)
9268c2ecf20Sopenharmony_ci		goto err;
9278c2ecf20Sopenharmony_ci	ret = 0;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	sg_set_page(rm->atomic.op_sg, page, 8, offset_in_page(args->local_addr));
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	if (rm->atomic.op_notify || rm->atomic.op_recverr) {
9328c2ecf20Sopenharmony_ci		/* We allocate an uninitialized notifier here, because
9338c2ecf20Sopenharmony_ci		 * we don't want to do that in the completion handler. We
9348c2ecf20Sopenharmony_ci		 * would have to use GFP_ATOMIC there, and don't want to deal
9358c2ecf20Sopenharmony_ci		 * with failed allocations.
9368c2ecf20Sopenharmony_ci		 */
9378c2ecf20Sopenharmony_ci		rm->atomic.op_notifier = kmalloc(sizeof(*rm->atomic.op_notifier), GFP_KERNEL);
9388c2ecf20Sopenharmony_ci		if (!rm->atomic.op_notifier) {
9398c2ecf20Sopenharmony_ci			ret = -ENOMEM;
9408c2ecf20Sopenharmony_ci			goto err;
9418c2ecf20Sopenharmony_ci		}
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci		rm->atomic.op_notifier->n_user_token = args->user_token;
9448c2ecf20Sopenharmony_ci		rm->atomic.op_notifier->n_status = RDS_RDMA_SUCCESS;
9458c2ecf20Sopenharmony_ci	}
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	rm->atomic.op_rkey = rds_rdma_cookie_key(args->cookie);
9488c2ecf20Sopenharmony_ci	rm->atomic.op_remote_addr = args->remote_addr + rds_rdma_cookie_offset(args->cookie);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	return ret;
9518c2ecf20Sopenharmony_cierr:
9528c2ecf20Sopenharmony_ci	if (page)
9538c2ecf20Sopenharmony_ci		unpin_user_page(page);
9548c2ecf20Sopenharmony_ci	rm->atomic.op_active = 0;
9558c2ecf20Sopenharmony_ci	kfree(rm->atomic.op_notifier);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	return ret;
9588c2ecf20Sopenharmony_ci}
959