sched.c 27 KB

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