sched.c 26 KB

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