sched.c 29 KB

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