sched.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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/smp_lock.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/mutex.h>
  20. #include <linux/sunrpc/clnt.h>
  21. #ifdef RPC_DEBUG
  22. #define RPCDBG_FACILITY RPCDBG_SCHED
  23. #define RPC_TASK_MAGIC_ID 0xf00baa
  24. static int rpc_task_id;
  25. #endif
  26. /*
  27. * RPC slabs and memory pools
  28. */
  29. #define RPC_BUFFER_MAXSIZE (2048)
  30. #define RPC_BUFFER_POOLSIZE (8)
  31. #define RPC_TASK_POOLSIZE (8)
  32. static kmem_cache_t *rpc_task_slabp __read_mostly;
  33. static kmem_cache_t *rpc_buffer_slabp __read_mostly;
  34. static mempool_t *rpc_task_mempool __read_mostly;
  35. static mempool_t *rpc_buffer_mempool __read_mostly;
  36. static void __rpc_default_timer(struct rpc_task *task);
  37. static void rpciod_killall(void);
  38. static void rpc_async_schedule(void *);
  39. /*
  40. * RPC tasks sit here while waiting for conditions to improve.
  41. */
  42. static RPC_WAITQ(delay_queue, "delayq");
  43. /*
  44. * All RPC tasks are linked into this list
  45. */
  46. static LIST_HEAD(all_tasks);
  47. /*
  48. * rpciod-related stuff
  49. */
  50. static DEFINE_MUTEX(rpciod_mutex);
  51. static unsigned int rpciod_users;
  52. struct workqueue_struct *rpciod_workqueue;
  53. /*
  54. * Spinlock for other critical sections of code.
  55. */
  56. static DEFINE_SPINLOCK(rpc_sched_lock);
  57. /*
  58. * Disable the timer for a given RPC task. Should be called with
  59. * queue->lock and bh_disabled in order to avoid races within
  60. * rpc_run_timer().
  61. */
  62. static inline void
  63. __rpc_disable_timer(struct rpc_task *task)
  64. {
  65. dprintk("RPC: %4d disabling timer\n", task->tk_pid);
  66. task->tk_timeout_fn = NULL;
  67. task->tk_timeout = 0;
  68. }
  69. /*
  70. * Run a timeout function.
  71. * We use the callback in order to allow __rpc_wake_up_task()
  72. * and friends to disable the timer synchronously on SMP systems
  73. * without calling del_timer_sync(). The latter could cause a
  74. * deadlock if called while we're holding spinlocks...
  75. */
  76. static void rpc_run_timer(struct rpc_task *task)
  77. {
  78. void (*callback)(struct rpc_task *);
  79. callback = task->tk_timeout_fn;
  80. task->tk_timeout_fn = NULL;
  81. if (callback && RPC_IS_QUEUED(task)) {
  82. dprintk("RPC: %4d running timer\n", task->tk_pid);
  83. callback(task);
  84. }
  85. smp_mb__before_clear_bit();
  86. clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
  87. smp_mb__after_clear_bit();
  88. }
  89. /*
  90. * Set up a timer for the current task.
  91. */
  92. static inline void
  93. __rpc_add_timer(struct rpc_task *task, rpc_action timer)
  94. {
  95. if (!task->tk_timeout)
  96. return;
  97. dprintk("RPC: %4d setting alarm for %lu ms\n",
  98. task->tk_pid, task->tk_timeout * 1000 / HZ);
  99. if (timer)
  100. task->tk_timeout_fn = timer;
  101. else
  102. task->tk_timeout_fn = __rpc_default_timer;
  103. set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
  104. mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
  105. }
  106. /*
  107. * Delete any timer for the current task. Because we use del_timer_sync(),
  108. * this function should never be called while holding queue->lock.
  109. */
  110. static void
  111. rpc_delete_timer(struct rpc_task *task)
  112. {
  113. if (RPC_IS_QUEUED(task))
  114. return;
  115. if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
  116. del_singleshot_timer_sync(&task->tk_timer);
  117. dprintk("RPC: %4d deleting timer\n", task->tk_pid);
  118. }
  119. }
  120. /*
  121. * Add new request to a priority queue.
  122. */
  123. static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
  124. {
  125. struct list_head *q;
  126. struct rpc_task *t;
  127. INIT_LIST_HEAD(&task->u.tk_wait.links);
  128. q = &queue->tasks[task->tk_priority];
  129. if (unlikely(task->tk_priority > queue->maxpriority))
  130. q = &queue->tasks[queue->maxpriority];
  131. list_for_each_entry(t, q, u.tk_wait.list) {
  132. if (t->tk_cookie == task->tk_cookie) {
  133. list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
  134. return;
  135. }
  136. }
  137. list_add_tail(&task->u.tk_wait.list, q);
  138. }
  139. /*
  140. * Add new request to wait queue.
  141. *
  142. * Swapper tasks always get inserted at the head of the queue.
  143. * This should avoid many nasty memory deadlocks and hopefully
  144. * improve overall performance.
  145. * Everyone else gets appended to the queue to ensure proper FIFO behavior.
  146. */
  147. static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
  148. {
  149. BUG_ON (RPC_IS_QUEUED(task));
  150. if (RPC_IS_PRIORITY(queue))
  151. __rpc_add_wait_queue_priority(queue, task);
  152. else if (RPC_IS_SWAPPER(task))
  153. list_add(&task->u.tk_wait.list, &queue->tasks[0]);
  154. else
  155. list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
  156. task->u.tk_wait.rpc_waitq = queue;
  157. queue->qlen++;
  158. rpc_set_queued(task);
  159. dprintk("RPC: %4d added to queue %p \"%s\"\n",
  160. task->tk_pid, queue, rpc_qname(queue));
  161. }
  162. /*
  163. * Remove request from a priority queue.
  164. */
  165. static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
  166. {
  167. struct rpc_task *t;
  168. if (!list_empty(&task->u.tk_wait.links)) {
  169. t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
  170. list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
  171. list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
  172. }
  173. list_del(&task->u.tk_wait.list);
  174. }
  175. /*
  176. * Remove request from queue.
  177. * Note: must be called with spin lock held.
  178. */
  179. static void __rpc_remove_wait_queue(struct rpc_task *task)
  180. {
  181. struct rpc_wait_queue *queue;
  182. queue = task->u.tk_wait.rpc_waitq;
  183. if (RPC_IS_PRIORITY(queue))
  184. __rpc_remove_wait_queue_priority(task);
  185. else
  186. list_del(&task->u.tk_wait.list);
  187. queue->qlen--;
  188. dprintk("RPC: %4d removed from queue %p \"%s\"\n",
  189. task->tk_pid, queue, rpc_qname(queue));
  190. }
  191. static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
  192. {
  193. queue->priority = priority;
  194. queue->count = 1 << (priority * 2);
  195. }
  196. static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
  197. {
  198. queue->cookie = cookie;
  199. queue->nr = RPC_BATCH_COUNT;
  200. }
  201. static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
  202. {
  203. rpc_set_waitqueue_priority(queue, queue->maxpriority);
  204. rpc_set_waitqueue_cookie(queue, 0);
  205. }
  206. static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
  207. {
  208. int i;
  209. spin_lock_init(&queue->lock);
  210. for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
  211. INIT_LIST_HEAD(&queue->tasks[i]);
  212. queue->maxpriority = maxprio;
  213. rpc_reset_waitqueue_priority(queue);
  214. #ifdef RPC_DEBUG
  215. queue->name = qname;
  216. #endif
  217. }
  218. void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  219. {
  220. __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
  221. }
  222. void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
  223. {
  224. __rpc_init_priority_wait_queue(queue, qname, 0);
  225. }
  226. EXPORT_SYMBOL(rpc_init_wait_queue);
  227. static int rpc_wait_bit_interruptible(void *word)
  228. {
  229. if (signal_pending(current))
  230. return -ERESTARTSYS;
  231. schedule();
  232. return 0;
  233. }
  234. static void rpc_set_active(struct rpc_task *task)
  235. {
  236. if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
  237. return;
  238. spin_lock(&rpc_sched_lock);
  239. #ifdef RPC_DEBUG
  240. task->tk_magic = RPC_TASK_MAGIC_ID;
  241. task->tk_pid = rpc_task_id++;
  242. #endif
  243. /* Add to global list of all tasks */
  244. list_add_tail(&task->tk_task, &all_tasks);
  245. spin_unlock(&rpc_sched_lock);
  246. }
  247. /*
  248. * Mark an RPC call as having completed by clearing the 'active' bit
  249. */
  250. static void rpc_mark_complete_task(struct rpc_task *task)
  251. {
  252. smp_mb__before_clear_bit();
  253. clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
  254. smp_mb__after_clear_bit();
  255. wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
  256. }
  257. /*
  258. * Allow callers to wait for completion of an RPC call
  259. */
  260. int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
  261. {
  262. if (action == NULL)
  263. action = rpc_wait_bit_interruptible;
  264. return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
  265. action, TASK_INTERRUPTIBLE);
  266. }
  267. EXPORT_SYMBOL(__rpc_wait_for_completion_task);
  268. /*
  269. * Make an RPC task runnable.
  270. *
  271. * Note: If the task is ASYNC, this must be called with
  272. * the spinlock held to protect the wait queue operation.
  273. */
  274. static void rpc_make_runnable(struct rpc_task *task)
  275. {
  276. BUG_ON(task->tk_timeout_fn);
  277. rpc_clear_queued(task);
  278. if (rpc_test_and_set_running(task))
  279. return;
  280. /* We might have raced */
  281. if (RPC_IS_QUEUED(task)) {
  282. rpc_clear_running(task);
  283. return;
  284. }
  285. if (RPC_IS_ASYNC(task)) {
  286. int status;
  287. INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
  288. status = queue_work(task->tk_workqueue, &task->u.tk_work);
  289. if (status < 0) {
  290. printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
  291. task->tk_status = status;
  292. return;
  293. }
  294. } else
  295. wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
  296. }
  297. /*
  298. * Prepare for sleeping on a wait queue.
  299. * By always appending tasks to the list we ensure FIFO behavior.
  300. * NB: An RPC task will only receive interrupt-driven events as long
  301. * as it's on a wait queue.
  302. */
  303. static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  304. rpc_action action, rpc_action timer)
  305. {
  306. dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
  307. rpc_qname(q), jiffies);
  308. if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
  309. printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
  310. return;
  311. }
  312. __rpc_add_wait_queue(q, task);
  313. BUG_ON(task->tk_callback != NULL);
  314. task->tk_callback = action;
  315. __rpc_add_timer(task, timer);
  316. }
  317. void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  318. rpc_action action, rpc_action timer)
  319. {
  320. /* Mark the task as being activated if so needed */
  321. rpc_set_active(task);
  322. /*
  323. * Protect the queue operations.
  324. */
  325. spin_lock_bh(&q->lock);
  326. __rpc_sleep_on(q, task, action, timer);
  327. spin_unlock_bh(&q->lock);
  328. }
  329. /**
  330. * __rpc_do_wake_up_task - wake up a single rpc_task
  331. * @task: task to be woken up
  332. *
  333. * Caller must hold queue->lock, and have cleared the task queued flag.
  334. */
  335. static void __rpc_do_wake_up_task(struct rpc_task *task)
  336. {
  337. dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
  338. #ifdef RPC_DEBUG
  339. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  340. #endif
  341. /* Has the task been executed yet? If not, we cannot wake it up! */
  342. if (!RPC_IS_ACTIVATED(task)) {
  343. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  344. return;
  345. }
  346. __rpc_disable_timer(task);
  347. __rpc_remove_wait_queue(task);
  348. rpc_make_runnable(task);
  349. dprintk("RPC: __rpc_wake_up_task done\n");
  350. }
  351. /*
  352. * Wake up the specified task
  353. */
  354. static void __rpc_wake_up_task(struct rpc_task *task)
  355. {
  356. if (rpc_start_wakeup(task)) {
  357. if (RPC_IS_QUEUED(task))
  358. __rpc_do_wake_up_task(task);
  359. rpc_finish_wakeup(task);
  360. }
  361. }
  362. /*
  363. * Default timeout handler if none specified by user
  364. */
  365. static void
  366. __rpc_default_timer(struct rpc_task *task)
  367. {
  368. dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
  369. task->tk_status = -ETIMEDOUT;
  370. rpc_wake_up_task(task);
  371. }
  372. /*
  373. * Wake up the specified task
  374. */
  375. void rpc_wake_up_task(struct rpc_task *task)
  376. {
  377. rcu_read_lock_bh();
  378. if (rpc_start_wakeup(task)) {
  379. if (RPC_IS_QUEUED(task)) {
  380. struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
  381. /* Note: we're already in a bh-safe context */
  382. spin_lock(&queue->lock);
  383. __rpc_do_wake_up_task(task);
  384. spin_unlock(&queue->lock);
  385. }
  386. rpc_finish_wakeup(task);
  387. }
  388. rcu_read_unlock_bh();
  389. }
  390. /*
  391. * Wake up the next task on a priority queue.
  392. */
  393. static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
  394. {
  395. struct list_head *q;
  396. struct rpc_task *task;
  397. /*
  398. * Service a batch of tasks from a single cookie.
  399. */
  400. q = &queue->tasks[queue->priority];
  401. if (!list_empty(q)) {
  402. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  403. if (queue->cookie == task->tk_cookie) {
  404. if (--queue->nr)
  405. goto out;
  406. list_move_tail(&task->u.tk_wait.list, q);
  407. }
  408. /*
  409. * Check if we need to switch queues.
  410. */
  411. if (--queue->count)
  412. goto new_cookie;
  413. }
  414. /*
  415. * Service the next queue.
  416. */
  417. do {
  418. if (q == &queue->tasks[0])
  419. q = &queue->tasks[queue->maxpriority];
  420. else
  421. q = q - 1;
  422. if (!list_empty(q)) {
  423. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  424. goto new_queue;
  425. }
  426. } while (q != &queue->tasks[queue->priority]);
  427. rpc_reset_waitqueue_priority(queue);
  428. return NULL;
  429. new_queue:
  430. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  431. new_cookie:
  432. rpc_set_waitqueue_cookie(queue, task->tk_cookie);
  433. out:
  434. __rpc_wake_up_task(task);
  435. return task;
  436. }
  437. /*
  438. * Wake up the next task on the wait queue.
  439. */
  440. struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
  441. {
  442. struct rpc_task *task = NULL;
  443. dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
  444. rcu_read_lock_bh();
  445. spin_lock(&queue->lock);
  446. if (RPC_IS_PRIORITY(queue))
  447. task = __rpc_wake_up_next_priority(queue);
  448. else {
  449. task_for_first(task, &queue->tasks[0])
  450. __rpc_wake_up_task(task);
  451. }
  452. spin_unlock(&queue->lock);
  453. rcu_read_unlock_bh();
  454. return task;
  455. }
  456. /**
  457. * rpc_wake_up - wake up all rpc_tasks
  458. * @queue: rpc_wait_queue on which the tasks are sleeping
  459. *
  460. * Grabs queue->lock
  461. */
  462. void rpc_wake_up(struct rpc_wait_queue *queue)
  463. {
  464. struct rpc_task *task, *next;
  465. struct list_head *head;
  466. rcu_read_lock_bh();
  467. spin_lock(&queue->lock);
  468. head = &queue->tasks[queue->maxpriority];
  469. for (;;) {
  470. list_for_each_entry_safe(task, next, head, u.tk_wait.list)
  471. __rpc_wake_up_task(task);
  472. if (head == &queue->tasks[0])
  473. break;
  474. head--;
  475. }
  476. spin_unlock(&queue->lock);
  477. rcu_read_unlock_bh();
  478. }
  479. /**
  480. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  481. * @queue: rpc_wait_queue on which the tasks are sleeping
  482. * @status: status value to set
  483. *
  484. * Grabs queue->lock
  485. */
  486. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  487. {
  488. struct rpc_task *task, *next;
  489. struct list_head *head;
  490. rcu_read_lock_bh();
  491. spin_lock(&queue->lock);
  492. head = &queue->tasks[queue->maxpriority];
  493. for (;;) {
  494. list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
  495. task->tk_status = status;
  496. __rpc_wake_up_task(task);
  497. }
  498. if (head == &queue->tasks[0])
  499. break;
  500. head--;
  501. }
  502. spin_unlock(&queue->lock);
  503. rcu_read_unlock_bh();
  504. }
  505. static void __rpc_atrun(struct rpc_task *task)
  506. {
  507. rpc_wake_up_task(task);
  508. }
  509. /*
  510. * Run a task at a later time
  511. */
  512. void rpc_delay(struct rpc_task *task, unsigned long delay)
  513. {
  514. task->tk_timeout = delay;
  515. rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
  516. }
  517. /*
  518. * Helper to call task->tk_ops->rpc_call_prepare
  519. */
  520. static void rpc_prepare_task(struct rpc_task *task)
  521. {
  522. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  523. }
  524. /*
  525. * Helper that calls task->tk_ops->rpc_call_done if it exists
  526. */
  527. void rpc_exit_task(struct rpc_task *task)
  528. {
  529. task->tk_action = NULL;
  530. if (task->tk_ops->rpc_call_done != NULL) {
  531. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  532. if (task->tk_action != NULL) {
  533. WARN_ON(RPC_ASSASSINATED(task));
  534. /* Always release the RPC slot and buffer memory */
  535. xprt_release(task);
  536. }
  537. }
  538. }
  539. EXPORT_SYMBOL(rpc_exit_task);
  540. /*
  541. * This is the RPC `scheduler' (or rather, the finite state machine).
  542. */
  543. static int __rpc_execute(struct rpc_task *task)
  544. {
  545. int status = 0;
  546. dprintk("RPC: %4d rpc_execute flgs %x\n",
  547. task->tk_pid, task->tk_flags);
  548. BUG_ON(RPC_IS_QUEUED(task));
  549. for (;;) {
  550. /*
  551. * Garbage collection of pending timers...
  552. */
  553. rpc_delete_timer(task);
  554. /*
  555. * Execute any pending callback.
  556. */
  557. if (RPC_DO_CALLBACK(task)) {
  558. /* Define a callback save pointer */
  559. void (*save_callback)(struct rpc_task *);
  560. /*
  561. * If a callback exists, save it, reset it,
  562. * call it.
  563. * The save is needed to stop from resetting
  564. * another callback set within the callback handler
  565. * - Dave
  566. */
  567. save_callback=task->tk_callback;
  568. task->tk_callback=NULL;
  569. lock_kernel();
  570. save_callback(task);
  571. unlock_kernel();
  572. }
  573. /*
  574. * Perform the next FSM step.
  575. * tk_action may be NULL when the task has been killed
  576. * by someone else.
  577. */
  578. if (!RPC_IS_QUEUED(task)) {
  579. if (task->tk_action == NULL)
  580. break;
  581. lock_kernel();
  582. task->tk_action(task);
  583. unlock_kernel();
  584. }
  585. /*
  586. * Lockless check for whether task is sleeping or not.
  587. */
  588. if (!RPC_IS_QUEUED(task))
  589. continue;
  590. rpc_clear_running(task);
  591. if (RPC_IS_ASYNC(task)) {
  592. /* Careful! we may have raced... */
  593. if (RPC_IS_QUEUED(task))
  594. return 0;
  595. if (rpc_test_and_set_running(task))
  596. return 0;
  597. continue;
  598. }
  599. /* sync task: sleep here */
  600. dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
  601. /* Note: Caller should be using rpc_clnt_sigmask() */
  602. status = out_of_line_wait_on_bit(&task->tk_runstate,
  603. RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
  604. TASK_INTERRUPTIBLE);
  605. if (status == -ERESTARTSYS) {
  606. /*
  607. * When a sync task receives a signal, it exits with
  608. * -ERESTARTSYS. In order to catch any callbacks that
  609. * clean up after sleeping on some queue, we don't
  610. * break the loop here, but go around once more.
  611. */
  612. dprintk("RPC: %4d got signal\n", task->tk_pid);
  613. task->tk_flags |= RPC_TASK_KILLED;
  614. rpc_exit(task, -ERESTARTSYS);
  615. rpc_wake_up_task(task);
  616. }
  617. rpc_set_running(task);
  618. dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
  619. }
  620. dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
  621. /* Release all resources associated with the task */
  622. rpc_release_task(task);
  623. return status;
  624. }
  625. /*
  626. * User-visible entry point to the scheduler.
  627. *
  628. * This may be called recursively if e.g. an async NFS task updates
  629. * the attributes and finds that dirty pages must be flushed.
  630. * NOTE: Upon exit of this function the task is guaranteed to be
  631. * released. In particular note that tk_release() will have
  632. * been called, so your task memory may have been freed.
  633. */
  634. int
  635. rpc_execute(struct rpc_task *task)
  636. {
  637. rpc_set_active(task);
  638. rpc_set_running(task);
  639. return __rpc_execute(task);
  640. }
  641. static void rpc_async_schedule(void *arg)
  642. {
  643. __rpc_execute((struct rpc_task *)arg);
  644. }
  645. /**
  646. * rpc_malloc - allocate an RPC buffer
  647. * @task: RPC task that will use this buffer
  648. * @size: requested byte size
  649. *
  650. * We try to ensure that some NFS reads and writes can always proceed
  651. * by using a mempool when allocating 'small' buffers.
  652. * In order to avoid memory starvation triggering more writebacks of
  653. * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
  654. */
  655. void * rpc_malloc(struct rpc_task *task, size_t size)
  656. {
  657. struct rpc_rqst *req = task->tk_rqstp;
  658. gfp_t gfp;
  659. if (task->tk_flags & RPC_TASK_SWAPPER)
  660. gfp = GFP_ATOMIC;
  661. else
  662. gfp = GFP_NOFS;
  663. if (size > RPC_BUFFER_MAXSIZE) {
  664. req->rq_buffer = kmalloc(size, gfp);
  665. if (req->rq_buffer)
  666. req->rq_bufsize = size;
  667. } else {
  668. req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
  669. if (req->rq_buffer)
  670. req->rq_bufsize = RPC_BUFFER_MAXSIZE;
  671. }
  672. return req->rq_buffer;
  673. }
  674. /**
  675. * rpc_free - free buffer allocated via rpc_malloc
  676. * @task: RPC task with a buffer to be freed
  677. *
  678. */
  679. void rpc_free(struct rpc_task *task)
  680. {
  681. struct rpc_rqst *req = task->tk_rqstp;
  682. if (req->rq_buffer) {
  683. if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
  684. mempool_free(req->rq_buffer, rpc_buffer_mempool);
  685. else
  686. kfree(req->rq_buffer);
  687. req->rq_buffer = NULL;
  688. req->rq_bufsize = 0;
  689. }
  690. }
  691. /*
  692. * Creation and deletion of RPC task structures
  693. */
  694. void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  695. {
  696. memset(task, 0, sizeof(*task));
  697. init_timer(&task->tk_timer);
  698. task->tk_timer.data = (unsigned long) task;
  699. task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
  700. atomic_set(&task->tk_count, 1);
  701. task->tk_client = clnt;
  702. task->tk_flags = flags;
  703. task->tk_ops = tk_ops;
  704. if (tk_ops->rpc_call_prepare != NULL)
  705. task->tk_action = rpc_prepare_task;
  706. task->tk_calldata = calldata;
  707. /* Initialize retry counters */
  708. task->tk_garb_retry = 2;
  709. task->tk_cred_retry = 2;
  710. task->tk_priority = RPC_PRIORITY_NORMAL;
  711. task->tk_cookie = (unsigned long)current;
  712. /* Initialize workqueue for async tasks */
  713. task->tk_workqueue = rpciod_workqueue;
  714. if (clnt) {
  715. atomic_inc(&clnt->cl_users);
  716. if (clnt->cl_softrtry)
  717. task->tk_flags |= RPC_TASK_SOFT;
  718. if (!clnt->cl_intr)
  719. task->tk_flags |= RPC_TASK_NOINTR;
  720. }
  721. BUG_ON(task->tk_ops == NULL);
  722. /* starting timestamp */
  723. task->tk_start = jiffies;
  724. dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
  725. current->pid);
  726. }
  727. static struct rpc_task *
  728. rpc_alloc_task(void)
  729. {
  730. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  731. }
  732. static void rpc_free_task(struct rcu_head *rcu)
  733. {
  734. struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
  735. dprintk("RPC: %4d freeing task\n", task->tk_pid);
  736. mempool_free(task, rpc_task_mempool);
  737. }
  738. /*
  739. * Create a new task for the specified client. We have to
  740. * clean up after an allocation failure, as the client may
  741. * have specified "oneshot".
  742. */
  743. struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  744. {
  745. struct rpc_task *task;
  746. task = rpc_alloc_task();
  747. if (!task)
  748. goto cleanup;
  749. rpc_init_task(task, clnt, flags, tk_ops, calldata);
  750. dprintk("RPC: %4d allocated task\n", task->tk_pid);
  751. task->tk_flags |= RPC_TASK_DYNAMIC;
  752. out:
  753. return task;
  754. cleanup:
  755. /* Check whether to release the client */
  756. if (clnt) {
  757. printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
  758. atomic_read(&clnt->cl_users), clnt->cl_oneshot);
  759. atomic_inc(&clnt->cl_users); /* pretend we were used ... */
  760. rpc_release_client(clnt);
  761. }
  762. goto out;
  763. }
  764. void rpc_put_task(struct rpc_task *task)
  765. {
  766. const struct rpc_call_ops *tk_ops = task->tk_ops;
  767. void *calldata = task->tk_calldata;
  768. if (!atomic_dec_and_test(&task->tk_count))
  769. return;
  770. /* Release resources */
  771. if (task->tk_rqstp)
  772. xprt_release(task);
  773. if (task->tk_msg.rpc_cred)
  774. rpcauth_unbindcred(task);
  775. if (task->tk_client) {
  776. rpc_release_client(task->tk_client);
  777. task->tk_client = NULL;
  778. }
  779. if (task->tk_flags & RPC_TASK_DYNAMIC)
  780. call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
  781. if (tk_ops->rpc_release)
  782. tk_ops->rpc_release(calldata);
  783. }
  784. EXPORT_SYMBOL(rpc_put_task);
  785. void rpc_release_task(struct rpc_task *task)
  786. {
  787. #ifdef RPC_DEBUG
  788. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  789. #endif
  790. dprintk("RPC: %4d release task\n", task->tk_pid);
  791. /* Remove from global task list */
  792. spin_lock(&rpc_sched_lock);
  793. list_del(&task->tk_task);
  794. spin_unlock(&rpc_sched_lock);
  795. BUG_ON (RPC_IS_QUEUED(task));
  796. /* Synchronously delete any running timer */
  797. rpc_delete_timer(task);
  798. #ifdef RPC_DEBUG
  799. task->tk_magic = 0;
  800. #endif
  801. /* Wake up anyone who is waiting for task completion */
  802. rpc_mark_complete_task(task);
  803. rpc_put_task(task);
  804. }
  805. /**
  806. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  807. * @clnt: pointer to RPC client
  808. * @flags: RPC flags
  809. * @ops: RPC call ops
  810. * @data: user call data
  811. */
  812. struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
  813. const struct rpc_call_ops *ops,
  814. void *data)
  815. {
  816. struct rpc_task *task;
  817. task = rpc_new_task(clnt, flags, ops, data);
  818. if (task == NULL) {
  819. if (ops->rpc_release != NULL)
  820. ops->rpc_release(data);
  821. return ERR_PTR(-ENOMEM);
  822. }
  823. atomic_inc(&task->tk_count);
  824. rpc_execute(task);
  825. return task;
  826. }
  827. EXPORT_SYMBOL(rpc_run_task);
  828. /*
  829. * Kill all tasks for the given client.
  830. * XXX: kill their descendants as well?
  831. */
  832. void rpc_killall_tasks(struct rpc_clnt *clnt)
  833. {
  834. struct rpc_task *rovr;
  835. struct list_head *le;
  836. dprintk("RPC: killing all tasks for client %p\n", clnt);
  837. /*
  838. * Spin lock all_tasks to prevent changes...
  839. */
  840. spin_lock(&rpc_sched_lock);
  841. alltask_for_each(rovr, le, &all_tasks) {
  842. if (! RPC_IS_ACTIVATED(rovr))
  843. continue;
  844. if (!clnt || rovr->tk_client == clnt) {
  845. rovr->tk_flags |= RPC_TASK_KILLED;
  846. rpc_exit(rovr, -EIO);
  847. rpc_wake_up_task(rovr);
  848. }
  849. }
  850. spin_unlock(&rpc_sched_lock);
  851. }
  852. static DECLARE_MUTEX_LOCKED(rpciod_running);
  853. static void rpciod_killall(void)
  854. {
  855. unsigned long flags;
  856. while (!list_empty(&all_tasks)) {
  857. clear_thread_flag(TIF_SIGPENDING);
  858. rpc_killall_tasks(NULL);
  859. flush_workqueue(rpciod_workqueue);
  860. if (!list_empty(&all_tasks)) {
  861. dprintk("rpciod_killall: waiting for tasks to exit\n");
  862. yield();
  863. }
  864. }
  865. spin_lock_irqsave(&current->sighand->siglock, flags);
  866. recalc_sigpending();
  867. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  868. }
  869. /*
  870. * Start up the rpciod process if it's not already running.
  871. */
  872. int
  873. rpciod_up(void)
  874. {
  875. struct workqueue_struct *wq;
  876. int error = 0;
  877. mutex_lock(&rpciod_mutex);
  878. dprintk("rpciod_up: users %d\n", rpciod_users);
  879. rpciod_users++;
  880. if (rpciod_workqueue)
  881. goto out;
  882. /*
  883. * If there's no pid, we should be the first user.
  884. */
  885. if (rpciod_users > 1)
  886. printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
  887. /*
  888. * Create the rpciod thread and wait for it to start.
  889. */
  890. error = -ENOMEM;
  891. wq = create_workqueue("rpciod");
  892. if (wq == NULL) {
  893. printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
  894. rpciod_users--;
  895. goto out;
  896. }
  897. rpciod_workqueue = wq;
  898. error = 0;
  899. out:
  900. mutex_unlock(&rpciod_mutex);
  901. return error;
  902. }
  903. void
  904. rpciod_down(void)
  905. {
  906. mutex_lock(&rpciod_mutex);
  907. dprintk("rpciod_down sema %d\n", rpciod_users);
  908. if (rpciod_users) {
  909. if (--rpciod_users)
  910. goto out;
  911. } else
  912. printk(KERN_WARNING "rpciod_down: no users??\n");
  913. if (!rpciod_workqueue) {
  914. dprintk("rpciod_down: Nothing to do!\n");
  915. goto out;
  916. }
  917. rpciod_killall();
  918. destroy_workqueue(rpciod_workqueue);
  919. rpciod_workqueue = NULL;
  920. out:
  921. mutex_unlock(&rpciod_mutex);
  922. }
  923. #ifdef RPC_DEBUG
  924. void rpc_show_tasks(void)
  925. {
  926. struct list_head *le;
  927. struct rpc_task *t;
  928. spin_lock(&rpc_sched_lock);
  929. if (list_empty(&all_tasks)) {
  930. spin_unlock(&rpc_sched_lock);
  931. return;
  932. }
  933. printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
  934. "-rpcwait -action- ---ops--\n");
  935. alltask_for_each(t, le, &all_tasks) {
  936. const char *rpc_waitq = "none";
  937. if (RPC_IS_QUEUED(t))
  938. rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
  939. printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
  940. t->tk_pid,
  941. (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
  942. t->tk_flags, t->tk_status,
  943. t->tk_client,
  944. (t->tk_client ? t->tk_client->cl_prog : 0),
  945. t->tk_rqstp, t->tk_timeout,
  946. rpc_waitq,
  947. t->tk_action, t->tk_ops);
  948. }
  949. spin_unlock(&rpc_sched_lock);
  950. }
  951. #endif
  952. void
  953. rpc_destroy_mempool(void)
  954. {
  955. if (rpc_buffer_mempool)
  956. mempool_destroy(rpc_buffer_mempool);
  957. if (rpc_task_mempool)
  958. mempool_destroy(rpc_task_mempool);
  959. if (rpc_task_slabp)
  960. kmem_cache_destroy(rpc_task_slabp);
  961. if (rpc_buffer_slabp)
  962. kmem_cache_destroy(rpc_buffer_slabp);
  963. }
  964. int
  965. rpc_init_mempool(void)
  966. {
  967. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  968. sizeof(struct rpc_task),
  969. 0, SLAB_HWCACHE_ALIGN,
  970. NULL, NULL);
  971. if (!rpc_task_slabp)
  972. goto err_nomem;
  973. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  974. RPC_BUFFER_MAXSIZE,
  975. 0, SLAB_HWCACHE_ALIGN,
  976. NULL, NULL);
  977. if (!rpc_buffer_slabp)
  978. goto err_nomem;
  979. rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
  980. rpc_task_slabp);
  981. if (!rpc_task_mempool)
  982. goto err_nomem;
  983. rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
  984. rpc_buffer_slabp);
  985. if (!rpc_buffer_mempool)
  986. goto err_nomem;
  987. return 0;
  988. err_nomem:
  989. rpc_destroy_mempool();
  990. return -ENOMEM;
  991. }