sched.c 30 KB

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