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