sched.c 25 KB

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