sched.c 25 KB

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