sched.c 26 KB

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