sched.c 26 KB

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