sched.c 26 KB

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