sched.c 28 KB

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