sched.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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;
  33. static kmem_cache_t *rpc_buffer_slabp;
  34. static mempool_t *rpc_task_mempool;
  35. static mempool_t *rpc_buffer_mempool;
  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 that calls task->tk_exit if it exists and then returns
  490. * true if we should exit __rpc_execute.
  491. */
  492. static inline int __rpc_do_exit(struct rpc_task *task)
  493. {
  494. if (task->tk_exit != NULL) {
  495. lock_kernel();
  496. task->tk_exit(task);
  497. unlock_kernel();
  498. /* If tk_action is non-null, we should restart the call */
  499. if (task->tk_action != NULL) {
  500. if (!RPC_ASSASSINATED(task)) {
  501. /* Release RPC slot and buffer memory */
  502. xprt_release(task);
  503. rpc_free(task);
  504. return 0;
  505. }
  506. printk(KERN_ERR "RPC: dead task tried to walk away.\n");
  507. }
  508. }
  509. return 1;
  510. }
  511. static int rpc_wait_bit_interruptible(void *word)
  512. {
  513. if (signal_pending(current))
  514. return -ERESTARTSYS;
  515. schedule();
  516. return 0;
  517. }
  518. /*
  519. * This is the RPC `scheduler' (or rather, the finite state machine).
  520. */
  521. static int __rpc_execute(struct rpc_task *task)
  522. {
  523. int status = 0;
  524. dprintk("RPC: %4d rpc_execute flgs %x\n",
  525. task->tk_pid, task->tk_flags);
  526. BUG_ON(RPC_IS_QUEUED(task));
  527. for (;;) {
  528. /*
  529. * Garbage collection of pending timers...
  530. */
  531. rpc_delete_timer(task);
  532. /*
  533. * Execute any pending callback.
  534. */
  535. if (RPC_DO_CALLBACK(task)) {
  536. /* Define a callback save pointer */
  537. void (*save_callback)(struct rpc_task *);
  538. /*
  539. * If a callback exists, save it, reset it,
  540. * call it.
  541. * The save is needed to stop from resetting
  542. * another callback set within the callback handler
  543. * - Dave
  544. */
  545. save_callback=task->tk_callback;
  546. task->tk_callback=NULL;
  547. lock_kernel();
  548. save_callback(task);
  549. unlock_kernel();
  550. }
  551. /*
  552. * Perform the next FSM step.
  553. * tk_action may be NULL when the task has been killed
  554. * by someone else.
  555. */
  556. if (!RPC_IS_QUEUED(task)) {
  557. if (task->tk_action != NULL) {
  558. lock_kernel();
  559. task->tk_action(task);
  560. unlock_kernel();
  561. } else if (__rpc_do_exit(task))
  562. break;
  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. int 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, rpc_action callback, int flags)
  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_exit = callback;
  677. /* Initialize retry counters */
  678. task->tk_garb_retry = 2;
  679. task->tk_cred_retry = 2;
  680. task->tk_priority = RPC_PRIORITY_NORMAL;
  681. task->tk_cookie = (unsigned long)current;
  682. /* Initialize workqueue for async tasks */
  683. task->tk_workqueue = rpciod_workqueue;
  684. if (clnt) {
  685. atomic_inc(&clnt->cl_users);
  686. if (clnt->cl_softrtry)
  687. task->tk_flags |= RPC_TASK_SOFT;
  688. if (!clnt->cl_intr)
  689. task->tk_flags |= RPC_TASK_NOINTR;
  690. }
  691. #ifdef RPC_DEBUG
  692. task->tk_magic = RPC_TASK_MAGIC_ID;
  693. task->tk_pid = rpc_task_id++;
  694. #endif
  695. /* Add to global list of all tasks */
  696. spin_lock(&rpc_sched_lock);
  697. list_add_tail(&task->tk_task, &all_tasks);
  698. spin_unlock(&rpc_sched_lock);
  699. dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
  700. current->pid);
  701. }
  702. static struct rpc_task *
  703. rpc_alloc_task(void)
  704. {
  705. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  706. }
  707. static void
  708. rpc_default_free_task(struct rpc_task *task)
  709. {
  710. dprintk("RPC: %4d freeing task\n", task->tk_pid);
  711. mempool_free(task, rpc_task_mempool);
  712. }
  713. /*
  714. * Create a new task for the specified client. We have to
  715. * clean up after an allocation failure, as the client may
  716. * have specified "oneshot".
  717. */
  718. struct rpc_task *
  719. rpc_new_task(struct rpc_clnt *clnt, rpc_action callback, int flags)
  720. {
  721. struct rpc_task *task;
  722. task = rpc_alloc_task();
  723. if (!task)
  724. goto cleanup;
  725. rpc_init_task(task, clnt, callback, flags);
  726. /* Replace tk_release */
  727. task->tk_release = rpc_default_free_task;
  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. dprintk("RPC: %4d release task\n", task->tk_pid);
  745. #ifdef RPC_DEBUG
  746. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  747. #endif
  748. /* Remove from global task list */
  749. spin_lock(&rpc_sched_lock);
  750. list_del(&task->tk_task);
  751. spin_unlock(&rpc_sched_lock);
  752. BUG_ON (RPC_IS_QUEUED(task));
  753. task->tk_active = 0;
  754. /* Synchronously delete any running timer */
  755. rpc_delete_timer(task);
  756. /* Release resources */
  757. if (task->tk_rqstp)
  758. xprt_release(task);
  759. if (task->tk_msg.rpc_cred)
  760. rpcauth_unbindcred(task);
  761. rpc_free(task);
  762. if (task->tk_client) {
  763. rpc_release_client(task->tk_client);
  764. task->tk_client = NULL;
  765. }
  766. #ifdef RPC_DEBUG
  767. task->tk_magic = 0;
  768. #endif
  769. if (task->tk_release)
  770. task->tk_release(task);
  771. }
  772. /**
  773. * rpc_find_parent - find the parent of a child task.
  774. * @child: child task
  775. *
  776. * Checks that the parent task is still sleeping on the
  777. * queue 'childq'. If so returns a pointer to the parent.
  778. * Upon failure returns NULL.
  779. *
  780. * Caller must hold childq.lock
  781. */
  782. static inline struct rpc_task *rpc_find_parent(struct rpc_task *child)
  783. {
  784. struct rpc_task *task, *parent;
  785. struct list_head *le;
  786. parent = (struct rpc_task *) child->tk_calldata;
  787. task_for_each(task, le, &childq.tasks[0])
  788. if (task == parent)
  789. return parent;
  790. return NULL;
  791. }
  792. static void rpc_child_exit(struct rpc_task *child)
  793. {
  794. struct rpc_task *parent;
  795. spin_lock_bh(&childq.lock);
  796. if ((parent = rpc_find_parent(child)) != NULL) {
  797. parent->tk_status = child->tk_status;
  798. __rpc_wake_up_task(parent);
  799. }
  800. spin_unlock_bh(&childq.lock);
  801. }
  802. /*
  803. * Note: rpc_new_task releases the client after a failure.
  804. */
  805. struct rpc_task *
  806. rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
  807. {
  808. struct rpc_task *task;
  809. task = rpc_new_task(clnt, NULL, RPC_TASK_ASYNC | RPC_TASK_CHILD);
  810. if (!task)
  811. goto fail;
  812. task->tk_exit = rpc_child_exit;
  813. task->tk_calldata = parent;
  814. return task;
  815. fail:
  816. parent->tk_status = -ENOMEM;
  817. return NULL;
  818. }
  819. void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
  820. {
  821. spin_lock_bh(&childq.lock);
  822. /* N.B. Is it possible for the child to have already finished? */
  823. __rpc_sleep_on(&childq, task, func, NULL);
  824. rpc_schedule_run(child);
  825. spin_unlock_bh(&childq.lock);
  826. }
  827. /*
  828. * Kill all tasks for the given client.
  829. * XXX: kill their descendants as well?
  830. */
  831. void rpc_killall_tasks(struct rpc_clnt *clnt)
  832. {
  833. struct rpc_task *rovr;
  834. struct list_head *le;
  835. dprintk("RPC: killing all tasks for client %p\n", clnt);
  836. /*
  837. * Spin lock all_tasks to prevent changes...
  838. */
  839. spin_lock(&rpc_sched_lock);
  840. alltask_for_each(rovr, le, &all_tasks) {
  841. if (! RPC_IS_ACTIVATED(rovr))
  842. continue;
  843. if (!clnt || rovr->tk_client == clnt) {
  844. rovr->tk_flags |= RPC_TASK_KILLED;
  845. rpc_exit(rovr, -EIO);
  846. rpc_wake_up_task(rovr);
  847. }
  848. }
  849. spin_unlock(&rpc_sched_lock);
  850. }
  851. static DECLARE_MUTEX_LOCKED(rpciod_running);
  852. static void rpciod_killall(void)
  853. {
  854. unsigned long flags;
  855. while (!list_empty(&all_tasks)) {
  856. clear_thread_flag(TIF_SIGPENDING);
  857. rpc_killall_tasks(NULL);
  858. flush_workqueue(rpciod_workqueue);
  859. if (!list_empty(&all_tasks)) {
  860. dprintk("rpciod_killall: waiting for tasks to exit\n");
  861. yield();
  862. }
  863. }
  864. spin_lock_irqsave(&current->sighand->siglock, flags);
  865. recalc_sigpending();
  866. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  867. }
  868. /*
  869. * Start up the rpciod process if it's not already running.
  870. */
  871. int
  872. rpciod_up(void)
  873. {
  874. struct workqueue_struct *wq;
  875. int error = 0;
  876. down(&rpciod_sema);
  877. dprintk("rpciod_up: users %d\n", rpciod_users);
  878. rpciod_users++;
  879. if (rpciod_workqueue)
  880. goto out;
  881. /*
  882. * If there's no pid, we should be the first user.
  883. */
  884. if (rpciod_users > 1)
  885. printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
  886. /*
  887. * Create the rpciod thread and wait for it to start.
  888. */
  889. error = -ENOMEM;
  890. wq = create_workqueue("rpciod");
  891. if (wq == NULL) {
  892. printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
  893. rpciod_users--;
  894. goto out;
  895. }
  896. rpciod_workqueue = wq;
  897. error = 0;
  898. out:
  899. up(&rpciod_sema);
  900. return error;
  901. }
  902. void
  903. rpciod_down(void)
  904. {
  905. down(&rpciod_sema);
  906. dprintk("rpciod_down sema %d\n", rpciod_users);
  907. if (rpciod_users) {
  908. if (--rpciod_users)
  909. goto out;
  910. } else
  911. printk(KERN_WARNING "rpciod_down: no users??\n");
  912. if (!rpciod_workqueue) {
  913. dprintk("rpciod_down: Nothing to do!\n");
  914. goto out;
  915. }
  916. rpciod_killall();
  917. destroy_workqueue(rpciod_workqueue);
  918. rpciod_workqueue = NULL;
  919. out:
  920. up(&rpciod_sema);
  921. }
  922. #ifdef RPC_DEBUG
  923. void rpc_show_tasks(void)
  924. {
  925. struct list_head *le;
  926. struct rpc_task *t;
  927. spin_lock(&rpc_sched_lock);
  928. if (list_empty(&all_tasks)) {
  929. spin_unlock(&rpc_sched_lock);
  930. return;
  931. }
  932. printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
  933. "-rpcwait -action- --exit--\n");
  934. alltask_for_each(t, le, &all_tasks) {
  935. const char *rpc_waitq = "none";
  936. if (RPC_IS_QUEUED(t))
  937. rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
  938. printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
  939. t->tk_pid,
  940. (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
  941. t->tk_flags, t->tk_status,
  942. t->tk_client,
  943. (t->tk_client ? t->tk_client->cl_prog : 0),
  944. t->tk_rqstp, t->tk_timeout,
  945. rpc_waitq,
  946. t->tk_action, t->tk_exit);
  947. }
  948. spin_unlock(&rpc_sched_lock);
  949. }
  950. #endif
  951. void
  952. rpc_destroy_mempool(void)
  953. {
  954. if (rpc_buffer_mempool)
  955. mempool_destroy(rpc_buffer_mempool);
  956. if (rpc_task_mempool)
  957. mempool_destroy(rpc_task_mempool);
  958. if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
  959. printk(KERN_INFO "rpc_task: not all structures were freed\n");
  960. if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
  961. printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
  962. }
  963. int
  964. rpc_init_mempool(void)
  965. {
  966. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  967. sizeof(struct rpc_task),
  968. 0, SLAB_HWCACHE_ALIGN,
  969. NULL, NULL);
  970. if (!rpc_task_slabp)
  971. goto err_nomem;
  972. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  973. RPC_BUFFER_MAXSIZE,
  974. 0, SLAB_HWCACHE_ALIGN,
  975. NULL, NULL);
  976. if (!rpc_buffer_slabp)
  977. goto err_nomem;
  978. rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
  979. mempool_alloc_slab,
  980. mempool_free_slab,
  981. rpc_task_slabp);
  982. if (!rpc_task_mempool)
  983. goto err_nomem;
  984. rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
  985. mempool_alloc_slab,
  986. mempool_free_slab,
  987. rpc_buffer_slabp);
  988. if (!rpc_buffer_mempool)
  989. goto err_nomem;
  990. return 0;
  991. err_nomem:
  992. rpc_destroy_mempool();
  993. return -ENOMEM;
  994. }