svc_rdma_backchannel.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015-2018 Oracle. All rights reserved.
  4. *
  5. * Support for reverse-direction RPCs on RPC/RDMA (server-side).
  6. */
  7. #include <linux/sunrpc/svc_rdma.h>
  8. #include "xprt_rdma.h"
  9. #include <trace/events/rpcrdma.h>
  10. /**
  11. * svc_rdma_handle_bc_reply - Process incoming backchannel Reply
  12. * @rqstp: resources for handling the Reply
  13. * @rctxt: Received message
  14. *
  15. */
  16. void svc_rdma_handle_bc_reply(struct svc_rqst *rqstp,
  17. struct svc_rdma_recv_ctxt *rctxt)
  18. {
  19. struct svc_xprt *sxprt = rqstp->rq_xprt;
  20. struct rpc_xprt *xprt = sxprt->xpt_bc_xprt;
  21. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  22. struct xdr_buf *rcvbuf = &rqstp->rq_arg;
  23. struct kvec *dst, *src = &rcvbuf->head[0];
  24. __be32 *rdma_resp = rctxt->rc_recv_buf;
  25. struct rpc_rqst *req;
  26. u32 credits;
  27. spin_lock(&xprt->queue_lock);
  28. req = xprt_lookup_rqst(xprt, *rdma_resp);
  29. if (!req)
  30. goto out_unlock;
  31. dst = &req->rq_private_buf.head[0];
  32. memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
  33. if (dst->iov_len < src->iov_len)
  34. goto out_unlock;
  35. memcpy(dst->iov_base, src->iov_base, src->iov_len);
  36. xprt_pin_rqst(req);
  37. spin_unlock(&xprt->queue_lock);
  38. credits = be32_to_cpup(rdma_resp + 2);
  39. if (credits == 0)
  40. credits = 1; /* don't deadlock */
  41. else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
  42. credits = r_xprt->rx_buf.rb_bc_max_requests;
  43. spin_lock(&xprt->transport_lock);
  44. xprt->cwnd = credits << RPC_CWNDSHIFT;
  45. spin_unlock(&xprt->transport_lock);
  46. spin_lock(&xprt->queue_lock);
  47. xprt_complete_rqst(req->rq_task, rcvbuf->len);
  48. xprt_unpin_rqst(req);
  49. rcvbuf->len = 0;
  50. out_unlock:
  51. spin_unlock(&xprt->queue_lock);
  52. }
  53. /* Send a reverse-direction RPC Call.
  54. *
  55. * Caller holds the connection's mutex and has already marshaled
  56. * the RPC/RDMA request.
  57. *
  58. * This is similar to svc_rdma_send_reply_msg, but takes a struct
  59. * rpc_rqst instead, does not support chunks, and avoids blocking
  60. * memory allocation.
  61. *
  62. * XXX: There is still an opportunity to block in svc_rdma_send()
  63. * if there are no SQ entries to post the Send. This may occur if
  64. * the adapter has a small maximum SQ depth.
  65. */
  66. static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
  67. struct rpc_rqst *rqst,
  68. struct svc_rdma_send_ctxt *sctxt)
  69. {
  70. struct svc_rdma_recv_ctxt *rctxt;
  71. int ret;
  72. rctxt = svc_rdma_recv_ctxt_get(rdma);
  73. if (!rctxt)
  74. return -EIO;
  75. ret = svc_rdma_map_reply_msg(rdma, sctxt, rctxt, &rqst->rq_snd_buf);
  76. svc_rdma_recv_ctxt_put(rdma, rctxt);
  77. if (ret < 0)
  78. return -EIO;
  79. /* Bump page refcnt so Send completion doesn't release
  80. * the rq_buffer before all retransmits are complete.
  81. */
  82. get_page(virt_to_page(rqst->rq_buffer));
  83. sctxt->sc_send_wr.opcode = IB_WR_SEND;
  84. ret = svc_rdma_send(rdma, sctxt);
  85. if (ret < 0)
  86. return ret;
  87. ret = wait_for_completion_killable(&sctxt->sc_done);
  88. svc_rdma_send_ctxt_put(rdma, sctxt);
  89. return ret;
  90. }
  91. /* Server-side transport endpoint wants a whole page for its send
  92. * buffer. The client RPC code constructs the RPC header in this
  93. * buffer before it invokes ->send_request.
  94. */
  95. static int
  96. xprt_rdma_bc_allocate(struct rpc_task *task)
  97. {
  98. struct rpc_rqst *rqst = task->tk_rqstp;
  99. size_t size = rqst->rq_callsize;
  100. struct page *page;
  101. if (size > PAGE_SIZE) {
  102. WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
  103. size);
  104. return -EINVAL;
  105. }
  106. page = alloc_page(GFP_NOIO | __GFP_NOWARN);
  107. if (!page)
  108. return -ENOMEM;
  109. rqst->rq_buffer = page_address(page);
  110. rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, GFP_NOIO | __GFP_NOWARN);
  111. if (!rqst->rq_rbuffer) {
  112. put_page(page);
  113. return -ENOMEM;
  114. }
  115. return 0;
  116. }
  117. static void
  118. xprt_rdma_bc_free(struct rpc_task *task)
  119. {
  120. struct rpc_rqst *rqst = task->tk_rqstp;
  121. put_page(virt_to_page(rqst->rq_buffer));
  122. kfree(rqst->rq_rbuffer);
  123. }
  124. static int
  125. rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
  126. {
  127. struct rpc_xprt *xprt = rqst->rq_xprt;
  128. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  129. struct svc_rdma_send_ctxt *ctxt;
  130. __be32 *p;
  131. int rc;
  132. ctxt = svc_rdma_send_ctxt_get(rdma);
  133. if (!ctxt)
  134. goto drop_connection;
  135. p = xdr_reserve_space(&ctxt->sc_stream, RPCRDMA_HDRLEN_MIN);
  136. if (!p)
  137. goto put_ctxt;
  138. *p++ = rqst->rq_xid;
  139. *p++ = rpcrdma_version;
  140. *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
  141. *p++ = rdma_msg;
  142. *p++ = xdr_zero;
  143. *p++ = xdr_zero;
  144. *p = xdr_zero;
  145. rqst->rq_xtime = ktime_get();
  146. rc = svc_rdma_bc_sendto(rdma, rqst, ctxt);
  147. if (rc)
  148. goto put_ctxt;
  149. return 0;
  150. put_ctxt:
  151. svc_rdma_send_ctxt_put(rdma, ctxt);
  152. drop_connection:
  153. return -ENOTCONN;
  154. }
  155. /**
  156. * xprt_rdma_bc_send_request - Send a reverse-direction Call
  157. * @rqst: rpc_rqst containing Call message to be sent
  158. *
  159. * Return values:
  160. * %0 if the message was sent successfully
  161. * %ENOTCONN if the message was not sent
  162. */
  163. static int xprt_rdma_bc_send_request(struct rpc_rqst *rqst)
  164. {
  165. struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
  166. struct svcxprt_rdma *rdma =
  167. container_of(sxprt, struct svcxprt_rdma, sc_xprt);
  168. int ret;
  169. if (test_bit(XPT_DEAD, &sxprt->xpt_flags))
  170. return -ENOTCONN;
  171. ret = rpcrdma_bc_send_request(rdma, rqst);
  172. if (ret == -ENOTCONN)
  173. svc_xprt_close(sxprt);
  174. return ret;
  175. }
  176. static void
  177. xprt_rdma_bc_close(struct rpc_xprt *xprt)
  178. {
  179. xprt_disconnect_done(xprt);
  180. xprt->cwnd = RPC_CWNDSHIFT;
  181. }
  182. static void
  183. xprt_rdma_bc_put(struct rpc_xprt *xprt)
  184. {
  185. xprt_rdma_free_addresses(xprt);
  186. xprt_free(xprt);
  187. }
  188. static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
  189. .reserve_xprt = xprt_reserve_xprt_cong,
  190. .release_xprt = xprt_release_xprt_cong,
  191. .alloc_slot = xprt_alloc_slot,
  192. .free_slot = xprt_free_slot,
  193. .release_request = xprt_release_rqst_cong,
  194. .buf_alloc = xprt_rdma_bc_allocate,
  195. .buf_free = xprt_rdma_bc_free,
  196. .send_request = xprt_rdma_bc_send_request,
  197. .wait_for_reply_request = xprt_wait_for_reply_request_def,
  198. .close = xprt_rdma_bc_close,
  199. .destroy = xprt_rdma_bc_put,
  200. .print_stats = xprt_rdma_print_stats
  201. };
  202. static const struct rpc_timeout xprt_rdma_bc_timeout = {
  203. .to_initval = 60 * HZ,
  204. .to_maxval = 60 * HZ,
  205. };
  206. /* It shouldn't matter if the number of backchannel session slots
  207. * doesn't match the number of RPC/RDMA credits. That just means
  208. * one or the other will have extra slots that aren't used.
  209. */
  210. static struct rpc_xprt *
  211. xprt_setup_rdma_bc(struct xprt_create *args)
  212. {
  213. struct rpc_xprt *xprt;
  214. struct rpcrdma_xprt *new_xprt;
  215. if (args->addrlen > sizeof(xprt->addr))
  216. return ERR_PTR(-EBADF);
  217. xprt = xprt_alloc(args->net, sizeof(*new_xprt),
  218. RPCRDMA_MAX_BC_REQUESTS,
  219. RPCRDMA_MAX_BC_REQUESTS);
  220. if (!xprt)
  221. return ERR_PTR(-ENOMEM);
  222. xprt->timeout = &xprt_rdma_bc_timeout;
  223. xprt_set_bound(xprt);
  224. xprt_set_connected(xprt);
  225. xprt->bind_timeout = 0;
  226. xprt->reestablish_timeout = 0;
  227. xprt->idle_timeout = 0;
  228. xprt->prot = XPRT_TRANSPORT_BC_RDMA;
  229. xprt->ops = &xprt_rdma_bc_procs;
  230. memcpy(&xprt->addr, args->dstaddr, args->addrlen);
  231. xprt->addrlen = args->addrlen;
  232. xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
  233. xprt->resvport = 0;
  234. xprt->max_payload = xprt_rdma_max_inline_read;
  235. new_xprt = rpcx_to_rdmax(xprt);
  236. new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
  237. xprt_get(xprt);
  238. args->bc_xprt->xpt_bc_xprt = xprt;
  239. xprt->bc_xprt = args->bc_xprt;
  240. /* Final put for backchannel xprt is in __svc_rdma_free */
  241. xprt_get(xprt);
  242. return xprt;
  243. }
  244. struct xprt_class xprt_rdma_bc = {
  245. .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
  246. .name = "rdma backchannel",
  247. .owner = THIS_MODULE,
  248. .ident = XPRT_TRANSPORT_BC_RDMA,
  249. .setup = xprt_setup_rdma_bc,
  250. };