sched.c 30 KB

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