]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/rds/rdma.c
2 * Copyright (c) 2007 Oracle. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/pagemap.h>
34 #include <linux/rbtree.h>
35 #include <linux/dma-mapping.h> /* for DMA_*_DEVICE */
42 * - should we limit the size of a mr region? let transport return failure?
43 * - should we detect duplicate keys on a socket? hmm.
44 * - an rdma is an mlock, apply rlimit?
48 * get the number of pages by looking at the page indices that the start and
49 * end addresses fall in.
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'.
55 static unsigned int rds_pages_in_vec(struct rds_iovec
*vec
)
57 if ((vec
->addr
+ vec
->bytes
<= vec
->addr
) ||
58 (vec
->bytes
> (u64
)UINT_MAX
))
61 return ((vec
->addr
+ vec
->bytes
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
) -
62 (vec
->addr
>> PAGE_SHIFT
);
65 static struct rds_mr
*rds_mr_tree_walk(struct rb_root
*root
, u64 key
,
66 struct rds_mr
*insert
)
68 struct rb_node
**p
= &root
->rb_node
;
69 struct rb_node
*parent
= NULL
;
74 mr
= rb_entry(parent
, struct rds_mr
, r_rb_node
);
78 else if (key
> mr
->r_key
)
85 rb_link_node(&insert
->r_rb_node
, parent
, p
);
86 rb_insert_color(&insert
->r_rb_node
, root
);
87 atomic_inc(&insert
->r_refcount
);
93 * Destroy the transport-specific part of a MR.
95 static void rds_destroy_mr(struct rds_mr
*mr
)
97 struct rds_sock
*rs
= mr
->r_sock
;
98 void *trans_private
= NULL
;
101 rdsdebug("RDS: destroy mr key is %x refcnt %u\n",
102 mr
->r_key
, atomic_read(&mr
->r_refcount
));
104 if (test_and_set_bit(RDS_MR_DEAD
, &mr
->r_state
))
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
);
115 mr
->r_trans
->free_mr(trans_private
, mr
->r_invalidate
);
118 void __rds_put_mr_final(struct rds_mr
*mr
)
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.
128 void rds_rdma_drop_keys(struct rds_sock
*rs
)
131 struct rb_node
*node
;
133 /* Release any MRs associated with this socket */
134 while ((node
= rb_first(&rs
->rs_rdma_keys
))) {
135 mr
= container_of(node
, struct rds_mr
, r_rb_node
);
136 if (mr
->r_trans
== rs
->rs_transport
)
137 mr
->r_invalidate
= 0;
141 if (rs
->rs_transport
&& rs
->rs_transport
->flush_mrs
)
142 rs
->rs_transport
->flush_mrs();
146 * Helper function to pin user pages.
148 static int rds_pin_pages(unsigned long user_addr
, unsigned int nr_pages
,
149 struct page
**pages
, int write
)
153 down_read(¤t
->mm
->mmap_sem
);
154 ret
= get_user_pages(current
, current
->mm
, user_addr
,
155 nr_pages
, write
, 0, pages
, NULL
);
156 up_read(¤t
->mm
->mmap_sem
);
158 if (0 <= ret
&& (unsigned) ret
< nr_pages
) {
160 put_page(pages
[ret
]);
167 static int __rds_rdma_map(struct rds_sock
*rs
, struct rds_get_mr_args
*args
,
168 u64
*cookie_ret
, struct rds_mr
**mr_ret
)
170 struct rds_mr
*mr
= NULL
, *found
;
171 unsigned int nr_pages
;
172 struct page
**pages
= NULL
;
173 struct scatterlist
*sg
;
176 rds_rdma_cookie_t cookie
;
181 if (rs
->rs_bound_addr
== 0) {
182 ret
= -ENOTCONN
; /* XXX not a great errno */
186 if (rs
->rs_transport
->get_mr
== NULL
) {
191 nr_pages
= rds_pages_in_vec(&args
->vec
);
197 rdsdebug("RDS: get_mr addr %llx len %llu nr_pages %u\n",
198 args
->vec
.addr
, args
->vec
.bytes
, nr_pages
);
200 /* XXX clamp nr_pages to limit the size of this alloc? */
201 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_KERNEL
);
207 mr
= kzalloc(sizeof(struct rds_mr
), GFP_KERNEL
);
213 atomic_set(&mr
->r_refcount
, 1);
214 RB_CLEAR_NODE(&mr
->r_rb_node
);
215 mr
->r_trans
= rs
->rs_transport
;
218 if (args
->flags
& RDS_RDMA_USE_ONCE
)
220 if (args
->flags
& RDS_RDMA_INVALIDATE
)
221 mr
->r_invalidate
= 1;
222 if (args
->flags
& RDS_RDMA_READWRITE
)
226 * Pin the pages that make up the user buffer and transfer the page
227 * pointers to the mr's sg array. We check to see if we've mapped
228 * the whole region after transferring the partial page references
229 * to the sg array so that we can have one page ref cleanup path.
231 * For now we have no flag that tells us whether the mapping is
232 * r/o or r/w. We need to assume r/w, or we'll do a lot of RDMA to
235 ret
= rds_pin_pages(args
->vec
.addr
& PAGE_MASK
, nr_pages
, pages
, 1);
240 sg
= kcalloc(nents
, sizeof(*sg
), GFP_KERNEL
);
246 sg_init_table(sg
, nents
);
248 /* Stick all pages into the scatterlist */
249 for (i
= 0 ; i
< nents
; i
++)
250 sg_set_page(&sg
[i
], pages
[i
], PAGE_SIZE
, 0);
252 rdsdebug("RDS: trans_private nents is %u\n", nents
);
254 /* Obtain a transport specific MR. If this succeeds, the
255 * s/g list is now owned by the MR.
256 * Note that dma_map() implies that pending writes are
257 * flushed to RAM, so no dma_sync is needed here. */
258 trans_private
= rs
->rs_transport
->get_mr(sg
, nents
, rs
,
261 if (IS_ERR(trans_private
)) {
262 for (i
= 0 ; i
< nents
; i
++)
263 put_page(sg_page(&sg
[i
]));
265 ret
= PTR_ERR(trans_private
);
269 mr
->r_trans_private
= trans_private
;
271 rdsdebug("RDS: get_mr put_user key is %x cookie_addr %p\n",
272 mr
->r_key
, (void *)(unsigned long) args
->cookie_addr
);
274 /* The user may pass us an unaligned address, but we can only
275 * map page aligned regions. So we keep the offset, and build
276 * a 64bit cookie containing <R_Key, offset> and pass that
278 cookie
= rds_rdma_make_cookie(mr
->r_key
, args
->vec
.addr
& ~PAGE_MASK
);
280 *cookie_ret
= cookie
;
282 if (args
->cookie_addr
&& put_user(cookie
, (u64 __user
*)(unsigned long) args
->cookie_addr
)) {
287 /* Inserting the new MR into the rbtree bumps its
288 * reference count. */
289 spin_lock_irqsave(&rs
->rs_rdma_lock
, flags
);
290 found
= rds_mr_tree_walk(&rs
->rs_rdma_keys
, mr
->r_key
, mr
);
291 spin_unlock_irqrestore(&rs
->rs_rdma_lock
, flags
);
293 BUG_ON(found
&& found
!= mr
);
295 rdsdebug("RDS: get_mr key is %x\n", mr
->r_key
);
297 atomic_inc(&mr
->r_refcount
);
309 int rds_get_mr(struct rds_sock
*rs
, char __user
*optval
, int optlen
)
311 struct rds_get_mr_args args
;
313 if (optlen
!= sizeof(struct rds_get_mr_args
))
316 if (copy_from_user(&args
, (struct rds_get_mr_args __user
*)optval
,
317 sizeof(struct rds_get_mr_args
)))
320 return __rds_rdma_map(rs
, &args
, NULL
, NULL
);
324 * Free the MR indicated by the given R_Key
326 int rds_free_mr(struct rds_sock
*rs
, char __user
*optval
, int optlen
)
328 struct rds_free_mr_args args
;
332 if (optlen
!= sizeof(struct rds_free_mr_args
))
335 if (copy_from_user(&args
, (struct rds_free_mr_args __user
*)optval
,
336 sizeof(struct rds_free_mr_args
)))
339 /* Special case - a null cookie means flush all unused MRs */
340 if (args
.cookie
== 0) {
341 if (!rs
->rs_transport
|| !rs
->rs_transport
->flush_mrs
)
343 rs
->rs_transport
->flush_mrs();
347 /* Look up the MR given its R_key and remove it from the rbtree
348 * so nobody else finds it.
349 * This should also prevent races with rds_rdma_unuse.
351 spin_lock_irqsave(&rs
->rs_rdma_lock
, flags
);
352 mr
= rds_mr_tree_walk(&rs
->rs_rdma_keys
, rds_rdma_cookie_key(args
.cookie
), NULL
);
354 rb_erase(&mr
->r_rb_node
, &rs
->rs_rdma_keys
);
355 RB_CLEAR_NODE(&mr
->r_rb_node
);
356 if (args
.flags
& RDS_RDMA_INVALIDATE
)
357 mr
->r_invalidate
= 1;
359 spin_unlock_irqrestore(&rs
->rs_rdma_lock
, flags
);
365 * call rds_destroy_mr() ourselves so that we're sure it's done by the time
366 * we return. If we let rds_mr_put() do it it might not happen until
367 * someone else drops their ref.
375 * This is called when we receive an extension header that
376 * tells us this MR was used. It allows us to implement
379 void rds_rdma_unuse(struct rds_sock
*rs
, u32 r_key
, int force
)
385 spin_lock_irqsave(&rs
->rs_rdma_lock
, flags
);
386 mr
= rds_mr_tree_walk(&rs
->rs_rdma_keys
, r_key
, NULL
);
387 if (mr
&& (mr
->r_use_once
|| force
)) {
388 rb_erase(&mr
->r_rb_node
, &rs
->rs_rdma_keys
);
389 RB_CLEAR_NODE(&mr
->r_rb_node
);
392 atomic_inc(&mr
->r_refcount
);
393 spin_unlock_irqrestore(&rs
->rs_rdma_lock
, flags
);
395 /* May have to issue a dma_sync on this memory region.
396 * Note we could avoid this if the operation was a RDMA READ,
397 * but at this point we can't tell. */
399 if (mr
->r_trans
->sync_mr
)
400 mr
->r_trans
->sync_mr(mr
->r_trans_private
, DMA_FROM_DEVICE
);
402 /* If the MR was marked as invalidate, this will
403 * trigger an async flush. */
410 void rds_rdma_free_op(struct rds_rdma_op
*ro
)
414 for (i
= 0; i
< ro
->r_nents
; i
++) {
415 struct page
*page
= sg_page(&ro
->r_sg
[i
]);
417 /* Mark page dirty if it was possibly modified, which
418 * is the case for a RDMA_READ which copies from remote
421 set_page_dirty(page
);
425 kfree(ro
->r_notifier
);
430 * args is a pointer to an in-kernel copy in the sendmsg cmsg.
432 static struct rds_rdma_op
*rds_rdma_prepare(struct rds_sock
*rs
,
433 struct rds_rdma_args
*args
)
435 struct rds_iovec vec
;
436 struct rds_rdma_op
*op
= NULL
;
437 unsigned int nr_pages
;
438 unsigned int max_pages
;
439 unsigned int nr_bytes
;
440 struct page
**pages
= NULL
;
441 struct rds_iovec __user
*local_vec
;
442 struct scatterlist
*sg
;
448 if (rs
->rs_bound_addr
== 0) {
449 ret
= -ENOTCONN
; /* XXX not a great errno */
453 if (args
->nr_local
> (u64
)UINT_MAX
) {
461 local_vec
= (struct rds_iovec __user
*)(unsigned long) args
->local_vec_addr
;
463 /* figure out the number of pages in the vector */
464 for (i
= 0; i
< args
->nr_local
; i
++) {
465 if (copy_from_user(&vec
, &local_vec
[i
],
466 sizeof(struct rds_iovec
))) {
471 nr
= rds_pages_in_vec(&vec
);
477 max_pages
= max(nr
, max_pages
);
481 pages
= kcalloc(max_pages
, sizeof(struct page
*), GFP_KERNEL
);
487 op
= kzalloc(offsetof(struct rds_rdma_op
, r_sg
[nr_pages
]), GFP_KERNEL
);
493 op
->r_write
= !!(args
->flags
& RDS_RDMA_READWRITE
);
494 op
->r_fence
= !!(args
->flags
& RDS_RDMA_FENCE
);
495 op
->r_notify
= !!(args
->flags
& RDS_RDMA_NOTIFY_ME
);
496 op
->r_recverr
= rs
->rs_recverr
;
498 sg_init_table(op
->r_sg
, nr_pages
);
500 if (op
->r_notify
|| op
->r_recverr
) {
501 /* We allocate an uninitialized notifier here, because
502 * we don't want to do that in the completion handler. We
503 * would have to use GFP_ATOMIC there, and don't want to deal
504 * with failed allocations.
506 op
->r_notifier
= kmalloc(sizeof(struct rds_notifier
), GFP_KERNEL
);
507 if (!op
->r_notifier
) {
511 op
->r_notifier
->n_user_token
= args
->user_token
;
512 op
->r_notifier
->n_status
= RDS_RDMA_SUCCESS
;
515 /* The cookie contains the R_Key of the remote memory region, and
516 * optionally an offset into it. This is how we implement RDMA into
518 * When setting up the RDMA, we need to add that offset to the
519 * destination address (which is really an offset into the MR)
520 * FIXME: We may want to move this into ib_rdma.c
522 op
->r_key
= rds_rdma_cookie_key(args
->cookie
);
523 op
->r_remote_addr
= args
->remote_vec
.addr
+ rds_rdma_cookie_offset(args
->cookie
);
527 rdsdebug("RDS: rdma prepare nr_local %llu rva %llx rkey %x\n",
528 (unsigned long long)args
->nr_local
,
529 (unsigned long long)args
->remote_vec
.addr
,
532 for (i
= 0; i
< args
->nr_local
; i
++) {
533 if (copy_from_user(&vec
, &local_vec
[i
],
534 sizeof(struct rds_iovec
))) {
539 nr
= rds_pages_in_vec(&vec
);
545 rs
->rs_user_addr
= vec
.addr
;
546 rs
->rs_user_bytes
= vec
.bytes
;
548 /* did the user change the vec under us? */
549 if (nr
> max_pages
|| op
->r_nents
+ nr
> nr_pages
) {
553 /* If it's a WRITE operation, we want to pin the pages for reading.
554 * If it's a READ operation, we need to pin the pages for writing.
556 ret
= rds_pin_pages(vec
.addr
& PAGE_MASK
, nr
, pages
, !op
->r_write
);
560 rdsdebug("RDS: nr_bytes %u nr %u vec.bytes %llu vec.addr %llx\n",
561 nr_bytes
, nr
, vec
.bytes
, vec
.addr
);
563 nr_bytes
+= vec
.bytes
;
565 for (j
= 0; j
< nr
; j
++) {
566 unsigned int offset
= vec
.addr
& ~PAGE_MASK
;
568 sg
= &op
->r_sg
[op
->r_nents
+ j
];
569 sg_set_page(sg
, pages
[j
],
570 min_t(unsigned int, vec
.bytes
, PAGE_SIZE
- offset
),
573 rdsdebug("RDS: sg->offset %x sg->len %x vec.addr %llx vec.bytes %llu\n",
574 sg
->offset
, sg
->length
, vec
.addr
, vec
.bytes
);
576 vec
.addr
+= sg
->length
;
577 vec
.bytes
-= sg
->length
;
584 if (nr_bytes
> args
->remote_vec
.bytes
) {
585 rdsdebug("RDS nr_bytes %u remote_bytes %u do not match\n",
587 (unsigned int) args
->remote_vec
.bytes
);
591 op
->r_bytes
= nr_bytes
;
598 rds_rdma_free_op(op
);
605 * The application asks for a RDMA transfer.
606 * Extract all arguments and set up the rdma_op
608 int rds_cmsg_rdma_args(struct rds_sock
*rs
, struct rds_message
*rm
,
609 struct cmsghdr
*cmsg
)
611 struct rds_rdma_op
*op
;
613 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct rds_rdma_args
))
614 || rm
->m_rdma_op
!= NULL
)
617 op
= rds_rdma_prepare(rs
, CMSG_DATA(cmsg
));
620 rds_stats_inc(s_send_rdma
);
626 * The application wants us to pass an RDMA destination (aka MR)
629 int rds_cmsg_rdma_dest(struct rds_sock
*rs
, struct rds_message
*rm
,
630 struct cmsghdr
*cmsg
)
637 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(rds_rdma_cookie_t
))
638 || rm
->m_rdma_cookie
!= 0)
641 memcpy(&rm
->m_rdma_cookie
, CMSG_DATA(cmsg
), sizeof(rm
->m_rdma_cookie
));
643 /* We are reusing a previously mapped MR here. Most likely, the
644 * application has written to the buffer, so we need to explicitly
645 * flush those writes to RAM. Otherwise the HCA may not see them
646 * when doing a DMA from that buffer.
648 r_key
= rds_rdma_cookie_key(rm
->m_rdma_cookie
);
650 spin_lock_irqsave(&rs
->rs_rdma_lock
, flags
);
651 mr
= rds_mr_tree_walk(&rs
->rs_rdma_keys
, r_key
, NULL
);
653 err
= -EINVAL
; /* invalid r_key */
655 atomic_inc(&mr
->r_refcount
);
656 spin_unlock_irqrestore(&rs
->rs_rdma_lock
, flags
);
659 mr
->r_trans
->sync_mr(mr
->r_trans_private
, DMA_TO_DEVICE
);
666 * The application passes us an address range it wants to enable RDMA
667 * to/from. We map the area, and save the <R_Key,offset> pair
668 * in rm->m_rdma_cookie. This causes it to be sent along to the peer
669 * in an extension header.
671 int rds_cmsg_rdma_map(struct rds_sock
*rs
, struct rds_message
*rm
,
672 struct cmsghdr
*cmsg
)
674 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct rds_get_mr_args
))
675 || rm
->m_rdma_cookie
!= 0)
678 return __rds_rdma_map(rs
, CMSG_DATA(cmsg
), &rm
->m_rdma_cookie
, &rm
->m_rdma_mr
);