sched.c 28 KB

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