sched.c 25 KB

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