sched.c 25 KB

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