sched.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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/smp_lock.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/mutex.h>
  20. #include <linux/sunrpc/clnt.h>
  21. #include <linux/sunrpc/xprt.h>
  22. #ifdef RPC_DEBUG
  23. #define RPCDBG_FACILITY RPCDBG_SCHED
  24. #define RPC_TASK_MAGIC_ID 0xf00baa
  25. static int rpc_task_id;
  26. #endif
  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 kmem_cache_t *rpc_task_slabp __read_mostly;
  34. static kmem_cache_t *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_default_timer(struct rpc_task *task);
  38. static void rpciod_killall(void);
  39. static void rpc_async_schedule(void *);
  40. /*
  41. * RPC tasks that create another task (e.g. for contacting the portmapper)
  42. * will wait on this queue for their child's completion
  43. */
  44. static RPC_WAITQ(childq, "childq");
  45. /*
  46. * RPC tasks sit here while waiting for conditions to improve.
  47. */
  48. static RPC_WAITQ(delay_queue, "delayq");
  49. /*
  50. * All RPC tasks are linked into this list
  51. */
  52. static LIST_HEAD(all_tasks);
  53. /*
  54. * rpciod-related stuff
  55. */
  56. static DEFINE_MUTEX(rpciod_mutex);
  57. static unsigned int rpciod_users;
  58. struct workqueue_struct *rpciod_workqueue;
  59. /*
  60. * Spinlock for other critical sections of code.
  61. */
  62. static DEFINE_SPINLOCK(rpc_sched_lock);
  63. /*
  64. * Disable the timer for a given RPC task. Should be called with
  65. * queue->lock and bh_disabled in order to avoid races within
  66. * rpc_run_timer().
  67. */
  68. static inline void
  69. __rpc_disable_timer(struct rpc_task *task)
  70. {
  71. dprintk("RPC: %4d disabling timer\n", task->tk_pid);
  72. task->tk_timeout_fn = NULL;
  73. task->tk_timeout = 0;
  74. }
  75. /*
  76. * Run a timeout function.
  77. * We use the callback in order to allow __rpc_wake_up_task()
  78. * and friends to disable the timer synchronously on SMP systems
  79. * without calling del_timer_sync(). The latter could cause a
  80. * deadlock if called while we're holding spinlocks...
  81. */
  82. static void rpc_run_timer(struct rpc_task *task)
  83. {
  84. void (*callback)(struct rpc_task *);
  85. callback = task->tk_timeout_fn;
  86. task->tk_timeout_fn = NULL;
  87. if (callback && RPC_IS_QUEUED(task)) {
  88. dprintk("RPC: %4d running timer\n", task->tk_pid);
  89. callback(task);
  90. }
  91. smp_mb__before_clear_bit();
  92. clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
  93. smp_mb__after_clear_bit();
  94. }
  95. /*
  96. * Set up a timer for the current task.
  97. */
  98. static inline void
  99. __rpc_add_timer(struct rpc_task *task, rpc_action timer)
  100. {
  101. if (!task->tk_timeout)
  102. return;
  103. dprintk("RPC: %4d setting alarm for %lu ms\n",
  104. task->tk_pid, task->tk_timeout * 1000 / HZ);
  105. if (timer)
  106. task->tk_timeout_fn = timer;
  107. else
  108. task->tk_timeout_fn = __rpc_default_timer;
  109. set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
  110. mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
  111. }
  112. /*
  113. * Delete any timer for the current task. Because we use del_timer_sync(),
  114. * this function should never be called while holding queue->lock.
  115. */
  116. static void
  117. rpc_delete_timer(struct rpc_task *task)
  118. {
  119. if (RPC_IS_QUEUED(task))
  120. return;
  121. if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
  122. del_singleshot_timer_sync(&task->tk_timer);
  123. dprintk("RPC: %4d deleting timer\n", task->tk_pid);
  124. }
  125. }
  126. /*
  127. * Add new request to a priority queue.
  128. */
  129. static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
  130. {
  131. struct list_head *q;
  132. struct rpc_task *t;
  133. INIT_LIST_HEAD(&task->u.tk_wait.links);
  134. q = &queue->tasks[task->tk_priority];
  135. if (unlikely(task->tk_priority > queue->maxpriority))
  136. q = &queue->tasks[queue->maxpriority];
  137. list_for_each_entry(t, q, u.tk_wait.list) {
  138. if (t->tk_cookie == task->tk_cookie) {
  139. list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
  140. return;
  141. }
  142. }
  143. list_add_tail(&task->u.tk_wait.list, q);
  144. }
  145. /*
  146. * Add new request to wait queue.
  147. *
  148. * Swapper tasks always get inserted at the head of the queue.
  149. * This should avoid many nasty memory deadlocks and hopefully
  150. * improve overall performance.
  151. * Everyone else gets appended to the queue to ensure proper FIFO behavior.
  152. */
  153. static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
  154. {
  155. BUG_ON (RPC_IS_QUEUED(task));
  156. if (RPC_IS_PRIORITY(queue))
  157. __rpc_add_wait_queue_priority(queue, task);
  158. else if (RPC_IS_SWAPPER(task))
  159. list_add(&task->u.tk_wait.list, &queue->tasks[0]);
  160. else
  161. list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
  162. task->u.tk_wait.rpc_waitq = queue;
  163. queue->qlen++;
  164. rpc_set_queued(task);
  165. dprintk("RPC: %4d added to queue %p \"%s\"\n",
  166. task->tk_pid, queue, rpc_qname(queue));
  167. }
  168. /*
  169. * Remove request from a priority queue.
  170. */
  171. static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
  172. {
  173. struct rpc_task *t;
  174. if (!list_empty(&task->u.tk_wait.links)) {
  175. t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
  176. list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
  177. list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
  178. }
  179. list_del(&task->u.tk_wait.list);
  180. }
  181. /*
  182. * Remove request from queue.
  183. * Note: must be called with spin lock held.
  184. */
  185. static void __rpc_remove_wait_queue(struct rpc_task *task)
  186. {
  187. struct rpc_wait_queue *queue;
  188. queue = task->u.tk_wait.rpc_waitq;
  189. if (RPC_IS_PRIORITY(queue))
  190. __rpc_remove_wait_queue_priority(task);
  191. else
  192. list_del(&task->u.tk_wait.list);
  193. queue->qlen--;
  194. dprintk("RPC: %4d removed from queue %p \"%s\"\n",
  195. task->tk_pid, queue, rpc_qname(queue));
  196. }
  197. static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
  198. {
  199. queue->priority = priority;
  200. queue->count = 1 << (priority * 2);
  201. }
  202. static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
  203. {
  204. queue->cookie = cookie;
  205. queue->nr = RPC_BATCH_COUNT;
  206. }
  207. static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
  208. {
  209. rpc_set_waitqueue_priority(queue, queue->maxpriority);
  210. rpc_set_waitqueue_cookie(queue, 0);
  211. }
  212. static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
  213. {
  214. int i;
  215. spin_lock_init(&queue->lock);
  216. for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
  217. INIT_LIST_HEAD(&queue->tasks[i]);
  218. queue->maxpriority = maxprio;
  219. rpc_reset_waitqueue_priority(queue);
  220. #ifdef RPC_DEBUG
  221. queue->name = qname;
  222. #endif
  223. }
  224. void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  225. {
  226. __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
  227. }
  228. void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  229. {
  230. __rpc_init_priority_wait_queue(queue, qname, 0);
  231. }
  232. EXPORT_SYMBOL(rpc_init_wait_queue);
  233. static int rpc_wait_bit_interruptible(void *word)
  234. {
  235. if (signal_pending(current))
  236. return -ERESTARTSYS;
  237. schedule();
  238. return 0;
  239. }
  240. /*
  241. * Mark an RPC call as having completed by clearing the 'active' bit
  242. */
  243. static inline void rpc_mark_complete_task(struct rpc_task *task)
  244. {
  245. rpc_clear_active(task);
  246. wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
  247. }
  248. /*
  249. * Allow callers to wait for completion of an RPC call
  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_interruptible;
  255. return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
  256. action, TASK_INTERRUPTIBLE);
  257. }
  258. EXPORT_SYMBOL(__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. int do_ret;
  268. BUG_ON(task->tk_timeout_fn);
  269. do_ret = rpc_test_and_set_running(task);
  270. rpc_clear_queued(task);
  271. if (do_ret)
  272. return;
  273. if (RPC_IS_ASYNC(task)) {
  274. int status;
  275. INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
  276. status = queue_work(task->tk_workqueue, &task->u.tk_work);
  277. if (status < 0) {
  278. printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
  279. task->tk_status = status;
  280. return;
  281. }
  282. } else
  283. wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
  284. }
  285. /*
  286. * Place a newly initialized task on the workqueue.
  287. */
  288. static inline void
  289. rpc_schedule_run(struct rpc_task *task)
  290. {
  291. rpc_set_active(task);
  292. rpc_make_runnable(task);
  293. }
  294. /*
  295. * Prepare for sleeping on a wait queue.
  296. * By always appending tasks to the list we ensure FIFO behavior.
  297. * NB: An RPC task will only receive interrupt-driven events as long
  298. * as it's on a wait queue.
  299. */
  300. static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  301. rpc_action action, rpc_action timer)
  302. {
  303. dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
  304. rpc_qname(q), jiffies);
  305. if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
  306. printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
  307. return;
  308. }
  309. /* Mark the task as being activated if so needed */
  310. rpc_set_active(task);
  311. __rpc_add_wait_queue(q, task);
  312. BUG_ON(task->tk_callback != NULL);
  313. task->tk_callback = action;
  314. __rpc_add_timer(task, timer);
  315. }
  316. void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  317. rpc_action action, rpc_action timer)
  318. {
  319. /*
  320. * Protect the queue operations.
  321. */
  322. spin_lock_bh(&q->lock);
  323. __rpc_sleep_on(q, task, action, timer);
  324. spin_unlock_bh(&q->lock);
  325. }
  326. /**
  327. * __rpc_do_wake_up_task - wake up a single rpc_task
  328. * @task: task to be woken up
  329. *
  330. * Caller must hold queue->lock, and have cleared the task queued flag.
  331. */
  332. static void __rpc_do_wake_up_task(struct rpc_task *task)
  333. {
  334. dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
  335. #ifdef RPC_DEBUG
  336. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  337. #endif
  338. /* Has the task been executed yet? If not, we cannot wake it up! */
  339. if (!RPC_IS_ACTIVATED(task)) {
  340. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  341. return;
  342. }
  343. __rpc_disable_timer(task);
  344. __rpc_remove_wait_queue(task);
  345. rpc_make_runnable(task);
  346. dprintk("RPC: __rpc_wake_up_task done\n");
  347. }
  348. /*
  349. * Wake up the specified task
  350. */
  351. static void __rpc_wake_up_task(struct rpc_task *task)
  352. {
  353. if (rpc_start_wakeup(task)) {
  354. if (RPC_IS_QUEUED(task))
  355. __rpc_do_wake_up_task(task);
  356. rpc_finish_wakeup(task);
  357. }
  358. }
  359. /*
  360. * Default timeout handler if none specified by user
  361. */
  362. static void
  363. __rpc_default_timer(struct rpc_task *task)
  364. {
  365. dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
  366. task->tk_status = -ETIMEDOUT;
  367. rpc_wake_up_task(task);
  368. }
  369. /*
  370. * Wake up the specified task
  371. */
  372. void rpc_wake_up_task(struct rpc_task *task)
  373. {
  374. if (rpc_start_wakeup(task)) {
  375. if (RPC_IS_QUEUED(task)) {
  376. struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
  377. spin_lock_bh(&queue->lock);
  378. __rpc_do_wake_up_task(task);
  379. spin_unlock_bh(&queue->lock);
  380. }
  381. rpc_finish_wakeup(task);
  382. }
  383. }
  384. /*
  385. * Wake up the next task on a priority queue.
  386. */
  387. static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
  388. {
  389. struct list_head *q;
  390. struct rpc_task *task;
  391. /*
  392. * Service a batch of tasks from a single cookie.
  393. */
  394. q = &queue->tasks[queue->priority];
  395. if (!list_empty(q)) {
  396. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  397. if (queue->cookie == task->tk_cookie) {
  398. if (--queue->nr)
  399. goto out;
  400. list_move_tail(&task->u.tk_wait.list, q);
  401. }
  402. /*
  403. * Check if we need to switch queues.
  404. */
  405. if (--queue->count)
  406. goto new_cookie;
  407. }
  408. /*
  409. * Service the next queue.
  410. */
  411. do {
  412. if (q == &queue->tasks[0])
  413. q = &queue->tasks[queue->maxpriority];
  414. else
  415. q = q - 1;
  416. if (!list_empty(q)) {
  417. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  418. goto new_queue;
  419. }
  420. } while (q != &queue->tasks[queue->priority]);
  421. rpc_reset_waitqueue_priority(queue);
  422. return NULL;
  423. new_queue:
  424. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  425. new_cookie:
  426. rpc_set_waitqueue_cookie(queue, task->tk_cookie);
  427. out:
  428. __rpc_wake_up_task(task);
  429. return task;
  430. }
  431. /*
  432. * Wake up the next task on the wait queue.
  433. */
  434. struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
  435. {
  436. struct rpc_task *task = NULL;
  437. dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
  438. spin_lock_bh(&queue->lock);
  439. if (RPC_IS_PRIORITY(queue))
  440. task = __rpc_wake_up_next_priority(queue);
  441. else {
  442. task_for_first(task, &queue->tasks[0])
  443. __rpc_wake_up_task(task);
  444. }
  445. spin_unlock_bh(&queue->lock);
  446. return task;
  447. }
  448. /**
  449. * rpc_wake_up - wake up all rpc_tasks
  450. * @queue: rpc_wait_queue on which the tasks are sleeping
  451. *
  452. * Grabs queue->lock
  453. */
  454. void rpc_wake_up(struct rpc_wait_queue *queue)
  455. {
  456. struct rpc_task *task, *next;
  457. struct list_head *head;
  458. spin_lock_bh(&queue->lock);
  459. head = &queue->tasks[queue->maxpriority];
  460. for (;;) {
  461. list_for_each_entry_safe(task, next, head, u.tk_wait.list)
  462. __rpc_wake_up_task(task);
  463. if (head == &queue->tasks[0])
  464. break;
  465. head--;
  466. }
  467. spin_unlock_bh(&queue->lock);
  468. }
  469. /**
  470. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  471. * @queue: rpc_wait_queue on which the tasks are sleeping
  472. * @status: status value to set
  473. *
  474. * Grabs queue->lock
  475. */
  476. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  477. {
  478. struct rpc_task *task, *next;
  479. struct list_head *head;
  480. spin_lock_bh(&queue->lock);
  481. head = &queue->tasks[queue->maxpriority];
  482. for (;;) {
  483. list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
  484. task->tk_status = status;
  485. __rpc_wake_up_task(task);
  486. }
  487. if (head == &queue->tasks[0])
  488. break;
  489. head--;
  490. }
  491. spin_unlock_bh(&queue->lock);
  492. }
  493. /*
  494. * Run a task at a later time
  495. */
  496. static void __rpc_atrun(struct rpc_task *);
  497. void
  498. rpc_delay(struct rpc_task *task, unsigned long delay)
  499. {
  500. task->tk_timeout = delay;
  501. rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
  502. }
  503. static void
  504. __rpc_atrun(struct rpc_task *task)
  505. {
  506. task->tk_status = 0;
  507. rpc_wake_up_task(task);
  508. }
  509. /*
  510. * Helper to call task->tk_ops->rpc_call_prepare
  511. */
  512. static void rpc_prepare_task(struct rpc_task *task)
  513. {
  514. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  515. }
  516. /*
  517. * Helper that calls task->tk_ops->rpc_call_done if it exists
  518. */
  519. void rpc_exit_task(struct rpc_task *task)
  520. {
  521. task->tk_action = NULL;
  522. if (task->tk_ops->rpc_call_done != NULL) {
  523. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  524. if (task->tk_action != NULL) {
  525. WARN_ON(RPC_ASSASSINATED(task));
  526. /* Always release the RPC slot and buffer memory */
  527. xprt_release(task);
  528. }
  529. }
  530. }
  531. EXPORT_SYMBOL(rpc_exit_task);
  532. /*
  533. * This is the RPC `scheduler' (or rather, the finite state machine).
  534. */
  535. static int __rpc_execute(struct rpc_task *task)
  536. {
  537. int status = 0;
  538. dprintk("RPC: %4d rpc_execute flgs %x\n",
  539. task->tk_pid, task->tk_flags);
  540. BUG_ON(RPC_IS_QUEUED(task));
  541. for (;;) {
  542. /*
  543. * Garbage collection of pending timers...
  544. */
  545. rpc_delete_timer(task);
  546. /*
  547. * Execute any pending callback.
  548. */
  549. if (RPC_DO_CALLBACK(task)) {
  550. /* Define a callback save pointer */
  551. void (*save_callback)(struct rpc_task *);
  552. /*
  553. * If a callback exists, save it, reset it,
  554. * call it.
  555. * The save is needed to stop from resetting
  556. * another callback set within the callback handler
  557. * - Dave
  558. */
  559. save_callback=task->tk_callback;
  560. task->tk_callback=NULL;
  561. lock_kernel();
  562. save_callback(task);
  563. unlock_kernel();
  564. }
  565. /*
  566. * Perform the next FSM step.
  567. * tk_action may be NULL when the task has been killed
  568. * by someone else.
  569. */
  570. if (!RPC_IS_QUEUED(task)) {
  571. if (task->tk_action == NULL)
  572. break;
  573. lock_kernel();
  574. task->tk_action(task);
  575. unlock_kernel();
  576. }
  577. /*
  578. * Lockless check for whether task is sleeping or not.
  579. */
  580. if (!RPC_IS_QUEUED(task))
  581. continue;
  582. rpc_clear_running(task);
  583. if (RPC_IS_ASYNC(task)) {
  584. /* Careful! we may have raced... */
  585. if (RPC_IS_QUEUED(task))
  586. return 0;
  587. if (rpc_test_and_set_running(task))
  588. return 0;
  589. continue;
  590. }
  591. /* sync task: sleep here */
  592. dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
  593. /* Note: Caller should be using rpc_clnt_sigmask() */
  594. status = out_of_line_wait_on_bit(&task->tk_runstate,
  595. RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
  596. TASK_INTERRUPTIBLE);
  597. if (status == -ERESTARTSYS) {
  598. /*
  599. * When a sync task receives a signal, it exits with
  600. * -ERESTARTSYS. In order to catch any callbacks that
  601. * clean up after sleeping on some queue, we don't
  602. * break the loop here, but go around once more.
  603. */
  604. dprintk("RPC: %4d got signal\n", task->tk_pid);
  605. task->tk_flags |= RPC_TASK_KILLED;
  606. rpc_exit(task, -ERESTARTSYS);
  607. rpc_wake_up_task(task);
  608. }
  609. rpc_set_running(task);
  610. dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
  611. }
  612. dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
  613. /* Wake up anyone who is waiting for task completion */
  614. rpc_mark_complete_task(task);
  615. /* Release all resources associated with the task */
  616. rpc_release_task(task);
  617. return status;
  618. }
  619. /*
  620. * User-visible entry point to the scheduler.
  621. *
  622. * This may be called recursively if e.g. an async NFS task updates
  623. * the attributes and finds that dirty pages must be flushed.
  624. * NOTE: Upon exit of this function the task is guaranteed to be
  625. * released. In particular note that tk_release() will have
  626. * been called, so your task memory may have been freed.
  627. */
  628. int
  629. rpc_execute(struct rpc_task *task)
  630. {
  631. rpc_set_active(task);
  632. rpc_set_running(task);
  633. return __rpc_execute(task);
  634. }
  635. static void rpc_async_schedule(void *arg)
  636. {
  637. __rpc_execute((struct rpc_task *)arg);
  638. }
  639. /**
  640. * rpc_malloc - allocate an RPC buffer
  641. * @task: RPC task that will use this buffer
  642. * @size: requested byte size
  643. *
  644. * We try to ensure that some NFS reads and writes can always proceed
  645. * by using a mempool when allocating 'small' buffers.
  646. * In order to avoid memory starvation triggering more writebacks of
  647. * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
  648. */
  649. void * rpc_malloc(struct rpc_task *task, size_t size)
  650. {
  651. struct rpc_rqst *req = task->tk_rqstp;
  652. gfp_t gfp;
  653. if (task->tk_flags & RPC_TASK_SWAPPER)
  654. gfp = GFP_ATOMIC;
  655. else
  656. gfp = GFP_NOFS;
  657. if (size > RPC_BUFFER_MAXSIZE) {
  658. req->rq_buffer = kmalloc(size, gfp);
  659. if (req->rq_buffer)
  660. req->rq_bufsize = size;
  661. } else {
  662. req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
  663. if (req->rq_buffer)
  664. req->rq_bufsize = RPC_BUFFER_MAXSIZE;
  665. }
  666. return req->rq_buffer;
  667. }
  668. /**
  669. * rpc_free - free buffer allocated via rpc_malloc
  670. * @task: RPC task with a buffer to be freed
  671. *
  672. */
  673. void rpc_free(struct rpc_task *task)
  674. {
  675. struct rpc_rqst *req = task->tk_rqstp;
  676. if (req->rq_buffer) {
  677. if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
  678. mempool_free(req->rq_buffer, rpc_buffer_mempool);
  679. else
  680. kfree(req->rq_buffer);
  681. req->rq_buffer = NULL;
  682. req->rq_bufsize = 0;
  683. }
  684. }
  685. /*
  686. * Creation and deletion of RPC task structures
  687. */
  688. void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  689. {
  690. memset(task, 0, sizeof(*task));
  691. init_timer(&task->tk_timer);
  692. task->tk_timer.data = (unsigned long) task;
  693. task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
  694. atomic_set(&task->tk_count, 1);
  695. task->tk_client = clnt;
  696. task->tk_flags = flags;
  697. task->tk_ops = tk_ops;
  698. if (tk_ops->rpc_call_prepare != NULL)
  699. task->tk_action = rpc_prepare_task;
  700. task->tk_calldata = calldata;
  701. /* Initialize retry counters */
  702. task->tk_garb_retry = 2;
  703. task->tk_cred_retry = 2;
  704. task->tk_priority = RPC_PRIORITY_NORMAL;
  705. task->tk_cookie = (unsigned long)current;
  706. /* Initialize workqueue for async tasks */
  707. task->tk_workqueue = rpciod_workqueue;
  708. if (clnt) {
  709. atomic_inc(&clnt->cl_users);
  710. if (clnt->cl_softrtry)
  711. task->tk_flags |= RPC_TASK_SOFT;
  712. if (!clnt->cl_intr)
  713. task->tk_flags |= RPC_TASK_NOINTR;
  714. }
  715. #ifdef RPC_DEBUG
  716. task->tk_magic = RPC_TASK_MAGIC_ID;
  717. task->tk_pid = rpc_task_id++;
  718. #endif
  719. /* Add to global list of all tasks */
  720. spin_lock(&rpc_sched_lock);
  721. list_add_tail(&task->tk_task, &all_tasks);
  722. spin_unlock(&rpc_sched_lock);
  723. BUG_ON(task->tk_ops == NULL);
  724. /* starting timestamp */
  725. task->tk_start = jiffies;
  726. dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
  727. current->pid);
  728. }
  729. static struct rpc_task *
  730. rpc_alloc_task(void)
  731. {
  732. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  733. }
  734. static void rpc_free_task(struct rpc_task *task)
  735. {
  736. dprintk("RPC: %4d freeing task\n", task->tk_pid);
  737. mempool_free(task, rpc_task_mempool);
  738. }
  739. /*
  740. * Create a new task for the specified client. We have to
  741. * clean up after an allocation failure, as the client may
  742. * have specified "oneshot".
  743. */
  744. struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  745. {
  746. struct rpc_task *task;
  747. task = rpc_alloc_task();
  748. if (!task)
  749. goto cleanup;
  750. rpc_init_task(task, clnt, flags, tk_ops, calldata);
  751. dprintk("RPC: %4d allocated task\n", task->tk_pid);
  752. task->tk_flags |= RPC_TASK_DYNAMIC;
  753. out:
  754. return task;
  755. cleanup:
  756. /* Check whether to release the client */
  757. if (clnt) {
  758. printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
  759. atomic_read(&clnt->cl_users), clnt->cl_oneshot);
  760. atomic_inc(&clnt->cl_users); /* pretend we were used ... */
  761. rpc_release_client(clnt);
  762. }
  763. goto out;
  764. }
  765. void rpc_release_task(struct rpc_task *task)
  766. {
  767. const struct rpc_call_ops *tk_ops = task->tk_ops;
  768. void *calldata = task->tk_calldata;
  769. #ifdef RPC_DEBUG
  770. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  771. #endif
  772. if (!atomic_dec_and_test(&task->tk_count))
  773. return;
  774. dprintk("RPC: %4d release task\n", task->tk_pid);
  775. /* Remove from global task list */
  776. spin_lock(&rpc_sched_lock);
  777. list_del(&task->tk_task);
  778. spin_unlock(&rpc_sched_lock);
  779. BUG_ON (RPC_IS_QUEUED(task));
  780. /* Synchronously delete any running timer */
  781. rpc_delete_timer(task);
  782. /* Release resources */
  783. if (task->tk_rqstp)
  784. xprt_release(task);
  785. if (task->tk_msg.rpc_cred)
  786. rpcauth_unbindcred(task);
  787. if (task->tk_client) {
  788. rpc_release_client(task->tk_client);
  789. task->tk_client = NULL;
  790. }
  791. #ifdef RPC_DEBUG
  792. task->tk_magic = 0;
  793. #endif
  794. if (task->tk_flags & RPC_TASK_DYNAMIC)
  795. rpc_free_task(task);
  796. if (tk_ops->rpc_release)
  797. tk_ops->rpc_release(calldata);
  798. }
  799. /**
  800. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  801. * @clnt: pointer to RPC client
  802. * @flags: RPC flags
  803. * @ops: RPC call ops
  804. * @data: user call data
  805. */
  806. struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
  807. const struct rpc_call_ops *ops,
  808. void *data)
  809. {
  810. struct rpc_task *task;
  811. task = rpc_new_task(clnt, flags, ops, data);
  812. if (task == NULL) {
  813. if (ops->rpc_release != NULL)
  814. ops->rpc_release(data);
  815. return ERR_PTR(-ENOMEM);
  816. }
  817. atomic_inc(&task->tk_count);
  818. rpc_execute(task);
  819. return task;
  820. }
  821. EXPORT_SYMBOL(rpc_run_task);
  822. /**
  823. * rpc_find_parent - find the parent of a child task.
  824. * @child: child task
  825. * @parent: parent task
  826. *
  827. * Checks that the parent task is still sleeping on the
  828. * queue 'childq'. If so returns a pointer to the parent.
  829. * Upon failure returns NULL.
  830. *
  831. * Caller must hold childq.lock
  832. */
  833. static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
  834. {
  835. struct rpc_task *task;
  836. struct list_head *le;
  837. task_for_each(task, le, &childq.tasks[0])
  838. if (task == parent)
  839. return parent;
  840. return NULL;
  841. }
  842. static void rpc_child_exit(struct rpc_task *child, void *calldata)
  843. {
  844. struct rpc_task *parent;
  845. spin_lock_bh(&childq.lock);
  846. if ((parent = rpc_find_parent(child, calldata)) != NULL) {
  847. parent->tk_status = child->tk_status;
  848. __rpc_wake_up_task(parent);
  849. }
  850. spin_unlock_bh(&childq.lock);
  851. }
  852. static const struct rpc_call_ops rpc_child_ops = {
  853. .rpc_call_done = rpc_child_exit,
  854. };
  855. /*
  856. * Note: rpc_new_task releases the client after a failure.
  857. */
  858. struct rpc_task *
  859. rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
  860. {
  861. struct rpc_task *task;
  862. task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
  863. if (!task)
  864. goto fail;
  865. return task;
  866. fail:
  867. parent->tk_status = -ENOMEM;
  868. return NULL;
  869. }
  870. void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
  871. {
  872. spin_lock_bh(&childq.lock);
  873. /* N.B. Is it possible for the child to have already finished? */
  874. __rpc_sleep_on(&childq, task, func, NULL);
  875. rpc_schedule_run(child);
  876. spin_unlock_bh(&childq.lock);
  877. }
  878. /*
  879. * Kill all tasks for the given client.
  880. * XXX: kill their descendants as well?
  881. */
  882. void rpc_killall_tasks(struct rpc_clnt *clnt)
  883. {
  884. struct rpc_task *rovr;
  885. struct list_head *le;
  886. dprintk("RPC: killing all tasks for client %p\n", clnt);
  887. /*
  888. * Spin lock all_tasks to prevent changes...
  889. */
  890. spin_lock(&rpc_sched_lock);
  891. alltask_for_each(rovr, le, &all_tasks) {
  892. if (! RPC_IS_ACTIVATED(rovr))
  893. continue;
  894. if (!clnt || rovr->tk_client == clnt) {
  895. rovr->tk_flags |= RPC_TASK_KILLED;
  896. rpc_exit(rovr, -EIO);
  897. rpc_wake_up_task(rovr);
  898. }
  899. }
  900. spin_unlock(&rpc_sched_lock);
  901. }
  902. static DECLARE_MUTEX_LOCKED(rpciod_running);
  903. static void rpciod_killall(void)
  904. {
  905. unsigned long flags;
  906. while (!list_empty(&all_tasks)) {
  907. clear_thread_flag(TIF_SIGPENDING);
  908. rpc_killall_tasks(NULL);
  909. flush_workqueue(rpciod_workqueue);
  910. if (!list_empty(&all_tasks)) {
  911. dprintk("rpciod_killall: waiting for tasks to exit\n");
  912. yield();
  913. }
  914. }
  915. spin_lock_irqsave(&current->sighand->siglock, flags);
  916. recalc_sigpending();
  917. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  918. }
  919. /*
  920. * Start up the rpciod process if it's not already running.
  921. */
  922. int
  923. rpciod_up(void)
  924. {
  925. struct workqueue_struct *wq;
  926. int error = 0;
  927. mutex_lock(&rpciod_mutex);
  928. dprintk("rpciod_up: users %d\n", rpciod_users);
  929. rpciod_users++;
  930. if (rpciod_workqueue)
  931. goto out;
  932. /*
  933. * If there's no pid, we should be the first user.
  934. */
  935. if (rpciod_users > 1)
  936. printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
  937. /*
  938. * Create the rpciod thread and wait for it to start.
  939. */
  940. error = -ENOMEM;
  941. wq = create_workqueue("rpciod");
  942. if (wq == NULL) {
  943. printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
  944. rpciod_users--;
  945. goto out;
  946. }
  947. rpciod_workqueue = wq;
  948. error = 0;
  949. out:
  950. mutex_unlock(&rpciod_mutex);
  951. return error;
  952. }
  953. void
  954. rpciod_down(void)
  955. {
  956. mutex_lock(&rpciod_mutex);
  957. dprintk("rpciod_down sema %d\n", rpciod_users);
  958. if (rpciod_users) {
  959. if (--rpciod_users)
  960. goto out;
  961. } else
  962. printk(KERN_WARNING "rpciod_down: no users??\n");
  963. if (!rpciod_workqueue) {
  964. dprintk("rpciod_down: Nothing to do!\n");
  965. goto out;
  966. }
  967. rpciod_killall();
  968. destroy_workqueue(rpciod_workqueue);
  969. rpciod_workqueue = NULL;
  970. out:
  971. mutex_unlock(&rpciod_mutex);
  972. }
  973. #ifdef RPC_DEBUG
  974. void rpc_show_tasks(void)
  975. {
  976. struct list_head *le;
  977. struct rpc_task *t;
  978. spin_lock(&rpc_sched_lock);
  979. if (list_empty(&all_tasks)) {
  980. spin_unlock(&rpc_sched_lock);
  981. return;
  982. }
  983. printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
  984. "-rpcwait -action- ---ops--\n");
  985. alltask_for_each(t, le, &all_tasks) {
  986. const char *rpc_waitq = "none";
  987. if (RPC_IS_QUEUED(t))
  988. rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
  989. printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
  990. t->tk_pid,
  991. (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
  992. t->tk_flags, t->tk_status,
  993. t->tk_client,
  994. (t->tk_client ? t->tk_client->cl_prog : 0),
  995. t->tk_rqstp, t->tk_timeout,
  996. rpc_waitq,
  997. t->tk_action, t->tk_ops);
  998. }
  999. spin_unlock(&rpc_sched_lock);
  1000. }
  1001. #endif
  1002. void
  1003. rpc_destroy_mempool(void)
  1004. {
  1005. if (rpc_buffer_mempool)
  1006. mempool_destroy(rpc_buffer_mempool);
  1007. if (rpc_task_mempool)
  1008. mempool_destroy(rpc_task_mempool);
  1009. if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
  1010. printk(KERN_INFO "rpc_task: not all structures were freed\n");
  1011. if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
  1012. printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
  1013. }
  1014. int
  1015. rpc_init_mempool(void)
  1016. {
  1017. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  1018. sizeof(struct rpc_task),
  1019. 0, SLAB_HWCACHE_ALIGN,
  1020. NULL, NULL);
  1021. if (!rpc_task_slabp)
  1022. goto err_nomem;
  1023. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  1024. RPC_BUFFER_MAXSIZE,
  1025. 0, SLAB_HWCACHE_ALIGN,
  1026. NULL, NULL);
  1027. if (!rpc_buffer_slabp)
  1028. goto err_nomem;
  1029. rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
  1030. rpc_task_slabp);
  1031. if (!rpc_task_mempool)
  1032. goto err_nomem;
  1033. rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
  1034. rpc_buffer_slabp);
  1035. if (!rpc_buffer_mempool)
  1036. goto err_nomem;
  1037. return 0;
  1038. err_nomem:
  1039. rpc_destroy_mempool();
  1040. return -ENOMEM;
  1041. }