sched.c 26 KB

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