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>
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_async_schedule(void *);
47 * RPC tasks sit here while waiting for conditions to improve.
49 static RPC_WAITQ(delay_queue, "delayq");
52 * All RPC tasks are linked into this list
54 static LIST_HEAD(all_tasks);
57 * rpciod-related stuff
59 static DEFINE_MUTEX(rpciod_mutex);
60 static unsigned int rpciod_users;
61 struct workqueue_struct *rpciod_workqueue;
64 * Spinlock for other critical sections of code.
66 static DEFINE_SPINLOCK(rpc_sched_lock);
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
74 __rpc_disable_timer(struct rpc_task *task)
76 dprintk("RPC: %4d disabling timer\n", task->tk_pid);
77 task->tk_timeout_fn = NULL;
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...
88 static void rpc_run_timer(struct rpc_task *task)
90 void (*callback)(struct rpc_task *);
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);
98 smp_mb__before_clear_bit();
99 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
100 smp_mb__after_clear_bit();
104 * Set up a timer for the current task.
107 __rpc_add_timer(struct rpc_task *task, rpc_action timer)
109 if (!task->tk_timeout)
112 dprintk("RPC: %4d setting alarm for %lu ms\n",
113 task->tk_pid, task->tk_timeout * 1000 / HZ);
116 task->tk_timeout_fn = timer;
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);
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.
128 rpc_delete_timer(struct rpc_task *task)
130 if (RPC_IS_QUEUED(task))
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);
139 * Add new request to a priority queue.
141 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
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);
156 list_add_tail(&task->u.tk_wait.list, q);
160 * Add new request to wait queue.
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.
167 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
169 BUG_ON (RPC_IS_QUEUED(task));
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]);
176 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
177 task->u.tk_wait.rpc_waitq = queue;
179 rpc_set_queued(task);
181 dprintk("RPC: %4d added to queue %p \"%s\"\n",
182 task->tk_pid, queue, rpc_qname(queue));
186 * Remove request from a priority queue.
188 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
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);
197 list_del(&task->u.tk_wait.list);
201 * Remove request from queue.
202 * Note: must be called with spin lock held.
204 static void __rpc_remove_wait_queue(struct rpc_task *task)
206 struct rpc_wait_queue *queue;
207 queue = task->u.tk_wait.rpc_waitq;
209 if (RPC_IS_PRIORITY(queue))
210 __rpc_remove_wait_queue_priority(task);
212 list_del(&task->u.tk_wait.list);
214 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
215 task->tk_pid, queue, rpc_qname(queue));
218 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
220 queue->priority = priority;
221 queue->count = 1 << (priority * 2);
224 static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
226 queue->cookie = cookie;
227 queue->nr = RPC_BATCH_COUNT;
230 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
232 rpc_set_waitqueue_priority(queue, queue->maxpriority);
233 rpc_set_waitqueue_cookie(queue, 0);
236 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
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);
250 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
252 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
255 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
257 __rpc_init_priority_wait_queue(queue, qname, 0);
259 EXPORT_SYMBOL(rpc_init_wait_queue);
261 static int rpc_wait_bit_interruptible(void *word)
263 if (signal_pending(current))
270 * Mark an RPC call as having completed by clearing the 'active' bit
272 static inline void rpc_mark_complete_task(struct rpc_task *task)
274 rpc_clear_active(task);
275 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
279 * Allow callers to wait for completion of an RPC call
281 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
284 action = rpc_wait_bit_interruptible;
285 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
286 action, TASK_INTERRUPTIBLE);
288 EXPORT_SYMBOL(__rpc_wait_for_completion_task);
291 * Make an RPC task runnable.
293 * Note: If the task is ASYNC, this must be called with
294 * the spinlock held to protect the wait queue operation.
296 static void rpc_make_runnable(struct rpc_task *task)
298 BUG_ON(task->tk_timeout_fn);
299 rpc_clear_queued(task);
300 if (rpc_test_and_set_running(task))
302 /* We might have raced */
303 if (RPC_IS_QUEUED(task)) {
304 rpc_clear_running(task);
307 if (RPC_IS_ASYNC(task)) {
310 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
311 status = queue_work(task->tk_workqueue, &task->u.tk_work);
313 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
314 task->tk_status = status;
318 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
322 * 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.
327 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
328 rpc_action action, rpc_action timer)
330 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
331 rpc_qname(q), jiffies);
333 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
334 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
338 /* Mark the task as being activated if so needed */
339 rpc_set_active(task);
341 __rpc_add_wait_queue(q, task);
343 BUG_ON(task->tk_callback != NULL);
344 task->tk_callback = action;
345 __rpc_add_timer(task, timer);
348 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
349 rpc_action action, rpc_action timer)
352 * Protect the queue operations.
354 spin_lock_bh(&q->lock);
355 __rpc_sleep_on(q, task, action, timer);
356 spin_unlock_bh(&q->lock);
360 * __rpc_do_wake_up_task - wake up a single rpc_task
361 * @task: task to be woken up
363 * Caller must hold queue->lock, and have cleared the task queued flag.
365 static void __rpc_do_wake_up_task(struct rpc_task *task)
367 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
370 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
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);
378 __rpc_disable_timer(task);
379 __rpc_remove_wait_queue(task);
381 rpc_make_runnable(task);
383 dprintk("RPC: __rpc_wake_up_task done\n");
387 * Wake up the specified task
389 static void __rpc_wake_up_task(struct rpc_task *task)
391 if (rpc_start_wakeup(task)) {
392 if (RPC_IS_QUEUED(task))
393 __rpc_do_wake_up_task(task);
394 rpc_finish_wakeup(task);
399 * Default timeout handler if none specified by user
402 __rpc_default_timer(struct rpc_task *task)
404 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
405 task->tk_status = -ETIMEDOUT;
406 rpc_wake_up_task(task);
410 * Wake up the specified task
412 void rpc_wake_up_task(struct rpc_task *task)
414 if (rpc_start_wakeup(task)) {
415 if (RPC_IS_QUEUED(task)) {
416 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
418 spin_lock_bh(&queue->lock);
419 __rpc_do_wake_up_task(task);
420 spin_unlock_bh(&queue->lock);
422 rpc_finish_wakeup(task);
427 * Wake up the next task on a priority queue.
429 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
432 struct rpc_task *task;
435 * Service a batch of tasks from a single cookie.
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) {
443 list_move_tail(&task->u.tk_wait.list, q);
446 * Check if we need to switch queues.
453 * Service the next queue.
456 if (q == &queue->tasks[0])
457 q = &queue->tasks[queue->maxpriority];
460 if (!list_empty(q)) {
461 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
464 } while (q != &queue->tasks[queue->priority]);
466 rpc_reset_waitqueue_priority(queue);
470 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
472 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
474 __rpc_wake_up_task(task);
479 * Wake up the next task on the wait queue.
481 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
483 struct rpc_task *task = NULL;
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);
490 task_for_first(task, &queue->tasks[0])
491 __rpc_wake_up_task(task);
493 spin_unlock_bh(&queue->lock);
499 * rpc_wake_up - wake up all rpc_tasks
500 * @queue: rpc_wait_queue on which the tasks are sleeping
504 void rpc_wake_up(struct rpc_wait_queue *queue)
506 struct rpc_task *task, *next;
507 struct list_head *head;
509 spin_lock_bh(&queue->lock);
510 head = &queue->tasks[queue->maxpriority];
512 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
513 __rpc_wake_up_task(task);
514 if (head == &queue->tasks[0])
518 spin_unlock_bh(&queue->lock);
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
528 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
530 struct rpc_task *task, *next;
531 struct list_head *head;
533 spin_lock_bh(&queue->lock);
534 head = &queue->tasks[queue->maxpriority];
536 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
537 task->tk_status = status;
538 __rpc_wake_up_task(task);
540 if (head == &queue->tasks[0])
544 spin_unlock_bh(&queue->lock);
547 static void __rpc_atrun(struct rpc_task *task)
549 rpc_wake_up_task(task);
553 * Run a task at a later time
555 void rpc_delay(struct rpc_task *task, unsigned long delay)
557 task->tk_timeout = delay;
558 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
562 * Helper to call task->tk_ops->rpc_call_prepare
564 static void rpc_prepare_task(struct rpc_task *task)
566 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
570 * Helper that calls task->tk_ops->rpc_call_done if it exists
572 void rpc_exit_task(struct rpc_task *task)
574 task->tk_action = NULL;
575 if (task->tk_ops->rpc_call_done != NULL) {
576 task->tk_ops->rpc_call_done(task, task->tk_calldata);
577 if (task->tk_action != NULL) {
578 WARN_ON(RPC_ASSASSINATED(task));
579 /* Always release the RPC slot and buffer memory */
584 EXPORT_SYMBOL(rpc_exit_task);
587 * This is the RPC `scheduler' (or rather, the finite state machine).
589 static int __rpc_execute(struct rpc_task *task)
593 dprintk("RPC: %4d rpc_execute flgs %x\n",
594 task->tk_pid, task->tk_flags);
596 BUG_ON(RPC_IS_QUEUED(task));
600 * Garbage collection of pending timers...
602 rpc_delete_timer(task);
605 * Execute any pending callback.
607 if (RPC_DO_CALLBACK(task)) {
608 /* Define a callback save pointer */
609 void (*save_callback)(struct rpc_task *);
612 * If a callback exists, save it, reset it,
614 * The save is needed to stop from resetting
615 * another callback set within the callback handler
618 save_callback=task->tk_callback;
619 task->tk_callback=NULL;
626 * Perform the next FSM step.
627 * tk_action may be NULL when the task has been killed
630 if (!RPC_IS_QUEUED(task)) {
631 if (task->tk_action == NULL)
634 task->tk_action(task);
639 * Lockless check for whether task is sleeping or not.
641 if (!RPC_IS_QUEUED(task))
643 rpc_clear_running(task);
644 if (RPC_IS_ASYNC(task)) {
645 /* Careful! we may have raced... */
646 if (RPC_IS_QUEUED(task))
648 if (rpc_test_and_set_running(task))
653 /* sync task: sleep here */
654 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
655 /* 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,
659 if (status == -ERESTARTSYS) {
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.
666 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);
671 rpc_set_running(task);
672 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
675 dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
676 /* Wake up anyone who is waiting for task completion */
677 rpc_mark_complete_task(task);
678 /* Release all resources associated with the task */
679 rpc_release_task(task);
684 * User-visible entry point to the scheduler.
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.
693 rpc_execute(struct rpc_task *task)
695 rpc_set_active(task);
696 rpc_set_running(task);
697 return __rpc_execute(task);
700 static void rpc_async_schedule(void *arg)
702 __rpc_execute((struct rpc_task *)arg);
706 * rpc_malloc - allocate an RPC buffer
707 * @task: RPC task that will use this buffer
708 * @size: requested byte size
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.
715 void * rpc_malloc(struct rpc_task *task, size_t size)
717 struct rpc_rqst *req = task->tk_rqstp;
720 if (task->tk_flags & RPC_TASK_SWAPPER)
725 if (size > RPC_BUFFER_MAXSIZE) {
726 req->rq_buffer = kmalloc(size, gfp);
728 req->rq_bufsize = size;
730 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
732 req->rq_bufsize = RPC_BUFFER_MAXSIZE;
734 return req->rq_buffer;
738 * rpc_free - free buffer allocated via rpc_malloc
739 * @task: RPC task with a buffer to be freed
742 void rpc_free(struct rpc_task *task)
744 struct rpc_rqst *req = task->tk_rqstp;
746 if (req->rq_buffer) {
747 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
748 mempool_free(req->rq_buffer, rpc_buffer_mempool);
750 kfree(req->rq_buffer);
751 req->rq_buffer = NULL;
757 * Creation and deletion of RPC task structures
759 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
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;
765 atomic_set(&task->tk_count, 1);
766 task->tk_client = clnt;
767 task->tk_flags = flags;
768 task->tk_ops = tk_ops;
769 if (tk_ops->rpc_call_prepare != NULL)
770 task->tk_action = rpc_prepare_task;
771 task->tk_calldata = calldata;
773 /* Initialize retry counters */
774 task->tk_garb_retry = 2;
775 task->tk_cred_retry = 2;
777 task->tk_priority = RPC_PRIORITY_NORMAL;
778 task->tk_cookie = (unsigned long)current;
780 /* Initialize workqueue for async tasks */
781 task->tk_workqueue = rpciod_workqueue;
784 atomic_inc(&clnt->cl_users);
785 if (clnt->cl_softrtry)
786 task->tk_flags |= RPC_TASK_SOFT;
788 task->tk_flags |= RPC_TASK_NOINTR;
792 task->tk_magic = RPC_TASK_MAGIC_ID;
793 task->tk_pid = rpc_task_id++;
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);
800 BUG_ON(task->tk_ops == NULL);
802 /* starting timestamp */
803 task->tk_start = jiffies;
805 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
809 static struct rpc_task *
812 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
815 static void rpc_free_task(struct rpc_task *task)
817 dprintk("RPC: %4d freeing task\n", task->tk_pid);
818 mempool_free(task, rpc_task_mempool);
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".
826 struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
828 struct rpc_task *task;
830 task = rpc_alloc_task();
834 rpc_init_task(task, clnt, flags, tk_ops, calldata);
836 dprintk("RPC: %4d allocated task\n", task->tk_pid);
837 task->tk_flags |= RPC_TASK_DYNAMIC;
842 /* Check whether to release the client */
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);
852 void rpc_release_task(struct rpc_task *task)
854 const struct rpc_call_ops *tk_ops = task->tk_ops;
855 void *calldata = task->tk_calldata;
858 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
860 if (!atomic_dec_and_test(&task->tk_count))
862 dprintk("RPC: %4d release task\n", task->tk_pid);
864 /* Remove from global task list */
865 spin_lock(&rpc_sched_lock);
866 list_del(&task->tk_task);
867 spin_unlock(&rpc_sched_lock);
869 BUG_ON (RPC_IS_QUEUED(task));
871 /* Synchronously delete any running timer */
872 rpc_delete_timer(task);
874 /* Release resources */
877 if (task->tk_msg.rpc_cred)
878 rpcauth_unbindcred(task);
879 if (task->tk_client) {
880 rpc_release_client(task->tk_client);
881 task->tk_client = NULL;
887 if (task->tk_flags & RPC_TASK_DYNAMIC)
889 if (tk_ops->rpc_release)
890 tk_ops->rpc_release(calldata);
894 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
895 * @clnt: pointer to RPC client
898 * @data: user call data
900 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
901 const struct rpc_call_ops *ops,
904 struct rpc_task *task;
905 task = rpc_new_task(clnt, flags, ops, data);
907 if (ops->rpc_release != NULL)
908 ops->rpc_release(data);
909 return ERR_PTR(-ENOMEM);
911 atomic_inc(&task->tk_count);
915 EXPORT_SYMBOL(rpc_run_task);
918 * Kill all tasks for the given client.
919 * XXX: kill their descendants as well?
921 void rpc_killall_tasks(struct rpc_clnt *clnt)
923 struct rpc_task *rovr;
924 struct list_head *le;
926 dprintk("RPC: killing all tasks for client %p\n", clnt);
929 * Spin lock all_tasks to prevent changes...
931 spin_lock(&rpc_sched_lock);
932 alltask_for_each(rovr, le, &all_tasks) {
933 if (! RPC_IS_ACTIVATED(rovr))
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);
941 spin_unlock(&rpc_sched_lock);
944 static DECLARE_MUTEX_LOCKED(rpciod_running);
946 static void rpciod_killall(void)
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");
960 spin_lock_irqsave(¤t->sighand->siglock, flags);
962 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
966 * Start up the rpciod process if it's not already running.
971 struct workqueue_struct *wq;
974 mutex_lock(&rpciod_mutex);
975 dprintk("rpciod_up: users %d\n", rpciod_users);
977 if (rpciod_workqueue)
980 * If there's no pid, we should be the first user.
982 if (rpciod_users > 1)
983 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
985 * Create the rpciod thread and wait for it to start.
988 wq = create_workqueue("rpciod");
990 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
994 rpciod_workqueue = wq;
997 mutex_unlock(&rpciod_mutex);
1004 mutex_lock(&rpciod_mutex);
1005 dprintk("rpciod_down sema %d\n", rpciod_users);
1010 printk(KERN_WARNING "rpciod_down: no users??\n");
1012 if (!rpciod_workqueue) {
1013 dprintk("rpciod_down: Nothing to do!\n");
1018 destroy_workqueue(rpciod_workqueue);
1019 rpciod_workqueue = NULL;
1021 mutex_unlock(&rpciod_mutex);
1025 void rpc_show_tasks(void)
1027 struct list_head *le;
1030 spin_lock(&rpc_sched_lock);
1031 if (list_empty(&all_tasks)) {
1032 spin_unlock(&rpc_sched_lock);
1035 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1036 "-rpcwait -action- ---ops--\n");
1037 alltask_for_each(t, le, &all_tasks) {
1038 const char *rpc_waitq = "none";
1040 if (RPC_IS_QUEUED(t))
1041 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1043 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1045 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1046 t->tk_flags, t->tk_status,
1048 (t->tk_client ? t->tk_client->cl_prog : 0),
1049 t->tk_rqstp, t->tk_timeout,
1051 t->tk_action, t->tk_ops);
1053 spin_unlock(&rpc_sched_lock);
1058 rpc_destroy_mempool(void)
1060 if (rpc_buffer_mempool)
1061 mempool_destroy(rpc_buffer_mempool);
1062 if (rpc_task_mempool)
1063 mempool_destroy(rpc_task_mempool);
1065 kmem_cache_destroy(rpc_task_slabp);
1066 if (rpc_buffer_slabp)
1067 kmem_cache_destroy(rpc_buffer_slabp);
1071 rpc_init_mempool(void)
1073 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1074 sizeof(struct rpc_task),
1075 0, SLAB_HWCACHE_ALIGN,
1077 if (!rpc_task_slabp)
1079 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1081 0, SLAB_HWCACHE_ALIGN,
1083 if (!rpc_buffer_slabp)
1085 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1087 if (!rpc_task_mempool)
1089 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1091 if (!rpc_buffer_mempool)
1095 rpc_destroy_mempool();