sched.c 26 KB

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