xref: /linux/net/sunrpc/sched.c (revision 416ad3c9c0066405b83ec875b75496523549be09)
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;
257*416ad3c9SColin Cross 	freezable_schedule_unsafe();
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.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	rpc_clear_queued(task);
331cc4dc59eSChristophe Saout 	if (rpc_test_and_set_running(task))
3321da177e4SLinus Torvalds 		return;
3331da177e4SLinus Torvalds 	if (RPC_IS_ASYNC(task)) {
33465f27f38SDavid Howells 		INIT_WORK(&task->u.tk_work, rpc_async_schedule);
335cee6a537SFred Isaman 		queue_work(rpciod_workqueue, &task->u.tk_work);
3361da177e4SLinus Torvalds 	} else
33796651ab3STrond Myklebust 		wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * Prepare for sleeping on a wait queue.
3421da177e4SLinus Torvalds  * By always appending tasks to the list we ensure FIFO behavior.
3431da177e4SLinus Torvalds  * NB: An RPC task will only receive interrupt-driven events as long
3441da177e4SLinus Torvalds  * as it's on a wait queue.
3451da177e4SLinus Torvalds  */
3463b27bad7STrond Myklebust static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
3473b27bad7STrond Myklebust 		struct rpc_task *task,
3483b27bad7STrond Myklebust 		rpc_action action,
3493b27bad7STrond Myklebust 		unsigned char queue_priority)
3501da177e4SLinus Torvalds {
35146121cf7SChuck Lever 	dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
35246121cf7SChuck Lever 			task->tk_pid, rpc_qname(q), jiffies);
3531da177e4SLinus Torvalds 
35482b0a4c3STrond Myklebust 	trace_rpc_task_sleep(task->tk_client, task, q);
35582b0a4c3STrond Myklebust 
3563b27bad7STrond Myklebust 	__rpc_add_wait_queue(q, task, queue_priority);
3571da177e4SLinus Torvalds 
358f50ad428SWeston Andros Adamson 	WARN_ON_ONCE(task->tk_callback != NULL);
3591da177e4SLinus Torvalds 	task->tk_callback = action;
360eb276c0eSTrond Myklebust 	__rpc_add_timer(q, task);
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3645d00837bSTrond Myklebust 				rpc_action action)
3651da177e4SLinus Torvalds {
36658f9612cSTrond Myklebust 	/* We shouldn't ever put an inactive task to sleep */
367e454a7a8SWeston Andros Adamson 	WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
368e454a7a8SWeston Andros Adamson 	if (!RPC_IS_ACTIVATED(task)) {
369e454a7a8SWeston Andros Adamson 		task->tk_status = -EIO;
370e454a7a8SWeston Andros Adamson 		rpc_put_task_async(task);
371e454a7a8SWeston Andros Adamson 		return;
372e454a7a8SWeston Andros Adamson 	}
373e6b3c4dbSTrond Myklebust 
3741da177e4SLinus Torvalds 	/*
3751da177e4SLinus Torvalds 	 * Protect the queue operations.
3761da177e4SLinus Torvalds 	 */
3771da177e4SLinus Torvalds 	spin_lock_bh(&q->lock);
3783b27bad7STrond Myklebust 	__rpc_sleep_on_priority(q, task, action, task->tk_priority);
3791da177e4SLinus Torvalds 	spin_unlock_bh(&q->lock);
3801da177e4SLinus Torvalds }
381e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on);
3821da177e4SLinus Torvalds 
3833b27bad7STrond Myklebust void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
3843b27bad7STrond Myklebust 		rpc_action action, int priority)
3853b27bad7STrond Myklebust {
3863b27bad7STrond Myklebust 	/* We shouldn't ever put an inactive task to sleep */
387e454a7a8SWeston Andros Adamson 	WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
388e454a7a8SWeston Andros Adamson 	if (!RPC_IS_ACTIVATED(task)) {
389e454a7a8SWeston Andros Adamson 		task->tk_status = -EIO;
390e454a7a8SWeston Andros Adamson 		rpc_put_task_async(task);
391e454a7a8SWeston Andros Adamson 		return;
392e454a7a8SWeston Andros Adamson 	}
3933b27bad7STrond Myklebust 
3943b27bad7STrond Myklebust 	/*
3953b27bad7STrond Myklebust 	 * Protect the queue operations.
3963b27bad7STrond Myklebust 	 */
3973b27bad7STrond Myklebust 	spin_lock_bh(&q->lock);
3983b27bad7STrond Myklebust 	__rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW);
3993b27bad7STrond Myklebust 	spin_unlock_bh(&q->lock);
4003b27bad7STrond Myklebust }
4011e1093c7STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
4023b27bad7STrond Myklebust 
4031da177e4SLinus Torvalds /**
4041da177e4SLinus Torvalds  * __rpc_do_wake_up_task - wake up a single rpc_task
40596ef13b2STrond Myklebust  * @queue: wait queue
4061da177e4SLinus Torvalds  * @task: task to be woken up
4071da177e4SLinus Torvalds  *
4081da177e4SLinus Torvalds  * Caller must hold queue->lock, and have cleared the task queued flag.
4091da177e4SLinus Torvalds  */
41096ef13b2STrond Myklebust static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
4111da177e4SLinus Torvalds {
41246121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
41346121cf7SChuck Lever 			task->tk_pid, jiffies);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	/* Has the task been executed yet? If not, we cannot wake it up! */
4161da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task)) {
4171da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
4181da177e4SLinus Torvalds 		return;
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
42182b0a4c3STrond Myklebust 	trace_rpc_task_wakeup(task->tk_client, task, queue);
42282b0a4c3STrond Myklebust 
42396ef13b2STrond Myklebust 	__rpc_remove_wait_queue(queue, task);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	rpc_make_runnable(task);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	dprintk("RPC:       __rpc_wake_up_task done\n");
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds /*
43196ef13b2STrond Myklebust  * Wake up a queued task while the queue lock is being held
4321da177e4SLinus Torvalds  */
43396ef13b2STrond Myklebust static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
4341da177e4SLinus Torvalds {
4351166fde6STrond Myklebust 	if (RPC_IS_QUEUED(task)) {
4361166fde6STrond Myklebust 		smp_rmb();
4371166fde6STrond Myklebust 		if (task->tk_waitqueue == queue)
43896ef13b2STrond Myklebust 			__rpc_do_wake_up_task(queue, task);
4391da177e4SLinus Torvalds 	}
4401166fde6STrond Myklebust }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /*
44348f18612SAlexandros Batsakis  * Tests whether rpc queue is empty
44448f18612SAlexandros Batsakis  */
44548f18612SAlexandros Batsakis int rpc_queue_empty(struct rpc_wait_queue *queue)
44648f18612SAlexandros Batsakis {
44748f18612SAlexandros Batsakis 	int res;
44848f18612SAlexandros Batsakis 
44948f18612SAlexandros Batsakis 	spin_lock_bh(&queue->lock);
45048f18612SAlexandros Batsakis 	res = queue->qlen;
45148f18612SAlexandros Batsakis 	spin_unlock_bh(&queue->lock);
452a02cec21SEric Dumazet 	return res == 0;
45348f18612SAlexandros Batsakis }
45448f18612SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_queue_empty);
45548f18612SAlexandros Batsakis 
45648f18612SAlexandros Batsakis /*
45796ef13b2STrond Myklebust  * Wake up a task on a specific queue
45896ef13b2STrond Myklebust  */
45996ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
46096ef13b2STrond Myklebust {
4615e4424afSTrond Myklebust 	spin_lock_bh(&queue->lock);
46296ef13b2STrond Myklebust 	rpc_wake_up_task_queue_locked(queue, task);
4635e4424afSTrond Myklebust 	spin_unlock_bh(&queue->lock);
46496ef13b2STrond Myklebust }
46596ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
46696ef13b2STrond Myklebust 
46796ef13b2STrond Myklebust /*
4681da177e4SLinus Torvalds  * Wake up the next task on a priority queue.
4691da177e4SLinus Torvalds  */
470961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	struct list_head *q;
4731da177e4SLinus Torvalds 	struct rpc_task *task;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	/*
4763ff7576dSTrond Myklebust 	 * Service a batch of tasks from a single owner.
4771da177e4SLinus Torvalds 	 */
4781da177e4SLinus Torvalds 	q = &queue->tasks[queue->priority];
4791da177e4SLinus Torvalds 	if (!list_empty(q)) {
4801da177e4SLinus Torvalds 		task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4813ff7576dSTrond Myklebust 		if (queue->owner == task->tk_owner) {
4821da177e4SLinus Torvalds 			if (--queue->nr)
4831da177e4SLinus Torvalds 				goto out;
4841da177e4SLinus Torvalds 			list_move_tail(&task->u.tk_wait.list, q);
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 		/*
4871da177e4SLinus Torvalds 		 * Check if we need to switch queues.
4881da177e4SLinus Torvalds 		 */
4893ff7576dSTrond Myklebust 		goto new_owner;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/*
4931da177e4SLinus Torvalds 	 * Service the next queue.
4941da177e4SLinus Torvalds 	 */
4951da177e4SLinus Torvalds 	do {
4961da177e4SLinus Torvalds 		if (q == &queue->tasks[0])
4971da177e4SLinus Torvalds 			q = &queue->tasks[queue->maxpriority];
4981da177e4SLinus Torvalds 		else
4991da177e4SLinus Torvalds 			q = q - 1;
5001da177e4SLinus Torvalds 		if (!list_empty(q)) {
5011da177e4SLinus Torvalds 			task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
5021da177e4SLinus Torvalds 			goto new_queue;
5031da177e4SLinus Torvalds 		}
5041da177e4SLinus Torvalds 	} while (q != &queue->tasks[queue->priority]);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
5071da177e4SLinus Torvalds 	return NULL;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds new_queue:
5101da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
5113ff7576dSTrond Myklebust new_owner:
5123ff7576dSTrond Myklebust 	rpc_set_waitqueue_owner(queue, task->tk_owner);
5131da177e4SLinus Torvalds out:
5141da177e4SLinus Torvalds 	return task;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
517961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
518961a828dSTrond Myklebust {
519961a828dSTrond Myklebust 	if (RPC_IS_PRIORITY(queue))
520961a828dSTrond Myklebust 		return __rpc_find_next_queued_priority(queue);
521961a828dSTrond Myklebust 	if (!list_empty(&queue->tasks[0]))
522961a828dSTrond Myklebust 		return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
523961a828dSTrond Myklebust 	return NULL;
524961a828dSTrond Myklebust }
525961a828dSTrond Myklebust 
526961a828dSTrond Myklebust /*
527961a828dSTrond Myklebust  * Wake up the first task on the wait queue.
528961a828dSTrond Myklebust  */
529961a828dSTrond Myklebust struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
530961a828dSTrond Myklebust 		bool (*func)(struct rpc_task *, void *), void *data)
531961a828dSTrond Myklebust {
532961a828dSTrond Myklebust 	struct rpc_task	*task = NULL;
533961a828dSTrond Myklebust 
534961a828dSTrond Myklebust 	dprintk("RPC:       wake_up_first(%p \"%s\")\n",
535961a828dSTrond Myklebust 			queue, rpc_qname(queue));
536961a828dSTrond Myklebust 	spin_lock_bh(&queue->lock);
537961a828dSTrond Myklebust 	task = __rpc_find_next_queued(queue);
538961a828dSTrond Myklebust 	if (task != NULL) {
539961a828dSTrond Myklebust 		if (func(task, data))
540961a828dSTrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
541961a828dSTrond Myklebust 		else
542961a828dSTrond Myklebust 			task = NULL;
543961a828dSTrond Myklebust 	}
544961a828dSTrond Myklebust 	spin_unlock_bh(&queue->lock);
545961a828dSTrond Myklebust 
546961a828dSTrond Myklebust 	return task;
547961a828dSTrond Myklebust }
548961a828dSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_first);
549961a828dSTrond Myklebust 
550961a828dSTrond Myklebust static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
551961a828dSTrond Myklebust {
552961a828dSTrond Myklebust 	return true;
553961a828dSTrond Myklebust }
554961a828dSTrond Myklebust 
5551da177e4SLinus Torvalds /*
5561da177e4SLinus Torvalds  * Wake up the next task on the wait queue.
5571da177e4SLinus Torvalds */
5581da177e4SLinus Torvalds struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
5591da177e4SLinus Torvalds {
560961a828dSTrond Myklebust 	return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
5611da177e4SLinus Torvalds }
562e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds /**
5651da177e4SLinus Torvalds  * rpc_wake_up - wake up all rpc_tasks
5661da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5671da177e4SLinus Torvalds  *
5681da177e4SLinus Torvalds  * Grabs queue->lock
5691da177e4SLinus Torvalds  */
5701da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	struct list_head *head;
573e6d83d55STrond Myklebust 
5745e4424afSTrond Myklebust 	spin_lock_bh(&queue->lock);
5751da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5761da177e4SLinus Torvalds 	for (;;) {
577540a0f75STrond Myklebust 		while (!list_empty(head)) {
578540a0f75STrond Myklebust 			struct rpc_task *task;
579540a0f75STrond Myklebust 			task = list_first_entry(head,
580540a0f75STrond Myklebust 					struct rpc_task,
581540a0f75STrond Myklebust 					u.tk_wait.list);
58296ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
583540a0f75STrond Myklebust 		}
5841da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5851da177e4SLinus Torvalds 			break;
5861da177e4SLinus Torvalds 		head--;
5871da177e4SLinus Torvalds 	}
5885e4424afSTrond Myklebust 	spin_unlock_bh(&queue->lock);
5891da177e4SLinus Torvalds }
590e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds /**
5931da177e4SLinus Torvalds  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
5941da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5951da177e4SLinus Torvalds  * @status: status value to set
5961da177e4SLinus Torvalds  *
5971da177e4SLinus Torvalds  * Grabs queue->lock
5981da177e4SLinus Torvalds  */
5991da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	struct list_head *head;
6021da177e4SLinus Torvalds 
6035e4424afSTrond Myklebust 	spin_lock_bh(&queue->lock);
6041da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
6051da177e4SLinus Torvalds 	for (;;) {
606540a0f75STrond Myklebust 		while (!list_empty(head)) {
607540a0f75STrond Myklebust 			struct rpc_task *task;
608540a0f75STrond Myklebust 			task = list_first_entry(head,
609540a0f75STrond Myklebust 					struct rpc_task,
610540a0f75STrond Myklebust 					u.tk_wait.list);
6111da177e4SLinus Torvalds 			task->tk_status = status;
61296ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
6131da177e4SLinus Torvalds 		}
6141da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
6151da177e4SLinus Torvalds 			break;
6161da177e4SLinus Torvalds 		head--;
6171da177e4SLinus Torvalds 	}
6185e4424afSTrond Myklebust 	spin_unlock_bh(&queue->lock);
6191da177e4SLinus Torvalds }
620e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status);
6211da177e4SLinus Torvalds 
62236df9aaeSTrond Myklebust static void __rpc_queue_timer_fn(unsigned long ptr)
62336df9aaeSTrond Myklebust {
62436df9aaeSTrond Myklebust 	struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
62536df9aaeSTrond Myklebust 	struct rpc_task *task, *n;
62636df9aaeSTrond Myklebust 	unsigned long expires, now, timeo;
62736df9aaeSTrond Myklebust 
62836df9aaeSTrond Myklebust 	spin_lock(&queue->lock);
62936df9aaeSTrond Myklebust 	expires = now = jiffies;
63036df9aaeSTrond Myklebust 	list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
63136df9aaeSTrond Myklebust 		timeo = task->u.tk_wait.expires;
63236df9aaeSTrond Myklebust 		if (time_after_eq(now, timeo)) {
63336df9aaeSTrond Myklebust 			dprintk("RPC: %5u timeout\n", task->tk_pid);
63436df9aaeSTrond Myklebust 			task->tk_status = -ETIMEDOUT;
63536df9aaeSTrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
63636df9aaeSTrond Myklebust 			continue;
63736df9aaeSTrond Myklebust 		}
63836df9aaeSTrond Myklebust 		if (expires == now || time_after(expires, timeo))
63936df9aaeSTrond Myklebust 			expires = timeo;
64036df9aaeSTrond Myklebust 	}
64136df9aaeSTrond Myklebust 	if (!list_empty(&queue->timer_list.list))
64236df9aaeSTrond Myklebust 		rpc_set_queue_timer(queue, expires);
64336df9aaeSTrond Myklebust 	spin_unlock(&queue->lock);
64436df9aaeSTrond Myklebust }
64536df9aaeSTrond Myklebust 
6468014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task)
6478014793bSTrond Myklebust {
6485d00837bSTrond Myklebust 	task->tk_status = 0;
6498014793bSTrond Myklebust }
6508014793bSTrond Myklebust 
6511da177e4SLinus Torvalds /*
6521da177e4SLinus Torvalds  * Run a task at a later time
6531da177e4SLinus Torvalds  */
6548014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	task->tk_timeout = delay;
6575d00837bSTrond Myklebust 	rpc_sleep_on(&delay_queue, task, __rpc_atrun);
6581da177e4SLinus Torvalds }
659e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
6624ce70adaSTrond Myklebust  * Helper to call task->tk_ops->rpc_call_prepare
6634ce70adaSTrond Myklebust  */
664aae2006eSAndy Adamson void rpc_prepare_task(struct rpc_task *task)
6654ce70adaSTrond Myklebust {
6664ce70adaSTrond Myklebust 	task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
6674ce70adaSTrond Myklebust }
6684ce70adaSTrond Myklebust 
6697fdcf13bSTrond Myklebust static void
6707fdcf13bSTrond Myklebust rpc_init_task_statistics(struct rpc_task *task)
6717fdcf13bSTrond Myklebust {
6727fdcf13bSTrond Myklebust 	/* Initialize retry counters */
6737fdcf13bSTrond Myklebust 	task->tk_garb_retry = 2;
6747fdcf13bSTrond Myklebust 	task->tk_cred_retry = 2;
6757fdcf13bSTrond Myklebust 	task->tk_rebind_retry = 2;
6767fdcf13bSTrond Myklebust 
6777fdcf13bSTrond Myklebust 	/* starting timestamp */
6787fdcf13bSTrond Myklebust 	task->tk_start = ktime_get();
6797fdcf13bSTrond Myklebust }
6807fdcf13bSTrond Myklebust 
6817fdcf13bSTrond Myklebust static void
6827fdcf13bSTrond Myklebust rpc_reset_task_statistics(struct rpc_task *task)
6837fdcf13bSTrond Myklebust {
6847fdcf13bSTrond Myklebust 	task->tk_timeouts = 0;
6857fdcf13bSTrond Myklebust 	task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT);
6867fdcf13bSTrond Myklebust 
6877fdcf13bSTrond Myklebust 	rpc_init_task_statistics(task);
6887fdcf13bSTrond Myklebust }
6897fdcf13bSTrond Myklebust 
6904ce70adaSTrond Myklebust /*
691963d8fe5STrond Myklebust  * Helper that calls task->tk_ops->rpc_call_done if it exists
692d05fdb0cSTrond Myklebust  */
693abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task)
694d05fdb0cSTrond Myklebust {
695abbcf28fSTrond Myklebust 	task->tk_action = NULL;
696963d8fe5STrond Myklebust 	if (task->tk_ops->rpc_call_done != NULL) {
697963d8fe5STrond Myklebust 		task->tk_ops->rpc_call_done(task, task->tk_calldata);
698d05fdb0cSTrond Myklebust 		if (task->tk_action != NULL) {
699abbcf28fSTrond Myklebust 			WARN_ON(RPC_ASSASSINATED(task));
700abbcf28fSTrond Myklebust 			/* Always release the RPC slot and buffer memory */
701d05fdb0cSTrond Myklebust 			xprt_release(task);
7027fdcf13bSTrond Myklebust 			rpc_reset_task_statistics(task);
703d05fdb0cSTrond Myklebust 		}
704d05fdb0cSTrond Myklebust 	}
705d05fdb0cSTrond Myklebust }
706d9b6cd94STrond Myklebust 
707d9b6cd94STrond Myklebust void rpc_exit(struct rpc_task *task, int status)
708d9b6cd94STrond Myklebust {
709d9b6cd94STrond Myklebust 	task->tk_status = status;
710d9b6cd94STrond Myklebust 	task->tk_action = rpc_exit_task;
711d9b6cd94STrond Myklebust 	if (RPC_IS_QUEUED(task))
712d9b6cd94STrond Myklebust 		rpc_wake_up_queued_task(task->tk_waitqueue, task);
713d9b6cd94STrond Myklebust }
714d9b6cd94STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit);
715d05fdb0cSTrond Myklebust 
716bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
717bbd5a1f9STrond Myklebust {
718a86dc496STrond Myklebust 	if (ops->rpc_release != NULL)
719bbd5a1f9STrond Myklebust 		ops->rpc_release(calldata);
720bbd5a1f9STrond Myklebust }
721bbd5a1f9STrond Myklebust 
722d05fdb0cSTrond Myklebust /*
7231da177e4SLinus Torvalds  * This is the RPC `scheduler' (or rather, the finite state machine).
7241da177e4SLinus Torvalds  */
7252efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task)
7261da177e4SLinus Torvalds {
727eb9b55abSTrond Myklebust 	struct rpc_wait_queue *queue;
728eb9b55abSTrond Myklebust 	int task_is_async = RPC_IS_ASYNC(task);
7291da177e4SLinus Torvalds 	int status = 0;
7301da177e4SLinus Torvalds 
73146121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
7321da177e4SLinus Torvalds 			task->tk_pid, task->tk_flags);
7331da177e4SLinus Torvalds 
7342bd4eef8SWeston Andros Adamson 	WARN_ON_ONCE(RPC_IS_QUEUED(task));
7352bd4eef8SWeston Andros Adamson 	if (RPC_IS_QUEUED(task))
7362bd4eef8SWeston Andros Adamson 		return;
7371da177e4SLinus Torvalds 
738d05fdb0cSTrond Myklebust 	for (;;) {
739b55c5989STrond Myklebust 		void (*do_action)(struct rpc_task *);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 		/*
742b55c5989STrond Myklebust 		 * Execute any pending callback first.
7431da177e4SLinus Torvalds 		 */
744b55c5989STrond Myklebust 		do_action = task->tk_callback;
7451da177e4SLinus Torvalds 		task->tk_callback = NULL;
746b55c5989STrond Myklebust 		if (do_action == NULL) {
7471da177e4SLinus Torvalds 			/*
7481da177e4SLinus Torvalds 			 * Perform the next FSM step.
749b55c5989STrond Myklebust 			 * tk_action may be NULL if the task has been killed.
750b55c5989STrond Myklebust 			 * In particular, note that rpc_killall_tasks may
751b55c5989STrond Myklebust 			 * do this at any time, so beware when dereferencing.
7521da177e4SLinus Torvalds 			 */
753b55c5989STrond Myklebust 			do_action = task->tk_action;
754b55c5989STrond Myklebust 			if (do_action == NULL)
755abbcf28fSTrond Myklebust 				break;
7561da177e4SLinus Torvalds 		}
75782b0a4c3STrond Myklebust 		trace_rpc_task_run_action(task->tk_client, task, task->tk_action);
758b55c5989STrond Myklebust 		do_action(task);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		/*
7611da177e4SLinus Torvalds 		 * Lockless check for whether task is sleeping or not.
7621da177e4SLinus Torvalds 		 */
7631da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task))
7641da177e4SLinus Torvalds 			continue;
765eb9b55abSTrond Myklebust 		/*
766eb9b55abSTrond Myklebust 		 * The queue->lock protects against races with
767eb9b55abSTrond Myklebust 		 * rpc_make_runnable().
768eb9b55abSTrond Myklebust 		 *
769eb9b55abSTrond Myklebust 		 * Note that once we clear RPC_TASK_RUNNING on an asynchronous
770eb9b55abSTrond Myklebust 		 * rpc_task, rpc_make_runnable() can assign it to a
771eb9b55abSTrond Myklebust 		 * different workqueue. We therefore cannot assume that the
772eb9b55abSTrond Myklebust 		 * rpc_task pointer may still be dereferenced.
773eb9b55abSTrond Myklebust 		 */
774eb9b55abSTrond Myklebust 		queue = task->tk_waitqueue;
775eb9b55abSTrond Myklebust 		spin_lock_bh(&queue->lock);
776eb9b55abSTrond Myklebust 		if (!RPC_IS_QUEUED(task)) {
777eb9b55abSTrond Myklebust 			spin_unlock_bh(&queue->lock);
7781da177e4SLinus Torvalds 			continue;
7791da177e4SLinus Torvalds 		}
780eb9b55abSTrond Myklebust 		rpc_clear_running(task);
781eb9b55abSTrond Myklebust 		spin_unlock_bh(&queue->lock);
782eb9b55abSTrond Myklebust 		if (task_is_async)
783eb9b55abSTrond Myklebust 			return;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 		/* sync task: sleep here */
78646121cf7SChuck Lever 		dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
78796651ab3STrond Myklebust 		status = out_of_line_wait_on_bit(&task->tk_runstate,
788150030b7SMatthew Wilcox 				RPC_TASK_QUEUED, rpc_wait_bit_killable,
789150030b7SMatthew Wilcox 				TASK_KILLABLE);
79096651ab3STrond Myklebust 		if (status == -ERESTARTSYS) {
7911da177e4SLinus Torvalds 			/*
7921da177e4SLinus Torvalds 			 * When a sync task receives a signal, it exits with
7931da177e4SLinus Torvalds 			 * -ERESTARTSYS. In order to catch any callbacks that
7941da177e4SLinus Torvalds 			 * clean up after sleeping on some queue, we don't
7951da177e4SLinus Torvalds 			 * break the loop here, but go around once more.
7961da177e4SLinus Torvalds 			 */
79746121cf7SChuck Lever 			dprintk("RPC: %5u got signal\n", task->tk_pid);
7981da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_KILLED;
7991da177e4SLinus Torvalds 			rpc_exit(task, -ERESTARTSYS);
8001da177e4SLinus Torvalds 		}
8011da177e4SLinus Torvalds 		rpc_set_running(task);
80246121cf7SChuck Lever 		dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
8031da177e4SLinus Torvalds 	}
8041da177e4SLinus Torvalds 
80546121cf7SChuck Lever 	dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
80646121cf7SChuck Lever 			task->tk_status);
8071da177e4SLinus Torvalds 	/* Release all resources associated with the task */
8081da177e4SLinus Torvalds 	rpc_release_task(task);
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds /*
8121da177e4SLinus Torvalds  * User-visible entry point to the scheduler.
8131da177e4SLinus Torvalds  *
8141da177e4SLinus Torvalds  * This may be called recursively if e.g. an async NFS task updates
8151da177e4SLinus Torvalds  * the attributes and finds that dirty pages must be flushed.
8161da177e4SLinus Torvalds  * NOTE: Upon exit of this function the task is guaranteed to be
8171da177e4SLinus Torvalds  *	 released. In particular note that tk_release() will have
8181da177e4SLinus Torvalds  *	 been called, so your task memory may have been freed.
8191da177e4SLinus Torvalds  */
8202efef837STrond Myklebust void rpc_execute(struct rpc_task *task)
8211da177e4SLinus Torvalds {
82244c28873STrond Myklebust 	rpc_set_active(task);
823d6a1ed08STrond Myklebust 	rpc_make_runnable(task);
824d6a1ed08STrond Myklebust 	if (!RPC_IS_ASYNC(task))
8252efef837STrond Myklebust 		__rpc_execute(task);
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
82865f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work)
8291da177e4SLinus Torvalds {
8305cf02d09SJeff Layton 	current->flags |= PF_FSTRANS;
83165f27f38SDavid Howells 	__rpc_execute(container_of(work, struct rpc_task, u.tk_work));
8325cf02d09SJeff Layton 	current->flags &= ~PF_FSTRANS;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
83502107148SChuck Lever /**
83602107148SChuck Lever  * rpc_malloc - allocate an RPC buffer
83702107148SChuck Lever  * @task: RPC task that will use this buffer
83802107148SChuck Lever  * @size: requested byte size
8391da177e4SLinus Torvalds  *
840c5a4dd8bSChuck Lever  * To prevent rpciod from hanging, this allocator never sleeps,
841c5a4dd8bSChuck Lever  * returning NULL if the request cannot be serviced immediately.
842c5a4dd8bSChuck Lever  * The caller can arrange to sleep in a way that is safe for rpciod.
843c5a4dd8bSChuck Lever  *
844c5a4dd8bSChuck Lever  * Most requests are 'small' (under 2KiB) and can be serviced from a
845c5a4dd8bSChuck Lever  * mempool, ensuring that NFS reads and writes can always proceed,
846c5a4dd8bSChuck Lever  * and that there is good locality of reference for these buffers.
847c5a4dd8bSChuck Lever  *
8481da177e4SLinus Torvalds  * In order to avoid memory starvation triggering more writebacks of
849c5a4dd8bSChuck Lever  * NFS requests, we avoid using GFP_KERNEL.
8501da177e4SLinus Torvalds  */
85102107148SChuck Lever void *rpc_malloc(struct rpc_task *task, size_t size)
8521da177e4SLinus Torvalds {
853aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
854a564b8f0SMel Gorman 	gfp_t gfp = GFP_NOWAIT;
855a564b8f0SMel Gorman 
856a564b8f0SMel Gorman 	if (RPC_IS_SWAPPER(task))
857a564b8f0SMel Gorman 		gfp |= __GFP_MEMALLOC;
8581da177e4SLinus Torvalds 
859aa3d1faeSChuck Lever 	size += sizeof(struct rpc_buffer);
860c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
861c5a4dd8bSChuck Lever 		buf = mempool_alloc(rpc_buffer_mempool, gfp);
8621da177e4SLinus Torvalds 	else
863c5a4dd8bSChuck Lever 		buf = kmalloc(size, gfp);
864ddce40dfSPeter Zijlstra 
865ddce40dfSPeter Zijlstra 	if (!buf)
866ddce40dfSPeter Zijlstra 		return NULL;
867ddce40dfSPeter Zijlstra 
868aa3d1faeSChuck Lever 	buf->len = size;
869215d0678SGeert Uytterhoeven 	dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
870c5a4dd8bSChuck Lever 			task->tk_pid, size, buf);
871aa3d1faeSChuck Lever 	return &buf->data;
8721da177e4SLinus Torvalds }
87312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc);
8741da177e4SLinus Torvalds 
87502107148SChuck Lever /**
87602107148SChuck Lever  * rpc_free - free buffer allocated via rpc_malloc
877c5a4dd8bSChuck Lever  * @buffer: buffer to free
87802107148SChuck Lever  *
87902107148SChuck Lever  */
880c5a4dd8bSChuck Lever void rpc_free(void *buffer)
8811da177e4SLinus Torvalds {
882aa3d1faeSChuck Lever 	size_t size;
883aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
88402107148SChuck Lever 
885c5a4dd8bSChuck Lever 	if (!buffer)
886c5a4dd8bSChuck Lever 		return;
887aa3d1faeSChuck Lever 
888aa3d1faeSChuck Lever 	buf = container_of(buffer, struct rpc_buffer, data);
889aa3d1faeSChuck Lever 	size = buf->len;
890c5a4dd8bSChuck Lever 
891215d0678SGeert Uytterhoeven 	dprintk("RPC:       freeing buffer of size %zu at %p\n",
892c5a4dd8bSChuck Lever 			size, buf);
893aa3d1faeSChuck Lever 
894c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
895c5a4dd8bSChuck Lever 		mempool_free(buf, rpc_buffer_mempool);
8961da177e4SLinus Torvalds 	else
897c5a4dd8bSChuck Lever 		kfree(buf);
8981da177e4SLinus Torvalds }
89912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds /*
9021da177e4SLinus Torvalds  * Creation and deletion of RPC task structures
9031da177e4SLinus Torvalds  */
90447fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	memset(task, 0, sizeof(*task));
90744c28873STrond Myklebust 	atomic_set(&task->tk_count, 1);
90884115e1cSTrond Myklebust 	task->tk_flags  = task_setup_data->flags;
90984115e1cSTrond Myklebust 	task->tk_ops = task_setup_data->callback_ops;
91084115e1cSTrond Myklebust 	task->tk_calldata = task_setup_data->callback_data;
9116529eba0STrond Myklebust 	INIT_LIST_HEAD(&task->tk_task);
9121da177e4SLinus Torvalds 
9133ff7576dSTrond Myklebust 	task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
9143ff7576dSTrond Myklebust 	task->tk_owner = current->tgid;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	/* Initialize workqueue for async tasks */
91732bfb5c0STrond Myklebust 	task->tk_workqueue = task_setup_data->workqueue;
9181da177e4SLinus Torvalds 
91984115e1cSTrond Myklebust 	if (task->tk_ops->rpc_call_prepare != NULL)
92084115e1cSTrond Myklebust 		task->tk_action = rpc_prepare_task;
921963d8fe5STrond Myklebust 
9227fdcf13bSTrond Myklebust 	rpc_init_task_statistics(task);
923ef759a2eSChuck Lever 
92446121cf7SChuck Lever 	dprintk("RPC:       new task initialized, procpid %u\n",
925ba25f9dcSPavel Emelyanov 				task_pid_nr(current));
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds static struct rpc_task *
9291da177e4SLinus Torvalds rpc_alloc_task(void)
9301da177e4SLinus Torvalds {
931a564b8f0SMel Gorman 	return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds /*
93590c5755fSTrond Myklebust  * Create a new task for the specified client.
9361da177e4SLinus Torvalds  */
93784115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
9381da177e4SLinus Torvalds {
939e8f5d77cSTrond Myklebust 	struct rpc_task	*task = setup_data->task;
940e8f5d77cSTrond Myklebust 	unsigned short flags = 0;
9411da177e4SLinus Torvalds 
942e8f5d77cSTrond Myklebust 	if (task == NULL) {
9431da177e4SLinus Torvalds 		task = rpc_alloc_task();
94419445b99STrond Myklebust 		if (task == NULL) {
94519445b99STrond Myklebust 			rpc_release_calldata(setup_data->callback_ops,
94619445b99STrond Myklebust 					setup_data->callback_data);
94719445b99STrond Myklebust 			return ERR_PTR(-ENOMEM);
94819445b99STrond Myklebust 		}
949e8f5d77cSTrond Myklebust 		flags = RPC_TASK_DYNAMIC;
950e8f5d77cSTrond Myklebust 	}
9511da177e4SLinus Torvalds 
95284115e1cSTrond Myklebust 	rpc_init_task(task, setup_data);
953e8f5d77cSTrond Myklebust 	task->tk_flags |= flags;
95446121cf7SChuck Lever 	dprintk("RPC:       allocated task %p\n", task);
9551da177e4SLinus Torvalds 	return task;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958c6567ed1STrond Myklebust /*
959c6567ed1STrond Myklebust  * rpc_free_task - release rpc task and perform cleanups
960c6567ed1STrond Myklebust  *
961c6567ed1STrond Myklebust  * Note that we free up the rpc_task _after_ rpc_release_calldata()
962c6567ed1STrond Myklebust  * in order to work around a workqueue dependency issue.
963c6567ed1STrond Myklebust  *
964c6567ed1STrond Myklebust  * Tejun Heo states:
965c6567ed1STrond Myklebust  * "Workqueue currently considers two work items to be the same if they're
966c6567ed1STrond Myklebust  * on the same address and won't execute them concurrently - ie. it
967c6567ed1STrond Myklebust  * makes a work item which is queued again while being executed wait
968c6567ed1STrond Myklebust  * for the previous execution to complete.
969c6567ed1STrond Myklebust  *
970c6567ed1STrond Myklebust  * If a work function frees the work item, and then waits for an event
971c6567ed1STrond Myklebust  * which should be performed by another work item and *that* work item
972c6567ed1STrond Myklebust  * recycles the freed work item, it can create a false dependency loop.
973c6567ed1STrond Myklebust  * There really is no reliable way to detect this short of verifying
974c6567ed1STrond Myklebust  * every memory free."
975c6567ed1STrond Myklebust  *
976c6567ed1STrond Myklebust  */
97732bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task)
9781da177e4SLinus Torvalds {
979c6567ed1STrond Myklebust 	unsigned short tk_flags = task->tk_flags;
9801da177e4SLinus Torvalds 
981c6567ed1STrond Myklebust 	rpc_release_calldata(task->tk_ops, task->tk_calldata);
982c6567ed1STrond Myklebust 
983c6567ed1STrond Myklebust 	if (tk_flags & RPC_TASK_DYNAMIC) {
9845e4424afSTrond Myklebust 		dprintk("RPC: %5u freeing task\n", task->tk_pid);
9855e4424afSTrond Myklebust 		mempool_free(task, rpc_task_mempool);
9865e4424afSTrond Myklebust 	}
98732bfb5c0STrond Myklebust }
98832bfb5c0STrond Myklebust 
98932bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work)
99032bfb5c0STrond Myklebust {
99132bfb5c0STrond Myklebust 	rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
99232bfb5c0STrond Myklebust }
99332bfb5c0STrond Myklebust 
994bf294b41STrond Myklebust static void rpc_release_resources_task(struct rpc_task *task)
99532bfb5c0STrond Myklebust {
996e6b3c4dbSTrond Myklebust 	xprt_release(task);
997a271c5a0SOGAWA Hirofumi 	if (task->tk_msg.rpc_cred) {
998a17c2153STrond Myklebust 		put_rpccred(task->tk_msg.rpc_cred);
999a271c5a0SOGAWA Hirofumi 		task->tk_msg.rpc_cred = NULL;
1000a271c5a0SOGAWA Hirofumi 	}
100158f9612cSTrond Myklebust 	rpc_task_release_client(task);
1002bf294b41STrond Myklebust }
1003bf294b41STrond Myklebust 
1004bf294b41STrond Myklebust static void rpc_final_put_task(struct rpc_task *task,
1005bf294b41STrond Myklebust 		struct workqueue_struct *q)
1006bf294b41STrond Myklebust {
1007bf294b41STrond Myklebust 	if (q != NULL) {
100832bfb5c0STrond Myklebust 		INIT_WORK(&task->u.tk_work, rpc_async_release);
1009bf294b41STrond Myklebust 		queue_work(q, &task->u.tk_work);
101032bfb5c0STrond Myklebust 	} else
101132bfb5c0STrond Myklebust 		rpc_free_task(task);
1012e6b3c4dbSTrond Myklebust }
1013bf294b41STrond Myklebust 
1014bf294b41STrond Myklebust static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
1015bf294b41STrond Myklebust {
1016bf294b41STrond Myklebust 	if (atomic_dec_and_test(&task->tk_count)) {
1017bf294b41STrond Myklebust 		rpc_release_resources_task(task);
1018bf294b41STrond Myklebust 		rpc_final_put_task(task, q);
1019bf294b41STrond Myklebust 	}
1020bf294b41STrond Myklebust }
1021bf294b41STrond Myklebust 
1022bf294b41STrond Myklebust void rpc_put_task(struct rpc_task *task)
1023bf294b41STrond Myklebust {
1024bf294b41STrond Myklebust 	rpc_do_put_task(task, NULL);
1025bf294b41STrond Myklebust }
1026e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task);
1027e6b3c4dbSTrond Myklebust 
1028bf294b41STrond Myklebust void rpc_put_task_async(struct rpc_task *task)
1029bf294b41STrond Myklebust {
1030bf294b41STrond Myklebust 	rpc_do_put_task(task, task->tk_workqueue);
1031bf294b41STrond Myklebust }
1032bf294b41STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task_async);
1033bf294b41STrond Myklebust 
1034bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task)
1035e6b3c4dbSTrond Myklebust {
103646121cf7SChuck Lever 	dprintk("RPC: %5u release task\n", task->tk_pid);
10371da177e4SLinus Torvalds 
10380a0c2a57SWeston Andros Adamson 	WARN_ON_ONCE(RPC_IS_QUEUED(task));
10391da177e4SLinus Torvalds 
1040bf294b41STrond Myklebust 	rpc_release_resources_task(task);
1041e6b3c4dbSTrond Myklebust 
1042bf294b41STrond Myklebust 	/*
1043bf294b41STrond Myklebust 	 * Note: at this point we have been removed from rpc_clnt->cl_tasks,
1044bf294b41STrond Myklebust 	 * so it should be safe to use task->tk_count as a test for whether
1045bf294b41STrond Myklebust 	 * or not any other processes still hold references to our rpc_task.
1046bf294b41STrond Myklebust 	 */
1047bf294b41STrond Myklebust 	if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
1048bf294b41STrond Myklebust 		/* Wake up anyone who may be waiting for task completion */
1049bf294b41STrond Myklebust 		if (!rpc_complete_task(task))
1050bf294b41STrond Myklebust 			return;
1051bf294b41STrond Myklebust 	} else {
1052bf294b41STrond Myklebust 		if (!atomic_dec_and_test(&task->tk_count))
1053bf294b41STrond Myklebust 			return;
1054bf294b41STrond Myklebust 	}
1055bf294b41STrond Myklebust 	rpc_final_put_task(task, task->tk_workqueue);
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
1058b247bbf1STrond Myklebust int rpciod_up(void)
1059b247bbf1STrond Myklebust {
1060b247bbf1STrond Myklebust 	return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1061b247bbf1STrond Myklebust }
1062b247bbf1STrond Myklebust 
1063b247bbf1STrond Myklebust void rpciod_down(void)
1064b247bbf1STrond Myklebust {
1065b247bbf1STrond Myklebust 	module_put(THIS_MODULE);
1066b247bbf1STrond Myklebust }
1067b247bbf1STrond Myklebust 
10681da177e4SLinus Torvalds /*
1069b247bbf1STrond Myklebust  * Start up the rpciod workqueue.
10701da177e4SLinus Torvalds  */
1071b247bbf1STrond Myklebust static int rpciod_start(void)
10721da177e4SLinus Torvalds {
10731da177e4SLinus Torvalds 	struct workqueue_struct *wq;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	/*
10761da177e4SLinus Torvalds 	 * Create the rpciod thread and wait for it to start.
10771da177e4SLinus Torvalds 	 */
1078ab418d70STrond Myklebust 	dprintk("RPC:       creating workqueue rpciod\n");
10799b96ce71STrond Myklebust 	wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM, 1);
10801da177e4SLinus Torvalds 	rpciod_workqueue = wq;
1081b247bbf1STrond Myklebust 	return rpciod_workqueue != NULL;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
1084b247bbf1STrond Myklebust static void rpciod_stop(void)
10851da177e4SLinus Torvalds {
1086b247bbf1STrond Myklebust 	struct workqueue_struct *wq = NULL;
1087ab418d70STrond Myklebust 
1088b247bbf1STrond Myklebust 	if (rpciod_workqueue == NULL)
1089b247bbf1STrond Myklebust 		return;
1090ab418d70STrond Myklebust 	dprintk("RPC:       destroying workqueue rpciod\n");
10911da177e4SLinus Torvalds 
1092b247bbf1STrond Myklebust 	wq = rpciod_workqueue;
10931da177e4SLinus Torvalds 	rpciod_workqueue = NULL;
1094b247bbf1STrond Myklebust 	destroy_workqueue(wq);
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds void
10981da177e4SLinus Torvalds rpc_destroy_mempool(void)
10991da177e4SLinus Torvalds {
1100b247bbf1STrond Myklebust 	rpciod_stop();
11011da177e4SLinus Torvalds 	if (rpc_buffer_mempool)
11021da177e4SLinus Torvalds 		mempool_destroy(rpc_buffer_mempool);
11031da177e4SLinus Torvalds 	if (rpc_task_mempool)
11041da177e4SLinus Torvalds 		mempool_destroy(rpc_task_mempool);
11051a1d92c1SAlexey Dobriyan 	if (rpc_task_slabp)
11061a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_task_slabp);
11071a1d92c1SAlexey Dobriyan 	if (rpc_buffer_slabp)
11081a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_buffer_slabp);
1109f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&delay_queue);
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds int
11131da177e4SLinus Torvalds rpc_init_mempool(void)
11141da177e4SLinus Torvalds {
1115f6a1cc89STrond Myklebust 	/*
1116f6a1cc89STrond Myklebust 	 * The following is not strictly a mempool initialisation,
1117f6a1cc89STrond Myklebust 	 * but there is no harm in doing it here
1118f6a1cc89STrond Myklebust 	 */
1119f6a1cc89STrond Myklebust 	rpc_init_wait_queue(&delay_queue, "delayq");
1120f6a1cc89STrond Myklebust 	if (!rpciod_start())
1121f6a1cc89STrond Myklebust 		goto err_nomem;
1122f6a1cc89STrond Myklebust 
11231da177e4SLinus Torvalds 	rpc_task_slabp = kmem_cache_create("rpc_tasks",
11241da177e4SLinus Torvalds 					     sizeof(struct rpc_task),
11251da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
112620c2df83SPaul Mundt 					     NULL);
11271da177e4SLinus Torvalds 	if (!rpc_task_slabp)
11281da177e4SLinus Torvalds 		goto err_nomem;
11291da177e4SLinus Torvalds 	rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
11301da177e4SLinus Torvalds 					     RPC_BUFFER_MAXSIZE,
11311da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
113220c2df83SPaul Mundt 					     NULL);
11331da177e4SLinus Torvalds 	if (!rpc_buffer_slabp)
11341da177e4SLinus Torvalds 		goto err_nomem;
113593d2341cSMatthew Dobson 	rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
11361da177e4SLinus Torvalds 						    rpc_task_slabp);
11371da177e4SLinus Torvalds 	if (!rpc_task_mempool)
11381da177e4SLinus Torvalds 		goto err_nomem;
113993d2341cSMatthew Dobson 	rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
11401da177e4SLinus Torvalds 						      rpc_buffer_slabp);
11411da177e4SLinus Torvalds 	if (!rpc_buffer_mempool)
11421da177e4SLinus Torvalds 		goto err_nomem;
11431da177e4SLinus Torvalds 	return 0;
11441da177e4SLinus Torvalds err_nomem:
11451da177e4SLinus Torvalds 	rpc_destroy_mempool();
11461da177e4SLinus Torvalds 	return -ENOMEM;
11471da177e4SLinus Torvalds }
1148