mm: remove unused memclear_highpage_flush()
[linux-2.6.git] / net / sunrpc / sched.c
blobfb20f25ddec9b70c805ad65d675441d02b62055b
1 /*
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/spinlock.h>
20 #include <linux/mutex.h>
21 #include <linux/freezer.h>
23 #include <linux/sunrpc/clnt.h>
25 #include "sunrpc.h"
27 #ifdef RPC_DEBUG
28 #define RPCDBG_FACILITY RPCDBG_SCHED
29 #endif
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/sunrpc.h>
35 * RPC slabs and memory pools
37 #define RPC_BUFFER_MAXSIZE (2048)
38 #define RPC_BUFFER_POOLSIZE (8)
39 #define RPC_TASK_POOLSIZE (8)
40 static struct kmem_cache *rpc_task_slabp __read_mostly;
41 static struct kmem_cache *rpc_buffer_slabp __read_mostly;
42 static mempool_t *rpc_task_mempool __read_mostly;
43 static mempool_t *rpc_buffer_mempool __read_mostly;
45 static void rpc_async_schedule(struct work_struct *);
46 static void rpc_release_task(struct rpc_task *task);
47 static void __rpc_queue_timer_fn(unsigned long ptr);
50 * RPC tasks sit here while waiting for conditions to improve.
52 static struct rpc_wait_queue delay_queue;
55 * rpciod-related stuff
57 struct workqueue_struct *rpciod_workqueue;
60 * Disable the timer for a given RPC task. Should be called with
61 * queue->lock and bh_disabled in order to avoid races within
62 * rpc_run_timer().
64 static void
65 __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
67 if (task->tk_timeout == 0)
68 return;
69 dprintk("RPC: %5u disabling timer\n", task->tk_pid);
70 task->tk_timeout = 0;
71 list_del(&task->u.tk_wait.timer_list);
72 if (list_empty(&queue->timer_list.list))
73 del_timer(&queue->timer_list.timer);
76 static void
77 rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
79 queue->timer_list.expires = expires;
80 mod_timer(&queue->timer_list.timer, expires);
84 * Set up a timer for the current task.
86 static void
87 __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
89 if (!task->tk_timeout)
90 return;
92 dprintk("RPC: %5u setting alarm for %lu ms\n",
93 task->tk_pid, task->tk_timeout * 1000 / HZ);
95 task->u.tk_wait.expires = jiffies + task->tk_timeout;
96 if (list_empty(&queue->timer_list.list) || time_before(task->u.tk_wait.expires, queue->timer_list.expires))
97 rpc_set_queue_timer(queue, task->u.tk_wait.expires);
98 list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list);
101 static void rpc_rotate_queue_owner(struct rpc_wait_queue *queue)
103 struct list_head *q = &queue->tasks[queue->priority];
104 struct rpc_task *task;
106 if (!list_empty(q)) {
107 task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
108 if (task->tk_owner == queue->owner)
109 list_move_tail(&task->u.tk_wait.list, q);
113 static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
115 if (queue->priority != priority) {
116 /* Fairness: rotate the list when changing priority */
117 rpc_rotate_queue_owner(queue);
118 queue->priority = priority;
122 static void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
124 queue->owner = pid;
125 queue->nr = RPC_BATCH_COUNT;
128 static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
130 rpc_set_waitqueue_priority(queue, queue->maxpriority);
131 rpc_set_waitqueue_owner(queue, 0);
135 * Add new request to a priority queue.
137 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue,
138 struct rpc_task *task,
139 unsigned char queue_priority)
141 struct list_head *q;
142 struct rpc_task *t;
144 INIT_LIST_HEAD(&task->u.tk_wait.links);
145 if (unlikely(queue_priority > queue->maxpriority))
146 queue_priority = queue->maxpriority;
147 if (queue_priority > queue->priority)
148 rpc_set_waitqueue_priority(queue, queue_priority);
149 q = &queue->tasks[queue_priority];
150 list_for_each_entry(t, q, u.tk_wait.list) {
151 if (t->tk_owner == task->tk_owner) {
152 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
153 return;
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,
168 struct rpc_task *task,
169 unsigned char queue_priority)
171 WARN_ON_ONCE(RPC_IS_QUEUED(task));
172 if (RPC_IS_QUEUED(task))
173 return;
175 if (RPC_IS_PRIORITY(queue))
176 __rpc_add_wait_queue_priority(queue, task, queue_priority);
177 else if (RPC_IS_SWAPPER(task))
178 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
179 else
180 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
181 task->tk_waitqueue = queue;
182 queue->qlen++;
183 rpc_set_queued(task);
185 dprintk("RPC: %5u added to queue %p \"%s\"\n",
186 task->tk_pid, queue, rpc_qname(queue));
190 * Remove request from a priority queue.
192 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
194 struct rpc_task *t;
196 if (!list_empty(&task->u.tk_wait.links)) {
197 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
198 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
199 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
204 * Remove request from queue.
205 * Note: must be called with spin lock held.
207 static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
209 __rpc_disable_timer(queue, task);
210 if (RPC_IS_PRIORITY(queue))
211 __rpc_remove_wait_queue_priority(task);
212 list_del(&task->u.tk_wait.list);
213 queue->qlen--;
214 dprintk("RPC: %5u removed from queue %p \"%s\"\n",
215 task->tk_pid, queue, rpc_qname(queue));
218 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
220 int i;
222 spin_lock_init(&queue->lock);
223 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
224 INIT_LIST_HEAD(&queue->tasks[i]);
225 queue->maxpriority = nr_queues - 1;
226 rpc_reset_waitqueue_priority(queue);
227 queue->qlen = 0;
228 setup_timer(&queue->timer_list.timer, __rpc_queue_timer_fn, (unsigned long)queue);
229 INIT_LIST_HEAD(&queue->timer_list.list);
230 rpc_assign_waitqueue_name(queue, qname);
233 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
235 __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
237 EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue);
239 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
241 __rpc_init_priority_wait_queue(queue, qname, 1);
243 EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
245 void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
247 del_timer_sync(&queue->timer_list.timer);
249 EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
251 static int rpc_wait_bit_killable(void *word)
253 if (fatal_signal_pending(current))
254 return -ERESTARTSYS;
255 freezable_schedule();
256 return 0;
259 #ifdef RPC_DEBUG
260 static void rpc_task_set_debuginfo(struct rpc_task *task)
262 static atomic_t rpc_pid;
264 task->tk_pid = atomic_inc_return(&rpc_pid);
266 #else
267 static inline void rpc_task_set_debuginfo(struct rpc_task *task)
270 #endif
272 static void rpc_set_active(struct rpc_task *task)
274 trace_rpc_task_begin(task->tk_client, task, NULL);
276 rpc_task_set_debuginfo(task);
277 set_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
281 * Mark an RPC call as having completed by clearing the 'active' bit
282 * and then waking up all tasks that were sleeping.
284 static int rpc_complete_task(struct rpc_task *task)
286 void *m = &task->tk_runstate;
287 wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE);
288 struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE);
289 unsigned long flags;
290 int ret;
292 trace_rpc_task_complete(task->tk_client, task, NULL);
294 spin_lock_irqsave(&wq->lock, flags);
295 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
296 ret = atomic_dec_and_test(&task->tk_count);
297 if (waitqueue_active(wq))
298 __wake_up_locked_key(wq, TASK_NORMAL, &k);
299 spin_unlock_irqrestore(&wq->lock, flags);
300 return ret;
304 * Allow callers to wait for completion of an RPC call
306 * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit()
307 * to enforce taking of the wq->lock and hence avoid races with
308 * rpc_complete_task().
310 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
312 if (action == NULL)
313 action = rpc_wait_bit_killable;
314 return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
315 action, TASK_KILLABLE);
317 EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
320 * Make an RPC task runnable.
322 * Note: If the task is ASYNC, and is being made runnable after sitting on an
323 * rpc_wait_queue, this must be called with the queue spinlock held to protect
324 * the wait queue operation.
326 static void rpc_make_runnable(struct rpc_task *task)
328 rpc_clear_queued(task);
329 if (rpc_test_and_set_running(task))
330 return;
331 if (RPC_IS_ASYNC(task)) {
332 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
333 queue_work(rpciod_workqueue, &task->u.tk_work);
334 } else
335 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
339 * Prepare for sleeping on a wait queue.
340 * By always appending tasks to the list we ensure FIFO behavior.
341 * NB: An RPC task will only receive interrupt-driven events as long
342 * as it's on a wait queue.
344 static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
345 struct rpc_task *task,
346 rpc_action action,
347 unsigned char queue_priority)
349 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
350 task->tk_pid, rpc_qname(q), jiffies);
352 trace_rpc_task_sleep(task->tk_client, task, q);
354 __rpc_add_wait_queue(q, task, queue_priority);
356 WARN_ON_ONCE(task->tk_callback != NULL);
357 task->tk_callback = action;
358 __rpc_add_timer(q, task);
361 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
362 rpc_action action)
364 /* We shouldn't ever put an inactive task to sleep */
365 WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
366 if (!RPC_IS_ACTIVATED(task)) {
367 task->tk_status = -EIO;
368 rpc_put_task_async(task);
369 return;
373 * Protect the queue operations.
375 spin_lock_bh(&q->lock);
376 __rpc_sleep_on_priority(q, task, action, task->tk_priority);
377 spin_unlock_bh(&q->lock);
379 EXPORT_SYMBOL_GPL(rpc_sleep_on);
381 void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
382 rpc_action action, int priority)
384 /* We shouldn't ever put an inactive task to sleep */
385 WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
386 if (!RPC_IS_ACTIVATED(task)) {
387 task->tk_status = -EIO;
388 rpc_put_task_async(task);
389 return;
393 * Protect the queue operations.
395 spin_lock_bh(&q->lock);
396 __rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW);
397 spin_unlock_bh(&q->lock);
399 EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
402 * __rpc_do_wake_up_task - wake up a single rpc_task
403 * @queue: wait queue
404 * @task: task to be woken up
406 * Caller must hold queue->lock, and have cleared the task queued flag.
408 static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
410 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
411 task->tk_pid, jiffies);
413 /* Has the task been executed yet? If not, we cannot wake it up! */
414 if (!RPC_IS_ACTIVATED(task)) {
415 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
416 return;
419 trace_rpc_task_wakeup(task->tk_client, task, queue);
421 __rpc_remove_wait_queue(queue, task);
423 rpc_make_runnable(task);
425 dprintk("RPC: __rpc_wake_up_task done\n");
429 * Wake up a queued task while the queue lock is being held
431 static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
433 if (RPC_IS_QUEUED(task) && task->tk_waitqueue == queue)
434 __rpc_do_wake_up_task(queue, task);
438 * Tests whether rpc queue is empty
440 int rpc_queue_empty(struct rpc_wait_queue *queue)
442 int res;
444 spin_lock_bh(&queue->lock);
445 res = queue->qlen;
446 spin_unlock_bh(&queue->lock);
447 return res == 0;
449 EXPORT_SYMBOL_GPL(rpc_queue_empty);
452 * Wake up a task on a specific queue
454 void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
456 spin_lock_bh(&queue->lock);
457 rpc_wake_up_task_queue_locked(queue, task);
458 spin_unlock_bh(&queue->lock);
460 EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
463 * Wake up the next task on a priority queue.
465 static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
467 struct list_head *q;
468 struct rpc_task *task;
471 * Service a batch of tasks from a single owner.
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->owner == task->tk_owner) {
477 if (--queue->nr)
478 goto out;
479 list_move_tail(&task->u.tk_wait.list, q);
482 * Check if we need to switch queues.
484 goto new_owner;
488 * Service the next queue.
490 do {
491 if (q == &queue->tasks[0])
492 q = &queue->tasks[queue->maxpriority];
493 else
494 q = q - 1;
495 if (!list_empty(q)) {
496 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
497 goto new_queue;
499 } while (q != &queue->tasks[queue->priority]);
501 rpc_reset_waitqueue_priority(queue);
502 return NULL;
504 new_queue:
505 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
506 new_owner:
507 rpc_set_waitqueue_owner(queue, task->tk_owner);
508 out:
509 return task;
512 static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
514 if (RPC_IS_PRIORITY(queue))
515 return __rpc_find_next_queued_priority(queue);
516 if (!list_empty(&queue->tasks[0]))
517 return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
518 return NULL;
522 * Wake up the first task on the wait queue.
524 struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
525 bool (*func)(struct rpc_task *, void *), void *data)
527 struct rpc_task *task = NULL;
529 dprintk("RPC: wake_up_first(%p \"%s\")\n",
530 queue, rpc_qname(queue));
531 spin_lock_bh(&queue->lock);
532 task = __rpc_find_next_queued(queue);
533 if (task != NULL) {
534 if (func(task, data))
535 rpc_wake_up_task_queue_locked(queue, task);
536 else
537 task = NULL;
539 spin_unlock_bh(&queue->lock);
541 return task;
543 EXPORT_SYMBOL_GPL(rpc_wake_up_first);
545 static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
547 return true;
551 * Wake up the next task on the wait queue.
553 struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
555 return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
557 EXPORT_SYMBOL_GPL(rpc_wake_up_next);
560 * rpc_wake_up - wake up all rpc_tasks
561 * @queue: rpc_wait_queue on which the tasks are sleeping
563 * Grabs queue->lock
565 void rpc_wake_up(struct rpc_wait_queue *queue)
567 struct list_head *head;
569 spin_lock_bh(&queue->lock);
570 head = &queue->tasks[queue->maxpriority];
571 for (;;) {
572 while (!list_empty(head)) {
573 struct rpc_task *task;
574 task = list_first_entry(head,
575 struct rpc_task,
576 u.tk_wait.list);
577 rpc_wake_up_task_queue_locked(queue, task);
579 if (head == &queue->tasks[0])
580 break;
581 head--;
583 spin_unlock_bh(&queue->lock);
585 EXPORT_SYMBOL_GPL(rpc_wake_up);
588 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
589 * @queue: rpc_wait_queue on which the tasks are sleeping
590 * @status: status value to set
592 * Grabs queue->lock
594 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
596 struct list_head *head;
598 spin_lock_bh(&queue->lock);
599 head = &queue->tasks[queue->maxpriority];
600 for (;;) {
601 while (!list_empty(head)) {
602 struct rpc_task *task;
603 task = list_first_entry(head,
604 struct rpc_task,
605 u.tk_wait.list);
606 task->tk_status = status;
607 rpc_wake_up_task_queue_locked(queue, task);
609 if (head == &queue->tasks[0])
610 break;
611 head--;
613 spin_unlock_bh(&queue->lock);
615 EXPORT_SYMBOL_GPL(rpc_wake_up_status);
617 static void __rpc_queue_timer_fn(unsigned long ptr)
619 struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
620 struct rpc_task *task, *n;
621 unsigned long expires, now, timeo;
623 spin_lock(&queue->lock);
624 expires = now = jiffies;
625 list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
626 timeo = task->u.tk_wait.expires;
627 if (time_after_eq(now, timeo)) {
628 dprintk("RPC: %5u timeout\n", task->tk_pid);
629 task->tk_status = -ETIMEDOUT;
630 rpc_wake_up_task_queue_locked(queue, task);
631 continue;
633 if (expires == now || time_after(expires, timeo))
634 expires = timeo;
636 if (!list_empty(&queue->timer_list.list))
637 rpc_set_queue_timer(queue, expires);
638 spin_unlock(&queue->lock);
641 static void __rpc_atrun(struct rpc_task *task)
643 task->tk_status = 0;
647 * Run a task at a later time
649 void rpc_delay(struct rpc_task *task, unsigned long delay)
651 task->tk_timeout = delay;
652 rpc_sleep_on(&delay_queue, task, __rpc_atrun);
654 EXPORT_SYMBOL_GPL(rpc_delay);
657 * Helper to call task->tk_ops->rpc_call_prepare
659 void rpc_prepare_task(struct rpc_task *task)
661 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
664 static void
665 rpc_init_task_statistics(struct rpc_task *task)
667 /* Initialize retry counters */
668 task->tk_garb_retry = 2;
669 task->tk_cred_retry = 2;
670 task->tk_rebind_retry = 2;
672 /* starting timestamp */
673 task->tk_start = ktime_get();
676 static void
677 rpc_reset_task_statistics(struct rpc_task *task)
679 task->tk_timeouts = 0;
680 task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT);
682 rpc_init_task_statistics(task);
686 * Helper that calls task->tk_ops->rpc_call_done if it exists
688 void rpc_exit_task(struct rpc_task *task)
690 task->tk_action = NULL;
691 if (task->tk_ops->rpc_call_done != NULL) {
692 task->tk_ops->rpc_call_done(task, task->tk_calldata);
693 if (task->tk_action != NULL) {
694 WARN_ON(RPC_ASSASSINATED(task));
695 /* Always release the RPC slot and buffer memory */
696 xprt_release(task);
697 rpc_reset_task_statistics(task);
702 void rpc_exit(struct rpc_task *task, int status)
704 task->tk_status = status;
705 task->tk_action = rpc_exit_task;
706 if (RPC_IS_QUEUED(task))
707 rpc_wake_up_queued_task(task->tk_waitqueue, task);
709 EXPORT_SYMBOL_GPL(rpc_exit);
711 void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
713 if (ops->rpc_release != NULL)
714 ops->rpc_release(calldata);
718 * This is the RPC `scheduler' (or rather, the finite state machine).
720 static void __rpc_execute(struct rpc_task *task)
722 struct rpc_wait_queue *queue;
723 int task_is_async = RPC_IS_ASYNC(task);
724 int status = 0;
726 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
727 task->tk_pid, task->tk_flags);
729 WARN_ON_ONCE(RPC_IS_QUEUED(task));
730 if (RPC_IS_QUEUED(task))
731 return;
733 for (;;) {
734 void (*do_action)(struct rpc_task *);
737 * Execute any pending callback first.
739 do_action = task->tk_callback;
740 task->tk_callback = NULL;
741 if (do_action == NULL) {
743 * Perform the next FSM step.
744 * tk_action may be NULL if the task has been killed.
745 * In particular, note that rpc_killall_tasks may
746 * do this at any time, so beware when dereferencing.
748 do_action = task->tk_action;
749 if (do_action == NULL)
750 break;
752 trace_rpc_task_run_action(task->tk_client, task, task->tk_action);
753 do_action(task);
756 * Lockless check for whether task is sleeping or not.
758 if (!RPC_IS_QUEUED(task))
759 continue;
761 * The queue->lock protects against races with
762 * rpc_make_runnable().
764 * Note that once we clear RPC_TASK_RUNNING on an asynchronous
765 * rpc_task, rpc_make_runnable() can assign it to a
766 * different workqueue. We therefore cannot assume that the
767 * rpc_task pointer may still be dereferenced.
769 queue = task->tk_waitqueue;
770 spin_lock_bh(&queue->lock);
771 if (!RPC_IS_QUEUED(task)) {
772 spin_unlock_bh(&queue->lock);
773 continue;
775 rpc_clear_running(task);
776 spin_unlock_bh(&queue->lock);
777 if (task_is_async)
778 return;
780 /* sync task: sleep here */
781 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
782 status = out_of_line_wait_on_bit(&task->tk_runstate,
783 RPC_TASK_QUEUED, rpc_wait_bit_killable,
784 TASK_KILLABLE);
785 if (status == -ERESTARTSYS) {
787 * When a sync task receives a signal, it exits with
788 * -ERESTARTSYS. In order to catch any callbacks that
789 * clean up after sleeping on some queue, we don't
790 * break the loop here, but go around once more.
792 dprintk("RPC: %5u got signal\n", task->tk_pid);
793 task->tk_flags |= RPC_TASK_KILLED;
794 rpc_exit(task, -ERESTARTSYS);
796 rpc_set_running(task);
797 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
800 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
801 task->tk_status);
802 /* Release all resources associated with the task */
803 rpc_release_task(task);
807 * User-visible entry point to the scheduler.
809 * This may be called recursively if e.g. an async NFS task updates
810 * the attributes and finds that dirty pages must be flushed.
811 * NOTE: Upon exit of this function the task is guaranteed to be
812 * released. In particular note that tk_release() will have
813 * been called, so your task memory may have been freed.
815 void rpc_execute(struct rpc_task *task)
817 rpc_set_active(task);
818 rpc_make_runnable(task);
819 if (!RPC_IS_ASYNC(task))
820 __rpc_execute(task);
823 static void rpc_async_schedule(struct work_struct *work)
825 current->flags |= PF_FSTRANS;
826 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
827 current->flags &= ~PF_FSTRANS;
831 * rpc_malloc - allocate an RPC buffer
832 * @task: RPC task that will use this buffer
833 * @size: requested byte size
835 * To prevent rpciod from hanging, this allocator never sleeps,
836 * returning NULL if the request cannot be serviced immediately.
837 * The caller can arrange to sleep in a way that is safe for rpciod.
839 * Most requests are 'small' (under 2KiB) and can be serviced from a
840 * mempool, ensuring that NFS reads and writes can always proceed,
841 * and that there is good locality of reference for these buffers.
843 * In order to avoid memory starvation triggering more writebacks of
844 * NFS requests, we avoid using GFP_KERNEL.
846 void *rpc_malloc(struct rpc_task *task, size_t size)
848 struct rpc_buffer *buf;
849 gfp_t gfp = GFP_NOWAIT;
851 if (RPC_IS_SWAPPER(task))
852 gfp |= __GFP_MEMALLOC;
854 size += sizeof(struct rpc_buffer);
855 if (size <= RPC_BUFFER_MAXSIZE)
856 buf = mempool_alloc(rpc_buffer_mempool, gfp);
857 else
858 buf = kmalloc(size, gfp);
860 if (!buf)
861 return NULL;
863 buf->len = size;
864 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
865 task->tk_pid, size, buf);
866 return &buf->data;
868 EXPORT_SYMBOL_GPL(rpc_malloc);
871 * rpc_free - free buffer allocated via rpc_malloc
872 * @buffer: buffer to free
875 void rpc_free(void *buffer)
877 size_t size;
878 struct rpc_buffer *buf;
880 if (!buffer)
881 return;
883 buf = container_of(buffer, struct rpc_buffer, data);
884 size = buf->len;
886 dprintk("RPC: freeing buffer of size %zu at %p\n",
887 size, buf);
889 if (size <= RPC_BUFFER_MAXSIZE)
890 mempool_free(buf, rpc_buffer_mempool);
891 else
892 kfree(buf);
894 EXPORT_SYMBOL_GPL(rpc_free);
897 * Creation and deletion of RPC task structures
899 static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
901 memset(task, 0, sizeof(*task));
902 atomic_set(&task->tk_count, 1);
903 task->tk_flags = task_setup_data->flags;
904 task->tk_ops = task_setup_data->callback_ops;
905 task->tk_calldata = task_setup_data->callback_data;
906 INIT_LIST_HEAD(&task->tk_task);
908 task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
909 task->tk_owner = current->tgid;
911 /* Initialize workqueue for async tasks */
912 task->tk_workqueue = task_setup_data->workqueue;
914 if (task->tk_ops->rpc_call_prepare != NULL)
915 task->tk_action = rpc_prepare_task;
917 rpc_init_task_statistics(task);
919 dprintk("RPC: new task initialized, procpid %u\n",
920 task_pid_nr(current));
923 static struct rpc_task *
924 rpc_alloc_task(void)
926 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO);
930 * Create a new task for the specified client.
932 struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
934 struct rpc_task *task = setup_data->task;
935 unsigned short flags = 0;
937 if (task == NULL) {
938 task = rpc_alloc_task();
939 if (task == NULL) {
940 rpc_release_calldata(setup_data->callback_ops,
941 setup_data->callback_data);
942 return ERR_PTR(-ENOMEM);
944 flags = RPC_TASK_DYNAMIC;
947 rpc_init_task(task, setup_data);
948 task->tk_flags |= flags;
949 dprintk("RPC: allocated task %p\n", task);
950 return task;
954 * rpc_free_task - release rpc task and perform cleanups
956 * Note that we free up the rpc_task _after_ rpc_release_calldata()
957 * in order to work around a workqueue dependency issue.
959 * Tejun Heo states:
960 * "Workqueue currently considers two work items to be the same if they're
961 * on the same address and won't execute them concurrently - ie. it
962 * makes a work item which is queued again while being executed wait
963 * for the previous execution to complete.
965 * If a work function frees the work item, and then waits for an event
966 * which should be performed by another work item and *that* work item
967 * recycles the freed work item, it can create a false dependency loop.
968 * There really is no reliable way to detect this short of verifying
969 * every memory free."
972 static void rpc_free_task(struct rpc_task *task)
974 unsigned short tk_flags = task->tk_flags;
976 rpc_release_calldata(task->tk_ops, task->tk_calldata);
978 if (tk_flags & RPC_TASK_DYNAMIC) {
979 dprintk("RPC: %5u freeing task\n", task->tk_pid);
980 mempool_free(task, rpc_task_mempool);
984 static void rpc_async_release(struct work_struct *work)
986 rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
989 static void rpc_release_resources_task(struct rpc_task *task)
991 xprt_release(task);
992 if (task->tk_msg.rpc_cred) {
993 put_rpccred(task->tk_msg.rpc_cred);
994 task->tk_msg.rpc_cred = NULL;
996 rpc_task_release_client(task);
999 static void rpc_final_put_task(struct rpc_task *task,
1000 struct workqueue_struct *q)
1002 if (q != NULL) {
1003 INIT_WORK(&task->u.tk_work, rpc_async_release);
1004 queue_work(q, &task->u.tk_work);
1005 } else
1006 rpc_free_task(task);
1009 static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
1011 if (atomic_dec_and_test(&task->tk_count)) {
1012 rpc_release_resources_task(task);
1013 rpc_final_put_task(task, q);
1017 void rpc_put_task(struct rpc_task *task)
1019 rpc_do_put_task(task, NULL);
1021 EXPORT_SYMBOL_GPL(rpc_put_task);
1023 void rpc_put_task_async(struct rpc_task *task)
1025 rpc_do_put_task(task, task->tk_workqueue);
1027 EXPORT_SYMBOL_GPL(rpc_put_task_async);
1029 static void rpc_release_task(struct rpc_task *task)
1031 dprintk("RPC: %5u release task\n", task->tk_pid);
1033 WARN_ON_ONCE(RPC_IS_QUEUED(task));
1035 rpc_release_resources_task(task);
1038 * Note: at this point we have been removed from rpc_clnt->cl_tasks,
1039 * so it should be safe to use task->tk_count as a test for whether
1040 * or not any other processes still hold references to our rpc_task.
1042 if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
1043 /* Wake up anyone who may be waiting for task completion */
1044 if (!rpc_complete_task(task))
1045 return;
1046 } else {
1047 if (!atomic_dec_and_test(&task->tk_count))
1048 return;
1050 rpc_final_put_task(task, task->tk_workqueue);
1053 int rpciod_up(void)
1055 return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1058 void rpciod_down(void)
1060 module_put(THIS_MODULE);
1064 * Start up the rpciod workqueue.
1066 static int rpciod_start(void)
1068 struct workqueue_struct *wq;
1071 * Create the rpciod thread and wait for it to start.
1073 dprintk("RPC: creating workqueue rpciod\n");
1074 wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM, 1);
1075 rpciod_workqueue = wq;
1076 return rpciod_workqueue != NULL;
1079 static void rpciod_stop(void)
1081 struct workqueue_struct *wq = NULL;
1083 if (rpciod_workqueue == NULL)
1084 return;
1085 dprintk("RPC: destroying workqueue rpciod\n");
1087 wq = rpciod_workqueue;
1088 rpciod_workqueue = NULL;
1089 destroy_workqueue(wq);
1092 void
1093 rpc_destroy_mempool(void)
1095 rpciod_stop();
1096 if (rpc_buffer_mempool)
1097 mempool_destroy(rpc_buffer_mempool);
1098 if (rpc_task_mempool)
1099 mempool_destroy(rpc_task_mempool);
1100 if (rpc_task_slabp)
1101 kmem_cache_destroy(rpc_task_slabp);
1102 if (rpc_buffer_slabp)
1103 kmem_cache_destroy(rpc_buffer_slabp);
1104 rpc_destroy_wait_queue(&delay_queue);
1108 rpc_init_mempool(void)
1111 * The following is not strictly a mempool initialisation,
1112 * but there is no harm in doing it here
1114 rpc_init_wait_queue(&delay_queue, "delayq");
1115 if (!rpciod_start())
1116 goto err_nomem;
1118 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1119 sizeof(struct rpc_task),
1120 0, SLAB_HWCACHE_ALIGN,
1121 NULL);
1122 if (!rpc_task_slabp)
1123 goto err_nomem;
1124 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1125 RPC_BUFFER_MAXSIZE,
1126 0, SLAB_HWCACHE_ALIGN,
1127 NULL);
1128 if (!rpc_buffer_slabp)
1129 goto err_nomem;
1130 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1131 rpc_task_slabp);
1132 if (!rpc_task_mempool)
1133 goto err_nomem;
1134 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1135 rpc_buffer_slabp);
1136 if (!rpc_buffer_mempool)
1137 goto err_nomem;
1138 return 0;
1139 err_nomem:
1140 rpc_destroy_mempool();
1141 return -ENOMEM;