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>
21 #include <linux/mutex.h>
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/xprt.h>
27 #define RPCDBG_FACILITY RPCDBG_SCHED
28 #define RPC_TASK_MAGIC_ID 0xf00baa
29 static int rpc_task_id;
33 * RPC slabs and memory pools
35 #define RPC_BUFFER_MAXSIZE (2048)
36 #define RPC_BUFFER_POOLSIZE (8)
37 #define RPC_TASK_POOLSIZE (8)
38 static kmem_cache_t *rpc_task_slabp __read_mostly;
39 static kmem_cache_t *rpc_buffer_slabp __read_mostly;
40 static mempool_t *rpc_task_mempool __read_mostly;
41 static mempool_t *rpc_buffer_mempool __read_mostly;
43 static void __rpc_default_timer(struct rpc_task *task);
44 static void rpciod_killall(void);
45 static void rpc_async_schedule(void *);
48 * RPC tasks that create another task (e.g. for contacting the portmapper)
49 * will wait on this queue for their child's completion
51 static RPC_WAITQ(childq, "childq");
54 * RPC tasks sit here while waiting for conditions to improve.
56 static RPC_WAITQ(delay_queue, "delayq");
59 * All RPC tasks are linked into this list
61 static LIST_HEAD(all_tasks);
64 * rpciod-related stuff
66 static DEFINE_MUTEX(rpciod_mutex);
67 static unsigned int rpciod_users;
68 static struct workqueue_struct *rpciod_workqueue;
71 * Spinlock for other critical sections of code.
73 static DEFINE_SPINLOCK(rpc_sched_lock);
76 * Disable the timer for a given RPC task. Should be called with
77 * queue->lock and bh_disabled in order to avoid races within
81 __rpc_disable_timer(struct rpc_task *task)
83 dprintk("RPC: %4d disabling timer\n", task->tk_pid);
84 task->tk_timeout_fn = NULL;
89 * Run a timeout function.
90 * We use the callback in order to allow __rpc_wake_up_task()
91 * and friends to disable the timer synchronously on SMP systems
92 * without calling del_timer_sync(). The latter could cause a
93 * deadlock if called while we're holding spinlocks...
95 static void rpc_run_timer(struct rpc_task *task)
97 void (*callback)(struct rpc_task *);
99 callback = task->tk_timeout_fn;
100 task->tk_timeout_fn = NULL;
101 if (callback && RPC_IS_QUEUED(task)) {
102 dprintk("RPC: %4d running timer\n", task->tk_pid);
105 smp_mb__before_clear_bit();
106 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
107 smp_mb__after_clear_bit();
111 * Set up a timer for the current task.
114 __rpc_add_timer(struct rpc_task *task, rpc_action timer)
116 if (!task->tk_timeout)
119 dprintk("RPC: %4d setting alarm for %lu ms\n",
120 task->tk_pid, task->tk_timeout * 1000 / HZ);
123 task->tk_timeout_fn = timer;
125 task->tk_timeout_fn = __rpc_default_timer;
126 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
127 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
131 * Delete any timer for the current task. Because we use del_timer_sync(),
132 * this function should never be called while holding queue->lock.
135 rpc_delete_timer(struct rpc_task *task)
137 if (RPC_IS_QUEUED(task))
139 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
140 del_singleshot_timer_sync(&task->tk_timer);
141 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
146 * Add new request to a priority queue.
148 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
153 INIT_LIST_HEAD(&task->u.tk_wait.links);
154 q = &queue->tasks[task->tk_priority];
155 if (unlikely(task->tk_priority > queue->maxpriority))
156 q = &queue->tasks[queue->maxpriority];
157 list_for_each_entry(t, q, u.tk_wait.list) {
158 if (t->tk_cookie == task->tk_cookie) {
159 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
163 list_add_tail(&task->u.tk_wait.list, q);
167 * Add new request to wait queue.
169 * Swapper tasks always get inserted at the head of the queue.
170 * This should avoid many nasty memory deadlocks and hopefully
171 * improve overall performance.
172 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
174 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
176 BUG_ON (RPC_IS_QUEUED(task));
178 if (RPC_IS_PRIORITY(queue))
179 __rpc_add_wait_queue_priority(queue, task);
180 else if (RPC_IS_SWAPPER(task))
181 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
183 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
184 task->u.tk_wait.rpc_waitq = queue;
185 rpc_set_queued(task);
187 dprintk("RPC: %4d added to queue %p \"%s\"\n",
188 task->tk_pid, queue, rpc_qname(queue));
192 * Remove request from a priority queue.
194 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
198 if (!list_empty(&task->u.tk_wait.links)) {
199 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
200 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
201 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
203 list_del(&task->u.tk_wait.list);
207 * Remove request from queue.
208 * Note: must be called with spin lock held.
210 static void __rpc_remove_wait_queue(struct rpc_task *task)
212 struct rpc_wait_queue *queue;
213 queue = task->u.tk_wait.rpc_waitq;
215 if (RPC_IS_PRIORITY(queue))
216 __rpc_remove_wait_queue_priority(task);
218 list_del(&task->u.tk_wait.list);
219 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
220 task->tk_pid, queue, rpc_qname(queue));
223 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
225 queue->priority = priority;
226 queue->count = 1 << (priority * 2);
229 static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
231 queue->cookie = cookie;
232 queue->nr = RPC_BATCH_COUNT;
235 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
237 rpc_set_waitqueue_priority(queue, queue->maxpriority);
238 rpc_set_waitqueue_cookie(queue, 0);
241 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
245 spin_lock_init(&queue->lock);
246 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
247 INIT_LIST_HEAD(&queue->tasks[i]);
248 queue->maxpriority = maxprio;
249 rpc_reset_waitqueue_priority(queue);
255 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
257 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
260 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
262 __rpc_init_priority_wait_queue(queue, qname, 0);
264 EXPORT_SYMBOL(rpc_init_wait_queue);
266 static int rpc_wait_bit_interruptible(void *word)
268 if (signal_pending(current))
275 * Mark an RPC call as having completed by clearing the 'active' bit
277 static inline void rpc_mark_complete_task(struct rpc_task *task)
279 rpc_clear_active(task);
280 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
284 * Allow callers to wait for completion of an RPC call
286 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
289 action = rpc_wait_bit_interruptible;
290 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
291 action, TASK_INTERRUPTIBLE);
293 EXPORT_SYMBOL(__rpc_wait_for_completion_task);
296 * Make an RPC task runnable.
298 * Note: If the task is ASYNC, this must be called with
299 * the spinlock held to protect the wait queue operation.
301 static void rpc_make_runnable(struct rpc_task *task)
305 BUG_ON(task->tk_timeout_fn);
306 do_ret = rpc_test_and_set_running(task);
307 rpc_clear_queued(task);
310 if (RPC_IS_ASYNC(task)) {
313 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
314 status = queue_work(task->tk_workqueue, &task->u.tk_work);
316 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
317 task->tk_status = status;
321 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
325 * Place a newly initialized task on the workqueue.
328 rpc_schedule_run(struct rpc_task *task)
330 rpc_set_active(task);
331 rpc_make_runnable(task);
335 * Prepare for sleeping on a wait queue.
336 * By always appending tasks to the list we ensure FIFO behavior.
337 * NB: An RPC task will only receive interrupt-driven events as long
338 * as it's on a wait queue.
340 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
341 rpc_action action, rpc_action timer)
343 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
344 rpc_qname(q), jiffies);
346 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
347 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
351 /* Mark the task as being activated if so needed */
352 rpc_set_active(task);
354 __rpc_add_wait_queue(q, task);
356 BUG_ON(task->tk_callback != NULL);
357 task->tk_callback = action;
358 __rpc_add_timer(task, timer);
361 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
362 rpc_action action, rpc_action timer)
365 * Protect the queue operations.
367 spin_lock_bh(&q->lock);
368 __rpc_sleep_on(q, task, action, timer);
369 spin_unlock_bh(&q->lock);
373 * __rpc_do_wake_up_task - wake up a single rpc_task
374 * @task: task to be woken up
376 * Caller must hold queue->lock, and have cleared the task queued flag.
378 static void __rpc_do_wake_up_task(struct rpc_task *task)
380 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
383 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
385 /* Has the task been executed yet? If not, we cannot wake it up! */
386 if (!RPC_IS_ACTIVATED(task)) {
387 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
391 __rpc_disable_timer(task);
392 __rpc_remove_wait_queue(task);
394 rpc_make_runnable(task);
396 dprintk("RPC: __rpc_wake_up_task done\n");
400 * Wake up the specified task
402 static void __rpc_wake_up_task(struct rpc_task *task)
404 if (rpc_start_wakeup(task)) {
405 if (RPC_IS_QUEUED(task))
406 __rpc_do_wake_up_task(task);
407 rpc_finish_wakeup(task);
412 * Default timeout handler if none specified by user
415 __rpc_default_timer(struct rpc_task *task)
417 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
418 task->tk_status = -ETIMEDOUT;
419 rpc_wake_up_task(task);
423 * Wake up the specified task
425 void rpc_wake_up_task(struct rpc_task *task)
427 if (rpc_start_wakeup(task)) {
428 if (RPC_IS_QUEUED(task)) {
429 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
431 spin_lock_bh(&queue->lock);
432 __rpc_do_wake_up_task(task);
433 spin_unlock_bh(&queue->lock);
435 rpc_finish_wakeup(task);
440 * Wake up the next task on a priority queue.
442 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
445 struct rpc_task *task;
448 * Service a batch of tasks from a single cookie.
450 q = &queue->tasks[queue->priority];
451 if (!list_empty(q)) {
452 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
453 if (queue->cookie == task->tk_cookie) {
456 list_move_tail(&task->u.tk_wait.list, q);
459 * Check if we need to switch queues.
466 * Service the next queue.
469 if (q == &queue->tasks[0])
470 q = &queue->tasks[queue->maxpriority];
473 if (!list_empty(q)) {
474 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
477 } while (q != &queue->tasks[queue->priority]);
479 rpc_reset_waitqueue_priority(queue);
483 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
485 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
487 __rpc_wake_up_task(task);
492 * Wake up the next task on the wait queue.
494 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
496 struct rpc_task *task = NULL;
498 dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
499 spin_lock_bh(&queue->lock);
500 if (RPC_IS_PRIORITY(queue))
501 task = __rpc_wake_up_next_priority(queue);
503 task_for_first(task, &queue->tasks[0])
504 __rpc_wake_up_task(task);
506 spin_unlock_bh(&queue->lock);
512 * rpc_wake_up - wake up all rpc_tasks
513 * @queue: rpc_wait_queue on which the tasks are sleeping
517 void rpc_wake_up(struct rpc_wait_queue *queue)
519 struct rpc_task *task, *next;
520 struct list_head *head;
522 spin_lock_bh(&queue->lock);
523 head = &queue->tasks[queue->maxpriority];
525 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
526 __rpc_wake_up_task(task);
527 if (head == &queue->tasks[0])
531 spin_unlock_bh(&queue->lock);
535 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
536 * @queue: rpc_wait_queue on which the tasks are sleeping
537 * @status: status value to set
541 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
543 struct rpc_task *task, *next;
544 struct list_head *head;
546 spin_lock_bh(&queue->lock);
547 head = &queue->tasks[queue->maxpriority];
549 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
550 task->tk_status = status;
551 __rpc_wake_up_task(task);
553 if (head == &queue->tasks[0])
557 spin_unlock_bh(&queue->lock);
561 * Run a task at a later time
563 static void __rpc_atrun(struct rpc_task *);
565 rpc_delay(struct rpc_task *task, unsigned long delay)
567 task->tk_timeout = delay;
568 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
572 __rpc_atrun(struct rpc_task *task)
575 rpc_wake_up_task(task);
579 * Helper to call task->tk_ops->rpc_call_prepare
581 static void rpc_prepare_task(struct rpc_task *task)
583 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
587 * Helper that calls task->tk_ops->rpc_call_done if it exists
589 void rpc_exit_task(struct rpc_task *task)
591 task->tk_action = NULL;
592 if (task->tk_ops->rpc_call_done != NULL) {
593 task->tk_ops->rpc_call_done(task, task->tk_calldata);
594 if (task->tk_action != NULL) {
595 WARN_ON(RPC_ASSASSINATED(task));
596 /* Always release the RPC slot and buffer memory */
601 EXPORT_SYMBOL(rpc_exit_task);
604 * This is the RPC `scheduler' (or rather, the finite state machine).
606 static int __rpc_execute(struct rpc_task *task)
610 dprintk("RPC: %4d rpc_execute flgs %x\n",
611 task->tk_pid, task->tk_flags);
613 BUG_ON(RPC_IS_QUEUED(task));
617 * Garbage collection of pending timers...
619 rpc_delete_timer(task);
622 * Execute any pending callback.
624 if (RPC_DO_CALLBACK(task)) {
625 /* Define a callback save pointer */
626 void (*save_callback)(struct rpc_task *);
629 * If a callback exists, save it, reset it,
631 * The save is needed to stop from resetting
632 * another callback set within the callback handler
635 save_callback=task->tk_callback;
636 task->tk_callback=NULL;
643 * Perform the next FSM step.
644 * tk_action may be NULL when the task has been killed
647 if (!RPC_IS_QUEUED(task)) {
648 if (task->tk_action == NULL)
651 task->tk_action(task);
656 * Lockless check for whether task is sleeping or not.
658 if (!RPC_IS_QUEUED(task))
660 rpc_clear_running(task);
661 if (RPC_IS_ASYNC(task)) {
662 /* Careful! we may have raced... */
663 if (RPC_IS_QUEUED(task))
665 if (rpc_test_and_set_running(task))
670 /* sync task: sleep here */
671 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
672 /* Note: Caller should be using rpc_clnt_sigmask() */
673 status = out_of_line_wait_on_bit(&task->tk_runstate,
674 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
676 if (status == -ERESTARTSYS) {
678 * When a sync task receives a signal, it exits with
679 * -ERESTARTSYS. In order to catch any callbacks that
680 * clean up after sleeping on some queue, we don't
681 * break the loop here, but go around once more.
683 dprintk("RPC: %4d got signal\n", task->tk_pid);
684 task->tk_flags |= RPC_TASK_KILLED;
685 rpc_exit(task, -ERESTARTSYS);
686 rpc_wake_up_task(task);
688 rpc_set_running(task);
689 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
692 dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
693 /* Wake up anyone who is waiting for task completion */
694 rpc_mark_complete_task(task);
695 /* Release all resources associated with the task */
696 rpc_release_task(task);
701 * User-visible entry point to the scheduler.
703 * This may be called recursively if e.g. an async NFS task updates
704 * the attributes and finds that dirty pages must be flushed.
705 * NOTE: Upon exit of this function the task is guaranteed to be
706 * released. In particular note that tk_release() will have
707 * been called, so your task memory may have been freed.
710 rpc_execute(struct rpc_task *task)
712 rpc_set_active(task);
713 rpc_set_running(task);
714 return __rpc_execute(task);
717 static void rpc_async_schedule(void *arg)
719 __rpc_execute((struct rpc_task *)arg);
723 * rpc_malloc - allocate an RPC buffer
724 * @task: RPC task that will use this buffer
725 * @size: requested byte size
727 * We try to ensure that some NFS reads and writes can always proceed
728 * by using a mempool when allocating 'small' buffers.
729 * In order to avoid memory starvation triggering more writebacks of
730 * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
732 void * rpc_malloc(struct rpc_task *task, size_t size)
734 struct rpc_rqst *req = task->tk_rqstp;
737 if (task->tk_flags & RPC_TASK_SWAPPER)
742 if (size > RPC_BUFFER_MAXSIZE) {
743 req->rq_buffer = kmalloc(size, gfp);
745 req->rq_bufsize = size;
747 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
749 req->rq_bufsize = RPC_BUFFER_MAXSIZE;
751 return req->rq_buffer;
755 * rpc_free - free buffer allocated via rpc_malloc
756 * @task: RPC task with a buffer to be freed
759 void rpc_free(struct rpc_task *task)
761 struct rpc_rqst *req = task->tk_rqstp;
763 if (req->rq_buffer) {
764 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
765 mempool_free(req->rq_buffer, rpc_buffer_mempool);
767 kfree(req->rq_buffer);
768 req->rq_buffer = NULL;
774 * Creation and deletion of RPC task structures
776 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
778 memset(task, 0, sizeof(*task));
779 init_timer(&task->tk_timer);
780 task->tk_timer.data = (unsigned long) task;
781 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
782 atomic_set(&task->tk_count, 1);
783 task->tk_client = clnt;
784 task->tk_flags = flags;
785 task->tk_ops = tk_ops;
786 if (tk_ops->rpc_call_prepare != NULL)
787 task->tk_action = rpc_prepare_task;
788 task->tk_calldata = calldata;
790 /* Initialize retry counters */
791 task->tk_garb_retry = 2;
792 task->tk_cred_retry = 2;
794 task->tk_priority = RPC_PRIORITY_NORMAL;
795 task->tk_cookie = (unsigned long)current;
797 /* Initialize workqueue for async tasks */
798 task->tk_workqueue = rpciod_workqueue;
801 atomic_inc(&clnt->cl_users);
802 if (clnt->cl_softrtry)
803 task->tk_flags |= RPC_TASK_SOFT;
805 task->tk_flags |= RPC_TASK_NOINTR;
809 task->tk_magic = RPC_TASK_MAGIC_ID;
810 task->tk_pid = rpc_task_id++;
812 /* Add to global list of all tasks */
813 spin_lock(&rpc_sched_lock);
814 list_add_tail(&task->tk_task, &all_tasks);
815 spin_unlock(&rpc_sched_lock);
817 BUG_ON(task->tk_ops == NULL);
819 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
823 static struct rpc_task *
826 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
829 static void rpc_free_task(struct rpc_task *task)
831 dprintk("RPC: %4d freeing task\n", task->tk_pid);
832 mempool_free(task, rpc_task_mempool);
836 * Create a new task for the specified client. We have to
837 * clean up after an allocation failure, as the client may
838 * have specified "oneshot".
840 struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
842 struct rpc_task *task;
844 task = rpc_alloc_task();
848 rpc_init_task(task, clnt, flags, tk_ops, calldata);
850 dprintk("RPC: %4d allocated task\n", task->tk_pid);
851 task->tk_flags |= RPC_TASK_DYNAMIC;
856 /* Check whether to release the client */
858 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
859 atomic_read(&clnt->cl_users), clnt->cl_oneshot);
860 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
861 rpc_release_client(clnt);
866 void rpc_release_task(struct rpc_task *task)
868 const struct rpc_call_ops *tk_ops = task->tk_ops;
869 void *calldata = task->tk_calldata;
872 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
874 if (!atomic_dec_and_test(&task->tk_count))
876 dprintk("RPC: %4d release task\n", task->tk_pid);
878 /* Remove from global task list */
879 spin_lock(&rpc_sched_lock);
880 list_del(&task->tk_task);
881 spin_unlock(&rpc_sched_lock);
883 BUG_ON (RPC_IS_QUEUED(task));
885 /* Synchronously delete any running timer */
886 rpc_delete_timer(task);
888 /* Release resources */
891 if (task->tk_msg.rpc_cred)
892 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
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.
931 * @parent: parent task
933 * Checks that the parent task is still sleeping on the
934 * queue 'childq'. If so returns a pointer to the parent.
935 * Upon failure returns NULL.
937 * Caller must hold childq.lock
939 static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
941 struct rpc_task *task;
942 struct list_head *le;
944 task_for_each(task, le, &childq.tasks[0])
951 static void rpc_child_exit(struct rpc_task *child, void *calldata)
953 struct rpc_task *parent;
955 spin_lock_bh(&childq.lock);
956 if ((parent = rpc_find_parent(child, calldata)) != NULL) {
957 parent->tk_status = child->tk_status;
958 __rpc_wake_up_task(parent);
960 spin_unlock_bh(&childq.lock);
963 static const struct rpc_call_ops rpc_child_ops = {
964 .rpc_call_done = rpc_child_exit,
968 * Note: rpc_new_task releases the client after a failure.
971 rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
973 struct rpc_task *task;
975 task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
981 parent->tk_status = -ENOMEM;
985 void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
987 spin_lock_bh(&childq.lock);
988 /* N.B. Is it possible for the child to have already finished? */
989 __rpc_sleep_on(&childq, task, func, NULL);
990 rpc_schedule_run(child);
991 spin_unlock_bh(&childq.lock);
995 * Kill all tasks for the given client.
996 * XXX: kill their descendants as well?
998 void rpc_killall_tasks(struct rpc_clnt *clnt)
1000 struct rpc_task *rovr;
1001 struct list_head *le;
1003 dprintk("RPC: killing all tasks for client %p\n", clnt);
1006 * Spin lock all_tasks to prevent changes...
1008 spin_lock(&rpc_sched_lock);
1009 alltask_for_each(rovr, le, &all_tasks) {
1010 if (! RPC_IS_ACTIVATED(rovr))
1012 if (!clnt || rovr->tk_client == clnt) {
1013 rovr->tk_flags |= RPC_TASK_KILLED;
1014 rpc_exit(rovr, -EIO);
1015 rpc_wake_up_task(rovr);
1018 spin_unlock(&rpc_sched_lock);
1021 static DECLARE_MUTEX_LOCKED(rpciod_running);
1023 static void rpciod_killall(void)
1025 unsigned long flags;
1027 while (!list_empty(&all_tasks)) {
1028 clear_thread_flag(TIF_SIGPENDING);
1029 rpc_killall_tasks(NULL);
1030 flush_workqueue(rpciod_workqueue);
1031 if (!list_empty(&all_tasks)) {
1032 dprintk("rpciod_killall: waiting for tasks to exit\n");
1037 spin_lock_irqsave(¤t->sighand->siglock, flags);
1038 recalc_sigpending();
1039 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
1043 * Start up the rpciod process if it's not already running.
1048 struct workqueue_struct *wq;
1051 mutex_lock(&rpciod_mutex);
1052 dprintk("rpciod_up: users %d\n", rpciod_users);
1054 if (rpciod_workqueue)
1057 * If there's no pid, we should be the first user.
1059 if (rpciod_users > 1)
1060 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
1062 * Create the rpciod thread and wait for it to start.
1065 wq = create_workqueue("rpciod");
1067 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
1071 rpciod_workqueue = wq;
1074 mutex_unlock(&rpciod_mutex);
1081 mutex_lock(&rpciod_mutex);
1082 dprintk("rpciod_down sema %d\n", rpciod_users);
1087 printk(KERN_WARNING "rpciod_down: no users??\n");
1089 if (!rpciod_workqueue) {
1090 dprintk("rpciod_down: Nothing to do!\n");
1095 destroy_workqueue(rpciod_workqueue);
1096 rpciod_workqueue = NULL;
1098 mutex_unlock(&rpciod_mutex);
1102 void rpc_show_tasks(void)
1104 struct list_head *le;
1107 spin_lock(&rpc_sched_lock);
1108 if (list_empty(&all_tasks)) {
1109 spin_unlock(&rpc_sched_lock);
1112 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1113 "-rpcwait -action- ---ops--\n");
1114 alltask_for_each(t, le, &all_tasks) {
1115 const char *rpc_waitq = "none";
1117 if (RPC_IS_QUEUED(t))
1118 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1120 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1122 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1123 t->tk_flags, t->tk_status,
1125 (t->tk_client ? t->tk_client->cl_prog : 0),
1126 t->tk_rqstp, t->tk_timeout,
1128 t->tk_action, t->tk_ops);
1130 spin_unlock(&rpc_sched_lock);
1135 rpc_destroy_mempool(void)
1137 if (rpc_buffer_mempool)
1138 mempool_destroy(rpc_buffer_mempool);
1139 if (rpc_task_mempool)
1140 mempool_destroy(rpc_task_mempool);
1141 if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
1142 printk(KERN_INFO "rpc_task: not all structures were freed\n");
1143 if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
1144 printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
1148 rpc_init_mempool(void)
1150 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1151 sizeof(struct rpc_task),
1152 0, SLAB_HWCACHE_ALIGN,
1154 if (!rpc_task_slabp)
1156 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1158 0, SLAB_HWCACHE_ALIGN,
1160 if (!rpc_buffer_slabp)
1162 rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
1166 if (!rpc_task_mempool)
1168 rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
1172 if (!rpc_buffer_mempool)
1176 rpc_destroy_mempool();