xprt.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/xprt.c
  4. *
  5. * This is a generic RPC call interface supporting congestion avoidance,
  6. * and asynchronous calls.
  7. *
  8. * The interface works like this:
  9. *
  10. * - When a process places a call, it allocates a request slot if
  11. * one is available. Otherwise, it sleeps on the backlog queue
  12. * (xprt_reserve).
  13. * - Next, the caller puts together the RPC message, stuffs it into
  14. * the request struct, and calls xprt_transmit().
  15. * - xprt_transmit sends the message and installs the caller on the
  16. * transport's wait list. At the same time, if a reply is expected,
  17. * it installs a timer that is run after the packet's timeout has
  18. * expired.
  19. * - When a packet arrives, the data_ready handler walks the list of
  20. * pending requests for that transport. If a matching XID is found, the
  21. * caller is woken up, and the timer removed.
  22. * - When no reply arrives within the timeout interval, the timer is
  23. * fired by the kernel and runs xprt_timer(). It either adjusts the
  24. * timeout values (minor timeout) or wakes up the caller with a status
  25. * of -ETIMEDOUT.
  26. * - When the caller receives a notification from RPC that a reply arrived,
  27. * it should release the RPC slot, and process the reply.
  28. * If the call timed out, it may choose to retry the operation by
  29. * adjusting the initial timeout value, and simply calling rpc_call
  30. * again.
  31. *
  32. * Support for async RPC is done through a set of RPC-specific scheduling
  33. * primitives that `transparently' work for processes as well as async
  34. * tasks that rely on callbacks.
  35. *
  36. * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
  37. *
  38. * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
  39. */
  40. #include <linux/module.h>
  41. #include <linux/types.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/net.h>
  45. #include <linux/ktime.h>
  46. #include <linux/sunrpc/clnt.h>
  47. #include <linux/sunrpc/metrics.h>
  48. #include <linux/sunrpc/bc_xprt.h>
  49. #include <linux/rcupdate.h>
  50. #include <linux/sched/mm.h>
  51. #include <trace/events/sunrpc.h>
  52. #include "sunrpc.h"
  53. #include "sysfs.h"
  54. #include "fail.h"
  55. /*
  56. * Local variables
  57. */
  58. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  59. # define RPCDBG_FACILITY RPCDBG_XPRT
  60. #endif
  61. /*
  62. * Local functions
  63. */
  64. static void xprt_init(struct rpc_xprt *xprt, struct net *net);
  65. static __be32 xprt_alloc_xid(struct rpc_xprt *xprt);
  66. static void xprt_destroy(struct rpc_xprt *xprt);
  67. static void xprt_request_init(struct rpc_task *task);
  68. static int xprt_request_prepare(struct rpc_rqst *req, struct xdr_buf *buf);
  69. static DEFINE_SPINLOCK(xprt_list_lock);
  70. static LIST_HEAD(xprt_list);
  71. static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
  72. {
  73. unsigned long timeout = jiffies + req->rq_timeout;
  74. if (time_before(timeout, req->rq_majortimeo))
  75. return timeout;
  76. return req->rq_majortimeo;
  77. }
  78. /**
  79. * xprt_register_transport - register a transport implementation
  80. * @transport: transport to register
  81. *
  82. * If a transport implementation is loaded as a kernel module, it can
  83. * call this interface to make itself known to the RPC client.
  84. *
  85. * Returns:
  86. * 0: transport successfully registered
  87. * -EEXIST: transport already registered
  88. * -EINVAL: transport module being unloaded
  89. */
  90. int xprt_register_transport(struct xprt_class *transport)
  91. {
  92. struct xprt_class *t;
  93. int result;
  94. result = -EEXIST;
  95. spin_lock(&xprt_list_lock);
  96. list_for_each_entry(t, &xprt_list, list) {
  97. /* don't register the same transport class twice */
  98. if (t->ident == transport->ident)
  99. goto out;
  100. }
  101. list_add_tail(&transport->list, &xprt_list);
  102. printk(KERN_INFO "RPC: Registered %s transport module.\n",
  103. transport->name);
  104. result = 0;
  105. out:
  106. spin_unlock(&xprt_list_lock);
  107. return result;
  108. }
  109. EXPORT_SYMBOL_GPL(xprt_register_transport);
  110. /**
  111. * xprt_unregister_transport - unregister a transport implementation
  112. * @transport: transport to unregister
  113. *
  114. * Returns:
  115. * 0: transport successfully unregistered
  116. * -ENOENT: transport never registered
  117. */
  118. int xprt_unregister_transport(struct xprt_class *transport)
  119. {
  120. struct xprt_class *t;
  121. int result;
  122. result = 0;
  123. spin_lock(&xprt_list_lock);
  124. list_for_each_entry(t, &xprt_list, list) {
  125. if (t == transport) {
  126. printk(KERN_INFO
  127. "RPC: Unregistered %s transport module.\n",
  128. transport->name);
  129. list_del_init(&transport->list);
  130. goto out;
  131. }
  132. }
  133. result = -ENOENT;
  134. out:
  135. spin_unlock(&xprt_list_lock);
  136. return result;
  137. }
  138. EXPORT_SYMBOL_GPL(xprt_unregister_transport);
  139. static void
  140. xprt_class_release(const struct xprt_class *t)
  141. {
  142. module_put(t->owner);
  143. }
  144. static const struct xprt_class *
  145. xprt_class_find_by_ident_locked(int ident)
  146. {
  147. const struct xprt_class *t;
  148. list_for_each_entry(t, &xprt_list, list) {
  149. if (t->ident != ident)
  150. continue;
  151. if (!try_module_get(t->owner))
  152. continue;
  153. return t;
  154. }
  155. return NULL;
  156. }
  157. static const struct xprt_class *
  158. xprt_class_find_by_ident(int ident)
  159. {
  160. const struct xprt_class *t;
  161. spin_lock(&xprt_list_lock);
  162. t = xprt_class_find_by_ident_locked(ident);
  163. spin_unlock(&xprt_list_lock);
  164. return t;
  165. }
  166. static const struct xprt_class *
  167. xprt_class_find_by_netid_locked(const char *netid)
  168. {
  169. const struct xprt_class *t;
  170. unsigned int i;
  171. list_for_each_entry(t, &xprt_list, list) {
  172. for (i = 0; t->netid[i][0] != '\0'; i++) {
  173. if (strcmp(t->netid[i], netid) != 0)
  174. continue;
  175. if (!try_module_get(t->owner))
  176. continue;
  177. return t;
  178. }
  179. }
  180. return NULL;
  181. }
  182. static const struct xprt_class *
  183. xprt_class_find_by_netid(const char *netid)
  184. {
  185. const struct xprt_class *t;
  186. spin_lock(&xprt_list_lock);
  187. t = xprt_class_find_by_netid_locked(netid);
  188. if (!t) {
  189. spin_unlock(&xprt_list_lock);
  190. request_module("rpc%s", netid);
  191. spin_lock(&xprt_list_lock);
  192. t = xprt_class_find_by_netid_locked(netid);
  193. }
  194. spin_unlock(&xprt_list_lock);
  195. return t;
  196. }
  197. /**
  198. * xprt_find_transport_ident - convert a netid into a transport identifier
  199. * @netid: transport to load
  200. *
  201. * Returns:
  202. * > 0: transport identifier
  203. * -ENOENT: transport module not available
  204. */
  205. int xprt_find_transport_ident(const char *netid)
  206. {
  207. const struct xprt_class *t;
  208. int ret;
  209. t = xprt_class_find_by_netid(netid);
  210. if (!t)
  211. return -ENOENT;
  212. ret = t->ident;
  213. xprt_class_release(t);
  214. return ret;
  215. }
  216. EXPORT_SYMBOL_GPL(xprt_find_transport_ident);
  217. static void xprt_clear_locked(struct rpc_xprt *xprt)
  218. {
  219. xprt->snd_task = NULL;
  220. if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state))
  221. clear_bit_unlock(XPRT_LOCKED, &xprt->state);
  222. else
  223. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  224. }
  225. /**
  226. * xprt_reserve_xprt - serialize write access to transports
  227. * @task: task that is requesting access to the transport
  228. * @xprt: pointer to the target transport
  229. *
  230. * This prevents mixing the payload of separate requests, and prevents
  231. * transport connects from colliding with writes. No congestion control
  232. * is provided.
  233. */
  234. int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  235. {
  236. struct rpc_rqst *req = task->tk_rqstp;
  237. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  238. if (task == xprt->snd_task)
  239. goto out_locked;
  240. goto out_sleep;
  241. }
  242. if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
  243. goto out_unlock;
  244. xprt->snd_task = task;
  245. out_locked:
  246. trace_xprt_reserve_xprt(xprt, task);
  247. return 1;
  248. out_unlock:
  249. xprt_clear_locked(xprt);
  250. out_sleep:
  251. task->tk_status = -EAGAIN;
  252. if (RPC_IS_SOFT(task) || RPC_IS_SOFTCONN(task))
  253. rpc_sleep_on_timeout(&xprt->sending, task, NULL,
  254. xprt_request_timeout(req));
  255. else
  256. rpc_sleep_on(&xprt->sending, task, NULL);
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
  260. static bool
  261. xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
  262. {
  263. return test_bit(XPRT_CWND_WAIT, &xprt->state);
  264. }
  265. static void
  266. xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
  267. {
  268. if (!list_empty(&xprt->xmit_queue)) {
  269. /* Peek at head of queue to see if it can make progress */
  270. if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
  271. rq_xmit)->rq_cong)
  272. return;
  273. }
  274. set_bit(XPRT_CWND_WAIT, &xprt->state);
  275. }
  276. static void
  277. xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
  278. {
  279. if (!RPCXPRT_CONGESTED(xprt))
  280. clear_bit(XPRT_CWND_WAIT, &xprt->state);
  281. }
  282. /*
  283. * xprt_reserve_xprt_cong - serialize write access to transports
  284. * @task: task that is requesting access to the transport
  285. *
  286. * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
  287. * integrated into the decision of whether a request is allowed to be
  288. * woken up and given access to the transport.
  289. * Note that the lock is only granted if we know there are free slots.
  290. */
  291. int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  292. {
  293. struct rpc_rqst *req = task->tk_rqstp;
  294. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  295. if (task == xprt->snd_task)
  296. goto out_locked;
  297. goto out_sleep;
  298. }
  299. if (req == NULL) {
  300. xprt->snd_task = task;
  301. goto out_locked;
  302. }
  303. if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
  304. goto out_unlock;
  305. if (!xprt_need_congestion_window_wait(xprt)) {
  306. xprt->snd_task = task;
  307. goto out_locked;
  308. }
  309. out_unlock:
  310. xprt_clear_locked(xprt);
  311. out_sleep:
  312. task->tk_status = -EAGAIN;
  313. if (RPC_IS_SOFT(task) || RPC_IS_SOFTCONN(task))
  314. rpc_sleep_on_timeout(&xprt->sending, task, NULL,
  315. xprt_request_timeout(req));
  316. else
  317. rpc_sleep_on(&xprt->sending, task, NULL);
  318. return 0;
  319. out_locked:
  320. trace_xprt_reserve_cong(xprt, task);
  321. return 1;
  322. }
  323. EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
  324. static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
  325. {
  326. int retval;
  327. if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
  328. return 1;
  329. spin_lock(&xprt->transport_lock);
  330. retval = xprt->ops->reserve_xprt(xprt, task);
  331. spin_unlock(&xprt->transport_lock);
  332. return retval;
  333. }
  334. static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
  335. {
  336. struct rpc_xprt *xprt = data;
  337. xprt->snd_task = task;
  338. return true;
  339. }
  340. static void __xprt_lock_write_next(struct rpc_xprt *xprt)
  341. {
  342. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  343. return;
  344. if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
  345. goto out_unlock;
  346. if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
  347. __xprt_lock_write_func, xprt))
  348. return;
  349. out_unlock:
  350. xprt_clear_locked(xprt);
  351. }
  352. static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
  353. {
  354. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  355. return;
  356. if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
  357. goto out_unlock;
  358. if (xprt_need_congestion_window_wait(xprt))
  359. goto out_unlock;
  360. if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
  361. __xprt_lock_write_func, xprt))
  362. return;
  363. out_unlock:
  364. xprt_clear_locked(xprt);
  365. }
  366. /**
  367. * xprt_release_xprt - allow other requests to use a transport
  368. * @xprt: transport with other tasks potentially waiting
  369. * @task: task that is releasing access to the transport
  370. *
  371. * Note that "task" can be NULL. No congestion control is provided.
  372. */
  373. void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  374. {
  375. if (xprt->snd_task == task) {
  376. xprt_clear_locked(xprt);
  377. __xprt_lock_write_next(xprt);
  378. }
  379. trace_xprt_release_xprt(xprt, task);
  380. }
  381. EXPORT_SYMBOL_GPL(xprt_release_xprt);
  382. /**
  383. * xprt_release_xprt_cong - allow other requests to use a transport
  384. * @xprt: transport with other tasks potentially waiting
  385. * @task: task that is releasing access to the transport
  386. *
  387. * Note that "task" can be NULL. Another task is awoken to use the
  388. * transport if the transport's congestion window allows it.
  389. */
  390. void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  391. {
  392. if (xprt->snd_task == task) {
  393. xprt_clear_locked(xprt);
  394. __xprt_lock_write_next_cong(xprt);
  395. }
  396. trace_xprt_release_cong(xprt, task);
  397. }
  398. EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
  399. void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
  400. {
  401. if (xprt->snd_task != task)
  402. return;
  403. spin_lock(&xprt->transport_lock);
  404. xprt->ops->release_xprt(xprt, task);
  405. spin_unlock(&xprt->transport_lock);
  406. }
  407. /*
  408. * Van Jacobson congestion avoidance. Check if the congestion window
  409. * overflowed. Put the task to sleep if this is the case.
  410. */
  411. static int
  412. __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
  413. {
  414. if (req->rq_cong)
  415. return 1;
  416. trace_xprt_get_cong(xprt, req->rq_task);
  417. if (RPCXPRT_CONGESTED(xprt)) {
  418. xprt_set_congestion_window_wait(xprt);
  419. return 0;
  420. }
  421. req->rq_cong = 1;
  422. xprt->cong += RPC_CWNDSCALE;
  423. return 1;
  424. }
  425. /*
  426. * Adjust the congestion window, and wake up the next task
  427. * that has been sleeping due to congestion
  428. */
  429. static void
  430. __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
  431. {
  432. if (!req->rq_cong)
  433. return;
  434. req->rq_cong = 0;
  435. xprt->cong -= RPC_CWNDSCALE;
  436. xprt_test_and_clear_congestion_window_wait(xprt);
  437. trace_xprt_put_cong(xprt, req->rq_task);
  438. __xprt_lock_write_next_cong(xprt);
  439. }
  440. /**
  441. * xprt_request_get_cong - Request congestion control credits
  442. * @xprt: pointer to transport
  443. * @req: pointer to RPC request
  444. *
  445. * Useful for transports that require congestion control.
  446. */
  447. bool
  448. xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
  449. {
  450. bool ret = false;
  451. if (req->rq_cong)
  452. return true;
  453. spin_lock(&xprt->transport_lock);
  454. ret = __xprt_get_cong(xprt, req) != 0;
  455. spin_unlock(&xprt->transport_lock);
  456. return ret;
  457. }
  458. EXPORT_SYMBOL_GPL(xprt_request_get_cong);
  459. /**
  460. * xprt_release_rqst_cong - housekeeping when request is complete
  461. * @task: RPC request that recently completed
  462. *
  463. * Useful for transports that require congestion control.
  464. */
  465. void xprt_release_rqst_cong(struct rpc_task *task)
  466. {
  467. struct rpc_rqst *req = task->tk_rqstp;
  468. __xprt_put_cong(req->rq_xprt, req);
  469. }
  470. EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
  471. static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
  472. {
  473. if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
  474. __xprt_lock_write_next_cong(xprt);
  475. }
  476. /*
  477. * Clear the congestion window wait flag and wake up the next
  478. * entry on xprt->sending
  479. */
  480. static void
  481. xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
  482. {
  483. if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
  484. spin_lock(&xprt->transport_lock);
  485. __xprt_lock_write_next_cong(xprt);
  486. spin_unlock(&xprt->transport_lock);
  487. }
  488. }
  489. /**
  490. * xprt_adjust_cwnd - adjust transport congestion window
  491. * @xprt: pointer to xprt
  492. * @task: recently completed RPC request used to adjust window
  493. * @result: result code of completed RPC request
  494. *
  495. * The transport code maintains an estimate on the maximum number of out-
  496. * standing RPC requests, using a smoothed version of the congestion
  497. * avoidance implemented in 44BSD. This is basically the Van Jacobson
  498. * congestion algorithm: If a retransmit occurs, the congestion window is
  499. * halved; otherwise, it is incremented by 1/cwnd when
  500. *
  501. * - a reply is received and
  502. * - a full number of requests are outstanding and
  503. * - the congestion window hasn't been updated recently.
  504. */
  505. void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
  506. {
  507. struct rpc_rqst *req = task->tk_rqstp;
  508. unsigned long cwnd = xprt->cwnd;
  509. if (result >= 0 && cwnd <= xprt->cong) {
  510. /* The (cwnd >> 1) term makes sure
  511. * the result gets rounded properly. */
  512. cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
  513. if (cwnd > RPC_MAXCWND(xprt))
  514. cwnd = RPC_MAXCWND(xprt);
  515. __xprt_lock_write_next_cong(xprt);
  516. } else if (result == -ETIMEDOUT) {
  517. cwnd >>= 1;
  518. if (cwnd < RPC_CWNDSCALE)
  519. cwnd = RPC_CWNDSCALE;
  520. }
  521. dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
  522. xprt->cong, xprt->cwnd, cwnd);
  523. xprt->cwnd = cwnd;
  524. __xprt_put_cong(xprt, req);
  525. }
  526. EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
  527. /**
  528. * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
  529. * @xprt: transport with waiting tasks
  530. * @status: result code to plant in each task before waking it
  531. *
  532. */
  533. void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
  534. {
  535. if (status < 0)
  536. rpc_wake_up_status(&xprt->pending, status);
  537. else
  538. rpc_wake_up(&xprt->pending);
  539. }
  540. EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
  541. /**
  542. * xprt_wait_for_buffer_space - wait for transport output buffer to clear
  543. * @xprt: transport
  544. *
  545. * Note that we only set the timer for the case of RPC_IS_SOFT(), since
  546. * we don't in general want to force a socket disconnection due to
  547. * an incomplete RPC call transmission.
  548. */
  549. void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
  550. {
  551. set_bit(XPRT_WRITE_SPACE, &xprt->state);
  552. }
  553. EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
  554. static bool
  555. xprt_clear_write_space_locked(struct rpc_xprt *xprt)
  556. {
  557. if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
  558. __xprt_lock_write_next(xprt);
  559. dprintk("RPC: write space: waking waiting task on "
  560. "xprt %p\n", xprt);
  561. return true;
  562. }
  563. return false;
  564. }
  565. /**
  566. * xprt_write_space - wake the task waiting for transport output buffer space
  567. * @xprt: transport with waiting tasks
  568. *
  569. * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
  570. */
  571. bool xprt_write_space(struct rpc_xprt *xprt)
  572. {
  573. bool ret;
  574. if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
  575. return false;
  576. spin_lock(&xprt->transport_lock);
  577. ret = xprt_clear_write_space_locked(xprt);
  578. spin_unlock(&xprt->transport_lock);
  579. return ret;
  580. }
  581. EXPORT_SYMBOL_GPL(xprt_write_space);
  582. static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
  583. {
  584. s64 delta = ktime_to_ns(ktime_get() - abstime);
  585. return likely(delta >= 0) ?
  586. jiffies - nsecs_to_jiffies(delta) :
  587. jiffies + nsecs_to_jiffies(-delta);
  588. }
  589. static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req,
  590. const struct rpc_timeout *to)
  591. {
  592. unsigned long majortimeo = req->rq_timeout;
  593. if (to->to_exponential)
  594. majortimeo <<= to->to_retries;
  595. else
  596. majortimeo += to->to_increment * to->to_retries;
  597. if (majortimeo > to->to_maxval || majortimeo == 0)
  598. majortimeo = to->to_maxval;
  599. return majortimeo;
  600. }
  601. static void xprt_reset_majortimeo(struct rpc_rqst *req,
  602. const struct rpc_timeout *to)
  603. {
  604. req->rq_majortimeo += xprt_calc_majortimeo(req, to);
  605. }
  606. static void xprt_reset_minortimeo(struct rpc_rqst *req)
  607. {
  608. req->rq_minortimeo += req->rq_timeout;
  609. }
  610. static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req,
  611. const struct rpc_timeout *to)
  612. {
  613. unsigned long time_init;
  614. struct rpc_xprt *xprt = req->rq_xprt;
  615. if (likely(xprt && xprt_connected(xprt)))
  616. time_init = jiffies;
  617. else
  618. time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
  619. req->rq_timeout = to->to_initval;
  620. req->rq_majortimeo = time_init + xprt_calc_majortimeo(req, to);
  621. req->rq_minortimeo = time_init + req->rq_timeout;
  622. }
  623. /**
  624. * xprt_adjust_timeout - adjust timeout values for next retransmit
  625. * @req: RPC request containing parameters to use for the adjustment
  626. *
  627. */
  628. int xprt_adjust_timeout(struct rpc_rqst *req)
  629. {
  630. struct rpc_xprt *xprt = req->rq_xprt;
  631. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  632. int status = 0;
  633. if (time_before(jiffies, req->rq_majortimeo)) {
  634. if (time_before(jiffies, req->rq_minortimeo))
  635. return status;
  636. if (to->to_exponential)
  637. req->rq_timeout <<= 1;
  638. else
  639. req->rq_timeout += to->to_increment;
  640. if (to->to_maxval && req->rq_timeout >= to->to_maxval)
  641. req->rq_timeout = to->to_maxval;
  642. req->rq_retries++;
  643. } else {
  644. req->rq_timeout = to->to_initval;
  645. req->rq_retries = 0;
  646. xprt_reset_majortimeo(req, to);
  647. /* Reset the RTT counters == "slow start" */
  648. spin_lock(&xprt->transport_lock);
  649. rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
  650. spin_unlock(&xprt->transport_lock);
  651. status = -ETIMEDOUT;
  652. }
  653. xprt_reset_minortimeo(req);
  654. if (req->rq_timeout == 0) {
  655. printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
  656. req->rq_timeout = 5 * HZ;
  657. }
  658. return status;
  659. }
  660. static void xprt_autoclose(struct work_struct *work)
  661. {
  662. struct rpc_xprt *xprt =
  663. container_of(work, struct rpc_xprt, task_cleanup);
  664. unsigned int pflags = memalloc_nofs_save();
  665. trace_xprt_disconnect_auto(xprt);
  666. xprt->connect_cookie++;
  667. smp_mb__before_atomic();
  668. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  669. xprt->ops->close(xprt);
  670. xprt_release_write(xprt, NULL);
  671. wake_up_bit(&xprt->state, XPRT_LOCKED);
  672. memalloc_nofs_restore(pflags);
  673. }
  674. /**
  675. * xprt_disconnect_done - mark a transport as disconnected
  676. * @xprt: transport to flag for disconnect
  677. *
  678. */
  679. void xprt_disconnect_done(struct rpc_xprt *xprt)
  680. {
  681. trace_xprt_disconnect_done(xprt);
  682. spin_lock(&xprt->transport_lock);
  683. xprt_clear_connected(xprt);
  684. xprt_clear_write_space_locked(xprt);
  685. xprt_clear_congestion_window_wait_locked(xprt);
  686. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  687. spin_unlock(&xprt->transport_lock);
  688. }
  689. EXPORT_SYMBOL_GPL(xprt_disconnect_done);
  690. /**
  691. * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
  692. * @xprt: transport to disconnect
  693. */
  694. static void xprt_schedule_autoclose_locked(struct rpc_xprt *xprt)
  695. {
  696. if (test_and_set_bit(XPRT_CLOSE_WAIT, &xprt->state))
  697. return;
  698. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  699. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  700. else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
  701. rpc_wake_up_queued_task_set_status(&xprt->pending,
  702. xprt->snd_task, -ENOTCONN);
  703. }
  704. /**
  705. * xprt_force_disconnect - force a transport to disconnect
  706. * @xprt: transport to disconnect
  707. *
  708. */
  709. void xprt_force_disconnect(struct rpc_xprt *xprt)
  710. {
  711. trace_xprt_disconnect_force(xprt);
  712. /* Don't race with the test_bit() in xprt_clear_locked() */
  713. spin_lock(&xprt->transport_lock);
  714. xprt_schedule_autoclose_locked(xprt);
  715. spin_unlock(&xprt->transport_lock);
  716. }
  717. EXPORT_SYMBOL_GPL(xprt_force_disconnect);
  718. static unsigned int
  719. xprt_connect_cookie(struct rpc_xprt *xprt)
  720. {
  721. return READ_ONCE(xprt->connect_cookie);
  722. }
  723. static bool
  724. xprt_request_retransmit_after_disconnect(struct rpc_task *task)
  725. {
  726. struct rpc_rqst *req = task->tk_rqstp;
  727. struct rpc_xprt *xprt = req->rq_xprt;
  728. return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
  729. !xprt_connected(xprt);
  730. }
  731. /**
  732. * xprt_conditional_disconnect - force a transport to disconnect
  733. * @xprt: transport to disconnect
  734. * @cookie: 'connection cookie'
  735. *
  736. * This attempts to break the connection if and only if 'cookie' matches
  737. * the current transport 'connection cookie'. It ensures that we don't
  738. * try to break the connection more than once when we need to retransmit
  739. * a batch of RPC requests.
  740. *
  741. */
  742. void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
  743. {
  744. /* Don't race with the test_bit() in xprt_clear_locked() */
  745. spin_lock(&xprt->transport_lock);
  746. if (cookie != xprt->connect_cookie)
  747. goto out;
  748. if (test_bit(XPRT_CLOSING, &xprt->state))
  749. goto out;
  750. xprt_schedule_autoclose_locked(xprt);
  751. out:
  752. spin_unlock(&xprt->transport_lock);
  753. }
  754. static bool
  755. xprt_has_timer(const struct rpc_xprt *xprt)
  756. {
  757. return xprt->idle_timeout != 0;
  758. }
  759. static void
  760. xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
  761. __must_hold(&xprt->transport_lock)
  762. {
  763. xprt->last_used = jiffies;
  764. if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
  765. mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
  766. }
  767. static void
  768. xprt_init_autodisconnect(struct timer_list *t)
  769. {
  770. struct rpc_xprt *xprt = timer_container_of(xprt, t, timer);
  771. if (!RB_EMPTY_ROOT(&xprt->recv_queue))
  772. return;
  773. /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
  774. xprt->last_used = jiffies;
  775. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  776. return;
  777. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  778. }
  779. #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
  780. static void xprt_inject_disconnect(struct rpc_xprt *xprt)
  781. {
  782. if (!fail_sunrpc.ignore_client_disconnect &&
  783. should_fail(&fail_sunrpc.attr, 1))
  784. xprt->ops->inject_disconnect(xprt);
  785. }
  786. #else
  787. static inline void xprt_inject_disconnect(struct rpc_xprt *xprt)
  788. {
  789. }
  790. #endif
  791. bool xprt_lock_connect(struct rpc_xprt *xprt,
  792. struct rpc_task *task,
  793. void *cookie)
  794. {
  795. bool ret = false;
  796. spin_lock(&xprt->transport_lock);
  797. if (!test_bit(XPRT_LOCKED, &xprt->state))
  798. goto out;
  799. if (xprt->snd_task != task)
  800. goto out;
  801. set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
  802. xprt->snd_task = cookie;
  803. ret = true;
  804. out:
  805. spin_unlock(&xprt->transport_lock);
  806. return ret;
  807. }
  808. EXPORT_SYMBOL_GPL(xprt_lock_connect);
  809. void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
  810. {
  811. spin_lock(&xprt->transport_lock);
  812. if (xprt->snd_task != cookie)
  813. goto out;
  814. if (!test_bit(XPRT_LOCKED, &xprt->state))
  815. goto out;
  816. xprt->snd_task =NULL;
  817. clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
  818. xprt->ops->release_xprt(xprt, NULL);
  819. xprt_schedule_autodisconnect(xprt);
  820. out:
  821. spin_unlock(&xprt->transport_lock);
  822. wake_up_bit(&xprt->state, XPRT_LOCKED);
  823. }
  824. EXPORT_SYMBOL_GPL(xprt_unlock_connect);
  825. /**
  826. * xprt_connect - schedule a transport connect operation
  827. * @task: RPC task that is requesting the connect
  828. *
  829. */
  830. void xprt_connect(struct rpc_task *task)
  831. {
  832. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  833. trace_xprt_connect(xprt);
  834. if (!xprt_bound(xprt)) {
  835. task->tk_status = -EAGAIN;
  836. return;
  837. }
  838. if (!xprt_lock_write(xprt, task))
  839. return;
  840. if (!xprt_connected(xprt) && !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
  841. task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
  842. rpc_sleep_on_timeout(&xprt->pending, task, NULL,
  843. xprt_request_timeout(task->tk_rqstp));
  844. if (test_bit(XPRT_CLOSING, &xprt->state))
  845. return;
  846. if (xprt_test_and_set_connecting(xprt))
  847. return;
  848. /* Race breaker */
  849. if (!xprt_connected(xprt)) {
  850. xprt->stat.connect_start = jiffies;
  851. xprt->ops->connect(xprt, task);
  852. } else {
  853. xprt_clear_connecting(xprt);
  854. task->tk_status = 0;
  855. rpc_wake_up_queued_task(&xprt->pending, task);
  856. }
  857. }
  858. xprt_release_write(xprt, task);
  859. }
  860. /**
  861. * xprt_reconnect_delay - compute the wait before scheduling a connect
  862. * @xprt: transport instance
  863. *
  864. */
  865. unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
  866. {
  867. unsigned long start, now = jiffies;
  868. start = xprt->stat.connect_start + xprt->reestablish_timeout;
  869. if (time_after(start, now))
  870. return start - now;
  871. return 0;
  872. }
  873. EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
  874. /**
  875. * xprt_reconnect_backoff - compute the new re-establish timeout
  876. * @xprt: transport instance
  877. * @init_to: initial reestablish timeout
  878. *
  879. */
  880. void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
  881. {
  882. xprt->reestablish_timeout <<= 1;
  883. if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
  884. xprt->reestablish_timeout = xprt->max_reconnect_timeout;
  885. if (xprt->reestablish_timeout < init_to)
  886. xprt->reestablish_timeout = init_to;
  887. }
  888. EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
  889. enum xprt_xid_rb_cmp {
  890. XID_RB_EQUAL,
  891. XID_RB_LEFT,
  892. XID_RB_RIGHT,
  893. };
  894. static enum xprt_xid_rb_cmp
  895. xprt_xid_cmp(__be32 xid1, __be32 xid2)
  896. {
  897. if (xid1 == xid2)
  898. return XID_RB_EQUAL;
  899. if ((__force u32)xid1 < (__force u32)xid2)
  900. return XID_RB_LEFT;
  901. return XID_RB_RIGHT;
  902. }
  903. static struct rpc_rqst *
  904. xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
  905. {
  906. struct rb_node *n = xprt->recv_queue.rb_node;
  907. struct rpc_rqst *req;
  908. while (n != NULL) {
  909. req = rb_entry(n, struct rpc_rqst, rq_recv);
  910. switch (xprt_xid_cmp(xid, req->rq_xid)) {
  911. case XID_RB_LEFT:
  912. n = n->rb_left;
  913. break;
  914. case XID_RB_RIGHT:
  915. n = n->rb_right;
  916. break;
  917. case XID_RB_EQUAL:
  918. return req;
  919. }
  920. }
  921. return NULL;
  922. }
  923. static void
  924. xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
  925. {
  926. struct rb_node **p = &xprt->recv_queue.rb_node;
  927. struct rb_node *n = NULL;
  928. struct rpc_rqst *req;
  929. while (*p != NULL) {
  930. n = *p;
  931. req = rb_entry(n, struct rpc_rqst, rq_recv);
  932. switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
  933. case XID_RB_LEFT:
  934. p = &n->rb_left;
  935. break;
  936. case XID_RB_RIGHT:
  937. p = &n->rb_right;
  938. break;
  939. case XID_RB_EQUAL:
  940. WARN_ON_ONCE(new != req);
  941. return;
  942. }
  943. }
  944. rb_link_node(&new->rq_recv, n, p);
  945. rb_insert_color(&new->rq_recv, &xprt->recv_queue);
  946. }
  947. static void
  948. xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
  949. {
  950. rb_erase(&req->rq_recv, &xprt->recv_queue);
  951. }
  952. /**
  953. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  954. * @xprt: transport on which the original request was transmitted
  955. * @xid: RPC XID of incoming reply
  956. *
  957. * Caller holds xprt->queue_lock.
  958. */
  959. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  960. {
  961. struct rpc_rqst *entry;
  962. entry = xprt_request_rb_find(xprt, xid);
  963. if (entry != NULL) {
  964. trace_xprt_lookup_rqst(xprt, xid, 0);
  965. entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
  966. return entry;
  967. }
  968. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  969. ntohl(xid));
  970. trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
  971. xprt->stat.bad_xids++;
  972. return NULL;
  973. }
  974. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  975. static bool
  976. xprt_is_pinned_rqst(struct rpc_rqst *req)
  977. {
  978. return atomic_read(&req->rq_pin) != 0;
  979. }
  980. /**
  981. * xprt_pin_rqst - Pin a request on the transport receive list
  982. * @req: Request to pin
  983. *
  984. * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
  985. * so should be holding xprt->queue_lock.
  986. */
  987. void xprt_pin_rqst(struct rpc_rqst *req)
  988. {
  989. atomic_inc(&req->rq_pin);
  990. }
  991. EXPORT_SYMBOL_GPL(xprt_pin_rqst);
  992. /**
  993. * xprt_unpin_rqst - Unpin a request on the transport receive list
  994. * @req: Request to pin
  995. *
  996. * Caller should be holding xprt->queue_lock.
  997. */
  998. void xprt_unpin_rqst(struct rpc_rqst *req)
  999. {
  1000. if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
  1001. atomic_dec(&req->rq_pin);
  1002. return;
  1003. }
  1004. if (atomic_dec_and_test(&req->rq_pin))
  1005. wake_up_var(&req->rq_pin);
  1006. }
  1007. EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
  1008. static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
  1009. {
  1010. wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
  1011. }
  1012. static bool
  1013. xprt_request_data_received(struct rpc_task *task)
  1014. {
  1015. return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
  1016. READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
  1017. }
  1018. static bool
  1019. xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
  1020. {
  1021. return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
  1022. READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
  1023. }
  1024. /**
  1025. * xprt_request_enqueue_receive - Add an request to the receive queue
  1026. * @task: RPC task
  1027. *
  1028. */
  1029. int
  1030. xprt_request_enqueue_receive(struct rpc_task *task)
  1031. {
  1032. struct rpc_rqst *req = task->tk_rqstp;
  1033. struct rpc_xprt *xprt = req->rq_xprt;
  1034. int ret;
  1035. if (!xprt_request_need_enqueue_receive(task, req))
  1036. return 0;
  1037. ret = xprt_request_prepare(task->tk_rqstp, &req->rq_rcv_buf);
  1038. if (ret)
  1039. return ret;
  1040. spin_lock(&xprt->queue_lock);
  1041. /* Update the softirq receive buffer */
  1042. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  1043. sizeof(req->rq_private_buf));
  1044. /* Add request to the receive list */
  1045. xprt_request_rb_insert(xprt, req);
  1046. set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
  1047. spin_unlock(&xprt->queue_lock);
  1048. /* Turn off autodisconnect */
  1049. timer_delete_sync(&xprt->timer);
  1050. return 0;
  1051. }
  1052. /**
  1053. * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
  1054. * @task: RPC task
  1055. *
  1056. * Caller must hold xprt->queue_lock.
  1057. */
  1058. static void
  1059. xprt_request_dequeue_receive_locked(struct rpc_task *task)
  1060. {
  1061. struct rpc_rqst *req = task->tk_rqstp;
  1062. if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
  1063. xprt_request_rb_remove(req->rq_xprt, req);
  1064. }
  1065. /**
  1066. * xprt_update_rtt - Update RPC RTT statistics
  1067. * @task: RPC request that recently completed
  1068. *
  1069. * Caller holds xprt->queue_lock.
  1070. */
  1071. void xprt_update_rtt(struct rpc_task *task)
  1072. {
  1073. struct rpc_rqst *req = task->tk_rqstp;
  1074. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  1075. unsigned int timer = task->tk_msg.rpc_proc->p_timer;
  1076. long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
  1077. if (timer) {
  1078. if (req->rq_ntrans == 1)
  1079. rpc_update_rtt(rtt, timer, m);
  1080. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  1081. }
  1082. }
  1083. EXPORT_SYMBOL_GPL(xprt_update_rtt);
  1084. /**
  1085. * xprt_complete_rqst - called when reply processing is complete
  1086. * @task: RPC request that recently completed
  1087. * @copied: actual number of bytes received from the transport
  1088. *
  1089. * Caller holds xprt->queue_lock.
  1090. */
  1091. void xprt_complete_rqst(struct rpc_task *task, int copied)
  1092. {
  1093. struct rpc_rqst *req = task->tk_rqstp;
  1094. struct rpc_xprt *xprt = req->rq_xprt;
  1095. xprt->stat.recvs++;
  1096. xdr_free_bvec(&req->rq_rcv_buf);
  1097. req->rq_private_buf.bvec = NULL;
  1098. req->rq_private_buf.len = copied;
  1099. /* Ensure all writes are done before we update */
  1100. /* req->rq_reply_bytes_recvd */
  1101. smp_wmb();
  1102. req->rq_reply_bytes_recvd = copied;
  1103. xprt_request_dequeue_receive_locked(task);
  1104. rpc_wake_up_queued_task(&xprt->pending, task);
  1105. }
  1106. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  1107. static void xprt_timer(struct rpc_task *task)
  1108. {
  1109. struct rpc_rqst *req = task->tk_rqstp;
  1110. struct rpc_xprt *xprt = req->rq_xprt;
  1111. if (task->tk_status != -ETIMEDOUT)
  1112. return;
  1113. trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
  1114. if (!req->rq_reply_bytes_recvd) {
  1115. if (xprt->ops->timer)
  1116. xprt->ops->timer(xprt, task);
  1117. } else
  1118. task->tk_status = 0;
  1119. }
  1120. /**
  1121. * xprt_wait_for_reply_request_def - wait for reply
  1122. * @task: pointer to rpc_task
  1123. *
  1124. * Set a request's retransmit timeout based on the transport's
  1125. * default timeout parameters. Used by transports that don't adjust
  1126. * the retransmit timeout based on round-trip time estimation,
  1127. * and put the task to sleep on the pending queue.
  1128. */
  1129. void xprt_wait_for_reply_request_def(struct rpc_task *task)
  1130. {
  1131. struct rpc_rqst *req = task->tk_rqstp;
  1132. rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
  1133. xprt_request_timeout(req));
  1134. }
  1135. EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
  1136. /**
  1137. * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
  1138. * @task: pointer to rpc_task
  1139. *
  1140. * Set a request's retransmit timeout using the RTT estimator,
  1141. * and put the task to sleep on the pending queue.
  1142. */
  1143. void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
  1144. {
  1145. int timer = task->tk_msg.rpc_proc->p_timer;
  1146. struct rpc_clnt *clnt = task->tk_client;
  1147. struct rpc_rtt *rtt = clnt->cl_rtt;
  1148. struct rpc_rqst *req = task->tk_rqstp;
  1149. unsigned long max_timeout = clnt->cl_timeout->to_maxval;
  1150. unsigned long timeout;
  1151. timeout = rpc_calc_rto(rtt, timer);
  1152. timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
  1153. if (timeout > max_timeout || timeout == 0)
  1154. timeout = max_timeout;
  1155. rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
  1156. jiffies + timeout);
  1157. }
  1158. EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
  1159. /**
  1160. * xprt_request_wait_receive - wait for the reply to an RPC request
  1161. * @task: RPC task about to send a request
  1162. *
  1163. */
  1164. void xprt_request_wait_receive(struct rpc_task *task)
  1165. {
  1166. struct rpc_rqst *req = task->tk_rqstp;
  1167. struct rpc_xprt *xprt = req->rq_xprt;
  1168. if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
  1169. return;
  1170. /*
  1171. * Sleep on the pending queue if we're expecting a reply.
  1172. * The spinlock ensures atomicity between the test of
  1173. * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
  1174. */
  1175. spin_lock(&xprt->queue_lock);
  1176. if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
  1177. xprt->ops->wait_for_reply_request(task);
  1178. /*
  1179. * Send an extra queue wakeup call if the
  1180. * connection was dropped in case the call to
  1181. * rpc_sleep_on() raced.
  1182. */
  1183. if (xprt_request_retransmit_after_disconnect(task))
  1184. rpc_wake_up_queued_task_set_status(&xprt->pending,
  1185. task, -ENOTCONN);
  1186. }
  1187. spin_unlock(&xprt->queue_lock);
  1188. }
  1189. static bool
  1190. xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
  1191. {
  1192. return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
  1193. }
  1194. /**
  1195. * xprt_request_enqueue_transmit - queue a task for transmission
  1196. * @task: pointer to rpc_task
  1197. *
  1198. * Add a task to the transmission queue.
  1199. */
  1200. void
  1201. xprt_request_enqueue_transmit(struct rpc_task *task)
  1202. {
  1203. struct rpc_rqst *pos, *req = task->tk_rqstp;
  1204. struct rpc_xprt *xprt = req->rq_xprt;
  1205. int ret;
  1206. if (xprt_request_need_enqueue_transmit(task, req)) {
  1207. ret = xprt_request_prepare(task->tk_rqstp, &req->rq_snd_buf);
  1208. if (ret) {
  1209. task->tk_status = ret;
  1210. return;
  1211. }
  1212. req->rq_bytes_sent = 0;
  1213. spin_lock(&xprt->queue_lock);
  1214. /*
  1215. * Requests that carry congestion control credits are added
  1216. * to the head of the list to avoid starvation issues.
  1217. */
  1218. if (req->rq_cong) {
  1219. xprt_clear_congestion_window_wait(xprt);
  1220. list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
  1221. if (pos->rq_cong)
  1222. continue;
  1223. /* Note: req is added _before_ pos */
  1224. list_add_tail(&req->rq_xmit, &pos->rq_xmit);
  1225. INIT_LIST_HEAD(&req->rq_xmit2);
  1226. goto out;
  1227. }
  1228. } else if (req->rq_seqno_count == 0) {
  1229. list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
  1230. if (pos->rq_task->tk_owner != task->tk_owner)
  1231. continue;
  1232. list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
  1233. INIT_LIST_HEAD(&req->rq_xmit);
  1234. goto out;
  1235. }
  1236. }
  1237. list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
  1238. INIT_LIST_HEAD(&req->rq_xmit2);
  1239. out:
  1240. atomic_long_inc(&xprt->xmit_queuelen);
  1241. set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
  1242. spin_unlock(&xprt->queue_lock);
  1243. }
  1244. }
  1245. /**
  1246. * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
  1247. * @task: pointer to rpc_task
  1248. *
  1249. * Remove a task from the transmission queue
  1250. * Caller must hold xprt->queue_lock
  1251. */
  1252. static void
  1253. xprt_request_dequeue_transmit_locked(struct rpc_task *task)
  1254. {
  1255. struct rpc_rqst *req = task->tk_rqstp;
  1256. if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1257. return;
  1258. if (!list_empty(&req->rq_xmit)) {
  1259. struct rpc_xprt *xprt = req->rq_xprt;
  1260. if (list_is_first(&req->rq_xmit, &xprt->xmit_queue) &&
  1261. xprt->ops->abort_send_request)
  1262. xprt->ops->abort_send_request(req);
  1263. list_del(&req->rq_xmit);
  1264. if (!list_empty(&req->rq_xmit2)) {
  1265. struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
  1266. struct rpc_rqst, rq_xmit2);
  1267. list_del(&req->rq_xmit2);
  1268. list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
  1269. }
  1270. } else
  1271. list_del(&req->rq_xmit2);
  1272. atomic_long_dec(&req->rq_xprt->xmit_queuelen);
  1273. xdr_free_bvec(&req->rq_snd_buf);
  1274. }
  1275. /**
  1276. * xprt_request_dequeue_transmit - remove a task from the transmission queue
  1277. * @task: pointer to rpc_task
  1278. *
  1279. * Remove a task from the transmission queue
  1280. */
  1281. static void
  1282. xprt_request_dequeue_transmit(struct rpc_task *task)
  1283. {
  1284. struct rpc_rqst *req = task->tk_rqstp;
  1285. struct rpc_xprt *xprt = req->rq_xprt;
  1286. spin_lock(&xprt->queue_lock);
  1287. xprt_request_dequeue_transmit_locked(task);
  1288. spin_unlock(&xprt->queue_lock);
  1289. }
  1290. /**
  1291. * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
  1292. * @task: pointer to rpc_task
  1293. *
  1294. * Remove a task from the transmit and receive queues, and ensure that
  1295. * it is not pinned by the receive work item.
  1296. */
  1297. void
  1298. xprt_request_dequeue_xprt(struct rpc_task *task)
  1299. {
  1300. struct rpc_rqst *req = task->tk_rqstp;
  1301. struct rpc_xprt *xprt = req->rq_xprt;
  1302. if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
  1303. test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
  1304. xprt_is_pinned_rqst(req)) {
  1305. spin_lock(&xprt->queue_lock);
  1306. while (xprt_is_pinned_rqst(req)) {
  1307. set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
  1308. spin_unlock(&xprt->queue_lock);
  1309. xprt_wait_on_pinned_rqst(req);
  1310. spin_lock(&xprt->queue_lock);
  1311. clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
  1312. }
  1313. xprt_request_dequeue_transmit_locked(task);
  1314. xprt_request_dequeue_receive_locked(task);
  1315. spin_unlock(&xprt->queue_lock);
  1316. xdr_free_bvec(&req->rq_rcv_buf);
  1317. }
  1318. }
  1319. /**
  1320. * xprt_request_prepare - prepare an encoded request for transport
  1321. * @req: pointer to rpc_rqst
  1322. * @buf: pointer to send/rcv xdr_buf
  1323. *
  1324. * Calls into the transport layer to do whatever is needed to prepare
  1325. * the request for transmission or receive.
  1326. * Returns error, or zero.
  1327. */
  1328. static int
  1329. xprt_request_prepare(struct rpc_rqst *req, struct xdr_buf *buf)
  1330. {
  1331. struct rpc_xprt *xprt = req->rq_xprt;
  1332. if (xprt->ops->prepare_request)
  1333. return xprt->ops->prepare_request(req, buf);
  1334. return 0;
  1335. }
  1336. /**
  1337. * xprt_request_need_retransmit - Test if a task needs retransmission
  1338. * @task: pointer to rpc_task
  1339. *
  1340. * Test for whether a connection breakage requires the task to retransmit
  1341. */
  1342. bool
  1343. xprt_request_need_retransmit(struct rpc_task *task)
  1344. {
  1345. return xprt_request_retransmit_after_disconnect(task);
  1346. }
  1347. /**
  1348. * xprt_prepare_transmit - reserve the transport before sending a request
  1349. * @task: RPC task about to send a request
  1350. *
  1351. */
  1352. bool xprt_prepare_transmit(struct rpc_task *task)
  1353. {
  1354. struct rpc_rqst *req = task->tk_rqstp;
  1355. struct rpc_xprt *xprt = req->rq_xprt;
  1356. if (!xprt_lock_write(xprt, task)) {
  1357. /* Race breaker: someone may have transmitted us */
  1358. if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1359. rpc_wake_up_queued_task_set_status(&xprt->sending,
  1360. task, 0);
  1361. return false;
  1362. }
  1363. if (atomic_read(&xprt->swapper))
  1364. /* This will be clear in __rpc_execute */
  1365. current->flags |= PF_MEMALLOC;
  1366. return true;
  1367. }
  1368. void xprt_end_transmit(struct rpc_task *task)
  1369. {
  1370. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1371. xprt_inject_disconnect(xprt);
  1372. xprt_release_write(xprt, task);
  1373. }
  1374. /**
  1375. * xprt_request_transmit - send an RPC request on a transport
  1376. * @req: pointer to request to transmit
  1377. * @snd_task: RPC task that owns the transport lock
  1378. *
  1379. * This performs the transmission of a single request.
  1380. * Note that if the request is not the same as snd_task, then it
  1381. * does need to be pinned.
  1382. * Returns '0' on success.
  1383. */
  1384. static int
  1385. xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
  1386. {
  1387. struct rpc_xprt *xprt = req->rq_xprt;
  1388. struct rpc_task *task = req->rq_task;
  1389. unsigned int connect_cookie;
  1390. int is_retrans = RPC_WAS_SENT(task);
  1391. int status;
  1392. if (test_bit(XPRT_CLOSE_WAIT, &xprt->state))
  1393. return -ENOTCONN;
  1394. if (!req->rq_bytes_sent) {
  1395. if (xprt_request_data_received(task)) {
  1396. status = 0;
  1397. goto out_dequeue;
  1398. }
  1399. /* Verify that our message lies in the RPCSEC_GSS window */
  1400. if (rpcauth_xmit_need_reencode(task)) {
  1401. status = -EBADMSG;
  1402. goto out_dequeue;
  1403. }
  1404. if (RPC_SIGNALLED(task)) {
  1405. status = -ERESTARTSYS;
  1406. goto out_dequeue;
  1407. }
  1408. }
  1409. /*
  1410. * Update req->rq_ntrans before transmitting to avoid races with
  1411. * xprt_update_rtt(), which needs to know that it is recording a
  1412. * reply to the first transmission.
  1413. */
  1414. req->rq_ntrans++;
  1415. trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
  1416. connect_cookie = xprt->connect_cookie;
  1417. status = xprt->ops->send_request(req);
  1418. if (status != 0) {
  1419. req->rq_ntrans--;
  1420. trace_xprt_transmit(req, status);
  1421. return status;
  1422. }
  1423. if (is_retrans) {
  1424. task->tk_client->cl_stats->rpcretrans++;
  1425. trace_xprt_retransmit(req);
  1426. }
  1427. xprt_inject_disconnect(xprt);
  1428. task->tk_flags |= RPC_TASK_SENT;
  1429. spin_lock(&xprt->transport_lock);
  1430. xprt->stat.sends++;
  1431. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  1432. xprt->stat.bklog_u += xprt->backlog.qlen;
  1433. xprt->stat.sending_u += xprt->sending.qlen;
  1434. xprt->stat.pending_u += xprt->pending.qlen;
  1435. spin_unlock(&xprt->transport_lock);
  1436. req->rq_connect_cookie = connect_cookie;
  1437. out_dequeue:
  1438. trace_xprt_transmit(req, status);
  1439. xprt_request_dequeue_transmit(task);
  1440. rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
  1441. return status;
  1442. }
  1443. /**
  1444. * xprt_transmit - send an RPC request on a transport
  1445. * @task: controlling RPC task
  1446. *
  1447. * Attempts to drain the transmit queue. On exit, either the transport
  1448. * signalled an error that needs to be handled before transmission can
  1449. * resume, or @task finished transmitting, and detected that it already
  1450. * received a reply.
  1451. */
  1452. void
  1453. xprt_transmit(struct rpc_task *task)
  1454. {
  1455. struct rpc_rqst *next, *req = task->tk_rqstp;
  1456. struct rpc_xprt *xprt = req->rq_xprt;
  1457. int status;
  1458. spin_lock(&xprt->queue_lock);
  1459. for (;;) {
  1460. next = list_first_entry_or_null(&xprt->xmit_queue,
  1461. struct rpc_rqst, rq_xmit);
  1462. if (!next)
  1463. break;
  1464. xprt_pin_rqst(next);
  1465. spin_unlock(&xprt->queue_lock);
  1466. status = xprt_request_transmit(next, task);
  1467. if (status == -EBADMSG && next != req)
  1468. status = 0;
  1469. spin_lock(&xprt->queue_lock);
  1470. xprt_unpin_rqst(next);
  1471. if (status < 0) {
  1472. if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1473. task->tk_status = status;
  1474. break;
  1475. }
  1476. /* Was @task transmitted, and has it received a reply? */
  1477. if (xprt_request_data_received(task) &&
  1478. !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1479. break;
  1480. cond_resched_lock(&xprt->queue_lock);
  1481. }
  1482. spin_unlock(&xprt->queue_lock);
  1483. }
  1484. static void xprt_complete_request_init(struct rpc_task *task)
  1485. {
  1486. if (task->tk_rqstp)
  1487. xprt_request_init(task);
  1488. }
  1489. void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
  1490. {
  1491. set_bit(XPRT_CONGESTED, &xprt->state);
  1492. rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
  1493. }
  1494. EXPORT_SYMBOL_GPL(xprt_add_backlog);
  1495. static bool __xprt_set_rq(struct rpc_task *task, void *data)
  1496. {
  1497. struct rpc_rqst *req = data;
  1498. if (task->tk_rqstp == NULL) {
  1499. memset(req, 0, sizeof(*req)); /* mark unused */
  1500. task->tk_rqstp = req;
  1501. return true;
  1502. }
  1503. return false;
  1504. }
  1505. bool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1506. {
  1507. if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) {
  1508. clear_bit(XPRT_CONGESTED, &xprt->state);
  1509. return false;
  1510. }
  1511. return true;
  1512. }
  1513. EXPORT_SYMBOL_GPL(xprt_wake_up_backlog);
  1514. static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
  1515. {
  1516. bool ret = false;
  1517. if (!test_bit(XPRT_CONGESTED, &xprt->state))
  1518. goto out;
  1519. spin_lock(&xprt->reserve_lock);
  1520. if (test_bit(XPRT_CONGESTED, &xprt->state)) {
  1521. xprt_add_backlog(xprt, task);
  1522. ret = true;
  1523. }
  1524. spin_unlock(&xprt->reserve_lock);
  1525. out:
  1526. return ret;
  1527. }
  1528. static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
  1529. {
  1530. struct rpc_rqst *req = ERR_PTR(-EAGAIN);
  1531. if (xprt->num_reqs >= xprt->max_reqs)
  1532. goto out;
  1533. ++xprt->num_reqs;
  1534. spin_unlock(&xprt->reserve_lock);
  1535. req = kzalloc_obj(*req, rpc_task_gfp_mask());
  1536. spin_lock(&xprt->reserve_lock);
  1537. if (req != NULL)
  1538. goto out;
  1539. --xprt->num_reqs;
  1540. req = ERR_PTR(-ENOMEM);
  1541. out:
  1542. return req;
  1543. }
  1544. static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1545. {
  1546. if (xprt->num_reqs > xprt->min_reqs) {
  1547. --xprt->num_reqs;
  1548. kfree(req);
  1549. return true;
  1550. }
  1551. return false;
  1552. }
  1553. void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  1554. {
  1555. struct rpc_rqst *req;
  1556. spin_lock(&xprt->reserve_lock);
  1557. if (!list_empty(&xprt->free)) {
  1558. req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  1559. list_del(&req->rq_list);
  1560. goto out_init_req;
  1561. }
  1562. req = xprt_dynamic_alloc_slot(xprt);
  1563. if (!IS_ERR(req))
  1564. goto out_init_req;
  1565. switch (PTR_ERR(req)) {
  1566. case -ENOMEM:
  1567. dprintk("RPC: dynamic allocation of request slot "
  1568. "failed! Retrying\n");
  1569. task->tk_status = -ENOMEM;
  1570. break;
  1571. case -EAGAIN:
  1572. xprt_add_backlog(xprt, task);
  1573. dprintk("RPC: waiting for request slot\n");
  1574. fallthrough;
  1575. default:
  1576. task->tk_status = -EAGAIN;
  1577. }
  1578. spin_unlock(&xprt->reserve_lock);
  1579. return;
  1580. out_init_req:
  1581. xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
  1582. xprt->num_reqs);
  1583. spin_unlock(&xprt->reserve_lock);
  1584. task->tk_status = 0;
  1585. task->tk_rqstp = req;
  1586. }
  1587. EXPORT_SYMBOL_GPL(xprt_alloc_slot);
  1588. void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1589. {
  1590. spin_lock(&xprt->reserve_lock);
  1591. if (!xprt_wake_up_backlog(xprt, req) &&
  1592. !xprt_dynamic_free_slot(xprt, req)) {
  1593. memset(req, 0, sizeof(*req)); /* mark unused */
  1594. list_add(&req->rq_list, &xprt->free);
  1595. }
  1596. spin_unlock(&xprt->reserve_lock);
  1597. }
  1598. EXPORT_SYMBOL_GPL(xprt_free_slot);
  1599. static void xprt_free_all_slots(struct rpc_xprt *xprt)
  1600. {
  1601. struct rpc_rqst *req;
  1602. while (!list_empty(&xprt->free)) {
  1603. req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
  1604. list_del(&req->rq_list);
  1605. kfree(req);
  1606. }
  1607. }
  1608. static DEFINE_IDA(rpc_xprt_ids);
  1609. void xprt_cleanup_ids(void)
  1610. {
  1611. ida_destroy(&rpc_xprt_ids);
  1612. }
  1613. static int xprt_alloc_id(struct rpc_xprt *xprt)
  1614. {
  1615. int id;
  1616. id = ida_alloc(&rpc_xprt_ids, GFP_KERNEL);
  1617. if (id < 0)
  1618. return id;
  1619. xprt->id = id;
  1620. return 0;
  1621. }
  1622. static void xprt_free_id(struct rpc_xprt *xprt)
  1623. {
  1624. ida_free(&rpc_xprt_ids, xprt->id);
  1625. }
  1626. struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
  1627. unsigned int num_prealloc,
  1628. unsigned int max_alloc)
  1629. {
  1630. struct rpc_xprt *xprt;
  1631. struct rpc_rqst *req;
  1632. int i;
  1633. xprt = kzalloc(size, GFP_KERNEL);
  1634. if (xprt == NULL)
  1635. goto out;
  1636. xprt_alloc_id(xprt);
  1637. xprt_init(xprt, net);
  1638. for (i = 0; i < num_prealloc; i++) {
  1639. req = kzalloc_obj(struct rpc_rqst);
  1640. if (!req)
  1641. goto out_free;
  1642. list_add(&req->rq_list, &xprt->free);
  1643. }
  1644. xprt->max_reqs = max_t(unsigned int, max_alloc, num_prealloc);
  1645. xprt->min_reqs = num_prealloc;
  1646. xprt->num_reqs = num_prealloc;
  1647. return xprt;
  1648. out_free:
  1649. xprt_free(xprt);
  1650. out:
  1651. return NULL;
  1652. }
  1653. EXPORT_SYMBOL_GPL(xprt_alloc);
  1654. void xprt_free(struct rpc_xprt *xprt)
  1655. {
  1656. put_net_track(xprt->xprt_net, &xprt->ns_tracker);
  1657. xprt_free_all_slots(xprt);
  1658. xprt_free_id(xprt);
  1659. rpc_sysfs_xprt_destroy(xprt);
  1660. kfree_rcu(xprt, rcu);
  1661. }
  1662. EXPORT_SYMBOL_GPL(xprt_free);
  1663. static void
  1664. xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
  1665. {
  1666. req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
  1667. }
  1668. static __be32
  1669. xprt_alloc_xid(struct rpc_xprt *xprt)
  1670. {
  1671. __be32 xid;
  1672. spin_lock(&xprt->reserve_lock);
  1673. xid = (__force __be32)xprt->xid++;
  1674. spin_unlock(&xprt->reserve_lock);
  1675. return xid;
  1676. }
  1677. static void
  1678. xprt_init_xid(struct rpc_xprt *xprt)
  1679. {
  1680. xprt->xid = get_random_u32();
  1681. }
  1682. static void
  1683. xprt_request_init(struct rpc_task *task)
  1684. {
  1685. struct rpc_xprt *xprt = task->tk_xprt;
  1686. struct rpc_rqst *req = task->tk_rqstp;
  1687. req->rq_task = task;
  1688. req->rq_xprt = xprt;
  1689. req->rq_buffer = NULL;
  1690. req->rq_xid = xprt_alloc_xid(xprt);
  1691. xprt_init_connect_cookie(req, xprt);
  1692. req->rq_snd_buf.len = 0;
  1693. req->rq_snd_buf.buflen = 0;
  1694. req->rq_rcv_buf.len = 0;
  1695. req->rq_rcv_buf.buflen = 0;
  1696. req->rq_snd_buf.bvec = NULL;
  1697. req->rq_rcv_buf.bvec = NULL;
  1698. req->rq_release_snd_buf = NULL;
  1699. req->rq_seqno_count = 0;
  1700. xprt_init_majortimeo(task, req, task->tk_client->cl_timeout);
  1701. trace_xprt_reserve(req);
  1702. }
  1703. static void
  1704. xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
  1705. {
  1706. xprt->ops->alloc_slot(xprt, task);
  1707. if (task->tk_rqstp != NULL)
  1708. xprt_request_init(task);
  1709. }
  1710. /**
  1711. * xprt_reserve - allocate an RPC request slot
  1712. * @task: RPC task requesting a slot allocation
  1713. *
  1714. * If the transport is marked as being congested, or if no more
  1715. * slots are available, place the task on the transport's
  1716. * backlog queue.
  1717. */
  1718. void xprt_reserve(struct rpc_task *task)
  1719. {
  1720. struct rpc_xprt *xprt = task->tk_xprt;
  1721. task->tk_status = 0;
  1722. if (task->tk_rqstp != NULL)
  1723. return;
  1724. task->tk_status = -EAGAIN;
  1725. if (!xprt_throttle_congested(xprt, task))
  1726. xprt_do_reserve(xprt, task);
  1727. }
  1728. /**
  1729. * xprt_retry_reserve - allocate an RPC request slot
  1730. * @task: RPC task requesting a slot allocation
  1731. *
  1732. * If no more slots are available, place the task on the transport's
  1733. * backlog queue.
  1734. * Note that the only difference with xprt_reserve is that we now
  1735. * ignore the value of the XPRT_CONGESTED flag.
  1736. */
  1737. void xprt_retry_reserve(struct rpc_task *task)
  1738. {
  1739. struct rpc_xprt *xprt = task->tk_xprt;
  1740. task->tk_status = 0;
  1741. if (task->tk_rqstp != NULL)
  1742. return;
  1743. task->tk_status = -EAGAIN;
  1744. xprt_do_reserve(xprt, task);
  1745. }
  1746. /**
  1747. * xprt_release - release an RPC request slot
  1748. * @task: task which is finished with the slot
  1749. *
  1750. */
  1751. void xprt_release(struct rpc_task *task)
  1752. {
  1753. struct rpc_xprt *xprt;
  1754. struct rpc_rqst *req = task->tk_rqstp;
  1755. if (req == NULL) {
  1756. if (task->tk_client) {
  1757. xprt = task->tk_xprt;
  1758. xprt_release_write(xprt, task);
  1759. }
  1760. return;
  1761. }
  1762. xprt = req->rq_xprt;
  1763. xprt_request_dequeue_xprt(task);
  1764. spin_lock(&xprt->transport_lock);
  1765. xprt->ops->release_xprt(xprt, task);
  1766. if (xprt->ops->release_request)
  1767. xprt->ops->release_request(task);
  1768. xprt_schedule_autodisconnect(xprt);
  1769. spin_unlock(&xprt->transport_lock);
  1770. if (req->rq_buffer)
  1771. xprt->ops->buf_free(task);
  1772. if (req->rq_cred != NULL)
  1773. put_rpccred(req->rq_cred);
  1774. if (req->rq_release_snd_buf)
  1775. req->rq_release_snd_buf(req);
  1776. task->tk_rqstp = NULL;
  1777. if (likely(!bc_prealloc(req)))
  1778. xprt->ops->free_slot(xprt, req);
  1779. else
  1780. xprt_free_bc_request(req);
  1781. }
  1782. #ifdef CONFIG_SUNRPC_BACKCHANNEL
  1783. void
  1784. xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task,
  1785. const struct rpc_timeout *to)
  1786. {
  1787. struct xdr_buf *xbufp = &req->rq_snd_buf;
  1788. task->tk_rqstp = req;
  1789. req->rq_task = task;
  1790. xprt_init_connect_cookie(req, req->rq_xprt);
  1791. /*
  1792. * Set up the xdr_buf length.
  1793. * This also indicates that the buffer is XDR encoded already.
  1794. */
  1795. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  1796. xbufp->tail[0].iov_len;
  1797. /*
  1798. * Backchannel Replies are sent with !RPC_TASK_SOFT and
  1799. * RPC_TASK_NO_RETRANS_TIMEOUT. The major timeout setting
  1800. * affects only how long each Reply waits to be sent when
  1801. * a transport connection cannot be established.
  1802. */
  1803. xprt_init_majortimeo(task, req, to);
  1804. }
  1805. #endif
  1806. static void xprt_init(struct rpc_xprt *xprt, struct net *net)
  1807. {
  1808. kref_init(&xprt->kref);
  1809. spin_lock_init(&xprt->transport_lock);
  1810. spin_lock_init(&xprt->reserve_lock);
  1811. spin_lock_init(&xprt->queue_lock);
  1812. INIT_LIST_HEAD(&xprt->free);
  1813. xprt->recv_queue = RB_ROOT;
  1814. INIT_LIST_HEAD(&xprt->xmit_queue);
  1815. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1816. spin_lock_init(&xprt->bc_pa_lock);
  1817. INIT_LIST_HEAD(&xprt->bc_pa_list);
  1818. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1819. INIT_LIST_HEAD(&xprt->xprt_switch);
  1820. xprt->last_used = jiffies;
  1821. xprt->cwnd = RPC_INITCWND;
  1822. xprt->bind_index = 0;
  1823. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1824. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1825. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  1826. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1827. xprt_init_xid(xprt);
  1828. xprt->xprt_net = get_net_track(net, &xprt->ns_tracker, GFP_KERNEL);
  1829. }
  1830. /**
  1831. * xprt_create_transport - create an RPC transport
  1832. * @args: rpc transport creation arguments
  1833. *
  1834. */
  1835. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  1836. {
  1837. struct rpc_xprt *xprt;
  1838. const struct xprt_class *t;
  1839. t = xprt_class_find_by_ident(args->ident);
  1840. if (!t) {
  1841. dprintk("RPC: transport (%d) not supported\n", args->ident);
  1842. return ERR_PTR(-EIO);
  1843. }
  1844. xprt = t->setup(args);
  1845. xprt_class_release(t);
  1846. if (IS_ERR(xprt))
  1847. goto out;
  1848. if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
  1849. xprt->idle_timeout = 0;
  1850. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  1851. if (xprt_has_timer(xprt))
  1852. timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
  1853. else
  1854. timer_setup(&xprt->timer, NULL, 0);
  1855. if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
  1856. xprt_destroy(xprt);
  1857. return ERR_PTR(-EINVAL);
  1858. }
  1859. xprt->servername = kstrdup(args->servername, GFP_KERNEL);
  1860. if (xprt->servername == NULL) {
  1861. xprt_destroy(xprt);
  1862. return ERR_PTR(-ENOMEM);
  1863. }
  1864. rpc_xprt_debugfs_register(xprt);
  1865. trace_xprt_create(xprt);
  1866. out:
  1867. return xprt;
  1868. }
  1869. static void xprt_destroy_cb(struct work_struct *work)
  1870. {
  1871. struct rpc_xprt *xprt =
  1872. container_of(work, struct rpc_xprt, task_cleanup);
  1873. trace_xprt_destroy(xprt);
  1874. rpc_xprt_debugfs_unregister(xprt);
  1875. rpc_destroy_wait_queue(&xprt->binding);
  1876. rpc_destroy_wait_queue(&xprt->pending);
  1877. rpc_destroy_wait_queue(&xprt->sending);
  1878. rpc_destroy_wait_queue(&xprt->backlog);
  1879. kfree(xprt->servername);
  1880. /*
  1881. * Destroy any existing back channel
  1882. */
  1883. xprt_destroy_backchannel(xprt, UINT_MAX);
  1884. /*
  1885. * Tear down transport state and free the rpc_xprt
  1886. */
  1887. xprt->ops->destroy(xprt);
  1888. }
  1889. /**
  1890. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1891. * @xprt: transport to destroy
  1892. *
  1893. */
  1894. static void xprt_destroy(struct rpc_xprt *xprt)
  1895. {
  1896. /*
  1897. * Exclude transport connect/disconnect handlers and autoclose
  1898. */
  1899. wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
  1900. /*
  1901. * xprt_schedule_autodisconnect() can run after XPRT_LOCKED
  1902. * is cleared. We use ->transport_lock to ensure the mod_timer()
  1903. * can only run *before* del_time_sync(), never after.
  1904. */
  1905. spin_lock(&xprt->transport_lock);
  1906. timer_delete_sync(&xprt->timer);
  1907. spin_unlock(&xprt->transport_lock);
  1908. /*
  1909. * Destroy sockets etc from the system workqueue so they can
  1910. * safely flush receive work running on rpciod.
  1911. */
  1912. INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
  1913. schedule_work(&xprt->task_cleanup);
  1914. }
  1915. static void xprt_destroy_kref(struct kref *kref)
  1916. {
  1917. xprt_destroy(container_of(kref, struct rpc_xprt, kref));
  1918. }
  1919. /**
  1920. * xprt_get - return a reference to an RPC transport.
  1921. * @xprt: pointer to the transport
  1922. *
  1923. */
  1924. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1925. {
  1926. if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
  1927. return xprt;
  1928. return NULL;
  1929. }
  1930. EXPORT_SYMBOL_GPL(xprt_get);
  1931. /**
  1932. * xprt_put - release a reference to an RPC transport.
  1933. * @xprt: pointer to the transport
  1934. *
  1935. */
  1936. void xprt_put(struct rpc_xprt *xprt)
  1937. {
  1938. if (xprt != NULL)
  1939. kref_put(&xprt->kref, xprt_destroy_kref);
  1940. }
  1941. EXPORT_SYMBOL_GPL(xprt_put);
  1942. void xprt_set_offline_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
  1943. {
  1944. if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
  1945. spin_lock(&xps->xps_lock);
  1946. xps->xps_nactive--;
  1947. spin_unlock(&xps->xps_lock);
  1948. }
  1949. }
  1950. void xprt_set_online_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
  1951. {
  1952. if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
  1953. spin_lock(&xps->xps_lock);
  1954. xps->xps_nactive++;
  1955. spin_unlock(&xps->xps_lock);
  1956. }
  1957. }
  1958. void xprt_delete_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
  1959. {
  1960. if (test_and_set_bit(XPRT_REMOVE, &xprt->state))
  1961. return;
  1962. xprt_force_disconnect(xprt);
  1963. if (!test_bit(XPRT_CONNECTED, &xprt->state))
  1964. return;
  1965. if (!xprt->sending.qlen && !xprt->pending.qlen &&
  1966. !xprt->backlog.qlen && !atomic_long_read(&xprt->queuelen))
  1967. rpc_xprt_switch_remove_xprt(xps, xprt, true);
  1968. }