sched.c 26 KB

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