sched.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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 rpc_task *task, *next;
  467. struct list_head *head;
  468. spin_lock_bh(&queue->lock);
  469. head = &queue->tasks[queue->maxpriority];
  470. for (;;) {
  471. list_for_each_entry_safe(task, next, head, u.tk_wait.list)
  472. rpc_wake_up_task_queue_locked(queue, task);
  473. if (head == &queue->tasks[0])
  474. break;
  475. head--;
  476. }
  477. spin_unlock_bh(&queue->lock);
  478. }
  479. EXPORT_SYMBOL_GPL(rpc_wake_up);
  480. /**
  481. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  482. * @queue: rpc_wait_queue on which the tasks are sleeping
  483. * @status: status value to set
  484. *
  485. * Grabs queue->lock
  486. */
  487. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  488. {
  489. struct rpc_task *task, *next;
  490. struct list_head *head;
  491. spin_lock_bh(&queue->lock);
  492. head = &queue->tasks[queue->maxpriority];
  493. for (;;) {
  494. list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
  495. task->tk_status = status;
  496. rpc_wake_up_task_queue_locked(queue, task);
  497. }
  498. if (head == &queue->tasks[0])
  499. break;
  500. head--;
  501. }
  502. spin_unlock_bh(&queue->lock);
  503. }
  504. EXPORT_SYMBOL_GPL(rpc_wake_up_status);
  505. static void __rpc_queue_timer_fn(unsigned long ptr)
  506. {
  507. struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
  508. struct rpc_task *task, *n;
  509. unsigned long expires, now, timeo;
  510. spin_lock(&queue->lock);
  511. expires = now = jiffies;
  512. list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
  513. timeo = task->u.tk_wait.expires;
  514. if (time_after_eq(now, timeo)) {
  515. dprintk("RPC: %5u timeout\n", task->tk_pid);
  516. task->tk_status = -ETIMEDOUT;
  517. rpc_wake_up_task_queue_locked(queue, task);
  518. continue;
  519. }
  520. if (expires == now || time_after(expires, timeo))
  521. expires = timeo;
  522. }
  523. if (!list_empty(&queue->timer_list.list))
  524. rpc_set_queue_timer(queue, expires);
  525. spin_unlock(&queue->lock);
  526. }
  527. static void __rpc_atrun(struct rpc_task *task)
  528. {
  529. task->tk_status = 0;
  530. }
  531. /*
  532. * Run a task at a later time
  533. */
  534. void rpc_delay(struct rpc_task *task, unsigned long delay)
  535. {
  536. task->tk_timeout = delay;
  537. rpc_sleep_on(&delay_queue, task, __rpc_atrun);
  538. }
  539. EXPORT_SYMBOL_GPL(rpc_delay);
  540. /*
  541. * Helper to call task->tk_ops->rpc_call_prepare
  542. */
  543. void rpc_prepare_task(struct rpc_task *task)
  544. {
  545. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  546. }
  547. static void
  548. rpc_init_task_statistics(struct rpc_task *task)
  549. {
  550. /* Initialize retry counters */
  551. task->tk_garb_retry = 2;
  552. task->tk_cred_retry = 2;
  553. task->tk_rebind_retry = 2;
  554. /* starting timestamp */
  555. task->tk_start = ktime_get();
  556. }
  557. static void
  558. rpc_reset_task_statistics(struct rpc_task *task)
  559. {
  560. task->tk_timeouts = 0;
  561. task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT);
  562. rpc_init_task_statistics(task);
  563. }
  564. /*
  565. * Helper that calls task->tk_ops->rpc_call_done if it exists
  566. */
  567. void rpc_exit_task(struct rpc_task *task)
  568. {
  569. task->tk_action = NULL;
  570. if (task->tk_ops->rpc_call_done != NULL) {
  571. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  572. if (task->tk_action != NULL) {
  573. WARN_ON(RPC_ASSASSINATED(task));
  574. /* Always release the RPC slot and buffer memory */
  575. xprt_release(task);
  576. rpc_reset_task_statistics(task);
  577. }
  578. }
  579. }
  580. void rpc_exit(struct rpc_task *task, int status)
  581. {
  582. task->tk_status = status;
  583. task->tk_action = rpc_exit_task;
  584. if (RPC_IS_QUEUED(task))
  585. rpc_wake_up_queued_task(task->tk_waitqueue, task);
  586. }
  587. EXPORT_SYMBOL_GPL(rpc_exit);
  588. void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
  589. {
  590. if (ops->rpc_release != NULL)
  591. ops->rpc_release(calldata);
  592. }
  593. /*
  594. * This is the RPC `scheduler' (or rather, the finite state machine).
  595. */
  596. static void __rpc_execute(struct rpc_task *task)
  597. {
  598. struct rpc_wait_queue *queue;
  599. int task_is_async = RPC_IS_ASYNC(task);
  600. int status = 0;
  601. dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
  602. task->tk_pid, task->tk_flags);
  603. BUG_ON(RPC_IS_QUEUED(task));
  604. for (;;) {
  605. void (*do_action)(struct rpc_task *);
  606. /*
  607. * Execute any pending callback first.
  608. */
  609. do_action = task->tk_callback;
  610. task->tk_callback = NULL;
  611. if (do_action == NULL) {
  612. /*
  613. * Perform the next FSM step.
  614. * tk_action may be NULL if the task has been killed.
  615. * In particular, note that rpc_killall_tasks may
  616. * do this at any time, so beware when dereferencing.
  617. */
  618. do_action = task->tk_action;
  619. if (do_action == NULL)
  620. break;
  621. }
  622. trace_rpc_task_run_action(task->tk_client, task, task->tk_action);
  623. do_action(task);
  624. /*
  625. * Lockless check for whether task is sleeping or not.
  626. */
  627. if (!RPC_IS_QUEUED(task))
  628. continue;
  629. /*
  630. * The queue->lock protects against races with
  631. * rpc_make_runnable().
  632. *
  633. * Note that once we clear RPC_TASK_RUNNING on an asynchronous
  634. * rpc_task, rpc_make_runnable() can assign it to a
  635. * different workqueue. We therefore cannot assume that the
  636. * rpc_task pointer may still be dereferenced.
  637. */
  638. queue = task->tk_waitqueue;
  639. spin_lock_bh(&queue->lock);
  640. if (!RPC_IS_QUEUED(task)) {
  641. spin_unlock_bh(&queue->lock);
  642. continue;
  643. }
  644. rpc_clear_running(task);
  645. spin_unlock_bh(&queue->lock);
  646. if (task_is_async)
  647. return;
  648. /* sync task: sleep here */
  649. dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
  650. status = out_of_line_wait_on_bit(&task->tk_runstate,
  651. RPC_TASK_QUEUED, rpc_wait_bit_killable,
  652. TASK_KILLABLE);
  653. if (status == -ERESTARTSYS) {
  654. /*
  655. * When a sync task receives a signal, it exits with
  656. * -ERESTARTSYS. In order to catch any callbacks that
  657. * clean up after sleeping on some queue, we don't
  658. * break the loop here, but go around once more.
  659. */
  660. dprintk("RPC: %5u got signal\n", task->tk_pid);
  661. task->tk_flags |= RPC_TASK_KILLED;
  662. rpc_exit(task, -ERESTARTSYS);
  663. }
  664. rpc_set_running(task);
  665. dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
  666. }
  667. dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
  668. task->tk_status);
  669. /* Release all resources associated with the task */
  670. rpc_release_task(task);
  671. }
  672. /*
  673. * User-visible entry point to the scheduler.
  674. *
  675. * This may be called recursively if e.g. an async NFS task updates
  676. * the attributes and finds that dirty pages must be flushed.
  677. * NOTE: Upon exit of this function the task is guaranteed to be
  678. * released. In particular note that tk_release() will have
  679. * been called, so your task memory may have been freed.
  680. */
  681. void rpc_execute(struct rpc_task *task)
  682. {
  683. rpc_set_active(task);
  684. rpc_make_runnable(task);
  685. if (!RPC_IS_ASYNC(task))
  686. __rpc_execute(task);
  687. }
  688. static void rpc_async_schedule(struct work_struct *work)
  689. {
  690. __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
  691. }
  692. /**
  693. * rpc_malloc - allocate an RPC buffer
  694. * @task: RPC task that will use this buffer
  695. * @size: requested byte size
  696. *
  697. * To prevent rpciod from hanging, this allocator never sleeps,
  698. * returning NULL if the request cannot be serviced immediately.
  699. * The caller can arrange to sleep in a way that is safe for rpciod.
  700. *
  701. * Most requests are 'small' (under 2KiB) and can be serviced from a
  702. * mempool, ensuring that NFS reads and writes can always proceed,
  703. * and that there is good locality of reference for these buffers.
  704. *
  705. * In order to avoid memory starvation triggering more writebacks of
  706. * NFS requests, we avoid using GFP_KERNEL.
  707. */
  708. void *rpc_malloc(struct rpc_task *task, size_t size)
  709. {
  710. struct rpc_buffer *buf;
  711. gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
  712. size += sizeof(struct rpc_buffer);
  713. if (size <= RPC_BUFFER_MAXSIZE)
  714. buf = mempool_alloc(rpc_buffer_mempool, gfp);
  715. else
  716. buf = kmalloc(size, gfp);
  717. if (!buf)
  718. return NULL;
  719. buf->len = size;
  720. dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
  721. task->tk_pid, size, buf);
  722. return &buf->data;
  723. }
  724. EXPORT_SYMBOL_GPL(rpc_malloc);
  725. /**
  726. * rpc_free - free buffer allocated via rpc_malloc
  727. * @buffer: buffer to free
  728. *
  729. */
  730. void rpc_free(void *buffer)
  731. {
  732. size_t size;
  733. struct rpc_buffer *buf;
  734. if (!buffer)
  735. return;
  736. buf = container_of(buffer, struct rpc_buffer, data);
  737. size = buf->len;
  738. dprintk("RPC: freeing buffer of size %zu at %p\n",
  739. size, buf);
  740. if (size <= RPC_BUFFER_MAXSIZE)
  741. mempool_free(buf, rpc_buffer_mempool);
  742. else
  743. kfree(buf);
  744. }
  745. EXPORT_SYMBOL_GPL(rpc_free);
  746. /*
  747. * Creation and deletion of RPC task structures
  748. */
  749. static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
  750. {
  751. memset(task, 0, sizeof(*task));
  752. atomic_set(&task->tk_count, 1);
  753. task->tk_flags = task_setup_data->flags;
  754. task->tk_ops = task_setup_data->callback_ops;
  755. task->tk_calldata = task_setup_data->callback_data;
  756. INIT_LIST_HEAD(&task->tk_task);
  757. task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
  758. task->tk_owner = current->tgid;
  759. /* Initialize workqueue for async tasks */
  760. task->tk_workqueue = task_setup_data->workqueue;
  761. if (task->tk_ops->rpc_call_prepare != NULL)
  762. task->tk_action = rpc_prepare_task;
  763. rpc_init_task_statistics(task);
  764. dprintk("RPC: new task initialized, procpid %u\n",
  765. task_pid_nr(current));
  766. }
  767. static struct rpc_task *
  768. rpc_alloc_task(void)
  769. {
  770. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  771. }
  772. /*
  773. * Create a new task for the specified client.
  774. */
  775. struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
  776. {
  777. struct rpc_task *task = setup_data->task;
  778. unsigned short flags = 0;
  779. if (task == NULL) {
  780. task = rpc_alloc_task();
  781. if (task == NULL) {
  782. rpc_release_calldata(setup_data->callback_ops,
  783. setup_data->callback_data);
  784. return ERR_PTR(-ENOMEM);
  785. }
  786. flags = RPC_TASK_DYNAMIC;
  787. }
  788. rpc_init_task(task, setup_data);
  789. task->tk_flags |= flags;
  790. dprintk("RPC: allocated task %p\n", task);
  791. return task;
  792. }
  793. static void rpc_free_task(struct rpc_task *task)
  794. {
  795. const struct rpc_call_ops *tk_ops = task->tk_ops;
  796. void *calldata = task->tk_calldata;
  797. if (task->tk_flags & RPC_TASK_DYNAMIC) {
  798. dprintk("RPC: %5u freeing task\n", task->tk_pid);
  799. mempool_free(task, rpc_task_mempool);
  800. }
  801. rpc_release_calldata(tk_ops, calldata);
  802. }
  803. static void rpc_async_release(struct work_struct *work)
  804. {
  805. rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
  806. }
  807. static void rpc_release_resources_task(struct rpc_task *task)
  808. {
  809. if (task->tk_rqstp)
  810. xprt_release(task);
  811. if (task->tk_msg.rpc_cred) {
  812. put_rpccred(task->tk_msg.rpc_cred);
  813. task->tk_msg.rpc_cred = NULL;
  814. }
  815. rpc_task_release_client(task);
  816. }
  817. static void rpc_final_put_task(struct rpc_task *task,
  818. struct workqueue_struct *q)
  819. {
  820. if (q != NULL) {
  821. INIT_WORK(&task->u.tk_work, rpc_async_release);
  822. queue_work(q, &task->u.tk_work);
  823. } else
  824. rpc_free_task(task);
  825. }
  826. static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
  827. {
  828. if (atomic_dec_and_test(&task->tk_count)) {
  829. rpc_release_resources_task(task);
  830. rpc_final_put_task(task, q);
  831. }
  832. }
  833. void rpc_put_task(struct rpc_task *task)
  834. {
  835. rpc_do_put_task(task, NULL);
  836. }
  837. EXPORT_SYMBOL_GPL(rpc_put_task);
  838. void rpc_put_task_async(struct rpc_task *task)
  839. {
  840. rpc_do_put_task(task, task->tk_workqueue);
  841. }
  842. EXPORT_SYMBOL_GPL(rpc_put_task_async);
  843. static void rpc_release_task(struct rpc_task *task)
  844. {
  845. dprintk("RPC: %5u release task\n", task->tk_pid);
  846. BUG_ON (RPC_IS_QUEUED(task));
  847. rpc_release_resources_task(task);
  848. /*
  849. * Note: at this point we have been removed from rpc_clnt->cl_tasks,
  850. * so it should be safe to use task->tk_count as a test for whether
  851. * or not any other processes still hold references to our rpc_task.
  852. */
  853. if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
  854. /* Wake up anyone who may be waiting for task completion */
  855. if (!rpc_complete_task(task))
  856. return;
  857. } else {
  858. if (!atomic_dec_and_test(&task->tk_count))
  859. return;
  860. }
  861. rpc_final_put_task(task, task->tk_workqueue);
  862. }
  863. int rpciod_up(void)
  864. {
  865. return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
  866. }
  867. void rpciod_down(void)
  868. {
  869. module_put(THIS_MODULE);
  870. }
  871. /*
  872. * Start up the rpciod workqueue.
  873. */
  874. static int rpciod_start(void)
  875. {
  876. struct workqueue_struct *wq;
  877. /*
  878. * Create the rpciod thread and wait for it to start.
  879. */
  880. dprintk("RPC: creating workqueue rpciod\n");
  881. wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM, 0);
  882. rpciod_workqueue = wq;
  883. return rpciod_workqueue != NULL;
  884. }
  885. static void rpciod_stop(void)
  886. {
  887. struct workqueue_struct *wq = NULL;
  888. if (rpciod_workqueue == NULL)
  889. return;
  890. dprintk("RPC: destroying workqueue rpciod\n");
  891. wq = rpciod_workqueue;
  892. rpciod_workqueue = NULL;
  893. destroy_workqueue(wq);
  894. }
  895. void
  896. rpc_destroy_mempool(void)
  897. {
  898. rpciod_stop();
  899. if (rpc_buffer_mempool)
  900. mempool_destroy(rpc_buffer_mempool);
  901. if (rpc_task_mempool)
  902. mempool_destroy(rpc_task_mempool);
  903. if (rpc_task_slabp)
  904. kmem_cache_destroy(rpc_task_slabp);
  905. if (rpc_buffer_slabp)
  906. kmem_cache_destroy(rpc_buffer_slabp);
  907. rpc_destroy_wait_queue(&delay_queue);
  908. }
  909. int
  910. rpc_init_mempool(void)
  911. {
  912. /*
  913. * The following is not strictly a mempool initialisation,
  914. * but there is no harm in doing it here
  915. */
  916. rpc_init_wait_queue(&delay_queue, "delayq");
  917. if (!rpciod_start())
  918. goto err_nomem;
  919. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  920. sizeof(struct rpc_task),
  921. 0, SLAB_HWCACHE_ALIGN,
  922. NULL);
  923. if (!rpc_task_slabp)
  924. goto err_nomem;
  925. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  926. RPC_BUFFER_MAXSIZE,
  927. 0, SLAB_HWCACHE_ALIGN,
  928. NULL);
  929. if (!rpc_buffer_slabp)
  930. goto err_nomem;
  931. rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
  932. rpc_task_slabp);
  933. if (!rpc_task_mempool)
  934. goto err_nomem;
  935. rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
  936. rpc_buffer_slabp);
  937. if (!rpc_buffer_mempool)
  938. goto err_nomem;
  939. return 0;
  940. err_nomem:
  941. rpc_destroy_mempool();
  942. return -ENOMEM;
  943. }