sched.c 33 KB

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