sched.c 28 KB

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