svc_rdma_rw.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016-2018 Oracle. All rights reserved.
  4. *
  5. * Use the core R/W API to move RPC-over-RDMA Read and Write chunks.
  6. */
  7. #include <rdma/rw.h>
  8. #include <linux/sunrpc/xdr.h>
  9. #include <linux/sunrpc/rpc_rdma.h>
  10. #include <linux/sunrpc/svc_rdma.h>
  11. #include "xprt_rdma.h"
  12. #include <trace/events/rpcrdma.h>
  13. static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc);
  14. static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc);
  15. /* Each R/W context contains state for one chain of RDMA Read or
  16. * Write Work Requests.
  17. *
  18. * Each WR chain handles a single contiguous server-side buffer,
  19. * because scatterlist entries after the first have to start on
  20. * page alignment. xdr_buf iovecs cannot guarantee alignment.
  21. *
  22. * Each WR chain handles only one R_key. Each RPC-over-RDMA segment
  23. * from a client may contain a unique R_key, so each WR chain moves
  24. * up to one segment at a time.
  25. *
  26. * The scatterlist makes this data structure over 4KB in size. To
  27. * make it less likely to fail, and to handle the allocation for
  28. * smaller I/O requests without disabling bottom-halves, these
  29. * contexts are created on demand, but cached and reused until the
  30. * controlling svcxprt_rdma is destroyed.
  31. */
  32. struct svc_rdma_rw_ctxt {
  33. struct llist_node rw_node;
  34. struct list_head rw_list;
  35. struct rdma_rw_ctx rw_ctx;
  36. unsigned int rw_nents;
  37. unsigned int rw_first_sgl_nents;
  38. struct sg_table rw_sg_table;
  39. struct scatterlist rw_first_sgl[];
  40. };
  41. static inline struct svc_rdma_rw_ctxt *
  42. svc_rdma_next_ctxt(struct list_head *list)
  43. {
  44. return list_first_entry_or_null(list, struct svc_rdma_rw_ctxt,
  45. rw_list);
  46. }
  47. static struct svc_rdma_rw_ctxt *
  48. svc_rdma_get_rw_ctxt(struct svcxprt_rdma *rdma, unsigned int sges)
  49. {
  50. struct ib_device *dev = rdma->sc_cm_id->device;
  51. unsigned int first_sgl_nents = dev->attrs.max_send_sge;
  52. struct svc_rdma_rw_ctxt *ctxt;
  53. struct llist_node *node;
  54. spin_lock(&rdma->sc_rw_ctxt_lock);
  55. node = llist_del_first(&rdma->sc_rw_ctxts);
  56. spin_unlock(&rdma->sc_rw_ctxt_lock);
  57. if (node) {
  58. ctxt = llist_entry(node, struct svc_rdma_rw_ctxt, rw_node);
  59. } else {
  60. ctxt = kmalloc_node(struct_size(ctxt, rw_first_sgl, first_sgl_nents),
  61. GFP_KERNEL, ibdev_to_node(dev));
  62. if (!ctxt)
  63. goto out_noctx;
  64. INIT_LIST_HEAD(&ctxt->rw_list);
  65. ctxt->rw_first_sgl_nents = first_sgl_nents;
  66. }
  67. ctxt->rw_sg_table.sgl = ctxt->rw_first_sgl;
  68. if (sg_alloc_table_chained(&ctxt->rw_sg_table, sges,
  69. ctxt->rw_sg_table.sgl,
  70. first_sgl_nents))
  71. goto out_free;
  72. return ctxt;
  73. out_free:
  74. kfree(ctxt);
  75. out_noctx:
  76. trace_svcrdma_rwctx_empty(rdma, sges);
  77. return NULL;
  78. }
  79. static void __svc_rdma_put_rw_ctxt(struct svc_rdma_rw_ctxt *ctxt,
  80. struct llist_head *list)
  81. {
  82. sg_free_table_chained(&ctxt->rw_sg_table, ctxt->rw_first_sgl_nents);
  83. llist_add(&ctxt->rw_node, list);
  84. }
  85. static void svc_rdma_put_rw_ctxt(struct svcxprt_rdma *rdma,
  86. struct svc_rdma_rw_ctxt *ctxt)
  87. {
  88. __svc_rdma_put_rw_ctxt(ctxt, &rdma->sc_rw_ctxts);
  89. }
  90. /**
  91. * svc_rdma_destroy_rw_ctxts - Free accumulated R/W contexts
  92. * @rdma: transport about to be destroyed
  93. *
  94. */
  95. void svc_rdma_destroy_rw_ctxts(struct svcxprt_rdma *rdma)
  96. {
  97. struct svc_rdma_rw_ctxt *ctxt;
  98. struct llist_node *node;
  99. while ((node = llist_del_first(&rdma->sc_rw_ctxts)) != NULL) {
  100. ctxt = llist_entry(node, struct svc_rdma_rw_ctxt, rw_node);
  101. kfree(ctxt);
  102. }
  103. }
  104. /**
  105. * svc_rdma_rw_ctx_init - Prepare a R/W context for I/O
  106. * @rdma: controlling transport instance
  107. * @ctxt: R/W context to prepare
  108. * @offset: RDMA offset
  109. * @handle: RDMA tag/handle
  110. * @direction: I/O direction
  111. *
  112. * Returns on success, the number of WQEs that will be needed
  113. * on the workqueue, or a negative errno.
  114. */
  115. static int svc_rdma_rw_ctx_init(struct svcxprt_rdma *rdma,
  116. struct svc_rdma_rw_ctxt *ctxt,
  117. u64 offset, u32 handle,
  118. enum dma_data_direction direction)
  119. {
  120. int ret;
  121. ret = rdma_rw_ctx_init(&ctxt->rw_ctx, rdma->sc_qp, rdma->sc_port_num,
  122. ctxt->rw_sg_table.sgl, ctxt->rw_nents,
  123. 0, offset, handle, direction);
  124. if (unlikely(ret < 0)) {
  125. trace_svcrdma_dma_map_rw_err(rdma, offset, handle,
  126. ctxt->rw_nents, ret);
  127. svc_rdma_put_rw_ctxt(rdma, ctxt);
  128. }
  129. return ret;
  130. }
  131. /**
  132. * svc_rdma_cc_init - Initialize an svc_rdma_chunk_ctxt
  133. * @rdma: controlling transport instance
  134. * @cc: svc_rdma_chunk_ctxt to be initialized
  135. */
  136. void svc_rdma_cc_init(struct svcxprt_rdma *rdma,
  137. struct svc_rdma_chunk_ctxt *cc)
  138. {
  139. struct rpc_rdma_cid *cid = &cc->cc_cid;
  140. if (unlikely(!cid->ci_completion_id))
  141. svc_rdma_send_cid_init(rdma, cid);
  142. INIT_LIST_HEAD(&cc->cc_rwctxts);
  143. cc->cc_sqecount = 0;
  144. }
  145. /**
  146. * svc_rdma_cc_release - Release resources held by a svc_rdma_chunk_ctxt
  147. * @rdma: controlling transport instance
  148. * @cc: svc_rdma_chunk_ctxt to be released
  149. * @dir: DMA direction
  150. */
  151. void svc_rdma_cc_release(struct svcxprt_rdma *rdma,
  152. struct svc_rdma_chunk_ctxt *cc,
  153. enum dma_data_direction dir)
  154. {
  155. struct llist_node *first, *last;
  156. struct svc_rdma_rw_ctxt *ctxt;
  157. LLIST_HEAD(free);
  158. trace_svcrdma_cc_release(&cc->cc_cid, cc->cc_sqecount);
  159. first = last = NULL;
  160. while ((ctxt = svc_rdma_next_ctxt(&cc->cc_rwctxts)) != NULL) {
  161. list_del(&ctxt->rw_list);
  162. rdma_rw_ctx_destroy(&ctxt->rw_ctx, rdma->sc_qp,
  163. rdma->sc_port_num, ctxt->rw_sg_table.sgl,
  164. ctxt->rw_nents, dir);
  165. __svc_rdma_put_rw_ctxt(ctxt, &free);
  166. ctxt->rw_node.next = first;
  167. first = &ctxt->rw_node;
  168. if (!last)
  169. last = first;
  170. }
  171. if (first)
  172. llist_add_batch(first, last, &rdma->sc_rw_ctxts);
  173. }
  174. static struct svc_rdma_write_info *
  175. svc_rdma_write_info_alloc(struct svcxprt_rdma *rdma,
  176. const struct svc_rdma_chunk *chunk)
  177. {
  178. struct svc_rdma_write_info *info;
  179. info = kzalloc_node(sizeof(*info), GFP_KERNEL,
  180. ibdev_to_node(rdma->sc_cm_id->device));
  181. if (!info)
  182. return info;
  183. info->wi_rdma = rdma;
  184. info->wi_chunk = chunk;
  185. svc_rdma_cc_init(rdma, &info->wi_cc);
  186. info->wi_cc.cc_cqe.done = svc_rdma_write_done;
  187. return info;
  188. }
  189. static void svc_rdma_write_info_free_async(struct work_struct *work)
  190. {
  191. struct svc_rdma_write_info *info;
  192. info = container_of(work, struct svc_rdma_write_info, wi_work);
  193. svc_rdma_cc_release(info->wi_rdma, &info->wi_cc, DMA_TO_DEVICE);
  194. kfree(info);
  195. }
  196. static void svc_rdma_write_info_free(struct svc_rdma_write_info *info)
  197. {
  198. INIT_WORK(&info->wi_work, svc_rdma_write_info_free_async);
  199. queue_work(svcrdma_wq, &info->wi_work);
  200. }
  201. /**
  202. * svc_rdma_reply_chunk_release - Release Reply chunk I/O resources
  203. * @rdma: controlling transport
  204. * @ctxt: Send context that is being released
  205. */
  206. void svc_rdma_reply_chunk_release(struct svcxprt_rdma *rdma,
  207. struct svc_rdma_send_ctxt *ctxt)
  208. {
  209. struct svc_rdma_chunk_ctxt *cc = &ctxt->sc_reply_info.wi_cc;
  210. if (!cc->cc_sqecount)
  211. return;
  212. svc_rdma_cc_release(rdma, cc, DMA_TO_DEVICE);
  213. }
  214. /**
  215. * svc_rdma_reply_done - Reply chunk Write completion handler
  216. * @cq: controlling Completion Queue
  217. * @wc: Work Completion report
  218. *
  219. * Pages under I/O are released by a subsequent Send completion.
  220. */
  221. static void svc_rdma_reply_done(struct ib_cq *cq, struct ib_wc *wc)
  222. {
  223. struct ib_cqe *cqe = wc->wr_cqe;
  224. struct svc_rdma_chunk_ctxt *cc =
  225. container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
  226. struct svcxprt_rdma *rdma = cq->cq_context;
  227. switch (wc->status) {
  228. case IB_WC_SUCCESS:
  229. trace_svcrdma_wc_reply(&cc->cc_cid);
  230. return;
  231. case IB_WC_WR_FLUSH_ERR:
  232. trace_svcrdma_wc_reply_flush(wc, &cc->cc_cid);
  233. break;
  234. default:
  235. trace_svcrdma_wc_reply_err(wc, &cc->cc_cid);
  236. }
  237. svc_xprt_deferred_close(&rdma->sc_xprt);
  238. }
  239. /**
  240. * svc_rdma_write_done - Write chunk completion
  241. * @cq: controlling Completion Queue
  242. * @wc: Work Completion
  243. *
  244. * Pages under I/O are freed by a subsequent Send completion.
  245. */
  246. static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
  247. {
  248. struct svcxprt_rdma *rdma = cq->cq_context;
  249. struct ib_cqe *cqe = wc->wr_cqe;
  250. struct svc_rdma_chunk_ctxt *cc =
  251. container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
  252. struct svc_rdma_write_info *info =
  253. container_of(cc, struct svc_rdma_write_info, wi_cc);
  254. switch (wc->status) {
  255. case IB_WC_SUCCESS:
  256. trace_svcrdma_wc_write(&cc->cc_cid);
  257. break;
  258. case IB_WC_WR_FLUSH_ERR:
  259. trace_svcrdma_wc_write_flush(wc, &cc->cc_cid);
  260. break;
  261. default:
  262. trace_svcrdma_wc_write_err(wc, &cc->cc_cid);
  263. }
  264. svc_rdma_wake_send_waiters(rdma, cc->cc_sqecount);
  265. if (unlikely(wc->status != IB_WC_SUCCESS))
  266. svc_xprt_deferred_close(&rdma->sc_xprt);
  267. svc_rdma_write_info_free(info);
  268. }
  269. /**
  270. * svc_rdma_wc_read_done - Handle completion of an RDMA Read ctx
  271. * @cq: controlling Completion Queue
  272. * @wc: Work Completion
  273. *
  274. */
  275. static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc)
  276. {
  277. struct svcxprt_rdma *rdma = cq->cq_context;
  278. struct ib_cqe *cqe = wc->wr_cqe;
  279. struct svc_rdma_chunk_ctxt *cc =
  280. container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
  281. struct svc_rdma_recv_ctxt *ctxt;
  282. svc_rdma_wake_send_waiters(rdma, cc->cc_sqecount);
  283. ctxt = container_of(cc, struct svc_rdma_recv_ctxt, rc_cc);
  284. switch (wc->status) {
  285. case IB_WC_SUCCESS:
  286. trace_svcrdma_wc_read(wc, &cc->cc_cid, ctxt->rc_readbytes,
  287. cc->cc_posttime);
  288. spin_lock(&rdma->sc_rq_dto_lock);
  289. list_add_tail(&ctxt->rc_list, &rdma->sc_read_complete_q);
  290. /* the unlock pairs with the smp_rmb in svc_xprt_ready */
  291. set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
  292. spin_unlock(&rdma->sc_rq_dto_lock);
  293. svc_xprt_enqueue(&rdma->sc_xprt);
  294. return;
  295. case IB_WC_WR_FLUSH_ERR:
  296. trace_svcrdma_wc_read_flush(wc, &cc->cc_cid);
  297. break;
  298. default:
  299. trace_svcrdma_wc_read_err(wc, &cc->cc_cid);
  300. }
  301. /* The RDMA Read has flushed, so the incoming RPC message
  302. * cannot be constructed and must be dropped. Signal the
  303. * loss to the client by closing the connection.
  304. */
  305. svc_rdma_cc_release(rdma, cc, DMA_FROM_DEVICE);
  306. svc_rdma_recv_ctxt_put(rdma, ctxt);
  307. svc_xprt_deferred_close(&rdma->sc_xprt);
  308. }
  309. /*
  310. * Assumptions:
  311. * - If ib_post_send() succeeds, only one completion is expected,
  312. * even if one or more WRs are flushed. This is true when posting
  313. * an rdma_rw_ctx or when posting a single signaled WR.
  314. */
  315. static int svc_rdma_post_chunk_ctxt(struct svcxprt_rdma *rdma,
  316. struct svc_rdma_chunk_ctxt *cc)
  317. {
  318. struct ib_send_wr *first_wr;
  319. const struct ib_send_wr *bad_wr;
  320. struct list_head *tmp;
  321. struct ib_cqe *cqe;
  322. int ret;
  323. might_sleep();
  324. if (cc->cc_sqecount > rdma->sc_sq_depth)
  325. return -EINVAL;
  326. first_wr = NULL;
  327. cqe = &cc->cc_cqe;
  328. list_for_each(tmp, &cc->cc_rwctxts) {
  329. struct svc_rdma_rw_ctxt *ctxt;
  330. ctxt = list_entry(tmp, struct svc_rdma_rw_ctxt, rw_list);
  331. first_wr = rdma_rw_ctx_wrs(&ctxt->rw_ctx, rdma->sc_qp,
  332. rdma->sc_port_num, cqe, first_wr);
  333. cqe = NULL;
  334. }
  335. do {
  336. if (atomic_sub_return(cc->cc_sqecount,
  337. &rdma->sc_sq_avail) > 0) {
  338. cc->cc_posttime = ktime_get();
  339. ret = ib_post_send(rdma->sc_qp, first_wr, &bad_wr);
  340. if (ret)
  341. break;
  342. return 0;
  343. }
  344. percpu_counter_inc(&svcrdma_stat_sq_starve);
  345. trace_svcrdma_sq_full(rdma, &cc->cc_cid);
  346. atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
  347. wait_event(rdma->sc_send_wait,
  348. atomic_read(&rdma->sc_sq_avail) > cc->cc_sqecount);
  349. trace_svcrdma_sq_retry(rdma, &cc->cc_cid);
  350. } while (1);
  351. trace_svcrdma_sq_post_err(rdma, &cc->cc_cid, ret);
  352. svc_xprt_deferred_close(&rdma->sc_xprt);
  353. /* If even one was posted, there will be a completion. */
  354. if (bad_wr != first_wr)
  355. return 0;
  356. atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
  357. wake_up(&rdma->sc_send_wait);
  358. return -ENOTCONN;
  359. }
  360. /* Build and DMA-map an SGL that covers one kvec in an xdr_buf
  361. */
  362. static void svc_rdma_vec_to_sg(struct svc_rdma_write_info *info,
  363. unsigned int len,
  364. struct svc_rdma_rw_ctxt *ctxt)
  365. {
  366. struct scatterlist *sg = ctxt->rw_sg_table.sgl;
  367. sg_set_buf(&sg[0], info->wi_base, len);
  368. info->wi_base += len;
  369. ctxt->rw_nents = 1;
  370. }
  371. /* Build and DMA-map an SGL that covers part of an xdr_buf's pagelist.
  372. */
  373. static void svc_rdma_pagelist_to_sg(struct svc_rdma_write_info *info,
  374. unsigned int remaining,
  375. struct svc_rdma_rw_ctxt *ctxt)
  376. {
  377. unsigned int sge_no, sge_bytes, page_off, page_no;
  378. const struct xdr_buf *xdr = info->wi_xdr;
  379. struct scatterlist *sg;
  380. struct page **page;
  381. page_off = info->wi_next_off + xdr->page_base;
  382. page_no = page_off >> PAGE_SHIFT;
  383. page_off = offset_in_page(page_off);
  384. page = xdr->pages + page_no;
  385. info->wi_next_off += remaining;
  386. sg = ctxt->rw_sg_table.sgl;
  387. sge_no = 0;
  388. do {
  389. sge_bytes = min_t(unsigned int, remaining,
  390. PAGE_SIZE - page_off);
  391. sg_set_page(sg, *page, sge_bytes, page_off);
  392. remaining -= sge_bytes;
  393. sg = sg_next(sg);
  394. page_off = 0;
  395. sge_no++;
  396. page++;
  397. } while (remaining);
  398. ctxt->rw_nents = sge_no;
  399. }
  400. /* Construct RDMA Write WRs to send a portion of an xdr_buf containing
  401. * an RPC Reply.
  402. */
  403. static int
  404. svc_rdma_build_writes(struct svc_rdma_write_info *info,
  405. void (*constructor)(struct svc_rdma_write_info *info,
  406. unsigned int len,
  407. struct svc_rdma_rw_ctxt *ctxt),
  408. unsigned int remaining)
  409. {
  410. struct svc_rdma_chunk_ctxt *cc = &info->wi_cc;
  411. struct svcxprt_rdma *rdma = info->wi_rdma;
  412. const struct svc_rdma_segment *seg;
  413. struct svc_rdma_rw_ctxt *ctxt;
  414. int ret;
  415. do {
  416. unsigned int write_len;
  417. u64 offset;
  418. if (info->wi_seg_no >= info->wi_chunk->ch_segcount)
  419. goto out_overflow;
  420. seg = &info->wi_chunk->ch_segments[info->wi_seg_no];
  421. write_len = min(remaining, seg->rs_length - info->wi_seg_off);
  422. if (!write_len)
  423. goto out_overflow;
  424. ctxt = svc_rdma_get_rw_ctxt(rdma,
  425. (write_len >> PAGE_SHIFT) + 2);
  426. if (!ctxt)
  427. return -ENOMEM;
  428. constructor(info, write_len, ctxt);
  429. offset = seg->rs_offset + info->wi_seg_off;
  430. ret = svc_rdma_rw_ctx_init(rdma, ctxt, offset, seg->rs_handle,
  431. DMA_TO_DEVICE);
  432. if (ret < 0)
  433. return -EIO;
  434. percpu_counter_inc(&svcrdma_stat_write);
  435. list_add(&ctxt->rw_list, &cc->cc_rwctxts);
  436. cc->cc_sqecount += ret;
  437. if (write_len == seg->rs_length - info->wi_seg_off) {
  438. info->wi_seg_no++;
  439. info->wi_seg_off = 0;
  440. } else {
  441. info->wi_seg_off += write_len;
  442. }
  443. remaining -= write_len;
  444. } while (remaining);
  445. return 0;
  446. out_overflow:
  447. trace_svcrdma_small_wrch_err(&cc->cc_cid, remaining, info->wi_seg_no,
  448. info->wi_chunk->ch_segcount);
  449. return -E2BIG;
  450. }
  451. /**
  452. * svc_rdma_iov_write - Construct RDMA Writes from an iov
  453. * @info: pointer to write arguments
  454. * @iov: kvec to write
  455. *
  456. * Returns:
  457. * On success, returns zero
  458. * %-E2BIG if the client-provided Write chunk is too small
  459. * %-ENOMEM if a resource has been exhausted
  460. * %-EIO if an rdma-rw error occurred
  461. */
  462. static int svc_rdma_iov_write(struct svc_rdma_write_info *info,
  463. const struct kvec *iov)
  464. {
  465. info->wi_base = iov->iov_base;
  466. return svc_rdma_build_writes(info, svc_rdma_vec_to_sg,
  467. iov->iov_len);
  468. }
  469. /**
  470. * svc_rdma_pages_write - Construct RDMA Writes from pages
  471. * @info: pointer to write arguments
  472. * @xdr: xdr_buf with pages to write
  473. * @offset: offset into the content of @xdr
  474. * @length: number of bytes to write
  475. *
  476. * Returns:
  477. * On success, returns zero
  478. * %-E2BIG if the client-provided Write chunk is too small
  479. * %-ENOMEM if a resource has been exhausted
  480. * %-EIO if an rdma-rw error occurred
  481. */
  482. static int svc_rdma_pages_write(struct svc_rdma_write_info *info,
  483. const struct xdr_buf *xdr,
  484. unsigned int offset,
  485. unsigned long length)
  486. {
  487. info->wi_xdr = xdr;
  488. info->wi_next_off = offset - xdr->head[0].iov_len;
  489. return svc_rdma_build_writes(info, svc_rdma_pagelist_to_sg,
  490. length);
  491. }
  492. /**
  493. * svc_rdma_xb_write - Construct RDMA Writes to write an xdr_buf
  494. * @xdr: xdr_buf to write
  495. * @data: pointer to write arguments
  496. *
  497. * Returns:
  498. * On success, returns zero
  499. * %-E2BIG if the client-provided Write chunk is too small
  500. * %-ENOMEM if a resource has been exhausted
  501. * %-EIO if an rdma-rw error occurred
  502. */
  503. static int svc_rdma_xb_write(const struct xdr_buf *xdr, void *data)
  504. {
  505. struct svc_rdma_write_info *info = data;
  506. int ret;
  507. if (xdr->head[0].iov_len) {
  508. ret = svc_rdma_iov_write(info, &xdr->head[0]);
  509. if (ret < 0)
  510. return ret;
  511. }
  512. if (xdr->page_len) {
  513. ret = svc_rdma_pages_write(info, xdr, xdr->head[0].iov_len,
  514. xdr->page_len);
  515. if (ret < 0)
  516. return ret;
  517. }
  518. if (xdr->tail[0].iov_len) {
  519. ret = svc_rdma_iov_write(info, &xdr->tail[0]);
  520. if (ret < 0)
  521. return ret;
  522. }
  523. return xdr->len;
  524. }
  525. static int svc_rdma_send_write_chunk(struct svcxprt_rdma *rdma,
  526. const struct svc_rdma_chunk *chunk,
  527. const struct xdr_buf *xdr)
  528. {
  529. struct svc_rdma_write_info *info;
  530. struct svc_rdma_chunk_ctxt *cc;
  531. struct xdr_buf payload;
  532. int ret;
  533. if (xdr_buf_subsegment(xdr, &payload, chunk->ch_position,
  534. chunk->ch_payload_length))
  535. return -EMSGSIZE;
  536. info = svc_rdma_write_info_alloc(rdma, chunk);
  537. if (!info)
  538. return -ENOMEM;
  539. cc = &info->wi_cc;
  540. ret = svc_rdma_xb_write(&payload, info);
  541. if (ret != payload.len)
  542. goto out_err;
  543. trace_svcrdma_post_write_chunk(&cc->cc_cid, cc->cc_sqecount);
  544. ret = svc_rdma_post_chunk_ctxt(rdma, cc);
  545. if (ret < 0)
  546. goto out_err;
  547. return 0;
  548. out_err:
  549. svc_rdma_write_info_free(info);
  550. return ret;
  551. }
  552. /**
  553. * svc_rdma_send_write_list - Send all chunks on the Write list
  554. * @rdma: controlling RDMA transport
  555. * @rctxt: Write list provisioned by the client
  556. * @xdr: xdr_buf containing an RPC Reply message
  557. *
  558. * Returns zero on success, or a negative errno if one or more
  559. * Write chunks could not be sent.
  560. */
  561. int svc_rdma_send_write_list(struct svcxprt_rdma *rdma,
  562. const struct svc_rdma_recv_ctxt *rctxt,
  563. const struct xdr_buf *xdr)
  564. {
  565. struct svc_rdma_chunk *chunk;
  566. int ret;
  567. pcl_for_each_chunk(chunk, &rctxt->rc_write_pcl) {
  568. if (!chunk->ch_payload_length)
  569. break;
  570. ret = svc_rdma_send_write_chunk(rdma, chunk, xdr);
  571. if (ret < 0)
  572. return ret;
  573. }
  574. return 0;
  575. }
  576. /**
  577. * svc_rdma_prepare_reply_chunk - Construct WR chain for writing the Reply chunk
  578. * @rdma: controlling RDMA transport
  579. * @write_pcl: Write chunk list provided by client
  580. * @reply_pcl: Reply chunk provided by client
  581. * @sctxt: Send WR resources
  582. * @xdr: xdr_buf containing an RPC Reply
  583. *
  584. * Returns a non-negative number of bytes the chunk consumed, or
  585. * %-E2BIG if the payload was larger than the Reply chunk,
  586. * %-EINVAL if client provided too many segments,
  587. * %-ENOMEM if rdma_rw context pool was exhausted,
  588. * %-ENOTCONN if posting failed (connection is lost),
  589. * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
  590. */
  591. int svc_rdma_prepare_reply_chunk(struct svcxprt_rdma *rdma,
  592. const struct svc_rdma_pcl *write_pcl,
  593. const struct svc_rdma_pcl *reply_pcl,
  594. struct svc_rdma_send_ctxt *sctxt,
  595. const struct xdr_buf *xdr)
  596. {
  597. struct svc_rdma_write_info *info = &sctxt->sc_reply_info;
  598. struct svc_rdma_chunk_ctxt *cc = &info->wi_cc;
  599. struct ib_send_wr *first_wr;
  600. struct list_head *pos;
  601. struct ib_cqe *cqe;
  602. int ret;
  603. info->wi_rdma = rdma;
  604. info->wi_chunk = pcl_first_chunk(reply_pcl);
  605. info->wi_seg_off = 0;
  606. info->wi_seg_no = 0;
  607. info->wi_cc.cc_cqe.done = svc_rdma_reply_done;
  608. ret = pcl_process_nonpayloads(write_pcl, xdr,
  609. svc_rdma_xb_write, info);
  610. if (ret < 0)
  611. return ret;
  612. first_wr = sctxt->sc_wr_chain;
  613. cqe = &cc->cc_cqe;
  614. list_for_each(pos, &cc->cc_rwctxts) {
  615. struct svc_rdma_rw_ctxt *rwc;
  616. rwc = list_entry(pos, struct svc_rdma_rw_ctxt, rw_list);
  617. first_wr = rdma_rw_ctx_wrs(&rwc->rw_ctx, rdma->sc_qp,
  618. rdma->sc_port_num, cqe, first_wr);
  619. cqe = NULL;
  620. }
  621. sctxt->sc_wr_chain = first_wr;
  622. sctxt->sc_sqecount += cc->cc_sqecount;
  623. trace_svcrdma_post_reply_chunk(&cc->cc_cid, cc->cc_sqecount);
  624. return xdr->len;
  625. }
  626. /**
  627. * svc_rdma_build_read_segment - Build RDMA Read WQEs to pull one RDMA segment
  628. * @rqstp: RPC transaction context
  629. * @head: context for ongoing I/O
  630. * @segment: co-ordinates of remote memory to be read
  631. *
  632. * Returns:
  633. * %0: the Read WR chain was constructed successfully
  634. * %-EINVAL: there were not enough rq_pages to finish
  635. * %-ENOMEM: allocating a local resources failed
  636. * %-EIO: a DMA mapping error occurred
  637. */
  638. static int svc_rdma_build_read_segment(struct svc_rqst *rqstp,
  639. struct svc_rdma_recv_ctxt *head,
  640. const struct svc_rdma_segment *segment)
  641. {
  642. struct svcxprt_rdma *rdma = svc_rdma_rqst_rdma(rqstp);
  643. struct svc_rdma_chunk_ctxt *cc = &head->rc_cc;
  644. unsigned int sge_no, seg_len, len;
  645. struct svc_rdma_rw_ctxt *ctxt;
  646. struct scatterlist *sg;
  647. int ret;
  648. len = segment->rs_length;
  649. sge_no = PAGE_ALIGN(head->rc_pageoff + len) >> PAGE_SHIFT;
  650. ctxt = svc_rdma_get_rw_ctxt(rdma, sge_no);
  651. if (!ctxt)
  652. return -ENOMEM;
  653. ctxt->rw_nents = sge_no;
  654. sg = ctxt->rw_sg_table.sgl;
  655. for (sge_no = 0; sge_no < ctxt->rw_nents; sge_no++) {
  656. seg_len = min_t(unsigned int, len,
  657. PAGE_SIZE - head->rc_pageoff);
  658. if (!head->rc_pageoff)
  659. head->rc_page_count++;
  660. sg_set_page(sg, rqstp->rq_pages[head->rc_curpage],
  661. seg_len, head->rc_pageoff);
  662. sg = sg_next(sg);
  663. head->rc_pageoff += seg_len;
  664. if (head->rc_pageoff == PAGE_SIZE) {
  665. head->rc_curpage++;
  666. head->rc_pageoff = 0;
  667. }
  668. len -= seg_len;
  669. if (len && ((head->rc_curpage + 1) > ARRAY_SIZE(rqstp->rq_pages)))
  670. goto out_overrun;
  671. }
  672. ret = svc_rdma_rw_ctx_init(rdma, ctxt, segment->rs_offset,
  673. segment->rs_handle, DMA_FROM_DEVICE);
  674. if (ret < 0)
  675. return -EIO;
  676. percpu_counter_inc(&svcrdma_stat_read);
  677. list_add(&ctxt->rw_list, &cc->cc_rwctxts);
  678. cc->cc_sqecount += ret;
  679. return 0;
  680. out_overrun:
  681. trace_svcrdma_page_overrun_err(&cc->cc_cid, head->rc_curpage);
  682. return -EINVAL;
  683. }
  684. /**
  685. * svc_rdma_build_read_chunk - Build RDMA Read WQEs to pull one RDMA chunk
  686. * @rqstp: RPC transaction context
  687. * @head: context for ongoing I/O
  688. * @chunk: Read chunk to pull
  689. *
  690. * Return values:
  691. * %0: the Read WR chain was constructed successfully
  692. * %-EINVAL: there were not enough resources to finish
  693. * %-ENOMEM: allocating a local resources failed
  694. * %-EIO: a DMA mapping error occurred
  695. */
  696. static int svc_rdma_build_read_chunk(struct svc_rqst *rqstp,
  697. struct svc_rdma_recv_ctxt *head,
  698. const struct svc_rdma_chunk *chunk)
  699. {
  700. const struct svc_rdma_segment *segment;
  701. int ret;
  702. ret = -EINVAL;
  703. pcl_for_each_segment(segment, chunk) {
  704. ret = svc_rdma_build_read_segment(rqstp, head, segment);
  705. if (ret < 0)
  706. break;
  707. head->rc_readbytes += segment->rs_length;
  708. }
  709. return ret;
  710. }
  711. /**
  712. * svc_rdma_copy_inline_range - Copy part of the inline content into pages
  713. * @rqstp: RPC transaction context
  714. * @head: context for ongoing I/O
  715. * @offset: offset into the Receive buffer of region to copy
  716. * @remaining: length of region to copy
  717. *
  718. * Take a page at a time from rqstp->rq_pages and copy the inline
  719. * content from the Receive buffer into that page. Update
  720. * head->rc_curpage and head->rc_pageoff so that the next RDMA Read
  721. * result will land contiguously with the copied content.
  722. *
  723. * Return values:
  724. * %0: Inline content was successfully copied
  725. * %-EINVAL: offset or length was incorrect
  726. */
  727. static int svc_rdma_copy_inline_range(struct svc_rqst *rqstp,
  728. struct svc_rdma_recv_ctxt *head,
  729. unsigned int offset,
  730. unsigned int remaining)
  731. {
  732. unsigned char *dst, *src = head->rc_recv_buf;
  733. unsigned int page_no, numpages;
  734. numpages = PAGE_ALIGN(head->rc_pageoff + remaining) >> PAGE_SHIFT;
  735. for (page_no = 0; page_no < numpages; page_no++) {
  736. unsigned int page_len;
  737. page_len = min_t(unsigned int, remaining,
  738. PAGE_SIZE - head->rc_pageoff);
  739. if (!head->rc_pageoff)
  740. head->rc_page_count++;
  741. dst = page_address(rqstp->rq_pages[head->rc_curpage]);
  742. memcpy(dst + head->rc_curpage, src + offset, page_len);
  743. head->rc_readbytes += page_len;
  744. head->rc_pageoff += page_len;
  745. if (head->rc_pageoff == PAGE_SIZE) {
  746. head->rc_curpage++;
  747. head->rc_pageoff = 0;
  748. }
  749. remaining -= page_len;
  750. offset += page_len;
  751. }
  752. return -EINVAL;
  753. }
  754. /**
  755. * svc_rdma_read_multiple_chunks - Construct RDMA Reads to pull data item Read chunks
  756. * @rqstp: RPC transaction context
  757. * @head: context for ongoing I/O
  758. *
  759. * The chunk data lands in rqstp->rq_arg as a series of contiguous pages,
  760. * like an incoming TCP call.
  761. *
  762. * Return values:
  763. * %0: RDMA Read WQEs were successfully built
  764. * %-EINVAL: client provided too many chunks or segments,
  765. * %-ENOMEM: rdma_rw context pool was exhausted,
  766. * %-ENOTCONN: posting failed (connection is lost),
  767. * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
  768. */
  769. static noinline int
  770. svc_rdma_read_multiple_chunks(struct svc_rqst *rqstp,
  771. struct svc_rdma_recv_ctxt *head)
  772. {
  773. const struct svc_rdma_pcl *pcl = &head->rc_read_pcl;
  774. struct svc_rdma_chunk *chunk, *next;
  775. unsigned int start, length;
  776. int ret;
  777. start = 0;
  778. chunk = pcl_first_chunk(pcl);
  779. length = chunk->ch_position;
  780. ret = svc_rdma_copy_inline_range(rqstp, head, start, length);
  781. if (ret < 0)
  782. return ret;
  783. pcl_for_each_chunk(chunk, pcl) {
  784. ret = svc_rdma_build_read_chunk(rqstp, head, chunk);
  785. if (ret < 0)
  786. return ret;
  787. next = pcl_next_chunk(pcl, chunk);
  788. if (!next)
  789. break;
  790. start += length;
  791. length = next->ch_position - head->rc_readbytes;
  792. ret = svc_rdma_copy_inline_range(rqstp, head, start, length);
  793. if (ret < 0)
  794. return ret;
  795. }
  796. start += length;
  797. length = head->rc_byte_len - start;
  798. return svc_rdma_copy_inline_range(rqstp, head, start, length);
  799. }
  800. /**
  801. * svc_rdma_read_data_item - Construct RDMA Reads to pull data item Read chunks
  802. * @rqstp: RPC transaction context
  803. * @head: context for ongoing I/O
  804. *
  805. * The chunk data lands in the page list of rqstp->rq_arg.pages.
  806. *
  807. * Currently NFSD does not look at the rqstp->rq_arg.tail[0] kvec.
  808. * Therefore, XDR round-up of the Read chunk and trailing
  809. * inline content must both be added at the end of the pagelist.
  810. *
  811. * Return values:
  812. * %0: RDMA Read WQEs were successfully built
  813. * %-EINVAL: client provided too many chunks or segments,
  814. * %-ENOMEM: rdma_rw context pool was exhausted,
  815. * %-ENOTCONN: posting failed (connection is lost),
  816. * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
  817. */
  818. static int svc_rdma_read_data_item(struct svc_rqst *rqstp,
  819. struct svc_rdma_recv_ctxt *head)
  820. {
  821. return svc_rdma_build_read_chunk(rqstp, head,
  822. pcl_first_chunk(&head->rc_read_pcl));
  823. }
  824. /**
  825. * svc_rdma_read_chunk_range - Build RDMA Read WRs for portion of a chunk
  826. * @rqstp: RPC transaction context
  827. * @head: context for ongoing I/O
  828. * @chunk: parsed Call chunk to pull
  829. * @offset: offset of region to pull
  830. * @length: length of region to pull
  831. *
  832. * Return values:
  833. * %0: RDMA Read WQEs were successfully built
  834. * %-EINVAL: there were not enough resources to finish
  835. * %-ENOMEM: rdma_rw context pool was exhausted,
  836. * %-ENOTCONN: posting failed (connection is lost),
  837. * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
  838. */
  839. static int svc_rdma_read_chunk_range(struct svc_rqst *rqstp,
  840. struct svc_rdma_recv_ctxt *head,
  841. const struct svc_rdma_chunk *chunk,
  842. unsigned int offset, unsigned int length)
  843. {
  844. const struct svc_rdma_segment *segment;
  845. int ret;
  846. ret = -EINVAL;
  847. pcl_for_each_segment(segment, chunk) {
  848. struct svc_rdma_segment dummy;
  849. if (offset > segment->rs_length) {
  850. offset -= segment->rs_length;
  851. continue;
  852. }
  853. dummy.rs_handle = segment->rs_handle;
  854. dummy.rs_length = min_t(u32, length, segment->rs_length) - offset;
  855. dummy.rs_offset = segment->rs_offset + offset;
  856. ret = svc_rdma_build_read_segment(rqstp, head, &dummy);
  857. if (ret < 0)
  858. break;
  859. head->rc_readbytes += dummy.rs_length;
  860. length -= dummy.rs_length;
  861. offset = 0;
  862. }
  863. return ret;
  864. }
  865. /**
  866. * svc_rdma_read_call_chunk - Build RDMA Read WQEs to pull a Long Message
  867. * @rqstp: RPC transaction context
  868. * @head: context for ongoing I/O
  869. *
  870. * Return values:
  871. * %0: RDMA Read WQEs were successfully built
  872. * %-EINVAL: there were not enough resources to finish
  873. * %-ENOMEM: rdma_rw context pool was exhausted,
  874. * %-ENOTCONN: posting failed (connection is lost),
  875. * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
  876. */
  877. static int svc_rdma_read_call_chunk(struct svc_rqst *rqstp,
  878. struct svc_rdma_recv_ctxt *head)
  879. {
  880. const struct svc_rdma_chunk *call_chunk =
  881. pcl_first_chunk(&head->rc_call_pcl);
  882. const struct svc_rdma_pcl *pcl = &head->rc_read_pcl;
  883. struct svc_rdma_chunk *chunk, *next;
  884. unsigned int start, length;
  885. int ret;
  886. if (pcl_is_empty(pcl))
  887. return svc_rdma_build_read_chunk(rqstp, head, call_chunk);
  888. start = 0;
  889. chunk = pcl_first_chunk(pcl);
  890. length = chunk->ch_position;
  891. ret = svc_rdma_read_chunk_range(rqstp, head, call_chunk,
  892. start, length);
  893. if (ret < 0)
  894. return ret;
  895. pcl_for_each_chunk(chunk, pcl) {
  896. ret = svc_rdma_build_read_chunk(rqstp, head, chunk);
  897. if (ret < 0)
  898. return ret;
  899. next = pcl_next_chunk(pcl, chunk);
  900. if (!next)
  901. break;
  902. start += length;
  903. length = next->ch_position - head->rc_readbytes;
  904. ret = svc_rdma_read_chunk_range(rqstp, head, call_chunk,
  905. start, length);
  906. if (ret < 0)
  907. return ret;
  908. }
  909. start += length;
  910. length = call_chunk->ch_length - start;
  911. return svc_rdma_read_chunk_range(rqstp, head, call_chunk,
  912. start, length);
  913. }
  914. /**
  915. * svc_rdma_read_special - Build RDMA Read WQEs to pull a Long Message
  916. * @rqstp: RPC transaction context
  917. * @head: context for ongoing I/O
  918. *
  919. * The start of the data lands in the first page just after the
  920. * Transport header, and the rest lands in rqstp->rq_arg.pages.
  921. *
  922. * Assumptions:
  923. * - A PZRC is never sent in an RDMA_MSG message, though it's
  924. * allowed by spec.
  925. *
  926. * Return values:
  927. * %0: RDMA Read WQEs were successfully built
  928. * %-EINVAL: client provided too many chunks or segments,
  929. * %-ENOMEM: rdma_rw context pool was exhausted,
  930. * %-ENOTCONN: posting failed (connection is lost),
  931. * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
  932. */
  933. static noinline int svc_rdma_read_special(struct svc_rqst *rqstp,
  934. struct svc_rdma_recv_ctxt *head)
  935. {
  936. return svc_rdma_read_call_chunk(rqstp, head);
  937. }
  938. /* Pages under I/O have been copied to head->rc_pages. Ensure that
  939. * svc_xprt_release() does not put them when svc_rdma_recvfrom()
  940. * returns. This has to be done after all Read WRs are constructed
  941. * to properly handle a page that happens to be part of I/O on behalf
  942. * of two different RDMA segments.
  943. *
  944. * Note: if the subsequent post_send fails, these pages have already
  945. * been moved to head->rc_pages and thus will be cleaned up by
  946. * svc_rdma_recv_ctxt_put().
  947. */
  948. static void svc_rdma_clear_rqst_pages(struct svc_rqst *rqstp,
  949. struct svc_rdma_recv_ctxt *head)
  950. {
  951. unsigned int i;
  952. for (i = 0; i < head->rc_page_count; i++) {
  953. head->rc_pages[i] = rqstp->rq_pages[i];
  954. rqstp->rq_pages[i] = NULL;
  955. }
  956. }
  957. /**
  958. * svc_rdma_process_read_list - Pull list of Read chunks from the client
  959. * @rdma: controlling RDMA transport
  960. * @rqstp: set of pages to use as Read sink buffers
  961. * @head: pages under I/O collect here
  962. *
  963. * The RPC/RDMA protocol assumes that the upper layer's XDR decoders
  964. * pull each Read chunk as they decode an incoming RPC message.
  965. *
  966. * On Linux, however, the server needs to have a fully-constructed RPC
  967. * message in rqstp->rq_arg when there is a positive return code from
  968. * ->xpo_recvfrom. So the Read list is safety-checked immediately when
  969. * it is received, then here the whole Read list is pulled all at once.
  970. * The ingress RPC message is fully reconstructed once all associated
  971. * RDMA Reads have completed.
  972. *
  973. * Return values:
  974. * %1: all needed RDMA Reads were posted successfully,
  975. * %-EINVAL: client provided too many chunks or segments,
  976. * %-ENOMEM: rdma_rw context pool was exhausted,
  977. * %-ENOTCONN: posting failed (connection is lost),
  978. * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
  979. */
  980. int svc_rdma_process_read_list(struct svcxprt_rdma *rdma,
  981. struct svc_rqst *rqstp,
  982. struct svc_rdma_recv_ctxt *head)
  983. {
  984. struct svc_rdma_chunk_ctxt *cc = &head->rc_cc;
  985. int ret;
  986. cc->cc_cqe.done = svc_rdma_wc_read_done;
  987. cc->cc_sqecount = 0;
  988. head->rc_pageoff = 0;
  989. head->rc_curpage = 0;
  990. head->rc_readbytes = 0;
  991. if (pcl_is_empty(&head->rc_call_pcl)) {
  992. if (head->rc_read_pcl.cl_count == 1)
  993. ret = svc_rdma_read_data_item(rqstp, head);
  994. else
  995. ret = svc_rdma_read_multiple_chunks(rqstp, head);
  996. } else
  997. ret = svc_rdma_read_special(rqstp, head);
  998. svc_rdma_clear_rqst_pages(rqstp, head);
  999. if (ret < 0)
  1000. return ret;
  1001. trace_svcrdma_post_read_chunk(&cc->cc_cid, cc->cc_sqecount);
  1002. ret = svc_rdma_post_chunk_ctxt(rdma, cc);
  1003. return ret < 0 ? ret : 1;
  1004. }