sched.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. /**
  332. * __rpc_do_wake_up_task - wake up a single rpc_task
  333. * @task: task to be woken up
  334. *
  335. * Caller must hold queue->lock, and have cleared the task queued flag.
  336. */
  337. static void __rpc_do_wake_up_task(struct rpc_task *task)
  338. {
  339. dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
  340. task->tk_pid, jiffies);
  341. #ifdef RPC_DEBUG
  342. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  343. #endif
  344. /* Has the task been executed yet? If not, we cannot wake it up! */
  345. if (!RPC_IS_ACTIVATED(task)) {
  346. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  347. return;
  348. }
  349. __rpc_disable_timer(task);
  350. __rpc_remove_wait_queue(task);
  351. rpc_make_runnable(task);
  352. dprintk("RPC: __rpc_wake_up_task done\n");
  353. }
  354. /*
  355. * Wake up the specified task
  356. */
  357. static void __rpc_wake_up_task(struct rpc_task *task)
  358. {
  359. if (rpc_start_wakeup(task)) {
  360. if (RPC_IS_QUEUED(task))
  361. __rpc_do_wake_up_task(task);
  362. rpc_finish_wakeup(task);
  363. }
  364. }
  365. /*
  366. * Default timeout handler if none specified by user
  367. */
  368. static void
  369. __rpc_default_timer(struct rpc_task *task)
  370. {
  371. dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid);
  372. task->tk_status = -ETIMEDOUT;
  373. rpc_wake_up_task(task);
  374. }
  375. /*
  376. * Wake up the specified task
  377. */
  378. void rpc_wake_up_task(struct rpc_task *task)
  379. {
  380. rcu_read_lock_bh();
  381. if (rpc_start_wakeup(task)) {
  382. if (RPC_IS_QUEUED(task)) {
  383. struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
  384. /* Note: we're already in a bh-safe context */
  385. spin_lock(&queue->lock);
  386. __rpc_do_wake_up_task(task);
  387. spin_unlock(&queue->lock);
  388. }
  389. rpc_finish_wakeup(task);
  390. }
  391. rcu_read_unlock_bh();
  392. }
  393. /*
  394. * Wake up the next task on a priority queue.
  395. */
  396. static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
  397. {
  398. struct list_head *q;
  399. struct rpc_task *task;
  400. /*
  401. * Service a batch of tasks from a single cookie.
  402. */
  403. q = &queue->tasks[queue->priority];
  404. if (!list_empty(q)) {
  405. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  406. if (queue->cookie == task->tk_cookie) {
  407. if (--queue->nr)
  408. goto out;
  409. list_move_tail(&task->u.tk_wait.list, q);
  410. }
  411. /*
  412. * Check if we need to switch queues.
  413. */
  414. if (--queue->count)
  415. goto new_cookie;
  416. }
  417. /*
  418. * Service the next queue.
  419. */
  420. do {
  421. if (q == &queue->tasks[0])
  422. q = &queue->tasks[queue->maxpriority];
  423. else
  424. q = q - 1;
  425. if (!list_empty(q)) {
  426. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  427. goto new_queue;
  428. }
  429. } while (q != &queue->tasks[queue->priority]);
  430. rpc_reset_waitqueue_priority(queue);
  431. return NULL;
  432. new_queue:
  433. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  434. new_cookie:
  435. rpc_set_waitqueue_cookie(queue, task->tk_cookie);
  436. out:
  437. __rpc_wake_up_task(task);
  438. return task;
  439. }
  440. /*
  441. * Wake up the next task on the wait queue.
  442. */
  443. struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
  444. {
  445. struct rpc_task *task = NULL;
  446. dprintk("RPC: wake_up_next(%p \"%s\")\n",
  447. queue, rpc_qname(queue));
  448. rcu_read_lock_bh();
  449. spin_lock(&queue->lock);
  450. if (RPC_IS_PRIORITY(queue))
  451. task = __rpc_wake_up_next_priority(queue);
  452. else {
  453. task_for_first(task, &queue->tasks[0])
  454. __rpc_wake_up_task(task);
  455. }
  456. spin_unlock(&queue->lock);
  457. rcu_read_unlock_bh();
  458. return task;
  459. }
  460. /**
  461. * rpc_wake_up - wake up all rpc_tasks
  462. * @queue: rpc_wait_queue on which the tasks are sleeping
  463. *
  464. * Grabs queue->lock
  465. */
  466. void rpc_wake_up(struct rpc_wait_queue *queue)
  467. {
  468. struct rpc_task *task, *next;
  469. struct list_head *head;
  470. rcu_read_lock_bh();
  471. spin_lock(&queue->lock);
  472. head = &queue->tasks[queue->maxpriority];
  473. for (;;) {
  474. list_for_each_entry_safe(task, next, head, u.tk_wait.list)
  475. __rpc_wake_up_task(task);
  476. if (head == &queue->tasks[0])
  477. break;
  478. head--;
  479. }
  480. spin_unlock(&queue->lock);
  481. rcu_read_unlock_bh();
  482. }
  483. /**
  484. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  485. * @queue: rpc_wait_queue on which the tasks are sleeping
  486. * @status: status value to set
  487. *
  488. * Grabs queue->lock
  489. */
  490. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  491. {
  492. struct rpc_task *task, *next;
  493. struct list_head *head;
  494. rcu_read_lock_bh();
  495. spin_lock(&queue->lock);
  496. head = &queue->tasks[queue->maxpriority];
  497. for (;;) {
  498. list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
  499. task->tk_status = status;
  500. __rpc_wake_up_task(task);
  501. }
  502. if (head == &queue->tasks[0])
  503. break;
  504. head--;
  505. }
  506. spin_unlock(&queue->lock);
  507. rcu_read_unlock_bh();
  508. }
  509. static void __rpc_atrun(struct rpc_task *task)
  510. {
  511. rpc_wake_up_task(task);
  512. }
  513. /*
  514. * Run a task at a later time
  515. */
  516. void rpc_delay(struct rpc_task *task, unsigned long delay)
  517. {
  518. task->tk_timeout = delay;
  519. rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
  520. }
  521. /*
  522. * Helper to call task->tk_ops->rpc_call_prepare
  523. */
  524. static void rpc_prepare_task(struct rpc_task *task)
  525. {
  526. lock_kernel();
  527. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  528. unlock_kernel();
  529. }
  530. /*
  531. * Helper that calls task->tk_ops->rpc_call_done if it exists
  532. */
  533. void rpc_exit_task(struct rpc_task *task)
  534. {
  535. task->tk_action = NULL;
  536. if (task->tk_ops->rpc_call_done != NULL) {
  537. lock_kernel();
  538. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  539. unlock_kernel();
  540. if (task->tk_action != NULL) {
  541. WARN_ON(RPC_ASSASSINATED(task));
  542. /* Always release the RPC slot and buffer memory */
  543. xprt_release(task);
  544. }
  545. }
  546. }
  547. EXPORT_SYMBOL(rpc_exit_task);
  548. void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
  549. {
  550. if (ops->rpc_release != NULL) {
  551. lock_kernel();
  552. ops->rpc_release(calldata);
  553. unlock_kernel();
  554. }
  555. }
  556. /*
  557. * This is the RPC `scheduler' (or rather, the finite state machine).
  558. */
  559. static void __rpc_execute(struct rpc_task *task)
  560. {
  561. int status = 0;
  562. dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
  563. task->tk_pid, task->tk_flags);
  564. BUG_ON(RPC_IS_QUEUED(task));
  565. for (;;) {
  566. /*
  567. * Garbage collection of pending timers...
  568. */
  569. rpc_delete_timer(task);
  570. /*
  571. * Execute any pending callback.
  572. */
  573. if (RPC_DO_CALLBACK(task)) {
  574. /* Define a callback save pointer */
  575. void (*save_callback)(struct rpc_task *);
  576. /*
  577. * If a callback exists, save it, reset it,
  578. * call it.
  579. * The save is needed to stop from resetting
  580. * another callback set within the callback handler
  581. * - Dave
  582. */
  583. save_callback=task->tk_callback;
  584. task->tk_callback=NULL;
  585. save_callback(task);
  586. }
  587. /*
  588. * Perform the next FSM step.
  589. * tk_action may be NULL when the task has been killed
  590. * by someone else.
  591. */
  592. if (!RPC_IS_QUEUED(task)) {
  593. if (task->tk_action == NULL)
  594. break;
  595. task->tk_action(task);
  596. }
  597. /*
  598. * Lockless check for whether task is sleeping or not.
  599. */
  600. if (!RPC_IS_QUEUED(task))
  601. continue;
  602. rpc_clear_running(task);
  603. if (RPC_IS_ASYNC(task)) {
  604. /* Careful! we may have raced... */
  605. if (RPC_IS_QUEUED(task))
  606. return;
  607. if (rpc_test_and_set_running(task))
  608. return;
  609. continue;
  610. }
  611. /* sync task: sleep here */
  612. dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
  613. /* Note: Caller should be using rpc_clnt_sigmask() */
  614. status = out_of_line_wait_on_bit(&task->tk_runstate,
  615. RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
  616. TASK_INTERRUPTIBLE);
  617. if (status == -ERESTARTSYS) {
  618. /*
  619. * When a sync task receives a signal, it exits with
  620. * -ERESTARTSYS. In order to catch any callbacks that
  621. * clean up after sleeping on some queue, we don't
  622. * break the loop here, but go around once more.
  623. */
  624. dprintk("RPC: %5u got signal\n", task->tk_pid);
  625. task->tk_flags |= RPC_TASK_KILLED;
  626. rpc_exit(task, -ERESTARTSYS);
  627. rpc_wake_up_task(task);
  628. }
  629. rpc_set_running(task);
  630. dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
  631. }
  632. dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
  633. task->tk_status);
  634. /* Release all resources associated with the task */
  635. rpc_release_task(task);
  636. }
  637. /*
  638. * User-visible entry point to the scheduler.
  639. *
  640. * This may be called recursively if e.g. an async NFS task updates
  641. * the attributes and finds that dirty pages must be flushed.
  642. * NOTE: Upon exit of this function the task is guaranteed to be
  643. * released. In particular note that tk_release() will have
  644. * been called, so your task memory may have been freed.
  645. */
  646. void rpc_execute(struct rpc_task *task)
  647. {
  648. rpc_set_active(task);
  649. rpc_set_running(task);
  650. __rpc_execute(task);
  651. }
  652. static void rpc_async_schedule(struct work_struct *work)
  653. {
  654. __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
  655. }
  656. struct rpc_buffer {
  657. size_t len;
  658. char data[];
  659. };
  660. /**
  661. * rpc_malloc - allocate an RPC buffer
  662. * @task: RPC task that will use this buffer
  663. * @size: requested byte size
  664. *
  665. * To prevent rpciod from hanging, this allocator never sleeps,
  666. * returning NULL if the request cannot be serviced immediately.
  667. * The caller can arrange to sleep in a way that is safe for rpciod.
  668. *
  669. * Most requests are 'small' (under 2KiB) and can be serviced from a
  670. * mempool, ensuring that NFS reads and writes can always proceed,
  671. * and that there is good locality of reference for these buffers.
  672. *
  673. * In order to avoid memory starvation triggering more writebacks of
  674. * NFS requests, we avoid using GFP_KERNEL.
  675. */
  676. void *rpc_malloc(struct rpc_task *task, size_t size)
  677. {
  678. struct rpc_buffer *buf;
  679. gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
  680. size += sizeof(struct rpc_buffer);
  681. if (size <= RPC_BUFFER_MAXSIZE)
  682. buf = mempool_alloc(rpc_buffer_mempool, gfp);
  683. else
  684. buf = kmalloc(size, gfp);
  685. if (!buf)
  686. return NULL;
  687. buf->len = size;
  688. dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
  689. task->tk_pid, size, buf);
  690. return &buf->data;
  691. }
  692. /**
  693. * rpc_free - free buffer allocated via rpc_malloc
  694. * @buffer: buffer to free
  695. *
  696. */
  697. void rpc_free(void *buffer)
  698. {
  699. size_t size;
  700. struct rpc_buffer *buf;
  701. if (!buffer)
  702. return;
  703. buf = container_of(buffer, struct rpc_buffer, data);
  704. size = buf->len;
  705. dprintk("RPC: freeing buffer of size %zu at %p\n",
  706. size, buf);
  707. if (size <= RPC_BUFFER_MAXSIZE)
  708. mempool_free(buf, rpc_buffer_mempool);
  709. else
  710. kfree(buf);
  711. }
  712. /*
  713. * Creation and deletion of RPC task structures
  714. */
  715. void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  716. {
  717. memset(task, 0, sizeof(*task));
  718. init_timer(&task->tk_timer);
  719. task->tk_timer.data = (unsigned long) task;
  720. task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
  721. atomic_set(&task->tk_count, 1);
  722. task->tk_client = clnt;
  723. task->tk_flags = flags;
  724. task->tk_ops = tk_ops;
  725. if (tk_ops->rpc_call_prepare != NULL)
  726. task->tk_action = rpc_prepare_task;
  727. task->tk_calldata = calldata;
  728. INIT_LIST_HEAD(&task->tk_task);
  729. /* Initialize retry counters */
  730. task->tk_garb_retry = 2;
  731. task->tk_cred_retry = 2;
  732. task->tk_priority = RPC_PRIORITY_NORMAL;
  733. task->tk_cookie = (unsigned long)current;
  734. /* Initialize workqueue for async tasks */
  735. task->tk_workqueue = rpciod_workqueue;
  736. if (clnt) {
  737. kref_get(&clnt->cl_kref);
  738. if (clnt->cl_softrtry)
  739. task->tk_flags |= RPC_TASK_SOFT;
  740. if (!clnt->cl_intr)
  741. task->tk_flags |= RPC_TASK_NOINTR;
  742. }
  743. BUG_ON(task->tk_ops == NULL);
  744. /* starting timestamp */
  745. task->tk_start = jiffies;
  746. dprintk("RPC: new task initialized, procpid %u\n",
  747. current->pid);
  748. }
  749. static struct rpc_task *
  750. rpc_alloc_task(void)
  751. {
  752. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  753. }
  754. static void rpc_free_task(struct rcu_head *rcu)
  755. {
  756. struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
  757. dprintk("RPC: %5u freeing task\n", task->tk_pid);
  758. mempool_free(task, rpc_task_mempool);
  759. }
  760. /*
  761. * Create a new task for the specified client.
  762. */
  763. struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  764. {
  765. struct rpc_task *task;
  766. task = rpc_alloc_task();
  767. if (!task)
  768. goto out;
  769. rpc_init_task(task, clnt, flags, tk_ops, calldata);
  770. dprintk("RPC: allocated task %p\n", task);
  771. task->tk_flags |= RPC_TASK_DYNAMIC;
  772. out:
  773. return task;
  774. }
  775. void rpc_put_task(struct rpc_task *task)
  776. {
  777. const struct rpc_call_ops *tk_ops = task->tk_ops;
  778. void *calldata = task->tk_calldata;
  779. if (!atomic_dec_and_test(&task->tk_count))
  780. return;
  781. /* Release resources */
  782. if (task->tk_rqstp)
  783. xprt_release(task);
  784. if (task->tk_msg.rpc_cred)
  785. rpcauth_unbindcred(task);
  786. if (task->tk_client) {
  787. rpc_release_client(task->tk_client);
  788. task->tk_client = NULL;
  789. }
  790. if (task->tk_flags & RPC_TASK_DYNAMIC)
  791. call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
  792. rpc_release_calldata(tk_ops, calldata);
  793. }
  794. EXPORT_SYMBOL(rpc_put_task);
  795. static void rpc_release_task(struct rpc_task *task)
  796. {
  797. #ifdef RPC_DEBUG
  798. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  799. #endif
  800. dprintk("RPC: %5u release task\n", task->tk_pid);
  801. if (!list_empty(&task->tk_task)) {
  802. struct rpc_clnt *clnt = task->tk_client;
  803. /* Remove from client task list */
  804. spin_lock(&clnt->cl_lock);
  805. list_del(&task->tk_task);
  806. spin_unlock(&clnt->cl_lock);
  807. }
  808. BUG_ON (RPC_IS_QUEUED(task));
  809. /* Synchronously delete any running timer */
  810. rpc_delete_timer(task);
  811. #ifdef RPC_DEBUG
  812. task->tk_magic = 0;
  813. #endif
  814. /* Wake up anyone who is waiting for task completion */
  815. rpc_mark_complete_task(task);
  816. rpc_put_task(task);
  817. }
  818. /*
  819. * Kill all tasks for the given client.
  820. * XXX: kill their descendants as well?
  821. */
  822. void rpc_killall_tasks(struct rpc_clnt *clnt)
  823. {
  824. struct rpc_task *rovr;
  825. if (list_empty(&clnt->cl_tasks))
  826. return;
  827. dprintk("RPC: killing all tasks for client %p\n", clnt);
  828. /*
  829. * Spin lock all_tasks to prevent changes...
  830. */
  831. spin_lock(&clnt->cl_lock);
  832. list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
  833. if (! RPC_IS_ACTIVATED(rovr))
  834. continue;
  835. if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
  836. rovr->tk_flags |= RPC_TASK_KILLED;
  837. rpc_exit(rovr, -EIO);
  838. rpc_wake_up_task(rovr);
  839. }
  840. }
  841. spin_unlock(&clnt->cl_lock);
  842. }
  843. int rpciod_up(void)
  844. {
  845. return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
  846. }
  847. void rpciod_down(void)
  848. {
  849. module_put(THIS_MODULE);
  850. }
  851. /*
  852. * Start up the rpciod workqueue.
  853. */
  854. static int rpciod_start(void)
  855. {
  856. struct workqueue_struct *wq;
  857. /*
  858. * Create the rpciod thread and wait for it to start.
  859. */
  860. dprintk("RPC: creating workqueue rpciod\n");
  861. wq = create_workqueue("rpciod");
  862. rpciod_workqueue = wq;
  863. return rpciod_workqueue != NULL;
  864. }
  865. static void rpciod_stop(void)
  866. {
  867. struct workqueue_struct *wq = NULL;
  868. if (rpciod_workqueue == NULL)
  869. return;
  870. dprintk("RPC: destroying workqueue rpciod\n");
  871. wq = rpciod_workqueue;
  872. rpciod_workqueue = NULL;
  873. destroy_workqueue(wq);
  874. }
  875. void
  876. rpc_destroy_mempool(void)
  877. {
  878. rpciod_stop();
  879. if (rpc_buffer_mempool)
  880. mempool_destroy(rpc_buffer_mempool);
  881. if (rpc_task_mempool)
  882. mempool_destroy(rpc_task_mempool);
  883. if (rpc_task_slabp)
  884. kmem_cache_destroy(rpc_task_slabp);
  885. if (rpc_buffer_slabp)
  886. kmem_cache_destroy(rpc_buffer_slabp);
  887. }
  888. int
  889. rpc_init_mempool(void)
  890. {
  891. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  892. sizeof(struct rpc_task),
  893. 0, SLAB_HWCACHE_ALIGN,
  894. NULL);
  895. if (!rpc_task_slabp)
  896. goto err_nomem;
  897. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  898. RPC_BUFFER_MAXSIZE,
  899. 0, SLAB_HWCACHE_ALIGN,
  900. NULL);
  901. if (!rpc_buffer_slabp)
  902. goto err_nomem;
  903. rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
  904. rpc_task_slabp);
  905. if (!rpc_task_mempool)
  906. goto err_nomem;
  907. rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
  908. rpc_buffer_slabp);
  909. if (!rpc_buffer_mempool)
  910. goto err_nomem;
  911. if (!rpciod_start())
  912. goto err_nomem;
  913. return 0;
  914. err_nomem:
  915. rpc_destroy_mempool();
  916. return -ENOMEM;
  917. }