xref: /linux/net/sunrpc/sched.c (revision f6a1cc89309f0ae847a9b6fe418d1c4215e5bc55)
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 
4165f27f38SDavid Howells static void			rpc_async_schedule(struct work_struct *);
42bde8f00cSTrond Myklebust static void			 rpc_release_task(struct rpc_task *task);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * RPC tasks sit here while waiting for conditions to improve.
461da177e4SLinus Torvalds  */
47a4a87499STrond Myklebust static struct rpc_wait_queue delay_queue;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * rpciod-related stuff
511da177e4SLinus Torvalds  */
5224c5d9d7STrond Myklebust struct workqueue_struct *rpciod_workqueue;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * Disable the timer for a given RPC task. Should be called with
561da177e4SLinus Torvalds  * queue->lock and bh_disabled in order to avoid races within
571da177e4SLinus Torvalds  * rpc_run_timer().
581da177e4SLinus Torvalds  */
595d00837bSTrond Myklebust static void
601da177e4SLinus Torvalds __rpc_disable_timer(struct rpc_task *task)
611da177e4SLinus Torvalds {
6246121cf7SChuck Lever 	dprintk("RPC: %5u disabling timer\n", task->tk_pid);
631da177e4SLinus Torvalds 	task->tk_timeout = 0;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * Set up a timer for the current task.
681da177e4SLinus Torvalds  */
695d00837bSTrond Myklebust static void
705d00837bSTrond Myklebust __rpc_add_timer(struct rpc_task *task)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	if (!task->tk_timeout)
731da177e4SLinus Torvalds 		return;
741da177e4SLinus Torvalds 
7546121cf7SChuck Lever 	dprintk("RPC: %5u setting alarm for %lu ms\n",
761da177e4SLinus Torvalds 			task->tk_pid, task->tk_timeout * 1000 / HZ);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
791da177e4SLinus Torvalds 	mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /*
831da177e4SLinus Torvalds  * Delete any timer for the current task. Because we use del_timer_sync(),
841da177e4SLinus Torvalds  * this function should never be called while holding queue->lock.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds static void
871da177e4SLinus Torvalds rpc_delete_timer(struct rpc_task *task)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	if (RPC_IS_QUEUED(task))
901da177e4SLinus Torvalds 		return;
911da177e4SLinus Torvalds 	if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
921da177e4SLinus Torvalds 		del_singleshot_timer_sync(&task->tk_timer);
9346121cf7SChuck Lever 		dprintk("RPC: %5u deleting timer\n", task->tk_pid);
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds  * Add new request to a priority queue.
991da177e4SLinus Torvalds  */
1001da177e4SLinus Torvalds static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct list_head *q;
1031da177e4SLinus Torvalds 	struct rpc_task *t;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	INIT_LIST_HEAD(&task->u.tk_wait.links);
1061da177e4SLinus Torvalds 	q = &queue->tasks[task->tk_priority];
1071da177e4SLinus Torvalds 	if (unlikely(task->tk_priority > queue->maxpriority))
1081da177e4SLinus Torvalds 		q = &queue->tasks[queue->maxpriority];
1091da177e4SLinus Torvalds 	list_for_each_entry(t, q, u.tk_wait.list) {
1103ff7576dSTrond Myklebust 		if (t->tk_owner == task->tk_owner) {
1111da177e4SLinus Torvalds 			list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
1121da177e4SLinus Torvalds 			return;
1131da177e4SLinus Torvalds 		}
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 	list_add_tail(&task->u.tk_wait.list, q);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /*
1191da177e4SLinus Torvalds  * Add new request to wait queue.
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  * Swapper tasks always get inserted at the head of the queue.
1221da177e4SLinus Torvalds  * This should avoid many nasty memory deadlocks and hopefully
1231da177e4SLinus Torvalds  * improve overall performance.
1241da177e4SLinus Torvalds  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1311da177e4SLinus Torvalds 		__rpc_add_wait_queue_priority(queue, task);
1321da177e4SLinus Torvalds 	else if (RPC_IS_SWAPPER(task))
1331da177e4SLinus Torvalds 		list_add(&task->u.tk_wait.list, &queue->tasks[0]);
1341da177e4SLinus Torvalds 	else
1351da177e4SLinus Torvalds 		list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
13696ef13b2STrond Myklebust 	task->tk_waitqueue = queue;
137e19b63daSChuck Lever 	queue->qlen++;
1381da177e4SLinus Torvalds 	rpc_set_queued(task);
1391da177e4SLinus Torvalds 
14046121cf7SChuck Lever 	dprintk("RPC: %5u added to queue %p \"%s\"\n",
1411da177e4SLinus Torvalds 			task->tk_pid, queue, rpc_qname(queue));
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * Remove request from a priority queue.
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct rpc_task *t;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	if (!list_empty(&task->u.tk_wait.links)) {
1521da177e4SLinus Torvalds 		t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
1531da177e4SLinus Torvalds 		list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
1541da177e4SLinus Torvalds 		list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 	list_del(&task->u.tk_wait.list);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /*
1601da177e4SLinus Torvalds  * Remove request from queue.
1611da177e4SLinus Torvalds  * Note: must be called with spin lock held.
1621da177e4SLinus Torvalds  */
16396ef13b2STrond Myklebust static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1661da177e4SLinus Torvalds 		__rpc_remove_wait_queue_priority(task);
1671da177e4SLinus Torvalds 	else
1681da177e4SLinus Torvalds 		list_del(&task->u.tk_wait.list);
169e19b63daSChuck Lever 	queue->qlen--;
17046121cf7SChuck Lever 	dprintk("RPC: %5u removed from queue %p \"%s\"\n",
1711da177e4SLinus Torvalds 			task->tk_pid, queue, rpc_qname(queue));
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	queue->priority = priority;
1771da177e4SLinus Torvalds 	queue->count = 1 << (priority * 2);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1803ff7576dSTrond Myklebust static inline void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
1811da177e4SLinus Torvalds {
1823ff7576dSTrond Myklebust 	queue->owner = pid;
1831da177e4SLinus Torvalds 	queue->nr = RPC_BATCH_COUNT;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, queue->maxpriority);
1893ff7576dSTrond Myklebust 	rpc_set_waitqueue_owner(queue, 0);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1923ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	int i;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	spin_lock_init(&queue->lock);
1971da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
1981da177e4SLinus Torvalds 		INIT_LIST_HEAD(&queue->tasks[i]);
1993ff7576dSTrond Myklebust 	queue->maxpriority = nr_queues - 1;
2001da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
2011da177e4SLinus Torvalds #ifdef RPC_DEBUG
2021da177e4SLinus Torvalds 	queue->name = qname;
2031da177e4SLinus Torvalds #endif
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2071da177e4SLinus Torvalds {
2083ff7576dSTrond Myklebust 	__rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2121da177e4SLinus Torvalds {
2133ff7576dSTrond Myklebust 	__rpc_init_priority_wait_queue(queue, qname, 1);
2141da177e4SLinus Torvalds }
215e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
2161da177e4SLinus Torvalds 
217*f6a1cc89STrond Myklebust void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
218*f6a1cc89STrond Myklebust {
219*f6a1cc89STrond Myklebust }
220*f6a1cc89STrond Myklebust EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
221*f6a1cc89STrond Myklebust 
222150030b7SMatthew Wilcox static int rpc_wait_bit_killable(void *word)
22344c28873STrond Myklebust {
224150030b7SMatthew Wilcox 	if (fatal_signal_pending(current))
22544c28873STrond Myklebust 		return -ERESTARTSYS;
22644c28873STrond Myklebust 	schedule();
22744c28873STrond Myklebust 	return 0;
22844c28873STrond Myklebust }
22944c28873STrond Myklebust 
230c44fe705STrond Myklebust #ifdef RPC_DEBUG
231c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task)
232c44fe705STrond Myklebust {
233c44fe705STrond Myklebust 	static atomic_t rpc_pid;
234c44fe705STrond Myklebust 
235c44fe705STrond Myklebust 	task->tk_magic = RPC_TASK_MAGIC_ID;
236c44fe705STrond Myklebust 	task->tk_pid = atomic_inc_return(&rpc_pid);
237c44fe705STrond Myklebust }
238c44fe705STrond Myklebust #else
239c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task)
240c44fe705STrond Myklebust {
241c44fe705STrond Myklebust }
242c44fe705STrond Myklebust #endif
243c44fe705STrond Myklebust 
244e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task)
245e6b3c4dbSTrond Myklebust {
2464bef61ffSTrond Myklebust 	struct rpc_clnt *clnt;
247e6b3c4dbSTrond Myklebust 	if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
248e6b3c4dbSTrond Myklebust 		return;
249c44fe705STrond Myklebust 	rpc_task_set_debuginfo(task);
250e6b3c4dbSTrond Myklebust 	/* Add to global list of all tasks */
2514bef61ffSTrond Myklebust 	clnt = task->tk_client;
2524bef61ffSTrond Myklebust 	if (clnt != NULL) {
2534bef61ffSTrond Myklebust 		spin_lock(&clnt->cl_lock);
2544bef61ffSTrond Myklebust 		list_add_tail(&task->tk_task, &clnt->cl_tasks);
2554bef61ffSTrond Myklebust 		spin_unlock(&clnt->cl_lock);
2564bef61ffSTrond Myklebust 	}
257e6b3c4dbSTrond Myklebust }
258e6b3c4dbSTrond Myklebust 
25944c28873STrond Myklebust /*
26044c28873STrond Myklebust  * Mark an RPC call as having completed by clearing the 'active' bit
26144c28873STrond Myklebust  */
262e6b3c4dbSTrond Myklebust static void rpc_mark_complete_task(struct rpc_task *task)
26344c28873STrond Myklebust {
264e6b3c4dbSTrond Myklebust 	smp_mb__before_clear_bit();
265e6b3c4dbSTrond Myklebust 	clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
266e6b3c4dbSTrond Myklebust 	smp_mb__after_clear_bit();
26744c28873STrond Myklebust 	wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
26844c28873STrond Myklebust }
26944c28873STrond Myklebust 
27044c28873STrond Myklebust /*
27144c28873STrond Myklebust  * Allow callers to wait for completion of an RPC call
27244c28873STrond Myklebust  */
27344c28873STrond Myklebust int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
27444c28873STrond Myklebust {
27544c28873STrond Myklebust 	if (action == NULL)
276150030b7SMatthew Wilcox 		action = rpc_wait_bit_killable;
27744c28873STrond Myklebust 	return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
278150030b7SMatthew Wilcox 			action, TASK_KILLABLE);
27944c28873STrond Myklebust }
280e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
28144c28873STrond Myklebust 
2821da177e4SLinus Torvalds /*
2831da177e4SLinus Torvalds  * Make an RPC task runnable.
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * Note: If the task is ASYNC, this must be called with
2861da177e4SLinus Torvalds  * the spinlock held to protect the wait queue operation.
2871da177e4SLinus Torvalds  */
2881da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	rpc_clear_queued(task);
291cc4dc59eSChristophe Saout 	if (rpc_test_and_set_running(task))
2921da177e4SLinus Torvalds 		return;
293cc4dc59eSChristophe Saout 	/* We might have raced */
294cc4dc59eSChristophe Saout 	if (RPC_IS_QUEUED(task)) {
295cc4dc59eSChristophe Saout 		rpc_clear_running(task);
296cc4dc59eSChristophe Saout 		return;
297cc4dc59eSChristophe Saout 	}
2981da177e4SLinus Torvalds 	if (RPC_IS_ASYNC(task)) {
2991da177e4SLinus Torvalds 		int status;
3001da177e4SLinus Torvalds 
30165f27f38SDavid Howells 		INIT_WORK(&task->u.tk_work, rpc_async_schedule);
30232bfb5c0STrond Myklebust 		status = queue_work(rpciod_workqueue, &task->u.tk_work);
3031da177e4SLinus Torvalds 		if (status < 0) {
3041da177e4SLinus Torvalds 			printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
3051da177e4SLinus Torvalds 			task->tk_status = status;
3061da177e4SLinus Torvalds 			return;
3071da177e4SLinus Torvalds 		}
3081da177e4SLinus Torvalds 	} else
30996651ab3STrond Myklebust 		wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /*
3131da177e4SLinus Torvalds  * Prepare for sleeping on a wait queue.
3141da177e4SLinus Torvalds  * By always appending tasks to the list we ensure FIFO behavior.
3151da177e4SLinus Torvalds  * NB: An RPC task will only receive interrupt-driven events as long
3161da177e4SLinus Torvalds  * as it's on a wait queue.
3171da177e4SLinus Torvalds  */
3181da177e4SLinus Torvalds static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3195d00837bSTrond Myklebust 			rpc_action action)
3201da177e4SLinus Torvalds {
32146121cf7SChuck Lever 	dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
32246121cf7SChuck Lever 			task->tk_pid, rpc_qname(q), jiffies);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
3251da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
3261da177e4SLinus Torvalds 		return;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	__rpc_add_wait_queue(q, task);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	BUG_ON(task->tk_callback != NULL);
3321da177e4SLinus Torvalds 	task->tk_callback = action;
3335d00837bSTrond Myklebust 	__rpc_add_timer(task);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3375d00837bSTrond Myklebust 				rpc_action action)
3381da177e4SLinus Torvalds {
339e6b3c4dbSTrond Myklebust 	/* Mark the task as being activated if so needed */
340e6b3c4dbSTrond Myklebust 	rpc_set_active(task);
341e6b3c4dbSTrond Myklebust 
3421da177e4SLinus Torvalds 	/*
3431da177e4SLinus Torvalds 	 * Protect the queue operations.
3441da177e4SLinus Torvalds 	 */
3451da177e4SLinus Torvalds 	spin_lock_bh(&q->lock);
3465d00837bSTrond Myklebust 	__rpc_sleep_on(q, task, action);
3471da177e4SLinus Torvalds 	spin_unlock_bh(&q->lock);
3481da177e4SLinus Torvalds }
349e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /**
3521da177e4SLinus Torvalds  * __rpc_do_wake_up_task - wake up a single rpc_task
35396ef13b2STrond Myklebust  * @queue: wait queue
3541da177e4SLinus Torvalds  * @task: task to be woken up
3551da177e4SLinus Torvalds  *
3561da177e4SLinus Torvalds  * Caller must hold queue->lock, and have cleared the task queued flag.
3571da177e4SLinus Torvalds  */
35896ef13b2STrond Myklebust static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
3591da177e4SLinus Torvalds {
36046121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
36146121cf7SChuck Lever 			task->tk_pid, jiffies);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds #ifdef RPC_DEBUG
3641da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 	/* Has the task been executed yet? If not, we cannot wake it up! */
3671da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task)) {
3681da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
3691da177e4SLinus Torvalds 		return;
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	__rpc_disable_timer(task);
37396ef13b2STrond Myklebust 	__rpc_remove_wait_queue(queue, task);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	rpc_make_runnable(task);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	dprintk("RPC:       __rpc_wake_up_task done\n");
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /*
38196ef13b2STrond Myklebust  * Wake up a queued task while the queue lock is being held
3821da177e4SLinus Torvalds  */
38396ef13b2STrond Myklebust static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
3841da177e4SLinus Torvalds {
38596ef13b2STrond Myklebust 	if (!RPC_IS_QUEUED(task) || task->tk_waitqueue != queue)
38696ef13b2STrond Myklebust 		return;
3871da177e4SLinus Torvalds 	if (rpc_start_wakeup(task)) {
38896ef13b2STrond Myklebust 			__rpc_do_wake_up_task(queue, task);
3891da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*
39496ef13b2STrond Myklebust  * Wake up a task on a specific queue
39596ef13b2STrond Myklebust  */
39696ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
39796ef13b2STrond Myklebust {
39896ef13b2STrond Myklebust 	rcu_read_lock_bh();
39996ef13b2STrond Myklebust 	spin_lock(&queue->lock);
40096ef13b2STrond Myklebust 	rpc_wake_up_task_queue_locked(queue, task);
40196ef13b2STrond Myklebust 	spin_unlock(&queue->lock);
40296ef13b2STrond Myklebust 	rcu_read_unlock_bh();
40396ef13b2STrond Myklebust }
40496ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
40596ef13b2STrond Myklebust 
40696ef13b2STrond Myklebust /*
4071da177e4SLinus Torvalds  * Wake up the specified task
4081da177e4SLinus Torvalds  */
409fda13939STrond Myklebust static void rpc_wake_up_task(struct rpc_task *task)
4101da177e4SLinus Torvalds {
41196ef13b2STrond Myklebust 	rpc_wake_up_queued_task(task->tk_waitqueue, task);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * Wake up the next task on a priority queue.
4161da177e4SLinus Torvalds  */
4171da177e4SLinus Torvalds static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	struct list_head *q;
4201da177e4SLinus Torvalds 	struct rpc_task *task;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	/*
4233ff7576dSTrond Myklebust 	 * Service a batch of tasks from a single owner.
4241da177e4SLinus Torvalds 	 */
4251da177e4SLinus Torvalds 	q = &queue->tasks[queue->priority];
4261da177e4SLinus Torvalds 	if (!list_empty(q)) {
4271da177e4SLinus Torvalds 		task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4283ff7576dSTrond Myklebust 		if (queue->owner == task->tk_owner) {
4291da177e4SLinus Torvalds 			if (--queue->nr)
4301da177e4SLinus Torvalds 				goto out;
4311da177e4SLinus Torvalds 			list_move_tail(&task->u.tk_wait.list, q);
4321da177e4SLinus Torvalds 		}
4331da177e4SLinus Torvalds 		/*
4341da177e4SLinus Torvalds 		 * Check if we need to switch queues.
4351da177e4SLinus Torvalds 		 */
4361da177e4SLinus Torvalds 		if (--queue->count)
4373ff7576dSTrond Myklebust 			goto new_owner;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	/*
4411da177e4SLinus Torvalds 	 * Service the next queue.
4421da177e4SLinus Torvalds 	 */
4431da177e4SLinus Torvalds 	do {
4441da177e4SLinus Torvalds 		if (q == &queue->tasks[0])
4451da177e4SLinus Torvalds 			q = &queue->tasks[queue->maxpriority];
4461da177e4SLinus Torvalds 		else
4471da177e4SLinus Torvalds 			q = q - 1;
4481da177e4SLinus Torvalds 		if (!list_empty(q)) {
4491da177e4SLinus Torvalds 			task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4501da177e4SLinus Torvalds 			goto new_queue;
4511da177e4SLinus Torvalds 		}
4521da177e4SLinus Torvalds 	} while (q != &queue->tasks[queue->priority]);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
4551da177e4SLinus Torvalds 	return NULL;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds new_queue:
4581da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
4593ff7576dSTrond Myklebust new_owner:
4603ff7576dSTrond Myklebust 	rpc_set_waitqueue_owner(queue, task->tk_owner);
4611da177e4SLinus Torvalds out:
46296ef13b2STrond Myklebust 	rpc_wake_up_task_queue_locked(queue, task);
4631da177e4SLinus Torvalds 	return task;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds /*
4671da177e4SLinus Torvalds  * Wake up the next task on the wait queue.
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	struct rpc_task	*task = NULL;
4721da177e4SLinus Torvalds 
47346121cf7SChuck Lever 	dprintk("RPC:       wake_up_next(%p \"%s\")\n",
47446121cf7SChuck Lever 			queue, rpc_qname(queue));
4758aca67f0STrond Myklebust 	rcu_read_lock_bh();
4768aca67f0STrond Myklebust 	spin_lock(&queue->lock);
4771da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
4781da177e4SLinus Torvalds 		task = __rpc_wake_up_next_priority(queue);
4791da177e4SLinus Torvalds 	else {
4801da177e4SLinus Torvalds 		task_for_first(task, &queue->tasks[0])
48196ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
4821da177e4SLinus Torvalds 	}
4838aca67f0STrond Myklebust 	spin_unlock(&queue->lock);
4848aca67f0STrond Myklebust 	rcu_read_unlock_bh();
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	return task;
4871da177e4SLinus Torvalds }
488e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /**
4911da177e4SLinus Torvalds  * rpc_wake_up - wake up all rpc_tasks
4921da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
4931da177e4SLinus Torvalds  *
4941da177e4SLinus Torvalds  * Grabs queue->lock
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue)
4971da177e4SLinus Torvalds {
498e6d83d55STrond Myklebust 	struct rpc_task *task, *next;
4991da177e4SLinus Torvalds 	struct list_head *head;
500e6d83d55STrond Myklebust 
5018aca67f0STrond Myklebust 	rcu_read_lock_bh();
5028aca67f0STrond Myklebust 	spin_lock(&queue->lock);
5031da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5041da177e4SLinus Torvalds 	for (;;) {
505e6d83d55STrond Myklebust 		list_for_each_entry_safe(task, next, head, u.tk_wait.list)
50696ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
5071da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5081da177e4SLinus Torvalds 			break;
5091da177e4SLinus Torvalds 		head--;
5101da177e4SLinus Torvalds 	}
5118aca67f0STrond Myklebust 	spin_unlock(&queue->lock);
5128aca67f0STrond Myklebust 	rcu_read_unlock_bh();
5131da177e4SLinus Torvalds }
514e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /**
5171da177e4SLinus Torvalds  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
5181da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5191da177e4SLinus Torvalds  * @status: status value to set
5201da177e4SLinus Torvalds  *
5211da177e4SLinus Torvalds  * Grabs queue->lock
5221da177e4SLinus Torvalds  */
5231da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
5241da177e4SLinus Torvalds {
525e6d83d55STrond Myklebust 	struct rpc_task *task, *next;
5261da177e4SLinus Torvalds 	struct list_head *head;
5271da177e4SLinus Torvalds 
5288aca67f0STrond Myklebust 	rcu_read_lock_bh();
5298aca67f0STrond Myklebust 	spin_lock(&queue->lock);
5301da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5311da177e4SLinus Torvalds 	for (;;) {
532e6d83d55STrond Myklebust 		list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
5331da177e4SLinus Torvalds 			task->tk_status = status;
53496ef13b2STrond Myklebust 			rpc_wake_up_task_queue_locked(queue, task);
5351da177e4SLinus Torvalds 		}
5361da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5371da177e4SLinus Torvalds 			break;
5381da177e4SLinus Torvalds 		head--;
5391da177e4SLinus Torvalds 	}
5408aca67f0STrond Myklebust 	spin_unlock(&queue->lock);
5418aca67f0STrond Myklebust 	rcu_read_unlock_bh();
5421da177e4SLinus Torvalds }
543e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status);
5441da177e4SLinus Torvalds 
545fde95c75STrond Myklebust /*
546fde95c75STrond Myklebust  * Run a timeout function.
547fde95c75STrond Myklebust  */
548fde95c75STrond Myklebust static void rpc_run_timer(unsigned long ptr)
549fde95c75STrond Myklebust {
550fde95c75STrond Myklebust 	struct rpc_task *task = (struct rpc_task *)ptr;
55196ef13b2STrond Myklebust 	struct rpc_wait_queue *queue = task->tk_waitqueue;
552fde95c75STrond Myklebust 
553fde95c75STrond Myklebust 	spin_lock(&queue->lock);
5545d00837bSTrond Myklebust 	if (RPC_IS_QUEUED(task) && task->tk_waitqueue == queue) {
5555d00837bSTrond Myklebust 		dprintk("RPC: %5u timeout\n", task->tk_pid);
5565d00837bSTrond Myklebust 		task->tk_status = -ETIMEDOUT;
55796ef13b2STrond Myklebust 		rpc_wake_up_task_queue_locked(queue, task);
558fde95c75STrond Myklebust 	}
5595d00837bSTrond Myklebust 	spin_unlock(&queue->lock);
560fde95c75STrond Myklebust 	smp_mb__before_clear_bit();
561fde95c75STrond Myklebust 	clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
562fde95c75STrond Myklebust 	smp_mb__after_clear_bit();
563fde95c75STrond Myklebust }
564fde95c75STrond Myklebust 
5658014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task)
5668014793bSTrond Myklebust {
5675d00837bSTrond Myklebust 	task->tk_status = 0;
5688014793bSTrond Myklebust }
5698014793bSTrond Myklebust 
5701da177e4SLinus Torvalds /*
5711da177e4SLinus Torvalds  * Run a task at a later time
5721da177e4SLinus Torvalds  */
5738014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	task->tk_timeout = delay;
5765d00837bSTrond Myklebust 	rpc_sleep_on(&delay_queue, task, __rpc_atrun);
5771da177e4SLinus Torvalds }
578e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds /*
5814ce70adaSTrond Myklebust  * Helper to call task->tk_ops->rpc_call_prepare
5824ce70adaSTrond Myklebust  */
5834ce70adaSTrond Myklebust static void rpc_prepare_task(struct rpc_task *task)
5844ce70adaSTrond Myklebust {
5856d5fcb5aSTrond Myklebust 	lock_kernel();
5864ce70adaSTrond Myklebust 	task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
5876d5fcb5aSTrond Myklebust 	unlock_kernel();
5884ce70adaSTrond Myklebust }
5894ce70adaSTrond Myklebust 
5904ce70adaSTrond Myklebust /*
591963d8fe5STrond Myklebust  * Helper that calls task->tk_ops->rpc_call_done if it exists
592d05fdb0cSTrond Myklebust  */
593abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task)
594d05fdb0cSTrond Myklebust {
595abbcf28fSTrond Myklebust 	task->tk_action = NULL;
596963d8fe5STrond Myklebust 	if (task->tk_ops->rpc_call_done != NULL) {
5976d5fcb5aSTrond Myklebust 		lock_kernel();
598963d8fe5STrond Myklebust 		task->tk_ops->rpc_call_done(task, task->tk_calldata);
5996d5fcb5aSTrond Myklebust 		unlock_kernel();
600d05fdb0cSTrond Myklebust 		if (task->tk_action != NULL) {
601abbcf28fSTrond Myklebust 			WARN_ON(RPC_ASSASSINATED(task));
602abbcf28fSTrond Myklebust 			/* Always release the RPC slot and buffer memory */
603d05fdb0cSTrond Myklebust 			xprt_release(task);
604d05fdb0cSTrond Myklebust 		}
605d05fdb0cSTrond Myklebust 	}
606d05fdb0cSTrond Myklebust }
607e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit_task);
608d05fdb0cSTrond Myklebust 
609bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
610bbd5a1f9STrond Myklebust {
611bbd5a1f9STrond Myklebust 	if (ops->rpc_release != NULL) {
612bbd5a1f9STrond Myklebust 		lock_kernel();
613bbd5a1f9STrond Myklebust 		ops->rpc_release(calldata);
614bbd5a1f9STrond Myklebust 		unlock_kernel();
615bbd5a1f9STrond Myklebust 	}
616bbd5a1f9STrond Myklebust }
617bbd5a1f9STrond Myklebust 
618d05fdb0cSTrond Myklebust /*
6191da177e4SLinus Torvalds  * This is the RPC `scheduler' (or rather, the finite state machine).
6201da177e4SLinus Torvalds  */
6212efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	int		status = 0;
6241da177e4SLinus Torvalds 
62546121cf7SChuck Lever 	dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
6261da177e4SLinus Torvalds 			task->tk_pid, task->tk_flags);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	BUG_ON(RPC_IS_QUEUED(task));
6291da177e4SLinus Torvalds 
630d05fdb0cSTrond Myklebust 	for (;;) {
6311da177e4SLinus Torvalds 		/*
6321da177e4SLinus Torvalds 		 * Garbage collection of pending timers...
6331da177e4SLinus Torvalds 		 */
6341da177e4SLinus Torvalds 		rpc_delete_timer(task);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 		/*
6371da177e4SLinus Torvalds 		 * Execute any pending callback.
6381da177e4SLinus Torvalds 		 */
6391da177e4SLinus Torvalds 		if (RPC_DO_CALLBACK(task)) {
6401da177e4SLinus Torvalds 			/* Define a callback save pointer */
6411da177e4SLinus Torvalds 			void (*save_callback)(struct rpc_task *);
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 			/*
6441da177e4SLinus Torvalds 			 * If a callback exists, save it, reset it,
6451da177e4SLinus Torvalds 			 * call it.
6461da177e4SLinus Torvalds 			 * The save is needed to stop from resetting
6471da177e4SLinus Torvalds 			 * another callback set within the callback handler
6481da177e4SLinus Torvalds 			 * - Dave
6491da177e4SLinus Torvalds 			 */
6501da177e4SLinus Torvalds 			save_callback=task->tk_callback;
6511da177e4SLinus Torvalds 			task->tk_callback=NULL;
6521da177e4SLinus Torvalds 			save_callback(task);
6531da177e4SLinus Torvalds 		}
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		/*
6561da177e4SLinus Torvalds 		 * Perform the next FSM step.
6571da177e4SLinus Torvalds 		 * tk_action may be NULL when the task has been killed
6581da177e4SLinus Torvalds 		 * by someone else.
6591da177e4SLinus Torvalds 		 */
6601da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task)) {
661abbcf28fSTrond Myklebust 			if (task->tk_action == NULL)
662abbcf28fSTrond Myklebust 				break;
6631da177e4SLinus Torvalds 			task->tk_action(task);
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 		/*
6671da177e4SLinus Torvalds 		 * Lockless check for whether task is sleeping or not.
6681da177e4SLinus Torvalds 		 */
6691da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task))
6701da177e4SLinus Torvalds 			continue;
6711da177e4SLinus Torvalds 		rpc_clear_running(task);
6721da177e4SLinus Torvalds 		if (RPC_IS_ASYNC(task)) {
6731da177e4SLinus Torvalds 			/* Careful! we may have raced... */
6741da177e4SLinus Torvalds 			if (RPC_IS_QUEUED(task))
6752efef837STrond Myklebust 				return;
6761da177e4SLinus Torvalds 			if (rpc_test_and_set_running(task))
6772efef837STrond Myklebust 				return;
6781da177e4SLinus Torvalds 			continue;
6791da177e4SLinus Torvalds 		}
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 		/* sync task: sleep here */
68246121cf7SChuck Lever 		dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
68396651ab3STrond Myklebust 		status = out_of_line_wait_on_bit(&task->tk_runstate,
684150030b7SMatthew Wilcox 				RPC_TASK_QUEUED, rpc_wait_bit_killable,
685150030b7SMatthew Wilcox 				TASK_KILLABLE);
68696651ab3STrond Myklebust 		if (status == -ERESTARTSYS) {
6871da177e4SLinus Torvalds 			/*
6881da177e4SLinus Torvalds 			 * When a sync task receives a signal, it exits with
6891da177e4SLinus Torvalds 			 * -ERESTARTSYS. In order to catch any callbacks that
6901da177e4SLinus Torvalds 			 * clean up after sleeping on some queue, we don't
6911da177e4SLinus Torvalds 			 * break the loop here, but go around once more.
6921da177e4SLinus Torvalds 			 */
69346121cf7SChuck Lever 			dprintk("RPC: %5u got signal\n", task->tk_pid);
6941da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_KILLED;
6951da177e4SLinus Torvalds 			rpc_exit(task, -ERESTARTSYS);
6961da177e4SLinus Torvalds 			rpc_wake_up_task(task);
6971da177e4SLinus Torvalds 		}
6981da177e4SLinus Torvalds 		rpc_set_running(task);
69946121cf7SChuck Lever 		dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
70246121cf7SChuck Lever 	dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
70346121cf7SChuck Lever 			task->tk_status);
7041da177e4SLinus Torvalds 	/* Release all resources associated with the task */
7051da177e4SLinus Torvalds 	rpc_release_task(task);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /*
7091da177e4SLinus Torvalds  * User-visible entry point to the scheduler.
7101da177e4SLinus Torvalds  *
7111da177e4SLinus Torvalds  * This may be called recursively if e.g. an async NFS task updates
7121da177e4SLinus Torvalds  * the attributes and finds that dirty pages must be flushed.
7131da177e4SLinus Torvalds  * NOTE: Upon exit of this function the task is guaranteed to be
7141da177e4SLinus Torvalds  *	 released. In particular note that tk_release() will have
7151da177e4SLinus Torvalds  *	 been called, so your task memory may have been freed.
7161da177e4SLinus Torvalds  */
7172efef837STrond Myklebust void rpc_execute(struct rpc_task *task)
7181da177e4SLinus Torvalds {
71944c28873STrond Myklebust 	rpc_set_active(task);
7201da177e4SLinus Torvalds 	rpc_set_running(task);
7212efef837STrond Myklebust 	__rpc_execute(task);
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
72465f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work)
7251da177e4SLinus Torvalds {
72665f27f38SDavid Howells 	__rpc_execute(container_of(work, struct rpc_task, u.tk_work));
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
729aa3d1faeSChuck Lever struct rpc_buffer {
730aa3d1faeSChuck Lever 	size_t	len;
731aa3d1faeSChuck Lever 	char	data[];
732aa3d1faeSChuck Lever };
733aa3d1faeSChuck Lever 
73402107148SChuck Lever /**
73502107148SChuck Lever  * rpc_malloc - allocate an RPC buffer
73602107148SChuck Lever  * @task: RPC task that will use this buffer
73702107148SChuck Lever  * @size: requested byte size
7381da177e4SLinus Torvalds  *
739c5a4dd8bSChuck Lever  * To prevent rpciod from hanging, this allocator never sleeps,
740c5a4dd8bSChuck Lever  * returning NULL if the request cannot be serviced immediately.
741c5a4dd8bSChuck Lever  * The caller can arrange to sleep in a way that is safe for rpciod.
742c5a4dd8bSChuck Lever  *
743c5a4dd8bSChuck Lever  * Most requests are 'small' (under 2KiB) and can be serviced from a
744c5a4dd8bSChuck Lever  * mempool, ensuring that NFS reads and writes can always proceed,
745c5a4dd8bSChuck Lever  * and that there is good locality of reference for these buffers.
746c5a4dd8bSChuck Lever  *
7471da177e4SLinus Torvalds  * In order to avoid memory starvation triggering more writebacks of
748c5a4dd8bSChuck Lever  * NFS requests, we avoid using GFP_KERNEL.
7491da177e4SLinus Torvalds  */
75002107148SChuck Lever void *rpc_malloc(struct rpc_task *task, size_t size)
7511da177e4SLinus Torvalds {
752aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
753c5a4dd8bSChuck Lever 	gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
7541da177e4SLinus Torvalds 
755aa3d1faeSChuck Lever 	size += sizeof(struct rpc_buffer);
756c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
757c5a4dd8bSChuck Lever 		buf = mempool_alloc(rpc_buffer_mempool, gfp);
7581da177e4SLinus Torvalds 	else
759c5a4dd8bSChuck Lever 		buf = kmalloc(size, gfp);
760ddce40dfSPeter Zijlstra 
761ddce40dfSPeter Zijlstra 	if (!buf)
762ddce40dfSPeter Zijlstra 		return NULL;
763ddce40dfSPeter Zijlstra 
764aa3d1faeSChuck Lever 	buf->len = size;
765215d0678SGeert Uytterhoeven 	dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
766c5a4dd8bSChuck Lever 			task->tk_pid, size, buf);
767aa3d1faeSChuck Lever 	return &buf->data;
7681da177e4SLinus Torvalds }
76912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc);
7701da177e4SLinus Torvalds 
77102107148SChuck Lever /**
77202107148SChuck Lever  * rpc_free - free buffer allocated via rpc_malloc
773c5a4dd8bSChuck Lever  * @buffer: buffer to free
77402107148SChuck Lever  *
77502107148SChuck Lever  */
776c5a4dd8bSChuck Lever void rpc_free(void *buffer)
7771da177e4SLinus Torvalds {
778aa3d1faeSChuck Lever 	size_t size;
779aa3d1faeSChuck Lever 	struct rpc_buffer *buf;
78002107148SChuck Lever 
781c5a4dd8bSChuck Lever 	if (!buffer)
782c5a4dd8bSChuck Lever 		return;
783aa3d1faeSChuck Lever 
784aa3d1faeSChuck Lever 	buf = container_of(buffer, struct rpc_buffer, data);
785aa3d1faeSChuck Lever 	size = buf->len;
786c5a4dd8bSChuck Lever 
787215d0678SGeert Uytterhoeven 	dprintk("RPC:       freeing buffer of size %zu at %p\n",
788c5a4dd8bSChuck Lever 			size, buf);
789aa3d1faeSChuck Lever 
790c5a4dd8bSChuck Lever 	if (size <= RPC_BUFFER_MAXSIZE)
791c5a4dd8bSChuck Lever 		mempool_free(buf, rpc_buffer_mempool);
7921da177e4SLinus Torvalds 	else
793c5a4dd8bSChuck Lever 		kfree(buf);
7941da177e4SLinus Torvalds }
79512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds /*
7981da177e4SLinus Torvalds  * Creation and deletion of RPC task structures
7991da177e4SLinus Torvalds  */
80047fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	memset(task, 0, sizeof(*task));
803fde95c75STrond Myklebust 	setup_timer(&task->tk_timer, rpc_run_timer, (unsigned long)task);
80444c28873STrond Myklebust 	atomic_set(&task->tk_count, 1);
80584115e1cSTrond Myklebust 	task->tk_flags  = task_setup_data->flags;
80684115e1cSTrond Myklebust 	task->tk_ops = task_setup_data->callback_ops;
80784115e1cSTrond Myklebust 	task->tk_calldata = task_setup_data->callback_data;
8086529eba0STrond Myklebust 	INIT_LIST_HEAD(&task->tk_task);
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	/* Initialize retry counters */
8111da177e4SLinus Torvalds 	task->tk_garb_retry = 2;
8121da177e4SLinus Torvalds 	task->tk_cred_retry = 2;
8131da177e4SLinus Torvalds 
8143ff7576dSTrond Myklebust 	task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
8153ff7576dSTrond Myklebust 	task->tk_owner = current->tgid;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/* Initialize workqueue for async tasks */
81832bfb5c0STrond Myklebust 	task->tk_workqueue = task_setup_data->workqueue;
8191da177e4SLinus Torvalds 
82084115e1cSTrond Myklebust 	task->tk_client = task_setup_data->rpc_client;
82184115e1cSTrond Myklebust 	if (task->tk_client != NULL) {
82284115e1cSTrond Myklebust 		kref_get(&task->tk_client->cl_kref);
82384115e1cSTrond Myklebust 		if (task->tk_client->cl_softrtry)
8241da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_SOFT;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 
82784115e1cSTrond Myklebust 	if (task->tk_ops->rpc_call_prepare != NULL)
82884115e1cSTrond Myklebust 		task->tk_action = rpc_prepare_task;
829963d8fe5STrond Myklebust 
830b3ef8b3bSTrond Myklebust 	if (task_setup_data->rpc_message != NULL) {
831b3ef8b3bSTrond Myklebust 		memcpy(&task->tk_msg, task_setup_data->rpc_message, sizeof(task->tk_msg));
832b3ef8b3bSTrond Myklebust 		/* Bind the user cred */
833b3ef8b3bSTrond Myklebust 		if (task->tk_msg.rpc_cred != NULL)
834b3ef8b3bSTrond Myklebust 			rpcauth_holdcred(task);
835b3ef8b3bSTrond Myklebust 		else
836b3ef8b3bSTrond Myklebust 			rpcauth_bindcred(task);
837b3ef8b3bSTrond Myklebust 		if (task->tk_action == NULL)
838b3ef8b3bSTrond Myklebust 			rpc_call_start(task);
839b3ef8b3bSTrond Myklebust 	}
840b3ef8b3bSTrond Myklebust 
841ef759a2eSChuck Lever 	/* starting timestamp */
842ef759a2eSChuck Lever 	task->tk_start = jiffies;
843ef759a2eSChuck Lever 
84446121cf7SChuck Lever 	dprintk("RPC:       new task initialized, procpid %u\n",
845ba25f9dcSPavel Emelyanov 				task_pid_nr(current));
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds static struct rpc_task *
8491da177e4SLinus Torvalds rpc_alloc_task(void)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
85432bfb5c0STrond Myklebust static void rpc_free_task_rcu(struct rcu_head *rcu)
8551da177e4SLinus Torvalds {
8568aca67f0STrond Myklebust 	struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
85746121cf7SChuck Lever 	dprintk("RPC: %5u freeing task\n", task->tk_pid);
8581da177e4SLinus Torvalds 	mempool_free(task, rpc_task_mempool);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /*
86290c5755fSTrond Myklebust  * Create a new task for the specified client.
8631da177e4SLinus Torvalds  */
86484115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
8651da177e4SLinus Torvalds {
866e8f5d77cSTrond Myklebust 	struct rpc_task	*task = setup_data->task;
867e8f5d77cSTrond Myklebust 	unsigned short flags = 0;
8681da177e4SLinus Torvalds 
869e8f5d77cSTrond Myklebust 	if (task == NULL) {
8701da177e4SLinus Torvalds 		task = rpc_alloc_task();
871e8f5d77cSTrond Myklebust 		if (task == NULL)
87290c5755fSTrond Myklebust 			goto out;
873e8f5d77cSTrond Myklebust 		flags = RPC_TASK_DYNAMIC;
874e8f5d77cSTrond Myklebust 	}
8751da177e4SLinus Torvalds 
87684115e1cSTrond Myklebust 	rpc_init_task(task, setup_data);
8771da177e4SLinus Torvalds 
878e8f5d77cSTrond Myklebust 	task->tk_flags |= flags;
87946121cf7SChuck Lever 	dprintk("RPC:       allocated task %p\n", task);
8801da177e4SLinus Torvalds out:
8811da177e4SLinus Torvalds 	return task;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
88432bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task)
8851da177e4SLinus Torvalds {
886963d8fe5STrond Myklebust 	const struct rpc_call_ops *tk_ops = task->tk_ops;
887963d8fe5STrond Myklebust 	void *calldata = task->tk_calldata;
8881da177e4SLinus Torvalds 
88932bfb5c0STrond Myklebust 	if (task->tk_flags & RPC_TASK_DYNAMIC)
89032bfb5c0STrond Myklebust 		call_rcu_bh(&task->u.tk_rcu, rpc_free_task_rcu);
89132bfb5c0STrond Myklebust 	rpc_release_calldata(tk_ops, calldata);
89232bfb5c0STrond Myklebust }
89332bfb5c0STrond Myklebust 
89432bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work)
89532bfb5c0STrond Myklebust {
89632bfb5c0STrond Myklebust 	rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
89732bfb5c0STrond Myklebust }
89832bfb5c0STrond Myklebust 
89932bfb5c0STrond Myklebust void rpc_put_task(struct rpc_task *task)
90032bfb5c0STrond Myklebust {
901e6b3c4dbSTrond Myklebust 	if (!atomic_dec_and_test(&task->tk_count))
902e6b3c4dbSTrond Myklebust 		return;
903e6b3c4dbSTrond Myklebust 	/* Release resources */
904e6b3c4dbSTrond Myklebust 	if (task->tk_rqstp)
905e6b3c4dbSTrond Myklebust 		xprt_release(task);
906e6b3c4dbSTrond Myklebust 	if (task->tk_msg.rpc_cred)
907e6b3c4dbSTrond Myklebust 		rpcauth_unbindcred(task);
908e6b3c4dbSTrond Myklebust 	if (task->tk_client) {
909e6b3c4dbSTrond Myklebust 		rpc_release_client(task->tk_client);
910e6b3c4dbSTrond Myklebust 		task->tk_client = NULL;
911e6b3c4dbSTrond Myklebust 	}
91232bfb5c0STrond Myklebust 	if (task->tk_workqueue != NULL) {
91332bfb5c0STrond Myklebust 		INIT_WORK(&task->u.tk_work, rpc_async_release);
91432bfb5c0STrond Myklebust 		queue_work(task->tk_workqueue, &task->u.tk_work);
91532bfb5c0STrond Myklebust 	} else
91632bfb5c0STrond Myklebust 		rpc_free_task(task);
917e6b3c4dbSTrond Myklebust }
918e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task);
919e6b3c4dbSTrond Myklebust 
920bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task)
921e6b3c4dbSTrond Myklebust {
9221da177e4SLinus Torvalds #ifdef RPC_DEBUG
9231da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
9241da177e4SLinus Torvalds #endif
92546121cf7SChuck Lever 	dprintk("RPC: %5u release task\n", task->tk_pid);
9261da177e4SLinus Torvalds 
9276529eba0STrond Myklebust 	if (!list_empty(&task->tk_task)) {
9284bef61ffSTrond Myklebust 		struct rpc_clnt *clnt = task->tk_client;
9296529eba0STrond Myklebust 		/* Remove from client task list */
9304bef61ffSTrond Myklebust 		spin_lock(&clnt->cl_lock);
9311da177e4SLinus Torvalds 		list_del(&task->tk_task);
9324bef61ffSTrond Myklebust 		spin_unlock(&clnt->cl_lock);
9336529eba0STrond Myklebust 	}
9341da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/* Synchronously delete any running timer */
9371da177e4SLinus Torvalds 	rpc_delete_timer(task);
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds #ifdef RPC_DEBUG
9401da177e4SLinus Torvalds 	task->tk_magic = 0;
9411da177e4SLinus Torvalds #endif
942e6b3c4dbSTrond Myklebust 	/* Wake up anyone who is waiting for task completion */
943e6b3c4dbSTrond Myklebust 	rpc_mark_complete_task(task);
944e6b3c4dbSTrond Myklebust 
945e6b3c4dbSTrond Myklebust 	rpc_put_task(task);
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds /*
9491da177e4SLinus Torvalds  * Kill all tasks for the given client.
9501da177e4SLinus Torvalds  * XXX: kill their descendants as well?
9511da177e4SLinus Torvalds  */
9524bef61ffSTrond Myklebust void rpc_killall_tasks(struct rpc_clnt *clnt)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	struct rpc_task	*rovr;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 
9574bef61ffSTrond Myklebust 	if (list_empty(&clnt->cl_tasks))
9584bef61ffSTrond Myklebust 		return;
9594bef61ffSTrond Myklebust 	dprintk("RPC:       killing all tasks for client %p\n", clnt);
9604bef61ffSTrond Myklebust 	/*
9614bef61ffSTrond Myklebust 	 * Spin lock all_tasks to prevent changes...
9624bef61ffSTrond Myklebust 	 */
9634bef61ffSTrond Myklebust 	spin_lock(&clnt->cl_lock);
9644bef61ffSTrond Myklebust 	list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
9651da177e4SLinus Torvalds 		if (! RPC_IS_ACTIVATED(rovr))
9661da177e4SLinus Torvalds 			continue;
9676529eba0STrond Myklebust 		if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
9681da177e4SLinus Torvalds 			rovr->tk_flags |= RPC_TASK_KILLED;
9691da177e4SLinus Torvalds 			rpc_exit(rovr, -EIO);
9701da177e4SLinus Torvalds 			rpc_wake_up_task(rovr);
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 	}
9734bef61ffSTrond Myklebust 	spin_unlock(&clnt->cl_lock);
9741da177e4SLinus Torvalds }
975e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_killall_tasks);
9761da177e4SLinus Torvalds 
977b247bbf1STrond Myklebust int rpciod_up(void)
978b247bbf1STrond Myklebust {
979b247bbf1STrond Myklebust 	return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
980b247bbf1STrond Myklebust }
981b247bbf1STrond Myklebust 
982b247bbf1STrond Myklebust void rpciod_down(void)
983b247bbf1STrond Myklebust {
984b247bbf1STrond Myklebust 	module_put(THIS_MODULE);
985b247bbf1STrond Myklebust }
986b247bbf1STrond Myklebust 
9871da177e4SLinus Torvalds /*
988b247bbf1STrond Myklebust  * Start up the rpciod workqueue.
9891da177e4SLinus Torvalds  */
990b247bbf1STrond Myklebust static int rpciod_start(void)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	struct workqueue_struct *wq;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	/*
9951da177e4SLinus Torvalds 	 * Create the rpciod thread and wait for it to start.
9961da177e4SLinus Torvalds 	 */
997ab418d70STrond Myklebust 	dprintk("RPC:       creating workqueue rpciod\n");
9981da177e4SLinus Torvalds 	wq = create_workqueue("rpciod");
9991da177e4SLinus Torvalds 	rpciod_workqueue = wq;
1000b247bbf1STrond Myklebust 	return rpciod_workqueue != NULL;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
1003b247bbf1STrond Myklebust static void rpciod_stop(void)
10041da177e4SLinus Torvalds {
1005b247bbf1STrond Myklebust 	struct workqueue_struct *wq = NULL;
1006ab418d70STrond Myklebust 
1007b247bbf1STrond Myklebust 	if (rpciod_workqueue == NULL)
1008b247bbf1STrond Myklebust 		return;
1009ab418d70STrond Myklebust 	dprintk("RPC:       destroying workqueue rpciod\n");
10101da177e4SLinus Torvalds 
1011b247bbf1STrond Myklebust 	wq = rpciod_workqueue;
10121da177e4SLinus Torvalds 	rpciod_workqueue = NULL;
1013b247bbf1STrond Myklebust 	destroy_workqueue(wq);
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds void
10171da177e4SLinus Torvalds rpc_destroy_mempool(void)
10181da177e4SLinus Torvalds {
1019b247bbf1STrond Myklebust 	rpciod_stop();
10201da177e4SLinus Torvalds 	if (rpc_buffer_mempool)
10211da177e4SLinus Torvalds 		mempool_destroy(rpc_buffer_mempool);
10221da177e4SLinus Torvalds 	if (rpc_task_mempool)
10231da177e4SLinus Torvalds 		mempool_destroy(rpc_task_mempool);
10241a1d92c1SAlexey Dobriyan 	if (rpc_task_slabp)
10251a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_task_slabp);
10261a1d92c1SAlexey Dobriyan 	if (rpc_buffer_slabp)
10271a1d92c1SAlexey Dobriyan 		kmem_cache_destroy(rpc_buffer_slabp);
1028*f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&delay_queue);
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds int
10321da177e4SLinus Torvalds rpc_init_mempool(void)
10331da177e4SLinus Torvalds {
1034*f6a1cc89STrond Myklebust 	/*
1035*f6a1cc89STrond Myklebust 	 * The following is not strictly a mempool initialisation,
1036*f6a1cc89STrond Myklebust 	 * but there is no harm in doing it here
1037*f6a1cc89STrond Myklebust 	 */
1038*f6a1cc89STrond Myklebust 	rpc_init_wait_queue(&delay_queue, "delayq");
1039*f6a1cc89STrond Myklebust 	if (!rpciod_start())
1040*f6a1cc89STrond Myklebust 		goto err_nomem;
1041*f6a1cc89STrond Myklebust 
10421da177e4SLinus Torvalds 	rpc_task_slabp = kmem_cache_create("rpc_tasks",
10431da177e4SLinus Torvalds 					     sizeof(struct rpc_task),
10441da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
104520c2df83SPaul Mundt 					     NULL);
10461da177e4SLinus Torvalds 	if (!rpc_task_slabp)
10471da177e4SLinus Torvalds 		goto err_nomem;
10481da177e4SLinus Torvalds 	rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
10491da177e4SLinus Torvalds 					     RPC_BUFFER_MAXSIZE,
10501da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
105120c2df83SPaul Mundt 					     NULL);
10521da177e4SLinus Torvalds 	if (!rpc_buffer_slabp)
10531da177e4SLinus Torvalds 		goto err_nomem;
105493d2341cSMatthew Dobson 	rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
10551da177e4SLinus Torvalds 						    rpc_task_slabp);
10561da177e4SLinus Torvalds 	if (!rpc_task_mempool)
10571da177e4SLinus Torvalds 		goto err_nomem;
105893d2341cSMatthew Dobson 	rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
10591da177e4SLinus Torvalds 						      rpc_buffer_slabp);
10601da177e4SLinus Torvalds 	if (!rpc_buffer_mempool)
10611da177e4SLinus Torvalds 		goto err_nomem;
10621da177e4SLinus Torvalds 	return 0;
10631da177e4SLinus Torvalds err_nomem:
10641da177e4SLinus Torvalds 	rpc_destroy_mempool();
10651da177e4SLinus Torvalds 	return -ENOMEM;
10661da177e4SLinus Torvalds }
1067