sched.c 26 KB

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