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 sit here while waiting for conditions to improve.
50 static RPC_WAITQ(delay_queue, "delayq");
53 * All RPC tasks are linked into this list
55 static LIST_HEAD(all_tasks);
58 * rpciod-related stuff
60 static DEFINE_MUTEX(rpciod_mutex);
61 static unsigned int rpciod_users;
62 struct workqueue_struct *rpciod_workqueue;
65 * Spinlock for other critical sections of code.
67 static DEFINE_SPINLOCK(rpc_sched_lock);
70 * Disable the timer for a given RPC task. Should be called with
71 * queue->lock and bh_disabled in order to avoid races within
75 __rpc_disable_timer(struct rpc_task *task)
77 dprintk("RPC: %4d disabling timer\n", task->tk_pid);
78 task->tk_timeout_fn = NULL;
83 * Run a timeout function.
84 * We use the callback in order to allow __rpc_wake_up_task()
85 * and friends to disable the timer synchronously on SMP systems
86 * without calling del_timer_sync(). The latter could cause a
87 * deadlock if called while we're holding spinlocks...
89 static void rpc_run_timer(struct rpc_task *task)
91 void (*callback)(struct rpc_task *);
93 callback = task->tk_timeout_fn;
94 task->tk_timeout_fn = NULL;
95 if (callback && RPC_IS_QUEUED(task)) {
96 dprintk("RPC: %4d running timer\n", task->tk_pid);
99 smp_mb__before_clear_bit();
100 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
101 smp_mb__after_clear_bit();
105 * Set up a timer for the current task.
108 __rpc_add_timer(struct rpc_task *task, rpc_action timer)
110 if (!task->tk_timeout)
113 dprintk("RPC: %4d setting alarm for %lu ms\n",
114 task->tk_pid, task->tk_timeout * 1000 / HZ);
117 task->tk_timeout_fn = timer;
119 task->tk_timeout_fn = __rpc_default_timer;
120 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
121 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
125 * Delete any timer for the current task. Because we use del_timer_sync(),
126 * this function should never be called while holding queue->lock.
129 rpc_delete_timer(struct rpc_task *task)
131 if (RPC_IS_QUEUED(task))
133 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
134 del_singleshot_timer_sync(&task->tk_timer);
135 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
140 * Add new request to a priority queue.
142 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
147 INIT_LIST_HEAD(&task->u.tk_wait.links);
148 q = &queue->tasks[task->tk_priority];
149 if (unlikely(task->tk_priority > queue->maxpriority))
150 q = &queue->tasks[queue->maxpriority];
151 list_for_each_entry(t, q, u.tk_wait.list) {
152 if (t->tk_cookie == task->tk_cookie) {
153 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
157 list_add_tail(&task->u.tk_wait.list, q);
161 * Add new request to wait queue.
163 * Swapper tasks always get inserted at the head of the queue.
164 * This should avoid many nasty memory deadlocks and hopefully
165 * improve overall performance.
166 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
168 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
170 BUG_ON (RPC_IS_QUEUED(task));
172 if (RPC_IS_PRIORITY(queue))
173 __rpc_add_wait_queue_priority(queue, task);
174 else if (RPC_IS_SWAPPER(task))
175 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
177 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
178 task->u.tk_wait.rpc_waitq = queue;
180 rpc_set_queued(task);
182 dprintk("RPC: %4d added to queue %p \"%s\"\n",
183 task->tk_pid, queue, rpc_qname(queue));
187 * Remove request from a priority queue.
189 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
193 if (!list_empty(&task->u.tk_wait.links)) {
194 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
195 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
196 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
198 list_del(&task->u.tk_wait.list);
202 * Remove request from queue.
203 * Note: must be called with spin lock held.
205 static void __rpc_remove_wait_queue(struct rpc_task *task)
207 struct rpc_wait_queue *queue;
208 queue = task->u.tk_wait.rpc_waitq;
210 if (RPC_IS_PRIORITY(queue))
211 __rpc_remove_wait_queue_priority(task);
213 list_del(&task->u.tk_wait.list);
215 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
216 task->tk_pid, queue, rpc_qname(queue));
219 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
221 queue->priority = priority;
222 queue->count = 1 << (priority * 2);
225 static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
227 queue->cookie = cookie;
228 queue->nr = RPC_BATCH_COUNT;
231 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
233 rpc_set_waitqueue_priority(queue, queue->maxpriority);
234 rpc_set_waitqueue_cookie(queue, 0);
237 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
241 spin_lock_init(&queue->lock);
242 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
243 INIT_LIST_HEAD(&queue->tasks[i]);
244 queue->maxpriority = maxprio;
245 rpc_reset_waitqueue_priority(queue);
251 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
253 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
256 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
258 __rpc_init_priority_wait_queue(queue, qname, 0);
260 EXPORT_SYMBOL(rpc_init_wait_queue);
262 static int rpc_wait_bit_interruptible(void *word)
264 if (signal_pending(current))
271 * Mark an RPC call as having completed by clearing the 'active' bit
273 static inline void rpc_mark_complete_task(struct rpc_task *task)
275 rpc_clear_active(task);
276 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
280 * Allow callers to wait for completion of an RPC call
282 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
285 action = rpc_wait_bit_interruptible;
286 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
287 action, TASK_INTERRUPTIBLE);
289 EXPORT_SYMBOL(__rpc_wait_for_completion_task);
292 * Make an RPC task runnable.
294 * Note: If the task is ASYNC, this must be called with
295 * the spinlock held to protect the wait queue operation.
297 static void rpc_make_runnable(struct rpc_task *task)
301 BUG_ON(task->tk_timeout_fn);
302 do_ret = rpc_test_and_set_running(task);
303 rpc_clear_queued(task);
306 if (RPC_IS_ASYNC(task)) {
309 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
310 status = queue_work(task->tk_workqueue, &task->u.tk_work);
312 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
313 task->tk_status = status;
317 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
321 * Prepare for sleeping on a wait queue.
322 * By always appending tasks to the list we ensure FIFO behavior.
323 * NB: An RPC task will only receive interrupt-driven events as long
324 * as it's on a wait queue.
326 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
327 rpc_action action, rpc_action timer)
329 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
330 rpc_qname(q), jiffies);
332 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
333 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
337 /* Mark the task as being activated if so needed */
338 rpc_set_active(task);
340 __rpc_add_wait_queue(q, task);
342 BUG_ON(task->tk_callback != NULL);
343 task->tk_callback = action;
344 __rpc_add_timer(task, timer);
347 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
348 rpc_action action, rpc_action timer)
351 * Protect the queue operations.
353 spin_lock_bh(&q->lock);
354 __rpc_sleep_on(q, task, action, timer);
355 spin_unlock_bh(&q->lock);
359 * __rpc_do_wake_up_task - wake up a single rpc_task
360 * @task: task to be woken up
362 * Caller must hold queue->lock, and have cleared the task queued flag.
364 static void __rpc_do_wake_up_task(struct rpc_task *task)
366 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
369 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
371 /* Has the task been executed yet? If not, we cannot wake it up! */
372 if (!RPC_IS_ACTIVATED(task)) {
373 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
377 __rpc_disable_timer(task);
378 __rpc_remove_wait_queue(task);
380 rpc_make_runnable(task);
382 dprintk("RPC: __rpc_wake_up_task done\n");
386 * Wake up the specified task
388 static void __rpc_wake_up_task(struct rpc_task *task)
390 if (rpc_start_wakeup(task)) {
391 if (RPC_IS_QUEUED(task))
392 __rpc_do_wake_up_task(task);
393 rpc_finish_wakeup(task);
398 * Default timeout handler if none specified by user
401 __rpc_default_timer(struct rpc_task *task)
403 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
404 task->tk_status = -ETIMEDOUT;
405 rpc_wake_up_task(task);
409 * Wake up the specified task
411 void rpc_wake_up_task(struct rpc_task *task)
413 if (rpc_start_wakeup(task)) {
414 if (RPC_IS_QUEUED(task)) {
415 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
417 spin_lock_bh(&queue->lock);
418 __rpc_do_wake_up_task(task);
419 spin_unlock_bh(&queue->lock);
421 rpc_finish_wakeup(task);
426 * Wake up the next task on a priority queue.
428 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
431 struct rpc_task *task;
434 * Service a batch of tasks from a single cookie.
436 q = &queue->tasks[queue->priority];
437 if (!list_empty(q)) {
438 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
439 if (queue->cookie == task->tk_cookie) {
442 list_move_tail(&task->u.tk_wait.list, q);
445 * Check if we need to switch queues.
452 * Service the next queue.
455 if (q == &queue->tasks[0])
456 q = &queue->tasks[queue->maxpriority];
459 if (!list_empty(q)) {
460 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
463 } while (q != &queue->tasks[queue->priority]);
465 rpc_reset_waitqueue_priority(queue);
469 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
471 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
473 __rpc_wake_up_task(task);
478 * Wake up the next task on the wait queue.
480 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
482 struct rpc_task *task = NULL;
484 dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
485 spin_lock_bh(&queue->lock);
486 if (RPC_IS_PRIORITY(queue))
487 task = __rpc_wake_up_next_priority(queue);
489 task_for_first(task, &queue->tasks[0])
490 __rpc_wake_up_task(task);
492 spin_unlock_bh(&queue->lock);
498 * rpc_wake_up - wake up all rpc_tasks
499 * @queue: rpc_wait_queue on which the tasks are sleeping
503 void rpc_wake_up(struct rpc_wait_queue *queue)
505 struct rpc_task *task, *next;
506 struct list_head *head;
508 spin_lock_bh(&queue->lock);
509 head = &queue->tasks[queue->maxpriority];
511 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
512 __rpc_wake_up_task(task);
513 if (head == &queue->tasks[0])
517 spin_unlock_bh(&queue->lock);
521 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
522 * @queue: rpc_wait_queue on which the tasks are sleeping
523 * @status: status value to set
527 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
529 struct rpc_task *task, *next;
530 struct list_head *head;
532 spin_lock_bh(&queue->lock);
533 head = &queue->tasks[queue->maxpriority];
535 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
536 task->tk_status = status;
537 __rpc_wake_up_task(task);
539 if (head == &queue->tasks[0])
543 spin_unlock_bh(&queue->lock);
547 * Run a task at a later time
549 static void __rpc_atrun(struct rpc_task *);
551 rpc_delay(struct rpc_task *task, unsigned long delay)
553 task->tk_timeout = delay;
554 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
558 __rpc_atrun(struct rpc_task *task)
561 rpc_wake_up_task(task);
565 * Helper to call task->tk_ops->rpc_call_prepare
567 static void rpc_prepare_task(struct rpc_task *task)
569 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
573 * Helper that calls task->tk_ops->rpc_call_done if it exists
575 void rpc_exit_task(struct rpc_task *task)
577 task->tk_action = NULL;
578 if (task->tk_ops->rpc_call_done != NULL) {
579 task->tk_ops->rpc_call_done(task, task->tk_calldata);
580 if (task->tk_action != NULL) {
581 WARN_ON(RPC_ASSASSINATED(task));
582 /* Always release the RPC slot and buffer memory */
587 EXPORT_SYMBOL(rpc_exit_task);
590 * This is the RPC `scheduler' (or rather, the finite state machine).
592 static int __rpc_execute(struct rpc_task *task)
596 dprintk("RPC: %4d rpc_execute flgs %x\n",
597 task->tk_pid, task->tk_flags);
599 BUG_ON(RPC_IS_QUEUED(task));
603 * Garbage collection of pending timers...
605 rpc_delete_timer(task);
608 * Execute any pending callback.
610 if (RPC_DO_CALLBACK(task)) {
611 /* Define a callback save pointer */
612 void (*save_callback)(struct rpc_task *);
615 * If a callback exists, save it, reset it,
617 * The save is needed to stop from resetting
618 * another callback set within the callback handler
621 save_callback=task->tk_callback;
622 task->tk_callback=NULL;
629 * Perform the next FSM step.
630 * tk_action may be NULL when the task has been killed
633 if (!RPC_IS_QUEUED(task)) {
634 if (task->tk_action == NULL)
637 task->tk_action(task);
642 * Lockless check for whether task is sleeping or not.
644 if (!RPC_IS_QUEUED(task))
646 rpc_clear_running(task);
647 if (RPC_IS_ASYNC(task)) {
648 /* Careful! we may have raced... */
649 if (RPC_IS_QUEUED(task))
651 if (rpc_test_and_set_running(task))
656 /* sync task: sleep here */
657 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
658 /* Note: Caller should be using rpc_clnt_sigmask() */
659 status = out_of_line_wait_on_bit(&task->tk_runstate,
660 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
662 if (status == -ERESTARTSYS) {
664 * When a sync task receives a signal, it exits with
665 * -ERESTARTSYS. In order to catch any callbacks that
666 * clean up after sleeping on some queue, we don't
667 * break the loop here, but go around once more.
669 dprintk("RPC: %4d got signal\n", task->tk_pid);
670 task->tk_flags |= RPC_TASK_KILLED;
671 rpc_exit(task, -ERESTARTSYS);
672 rpc_wake_up_task(task);
674 rpc_set_running(task);
675 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
678 dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
679 /* Wake up anyone who is waiting for task completion */
680 rpc_mark_complete_task(task);
681 /* Release all resources associated with the task */
682 rpc_release_task(task);
687 * User-visible entry point to the scheduler.
689 * This may be called recursively if e.g. an async NFS task updates
690 * the attributes and finds that dirty pages must be flushed.
691 * NOTE: Upon exit of this function the task is guaranteed to be
692 * released. In particular note that tk_release() will have
693 * been called, so your task memory may have been freed.
696 rpc_execute(struct rpc_task *task)
698 rpc_set_active(task);
699 rpc_set_running(task);
700 return __rpc_execute(task);
703 static void rpc_async_schedule(void *arg)
705 __rpc_execute((struct rpc_task *)arg);
709 * rpc_malloc - allocate an RPC buffer
710 * @task: RPC task that will use this buffer
711 * @size: requested byte size
713 * We try to ensure that some NFS reads and writes can always proceed
714 * by using a mempool when allocating 'small' buffers.
715 * In order to avoid memory starvation triggering more writebacks of
716 * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
718 void * rpc_malloc(struct rpc_task *task, size_t size)
720 struct rpc_rqst *req = task->tk_rqstp;
723 if (task->tk_flags & RPC_TASK_SWAPPER)
728 if (size > RPC_BUFFER_MAXSIZE) {
729 req->rq_buffer = kmalloc(size, gfp);
731 req->rq_bufsize = size;
733 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
735 req->rq_bufsize = RPC_BUFFER_MAXSIZE;
737 return req->rq_buffer;
741 * rpc_free - free buffer allocated via rpc_malloc
742 * @task: RPC task with a buffer to be freed
745 void rpc_free(struct rpc_task *task)
747 struct rpc_rqst *req = task->tk_rqstp;
749 if (req->rq_buffer) {
750 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
751 mempool_free(req->rq_buffer, rpc_buffer_mempool);
753 kfree(req->rq_buffer);
754 req->rq_buffer = NULL;
760 * Creation and deletion of RPC task structures
762 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
764 memset(task, 0, sizeof(*task));
765 init_timer(&task->tk_timer);
766 task->tk_timer.data = (unsigned long) task;
767 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
768 atomic_set(&task->tk_count, 1);
769 task->tk_client = clnt;
770 task->tk_flags = flags;
771 task->tk_ops = tk_ops;
772 if (tk_ops->rpc_call_prepare != NULL)
773 task->tk_action = rpc_prepare_task;
774 task->tk_calldata = calldata;
776 /* Initialize retry counters */
777 task->tk_garb_retry = 2;
778 task->tk_cred_retry = 2;
780 task->tk_priority = RPC_PRIORITY_NORMAL;
781 task->tk_cookie = (unsigned long)current;
783 /* Initialize workqueue for async tasks */
784 task->tk_workqueue = rpciod_workqueue;
787 atomic_inc(&clnt->cl_users);
788 if (clnt->cl_softrtry)
789 task->tk_flags |= RPC_TASK_SOFT;
791 task->tk_flags |= RPC_TASK_NOINTR;
795 task->tk_magic = RPC_TASK_MAGIC_ID;
796 task->tk_pid = rpc_task_id++;
798 /* Add to global list of all tasks */
799 spin_lock(&rpc_sched_lock);
800 list_add_tail(&task->tk_task, &all_tasks);
801 spin_unlock(&rpc_sched_lock);
803 BUG_ON(task->tk_ops == NULL);
805 /* starting timestamp */
806 task->tk_start = jiffies;
808 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
812 static struct rpc_task *
815 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
818 static void rpc_free_task(struct rpc_task *task)
820 dprintk("RPC: %4d freeing task\n", task->tk_pid);
821 mempool_free(task, rpc_task_mempool);
825 * Create a new task for the specified client. We have to
826 * clean up after an allocation failure, as the client may
827 * have specified "oneshot".
829 struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
831 struct rpc_task *task;
833 task = rpc_alloc_task();
837 rpc_init_task(task, clnt, flags, tk_ops, calldata);
839 dprintk("RPC: %4d allocated task\n", task->tk_pid);
840 task->tk_flags |= RPC_TASK_DYNAMIC;
845 /* Check whether to release the client */
847 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
848 atomic_read(&clnt->cl_users), clnt->cl_oneshot);
849 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
850 rpc_release_client(clnt);
855 void rpc_release_task(struct rpc_task *task)
857 const struct rpc_call_ops *tk_ops = task->tk_ops;
858 void *calldata = task->tk_calldata;
861 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
863 if (!atomic_dec_and_test(&task->tk_count))
865 dprintk("RPC: %4d release task\n", task->tk_pid);
867 /* Remove from global task list */
868 spin_lock(&rpc_sched_lock);
869 list_del(&task->tk_task);
870 spin_unlock(&rpc_sched_lock);
872 BUG_ON (RPC_IS_QUEUED(task));
874 /* Synchronously delete any running timer */
875 rpc_delete_timer(task);
877 /* Release resources */
880 if (task->tk_msg.rpc_cred)
881 rpcauth_unbindcred(task);
882 if (task->tk_client) {
883 rpc_release_client(task->tk_client);
884 task->tk_client = NULL;
890 if (task->tk_flags & RPC_TASK_DYNAMIC)
892 if (tk_ops->rpc_release)
893 tk_ops->rpc_release(calldata);
897 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
898 * @clnt: pointer to RPC client
901 * @data: user call data
903 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
904 const struct rpc_call_ops *ops,
907 struct rpc_task *task;
908 task = rpc_new_task(clnt, flags, ops, data);
910 if (ops->rpc_release != NULL)
911 ops->rpc_release(data);
912 return ERR_PTR(-ENOMEM);
914 atomic_inc(&task->tk_count);
918 EXPORT_SYMBOL(rpc_run_task);
921 * Kill all tasks for the given client.
922 * XXX: kill their descendants as well?
924 void rpc_killall_tasks(struct rpc_clnt *clnt)
926 struct rpc_task *rovr;
927 struct list_head *le;
929 dprintk("RPC: killing all tasks for client %p\n", clnt);
932 * Spin lock all_tasks to prevent changes...
934 spin_lock(&rpc_sched_lock);
935 alltask_for_each(rovr, le, &all_tasks) {
936 if (! RPC_IS_ACTIVATED(rovr))
938 if (!clnt || rovr->tk_client == clnt) {
939 rovr->tk_flags |= RPC_TASK_KILLED;
940 rpc_exit(rovr, -EIO);
941 rpc_wake_up_task(rovr);
944 spin_unlock(&rpc_sched_lock);
947 static DECLARE_MUTEX_LOCKED(rpciod_running);
949 static void rpciod_killall(void)
953 while (!list_empty(&all_tasks)) {
954 clear_thread_flag(TIF_SIGPENDING);
955 rpc_killall_tasks(NULL);
956 flush_workqueue(rpciod_workqueue);
957 if (!list_empty(&all_tasks)) {
958 dprintk("rpciod_killall: waiting for tasks to exit\n");
963 spin_lock_irqsave(¤t->sighand->siglock, flags);
965 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
969 * Start up the rpciod process if it's not already running.
974 struct workqueue_struct *wq;
977 mutex_lock(&rpciod_mutex);
978 dprintk("rpciod_up: users %d\n", rpciod_users);
980 if (rpciod_workqueue)
983 * If there's no pid, we should be the first user.
985 if (rpciod_users > 1)
986 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
988 * Create the rpciod thread and wait for it to start.
991 wq = create_workqueue("rpciod");
993 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
997 rpciod_workqueue = wq;
1000 mutex_unlock(&rpciod_mutex);
1007 mutex_lock(&rpciod_mutex);
1008 dprintk("rpciod_down sema %d\n", rpciod_users);
1013 printk(KERN_WARNING "rpciod_down: no users??\n");
1015 if (!rpciod_workqueue) {
1016 dprintk("rpciod_down: Nothing to do!\n");
1021 destroy_workqueue(rpciod_workqueue);
1022 rpciod_workqueue = NULL;
1024 mutex_unlock(&rpciod_mutex);
1028 void rpc_show_tasks(void)
1030 struct list_head *le;
1033 spin_lock(&rpc_sched_lock);
1034 if (list_empty(&all_tasks)) {
1035 spin_unlock(&rpc_sched_lock);
1038 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1039 "-rpcwait -action- ---ops--\n");
1040 alltask_for_each(t, le, &all_tasks) {
1041 const char *rpc_waitq = "none";
1043 if (RPC_IS_QUEUED(t))
1044 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1046 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1048 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1049 t->tk_flags, t->tk_status,
1051 (t->tk_client ? t->tk_client->cl_prog : 0),
1052 t->tk_rqstp, t->tk_timeout,
1054 t->tk_action, t->tk_ops);
1056 spin_unlock(&rpc_sched_lock);
1061 rpc_destroy_mempool(void)
1063 if (rpc_buffer_mempool)
1064 mempool_destroy(rpc_buffer_mempool);
1065 if (rpc_task_mempool)
1066 mempool_destroy(rpc_task_mempool);
1067 if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
1068 printk(KERN_INFO "rpc_task: not all structures were freed\n");
1069 if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
1070 printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
1074 rpc_init_mempool(void)
1076 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1077 sizeof(struct rpc_task),
1078 0, SLAB_HWCACHE_ALIGN,
1080 if (!rpc_task_slabp)
1082 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1084 0, SLAB_HWCACHE_ALIGN,
1086 if (!rpc_buffer_slabp)
1088 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1090 if (!rpc_task_mempool)
1092 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1094 if (!rpc_buffer_mempool)
1098 rpc_destroy_mempool();