xref: /linux/net/sunrpc/sched.c (revision 3ff7576ddac06c3d07089e241b40826d24bbf1ac)
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/smp_lock.h>
201da177e4SLinus Torvalds #include <linux/spinlock.h>
214a3e2f71SArjan van de Ven #include <linux/mutex.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #ifdef RPC_DEBUG
261da177e4SLinus Torvalds #define RPCDBG_FACILITY		RPCDBG_SCHED
271da177e4SLinus Torvalds #define RPC_TASK_MAGIC_ID	0xf00baa
281da177e4SLinus Torvalds #endif
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * RPC slabs and memory pools
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE	(2048)
341da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE	(8)
351da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE	(8)
36e18b890bSChristoph Lameter static struct kmem_cache	*rpc_task_slabp __read_mostly;
37e18b890bSChristoph Lameter static struct kmem_cache	*rpc_buffer_slabp __read_mostly;
38ba89966cSEric Dumazet static mempool_t	*rpc_task_mempool __read_mostly;
39ba89966cSEric Dumazet static mempool_t	*rpc_buffer_mempool __read_mostly;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static void			__rpc_default_timer(struct rpc_task *task);
4265f27f38SDavid Howells static void			rpc_async_schedule(struct work_struct *);
43bde8f00cSTrond Myklebust static void			 rpc_release_task(struct rpc_task *task);
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  * RPC tasks sit here while waiting for conditions to improve.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds static RPC_WAITQ(delay_queue, "delayq");
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * rpciod-related stuff
521da177e4SLinus Torvalds  */
5324c5d9d7STrond Myklebust struct workqueue_struct *rpciod_workqueue;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * Disable the timer for a given RPC task. Should be called with
571da177e4SLinus Torvalds  * queue->lock and bh_disabled in order to avoid races within
581da177e4SLinus Torvalds  * rpc_run_timer().
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds static inline void
611da177e4SLinus Torvalds __rpc_disable_timer(struct rpc_task *task)
621da177e4SLinus Torvalds {
6346121cf7SChuck Lever 	dprintk("RPC: %5u disabling timer\n", task->tk_pid);
641da177e4SLinus Torvalds 	task->tk_timeout_fn = NULL;
651da177e4SLinus Torvalds 	task->tk_timeout = 0;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * Run a timeout function.
701da177e4SLinus Torvalds  * We use the callback in order to allow __rpc_wake_up_task()
711da177e4SLinus Torvalds  * and friends to disable the timer synchronously on SMP systems
721da177e4SLinus Torvalds  * without calling del_timer_sync(). The latter could cause a
731da177e4SLinus Torvalds  * deadlock if called while we're holding spinlocks...
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds static void rpc_run_timer(struct rpc_task *task)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	void (*callback)(struct rpc_task *);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	callback = task->tk_timeout_fn;
801da177e4SLinus Torvalds 	task->tk_timeout_fn = NULL;
811da177e4SLinus Torvalds 	if (callback && RPC_IS_QUEUED(task)) {
8246121cf7SChuck Lever 		dprintk("RPC: %5u running timer\n", task->tk_pid);
831da177e4SLinus Torvalds 		callback(task);
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 	smp_mb__before_clear_bit();
861da177e4SLinus Torvalds 	clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
871da177e4SLinus Torvalds 	smp_mb__after_clear_bit();
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds  * Set up a timer for the current task.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds static inline void
941da177e4SLinus Torvalds __rpc_add_timer(struct rpc_task *task, rpc_action timer)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	if (!task->tk_timeout)
971da177e4SLinus Torvalds 		return;
981da177e4SLinus Torvalds 
9946121cf7SChuck Lever 	dprintk("RPC: %5u setting alarm for %lu ms\n",
1001da177e4SLinus Torvalds 			task->tk_pid, task->tk_timeout * 1000 / HZ);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	if (timer)
1031da177e4SLinus Torvalds 		task->tk_timeout_fn = timer;
1041da177e4SLinus Torvalds 	else
1051da177e4SLinus Torvalds 		task->tk_timeout_fn = __rpc_default_timer;
1061da177e4SLinus Torvalds 	set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
1071da177e4SLinus Torvalds 	mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * Delete any timer for the current task. Because we use del_timer_sync(),
1121da177e4SLinus Torvalds  * this function should never be called while holding queue->lock.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static void
1151da177e4SLinus Torvalds rpc_delete_timer(struct rpc_task *task)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	if (RPC_IS_QUEUED(task))
1181da177e4SLinus Torvalds 		return;
1191da177e4SLinus Torvalds 	if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
1201da177e4SLinus Torvalds 		del_singleshot_timer_sync(&task->tk_timer);
12146121cf7SChuck Lever 		dprintk("RPC: %5u deleting timer\n", task->tk_pid);
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  * Add new request to a priority queue.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct list_head *q;
1311da177e4SLinus Torvalds 	struct rpc_task *t;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	INIT_LIST_HEAD(&task->u.tk_wait.links);
1341da177e4SLinus Torvalds 	q = &queue->tasks[task->tk_priority];
1351da177e4SLinus Torvalds 	if (unlikely(task->tk_priority > queue->maxpriority))
1361da177e4SLinus Torvalds 		q = &queue->tasks[queue->maxpriority];
1371da177e4SLinus Torvalds 	list_for_each_entry(t, q, u.tk_wait.list) {
138*3ff7576dSTrond Myklebust 		if (t->tk_owner == task->tk_owner) {
1391da177e4SLinus Torvalds 			list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
1401da177e4SLinus Torvalds 			return;
1411da177e4SLinus Torvalds 		}
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 	list_add_tail(&task->u.tk_wait.list, q);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
1471da177e4SLinus Torvalds  * Add new request to wait queue.
1481da177e4SLinus Torvalds  *
1491da177e4SLinus Torvalds  * Swapper tasks always get inserted at the head of the queue.
1501da177e4SLinus Torvalds  * This should avoid many nasty memory deadlocks and hopefully
1511da177e4SLinus Torvalds  * improve overall performance.
1521da177e4SLinus Torvalds  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1591da177e4SLinus Torvalds 		__rpc_add_wait_queue_priority(queue, task);
1601da177e4SLinus Torvalds 	else if (RPC_IS_SWAPPER(task))
1611da177e4SLinus Torvalds 		list_add(&task->u.tk_wait.list, &queue->tasks[0]);
1621da177e4SLinus Torvalds 	else
1631da177e4SLinus Torvalds 		list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
1641da177e4SLinus Torvalds 	task->u.tk_wait.rpc_waitq = queue;
165e19b63daSChuck Lever 	queue->qlen++;
1661da177e4SLinus Torvalds 	rpc_set_queued(task);
1671da177e4SLinus Torvalds 
16846121cf7SChuck Lever 	dprintk("RPC: %5u added to queue %p \"%s\"\n",
1691da177e4SLinus Torvalds 			task->tk_pid, queue, rpc_qname(queue));
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * Remove request from a priority queue.
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct rpc_task *t;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (!list_empty(&task->u.tk_wait.links)) {
1801da177e4SLinus Torvalds 		t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
1811da177e4SLinus Torvalds 		list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
1821da177e4SLinus Torvalds 		list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	list_del(&task->u.tk_wait.list);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * Remove request from queue.
1891da177e4SLinus Torvalds  * Note: must be called with spin lock held.
1901da177e4SLinus Torvalds  */
1911da177e4SLinus Torvalds static void __rpc_remove_wait_queue(struct rpc_task *task)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	struct rpc_wait_queue *queue;
1941da177e4SLinus Torvalds 	queue = task->u.tk_wait.rpc_waitq;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1971da177e4SLinus Torvalds 		__rpc_remove_wait_queue_priority(task);
1981da177e4SLinus Torvalds 	else
1991da177e4SLinus Torvalds 		list_del(&task->u.tk_wait.list);
200e19b63daSChuck Lever 	queue->qlen--;
20146121cf7SChuck Lever 	dprintk("RPC: %5u removed from queue %p \"%s\"\n",
2021da177e4SLinus Torvalds 			task->tk_pid, queue, rpc_qname(queue));
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	queue->priority = priority;
2081da177e4SLinus Torvalds 	queue->count = 1 << (priority * 2);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
211*3ff7576dSTrond Myklebust static inline void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
2121da177e4SLinus Torvalds {
213*3ff7576dSTrond Myklebust 	queue->owner = pid;
2141da177e4SLinus Torvalds 	queue->nr = RPC_BATCH_COUNT;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, queue->maxpriority);
220*3ff7576dSTrond Myklebust 	rpc_set_waitqueue_owner(queue, 0);
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
223*3ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	int i;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	spin_lock_init(&queue->lock);
2281da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
2291da177e4SLinus Torvalds 		INIT_LIST_HEAD(&queue->tasks[i]);
230*3ff7576dSTrond Myklebust 	queue->maxpriority = nr_queues - 1;
2311da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
2321da177e4SLinus Torvalds #ifdef RPC_DEBUG
2331da177e4SLinus Torvalds 	queue->name = qname;
2341da177e4SLinus Torvalds #endif
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2381da177e4SLinus Torvalds {
239*3ff7576dSTrond Myklebust 	__rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2431da177e4SLinus Torvalds {
244*3ff7576dSTrond Myklebust 	__rpc_init_priority_wait_queue(queue, qname, 1);
2451da177e4SLinus Torvalds }
246e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
2471da177e4SLinus Torvalds 
24844c28873STrond Myklebust static int rpc_wait_bit_interruptible(void *word)
24944c28873STrond Myklebust {
25044c28873STrond Myklebust 	if (signal_pending(current))
25144c28873STrond Myklebust 		return -ERESTARTSYS;
25244c28873STrond Myklebust 	schedule();
25344c28873STrond Myklebust 	return 0;
25444c28873STrond Myklebust }
25544c28873STrond Myklebust 
256c44fe705STrond Myklebust #ifdef RPC_DEBUG
257c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task)
258c44fe705STrond Myklebust {
259c44fe705STrond Myklebust 	static atomic_t rpc_pid;
260c44fe705STrond Myklebust 
261c44fe705STrond Myklebust 	task->tk_magic = RPC_TASK_MAGIC_ID;
262c44fe705STrond Myklebust 	task->tk_pid = atomic_inc_return(&rpc_pid);
263c44fe705STrond Myklebust }
264c44fe705STrond Myklebust #else
265c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task)
266c44fe705STrond Myklebust {
267c44fe705STrond Myklebust }
268c44fe705STrond Myklebust #endif
269c44fe705STrond Myklebust 
270e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task)
271e6b3c4dbSTrond Myklebust {
2724bef61ffSTrond Myklebust 	struct rpc_clnt *clnt;
273e6b3c4dbSTrond Myklebust 	if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
274e6b3c4dbSTrond Myklebust 		return;
275c44fe705STrond Myklebust 	rpc_task_set_debuginfo(task);
276e6b3c4dbSTrond Myklebust 	/* Add to global list of all tasks */
2774bef61ffSTrond Myklebust 	clnt = task->tk_client;
2784bef61ffSTrond Myklebust 	if (clnt != NULL) {
2794bef61ffSTrond Myklebust 		spin_lock(&clnt->cl_lock);
2804bef61ffSTrond Myklebust 		list_add_tail(&task->tk_task, &clnt->cl_tasks);
2814bef61ffSTrond Myklebust 		spin_unlock(&clnt->cl_lock);
2824bef61ffSTrond Myklebust 	}
283e6b3c4dbSTrond Myklebust }
284e6b3c4dbSTrond Myklebust 
28544c28873STrond Myklebust /*
28644c28873STrond Myklebust  * Mark an RPC call as having completed by clearing the 'active' bit
28744c28873STrond Myklebust  */
288e6b3c4dbSTrond Myklebust static void rpc_mark_complete_task(struct rpc_task *task)
28944c28873STrond Myklebust {
290e6b3c4dbSTrond Myklebust 	smp_mb__before_clear_bit();
291e6b3c4dbSTrond Myklebust 	clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
292e6b3c4dbSTrond Myklebust 	smp_mb__after_clear_bit();
29344c28873STrond Myklebust 	wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
29444c28873STrond Myklebust }
29544c28873STrond Myklebust 
29644c28873STrond Myklebust /*
29744c28873STrond Myklebust  * Allow callers to wait for completion of an RPC call
29844c28873STrond Myklebust  */
29944c28873STrond Myklebust int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
30044c28873STrond Myklebust {
30144c28873STrond Myklebust 	if (action == NULL)
30244c28873STrond Myklebust 		action = rpc_wait_bit_interruptible;
30344c28873STrond Myklebust 	return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
30444c28873STrond Myklebust 			action, TASK_INTERRUPTIBLE);
30544c28873STrond Myklebust }
306e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
30744c28873STrond Myklebust 
3081da177e4SLinus Torvalds /*
3091da177e4SLinus Torvalds  * Make an RPC task runnable.
3101da177e4SLinus Torvalds  *
3111da177e4SLinus Torvalds  * Note: If the task is ASYNC, this must be called with
3121da177e4SLinus Torvalds  * the spinlock held to protect the wait queue operation.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	BUG_ON(task->tk_timeout_fn);
3171da177e4SLinus Torvalds 	rpc_clear_queued(task);
318cc4dc59eSChristophe Saout 	if (rpc_test_and_set_running(task))
3191da177e4SLinus Torvalds 		return;
320cc4dc59eSChristophe Saout 	/* We might have raced */
321cc4dc59eSChristophe Saout 	if (RPC_IS_QUEUED(task)) {
322cc4dc59eSChristophe Saout 		rpc_clear_running(task);
323cc4dc59eSChristophe Saout 		return;
324cc4dc59eSChristophe Saout 	}
3251da177e4SLinus Torvalds 	if (RPC_IS_ASYNC(task)) {
3261da177e4SLinus Torvalds 		int status;
3271da177e4SLinus Torvalds 
32865f27f38SDavid Howells 		INIT_WORK(&task->u.tk_work, rpc_async_schedule);
3291da177e4SLinus Torvalds 		status = queue_work(task->tk_workqueue, &task->u.tk_work);
3301da177e4SLinus Torvalds 		if (status < 0) {
3311da177e4SLinus Torvalds 			printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
3321da177e4SLinus Torvalds 			task->tk_status = status;
3331da177e4SLinus Torvalds 			return;
3341da177e4SLinus Torvalds 		}
3351da177e4SLinus Torvalds 	} else
33696651ab3STrond Myklebust 		wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds /*
3401da177e4SLinus Torvalds  * Prepare for sleeping on a wait queue.
3411da177e4SLinus Torvalds  * By always appending tasks to the list we ensure FIFO behavior.
3421da177e4SLinus Torvalds  * NB: An RPC task will only receive interrupt-driven events as long
3431da177e4SLinus Torvalds  * as it's on a wait queue.
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3461da177e4SLinus Torvalds 			rpc_action action, rpc_action timer)
3471da177e4SLinus Torvalds {
34846121cf7SChuck Lever 	dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
34946121cf7SChuck Lever 			task->tk_pid, rpc_qname(q), jiffies);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
3521da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
3531da177e4SLinus Torvalds 		return;
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	__rpc_add_wait_queue(q, task);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	BUG_ON(task->tk_callback != NULL);
3591da177e4SLinus Torvalds 	task->tk_callback = action;
3601da177e4SLinus Torvalds 	__rpc_add_timer(task, timer);
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3641da177e4SLinus Torvalds 				rpc_action action, rpc_action timer)
3651da177e4SLinus Torvalds {
366e6b3c4dbSTrond Myklebust 	/* Mark the task as being activated if so needed */
367e6b3c4dbSTrond Myklebust 	rpc_set_active(task);
368e6b3c4dbSTrond Myklebust 
3691da177e4SLinus Torvalds 	/*
3701da177e4SLinus Torvalds 	 * Protect the queue operations.
3711da177e4SLinus Torvalds 	 */
3721da177e4SLinus Torvalds 	spin_lock_bh(&q->lock);
3731da177e4SLinus Torvalds 	__rpc_sleep_on(q, task, action, timer);
3741da177e4SLinus Torvalds 	spin_unlock_bh(&q->lock);
3751da177e4SLinus Torvalds }
376e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds /**
3791da177e4SLinus Torvalds  * __rpc_do_wake_up_task - wake up a single rpc_task
3801da177e4SLinus Torvalds  * @task: task to be woken up
3811da177e4SLinus Torvalds  *
3821da177e4SLinus Torvalds  * Caller must hold queue->lock, and have cleared the task queued flag.
3831da177e4SLinus Torvalds  */
3841da177e4SLinus Torvalds static void __rpc_do_wake_up_task(struct rpc_task *task)
3851da177e4SLinus Torvalds {
38646121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
38746121cf7SChuck Lever 			task->tk_pid, jiffies);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds #ifdef RPC_DEBUG
3901da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
3911da177e4SLinus Torvalds #endif
3921da177e4SLinus Torvalds 	/* Has the task been executed yet? If not, we cannot wake it up! */
3931da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task)) {
3941da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
3951da177e4SLinus Torvalds 		return;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	__rpc_disable_timer(task);
3991da177e4SLinus Torvalds 	__rpc_remove_wait_queue(task);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	rpc_make_runnable(task);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	dprintk("RPC:       __rpc_wake_up_task done\n");
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  * Wake up the specified task
4081da177e4SLinus Torvalds  */
4091da177e4SLinus Torvalds static void __rpc_wake_up_task(struct rpc_task *task)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	if (rpc_start_wakeup(task)) {
4121da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(task))
4131da177e4SLinus Torvalds 			__rpc_do_wake_up_task(task);
4141da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds  * Default timeout handler if none specified by user
4201da177e4SLinus Torvalds  */
4211da177e4SLinus Torvalds static void
4221da177e4SLinus Torvalds __rpc_default_timer(struct rpc_task *task)
4231da177e4SLinus Torvalds {
42446121cf7SChuck Lever 	dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid);
4251da177e4SLinus Torvalds 	task->tk_status = -ETIMEDOUT;
4261da177e4SLinus Torvalds 	rpc_wake_up_task(task);
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds  * Wake up the specified task
4311da177e4SLinus Torvalds  */
4321da177e4SLinus Torvalds void rpc_wake_up_task(struct rpc_task *task)
4331da177e4SLinus Torvalds {
4348aca67f0STrond Myklebust 	rcu_read_lock_bh();
4351da177e4SLinus Torvalds 	if (rpc_start_wakeup(task)) {
4361da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(task)) {
4371da177e4SLinus Torvalds 			struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
4381da177e4SLinus Torvalds 
4398aca67f0STrond Myklebust 			/* Note: we're already in a bh-safe context */
4408aca67f0STrond Myklebust 			spin_lock(&queue->lock);
4411da177e4SLinus Torvalds 			__rpc_do_wake_up_task(task);
4428aca67f0STrond Myklebust 			spin_unlock(&queue->lock);
4431da177e4SLinus Torvalds 		}
4441da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
4451da177e4SLinus Torvalds 	}
4468aca67f0STrond Myklebust 	rcu_read_unlock_bh();
4471da177e4SLinus Torvalds }
448e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_task);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /*
4511da177e4SLinus Torvalds  * Wake up the next task on a priority queue.
4521da177e4SLinus Torvalds  */
4531da177e4SLinus Torvalds static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct list_head *q;
4561da177e4SLinus Torvalds 	struct rpc_task *task;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/*
459*3ff7576dSTrond Myklebust 	 * Service a batch of tasks from a single owner.
4601da177e4SLinus Torvalds 	 */
4611da177e4SLinus Torvalds 	q = &queue->tasks[queue->priority];
4621da177e4SLinus Torvalds 	if (!list_empty(q)) {
4631da177e4SLinus Torvalds 		task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
464*3ff7576dSTrond Myklebust 		if (queue->owner == task->tk_owner) {
4651da177e4SLinus Torvalds 			if (--queue->nr)
4661da177e4SLinus Torvalds 				goto out;
4671da177e4SLinus Torvalds 			list_move_tail(&task->u.tk_wait.list, q);
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 		/*
4701da177e4SLinus Torvalds 		 * Check if we need to switch queues.
4711da177e4SLinus Torvalds 		 */
4721da177e4SLinus Torvalds 		if (--queue->count)
473*3ff7576dSTrond Myklebust 			goto new_owner;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	/*
4771da177e4SLinus Torvalds 	 * Service the next queue.
4781da177e4SLinus Torvalds 	 */
4791da177e4SLinus Torvalds 	do {
4801da177e4SLinus Torvalds 		if (q == &queue->tasks[0])
4811da177e4SLinus Torvalds 			q = &queue->tasks[queue->maxpriority];
4821da177e4SLinus Torvalds 		else
4831da177e4SLinus Torvalds 			q = q - 1;
4841da177e4SLinus Torvalds 		if (!list_empty(q)) {
4851da177e4SLinus Torvalds 			task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4861da177e4SLinus Torvalds 			goto new_queue;
4871da177e4SLinus Torvalds 		}
4881da177e4SLinus Torvalds 	} while (q != &queue->tasks[queue->priority]);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
4911da177e4SLinus Torvalds 	return NULL;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds new_queue:
4941da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
495*3ff7576dSTrond Myklebust new_owner:
496*3ff7576dSTrond Myklebust 	rpc_set_waitqueue_owner(queue, task->tk_owner);
4971da177e4SLinus Torvalds out:
4981da177e4SLinus Torvalds 	__rpc_wake_up_task(task);
4991da177e4SLinus Torvalds 	return task;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds /*
5031da177e4SLinus Torvalds  * Wake up the next task on the wait queue.
5041da177e4SLinus Torvalds  */
5051da177e4SLinus Torvalds struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	struct rpc_task	*task = NULL;
5081da177e4SLinus Torvalds 
50946121cf7SChuck Lever 	dprintk("RPC:       wake_up_next(%p \"%s\")\n",
51046121cf7SChuck Lever 			queue, rpc_qname(queue));
5118aca67f0STrond Myklebust 	rcu_read_lock_bh();
5128aca67f0STrond Myklebust 	spin_lock(&queue->lock);
5131da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
5141da177e4SLinus Torvalds 		task = __rpc_wake_up_next_priority(queue);
5151da177e4SLinus Torvalds 	else {
5161da177e4SLinus Torvalds 		task_for_first(task, &queue->tasks[0])
5171da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5181da177e4SLinus Torvalds 	}
5198aca67f0STrond Myklebust 	spin_unlock(&queue->lock);
5208aca67f0STrond Myklebust 	rcu_read_unlock_bh();
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	return task;
5231da177e4SLinus Torvalds }
524e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next);
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds /**
5271da177e4SLinus Torvalds  * rpc_wake_up - wake up all rpc_tasks
5281da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5291da177e4SLinus Torvalds  *
5301da177e4SLinus Torvalds  * Grabs queue->lock
5311da177e4SLinus Torvalds  */
5321da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue)
5331da177e4SLinus Torvalds {
534e6d83d55STrond Myklebust 	struct rpc_task *task, *next;
5351da177e4SLinus Torvalds 	struct list_head *head;
536e6d83d55STrond Myklebust 
5378aca67f0STrond Myklebust 	rcu_read_lock_bh();
5388aca67f0STrond Myklebust 	spin_lock(&queue->lock);
5391da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5401da177e4SLinus Torvalds 	for (;;) {
541e6d83d55STrond Myklebust 		list_for_each_entry_safe(task, next, head, u.tk_wait.list)
5421da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5431da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5441da177e4SLinus Torvalds 			break;
5451da177e4SLinus Torvalds 		head--;
5461da177e4SLinus Torvalds 	}
5478aca67f0STrond Myklebust 	spin_unlock(&queue->lock);
5488aca67f0STrond Myklebust 	rcu_read_unlock_bh();
5491da177e4SLinus Torvalds }
550e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /**
5531da177e4SLinus Torvalds  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
5541da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5551da177e4SLinus Torvalds  * @status: status value to set
5561da177e4SLinus Torvalds  *
5571da177e4SLinus Torvalds  * Grabs queue->lock
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
5601da177e4SLinus Torvalds {
561e6d83d55STrond Myklebust 	struct rpc_task *task, *next;
5621da177e4SLinus Torvalds 	struct list_head *head;
5631da177e4SLinus Torvalds 
5648aca67f0STrond Myklebust 	rcu_read_lock_bh();
5658aca67f0STrond Myklebust 	spin_lock(&queue->lock);
5661da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5671da177e4SLinus Torvalds 	for (;;) {
568e6d83d55STrond Myklebust 		list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
5691da177e4SLinus Torvalds 			task->tk_status = status;
5701da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5731da177e4SLinus Torvalds 			break;
5741da177e4SLinus Torvalds 		head--;
5751da177e4SLinus Torvalds 	}
5768aca67f0STrond Myklebust 	spin_unlock(&queue->lock);
5778aca67f0STrond Myklebust 	rcu_read_unlock_bh();
5781da177e4SLinus Torvalds }
579e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status);
5801da177e4SLinus Torvalds 
5818014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task)
5828014793bSTrond Myklebust {
5838014793bSTrond Myklebust 	rpc_wake_up_task(task);
5848014793bSTrond Myklebust }
5858014793bSTrond Myklebust 
5861da177e4SLinus Torvalds /*
5871da177e4SLinus Torvalds  * Run a task at a later time
5881da177e4SLinus Torvalds  */
5898014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	task->tk_timeout = delay;
5921da177e4SLinus Torvalds 	rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
5931da177e4SLinus Torvalds }
594e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds /*
5974ce70adaSTrond Myklebust  * Helper to call task->tk_ops->rpc_call_prepare
5984ce70adaSTrond Myklebust  */
5994ce70adaSTrond Myklebust static void rpc_prepare_task(struct rpc_task *task)
6004ce70adaSTrond Myklebust {
6016d5fcb5aSTrond Myklebust 	lock_kernel();
6024ce70adaSTrond Myklebust 	task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
6036d5fcb5aSTrond Myklebust 	unlock_kernel();
6044ce70adaSTrond Myklebust }
6054ce70adaSTrond Myklebust 
6064ce70adaSTrond Myklebust /*
607963d8fe5STrond Myklebust  * Helper that calls task->tk_ops->rpc_call_done if it exists
608d05fdb0cSTrond Myklebust  */
609abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task)
610d05fdb0cSTrond Myklebust {
611abbcf28fSTrond Myklebust 	task->tk_action = NULL;
612963d8fe5STrond Myklebust 	if (task->tk_ops->rpc_call_done != NULL) {
6136d5fcb5aSTrond Myklebust 		lock_kernel();
614963d8fe5STrond Myklebust 		task->tk_ops->rpc_call_done(task, task->tk_calldata);
6156d5fcb5aSTrond Myklebust 		unlock_kernel();
616d05fdb0cSTrond Myklebust 		if (task->tk_action != NULL) {
617abbcf28fSTrond Myklebust 			WARN_ON(RPC_ASSASSINATED(task));
618abbcf28fSTrond Myklebust 			/* Always release the RPC slot and buffer memory */
619d05fdb0cSTrond Myklebust 			xprt_release(task);
620d05fdb0cSTrond Myklebust 		}
621d05fdb0cSTrond Myklebust 	}
622d05fdb0cSTrond Myklebust }
623e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit_task);
624d05fdb0cSTrond Myklebust 
625bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
626bbd5a1f9STrond Myklebust {
627bbd5a1f9STrond Myklebust 	if (ops->rpc_release != NULL) {
628bbd5a1f9STrond Myklebust 		lock_kernel();
629bbd5a1f9STrond Myklebust 		ops->rpc_release(calldata);
630bbd5a1f9STrond Myklebust 		unlock_kernel();
631bbd5a1f9STrond Myklebust 	}
632bbd5a1f9STrond Myklebust }
633bbd5a1f9STrond Myklebust 
634d05fdb0cSTrond Myklebust /*
6351da177e4SLinus Torvalds  * This is the RPC `scheduler' (or rather, the finite state machine).
6361da177e4SLinus Torvalds  */
6372efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task)
6381da177e4SLinus Torvalds {
6391da177e4SLinus Torvalds 	int		status = 0;
6401da177e4SLinus Torvalds 
64146121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
6421da177e4SLinus Torvalds 			task->tk_pid, task->tk_flags);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	BUG_ON(RPC_IS_QUEUED(task));
6451da177e4SLinus Torvalds 
646d05fdb0cSTrond Myklebust 	for (;;) {
6471da177e4SLinus Torvalds 		/*
6481da177e4SLinus Torvalds 		 * Garbage collection of pending timers...
6491da177e4SLinus Torvalds 		 */
6501da177e4SLinus Torvalds 		rpc_delete_timer(task);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 		/*
6531da177e4SLinus Torvalds 		 * Execute any pending callback.
6541da177e4SLinus Torvalds 		 */
6551da177e4SLinus Torvalds 		if (RPC_DO_CALLBACK(task)) {
6561da177e4SLinus Torvalds 			/* Define a callback save pointer */
6571da177e4SLinus Torvalds 			void (*save_callback)(struct rpc_task *);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 			/*
6601da177e4SLinus Torvalds 			 * If a callback exists, save it, reset it,
6611da177e4SLinus Torvalds 			 * call it.
6621da177e4SLinus Torvalds 			 * The save is needed to stop from resetting
6631da177e4SLinus Torvalds 			 * another callback set within the callback handler
6641da177e4SLinus Torvalds 			 * - Dave
6651da177e4SLinus Torvalds 			 */
6661da177e4SLinus Torvalds 			save_callback=task->tk_callback;
6671da177e4SLinus Torvalds 			task->tk_callback=NULL;
6681da177e4SLinus Torvalds 			save_callback(task);
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		/*
6721da177e4SLinus Torvalds 		 * Perform the next FSM step.
6731da177e4SLinus Torvalds 		 * tk_action may be NULL when the task has been killed
6741da177e4SLinus Torvalds 		 * by someone else.
6751da177e4SLinus Torvalds 		 */
6761da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task)) {
677abbcf28fSTrond Myklebust 			if (task->tk_action == NULL)
678abbcf28fSTrond Myklebust 				break;
6791da177e4SLinus Torvalds 			task->tk_action(task);
6801da177e4SLinus Torvalds 		}
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 		/*
6831da177e4SLinus Torvalds 		 * Lockless check for whether task is sleeping or not.
6841da177e4SLinus Torvalds 		 */
6851da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task))
6861da177e4SLinus Torvalds 			continue;
6871da177e4SLinus Torvalds 		rpc_clear_running(task);
6881da177e4SLinus Torvalds 		if (RPC_IS_ASYNC(task)) {
6891da177e4SLinus Torvalds 			/* Careful! we may have raced... */
6901da177e4SLinus Torvalds 			if (RPC_IS_QUEUED(task))
6912efef837STrond Myklebust 				return;
6921da177e4SLinus Torvalds 			if (rpc_test_and_set_running(task))
6932efef837STrond Myklebust 				return;
6941da177e4SLinus Torvalds 			continue;
6951da177e4SLinus Torvalds 		}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		/* sync task: sleep here */
69846121cf7SChuck Lever 		dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
69996651ab3STrond Myklebust 		/* Note: Caller should be using rpc_clnt_sigmask() */
70096651ab3STrond Myklebust 		status = out_of_line_wait_on_bit(&task->tk_runstate,
70196651ab3STrond Myklebust 				RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
70296651ab3STrond Myklebust 				TASK_INTERRUPTIBLE);
70396651ab3STrond Myklebust 		if (status == -ERESTARTSYS) {
7041da177e4SLinus Torvalds 			/*
7051da177e4SLinus Torvalds 			 * When a sync task receives a signal, it exits with
7061da177e4SLinus Torvalds 			 * -ERESTARTSYS. In order to catch any callbacks that
7071da177e4SLinus Torvalds 			 * clean up after sleeping on some queue, we don't
7081da177e4SLinus Torvalds 			 * break the loop here, but go around once more.
7091da177e4SLinus Torvalds 			 */
71046121cf7SChuck Lever 			dprintk("RPC: %5u got signal\n", task->tk_pid);
7111da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_KILLED;
7121da177e4SLinus Torvalds 			rpc_exit(task, -ERESTARTSYS);
7131da177e4SLinus Torvalds 			rpc_wake_up_task(task);
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 		rpc_set_running(task);
71646121cf7SChuck Lever 		dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
71946121cf7SChuck Lever 	dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
72046121cf7SChuck Lever 			task->tk_status);
7211da177e4SLinus Torvalds 	/* Release all resources associated with the task */
7221da177e4SLinus Torvalds 	rpc_release_task(task);
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /*
7261da177e4SLinus Torvalds  * User-visible entry point to the scheduler.
7271da177e4SLinus Torvalds  *
7281da177e4SLinus Torvalds  * This may be called recursively if e.g. an async NFS task updates
7291da177e4SLinus Torvalds  * the attributes and finds that dirty pages must be flushed.
7301da177e4SLinus Torvalds  * NOTE: Upon exit of this function the task is guaranteed to be
7311da177e4SLinus Torvalds  *	 released. In particular note that tk_release() will have
7321da177e4SLinus Torvalds  *	 been called, so your task memory may have been freed.
7331da177e4SLinus Torvalds  */
7342efef837STrond Myklebust void rpc_execute(struct rpc_task *task)
7351da177e4SLinus Torvalds {
73644c28873STrond Myklebust 	rpc_set_active(task);
7371da177e4SLinus Torvalds 	rpc_set_running(task);
7382efef837STrond Myklebust 	__rpc_execute(task);
7391da177e4SLinus Torvalds }
740e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_execute);
7411da177e4SLinus Torvalds 
74265f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work)
7431da177e4SLinus Torvalds {
74465f27f38SDavid Howells 	__rpc_execute(container_of(work, struct rpc_task, u.tk_work));
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
747aa3d1faeSChuck Lever struct rpc_buffer {
748aa3d1faeSChuck Lever 	size_t	len;
749aa3d1faeSChuck Lever 	char	data[];
750aa3d1faeSChuck Lever };
751aa3d1faeSChuck Lever 
75202107148SChuck Lever /**
75302107148SChuck Lever  * rpc_malloc - allocate an RPC buffer
75402107148SChuck Lever  * @task: RPC task that will use this buffer
75502107148SChuck Lever  * @size: requested byte size
7561da177e4SLinus Torvalds  *
757c5a4dd8bSChuck Lever  * To prevent rpciod from hanging, this allocator never sleeps,
758c5a4dd8bSChuck Lever  * returning NULL if the request cannot be serviced immediately.
759c5a4dd8bSChuck Lever  * The caller can arrange to sleep in a way that is safe for rpciod.
760c5a4dd8bSChuck Lever  *
761c5a4dd8bSChuck Lever  * Most requests are 'small' (under 2KiB) and can be serviced from a
762c5a4dd8bSChuck Lever  * mempool, ensuring that NFS reads and writes can always proceed,
763c5a4dd8bSChuck Lever  * and that there is good locality of reference for these buffers.
764c5a4dd8bSChuck Lever  *
7651da177e4SLinus Torvalds  * In order to avoid memory starvation triggering more writebacks of
766c5a4dd8bSChuck Lever  * NFS requests, we avoid using GFP_KERNEL.
7671da177e4SLinus Torvalds  */
76802107148SChuck Lever void *rpc_malloc(struct rpc_task *task, size_t size)
7691da177e4SLinus Torvalds {
770aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
771c5a4dd8bSChuck Lever 	gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
7721da177e4SLinus Torvalds 
773aa3d1faeSChuck Lever 	size += sizeof(struct rpc_buffer);
774c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
775c5a4dd8bSChuck Lever 		buf = mempool_alloc(rpc_buffer_mempool, gfp);
7761da177e4SLinus Torvalds 	else
777c5a4dd8bSChuck Lever 		buf = kmalloc(size, gfp);
778ddce40dfSPeter Zijlstra 
779ddce40dfSPeter Zijlstra 	if (!buf)
780ddce40dfSPeter Zijlstra 		return NULL;
781ddce40dfSPeter Zijlstra 
782aa3d1faeSChuck Lever 	buf->len = size;
783215d0678SGeert Uytterhoeven 	dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
784c5a4dd8bSChuck Lever 			task->tk_pid, size, buf);
785aa3d1faeSChuck Lever 	return &buf->data;
7861da177e4SLinus Torvalds }
78712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc);
7881da177e4SLinus Torvalds 
78902107148SChuck Lever /**
79002107148SChuck Lever  * rpc_free - free buffer allocated via rpc_malloc
791c5a4dd8bSChuck Lever  * @buffer: buffer to free
79202107148SChuck Lever  *
79302107148SChuck Lever  */
794c5a4dd8bSChuck Lever void rpc_free(void *buffer)
7951da177e4SLinus Torvalds {
796aa3d1faeSChuck Lever 	size_t size;
797aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
79802107148SChuck Lever 
799c5a4dd8bSChuck Lever 	if (!buffer)
800c5a4dd8bSChuck Lever 		return;
801aa3d1faeSChuck Lever 
802aa3d1faeSChuck Lever 	buf = container_of(buffer, struct rpc_buffer, data);
803aa3d1faeSChuck Lever 	size = buf->len;
804c5a4dd8bSChuck Lever 
805215d0678SGeert Uytterhoeven 	dprintk("RPC:       freeing buffer of size %zu at %p\n",
806c5a4dd8bSChuck Lever 			size, buf);
807aa3d1faeSChuck Lever 
808c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
809c5a4dd8bSChuck Lever 		mempool_free(buf, rpc_buffer_mempool);
8101da177e4SLinus Torvalds 	else
811c5a4dd8bSChuck Lever 		kfree(buf);
8121da177e4SLinus Torvalds }
81312444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds /*
8161da177e4SLinus Torvalds  * Creation and deletion of RPC task structures
8171da177e4SLinus Torvalds  */
81884115e1cSTrond Myklebust void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	memset(task, 0, sizeof(*task));
821b24b8a24SPavel Emelyanov 	setup_timer(&task->tk_timer, (void (*)(unsigned long))rpc_run_timer,
822b24b8a24SPavel Emelyanov 			(unsigned long)task);
82344c28873STrond Myklebust 	atomic_set(&task->tk_count, 1);
82484115e1cSTrond Myklebust 	task->tk_flags  = task_setup_data->flags;
82584115e1cSTrond Myklebust 	task->tk_ops = task_setup_data->callback_ops;
82684115e1cSTrond Myklebust 	task->tk_calldata = task_setup_data->callback_data;
8276529eba0STrond Myklebust 	INIT_LIST_HEAD(&task->tk_task);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	/* Initialize retry counters */
8301da177e4SLinus Torvalds 	task->tk_garb_retry = 2;
8311da177e4SLinus Torvalds 	task->tk_cred_retry = 2;
8321da177e4SLinus Torvalds 
833*3ff7576dSTrond Myklebust 	task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
834*3ff7576dSTrond Myklebust 	task->tk_owner = current->tgid;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	/* Initialize workqueue for async tasks */
8371da177e4SLinus Torvalds 	task->tk_workqueue = rpciod_workqueue;
8381da177e4SLinus Torvalds 
83984115e1cSTrond Myklebust 	task->tk_client = task_setup_data->rpc_client;
84084115e1cSTrond Myklebust 	if (task->tk_client != NULL) {
84184115e1cSTrond Myklebust 		kref_get(&task->tk_client->cl_kref);
84284115e1cSTrond Myklebust 		if (task->tk_client->cl_softrtry)
8431da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_SOFT;
84484115e1cSTrond Myklebust 		if (!task->tk_client->cl_intr)
8451da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_NOINTR;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 
84884115e1cSTrond Myklebust 	if (task->tk_ops->rpc_call_prepare != NULL)
84984115e1cSTrond Myklebust 		task->tk_action = rpc_prepare_task;
850963d8fe5STrond Myklebust 
851ef759a2eSChuck Lever 	/* starting timestamp */
852ef759a2eSChuck Lever 	task->tk_start = jiffies;
853ef759a2eSChuck Lever 
85446121cf7SChuck Lever 	dprintk("RPC:       new task initialized, procpid %u\n",
855ba25f9dcSPavel Emelyanov 				task_pid_nr(current));
8561da177e4SLinus Torvalds }
857e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_task);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds static struct rpc_task *
8601da177e4SLinus Torvalds rpc_alloc_task(void)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8658aca67f0STrond Myklebust static void rpc_free_task(struct rcu_head *rcu)
8661da177e4SLinus Torvalds {
8678aca67f0STrond Myklebust 	struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
86846121cf7SChuck Lever 	dprintk("RPC: %5u freeing task\n", task->tk_pid);
8691da177e4SLinus Torvalds 	mempool_free(task, rpc_task_mempool);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /*
87390c5755fSTrond Myklebust  * Create a new task for the specified client.
8741da177e4SLinus Torvalds  */
87584115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds 	struct rpc_task	*task;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	task = rpc_alloc_task();
8801da177e4SLinus Torvalds 	if (!task)
88190c5755fSTrond Myklebust 		goto out;
8821da177e4SLinus Torvalds 
88384115e1cSTrond Myklebust 	rpc_init_task(task, setup_data);
8841da177e4SLinus Torvalds 
88546121cf7SChuck Lever 	dprintk("RPC:       allocated task %p\n", task);
8861da177e4SLinus Torvalds 	task->tk_flags |= RPC_TASK_DYNAMIC;
8871da177e4SLinus Torvalds out:
8881da177e4SLinus Torvalds 	return task;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891e6b3c4dbSTrond Myklebust 
892e6b3c4dbSTrond Myklebust void rpc_put_task(struct rpc_task *task)
8931da177e4SLinus Torvalds {
894963d8fe5STrond Myklebust 	const struct rpc_call_ops *tk_ops = task->tk_ops;
895963d8fe5STrond Myklebust 	void *calldata = task->tk_calldata;
8961da177e4SLinus Torvalds 
897e6b3c4dbSTrond Myklebust 	if (!atomic_dec_and_test(&task->tk_count))
898e6b3c4dbSTrond Myklebust 		return;
899e6b3c4dbSTrond Myklebust 	/* Release resources */
900e6b3c4dbSTrond Myklebust 	if (task->tk_rqstp)
901e6b3c4dbSTrond Myklebust 		xprt_release(task);
902e6b3c4dbSTrond Myklebust 	if (task->tk_msg.rpc_cred)
903e6b3c4dbSTrond Myklebust 		rpcauth_unbindcred(task);
904e6b3c4dbSTrond Myklebust 	if (task->tk_client) {
905e6b3c4dbSTrond Myklebust 		rpc_release_client(task->tk_client);
906e6b3c4dbSTrond Myklebust 		task->tk_client = NULL;
907e6b3c4dbSTrond Myklebust 	}
908e6b3c4dbSTrond Myklebust 	if (task->tk_flags & RPC_TASK_DYNAMIC)
9098aca67f0STrond Myklebust 		call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
910bbd5a1f9STrond Myklebust 	rpc_release_calldata(tk_ops, calldata);
911e6b3c4dbSTrond Myklebust }
912e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task);
913e6b3c4dbSTrond Myklebust 
914bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task)
915e6b3c4dbSTrond Myklebust {
9161da177e4SLinus Torvalds #ifdef RPC_DEBUG
9171da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
9181da177e4SLinus Torvalds #endif
91946121cf7SChuck Lever 	dprintk("RPC: %5u release task\n", task->tk_pid);
9201da177e4SLinus Torvalds 
9216529eba0STrond Myklebust 	if (!list_empty(&task->tk_task)) {
9224bef61ffSTrond Myklebust 		struct rpc_clnt *clnt = task->tk_client;
9236529eba0STrond Myklebust 		/* Remove from client task list */
9244bef61ffSTrond Myklebust 		spin_lock(&clnt->cl_lock);
9251da177e4SLinus Torvalds 		list_del(&task->tk_task);
9264bef61ffSTrond Myklebust 		spin_unlock(&clnt->cl_lock);
9276529eba0STrond Myklebust 	}
9281da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	/* Synchronously delete any running timer */
9311da177e4SLinus Torvalds 	rpc_delete_timer(task);
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds #ifdef RPC_DEBUG
9341da177e4SLinus Torvalds 	task->tk_magic = 0;
9351da177e4SLinus Torvalds #endif
936e6b3c4dbSTrond Myklebust 	/* Wake up anyone who is waiting for task completion */
937e6b3c4dbSTrond Myklebust 	rpc_mark_complete_task(task);
938e6b3c4dbSTrond Myklebust 
939e6b3c4dbSTrond Myklebust 	rpc_put_task(task);
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds /*
9431da177e4SLinus Torvalds  * Kill all tasks for the given client.
9441da177e4SLinus Torvalds  * XXX: kill their descendants as well?
9451da177e4SLinus Torvalds  */
9464bef61ffSTrond Myklebust void rpc_killall_tasks(struct rpc_clnt *clnt)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	struct rpc_task	*rovr;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 
9514bef61ffSTrond Myklebust 	if (list_empty(&clnt->cl_tasks))
9524bef61ffSTrond Myklebust 		return;
9534bef61ffSTrond Myklebust 	dprintk("RPC:       killing all tasks for client %p\n", clnt);
9544bef61ffSTrond Myklebust 	/*
9554bef61ffSTrond Myklebust 	 * Spin lock all_tasks to prevent changes...
9564bef61ffSTrond Myklebust 	 */
9574bef61ffSTrond Myklebust 	spin_lock(&clnt->cl_lock);
9584bef61ffSTrond Myklebust 	list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
9591da177e4SLinus Torvalds 		if (! RPC_IS_ACTIVATED(rovr))
9601da177e4SLinus Torvalds 			continue;
9616529eba0STrond Myklebust 		if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
9621da177e4SLinus Torvalds 			rovr->tk_flags |= RPC_TASK_KILLED;
9631da177e4SLinus Torvalds 			rpc_exit(rovr, -EIO);
9641da177e4SLinus Torvalds 			rpc_wake_up_task(rovr);
9651da177e4SLinus Torvalds 		}
9661da177e4SLinus Torvalds 	}
9674bef61ffSTrond Myklebust 	spin_unlock(&clnt->cl_lock);
9681da177e4SLinus Torvalds }
969e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_killall_tasks);
9701da177e4SLinus Torvalds 
971b247bbf1STrond Myklebust int rpciod_up(void)
972b247bbf1STrond Myklebust {
973b247bbf1STrond Myklebust 	return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
974b247bbf1STrond Myklebust }
975b247bbf1STrond Myklebust 
976b247bbf1STrond Myklebust void rpciod_down(void)
977b247bbf1STrond Myklebust {
978b247bbf1STrond Myklebust 	module_put(THIS_MODULE);
979b247bbf1STrond Myklebust }
980b247bbf1STrond Myklebust 
9811da177e4SLinus Torvalds /*
982b247bbf1STrond Myklebust  * Start up the rpciod workqueue.
9831da177e4SLinus Torvalds  */
984b247bbf1STrond Myklebust static int rpciod_start(void)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	struct workqueue_struct *wq;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	/*
9891da177e4SLinus Torvalds 	 * Create the rpciod thread and wait for it to start.
9901da177e4SLinus Torvalds 	 */
991ab418d70STrond Myklebust 	dprintk("RPC:       creating workqueue rpciod\n");
9921da177e4SLinus Torvalds 	wq = create_workqueue("rpciod");
9931da177e4SLinus Torvalds 	rpciod_workqueue = wq;
994b247bbf1STrond Myklebust 	return rpciod_workqueue != NULL;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
997b247bbf1STrond Myklebust static void rpciod_stop(void)
9981da177e4SLinus Torvalds {
999b247bbf1STrond Myklebust 	struct workqueue_struct *wq = NULL;
1000ab418d70STrond Myklebust 
1001b247bbf1STrond Myklebust 	if (rpciod_workqueue == NULL)
1002b247bbf1STrond Myklebust 		return;
1003ab418d70STrond Myklebust 	dprintk("RPC:       destroying workqueue rpciod\n");
10041da177e4SLinus Torvalds 
1005b247bbf1STrond Myklebust 	wq = rpciod_workqueue;
10061da177e4SLinus Torvalds 	rpciod_workqueue = NULL;
1007b247bbf1STrond Myklebust 	destroy_workqueue(wq);
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds void
10111da177e4SLinus Torvalds rpc_destroy_mempool(void)
10121da177e4SLinus Torvalds {
1013b247bbf1STrond Myklebust 	rpciod_stop();
10141da177e4SLinus Torvalds 	if (rpc_buffer_mempool)
10151da177e4SLinus Torvalds 		mempool_destroy(rpc_buffer_mempool);
10161da177e4SLinus Torvalds 	if (rpc_task_mempool)
10171da177e4SLinus Torvalds 		mempool_destroy(rpc_task_mempool);
10181a1d92c1SAlexey Dobriyan 	if (rpc_task_slabp)
10191a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_task_slabp);
10201a1d92c1SAlexey Dobriyan 	if (rpc_buffer_slabp)
10211a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_buffer_slabp);
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds int
10251da177e4SLinus Torvalds rpc_init_mempool(void)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	rpc_task_slabp = kmem_cache_create("rpc_tasks",
10281da177e4SLinus Torvalds 					     sizeof(struct rpc_task),
10291da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
103020c2df83SPaul Mundt 					     NULL);
10311da177e4SLinus Torvalds 	if (!rpc_task_slabp)
10321da177e4SLinus Torvalds 		goto err_nomem;
10331da177e4SLinus Torvalds 	rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
10341da177e4SLinus Torvalds 					     RPC_BUFFER_MAXSIZE,
10351da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
103620c2df83SPaul Mundt 					     NULL);
10371da177e4SLinus Torvalds 	if (!rpc_buffer_slabp)
10381da177e4SLinus Torvalds 		goto err_nomem;
103993d2341cSMatthew Dobson 	rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
10401da177e4SLinus Torvalds 						    rpc_task_slabp);
10411da177e4SLinus Torvalds 	if (!rpc_task_mempool)
10421da177e4SLinus Torvalds 		goto err_nomem;
104393d2341cSMatthew Dobson 	rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
10441da177e4SLinus Torvalds 						      rpc_buffer_slabp);
10451da177e4SLinus Torvalds 	if (!rpc_buffer_mempool)
10461da177e4SLinus Torvalds 		goto err_nomem;
1047b247bbf1STrond Myklebust 	if (!rpciod_start())
1048b247bbf1STrond Myklebust 		goto err_nomem;
10491da177e4SLinus Torvalds 	return 0;
10501da177e4SLinus Torvalds err_nomem:
10511da177e4SLinus Torvalds 	rpc_destroy_mempool();
10521da177e4SLinus Torvalds 	return -ENOMEM;
10531da177e4SLinus Torvalds }
1054