sched.c 26 KB

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