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