sched.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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, *next;
  454. struct list_head *head;
  455. spin_lock_bh(&queue->lock);
  456. head = &queue->tasks[queue->maxpriority];
  457. for (;;) {
  458. list_for_each_entry_safe(task, next, head, u.tk_wait.list)
  459. __rpc_wake_up_task(task);
  460. if (head == &queue->tasks[0])
  461. break;
  462. head--;
  463. }
  464. spin_unlock_bh(&queue->lock);
  465. }
  466. /**
  467. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  468. * @queue: rpc_wait_queue on which the tasks are sleeping
  469. * @status: status value to set
  470. *
  471. * Grabs queue->lock
  472. */
  473. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  474. {
  475. struct rpc_task *task, *next;
  476. struct list_head *head;
  477. spin_lock_bh(&queue->lock);
  478. head = &queue->tasks[queue->maxpriority];
  479. for (;;) {
  480. list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
  481. task->tk_status = status;
  482. __rpc_wake_up_task(task);
  483. }
  484. if (head == &queue->tasks[0])
  485. break;
  486. head--;
  487. }
  488. spin_unlock_bh(&queue->lock);
  489. }
  490. /*
  491. * Run a task at a later time
  492. */
  493. static void __rpc_atrun(struct rpc_task *);
  494. void
  495. rpc_delay(struct rpc_task *task, unsigned long delay)
  496. {
  497. task->tk_timeout = delay;
  498. rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
  499. }
  500. static void
  501. __rpc_atrun(struct rpc_task *task)
  502. {
  503. task->tk_status = 0;
  504. rpc_wake_up_task(task);
  505. }
  506. /*
  507. * Helper to call task->tk_ops->rpc_call_prepare
  508. */
  509. static void rpc_prepare_task(struct rpc_task *task)
  510. {
  511. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  512. }
  513. /*
  514. * Helper that calls task->tk_ops->rpc_call_done if it exists
  515. */
  516. void rpc_exit_task(struct rpc_task *task)
  517. {
  518. task->tk_action = NULL;
  519. if (task->tk_ops->rpc_call_done != NULL) {
  520. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  521. if (task->tk_action != NULL) {
  522. WARN_ON(RPC_ASSASSINATED(task));
  523. /* Always release the RPC slot and buffer memory */
  524. xprt_release(task);
  525. }
  526. }
  527. }
  528. EXPORT_SYMBOL(rpc_exit_task);
  529. /*
  530. * This is the RPC `scheduler' (or rather, the finite state machine).
  531. */
  532. static int __rpc_execute(struct rpc_task *task)
  533. {
  534. int status = 0;
  535. dprintk("RPC: %4d rpc_execute flgs %x\n",
  536. task->tk_pid, task->tk_flags);
  537. BUG_ON(RPC_IS_QUEUED(task));
  538. for (;;) {
  539. /*
  540. * Garbage collection of pending timers...
  541. */
  542. rpc_delete_timer(task);
  543. /*
  544. * Execute any pending callback.
  545. */
  546. if (RPC_DO_CALLBACK(task)) {
  547. /* Define a callback save pointer */
  548. void (*save_callback)(struct rpc_task *);
  549. /*
  550. * If a callback exists, save it, reset it,
  551. * call it.
  552. * The save is needed to stop from resetting
  553. * another callback set within the callback handler
  554. * - Dave
  555. */
  556. save_callback=task->tk_callback;
  557. task->tk_callback=NULL;
  558. lock_kernel();
  559. save_callback(task);
  560. unlock_kernel();
  561. }
  562. /*
  563. * Perform the next FSM step.
  564. * tk_action may be NULL when the task has been killed
  565. * by someone else.
  566. */
  567. if (!RPC_IS_QUEUED(task)) {
  568. if (task->tk_action == NULL)
  569. break;
  570. lock_kernel();
  571. task->tk_action(task);
  572. unlock_kernel();
  573. }
  574. /*
  575. * Lockless check for whether task is sleeping or not.
  576. */
  577. if (!RPC_IS_QUEUED(task))
  578. continue;
  579. rpc_clear_running(task);
  580. if (RPC_IS_ASYNC(task)) {
  581. /* Careful! we may have raced... */
  582. if (RPC_IS_QUEUED(task))
  583. return 0;
  584. if (rpc_test_and_set_running(task))
  585. return 0;
  586. continue;
  587. }
  588. /* sync task: sleep here */
  589. dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
  590. /* Note: Caller should be using rpc_clnt_sigmask() */
  591. status = out_of_line_wait_on_bit(&task->tk_runstate,
  592. RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
  593. TASK_INTERRUPTIBLE);
  594. if (status == -ERESTARTSYS) {
  595. /*
  596. * When a sync task receives a signal, it exits with
  597. * -ERESTARTSYS. In order to catch any callbacks that
  598. * clean up after sleeping on some queue, we don't
  599. * break the loop here, but go around once more.
  600. */
  601. dprintk("RPC: %4d got signal\n", task->tk_pid);
  602. task->tk_flags |= RPC_TASK_KILLED;
  603. rpc_exit(task, -ERESTARTSYS);
  604. rpc_wake_up_task(task);
  605. }
  606. rpc_set_running(task);
  607. dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
  608. }
  609. dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
  610. /* Wake up anyone who is waiting for task completion */
  611. rpc_mark_complete_task(task);
  612. /* Release all resources associated with the task */
  613. rpc_release_task(task);
  614. return status;
  615. }
  616. /*
  617. * User-visible entry point to the scheduler.
  618. *
  619. * This may be called recursively if e.g. an async NFS task updates
  620. * the attributes and finds that dirty pages must be flushed.
  621. * NOTE: Upon exit of this function the task is guaranteed to be
  622. * released. In particular note that tk_release() will have
  623. * been called, so your task memory may have been freed.
  624. */
  625. int
  626. rpc_execute(struct rpc_task *task)
  627. {
  628. rpc_set_active(task);
  629. rpc_set_running(task);
  630. return __rpc_execute(task);
  631. }
  632. static void rpc_async_schedule(void *arg)
  633. {
  634. __rpc_execute((struct rpc_task *)arg);
  635. }
  636. /**
  637. * rpc_malloc - allocate an RPC buffer
  638. * @task: RPC task that will use this buffer
  639. * @size: requested byte size
  640. *
  641. * We try to ensure that some NFS reads and writes can always proceed
  642. * by using a mempool when allocating 'small' buffers.
  643. * In order to avoid memory starvation triggering more writebacks of
  644. * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
  645. */
  646. void * rpc_malloc(struct rpc_task *task, size_t size)
  647. {
  648. struct rpc_rqst *req = task->tk_rqstp;
  649. gfp_t gfp;
  650. if (task->tk_flags & RPC_TASK_SWAPPER)
  651. gfp = GFP_ATOMIC;
  652. else
  653. gfp = GFP_NOFS;
  654. if (size > RPC_BUFFER_MAXSIZE) {
  655. req->rq_buffer = kmalloc(size, gfp);
  656. if (req->rq_buffer)
  657. req->rq_bufsize = size;
  658. } else {
  659. req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
  660. if (req->rq_buffer)
  661. req->rq_bufsize = RPC_BUFFER_MAXSIZE;
  662. }
  663. return req->rq_buffer;
  664. }
  665. /**
  666. * rpc_free - free buffer allocated via rpc_malloc
  667. * @task: RPC task with a buffer to be freed
  668. *
  669. */
  670. void rpc_free(struct rpc_task *task)
  671. {
  672. struct rpc_rqst *req = task->tk_rqstp;
  673. if (req->rq_buffer) {
  674. if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
  675. mempool_free(req->rq_buffer, rpc_buffer_mempool);
  676. else
  677. kfree(req->rq_buffer);
  678. req->rq_buffer = NULL;
  679. req->rq_bufsize = 0;
  680. }
  681. }
  682. /*
  683. * Creation and deletion of RPC task structures
  684. */
  685. void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  686. {
  687. memset(task, 0, sizeof(*task));
  688. init_timer(&task->tk_timer);
  689. task->tk_timer.data = (unsigned long) task;
  690. task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
  691. atomic_set(&task->tk_count, 1);
  692. task->tk_client = clnt;
  693. task->tk_flags = flags;
  694. task->tk_ops = tk_ops;
  695. if (tk_ops->rpc_call_prepare != NULL)
  696. task->tk_action = rpc_prepare_task;
  697. task->tk_calldata = calldata;
  698. /* Initialize retry counters */
  699. task->tk_garb_retry = 2;
  700. task->tk_cred_retry = 2;
  701. task->tk_priority = RPC_PRIORITY_NORMAL;
  702. task->tk_cookie = (unsigned long)current;
  703. /* Initialize workqueue for async tasks */
  704. task->tk_workqueue = rpciod_workqueue;
  705. if (clnt) {
  706. atomic_inc(&clnt->cl_users);
  707. if (clnt->cl_softrtry)
  708. task->tk_flags |= RPC_TASK_SOFT;
  709. if (!clnt->cl_intr)
  710. task->tk_flags |= RPC_TASK_NOINTR;
  711. }
  712. #ifdef RPC_DEBUG
  713. task->tk_magic = RPC_TASK_MAGIC_ID;
  714. task->tk_pid = rpc_task_id++;
  715. #endif
  716. /* Add to global list of all tasks */
  717. spin_lock(&rpc_sched_lock);
  718. list_add_tail(&task->tk_task, &all_tasks);
  719. spin_unlock(&rpc_sched_lock);
  720. BUG_ON(task->tk_ops == NULL);
  721. dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
  722. current->pid);
  723. }
  724. static struct rpc_task *
  725. rpc_alloc_task(void)
  726. {
  727. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  728. }
  729. static void rpc_free_task(struct rpc_task *task)
  730. {
  731. dprintk("RPC: %4d freeing task\n", task->tk_pid);
  732. mempool_free(task, rpc_task_mempool);
  733. }
  734. /*
  735. * Create a new task for the specified client. We have to
  736. * clean up after an allocation failure, as the client may
  737. * have specified "oneshot".
  738. */
  739. struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  740. {
  741. struct rpc_task *task;
  742. task = rpc_alloc_task();
  743. if (!task)
  744. goto cleanup;
  745. rpc_init_task(task, clnt, flags, tk_ops, calldata);
  746. dprintk("RPC: %4d allocated task\n", task->tk_pid);
  747. task->tk_flags |= RPC_TASK_DYNAMIC;
  748. out:
  749. return task;
  750. cleanup:
  751. /* Check whether to release the client */
  752. if (clnt) {
  753. printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
  754. atomic_read(&clnt->cl_users), clnt->cl_oneshot);
  755. atomic_inc(&clnt->cl_users); /* pretend we were used ... */
  756. rpc_release_client(clnt);
  757. }
  758. goto out;
  759. }
  760. void rpc_release_task(struct rpc_task *task)
  761. {
  762. const struct rpc_call_ops *tk_ops = task->tk_ops;
  763. void *calldata = task->tk_calldata;
  764. #ifdef RPC_DEBUG
  765. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  766. #endif
  767. if (!atomic_dec_and_test(&task->tk_count))
  768. return;
  769. dprintk("RPC: %4d release task\n", task->tk_pid);
  770. /* Remove from global task list */
  771. spin_lock(&rpc_sched_lock);
  772. list_del(&task->tk_task);
  773. spin_unlock(&rpc_sched_lock);
  774. BUG_ON (RPC_IS_QUEUED(task));
  775. /* Synchronously delete any running timer */
  776. rpc_delete_timer(task);
  777. /* Release resources */
  778. if (task->tk_rqstp)
  779. xprt_release(task);
  780. if (task->tk_msg.rpc_cred)
  781. rpcauth_unbindcred(task);
  782. if (task->tk_client) {
  783. rpc_release_client(task->tk_client);
  784. task->tk_client = NULL;
  785. }
  786. #ifdef RPC_DEBUG
  787. task->tk_magic = 0;
  788. #endif
  789. if (task->tk_flags & RPC_TASK_DYNAMIC)
  790. rpc_free_task(task);
  791. if (tk_ops->rpc_release)
  792. tk_ops->rpc_release(calldata);
  793. }
  794. /**
  795. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  796. * @clnt: pointer to RPC client
  797. * @flags: RPC flags
  798. * @ops: RPC call ops
  799. * @data: user call data
  800. */
  801. struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
  802. const struct rpc_call_ops *ops,
  803. void *data)
  804. {
  805. struct rpc_task *task;
  806. task = rpc_new_task(clnt, flags, ops, data);
  807. if (task == NULL)
  808. return ERR_PTR(-ENOMEM);
  809. atomic_inc(&task->tk_count);
  810. rpc_execute(task);
  811. return task;
  812. }
  813. EXPORT_SYMBOL(rpc_run_task);
  814. /**
  815. * rpc_find_parent - find the parent of a child task.
  816. * @child: child task
  817. * @parent: parent task
  818. *
  819. * Checks that the parent task is still sleeping on the
  820. * queue 'childq'. If so returns a pointer to the parent.
  821. * Upon failure returns NULL.
  822. *
  823. * Caller must hold childq.lock
  824. */
  825. static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
  826. {
  827. struct rpc_task *task;
  828. struct list_head *le;
  829. task_for_each(task, le, &childq.tasks[0])
  830. if (task == parent)
  831. return parent;
  832. return NULL;
  833. }
  834. static void rpc_child_exit(struct rpc_task *child, void *calldata)
  835. {
  836. struct rpc_task *parent;
  837. spin_lock_bh(&childq.lock);
  838. if ((parent = rpc_find_parent(child, calldata)) != NULL) {
  839. parent->tk_status = child->tk_status;
  840. __rpc_wake_up_task(parent);
  841. }
  842. spin_unlock_bh(&childq.lock);
  843. }
  844. static const struct rpc_call_ops rpc_child_ops = {
  845. .rpc_call_done = rpc_child_exit,
  846. };
  847. /*
  848. * Note: rpc_new_task releases the client after a failure.
  849. */
  850. struct rpc_task *
  851. rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
  852. {
  853. struct rpc_task *task;
  854. task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
  855. if (!task)
  856. goto fail;
  857. return task;
  858. fail:
  859. parent->tk_status = -ENOMEM;
  860. return NULL;
  861. }
  862. void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
  863. {
  864. spin_lock_bh(&childq.lock);
  865. /* N.B. Is it possible for the child to have already finished? */
  866. __rpc_sleep_on(&childq, task, func, NULL);
  867. rpc_schedule_run(child);
  868. spin_unlock_bh(&childq.lock);
  869. }
  870. /*
  871. * Kill all tasks for the given client.
  872. * XXX: kill their descendants as well?
  873. */
  874. void rpc_killall_tasks(struct rpc_clnt *clnt)
  875. {
  876. struct rpc_task *rovr;
  877. struct list_head *le;
  878. dprintk("RPC: killing all tasks for client %p\n", clnt);
  879. /*
  880. * Spin lock all_tasks to prevent changes...
  881. */
  882. spin_lock(&rpc_sched_lock);
  883. alltask_for_each(rovr, le, &all_tasks) {
  884. if (! RPC_IS_ACTIVATED(rovr))
  885. continue;
  886. if (!clnt || rovr->tk_client == clnt) {
  887. rovr->tk_flags |= RPC_TASK_KILLED;
  888. rpc_exit(rovr, -EIO);
  889. rpc_wake_up_task(rovr);
  890. }
  891. }
  892. spin_unlock(&rpc_sched_lock);
  893. }
  894. static DECLARE_MUTEX_LOCKED(rpciod_running);
  895. static void rpciod_killall(void)
  896. {
  897. unsigned long flags;
  898. while (!list_empty(&all_tasks)) {
  899. clear_thread_flag(TIF_SIGPENDING);
  900. rpc_killall_tasks(NULL);
  901. flush_workqueue(rpciod_workqueue);
  902. if (!list_empty(&all_tasks)) {
  903. dprintk("rpciod_killall: waiting for tasks to exit\n");
  904. yield();
  905. }
  906. }
  907. spin_lock_irqsave(&current->sighand->siglock, flags);
  908. recalc_sigpending();
  909. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  910. }
  911. /*
  912. * Start up the rpciod process if it's not already running.
  913. */
  914. int
  915. rpciod_up(void)
  916. {
  917. struct workqueue_struct *wq;
  918. int error = 0;
  919. down(&rpciod_sema);
  920. dprintk("rpciod_up: users %d\n", rpciod_users);
  921. rpciod_users++;
  922. if (rpciod_workqueue)
  923. goto out;
  924. /*
  925. * If there's no pid, we should be the first user.
  926. */
  927. if (rpciod_users > 1)
  928. printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
  929. /*
  930. * Create the rpciod thread and wait for it to start.
  931. */
  932. error = -ENOMEM;
  933. wq = create_workqueue("rpciod");
  934. if (wq == NULL) {
  935. printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
  936. rpciod_users--;
  937. goto out;
  938. }
  939. rpciod_workqueue = wq;
  940. error = 0;
  941. out:
  942. up(&rpciod_sema);
  943. return error;
  944. }
  945. void
  946. rpciod_down(void)
  947. {
  948. down(&rpciod_sema);
  949. dprintk("rpciod_down sema %d\n", rpciod_users);
  950. if (rpciod_users) {
  951. if (--rpciod_users)
  952. goto out;
  953. } else
  954. printk(KERN_WARNING "rpciod_down: no users??\n");
  955. if (!rpciod_workqueue) {
  956. dprintk("rpciod_down: Nothing to do!\n");
  957. goto out;
  958. }
  959. rpciod_killall();
  960. destroy_workqueue(rpciod_workqueue);
  961. rpciod_workqueue = NULL;
  962. out:
  963. up(&rpciod_sema);
  964. }
  965. #ifdef RPC_DEBUG
  966. void rpc_show_tasks(void)
  967. {
  968. struct list_head *le;
  969. struct rpc_task *t;
  970. spin_lock(&rpc_sched_lock);
  971. if (list_empty(&all_tasks)) {
  972. spin_unlock(&rpc_sched_lock);
  973. return;
  974. }
  975. printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
  976. "-rpcwait -action- ---ops--\n");
  977. alltask_for_each(t, le, &all_tasks) {
  978. const char *rpc_waitq = "none";
  979. if (RPC_IS_QUEUED(t))
  980. rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
  981. printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
  982. t->tk_pid,
  983. (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
  984. t->tk_flags, t->tk_status,
  985. t->tk_client,
  986. (t->tk_client ? t->tk_client->cl_prog : 0),
  987. t->tk_rqstp, t->tk_timeout,
  988. rpc_waitq,
  989. t->tk_action, t->tk_ops);
  990. }
  991. spin_unlock(&rpc_sched_lock);
  992. }
  993. #endif
  994. void
  995. rpc_destroy_mempool(void)
  996. {
  997. if (rpc_buffer_mempool)
  998. mempool_destroy(rpc_buffer_mempool);
  999. if (rpc_task_mempool)
  1000. mempool_destroy(rpc_task_mempool);
  1001. if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
  1002. printk(KERN_INFO "rpc_task: not all structures were freed\n");
  1003. if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
  1004. printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
  1005. }
  1006. int
  1007. rpc_init_mempool(void)
  1008. {
  1009. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  1010. sizeof(struct rpc_task),
  1011. 0, SLAB_HWCACHE_ALIGN,
  1012. NULL, NULL);
  1013. if (!rpc_task_slabp)
  1014. goto err_nomem;
  1015. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  1016. RPC_BUFFER_MAXSIZE,
  1017. 0, SLAB_HWCACHE_ALIGN,
  1018. NULL, NULL);
  1019. if (!rpc_buffer_slabp)
  1020. goto err_nomem;
  1021. rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
  1022. mempool_alloc_slab,
  1023. mempool_free_slab,
  1024. rpc_task_slabp);
  1025. if (!rpc_task_mempool)
  1026. goto err_nomem;
  1027. rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
  1028. mempool_alloc_slab,
  1029. mempool_free_slab,
  1030. rpc_buffer_slabp);
  1031. if (!rpc_buffer_mempool)
  1032. goto err_nomem;
  1033. return 0;
  1034. err_nomem:
  1035. rpc_destroy_mempool();
  1036. return -ENOMEM;
  1037. }