sched.c 26 KB

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