sched.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/sched.c
  4. *
  5. * Scheduling for synchronous and asynchronous RPC requests.
  6. *
  7. * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
  8. *
  9. * TCP NFS related read + write fixes
  10. * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/mempool.h>
  17. #include <linux/smp.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/mutex.h>
  20. #include <linux/freezer.h>
  21. #include <linux/sched/mm.h>
  22. #include <linux/sunrpc/clnt.h>
  23. #include <linux/sunrpc/metrics.h>
  24. #include "sunrpc.h"
  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(struct work_struct *);
  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 __read_mostly;
  48. struct workqueue_struct *xprtiod_workqueue __read_mostly;
  49. EXPORT_SYMBOL_GPL(xprtiod_workqueue);
  50. gfp_t rpc_task_gfp_mask(void)
  51. {
  52. if (current->flags & PF_WQ_WORKER)
  53. return GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
  54. return GFP_KERNEL;
  55. }
  56. EXPORT_SYMBOL_GPL(rpc_task_gfp_mask);
  57. bool rpc_task_set_rpc_status(struct rpc_task *task, int rpc_status)
  58. {
  59. if (cmpxchg(&task->tk_rpc_status, 0, rpc_status) == 0)
  60. return true;
  61. return false;
  62. }
  63. unsigned long
  64. rpc_task_timeout(const struct rpc_task *task)
  65. {
  66. unsigned long timeout = READ_ONCE(task->tk_timeout);
  67. if (timeout != 0) {
  68. unsigned long now = jiffies;
  69. if (time_before(now, timeout))
  70. return timeout - now;
  71. }
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(rpc_task_timeout);
  75. /*
  76. * Disable the timer for a given RPC task. Should be called with
  77. * queue->lock and bh_disabled in order to avoid races within
  78. * rpc_run_timer().
  79. */
  80. static void
  81. __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
  82. {
  83. if (list_empty(&task->u.tk_wait.timer_list))
  84. return;
  85. task->tk_timeout = 0;
  86. list_del(&task->u.tk_wait.timer_list);
  87. if (list_empty(&queue->timer_list.list))
  88. cancel_delayed_work(&queue->timer_list.dwork);
  89. }
  90. static void
  91. rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
  92. {
  93. unsigned long now = jiffies;
  94. queue->timer_list.expires = expires;
  95. if (time_before_eq(expires, now))
  96. expires = 0;
  97. else
  98. expires -= now;
  99. mod_delayed_work(rpciod_workqueue, &queue->timer_list.dwork, expires);
  100. }
  101. /*
  102. * Set up a timer for the current task.
  103. */
  104. static void
  105. __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task,
  106. unsigned long timeout)
  107. {
  108. task->tk_timeout = timeout;
  109. if (list_empty(&queue->timer_list.list) || time_before(timeout, queue->timer_list.expires))
  110. rpc_set_queue_timer(queue, timeout);
  111. list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list);
  112. }
  113. static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
  114. {
  115. if (queue->priority != priority) {
  116. queue->priority = priority;
  117. queue->nr = 1U << priority;
  118. }
  119. }
  120. static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
  121. {
  122. rpc_set_waitqueue_priority(queue, queue->maxpriority);
  123. }
  124. /*
  125. * Add a request to a queue list
  126. */
  127. static void
  128. __rpc_list_enqueue_task(struct list_head *q, struct rpc_task *task)
  129. {
  130. struct rpc_task *t;
  131. list_for_each_entry(t, q, u.tk_wait.list) {
  132. if (t->tk_owner == task->tk_owner) {
  133. list_add_tail(&task->u.tk_wait.links,
  134. &t->u.tk_wait.links);
  135. /* Cache the queue head in task->u.tk_wait.list */
  136. task->u.tk_wait.list.next = q;
  137. task->u.tk_wait.list.prev = NULL;
  138. return;
  139. }
  140. }
  141. INIT_LIST_HEAD(&task->u.tk_wait.links);
  142. list_add_tail(&task->u.tk_wait.list, q);
  143. }
  144. /*
  145. * Remove request from a queue list
  146. */
  147. static void
  148. __rpc_list_dequeue_task(struct rpc_task *task)
  149. {
  150. struct list_head *q;
  151. struct rpc_task *t;
  152. if (task->u.tk_wait.list.prev == NULL) {
  153. list_del(&task->u.tk_wait.links);
  154. return;
  155. }
  156. if (!list_empty(&task->u.tk_wait.links)) {
  157. t = list_first_entry(&task->u.tk_wait.links,
  158. struct rpc_task,
  159. u.tk_wait.links);
  160. /* Assume __rpc_list_enqueue_task() cached the queue head */
  161. q = t->u.tk_wait.list.next;
  162. list_add_tail(&t->u.tk_wait.list, q);
  163. list_del(&task->u.tk_wait.links);
  164. }
  165. list_del(&task->u.tk_wait.list);
  166. }
  167. /*
  168. * Add new request to a priority queue.
  169. */
  170. static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue,
  171. struct rpc_task *task,
  172. unsigned char queue_priority)
  173. {
  174. if (unlikely(queue_priority > queue->maxpriority))
  175. queue_priority = queue->maxpriority;
  176. __rpc_list_enqueue_task(&queue->tasks[queue_priority], task);
  177. }
  178. /*
  179. * Add new request to wait queue.
  180. */
  181. static void __rpc_add_wait_queue(struct rpc_wait_queue *queue,
  182. struct rpc_task *task,
  183. unsigned char queue_priority)
  184. {
  185. INIT_LIST_HEAD(&task->u.tk_wait.timer_list);
  186. if (RPC_IS_PRIORITY(queue))
  187. __rpc_add_wait_queue_priority(queue, task, queue_priority);
  188. else
  189. list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
  190. task->tk_waitqueue = queue;
  191. queue->qlen++;
  192. /* barrier matches the read in rpc_wake_up_task_queue_locked() */
  193. smp_wmb();
  194. rpc_set_queued(task);
  195. }
  196. /*
  197. * Remove request from a priority queue.
  198. */
  199. static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
  200. {
  201. __rpc_list_dequeue_task(task);
  202. }
  203. /*
  204. * Remove request from queue.
  205. * Note: must be called with spin lock held.
  206. */
  207. static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
  208. {
  209. __rpc_disable_timer(queue, task);
  210. if (RPC_IS_PRIORITY(queue))
  211. __rpc_remove_wait_queue_priority(task);
  212. else
  213. list_del(&task->u.tk_wait.list);
  214. queue->qlen--;
  215. }
  216. static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
  217. {
  218. int i;
  219. spin_lock_init(&queue->lock);
  220. for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
  221. INIT_LIST_HEAD(&queue->tasks[i]);
  222. queue->maxpriority = nr_queues - 1;
  223. rpc_reset_waitqueue_priority(queue);
  224. queue->qlen = 0;
  225. queue->timer_list.expires = 0;
  226. INIT_DELAYED_WORK(&queue->timer_list.dwork, __rpc_queue_timer_fn);
  227. INIT_LIST_HEAD(&queue->timer_list.list);
  228. rpc_assign_waitqueue_name(queue, qname);
  229. }
  230. void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  231. {
  232. __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
  233. }
  234. EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue);
  235. void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  236. {
  237. __rpc_init_priority_wait_queue(queue, qname, 1);
  238. }
  239. EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
  240. void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
  241. {
  242. cancel_delayed_work_sync(&queue->timer_list.dwork);
  243. }
  244. EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
  245. static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode)
  246. {
  247. schedule();
  248. if (signal_pending_state(mode, current))
  249. return -ERESTARTSYS;
  250. return 0;
  251. }
  252. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
  253. static void rpc_task_set_debuginfo(struct rpc_task *task)
  254. {
  255. struct rpc_clnt *clnt = task->tk_client;
  256. /* Might be a task carrying a reverse-direction operation */
  257. if (!clnt) {
  258. static atomic_t rpc_pid;
  259. task->tk_pid = atomic_inc_return(&rpc_pid);
  260. return;
  261. }
  262. task->tk_pid = atomic_inc_return(&clnt->cl_pid);
  263. }
  264. #else
  265. static inline void rpc_task_set_debuginfo(struct rpc_task *task)
  266. {
  267. }
  268. #endif
  269. static void rpc_set_active(struct rpc_task *task)
  270. {
  271. rpc_task_set_debuginfo(task);
  272. set_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
  273. trace_rpc_task_begin(task, NULL);
  274. }
  275. /*
  276. * Mark an RPC call as having completed by clearing the 'active' bit
  277. * and then waking up all tasks that were sleeping.
  278. */
  279. static int rpc_complete_task(struct rpc_task *task)
  280. {
  281. void *m = &task->tk_runstate;
  282. wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE);
  283. struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE);
  284. unsigned long flags;
  285. int ret;
  286. trace_rpc_task_complete(task, NULL);
  287. spin_lock_irqsave(&wq->lock, flags);
  288. clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
  289. ret = atomic_dec_and_test(&task->tk_count);
  290. if (waitqueue_active(wq))
  291. __wake_up_locked_key(wq, TASK_NORMAL, &k);
  292. spin_unlock_irqrestore(&wq->lock, flags);
  293. return ret;
  294. }
  295. /*
  296. * Allow callers to wait for completion of an RPC call
  297. *
  298. * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit()
  299. * to enforce taking of the wq->lock and hence avoid races with
  300. * rpc_complete_task().
  301. */
  302. int rpc_wait_for_completion_task(struct rpc_task *task)
  303. {
  304. return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
  305. rpc_wait_bit_killable, TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  306. }
  307. EXPORT_SYMBOL_GPL(rpc_wait_for_completion_task);
  308. /*
  309. * Make an RPC task runnable.
  310. *
  311. * Note: If the task is ASYNC, and is being made runnable after sitting on an
  312. * rpc_wait_queue, this must be called with the queue spinlock held to protect
  313. * the wait queue operation.
  314. * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(),
  315. * which is needed to ensure that __rpc_execute() doesn't loop (due to the
  316. * lockless RPC_IS_QUEUED() test) before we've had a chance to test
  317. * the RPC_TASK_RUNNING flag.
  318. */
  319. static void rpc_make_runnable(struct workqueue_struct *wq,
  320. struct rpc_task *task)
  321. {
  322. bool need_wakeup = !rpc_test_and_set_running(task);
  323. rpc_clear_queued(task);
  324. if (!need_wakeup)
  325. return;
  326. if (RPC_IS_ASYNC(task)) {
  327. INIT_WORK(&task->u.tk_work, rpc_async_schedule);
  328. queue_work(wq, &task->u.tk_work);
  329. } else {
  330. smp_mb__after_atomic();
  331. wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
  332. }
  333. }
  334. /*
  335. * Prepare for sleeping on a wait queue.
  336. * By always appending tasks to the list we ensure FIFO behavior.
  337. * NB: An RPC task will only receive interrupt-driven events as long
  338. * as it's on a wait queue.
  339. */
  340. static void __rpc_do_sleep_on_priority(struct rpc_wait_queue *q,
  341. struct rpc_task *task,
  342. unsigned char queue_priority)
  343. {
  344. trace_rpc_task_sleep(task, q);
  345. __rpc_add_wait_queue(q, task, queue_priority);
  346. }
  347. static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
  348. struct rpc_task *task,
  349. unsigned char queue_priority)
  350. {
  351. if (WARN_ON_ONCE(RPC_IS_QUEUED(task)))
  352. return;
  353. __rpc_do_sleep_on_priority(q, task, queue_priority);
  354. }
  355. static void __rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q,
  356. struct rpc_task *task, unsigned long timeout,
  357. unsigned char queue_priority)
  358. {
  359. if (WARN_ON_ONCE(RPC_IS_QUEUED(task)))
  360. return;
  361. if (time_is_after_jiffies(timeout)) {
  362. __rpc_do_sleep_on_priority(q, task, queue_priority);
  363. __rpc_add_timer(q, task, timeout);
  364. } else
  365. task->tk_status = -ETIMEDOUT;
  366. }
  367. static void rpc_set_tk_callback(struct rpc_task *task, rpc_action action)
  368. {
  369. if (action && !WARN_ON_ONCE(task->tk_callback != NULL))
  370. task->tk_callback = action;
  371. }
  372. static bool rpc_sleep_check_activated(struct rpc_task *task)
  373. {
  374. /* We shouldn't ever put an inactive task to sleep */
  375. if (WARN_ON_ONCE(!RPC_IS_ACTIVATED(task))) {
  376. task->tk_status = -EIO;
  377. rpc_put_task_async(task);
  378. return false;
  379. }
  380. return true;
  381. }
  382. void rpc_sleep_on_timeout(struct rpc_wait_queue *q, struct rpc_task *task,
  383. rpc_action action, unsigned long timeout)
  384. {
  385. if (!rpc_sleep_check_activated(task))
  386. return;
  387. rpc_set_tk_callback(task, action);
  388. /*
  389. * Protect the queue operations.
  390. */
  391. spin_lock(&q->lock);
  392. __rpc_sleep_on_priority_timeout(q, task, timeout, task->tk_priority);
  393. spin_unlock(&q->lock);
  394. }
  395. EXPORT_SYMBOL_GPL(rpc_sleep_on_timeout);
  396. void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  397. rpc_action action)
  398. {
  399. if (!rpc_sleep_check_activated(task))
  400. return;
  401. rpc_set_tk_callback(task, action);
  402. WARN_ON_ONCE(task->tk_timeout != 0);
  403. /*
  404. * Protect the queue operations.
  405. */
  406. spin_lock(&q->lock);
  407. __rpc_sleep_on_priority(q, task, task->tk_priority);
  408. spin_unlock(&q->lock);
  409. }
  410. EXPORT_SYMBOL_GPL(rpc_sleep_on);
  411. void rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q,
  412. struct rpc_task *task, unsigned long timeout, int priority)
  413. {
  414. if (!rpc_sleep_check_activated(task))
  415. return;
  416. priority -= RPC_PRIORITY_LOW;
  417. /*
  418. * Protect the queue operations.
  419. */
  420. spin_lock(&q->lock);
  421. __rpc_sleep_on_priority_timeout(q, task, timeout, priority);
  422. spin_unlock(&q->lock);
  423. }
  424. EXPORT_SYMBOL_GPL(rpc_sleep_on_priority_timeout);
  425. void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
  426. int priority)
  427. {
  428. if (!rpc_sleep_check_activated(task))
  429. return;
  430. WARN_ON_ONCE(task->tk_timeout != 0);
  431. priority -= RPC_PRIORITY_LOW;
  432. /*
  433. * Protect the queue operations.
  434. */
  435. spin_lock(&q->lock);
  436. __rpc_sleep_on_priority(q, task, priority);
  437. spin_unlock(&q->lock);
  438. }
  439. EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
  440. /**
  441. * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task
  442. * @wq: workqueue on which to run task
  443. * @queue: wait queue
  444. * @task: task to be woken up
  445. *
  446. * Caller must hold queue->lock, and have cleared the task queued flag.
  447. */
  448. static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq,
  449. struct rpc_wait_queue *queue,
  450. struct rpc_task *task)
  451. {
  452. /* Has the task been executed yet? If not, we cannot wake it up! */
  453. if (!RPC_IS_ACTIVATED(task)) {
  454. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  455. return;
  456. }
  457. trace_rpc_task_wakeup(task, queue);
  458. __rpc_remove_wait_queue(queue, task);
  459. rpc_make_runnable(wq, task);
  460. }
  461. /*
  462. * Wake up a queued task while the queue lock is being held
  463. */
  464. static struct rpc_task *
  465. rpc_wake_up_task_on_wq_queue_action_locked(struct workqueue_struct *wq,
  466. struct rpc_wait_queue *queue, struct rpc_task *task,
  467. bool (*action)(struct rpc_task *, void *), void *data)
  468. {
  469. if (RPC_IS_QUEUED(task)) {
  470. smp_rmb();
  471. if (task->tk_waitqueue == queue) {
  472. if (action == NULL || action(task, data)) {
  473. __rpc_do_wake_up_task_on_wq(wq, queue, task);
  474. return task;
  475. }
  476. }
  477. }
  478. return NULL;
  479. }
  480. /*
  481. * Wake up a queued task while the queue lock is being held
  482. */
  483. static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue,
  484. struct rpc_task *task)
  485. {
  486. rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue,
  487. task, NULL, NULL);
  488. }
  489. /*
  490. * Wake up a task on a specific queue
  491. */
  492. void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
  493. {
  494. if (!RPC_IS_QUEUED(task))
  495. return;
  496. spin_lock(&queue->lock);
  497. rpc_wake_up_task_queue_locked(queue, task);
  498. spin_unlock(&queue->lock);
  499. }
  500. EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
  501. static bool rpc_task_action_set_status(struct rpc_task *task, void *status)
  502. {
  503. task->tk_status = *(int *)status;
  504. return true;
  505. }
  506. static void
  507. rpc_wake_up_task_queue_set_status_locked(struct rpc_wait_queue *queue,
  508. struct rpc_task *task, int status)
  509. {
  510. rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue,
  511. task, rpc_task_action_set_status, &status);
  512. }
  513. /**
  514. * rpc_wake_up_queued_task_set_status - wake up a task and set task->tk_status
  515. * @queue: pointer to rpc_wait_queue
  516. * @task: pointer to rpc_task
  517. * @status: integer error value
  518. *
  519. * If @task is queued on @queue, then it is woken up, and @task->tk_status is
  520. * set to the value of @status.
  521. */
  522. void
  523. rpc_wake_up_queued_task_set_status(struct rpc_wait_queue *queue,
  524. struct rpc_task *task, int status)
  525. {
  526. if (!RPC_IS_QUEUED(task))
  527. return;
  528. spin_lock(&queue->lock);
  529. rpc_wake_up_task_queue_set_status_locked(queue, task, status);
  530. spin_unlock(&queue->lock);
  531. }
  532. /*
  533. * Wake up the next task on a priority queue.
  534. */
  535. static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
  536. {
  537. struct list_head *q;
  538. struct rpc_task *task;
  539. /*
  540. * Service the privileged queue.
  541. */
  542. q = &queue->tasks[RPC_NR_PRIORITY - 1];
  543. if (queue->maxpriority > RPC_PRIORITY_PRIVILEGED && !list_empty(q)) {
  544. task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
  545. goto out;
  546. }
  547. /*
  548. * Service a batch of tasks from a single owner.
  549. */
  550. q = &queue->tasks[queue->priority];
  551. if (!list_empty(q) && queue->nr) {
  552. queue->nr--;
  553. task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
  554. goto out;
  555. }
  556. /*
  557. * Service the next queue.
  558. */
  559. do {
  560. if (q == &queue->tasks[0])
  561. q = &queue->tasks[queue->maxpriority];
  562. else
  563. q = q - 1;
  564. if (!list_empty(q)) {
  565. task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
  566. goto new_queue;
  567. }
  568. } while (q != &queue->tasks[queue->priority]);
  569. rpc_reset_waitqueue_priority(queue);
  570. return NULL;
  571. new_queue:
  572. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  573. out:
  574. return task;
  575. }
  576. static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
  577. {
  578. if (RPC_IS_PRIORITY(queue))
  579. return __rpc_find_next_queued_priority(queue);
  580. if (!list_empty(&queue->tasks[0]))
  581. return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
  582. return NULL;
  583. }
  584. /*
  585. * Wake up the first task on the wait queue.
  586. */
  587. struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq,
  588. struct rpc_wait_queue *queue,
  589. bool (*func)(struct rpc_task *, void *), void *data)
  590. {
  591. struct rpc_task *task = NULL;
  592. spin_lock(&queue->lock);
  593. task = __rpc_find_next_queued(queue);
  594. if (task != NULL)
  595. task = rpc_wake_up_task_on_wq_queue_action_locked(wq, queue,
  596. task, func, data);
  597. spin_unlock(&queue->lock);
  598. return task;
  599. }
  600. /*
  601. * Wake up the first task on the wait queue.
  602. */
  603. struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
  604. bool (*func)(struct rpc_task *, void *), void *data)
  605. {
  606. return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data);
  607. }
  608. EXPORT_SYMBOL_GPL(rpc_wake_up_first);
  609. static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
  610. {
  611. return true;
  612. }
  613. /*
  614. * Wake up the next task on the wait queue.
  615. */
  616. struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
  617. {
  618. return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
  619. }
  620. EXPORT_SYMBOL_GPL(rpc_wake_up_next);
  621. /**
  622. * rpc_wake_up_locked - wake up all rpc_tasks
  623. * @queue: rpc_wait_queue on which the tasks are sleeping
  624. *
  625. */
  626. static void rpc_wake_up_locked(struct rpc_wait_queue *queue)
  627. {
  628. struct rpc_task *task;
  629. for (;;) {
  630. task = __rpc_find_next_queued(queue);
  631. if (task == NULL)
  632. break;
  633. rpc_wake_up_task_queue_locked(queue, task);
  634. }
  635. }
  636. /**
  637. * rpc_wake_up - wake up all rpc_tasks
  638. * @queue: rpc_wait_queue on which the tasks are sleeping
  639. *
  640. * Grabs queue->lock
  641. */
  642. void rpc_wake_up(struct rpc_wait_queue *queue)
  643. {
  644. spin_lock(&queue->lock);
  645. rpc_wake_up_locked(queue);
  646. spin_unlock(&queue->lock);
  647. }
  648. EXPORT_SYMBOL_GPL(rpc_wake_up);
  649. /**
  650. * rpc_wake_up_status_locked - wake up all rpc_tasks and set their status value.
  651. * @queue: rpc_wait_queue on which the tasks are sleeping
  652. * @status: status value to set
  653. */
  654. static void rpc_wake_up_status_locked(struct rpc_wait_queue *queue, int status)
  655. {
  656. struct rpc_task *task;
  657. for (;;) {
  658. task = __rpc_find_next_queued(queue);
  659. if (task == NULL)
  660. break;
  661. rpc_wake_up_task_queue_set_status_locked(queue, task, status);
  662. }
  663. }
  664. /**
  665. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  666. * @queue: rpc_wait_queue on which the tasks are sleeping
  667. * @status: status value to set
  668. *
  669. * Grabs queue->lock
  670. */
  671. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  672. {
  673. spin_lock(&queue->lock);
  674. rpc_wake_up_status_locked(queue, status);
  675. spin_unlock(&queue->lock);
  676. }
  677. EXPORT_SYMBOL_GPL(rpc_wake_up_status);
  678. static void __rpc_queue_timer_fn(struct work_struct *work)
  679. {
  680. struct rpc_wait_queue *queue = container_of(work,
  681. struct rpc_wait_queue,
  682. timer_list.dwork.work);
  683. struct rpc_task *task, *n;
  684. unsigned long expires, now, timeo;
  685. spin_lock(&queue->lock);
  686. expires = now = jiffies;
  687. list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
  688. timeo = task->tk_timeout;
  689. if (time_after_eq(now, timeo)) {
  690. trace_rpc_task_timeout(task, task->tk_action);
  691. task->tk_status = -ETIMEDOUT;
  692. rpc_wake_up_task_queue_locked(queue, task);
  693. continue;
  694. }
  695. if (expires == now || time_after(expires, timeo))
  696. expires = timeo;
  697. }
  698. if (!list_empty(&queue->timer_list.list))
  699. rpc_set_queue_timer(queue, expires);
  700. spin_unlock(&queue->lock);
  701. }
  702. static void __rpc_atrun(struct rpc_task *task)
  703. {
  704. if (task->tk_status == -ETIMEDOUT)
  705. task->tk_status = 0;
  706. }
  707. /*
  708. * Run a task at a later time
  709. */
  710. void rpc_delay(struct rpc_task *task, unsigned long delay)
  711. {
  712. rpc_sleep_on_timeout(&delay_queue, task, __rpc_atrun, jiffies + delay);
  713. }
  714. EXPORT_SYMBOL_GPL(rpc_delay);
  715. /*
  716. * Helper to call task->tk_ops->rpc_call_prepare
  717. */
  718. void rpc_prepare_task(struct rpc_task *task)
  719. {
  720. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  721. }
  722. static void
  723. rpc_init_task_statistics(struct rpc_task *task)
  724. {
  725. /* Initialize retry counters */
  726. task->tk_garb_retry = 2;
  727. task->tk_cred_retry = 2;
  728. /* starting timestamp */
  729. task->tk_start = ktime_get();
  730. }
  731. static void
  732. rpc_reset_task_statistics(struct rpc_task *task)
  733. {
  734. task->tk_timeouts = 0;
  735. task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_SENT);
  736. rpc_init_task_statistics(task);
  737. }
  738. /*
  739. * Helper that calls task->tk_ops->rpc_call_done if it exists
  740. */
  741. void rpc_exit_task(struct rpc_task *task)
  742. {
  743. trace_rpc_task_end(task, task->tk_action);
  744. task->tk_action = NULL;
  745. if (task->tk_ops->rpc_count_stats)
  746. task->tk_ops->rpc_count_stats(task, task->tk_calldata);
  747. else if (task->tk_client)
  748. rpc_count_iostats(task, task->tk_client->cl_metrics);
  749. if (task->tk_ops->rpc_call_done != NULL) {
  750. trace_rpc_task_call_done(task, task->tk_ops->rpc_call_done);
  751. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  752. if (task->tk_action != NULL) {
  753. /* Always release the RPC slot and buffer memory */
  754. xprt_release(task);
  755. rpc_reset_task_statistics(task);
  756. }
  757. }
  758. }
  759. void rpc_signal_task(struct rpc_task *task)
  760. {
  761. struct rpc_wait_queue *queue;
  762. if (!RPC_IS_ACTIVATED(task))
  763. return;
  764. if (!rpc_task_set_rpc_status(task, -ERESTARTSYS))
  765. return;
  766. trace_rpc_task_signalled(task, task->tk_action);
  767. queue = READ_ONCE(task->tk_waitqueue);
  768. if (queue)
  769. rpc_wake_up_queued_task(queue, task);
  770. }
  771. void rpc_task_try_cancel(struct rpc_task *task, int error)
  772. {
  773. struct rpc_wait_queue *queue;
  774. if (!rpc_task_set_rpc_status(task, error))
  775. return;
  776. queue = READ_ONCE(task->tk_waitqueue);
  777. if (queue)
  778. rpc_wake_up_queued_task(queue, task);
  779. }
  780. void rpc_exit(struct rpc_task *task, int status)
  781. {
  782. task->tk_status = status;
  783. task->tk_action = rpc_exit_task;
  784. rpc_wake_up_queued_task(task->tk_waitqueue, task);
  785. }
  786. EXPORT_SYMBOL_GPL(rpc_exit);
  787. void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
  788. {
  789. if (ops->rpc_release != NULL)
  790. ops->rpc_release(calldata);
  791. }
  792. static bool xprt_needs_memalloc(struct rpc_xprt *xprt, struct rpc_task *tk)
  793. {
  794. if (!xprt)
  795. return false;
  796. if (!atomic_read(&xprt->swapper))
  797. return false;
  798. return test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == tk;
  799. }
  800. /*
  801. * This is the RPC `scheduler' (or rather, the finite state machine).
  802. */
  803. static void __rpc_execute(struct rpc_task *task)
  804. {
  805. struct rpc_wait_queue *queue;
  806. int task_is_async = RPC_IS_ASYNC(task);
  807. int status = 0;
  808. unsigned long pflags = current->flags;
  809. WARN_ON_ONCE(RPC_IS_QUEUED(task));
  810. if (RPC_IS_QUEUED(task))
  811. return;
  812. for (;;) {
  813. void (*do_action)(struct rpc_task *);
  814. /*
  815. * Perform the next FSM step or a pending callback.
  816. *
  817. * tk_action may be NULL if the task has been killed.
  818. */
  819. do_action = task->tk_action;
  820. /* Tasks with an RPC error status should exit */
  821. if (do_action && do_action != rpc_exit_task &&
  822. (status = READ_ONCE(task->tk_rpc_status)) != 0) {
  823. task->tk_status = status;
  824. do_action = rpc_exit_task;
  825. }
  826. /* Callbacks override all actions */
  827. if (task->tk_callback) {
  828. do_action = task->tk_callback;
  829. task->tk_callback = NULL;
  830. }
  831. if (!do_action)
  832. break;
  833. if (RPC_IS_SWAPPER(task) ||
  834. xprt_needs_memalloc(task->tk_xprt, task))
  835. current->flags |= PF_MEMALLOC;
  836. trace_rpc_task_run_action(task, do_action);
  837. do_action(task);
  838. /*
  839. * Lockless check for whether task is sleeping or not.
  840. */
  841. if (!RPC_IS_QUEUED(task)) {
  842. cond_resched();
  843. continue;
  844. }
  845. /*
  846. * The queue->lock protects against races with
  847. * rpc_make_runnable().
  848. *
  849. * Note that once we clear RPC_TASK_RUNNING on an asynchronous
  850. * rpc_task, rpc_make_runnable() can assign it to a
  851. * different workqueue. We therefore cannot assume that the
  852. * rpc_task pointer may still be dereferenced.
  853. */
  854. queue = task->tk_waitqueue;
  855. spin_lock(&queue->lock);
  856. if (!RPC_IS_QUEUED(task)) {
  857. spin_unlock(&queue->lock);
  858. continue;
  859. }
  860. /* Wake up any task that has an exit status */
  861. if (READ_ONCE(task->tk_rpc_status) != 0) {
  862. rpc_wake_up_task_queue_locked(queue, task);
  863. spin_unlock(&queue->lock);
  864. continue;
  865. }
  866. rpc_clear_running(task);
  867. spin_unlock(&queue->lock);
  868. if (task_is_async)
  869. goto out;
  870. /* sync task: sleep here */
  871. trace_rpc_task_sync_sleep(task, task->tk_action);
  872. status = out_of_line_wait_on_bit(&task->tk_runstate,
  873. RPC_TASK_QUEUED, rpc_wait_bit_killable,
  874. TASK_KILLABLE|TASK_FREEZABLE);
  875. if (status < 0) {
  876. /*
  877. * When a sync task receives a signal, it exits with
  878. * -ERESTARTSYS. In order to catch any callbacks that
  879. * clean up after sleeping on some queue, we don't
  880. * break the loop here, but go around once more.
  881. */
  882. rpc_signal_task(task);
  883. }
  884. trace_rpc_task_sync_wake(task, task->tk_action);
  885. }
  886. /* Release all resources associated with the task */
  887. rpc_release_task(task);
  888. out:
  889. current_restore_flags(pflags, PF_MEMALLOC);
  890. }
  891. /*
  892. * User-visible entry point to the scheduler.
  893. *
  894. * This may be called recursively if e.g. an async NFS task updates
  895. * the attributes and finds that dirty pages must be flushed.
  896. * NOTE: Upon exit of this function the task is guaranteed to be
  897. * released. In particular note that tk_release() will have
  898. * been called, so your task memory may have been freed.
  899. */
  900. void rpc_execute(struct rpc_task *task)
  901. {
  902. bool is_async = RPC_IS_ASYNC(task);
  903. rpc_set_active(task);
  904. rpc_make_runnable(rpciod_workqueue, task);
  905. if (!is_async) {
  906. unsigned int pflags = memalloc_nofs_save();
  907. __rpc_execute(task);
  908. memalloc_nofs_restore(pflags);
  909. }
  910. }
  911. static void rpc_async_schedule(struct work_struct *work)
  912. {
  913. unsigned int pflags = memalloc_nofs_save();
  914. __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
  915. memalloc_nofs_restore(pflags);
  916. }
  917. /**
  918. * rpc_malloc - allocate RPC buffer resources
  919. * @task: RPC task
  920. *
  921. * A single memory region is allocated, which is split between the
  922. * RPC call and RPC reply that this task is being used for. When
  923. * this RPC is retired, the memory is released by calling rpc_free.
  924. *
  925. * To prevent rpciod from hanging, this allocator never sleeps,
  926. * returning -ENOMEM and suppressing warning if the request cannot
  927. * be serviced immediately. The caller can arrange to sleep in a
  928. * way that is safe for rpciod.
  929. *
  930. * Most requests are 'small' (under 2KiB) and can be serviced from a
  931. * mempool, ensuring that NFS reads and writes can always proceed,
  932. * and that there is good locality of reference for these buffers.
  933. */
  934. int rpc_malloc(struct rpc_task *task)
  935. {
  936. struct rpc_rqst *rqst = task->tk_rqstp;
  937. size_t size = rqst->rq_callsize + rqst->rq_rcvsize;
  938. struct rpc_buffer *buf;
  939. gfp_t gfp = rpc_task_gfp_mask();
  940. size += sizeof(struct rpc_buffer);
  941. if (size <= RPC_BUFFER_MAXSIZE) {
  942. buf = kmem_cache_alloc(rpc_buffer_slabp, gfp);
  943. /* Reach for the mempool if dynamic allocation fails */
  944. if (!buf && RPC_IS_ASYNC(task))
  945. buf = mempool_alloc(rpc_buffer_mempool, GFP_NOWAIT);
  946. } else
  947. buf = kmalloc(size, gfp);
  948. if (!buf)
  949. return -ENOMEM;
  950. buf->len = size;
  951. rqst->rq_buffer = buf->data;
  952. rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
  953. return 0;
  954. }
  955. /**
  956. * rpc_free - free RPC buffer resources allocated via rpc_malloc
  957. * @task: RPC task
  958. *
  959. */
  960. void rpc_free(struct rpc_task *task)
  961. {
  962. void *buffer = task->tk_rqstp->rq_buffer;
  963. size_t size;
  964. struct rpc_buffer *buf;
  965. buf = container_of(buffer, struct rpc_buffer, data);
  966. size = buf->len;
  967. if (size <= RPC_BUFFER_MAXSIZE)
  968. mempool_free(buf, rpc_buffer_mempool);
  969. else
  970. kfree(buf);
  971. }
  972. /*
  973. * Creation and deletion of RPC task structures
  974. */
  975. static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
  976. {
  977. memset(task, 0, sizeof(*task));
  978. atomic_set(&task->tk_count, 1);
  979. task->tk_flags = task_setup_data->flags;
  980. task->tk_ops = task_setup_data->callback_ops;
  981. task->tk_calldata = task_setup_data->callback_data;
  982. INIT_LIST_HEAD(&task->tk_task);
  983. task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
  984. task->tk_owner = current->tgid;
  985. /* Initialize workqueue for async tasks */
  986. task->tk_workqueue = task_setup_data->workqueue;
  987. task->tk_xprt = rpc_task_get_xprt(task_setup_data->rpc_client,
  988. xprt_get(task_setup_data->rpc_xprt));
  989. task->tk_op_cred = get_rpccred(task_setup_data->rpc_op_cred);
  990. if (task->tk_ops->rpc_call_prepare != NULL)
  991. task->tk_action = rpc_prepare_task;
  992. rpc_init_task_statistics(task);
  993. }
  994. static struct rpc_task *rpc_alloc_task(void)
  995. {
  996. struct rpc_task *task;
  997. task = kmem_cache_alloc(rpc_task_slabp, rpc_task_gfp_mask());
  998. if (task)
  999. return task;
  1000. return mempool_alloc(rpc_task_mempool, GFP_NOWAIT);
  1001. }
  1002. /*
  1003. * Create a new task for the specified client.
  1004. */
  1005. struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
  1006. {
  1007. struct rpc_task *task = setup_data->task;
  1008. unsigned short flags = 0;
  1009. if (task == NULL) {
  1010. task = rpc_alloc_task();
  1011. if (task == NULL) {
  1012. rpc_release_calldata(setup_data->callback_ops,
  1013. setup_data->callback_data);
  1014. return ERR_PTR(-ENOMEM);
  1015. }
  1016. flags = RPC_TASK_DYNAMIC;
  1017. }
  1018. rpc_init_task(task, setup_data);
  1019. task->tk_flags |= flags;
  1020. return task;
  1021. }
  1022. /*
  1023. * rpc_free_task - release rpc task and perform cleanups
  1024. *
  1025. * Note that we free up the rpc_task _after_ rpc_release_calldata()
  1026. * in order to work around a workqueue dependency issue.
  1027. *
  1028. * Tejun Heo states:
  1029. * "Workqueue currently considers two work items to be the same if they're
  1030. * on the same address and won't execute them concurrently - ie. it
  1031. * makes a work item which is queued again while being executed wait
  1032. * for the previous execution to complete.
  1033. *
  1034. * If a work function frees the work item, and then waits for an event
  1035. * which should be performed by another work item and *that* work item
  1036. * recycles the freed work item, it can create a false dependency loop.
  1037. * There really is no reliable way to detect this short of verifying
  1038. * every memory free."
  1039. *
  1040. */
  1041. static void rpc_free_task(struct rpc_task *task)
  1042. {
  1043. unsigned short tk_flags = task->tk_flags;
  1044. put_rpccred(task->tk_op_cred);
  1045. rpc_release_calldata(task->tk_ops, task->tk_calldata);
  1046. if (tk_flags & RPC_TASK_DYNAMIC)
  1047. mempool_free(task, rpc_task_mempool);
  1048. }
  1049. static void rpc_async_release(struct work_struct *work)
  1050. {
  1051. unsigned int pflags = memalloc_nofs_save();
  1052. rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
  1053. memalloc_nofs_restore(pflags);
  1054. }
  1055. static void rpc_release_resources_task(struct rpc_task *task)
  1056. {
  1057. xprt_release(task);
  1058. if (task->tk_msg.rpc_cred) {
  1059. if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
  1060. put_cred(task->tk_msg.rpc_cred);
  1061. task->tk_msg.rpc_cred = NULL;
  1062. }
  1063. rpc_task_release_client(task);
  1064. }
  1065. static void rpc_final_put_task(struct rpc_task *task,
  1066. struct workqueue_struct *q)
  1067. {
  1068. if (q != NULL) {
  1069. INIT_WORK(&task->u.tk_work, rpc_async_release);
  1070. queue_work(q, &task->u.tk_work);
  1071. } else
  1072. rpc_free_task(task);
  1073. }
  1074. static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
  1075. {
  1076. if (atomic_dec_and_test(&task->tk_count)) {
  1077. rpc_release_resources_task(task);
  1078. rpc_final_put_task(task, q);
  1079. }
  1080. }
  1081. void rpc_put_task(struct rpc_task *task)
  1082. {
  1083. rpc_do_put_task(task, NULL);
  1084. }
  1085. EXPORT_SYMBOL_GPL(rpc_put_task);
  1086. void rpc_put_task_async(struct rpc_task *task)
  1087. {
  1088. rpc_do_put_task(task, task->tk_workqueue);
  1089. }
  1090. EXPORT_SYMBOL_GPL(rpc_put_task_async);
  1091. static void rpc_release_task(struct rpc_task *task)
  1092. {
  1093. WARN_ON_ONCE(RPC_IS_QUEUED(task));
  1094. rpc_release_resources_task(task);
  1095. /*
  1096. * Note: at this point we have been removed from rpc_clnt->cl_tasks,
  1097. * so it should be safe to use task->tk_count as a test for whether
  1098. * or not any other processes still hold references to our rpc_task.
  1099. */
  1100. if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
  1101. /* Wake up anyone who may be waiting for task completion */
  1102. if (!rpc_complete_task(task))
  1103. return;
  1104. } else {
  1105. if (!atomic_dec_and_test(&task->tk_count))
  1106. return;
  1107. }
  1108. rpc_final_put_task(task, task->tk_workqueue);
  1109. }
  1110. int rpciod_up(void)
  1111. {
  1112. return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
  1113. }
  1114. void rpciod_down(void)
  1115. {
  1116. module_put(THIS_MODULE);
  1117. }
  1118. /*
  1119. * Start up the rpciod workqueue.
  1120. */
  1121. static int rpciod_start(void)
  1122. {
  1123. struct workqueue_struct *wq;
  1124. /*
  1125. * Create the rpciod thread and wait for it to start.
  1126. */
  1127. wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  1128. if (!wq)
  1129. goto out_failed;
  1130. rpciod_workqueue = wq;
  1131. wq = alloc_workqueue("xprtiod", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
  1132. if (!wq)
  1133. goto free_rpciod;
  1134. xprtiod_workqueue = wq;
  1135. return 1;
  1136. free_rpciod:
  1137. wq = rpciod_workqueue;
  1138. rpciod_workqueue = NULL;
  1139. destroy_workqueue(wq);
  1140. out_failed:
  1141. return 0;
  1142. }
  1143. static void rpciod_stop(void)
  1144. {
  1145. struct workqueue_struct *wq = NULL;
  1146. if (rpciod_workqueue == NULL)
  1147. return;
  1148. wq = rpciod_workqueue;
  1149. rpciod_workqueue = NULL;
  1150. destroy_workqueue(wq);
  1151. wq = xprtiod_workqueue;
  1152. xprtiod_workqueue = NULL;
  1153. destroy_workqueue(wq);
  1154. }
  1155. void
  1156. rpc_destroy_mempool(void)
  1157. {
  1158. rpciod_stop();
  1159. mempool_destroy(rpc_buffer_mempool);
  1160. mempool_destroy(rpc_task_mempool);
  1161. kmem_cache_destroy(rpc_task_slabp);
  1162. kmem_cache_destroy(rpc_buffer_slabp);
  1163. rpc_destroy_wait_queue(&delay_queue);
  1164. }
  1165. int
  1166. rpc_init_mempool(void)
  1167. {
  1168. /*
  1169. * The following is not strictly a mempool initialisation,
  1170. * but there is no harm in doing it here
  1171. */
  1172. rpc_init_wait_queue(&delay_queue, "delayq");
  1173. if (!rpciod_start())
  1174. goto err_nomem;
  1175. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  1176. sizeof(struct rpc_task),
  1177. 0, SLAB_HWCACHE_ALIGN,
  1178. NULL);
  1179. if (!rpc_task_slabp)
  1180. goto err_nomem;
  1181. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  1182. RPC_BUFFER_MAXSIZE,
  1183. 0, SLAB_HWCACHE_ALIGN,
  1184. NULL);
  1185. if (!rpc_buffer_slabp)
  1186. goto err_nomem;
  1187. rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
  1188. rpc_task_slabp);
  1189. if (!rpc_task_mempool)
  1190. goto err_nomem;
  1191. rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
  1192. rpc_buffer_slabp);
  1193. if (!rpc_buffer_mempool)
  1194. goto err_nomem;
  1195. return 0;
  1196. err_nomem:
  1197. rpc_destroy_mempool();
  1198. return -ENOMEM;
  1199. }