xprt.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  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 <[email protected]>
  37. *
  38. * Transport switch API copyright (C) 2005, Chuck Lever <[email protected]>
  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))
  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))
  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. {
  591. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  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. {
  603. req->rq_majortimeo += xprt_calc_majortimeo(req);
  604. }
  605. static void xprt_reset_minortimeo(struct rpc_rqst *req)
  606. {
  607. req->rq_minortimeo += req->rq_timeout;
  608. }
  609. static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
  610. {
  611. unsigned long time_init;
  612. struct rpc_xprt *xprt = req->rq_xprt;
  613. if (likely(xprt && xprt_connected(xprt)))
  614. time_init = jiffies;
  615. else
  616. time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
  617. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  618. req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
  619. req->rq_minortimeo = time_init + req->rq_timeout;
  620. }
  621. /**
  622. * xprt_adjust_timeout - adjust timeout values for next retransmit
  623. * @req: RPC request containing parameters to use for the adjustment
  624. *
  625. */
  626. int xprt_adjust_timeout(struct rpc_rqst *req)
  627. {
  628. struct rpc_xprt *xprt = req->rq_xprt;
  629. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  630. int status = 0;
  631. if (time_before(jiffies, req->rq_majortimeo)) {
  632. if (time_before(jiffies, req->rq_minortimeo))
  633. return status;
  634. if (to->to_exponential)
  635. req->rq_timeout <<= 1;
  636. else
  637. req->rq_timeout += to->to_increment;
  638. if (to->to_maxval && req->rq_timeout >= to->to_maxval)
  639. req->rq_timeout = to->to_maxval;
  640. req->rq_retries++;
  641. } else {
  642. req->rq_timeout = to->to_initval;
  643. req->rq_retries = 0;
  644. xprt_reset_majortimeo(req);
  645. /* Reset the RTT counters == "slow start" */
  646. spin_lock(&xprt->transport_lock);
  647. rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
  648. spin_unlock(&xprt->transport_lock);
  649. status = -ETIMEDOUT;
  650. }
  651. xprt_reset_minortimeo(req);
  652. if (req->rq_timeout == 0) {
  653. printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
  654. req->rq_timeout = 5 * HZ;
  655. }
  656. return status;
  657. }
  658. static void xprt_autoclose(struct work_struct *work)
  659. {
  660. struct rpc_xprt *xprt =
  661. container_of(work, struct rpc_xprt, task_cleanup);
  662. unsigned int pflags = memalloc_nofs_save();
  663. trace_xprt_disconnect_auto(xprt);
  664. xprt->connect_cookie++;
  665. smp_mb__before_atomic();
  666. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  667. xprt->ops->close(xprt);
  668. xprt_release_write(xprt, NULL);
  669. wake_up_bit(&xprt->state, XPRT_LOCKED);
  670. memalloc_nofs_restore(pflags);
  671. }
  672. /**
  673. * xprt_disconnect_done - mark a transport as disconnected
  674. * @xprt: transport to flag for disconnect
  675. *
  676. */
  677. void xprt_disconnect_done(struct rpc_xprt *xprt)
  678. {
  679. trace_xprt_disconnect_done(xprt);
  680. spin_lock(&xprt->transport_lock);
  681. xprt_clear_connected(xprt);
  682. xprt_clear_write_space_locked(xprt);
  683. xprt_clear_congestion_window_wait_locked(xprt);
  684. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  685. spin_unlock(&xprt->transport_lock);
  686. }
  687. EXPORT_SYMBOL_GPL(xprt_disconnect_done);
  688. /**
  689. * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
  690. * @xprt: transport to disconnect
  691. */
  692. static void xprt_schedule_autoclose_locked(struct rpc_xprt *xprt)
  693. {
  694. if (test_and_set_bit(XPRT_CLOSE_WAIT, &xprt->state))
  695. return;
  696. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  697. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  698. else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
  699. rpc_wake_up_queued_task_set_status(&xprt->pending,
  700. xprt->snd_task, -ENOTCONN);
  701. }
  702. /**
  703. * xprt_force_disconnect - force a transport to disconnect
  704. * @xprt: transport to disconnect
  705. *
  706. */
  707. void xprt_force_disconnect(struct rpc_xprt *xprt)
  708. {
  709. trace_xprt_disconnect_force(xprt);
  710. /* Don't race with the test_bit() in xprt_clear_locked() */
  711. spin_lock(&xprt->transport_lock);
  712. xprt_schedule_autoclose_locked(xprt);
  713. spin_unlock(&xprt->transport_lock);
  714. }
  715. EXPORT_SYMBOL_GPL(xprt_force_disconnect);
  716. static unsigned int
  717. xprt_connect_cookie(struct rpc_xprt *xprt)
  718. {
  719. return READ_ONCE(xprt->connect_cookie);
  720. }
  721. static bool
  722. xprt_request_retransmit_after_disconnect(struct rpc_task *task)
  723. {
  724. struct rpc_rqst *req = task->tk_rqstp;
  725. struct rpc_xprt *xprt = req->rq_xprt;
  726. return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
  727. !xprt_connected(xprt);
  728. }
  729. /**
  730. * xprt_conditional_disconnect - force a transport to disconnect
  731. * @xprt: transport to disconnect
  732. * @cookie: 'connection cookie'
  733. *
  734. * This attempts to break the connection if and only if 'cookie' matches
  735. * the current transport 'connection cookie'. It ensures that we don't
  736. * try to break the connection more than once when we need to retransmit
  737. * a batch of RPC requests.
  738. *
  739. */
  740. void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
  741. {
  742. /* Don't race with the test_bit() in xprt_clear_locked() */
  743. spin_lock(&xprt->transport_lock);
  744. if (cookie != xprt->connect_cookie)
  745. goto out;
  746. if (test_bit(XPRT_CLOSING, &xprt->state))
  747. goto out;
  748. xprt_schedule_autoclose_locked(xprt);
  749. out:
  750. spin_unlock(&xprt->transport_lock);
  751. }
  752. static bool
  753. xprt_has_timer(const struct rpc_xprt *xprt)
  754. {
  755. return xprt->idle_timeout != 0;
  756. }
  757. static void
  758. xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
  759. __must_hold(&xprt->transport_lock)
  760. {
  761. xprt->last_used = jiffies;
  762. if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
  763. mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
  764. }
  765. static void
  766. xprt_init_autodisconnect(struct timer_list *t)
  767. {
  768. struct rpc_xprt *xprt = from_timer(xprt, t, timer);
  769. if (!RB_EMPTY_ROOT(&xprt->recv_queue))
  770. return;
  771. /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
  772. xprt->last_used = jiffies;
  773. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  774. return;
  775. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  776. }
  777. #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
  778. static void xprt_inject_disconnect(struct rpc_xprt *xprt)
  779. {
  780. if (!fail_sunrpc.ignore_client_disconnect &&
  781. should_fail(&fail_sunrpc.attr, 1))
  782. xprt->ops->inject_disconnect(xprt);
  783. }
  784. #else
  785. static inline void xprt_inject_disconnect(struct rpc_xprt *xprt)
  786. {
  787. }
  788. #endif
  789. bool xprt_lock_connect(struct rpc_xprt *xprt,
  790. struct rpc_task *task,
  791. void *cookie)
  792. {
  793. bool ret = false;
  794. spin_lock(&xprt->transport_lock);
  795. if (!test_bit(XPRT_LOCKED, &xprt->state))
  796. goto out;
  797. if (xprt->snd_task != task)
  798. goto out;
  799. set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
  800. xprt->snd_task = cookie;
  801. ret = true;
  802. out:
  803. spin_unlock(&xprt->transport_lock);
  804. return ret;
  805. }
  806. EXPORT_SYMBOL_GPL(xprt_lock_connect);
  807. void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
  808. {
  809. spin_lock(&xprt->transport_lock);
  810. if (xprt->snd_task != cookie)
  811. goto out;
  812. if (!test_bit(XPRT_LOCKED, &xprt->state))
  813. goto out;
  814. xprt->snd_task =NULL;
  815. clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
  816. xprt->ops->release_xprt(xprt, NULL);
  817. xprt_schedule_autodisconnect(xprt);
  818. out:
  819. spin_unlock(&xprt->transport_lock);
  820. wake_up_bit(&xprt->state, XPRT_LOCKED);
  821. }
  822. EXPORT_SYMBOL_GPL(xprt_unlock_connect);
  823. /**
  824. * xprt_connect - schedule a transport connect operation
  825. * @task: RPC task that is requesting the connect
  826. *
  827. */
  828. void xprt_connect(struct rpc_task *task)
  829. {
  830. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  831. trace_xprt_connect(xprt);
  832. if (!xprt_bound(xprt)) {
  833. task->tk_status = -EAGAIN;
  834. return;
  835. }
  836. if (!xprt_lock_write(xprt, task))
  837. return;
  838. if (!xprt_connected(xprt) && !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
  839. task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
  840. rpc_sleep_on_timeout(&xprt->pending, task, NULL,
  841. xprt_request_timeout(task->tk_rqstp));
  842. if (test_bit(XPRT_CLOSING, &xprt->state))
  843. return;
  844. if (xprt_test_and_set_connecting(xprt))
  845. return;
  846. /* Race breaker */
  847. if (!xprt_connected(xprt)) {
  848. xprt->stat.connect_start = jiffies;
  849. xprt->ops->connect(xprt, task);
  850. } else {
  851. xprt_clear_connecting(xprt);
  852. task->tk_status = 0;
  853. rpc_wake_up_queued_task(&xprt->pending, task);
  854. }
  855. }
  856. xprt_release_write(xprt, task);
  857. }
  858. /**
  859. * xprt_reconnect_delay - compute the wait before scheduling a connect
  860. * @xprt: transport instance
  861. *
  862. */
  863. unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
  864. {
  865. unsigned long start, now = jiffies;
  866. start = xprt->stat.connect_start + xprt->reestablish_timeout;
  867. if (time_after(start, now))
  868. return start - now;
  869. return 0;
  870. }
  871. EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
  872. /**
  873. * xprt_reconnect_backoff - compute the new re-establish timeout
  874. * @xprt: transport instance
  875. * @init_to: initial reestablish timeout
  876. *
  877. */
  878. void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
  879. {
  880. xprt->reestablish_timeout <<= 1;
  881. if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
  882. xprt->reestablish_timeout = xprt->max_reconnect_timeout;
  883. if (xprt->reestablish_timeout < init_to)
  884. xprt->reestablish_timeout = init_to;
  885. }
  886. EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
  887. enum xprt_xid_rb_cmp {
  888. XID_RB_EQUAL,
  889. XID_RB_LEFT,
  890. XID_RB_RIGHT,
  891. };
  892. static enum xprt_xid_rb_cmp
  893. xprt_xid_cmp(__be32 xid1, __be32 xid2)
  894. {
  895. if (xid1 == xid2)
  896. return XID_RB_EQUAL;
  897. if ((__force u32)xid1 < (__force u32)xid2)
  898. return XID_RB_LEFT;
  899. return XID_RB_RIGHT;
  900. }
  901. static struct rpc_rqst *
  902. xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
  903. {
  904. struct rb_node *n = xprt->recv_queue.rb_node;
  905. struct rpc_rqst *req;
  906. while (n != NULL) {
  907. req = rb_entry(n, struct rpc_rqst, rq_recv);
  908. switch (xprt_xid_cmp(xid, req->rq_xid)) {
  909. case XID_RB_LEFT:
  910. n = n->rb_left;
  911. break;
  912. case XID_RB_RIGHT:
  913. n = n->rb_right;
  914. break;
  915. case XID_RB_EQUAL:
  916. return req;
  917. }
  918. }
  919. return NULL;
  920. }
  921. static void
  922. xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
  923. {
  924. struct rb_node **p = &xprt->recv_queue.rb_node;
  925. struct rb_node *n = NULL;
  926. struct rpc_rqst *req;
  927. while (*p != NULL) {
  928. n = *p;
  929. req = rb_entry(n, struct rpc_rqst, rq_recv);
  930. switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
  931. case XID_RB_LEFT:
  932. p = &n->rb_left;
  933. break;
  934. case XID_RB_RIGHT:
  935. p = &n->rb_right;
  936. break;
  937. case XID_RB_EQUAL:
  938. WARN_ON_ONCE(new != req);
  939. return;
  940. }
  941. }
  942. rb_link_node(&new->rq_recv, n, p);
  943. rb_insert_color(&new->rq_recv, &xprt->recv_queue);
  944. }
  945. static void
  946. xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
  947. {
  948. rb_erase(&req->rq_recv, &xprt->recv_queue);
  949. }
  950. /**
  951. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  952. * @xprt: transport on which the original request was transmitted
  953. * @xid: RPC XID of incoming reply
  954. *
  955. * Caller holds xprt->queue_lock.
  956. */
  957. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  958. {
  959. struct rpc_rqst *entry;
  960. entry = xprt_request_rb_find(xprt, xid);
  961. if (entry != NULL) {
  962. trace_xprt_lookup_rqst(xprt, xid, 0);
  963. entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
  964. return entry;
  965. }
  966. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  967. ntohl(xid));
  968. trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
  969. xprt->stat.bad_xids++;
  970. return NULL;
  971. }
  972. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  973. static bool
  974. xprt_is_pinned_rqst(struct rpc_rqst *req)
  975. {
  976. return atomic_read(&req->rq_pin) != 0;
  977. }
  978. /**
  979. * xprt_pin_rqst - Pin a request on the transport receive list
  980. * @req: Request to pin
  981. *
  982. * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
  983. * so should be holding xprt->queue_lock.
  984. */
  985. void xprt_pin_rqst(struct rpc_rqst *req)
  986. {
  987. atomic_inc(&req->rq_pin);
  988. }
  989. EXPORT_SYMBOL_GPL(xprt_pin_rqst);
  990. /**
  991. * xprt_unpin_rqst - Unpin a request on the transport receive list
  992. * @req: Request to pin
  993. *
  994. * Caller should be holding xprt->queue_lock.
  995. */
  996. void xprt_unpin_rqst(struct rpc_rqst *req)
  997. {
  998. if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
  999. atomic_dec(&req->rq_pin);
  1000. return;
  1001. }
  1002. if (atomic_dec_and_test(&req->rq_pin))
  1003. wake_up_var(&req->rq_pin);
  1004. }
  1005. EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
  1006. static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
  1007. {
  1008. wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
  1009. }
  1010. static bool
  1011. xprt_request_data_received(struct rpc_task *task)
  1012. {
  1013. return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
  1014. READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
  1015. }
  1016. static bool
  1017. xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
  1018. {
  1019. return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
  1020. READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
  1021. }
  1022. /**
  1023. * xprt_request_enqueue_receive - Add an request to the receive queue
  1024. * @task: RPC task
  1025. *
  1026. */
  1027. int
  1028. xprt_request_enqueue_receive(struct rpc_task *task)
  1029. {
  1030. struct rpc_rqst *req = task->tk_rqstp;
  1031. struct rpc_xprt *xprt = req->rq_xprt;
  1032. int ret;
  1033. if (!xprt_request_need_enqueue_receive(task, req))
  1034. return 0;
  1035. ret = xprt_request_prepare(task->tk_rqstp, &req->rq_rcv_buf);
  1036. if (ret)
  1037. return ret;
  1038. spin_lock(&xprt->queue_lock);
  1039. /* Update the softirq receive buffer */
  1040. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  1041. sizeof(req->rq_private_buf));
  1042. /* Add request to the receive list */
  1043. xprt_request_rb_insert(xprt, req);
  1044. set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
  1045. spin_unlock(&xprt->queue_lock);
  1046. /* Turn off autodisconnect */
  1047. del_singleshot_timer_sync(&xprt->timer);
  1048. return 0;
  1049. }
  1050. /**
  1051. * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
  1052. * @task: RPC task
  1053. *
  1054. * Caller must hold xprt->queue_lock.
  1055. */
  1056. static void
  1057. xprt_request_dequeue_receive_locked(struct rpc_task *task)
  1058. {
  1059. struct rpc_rqst *req = task->tk_rqstp;
  1060. if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
  1061. xprt_request_rb_remove(req->rq_xprt, req);
  1062. }
  1063. /**
  1064. * xprt_update_rtt - Update RPC RTT statistics
  1065. * @task: RPC request that recently completed
  1066. *
  1067. * Caller holds xprt->queue_lock.
  1068. */
  1069. void xprt_update_rtt(struct rpc_task *task)
  1070. {
  1071. struct rpc_rqst *req = task->tk_rqstp;
  1072. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  1073. unsigned int timer = task->tk_msg.rpc_proc->p_timer;
  1074. long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
  1075. if (timer) {
  1076. if (req->rq_ntrans == 1)
  1077. rpc_update_rtt(rtt, timer, m);
  1078. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  1079. }
  1080. }
  1081. EXPORT_SYMBOL_GPL(xprt_update_rtt);
  1082. /**
  1083. * xprt_complete_rqst - called when reply processing is complete
  1084. * @task: RPC request that recently completed
  1085. * @copied: actual number of bytes received from the transport
  1086. *
  1087. * Caller holds xprt->queue_lock.
  1088. */
  1089. void xprt_complete_rqst(struct rpc_task *task, int copied)
  1090. {
  1091. struct rpc_rqst *req = task->tk_rqstp;
  1092. struct rpc_xprt *xprt = req->rq_xprt;
  1093. xprt->stat.recvs++;
  1094. xdr_free_bvec(&req->rq_rcv_buf);
  1095. req->rq_private_buf.bvec = NULL;
  1096. req->rq_private_buf.len = copied;
  1097. /* Ensure all writes are done before we update */
  1098. /* req->rq_reply_bytes_recvd */
  1099. smp_wmb();
  1100. req->rq_reply_bytes_recvd = copied;
  1101. xprt_request_dequeue_receive_locked(task);
  1102. rpc_wake_up_queued_task(&xprt->pending, task);
  1103. }
  1104. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  1105. static void xprt_timer(struct rpc_task *task)
  1106. {
  1107. struct rpc_rqst *req = task->tk_rqstp;
  1108. struct rpc_xprt *xprt = req->rq_xprt;
  1109. if (task->tk_status != -ETIMEDOUT)
  1110. return;
  1111. trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
  1112. if (!req->rq_reply_bytes_recvd) {
  1113. if (xprt->ops->timer)
  1114. xprt->ops->timer(xprt, task);
  1115. } else
  1116. task->tk_status = 0;
  1117. }
  1118. /**
  1119. * xprt_wait_for_reply_request_def - wait for reply
  1120. * @task: pointer to rpc_task
  1121. *
  1122. * Set a request's retransmit timeout based on the transport's
  1123. * default timeout parameters. Used by transports that don't adjust
  1124. * the retransmit timeout based on round-trip time estimation,
  1125. * and put the task to sleep on the pending queue.
  1126. */
  1127. void xprt_wait_for_reply_request_def(struct rpc_task *task)
  1128. {
  1129. struct rpc_rqst *req = task->tk_rqstp;
  1130. rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
  1131. xprt_request_timeout(req));
  1132. }
  1133. EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
  1134. /**
  1135. * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
  1136. * @task: pointer to rpc_task
  1137. *
  1138. * Set a request's retransmit timeout using the RTT estimator,
  1139. * and put the task to sleep on the pending queue.
  1140. */
  1141. void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
  1142. {
  1143. int timer = task->tk_msg.rpc_proc->p_timer;
  1144. struct rpc_clnt *clnt = task->tk_client;
  1145. struct rpc_rtt *rtt = clnt->cl_rtt;
  1146. struct rpc_rqst *req = task->tk_rqstp;
  1147. unsigned long max_timeout = clnt->cl_timeout->to_maxval;
  1148. unsigned long timeout;
  1149. timeout = rpc_calc_rto(rtt, timer);
  1150. timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
  1151. if (timeout > max_timeout || timeout == 0)
  1152. timeout = max_timeout;
  1153. rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
  1154. jiffies + timeout);
  1155. }
  1156. EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
  1157. /**
  1158. * xprt_request_wait_receive - wait for the reply to an RPC request
  1159. * @task: RPC task about to send a request
  1160. *
  1161. */
  1162. void xprt_request_wait_receive(struct rpc_task *task)
  1163. {
  1164. struct rpc_rqst *req = task->tk_rqstp;
  1165. struct rpc_xprt *xprt = req->rq_xprt;
  1166. if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
  1167. return;
  1168. /*
  1169. * Sleep on the pending queue if we're expecting a reply.
  1170. * The spinlock ensures atomicity between the test of
  1171. * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
  1172. */
  1173. spin_lock(&xprt->queue_lock);
  1174. if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
  1175. xprt->ops->wait_for_reply_request(task);
  1176. /*
  1177. * Send an extra queue wakeup call if the
  1178. * connection was dropped in case the call to
  1179. * rpc_sleep_on() raced.
  1180. */
  1181. if (xprt_request_retransmit_after_disconnect(task))
  1182. rpc_wake_up_queued_task_set_status(&xprt->pending,
  1183. task, -ENOTCONN);
  1184. }
  1185. spin_unlock(&xprt->queue_lock);
  1186. }
  1187. static bool
  1188. xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
  1189. {
  1190. return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
  1191. }
  1192. /**
  1193. * xprt_request_enqueue_transmit - queue a task for transmission
  1194. * @task: pointer to rpc_task
  1195. *
  1196. * Add a task to the transmission queue.
  1197. */
  1198. void
  1199. xprt_request_enqueue_transmit(struct rpc_task *task)
  1200. {
  1201. struct rpc_rqst *pos, *req = task->tk_rqstp;
  1202. struct rpc_xprt *xprt = req->rq_xprt;
  1203. int ret;
  1204. if (xprt_request_need_enqueue_transmit(task, req)) {
  1205. ret = xprt_request_prepare(task->tk_rqstp, &req->rq_snd_buf);
  1206. if (ret) {
  1207. task->tk_status = ret;
  1208. return;
  1209. }
  1210. req->rq_bytes_sent = 0;
  1211. spin_lock(&xprt->queue_lock);
  1212. /*
  1213. * Requests that carry congestion control credits are added
  1214. * to the head of the list to avoid starvation issues.
  1215. */
  1216. if (req->rq_cong) {
  1217. xprt_clear_congestion_window_wait(xprt);
  1218. list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
  1219. if (pos->rq_cong)
  1220. continue;
  1221. /* Note: req is added _before_ pos */
  1222. list_add_tail(&req->rq_xmit, &pos->rq_xmit);
  1223. INIT_LIST_HEAD(&req->rq_xmit2);
  1224. goto out;
  1225. }
  1226. } else if (!req->rq_seqno) {
  1227. list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
  1228. if (pos->rq_task->tk_owner != task->tk_owner)
  1229. continue;
  1230. list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
  1231. INIT_LIST_HEAD(&req->rq_xmit);
  1232. goto out;
  1233. }
  1234. }
  1235. list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
  1236. INIT_LIST_HEAD(&req->rq_xmit2);
  1237. out:
  1238. atomic_long_inc(&xprt->xmit_queuelen);
  1239. set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
  1240. spin_unlock(&xprt->queue_lock);
  1241. }
  1242. }
  1243. /**
  1244. * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
  1245. * @task: pointer to rpc_task
  1246. *
  1247. * Remove a task from the transmission queue
  1248. * Caller must hold xprt->queue_lock
  1249. */
  1250. static void
  1251. xprt_request_dequeue_transmit_locked(struct rpc_task *task)
  1252. {
  1253. struct rpc_rqst *req = task->tk_rqstp;
  1254. if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1255. return;
  1256. if (!list_empty(&req->rq_xmit)) {
  1257. list_del(&req->rq_xmit);
  1258. if (!list_empty(&req->rq_xmit2)) {
  1259. struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
  1260. struct rpc_rqst, rq_xmit2);
  1261. list_del(&req->rq_xmit2);
  1262. list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
  1263. }
  1264. } else
  1265. list_del(&req->rq_xmit2);
  1266. atomic_long_dec(&req->rq_xprt->xmit_queuelen);
  1267. xdr_free_bvec(&req->rq_snd_buf);
  1268. }
  1269. /**
  1270. * xprt_request_dequeue_transmit - remove a task from the transmission queue
  1271. * @task: pointer to rpc_task
  1272. *
  1273. * Remove a task from the transmission queue
  1274. */
  1275. static void
  1276. xprt_request_dequeue_transmit(struct rpc_task *task)
  1277. {
  1278. struct rpc_rqst *req = task->tk_rqstp;
  1279. struct rpc_xprt *xprt = req->rq_xprt;
  1280. spin_lock(&xprt->queue_lock);
  1281. xprt_request_dequeue_transmit_locked(task);
  1282. spin_unlock(&xprt->queue_lock);
  1283. }
  1284. /**
  1285. * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
  1286. * @task: pointer to rpc_task
  1287. *
  1288. * Remove a task from the transmit and receive queues, and ensure that
  1289. * it is not pinned by the receive work item.
  1290. */
  1291. void
  1292. xprt_request_dequeue_xprt(struct rpc_task *task)
  1293. {
  1294. struct rpc_rqst *req = task->tk_rqstp;
  1295. struct rpc_xprt *xprt = req->rq_xprt;
  1296. if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
  1297. test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
  1298. xprt_is_pinned_rqst(req)) {
  1299. spin_lock(&xprt->queue_lock);
  1300. while (xprt_is_pinned_rqst(req)) {
  1301. set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
  1302. spin_unlock(&xprt->queue_lock);
  1303. xprt_wait_on_pinned_rqst(req);
  1304. spin_lock(&xprt->queue_lock);
  1305. clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
  1306. }
  1307. xprt_request_dequeue_transmit_locked(task);
  1308. xprt_request_dequeue_receive_locked(task);
  1309. spin_unlock(&xprt->queue_lock);
  1310. xdr_free_bvec(&req->rq_rcv_buf);
  1311. }
  1312. }
  1313. /**
  1314. * xprt_request_prepare - prepare an encoded request for transport
  1315. * @req: pointer to rpc_rqst
  1316. * @buf: pointer to send/rcv xdr_buf
  1317. *
  1318. * Calls into the transport layer to do whatever is needed to prepare
  1319. * the request for transmission or receive.
  1320. * Returns error, or zero.
  1321. */
  1322. static int
  1323. xprt_request_prepare(struct rpc_rqst *req, struct xdr_buf *buf)
  1324. {
  1325. struct rpc_xprt *xprt = req->rq_xprt;
  1326. if (xprt->ops->prepare_request)
  1327. return xprt->ops->prepare_request(req, buf);
  1328. return 0;
  1329. }
  1330. /**
  1331. * xprt_request_need_retransmit - Test if a task needs retransmission
  1332. * @task: pointer to rpc_task
  1333. *
  1334. * Test for whether a connection breakage requires the task to retransmit
  1335. */
  1336. bool
  1337. xprt_request_need_retransmit(struct rpc_task *task)
  1338. {
  1339. return xprt_request_retransmit_after_disconnect(task);
  1340. }
  1341. /**
  1342. * xprt_prepare_transmit - reserve the transport before sending a request
  1343. * @task: RPC task about to send a request
  1344. *
  1345. */
  1346. bool xprt_prepare_transmit(struct rpc_task *task)
  1347. {
  1348. struct rpc_rqst *req = task->tk_rqstp;
  1349. struct rpc_xprt *xprt = req->rq_xprt;
  1350. if (!xprt_lock_write(xprt, task)) {
  1351. /* Race breaker: someone may have transmitted us */
  1352. if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1353. rpc_wake_up_queued_task_set_status(&xprt->sending,
  1354. task, 0);
  1355. return false;
  1356. }
  1357. if (atomic_read(&xprt->swapper))
  1358. /* This will be clear in __rpc_execute */
  1359. current->flags |= PF_MEMALLOC;
  1360. return true;
  1361. }
  1362. void xprt_end_transmit(struct rpc_task *task)
  1363. {
  1364. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1365. xprt_inject_disconnect(xprt);
  1366. xprt_release_write(xprt, task);
  1367. }
  1368. /**
  1369. * xprt_request_transmit - send an RPC request on a transport
  1370. * @req: pointer to request to transmit
  1371. * @snd_task: RPC task that owns the transport lock
  1372. *
  1373. * This performs the transmission of a single request.
  1374. * Note that if the request is not the same as snd_task, then it
  1375. * does need to be pinned.
  1376. * Returns '0' on success.
  1377. */
  1378. static int
  1379. xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
  1380. {
  1381. struct rpc_xprt *xprt = req->rq_xprt;
  1382. struct rpc_task *task = req->rq_task;
  1383. unsigned int connect_cookie;
  1384. int is_retrans = RPC_WAS_SENT(task);
  1385. int status;
  1386. if (!req->rq_bytes_sent) {
  1387. if (xprt_request_data_received(task)) {
  1388. status = 0;
  1389. goto out_dequeue;
  1390. }
  1391. /* Verify that our message lies in the RPCSEC_GSS window */
  1392. if (rpcauth_xmit_need_reencode(task)) {
  1393. status = -EBADMSG;
  1394. goto out_dequeue;
  1395. }
  1396. if (RPC_SIGNALLED(task)) {
  1397. status = -ERESTARTSYS;
  1398. goto out_dequeue;
  1399. }
  1400. }
  1401. /*
  1402. * Update req->rq_ntrans before transmitting to avoid races with
  1403. * xprt_update_rtt(), which needs to know that it is recording a
  1404. * reply to the first transmission.
  1405. */
  1406. req->rq_ntrans++;
  1407. trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
  1408. connect_cookie = xprt->connect_cookie;
  1409. status = xprt->ops->send_request(req);
  1410. if (status != 0) {
  1411. req->rq_ntrans--;
  1412. trace_xprt_transmit(req, status);
  1413. return status;
  1414. }
  1415. if (is_retrans) {
  1416. task->tk_client->cl_stats->rpcretrans++;
  1417. trace_xprt_retransmit(req);
  1418. }
  1419. xprt_inject_disconnect(xprt);
  1420. task->tk_flags |= RPC_TASK_SENT;
  1421. spin_lock(&xprt->transport_lock);
  1422. xprt->stat.sends++;
  1423. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  1424. xprt->stat.bklog_u += xprt->backlog.qlen;
  1425. xprt->stat.sending_u += xprt->sending.qlen;
  1426. xprt->stat.pending_u += xprt->pending.qlen;
  1427. spin_unlock(&xprt->transport_lock);
  1428. req->rq_connect_cookie = connect_cookie;
  1429. out_dequeue:
  1430. trace_xprt_transmit(req, status);
  1431. xprt_request_dequeue_transmit(task);
  1432. rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
  1433. return status;
  1434. }
  1435. /**
  1436. * xprt_transmit - send an RPC request on a transport
  1437. * @task: controlling RPC task
  1438. *
  1439. * Attempts to drain the transmit queue. On exit, either the transport
  1440. * signalled an error that needs to be handled before transmission can
  1441. * resume, or @task finished transmitting, and detected that it already
  1442. * received a reply.
  1443. */
  1444. void
  1445. xprt_transmit(struct rpc_task *task)
  1446. {
  1447. struct rpc_rqst *next, *req = task->tk_rqstp;
  1448. struct rpc_xprt *xprt = req->rq_xprt;
  1449. int status;
  1450. spin_lock(&xprt->queue_lock);
  1451. for (;;) {
  1452. next = list_first_entry_or_null(&xprt->xmit_queue,
  1453. struct rpc_rqst, rq_xmit);
  1454. if (!next)
  1455. break;
  1456. xprt_pin_rqst(next);
  1457. spin_unlock(&xprt->queue_lock);
  1458. status = xprt_request_transmit(next, task);
  1459. if (status == -EBADMSG && next != req)
  1460. status = 0;
  1461. spin_lock(&xprt->queue_lock);
  1462. xprt_unpin_rqst(next);
  1463. if (status < 0) {
  1464. if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1465. task->tk_status = status;
  1466. break;
  1467. }
  1468. /* Was @task transmitted, and has it received a reply? */
  1469. if (xprt_request_data_received(task) &&
  1470. !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
  1471. break;
  1472. cond_resched_lock(&xprt->queue_lock);
  1473. }
  1474. spin_unlock(&xprt->queue_lock);
  1475. }
  1476. static void xprt_complete_request_init(struct rpc_task *task)
  1477. {
  1478. if (task->tk_rqstp)
  1479. xprt_request_init(task);
  1480. }
  1481. void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
  1482. {
  1483. set_bit(XPRT_CONGESTED, &xprt->state);
  1484. rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
  1485. }
  1486. EXPORT_SYMBOL_GPL(xprt_add_backlog);
  1487. static bool __xprt_set_rq(struct rpc_task *task, void *data)
  1488. {
  1489. struct rpc_rqst *req = data;
  1490. if (task->tk_rqstp == NULL) {
  1491. memset(req, 0, sizeof(*req)); /* mark unused */
  1492. task->tk_rqstp = req;
  1493. return true;
  1494. }
  1495. return false;
  1496. }
  1497. bool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1498. {
  1499. if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) {
  1500. clear_bit(XPRT_CONGESTED, &xprt->state);
  1501. return false;
  1502. }
  1503. return true;
  1504. }
  1505. EXPORT_SYMBOL_GPL(xprt_wake_up_backlog);
  1506. static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
  1507. {
  1508. bool ret = false;
  1509. if (!test_bit(XPRT_CONGESTED, &xprt->state))
  1510. goto out;
  1511. spin_lock(&xprt->reserve_lock);
  1512. if (test_bit(XPRT_CONGESTED, &xprt->state)) {
  1513. xprt_add_backlog(xprt, task);
  1514. ret = true;
  1515. }
  1516. spin_unlock(&xprt->reserve_lock);
  1517. out:
  1518. return ret;
  1519. }
  1520. static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
  1521. {
  1522. struct rpc_rqst *req = ERR_PTR(-EAGAIN);
  1523. if (xprt->num_reqs >= xprt->max_reqs)
  1524. goto out;
  1525. ++xprt->num_reqs;
  1526. spin_unlock(&xprt->reserve_lock);
  1527. req = kzalloc(sizeof(*req), rpc_task_gfp_mask());
  1528. spin_lock(&xprt->reserve_lock);
  1529. if (req != NULL)
  1530. goto out;
  1531. --xprt->num_reqs;
  1532. req = ERR_PTR(-ENOMEM);
  1533. out:
  1534. return req;
  1535. }
  1536. static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1537. {
  1538. if (xprt->num_reqs > xprt->min_reqs) {
  1539. --xprt->num_reqs;
  1540. kfree(req);
  1541. return true;
  1542. }
  1543. return false;
  1544. }
  1545. void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  1546. {
  1547. struct rpc_rqst *req;
  1548. spin_lock(&xprt->reserve_lock);
  1549. if (!list_empty(&xprt->free)) {
  1550. req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  1551. list_del(&req->rq_list);
  1552. goto out_init_req;
  1553. }
  1554. req = xprt_dynamic_alloc_slot(xprt);
  1555. if (!IS_ERR(req))
  1556. goto out_init_req;
  1557. switch (PTR_ERR(req)) {
  1558. case -ENOMEM:
  1559. dprintk("RPC: dynamic allocation of request slot "
  1560. "failed! Retrying\n");
  1561. task->tk_status = -ENOMEM;
  1562. break;
  1563. case -EAGAIN:
  1564. xprt_add_backlog(xprt, task);
  1565. dprintk("RPC: waiting for request slot\n");
  1566. fallthrough;
  1567. default:
  1568. task->tk_status = -EAGAIN;
  1569. }
  1570. spin_unlock(&xprt->reserve_lock);
  1571. return;
  1572. out_init_req:
  1573. xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
  1574. xprt->num_reqs);
  1575. spin_unlock(&xprt->reserve_lock);
  1576. task->tk_status = 0;
  1577. task->tk_rqstp = req;
  1578. }
  1579. EXPORT_SYMBOL_GPL(xprt_alloc_slot);
  1580. void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1581. {
  1582. spin_lock(&xprt->reserve_lock);
  1583. if (!xprt_wake_up_backlog(xprt, req) &&
  1584. !xprt_dynamic_free_slot(xprt, req)) {
  1585. memset(req, 0, sizeof(*req)); /* mark unused */
  1586. list_add(&req->rq_list, &xprt->free);
  1587. }
  1588. spin_unlock(&xprt->reserve_lock);
  1589. }
  1590. EXPORT_SYMBOL_GPL(xprt_free_slot);
  1591. static void xprt_free_all_slots(struct rpc_xprt *xprt)
  1592. {
  1593. struct rpc_rqst *req;
  1594. while (!list_empty(&xprt->free)) {
  1595. req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
  1596. list_del(&req->rq_list);
  1597. kfree(req);
  1598. }
  1599. }
  1600. static DEFINE_IDA(rpc_xprt_ids);
  1601. void xprt_cleanup_ids(void)
  1602. {
  1603. ida_destroy(&rpc_xprt_ids);
  1604. }
  1605. static int xprt_alloc_id(struct rpc_xprt *xprt)
  1606. {
  1607. int id;
  1608. id = ida_alloc(&rpc_xprt_ids, GFP_KERNEL);
  1609. if (id < 0)
  1610. return id;
  1611. xprt->id = id;
  1612. return 0;
  1613. }
  1614. static void xprt_free_id(struct rpc_xprt *xprt)
  1615. {
  1616. ida_free(&rpc_xprt_ids, xprt->id);
  1617. }
  1618. struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
  1619. unsigned int num_prealloc,
  1620. unsigned int max_alloc)
  1621. {
  1622. struct rpc_xprt *xprt;
  1623. struct rpc_rqst *req;
  1624. int i;
  1625. xprt = kzalloc(size, GFP_KERNEL);
  1626. if (xprt == NULL)
  1627. goto out;
  1628. xprt_alloc_id(xprt);
  1629. xprt_init(xprt, net);
  1630. for (i = 0; i < num_prealloc; i++) {
  1631. req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
  1632. if (!req)
  1633. goto out_free;
  1634. list_add(&req->rq_list, &xprt->free);
  1635. }
  1636. xprt->max_reqs = max_t(unsigned int, max_alloc, num_prealloc);
  1637. xprt->min_reqs = num_prealloc;
  1638. xprt->num_reqs = num_prealloc;
  1639. return xprt;
  1640. out_free:
  1641. xprt_free(xprt);
  1642. out:
  1643. return NULL;
  1644. }
  1645. EXPORT_SYMBOL_GPL(xprt_alloc);
  1646. void xprt_free(struct rpc_xprt *xprt)
  1647. {
  1648. put_net_track(xprt->xprt_net, &xprt->ns_tracker);
  1649. xprt_free_all_slots(xprt);
  1650. xprt_free_id(xprt);
  1651. rpc_sysfs_xprt_destroy(xprt);
  1652. kfree_rcu(xprt, rcu);
  1653. }
  1654. EXPORT_SYMBOL_GPL(xprt_free);
  1655. static void
  1656. xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
  1657. {
  1658. req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
  1659. }
  1660. static __be32
  1661. xprt_alloc_xid(struct rpc_xprt *xprt)
  1662. {
  1663. __be32 xid;
  1664. spin_lock(&xprt->reserve_lock);
  1665. xid = (__force __be32)xprt->xid++;
  1666. spin_unlock(&xprt->reserve_lock);
  1667. return xid;
  1668. }
  1669. static void
  1670. xprt_init_xid(struct rpc_xprt *xprt)
  1671. {
  1672. xprt->xid = get_random_u32();
  1673. }
  1674. static void
  1675. xprt_request_init(struct rpc_task *task)
  1676. {
  1677. struct rpc_xprt *xprt = task->tk_xprt;
  1678. struct rpc_rqst *req = task->tk_rqstp;
  1679. req->rq_task = task;
  1680. req->rq_xprt = xprt;
  1681. req->rq_buffer = NULL;
  1682. req->rq_xid = xprt_alloc_xid(xprt);
  1683. xprt_init_connect_cookie(req, xprt);
  1684. req->rq_snd_buf.len = 0;
  1685. req->rq_snd_buf.buflen = 0;
  1686. req->rq_rcv_buf.len = 0;
  1687. req->rq_rcv_buf.buflen = 0;
  1688. req->rq_snd_buf.bvec = NULL;
  1689. req->rq_rcv_buf.bvec = NULL;
  1690. req->rq_release_snd_buf = NULL;
  1691. xprt_init_majortimeo(task, req);
  1692. trace_xprt_reserve(req);
  1693. }
  1694. static void
  1695. xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
  1696. {
  1697. xprt->ops->alloc_slot(xprt, task);
  1698. if (task->tk_rqstp != NULL)
  1699. xprt_request_init(task);
  1700. }
  1701. /**
  1702. * xprt_reserve - allocate an RPC request slot
  1703. * @task: RPC task requesting a slot allocation
  1704. *
  1705. * If the transport is marked as being congested, or if no more
  1706. * slots are available, place the task on the transport's
  1707. * backlog queue.
  1708. */
  1709. void xprt_reserve(struct rpc_task *task)
  1710. {
  1711. struct rpc_xprt *xprt = task->tk_xprt;
  1712. task->tk_status = 0;
  1713. if (task->tk_rqstp != NULL)
  1714. return;
  1715. task->tk_status = -EAGAIN;
  1716. if (!xprt_throttle_congested(xprt, task))
  1717. xprt_do_reserve(xprt, task);
  1718. }
  1719. /**
  1720. * xprt_retry_reserve - allocate an RPC request slot
  1721. * @task: RPC task requesting a slot allocation
  1722. *
  1723. * If no more slots are available, place the task on the transport's
  1724. * backlog queue.
  1725. * Note that the only difference with xprt_reserve is that we now
  1726. * ignore the value of the XPRT_CONGESTED flag.
  1727. */
  1728. void xprt_retry_reserve(struct rpc_task *task)
  1729. {
  1730. struct rpc_xprt *xprt = task->tk_xprt;
  1731. task->tk_status = 0;
  1732. if (task->tk_rqstp != NULL)
  1733. return;
  1734. task->tk_status = -EAGAIN;
  1735. xprt_do_reserve(xprt, task);
  1736. }
  1737. /**
  1738. * xprt_release - release an RPC request slot
  1739. * @task: task which is finished with the slot
  1740. *
  1741. */
  1742. void xprt_release(struct rpc_task *task)
  1743. {
  1744. struct rpc_xprt *xprt;
  1745. struct rpc_rqst *req = task->tk_rqstp;
  1746. if (req == NULL) {
  1747. if (task->tk_client) {
  1748. xprt = task->tk_xprt;
  1749. xprt_release_write(xprt, task);
  1750. }
  1751. return;
  1752. }
  1753. xprt = req->rq_xprt;
  1754. xprt_request_dequeue_xprt(task);
  1755. spin_lock(&xprt->transport_lock);
  1756. xprt->ops->release_xprt(xprt, task);
  1757. if (xprt->ops->release_request)
  1758. xprt->ops->release_request(task);
  1759. xprt_schedule_autodisconnect(xprt);
  1760. spin_unlock(&xprt->transport_lock);
  1761. if (req->rq_buffer)
  1762. xprt->ops->buf_free(task);
  1763. if (req->rq_cred != NULL)
  1764. put_rpccred(req->rq_cred);
  1765. if (req->rq_release_snd_buf)
  1766. req->rq_release_snd_buf(req);
  1767. task->tk_rqstp = NULL;
  1768. if (likely(!bc_prealloc(req)))
  1769. xprt->ops->free_slot(xprt, req);
  1770. else
  1771. xprt_free_bc_request(req);
  1772. }
  1773. #ifdef CONFIG_SUNRPC_BACKCHANNEL
  1774. void
  1775. xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
  1776. {
  1777. struct xdr_buf *xbufp = &req->rq_snd_buf;
  1778. task->tk_rqstp = req;
  1779. req->rq_task = task;
  1780. xprt_init_connect_cookie(req, req->rq_xprt);
  1781. /*
  1782. * Set up the xdr_buf length.
  1783. * This also indicates that the buffer is XDR encoded already.
  1784. */
  1785. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  1786. xbufp->tail[0].iov_len;
  1787. }
  1788. #endif
  1789. static void xprt_init(struct rpc_xprt *xprt, struct net *net)
  1790. {
  1791. kref_init(&xprt->kref);
  1792. spin_lock_init(&xprt->transport_lock);
  1793. spin_lock_init(&xprt->reserve_lock);
  1794. spin_lock_init(&xprt->queue_lock);
  1795. INIT_LIST_HEAD(&xprt->free);
  1796. xprt->recv_queue = RB_ROOT;
  1797. INIT_LIST_HEAD(&xprt->xmit_queue);
  1798. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1799. spin_lock_init(&xprt->bc_pa_lock);
  1800. INIT_LIST_HEAD(&xprt->bc_pa_list);
  1801. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1802. INIT_LIST_HEAD(&xprt->xprt_switch);
  1803. xprt->last_used = jiffies;
  1804. xprt->cwnd = RPC_INITCWND;
  1805. xprt->bind_index = 0;
  1806. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1807. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1808. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  1809. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1810. xprt_init_xid(xprt);
  1811. xprt->xprt_net = get_net_track(net, &xprt->ns_tracker, GFP_KERNEL);
  1812. }
  1813. /**
  1814. * xprt_create_transport - create an RPC transport
  1815. * @args: rpc transport creation arguments
  1816. *
  1817. */
  1818. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  1819. {
  1820. struct rpc_xprt *xprt;
  1821. const struct xprt_class *t;
  1822. t = xprt_class_find_by_ident(args->ident);
  1823. if (!t) {
  1824. dprintk("RPC: transport (%d) not supported\n", args->ident);
  1825. return ERR_PTR(-EIO);
  1826. }
  1827. xprt = t->setup(args);
  1828. xprt_class_release(t);
  1829. if (IS_ERR(xprt))
  1830. goto out;
  1831. if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
  1832. xprt->idle_timeout = 0;
  1833. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  1834. if (xprt_has_timer(xprt))
  1835. timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
  1836. else
  1837. timer_setup(&xprt->timer, NULL, 0);
  1838. if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
  1839. xprt_destroy(xprt);
  1840. return ERR_PTR(-EINVAL);
  1841. }
  1842. xprt->servername = kstrdup(args->servername, GFP_KERNEL);
  1843. if (xprt->servername == NULL) {
  1844. xprt_destroy(xprt);
  1845. return ERR_PTR(-ENOMEM);
  1846. }
  1847. rpc_xprt_debugfs_register(xprt);
  1848. trace_xprt_create(xprt);
  1849. out:
  1850. return xprt;
  1851. }
  1852. static void xprt_destroy_cb(struct work_struct *work)
  1853. {
  1854. struct rpc_xprt *xprt =
  1855. container_of(work, struct rpc_xprt, task_cleanup);
  1856. trace_xprt_destroy(xprt);
  1857. rpc_xprt_debugfs_unregister(xprt);
  1858. rpc_destroy_wait_queue(&xprt->binding);
  1859. rpc_destroy_wait_queue(&xprt->pending);
  1860. rpc_destroy_wait_queue(&xprt->sending);
  1861. rpc_destroy_wait_queue(&xprt->backlog);
  1862. kfree(xprt->servername);
  1863. /*
  1864. * Destroy any existing back channel
  1865. */
  1866. xprt_destroy_backchannel(xprt, UINT_MAX);
  1867. /*
  1868. * Tear down transport state and free the rpc_xprt
  1869. */
  1870. xprt->ops->destroy(xprt);
  1871. }
  1872. /**
  1873. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1874. * @xprt: transport to destroy
  1875. *
  1876. */
  1877. static void xprt_destroy(struct rpc_xprt *xprt)
  1878. {
  1879. /*
  1880. * Exclude transport connect/disconnect handlers and autoclose
  1881. */
  1882. wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
  1883. /*
  1884. * xprt_schedule_autodisconnect() can run after XPRT_LOCKED
  1885. * is cleared. We use ->transport_lock to ensure the mod_timer()
  1886. * can only run *before* del_time_sync(), never after.
  1887. */
  1888. spin_lock(&xprt->transport_lock);
  1889. del_timer_sync(&xprt->timer);
  1890. spin_unlock(&xprt->transport_lock);
  1891. /*
  1892. * Destroy sockets etc from the system workqueue so they can
  1893. * safely flush receive work running on rpciod.
  1894. */
  1895. INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
  1896. schedule_work(&xprt->task_cleanup);
  1897. }
  1898. static void xprt_destroy_kref(struct kref *kref)
  1899. {
  1900. xprt_destroy(container_of(kref, struct rpc_xprt, kref));
  1901. }
  1902. /**
  1903. * xprt_get - return a reference to an RPC transport.
  1904. * @xprt: pointer to the transport
  1905. *
  1906. */
  1907. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1908. {
  1909. if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
  1910. return xprt;
  1911. return NULL;
  1912. }
  1913. EXPORT_SYMBOL_GPL(xprt_get);
  1914. /**
  1915. * xprt_put - release a reference to an RPC transport.
  1916. * @xprt: pointer to the transport
  1917. *
  1918. */
  1919. void xprt_put(struct rpc_xprt *xprt)
  1920. {
  1921. if (xprt != NULL)
  1922. kref_put(&xprt->kref, xprt_destroy_kref);
  1923. }
  1924. EXPORT_SYMBOL_GPL(xprt_put);
  1925. void xprt_set_offline_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
  1926. {
  1927. if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
  1928. spin_lock(&xps->xps_lock);
  1929. xps->xps_nactive--;
  1930. spin_unlock(&xps->xps_lock);
  1931. }
  1932. }
  1933. void xprt_set_online_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
  1934. {
  1935. if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
  1936. spin_lock(&xps->xps_lock);
  1937. xps->xps_nactive++;
  1938. spin_unlock(&xps->xps_lock);
  1939. }
  1940. }
  1941. void xprt_delete_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
  1942. {
  1943. if (test_and_set_bit(XPRT_REMOVE, &xprt->state))
  1944. return;
  1945. xprt_force_disconnect(xprt);
  1946. if (!test_bit(XPRT_CONNECTED, &xprt->state))
  1947. return;
  1948. if (!xprt->sending.qlen && !xprt->pending.qlen &&
  1949. !xprt->backlog.qlen && !atomic_long_read(&xprt->queuelen))
  1950. rpc_xprt_switch_remove_xprt(xps, xprt, true);
  1951. }