sched.c 26 KB

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