svc_rdma_sendto.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2016-2018 Oracle. All rights reserved.
  4. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  5. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the BSD-type
  11. * license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * Neither the name of the Network Appliance, Inc. nor the names of
  26. * its contributors may be used to endorse or promote products
  27. * derived from this software without specific prior written
  28. * permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * Author: Tom Tucker <tom@opengridcomputing.com>
  43. */
  44. /* Operation
  45. *
  46. * The main entry point is svc_rdma_sendto. This is called by the
  47. * RPC server when an RPC Reply is ready to be transmitted to a client.
  48. *
  49. * The passed-in svc_rqst contains a struct xdr_buf which holds an
  50. * XDR-encoded RPC Reply message. sendto must construct the RPC-over-RDMA
  51. * transport header, post all Write WRs needed for this Reply, then post
  52. * a Send WR conveying the transport header and the RPC message itself to
  53. * the client.
  54. *
  55. * svc_rdma_sendto must fully transmit the Reply before returning, as
  56. * the svc_rqst will be recycled as soon as sendto returns. Remaining
  57. * resources referred to by the svc_rqst are also recycled at that time.
  58. * Therefore any resources that must remain longer must be detached
  59. * from the svc_rqst and released later.
  60. *
  61. * Page Management
  62. *
  63. * The I/O that performs Reply transmission is asynchronous, and may
  64. * complete well after sendto returns. Thus pages under I/O must be
  65. * removed from the svc_rqst before sendto returns.
  66. *
  67. * The logic here depends on Send Queue and completion ordering. Since
  68. * the Send WR is always posted last, it will always complete last. Thus
  69. * when it completes, it is guaranteed that all previous Write WRs have
  70. * also completed.
  71. *
  72. * Write WRs are constructed and posted. Each Write segment gets its own
  73. * svc_rdma_rw_ctxt, allowing the Write completion handler to find and
  74. * DMA-unmap the pages under I/O for that Write segment. The Write
  75. * completion handler does not release any pages.
  76. *
  77. * When the Send WR is constructed, it also gets its own svc_rdma_send_ctxt.
  78. * The ownership of all of the Reply's pages are transferred into that
  79. * ctxt, the Send WR is posted, and sendto returns.
  80. *
  81. * The svc_rdma_send_ctxt is presented when the Send WR completes. The
  82. * Send completion handler finally releases the Reply's pages.
  83. *
  84. * This mechanism also assumes that completions on the transport's Send
  85. * Completion Queue do not run in parallel. Otherwise a Write completion
  86. * and Send completion running at the same time could release pages that
  87. * are still DMA-mapped.
  88. *
  89. * Error Handling
  90. *
  91. * - If the Send WR is posted successfully, it will either complete
  92. * successfully, or get flushed. Either way, the Send completion
  93. * handler releases the Reply's pages.
  94. * - If the Send WR cannot be not posted, the forward path releases
  95. * the Reply's pages.
  96. *
  97. * This handles the case, without the use of page reference counting,
  98. * where two different Write segments send portions of the same page.
  99. */
  100. #include <linux/spinlock.h>
  101. #include <linux/unaligned.h>
  102. #include <rdma/ib_verbs.h>
  103. #include <rdma/rdma_cm.h>
  104. #include <linux/sunrpc/debug.h>
  105. #include <linux/sunrpc/svc_rdma.h>
  106. #include "xprt_rdma.h"
  107. #include <trace/events/rpcrdma.h>
  108. static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
  109. static struct svc_rdma_send_ctxt *
  110. svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
  111. {
  112. int node = ibdev_to_node(rdma->sc_cm_id->device);
  113. struct svc_rdma_send_ctxt *ctxt;
  114. dma_addr_t addr;
  115. void *buffer;
  116. int i;
  117. ctxt = kzalloc_node(struct_size(ctxt, sc_sges, rdma->sc_max_send_sges),
  118. GFP_KERNEL, node);
  119. if (!ctxt)
  120. goto fail0;
  121. buffer = kmalloc_node(rdma->sc_max_req_size, GFP_KERNEL, node);
  122. if (!buffer)
  123. goto fail1;
  124. addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
  125. rdma->sc_max_req_size, DMA_TO_DEVICE);
  126. if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
  127. goto fail2;
  128. svc_rdma_send_cid_init(rdma, &ctxt->sc_cid);
  129. ctxt->sc_rdma = rdma;
  130. ctxt->sc_send_wr.next = NULL;
  131. ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
  132. ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
  133. ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
  134. ctxt->sc_cqe.done = svc_rdma_wc_send;
  135. ctxt->sc_xprt_buf = buffer;
  136. xdr_buf_init(&ctxt->sc_hdrbuf, ctxt->sc_xprt_buf,
  137. rdma->sc_max_req_size);
  138. ctxt->sc_sges[0].addr = addr;
  139. for (i = 0; i < rdma->sc_max_send_sges; i++)
  140. ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
  141. return ctxt;
  142. fail2:
  143. kfree(buffer);
  144. fail1:
  145. kfree(ctxt);
  146. fail0:
  147. return NULL;
  148. }
  149. /**
  150. * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt
  151. * @rdma: svcxprt_rdma being torn down
  152. *
  153. */
  154. void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
  155. {
  156. struct svc_rdma_send_ctxt *ctxt;
  157. struct llist_node *node;
  158. while ((node = llist_del_first(&rdma->sc_send_ctxts)) != NULL) {
  159. ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
  160. ib_dma_unmap_single(rdma->sc_pd->device,
  161. ctxt->sc_sges[0].addr,
  162. rdma->sc_max_req_size,
  163. DMA_TO_DEVICE);
  164. kfree(ctxt->sc_xprt_buf);
  165. kfree(ctxt);
  166. }
  167. }
  168. /**
  169. * svc_rdma_send_ctxt_get - Get a free send_ctxt
  170. * @rdma: controlling svcxprt_rdma
  171. *
  172. * Returns a ready-to-use send_ctxt, or NULL if none are
  173. * available and a fresh one cannot be allocated.
  174. */
  175. struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
  176. {
  177. struct svc_rdma_send_ctxt *ctxt;
  178. struct llist_node *node;
  179. spin_lock(&rdma->sc_send_lock);
  180. node = llist_del_first(&rdma->sc_send_ctxts);
  181. spin_unlock(&rdma->sc_send_lock);
  182. if (!node)
  183. goto out_empty;
  184. ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
  185. out:
  186. rpcrdma_set_xdrlen(&ctxt->sc_hdrbuf, 0);
  187. xdr_init_encode(&ctxt->sc_stream, &ctxt->sc_hdrbuf,
  188. ctxt->sc_xprt_buf, NULL);
  189. svc_rdma_cc_init(rdma, &ctxt->sc_reply_info.wi_cc);
  190. ctxt->sc_send_wr.num_sge = 0;
  191. ctxt->sc_cur_sge_no = 0;
  192. ctxt->sc_page_count = 0;
  193. ctxt->sc_wr_chain = &ctxt->sc_send_wr;
  194. ctxt->sc_sqecount = 1;
  195. return ctxt;
  196. out_empty:
  197. ctxt = svc_rdma_send_ctxt_alloc(rdma);
  198. if (!ctxt)
  199. return NULL;
  200. goto out;
  201. }
  202. static void svc_rdma_send_ctxt_release(struct svcxprt_rdma *rdma,
  203. struct svc_rdma_send_ctxt *ctxt)
  204. {
  205. struct ib_device *device = rdma->sc_cm_id->device;
  206. unsigned int i;
  207. svc_rdma_reply_chunk_release(rdma, ctxt);
  208. if (ctxt->sc_page_count)
  209. release_pages(ctxt->sc_pages, ctxt->sc_page_count);
  210. /* The first SGE contains the transport header, which
  211. * remains mapped until @ctxt is destroyed.
  212. */
  213. for (i = 1; i < ctxt->sc_send_wr.num_sge; i++) {
  214. trace_svcrdma_dma_unmap_page(&ctxt->sc_cid,
  215. ctxt->sc_sges[i].addr,
  216. ctxt->sc_sges[i].length);
  217. ib_dma_unmap_page(device,
  218. ctxt->sc_sges[i].addr,
  219. ctxt->sc_sges[i].length,
  220. DMA_TO_DEVICE);
  221. }
  222. llist_add(&ctxt->sc_node, &rdma->sc_send_ctxts);
  223. }
  224. static void svc_rdma_send_ctxt_put_async(struct work_struct *work)
  225. {
  226. struct svc_rdma_send_ctxt *ctxt;
  227. ctxt = container_of(work, struct svc_rdma_send_ctxt, sc_work);
  228. svc_rdma_send_ctxt_release(ctxt->sc_rdma, ctxt);
  229. }
  230. /**
  231. * svc_rdma_send_ctxt_put - Return send_ctxt to free list
  232. * @rdma: controlling svcxprt_rdma
  233. * @ctxt: object to return to the free list
  234. *
  235. * Pages left in sc_pages are DMA unmapped and released.
  236. */
  237. void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
  238. struct svc_rdma_send_ctxt *ctxt)
  239. {
  240. INIT_WORK(&ctxt->sc_work, svc_rdma_send_ctxt_put_async);
  241. queue_work(svcrdma_wq, &ctxt->sc_work);
  242. }
  243. /**
  244. * svc_rdma_wake_send_waiters - manage Send Queue accounting
  245. * @rdma: controlling transport
  246. * @avail: Number of additional SQEs that are now available
  247. *
  248. */
  249. void svc_rdma_wake_send_waiters(struct svcxprt_rdma *rdma, int avail)
  250. {
  251. atomic_add(avail, &rdma->sc_sq_avail);
  252. smp_mb__after_atomic();
  253. if (unlikely(waitqueue_active(&rdma->sc_send_wait)))
  254. wake_up(&rdma->sc_send_wait);
  255. }
  256. /**
  257. * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
  258. * @cq: Completion Queue context
  259. * @wc: Work Completion object
  260. *
  261. * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
  262. * the Send completion handler could be running.
  263. */
  264. static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
  265. {
  266. struct svcxprt_rdma *rdma = cq->cq_context;
  267. struct ib_cqe *cqe = wc->wr_cqe;
  268. struct svc_rdma_send_ctxt *ctxt =
  269. container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
  270. svc_rdma_wake_send_waiters(rdma, ctxt->sc_sqecount);
  271. if (unlikely(wc->status != IB_WC_SUCCESS))
  272. goto flushed;
  273. trace_svcrdma_wc_send(&ctxt->sc_cid);
  274. svc_rdma_send_ctxt_put(rdma, ctxt);
  275. return;
  276. flushed:
  277. if (wc->status != IB_WC_WR_FLUSH_ERR)
  278. trace_svcrdma_wc_send_err(wc, &ctxt->sc_cid);
  279. else
  280. trace_svcrdma_wc_send_flush(wc, &ctxt->sc_cid);
  281. svc_rdma_send_ctxt_put(rdma, ctxt);
  282. svc_xprt_deferred_close(&rdma->sc_xprt);
  283. }
  284. /**
  285. * svc_rdma_post_send - Post a WR chain to the Send Queue
  286. * @rdma: transport context
  287. * @ctxt: WR chain to post
  288. *
  289. * Copy fields in @ctxt to stack variables in order to guarantee
  290. * that these values remain available after the ib_post_send() call.
  291. * In some error flow cases, svc_rdma_wc_send() releases @ctxt.
  292. *
  293. * Note there is potential for starvation when the Send Queue is
  294. * full because there is no order to when waiting threads are
  295. * awoken. The transport is typically provisioned with a deep
  296. * enough Send Queue that SQ exhaustion should be a rare event.
  297. *
  298. * Return values:
  299. * %0: @ctxt's WR chain was posted successfully
  300. * %-ENOTCONN: The connection was lost
  301. */
  302. int svc_rdma_post_send(struct svcxprt_rdma *rdma,
  303. struct svc_rdma_send_ctxt *ctxt)
  304. {
  305. struct ib_send_wr *first_wr = ctxt->sc_wr_chain;
  306. struct ib_send_wr *send_wr = &ctxt->sc_send_wr;
  307. const struct ib_send_wr *bad_wr = first_wr;
  308. struct rpc_rdma_cid cid = ctxt->sc_cid;
  309. int ret, sqecount = ctxt->sc_sqecount;
  310. might_sleep();
  311. /* Sync the transport header buffer */
  312. ib_dma_sync_single_for_device(rdma->sc_pd->device,
  313. send_wr->sg_list[0].addr,
  314. send_wr->sg_list[0].length,
  315. DMA_TO_DEVICE);
  316. /* If the SQ is full, wait until an SQ entry is available */
  317. while (!test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags)) {
  318. if (atomic_sub_return(sqecount, &rdma->sc_sq_avail) < 0) {
  319. svc_rdma_wake_send_waiters(rdma, sqecount);
  320. /* When the transport is torn down, assume
  321. * ib_drain_sq() will trigger enough Send
  322. * completions to wake us. The XPT_CLOSE test
  323. * above should then cause the while loop to
  324. * exit.
  325. */
  326. percpu_counter_inc(&svcrdma_stat_sq_starve);
  327. trace_svcrdma_sq_full(rdma, &cid);
  328. wait_event(rdma->sc_send_wait,
  329. atomic_read(&rdma->sc_sq_avail) > 0);
  330. trace_svcrdma_sq_retry(rdma, &cid);
  331. continue;
  332. }
  333. trace_svcrdma_post_send(ctxt);
  334. ret = ib_post_send(rdma->sc_qp, first_wr, &bad_wr);
  335. if (ret) {
  336. trace_svcrdma_sq_post_err(rdma, &cid, ret);
  337. svc_xprt_deferred_close(&rdma->sc_xprt);
  338. /* If even one WR was posted, there will be a
  339. * Send completion that bumps sc_sq_avail.
  340. */
  341. if (bad_wr == first_wr) {
  342. svc_rdma_wake_send_waiters(rdma, sqecount);
  343. break;
  344. }
  345. }
  346. return 0;
  347. }
  348. return -ENOTCONN;
  349. }
  350. /**
  351. * svc_rdma_encode_read_list - Encode RPC Reply's Read chunk list
  352. * @sctxt: Send context for the RPC Reply
  353. *
  354. * Return values:
  355. * On success, returns length in bytes of the Reply XDR buffer
  356. * that was consumed by the Reply Read list
  357. * %-EMSGSIZE on XDR buffer overflow
  358. */
  359. static ssize_t svc_rdma_encode_read_list(struct svc_rdma_send_ctxt *sctxt)
  360. {
  361. /* RPC-over-RDMA version 1 replies never have a Read list. */
  362. return xdr_stream_encode_item_absent(&sctxt->sc_stream);
  363. }
  364. /**
  365. * svc_rdma_encode_write_segment - Encode one Write segment
  366. * @sctxt: Send context for the RPC Reply
  367. * @chunk: Write chunk to push
  368. * @remaining: remaining bytes of the payload left in the Write chunk
  369. * @segno: which segment in the chunk
  370. *
  371. * Return values:
  372. * On success, returns length in bytes of the Reply XDR buffer
  373. * that was consumed by the Write segment, and updates @remaining
  374. * %-EMSGSIZE on XDR buffer overflow
  375. */
  376. static ssize_t svc_rdma_encode_write_segment(struct svc_rdma_send_ctxt *sctxt,
  377. const struct svc_rdma_chunk *chunk,
  378. u32 *remaining, unsigned int segno)
  379. {
  380. const struct svc_rdma_segment *segment = &chunk->ch_segments[segno];
  381. const size_t len = rpcrdma_segment_maxsz * sizeof(__be32);
  382. u32 length;
  383. __be32 *p;
  384. p = xdr_reserve_space(&sctxt->sc_stream, len);
  385. if (!p)
  386. return -EMSGSIZE;
  387. length = min_t(u32, *remaining, segment->rs_length);
  388. *remaining -= length;
  389. xdr_encode_rdma_segment(p, segment->rs_handle, length,
  390. segment->rs_offset);
  391. trace_svcrdma_encode_wseg(sctxt, segno, segment->rs_handle, length,
  392. segment->rs_offset);
  393. return len;
  394. }
  395. /**
  396. * svc_rdma_encode_write_chunk - Encode one Write chunk
  397. * @sctxt: Send context for the RPC Reply
  398. * @chunk: Write chunk to push
  399. *
  400. * Copy a Write chunk from the Call transport header to the
  401. * Reply transport header. Update each segment's length field
  402. * to reflect the number of bytes written in that segment.
  403. *
  404. * Return values:
  405. * On success, returns length in bytes of the Reply XDR buffer
  406. * that was consumed by the Write chunk
  407. * %-EMSGSIZE on XDR buffer overflow
  408. */
  409. static ssize_t svc_rdma_encode_write_chunk(struct svc_rdma_send_ctxt *sctxt,
  410. const struct svc_rdma_chunk *chunk)
  411. {
  412. u32 remaining = chunk->ch_payload_length;
  413. unsigned int segno;
  414. ssize_t len, ret;
  415. len = 0;
  416. ret = xdr_stream_encode_item_present(&sctxt->sc_stream);
  417. if (ret < 0)
  418. return ret;
  419. len += ret;
  420. ret = xdr_stream_encode_u32(&sctxt->sc_stream, chunk->ch_segcount);
  421. if (ret < 0)
  422. return ret;
  423. len += ret;
  424. for (segno = 0; segno < chunk->ch_segcount; segno++) {
  425. ret = svc_rdma_encode_write_segment(sctxt, chunk, &remaining, segno);
  426. if (ret < 0)
  427. return ret;
  428. len += ret;
  429. }
  430. return len;
  431. }
  432. /**
  433. * svc_rdma_encode_write_list - Encode RPC Reply's Write chunk list
  434. * @rctxt: Reply context with information about the RPC Call
  435. * @sctxt: Send context for the RPC Reply
  436. *
  437. * Return values:
  438. * On success, returns length in bytes of the Reply XDR buffer
  439. * that was consumed by the Reply's Write list
  440. * %-EMSGSIZE on XDR buffer overflow
  441. */
  442. static ssize_t svc_rdma_encode_write_list(struct svc_rdma_recv_ctxt *rctxt,
  443. struct svc_rdma_send_ctxt *sctxt)
  444. {
  445. struct svc_rdma_chunk *chunk;
  446. ssize_t len, ret;
  447. len = 0;
  448. pcl_for_each_chunk(chunk, &rctxt->rc_write_pcl) {
  449. ret = svc_rdma_encode_write_chunk(sctxt, chunk);
  450. if (ret < 0)
  451. return ret;
  452. len += ret;
  453. }
  454. /* Terminate the Write list */
  455. ret = xdr_stream_encode_item_absent(&sctxt->sc_stream);
  456. if (ret < 0)
  457. return ret;
  458. return len + ret;
  459. }
  460. /**
  461. * svc_rdma_encode_reply_chunk - Encode RPC Reply's Reply chunk
  462. * @rctxt: Reply context with information about the RPC Call
  463. * @sctxt: Send context for the RPC Reply
  464. * @length: size in bytes of the payload in the Reply chunk
  465. *
  466. * Return values:
  467. * On success, returns length in bytes of the Reply XDR buffer
  468. * that was consumed by the Reply's Reply chunk
  469. * %-EMSGSIZE on XDR buffer overflow
  470. * %-E2BIG if the RPC message is larger than the Reply chunk
  471. */
  472. static ssize_t
  473. svc_rdma_encode_reply_chunk(struct svc_rdma_recv_ctxt *rctxt,
  474. struct svc_rdma_send_ctxt *sctxt,
  475. unsigned int length)
  476. {
  477. struct svc_rdma_chunk *chunk;
  478. if (pcl_is_empty(&rctxt->rc_reply_pcl))
  479. return xdr_stream_encode_item_absent(&sctxt->sc_stream);
  480. chunk = pcl_first_chunk(&rctxt->rc_reply_pcl);
  481. if (length > chunk->ch_length)
  482. return -E2BIG;
  483. chunk->ch_payload_length = length;
  484. return svc_rdma_encode_write_chunk(sctxt, chunk);
  485. }
  486. struct svc_rdma_map_data {
  487. struct svcxprt_rdma *md_rdma;
  488. struct svc_rdma_send_ctxt *md_ctxt;
  489. };
  490. /**
  491. * svc_rdma_page_dma_map - DMA map one page
  492. * @data: pointer to arguments
  493. * @page: struct page to DMA map
  494. * @offset: offset into the page
  495. * @len: number of bytes to map
  496. *
  497. * Returns:
  498. * %0 if DMA mapping was successful
  499. * %-EIO if the page cannot be DMA mapped
  500. */
  501. static int svc_rdma_page_dma_map(void *data, struct page *page,
  502. unsigned long offset, unsigned int len)
  503. {
  504. struct svc_rdma_map_data *args = data;
  505. struct svcxprt_rdma *rdma = args->md_rdma;
  506. struct svc_rdma_send_ctxt *ctxt = args->md_ctxt;
  507. struct ib_device *dev = rdma->sc_cm_id->device;
  508. dma_addr_t dma_addr;
  509. ++ctxt->sc_cur_sge_no;
  510. dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
  511. if (ib_dma_mapping_error(dev, dma_addr))
  512. goto out_maperr;
  513. trace_svcrdma_dma_map_page(&ctxt->sc_cid, dma_addr, len);
  514. ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
  515. ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
  516. ctxt->sc_send_wr.num_sge++;
  517. return 0;
  518. out_maperr:
  519. trace_svcrdma_dma_map_err(&ctxt->sc_cid, dma_addr, len);
  520. return -EIO;
  521. }
  522. /**
  523. * svc_rdma_iov_dma_map - DMA map an iovec
  524. * @data: pointer to arguments
  525. * @iov: kvec to DMA map
  526. *
  527. * ib_dma_map_page() is used here because svc_rdma_dma_unmap()
  528. * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
  529. *
  530. * Returns:
  531. * %0 if DMA mapping was successful
  532. * %-EIO if the iovec cannot be DMA mapped
  533. */
  534. static int svc_rdma_iov_dma_map(void *data, const struct kvec *iov)
  535. {
  536. if (!iov->iov_len)
  537. return 0;
  538. return svc_rdma_page_dma_map(data, virt_to_page(iov->iov_base),
  539. offset_in_page(iov->iov_base),
  540. iov->iov_len);
  541. }
  542. /**
  543. * svc_rdma_xb_dma_map - DMA map all segments of an xdr_buf
  544. * @xdr: xdr_buf containing portion of an RPC message to transmit
  545. * @data: pointer to arguments
  546. *
  547. * Returns:
  548. * %0 if DMA mapping was successful
  549. * %-EIO if DMA mapping failed
  550. *
  551. * On failure, any DMA mappings that have been already done must be
  552. * unmapped by the caller.
  553. */
  554. static int svc_rdma_xb_dma_map(const struct xdr_buf *xdr, void *data)
  555. {
  556. unsigned int len, remaining;
  557. unsigned long pageoff;
  558. struct page **ppages;
  559. int ret;
  560. ret = svc_rdma_iov_dma_map(data, &xdr->head[0]);
  561. if (ret < 0)
  562. return ret;
  563. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  564. pageoff = offset_in_page(xdr->page_base);
  565. remaining = xdr->page_len;
  566. while (remaining) {
  567. len = min_t(u32, PAGE_SIZE - pageoff, remaining);
  568. ret = svc_rdma_page_dma_map(data, *ppages++, pageoff, len);
  569. if (ret < 0)
  570. return ret;
  571. remaining -= len;
  572. pageoff = 0;
  573. }
  574. ret = svc_rdma_iov_dma_map(data, &xdr->tail[0]);
  575. if (ret < 0)
  576. return ret;
  577. return xdr->len;
  578. }
  579. struct svc_rdma_pullup_data {
  580. u8 *pd_dest;
  581. unsigned int pd_length;
  582. unsigned int pd_num_sges;
  583. };
  584. /**
  585. * svc_rdma_xb_count_sges - Count how many SGEs will be needed
  586. * @xdr: xdr_buf containing portion of an RPC message to transmit
  587. * @data: pointer to arguments
  588. *
  589. * Returns:
  590. * Number of SGEs needed to Send the contents of @xdr inline
  591. */
  592. static int svc_rdma_xb_count_sges(const struct xdr_buf *xdr,
  593. void *data)
  594. {
  595. struct svc_rdma_pullup_data *args = data;
  596. unsigned int remaining;
  597. unsigned long offset;
  598. if (xdr->head[0].iov_len)
  599. ++args->pd_num_sges;
  600. offset = offset_in_page(xdr->page_base);
  601. remaining = xdr->page_len;
  602. while (remaining) {
  603. ++args->pd_num_sges;
  604. remaining -= min_t(u32, PAGE_SIZE - offset, remaining);
  605. offset = 0;
  606. }
  607. if (xdr->tail[0].iov_len)
  608. ++args->pd_num_sges;
  609. args->pd_length += xdr->len;
  610. return 0;
  611. }
  612. /**
  613. * svc_rdma_pull_up_needed - Determine whether to use pull-up
  614. * @rdma: controlling transport
  615. * @sctxt: send_ctxt for the Send WR
  616. * @write_pcl: Write chunk list provided by client
  617. * @xdr: xdr_buf containing RPC message to transmit
  618. *
  619. * Returns:
  620. * %true if pull-up must be used
  621. * %false otherwise
  622. */
  623. static bool svc_rdma_pull_up_needed(const struct svcxprt_rdma *rdma,
  624. const struct svc_rdma_send_ctxt *sctxt,
  625. const struct svc_rdma_pcl *write_pcl,
  626. const struct xdr_buf *xdr)
  627. {
  628. /* Resources needed for the transport header */
  629. struct svc_rdma_pullup_data args = {
  630. .pd_length = sctxt->sc_hdrbuf.len,
  631. .pd_num_sges = 1,
  632. };
  633. int ret;
  634. ret = pcl_process_nonpayloads(write_pcl, xdr,
  635. svc_rdma_xb_count_sges, &args);
  636. if (ret < 0)
  637. return false;
  638. if (args.pd_length < RPCRDMA_PULLUP_THRESH)
  639. return true;
  640. return args.pd_num_sges >= rdma->sc_max_send_sges;
  641. }
  642. /**
  643. * svc_rdma_xb_linearize - Copy region of xdr_buf to flat buffer
  644. * @xdr: xdr_buf containing portion of an RPC message to copy
  645. * @data: pointer to arguments
  646. *
  647. * Returns:
  648. * Always zero.
  649. */
  650. static int svc_rdma_xb_linearize(const struct xdr_buf *xdr,
  651. void *data)
  652. {
  653. struct svc_rdma_pullup_data *args = data;
  654. unsigned int len, remaining;
  655. unsigned long pageoff;
  656. struct page **ppages;
  657. if (xdr->head[0].iov_len) {
  658. memcpy(args->pd_dest, xdr->head[0].iov_base, xdr->head[0].iov_len);
  659. args->pd_dest += xdr->head[0].iov_len;
  660. }
  661. ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
  662. pageoff = offset_in_page(xdr->page_base);
  663. remaining = xdr->page_len;
  664. while (remaining) {
  665. len = min_t(u32, PAGE_SIZE - pageoff, remaining);
  666. memcpy(args->pd_dest, page_address(*ppages) + pageoff, len);
  667. remaining -= len;
  668. args->pd_dest += len;
  669. pageoff = 0;
  670. ppages++;
  671. }
  672. if (xdr->tail[0].iov_len) {
  673. memcpy(args->pd_dest, xdr->tail[0].iov_base, xdr->tail[0].iov_len);
  674. args->pd_dest += xdr->tail[0].iov_len;
  675. }
  676. args->pd_length += xdr->len;
  677. return 0;
  678. }
  679. /**
  680. * svc_rdma_pull_up_reply_msg - Copy Reply into a single buffer
  681. * @rdma: controlling transport
  682. * @sctxt: send_ctxt for the Send WR; xprt hdr is already prepared
  683. * @write_pcl: Write chunk list provided by client
  684. * @xdr: prepared xdr_buf containing RPC message
  685. *
  686. * The device is not capable of sending the reply directly.
  687. * Assemble the elements of @xdr into the transport header buffer.
  688. *
  689. * Assumptions:
  690. * pull_up_needed has determined that @xdr will fit in the buffer.
  691. *
  692. * Returns:
  693. * %0 if pull-up was successful
  694. * %-EMSGSIZE if a buffer manipulation problem occurred
  695. */
  696. static int svc_rdma_pull_up_reply_msg(const struct svcxprt_rdma *rdma,
  697. struct svc_rdma_send_ctxt *sctxt,
  698. const struct svc_rdma_pcl *write_pcl,
  699. const struct xdr_buf *xdr)
  700. {
  701. struct svc_rdma_pullup_data args = {
  702. .pd_dest = sctxt->sc_xprt_buf + sctxt->sc_hdrbuf.len,
  703. };
  704. int ret;
  705. ret = pcl_process_nonpayloads(write_pcl, xdr,
  706. svc_rdma_xb_linearize, &args);
  707. if (ret < 0)
  708. return ret;
  709. sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len + args.pd_length;
  710. trace_svcrdma_send_pullup(sctxt, args.pd_length);
  711. return 0;
  712. }
  713. /* svc_rdma_map_reply_msg - DMA map the buffer holding RPC message
  714. * @rdma: controlling transport
  715. * @sctxt: send_ctxt for the Send WR
  716. * @write_pcl: Write chunk list provided by client
  717. * @reply_pcl: Reply chunk provided by client
  718. * @xdr: prepared xdr_buf containing RPC message
  719. *
  720. * Returns:
  721. * %0 if DMA mapping was successful.
  722. * %-EMSGSIZE if a buffer manipulation problem occurred
  723. * %-EIO if DMA mapping failed
  724. *
  725. * The Send WR's num_sge field is set in all cases.
  726. */
  727. int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
  728. struct svc_rdma_send_ctxt *sctxt,
  729. const struct svc_rdma_pcl *write_pcl,
  730. const struct svc_rdma_pcl *reply_pcl,
  731. const struct xdr_buf *xdr)
  732. {
  733. struct svc_rdma_map_data args = {
  734. .md_rdma = rdma,
  735. .md_ctxt = sctxt,
  736. };
  737. /* Set up the (persistently-mapped) transport header SGE. */
  738. sctxt->sc_send_wr.num_sge = 1;
  739. sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
  740. /* If there is a Reply chunk, nothing follows the transport
  741. * header, so there is nothing to map.
  742. */
  743. if (!pcl_is_empty(reply_pcl))
  744. return 0;
  745. /* For pull-up, svc_rdma_send() will sync the transport header.
  746. * No additional DMA mapping is necessary.
  747. */
  748. if (svc_rdma_pull_up_needed(rdma, sctxt, write_pcl, xdr))
  749. return svc_rdma_pull_up_reply_msg(rdma, sctxt, write_pcl, xdr);
  750. return pcl_process_nonpayloads(write_pcl, xdr,
  751. svc_rdma_xb_dma_map, &args);
  752. }
  753. /* The svc_rqst and all resources it owns are released as soon as
  754. * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt
  755. * so they are released by the Send completion handler.
  756. */
  757. static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
  758. struct svc_rdma_send_ctxt *ctxt)
  759. {
  760. int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
  761. ctxt->sc_page_count += pages;
  762. for (i = 0; i < pages; i++) {
  763. ctxt->sc_pages[i] = rqstp->rq_respages[i];
  764. rqstp->rq_respages[i] = NULL;
  765. }
  766. /* Prevent svc_xprt_release from releasing pages in rq_pages */
  767. rqstp->rq_next_page = rqstp->rq_respages;
  768. }
  769. /* Prepare the portion of the RPC Reply that will be transmitted
  770. * via RDMA Send. The RPC-over-RDMA transport header is prepared
  771. * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
  772. *
  773. * Depending on whether a Write list or Reply chunk is present,
  774. * the server may Send all, a portion of, or none of the xdr_buf.
  775. * In the latter case, only the transport header (sc_sges[0]) is
  776. * transmitted.
  777. *
  778. * Assumptions:
  779. * - The Reply's transport header will never be larger than a page.
  780. */
  781. static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
  782. struct svc_rdma_send_ctxt *sctxt,
  783. const struct svc_rdma_recv_ctxt *rctxt,
  784. struct svc_rqst *rqstp)
  785. {
  786. struct ib_send_wr *send_wr = &sctxt->sc_send_wr;
  787. int ret;
  788. ret = svc_rdma_map_reply_msg(rdma, sctxt, &rctxt->rc_write_pcl,
  789. &rctxt->rc_reply_pcl, &rqstp->rq_res);
  790. if (ret < 0)
  791. return ret;
  792. /* Transfer pages involved in RDMA Writes to the sctxt's
  793. * page array. Completion handling releases these pages.
  794. */
  795. svc_rdma_save_io_pages(rqstp, sctxt);
  796. if (rctxt->rc_inv_rkey) {
  797. send_wr->opcode = IB_WR_SEND_WITH_INV;
  798. send_wr->ex.invalidate_rkey = rctxt->rc_inv_rkey;
  799. } else {
  800. send_wr->opcode = IB_WR_SEND;
  801. }
  802. return svc_rdma_post_send(rdma, sctxt);
  803. }
  804. /**
  805. * svc_rdma_send_error_msg - Send an RPC/RDMA v1 error response
  806. * @rdma: controlling transport context
  807. * @sctxt: Send context for the response
  808. * @rctxt: Receive context for incoming bad message
  809. * @status: negative errno indicating error that occurred
  810. *
  811. * Given the client-provided Read, Write, and Reply chunks, the
  812. * server was not able to parse the Call or form a complete Reply.
  813. * Return an RDMA_ERROR message so the client can retire the RPC
  814. * transaction.
  815. *
  816. * The caller does not have to release @sctxt. It is released by
  817. * Send completion, or by this function on error.
  818. */
  819. void svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
  820. struct svc_rdma_send_ctxt *sctxt,
  821. struct svc_rdma_recv_ctxt *rctxt,
  822. int status)
  823. {
  824. __be32 *rdma_argp = rctxt->rc_recv_buf;
  825. __be32 *p;
  826. rpcrdma_set_xdrlen(&sctxt->sc_hdrbuf, 0);
  827. xdr_init_encode(&sctxt->sc_stream, &sctxt->sc_hdrbuf,
  828. sctxt->sc_xprt_buf, NULL);
  829. p = xdr_reserve_space(&sctxt->sc_stream,
  830. rpcrdma_fixed_maxsz * sizeof(*p));
  831. if (!p)
  832. goto put_ctxt;
  833. *p++ = *rdma_argp;
  834. *p++ = *(rdma_argp + 1);
  835. *p++ = rdma->sc_fc_credits;
  836. *p = rdma_error;
  837. switch (status) {
  838. case -EPROTONOSUPPORT:
  839. p = xdr_reserve_space(&sctxt->sc_stream, 3 * sizeof(*p));
  840. if (!p)
  841. goto put_ctxt;
  842. *p++ = err_vers;
  843. *p++ = rpcrdma_version;
  844. *p = rpcrdma_version;
  845. trace_svcrdma_err_vers(*rdma_argp);
  846. break;
  847. default:
  848. p = xdr_reserve_space(&sctxt->sc_stream, sizeof(*p));
  849. if (!p)
  850. goto put_ctxt;
  851. *p = err_chunk;
  852. trace_svcrdma_err_chunk(*rdma_argp);
  853. }
  854. /* Remote Invalidation is skipped for simplicity. */
  855. sctxt->sc_send_wr.num_sge = 1;
  856. sctxt->sc_send_wr.opcode = IB_WR_SEND;
  857. sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
  858. if (svc_rdma_post_send(rdma, sctxt))
  859. goto put_ctxt;
  860. return;
  861. put_ctxt:
  862. svc_rdma_send_ctxt_put(rdma, sctxt);
  863. }
  864. /**
  865. * svc_rdma_sendto - Transmit an RPC reply
  866. * @rqstp: processed RPC request, reply XDR already in ::rq_res
  867. *
  868. * Any resources still associated with @rqstp are released upon return.
  869. * If no reply message was possible, the connection is closed.
  870. *
  871. * Returns:
  872. * %0 if an RPC reply has been successfully posted,
  873. * %-ENOMEM if a resource shortage occurred (connection is lost),
  874. * %-ENOTCONN if posting failed (connection is lost).
  875. */
  876. int svc_rdma_sendto(struct svc_rqst *rqstp)
  877. {
  878. struct svc_xprt *xprt = rqstp->rq_xprt;
  879. struct svcxprt_rdma *rdma =
  880. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  881. struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
  882. __be32 *rdma_argp = rctxt->rc_recv_buf;
  883. struct svc_rdma_send_ctxt *sctxt;
  884. unsigned int rc_size;
  885. __be32 *p;
  886. int ret;
  887. ret = -ENOTCONN;
  888. if (svc_xprt_is_dead(xprt))
  889. goto drop_connection;
  890. ret = -ENOMEM;
  891. sctxt = svc_rdma_send_ctxt_get(rdma);
  892. if (!sctxt)
  893. goto drop_connection;
  894. ret = -EMSGSIZE;
  895. p = xdr_reserve_space(&sctxt->sc_stream,
  896. rpcrdma_fixed_maxsz * sizeof(*p));
  897. if (!p)
  898. goto put_ctxt;
  899. ret = svc_rdma_send_write_list(rdma, rctxt, &rqstp->rq_res);
  900. if (ret < 0)
  901. goto put_ctxt;
  902. rc_size = 0;
  903. if (!pcl_is_empty(&rctxt->rc_reply_pcl)) {
  904. ret = svc_rdma_prepare_reply_chunk(rdma, &rctxt->rc_write_pcl,
  905. &rctxt->rc_reply_pcl, sctxt,
  906. &rqstp->rq_res);
  907. if (ret < 0)
  908. goto reply_chunk;
  909. rc_size = ret;
  910. }
  911. *p++ = *rdma_argp;
  912. *p++ = *(rdma_argp + 1);
  913. *p++ = rdma->sc_fc_credits;
  914. *p = pcl_is_empty(&rctxt->rc_reply_pcl) ? rdma_msg : rdma_nomsg;
  915. ret = svc_rdma_encode_read_list(sctxt);
  916. if (ret < 0)
  917. goto put_ctxt;
  918. ret = svc_rdma_encode_write_list(rctxt, sctxt);
  919. if (ret < 0)
  920. goto put_ctxt;
  921. ret = svc_rdma_encode_reply_chunk(rctxt, sctxt, rc_size);
  922. if (ret < 0)
  923. goto put_ctxt;
  924. ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp);
  925. if (ret < 0)
  926. goto put_ctxt;
  927. return 0;
  928. reply_chunk:
  929. if (ret != -E2BIG && ret != -EINVAL)
  930. goto put_ctxt;
  931. /* Send completion releases payload pages that were part
  932. * of previously posted RDMA Writes.
  933. */
  934. svc_rdma_save_io_pages(rqstp, sctxt);
  935. svc_rdma_send_error_msg(rdma, sctxt, rctxt, ret);
  936. return 0;
  937. put_ctxt:
  938. svc_rdma_send_ctxt_put(rdma, sctxt);
  939. drop_connection:
  940. trace_svcrdma_send_err(rqstp, ret);
  941. svc_xprt_deferred_close(&rdma->sc_xprt);
  942. return -ENOTCONN;
  943. }
  944. /**
  945. * svc_rdma_result_payload - special processing for a result payload
  946. * @rqstp: RPC transaction context
  947. * @offset: payload's byte offset in @rqstp->rq_res
  948. * @length: size of payload, in bytes
  949. *
  950. * Assign the passed-in result payload to the current Write chunk,
  951. * and advance to cur_result_payload to the next Write chunk, if
  952. * there is one.
  953. *
  954. * Return values:
  955. * %0 if successful or nothing needed to be done
  956. * %-E2BIG if the payload was larger than the Write chunk
  957. */
  958. int svc_rdma_result_payload(struct svc_rqst *rqstp, unsigned int offset,
  959. unsigned int length)
  960. {
  961. struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
  962. struct svc_rdma_chunk *chunk;
  963. chunk = rctxt->rc_cur_result_payload;
  964. if (!length || !chunk)
  965. return 0;
  966. rctxt->rc_cur_result_payload =
  967. pcl_next_chunk(&rctxt->rc_write_pcl, chunk);
  968. if (length > chunk->ch_length)
  969. return -E2BIG;
  970. chunk->ch_position = offset;
  971. chunk->ch_payload_length = length;
  972. return 0;
  973. }