backchannel_rqst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. xdr_buf_init(buf, page_address(page), PAGE_SIZE);
  68. return 0;
  69. }
  70. static
  71. struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt, gfp_t gfp_flags)
  72. {
  73. struct rpc_rqst *req;
  74. /* Pre-allocate one backchannel rpc_rqst */
  75. req = kzalloc(sizeof(*req), gfp_flags);
  76. if (req == NULL)
  77. return NULL;
  78. req->rq_xprt = xprt;
  79. INIT_LIST_HEAD(&req->rq_list);
  80. INIT_LIST_HEAD(&req->rq_bc_list);
  81. /* Preallocate one XDR receive buffer */
  82. if (xprt_alloc_xdr_buf(&req->rq_rcv_buf, gfp_flags) < 0) {
  83. printk(KERN_ERR "Failed to create bc receive xbuf\n");
  84. goto out_free;
  85. }
  86. req->rq_rcv_buf.len = PAGE_SIZE;
  87. /* Preallocate one XDR send buffer */
  88. if (xprt_alloc_xdr_buf(&req->rq_snd_buf, gfp_flags) < 0) {
  89. printk(KERN_ERR "Failed to create bc snd xbuf\n");
  90. goto out_free;
  91. }
  92. return req;
  93. out_free:
  94. xprt_free_allocation(req);
  95. return NULL;
  96. }
  97. /*
  98. * Preallocate up to min_reqs structures and related buffers for use
  99. * by the backchannel. This function can be called multiple times
  100. * when creating new sessions that use the same rpc_xprt. The
  101. * preallocated buffers are added to the pool of resources used by
  102. * the rpc_xprt. Anyone of these resources may be used used by an
  103. * incoming callback request. It's up to the higher levels in the
  104. * stack to enforce that the maximum number of session slots is not
  105. * being exceeded.
  106. *
  107. * Some callback arguments can be large. For example, a pNFS server
  108. * using multiple deviceids. The list can be unbound, but the client
  109. * has the ability to tell the server the maximum size of the callback
  110. * requests. Each deviceID is 16 bytes, so allocate one page
  111. * for the arguments to have enough room to receive a number of these
  112. * deviceIDs. The NFS client indicates to the pNFS server that its
  113. * callback requests can be up to 4096 bytes in size.
  114. */
  115. int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
  116. {
  117. if (!xprt->ops->bc_setup)
  118. return 0;
  119. return xprt->ops->bc_setup(xprt, min_reqs);
  120. }
  121. EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
  122. int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs)
  123. {
  124. struct rpc_rqst *req;
  125. struct list_head tmp_list;
  126. int i;
  127. dprintk("RPC: setup backchannel transport\n");
  128. /*
  129. * We use a temporary list to keep track of the preallocated
  130. * buffers. Once we're done building the list we splice it
  131. * into the backchannel preallocation list off of the rpc_xprt
  132. * struct. This helps minimize the amount of time the list
  133. * lock is held on the rpc_xprt struct. It also makes cleanup
  134. * easier in case of memory allocation errors.
  135. */
  136. INIT_LIST_HEAD(&tmp_list);
  137. for (i = 0; i < min_reqs; i++) {
  138. /* Pre-allocate one backchannel rpc_rqst */
  139. req = xprt_alloc_bc_req(xprt, GFP_KERNEL);
  140. if (req == NULL) {
  141. printk(KERN_ERR "Failed to create bc rpc_rqst\n");
  142. goto out_free;
  143. }
  144. /* Add the allocated buffer to the tmp list */
  145. dprintk("RPC: adding req= %p\n", req);
  146. list_add(&req->rq_bc_pa_list, &tmp_list);
  147. }
  148. /*
  149. * Add the temporary list to the backchannel preallocation list
  150. */
  151. spin_lock(&xprt->bc_pa_lock);
  152. list_splice(&tmp_list, &xprt->bc_pa_list);
  153. xprt_inc_alloc_count(xprt, min_reqs);
  154. spin_unlock(&xprt->bc_pa_lock);
  155. dprintk("RPC: setup backchannel transport done\n");
  156. return 0;
  157. out_free:
  158. /*
  159. * Memory allocation failed, free the temporary list
  160. */
  161. while (!list_empty(&tmp_list)) {
  162. req = list_first_entry(&tmp_list,
  163. struct rpc_rqst,
  164. rq_bc_pa_list);
  165. list_del(&req->rq_bc_pa_list);
  166. xprt_free_allocation(req);
  167. }
  168. dprintk("RPC: setup backchannel transport failed\n");
  169. return -ENOMEM;
  170. }
  171. /**
  172. * xprt_destroy_backchannel - Destroys the backchannel preallocated structures.
  173. * @xprt: the transport holding the preallocated strucures
  174. * @max_reqs the maximum number of preallocated structures to destroy
  175. *
  176. * Since these structures may have been allocated by multiple calls
  177. * to xprt_setup_backchannel, we only destroy up to the maximum number
  178. * of reqs specified by the caller.
  179. */
  180. void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
  181. {
  182. if (xprt->ops->bc_destroy)
  183. xprt->ops->bc_destroy(xprt, max_reqs);
  184. }
  185. EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
  186. void xprt_destroy_bc(struct rpc_xprt *xprt, unsigned int max_reqs)
  187. {
  188. struct rpc_rqst *req = NULL, *tmp = NULL;
  189. dprintk("RPC: destroy backchannel transport\n");
  190. if (max_reqs == 0)
  191. goto out;
  192. spin_lock_bh(&xprt->bc_pa_lock);
  193. xprt_dec_alloc_count(xprt, max_reqs);
  194. list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  195. dprintk("RPC: req=%p\n", req);
  196. list_del(&req->rq_bc_pa_list);
  197. xprt_free_allocation(req);
  198. if (--max_reqs == 0)
  199. break;
  200. }
  201. spin_unlock_bh(&xprt->bc_pa_lock);
  202. out:
  203. dprintk("RPC: backchannel list empty= %s\n",
  204. list_empty(&xprt->bc_pa_list) ? "true" : "false");
  205. }
  206. static struct rpc_rqst *xprt_alloc_bc_request(struct rpc_xprt *xprt, __be32 xid)
  207. {
  208. struct rpc_rqst *req = NULL;
  209. dprintk("RPC: allocate a backchannel request\n");
  210. if (atomic_read(&xprt->bc_free_slots) <= 0)
  211. goto not_found;
  212. if (list_empty(&xprt->bc_pa_list)) {
  213. req = xprt_alloc_bc_req(xprt, GFP_ATOMIC);
  214. if (!req)
  215. goto not_found;
  216. list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
  217. xprt->bc_alloc_count++;
  218. }
  219. req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
  220. rq_bc_pa_list);
  221. req->rq_reply_bytes_recvd = 0;
  222. req->rq_bytes_sent = 0;
  223. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  224. sizeof(req->rq_private_buf));
  225. req->rq_xid = xid;
  226. req->rq_connect_cookie = xprt->connect_cookie;
  227. not_found:
  228. dprintk("RPC: backchannel req=%p\n", req);
  229. return req;
  230. }
  231. /*
  232. * Return the preallocated rpc_rqst structure and XDR buffers
  233. * associated with this rpc_task.
  234. */
  235. void xprt_free_bc_request(struct rpc_rqst *req)
  236. {
  237. struct rpc_xprt *xprt = req->rq_xprt;
  238. xprt->ops->bc_free_rqst(req);
  239. }
  240. void xprt_free_bc_rqst(struct rpc_rqst *req)
  241. {
  242. struct rpc_xprt *xprt = req->rq_xprt;
  243. dprintk("RPC: free backchannel req=%p\n", req);
  244. req->rq_connect_cookie = xprt->connect_cookie - 1;
  245. smp_mb__before_atomic();
  246. clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  247. smp_mb__after_atomic();
  248. /*
  249. * Return it to the list of preallocations so that it
  250. * may be reused by a new callback request.
  251. */
  252. spin_lock_bh(&xprt->bc_pa_lock);
  253. if (xprt_need_to_requeue(xprt)) {
  254. list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
  255. xprt->bc_alloc_count++;
  256. req = NULL;
  257. }
  258. spin_unlock_bh(&xprt->bc_pa_lock);
  259. if (req != NULL) {
  260. /*
  261. * The last remaining session was destroyed while this
  262. * entry was in use. Free the entry and don't attempt
  263. * to add back to the list because there is no need to
  264. * have anymore preallocated entries.
  265. */
  266. dprintk("RPC: Last session removed req=%p\n", req);
  267. xprt_free_allocation(req);
  268. return;
  269. }
  270. }
  271. /*
  272. * One or more rpc_rqst structure have been preallocated during the
  273. * backchannel setup. Buffer space for the send and private XDR buffers
  274. * has been preallocated as well. Use xprt_alloc_bc_request to allocate
  275. * to this request. Use xprt_free_bc_request to return it.
  276. *
  277. * We know that we're called in soft interrupt context, grab the spin_lock
  278. * since there is no need to grab the bottom half spin_lock.
  279. *
  280. * Return an available rpc_rqst, otherwise NULL if non are available.
  281. */
  282. struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)
  283. {
  284. struct rpc_rqst *req;
  285. spin_lock(&xprt->bc_pa_lock);
  286. list_for_each_entry(req, &xprt->bc_pa_list, rq_bc_pa_list) {
  287. if (req->rq_connect_cookie != xprt->connect_cookie)
  288. continue;
  289. if (req->rq_xid == xid)
  290. goto found;
  291. }
  292. req = xprt_alloc_bc_request(xprt, xid);
  293. found:
  294. spin_unlock(&xprt->bc_pa_lock);
  295. return req;
  296. }
  297. /*
  298. * Add callback request to callback list. The callback
  299. * service sleeps on the sv_cb_waitq waiting for new
  300. * requests. Wake it up after adding enqueing the
  301. * request.
  302. */
  303. void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)
  304. {
  305. struct rpc_xprt *xprt = req->rq_xprt;
  306. struct svc_serv *bc_serv = xprt->bc_serv;
  307. spin_lock(&xprt->bc_pa_lock);
  308. list_del(&req->rq_bc_pa_list);
  309. xprt_dec_alloc_count(xprt, 1);
  310. spin_unlock(&xprt->bc_pa_lock);
  311. req->rq_private_buf.len = copied;
  312. set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  313. dprintk("RPC: add callback request to list\n");
  314. spin_lock(&bc_serv->sv_cb_lock);
  315. list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
  316. wake_up(&bc_serv->sv_cb_waitq);
  317. spin_unlock(&bc_serv->sv_cb_lock);
  318. }