blob: b57d4062d429edf31d0d5ba3869473eb88f0c1fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
12#include <linux/module.h>
13
14#include <linux/sched.h>
15#include <linux/interrupt.h>
16#include <linux/slab.h>
17#include <linux/mempool.h>
18#include <linux/smp.h>
19#include <linux/smp_lock.h>
20#include <linux/spinlock.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#ifdef RPC_DEBUG
26#define RPCDBG_FACILITY RPCDBG_SCHED
27#define RPC_TASK_MAGIC_ID 0xf00baa
28static int rpc_task_id;
29#endif
30
31/*
32 * RPC slabs and memory pools
33 */
34#define RPC_BUFFER_MAXSIZE (2048)
35#define RPC_BUFFER_POOLSIZE (8)
36#define RPC_TASK_POOLSIZE (8)
Eric Dumazetba899662005-08-26 12:05:31 -070037static kmem_cache_t *rpc_task_slabp __read_mostly;
38static kmem_cache_t *rpc_buffer_slabp __read_mostly;
39static mempool_t *rpc_task_mempool __read_mostly;
40static mempool_t *rpc_buffer_mempool __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static void __rpc_default_timer(struct rpc_task *task);
43static void rpciod_killall(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void rpc_async_schedule(void *);
45
46/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * RPC tasks sit here while waiting for conditions to improve.
48 */
49static RPC_WAITQ(delay_queue, "delayq");
50
51/*
52 * All RPC tasks are linked into this list
53 */
54static LIST_HEAD(all_tasks);
55
56/*
57 * rpciod-related stuff
58 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080059static DEFINE_MUTEX(rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static unsigned int rpciod_users;
Trond Myklebust24c5d9d2006-03-20 13:44:08 -050061struct workqueue_struct *rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * Spinlock for other critical sections of code.
65 */
66static DEFINE_SPINLOCK(rpc_sched_lock);
67
68/*
69 * Disable the timer for a given RPC task. Should be called with
70 * queue->lock and bh_disabled in order to avoid races within
71 * rpc_run_timer().
72 */
73static inline void
74__rpc_disable_timer(struct rpc_task *task)
75{
76 dprintk("RPC: %4d disabling timer\n", task->tk_pid);
77 task->tk_timeout_fn = NULL;
78 task->tk_timeout = 0;
79}
80
81/*
82 * Run a timeout function.
83 * We use the callback in order to allow __rpc_wake_up_task()
84 * and friends to disable the timer synchronously on SMP systems
85 * without calling del_timer_sync(). The latter could cause a
86 * deadlock if called while we're holding spinlocks...
87 */
88static void rpc_run_timer(struct rpc_task *task)
89{
90 void (*callback)(struct rpc_task *);
91
92 callback = task->tk_timeout_fn;
93 task->tk_timeout_fn = NULL;
94 if (callback && RPC_IS_QUEUED(task)) {
95 dprintk("RPC: %4d running timer\n", task->tk_pid);
96 callback(task);
97 }
98 smp_mb__before_clear_bit();
99 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
100 smp_mb__after_clear_bit();
101}
102
103/*
104 * Set up a timer for the current task.
105 */
106static inline void
107__rpc_add_timer(struct rpc_task *task, rpc_action timer)
108{
109 if (!task->tk_timeout)
110 return;
111
112 dprintk("RPC: %4d setting alarm for %lu ms\n",
113 task->tk_pid, task->tk_timeout * 1000 / HZ);
114
115 if (timer)
116 task->tk_timeout_fn = timer;
117 else
118 task->tk_timeout_fn = __rpc_default_timer;
119 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
120 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
121}
122
123/*
124 * Delete any timer for the current task. Because we use del_timer_sync(),
125 * this function should never be called while holding queue->lock.
126 */
127static void
128rpc_delete_timer(struct rpc_task *task)
129{
130 if (RPC_IS_QUEUED(task))
131 return;
132 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
133 del_singleshot_timer_sync(&task->tk_timer);
134 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
135 }
136}
137
138/*
139 * Add new request to a priority queue.
140 */
141static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
142{
143 struct list_head *q;
144 struct rpc_task *t;
145
146 INIT_LIST_HEAD(&task->u.tk_wait.links);
147 q = &queue->tasks[task->tk_priority];
148 if (unlikely(task->tk_priority > queue->maxpriority))
149 q = &queue->tasks[queue->maxpriority];
150 list_for_each_entry(t, q, u.tk_wait.list) {
151 if (t->tk_cookie == task->tk_cookie) {
152 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
153 return;
154 }
155 }
156 list_add_tail(&task->u.tk_wait.list, q);
157}
158
159/*
160 * Add new request to wait queue.
161 *
162 * Swapper tasks always get inserted at the head of the queue.
163 * This should avoid many nasty memory deadlocks and hopefully
164 * improve overall performance.
165 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
166 */
167static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
168{
169 BUG_ON (RPC_IS_QUEUED(task));
170
171 if (RPC_IS_PRIORITY(queue))
172 __rpc_add_wait_queue_priority(queue, task);
173 else if (RPC_IS_SWAPPER(task))
174 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
175 else
176 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
177 task->u.tk_wait.rpc_waitq = queue;
Chuck Levere19b63d2006-03-20 13:44:15 -0500178 queue->qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 rpc_set_queued(task);
180
181 dprintk("RPC: %4d added to queue %p \"%s\"\n",
182 task->tk_pid, queue, rpc_qname(queue));
183}
184
185/*
186 * Remove request from a priority queue.
187 */
188static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
189{
190 struct rpc_task *t;
191
192 if (!list_empty(&task->u.tk_wait.links)) {
193 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
194 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
195 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
196 }
197 list_del(&task->u.tk_wait.list);
198}
199
200/*
201 * Remove request from queue.
202 * Note: must be called with spin lock held.
203 */
204static void __rpc_remove_wait_queue(struct rpc_task *task)
205{
206 struct rpc_wait_queue *queue;
207 queue = task->u.tk_wait.rpc_waitq;
208
209 if (RPC_IS_PRIORITY(queue))
210 __rpc_remove_wait_queue_priority(task);
211 else
212 list_del(&task->u.tk_wait.list);
Chuck Levere19b63d2006-03-20 13:44:15 -0500213 queue->qlen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
215 task->tk_pid, queue, rpc_qname(queue));
216}
217
218static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
219{
220 queue->priority = priority;
221 queue->count = 1 << (priority * 2);
222}
223
224static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
225{
226 queue->cookie = cookie;
227 queue->nr = RPC_BATCH_COUNT;
228}
229
230static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
231{
232 rpc_set_waitqueue_priority(queue, queue->maxpriority);
233 rpc_set_waitqueue_cookie(queue, 0);
234}
235
236static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
237{
238 int i;
239
240 spin_lock_init(&queue->lock);
241 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
242 INIT_LIST_HEAD(&queue->tasks[i]);
243 queue->maxpriority = maxprio;
244 rpc_reset_waitqueue_priority(queue);
245#ifdef RPC_DEBUG
246 queue->name = qname;
247#endif
248}
249
250void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
251{
252 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
253}
254
255void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
256{
257 __rpc_init_priority_wait_queue(queue, qname, 0);
258}
259EXPORT_SYMBOL(rpc_init_wait_queue);
260
Trond Myklebust44c28872006-01-03 09:55:06 +0100261static int rpc_wait_bit_interruptible(void *word)
262{
263 if (signal_pending(current))
264 return -ERESTARTSYS;
265 schedule();
266 return 0;
267}
268
269/*
270 * Mark an RPC call as having completed by clearing the 'active' bit
271 */
272static inline void rpc_mark_complete_task(struct rpc_task *task)
273{
274 rpc_clear_active(task);
275 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
276}
277
278/*
279 * Allow callers to wait for completion of an RPC call
280 */
281int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
282{
283 if (action == NULL)
284 action = rpc_wait_bit_interruptible;
285 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
286 action, TASK_INTERRUPTIBLE);
287}
288EXPORT_SYMBOL(__rpc_wait_for_completion_task);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
291 * Make an RPC task runnable.
292 *
293 * Note: If the task is ASYNC, this must be called with
294 * the spinlock held to protect the wait queue operation.
295 */
296static void rpc_make_runnable(struct rpc_task *task)
297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 BUG_ON(task->tk_timeout_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rpc_clear_queued(task);
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100300 if (rpc_test_and_set_running(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return;
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100302 /* We might have raced */
303 if (RPC_IS_QUEUED(task)) {
304 rpc_clear_running(task);
305 return;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (RPC_IS_ASYNC(task)) {
308 int status;
309
310 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
311 status = queue_work(task->tk_workqueue, &task->u.tk_work);
312 if (status < 0) {
313 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
314 task->tk_status = status;
315 return;
316 }
317 } else
Trond Myklebust96651ab2005-06-22 17:16:21 +0000318 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * Prepare for sleeping on a wait queue.
323 * By always appending tasks to the list we ensure FIFO behavior.
324 * NB: An RPC task will only receive interrupt-driven events as long
325 * as it's on a wait queue.
326 */
327static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
328 rpc_action action, rpc_action timer)
329{
330 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
331 rpc_qname(q), jiffies);
332
333 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
334 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
335 return;
336 }
337
338 /* Mark the task as being activated if so needed */
Trond Myklebust44c28872006-01-03 09:55:06 +0100339 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 __rpc_add_wait_queue(q, task);
342
343 BUG_ON(task->tk_callback != NULL);
344 task->tk_callback = action;
345 __rpc_add_timer(task, timer);
346}
347
348void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
349 rpc_action action, rpc_action timer)
350{
351 /*
352 * Protect the queue operations.
353 */
354 spin_lock_bh(&q->lock);
355 __rpc_sleep_on(q, task, action, timer);
356 spin_unlock_bh(&q->lock);
357}
358
359/**
360 * __rpc_do_wake_up_task - wake up a single rpc_task
361 * @task: task to be woken up
362 *
363 * Caller must hold queue->lock, and have cleared the task queued flag.
364 */
365static void __rpc_do_wake_up_task(struct rpc_task *task)
366{
367 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
368
369#ifdef RPC_DEBUG
370 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
371#endif
372 /* Has the task been executed yet? If not, we cannot wake it up! */
373 if (!RPC_IS_ACTIVATED(task)) {
374 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
375 return;
376 }
377
378 __rpc_disable_timer(task);
379 __rpc_remove_wait_queue(task);
380
381 rpc_make_runnable(task);
382
383 dprintk("RPC: __rpc_wake_up_task done\n");
384}
385
386/*
387 * Wake up the specified task
388 */
389static void __rpc_wake_up_task(struct rpc_task *task)
390{
391 if (rpc_start_wakeup(task)) {
392 if (RPC_IS_QUEUED(task))
393 __rpc_do_wake_up_task(task);
394 rpc_finish_wakeup(task);
395 }
396}
397
398/*
399 * Default timeout handler if none specified by user
400 */
401static void
402__rpc_default_timer(struct rpc_task *task)
403{
404 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
405 task->tk_status = -ETIMEDOUT;
406 rpc_wake_up_task(task);
407}
408
409/*
410 * Wake up the specified task
411 */
412void rpc_wake_up_task(struct rpc_task *task)
413{
414 if (rpc_start_wakeup(task)) {
415 if (RPC_IS_QUEUED(task)) {
416 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
417
418 spin_lock_bh(&queue->lock);
419 __rpc_do_wake_up_task(task);
420 spin_unlock_bh(&queue->lock);
421 }
422 rpc_finish_wakeup(task);
423 }
424}
425
426/*
427 * Wake up the next task on a priority queue.
428 */
429static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
430{
431 struct list_head *q;
432 struct rpc_task *task;
433
434 /*
435 * Service a batch of tasks from a single cookie.
436 */
437 q = &queue->tasks[queue->priority];
438 if (!list_empty(q)) {
439 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
440 if (queue->cookie == task->tk_cookie) {
441 if (--queue->nr)
442 goto out;
443 list_move_tail(&task->u.tk_wait.list, q);
444 }
445 /*
446 * Check if we need to switch queues.
447 */
448 if (--queue->count)
449 goto new_cookie;
450 }
451
452 /*
453 * Service the next queue.
454 */
455 do {
456 if (q == &queue->tasks[0])
457 q = &queue->tasks[queue->maxpriority];
458 else
459 q = q - 1;
460 if (!list_empty(q)) {
461 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
462 goto new_queue;
463 }
464 } while (q != &queue->tasks[queue->priority]);
465
466 rpc_reset_waitqueue_priority(queue);
467 return NULL;
468
469new_queue:
470 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
471new_cookie:
472 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
473out:
474 __rpc_wake_up_task(task);
475 return task;
476}
477
478/*
479 * Wake up the next task on the wait queue.
480 */
481struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
482{
483 struct rpc_task *task = NULL;
484
485 dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
486 spin_lock_bh(&queue->lock);
487 if (RPC_IS_PRIORITY(queue))
488 task = __rpc_wake_up_next_priority(queue);
489 else {
490 task_for_first(task, &queue->tasks[0])
491 __rpc_wake_up_task(task);
492 }
493 spin_unlock_bh(&queue->lock);
494
495 return task;
496}
497
498/**
499 * rpc_wake_up - wake up all rpc_tasks
500 * @queue: rpc_wait_queue on which the tasks are sleeping
501 *
502 * Grabs queue->lock
503 */
504void rpc_wake_up(struct rpc_wait_queue *queue)
505{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800506 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct list_head *head;
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 spin_lock_bh(&queue->lock);
510 head = &queue->tasks[queue->maxpriority];
511 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800512 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 __rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (head == &queue->tasks[0])
515 break;
516 head--;
517 }
518 spin_unlock_bh(&queue->lock);
519}
520
521/**
522 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
523 * @queue: rpc_wait_queue on which the tasks are sleeping
524 * @status: status value to set
525 *
526 * Grabs queue->lock
527 */
528void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
529{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800530 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 spin_lock_bh(&queue->lock);
534 head = &queue->tasks[queue->maxpriority];
535 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800536 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 task->tk_status = status;
538 __rpc_wake_up_task(task);
539 }
540 if (head == &queue->tasks[0])
541 break;
542 head--;
543 }
544 spin_unlock_bh(&queue->lock);
545}
546
Trond Myklebust80147932006-08-31 18:24:08 -0400547static void __rpc_atrun(struct rpc_task *task)
548{
549 rpc_wake_up_task(task);
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
553 * Run a task at a later time
554 */
Trond Myklebust80147932006-08-31 18:24:08 -0400555void rpc_delay(struct rpc_task *task, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 task->tk_timeout = delay;
558 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/*
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100562 * Helper to call task->tk_ops->rpc_call_prepare
563 */
564static void rpc_prepare_task(struct rpc_task *task)
565{
566 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
567}
568
569/*
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100570 * Helper that calls task->tk_ops->rpc_call_done if it exists
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000571 */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100572void rpc_exit_task(struct rpc_task *task)
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000573{
Trond Myklebustabbcf282006-01-03 09:55:03 +0100574 task->tk_action = NULL;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100575 if (task->tk_ops->rpc_call_done != NULL) {
576 task->tk_ops->rpc_call_done(task, task->tk_calldata);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000577 if (task->tk_action != NULL) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100578 WARN_ON(RPC_ASSASSINATED(task));
579 /* Always release the RPC slot and buffer memory */
580 xprt_release(task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000581 }
582 }
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000583}
Trond Myklebustabbcf282006-01-03 09:55:03 +0100584EXPORT_SYMBOL(rpc_exit_task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000585
586/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * This is the RPC `scheduler' (or rather, the finite state machine).
588 */
589static int __rpc_execute(struct rpc_task *task)
590{
591 int status = 0;
592
593 dprintk("RPC: %4d rpc_execute flgs %x\n",
594 task->tk_pid, task->tk_flags);
595
596 BUG_ON(RPC_IS_QUEUED(task));
597
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000598 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * Garbage collection of pending timers...
601 */
602 rpc_delete_timer(task);
603
604 /*
605 * Execute any pending callback.
606 */
607 if (RPC_DO_CALLBACK(task)) {
608 /* Define a callback save pointer */
609 void (*save_callback)(struct rpc_task *);
610
611 /*
612 * If a callback exists, save it, reset it,
613 * call it.
614 * The save is needed to stop from resetting
615 * another callback set within the callback handler
616 * - Dave
617 */
618 save_callback=task->tk_callback;
619 task->tk_callback=NULL;
620 lock_kernel();
621 save_callback(task);
622 unlock_kernel();
623 }
624
625 /*
626 * Perform the next FSM step.
627 * tk_action may be NULL when the task has been killed
628 * by someone else.
629 */
630 if (!RPC_IS_QUEUED(task)) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100631 if (task->tk_action == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
Trond Myklebustabbcf282006-01-03 09:55:03 +0100633 lock_kernel();
634 task->tk_action(task);
635 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
638 /*
639 * Lockless check for whether task is sleeping or not.
640 */
641 if (!RPC_IS_QUEUED(task))
642 continue;
643 rpc_clear_running(task);
644 if (RPC_IS_ASYNC(task)) {
645 /* Careful! we may have raced... */
646 if (RPC_IS_QUEUED(task))
647 return 0;
648 if (rpc_test_and_set_running(task))
649 return 0;
650 continue;
651 }
652
653 /* sync task: sleep here */
654 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000655 /* Note: Caller should be using rpc_clnt_sigmask() */
656 status = out_of_line_wait_on_bit(&task->tk_runstate,
657 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
658 TASK_INTERRUPTIBLE);
659 if (status == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * When a sync task receives a signal, it exits with
662 * -ERESTARTSYS. In order to catch any callbacks that
663 * clean up after sleeping on some queue, we don't
664 * break the loop here, but go around once more.
665 */
Trond Myklebust96651ab2005-06-22 17:16:21 +0000666 dprintk("RPC: %4d got signal\n", task->tk_pid);
667 task->tk_flags |= RPC_TASK_KILLED;
668 rpc_exit(task, -ERESTARTSYS);
669 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 rpc_set_running(task);
672 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
673 }
674
Trond Myklebuste60859a2006-01-03 09:55:10 +0100675 dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
Trond Myklebust44c28872006-01-03 09:55:06 +0100676 /* Wake up anyone who is waiting for task completion */
677 rpc_mark_complete_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* Release all resources associated with the task */
679 rpc_release_task(task);
680 return status;
681}
682
683/*
684 * User-visible entry point to the scheduler.
685 *
686 * This may be called recursively if e.g. an async NFS task updates
687 * the attributes and finds that dirty pages must be flushed.
688 * NOTE: Upon exit of this function the task is guaranteed to be
689 * released. In particular note that tk_release() will have
690 * been called, so your task memory may have been freed.
691 */
692int
693rpc_execute(struct rpc_task *task)
694{
Trond Myklebust44c28872006-01-03 09:55:06 +0100695 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rpc_set_running(task);
697 return __rpc_execute(task);
698}
699
700static void rpc_async_schedule(void *arg)
701{
702 __rpc_execute((struct rpc_task *)arg);
703}
704
Chuck Lever02107142006-01-03 09:55:49 +0100705/**
706 * rpc_malloc - allocate an RPC buffer
707 * @task: RPC task that will use this buffer
708 * @size: requested byte size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
710 * We try to ensure that some NFS reads and writes can always proceed
711 * by using a mempool when allocating 'small' buffers.
712 * In order to avoid memory starvation triggering more writebacks of
713 * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
714 */
Chuck Lever02107142006-01-03 09:55:49 +0100715void * rpc_malloc(struct rpc_task *task, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Chuck Lever02107142006-01-03 09:55:49 +0100717 struct rpc_rqst *req = task->tk_rqstp;
Al Virodd0fc662005-10-07 07:46:04 +0100718 gfp_t gfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (task->tk_flags & RPC_TASK_SWAPPER)
721 gfp = GFP_ATOMIC;
722 else
723 gfp = GFP_NOFS;
724
725 if (size > RPC_BUFFER_MAXSIZE) {
Chuck Lever02107142006-01-03 09:55:49 +0100726 req->rq_buffer = kmalloc(size, gfp);
727 if (req->rq_buffer)
728 req->rq_bufsize = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 } else {
Chuck Lever02107142006-01-03 09:55:49 +0100730 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
731 if (req->rq_buffer)
732 req->rq_bufsize = RPC_BUFFER_MAXSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Chuck Lever02107142006-01-03 09:55:49 +0100734 return req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Chuck Lever02107142006-01-03 09:55:49 +0100737/**
738 * rpc_free - free buffer allocated via rpc_malloc
739 * @task: RPC task with a buffer to be freed
740 *
741 */
742void rpc_free(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Chuck Lever02107142006-01-03 09:55:49 +0100744 struct rpc_rqst *req = task->tk_rqstp;
745
746 if (req->rq_buffer) {
747 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
748 mempool_free(req->rq_buffer, rpc_buffer_mempool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 else
Chuck Lever02107142006-01-03 09:55:49 +0100750 kfree(req->rq_buffer);
751 req->rq_buffer = NULL;
752 req->rq_bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754}
755
756/*
757 * Creation and deletion of RPC task structures
758 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100759void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 memset(task, 0, sizeof(*task));
762 init_timer(&task->tk_timer);
763 task->tk_timer.data = (unsigned long) task;
764 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
Trond Myklebust44c28872006-01-03 09:55:06 +0100765 atomic_set(&task->tk_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 task->tk_client = clnt;
767 task->tk_flags = flags;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100768 task->tk_ops = tk_ops;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100769 if (tk_ops->rpc_call_prepare != NULL)
770 task->tk_action = rpc_prepare_task;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100771 task->tk_calldata = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* Initialize retry counters */
774 task->tk_garb_retry = 2;
775 task->tk_cred_retry = 2;
776
777 task->tk_priority = RPC_PRIORITY_NORMAL;
778 task->tk_cookie = (unsigned long)current;
779
780 /* Initialize workqueue for async tasks */
781 task->tk_workqueue = rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (clnt) {
784 atomic_inc(&clnt->cl_users);
785 if (clnt->cl_softrtry)
786 task->tk_flags |= RPC_TASK_SOFT;
787 if (!clnt->cl_intr)
788 task->tk_flags |= RPC_TASK_NOINTR;
789 }
790
791#ifdef RPC_DEBUG
792 task->tk_magic = RPC_TASK_MAGIC_ID;
793 task->tk_pid = rpc_task_id++;
794#endif
795 /* Add to global list of all tasks */
796 spin_lock(&rpc_sched_lock);
797 list_add_tail(&task->tk_task, &all_tasks);
798 spin_unlock(&rpc_sched_lock);
799
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100800 BUG_ON(task->tk_ops == NULL);
801
Chuck Leveref759a22006-03-20 13:44:17 -0500802 /* starting timestamp */
803 task->tk_start = jiffies;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
806 current->pid);
807}
808
809static struct rpc_task *
810rpc_alloc_task(void)
811{
812 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
813}
814
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100815static void rpc_free_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 dprintk("RPC: %4d freeing task\n", task->tk_pid);
818 mempool_free(task, rpc_task_mempool);
819}
820
821/*
822 * Create a new task for the specified client. We have to
823 * clean up after an allocation failure, as the client may
824 * have specified "oneshot".
825 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100826struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct rpc_task *task;
829
830 task = rpc_alloc_task();
831 if (!task)
832 goto cleanup;
833
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100834 rpc_init_task(task, clnt, flags, tk_ops, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 dprintk("RPC: %4d allocated task\n", task->tk_pid);
837 task->tk_flags |= RPC_TASK_DYNAMIC;
838out:
839 return task;
840
841cleanup:
842 /* Check whether to release the client */
843 if (clnt) {
844 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
845 atomic_read(&clnt->cl_users), clnt->cl_oneshot);
846 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
847 rpc_release_client(clnt);
848 }
849 goto out;
850}
851
852void rpc_release_task(struct rpc_task *task)
853{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100854 const struct rpc_call_ops *tk_ops = task->tk_ops;
855 void *calldata = task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857#ifdef RPC_DEBUG
858 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
859#endif
Trond Myklebust44c28872006-01-03 09:55:06 +0100860 if (!atomic_dec_and_test(&task->tk_count))
861 return;
862 dprintk("RPC: %4d release task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* Remove from global task list */
865 spin_lock(&rpc_sched_lock);
866 list_del(&task->tk_task);
867 spin_unlock(&rpc_sched_lock);
868
869 BUG_ON (RPC_IS_QUEUED(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* Synchronously delete any running timer */
872 rpc_delete_timer(task);
873
874 /* Release resources */
875 if (task->tk_rqstp)
876 xprt_release(task);
877 if (task->tk_msg.rpc_cred)
878 rpcauth_unbindcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (task->tk_client) {
880 rpc_release_client(task->tk_client);
881 task->tk_client = NULL;
882 }
883
884#ifdef RPC_DEBUG
885 task->tk_magic = 0;
886#endif
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100887 if (task->tk_flags & RPC_TASK_DYNAMIC)
888 rpc_free_task(task);
889 if (tk_ops->rpc_release)
890 tk_ops->rpc_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/**
Trond Myklebust44c28872006-01-03 09:55:06 +0100894 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
Martin Waitz99acf042006-02-01 03:06:56 -0800895 * @clnt: pointer to RPC client
896 * @flags: RPC flags
897 * @ops: RPC call ops
898 * @data: user call data
Trond Myklebust44c28872006-01-03 09:55:06 +0100899 */
900struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
901 const struct rpc_call_ops *ops,
902 void *data)
903{
904 struct rpc_task *task;
905 task = rpc_new_task(clnt, flags, ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500906 if (task == NULL) {
907 if (ops->rpc_release != NULL)
908 ops->rpc_release(data);
Trond Myklebust44c28872006-01-03 09:55:06 +0100909 return ERR_PTR(-ENOMEM);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500910 }
Trond Myklebust44c28872006-01-03 09:55:06 +0100911 atomic_inc(&task->tk_count);
912 rpc_execute(task);
913 return task;
914}
915EXPORT_SYMBOL(rpc_run_task);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
918 * Kill all tasks for the given client.
919 * XXX: kill their descendants as well?
920 */
921void rpc_killall_tasks(struct rpc_clnt *clnt)
922{
923 struct rpc_task *rovr;
924 struct list_head *le;
925
926 dprintk("RPC: killing all tasks for client %p\n", clnt);
927
928 /*
929 * Spin lock all_tasks to prevent changes...
930 */
931 spin_lock(&rpc_sched_lock);
932 alltask_for_each(rovr, le, &all_tasks) {
933 if (! RPC_IS_ACTIVATED(rovr))
934 continue;
935 if (!clnt || rovr->tk_client == clnt) {
936 rovr->tk_flags |= RPC_TASK_KILLED;
937 rpc_exit(rovr, -EIO);
938 rpc_wake_up_task(rovr);
939 }
940 }
941 spin_unlock(&rpc_sched_lock);
942}
943
944static DECLARE_MUTEX_LOCKED(rpciod_running);
945
946static void rpciod_killall(void)
947{
948 unsigned long flags;
949
950 while (!list_empty(&all_tasks)) {
951 clear_thread_flag(TIF_SIGPENDING);
952 rpc_killall_tasks(NULL);
953 flush_workqueue(rpciod_workqueue);
954 if (!list_empty(&all_tasks)) {
955 dprintk("rpciod_killall: waiting for tasks to exit\n");
956 yield();
957 }
958 }
959
960 spin_lock_irqsave(&current->sighand->siglock, flags);
961 recalc_sigpending();
962 spin_unlock_irqrestore(&current->sighand->siglock, flags);
963}
964
965/*
966 * Start up the rpciod process if it's not already running.
967 */
968int
969rpciod_up(void)
970{
971 struct workqueue_struct *wq;
972 int error = 0;
973
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800974 mutex_lock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 dprintk("rpciod_up: users %d\n", rpciod_users);
976 rpciod_users++;
977 if (rpciod_workqueue)
978 goto out;
979 /*
980 * If there's no pid, we should be the first user.
981 */
982 if (rpciod_users > 1)
983 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
984 /*
985 * Create the rpciod thread and wait for it to start.
986 */
987 error = -ENOMEM;
988 wq = create_workqueue("rpciod");
989 if (wq == NULL) {
990 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
991 rpciod_users--;
992 goto out;
993 }
994 rpciod_workqueue = wq;
995 error = 0;
996out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800997 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return error;
999}
1000
1001void
1002rpciod_down(void)
1003{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001004 mutex_lock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dprintk("rpciod_down sema %d\n", rpciod_users);
1006 if (rpciod_users) {
1007 if (--rpciod_users)
1008 goto out;
1009 } else
1010 printk(KERN_WARNING "rpciod_down: no users??\n");
1011
1012 if (!rpciod_workqueue) {
1013 dprintk("rpciod_down: Nothing to do!\n");
1014 goto out;
1015 }
1016 rpciod_killall();
1017
1018 destroy_workqueue(rpciod_workqueue);
1019 rpciod_workqueue = NULL;
1020 out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001021 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024#ifdef RPC_DEBUG
1025void rpc_show_tasks(void)
1026{
1027 struct list_head *le;
1028 struct rpc_task *t;
1029
1030 spin_lock(&rpc_sched_lock);
1031 if (list_empty(&all_tasks)) {
1032 spin_unlock(&rpc_sched_lock);
1033 return;
1034 }
1035 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001036 "-rpcwait -action- ---ops--\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 alltask_for_each(t, le, &all_tasks) {
1038 const char *rpc_waitq = "none";
1039
1040 if (RPC_IS_QUEUED(t))
1041 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1042
1043 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1044 t->tk_pid,
1045 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1046 t->tk_flags, t->tk_status,
1047 t->tk_client,
1048 (t->tk_client ? t->tk_client->cl_prog : 0),
1049 t->tk_rqstp, t->tk_timeout,
1050 rpc_waitq,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001051 t->tk_action, t->tk_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 spin_unlock(&rpc_sched_lock);
1054}
1055#endif
1056
1057void
1058rpc_destroy_mempool(void)
1059{
1060 if (rpc_buffer_mempool)
1061 mempool_destroy(rpc_buffer_mempool);
1062 if (rpc_task_mempool)
1063 mempool_destroy(rpc_task_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001064 if (rpc_task_slabp)
1065 kmem_cache_destroy(rpc_task_slabp);
1066 if (rpc_buffer_slabp)
1067 kmem_cache_destroy(rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070int
1071rpc_init_mempool(void)
1072{
1073 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1074 sizeof(struct rpc_task),
1075 0, SLAB_HWCACHE_ALIGN,
1076 NULL, NULL);
1077 if (!rpc_task_slabp)
1078 goto err_nomem;
1079 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1080 RPC_BUFFER_MAXSIZE,
1081 0, SLAB_HWCACHE_ALIGN,
1082 NULL, NULL);
1083 if (!rpc_buffer_slabp)
1084 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001085 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1086 rpc_task_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!rpc_task_mempool)
1088 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001089 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1090 rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!rpc_buffer_mempool)
1092 goto err_nomem;
1093 return 0;
1094err_nomem:
1095 rpc_destroy_mempool();
1096 return -ENOMEM;
1097}