backchannel_rqst.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. (c) 2007 Network Appliance, Inc. All Rights Reserved.
  4. (c) 2009 NetApp. All Rights Reserved.
  5. ******************************************************************************/
  6. #include <linux/tcp.h>
  7. #include <linux/slab.h>
  8. #include <linux/sunrpc/xprt.h>
  9. #include <linux/export.h>
  10. #include <linux/sunrpc/bc_xprt.h>
  11. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  12. #define RPCDBG_FACILITY RPCDBG_TRANS
  13. #endif
  14. #define BC_MAX_SLOTS 64U
  15. unsigned int xprt_bc_max_slots(struct rpc_xprt *xprt)
  16. {
  17. return BC_MAX_SLOTS;
  18. }
  19. /*
  20. * Helper routines that track the number of preallocation elements
  21. * on the transport.
  22. */
  23. static inline int xprt_need_to_requeue(struct rpc_xprt *xprt)
  24. {
  25. return xprt->bc_alloc_count < xprt->bc_alloc_max;
  26. }
  27. /*
  28. * Free the preallocated rpc_rqst structure and the memory
  29. * buffers hanging off of it.
  30. */
  31. static void xprt_free_allocation(struct rpc_rqst *req)
  32. {
  33. struct xdr_buf *xbufp;
  34. dprintk("RPC: free allocations for req= %p\n", req);
  35. WARN_ON_ONCE(test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state));
  36. xbufp = &req->rq_rcv_buf;
  37. free_page((unsigned long)xbufp->head[0].iov_base);
  38. xbufp = &req->rq_snd_buf;
  39. free_page((unsigned long)xbufp->head[0].iov_base);
  40. kfree(req);
  41. }
  42. static void xprt_bc_reinit_xdr_buf(struct xdr_buf *buf)
  43. {
  44. buf->head[0].iov_len = PAGE_SIZE;
  45. buf->tail[0].iov_len = 0;
  46. buf->pages = NULL;
  47. buf->page_len = 0;
  48. buf->flags = 0;
  49. buf->len = 0;
  50. buf->buflen = PAGE_SIZE;
  51. }
  52. static int xprt_alloc_xdr_buf(struct xdr_buf *buf, gfp_t gfp_flags)
  53. {
  54. struct page *page;
  55. /* Preallocate one XDR receive buffer */
  56. page = alloc_page(gfp_flags);
  57. if (page == NULL)
  58. return -ENOMEM;
  59. xdr_buf_init(buf, page_address(page), PAGE_SIZE);
  60. return 0;
  61. }
  62. static struct rpc_rqst *xprt_alloc_bc_req(struct rpc_xprt *xprt)
  63. {
  64. gfp_t gfp_flags = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
  65. struct rpc_rqst *req;
  66. /* Pre-allocate one backchannel rpc_rqst */
  67. req = kzalloc(sizeof(*req), gfp_flags);
  68. if (req == NULL)
  69. return NULL;
  70. req->rq_xprt = xprt;
  71. /* Preallocate one XDR receive buffer */
  72. if (xprt_alloc_xdr_buf(&req->rq_rcv_buf, gfp_flags) < 0) {
  73. printk(KERN_ERR "Failed to create bc receive xbuf\n");
  74. goto out_free;
  75. }
  76. req->rq_rcv_buf.len = PAGE_SIZE;
  77. /* Preallocate one XDR send buffer */
  78. if (xprt_alloc_xdr_buf(&req->rq_snd_buf, gfp_flags) < 0) {
  79. printk(KERN_ERR "Failed to create bc snd xbuf\n");
  80. goto out_free;
  81. }
  82. return req;
  83. out_free:
  84. xprt_free_allocation(req);
  85. return NULL;
  86. }
  87. /*
  88. * Preallocate up to min_reqs structures and related buffers for use
  89. * by the backchannel. This function can be called multiple times
  90. * when creating new sessions that use the same rpc_xprt. The
  91. * preallocated buffers are added to the pool of resources used by
  92. * the rpc_xprt. Any one of these resources may be used by an
  93. * incoming callback request. It's up to the higher levels in the
  94. * stack to enforce that the maximum number of session slots is not
  95. * being exceeded.
  96. *
  97. * Some callback arguments can be large. For example, a pNFS server
  98. * using multiple deviceids. The list can be unbound, but the client
  99. * has the ability to tell the server the maximum size of the callback
  100. * requests. Each deviceID is 16 bytes, so allocate one page
  101. * for the arguments to have enough room to receive a number of these
  102. * deviceIDs. The NFS client indicates to the pNFS server that its
  103. * callback requests can be up to 4096 bytes in size.
  104. */
  105. int xprt_setup_backchannel(struct rpc_xprt *xprt, unsigned int min_reqs)
  106. {
  107. if (!xprt->ops->bc_setup)
  108. return 0;
  109. return xprt->ops->bc_setup(xprt, min_reqs);
  110. }
  111. EXPORT_SYMBOL_GPL(xprt_setup_backchannel);
  112. int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs)
  113. {
  114. struct rpc_rqst *req;
  115. struct list_head tmp_list;
  116. int i;
  117. dprintk("RPC: setup backchannel transport\n");
  118. if (min_reqs > BC_MAX_SLOTS)
  119. min_reqs = BC_MAX_SLOTS;
  120. /*
  121. * We use a temporary list to keep track of the preallocated
  122. * buffers. Once we're done building the list we splice it
  123. * into the backchannel preallocation list off of the rpc_xprt
  124. * struct. This helps minimize the amount of time the list
  125. * lock is held on the rpc_xprt struct. It also makes cleanup
  126. * easier in case of memory allocation errors.
  127. */
  128. INIT_LIST_HEAD(&tmp_list);
  129. for (i = 0; i < min_reqs; i++) {
  130. /* Pre-allocate one backchannel rpc_rqst */
  131. req = xprt_alloc_bc_req(xprt);
  132. if (req == NULL) {
  133. printk(KERN_ERR "Failed to create bc rpc_rqst\n");
  134. goto out_free;
  135. }
  136. /* Add the allocated buffer to the tmp list */
  137. dprintk("RPC: adding req= %p\n", req);
  138. list_add(&req->rq_bc_pa_list, &tmp_list);
  139. }
  140. /*
  141. * Add the temporary list to the backchannel preallocation list
  142. */
  143. spin_lock(&xprt->bc_pa_lock);
  144. list_splice(&tmp_list, &xprt->bc_pa_list);
  145. xprt->bc_alloc_count += min_reqs;
  146. xprt->bc_alloc_max += min_reqs;
  147. atomic_add(min_reqs, &xprt->bc_slot_count);
  148. spin_unlock(&xprt->bc_pa_lock);
  149. dprintk("RPC: setup backchannel transport done\n");
  150. return 0;
  151. out_free:
  152. /*
  153. * Memory allocation failed, free the temporary list
  154. */
  155. while (!list_empty(&tmp_list)) {
  156. req = list_first_entry(&tmp_list,
  157. struct rpc_rqst,
  158. rq_bc_pa_list);
  159. list_del(&req->rq_bc_pa_list);
  160. xprt_free_allocation(req);
  161. }
  162. dprintk("RPC: setup backchannel transport failed\n");
  163. return -ENOMEM;
  164. }
  165. /**
  166. * xprt_destroy_backchannel - Destroys the backchannel preallocated structures.
  167. * @xprt: the transport holding the preallocated strucures
  168. * @max_reqs: the maximum number of preallocated structures to destroy
  169. *
  170. * Since these structures may have been allocated by multiple calls
  171. * to xprt_setup_backchannel, we only destroy up to the maximum number
  172. * of reqs specified by the caller.
  173. */
  174. void xprt_destroy_backchannel(struct rpc_xprt *xprt, unsigned int max_reqs)
  175. {
  176. if (xprt->ops->bc_destroy)
  177. xprt->ops->bc_destroy(xprt, max_reqs);
  178. }
  179. EXPORT_SYMBOL_GPL(xprt_destroy_backchannel);
  180. void xprt_destroy_bc(struct rpc_xprt *xprt, unsigned int max_reqs)
  181. {
  182. struct rpc_rqst *req = NULL, *tmp = NULL;
  183. dprintk("RPC: destroy backchannel transport\n");
  184. if (max_reqs == 0)
  185. goto out;
  186. spin_lock_bh(&xprt->bc_pa_lock);
  187. xprt->bc_alloc_max -= min(max_reqs, xprt->bc_alloc_max);
  188. list_for_each_entry_safe(req, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  189. dprintk("RPC: req=%p\n", req);
  190. list_del(&req->rq_bc_pa_list);
  191. xprt_free_allocation(req);
  192. xprt->bc_alloc_count--;
  193. atomic_dec(&xprt->bc_slot_count);
  194. if (--max_reqs == 0)
  195. break;
  196. }
  197. spin_unlock_bh(&xprt->bc_pa_lock);
  198. out:
  199. dprintk("RPC: backchannel list empty= %s\n",
  200. list_empty(&xprt->bc_pa_list) ? "true" : "false");
  201. }
  202. static struct rpc_rqst *xprt_get_bc_request(struct rpc_xprt *xprt, __be32 xid,
  203. struct rpc_rqst *new)
  204. {
  205. struct rpc_rqst *req = NULL;
  206. dprintk("RPC: allocate a backchannel request\n");
  207. if (list_empty(&xprt->bc_pa_list)) {
  208. if (!new)
  209. goto not_found;
  210. if (atomic_read(&xprt->bc_slot_count) >= BC_MAX_SLOTS)
  211. goto not_found;
  212. list_add_tail(&new->rq_bc_pa_list, &xprt->bc_pa_list);
  213. xprt->bc_alloc_count++;
  214. atomic_inc(&xprt->bc_slot_count);
  215. }
  216. req = list_first_entry(&xprt->bc_pa_list, struct rpc_rqst,
  217. rq_bc_pa_list);
  218. req->rq_reply_bytes_recvd = 0;
  219. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  220. sizeof(req->rq_private_buf));
  221. req->rq_xid = xid;
  222. req->rq_connect_cookie = xprt->connect_cookie;
  223. dprintk("RPC: backchannel req=%p\n", req);
  224. not_found:
  225. return req;
  226. }
  227. /*
  228. * Return the preallocated rpc_rqst structure and XDR buffers
  229. * associated with this rpc_task.
  230. */
  231. void xprt_free_bc_request(struct rpc_rqst *req)
  232. {
  233. struct rpc_xprt *xprt = req->rq_xprt;
  234. xprt->ops->bc_free_rqst(req);
  235. }
  236. void xprt_free_bc_rqst(struct rpc_rqst *req)
  237. {
  238. struct rpc_xprt *xprt = req->rq_xprt;
  239. dprintk("RPC: free backchannel req=%p\n", req);
  240. req->rq_connect_cookie = xprt->connect_cookie - 1;
  241. smp_mb__before_atomic();
  242. clear_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  243. smp_mb__after_atomic();
  244. /*
  245. * Return it to the list of preallocations so that it
  246. * may be reused by a new callback request.
  247. */
  248. spin_lock_bh(&xprt->bc_pa_lock);
  249. if (xprt_need_to_requeue(xprt)) {
  250. xprt_bc_reinit_xdr_buf(&req->rq_snd_buf);
  251. xprt_bc_reinit_xdr_buf(&req->rq_rcv_buf);
  252. req->rq_rcv_buf.len = PAGE_SIZE;
  253. list_add_tail(&req->rq_bc_pa_list, &xprt->bc_pa_list);
  254. xprt->bc_alloc_count++;
  255. atomic_inc(&xprt->bc_slot_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. }
  269. xprt_put(xprt);
  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, *new = NULL;
  285. do {
  286. spin_lock(&xprt->bc_pa_lock);
  287. list_for_each_entry(req, &xprt->bc_pa_list, rq_bc_pa_list) {
  288. if (req->rq_connect_cookie != xprt->connect_cookie)
  289. continue;
  290. if (req->rq_xid == xid)
  291. goto found;
  292. }
  293. req = xprt_get_bc_request(xprt, xid, new);
  294. found:
  295. spin_unlock(&xprt->bc_pa_lock);
  296. if (new) {
  297. if (req != new)
  298. xprt_free_allocation(new);
  299. break;
  300. } else if (req)
  301. break;
  302. new = xprt_alloc_bc_req(xprt);
  303. } while (new);
  304. return req;
  305. }
  306. /*
  307. * Add callback request to callback list. Wake a thread
  308. * on the first pool (usually the only pool) to handle it.
  309. */
  310. void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)
  311. {
  312. struct rpc_xprt *xprt = req->rq_xprt;
  313. struct svc_serv *bc_serv = xprt->bc_serv;
  314. spin_lock(&xprt->bc_pa_lock);
  315. list_del(&req->rq_bc_pa_list);
  316. xprt->bc_alloc_count--;
  317. spin_unlock(&xprt->bc_pa_lock);
  318. req->rq_private_buf.len = copied;
  319. set_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
  320. dprintk("RPC: add callback request to list\n");
  321. xprt_get(xprt);
  322. lwq_enqueue(&req->rq_bc_list, &bc_serv->sv_cb_list);
  323. svc_pool_wake_idle_thread(&bc_serv->sv_pools[0]);
  324. }