sched.c 27 KB

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