sched.c 27 KB

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