blob: 116cf87ccb898081e031e07347a32bd907b843c5 [file] [log] [blame]
Andy Grover7875e182009-02-24 15:30:26 +00001/*
2 * Copyright (c) 2006 Oracle. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040035#include <linux/export.h>
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080036#include <linux/skbuff.h>
37#include <linux/list.h>
38#include <linux/errqueue.h>
Andy Grover7875e182009-02-24 15:30:26 +000039
40#include "rds.h"
Andy Grover7875e182009-02-24 15:30:26 +000041
Andy Grover7875e182009-02-24 15:30:26 +000042static unsigned int rds_exthdr_size[__RDS_EXTHDR_MAX] = {
43[RDS_EXTHDR_NONE] = 0,
44[RDS_EXTHDR_VERSION] = sizeof(struct rds_ext_header_version),
45[RDS_EXTHDR_RDMA] = sizeof(struct rds_ext_header_rdma),
46[RDS_EXTHDR_RDMA_DEST] = sizeof(struct rds_ext_header_rdma_dest),
Sowmini Varadhan5916e2c2016-07-14 03:51:03 -070047[RDS_EXTHDR_NPATHS] = sizeof(u16),
Sowmini Varadhan905dd412016-11-16 13:29:49 -080048[RDS_EXTHDR_GEN_NUM] = sizeof(u32),
Andy Grover7875e182009-02-24 15:30:26 +000049};
50
51
52void rds_message_addref(struct rds_message *rm)
53{
Reshetova, Elena6c5a1c42017-07-04 15:53:18 +030054 rdsdebug("addref rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
55 refcount_inc(&rm->m_refcount);
Andy Grover7875e182009-02-24 15:30:26 +000056}
Andy Grover616b7572009-08-21 12:28:32 +000057EXPORT_SYMBOL_GPL(rds_message_addref);
Andy Grover7875e182009-02-24 15:30:26 +000058
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080059static inline bool skb_zcookie_add(struct sk_buff *skb, u32 cookie)
60{
Sowmini Varadhan401910d2018-02-27 09:52:43 -080061 struct rds_zcopy_cookies *ck = (struct rds_zcopy_cookies *)skb->cb;
62 int ncookies = ck->num;
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080063
Sowmini Varadhan401910d2018-02-27 09:52:43 -080064 if (ncookies == RDS_MAX_ZCOOKIES)
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080065 return false;
Sowmini Varadhan401910d2018-02-27 09:52:43 -080066 ck->cookies[ncookies] = cookie;
67 ck->num = ++ncookies;
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080068 return true;
69}
70
71static void rds_rm_zerocopy_callback(struct rds_sock *rs,
72 struct rds_znotifier *znotif)
73{
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080074 struct sk_buff *skb, *tail;
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080075 unsigned long flags;
76 struct sk_buff_head *q;
77 u32 cookie = znotif->z_cookie;
Sowmini Varadhan401910d2018-02-27 09:52:43 -080078 struct rds_zcopy_cookies *ck;
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080079
Sowmini Varadhan401910d2018-02-27 09:52:43 -080080 q = &rs->rs_zcookie_queue;
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080081 spin_lock_irqsave(&q->lock, flags);
82 tail = skb_peek_tail(q);
83
84 if (tail && skb_zcookie_add(tail, cookie)) {
85 spin_unlock_irqrestore(&q->lock, flags);
86 mm_unaccount_pinned_pages(&znotif->z_mmp);
87 consume_skb(rds_skb_from_znotifier(znotif));
Sowmini Varadhan401910d2018-02-27 09:52:43 -080088 /* caller invokes rds_wake_sk_sleep() */
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080089 return;
90 }
91
92 skb = rds_skb_from_znotifier(znotif);
Sowmini Varadhan401910d2018-02-27 09:52:43 -080093 ck = (struct rds_zcopy_cookies *)skb->cb;
94 memset(ck, 0, sizeof(*ck));
Sowmini Varadhan01883ed2018-02-15 10:49:35 -080095 WARN_ON(!skb_zcookie_add(skb, cookie));
96
97 __skb_queue_tail(q, skb);
98
99 spin_unlock_irqrestore(&q->lock, flags);
Sowmini Varadhan401910d2018-02-27 09:52:43 -0800100 /* caller invokes rds_wake_sk_sleep() */
Sowmini Varadhan01883ed2018-02-15 10:49:35 -0800101
102 mm_unaccount_pinned_pages(&znotif->z_mmp);
103}
104
Andy Grover7875e182009-02-24 15:30:26 +0000105/*
106 * This relies on dma_map_sg() not touching sg[].page during merging.
107 */
108static void rds_message_purge(struct rds_message *rm)
109{
Sowmini Varadhanea8994c2018-02-15 10:49:33 -0800110 unsigned long i, flags;
Sowmini Varadhan01883ed2018-02-15 10:49:35 -0800111 bool zcopy = false;
Andy Grover7875e182009-02-24 15:30:26 +0000112
113 if (unlikely(test_bit(RDS_MSG_PAGEVEC, &rm->m_flags)))
114 return;
115
Sowmini Varadhanea8994c2018-02-15 10:49:33 -0800116 spin_lock_irqsave(&rm->m_rs_lock, flags);
117 if (rm->m_rs) {
Sowmini Varadhan01883ed2018-02-15 10:49:35 -0800118 struct rds_sock *rs = rm->m_rs;
119
120 if (rm->data.op_mmp_znotifier) {
121 zcopy = true;
122 rds_rm_zerocopy_callback(rs, rm->data.op_mmp_znotifier);
Sowmini Varadhan401910d2018-02-27 09:52:43 -0800123 rds_wake_sk_sleep(rs);
Sowmini Varadhan01883ed2018-02-15 10:49:35 -0800124 rm->data.op_mmp_znotifier = NULL;
125 }
126 sock_put(rds_rs_to_sk(rs));
Sowmini Varadhanea8994c2018-02-15 10:49:33 -0800127 rm->m_rs = NULL;
128 }
129 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover7875e182009-02-24 15:30:26 +0000130
Sowmini Varadhan01883ed2018-02-15 10:49:35 -0800131 for (i = 0; i < rm->data.op_nents; i++) {
132 /* XXX will have to put_page for page refs */
133 if (!zcopy)
134 __free_page(sg_page(&rm->data.op_sg[i]));
135 else
136 put_page(sg_page(&rm->data.op_sg[i]));
137 }
138 rm->data.op_nents = 0;
139
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800140 if (rm->rdma.op_active)
141 rds_rdma_free_op(&rm->rdma);
142 if (rm->rdma.op_rdma_mr)
143 rds_mr_put(rm->rdma.op_rdma_mr);
Andy Groverd0ab25a2010-01-27 16:15:48 -0800144
145 if (rm->atomic.op_active)
146 rds_atomic_free_op(&rm->atomic);
147 if (rm->atomic.op_rdma_mr)
148 rds_mr_put(rm->atomic.op_rdma_mr);
Andy Grover7875e182009-02-24 15:30:26 +0000149}
150
Andy Grover7875e182009-02-24 15:30:26 +0000151void rds_message_put(struct rds_message *rm)
152{
Reshetova, Elena6c5a1c42017-07-04 15:53:18 +0300153 rdsdebug("put rm %p ref %d\n", rm, refcount_read(&rm->m_refcount));
154 WARN(!refcount_read(&rm->m_refcount), "danger refcount zero on %p\n", rm);
155 if (refcount_dec_and_test(&rm->m_refcount)) {
Andy Grover7875e182009-02-24 15:30:26 +0000156 BUG_ON(!list_empty(&rm->m_sock_item));
157 BUG_ON(!list_empty(&rm->m_conn_item));
158 rds_message_purge(rm);
159
160 kfree(rm);
161 }
162}
Andy Grover616b7572009-08-21 12:28:32 +0000163EXPORT_SYMBOL_GPL(rds_message_put);
Andy Grover7875e182009-02-24 15:30:26 +0000164
Andy Grover7875e182009-02-24 15:30:26 +0000165void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
166 __be16 dport, u64 seq)
167{
168 hdr->h_flags = 0;
169 hdr->h_sport = sport;
170 hdr->h_dport = dport;
171 hdr->h_sequence = cpu_to_be64(seq);
172 hdr->h_exthdr[0] = RDS_EXTHDR_NONE;
173}
Andy Grover616b7572009-08-21 12:28:32 +0000174EXPORT_SYMBOL_GPL(rds_message_populate_header);
Andy Grover7875e182009-02-24 15:30:26 +0000175
stephen hemmingerff51bf82010-10-19 08:08:33 +0000176int rds_message_add_extension(struct rds_header *hdr, unsigned int type,
177 const void *data, unsigned int len)
Andy Grover7875e182009-02-24 15:30:26 +0000178{
179 unsigned int ext_len = sizeof(u8) + len;
180 unsigned char *dst;
181
182 /* For now, refuse to add more than one extension header */
183 if (hdr->h_exthdr[0] != RDS_EXTHDR_NONE)
184 return 0;
185
Joe Perchesf64f9e72009-11-29 16:55:45 -0800186 if (type >= __RDS_EXTHDR_MAX || len != rds_exthdr_size[type])
Andy Grover7875e182009-02-24 15:30:26 +0000187 return 0;
188
189 if (ext_len >= RDS_HEADER_EXT_SPACE)
190 return 0;
191 dst = hdr->h_exthdr;
192
193 *dst++ = type;
194 memcpy(dst, data, len);
195
196 dst[len] = RDS_EXTHDR_NONE;
197 return 1;
198}
Andy Grover616b7572009-08-21 12:28:32 +0000199EXPORT_SYMBOL_GPL(rds_message_add_extension);
Andy Grover7875e182009-02-24 15:30:26 +0000200
201/*
202 * If a message has extension headers, retrieve them here.
203 * Call like this:
204 *
205 * unsigned int pos = 0;
206 *
207 * while (1) {
208 * buflen = sizeof(buffer);
209 * type = rds_message_next_extension(hdr, &pos, buffer, &buflen);
210 * if (type == RDS_EXTHDR_NONE)
211 * break;
212 * ...
213 * }
214 */
215int rds_message_next_extension(struct rds_header *hdr,
216 unsigned int *pos, void *buf, unsigned int *buflen)
217{
218 unsigned int offset, ext_type, ext_len;
219 u8 *src = hdr->h_exthdr;
220
221 offset = *pos;
222 if (offset >= RDS_HEADER_EXT_SPACE)
223 goto none;
224
225 /* Get the extension type and length. For now, the
226 * length is implied by the extension type. */
227 ext_type = src[offset++];
228
229 if (ext_type == RDS_EXTHDR_NONE || ext_type >= __RDS_EXTHDR_MAX)
230 goto none;
231 ext_len = rds_exthdr_size[ext_type];
232 if (offset + ext_len > RDS_HEADER_EXT_SPACE)
233 goto none;
234
235 *pos = offset + ext_len;
236 if (ext_len < *buflen)
237 *buflen = ext_len;
238 memcpy(buf, src + offset, *buflen);
239 return ext_type;
240
241none:
242 *pos = RDS_HEADER_EXT_SPACE;
243 *buflen = 0;
244 return RDS_EXTHDR_NONE;
245}
246
Andy Grover7875e182009-02-24 15:30:26 +0000247int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset)
248{
249 struct rds_ext_header_rdma_dest ext_hdr;
250
251 ext_hdr.h_rdma_rkey = cpu_to_be32(r_key);
252 ext_hdr.h_rdma_offset = cpu_to_be32(offset);
253 return rds_message_add_extension(hdr, RDS_EXTHDR_RDMA_DEST, &ext_hdr, sizeof(ext_hdr));
254}
Andy Grover616b7572009-08-21 12:28:32 +0000255EXPORT_SYMBOL_GPL(rds_message_add_rdma_dest_extension);
Andy Grover7875e182009-02-24 15:30:26 +0000256
Andy Groverfc445082010-01-12 12:56:06 -0800257/*
258 * Each rds_message is allocated with extra space for the scatterlist entries
259 * rds ops will need. This is to minimize memory allocation count. Then, each rds op
260 * can grab SGs when initializing its part of the rds_message.
261 */
262struct rds_message *rds_message_alloc(unsigned int extra_len, gfp_t gfp)
Andy Grover7875e182009-02-24 15:30:26 +0000263{
264 struct rds_message *rm;
265
Cong Wangece6b0a2013-03-03 16:18:11 +0000266 if (extra_len > KMALLOC_MAX_SIZE - sizeof(struct rds_message))
267 return NULL;
268
Andy Groverfc445082010-01-12 12:56:06 -0800269 rm = kzalloc(sizeof(struct rds_message) + extra_len, gfp);
Andy Grover7875e182009-02-24 15:30:26 +0000270 if (!rm)
271 goto out;
272
Andy Groverfc445082010-01-12 12:56:06 -0800273 rm->m_used_sgs = 0;
274 rm->m_total_sgs = extra_len / sizeof(struct scatterlist);
275
Reshetova, Elena6c5a1c42017-07-04 15:53:18 +0300276 refcount_set(&rm->m_refcount, 1);
Andy Grover7875e182009-02-24 15:30:26 +0000277 INIT_LIST_HEAD(&rm->m_sock_item);
278 INIT_LIST_HEAD(&rm->m_conn_item);
279 spin_lock_init(&rm->m_rs_lock);
Chris Masonc83188d2010-04-21 13:09:28 -0700280 init_waitqueue_head(&rm->m_flush_wait);
Andy Grover7875e182009-02-24 15:30:26 +0000281
282out:
283 return rm;
284}
285
Andy Groverfc445082010-01-12 12:56:06 -0800286/*
287 * RDS ops use this to grab SG entries from the rm's sg pool.
288 */
289struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents)
290{
291 struct scatterlist *sg_first = (struct scatterlist *) &rm[1];
292 struct scatterlist *sg_ret;
293
294 WARN_ON(rm->m_used_sgs + nents > rm->m_total_sgs);
Andy Groveree4c7b42010-02-03 19:41:52 -0800295 WARN_ON(!nents);
Andy Groverfc445082010-01-12 12:56:06 -0800296
Andy Groverd139ff02010-10-28 15:40:59 +0000297 if (rm->m_used_sgs + nents > rm->m_total_sgs)
298 return NULL;
299
Andy Groverfc445082010-01-12 12:56:06 -0800300 sg_ret = &sg_first[rm->m_used_sgs];
Andy Groverf4dd96f72010-01-12 14:17:31 -0800301 sg_init_table(sg_ret, nents);
Andy Groverfc445082010-01-12 12:56:06 -0800302 rm->m_used_sgs += nents;
303
304 return sg_ret;
305}
306
Andy Grover7875e182009-02-24 15:30:26 +0000307struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len)
308{
309 struct rds_message *rm;
310 unsigned int i;
Andy Groverff87e972010-01-12 14:13:15 -0800311 int num_sgs = ceil(total_len, PAGE_SIZE);
312 int extra_bytes = num_sgs * sizeof(struct scatterlist);
Andy Grover7875e182009-02-24 15:30:26 +0000313
Andy Groverf2ec76f2010-03-29 16:46:46 -0700314 rm = rds_message_alloc(extra_bytes, GFP_NOWAIT);
Andy Grover8690bfa2010-01-12 11:56:44 -0800315 if (!rm)
Andy Grover7875e182009-02-24 15:30:26 +0000316 return ERR_PTR(-ENOMEM);
317
318 set_bit(RDS_MSG_PAGEVEC, &rm->m_flags);
319 rm->m_inc.i_hdr.h_len = cpu_to_be32(total_len);
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800320 rm->data.op_nents = ceil(total_len, PAGE_SIZE);
321 rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
Pavel Emelyanovaa581632010-11-08 06:20:50 +0000322 if (!rm->data.op_sg) {
323 rds_message_put(rm);
Andy Groverd139ff02010-10-28 15:40:59 +0000324 return ERR_PTR(-ENOMEM);
Pavel Emelyanovaa581632010-11-08 06:20:50 +0000325 }
Andy Grover7875e182009-02-24 15:30:26 +0000326
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800327 for (i = 0; i < rm->data.op_nents; ++i) {
328 sg_set_page(&rm->data.op_sg[i],
Andy Grover7875e182009-02-24 15:30:26 +0000329 virt_to_page(page_addrs[i]),
330 PAGE_SIZE, 0);
331 }
332
333 return rm;
334}
335
Sowmini Varadhan0cebacc2018-02-15 10:49:36 -0800336int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
337 bool zcopy)
Andy Grover7875e182009-02-24 15:30:26 +0000338{
Sowmini Varadhand0a47d32015-02-05 17:41:43 -0500339 unsigned long to_copy, nbytes;
Andy Grover7875e182009-02-24 15:30:26 +0000340 unsigned long sg_off;
Andy Grover7875e182009-02-24 15:30:26 +0000341 struct scatterlist *sg;
Andy Groverfc445082010-01-12 12:56:06 -0800342 int ret = 0;
Sowmini Varadhan0cebacc2018-02-15 10:49:36 -0800343 int length = iov_iter_count(from);
Andy Grover7875e182009-02-24 15:30:26 +0000344
Al Viro083735f2014-11-20 09:31:08 -0500345 rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from));
Andy Grover7875e182009-02-24 15:30:26 +0000346
347 /*
348 * now allocate and copy in the data payload.
349 */
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800350 sg = rm->data.op_sg;
Andy Grover7875e182009-02-24 15:30:26 +0000351 sg_off = 0; /* Dear gcc, sg->page will be null from kzalloc. */
352
Sowmini Varadhan0cebacc2018-02-15 10:49:36 -0800353 if (zcopy) {
354 int total_copied = 0;
355 struct sk_buff *skb;
356
Sowmini Varadhan401910d2018-02-27 09:52:43 -0800357 skb = alloc_skb(0, GFP_KERNEL);
Sowmini Varadhan0cebacc2018-02-15 10:49:36 -0800358 if (!skb)
359 return -ENOMEM;
Sowmini Varadhan401910d2018-02-27 09:52:43 -0800360 BUILD_BUG_ON(sizeof(skb->cb) <
361 max_t(int, sizeof(struct rds_znotifier),
362 sizeof(struct rds_zcopy_cookies)));
Sowmini Varadhan0cebacc2018-02-15 10:49:36 -0800363 rm->data.op_mmp_znotifier = RDS_ZCOPY_SKB(skb);
364 if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp,
365 length)) {
366 ret = -ENOMEM;
367 goto err;
368 }
369 while (iov_iter_count(from)) {
370 struct page *pages;
371 size_t start;
372 ssize_t copied;
373
374 copied = iov_iter_get_pages(from, &pages, PAGE_SIZE,
375 1, &start);
376 if (copied < 0) {
377 struct mmpin *mmp;
378 int i;
379
380 for (i = 0; i < rm->data.op_nents; i++)
381 put_page(sg_page(&rm->data.op_sg[i]));
382 mmp = &rm->data.op_mmp_znotifier->z_mmp;
383 mm_unaccount_pinned_pages(mmp);
384 ret = -EFAULT;
385 goto err;
386 }
387 total_copied += copied;
388 iov_iter_advance(from, copied);
389 length -= copied;
390 sg_set_page(sg, pages, copied, start);
391 rm->data.op_nents++;
392 sg++;
393 }
394 WARN_ON_ONCE(length != 0);
395 return ret;
396err:
397 consume_skb(skb);
398 rm->data.op_mmp_znotifier = NULL;
399 return ret;
400 } /* zcopy */
401
Al Viro083735f2014-11-20 09:31:08 -0500402 while (iov_iter_count(from)) {
Andy Grover8690bfa2010-01-12 11:56:44 -0800403 if (!sg_page(sg)) {
Al Viro083735f2014-11-20 09:31:08 -0500404 ret = rds_page_remainder_alloc(sg, iov_iter_count(from),
Andy Grover7875e182009-02-24 15:30:26 +0000405 GFP_HIGHUSER);
406 if (ret)
Al Viro083735f2014-11-20 09:31:08 -0500407 return ret;
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800408 rm->data.op_nents++;
Andy Grover7875e182009-02-24 15:30:26 +0000409 sg_off = 0;
410 }
411
Al Viro083735f2014-11-20 09:31:08 -0500412 to_copy = min_t(unsigned long, iov_iter_count(from),
413 sg->length - sg_off);
Andy Grover7875e182009-02-24 15:30:26 +0000414
Al Viro083735f2014-11-20 09:31:08 -0500415 rds_stats_add(s_copy_from_user, to_copy);
Sowmini Varadhand0a47d32015-02-05 17:41:43 -0500416 nbytes = copy_page_from_iter(sg_page(sg), sg->offset + sg_off,
417 to_copy, from);
418 if (nbytes != to_copy)
Al Viro083735f2014-11-20 09:31:08 -0500419 return -EFAULT;
Andy Grover7875e182009-02-24 15:30:26 +0000420
Andy Grover7875e182009-02-24 15:30:26 +0000421 sg_off += to_copy;
422
423 if (sg_off == sg->length)
424 sg++;
425 }
426
Andy Groverfc445082010-01-12 12:56:06 -0800427 return ret;
Andy Grover7875e182009-02-24 15:30:26 +0000428}
429
Al Viroc310e722014-11-20 09:21:14 -0500430int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
Andy Grover7875e182009-02-24 15:30:26 +0000431{
432 struct rds_message *rm;
Andy Grover7875e182009-02-24 15:30:26 +0000433 struct scatterlist *sg;
434 unsigned long to_copy;
Andy Grover7875e182009-02-24 15:30:26 +0000435 unsigned long vec_off;
436 int copied;
437 int ret;
438 u32 len;
439
440 rm = container_of(inc, struct rds_message, m_inc);
441 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
442
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800443 sg = rm->data.op_sg;
Andy Grover7875e182009-02-24 15:30:26 +0000444 vec_off = 0;
445 copied = 0;
446
Al Viroc310e722014-11-20 09:21:14 -0500447 while (iov_iter_count(to) && copied < len) {
Geert Uytterhoeven6ff4a8a2014-12-15 13:21:42 +0100448 to_copy = min_t(unsigned long, iov_iter_count(to),
449 sg->length - vec_off);
Andy Grover7875e182009-02-24 15:30:26 +0000450 to_copy = min_t(unsigned long, to_copy, len - copied);
451
Al Viroc310e722014-11-20 09:21:14 -0500452 rds_stats_add(s_copy_to_user, to_copy);
453 ret = copy_page_to_iter(sg_page(sg), sg->offset + vec_off,
454 to_copy, to);
455 if (ret != to_copy)
456 return -EFAULT;
Andy Grover7875e182009-02-24 15:30:26 +0000457
Andy Grover7875e182009-02-24 15:30:26 +0000458 vec_off += to_copy;
459 copied += to_copy;
460
461 if (vec_off == sg->length) {
462 vec_off = 0;
463 sg++;
464 }
465 }
466
467 return copied;
468}
469
470/*
471 * If the message is still on the send queue, wait until the transport
472 * is done with it. This is particularly important for RDMA operations.
473 */
474void rds_message_wait(struct rds_message *rm)
475{
Chris Masonc83188d2010-04-21 13:09:28 -0700476 wait_event_interruptible(rm->m_flush_wait,
Andy Grover7875e182009-02-24 15:30:26 +0000477 !test_bit(RDS_MSG_MAPPED, &rm->m_flags));
478}
479
480void rds_message_unmapped(struct rds_message *rm)
481{
482 clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
Chris Masonc83188d2010-04-21 13:09:28 -0700483 wake_up_interruptible(&rm->m_flush_wait);
Andy Grover7875e182009-02-24 15:30:26 +0000484}
Andy Grover616b7572009-08-21 12:28:32 +0000485EXPORT_SYMBOL_GPL(rds_message_unmapped);
Andy Grover7875e182009-02-24 15:30:26 +0000486