xref: /linux/net/sunrpc/sched.c (revision a76580fbf09e6e19c2040c08969af5137e064eda)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/net/sunrpc/sched.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Scheduling for synchronous and asynchronous RPC requests.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * TCP NFS related read + write fixes
91da177e4SLinus Torvalds  * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/interrupt.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/smp.h>
191da177e4SLinus Torvalds #include <linux/spinlock.h>
204a3e2f71SArjan van de Ven #include <linux/mutex.h>
21d310310cSJeff Layton #include <linux/freezer.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
241da177e4SLinus Torvalds 
256951867bSBenny Halevy #include "sunrpc.h"
266951867bSBenny Halevy 
271da177e4SLinus Torvalds #ifdef RPC_DEBUG
281da177e4SLinus Torvalds #define RPCDBG_FACILITY		RPCDBG_SCHED
291da177e4SLinus Torvalds #endif
301da177e4SLinus Torvalds 
3182b0a4c3STrond Myklebust #define CREATE_TRACE_POINTS
3282b0a4c3STrond Myklebust #include <trace/events/sunrpc.h>
3382b0a4c3STrond Myklebust 
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * RPC slabs and memory pools
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE	(2048)
381da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE	(8)
391da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE	(8)
40e18b890bSChristoph Lameter static struct kmem_cache	*rpc_task_slabp __read_mostly;
41e18b890bSChristoph Lameter static struct kmem_cache	*rpc_buffer_slabp __read_mostly;
42ba89966cSEric Dumazet static mempool_t	*rpc_task_mempool __read_mostly;
43ba89966cSEric Dumazet static mempool_t	*rpc_buffer_mempool __read_mostly;
441da177e4SLinus Torvalds 
4565f27f38SDavid Howells static void			rpc_async_schedule(struct work_struct *);
46bde8f00cSTrond Myklebust static void			 rpc_release_task(struct rpc_task *task);
4736df9aaeSTrond Myklebust static void __rpc_queue_timer_fn(unsigned long ptr);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * RPC tasks sit here while waiting for conditions to improve.
511da177e4SLinus Torvalds  */
52a4a87499STrond Myklebust static struct rpc_wait_queue delay_queue;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * rpciod-related stuff
561da177e4SLinus Torvalds  */
5724c5d9d7STrond Myklebust struct workqueue_struct *rpciod_workqueue;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * Disable the timer for a given RPC task. Should be called with
611da177e4SLinus Torvalds  * queue->lock and bh_disabled in order to avoid races within
621da177e4SLinus Torvalds  * rpc_run_timer().
631da177e4SLinus Torvalds  */
645d00837bSTrond Myklebust static void
65eb276c0eSTrond Myklebust __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
661da177e4SLinus Torvalds {
6736df9aaeSTrond Myklebust 	if (task->tk_timeout == 0)
6836df9aaeSTrond Myklebust 		return;
6946121cf7SChuck Lever 	dprintk("RPC: %5u disabling timer\n", task->tk_pid);
701da177e4SLinus Torvalds 	task->tk_timeout = 0;
7136df9aaeSTrond Myklebust 	list_del(&task->u.tk_wait.timer_list);
72eb276c0eSTrond Myklebust 	if (list_empty(&queue->timer_list.list))
73eb276c0eSTrond Myklebust 		del_timer(&queue->timer_list.timer);
7436df9aaeSTrond Myklebust }
7536df9aaeSTrond Myklebust 
7636df9aaeSTrond Myklebust static void
7736df9aaeSTrond Myklebust rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
7836df9aaeSTrond Myklebust {
7936df9aaeSTrond Myklebust 	queue->timer_list.expires = expires;
8036df9aaeSTrond Myklebust 	mod_timer(&queue->timer_list.timer, expires);
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * Set up a timer for the current task.
851da177e4SLinus Torvalds  */
865d00837bSTrond Myklebust static void
87eb276c0eSTrond Myklebust __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	if (!task->tk_timeout)
901da177e4SLinus Torvalds 		return;
911da177e4SLinus Torvalds 
9246121cf7SChuck Lever 	dprintk("RPC: %5u setting alarm for %lu ms\n",
931da177e4SLinus Torvalds 			task->tk_pid, task->tk_timeout * 1000 / HZ);
941da177e4SLinus Torvalds 
95eb276c0eSTrond Myklebust 	task->u.tk_wait.expires = jiffies + task->tk_timeout;
96eb276c0eSTrond Myklebust 	if (list_empty(&queue->timer_list.list) || time_before(task->u.tk_wait.expires, queue->timer_list.expires))
97eb276c0eSTrond Myklebust 		rpc_set_queue_timer(queue, task->u.tk_wait.expires);
98eb276c0eSTrond Myklebust 	list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list);
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
101edd2e36fSTrond Myklebust static void rpc_rotate_queue_owner(struct rpc_wait_queue *queue)
102edd2e36fSTrond Myklebust {
103edd2e36fSTrond Myklebust 	struct list_head *q = &queue->tasks[queue->priority];
104edd2e36fSTrond Myklebust 	struct rpc_task *task;
105edd2e36fSTrond Myklebust 
106edd2e36fSTrond Myklebust 	if (!list_empty(q)) {
107edd2e36fSTrond Myklebust 		task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
108edd2e36fSTrond Myklebust 		if (task->tk_owner == queue->owner)
109edd2e36fSTrond Myklebust 			list_move_tail(&task->u.tk_wait.list, q);
110edd2e36fSTrond Myklebust 	}
111edd2e36fSTrond Myklebust }
112edd2e36fSTrond Myklebust 
113c05eecf6STrond Myklebust static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
114c05eecf6STrond Myklebust {
115edd2e36fSTrond Myklebust 	if (queue->priority != priority) {
116edd2e36fSTrond Myklebust 		/* Fairness: rotate the list when changing priority */
117edd2e36fSTrond Myklebust 		rpc_rotate_queue_owner(queue);
118c05eecf6STrond Myklebust 		queue->priority = priority;
119c05eecf6STrond Myklebust 	}
120edd2e36fSTrond Myklebust }
121c05eecf6STrond Myklebust 
122c05eecf6STrond Myklebust static void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
123c05eecf6STrond Myklebust {
124c05eecf6STrond Myklebust 	queue->owner = pid;
125c05eecf6STrond Myklebust 	queue->nr = RPC_BATCH_COUNT;
126c05eecf6STrond Myklebust }
127c05eecf6STrond Myklebust 
128c05eecf6STrond Myklebust static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
129c05eecf6STrond Myklebust {
130c05eecf6STrond Myklebust 	rpc_set_waitqueue_priority(queue, queue->maxpriority);
131c05eecf6STrond Myklebust 	rpc_set_waitqueue_owner(queue, 0);
132c05eecf6STrond Myklebust }
133c05eecf6STrond Myklebust 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * Add new request to a priority queue.
1361da177e4SLinus Torvalds  */
1373b27bad7STrond Myklebust static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue,
1383b27bad7STrond Myklebust 		struct rpc_task *task,
1393b27bad7STrond Myklebust 		unsigned char queue_priority)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct list_head *q;
1421da177e4SLinus Torvalds 	struct rpc_task *t;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	INIT_LIST_HEAD(&task->u.tk_wait.links);
1453b27bad7STrond Myklebust 	if (unlikely(queue_priority > queue->maxpriority))
146c05eecf6STrond Myklebust 		queue_priority = queue->maxpriority;
147c05eecf6STrond Myklebust 	if (queue_priority > queue->priority)
148c05eecf6STrond Myklebust 		rpc_set_waitqueue_priority(queue, queue_priority);
149c05eecf6STrond Myklebust 	q = &queue->tasks[queue_priority];
1501da177e4SLinus Torvalds 	list_for_each_entry(t, q, u.tk_wait.list) {
1513ff7576dSTrond Myklebust 		if (t->tk_owner == task->tk_owner) {
1521da177e4SLinus Torvalds 			list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
1531da177e4SLinus Torvalds 			return;
1541da177e4SLinus Torvalds 		}
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 	list_add_tail(&task->u.tk_wait.list, q);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /*
1601da177e4SLinus Torvalds  * Add new request to wait queue.
1611da177e4SLinus Torvalds  *
1621da177e4SLinus Torvalds  * Swapper tasks always get inserted at the head of the queue.
1631da177e4SLinus Torvalds  * This should avoid many nasty memory deadlocks and hopefully
1641da177e4SLinus Torvalds  * improve overall performance.
1651da177e4SLinus Torvalds  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
1661da177e4SLinus Torvalds  */
1673b27bad7STrond Myklebust static void __rpc_add_wait_queue(struct rpc_wait_queue *queue,
1683b27bad7STrond Myklebust 		struct rpc_task *task,
1693b27bad7STrond Myklebust 		unsigned char queue_priority)
1701da177e4SLinus Torvalds {
1712bd4eef8SWeston Andros Adamson 	WARN_ON_ONCE(RPC_IS_QUEUED(task));
1722bd4eef8SWeston Andros Adamson 	if (RPC_IS_QUEUED(task))
1732bd4eef8SWeston Andros Adamson 		return;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1763b27bad7STrond Myklebust 		__rpc_add_wait_queue_priority(queue, task, queue_priority);
1771da177e4SLinus Torvalds 	else if (RPC_IS_SWAPPER(task))
1781da177e4SLinus Torvalds 		list_add(&task->u.tk_wait.list, &queue->tasks[0]);
1791da177e4SLinus Torvalds 	else
1801da177e4SLinus Torvalds 		list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
18196ef13b2STrond Myklebust 	task->tk_waitqueue = queue;
182e19b63daSChuck Lever 	queue->qlen++;
1831166fde6STrond Myklebust 	/* barrier matches the read in rpc_wake_up_task_queue_locked() */
1841166fde6STrond Myklebust 	smp_wmb();
1851da177e4SLinus Torvalds 	rpc_set_queued(task);
1861da177e4SLinus Torvalds 
18746121cf7SChuck Lever 	dprintk("RPC: %5u added to queue %p \"%s\"\n",
1881da177e4SLinus Torvalds 			task->tk_pid, queue, rpc_qname(queue));
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*
1921da177e4SLinus Torvalds  * Remove request from a priority queue.
1931da177e4SLinus Torvalds  */
1941da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	struct rpc_task *t;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (!list_empty(&task->u.tk_wait.links)) {
1991da177e4SLinus Torvalds 		t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
2001da177e4SLinus Torvalds 		list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
2011da177e4SLinus Torvalds 		list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  * Remove request from queue.
2071da177e4SLinus Torvalds  * Note: must be called with spin lock held.
2081da177e4SLinus Torvalds  */
20996ef13b2STrond Myklebust static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
2101da177e4SLinus Torvalds {
211eb276c0eSTrond Myklebust 	__rpc_disable_timer(queue, task);
2121da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
2131da177e4SLinus Torvalds 		__rpc_remove_wait_queue_priority(task);
2141da177e4SLinus Torvalds 	list_del(&task->u.tk_wait.list);
215e19b63daSChuck Lever 	queue->qlen--;
21646121cf7SChuck Lever 	dprintk("RPC: %5u removed from queue %p \"%s\"\n",
2171da177e4SLinus Torvalds 			task->tk_pid, queue, rpc_qname(queue));
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2203ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	int i;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	spin_lock_init(&queue->lock);
2251da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
2261da177e4SLinus Torvalds 		INIT_LIST_HEAD(&queue->tasks[i]);
2273ff7576dSTrond Myklebust 	queue->maxpriority = nr_queues - 1;
2281da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
22936df9aaeSTrond Myklebust 	queue->qlen = 0;
23036df9aaeSTrond Myklebust 	setup_timer(&queue->timer_list.timer, __rpc_queue_timer_fn, (unsigned long)queue);
23136df9aaeSTrond Myklebust 	INIT_LIST_HEAD(&queue->timer_list.list);
2322f09c242STrond Myklebust 	rpc_assign_waitqueue_name(queue, qname);
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2361da177e4SLinus Torvalds {
2373ff7576dSTrond Myklebust 	__rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
2381da177e4SLinus Torvalds }
239689cf5c1SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2421da177e4SLinus Torvalds {
2433ff7576dSTrond Myklebust 	__rpc_init_priority_wait_queue(queue, qname, 1);
2441da177e4SLinus Torvalds }
245e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
2461da177e4SLinus Torvalds 
247f6a1cc89STrond Myklebust void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
248f6a1cc89STrond Myklebust {
24936df9aaeSTrond Myklebust 	del_timer_sync(&queue->timer_list.timer);
250f6a1cc89STrond Myklebust }
251f6a1cc89STrond Myklebust EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
252f6a1cc89STrond Myklebust 
253150030b7SMatthew Wilcox static int rpc_wait_bit_killable(void *word)
25444c28873STrond Myklebust {
255150030b7SMatthew Wilcox 	if (fatal_signal_pending(current))
25644c28873STrond Myklebust 		return -ERESTARTSYS;
257d310310cSJeff Layton 	freezable_schedule();
25844c28873STrond Myklebust 	return 0;
25944c28873STrond Myklebust }
26044c28873STrond Myklebust 
261c44fe705STrond Myklebust #ifdef RPC_DEBUG
262c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task)
263c44fe705STrond Myklebust {
264c44fe705STrond Myklebust 	static atomic_t rpc_pid;
265c44fe705STrond Myklebust 
266c44fe705STrond Myklebust 	task->tk_pid = atomic_inc_return(&rpc_pid);
267c44fe705STrond Myklebust }
268c44fe705STrond Myklebust #else
269c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task)
270c44fe705STrond Myklebust {
271c44fe705STrond Myklebust }
272c44fe705STrond Myklebust #endif
273c44fe705STrond Myklebust 
274e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task)
275e6b3c4dbSTrond Myklebust {
27682b0a4c3STrond Myklebust 	trace_rpc_task_begin(task->tk_client, task, NULL);
27782b0a4c3STrond Myklebust 
278c44fe705STrond Myklebust 	rpc_task_set_debuginfo(task);
27958f9612cSTrond Myklebust 	set_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
280e6b3c4dbSTrond Myklebust }
281e6b3c4dbSTrond Myklebust 
28244c28873STrond Myklebust /*
28344c28873STrond Myklebust  * Mark an RPC call as having completed by clearing the 'active' bit
284bf294b41STrond Myklebust  * and then waking up all tasks that were sleeping.
28544c28873STrond Myklebust  */
286bf294b41STrond Myklebust static int rpc_complete_task(struct rpc_task *task)
28744c28873STrond Myklebust {
288bf294b41STrond Myklebust 	void *m = &task->tk_runstate;
289bf294b41STrond Myklebust 	wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE);
290bf294b41STrond Myklebust 	struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE);
291bf294b41STrond Myklebust 	unsigned long flags;
292bf294b41STrond Myklebust 	int ret;
293bf294b41STrond Myklebust 
29482b0a4c3STrond Myklebust 	trace_rpc_task_complete(task->tk_client, task, NULL);
29582b0a4c3STrond Myklebust 
296bf294b41STrond Myklebust 	spin_lock_irqsave(&wq->lock, flags);
297e6b3c4dbSTrond Myklebust 	clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
298bf294b41STrond Myklebust 	ret = atomic_dec_and_test(&task->tk_count);
299bf294b41STrond Myklebust 	if (waitqueue_active(wq))
300bf294b41STrond Myklebust 		__wake_up_locked_key(wq, TASK_NORMAL, &k);
301bf294b41STrond Myklebust 	spin_unlock_irqrestore(&wq->lock, flags);
302bf294b41STrond Myklebust 	return ret;
30344c28873STrond Myklebust }
30444c28873STrond Myklebust 
30544c28873STrond Myklebust /*
30644c28873STrond Myklebust  * Allow callers to wait for completion of an RPC call
307bf294b41STrond Myklebust  *
308bf294b41STrond Myklebust  * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit()
309bf294b41STrond Myklebust  * to enforce taking of the wq->lock and hence avoid races with
310bf294b41STrond Myklebust  * rpc_complete_task().
31144c28873STrond Myklebust  */
31244c28873STrond Myklebust int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
31344c28873STrond Myklebust {
31444c28873STrond Myklebust 	if (action == NULL)
315150030b7SMatthew Wilcox 		action = rpc_wait_bit_killable;
316bf294b41STrond Myklebust 	return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
317150030b7SMatthew Wilcox 			action, TASK_KILLABLE);
31844c28873STrond Myklebust }
319e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
32044c28873STrond Myklebust 
3211da177e4SLinus Torvalds /*
3221da177e4SLinus Torvalds  * Make an RPC task runnable.
3231da177e4SLinus Torvalds  *
324506026c3SJeff Layton  * Note: If the task is ASYNC, and is being made runnable after sitting on an
325506026c3SJeff Layton  * rpc_wait_queue, this must be called with the queue spinlock held to protect
326506026c3SJeff Layton  * the wait queue operation.
327a3c3cac5STrond Myklebust  * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(),
328a3c3cac5STrond Myklebust  * which is needed to ensure that __rpc_execute() doesn't loop (due to the
329a3c3cac5STrond Myklebust  * lockless RPC_IS_QUEUED() test) before we've had a chance to test
330a3c3cac5STrond Myklebust  * the RPC_TASK_RUNNING flag.
3311da177e4SLinus Torvalds  */
3321da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task)
3331da177e4SLinus Torvalds {
334a3c3cac5STrond Myklebust 	bool need_wakeup = !rpc_test_and_set_running(task);
335a3c3cac5STrond Myklebust 
3361da177e4SLinus Torvalds 	rpc_clear_queued(task);
337a3c3cac5STrond Myklebust 	if (!need_wakeup)
3381da177e4SLinus Torvalds 		return;
3391da177e4SLinus Torvalds 	if (RPC_IS_ASYNC(task)) {
34065f27f38SDavid Howells 		INIT_WORK(&task->u.tk_work, rpc_async_schedule);
341cee6a537SFred Isaman 		queue_work(rpciod_workqueue, &task->u.tk_work);
3421da177e4SLinus Torvalds 	} else
34396651ab3STrond Myklebust 		wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /*
3471da177e4SLinus Torvalds  * Prepare for sleeping on a wait queue.
3481da177e4SLinus Torvalds  * By always appending tasks to the list we ensure FIFO behavior.
3491da177e4SLinus Torvalds  * NB: An RPC task will only receive interrupt-driven events as long
3501da177e4SLinus Torvalds  * as it's on a wait queue.
3511da177e4SLinus Torvalds  */
3523b27bad7STrond Myklebust static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
3533b27bad7STrond Myklebust 		struct rpc_task *task,
3543b27bad7STrond Myklebust 		rpc_action action,
3553b27bad7STrond Myklebust 		unsigned char queue_priority)
3561da177e4SLinus Torvalds {
35746121cf7SChuck Lever 	dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
35846121cf7SChuck Lever 			task->tk_pid, rpc_qname(q), jiffies);
3591da177e4SLinus Torvalds 
36082b0a4c3STrond Myklebust 	trace_rpc_task_sleep(task->tk_client, task, q);
36182b0a4c3STrond Myklebust 
3623b27bad7STrond Myklebust 	__rpc_add_wait_queue(q, task, queue_priority);
3631da177e4SLinus Torvalds 
364f50ad428SWeston Andros Adamson 	WARN_ON_ONCE(task->tk_callback != NULL);
3651da177e4SLinus Torvalds 	task->tk_callback = action;
366eb276c0eSTrond Myklebust 	__rpc_add_timer(q, task);
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3705d00837bSTrond Myklebust 				rpc_action action)
3711da177e4SLinus Torvalds {
37258f9612cSTrond Myklebust 	/* We shouldn't ever put an inactive task to sleep */
373e454a7a8SWeston Andros Adamson 	WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
374e454a7a8SWeston Andros Adamson 	if (!RPC_IS_ACTIVATED(task)) {
375e454a7a8SWeston Andros Adamson 		task->tk_status = -EIO;
376e454a7a8SWeston Andros Adamson 		rpc_put_task_async(task);
377e454a7a8SWeston Andros Adamson 		return;
378e454a7a8SWeston Andros Adamson 	}
379e6b3c4dbSTrond Myklebust 
3801da177e4SLinus Torvalds 	/*
3811da177e4SLinus Torvalds 	 * Protect the queue operations.
3821da177e4SLinus Torvalds 	 */
3831da177e4SLinus Torvalds 	spin_lock_bh(&q->lock);
3843b27bad7STrond Myklebust 	__rpc_sleep_on_priority(q, task, action, task->tk_priority);
3851da177e4SLinus Torvalds 	spin_unlock_bh(&q->lock);
3861da177e4SLinus Torvalds }
387e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on);
3881da177e4SLinus Torvalds 
3893b27bad7STrond Myklebust void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
3903b27bad7STrond Myklebust 		rpc_action action, int priority)
3913b27bad7STrond Myklebust {
3923b27bad7STrond Myklebust 	/* We shouldn't ever put an inactive task to sleep */
393e454a7a8SWeston Andros Adamson 	WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
394e454a7a8SWeston Andros Adamson 	if (!RPC_IS_ACTIVATED(task)) {
395e454a7a8SWeston Andros Adamson 		task->tk_status = -EIO;
396e454a7a8SWeston Andros Adamson 		rpc_put_task_async(task);
397e454a7a8SWeston Andros Adamson 		return;
398e454a7a8SWeston Andros Adamson 	}
3993b27bad7STrond Myklebust 
4003b27bad7STrond Myklebust 	/*
4013b27bad7STrond Myklebust 	 * Protect the queue operations.
4023b27bad7STrond Myklebust 	 */
4033b27bad7STrond Myklebust 	spin_lock_bh(&q->lock);
4043b27bad7STrond Myklebust 	__rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW);
4053b27bad7STrond Myklebust 	spin_unlock_bh(&q->lock);
4063b27bad7STrond Myklebust }
4071e1093c7STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
4083b27bad7STrond Myklebust 
4091da177e4SLinus Torvalds /**
4101da177e4SLinus Torvalds  * __rpc_do_wake_up_task - wake up a single rpc_task
41196ef13b2STrond Myklebust  * @queue: wait queue
4121da177e4SLinus Torvalds  * @task: task to be woken up
4131da177e4SLinus Torvalds  *
4141da177e4SLinus Torvalds  * Caller must hold queue->lock, and have cleared the task queued flag.
4151da177e4SLinus Torvalds  */
41696ef13b2STrond Myklebust static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
4171da177e4SLinus Torvalds {
41846121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
41946121cf7SChuck Lever 			task->tk_pid, jiffies);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	/* Has the task been executed yet? If not, we cannot wake it up! */
4221da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task)) {
4231da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
4241da177e4SLinus Torvalds 		return;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
42782b0a4c3STrond Myklebust 	trace_rpc_task_wakeup(task->tk_client, task, queue);
42882b0a4c3STrond Myklebust 
42996ef13b2STrond Myklebust 	__rpc_remove_wait_queue(queue, task);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	rpc_make_runnable(task);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	dprintk("RPC:       __rpc_wake_up_task done\n");
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /*
43796ef13b2STrond Myklebust  * Wake up a queued task while the queue lock is being held
4381da177e4SLinus Torvalds  */
43996ef13b2STrond Myklebust static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
4401da177e4SLinus Torvalds {
4411166fde6STrond Myklebust 	if (RPC_IS_QUEUED(task)) {
4421166fde6STrond Myklebust 		smp_rmb();
4431166fde6STrond Myklebust 		if (task->tk_waitqueue == queue)
44496ef13b2STrond Myklebust 			__rpc_do_wake_up_task(queue, task);
4451da177e4SLinus Torvalds 	}
4461166fde6STrond Myklebust }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /*
44948f18612SAlexandros Batsakis  * Tests whether rpc queue is empty
45048f18612SAlexandros Batsakis  */
45148f18612SAlexandros Batsakis int rpc_queue_empty(struct rpc_wait_queue *queue)
45248f18612SAlexandros Batsakis {
45348f18612SAlexandros Batsakis 	int res;
45448f18612SAlexandros Batsakis 
45548f18612SAlexandros Batsakis 	spin_lock_bh(&queue->lock);
45648f18612SAlexandros Batsakis 	res = queue->qlen;
45748f18612SAlexandros Batsakis 	spin_unlock_bh(&queue->lock);
458a02cec21SEric Dumazet 	return res == 0;
45948f18612SAlexandros Batsakis }
46048f18612SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_queue_empty);
46148f18612SAlexandros Batsakis 
46248f18612SAlexandros Batsakis /*
46396ef13b2STrond Myklebust  * Wake up a task on a specific queue
46496ef13b2STrond Myklebust  */
46596ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
46696ef13b2STrond Myklebust {
4675e4424afSTrond Myklebust 	spin_lock_bh(&queue->lock);
46896ef13b2STrond Myklebust 	rpc_wake_up_task_queue_locked(queue, task);
4695e4424afSTrond Myklebust 	spin_unlock_bh(&queue->lock);
47096ef13b2STrond Myklebust }
47196ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
47296ef13b2STrond Myklebust 
47396ef13b2STrond Myklebust /*
4741da177e4SLinus Torvalds  * Wake up the next task on a priority queue.
4751da177e4SLinus Torvalds  */
476961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct list_head *q;
4791da177e4SLinus Torvalds 	struct rpc_task *task;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/*
4823ff7576dSTrond Myklebust 	 * Service a batch of tasks from a single owner.
4831da177e4SLinus Torvalds 	 */
4841da177e4SLinus Torvalds 	q = &queue->tasks[queue->priority];
4851da177e4SLinus Torvalds 	if (!list_empty(q)) {
4861da177e4SLinus Torvalds 		task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4873ff7576dSTrond Myklebust 		if (queue->owner == task->tk_owner) {
4881da177e4SLinus Torvalds 			if (--queue->nr)
4891da177e4SLinus Torvalds 				goto out;
4901da177e4SLinus Torvalds 			list_move_tail(&task->u.tk_wait.list, q);
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 		/*
4931da177e4SLinus Torvalds 		 * Check if we need to switch queues.
4941da177e4SLinus Torvalds 		 */
4953ff7576dSTrond Myklebust 		goto new_owner;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/*
4991da177e4SLinus Torvalds 	 * Service the next queue.
5001da177e4SLinus Torvalds 	 */
5011da177e4SLinus Torvalds 	do {
5021da177e4SLinus Torvalds 		if (q == &queue->tasks[0])
5031da177e4SLinus Torvalds 			q = &queue->tasks[queue->maxpriority];
5041da177e4SLinus Torvalds 		else
5051da177e4SLinus Torvalds 			q = q - 1;
5061da177e4SLinus Torvalds 		if (!list_empty(q)) {
5071da177e4SLinus Torvalds 			task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
5081da177e4SLinus Torvalds 			goto new_queue;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 	} while (q != &queue->tasks[queue->priority]);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
5131da177e4SLinus Torvalds 	return NULL;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds new_queue:
5161da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
5173ff7576dSTrond Myklebust new_owner:
5183ff7576dSTrond Myklebust 	rpc_set_waitqueue_owner(queue, task->tk_owner);
5191da177e4SLinus Torvalds out:
5201da177e4SLinus Torvalds 	return task;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
523961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
524961a828dSTrond Myklebust {
525961a828dSTrond Myklebust 	if (RPC_IS_PRIORITY(queue))
526961a828dSTrond Myklebust 		return __rpc_find_next_queued_priority(queue);
527961a828dSTrond Myklebust 	if (!list_empty(&queue->tasks[0]))
528961a828dSTrond Myklebust 		return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
529961a828dSTrond Myklebust 	return NULL;
530961a828dSTrond Myklebust }
531961a828dSTrond Myklebust 
532961a828dSTrond Myklebust /*
533961a828dSTrond Myklebust  * Wake up the first task on the wait queue.
534961a828dSTrond Myklebust  */
535961a828dSTrond Myklebust struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
536961a828dSTrond Myklebust 		bool (*func)(struct rpc_task *, void *), void *data)
537961a828dSTrond Myklebust {
538961a828dSTrond Myklebust 	struct rpc_task	*task = NULL;
539961a828dSTrond Myklebust 
540961a828dSTrond Myklebust 	dprintk("RPC:       wake_up_first(%p \"%s\")\n",
541961a828dSTrond Myklebust 			queue, rpc_qname(queue));
542961a828dSTrond Myklebust 	spin_lock_bh(&queue->lock);
543961a828dSTrond Myklebust 	task = __rpc_find_next_queued(queue);
544961a828dSTrond Myklebust 	if (task != NULL) {
545961a828dSTrond Myklebust 		if (func(task, data))
546961a828dSTrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
547961a828dSTrond Myklebust 		else
548961a828dSTrond Myklebust 			task = NULL;
549961a828dSTrond Myklebust 	}
550961a828dSTrond Myklebust 	spin_unlock_bh(&queue->lock);
551961a828dSTrond Myklebust 
552961a828dSTrond Myklebust 	return task;
553961a828dSTrond Myklebust }
554961a828dSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_first);
555961a828dSTrond Myklebust 
556961a828dSTrond Myklebust static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
557961a828dSTrond Myklebust {
558961a828dSTrond Myklebust 	return true;
559961a828dSTrond Myklebust }
560961a828dSTrond Myklebust 
5611da177e4SLinus Torvalds /*
5621da177e4SLinus Torvalds  * Wake up the next task on the wait queue.
5631da177e4SLinus Torvalds */
5641da177e4SLinus Torvalds struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
5651da177e4SLinus Torvalds {
566961a828dSTrond Myklebust 	return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
5671da177e4SLinus Torvalds }
568e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /**
5711da177e4SLinus Torvalds  * rpc_wake_up - wake up all rpc_tasks
5721da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5731da177e4SLinus Torvalds  *
5741da177e4SLinus Torvalds  * Grabs queue->lock
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	struct list_head *head;
579e6d83d55STrond Myklebust 
5805e4424afSTrond Myklebust 	spin_lock_bh(&queue->lock);
5811da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5821da177e4SLinus Torvalds 	for (;;) {
583540a0f75STrond Myklebust 		while (!list_empty(head)) {
584540a0f75STrond Myklebust 			struct rpc_task *task;
585540a0f75STrond Myklebust 			task = list_first_entry(head,
586540a0f75STrond Myklebust 					struct rpc_task,
587540a0f75STrond Myklebust 					u.tk_wait.list);
58896ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
589540a0f75STrond Myklebust 		}
5901da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5911da177e4SLinus Torvalds 			break;
5921da177e4SLinus Torvalds 		head--;
5931da177e4SLinus Torvalds 	}
5945e4424afSTrond Myklebust 	spin_unlock_bh(&queue->lock);
5951da177e4SLinus Torvalds }
596e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /**
5991da177e4SLinus Torvalds  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
6001da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
6011da177e4SLinus Torvalds  * @status: status value to set
6021da177e4SLinus Torvalds  *
6031da177e4SLinus Torvalds  * Grabs queue->lock
6041da177e4SLinus Torvalds  */
6051da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	struct list_head *head;
6081da177e4SLinus Torvalds 
6095e4424afSTrond Myklebust 	spin_lock_bh(&queue->lock);
6101da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
6111da177e4SLinus Torvalds 	for (;;) {
612540a0f75STrond Myklebust 		while (!list_empty(head)) {
613540a0f75STrond Myklebust 			struct rpc_task *task;
614540a0f75STrond Myklebust 			task = list_first_entry(head,
615540a0f75STrond Myklebust 					struct rpc_task,
616540a0f75STrond Myklebust 					u.tk_wait.list);
6171da177e4SLinus Torvalds 			task->tk_status = status;
61896ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
6191da177e4SLinus Torvalds 		}
6201da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
6211da177e4SLinus Torvalds 			break;
6221da177e4SLinus Torvalds 		head--;
6231da177e4SLinus Torvalds 	}
6245e4424afSTrond Myklebust 	spin_unlock_bh(&queue->lock);
6251da177e4SLinus Torvalds }
626e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status);
6271da177e4SLinus Torvalds 
62836df9aaeSTrond Myklebust static void __rpc_queue_timer_fn(unsigned long ptr)
62936df9aaeSTrond Myklebust {
63036df9aaeSTrond Myklebust 	struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
63136df9aaeSTrond Myklebust 	struct rpc_task *task, *n;
63236df9aaeSTrond Myklebust 	unsigned long expires, now, timeo;
63336df9aaeSTrond Myklebust 
63436df9aaeSTrond Myklebust 	spin_lock(&queue->lock);
63536df9aaeSTrond Myklebust 	expires = now = jiffies;
63636df9aaeSTrond Myklebust 	list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
63736df9aaeSTrond Myklebust 		timeo = task->u.tk_wait.expires;
63836df9aaeSTrond Myklebust 		if (time_after_eq(now, timeo)) {
63936df9aaeSTrond Myklebust 			dprintk("RPC: %5u timeout\n", task->tk_pid);
64036df9aaeSTrond Myklebust 			task->tk_status = -ETIMEDOUT;
64136df9aaeSTrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
64236df9aaeSTrond Myklebust 			continue;
64336df9aaeSTrond Myklebust 		}
64436df9aaeSTrond Myklebust 		if (expires == now || time_after(expires, timeo))
64536df9aaeSTrond Myklebust 			expires = timeo;
64636df9aaeSTrond Myklebust 	}
64736df9aaeSTrond Myklebust 	if (!list_empty(&queue->timer_list.list))
64836df9aaeSTrond Myklebust 		rpc_set_queue_timer(queue, expires);
64936df9aaeSTrond Myklebust 	spin_unlock(&queue->lock);
65036df9aaeSTrond Myklebust }
65136df9aaeSTrond Myklebust 
6528014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task)
6538014793bSTrond Myklebust {
6545d00837bSTrond Myklebust 	task->tk_status = 0;
6558014793bSTrond Myklebust }
6568014793bSTrond Myklebust 
6571da177e4SLinus Torvalds /*
6581da177e4SLinus Torvalds  * Run a task at a later time
6591da177e4SLinus Torvalds  */
6608014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	task->tk_timeout = delay;
6635d00837bSTrond Myklebust 	rpc_sleep_on(&delay_queue, task, __rpc_atrun);
6641da177e4SLinus Torvalds }
665e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds /*
6684ce70adaSTrond Myklebust  * Helper to call task->tk_ops->rpc_call_prepare
6694ce70adaSTrond Myklebust  */
670aae2006eSAndy Adamson void rpc_prepare_task(struct rpc_task *task)
6714ce70adaSTrond Myklebust {
6724ce70adaSTrond Myklebust 	task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
6734ce70adaSTrond Myklebust }
6744ce70adaSTrond Myklebust 
6757fdcf13bSTrond Myklebust static void
6767fdcf13bSTrond Myklebust rpc_init_task_statistics(struct rpc_task *task)
6777fdcf13bSTrond Myklebust {
6787fdcf13bSTrond Myklebust 	/* Initialize retry counters */
6797fdcf13bSTrond Myklebust 	task->tk_garb_retry = 2;
6807fdcf13bSTrond Myklebust 	task->tk_cred_retry = 2;
6817fdcf13bSTrond Myklebust 	task->tk_rebind_retry = 2;
6827fdcf13bSTrond Myklebust 
6837fdcf13bSTrond Myklebust 	/* starting timestamp */
6847fdcf13bSTrond Myklebust 	task->tk_start = ktime_get();
6857fdcf13bSTrond Myklebust }
6867fdcf13bSTrond Myklebust 
6877fdcf13bSTrond Myklebust static void
6887fdcf13bSTrond Myklebust rpc_reset_task_statistics(struct rpc_task *task)
6897fdcf13bSTrond Myklebust {
6907fdcf13bSTrond Myklebust 	task->tk_timeouts = 0;
6917fdcf13bSTrond Myklebust 	task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT);
6927fdcf13bSTrond Myklebust 
6937fdcf13bSTrond Myklebust 	rpc_init_task_statistics(task);
6947fdcf13bSTrond Myklebust }
6957fdcf13bSTrond Myklebust 
6964ce70adaSTrond Myklebust /*
697963d8fe5STrond Myklebust  * Helper that calls task->tk_ops->rpc_call_done if it exists
698d05fdb0cSTrond Myklebust  */
699abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task)
700d05fdb0cSTrond Myklebust {
701abbcf28fSTrond Myklebust 	task->tk_action = NULL;
702963d8fe5STrond Myklebust 	if (task->tk_ops->rpc_call_done != NULL) {
703963d8fe5STrond Myklebust 		task->tk_ops->rpc_call_done(task, task->tk_calldata);
704d05fdb0cSTrond Myklebust 		if (task->tk_action != NULL) {
705abbcf28fSTrond Myklebust 			WARN_ON(RPC_ASSASSINATED(task));
706abbcf28fSTrond Myklebust 			/* Always release the RPC slot and buffer memory */
707d05fdb0cSTrond Myklebust 			xprt_release(task);
7087fdcf13bSTrond Myklebust 			rpc_reset_task_statistics(task);
709d05fdb0cSTrond Myklebust 		}
710d05fdb0cSTrond Myklebust 	}
711d05fdb0cSTrond Myklebust }
712d9b6cd94STrond Myklebust 
713d9b6cd94STrond Myklebust void rpc_exit(struct rpc_task *task, int status)
714d9b6cd94STrond Myklebust {
715d9b6cd94STrond Myklebust 	task->tk_status = status;
716d9b6cd94STrond Myklebust 	task->tk_action = rpc_exit_task;
717d9b6cd94STrond Myklebust 	if (RPC_IS_QUEUED(task))
718d9b6cd94STrond Myklebust 		rpc_wake_up_queued_task(task->tk_waitqueue, task);
719d9b6cd94STrond Myklebust }
720d9b6cd94STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit);
721d05fdb0cSTrond Myklebust 
722bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
723bbd5a1f9STrond Myklebust {
724a86dc496STrond Myklebust 	if (ops->rpc_release != NULL)
725bbd5a1f9STrond Myklebust 		ops->rpc_release(calldata);
726bbd5a1f9STrond Myklebust }
727bbd5a1f9STrond Myklebust 
728d05fdb0cSTrond Myklebust /*
7291da177e4SLinus Torvalds  * This is the RPC `scheduler' (or rather, the finite state machine).
7301da177e4SLinus Torvalds  */
7312efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task)
7321da177e4SLinus Torvalds {
733eb9b55abSTrond Myklebust 	struct rpc_wait_queue *queue;
734eb9b55abSTrond Myklebust 	int task_is_async = RPC_IS_ASYNC(task);
7351da177e4SLinus Torvalds 	int status = 0;
7361da177e4SLinus Torvalds 
73746121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
7381da177e4SLinus Torvalds 			task->tk_pid, task->tk_flags);
7391da177e4SLinus Torvalds 
7402bd4eef8SWeston Andros Adamson 	WARN_ON_ONCE(RPC_IS_QUEUED(task));
7412bd4eef8SWeston Andros Adamson 	if (RPC_IS_QUEUED(task))
7422bd4eef8SWeston Andros Adamson 		return;
7431da177e4SLinus Torvalds 
744d05fdb0cSTrond Myklebust 	for (;;) {
745b55c5989STrond Myklebust 		void (*do_action)(struct rpc_task *);
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 		/*
748b55c5989STrond Myklebust 		 * Execute any pending callback first.
7491da177e4SLinus Torvalds 		 */
750b55c5989STrond Myklebust 		do_action = task->tk_callback;
7511da177e4SLinus Torvalds 		task->tk_callback = NULL;
752b55c5989STrond Myklebust 		if (do_action == NULL) {
7531da177e4SLinus Torvalds 			/*
7541da177e4SLinus Torvalds 			 * Perform the next FSM step.
755b55c5989STrond Myklebust 			 * tk_action may be NULL if the task has been killed.
756b55c5989STrond Myklebust 			 * In particular, note that rpc_killall_tasks may
757b55c5989STrond Myklebust 			 * do this at any time, so beware when dereferencing.
7581da177e4SLinus Torvalds 			 */
759b55c5989STrond Myklebust 			do_action = task->tk_action;
760b55c5989STrond Myklebust 			if (do_action == NULL)
761abbcf28fSTrond Myklebust 				break;
7621da177e4SLinus Torvalds 		}
76382b0a4c3STrond Myklebust 		trace_rpc_task_run_action(task->tk_client, task, task->tk_action);
764b55c5989STrond Myklebust 		do_action(task);
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 		/*
7671da177e4SLinus Torvalds 		 * Lockless check for whether task is sleeping or not.
7681da177e4SLinus Torvalds 		 */
7691da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task))
7701da177e4SLinus Torvalds 			continue;
771eb9b55abSTrond Myklebust 		/*
772eb9b55abSTrond Myklebust 		 * The queue->lock protects against races with
773eb9b55abSTrond Myklebust 		 * rpc_make_runnable().
774eb9b55abSTrond Myklebust 		 *
775eb9b55abSTrond Myklebust 		 * Note that once we clear RPC_TASK_RUNNING on an asynchronous
776eb9b55abSTrond Myklebust 		 * rpc_task, rpc_make_runnable() can assign it to a
777eb9b55abSTrond Myklebust 		 * different workqueue. We therefore cannot assume that the
778eb9b55abSTrond Myklebust 		 * rpc_task pointer may still be dereferenced.
779eb9b55abSTrond Myklebust 		 */
780eb9b55abSTrond Myklebust 		queue = task->tk_waitqueue;
781eb9b55abSTrond Myklebust 		spin_lock_bh(&queue->lock);
782eb9b55abSTrond Myklebust 		if (!RPC_IS_QUEUED(task)) {
783eb9b55abSTrond Myklebust 			spin_unlock_bh(&queue->lock);
7841da177e4SLinus Torvalds 			continue;
7851da177e4SLinus Torvalds 		}
786eb9b55abSTrond Myklebust 		rpc_clear_running(task);
787eb9b55abSTrond Myklebust 		spin_unlock_bh(&queue->lock);
788eb9b55abSTrond Myklebust 		if (task_is_async)
789eb9b55abSTrond Myklebust 			return;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		/* sync task: sleep here */
79246121cf7SChuck Lever 		dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
79396651ab3STrond Myklebust 		status = out_of_line_wait_on_bit(&task->tk_runstate,
794150030b7SMatthew Wilcox 				RPC_TASK_QUEUED, rpc_wait_bit_killable,
795150030b7SMatthew Wilcox 				TASK_KILLABLE);
79696651ab3STrond Myklebust 		if (status == -ERESTARTSYS) {
7971da177e4SLinus Torvalds 			/*
7981da177e4SLinus Torvalds 			 * When a sync task receives a signal, it exits with
7991da177e4SLinus Torvalds 			 * -ERESTARTSYS. In order to catch any callbacks that
8001da177e4SLinus Torvalds 			 * clean up after sleeping on some queue, we don't
8011da177e4SLinus Torvalds 			 * break the loop here, but go around once more.
8021da177e4SLinus Torvalds 			 */
80346121cf7SChuck Lever 			dprintk("RPC: %5u got signal\n", task->tk_pid);
8041da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_KILLED;
8051da177e4SLinus Torvalds 			rpc_exit(task, -ERESTARTSYS);
8061da177e4SLinus Torvalds 		}
8071da177e4SLinus Torvalds 		rpc_set_running(task);
80846121cf7SChuck Lever 		dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds 
81146121cf7SChuck Lever 	dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
81246121cf7SChuck Lever 			task->tk_status);
8131da177e4SLinus Torvalds 	/* Release all resources associated with the task */
8141da177e4SLinus Torvalds 	rpc_release_task(task);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /*
8181da177e4SLinus Torvalds  * User-visible entry point to the scheduler.
8191da177e4SLinus Torvalds  *
8201da177e4SLinus Torvalds  * This may be called recursively if e.g. an async NFS task updates
8211da177e4SLinus Torvalds  * the attributes and finds that dirty pages must be flushed.
8221da177e4SLinus Torvalds  * NOTE: Upon exit of this function the task is guaranteed to be
8231da177e4SLinus Torvalds  *	 released. In particular note that tk_release() will have
8241da177e4SLinus Torvalds  *	 been called, so your task memory may have been freed.
8251da177e4SLinus Torvalds  */
8262efef837STrond Myklebust void rpc_execute(struct rpc_task *task)
8271da177e4SLinus Torvalds {
828*a76580fbSTrond Myklebust 	bool is_async = RPC_IS_ASYNC(task);
829*a76580fbSTrond Myklebust 
83044c28873STrond Myklebust 	rpc_set_active(task);
831d6a1ed08STrond Myklebust 	rpc_make_runnable(task);
832*a76580fbSTrond Myklebust 	if (!is_async)
8332efef837STrond Myklebust 		__rpc_execute(task);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
83665f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work)
8371da177e4SLinus Torvalds {
8385cf02d09SJeff Layton 	current->flags |= PF_FSTRANS;
83965f27f38SDavid Howells 	__rpc_execute(container_of(work, struct rpc_task, u.tk_work));
8405cf02d09SJeff Layton 	current->flags &= ~PF_FSTRANS;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
84302107148SChuck Lever /**
84402107148SChuck Lever  * rpc_malloc - allocate an RPC buffer
84502107148SChuck Lever  * @task: RPC task that will use this buffer
84602107148SChuck Lever  * @size: requested byte size
8471da177e4SLinus Torvalds  *
848c5a4dd8bSChuck Lever  * To prevent rpciod from hanging, this allocator never sleeps,
849c5a4dd8bSChuck Lever  * returning NULL if the request cannot be serviced immediately.
850c5a4dd8bSChuck Lever  * The caller can arrange to sleep in a way that is safe for rpciod.
851c5a4dd8bSChuck Lever  *
852c5a4dd8bSChuck Lever  * Most requests are 'small' (under 2KiB) and can be serviced from a
853c5a4dd8bSChuck Lever  * mempool, ensuring that NFS reads and writes can always proceed,
854c5a4dd8bSChuck Lever  * and that there is good locality of reference for these buffers.
855c5a4dd8bSChuck Lever  *
8561da177e4SLinus Torvalds  * In order to avoid memory starvation triggering more writebacks of
857c5a4dd8bSChuck Lever  * NFS requests, we avoid using GFP_KERNEL.
8581da177e4SLinus Torvalds  */
85902107148SChuck Lever void *rpc_malloc(struct rpc_task *task, size_t size)
8601da177e4SLinus Torvalds {
861aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
862a564b8f0SMel Gorman 	gfp_t gfp = GFP_NOWAIT;
863a564b8f0SMel Gorman 
864a564b8f0SMel Gorman 	if (RPC_IS_SWAPPER(task))
865a564b8f0SMel Gorman 		gfp |= __GFP_MEMALLOC;
8661da177e4SLinus Torvalds 
867aa3d1faeSChuck Lever 	size += sizeof(struct rpc_buffer);
868c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
869c5a4dd8bSChuck Lever 		buf = mempool_alloc(rpc_buffer_mempool, gfp);
8701da177e4SLinus Torvalds 	else
871c5a4dd8bSChuck Lever 		buf = kmalloc(size, gfp);
872ddce40dfSPeter Zijlstra 
873ddce40dfSPeter Zijlstra 	if (!buf)
874ddce40dfSPeter Zijlstra 		return NULL;
875ddce40dfSPeter Zijlstra 
876aa3d1faeSChuck Lever 	buf->len = size;
877215d0678SGeert Uytterhoeven 	dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
878c5a4dd8bSChuck Lever 			task->tk_pid, size, buf);
879aa3d1faeSChuck Lever 	return &buf->data;
8801da177e4SLinus Torvalds }
88112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc);
8821da177e4SLinus Torvalds 
88302107148SChuck Lever /**
88402107148SChuck Lever  * rpc_free - free buffer allocated via rpc_malloc
885c5a4dd8bSChuck Lever  * @buffer: buffer to free
88602107148SChuck Lever  *
88702107148SChuck Lever  */
888c5a4dd8bSChuck Lever void rpc_free(void *buffer)
8891da177e4SLinus Torvalds {
890aa3d1faeSChuck Lever 	size_t size;
891aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
89202107148SChuck Lever 
893c5a4dd8bSChuck Lever 	if (!buffer)
894c5a4dd8bSChuck Lever 		return;
895aa3d1faeSChuck Lever 
896aa3d1faeSChuck Lever 	buf = container_of(buffer, struct rpc_buffer, data);
897aa3d1faeSChuck Lever 	size = buf->len;
898c5a4dd8bSChuck Lever 
899215d0678SGeert Uytterhoeven 	dprintk("RPC:       freeing buffer of size %zu at %p\n",
900c5a4dd8bSChuck Lever 			size, buf);
901aa3d1faeSChuck Lever 
902c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
903c5a4dd8bSChuck Lever 		mempool_free(buf, rpc_buffer_mempool);
9041da177e4SLinus Torvalds 	else
905c5a4dd8bSChuck Lever 		kfree(buf);
9061da177e4SLinus Torvalds }
90712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free);
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds /*
9101da177e4SLinus Torvalds  * Creation and deletion of RPC task structures
9111da177e4SLinus Torvalds  */
91247fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	memset(task, 0, sizeof(*task));
91544c28873STrond Myklebust 	atomic_set(&task->tk_count, 1);
91684115e1cSTrond Myklebust 	task->tk_flags  = task_setup_data->flags;
91784115e1cSTrond Myklebust 	task->tk_ops = task_setup_data->callback_ops;
91884115e1cSTrond Myklebust 	task->tk_calldata = task_setup_data->callback_data;
9196529eba0STrond Myklebust 	INIT_LIST_HEAD(&task->tk_task);
9201da177e4SLinus Torvalds 
9213ff7576dSTrond Myklebust 	task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
9223ff7576dSTrond Myklebust 	task->tk_owner = current->tgid;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	/* Initialize workqueue for async tasks */
92532bfb5c0STrond Myklebust 	task->tk_workqueue = task_setup_data->workqueue;
9261da177e4SLinus Torvalds 
92784115e1cSTrond Myklebust 	if (task->tk_ops->rpc_call_prepare != NULL)
92884115e1cSTrond Myklebust 		task->tk_action = rpc_prepare_task;
929963d8fe5STrond Myklebust 
9307fdcf13bSTrond Myklebust 	rpc_init_task_statistics(task);
931ef759a2eSChuck Lever 
93246121cf7SChuck Lever 	dprintk("RPC:       new task initialized, procpid %u\n",
933ba25f9dcSPavel Emelyanov 				task_pid_nr(current));
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds static struct rpc_task *
9371da177e4SLinus Torvalds rpc_alloc_task(void)
9381da177e4SLinus Torvalds {
939a564b8f0SMel Gorman 	return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO);
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds /*
94390c5755fSTrond Myklebust  * Create a new task for the specified client.
9441da177e4SLinus Torvalds  */
94584115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
9461da177e4SLinus Torvalds {
947e8f5d77cSTrond Myklebust 	struct rpc_task	*task = setup_data->task;
948e8f5d77cSTrond Myklebust 	unsigned short flags = 0;
9491da177e4SLinus Torvalds 
950e8f5d77cSTrond Myklebust 	if (task == NULL) {
9511da177e4SLinus Torvalds 		task = rpc_alloc_task();
95219445b99STrond Myklebust 		if (task == NULL) {
95319445b99STrond Myklebust 			rpc_release_calldata(setup_data->callback_ops,
95419445b99STrond Myklebust 					setup_data->callback_data);
95519445b99STrond Myklebust 			return ERR_PTR(-ENOMEM);
95619445b99STrond Myklebust 		}
957e8f5d77cSTrond Myklebust 		flags = RPC_TASK_DYNAMIC;
958e8f5d77cSTrond Myklebust 	}
9591da177e4SLinus Torvalds 
96084115e1cSTrond Myklebust 	rpc_init_task(task, setup_data);
961e8f5d77cSTrond Myklebust 	task->tk_flags |= flags;
96246121cf7SChuck Lever 	dprintk("RPC:       allocated task %p\n", task);
9631da177e4SLinus Torvalds 	return task;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
966c6567ed1STrond Myklebust /*
967c6567ed1STrond Myklebust  * rpc_free_task - release rpc task and perform cleanups
968c6567ed1STrond Myklebust  *
969c6567ed1STrond Myklebust  * Note that we free up the rpc_task _after_ rpc_release_calldata()
970c6567ed1STrond Myklebust  * in order to work around a workqueue dependency issue.
971c6567ed1STrond Myklebust  *
972c6567ed1STrond Myklebust  * Tejun Heo states:
973c6567ed1STrond Myklebust  * "Workqueue currently considers two work items to be the same if they're
974c6567ed1STrond Myklebust  * on the same address and won't execute them concurrently - ie. it
975c6567ed1STrond Myklebust  * makes a work item which is queued again while being executed wait
976c6567ed1STrond Myklebust  * for the previous execution to complete.
977c6567ed1STrond Myklebust  *
978c6567ed1STrond Myklebust  * If a work function frees the work item, and then waits for an event
979c6567ed1STrond Myklebust  * which should be performed by another work item and *that* work item
980c6567ed1STrond Myklebust  * recycles the freed work item, it can create a false dependency loop.
981c6567ed1STrond Myklebust  * There really is no reliable way to detect this short of verifying
982c6567ed1STrond Myklebust  * every memory free."
983c6567ed1STrond Myklebust  *
984c6567ed1STrond Myklebust  */
98532bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task)
9861da177e4SLinus Torvalds {
987c6567ed1STrond Myklebust 	unsigned short tk_flags = task->tk_flags;
9881da177e4SLinus Torvalds 
989c6567ed1STrond Myklebust 	rpc_release_calldata(task->tk_ops, task->tk_calldata);
990c6567ed1STrond Myklebust 
991c6567ed1STrond Myklebust 	if (tk_flags & RPC_TASK_DYNAMIC) {
9925e4424afSTrond Myklebust 		dprintk("RPC: %5u freeing task\n", task->tk_pid);
9935e4424afSTrond Myklebust 		mempool_free(task, rpc_task_mempool);
9945e4424afSTrond Myklebust 	}
99532bfb5c0STrond Myklebust }
99632bfb5c0STrond Myklebust 
99732bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work)
99832bfb5c0STrond Myklebust {
99932bfb5c0STrond Myklebust 	rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
100032bfb5c0STrond Myklebust }
100132bfb5c0STrond Myklebust 
1002bf294b41STrond Myklebust static void rpc_release_resources_task(struct rpc_task *task)
100332bfb5c0STrond Myklebust {
1004e6b3c4dbSTrond Myklebust 	xprt_release(task);
1005a271c5a0SOGAWA Hirofumi 	if (task->tk_msg.rpc_cred) {
1006a17c2153STrond Myklebust 		put_rpccred(task->tk_msg.rpc_cred);
1007a271c5a0SOGAWA Hirofumi 		task->tk_msg.rpc_cred = NULL;
1008a271c5a0SOGAWA Hirofumi 	}
100958f9612cSTrond Myklebust 	rpc_task_release_client(task);
1010bf294b41STrond Myklebust }
1011bf294b41STrond Myklebust 
1012bf294b41STrond Myklebust static void rpc_final_put_task(struct rpc_task *task,
1013bf294b41STrond Myklebust 		struct workqueue_struct *q)
1014bf294b41STrond Myklebust {
1015bf294b41STrond Myklebust 	if (q != NULL) {
101632bfb5c0STrond Myklebust 		INIT_WORK(&task->u.tk_work, rpc_async_release);
1017bf294b41STrond Myklebust 		queue_work(q, &task->u.tk_work);
101832bfb5c0STrond Myklebust 	} else
101932bfb5c0STrond Myklebust 		rpc_free_task(task);
1020e6b3c4dbSTrond Myklebust }
1021bf294b41STrond Myklebust 
1022bf294b41STrond Myklebust static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
1023bf294b41STrond Myklebust {
1024bf294b41STrond Myklebust 	if (atomic_dec_and_test(&task->tk_count)) {
1025bf294b41STrond Myklebust 		rpc_release_resources_task(task);
1026bf294b41STrond Myklebust 		rpc_final_put_task(task, q);
1027bf294b41STrond Myklebust 	}
1028bf294b41STrond Myklebust }
1029bf294b41STrond Myklebust 
1030bf294b41STrond Myklebust void rpc_put_task(struct rpc_task *task)
1031bf294b41STrond Myklebust {
1032bf294b41STrond Myklebust 	rpc_do_put_task(task, NULL);
1033bf294b41STrond Myklebust }
1034e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task);
1035e6b3c4dbSTrond Myklebust 
1036bf294b41STrond Myklebust void rpc_put_task_async(struct rpc_task *task)
1037bf294b41STrond Myklebust {
1038bf294b41STrond Myklebust 	rpc_do_put_task(task, task->tk_workqueue);
1039bf294b41STrond Myklebust }
1040bf294b41STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task_async);
1041bf294b41STrond Myklebust 
1042bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task)
1043e6b3c4dbSTrond Myklebust {
104446121cf7SChuck Lever 	dprintk("RPC: %5u release task\n", task->tk_pid);
10451da177e4SLinus Torvalds 
10460a0c2a57SWeston Andros Adamson 	WARN_ON_ONCE(RPC_IS_QUEUED(task));
10471da177e4SLinus Torvalds 
1048bf294b41STrond Myklebust 	rpc_release_resources_task(task);
1049e6b3c4dbSTrond Myklebust 
1050bf294b41STrond Myklebust 	/*
1051bf294b41STrond Myklebust 	 * Note: at this point we have been removed from rpc_clnt->cl_tasks,
1052bf294b41STrond Myklebust 	 * so it should be safe to use task->tk_count as a test for whether
1053bf294b41STrond Myklebust 	 * or not any other processes still hold references to our rpc_task.
1054bf294b41STrond Myklebust 	 */
1055bf294b41STrond Myklebust 	if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
1056bf294b41STrond Myklebust 		/* Wake up anyone who may be waiting for task completion */
1057bf294b41STrond Myklebust 		if (!rpc_complete_task(task))
1058bf294b41STrond Myklebust 			return;
1059bf294b41STrond Myklebust 	} else {
1060bf294b41STrond Myklebust 		if (!atomic_dec_and_test(&task->tk_count))
1061bf294b41STrond Myklebust 			return;
1062bf294b41STrond Myklebust 	}
1063bf294b41STrond Myklebust 	rpc_final_put_task(task, task->tk_workqueue);
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
1066b247bbf1STrond Myklebust int rpciod_up(void)
1067b247bbf1STrond Myklebust {
1068b247bbf1STrond Myklebust 	return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1069b247bbf1STrond Myklebust }
1070b247bbf1STrond Myklebust 
1071b247bbf1STrond Myklebust void rpciod_down(void)
1072b247bbf1STrond Myklebust {
1073b247bbf1STrond Myklebust 	module_put(THIS_MODULE);
1074b247bbf1STrond Myklebust }
1075b247bbf1STrond Myklebust 
10761da177e4SLinus Torvalds /*
1077b247bbf1STrond Myklebust  * Start up the rpciod workqueue.
10781da177e4SLinus Torvalds  */
1079b247bbf1STrond Myklebust static int rpciod_start(void)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	struct workqueue_struct *wq;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	/*
10841da177e4SLinus Torvalds 	 * Create the rpciod thread and wait for it to start.
10851da177e4SLinus Torvalds 	 */
1086ab418d70STrond Myklebust 	dprintk("RPC:       creating workqueue rpciod\n");
10879b96ce71STrond Myklebust 	wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM, 1);
10881da177e4SLinus Torvalds 	rpciod_workqueue = wq;
1089b247bbf1STrond Myklebust 	return rpciod_workqueue != NULL;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
1092b247bbf1STrond Myklebust static void rpciod_stop(void)
10931da177e4SLinus Torvalds {
1094b247bbf1STrond Myklebust 	struct workqueue_struct *wq = NULL;
1095ab418d70STrond Myklebust 
1096b247bbf1STrond Myklebust 	if (rpciod_workqueue == NULL)
1097b247bbf1STrond Myklebust 		return;
1098ab418d70STrond Myklebust 	dprintk("RPC:       destroying workqueue rpciod\n");
10991da177e4SLinus Torvalds 
1100b247bbf1STrond Myklebust 	wq = rpciod_workqueue;
11011da177e4SLinus Torvalds 	rpciod_workqueue = NULL;
1102b247bbf1STrond Myklebust 	destroy_workqueue(wq);
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds void
11061da177e4SLinus Torvalds rpc_destroy_mempool(void)
11071da177e4SLinus Torvalds {
1108b247bbf1STrond Myklebust 	rpciod_stop();
11091da177e4SLinus Torvalds 	if (rpc_buffer_mempool)
11101da177e4SLinus Torvalds 		mempool_destroy(rpc_buffer_mempool);
11111da177e4SLinus Torvalds 	if (rpc_task_mempool)
11121da177e4SLinus Torvalds 		mempool_destroy(rpc_task_mempool);
11131a1d92c1SAlexey Dobriyan 	if (rpc_task_slabp)
11141a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_task_slabp);
11151a1d92c1SAlexey Dobriyan 	if (rpc_buffer_slabp)
11161a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_buffer_slabp);
1117f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&delay_queue);
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds int
11211da177e4SLinus Torvalds rpc_init_mempool(void)
11221da177e4SLinus Torvalds {
1123f6a1cc89STrond Myklebust 	/*
1124f6a1cc89STrond Myklebust 	 * The following is not strictly a mempool initialisation,
1125f6a1cc89STrond Myklebust 	 * but there is no harm in doing it here
1126f6a1cc89STrond Myklebust 	 */
1127f6a1cc89STrond Myklebust 	rpc_init_wait_queue(&delay_queue, "delayq");
1128f6a1cc89STrond Myklebust 	if (!rpciod_start())
1129f6a1cc89STrond Myklebust 		goto err_nomem;
1130f6a1cc89STrond Myklebust 
11311da177e4SLinus Torvalds 	rpc_task_slabp = kmem_cache_create("rpc_tasks",
11321da177e4SLinus Torvalds 					     sizeof(struct rpc_task),
11331da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
113420c2df83SPaul Mundt 					     NULL);
11351da177e4SLinus Torvalds 	if (!rpc_task_slabp)
11361da177e4SLinus Torvalds 		goto err_nomem;
11371da177e4SLinus Torvalds 	rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
11381da177e4SLinus Torvalds 					     RPC_BUFFER_MAXSIZE,
11391da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
114020c2df83SPaul Mundt 					     NULL);
11411da177e4SLinus Torvalds 	if (!rpc_buffer_slabp)
11421da177e4SLinus Torvalds 		goto err_nomem;
114393d2341cSMatthew Dobson 	rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
11441da177e4SLinus Torvalds 						    rpc_task_slabp);
11451da177e4SLinus Torvalds 	if (!rpc_task_mempool)
11461da177e4SLinus Torvalds 		goto err_nomem;
114793d2341cSMatthew Dobson 	rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
11481da177e4SLinus Torvalds 						      rpc_buffer_slabp);
11491da177e4SLinus Torvalds 	if (!rpc_buffer_mempool)
11501da177e4SLinus Torvalds 		goto err_nomem;
11511da177e4SLinus Torvalds 	return 0;
11521da177e4SLinus Torvalds err_nomem:
11531da177e4SLinus Torvalds 	rpc_destroy_mempool();
11541da177e4SLinus Torvalds 	return -ENOMEM;
11551da177e4SLinus Torvalds }
1156