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
31 * RPC slabs and memory pools
33 #define RPC_BUFFER_MAXSIZE (2048)
34 #define RPC_BUFFER_POOLSIZE (8)
35 #define RPC_TASK_POOLSIZE (8)
36 static struct kmem_cache *rpc_task_slabp __read_mostly;
37 static struct kmem_cache *rpc_buffer_slabp __read_mostly;
38 static mempool_t *rpc_task_mempool __read_mostly;
39 static mempool_t *rpc_buffer_mempool __read_mostly;
41 static void __rpc_default_timer(struct rpc_task *task);
42 static void rpciod_killall(void);
43 static void rpc_async_schedule(struct work_struct *);
44 static void rpc_release_task(struct rpc_task *task);
47 * RPC tasks sit here while waiting for conditions to improve.
49 static RPC_WAITQ(delay_queue, "delayq");
52 * All RPC clients are linked into this list
54 static LIST_HEAD(all_clients);
55 static DECLARE_WAIT_QUEUE_HEAD(client_kill_wait);
58 * rpciod-related stuff
60 static DEFINE_MUTEX(rpciod_mutex);
61 static atomic_t rpciod_users = ATOMIC_INIT(0);
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: %5u 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: %5u 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: %5u 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: %5u 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: %5u 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: %5u 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 static void rpc_task_set_debuginfo(struct rpc_task *task)
273 static atomic_t rpc_pid;
275 task->tk_magic = RPC_TASK_MAGIC_ID;
276 task->tk_pid = atomic_inc_return(&rpc_pid);
279 static inline void rpc_task_set_debuginfo(struct rpc_task *task)
284 static void rpc_set_active(struct rpc_task *task)
286 struct rpc_clnt *clnt;
287 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
289 rpc_task_set_debuginfo(task);
290 /* Add to global list of all tasks */
291 clnt = task->tk_client;
293 spin_lock(&clnt->cl_lock);
294 list_add_tail(&task->tk_task, &clnt->cl_tasks);
295 spin_unlock(&clnt->cl_lock);
300 * Mark an RPC call as having completed by clearing the 'active' bit
302 static void rpc_mark_complete_task(struct rpc_task *task)
304 smp_mb__before_clear_bit();
305 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
306 smp_mb__after_clear_bit();
307 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
311 * Allow callers to wait for completion of an RPC call
313 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
316 action = rpc_wait_bit_interruptible;
317 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
318 action, TASK_INTERRUPTIBLE);
320 EXPORT_SYMBOL(__rpc_wait_for_completion_task);
323 * Make an RPC task runnable.
325 * Note: If the task is ASYNC, this must be called with
326 * the spinlock held to protect the wait queue operation.
328 static void rpc_make_runnable(struct rpc_task *task)
330 BUG_ON(task->tk_timeout_fn);
331 rpc_clear_queued(task);
332 if (rpc_test_and_set_running(task))
334 /* We might have raced */
335 if (RPC_IS_QUEUED(task)) {
336 rpc_clear_running(task);
339 if (RPC_IS_ASYNC(task)) {
342 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
343 status = queue_work(task->tk_workqueue, &task->u.tk_work);
345 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
346 task->tk_status = status;
350 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
354 * Prepare for sleeping on a wait queue.
355 * By always appending tasks to the list we ensure FIFO behavior.
356 * NB: An RPC task will only receive interrupt-driven events as long
357 * as it's on a wait queue.
359 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
360 rpc_action action, rpc_action timer)
362 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
363 task->tk_pid, rpc_qname(q), jiffies);
365 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
366 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
370 __rpc_add_wait_queue(q, task);
372 BUG_ON(task->tk_callback != NULL);
373 task->tk_callback = action;
374 __rpc_add_timer(task, timer);
377 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
378 rpc_action action, rpc_action timer)
380 /* Mark the task as being activated if so needed */
381 rpc_set_active(task);
384 * Protect the queue operations.
386 spin_lock_bh(&q->lock);
387 __rpc_sleep_on(q, task, action, timer);
388 spin_unlock_bh(&q->lock);
392 * __rpc_do_wake_up_task - wake up a single rpc_task
393 * @task: task to be woken up
395 * Caller must hold queue->lock, and have cleared the task queued flag.
397 static void __rpc_do_wake_up_task(struct rpc_task *task)
399 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
400 task->tk_pid, jiffies);
403 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
405 /* Has the task been executed yet? If not, we cannot wake it up! */
406 if (!RPC_IS_ACTIVATED(task)) {
407 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
411 __rpc_disable_timer(task);
412 __rpc_remove_wait_queue(task);
414 rpc_make_runnable(task);
416 dprintk("RPC: __rpc_wake_up_task done\n");
420 * Wake up the specified task
422 static void __rpc_wake_up_task(struct rpc_task *task)
424 if (rpc_start_wakeup(task)) {
425 if (RPC_IS_QUEUED(task))
426 __rpc_do_wake_up_task(task);
427 rpc_finish_wakeup(task);
432 * Default timeout handler if none specified by user
435 __rpc_default_timer(struct rpc_task *task)
437 dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid);
438 task->tk_status = -ETIMEDOUT;
439 rpc_wake_up_task(task);
443 * Wake up the specified task
445 void rpc_wake_up_task(struct rpc_task *task)
448 if (rpc_start_wakeup(task)) {
449 if (RPC_IS_QUEUED(task)) {
450 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
452 /* Note: we're already in a bh-safe context */
453 spin_lock(&queue->lock);
454 __rpc_do_wake_up_task(task);
455 spin_unlock(&queue->lock);
457 rpc_finish_wakeup(task);
459 rcu_read_unlock_bh();
463 * Wake up the next task on a priority queue.
465 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
468 struct rpc_task *task;
471 * Service a batch of tasks from a single cookie.
473 q = &queue->tasks[queue->priority];
474 if (!list_empty(q)) {
475 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
476 if (queue->cookie == task->tk_cookie) {
479 list_move_tail(&task->u.tk_wait.list, q);
482 * Check if we need to switch queues.
489 * Service the next queue.
492 if (q == &queue->tasks[0])
493 q = &queue->tasks[queue->maxpriority];
496 if (!list_empty(q)) {
497 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
500 } while (q != &queue->tasks[queue->priority]);
502 rpc_reset_waitqueue_priority(queue);
506 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
508 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
510 __rpc_wake_up_task(task);
515 * Wake up the next task on the wait queue.
517 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
519 struct rpc_task *task = NULL;
521 dprintk("RPC: wake_up_next(%p \"%s\")\n",
522 queue, rpc_qname(queue));
524 spin_lock(&queue->lock);
525 if (RPC_IS_PRIORITY(queue))
526 task = __rpc_wake_up_next_priority(queue);
528 task_for_first(task, &queue->tasks[0])
529 __rpc_wake_up_task(task);
531 spin_unlock(&queue->lock);
532 rcu_read_unlock_bh();
538 * rpc_wake_up - wake up all rpc_tasks
539 * @queue: rpc_wait_queue on which the tasks are sleeping
543 void rpc_wake_up(struct rpc_wait_queue *queue)
545 struct rpc_task *task, *next;
546 struct list_head *head;
549 spin_lock(&queue->lock);
550 head = &queue->tasks[queue->maxpriority];
552 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
553 __rpc_wake_up_task(task);
554 if (head == &queue->tasks[0])
558 spin_unlock(&queue->lock);
559 rcu_read_unlock_bh();
563 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
564 * @queue: rpc_wait_queue on which the tasks are sleeping
565 * @status: status value to set
569 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
571 struct rpc_task *task, *next;
572 struct list_head *head;
575 spin_lock(&queue->lock);
576 head = &queue->tasks[queue->maxpriority];
578 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
579 task->tk_status = status;
580 __rpc_wake_up_task(task);
582 if (head == &queue->tasks[0])
586 spin_unlock(&queue->lock);
587 rcu_read_unlock_bh();
590 static void __rpc_atrun(struct rpc_task *task)
592 rpc_wake_up_task(task);
596 * Run a task at a later time
598 void rpc_delay(struct rpc_task *task, unsigned long delay)
600 task->tk_timeout = delay;
601 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
605 * Helper to call task->tk_ops->rpc_call_prepare
607 static void rpc_prepare_task(struct rpc_task *task)
610 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
615 * Helper that calls task->tk_ops->rpc_call_done if it exists
617 void rpc_exit_task(struct rpc_task *task)
619 task->tk_action = NULL;
620 if (task->tk_ops->rpc_call_done != NULL) {
622 task->tk_ops->rpc_call_done(task, task->tk_calldata);
624 if (task->tk_action != NULL) {
625 WARN_ON(RPC_ASSASSINATED(task));
626 /* Always release the RPC slot and buffer memory */
631 EXPORT_SYMBOL(rpc_exit_task);
633 void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
635 if (ops->rpc_release != NULL) {
637 ops->rpc_release(calldata);
643 * This is the RPC `scheduler' (or rather, the finite state machine).
645 static void __rpc_execute(struct rpc_task *task)
649 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
650 task->tk_pid, task->tk_flags);
652 BUG_ON(RPC_IS_QUEUED(task));
656 * Garbage collection of pending timers...
658 rpc_delete_timer(task);
661 * Execute any pending callback.
663 if (RPC_DO_CALLBACK(task)) {
664 /* Define a callback save pointer */
665 void (*save_callback)(struct rpc_task *);
668 * If a callback exists, save it, reset it,
670 * The save is needed to stop from resetting
671 * another callback set within the callback handler
674 save_callback=task->tk_callback;
675 task->tk_callback=NULL;
680 * Perform the next FSM step.
681 * tk_action may be NULL when the task has been killed
684 if (!RPC_IS_QUEUED(task)) {
685 if (task->tk_action == NULL)
687 task->tk_action(task);
691 * Lockless check for whether task is sleeping or not.
693 if (!RPC_IS_QUEUED(task))
695 rpc_clear_running(task);
696 if (RPC_IS_ASYNC(task)) {
697 /* Careful! we may have raced... */
698 if (RPC_IS_QUEUED(task))
700 if (rpc_test_and_set_running(task))
705 /* sync task: sleep here */
706 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
707 /* Note: Caller should be using rpc_clnt_sigmask() */
708 status = out_of_line_wait_on_bit(&task->tk_runstate,
709 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
711 if (status == -ERESTARTSYS) {
713 * When a sync task receives a signal, it exits with
714 * -ERESTARTSYS. In order to catch any callbacks that
715 * clean up after sleeping on some queue, we don't
716 * break the loop here, but go around once more.
718 dprintk("RPC: %5u got signal\n", task->tk_pid);
719 task->tk_flags |= RPC_TASK_KILLED;
720 rpc_exit(task, -ERESTARTSYS);
721 rpc_wake_up_task(task);
723 rpc_set_running(task);
724 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
727 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
729 /* Release all resources associated with the task */
730 rpc_release_task(task);
734 * User-visible entry point to the scheduler.
736 * This may be called recursively if e.g. an async NFS task updates
737 * the attributes and finds that dirty pages must be flushed.
738 * NOTE: Upon exit of this function the task is guaranteed to be
739 * released. In particular note that tk_release() will have
740 * been called, so your task memory may have been freed.
742 void rpc_execute(struct rpc_task *task)
744 rpc_set_active(task);
745 rpc_set_running(task);
749 static void rpc_async_schedule(struct work_struct *work)
751 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
760 * rpc_malloc - allocate an RPC buffer
761 * @task: RPC task that will use this buffer
762 * @size: requested byte size
764 * To prevent rpciod from hanging, this allocator never sleeps,
765 * returning NULL if the request cannot be serviced immediately.
766 * The caller can arrange to sleep in a way that is safe for rpciod.
768 * Most requests are 'small' (under 2KiB) and can be serviced from a
769 * mempool, ensuring that NFS reads and writes can always proceed,
770 * and that there is good locality of reference for these buffers.
772 * In order to avoid memory starvation triggering more writebacks of
773 * NFS requests, we avoid using GFP_KERNEL.
775 void *rpc_malloc(struct rpc_task *task, size_t size)
777 struct rpc_buffer *buf;
778 gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
780 size += sizeof(struct rpc_buffer);
781 if (size <= RPC_BUFFER_MAXSIZE)
782 buf = mempool_alloc(rpc_buffer_mempool, gfp);
784 buf = kmalloc(size, gfp);
790 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
791 task->tk_pid, size, buf);
796 * rpc_free - free buffer allocated via rpc_malloc
797 * @buffer: buffer to free
800 void rpc_free(void *buffer)
803 struct rpc_buffer *buf;
808 buf = container_of(buffer, struct rpc_buffer, data);
811 dprintk("RPC: freeing buffer of size %zu at %p\n",
814 if (size <= RPC_BUFFER_MAXSIZE)
815 mempool_free(buf, rpc_buffer_mempool);
821 * Creation and deletion of RPC task structures
823 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
825 memset(task, 0, sizeof(*task));
826 init_timer(&task->tk_timer);
827 task->tk_timer.data = (unsigned long) task;
828 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
829 atomic_set(&task->tk_count, 1);
830 task->tk_client = clnt;
831 task->tk_flags = flags;
832 task->tk_ops = tk_ops;
833 if (tk_ops->rpc_call_prepare != NULL)
834 task->tk_action = rpc_prepare_task;
835 task->tk_calldata = calldata;
836 INIT_LIST_HEAD(&task->tk_task);
838 /* Initialize retry counters */
839 task->tk_garb_retry = 2;
840 task->tk_cred_retry = 2;
842 task->tk_priority = RPC_PRIORITY_NORMAL;
843 task->tk_cookie = (unsigned long)current;
845 /* Initialize workqueue for async tasks */
846 task->tk_workqueue = rpciod_workqueue;
849 kref_get(&clnt->cl_kref);
850 if (clnt->cl_softrtry)
851 task->tk_flags |= RPC_TASK_SOFT;
853 task->tk_flags |= RPC_TASK_NOINTR;
856 BUG_ON(task->tk_ops == NULL);
858 /* starting timestamp */
859 task->tk_start = jiffies;
861 dprintk("RPC: new task initialized, procpid %u\n",
865 static struct rpc_task *
868 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
871 static void rpc_free_task(struct rcu_head *rcu)
873 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
874 dprintk("RPC: %5u freeing task\n", task->tk_pid);
875 mempool_free(task, rpc_task_mempool);
879 * Create a new task for the specified client.
881 struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
883 struct rpc_task *task;
885 task = rpc_alloc_task();
889 rpc_init_task(task, clnt, flags, tk_ops, calldata);
891 dprintk("RPC: allocated task %p\n", task);
892 task->tk_flags |= RPC_TASK_DYNAMIC;
898 void rpc_put_task(struct rpc_task *task)
900 const struct rpc_call_ops *tk_ops = task->tk_ops;
901 void *calldata = task->tk_calldata;
903 if (!atomic_dec_and_test(&task->tk_count))
905 /* Release resources */
908 if (task->tk_msg.rpc_cred)
909 rpcauth_unbindcred(task);
910 if (task->tk_client) {
911 rpc_release_client(task->tk_client);
912 task->tk_client = NULL;
914 if (task->tk_flags & RPC_TASK_DYNAMIC)
915 call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
916 rpc_release_calldata(tk_ops, calldata);
918 EXPORT_SYMBOL(rpc_put_task);
920 static void rpc_release_task(struct rpc_task *task)
923 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
925 dprintk("RPC: %5u release task\n", task->tk_pid);
927 if (!list_empty(&task->tk_task)) {
928 struct rpc_clnt *clnt = task->tk_client;
929 /* Remove from client task list */
930 spin_lock(&clnt->cl_lock);
931 list_del(&task->tk_task);
932 spin_unlock(&clnt->cl_lock);
934 BUG_ON (RPC_IS_QUEUED(task));
936 /* Synchronously delete any running timer */
937 rpc_delete_timer(task);
942 /* Wake up anyone who is waiting for task completion */
943 rpc_mark_complete_task(task);
949 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
950 * @clnt: pointer to RPC client
953 * @data: user call data
955 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
956 const struct rpc_call_ops *ops,
959 struct rpc_task *task;
960 task = rpc_new_task(clnt, flags, ops, data);
962 rpc_release_calldata(ops, data);
963 return ERR_PTR(-ENOMEM);
965 atomic_inc(&task->tk_count);
969 EXPORT_SYMBOL(rpc_run_task);
972 * Kill all tasks for the given client.
973 * XXX: kill their descendants as well?
975 void rpc_killall_tasks(struct rpc_clnt *clnt)
977 struct rpc_task *rovr;
980 if (list_empty(&clnt->cl_tasks))
982 dprintk("RPC: killing all tasks for client %p\n", clnt);
984 * Spin lock all_tasks to prevent changes...
986 spin_lock(&clnt->cl_lock);
987 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
988 if (! RPC_IS_ACTIVATED(rovr))
990 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
991 rovr->tk_flags |= RPC_TASK_KILLED;
992 rpc_exit(rovr, -EIO);
993 rpc_wake_up_task(rovr);
996 spin_unlock(&clnt->cl_lock);
999 static void rpciod_killall(void)
1001 struct rpc_clnt *clnt;
1002 unsigned long flags;
1005 clear_thread_flag(TIF_SIGPENDING);
1007 spin_lock(&rpc_sched_lock);
1008 list_for_each_entry(clnt, &all_clients, cl_clients)
1009 rpc_killall_tasks(clnt);
1010 spin_unlock(&rpc_sched_lock);
1011 flush_workqueue(rpciod_workqueue);
1012 if (!list_empty(&all_clients))
1014 dprintk("RPC: rpciod_killall: waiting for tasks "
1016 wait_event_timeout(client_kill_wait,
1017 list_empty(&all_clients), 1*HZ);
1020 spin_lock_irqsave(¤t->sighand->siglock, flags);
1021 recalc_sigpending();
1022 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
1025 void rpc_register_client(struct rpc_clnt *clnt)
1027 spin_lock(&rpc_sched_lock);
1028 list_add(&clnt->cl_clients, &all_clients);
1029 spin_unlock(&rpc_sched_lock);
1032 void rpc_unregister_client(struct rpc_clnt *clnt)
1034 spin_lock(&rpc_sched_lock);
1035 list_del(&clnt->cl_clients);
1036 if (list_empty(&all_clients))
1037 wake_up(&client_kill_wait);
1038 spin_unlock(&rpc_sched_lock);
1042 * Start up the rpciod process if it's not already running.
1047 struct workqueue_struct *wq;
1050 if (atomic_inc_not_zero(&rpciod_users))
1053 mutex_lock(&rpciod_mutex);
1055 /* Guard against races with rpciod_down() */
1056 if (rpciod_workqueue != NULL)
1059 * Create the rpciod thread and wait for it to start.
1061 dprintk("RPC: creating workqueue rpciod\n");
1063 wq = create_workqueue("rpciod");
1067 rpciod_workqueue = wq;
1070 atomic_inc(&rpciod_users);
1072 mutex_unlock(&rpciod_mutex);
1079 if (!atomic_dec_and_test(&rpciod_users))
1082 mutex_lock(&rpciod_mutex);
1083 dprintk("RPC: destroying workqueue rpciod\n");
1085 if (atomic_read(&rpciod_users) == 0 && rpciod_workqueue != NULL) {
1087 destroy_workqueue(rpciod_workqueue);
1088 rpciod_workqueue = NULL;
1090 mutex_unlock(&rpciod_mutex);
1094 void rpc_show_tasks(void)
1096 struct rpc_clnt *clnt;
1099 spin_lock(&rpc_sched_lock);
1100 if (list_empty(&all_clients))
1102 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1103 "-rpcwait -action- ---ops--\n");
1104 list_for_each_entry(clnt, &all_clients, cl_clients) {
1105 if (list_empty(&clnt->cl_tasks))
1107 spin_lock(&clnt->cl_lock);
1108 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1109 const char *rpc_waitq = "none";
1111 if (RPC_IS_QUEUED(t))
1112 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1114 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1116 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1117 t->tk_flags, t->tk_status,
1119 (t->tk_client ? t->tk_client->cl_prog : 0),
1120 t->tk_rqstp, t->tk_timeout,
1122 t->tk_action, t->tk_ops);
1124 spin_unlock(&clnt->cl_lock);
1127 spin_unlock(&rpc_sched_lock);
1132 rpc_destroy_mempool(void)
1134 if (rpc_buffer_mempool)
1135 mempool_destroy(rpc_buffer_mempool);
1136 if (rpc_task_mempool)
1137 mempool_destroy(rpc_task_mempool);
1139 kmem_cache_destroy(rpc_task_slabp);
1140 if (rpc_buffer_slabp)
1141 kmem_cache_destroy(rpc_buffer_slabp);
1145 rpc_init_mempool(void)
1147 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1148 sizeof(struct rpc_task),
1149 0, SLAB_HWCACHE_ALIGN,
1151 if (!rpc_task_slabp)
1153 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1155 0, SLAB_HWCACHE_ALIGN,
1157 if (!rpc_buffer_slabp)
1159 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1161 if (!rpc_task_mempool)
1163 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1165 if (!rpc_buffer_mempool)
1169 rpc_destroy_mempool();