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_async_schedule(struct work_struct *);
42 static void rpc_release_task(struct rpc_task *task);
43 static void __rpc_queue_timer_fn(unsigned long ptr);
46 * RPC tasks sit here while waiting for conditions to improve.
48 static struct rpc_wait_queue delay_queue;
51 * rpciod-related stuff
53 struct workqueue_struct *rpciod_workqueue;
56 * Disable the timer for a given RPC task. Should be called with
57 * queue->lock and bh_disabled in order to avoid races within
61 __rpc_disable_timer(struct rpc_task *task)
63 if (task->tk_timeout == 0)
65 dprintk("RPC: %5u disabling timer\n", task->tk_pid);
67 list_del(&task->u.tk_wait.timer_list);
71 rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
73 queue->timer_list.expires = expires;
74 mod_timer(&queue->timer_list.timer, expires);
78 * Set up a timer for the current task.
81 __rpc_add_timer(struct rpc_task *task)
83 if (!task->tk_timeout)
86 dprintk("RPC: %5u setting alarm for %lu ms\n",
87 task->tk_pid, task->tk_timeout * 1000 / HZ);
89 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
90 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
94 * Delete any timer for the current task. Because we use del_timer_sync(),
95 * this function should never be called while holding queue->lock.
98 rpc_delete_timer(struct rpc_task *task)
100 if (RPC_IS_QUEUED(task))
102 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
103 del_singleshot_timer_sync(&task->tk_timer);
104 dprintk("RPC: %5u deleting timer\n", task->tk_pid);
109 * Add new request to a priority queue.
111 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
116 INIT_LIST_HEAD(&task->u.tk_wait.links);
117 q = &queue->tasks[task->tk_priority];
118 if (unlikely(task->tk_priority > queue->maxpriority))
119 q = &queue->tasks[queue->maxpriority];
120 list_for_each_entry(t, q, u.tk_wait.list) {
121 if (t->tk_owner == task->tk_owner) {
122 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
126 list_add_tail(&task->u.tk_wait.list, q);
130 * Add new request to wait queue.
132 * Swapper tasks always get inserted at the head of the queue.
133 * This should avoid many nasty memory deadlocks and hopefully
134 * improve overall performance.
135 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
137 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
139 BUG_ON (RPC_IS_QUEUED(task));
141 if (RPC_IS_PRIORITY(queue))
142 __rpc_add_wait_queue_priority(queue, task);
143 else if (RPC_IS_SWAPPER(task))
144 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
146 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
147 task->tk_waitqueue = queue;
149 rpc_set_queued(task);
151 dprintk("RPC: %5u added to queue %p \"%s\"\n",
152 task->tk_pid, queue, rpc_qname(queue));
156 * Remove request from a priority queue.
158 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
162 if (!list_empty(&task->u.tk_wait.links)) {
163 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
164 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
165 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
170 * Remove request from queue.
171 * Note: must be called with spin lock held.
173 static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
175 __rpc_disable_timer(task);
176 if (RPC_IS_PRIORITY(queue))
177 __rpc_remove_wait_queue_priority(task);
178 list_del(&task->u.tk_wait.list);
180 dprintk("RPC: %5u removed from queue %p \"%s\"\n",
181 task->tk_pid, queue, rpc_qname(queue));
184 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
186 queue->priority = priority;
187 queue->count = 1 << (priority * 2);
190 static inline void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
193 queue->nr = RPC_BATCH_COUNT;
196 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
198 rpc_set_waitqueue_priority(queue, queue->maxpriority);
199 rpc_set_waitqueue_owner(queue, 0);
202 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
206 spin_lock_init(&queue->lock);
207 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
208 INIT_LIST_HEAD(&queue->tasks[i]);
209 queue->maxpriority = nr_queues - 1;
210 rpc_reset_waitqueue_priority(queue);
212 setup_timer(&queue->timer_list.timer, __rpc_queue_timer_fn, (unsigned long)queue);
213 INIT_LIST_HEAD(&queue->timer_list.list);
219 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
221 __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
224 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
226 __rpc_init_priority_wait_queue(queue, qname, 1);
228 EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
230 void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
232 del_timer_sync(&queue->timer_list.timer);
234 EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
236 static int rpc_wait_bit_killable(void *word)
238 if (fatal_signal_pending(current))
245 static void rpc_task_set_debuginfo(struct rpc_task *task)
247 static atomic_t rpc_pid;
249 task->tk_magic = RPC_TASK_MAGIC_ID;
250 task->tk_pid = atomic_inc_return(&rpc_pid);
253 static inline void rpc_task_set_debuginfo(struct rpc_task *task)
258 static void rpc_set_active(struct rpc_task *task)
260 struct rpc_clnt *clnt;
261 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
263 rpc_task_set_debuginfo(task);
264 /* Add to global list of all tasks */
265 clnt = task->tk_client;
267 spin_lock(&clnt->cl_lock);
268 list_add_tail(&task->tk_task, &clnt->cl_tasks);
269 spin_unlock(&clnt->cl_lock);
274 * Mark an RPC call as having completed by clearing the 'active' bit
276 static void rpc_mark_complete_task(struct rpc_task *task)
278 smp_mb__before_clear_bit();
279 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
280 smp_mb__after_clear_bit();
281 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
285 * Allow callers to wait for completion of an RPC call
287 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
290 action = rpc_wait_bit_killable;
291 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
292 action, TASK_KILLABLE);
294 EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
297 * Make an RPC task runnable.
299 * Note: If the task is ASYNC, this must be called with
300 * the spinlock held to protect the wait queue operation.
302 static void rpc_make_runnable(struct rpc_task *task)
304 rpc_clear_queued(task);
305 if (rpc_test_and_set_running(task))
307 /* We might have raced */
308 if (RPC_IS_QUEUED(task)) {
309 rpc_clear_running(task);
312 if (RPC_IS_ASYNC(task)) {
315 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
316 status = queue_work(rpciod_workqueue, &task->u.tk_work);
318 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
319 task->tk_status = status;
323 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
327 * Prepare for sleeping on a wait queue.
328 * By always appending tasks to the list we ensure FIFO behavior.
329 * NB: An RPC task will only receive interrupt-driven events as long
330 * as it's on a wait queue.
332 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
335 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
336 task->tk_pid, rpc_qname(q), jiffies);
338 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
339 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
343 __rpc_add_wait_queue(q, task);
345 BUG_ON(task->tk_callback != NULL);
346 task->tk_callback = action;
347 __rpc_add_timer(task);
350 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
353 /* Mark the task as being activated if so needed */
354 rpc_set_active(task);
357 * Protect the queue operations.
359 spin_lock_bh(&q->lock);
360 __rpc_sleep_on(q, task, action);
361 spin_unlock_bh(&q->lock);
363 EXPORT_SYMBOL_GPL(rpc_sleep_on);
366 * __rpc_do_wake_up_task - wake up a single rpc_task
368 * @task: task to be woken up
370 * Caller must hold queue->lock, and have cleared the task queued flag.
372 static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
374 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
375 task->tk_pid, jiffies);
378 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
380 /* Has the task been executed yet? If not, we cannot wake it up! */
381 if (!RPC_IS_ACTIVATED(task)) {
382 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
386 __rpc_remove_wait_queue(queue, task);
388 rpc_make_runnable(task);
390 dprintk("RPC: __rpc_wake_up_task done\n");
394 * Wake up a queued task while the queue lock is being held
396 static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
398 if (!RPC_IS_QUEUED(task) || task->tk_waitqueue != queue)
400 if (rpc_start_wakeup(task)) {
401 __rpc_do_wake_up_task(queue, task);
402 rpc_finish_wakeup(task);
407 * Wake up a task on a specific queue
409 void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
412 spin_lock(&queue->lock);
413 rpc_wake_up_task_queue_locked(queue, task);
414 spin_unlock(&queue->lock);
415 rcu_read_unlock_bh();
417 EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
420 * Wake up the specified task
422 static void rpc_wake_up_task(struct rpc_task *task)
424 rpc_wake_up_queued_task(task->tk_waitqueue, task);
428 * Wake up the next task on a priority queue.
430 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
433 struct rpc_task *task;
436 * Service a batch of tasks from a single owner.
438 q = &queue->tasks[queue->priority];
439 if (!list_empty(q)) {
440 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
441 if (queue->owner == task->tk_owner) {
444 list_move_tail(&task->u.tk_wait.list, q);
447 * Check if we need to switch queues.
454 * Service the next queue.
457 if (q == &queue->tasks[0])
458 q = &queue->tasks[queue->maxpriority];
461 if (!list_empty(q)) {
462 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
465 } while (q != &queue->tasks[queue->priority]);
467 rpc_reset_waitqueue_priority(queue);
471 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
473 rpc_set_waitqueue_owner(queue, task->tk_owner);
475 rpc_wake_up_task_queue_locked(queue, task);
480 * Wake up the next task on the wait queue.
482 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
484 struct rpc_task *task = NULL;
486 dprintk("RPC: wake_up_next(%p \"%s\")\n",
487 queue, rpc_qname(queue));
489 spin_lock(&queue->lock);
490 if (RPC_IS_PRIORITY(queue))
491 task = __rpc_wake_up_next_priority(queue);
493 task_for_first(task, &queue->tasks[0])
494 rpc_wake_up_task_queue_locked(queue, task);
496 spin_unlock(&queue->lock);
497 rcu_read_unlock_bh();
501 EXPORT_SYMBOL_GPL(rpc_wake_up_next);
504 * rpc_wake_up - wake up all rpc_tasks
505 * @queue: rpc_wait_queue on which the tasks are sleeping
509 void rpc_wake_up(struct rpc_wait_queue *queue)
511 struct rpc_task *task, *next;
512 struct list_head *head;
515 spin_lock(&queue->lock);
516 head = &queue->tasks[queue->maxpriority];
518 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
519 rpc_wake_up_task_queue_locked(queue, task);
520 if (head == &queue->tasks[0])
524 spin_unlock(&queue->lock);
525 rcu_read_unlock_bh();
527 EXPORT_SYMBOL_GPL(rpc_wake_up);
530 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
531 * @queue: rpc_wait_queue on which the tasks are sleeping
532 * @status: status value to set
536 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
538 struct rpc_task *task, *next;
539 struct list_head *head;
542 spin_lock(&queue->lock);
543 head = &queue->tasks[queue->maxpriority];
545 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
546 task->tk_status = status;
547 rpc_wake_up_task_queue_locked(queue, task);
549 if (head == &queue->tasks[0])
553 spin_unlock(&queue->lock);
554 rcu_read_unlock_bh();
556 EXPORT_SYMBOL_GPL(rpc_wake_up_status);
559 * Run a timeout function.
561 static void rpc_run_timer(unsigned long ptr)
563 struct rpc_task *task = (struct rpc_task *)ptr;
564 struct rpc_wait_queue *queue = task->tk_waitqueue;
566 spin_lock(&queue->lock);
567 if (RPC_IS_QUEUED(task) && task->tk_waitqueue == queue) {
568 dprintk("RPC: %5u timeout\n", task->tk_pid);
569 task->tk_status = -ETIMEDOUT;
570 rpc_wake_up_task_queue_locked(queue, task);
572 spin_unlock(&queue->lock);
573 smp_mb__before_clear_bit();
574 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
575 smp_mb__after_clear_bit();
578 static void __rpc_queue_timer_fn(unsigned long ptr)
580 struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
581 struct rpc_task *task, *n;
582 unsigned long expires, now, timeo;
584 spin_lock(&queue->lock);
585 expires = now = jiffies;
586 list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
587 timeo = task->u.tk_wait.expires;
588 if (time_after_eq(now, timeo)) {
589 list_del_init(&task->u.tk_wait.timer_list);
590 dprintk("RPC: %5u timeout\n", task->tk_pid);
591 task->tk_status = -ETIMEDOUT;
592 rpc_wake_up_task_queue_locked(queue, task);
595 if (expires == now || time_after(expires, timeo))
598 if (!list_empty(&queue->timer_list.list))
599 rpc_set_queue_timer(queue, expires);
600 spin_unlock(&queue->lock);
603 static void __rpc_atrun(struct rpc_task *task)
609 * Run a task at a later time
611 void rpc_delay(struct rpc_task *task, unsigned long delay)
613 task->tk_timeout = delay;
614 rpc_sleep_on(&delay_queue, task, __rpc_atrun);
616 EXPORT_SYMBOL_GPL(rpc_delay);
619 * Helper to call task->tk_ops->rpc_call_prepare
621 static void rpc_prepare_task(struct rpc_task *task)
624 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
629 * Helper that calls task->tk_ops->rpc_call_done if it exists
631 void rpc_exit_task(struct rpc_task *task)
633 task->tk_action = NULL;
634 if (task->tk_ops->rpc_call_done != NULL) {
636 task->tk_ops->rpc_call_done(task, task->tk_calldata);
638 if (task->tk_action != NULL) {
639 WARN_ON(RPC_ASSASSINATED(task));
640 /* Always release the RPC slot and buffer memory */
645 EXPORT_SYMBOL_GPL(rpc_exit_task);
647 void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
649 if (ops->rpc_release != NULL) {
651 ops->rpc_release(calldata);
657 * This is the RPC `scheduler' (or rather, the finite state machine).
659 static void __rpc_execute(struct rpc_task *task)
663 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
664 task->tk_pid, task->tk_flags);
666 BUG_ON(RPC_IS_QUEUED(task));
670 * Garbage collection of pending timers...
672 rpc_delete_timer(task);
675 * Execute any pending callback.
677 if (RPC_DO_CALLBACK(task)) {
678 /* Define a callback save pointer */
679 void (*save_callback)(struct rpc_task *);
682 * If a callback exists, save it, reset it,
684 * The save is needed to stop from resetting
685 * another callback set within the callback handler
688 save_callback=task->tk_callback;
689 task->tk_callback=NULL;
694 * Perform the next FSM step.
695 * tk_action may be NULL when the task has been killed
698 if (!RPC_IS_QUEUED(task)) {
699 if (task->tk_action == NULL)
701 task->tk_action(task);
705 * Lockless check for whether task is sleeping or not.
707 if (!RPC_IS_QUEUED(task))
709 rpc_clear_running(task);
710 if (RPC_IS_ASYNC(task)) {
711 /* Careful! we may have raced... */
712 if (RPC_IS_QUEUED(task))
714 if (rpc_test_and_set_running(task))
719 /* sync task: sleep here */
720 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
721 status = out_of_line_wait_on_bit(&task->tk_runstate,
722 RPC_TASK_QUEUED, rpc_wait_bit_killable,
724 if (status == -ERESTARTSYS) {
726 * When a sync task receives a signal, it exits with
727 * -ERESTARTSYS. In order to catch any callbacks that
728 * clean up after sleeping on some queue, we don't
729 * break the loop here, but go around once more.
731 dprintk("RPC: %5u got signal\n", task->tk_pid);
732 task->tk_flags |= RPC_TASK_KILLED;
733 rpc_exit(task, -ERESTARTSYS);
734 rpc_wake_up_task(task);
736 rpc_set_running(task);
737 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
740 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
742 /* Release all resources associated with the task */
743 rpc_release_task(task);
747 * User-visible entry point to the scheduler.
749 * This may be called recursively if e.g. an async NFS task updates
750 * the attributes and finds that dirty pages must be flushed.
751 * NOTE: Upon exit of this function the task is guaranteed to be
752 * released. In particular note that tk_release() will have
753 * been called, so your task memory may have been freed.
755 void rpc_execute(struct rpc_task *task)
757 rpc_set_active(task);
758 rpc_set_running(task);
762 static void rpc_async_schedule(struct work_struct *work)
764 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
773 * rpc_malloc - allocate an RPC buffer
774 * @task: RPC task that will use this buffer
775 * @size: requested byte size
777 * To prevent rpciod from hanging, this allocator never sleeps,
778 * returning NULL if the request cannot be serviced immediately.
779 * The caller can arrange to sleep in a way that is safe for rpciod.
781 * Most requests are 'small' (under 2KiB) and can be serviced from a
782 * mempool, ensuring that NFS reads and writes can always proceed,
783 * and that there is good locality of reference for these buffers.
785 * In order to avoid memory starvation triggering more writebacks of
786 * NFS requests, we avoid using GFP_KERNEL.
788 void *rpc_malloc(struct rpc_task *task, size_t size)
790 struct rpc_buffer *buf;
791 gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
793 size += sizeof(struct rpc_buffer);
794 if (size <= RPC_BUFFER_MAXSIZE)
795 buf = mempool_alloc(rpc_buffer_mempool, gfp);
797 buf = kmalloc(size, gfp);
803 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
804 task->tk_pid, size, buf);
807 EXPORT_SYMBOL_GPL(rpc_malloc);
810 * rpc_free - free buffer allocated via rpc_malloc
811 * @buffer: buffer to free
814 void rpc_free(void *buffer)
817 struct rpc_buffer *buf;
822 buf = container_of(buffer, struct rpc_buffer, data);
825 dprintk("RPC: freeing buffer of size %zu at %p\n",
828 if (size <= RPC_BUFFER_MAXSIZE)
829 mempool_free(buf, rpc_buffer_mempool);
833 EXPORT_SYMBOL_GPL(rpc_free);
836 * Creation and deletion of RPC task structures
838 static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
840 memset(task, 0, sizeof(*task));
841 setup_timer(&task->tk_timer, rpc_run_timer, (unsigned long)task);
842 atomic_set(&task->tk_count, 1);
843 task->tk_flags = task_setup_data->flags;
844 task->tk_ops = task_setup_data->callback_ops;
845 task->tk_calldata = task_setup_data->callback_data;
846 INIT_LIST_HEAD(&task->tk_task);
848 /* Initialize retry counters */
849 task->tk_garb_retry = 2;
850 task->tk_cred_retry = 2;
852 task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
853 task->tk_owner = current->tgid;
855 /* Initialize workqueue for async tasks */
856 task->tk_workqueue = task_setup_data->workqueue;
858 task->tk_client = task_setup_data->rpc_client;
859 if (task->tk_client != NULL) {
860 kref_get(&task->tk_client->cl_kref);
861 if (task->tk_client->cl_softrtry)
862 task->tk_flags |= RPC_TASK_SOFT;
865 if (task->tk_ops->rpc_call_prepare != NULL)
866 task->tk_action = rpc_prepare_task;
868 if (task_setup_data->rpc_message != NULL) {
869 memcpy(&task->tk_msg, task_setup_data->rpc_message, sizeof(task->tk_msg));
870 /* Bind the user cred */
871 if (task->tk_msg.rpc_cred != NULL)
872 rpcauth_holdcred(task);
874 rpcauth_bindcred(task);
875 if (task->tk_action == NULL)
876 rpc_call_start(task);
879 /* starting timestamp */
880 task->tk_start = jiffies;
882 dprintk("RPC: new task initialized, procpid %u\n",
883 task_pid_nr(current));
886 static struct rpc_task *
889 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
892 static void rpc_free_task_rcu(struct rcu_head *rcu)
894 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
895 dprintk("RPC: %5u freeing task\n", task->tk_pid);
896 mempool_free(task, rpc_task_mempool);
900 * Create a new task for the specified client.
902 struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
904 struct rpc_task *task = setup_data->task;
905 unsigned short flags = 0;
908 task = rpc_alloc_task();
911 flags = RPC_TASK_DYNAMIC;
914 rpc_init_task(task, setup_data);
916 task->tk_flags |= flags;
917 dprintk("RPC: allocated task %p\n", task);
922 static void rpc_free_task(struct rpc_task *task)
924 const struct rpc_call_ops *tk_ops = task->tk_ops;
925 void *calldata = task->tk_calldata;
927 if (task->tk_flags & RPC_TASK_DYNAMIC)
928 call_rcu_bh(&task->u.tk_rcu, rpc_free_task_rcu);
929 rpc_release_calldata(tk_ops, calldata);
932 static void rpc_async_release(struct work_struct *work)
934 rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
937 void rpc_put_task(struct rpc_task *task)
939 if (!atomic_dec_and_test(&task->tk_count))
941 /* Release resources */
944 if (task->tk_msg.rpc_cred)
945 rpcauth_unbindcred(task);
946 if (task->tk_client) {
947 rpc_release_client(task->tk_client);
948 task->tk_client = NULL;
950 if (task->tk_workqueue != NULL) {
951 INIT_WORK(&task->u.tk_work, rpc_async_release);
952 queue_work(task->tk_workqueue, &task->u.tk_work);
956 EXPORT_SYMBOL_GPL(rpc_put_task);
958 static void rpc_release_task(struct rpc_task *task)
961 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
963 dprintk("RPC: %5u release task\n", task->tk_pid);
965 if (!list_empty(&task->tk_task)) {
966 struct rpc_clnt *clnt = task->tk_client;
967 /* Remove from client task list */
968 spin_lock(&clnt->cl_lock);
969 list_del(&task->tk_task);
970 spin_unlock(&clnt->cl_lock);
972 BUG_ON (RPC_IS_QUEUED(task));
974 /* Synchronously delete any running timer */
975 rpc_delete_timer(task);
980 /* Wake up anyone who is waiting for task completion */
981 rpc_mark_complete_task(task);
987 * Kill all tasks for the given client.
988 * XXX: kill their descendants as well?
990 void rpc_killall_tasks(struct rpc_clnt *clnt)
992 struct rpc_task *rovr;
995 if (list_empty(&clnt->cl_tasks))
997 dprintk("RPC: killing all tasks for client %p\n", clnt);
999 * Spin lock all_tasks to prevent changes...
1001 spin_lock(&clnt->cl_lock);
1002 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
1003 if (! RPC_IS_ACTIVATED(rovr))
1005 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
1006 rovr->tk_flags |= RPC_TASK_KILLED;
1007 rpc_exit(rovr, -EIO);
1008 rpc_wake_up_task(rovr);
1011 spin_unlock(&clnt->cl_lock);
1013 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
1017 return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1020 void rpciod_down(void)
1022 module_put(THIS_MODULE);
1026 * Start up the rpciod workqueue.
1028 static int rpciod_start(void)
1030 struct workqueue_struct *wq;
1033 * Create the rpciod thread and wait for it to start.
1035 dprintk("RPC: creating workqueue rpciod\n");
1036 wq = create_workqueue("rpciod");
1037 rpciod_workqueue = wq;
1038 return rpciod_workqueue != NULL;
1041 static void rpciod_stop(void)
1043 struct workqueue_struct *wq = NULL;
1045 if (rpciod_workqueue == NULL)
1047 dprintk("RPC: destroying workqueue rpciod\n");
1049 wq = rpciod_workqueue;
1050 rpciod_workqueue = NULL;
1051 destroy_workqueue(wq);
1055 rpc_destroy_mempool(void)
1058 if (rpc_buffer_mempool)
1059 mempool_destroy(rpc_buffer_mempool);
1060 if (rpc_task_mempool)
1061 mempool_destroy(rpc_task_mempool);
1063 kmem_cache_destroy(rpc_task_slabp);
1064 if (rpc_buffer_slabp)
1065 kmem_cache_destroy(rpc_buffer_slabp);
1066 rpc_destroy_wait_queue(&delay_queue);
1070 rpc_init_mempool(void)
1073 * The following is not strictly a mempool initialisation,
1074 * but there is no harm in doing it here
1076 rpc_init_wait_queue(&delay_queue, "delayq");
1077 if (!rpciod_start())
1080 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1081 sizeof(struct rpc_task),
1082 0, SLAB_HWCACHE_ALIGN,
1084 if (!rpc_task_slabp)
1086 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1088 0, SLAB_HWCACHE_ALIGN,
1090 if (!rpc_buffer_slabp)
1092 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1094 if (!rpc_task_mempool)
1096 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1098 if (!rpc_buffer_mempool)
1102 rpc_destroy_mempool();