sched.c 27 KB

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