sched.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * linux/net/sunrpc/sched.c
  3. *
  4. * Scheduling for synchronous and asynchronous RPC requests.
  5. *
  6. * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
  7. *
  8. * TCP NFS related read + write fixes
  9. * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/slab.h>
  15. #include <linux/mempool.h>
  16. #include <linux/smp.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/mutex.h>
  19. #include <linux/freezer.h>
  20. #include <linux/sunrpc/clnt.h>
  21. #include "sunrpc.h"
  22. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  23. #define RPCDBG_FACILITY RPCDBG_SCHED
  24. #endif
  25. #define CREATE_TRACE_POINTS
  26. #include <trace/events/sunrpc.h>
  27. /*
  28. * RPC slabs and memory pools
  29. */
  30. #define RPC_BUFFER_MAXSIZE (2048)
  31. #define RPC_BUFFER_POOLSIZE (8)
  32. #define RPC_TASK_POOLSIZE (8)
  33. static struct kmem_cache *rpc_task_slabp __read_mostly;
  34. static struct kmem_cache *rpc_buffer_slabp __read_mostly;
  35. static mempool_t *rpc_task_mempool __read_mostly;
  36. static mempool_t *rpc_buffer_mempool __read_mostly;
  37. static void rpc_async_schedule(struct work_struct *);
  38. static void rpc_release_task(struct rpc_task *task);
  39. static void __rpc_queue_timer_fn(struct timer_list *t);
  40. /*
  41. * RPC tasks sit here while waiting for conditions to improve.
  42. */
  43. static struct rpc_wait_queue delay_queue;
  44. /*
  45. * rpciod-related stuff
  46. */
  47. struct workqueue_struct *rpciod_workqueue __read_mostly;
  48. struct workqueue_struct *xprtiod_workqueue __read_mostly;
  49. /*
  50. * Disable the timer for a given RPC task. Should be called with
  51. * queue->lock and bh_disabled in order to avoid races within
  52. * rpc_run_timer().
  53. */
  54. static void
  55. __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
  56. {
  57. if (task->tk_timeout == 0)
  58. return;
  59. dprintk("RPC: %5u disabling timer\n", task->tk_pid);
  60. task->tk_timeout = 0;
  61. list_del(&task->u.tk_wait.timer_list);
  62. if (list_empty(&queue->timer_list.list))
  63. del_timer(&queue->timer_list.timer);
  64. }
  65. static void
  66. rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
  67. {
  68. queue->timer_list.expires = expires;
  69. mod_timer(&queue->timer_list.timer, expires);
  70. }
  71. /*
  72. * Set up a timer for the current task.
  73. */
  74. static void
  75. __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
  76. {
  77. if (!task->tk_timeout)
  78. return;
  79. dprintk("RPC: %5u setting alarm for %u ms\n",
  80. task->tk_pid, jiffies_to_msecs(task->tk_timeout));
  81. task->u.tk_wait.expires = jiffies + task->tk_timeout;
  82. if (list_empty(&queue->timer_list.list) || time_before(task->u.tk_wait.expires, queue->timer_list.expires))
  83. rpc_set_queue_timer(queue, task->u.tk_wait.expires);
  84. list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list);
  85. }
  86. static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
  87. {
  88. if (queue->priority != priority) {
  89. queue->priority = priority;
  90. queue->nr = 1U << priority;
  91. }
  92. }
  93. static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
  94. {
  95. rpc_set_waitqueue_priority(queue, queue->maxpriority);
  96. }
  97. /*
  98. * Add a request to a queue list
  99. */
  100. static void
  101. __rpc_list_enqueue_task(struct list_head *q, struct rpc_task *task)
  102. {
  103. struct rpc_task *t;
  104. list_for_each_entry(t, q, u.tk_wait.list) {
  105. if (t->tk_owner == task->tk_owner) {
  106. list_add_tail(&task->u.tk_wait.links,
  107. &t->u.tk_wait.links);
  108. /* Cache the queue head in task->u.tk_wait.list */
  109. task->u.tk_wait.list.next = q;
  110. task->u.tk_wait.list.prev = NULL;
  111. return;
  112. }
  113. }
  114. INIT_LIST_HEAD(&task->u.tk_wait.links);
  115. list_add_tail(&task->u.tk_wait.list, q);
  116. }
  117. /*
  118. * Remove request from a queue list
  119. */
  120. static void
  121. __rpc_list_dequeue_task(struct rpc_task *task)
  122. {
  123. struct list_head *q;
  124. struct rpc_task *t;
  125. if (task->u.tk_wait.list.prev == NULL) {
  126. list_del(&task->u.tk_wait.links);
  127. return;
  128. }
  129. if (!list_empty(&task->u.tk_wait.links)) {
  130. t = list_first_entry(&task->u.tk_wait.links,
  131. struct rpc_task,
  132. u.tk_wait.links);
  133. /* Assume __rpc_list_enqueue_task() cached the queue head */
  134. q = t->u.tk_wait.list.next;
  135. list_add_tail(&t->u.tk_wait.list, q);
  136. list_del(&task->u.tk_wait.links);
  137. }
  138. list_del(&task->u.tk_wait.list);
  139. }
  140. /*
  141. * Add new request to a priority queue.
  142. */
  143. static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue,
  144. struct rpc_task *task,
  145. unsigned char queue_priority)
  146. {
  147. if (unlikely(queue_priority > queue->maxpriority))
  148. queue_priority = queue->maxpriority;
  149. __rpc_list_enqueue_task(&queue->tasks[queue_priority], task);
  150. }
  151. /*
  152. * Add new request to wait queue.
  153. *
  154. * Swapper tasks always get inserted at the head of the queue.
  155. * This should avoid many nasty memory deadlocks and hopefully
  156. * improve overall performance.
  157. * Everyone else gets appended to the queue to ensure proper FIFO behavior.
  158. */
  159. static void __rpc_add_wait_queue(struct rpc_wait_queue *queue,
  160. struct rpc_task *task,
  161. unsigned char queue_priority)
  162. {
  163. WARN_ON_ONCE(RPC_IS_QUEUED(task));
  164. if (RPC_IS_QUEUED(task))
  165. return;
  166. if (RPC_IS_PRIORITY(queue))
  167. __rpc_add_wait_queue_priority(queue, task, queue_priority);
  168. else if (RPC_IS_SWAPPER(task))
  169. list_add(&task->u.tk_wait.list, &queue->tasks[0]);
  170. else
  171. list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
  172. task->tk_waitqueue = queue;
  173. queue->qlen++;
  174. /* barrier matches the read in rpc_wake_up_task_queue_locked() */
  175. smp_wmb();
  176. rpc_set_queued(task);
  177. dprintk("RPC: %5u added to queue %p \"%s\"\n",
  178. task->tk_pid, queue, rpc_qname(queue));
  179. }
  180. /*
  181. * Remove request from a priority queue.
  182. */
  183. static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
  184. {
  185. __rpc_list_dequeue_task(task);
  186. }
  187. /*
  188. * Remove request from queue.
  189. * Note: must be called with spin lock held.
  190. */
  191. static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
  192. {
  193. __rpc_disable_timer(queue, task);
  194. if (RPC_IS_PRIORITY(queue))
  195. __rpc_remove_wait_queue_priority(task);
  196. else
  197. list_del(&task->u.tk_wait.list);
  198. queue->qlen--;
  199. dprintk("RPC: %5u removed from queue %p \"%s\"\n",
  200. task->tk_pid, queue, rpc_qname(queue));
  201. }
  202. static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
  203. {
  204. int i;
  205. spin_lock_init(&queue->lock);
  206. for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
  207. INIT_LIST_HEAD(&queue->tasks[i]);
  208. queue->maxpriority = nr_queues - 1;
  209. rpc_reset_waitqueue_priority(queue);
  210. queue->qlen = 0;
  211. timer_setup(&queue->timer_list.timer, __rpc_queue_timer_fn, 0);
  212. INIT_LIST_HEAD(&queue->timer_list.list);
  213. rpc_assign_waitqueue_name(queue, qname);
  214. }
  215. void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  216. {
  217. __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
  218. }
  219. EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue);
  220. void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  221. {
  222. __rpc_init_priority_wait_queue(queue, qname, 1);
  223. }
  224. EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
  225. void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
  226. {
  227. del_timer_sync(&queue->timer_list.timer);
  228. }
  229. EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
  230. static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode)
  231. {
  232. freezable_schedule_unsafe();
  233. if (signal_pending_state(mode, current))
  234. return -ERESTARTSYS;
  235. return 0;
  236. }
  237. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
  238. static void rpc_task_set_debuginfo(struct rpc_task *task)
  239. {
  240. static atomic_t rpc_pid;
  241. task->tk_pid = atomic_inc_return(&rpc_pid);
  242. }
  243. #else
  244. static inline void rpc_task_set_debuginfo(struct rpc_task *task)
  245. {
  246. }
  247. #endif
  248. static void rpc_set_active(struct rpc_task *task)
  249. {
  250. rpc_task_set_debuginfo(task);
  251. set_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
  252. trace_rpc_task_begin(task, NULL);
  253. }
  254. /*
  255. * Mark an RPC call as having completed by clearing the 'active' bit
  256. * and then waking up all tasks that were sleeping.
  257. */
  258. static int rpc_complete_task(struct rpc_task *task)
  259. {
  260. void *m = &task->tk_runstate;
  261. wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE);
  262. struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE);
  263. unsigned long flags;
  264. int ret;
  265. trace_rpc_task_complete(task, NULL);
  266. spin_lock_irqsave(&wq->lock, flags);
  267. clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
  268. ret = atomic_dec_and_test(&task->tk_count);
  269. if (waitqueue_active(wq))
  270. __wake_up_locked_key(wq, TASK_NORMAL, &k);
  271. spin_unlock_irqrestore(&wq->lock, flags);
  272. return ret;
  273. }
  274. /*
  275. * Allow callers to wait for completion of an RPC call
  276. *
  277. * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit()
  278. * to enforce taking of the wq->lock and hence avoid races with
  279. * rpc_complete_task().
  280. */
  281. int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action)
  282. {
  283. if (action == NULL)
  284. action = rpc_wait_bit_killable;
  285. return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
  286. action, TASK_KILLABLE);
  287. }
  288. EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
  289. /*
  290. * Make an RPC task runnable.
  291. *
  292. * Note: If the task is ASYNC, and is being made runnable after sitting on an
  293. * rpc_wait_queue, this must be called with the queue spinlock held to protect
  294. * the wait queue operation.
  295. * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(),
  296. * which is needed to ensure that __rpc_execute() doesn't loop (due to the
  297. * lockless RPC_IS_QUEUED() test) before we've had a chance to test
  298. * the RPC_TASK_RUNNING flag.
  299. */
  300. static void rpc_make_runnable(struct workqueue_struct *wq,
  301. struct rpc_task *task)
  302. {
  303. bool need_wakeup = !rpc_test_and_set_running(task);
  304. rpc_clear_queued(task);
  305. if (!need_wakeup)
  306. return;
  307. if (RPC_IS_ASYNC(task)) {
  308. INIT_WORK(&task->u.tk_work, rpc_async_schedule);
  309. queue_work(wq, &task->u.tk_work);
  310. } else
  311. wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
  312. }
  313. /*
  314. * Prepare for sleeping on a wait queue.
  315. * By always appending tasks to the list we ensure FIFO behavior.
  316. * NB: An RPC task will only receive interrupt-driven events as long
  317. * as it's on a wait queue.
  318. */
  319. static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
  320. struct rpc_task *task,
  321. rpc_action action,
  322. unsigned char queue_priority)
  323. {
  324. dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
  325. task->tk_pid, rpc_qname(q), jiffies);
  326. trace_rpc_task_sleep(task, q);
  327. __rpc_add_wait_queue(q, task, queue_priority);
  328. WARN_ON_ONCE(task->tk_callback != NULL);
  329. task->tk_callback = action;
  330. __rpc_add_timer(q, task);
  331. }
  332. void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  333. rpc_action action)
  334. {
  335. /* We shouldn't ever put an inactive task to sleep */
  336. WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
  337. if (!RPC_IS_ACTIVATED(task)) {
  338. task->tk_status = -EIO;
  339. rpc_put_task_async(task);
  340. return;
  341. }
  342. /*
  343. * Protect the queue operations.
  344. */
  345. spin_lock_bh(&q->lock);
  346. __rpc_sleep_on_priority(q, task, action, task->tk_priority);
  347. spin_unlock_bh(&q->lock);
  348. }
  349. EXPORT_SYMBOL_GPL(rpc_sleep_on);
  350. void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
  351. rpc_action action, int priority)
  352. {
  353. /* We shouldn't ever put an inactive task to sleep */
  354. WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
  355. if (!RPC_IS_ACTIVATED(task)) {
  356. task->tk_status = -EIO;
  357. rpc_put_task_async(task);
  358. return;
  359. }
  360. /*
  361. * Protect the queue operations.
  362. */
  363. spin_lock_bh(&q->lock);
  364. __rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW);
  365. spin_unlock_bh(&q->lock);
  366. }
  367. EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
  368. /**
  369. * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task
  370. * @wq: workqueue on which to run task
  371. * @queue: wait queue
  372. * @task: task to be woken up
  373. *
  374. * Caller must hold queue->lock, and have cleared the task queued flag.
  375. */
  376. static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq,
  377. struct rpc_wait_queue *queue,
  378. struct rpc_task *task)
  379. {
  380. dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
  381. task->tk_pid, jiffies);
  382. /* Has the task been executed yet? If not, we cannot wake it up! */
  383. if (!RPC_IS_ACTIVATED(task)) {
  384. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  385. return;
  386. }
  387. trace_rpc_task_wakeup(task, queue);
  388. __rpc_remove_wait_queue(queue, task);
  389. rpc_make_runnable(wq, task);
  390. dprintk("RPC: __rpc_wake_up_task done\n");
  391. }
  392. /*
  393. * Wake up a queued task while the queue lock is being held
  394. */
  395. static struct rpc_task *
  396. rpc_wake_up_task_on_wq_queue_action_locked(struct workqueue_struct *wq,
  397. struct rpc_wait_queue *queue, struct rpc_task *task,
  398. bool (*action)(struct rpc_task *, void *), void *data)
  399. {
  400. if (RPC_IS_QUEUED(task)) {
  401. smp_rmb();
  402. if (task->tk_waitqueue == queue) {
  403. if (action == NULL || action(task, data)) {
  404. __rpc_do_wake_up_task_on_wq(wq, queue, task);
  405. return task;
  406. }
  407. }
  408. }
  409. return NULL;
  410. }
  411. static void
  412. rpc_wake_up_task_on_wq_queue_locked(struct workqueue_struct *wq,
  413. struct rpc_wait_queue *queue, struct rpc_task *task)
  414. {
  415. rpc_wake_up_task_on_wq_queue_action_locked(wq, queue, task, NULL, NULL);
  416. }
  417. /*
  418. * Wake up a queued task while the queue lock is being held
  419. */
  420. static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
  421. {
  422. rpc_wake_up_task_on_wq_queue_locked(rpciod_workqueue, queue, task);
  423. }
  424. /*
  425. * Wake up a task on a specific queue
  426. */
  427. void rpc_wake_up_queued_task_on_wq(struct workqueue_struct *wq,
  428. struct rpc_wait_queue *queue,
  429. struct rpc_task *task)
  430. {
  431. if (!RPC_IS_QUEUED(task))
  432. return;
  433. spin_lock_bh(&queue->lock);
  434. rpc_wake_up_task_on_wq_queue_locked(wq, queue, task);
  435. spin_unlock_bh(&queue->lock);
  436. }
  437. /*
  438. * Wake up a task on a specific queue
  439. */
  440. void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
  441. {
  442. if (!RPC_IS_QUEUED(task))
  443. return;
  444. spin_lock_bh(&queue->lock);
  445. rpc_wake_up_task_queue_locked(queue, task);
  446. spin_unlock_bh(&queue->lock);
  447. }
  448. EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
  449. static bool rpc_task_action_set_status(struct rpc_task *task, void *status)
  450. {
  451. task->tk_status = *(int *)status;
  452. return true;
  453. }
  454. static void
  455. rpc_wake_up_task_queue_set_status_locked(struct rpc_wait_queue *queue,
  456. struct rpc_task *task, int status)
  457. {
  458. rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue,
  459. task, rpc_task_action_set_status, &status);
  460. }
  461. /**
  462. * rpc_wake_up_queued_task_set_status - wake up a task and set task->tk_status
  463. * @queue: pointer to rpc_wait_queue
  464. * @task: pointer to rpc_task
  465. * @status: integer error value
  466. *
  467. * If @task is queued on @queue, then it is woken up, and @task->tk_status is
  468. * set to the value of @status.
  469. */
  470. void
  471. rpc_wake_up_queued_task_set_status(struct rpc_wait_queue *queue,
  472. struct rpc_task *task, int status)
  473. {
  474. if (!RPC_IS_QUEUED(task))
  475. return;
  476. spin_lock_bh(&queue->lock);
  477. rpc_wake_up_task_queue_set_status_locked(queue, task, status);
  478. spin_unlock_bh(&queue->lock);
  479. }
  480. /*
  481. * Wake up the next task on a priority queue.
  482. */
  483. static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
  484. {
  485. struct list_head *q;
  486. struct rpc_task *task;
  487. /*
  488. * Service a batch of tasks from a single owner.
  489. */
  490. q = &queue->tasks[queue->priority];
  491. if (!list_empty(q) && --queue->nr) {
  492. task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
  493. goto out;
  494. }
  495. /*
  496. * Service the next queue.
  497. */
  498. do {
  499. if (q == &queue->tasks[0])
  500. q = &queue->tasks[queue->maxpriority];
  501. else
  502. q = q - 1;
  503. if (!list_empty(q)) {
  504. task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
  505. goto new_queue;
  506. }
  507. } while (q != &queue->tasks[queue->priority]);
  508. rpc_reset_waitqueue_priority(queue);
  509. return NULL;
  510. new_queue:
  511. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  512. out:
  513. return task;
  514. }
  515. static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
  516. {
  517. if (RPC_IS_PRIORITY(queue))
  518. return __rpc_find_next_queued_priority(queue);
  519. if (!list_empty(&queue->tasks[0]))
  520. return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
  521. return NULL;
  522. }
  523. /*
  524. * Wake up the first task on the wait queue.
  525. */
  526. struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq,
  527. struct rpc_wait_queue *queue,
  528. bool (*func)(struct rpc_task *, void *), void *data)
  529. {
  530. struct rpc_task *task = NULL;
  531. dprintk("RPC: wake_up_first(%p \"%s\")\n",
  532. queue, rpc_qname(queue));
  533. spin_lock_bh(&queue->lock);
  534. task = __rpc_find_next_queued(queue);
  535. if (task != NULL)
  536. task = rpc_wake_up_task_on_wq_queue_action_locked(wq, queue,
  537. task, func, data);
  538. spin_unlock_bh(&queue->lock);
  539. return task;
  540. }
  541. /*
  542. * Wake up the first task on the wait queue.
  543. */
  544. struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
  545. bool (*func)(struct rpc_task *, void *), void *data)
  546. {
  547. return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data);
  548. }
  549. EXPORT_SYMBOL_GPL(rpc_wake_up_first);
  550. static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
  551. {
  552. return true;
  553. }
  554. /*
  555. * Wake up the next task on the wait queue.
  556. */
  557. struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
  558. {
  559. return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
  560. }
  561. EXPORT_SYMBOL_GPL(rpc_wake_up_next);
  562. /**
  563. * rpc_wake_up - wake up all rpc_tasks
  564. * @queue: rpc_wait_queue on which the tasks are sleeping
  565. *
  566. * Grabs queue->lock
  567. */
  568. void rpc_wake_up(struct rpc_wait_queue *queue)
  569. {
  570. struct list_head *head;
  571. spin_lock_bh(&queue->lock);
  572. head = &queue->tasks[queue->maxpriority];
  573. for (;;) {
  574. while (!list_empty(head)) {
  575. struct rpc_task *task;
  576. task = list_first_entry(head,
  577. struct rpc_task,
  578. u.tk_wait.list);
  579. rpc_wake_up_task_queue_locked(queue, task);
  580. }
  581. if (head == &queue->tasks[0])
  582. break;
  583. head--;
  584. }
  585. spin_unlock_bh(&queue->lock);
  586. }
  587. EXPORT_SYMBOL_GPL(rpc_wake_up);
  588. /**
  589. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  590. * @queue: rpc_wait_queue on which the tasks are sleeping
  591. * @status: status value to set
  592. *
  593. * Grabs queue->lock
  594. */
  595. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  596. {
  597. struct list_head *head;
  598. spin_lock_bh(&queue->lock);
  599. head = &queue->tasks[queue->maxpriority];
  600. for (;;) {
  601. while (!list_empty(head)) {
  602. struct rpc_task *task;
  603. task = list_first_entry(head,
  604. struct rpc_task,
  605. u.tk_wait.list);
  606. task->tk_status = status;
  607. rpc_wake_up_task_queue_locked(queue, task);
  608. }
  609. if (head == &queue->tasks[0])
  610. break;
  611. head--;
  612. }
  613. spin_unlock_bh(&queue->lock);
  614. }
  615. EXPORT_SYMBOL_GPL(rpc_wake_up_status);
  616. static void __rpc_queue_timer_fn(struct timer_list *t)
  617. {
  618. struct rpc_wait_queue *queue = from_timer(queue, t, timer_list.timer);
  619. struct rpc_task *task, *n;
  620. unsigned long expires, now, timeo;
  621. spin_lock(&queue->lock);
  622. expires = now = jiffies;
  623. list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
  624. timeo = task->u.tk_wait.expires;
  625. if (time_after_eq(now, timeo)) {
  626. dprintk("RPC: %5u timeout\n", task->tk_pid);
  627. task->tk_status = -ETIMEDOUT;
  628. rpc_wake_up_task_queue_locked(queue, task);
  629. continue;
  630. }
  631. if (expires == now || time_after(expires, timeo))
  632. expires = timeo;
  633. }
  634. if (!list_empty(&queue->timer_list.list))
  635. rpc_set_queue_timer(queue, expires);
  636. spin_unlock(&queue->lock);
  637. }
  638. static void __rpc_atrun(struct rpc_task *task)
  639. {
  640. if (task->tk_status == -ETIMEDOUT)
  641. task->tk_status = 0;
  642. }
  643. /*
  644. * Run a task at a later time
  645. */
  646. void rpc_delay(struct rpc_task *task, unsigned long delay)
  647. {
  648. task->tk_timeout = delay;
  649. rpc_sleep_on(&delay_queue, task, __rpc_atrun);
  650. }
  651. EXPORT_SYMBOL_GPL(rpc_delay);
  652. /*
  653. * Helper to call task->tk_ops->rpc_call_prepare
  654. */
  655. void rpc_prepare_task(struct rpc_task *task)
  656. {
  657. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  658. }
  659. static void
  660. rpc_init_task_statistics(struct rpc_task *task)
  661. {
  662. /* Initialize retry counters */
  663. task->tk_garb_retry = 2;
  664. task->tk_cred_retry = 2;
  665. task->tk_rebind_retry = 2;
  666. /* starting timestamp */
  667. task->tk_start = ktime_get();
  668. }
  669. static void
  670. rpc_reset_task_statistics(struct rpc_task *task)
  671. {
  672. task->tk_timeouts = 0;
  673. task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT);
  674. rpc_init_task_statistics(task);
  675. }
  676. /*
  677. * Helper that calls task->tk_ops->rpc_call_done if it exists
  678. */
  679. void rpc_exit_task(struct rpc_task *task)
  680. {
  681. task->tk_action = NULL;
  682. if (task->tk_ops->rpc_call_done != NULL) {
  683. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  684. if (task->tk_action != NULL) {
  685. WARN_ON(RPC_ASSASSINATED(task));
  686. /* Always release the RPC slot and buffer memory */
  687. xprt_release(task);
  688. rpc_reset_task_statistics(task);
  689. }
  690. }
  691. }
  692. void rpc_exit(struct rpc_task *task, int status)
  693. {
  694. task->tk_status = status;
  695. task->tk_action = rpc_exit_task;
  696. if (RPC_IS_QUEUED(task))
  697. rpc_wake_up_queued_task(task->tk_waitqueue, task);
  698. }
  699. EXPORT_SYMBOL_GPL(rpc_exit);
  700. void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
  701. {
  702. if (ops->rpc_release != NULL)
  703. ops->rpc_release(calldata);
  704. }
  705. /*
  706. * This is the RPC `scheduler' (or rather, the finite state machine).
  707. */
  708. static void __rpc_execute(struct rpc_task *task)
  709. {
  710. struct rpc_wait_queue *queue;
  711. int task_is_async = RPC_IS_ASYNC(task);
  712. int status = 0;
  713. dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
  714. task->tk_pid, task->tk_flags);
  715. WARN_ON_ONCE(RPC_IS_QUEUED(task));
  716. if (RPC_IS_QUEUED(task))
  717. return;
  718. for (;;) {
  719. void (*do_action)(struct rpc_task *);
  720. /*
  721. * Perform the next FSM step or a pending callback.
  722. *
  723. * tk_action may be NULL if the task has been killed.
  724. * In particular, note that rpc_killall_tasks may
  725. * do this at any time, so beware when dereferencing.
  726. */
  727. do_action = task->tk_action;
  728. if (task->tk_callback) {
  729. do_action = task->tk_callback;
  730. task->tk_callback = NULL;
  731. }
  732. if (!do_action)
  733. break;
  734. trace_rpc_task_run_action(task, do_action);
  735. do_action(task);
  736. /*
  737. * Lockless check for whether task is sleeping or not.
  738. */
  739. if (!RPC_IS_QUEUED(task))
  740. continue;
  741. /*
  742. * The queue->lock protects against races with
  743. * rpc_make_runnable().
  744. *
  745. * Note that once we clear RPC_TASK_RUNNING on an asynchronous
  746. * rpc_task, rpc_make_runnable() can assign it to a
  747. * different workqueue. We therefore cannot assume that the
  748. * rpc_task pointer may still be dereferenced.
  749. */
  750. queue = task->tk_waitqueue;
  751. spin_lock_bh(&queue->lock);
  752. if (!RPC_IS_QUEUED(task)) {
  753. spin_unlock_bh(&queue->lock);
  754. continue;
  755. }
  756. rpc_clear_running(task);
  757. spin_unlock_bh(&queue->lock);
  758. if (task_is_async)
  759. return;
  760. /* sync task: sleep here */
  761. dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
  762. status = out_of_line_wait_on_bit(&task->tk_runstate,
  763. RPC_TASK_QUEUED, rpc_wait_bit_killable,
  764. TASK_KILLABLE);
  765. if (status == -ERESTARTSYS) {
  766. /*
  767. * When a sync task receives a signal, it exits with
  768. * -ERESTARTSYS. In order to catch any callbacks that
  769. * clean up after sleeping on some queue, we don't
  770. * break the loop here, but go around once more.
  771. */
  772. dprintk("RPC: %5u got signal\n", task->tk_pid);
  773. task->tk_flags |= RPC_TASK_KILLED;
  774. rpc_exit(task, -ERESTARTSYS);
  775. }
  776. dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
  777. }
  778. dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
  779. task->tk_status);
  780. /* Release all resources associated with the task */
  781. rpc_release_task(task);
  782. }
  783. /*
  784. * User-visible entry point to the scheduler.
  785. *
  786. * This may be called recursively if e.g. an async NFS task updates
  787. * the attributes and finds that dirty pages must be flushed.
  788. * NOTE: Upon exit of this function the task is guaranteed to be
  789. * released. In particular note that tk_release() will have
  790. * been called, so your task memory may have been freed.
  791. */
  792. void rpc_execute(struct rpc_task *task)
  793. {
  794. bool is_async = RPC_IS_ASYNC(task);
  795. rpc_set_active(task);
  796. rpc_make_runnable(rpciod_workqueue, task);
  797. if (!is_async)
  798. __rpc_execute(task);
  799. }
  800. static void rpc_async_schedule(struct work_struct *work)
  801. {
  802. __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
  803. }
  804. /**
  805. * rpc_malloc - allocate RPC buffer resources
  806. * @task: RPC task
  807. *
  808. * A single memory region is allocated, which is split between the
  809. * RPC call and RPC reply that this task is being used for. When
  810. * this RPC is retired, the memory is released by calling rpc_free.
  811. *
  812. * To prevent rpciod from hanging, this allocator never sleeps,
  813. * returning -ENOMEM and suppressing warning if the request cannot
  814. * be serviced immediately. The caller can arrange to sleep in a
  815. * way that is safe for rpciod.
  816. *
  817. * Most requests are 'small' (under 2KiB) and can be serviced from a
  818. * mempool, ensuring that NFS reads and writes can always proceed,
  819. * and that there is good locality of reference for these buffers.
  820. *
  821. * In order to avoid memory starvation triggering more writebacks of
  822. * NFS requests, we avoid using GFP_KERNEL.
  823. */
  824. int rpc_malloc(struct rpc_task *task)
  825. {
  826. struct rpc_rqst *rqst = task->tk_rqstp;
  827. size_t size = rqst->rq_callsize + rqst->rq_rcvsize;
  828. struct rpc_buffer *buf;
  829. gfp_t gfp = GFP_NOIO | __GFP_NOWARN;
  830. if (RPC_IS_SWAPPER(task))
  831. gfp = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;
  832. size += sizeof(struct rpc_buffer);
  833. if (size <= RPC_BUFFER_MAXSIZE)
  834. buf = mempool_alloc(rpc_buffer_mempool, gfp);
  835. else
  836. buf = kmalloc(size, gfp);
  837. if (!buf)
  838. return -ENOMEM;
  839. buf->len = size;
  840. dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
  841. task->tk_pid, size, buf);
  842. rqst->rq_buffer = buf->data;
  843. rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
  844. return 0;
  845. }
  846. EXPORT_SYMBOL_GPL(rpc_malloc);
  847. /**
  848. * rpc_free - free RPC buffer resources allocated via rpc_malloc
  849. * @task: RPC task
  850. *
  851. */
  852. void rpc_free(struct rpc_task *task)
  853. {
  854. void *buffer = task->tk_rqstp->rq_buffer;
  855. size_t size;
  856. struct rpc_buffer *buf;
  857. buf = container_of(buffer, struct rpc_buffer, data);
  858. size = buf->len;
  859. dprintk("RPC: freeing buffer of size %zu at %p\n",
  860. size, buf);
  861. if (size <= RPC_BUFFER_MAXSIZE)
  862. mempool_free(buf, rpc_buffer_mempool);
  863. else
  864. kfree(buf);
  865. }
  866. EXPORT_SYMBOL_GPL(rpc_free);
  867. /*
  868. * Creation and deletion of RPC task structures
  869. */
  870. static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
  871. {
  872. memset(task, 0, sizeof(*task));
  873. atomic_set(&task->tk_count, 1);
  874. task->tk_flags = task_setup_data->flags;
  875. task->tk_ops = task_setup_data->callback_ops;
  876. task->tk_calldata = task_setup_data->callback_data;
  877. INIT_LIST_HEAD(&task->tk_task);
  878. task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
  879. task->tk_owner = current->tgid;
  880. /* Initialize workqueue for async tasks */
  881. task->tk_workqueue = task_setup_data->workqueue;
  882. task->tk_xprt = xprt_get(task_setup_data->rpc_xprt);
  883. if (task->tk_ops->rpc_call_prepare != NULL)
  884. task->tk_action = rpc_prepare_task;
  885. rpc_init_task_statistics(task);
  886. dprintk("RPC: new task initialized, procpid %u\n",
  887. task_pid_nr(current));
  888. }
  889. static struct rpc_task *
  890. rpc_alloc_task(void)
  891. {
  892. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO);
  893. }
  894. /*
  895. * Create a new task for the specified client.
  896. */
  897. struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
  898. {
  899. struct rpc_task *task = setup_data->task;
  900. unsigned short flags = 0;
  901. if (task == NULL) {
  902. task = rpc_alloc_task();
  903. flags = RPC_TASK_DYNAMIC;
  904. }
  905. rpc_init_task(task, setup_data);
  906. task->tk_flags |= flags;
  907. dprintk("RPC: allocated task %p\n", task);
  908. return task;
  909. }
  910. /*
  911. * rpc_free_task - release rpc task and perform cleanups
  912. *
  913. * Note that we free up the rpc_task _after_ rpc_release_calldata()
  914. * in order to work around a workqueue dependency issue.
  915. *
  916. * Tejun Heo states:
  917. * "Workqueue currently considers two work items to be the same if they're
  918. * on the same address and won't execute them concurrently - ie. it
  919. * makes a work item which is queued again while being executed wait
  920. * for the previous execution to complete.
  921. *
  922. * If a work function frees the work item, and then waits for an event
  923. * which should be performed by another work item and *that* work item
  924. * recycles the freed work item, it can create a false dependency loop.
  925. * There really is no reliable way to detect this short of verifying
  926. * every memory free."
  927. *
  928. */
  929. static void rpc_free_task(struct rpc_task *task)
  930. {
  931. unsigned short tk_flags = task->tk_flags;
  932. rpc_release_calldata(task->tk_ops, task->tk_calldata);
  933. if (tk_flags & RPC_TASK_DYNAMIC) {
  934. dprintk("RPC: %5u freeing task\n", task->tk_pid);
  935. mempool_free(task, rpc_task_mempool);
  936. }
  937. }
  938. static void rpc_async_release(struct work_struct *work)
  939. {
  940. rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
  941. }
  942. static void rpc_release_resources_task(struct rpc_task *task)
  943. {
  944. xprt_release(task);
  945. if (task->tk_msg.rpc_cred) {
  946. put_rpccred(task->tk_msg.rpc_cred);
  947. task->tk_msg.rpc_cred = NULL;
  948. }
  949. rpc_task_release_client(task);
  950. }
  951. static void rpc_final_put_task(struct rpc_task *task,
  952. struct workqueue_struct *q)
  953. {
  954. if (q != NULL) {
  955. INIT_WORK(&task->u.tk_work, rpc_async_release);
  956. queue_work(q, &task->u.tk_work);
  957. } else
  958. rpc_free_task(task);
  959. }
  960. static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
  961. {
  962. if (atomic_dec_and_test(&task->tk_count)) {
  963. rpc_release_resources_task(task);
  964. rpc_final_put_task(task, q);
  965. }
  966. }
  967. void rpc_put_task(struct rpc_task *task)
  968. {
  969. rpc_do_put_task(task, NULL);
  970. }
  971. EXPORT_SYMBOL_GPL(rpc_put_task);
  972. void rpc_put_task_async(struct rpc_task *task)
  973. {
  974. rpc_do_put_task(task, task->tk_workqueue);
  975. }
  976. EXPORT_SYMBOL_GPL(rpc_put_task_async);
  977. static void rpc_release_task(struct rpc_task *task)
  978. {
  979. dprintk("RPC: %5u release task\n", task->tk_pid);
  980. WARN_ON_ONCE(RPC_IS_QUEUED(task));
  981. rpc_release_resources_task(task);
  982. /*
  983. * Note: at this point we have been removed from rpc_clnt->cl_tasks,
  984. * so it should be safe to use task->tk_count as a test for whether
  985. * or not any other processes still hold references to our rpc_task.
  986. */
  987. if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
  988. /* Wake up anyone who may be waiting for task completion */
  989. if (!rpc_complete_task(task))
  990. return;
  991. } else {
  992. if (!atomic_dec_and_test(&task->tk_count))
  993. return;
  994. }
  995. rpc_final_put_task(task, task->tk_workqueue);
  996. }
  997. int rpciod_up(void)
  998. {
  999. return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
  1000. }
  1001. void rpciod_down(void)
  1002. {
  1003. module_put(THIS_MODULE);
  1004. }
  1005. /*
  1006. * Start up the rpciod workqueue.
  1007. */
  1008. static int rpciod_start(void)
  1009. {
  1010. struct workqueue_struct *wq;
  1011. /*
  1012. * Create the rpciod thread and wait for it to start.
  1013. */
  1014. dprintk("RPC: creating workqueue rpciod\n");
  1015. wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  1016. if (!wq)
  1017. goto out_failed;
  1018. rpciod_workqueue = wq;
  1019. /* Note: highpri because network receive is latency sensitive */
  1020. wq = alloc_workqueue("xprtiod", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_HIGHPRI, 0);
  1021. if (!wq)
  1022. goto free_rpciod;
  1023. xprtiod_workqueue = wq;
  1024. return 1;
  1025. free_rpciod:
  1026. wq = rpciod_workqueue;
  1027. rpciod_workqueue = NULL;
  1028. destroy_workqueue(wq);
  1029. out_failed:
  1030. return 0;
  1031. }
  1032. static void rpciod_stop(void)
  1033. {
  1034. struct workqueue_struct *wq = NULL;
  1035. if (rpciod_workqueue == NULL)
  1036. return;
  1037. dprintk("RPC: destroying workqueue rpciod\n");
  1038. wq = rpciod_workqueue;
  1039. rpciod_workqueue = NULL;
  1040. destroy_workqueue(wq);
  1041. wq = xprtiod_workqueue;
  1042. xprtiod_workqueue = NULL;
  1043. destroy_workqueue(wq);
  1044. }
  1045. void
  1046. rpc_destroy_mempool(void)
  1047. {
  1048. rpciod_stop();
  1049. mempool_destroy(rpc_buffer_mempool);
  1050. mempool_destroy(rpc_task_mempool);
  1051. kmem_cache_destroy(rpc_task_slabp);
  1052. kmem_cache_destroy(rpc_buffer_slabp);
  1053. rpc_destroy_wait_queue(&delay_queue);
  1054. }
  1055. int
  1056. rpc_init_mempool(void)
  1057. {
  1058. /*
  1059. * The following is not strictly a mempool initialisation,
  1060. * but there is no harm in doing it here
  1061. */
  1062. rpc_init_wait_queue(&delay_queue, "delayq");
  1063. if (!rpciod_start())
  1064. goto err_nomem;
  1065. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  1066. sizeof(struct rpc_task),
  1067. 0, SLAB_HWCACHE_ALIGN,
  1068. NULL);
  1069. if (!rpc_task_slabp)
  1070. goto err_nomem;
  1071. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  1072. RPC_BUFFER_MAXSIZE,
  1073. 0, SLAB_HWCACHE_ALIGN,
  1074. NULL);
  1075. if (!rpc_buffer_slabp)
  1076. goto err_nomem;
  1077. rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
  1078. rpc_task_slabp);
  1079. if (!rpc_task_mempool)
  1080. goto err_nomem;
  1081. rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
  1082. rpc_buffer_slabp);
  1083. if (!rpc_buffer_mempool)
  1084. goto err_nomem;
  1085. return 0;
  1086. err_nomem:
  1087. rpc_destroy_mempool();
  1088. return -ENOMEM;
  1089. }