backchannel_rqst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /******************************************************************************
  2. (c) 2007 Network Appliance, Inc. All Rights Reserved.
  3. (c) 2009 NetApp. All Rights Reserved.
  4. NetApp provides this source code under the GPL v2 License.
  5. The GPL v2 license is available at
  6. http://opensource.org/licenses/gpl-license.php.
  7. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  10. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  14. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  15. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  16. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  17. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. ******************************************************************************/
  19. #include <linux/tcp.h>
  20. #include <linux/slab.h>
  21. #include <linux/sunrpc/xprt.h>
  22. #include <linux/export.h>
  23. #include <linux/sunrpc/bc_xprt.h>
  24. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  25. #define RPCDBG_FACILITY RPCDBG_TRANS
  26. #endif
  27. /*
  28. * Helper routines that track the number of preallocation elements
  29. * on the transport.
  30. */
  31. static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)
  32. {
  33. return xprt->bc_alloc_count < atomic_read(&xprt->bc_free_slots);
  34. }
  35. static inline void xprt_inc_alloc_count(struct rpc_xprt *xprt, unsigned int n)
  36. {
  37. atomic_add(n, &xprt->bc_free_slots);
  38. xprt->bc_alloc_count += n;
  39. }
  40. static inline int xprt_dec_alloc_count(struct rpc_xprt *xprt, unsigned int n)
  41. {
  42. atomic_sub(n, &xprt->bc_free_slots);
  43. return xprt->bc_alloc_count -= n;
  44. }
  45. /*
  46. * Free the preallocated rpc_rqst structure and the memory
  47. * buffers hanging off of it.
  48. */
  49. static void xprt_free_allocation(struct rpc_rqst *req)
  50. {
  51. struct xdr_buf *xbufp;
  52. dprintk("RPC: free allocations for req= %p\n", req);
  53. WARN_ON_ONCE(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
  54. xbufp = &req->rq_rcv_buf;
  55. free_page((unsigned long)xbufp->head[0].iov_base);
  56. xbufp = &req->rq_snd_buf;
  57. free_page((unsigned long)xbufp->head[0].iov_base);
  58. kfree(req);
  59. }
  60. static int xprt_alloc_xdr_buf(struct xdr_buf *buf, gfp_t gfp_flags)
  61. {
  62. struct page *page;
  63. /* Preallocate one XDR receive buffer */
  64. page = alloc_page(gfp_flags);
  65. if (page == NULL)
  66. return -ENOMEM;
  67. buf->head[0].iov_base = page_address(page);
  68. buf->head[0].iov_len = PAGE_SIZE;
  69. buf->tail[0].iov_base = NULL;
  70. buf->tail[0].iov_len = 0;
  71. buf->page_len = 0;
  72. buf->len = 0;
  73. buf->buflen = PAGE_SIZE;
  74. return 0;
  75. }
  76. static
  77. struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt, gfp_t gfp_flags)
  78. {
  79. struct rpc_rqst *req;
  80. /* Pre-allocate one backchannel rpc_rqst */
  81. req = kzalloc(sizeof(*req), gfp_flags);
  82. if (req == NULL)
  83. return NULL;
  84. req->rq_xprt = xprt;
  85. INIT_LIST_HEAD(&req->rq_list);
  86. INIT_LIST_HEAD(&req->rq_bc_list);
  87. /* Preallocate one XDR receive buffer */
  88. if (xprt_alloc_xdr_buf(&req->rq_rcv_buf, gfp_flags) < 0) {
  89. printk(KERN_ERR "Failed to create bc receive xbuf\n");
  90. goto out_free;
  91. }
  92. req->rq_rcv_buf.len = PAGE_SIZE;
  93. /* Preallocate one XDR send buffer */
  94. if (xprt_alloc_xdr_buf(&req->rq_snd_buf, gfp_flags) < 0) {
  95. printk(KERN_ERR "Failed to create bc snd xbuf\n");
  96. goto out_free;
  97. }
  98. return req;
  99. out_free:
  100. xprt_free_allocation(req);
  101. return NULL;
  102. }
  103. /*
  104. * Preallocate up to min_reqs structures and related buffers for use
  105. * by the backchannel. This function can be called multiple times
  106. * when creating new sessions that use the same rpc_xprt. The
  107. * preallocated buffers are added to the pool of resources used by
  108. * the rpc_xprt. Anyone of these resources may be used used by an
  109. * incoming callback request. It's up to the higher levels in the
  110. * stack to enforce that the maximum number of session slots is not
  111. * being exceeded.
  112. *
  113. * Some callback arguments can be large. For example, a pNFS server
  114. * using multiple deviceids. The list can be unbound, but the client
  115. * has the ability to tell the server the maximum size of the callback
  116. * requests. Each deviceID is 16 bytes, so allocate one page
  117. * for the arguments to have enough room to receive a number of these
  118. * deviceIDs. The NFS client indicates to the pNFS server that its
  119. * callback requests can be up to 4096 bytes in size.
  120. */
  121. int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
  122. {
  123. if (!xprt->ops->bc_setup)
  124. return 0;
  125. return xprt->ops->bc_setup(xprt, min_reqs);
  126. }
  127. EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
  128. int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs)
  129. {
  130. struct rpc_rqst *req;
  131. struct list_head tmp_list;
  132. int i;
  133. dprintk("RPC: setup backchannel transport\n");
  134. /*
  135. * We use a temporary list to keep track of the preallocated
  136. * buffers. Once we're done building the list we splice it
  137. * into the backchannel preallocation list off of the rpc_xprt
  138. * struct. This helps minimize the amount of time the list
  139. * lock is held on the rpc_xprt struct. It also makes cleanup
  140. * easier in case of memory allocation errors.
  141. */
  142. INIT_LIST_HEAD(&tmp_list);
  143. for (i = 0; i < min_reqs; i++) {
  144. /* Pre-allocate one backchannel rpc_rqst */
  145. req = xprt_alloc_bc_req(xprt, GFP_KERNEL);
  146. if (req == NULL) {
  147. printk(KERN_ERR "Failed to create bc rpc_rqst\n");
  148. goto out_free;
  149. }
  150. /* Add the allocated buffer to the tmp list */
  151. dprintk("RPC: adding req= %p\n", req);
  152. list_add(&req->rq_bc_pa_list, &tmp_list);
  153. }
  154. /*
  155. * Add the temporary list to the backchannel preallocation list
  156. */
  157. spin_lock_bh(&xprt->bc_pa_lock);
  158. list_splice(&tmp_list, &xprt->bc_pa_list);
  159. xprt_inc_alloc_count(xprt, min_reqs);
  160. spin_unlock_bh(&xprt->bc_pa_lock);
  161. dprintk("RPC: setup backchannel transport done\n");
  162. return 0;
  163. out_free:
  164. /*
  165. * Memory allocation failed, free the temporary list
  166. */
  167. while (!list_empty(&tmp_list)) {
  168. req = list_first_entry(&tmp_list,
  169. struct rpc_rqst,
  170. rq_bc_pa_list);
  171. list_del(&req->rq_bc_pa_list);
  172. xprt_free_allocation(req);
  173. }
  174. dprintk("RPC: setup backchannel transport failed\n");
  175. return -ENOMEM;
  176. }
  177. /**
  178. * xprt_destroy_backchannel - Destroys the backchannel preallocated structures.
  179. * @xprt: the transport holding the preallocated strucures
  180. * @max_reqs the maximum number of preallocated structures to destroy
  181. *
  182. * Since these structures may have been allocated by multiple calls
  183. * to xprt_setup_backchannel, we only destroy up to the maximum number
  184. * of reqs specified by the caller.
  185. */
  186. void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
  187. {
  188. if (xprt->ops->bc_destroy)
  189. xprt->ops->bc_destroy(xprt, max_reqs);
  190. }
  191. EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
  192. void xprt_destroy_bc(struct rpc_xprt *xprt, unsigned int max_reqs)
  193. {
  194. struct rpc_rqst *req = NULL, *tmp = NULL;
  195. dprintk("RPC: destroy backchannel transport\n");
  196. if (max_reqs == 0)
  197. goto out;
  198. spin_lock_bh(&xprt->bc_pa_lock);
  199. xprt_dec_alloc_count(xprt, max_reqs);
  200. list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  201. dprintk("RPC: req=%p\n", req);
  202. list_del(&req->rq_bc_pa_list);
  203. xprt_free_allocation(req);
  204. if (--max_reqs == 0)
  205. break;
  206. }
  207. spin_unlock_bh(&xprt->bc_pa_lock);
  208. out:
  209. dprintk("RPC: backchannel list empty= %s\n",
  210. list_empty(&xprt->bc_pa_list) ? "true" : "false");
  211. }
  212. static struct rpc_rqst *xprt_alloc_bc_request(struct rpc_xprt *xprt, __be32 xid)
  213. {
  214. struct rpc_rqst *req = NULL;
  215. dprintk("RPC: allocate a backchannel request\n");
  216. if (atomic_read(&xprt->bc_free_slots) <= 0)
  217. goto not_found;
  218. if (list_empty(&xprt->bc_pa_list)) {
  219. req = xprt_alloc_bc_req(xprt, GFP_ATOMIC);
  220. if (!req)
  221. goto not_found;
  222. list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
  223. xprt->bc_alloc_count++;
  224. }
  225. req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
  226. rq_bc_pa_list);
  227. req->rq_reply_bytes_recvd = 0;
  228. req->rq_bytes_sent = 0;
  229. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  230. sizeof(req->rq_private_buf));
  231. req->rq_xid = xid;
  232. req->rq_connect_cookie = xprt->connect_cookie;
  233. not_found:
  234. dprintk("RPC: backchannel req=%p\n", req);
  235. return req;
  236. }
  237. /*
  238. * Return the preallocated rpc_rqst structure and XDR buffers
  239. * associated with this rpc_task.
  240. */
  241. void xprt_free_bc_request(struct rpc_rqst *req)
  242. {
  243. struct rpc_xprt *xprt = req->rq_xprt;
  244. xprt->ops->bc_free_rqst(req);
  245. }
  246. void xprt_free_bc_rqst(struct rpc_rqst *req)
  247. {
  248. struct rpc_xprt *xprt = req->rq_xprt;
  249. dprintk("RPC: free backchannel req=%p\n", req);
  250. req->rq_connect_cookie = xprt->connect_cookie - 1;
  251. smp_mb__before_atomic();
  252. clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  253. smp_mb__after_atomic();
  254. /*
  255. * Return it to the list of preallocations so that it
  256. * may be reused by a new callback request.
  257. */
  258. spin_lock_bh(&xprt->bc_pa_lock);
  259. if (xprt_need_to_requeue(xprt)) {
  260. list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
  261. xprt->bc_alloc_count++;
  262. req = NULL;
  263. }
  264. spin_unlock_bh(&xprt->bc_pa_lock);
  265. if (req != NULL) {
  266. /*
  267. * The last remaining session was destroyed while this
  268. * entry was in use. Free the entry and don't attempt
  269. * to add back to the list because there is no need to
  270. * have anymore preallocated entries.
  271. */
  272. dprintk("RPC: Last session removed req=%p\n", req);
  273. xprt_free_allocation(req);
  274. return;
  275. }
  276. }
  277. /*
  278. * One or more rpc_rqst structure have been preallocated during the
  279. * backchannel setup. Buffer space for the send and private XDR buffers
  280. * has been preallocated as well. Use xprt_alloc_bc_request to allocate
  281. * to this request. Use xprt_free_bc_request to return it.
  282. *
  283. * We know that we're called in soft interrupt context, grab the spin_lock
  284. * since there is no need to grab the bottom half spin_lock.
  285. *
  286. * Return an available rpc_rqst, otherwise NULL if non are available.
  287. */
  288. struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)
  289. {
  290. struct rpc_rqst *req;
  291. spin_lock(&xprt->bc_pa_lock);
  292. list_for_each_entry(req, &xprt->bc_pa_list, rq_bc_pa_list) {
  293. if (req->rq_connect_cookie != xprt->connect_cookie)
  294. continue;
  295. if (req->rq_xid == xid)
  296. goto found;
  297. }
  298. req = xprt_alloc_bc_request(xprt, xid);
  299. found:
  300. spin_unlock(&xprt->bc_pa_lock);
  301. return req;
  302. }
  303. /*
  304. * Add callback request to callback list. The callback
  305. * service sleeps on the sv_cb_waitq waiting for new
  306. * requests. Wake it up after adding enqueing the
  307. * request.
  308. */
  309. void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)
  310. {
  311. struct rpc_xprt *xprt = req->rq_xprt;
  312. struct svc_serv *bc_serv = xprt->bc_serv;
  313. spin_lock(&xprt->bc_pa_lock);
  314. list_del(&req->rq_bc_pa_list);
  315. xprt_dec_alloc_count(xprt, 1);
  316. spin_unlock(&xprt->bc_pa_lock);
  317. req->rq_private_buf.len = copied;
  318. set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  319. dprintk("RPC: add callback request to list\n");
  320. spin_lock(&bc_serv->sv_cb_lock);
  321. list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
  322. wake_up(&bc_serv->sv_cb_waitq);
  323. spin_unlock(&bc_serv->sv_cb_lock);
  324. }