sched.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. static 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. rpc_set_queued(task);
  163. dprintk("RPC: %4d added to queue %p \"%s\"\n",
  164. task->tk_pid, queue, rpc_qname(queue));
  165. }
  166. /*
  167. * Remove request from a priority queue.
  168. */
  169. static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
  170. {
  171. struct rpc_task *t;
  172. if (!list_empty(&task->u.tk_wait.links)) {
  173. t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
  174. list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
  175. list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
  176. }
  177. list_del(&task->u.tk_wait.list);
  178. }
  179. /*
  180. * Remove request from queue.
  181. * Note: must be called with spin lock held.
  182. */
  183. static void __rpc_remove_wait_queue(struct rpc_task *task)
  184. {
  185. struct rpc_wait_queue *queue;
  186. queue = task->u.tk_wait.rpc_waitq;
  187. if (RPC_IS_PRIORITY(queue))
  188. __rpc_remove_wait_queue_priority(task);
  189. else
  190. list_del(&task->u.tk_wait.list);
  191. dprintk("RPC: %4d removed from queue %p \"%s\"\n",
  192. task->tk_pid, queue, rpc_qname(queue));
  193. }
  194. static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
  195. {
  196. queue->priority = priority;
  197. queue->count = 1 << (priority * 2);
  198. }
  199. static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
  200. {
  201. queue->cookie = cookie;
  202. queue->nr = RPC_BATCH_COUNT;
  203. }
  204. static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
  205. {
  206. rpc_set_waitqueue_priority(queue, queue->maxpriority);
  207. rpc_set_waitqueue_cookie(queue, 0);
  208. }
  209. static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
  210. {
  211. int i;
  212. spin_lock_init(&queue->lock);
  213. for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
  214. INIT_LIST_HEAD(&queue->tasks[i]);
  215. queue->maxpriority = maxprio;
  216. rpc_reset_waitqueue_priority(queue);
  217. #ifdef RPC_DEBUG
  218. queue->name = qname;
  219. #endif
  220. }
  221. void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  222. {
  223. __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
  224. }
  225. void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  226. {
  227. __rpc_init_priority_wait_queue(queue, qname, 0);
  228. }
  229. EXPORT_SYMBOL(rpc_init_wait_queue);
  230. static int rpc_wait_bit_interruptible(void *word)
  231. {
  232. if (signal_pending(current))
  233. return -ERESTARTSYS;
  234. schedule();
  235. return 0;
  236. }
  237. /*
  238. * Mark an RPC call as having completed by clearing the 'active' bit
  239. */
  240. static inline void rpc_mark_complete_task(struct rpc_task *task)
  241. {
  242. rpc_clear_active(task);
  243. wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
  244. }
  245. /*
  246. * Allow callers to wait for completion of an RPC call
  247. */
  248. int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
  249. {
  250. if (action == NULL)
  251. action = rpc_wait_bit_interruptible;
  252. return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
  253. action, TASK_INTERRUPTIBLE);
  254. }
  255. EXPORT_SYMBOL(__rpc_wait_for_completion_task);
  256. /*
  257. * Make an RPC task runnable.
  258. *
  259. * Note: If the task is ASYNC, this must be called with
  260. * the spinlock held to protect the wait queue operation.
  261. */
  262. static void rpc_make_runnable(struct rpc_task *task)
  263. {
  264. int do_ret;
  265. BUG_ON(task->tk_timeout_fn);
  266. do_ret = rpc_test_and_set_running(task);
  267. rpc_clear_queued(task);
  268. if (do_ret)
  269. return;
  270. if (RPC_IS_ASYNC(task)) {
  271. int status;
  272. INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
  273. status = queue_work(task->tk_workqueue, &task->u.tk_work);
  274. if (status < 0) {
  275. printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
  276. task->tk_status = status;
  277. return;
  278. }
  279. } else
  280. wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
  281. }
  282. /*
  283. * Place a newly initialized task on the workqueue.
  284. */
  285. static inline void
  286. rpc_schedule_run(struct rpc_task *task)
  287. {
  288. rpc_set_active(task);
  289. rpc_make_runnable(task);
  290. }
  291. /*
  292. * Prepare for sleeping on a wait queue.
  293. * By always appending tasks to the list we ensure FIFO behavior.
  294. * NB: An RPC task will only receive interrupt-driven events as long
  295. * as it's on a wait queue.
  296. */
  297. static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  298. rpc_action action, rpc_action timer)
  299. {
  300. dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
  301. rpc_qname(q), jiffies);
  302. if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
  303. printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
  304. return;
  305. }
  306. /* Mark the task as being activated if so needed */
  307. rpc_set_active(task);
  308. __rpc_add_wait_queue(q, task);
  309. BUG_ON(task->tk_callback != NULL);
  310. task->tk_callback = action;
  311. __rpc_add_timer(task, timer);
  312. }
  313. void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  314. rpc_action action, rpc_action timer)
  315. {
  316. /*
  317. * Protect the queue operations.
  318. */
  319. spin_lock_bh(&q->lock);
  320. __rpc_sleep_on(q, task, action, timer);
  321. spin_unlock_bh(&q->lock);
  322. }
  323. /**
  324. * __rpc_do_wake_up_task - wake up a single rpc_task
  325. * @task: task to be woken up
  326. *
  327. * Caller must hold queue->lock, and have cleared the task queued flag.
  328. */
  329. static void __rpc_do_wake_up_task(struct rpc_task *task)
  330. {
  331. dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
  332. #ifdef RPC_DEBUG
  333. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  334. #endif
  335. /* Has the task been executed yet? If not, we cannot wake it up! */
  336. if (!RPC_IS_ACTIVATED(task)) {
  337. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  338. return;
  339. }
  340. __rpc_disable_timer(task);
  341. __rpc_remove_wait_queue(task);
  342. rpc_make_runnable(task);
  343. dprintk("RPC: __rpc_wake_up_task done\n");
  344. }
  345. /*
  346. * Wake up the specified task
  347. */
  348. static void __rpc_wake_up_task(struct rpc_task *task)
  349. {
  350. if (rpc_start_wakeup(task)) {
  351. if (RPC_IS_QUEUED(task))
  352. __rpc_do_wake_up_task(task);
  353. rpc_finish_wakeup(task);
  354. }
  355. }
  356. /*
  357. * Default timeout handler if none specified by user
  358. */
  359. static void
  360. __rpc_default_timer(struct rpc_task *task)
  361. {
  362. dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
  363. task->tk_status = -ETIMEDOUT;
  364. rpc_wake_up_task(task);
  365. }
  366. /*
  367. * Wake up the specified task
  368. */
  369. void rpc_wake_up_task(struct rpc_task *task)
  370. {
  371. if (rpc_start_wakeup(task)) {
  372. if (RPC_IS_QUEUED(task)) {
  373. struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
  374. spin_lock_bh(&queue->lock);
  375. __rpc_do_wake_up_task(task);
  376. spin_unlock_bh(&queue->lock);
  377. }
  378. rpc_finish_wakeup(task);
  379. }
  380. }
  381. /*
  382. * Wake up the next task on a priority queue.
  383. */
  384. static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
  385. {
  386. struct list_head *q;
  387. struct rpc_task *task;
  388. /*
  389. * Service a batch of tasks from a single cookie.
  390. */
  391. q = &queue->tasks[queue->priority];
  392. if (!list_empty(q)) {
  393. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  394. if (queue->cookie == task->tk_cookie) {
  395. if (--queue->nr)
  396. goto out;
  397. list_move_tail(&task->u.tk_wait.list, q);
  398. }
  399. /*
  400. * Check if we need to switch queues.
  401. */
  402. if (--queue->count)
  403. goto new_cookie;
  404. }
  405. /*
  406. * Service the next queue.
  407. */
  408. do {
  409. if (q == &queue->tasks[0])
  410. q = &queue->tasks[queue->maxpriority];
  411. else
  412. q = q - 1;
  413. if (!list_empty(q)) {
  414. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  415. goto new_queue;
  416. }
  417. } while (q != &queue->tasks[queue->priority]);
  418. rpc_reset_waitqueue_priority(queue);
  419. return NULL;
  420. new_queue:
  421. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  422. new_cookie:
  423. rpc_set_waitqueue_cookie(queue, task->tk_cookie);
  424. out:
  425. __rpc_wake_up_task(task);
  426. return task;
  427. }
  428. /*
  429. * Wake up the next task on the wait queue.
  430. */
  431. struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
  432. {
  433. struct rpc_task *task = NULL;
  434. dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
  435. spin_lock_bh(&queue->lock);
  436. if (RPC_IS_PRIORITY(queue))
  437. task = __rpc_wake_up_next_priority(queue);
  438. else {
  439. task_for_first(task, &queue->tasks[0])
  440. __rpc_wake_up_task(task);
  441. }
  442. spin_unlock_bh(&queue->lock);
  443. return task;
  444. }
  445. /**
  446. * rpc_wake_up - wake up all rpc_tasks
  447. * @queue: rpc_wait_queue on which the tasks are sleeping
  448. *
  449. * Grabs queue->lock
  450. */
  451. void rpc_wake_up(struct rpc_wait_queue *queue)
  452. {
  453. struct rpc_task *task;
  454. struct list_head *head;
  455. spin_lock_bh(&queue->lock);
  456. head = &queue->tasks[queue->maxpriority];
  457. for (;;) {
  458. while (!list_empty(head)) {
  459. task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
  460. __rpc_wake_up_task(task);
  461. }
  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 list_head *head;
  478. struct rpc_task *task;
  479. spin_lock_bh(&queue->lock);
  480. head = &queue->tasks[queue->maxpriority];
  481. for (;;) {
  482. while (!list_empty(head)) {
  483. task = list_entry(head->next, struct rpc_task, 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. dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
  725. current->pid);
  726. }
  727. static struct rpc_task *
  728. rpc_alloc_task(void)
  729. {
  730. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  731. }
  732. static void rpc_free_task(struct rpc_task *task)
  733. {
  734. dprintk("RPC: %4d freeing task\n", task->tk_pid);
  735. mempool_free(task, rpc_task_mempool);
  736. }
  737. /*
  738. * Create a new task for the specified client. We have to
  739. * clean up after an allocation failure, as the client may
  740. * have specified "oneshot".
  741. */
  742. struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  743. {
  744. struct rpc_task *task;
  745. task = rpc_alloc_task();
  746. if (!task)
  747. goto cleanup;
  748. rpc_init_task(task, clnt, flags, tk_ops, calldata);
  749. dprintk("RPC: %4d allocated task\n", task->tk_pid);
  750. task->tk_flags |= RPC_TASK_DYNAMIC;
  751. out:
  752. return task;
  753. cleanup:
  754. /* Check whether to release the client */
  755. if (clnt) {
  756. printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
  757. atomic_read(&clnt->cl_users), clnt->cl_oneshot);
  758. atomic_inc(&clnt->cl_users); /* pretend we were used ... */
  759. rpc_release_client(clnt);
  760. }
  761. goto out;
  762. }
  763. void rpc_release_task(struct rpc_task *task)
  764. {
  765. const struct rpc_call_ops *tk_ops = task->tk_ops;
  766. void *calldata = task->tk_calldata;
  767. #ifdef RPC_DEBUG
  768. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  769. #endif
  770. if (!atomic_dec_and_test(&task->tk_count))
  771. return;
  772. dprintk("RPC: %4d release task\n", task->tk_pid);
  773. /* Remove from global task list */
  774. spin_lock(&rpc_sched_lock);
  775. list_del(&task->tk_task);
  776. spin_unlock(&rpc_sched_lock);
  777. BUG_ON (RPC_IS_QUEUED(task));
  778. /* Synchronously delete any running timer */
  779. rpc_delete_timer(task);
  780. /* Release resources */
  781. if (task->tk_rqstp)
  782. xprt_release(task);
  783. if (task->tk_msg.rpc_cred)
  784. rpcauth_unbindcred(task);
  785. if (task->tk_client) {
  786. rpc_release_client(task->tk_client);
  787. task->tk_client = NULL;
  788. }
  789. #ifdef RPC_DEBUG
  790. task->tk_magic = 0;
  791. #endif
  792. if (task->tk_flags & RPC_TASK_DYNAMIC)
  793. rpc_free_task(task);
  794. if (tk_ops->rpc_release)
  795. tk_ops->rpc_release(calldata);
  796. }
  797. /**
  798. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  799. * @clnt - pointer to RPC client
  800. * @flags - RPC flags
  801. * @ops - RPC call ops
  802. * @data - user call data
  803. */
  804. struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
  805. const struct rpc_call_ops *ops,
  806. void *data)
  807. {
  808. struct rpc_task *task;
  809. task = rpc_new_task(clnt, flags, ops, data);
  810. if (task == NULL)
  811. return ERR_PTR(-ENOMEM);
  812. atomic_inc(&task->tk_count);
  813. rpc_execute(task);
  814. return task;
  815. }
  816. EXPORT_SYMBOL(rpc_run_task);
  817. /**
  818. * rpc_find_parent - find the parent of a child task.
  819. * @child: child task
  820. *
  821. * Checks that the parent task is still sleeping on the
  822. * queue 'childq'. If so returns a pointer to the parent.
  823. * Upon failure returns NULL.
  824. *
  825. * Caller must hold childq.lock
  826. */
  827. static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
  828. {
  829. struct rpc_task *task;
  830. struct list_head *le;
  831. task_for_each(task, le, &childq.tasks[0])
  832. if (task == parent)
  833. return parent;
  834. return NULL;
  835. }
  836. static void rpc_child_exit(struct rpc_task *child, void *calldata)
  837. {
  838. struct rpc_task *parent;
  839. spin_lock_bh(&childq.lock);
  840. if ((parent = rpc_find_parent(child, calldata)) != NULL) {
  841. parent->tk_status = child->tk_status;
  842. __rpc_wake_up_task(parent);
  843. }
  844. spin_unlock_bh(&childq.lock);
  845. }
  846. static const struct rpc_call_ops rpc_child_ops = {
  847. .rpc_call_done = rpc_child_exit,
  848. };
  849. /*
  850. * Note: rpc_new_task releases the client after a failure.
  851. */
  852. struct rpc_task *
  853. rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
  854. {
  855. struct rpc_task *task;
  856. task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
  857. if (!task)
  858. goto fail;
  859. return task;
  860. fail:
  861. parent->tk_status = -ENOMEM;
  862. return NULL;
  863. }
  864. void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
  865. {
  866. spin_lock_bh(&childq.lock);
  867. /* N.B. Is it possible for the child to have already finished? */
  868. __rpc_sleep_on(&childq, task, func, NULL);
  869. rpc_schedule_run(child);
  870. spin_unlock_bh(&childq.lock);
  871. }
  872. /*
  873. * Kill all tasks for the given client.
  874. * XXX: kill their descendants as well?
  875. */
  876. void rpc_killall_tasks(struct rpc_clnt *clnt)
  877. {
  878. struct rpc_task *rovr;
  879. struct list_head *le;
  880. dprintk("RPC: killing all tasks for client %p\n", clnt);
  881. /*
  882. * Spin lock all_tasks to prevent changes...
  883. */
  884. spin_lock(&rpc_sched_lock);
  885. alltask_for_each(rovr, le, &all_tasks) {
  886. if (! RPC_IS_ACTIVATED(rovr))
  887. continue;
  888. if (!clnt || rovr->tk_client == clnt) {
  889. rovr->tk_flags |= RPC_TASK_KILLED;
  890. rpc_exit(rovr, -EIO);
  891. rpc_wake_up_task(rovr);
  892. }
  893. }
  894. spin_unlock(&rpc_sched_lock);
  895. }
  896. static DECLARE_MUTEX_LOCKED(rpciod_running);
  897. static void rpciod_killall(void)
  898. {
  899. unsigned long flags;
  900. while (!list_empty(&all_tasks)) {
  901. clear_thread_flag(TIF_SIGPENDING);
  902. rpc_killall_tasks(NULL);
  903. flush_workqueue(rpciod_workqueue);
  904. if (!list_empty(&all_tasks)) {
  905. dprintk("rpciod_killall: waiting for tasks to exit\n");
  906. yield();
  907. }
  908. }
  909. spin_lock_irqsave(&current->sighand->siglock, flags);
  910. recalc_sigpending();
  911. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  912. }
  913. /*
  914. * Start up the rpciod process if it's not already running.
  915. */
  916. int
  917. rpciod_up(void)
  918. {
  919. struct workqueue_struct *wq;
  920. int error = 0;
  921. down(&rpciod_sema);
  922. dprintk("rpciod_up: users %d\n", rpciod_users);
  923. rpciod_users++;
  924. if (rpciod_workqueue)
  925. goto out;
  926. /*
  927. * If there's no pid, we should be the first user.
  928. */
  929. if (rpciod_users > 1)
  930. printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
  931. /*
  932. * Create the rpciod thread and wait for it to start.
  933. */
  934. error = -ENOMEM;
  935. wq = create_workqueue("rpciod");
  936. if (wq == NULL) {
  937. printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
  938. rpciod_users--;
  939. goto out;
  940. }
  941. rpciod_workqueue = wq;
  942. error = 0;
  943. out:
  944. up(&rpciod_sema);
  945. return error;
  946. }
  947. void
  948. rpciod_down(void)
  949. {
  950. down(&rpciod_sema);
  951. dprintk("rpciod_down sema %d\n", rpciod_users);
  952. if (rpciod_users) {
  953. if (--rpciod_users)
  954. goto out;
  955. } else
  956. printk(KERN_WARNING "rpciod_down: no users??\n");
  957. if (!rpciod_workqueue) {
  958. dprintk("rpciod_down: Nothing to do!\n");
  959. goto out;
  960. }
  961. rpciod_killall();
  962. destroy_workqueue(rpciod_workqueue);
  963. rpciod_workqueue = NULL;
  964. out:
  965. up(&rpciod_sema);
  966. }
  967. #ifdef RPC_DEBUG
  968. void rpc_show_tasks(void)
  969. {
  970. struct list_head *le;
  971. struct rpc_task *t;
  972. spin_lock(&rpc_sched_lock);
  973. if (list_empty(&all_tasks)) {
  974. spin_unlock(&rpc_sched_lock);
  975. return;
  976. }
  977. printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
  978. "-rpcwait -action- ---ops--\n");
  979. alltask_for_each(t, le, &all_tasks) {
  980. const char *rpc_waitq = "none";
  981. if (RPC_IS_QUEUED(t))
  982. rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
  983. printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
  984. t->tk_pid,
  985. (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
  986. t->tk_flags, t->tk_status,
  987. t->tk_client,
  988. (t->tk_client ? t->tk_client->cl_prog : 0),
  989. t->tk_rqstp, t->tk_timeout,
  990. rpc_waitq,
  991. t->tk_action, t->tk_ops);
  992. }
  993. spin_unlock(&rpc_sched_lock);
  994. }
  995. #endif
  996. void
  997. rpc_destroy_mempool(void)
  998. {
  999. if (rpc_buffer_mempool)
  1000. mempool_destroy(rpc_buffer_mempool);
  1001. if (rpc_task_mempool)
  1002. mempool_destroy(rpc_task_mempool);
  1003. if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
  1004. printk(KERN_INFO "rpc_task: not all structures were freed\n");
  1005. if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
  1006. printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
  1007. }
  1008. int
  1009. rpc_init_mempool(void)
  1010. {
  1011. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  1012. sizeof(struct rpc_task),
  1013. 0, SLAB_HWCACHE_ALIGN,
  1014. NULL, NULL);
  1015. if (!rpc_task_slabp)
  1016. goto err_nomem;
  1017. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  1018. RPC_BUFFER_MAXSIZE,
  1019. 0, SLAB_HWCACHE_ALIGN,
  1020. NULL, NULL);
  1021. if (!rpc_buffer_slabp)
  1022. goto err_nomem;
  1023. rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
  1024. mempool_alloc_slab,
  1025. mempool_free_slab,
  1026. rpc_task_slabp);
  1027. if (!rpc_task_mempool)
  1028. goto err_nomem;
  1029. rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
  1030. mempool_alloc_slab,
  1031. mempool_free_slab,
  1032. rpc_buffer_slabp);
  1033. if (!rpc_buffer_mempool)
  1034. goto err_nomem;
  1035. return 0;
  1036. err_nomem:
  1037. rpc_destroy_mempool();
  1038. return -ENOMEM;
  1039. }