sched.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. #include <linux/sunrpc/xprt.h>
  22. #ifdef RPC_DEBUG
  23. #define RPCDBG_FACILITY RPCDBG_SCHED
  24. #define RPC_TASK_MAGIC_ID 0xf00baa
  25. static int rpc_task_id;
  26. #endif
  27. /*
  28. * RPC slabs and memory pools
  29. */
  30. #define RPC_BUFFER_MAXSIZE (2048)
  31. #define RPC_BUFFER_POOLSIZE (8)
  32. #define RPC_TASK_POOLSIZE (8)
  33. static kmem_cache_t *rpc_task_slabp __read_mostly;
  34. static kmem_cache_t *rpc_buffer_slabp __read_mostly;
  35. static mempool_t *rpc_task_mempool __read_mostly;
  36. static mempool_t *rpc_buffer_mempool __read_mostly;
  37. static void __rpc_default_timer(struct rpc_task *task);
  38. static void rpciod_killall(void);
  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 DEFINE_MUTEX(rpciod_mutex);
  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. static int rpc_wait_bit_interruptible(void *word)
  232. {
  233. if (signal_pending(current))
  234. return -ERESTARTSYS;
  235. schedule();
  236. return 0;
  237. }
  238. /*
  239. * Mark an RPC call as having completed by clearing the 'active' bit
  240. */
  241. static inline void rpc_mark_complete_task(struct rpc_task *task)
  242. {
  243. rpc_clear_active(task);
  244. wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
  245. }
  246. /*
  247. * Allow callers to wait for completion of an RPC call
  248. */
  249. int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
  250. {
  251. if (action == NULL)
  252. action = rpc_wait_bit_interruptible;
  253. return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
  254. action, TASK_INTERRUPTIBLE);
  255. }
  256. EXPORT_SYMBOL(__rpc_wait_for_completion_task);
  257. /*
  258. * Make an RPC task runnable.
  259. *
  260. * Note: If the task is ASYNC, this must be called with
  261. * the spinlock held to protect the wait queue operation.
  262. */
  263. static void rpc_make_runnable(struct rpc_task *task)
  264. {
  265. int do_ret;
  266. BUG_ON(task->tk_timeout_fn);
  267. do_ret = rpc_test_and_set_running(task);
  268. rpc_clear_queued(task);
  269. if (do_ret)
  270. return;
  271. if (RPC_IS_ASYNC(task)) {
  272. int status;
  273. INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
  274. status = queue_work(task->tk_workqueue, &task->u.tk_work);
  275. if (status < 0) {
  276. printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
  277. task->tk_status = status;
  278. return;
  279. }
  280. } else
  281. wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
  282. }
  283. /*
  284. * Place a newly initialized task on the workqueue.
  285. */
  286. static inline void
  287. rpc_schedule_run(struct rpc_task *task)
  288. {
  289. rpc_set_active(task);
  290. rpc_make_runnable(task);
  291. }
  292. /*
  293. * Prepare for sleeping on a wait queue.
  294. * By always appending tasks to the list we ensure FIFO behavior.
  295. * NB: An RPC task will only receive interrupt-driven events as long
  296. * as it's on a wait queue.
  297. */
  298. static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  299. rpc_action action, rpc_action timer)
  300. {
  301. dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
  302. rpc_qname(q), jiffies);
  303. if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
  304. printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
  305. return;
  306. }
  307. /* Mark the task as being activated if so needed */
  308. rpc_set_active(task);
  309. __rpc_add_wait_queue(q, task);
  310. BUG_ON(task->tk_callback != NULL);
  311. task->tk_callback = action;
  312. __rpc_add_timer(task, timer);
  313. }
  314. void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
  315. rpc_action action, rpc_action timer)
  316. {
  317. /*
  318. * Protect the queue operations.
  319. */
  320. spin_lock_bh(&q->lock);
  321. __rpc_sleep_on(q, task, action, timer);
  322. spin_unlock_bh(&q->lock);
  323. }
  324. /**
  325. * __rpc_do_wake_up_task - wake up a single rpc_task
  326. * @task: task to be woken up
  327. *
  328. * Caller must hold queue->lock, and have cleared the task queued flag.
  329. */
  330. static void __rpc_do_wake_up_task(struct rpc_task *task)
  331. {
  332. dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
  333. #ifdef RPC_DEBUG
  334. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  335. #endif
  336. /* Has the task been executed yet? If not, we cannot wake it up! */
  337. if (!RPC_IS_ACTIVATED(task)) {
  338. printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
  339. return;
  340. }
  341. __rpc_disable_timer(task);
  342. __rpc_remove_wait_queue(task);
  343. rpc_make_runnable(task);
  344. dprintk("RPC: __rpc_wake_up_task done\n");
  345. }
  346. /*
  347. * Wake up the specified task
  348. */
  349. static void __rpc_wake_up_task(struct rpc_task *task)
  350. {
  351. if (rpc_start_wakeup(task)) {
  352. if (RPC_IS_QUEUED(task))
  353. __rpc_do_wake_up_task(task);
  354. rpc_finish_wakeup(task);
  355. }
  356. }
  357. /*
  358. * Default timeout handler if none specified by user
  359. */
  360. static void
  361. __rpc_default_timer(struct rpc_task *task)
  362. {
  363. dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
  364. task->tk_status = -ETIMEDOUT;
  365. rpc_wake_up_task(task);
  366. }
  367. /*
  368. * Wake up the specified task
  369. */
  370. void rpc_wake_up_task(struct rpc_task *task)
  371. {
  372. if (rpc_start_wakeup(task)) {
  373. if (RPC_IS_QUEUED(task)) {
  374. struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
  375. spin_lock_bh(&queue->lock);
  376. __rpc_do_wake_up_task(task);
  377. spin_unlock_bh(&queue->lock);
  378. }
  379. rpc_finish_wakeup(task);
  380. }
  381. }
  382. /*
  383. * Wake up the next task on a priority queue.
  384. */
  385. static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
  386. {
  387. struct list_head *q;
  388. struct rpc_task *task;
  389. /*
  390. * Service a batch of tasks from a single cookie.
  391. */
  392. q = &queue->tasks[queue->priority];
  393. if (!list_empty(q)) {
  394. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  395. if (queue->cookie == task->tk_cookie) {
  396. if (--queue->nr)
  397. goto out;
  398. list_move_tail(&task->u.tk_wait.list, q);
  399. }
  400. /*
  401. * Check if we need to switch queues.
  402. */
  403. if (--queue->count)
  404. goto new_cookie;
  405. }
  406. /*
  407. * Service the next queue.
  408. */
  409. do {
  410. if (q == &queue->tasks[0])
  411. q = &queue->tasks[queue->maxpriority];
  412. else
  413. q = q - 1;
  414. if (!list_empty(q)) {
  415. task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
  416. goto new_queue;
  417. }
  418. } while (q != &queue->tasks[queue->priority]);
  419. rpc_reset_waitqueue_priority(queue);
  420. return NULL;
  421. new_queue:
  422. rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
  423. new_cookie:
  424. rpc_set_waitqueue_cookie(queue, task->tk_cookie);
  425. out:
  426. __rpc_wake_up_task(task);
  427. return task;
  428. }
  429. /*
  430. * Wake up the next task on the wait queue.
  431. */
  432. struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
  433. {
  434. struct rpc_task *task = NULL;
  435. dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
  436. spin_lock_bh(&queue->lock);
  437. if (RPC_IS_PRIORITY(queue))
  438. task = __rpc_wake_up_next_priority(queue);
  439. else {
  440. task_for_first(task, &queue->tasks[0])
  441. __rpc_wake_up_task(task);
  442. }
  443. spin_unlock_bh(&queue->lock);
  444. return task;
  445. }
  446. /**
  447. * rpc_wake_up - wake up all rpc_tasks
  448. * @queue: rpc_wait_queue on which the tasks are sleeping
  449. *
  450. * Grabs queue->lock
  451. */
  452. void rpc_wake_up(struct rpc_wait_queue *queue)
  453. {
  454. struct rpc_task *task, *next;
  455. struct list_head *head;
  456. spin_lock_bh(&queue->lock);
  457. head = &queue->tasks[queue->maxpriority];
  458. for (;;) {
  459. list_for_each_entry_safe(task, next, head, u.tk_wait.list)
  460. __rpc_wake_up_task(task);
  461. if (head == &queue->tasks[0])
  462. break;
  463. head--;
  464. }
  465. spin_unlock_bh(&queue->lock);
  466. }
  467. /**
  468. * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
  469. * @queue: rpc_wait_queue on which the tasks are sleeping
  470. * @status: status value to set
  471. *
  472. * Grabs queue->lock
  473. */
  474. void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
  475. {
  476. struct rpc_task *task, *next;
  477. struct list_head *head;
  478. spin_lock_bh(&queue->lock);
  479. head = &queue->tasks[queue->maxpriority];
  480. for (;;) {
  481. list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
  482. task->tk_status = status;
  483. __rpc_wake_up_task(task);
  484. }
  485. if (head == &queue->tasks[0])
  486. break;
  487. head--;
  488. }
  489. spin_unlock_bh(&queue->lock);
  490. }
  491. /*
  492. * Run a task at a later time
  493. */
  494. static void __rpc_atrun(struct rpc_task *);
  495. void
  496. rpc_delay(struct rpc_task *task, unsigned long delay)
  497. {
  498. task->tk_timeout = delay;
  499. rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
  500. }
  501. static void
  502. __rpc_atrun(struct rpc_task *task)
  503. {
  504. task->tk_status = 0;
  505. rpc_wake_up_task(task);
  506. }
  507. /*
  508. * Helper to call task->tk_ops->rpc_call_prepare
  509. */
  510. static void rpc_prepare_task(struct rpc_task *task)
  511. {
  512. task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
  513. }
  514. /*
  515. * Helper that calls task->tk_ops->rpc_call_done if it exists
  516. */
  517. void rpc_exit_task(struct rpc_task *task)
  518. {
  519. task->tk_action = NULL;
  520. if (task->tk_ops->rpc_call_done != NULL) {
  521. task->tk_ops->rpc_call_done(task, task->tk_calldata);
  522. if (task->tk_action != NULL) {
  523. WARN_ON(RPC_ASSASSINATED(task));
  524. /* Always release the RPC slot and buffer memory */
  525. xprt_release(task);
  526. }
  527. }
  528. }
  529. EXPORT_SYMBOL(rpc_exit_task);
  530. /*
  531. * This is the RPC `scheduler' (or rather, the finite state machine).
  532. */
  533. static int __rpc_execute(struct rpc_task *task)
  534. {
  535. int status = 0;
  536. dprintk("RPC: %4d rpc_execute flgs %x\n",
  537. task->tk_pid, task->tk_flags);
  538. BUG_ON(RPC_IS_QUEUED(task));
  539. for (;;) {
  540. /*
  541. * Garbage collection of pending timers...
  542. */
  543. rpc_delete_timer(task);
  544. /*
  545. * Execute any pending callback.
  546. */
  547. if (RPC_DO_CALLBACK(task)) {
  548. /* Define a callback save pointer */
  549. void (*save_callback)(struct rpc_task *);
  550. /*
  551. * If a callback exists, save it, reset it,
  552. * call it.
  553. * The save is needed to stop from resetting
  554. * another callback set within the callback handler
  555. * - Dave
  556. */
  557. save_callback=task->tk_callback;
  558. task->tk_callback=NULL;
  559. lock_kernel();
  560. save_callback(task);
  561. unlock_kernel();
  562. }
  563. /*
  564. * Perform the next FSM step.
  565. * tk_action may be NULL when the task has been killed
  566. * by someone else.
  567. */
  568. if (!RPC_IS_QUEUED(task)) {
  569. if (task->tk_action == NULL)
  570. break;
  571. lock_kernel();
  572. task->tk_action(task);
  573. unlock_kernel();
  574. }
  575. /*
  576. * Lockless check for whether task is sleeping or not.
  577. */
  578. if (!RPC_IS_QUEUED(task))
  579. continue;
  580. rpc_clear_running(task);
  581. if (RPC_IS_ASYNC(task)) {
  582. /* Careful! we may have raced... */
  583. if (RPC_IS_QUEUED(task))
  584. return 0;
  585. if (rpc_test_and_set_running(task))
  586. return 0;
  587. continue;
  588. }
  589. /* sync task: sleep here */
  590. dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
  591. /* Note: Caller should be using rpc_clnt_sigmask() */
  592. status = out_of_line_wait_on_bit(&task->tk_runstate,
  593. RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
  594. TASK_INTERRUPTIBLE);
  595. if (status == -ERESTARTSYS) {
  596. /*
  597. * When a sync task receives a signal, it exits with
  598. * -ERESTARTSYS. In order to catch any callbacks that
  599. * clean up after sleeping on some queue, we don't
  600. * break the loop here, but go around once more.
  601. */
  602. dprintk("RPC: %4d got signal\n", task->tk_pid);
  603. task->tk_flags |= RPC_TASK_KILLED;
  604. rpc_exit(task, -ERESTARTSYS);
  605. rpc_wake_up_task(task);
  606. }
  607. rpc_set_running(task);
  608. dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
  609. }
  610. dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
  611. /* Wake up anyone who is waiting for task completion */
  612. rpc_mark_complete_task(task);
  613. /* Release all resources associated with the task */
  614. rpc_release_task(task);
  615. return status;
  616. }
  617. /*
  618. * User-visible entry point to the scheduler.
  619. *
  620. * This may be called recursively if e.g. an async NFS task updates
  621. * the attributes and finds that dirty pages must be flushed.
  622. * NOTE: Upon exit of this function the task is guaranteed to be
  623. * released. In particular note that tk_release() will have
  624. * been called, so your task memory may have been freed.
  625. */
  626. int
  627. rpc_execute(struct rpc_task *task)
  628. {
  629. rpc_set_active(task);
  630. rpc_set_running(task);
  631. return __rpc_execute(task);
  632. }
  633. static void rpc_async_schedule(void *arg)
  634. {
  635. __rpc_execute((struct rpc_task *)arg);
  636. }
  637. /**
  638. * rpc_malloc - allocate an RPC buffer
  639. * @task: RPC task that will use this buffer
  640. * @size: requested byte size
  641. *
  642. * We try to ensure that some NFS reads and writes can always proceed
  643. * by using a mempool when allocating 'small' buffers.
  644. * In order to avoid memory starvation triggering more writebacks of
  645. * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
  646. */
  647. void * rpc_malloc(struct rpc_task *task, size_t size)
  648. {
  649. struct rpc_rqst *req = task->tk_rqstp;
  650. gfp_t gfp;
  651. if (task->tk_flags & RPC_TASK_SWAPPER)
  652. gfp = GFP_ATOMIC;
  653. else
  654. gfp = GFP_NOFS;
  655. if (size > RPC_BUFFER_MAXSIZE) {
  656. req->rq_buffer = kmalloc(size, gfp);
  657. if (req->rq_buffer)
  658. req->rq_bufsize = size;
  659. } else {
  660. req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
  661. if (req->rq_buffer)
  662. req->rq_bufsize = RPC_BUFFER_MAXSIZE;
  663. }
  664. return req->rq_buffer;
  665. }
  666. /**
  667. * rpc_free - free buffer allocated via rpc_malloc
  668. * @task: RPC task with a buffer to be freed
  669. *
  670. */
  671. void rpc_free(struct rpc_task *task)
  672. {
  673. struct rpc_rqst *req = task->tk_rqstp;
  674. if (req->rq_buffer) {
  675. if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
  676. mempool_free(req->rq_buffer, rpc_buffer_mempool);
  677. else
  678. kfree(req->rq_buffer);
  679. req->rq_buffer = NULL;
  680. req->rq_bufsize = 0;
  681. }
  682. }
  683. /*
  684. * Creation and deletion of RPC task structures
  685. */
  686. void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  687. {
  688. memset(task, 0, sizeof(*task));
  689. init_timer(&task->tk_timer);
  690. task->tk_timer.data = (unsigned long) task;
  691. task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
  692. atomic_set(&task->tk_count, 1);
  693. task->tk_client = clnt;
  694. task->tk_flags = flags;
  695. task->tk_ops = tk_ops;
  696. if (tk_ops->rpc_call_prepare != NULL)
  697. task->tk_action = rpc_prepare_task;
  698. task->tk_calldata = calldata;
  699. /* Initialize retry counters */
  700. task->tk_garb_retry = 2;
  701. task->tk_cred_retry = 2;
  702. task->tk_priority = RPC_PRIORITY_NORMAL;
  703. task->tk_cookie = (unsigned long)current;
  704. /* Initialize workqueue for async tasks */
  705. task->tk_workqueue = rpciod_workqueue;
  706. if (clnt) {
  707. atomic_inc(&clnt->cl_users);
  708. if (clnt->cl_softrtry)
  709. task->tk_flags |= RPC_TASK_SOFT;
  710. if (!clnt->cl_intr)
  711. task->tk_flags |= RPC_TASK_NOINTR;
  712. }
  713. #ifdef RPC_DEBUG
  714. task->tk_magic = RPC_TASK_MAGIC_ID;
  715. task->tk_pid = rpc_task_id++;
  716. #endif
  717. /* Add to global list of all tasks */
  718. spin_lock(&rpc_sched_lock);
  719. list_add_tail(&task->tk_task, &all_tasks);
  720. spin_unlock(&rpc_sched_lock);
  721. BUG_ON(task->tk_ops == NULL);
  722. dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
  723. current->pid);
  724. }
  725. static struct rpc_task *
  726. rpc_alloc_task(void)
  727. {
  728. return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
  729. }
  730. static void rpc_free_task(struct rpc_task *task)
  731. {
  732. dprintk("RPC: %4d freeing task\n", task->tk_pid);
  733. mempool_free(task, rpc_task_mempool);
  734. }
  735. /*
  736. * Create a new task for the specified client. We have to
  737. * clean up after an allocation failure, as the client may
  738. * have specified "oneshot".
  739. */
  740. struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
  741. {
  742. struct rpc_task *task;
  743. task = rpc_alloc_task();
  744. if (!task)
  745. goto cleanup;
  746. rpc_init_task(task, clnt, flags, tk_ops, calldata);
  747. dprintk("RPC: %4d allocated task\n", task->tk_pid);
  748. task->tk_flags |= RPC_TASK_DYNAMIC;
  749. out:
  750. return task;
  751. cleanup:
  752. /* Check whether to release the client */
  753. if (clnt) {
  754. printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
  755. atomic_read(&clnt->cl_users), clnt->cl_oneshot);
  756. atomic_inc(&clnt->cl_users); /* pretend we were used ... */
  757. rpc_release_client(clnt);
  758. }
  759. goto out;
  760. }
  761. void rpc_release_task(struct rpc_task *task)
  762. {
  763. const struct rpc_call_ops *tk_ops = task->tk_ops;
  764. void *calldata = task->tk_calldata;
  765. #ifdef RPC_DEBUG
  766. BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
  767. #endif
  768. if (!atomic_dec_and_test(&task->tk_count))
  769. return;
  770. dprintk("RPC: %4d release task\n", task->tk_pid);
  771. /* Remove from global task list */
  772. spin_lock(&rpc_sched_lock);
  773. list_del(&task->tk_task);
  774. spin_unlock(&rpc_sched_lock);
  775. BUG_ON (RPC_IS_QUEUED(task));
  776. /* Synchronously delete any running timer */
  777. rpc_delete_timer(task);
  778. /* Release resources */
  779. if (task->tk_rqstp)
  780. xprt_release(task);
  781. if (task->tk_msg.rpc_cred)
  782. rpcauth_unbindcred(task);
  783. if (task->tk_client) {
  784. rpc_release_client(task->tk_client);
  785. task->tk_client = NULL;
  786. }
  787. #ifdef RPC_DEBUG
  788. task->tk_magic = 0;
  789. #endif
  790. if (task->tk_flags & RPC_TASK_DYNAMIC)
  791. rpc_free_task(task);
  792. if (tk_ops->rpc_release)
  793. tk_ops->rpc_release(calldata);
  794. }
  795. /**
  796. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  797. * @clnt: pointer to RPC client
  798. * @flags: RPC flags
  799. * @ops: RPC call ops
  800. * @data: user call data
  801. */
  802. struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
  803. const struct rpc_call_ops *ops,
  804. void *data)
  805. {
  806. struct rpc_task *task;
  807. task = rpc_new_task(clnt, flags, ops, data);
  808. if (task == NULL)
  809. return ERR_PTR(-ENOMEM);
  810. atomic_inc(&task->tk_count);
  811. rpc_execute(task);
  812. return task;
  813. }
  814. EXPORT_SYMBOL(rpc_run_task);
  815. /**
  816. * rpc_find_parent - find the parent of a child task.
  817. * @child: child task
  818. * @parent: parent task
  819. *
  820. * Checks that the parent task is still sleeping on the
  821. * queue 'childq'. If so returns a pointer to the parent.
  822. * Upon failure returns NULL.
  823. *
  824. * Caller must hold childq.lock
  825. */
  826. static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
  827. {
  828. struct rpc_task *task;
  829. struct list_head *le;
  830. task_for_each(task, le, &childq.tasks[0])
  831. if (task == parent)
  832. return parent;
  833. return NULL;
  834. }
  835. static void rpc_child_exit(struct rpc_task *child, void *calldata)
  836. {
  837. struct rpc_task *parent;
  838. spin_lock_bh(&childq.lock);
  839. if ((parent = rpc_find_parent(child, calldata)) != NULL) {
  840. parent->tk_status = child->tk_status;
  841. __rpc_wake_up_task(parent);
  842. }
  843. spin_unlock_bh(&childq.lock);
  844. }
  845. static const struct rpc_call_ops rpc_child_ops = {
  846. .rpc_call_done = rpc_child_exit,
  847. };
  848. /*
  849. * Note: rpc_new_task releases the client after a failure.
  850. */
  851. struct rpc_task *
  852. rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
  853. {
  854. struct rpc_task *task;
  855. task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
  856. if (!task)
  857. goto fail;
  858. return task;
  859. fail:
  860. parent->tk_status = -ENOMEM;
  861. return NULL;
  862. }
  863. void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
  864. {
  865. spin_lock_bh(&childq.lock);
  866. /* N.B. Is it possible for the child to have already finished? */
  867. __rpc_sleep_on(&childq, task, func, NULL);
  868. rpc_schedule_run(child);
  869. spin_unlock_bh(&childq.lock);
  870. }
  871. /*
  872. * Kill all tasks for the given client.
  873. * XXX: kill their descendants as well?
  874. */
  875. void rpc_killall_tasks(struct rpc_clnt *clnt)
  876. {
  877. struct rpc_task *rovr;
  878. struct list_head *le;
  879. dprintk("RPC: killing all tasks for client %p\n", clnt);
  880. /*
  881. * Spin lock all_tasks to prevent changes...
  882. */
  883. spin_lock(&rpc_sched_lock);
  884. alltask_for_each(rovr, le, &all_tasks) {
  885. if (! RPC_IS_ACTIVATED(rovr))
  886. continue;
  887. if (!clnt || rovr->tk_client == clnt) {
  888. rovr->tk_flags |= RPC_TASK_KILLED;
  889. rpc_exit(rovr, -EIO);
  890. rpc_wake_up_task(rovr);
  891. }
  892. }
  893. spin_unlock(&rpc_sched_lock);
  894. }
  895. static DECLARE_MUTEX_LOCKED(rpciod_running);
  896. static void rpciod_killall(void)
  897. {
  898. unsigned long flags;
  899. while (!list_empty(&all_tasks)) {
  900. clear_thread_flag(TIF_SIGPENDING);
  901. rpc_killall_tasks(NULL);
  902. flush_workqueue(rpciod_workqueue);
  903. if (!list_empty(&all_tasks)) {
  904. dprintk("rpciod_killall: waiting for tasks to exit\n");
  905. yield();
  906. }
  907. }
  908. spin_lock_irqsave(&current->sighand->siglock, flags);
  909. recalc_sigpending();
  910. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  911. }
  912. /*
  913. * Start up the rpciod process if it's not already running.
  914. */
  915. int
  916. rpciod_up(void)
  917. {
  918. struct workqueue_struct *wq;
  919. int error = 0;
  920. mutex_lock(&rpciod_mutex);
  921. dprintk("rpciod_up: users %d\n", rpciod_users);
  922. rpciod_users++;
  923. if (rpciod_workqueue)
  924. goto out;
  925. /*
  926. * If there's no pid, we should be the first user.
  927. */
  928. if (rpciod_users > 1)
  929. printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
  930. /*
  931. * Create the rpciod thread and wait for it to start.
  932. */
  933. error = -ENOMEM;
  934. wq = create_workqueue("rpciod");
  935. if (wq == NULL) {
  936. printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
  937. rpciod_users--;
  938. goto out;
  939. }
  940. rpciod_workqueue = wq;
  941. error = 0;
  942. out:
  943. mutex_unlock(&rpciod_mutex);
  944. return error;
  945. }
  946. void
  947. rpciod_down(void)
  948. {
  949. mutex_lock(&rpciod_mutex);
  950. dprintk("rpciod_down sema %d\n", rpciod_users);
  951. if (rpciod_users) {
  952. if (--rpciod_users)
  953. goto out;
  954. } else
  955. printk(KERN_WARNING "rpciod_down: no users??\n");
  956. if (!rpciod_workqueue) {
  957. dprintk("rpciod_down: Nothing to do!\n");
  958. goto out;
  959. }
  960. rpciod_killall();
  961. destroy_workqueue(rpciod_workqueue);
  962. rpciod_workqueue = NULL;
  963. out:
  964. mutex_unlock(&rpciod_mutex);
  965. }
  966. #ifdef RPC_DEBUG
  967. void rpc_show_tasks(void)
  968. {
  969. struct list_head *le;
  970. struct rpc_task *t;
  971. spin_lock(&rpc_sched_lock);
  972. if (list_empty(&all_tasks)) {
  973. spin_unlock(&rpc_sched_lock);
  974. return;
  975. }
  976. printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
  977. "-rpcwait -action- ---ops--\n");
  978. alltask_for_each(t, le, &all_tasks) {
  979. const char *rpc_waitq = "none";
  980. if (RPC_IS_QUEUED(t))
  981. rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
  982. printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
  983. t->tk_pid,
  984. (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
  985. t->tk_flags, t->tk_status,
  986. t->tk_client,
  987. (t->tk_client ? t->tk_client->cl_prog : 0),
  988. t->tk_rqstp, t->tk_timeout,
  989. rpc_waitq,
  990. t->tk_action, t->tk_ops);
  991. }
  992. spin_unlock(&rpc_sched_lock);
  993. }
  994. #endif
  995. void
  996. rpc_destroy_mempool(void)
  997. {
  998. if (rpc_buffer_mempool)
  999. mempool_destroy(rpc_buffer_mempool);
  1000. if (rpc_task_mempool)
  1001. mempool_destroy(rpc_task_mempool);
  1002. if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
  1003. printk(KERN_INFO "rpc_task: not all structures were freed\n");
  1004. if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
  1005. printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
  1006. }
  1007. int
  1008. rpc_init_mempool(void)
  1009. {
  1010. rpc_task_slabp = kmem_cache_create("rpc_tasks",
  1011. sizeof(struct rpc_task),
  1012. 0, SLAB_HWCACHE_ALIGN,
  1013. NULL, NULL);
  1014. if (!rpc_task_slabp)
  1015. goto err_nomem;
  1016. rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
  1017. RPC_BUFFER_MAXSIZE,
  1018. 0, SLAB_HWCACHE_ALIGN,
  1019. NULL, NULL);
  1020. if (!rpc_buffer_slabp)
  1021. goto err_nomem;
  1022. rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
  1023. mempool_alloc_slab,
  1024. mempool_free_slab,
  1025. rpc_task_slabp);
  1026. if (!rpc_task_mempool)
  1027. goto err_nomem;
  1028. rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
  1029. mempool_alloc_slab,
  1030. mempool_free_slab,
  1031. rpc_buffer_slabp);
  1032. if (!rpc_buffer_mempool)
  1033. goto err_nomem;
  1034. return 0;
  1035. err_nomem:
  1036. rpc_destroy_mempool();
  1037. return -ENOMEM;
  1038. }