2 * linux/net/sunrpc/sched.c
4 * Scheduling for synchronous and asynchronous RPC requests.
6 * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
12 #include <linux/module.h>
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>
22 #include <linux/sunrpc/clnt.h>
23 #include <linux/sunrpc/xprt.h>
26 #define RPCDBG_FACILITY RPCDBG_SCHED
27 #define RPC_TASK_MAGIC_ID 0xf00baa
28 static int rpc_task_id;
32 * RPC slabs and memory pools
34 #define RPC_BUFFER_MAXSIZE (2048)
35 #define RPC_BUFFER_POOLSIZE (8)
36 #define RPC_TASK_POOLSIZE (8)
37 static kmem_cache_t *rpc_task_slabp __read_mostly;
38 static kmem_cache_t *rpc_buffer_slabp __read_mostly;
39 static mempool_t *rpc_task_mempool __read_mostly;
40 static mempool_t *rpc_buffer_mempool __read_mostly;
42 static void __rpc_default_timer(struct rpc_task *task);
43 static void rpciod_killall(void);
44 static void rpc_free(struct rpc_task *task);
46 static void rpc_async_schedule(void *);
49 * RPC tasks that create another task (e.g. for contacting the portmapper)
50 * will wait on this queue for their child's completion
52 static RPC_WAITQ(childq, "childq");
55 * RPC tasks sit here while waiting for conditions to improve.
57 static RPC_WAITQ(delay_queue, "delayq");
60 * All RPC tasks are linked into this list
62 static LIST_HEAD(all_tasks);
65 * rpciod-related stuff
67 static DECLARE_MUTEX(rpciod_sema);
68 static unsigned int rpciod_users;
69 static struct workqueue_struct *rpciod_workqueue;
72 * Spinlock for other critical sections of code.
74 static DEFINE_SPINLOCK(rpc_sched_lock);
77 * Disable the timer for a given RPC task. Should be called with
78 * queue->lock and bh_disabled in order to avoid races within
82 __rpc_disable_timer(struct rpc_task *task)
84 dprintk("RPC: %4d disabling timer\n", task->tk_pid);
85 task->tk_timeout_fn = NULL;
90 * Run a timeout function.
91 * We use the callback in order to allow __rpc_wake_up_task()
92 * and friends to disable the timer synchronously on SMP systems
93 * without calling del_timer_sync(). The latter could cause a
94 * deadlock if called while we're holding spinlocks...
96 static void rpc_run_timer(struct rpc_task *task)
98 void (*callback)(struct rpc_task *);
100 callback = task->tk_timeout_fn;
101 task->tk_timeout_fn = NULL;
102 if (callback && RPC_IS_QUEUED(task)) {
103 dprintk("RPC: %4d running timer\n", task->tk_pid);
106 smp_mb__before_clear_bit();
107 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
108 smp_mb__after_clear_bit();
112 * Set up a timer for the current task.
115 __rpc_add_timer(struct rpc_task *task, rpc_action timer)
117 if (!task->tk_timeout)
120 dprintk("RPC: %4d setting alarm for %lu ms\n",
121 task->tk_pid, task->tk_timeout * 1000 / HZ);
124 task->tk_timeout_fn = timer;
126 task->tk_timeout_fn = __rpc_default_timer;
127 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
128 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
132 * Delete any timer for the current task. Because we use del_timer_sync(),
133 * this function should never be called while holding queue->lock.
136 rpc_delete_timer(struct rpc_task *task)
138 if (RPC_IS_QUEUED(task))
140 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
141 del_singleshot_timer_sync(&task->tk_timer);
142 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
147 * Add new request to a priority queue.
149 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
154 INIT_LIST_HEAD(&task->u.tk_wait.links);
155 q = &queue->tasks[task->tk_priority];
156 if (unlikely(task->tk_priority > queue->maxpriority))
157 q = &queue->tasks[queue->maxpriority];
158 list_for_each_entry(t, q, u.tk_wait.list) {
159 if (t->tk_cookie == task->tk_cookie) {
160 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
164 list_add_tail(&task->u.tk_wait.list, q);
168 * Add new request to wait queue.
170 * Swapper tasks always get inserted at the head of the queue.
171 * This should avoid many nasty memory deadlocks and hopefully
172 * improve overall performance.
173 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
175 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
177 BUG_ON (RPC_IS_QUEUED(task));
179 if (RPC_IS_PRIORITY(queue))
180 __rpc_add_wait_queue_priority(queue, task);
181 else if (RPC_IS_SWAPPER(task))
182 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
184 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
185 task->u.tk_wait.rpc_waitq = queue;
186 rpc_set_queued(task);
188 dprintk("RPC: %4d added to queue %p \"%s\"\n",
189 task->tk_pid, queue, rpc_qname(queue));
193 * Remove request from a priority queue.
195 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
199 if (!list_empty(&task->u.tk_wait.links)) {
200 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
201 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
202 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
204 list_del(&task->u.tk_wait.list);
208 * Remove request from queue.
209 * Note: must be called with spin lock held.
211 static void __rpc_remove_wait_queue(struct rpc_task *task)
213 struct rpc_wait_queue *queue;
214 queue = task->u.tk_wait.rpc_waitq;
216 if (RPC_IS_PRIORITY(queue))
217 __rpc_remove_wait_queue_priority(task);
219 list_del(&task->u.tk_wait.list);
220 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
221 task->tk_pid, queue, rpc_qname(queue));
224 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
226 queue->priority = priority;
227 queue->count = 1 << (priority * 2);
230 static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
232 queue->cookie = cookie;
233 queue->nr = RPC_BATCH_COUNT;
236 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
238 rpc_set_waitqueue_priority(queue, queue->maxpriority);
239 rpc_set_waitqueue_cookie(queue, 0);
242 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
246 spin_lock_init(&queue->lock);
247 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
248 INIT_LIST_HEAD(&queue->tasks[i]);
249 queue->maxpriority = maxprio;
250 rpc_reset_waitqueue_priority(queue);
256 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
258 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
261 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
263 __rpc_init_priority_wait_queue(queue, qname, 0);
265 EXPORT_SYMBOL(rpc_init_wait_queue);
267 static int rpc_wait_bit_interruptible(void *word)
269 if (signal_pending(current))
276 * Mark an RPC call as having completed by clearing the 'active' bit
278 static inline void rpc_mark_complete_task(struct rpc_task *task)
280 rpc_clear_active(task);
281 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
285 * Allow callers to wait for completion of an RPC call
287 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
290 action = rpc_wait_bit_interruptible;
291 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
292 action, TASK_INTERRUPTIBLE);
294 EXPORT_SYMBOL(__rpc_wait_for_completion_task);
297 * Make an RPC task runnable.
299 * Note: If the task is ASYNC, this must be called with
300 * the spinlock held to protect the wait queue operation.
302 static void rpc_make_runnable(struct rpc_task *task)
306 BUG_ON(task->tk_timeout_fn);
307 do_ret = rpc_test_and_set_running(task);
308 rpc_clear_queued(task);
311 if (RPC_IS_ASYNC(task)) {
314 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
315 status = queue_work(task->tk_workqueue, &task->u.tk_work);
317 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
318 task->tk_status = status;
322 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
326 * Place a newly initialized task on the workqueue.
329 rpc_schedule_run(struct rpc_task *task)
331 rpc_set_active(task);
332 rpc_make_runnable(task);
336 * Prepare for sleeping on a wait queue.
337 * By always appending tasks to the list we ensure FIFO behavior.
338 * NB: An RPC task will only receive interrupt-driven events as long
339 * as it's on a wait queue.
341 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
342 rpc_action action, rpc_action timer)
344 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
345 rpc_qname(q), jiffies);
347 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
348 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
352 /* Mark the task as being activated if so needed */
353 rpc_set_active(task);
355 __rpc_add_wait_queue(q, task);
357 BUG_ON(task->tk_callback != NULL);
358 task->tk_callback = action;
359 __rpc_add_timer(task, timer);
362 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
363 rpc_action action, rpc_action timer)
366 * Protect the queue operations.
368 spin_lock_bh(&q->lock);
369 __rpc_sleep_on(q, task, action, timer);
370 spin_unlock_bh(&q->lock);
374 * __rpc_do_wake_up_task - wake up a single rpc_task
375 * @task: task to be woken up
377 * Caller must hold queue->lock, and have cleared the task queued flag.
379 static void __rpc_do_wake_up_task(struct rpc_task *task)
381 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
384 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
386 /* Has the task been executed yet? If not, we cannot wake it up! */
387 if (!RPC_IS_ACTIVATED(task)) {
388 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
392 __rpc_disable_timer(task);
393 __rpc_remove_wait_queue(task);
395 rpc_make_runnable(task);
397 dprintk("RPC: __rpc_wake_up_task done\n");
401 * Wake up the specified task
403 static void __rpc_wake_up_task(struct rpc_task *task)
405 if (rpc_start_wakeup(task)) {
406 if (RPC_IS_QUEUED(task))
407 __rpc_do_wake_up_task(task);
408 rpc_finish_wakeup(task);
413 * Default timeout handler if none specified by user
416 __rpc_default_timer(struct rpc_task *task)
418 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
419 task->tk_status = -ETIMEDOUT;
420 rpc_wake_up_task(task);
424 * Wake up the specified task
426 void rpc_wake_up_task(struct rpc_task *task)
428 if (rpc_start_wakeup(task)) {
429 if (RPC_IS_QUEUED(task)) {
430 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
432 spin_lock_bh(&queue->lock);
433 __rpc_do_wake_up_task(task);
434 spin_unlock_bh(&queue->lock);
436 rpc_finish_wakeup(task);
441 * Wake up the next task on a priority queue.
443 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
446 struct rpc_task *task;
449 * Service a batch of tasks from a single cookie.
451 q = &queue->tasks[queue->priority];
452 if (!list_empty(q)) {
453 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
454 if (queue->cookie == task->tk_cookie) {
457 list_move_tail(&task->u.tk_wait.list, q);
460 * Check if we need to switch queues.
467 * Service the next queue.
470 if (q == &queue->tasks[0])
471 q = &queue->tasks[queue->maxpriority];
474 if (!list_empty(q)) {
475 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
478 } while (q != &queue->tasks[queue->priority]);
480 rpc_reset_waitqueue_priority(queue);
484 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
486 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
488 __rpc_wake_up_task(task);
493 * Wake up the next task on the wait queue.
495 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
497 struct rpc_task *task = NULL;
499 dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
500 spin_lock_bh(&queue->lock);
501 if (RPC_IS_PRIORITY(queue))
502 task = __rpc_wake_up_next_priority(queue);
504 task_for_first(task, &queue->tasks[0])
505 __rpc_wake_up_task(task);
507 spin_unlock_bh(&queue->lock);
513 * rpc_wake_up - wake up all rpc_tasks
514 * @queue: rpc_wait_queue on which the tasks are sleeping
518 void rpc_wake_up(struct rpc_wait_queue *queue)
520 struct rpc_task *task;
522 struct list_head *head;
523 spin_lock_bh(&queue->lock);
524 head = &queue->tasks[queue->maxpriority];
526 while (!list_empty(head)) {
527 task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
528 __rpc_wake_up_task(task);
530 if (head == &queue->tasks[0])
534 spin_unlock_bh(&queue->lock);
538 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
539 * @queue: rpc_wait_queue on which the tasks are sleeping
540 * @status: status value to set
544 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
546 struct list_head *head;
547 struct rpc_task *task;
549 spin_lock_bh(&queue->lock);
550 head = &queue->tasks[queue->maxpriority];
552 while (!list_empty(head)) {
553 task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
554 task->tk_status = status;
555 __rpc_wake_up_task(task);
557 if (head == &queue->tasks[0])
561 spin_unlock_bh(&queue->lock);
565 * Run a task at a later time
567 static void __rpc_atrun(struct rpc_task *);
569 rpc_delay(struct rpc_task *task, unsigned long delay)
571 task->tk_timeout = delay;
572 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
576 __rpc_atrun(struct rpc_task *task)
579 rpc_wake_up_task(task);
583 * Helper to call task->tk_ops->rpc_call_prepare
585 static void rpc_prepare_task(struct rpc_task *task)
587 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
591 * Helper that calls task->tk_ops->rpc_call_done if it exists
593 void rpc_exit_task(struct rpc_task *task)
595 task->tk_action = NULL;
596 if (task->tk_ops->rpc_call_done != NULL) {
597 task->tk_ops->rpc_call_done(task, task->tk_calldata);
598 if (task->tk_action != NULL) {
599 WARN_ON(RPC_ASSASSINATED(task));
600 /* Always release the RPC slot and buffer memory */
606 EXPORT_SYMBOL(rpc_exit_task);
609 * This is the RPC `scheduler' (or rather, the finite state machine).
611 static int __rpc_execute(struct rpc_task *task)
615 dprintk("RPC: %4d rpc_execute flgs %x\n",
616 task->tk_pid, task->tk_flags);
618 BUG_ON(RPC_IS_QUEUED(task));
622 * Garbage collection of pending timers...
624 rpc_delete_timer(task);
627 * Execute any pending callback.
629 if (RPC_DO_CALLBACK(task)) {
630 /* Define a callback save pointer */
631 void (*save_callback)(struct rpc_task *);
634 * If a callback exists, save it, reset it,
636 * The save is needed to stop from resetting
637 * another callback set within the callback handler
640 save_callback=task->tk_callback;
641 task->tk_callback=NULL;
648 * Perform the next FSM step.
649 * tk_action may be NULL when the task has been killed
652 if (!RPC_IS_QUEUED(task)) {
653 if (task->tk_action == NULL)
656 task->tk_action(task);
661 * Lockless check for whether task is sleeping or not.
663 if (!RPC_IS_QUEUED(task))
665 rpc_clear_running(task);
666 if (RPC_IS_ASYNC(task)) {
667 /* Careful! we may have raced... */
668 if (RPC_IS_QUEUED(task))
670 if (rpc_test_and_set_running(task))
675 /* sync task: sleep here */
676 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
677 /* Note: Caller should be using rpc_clnt_sigmask() */
678 status = out_of_line_wait_on_bit(&task->tk_runstate,
679 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
681 if (status == -ERESTARTSYS) {
683 * When a sync task receives a signal, it exits with
684 * -ERESTARTSYS. In order to catch any callbacks that
685 * clean up after sleeping on some queue, we don't
686 * break the loop here, but go around once more.
688 dprintk("RPC: %4d got signal\n", task->tk_pid);
689 task->tk_flags |= RPC_TASK_KILLED;
690 rpc_exit(task, -ERESTARTSYS);
691 rpc_wake_up_task(task);
693 rpc_set_running(task);
694 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
697 dprintk("RPC: %4d exit() = %d\n", task->tk_pid, task->tk_status);
698 status = task->tk_status;
700 /* Wake up anyone who is waiting for task completion */
701 rpc_mark_complete_task(task);
702 /* Release all resources associated with the task */
703 rpc_release_task(task);
708 * User-visible entry point to the scheduler.
710 * This may be called recursively if e.g. an async NFS task updates
711 * the attributes and finds that dirty pages must be flushed.
712 * NOTE: Upon exit of this function the task is guaranteed to be
713 * released. In particular note that tk_release() will have
714 * been called, so your task memory may have been freed.
717 rpc_execute(struct rpc_task *task)
719 rpc_set_active(task);
720 rpc_set_running(task);
721 return __rpc_execute(task);
724 static void rpc_async_schedule(void *arg)
726 __rpc_execute((struct rpc_task *)arg);
730 * Allocate memory for RPC purposes.
732 * We try to ensure that some NFS reads and writes can always proceed
733 * by using a mempool when allocating 'small' buffers.
734 * In order to avoid memory starvation triggering more writebacks of
735 * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
738 rpc_malloc(struct rpc_task *task, size_t size)
742 if (task->tk_flags & RPC_TASK_SWAPPER)
747 if (size > RPC_BUFFER_MAXSIZE) {
748 task->tk_buffer = kmalloc(size, gfp);
750 task->tk_bufsize = size;
752 task->tk_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
754 task->tk_bufsize = RPC_BUFFER_MAXSIZE;
756 return task->tk_buffer;
760 rpc_free(struct rpc_task *task)
762 if (task->tk_buffer) {
763 if (task->tk_bufsize == RPC_BUFFER_MAXSIZE)
764 mempool_free(task->tk_buffer, rpc_buffer_mempool);
766 kfree(task->tk_buffer);
767 task->tk_buffer = NULL;
768 task->tk_bufsize = 0;
773 * Creation and deletion of RPC task structures
775 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
777 memset(task, 0, sizeof(*task));
778 init_timer(&task->tk_timer);
779 task->tk_timer.data = (unsigned long) task;
780 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
781 atomic_set(&task->tk_count, 1);
782 task->tk_client = clnt;
783 task->tk_flags = flags;
784 task->tk_ops = tk_ops;
785 if (tk_ops->rpc_call_prepare != NULL)
786 task->tk_action = rpc_prepare_task;
787 task->tk_calldata = calldata;
789 /* Initialize retry counters */
790 task->tk_garb_retry = 2;
791 task->tk_cred_retry = 2;
793 task->tk_priority = RPC_PRIORITY_NORMAL;
794 task->tk_cookie = (unsigned long)current;
796 /* Initialize workqueue for async tasks */
797 task->tk_workqueue = rpciod_workqueue;
800 atomic_inc(&clnt->cl_users);
801 if (clnt->cl_softrtry)
802 task->tk_flags |= RPC_TASK_SOFT;
804 task->tk_flags |= RPC_TASK_NOINTR;
808 task->tk_magic = RPC_TASK_MAGIC_ID;
809 task->tk_pid = rpc_task_id++;
811 /* Add to global list of all tasks */
812 spin_lock(&rpc_sched_lock);
813 list_add_tail(&task->tk_task, &all_tasks);
814 spin_unlock(&rpc_sched_lock);
816 BUG_ON(task->tk_ops == NULL);
818 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
822 static struct rpc_task *
825 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
828 static void rpc_free_task(struct rpc_task *task)
830 dprintk("RPC: %4d freeing task\n", task->tk_pid);
831 mempool_free(task, rpc_task_mempool);
835 * Create a new task for the specified client. We have to
836 * clean up after an allocation failure, as the client may
837 * have specified "oneshot".
839 struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
841 struct rpc_task *task;
843 task = rpc_alloc_task();
847 rpc_init_task(task, clnt, flags, tk_ops, calldata);
849 dprintk("RPC: %4d allocated task\n", task->tk_pid);
850 task->tk_flags |= RPC_TASK_DYNAMIC;
855 /* Check whether to release the client */
857 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
858 atomic_read(&clnt->cl_users), clnt->cl_oneshot);
859 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
860 rpc_release_client(clnt);
865 void rpc_release_task(struct rpc_task *task)
867 const struct rpc_call_ops *tk_ops = task->tk_ops;
868 void *calldata = task->tk_calldata;
871 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
873 if (!atomic_dec_and_test(&task->tk_count))
875 dprintk("RPC: %4d release task\n", task->tk_pid);
877 /* Remove from global task list */
878 spin_lock(&rpc_sched_lock);
879 list_del(&task->tk_task);
880 spin_unlock(&rpc_sched_lock);
882 BUG_ON (RPC_IS_QUEUED(task));
884 /* Synchronously delete any running timer */
885 rpc_delete_timer(task);
887 /* Release resources */
890 if (task->tk_msg.rpc_cred)
891 rpcauth_unbindcred(task);
893 if (task->tk_client) {
894 rpc_release_client(task->tk_client);
895 task->tk_client = NULL;
901 if (task->tk_flags & RPC_TASK_DYNAMIC)
903 if (tk_ops->rpc_release)
904 tk_ops->rpc_release(calldata);
908 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
909 * @clnt - pointer to RPC client
911 * @ops - RPC call ops
912 * @data - user call data
914 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
915 const struct rpc_call_ops *ops,
918 struct rpc_task *task;
919 task = rpc_new_task(clnt, flags, ops, data);
921 return ERR_PTR(-ENOMEM);
922 atomic_inc(&task->tk_count);
926 EXPORT_SYMBOL(rpc_run_task);
929 * rpc_find_parent - find the parent of a child task.
932 * Checks that the parent task is still sleeping on the
933 * queue 'childq'. If so returns a pointer to the parent.
934 * Upon failure returns NULL.
936 * Caller must hold childq.lock
938 static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
940 struct rpc_task *task;
941 struct list_head *le;
943 task_for_each(task, le, &childq.tasks[0])
950 static void rpc_child_exit(struct rpc_task *child, void *calldata)
952 struct rpc_task *parent;
954 spin_lock_bh(&childq.lock);
955 if ((parent = rpc_find_parent(child, calldata)) != NULL) {
956 parent->tk_status = child->tk_status;
957 __rpc_wake_up_task(parent);
959 spin_unlock_bh(&childq.lock);
962 static const struct rpc_call_ops rpc_child_ops = {
963 .rpc_call_done = rpc_child_exit,
967 * Note: rpc_new_task releases the client after a failure.
970 rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
972 struct rpc_task *task;
974 task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
980 parent->tk_status = -ENOMEM;
984 void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
986 spin_lock_bh(&childq.lock);
987 /* N.B. Is it possible for the child to have already finished? */
988 __rpc_sleep_on(&childq, task, func, NULL);
989 rpc_schedule_run(child);
990 spin_unlock_bh(&childq.lock);
994 * Kill all tasks for the given client.
995 * XXX: kill their descendants as well?
997 void rpc_killall_tasks(struct rpc_clnt *clnt)
999 struct rpc_task *rovr;
1000 struct list_head *le;
1002 dprintk("RPC: killing all tasks for client %p\n", clnt);
1005 * Spin lock all_tasks to prevent changes...
1007 spin_lock(&rpc_sched_lock);
1008 alltask_for_each(rovr, le, &all_tasks) {
1009 if (! RPC_IS_ACTIVATED(rovr))
1011 if (!clnt || rovr->tk_client == clnt) {
1012 rovr->tk_flags |= RPC_TASK_KILLED;
1013 rpc_exit(rovr, -EIO);
1014 rpc_wake_up_task(rovr);
1017 spin_unlock(&rpc_sched_lock);
1020 static DECLARE_MUTEX_LOCKED(rpciod_running);
1022 static void rpciod_killall(void)
1024 unsigned long flags;
1026 while (!list_empty(&all_tasks)) {
1027 clear_thread_flag(TIF_SIGPENDING);
1028 rpc_killall_tasks(NULL);
1029 flush_workqueue(rpciod_workqueue);
1030 if (!list_empty(&all_tasks)) {
1031 dprintk("rpciod_killall: waiting for tasks to exit\n");
1036 spin_lock_irqsave(¤t->sighand->siglock, flags);
1037 recalc_sigpending();
1038 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
1042 * Start up the rpciod process if it's not already running.
1047 struct workqueue_struct *wq;
1051 dprintk("rpciod_up: users %d\n", rpciod_users);
1053 if (rpciod_workqueue)
1056 * If there's no pid, we should be the first user.
1058 if (rpciod_users > 1)
1059 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
1061 * Create the rpciod thread and wait for it to start.
1064 wq = create_workqueue("rpciod");
1066 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
1070 rpciod_workqueue = wq;
1081 dprintk("rpciod_down sema %d\n", rpciod_users);
1086 printk(KERN_WARNING "rpciod_down: no users??\n");
1088 if (!rpciod_workqueue) {
1089 dprintk("rpciod_down: Nothing to do!\n");
1094 destroy_workqueue(rpciod_workqueue);
1095 rpciod_workqueue = NULL;
1101 void rpc_show_tasks(void)
1103 struct list_head *le;
1106 spin_lock(&rpc_sched_lock);
1107 if (list_empty(&all_tasks)) {
1108 spin_unlock(&rpc_sched_lock);
1111 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1112 "-rpcwait -action- ---ops--\n");
1113 alltask_for_each(t, le, &all_tasks) {
1114 const char *rpc_waitq = "none";
1116 if (RPC_IS_QUEUED(t))
1117 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1119 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1121 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1122 t->tk_flags, t->tk_status,
1124 (t->tk_client ? t->tk_client->cl_prog : 0),
1125 t->tk_rqstp, t->tk_timeout,
1127 t->tk_action, t->tk_ops);
1129 spin_unlock(&rpc_sched_lock);
1134 rpc_destroy_mempool(void)
1136 if (rpc_buffer_mempool)
1137 mempool_destroy(rpc_buffer_mempool);
1138 if (rpc_task_mempool)
1139 mempool_destroy(rpc_task_mempool);
1140 if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
1141 printk(KERN_INFO "rpc_task: not all structures were freed\n");
1142 if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
1143 printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
1147 rpc_init_mempool(void)
1149 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1150 sizeof(struct rpc_task),
1151 0, SLAB_HWCACHE_ALIGN,
1153 if (!rpc_task_slabp)
1155 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1157 0, SLAB_HWCACHE_ALIGN,
1159 if (!rpc_buffer_slabp)
1161 rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
1165 if (!rpc_task_mempool)
1167 rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
1171 if (!rpc_buffer_mempool)
1175 rpc_destroy_mempool();