xref: /linux/net/sunrpc/sched.c (revision 02107148349f31eee7c0fb06fd7a880df73dbd20)
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>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
231da177e4SLinus Torvalds #include <linux/sunrpc/xprt.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 static int			rpc_task_id;
291da177e4SLinus Torvalds #endif
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds  * RPC slabs and memory pools
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE	(2048)
351da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE	(8)
361da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE	(8)
37ba89966cSEric Dumazet static kmem_cache_t	*rpc_task_slabp __read_mostly;
38ba89966cSEric Dumazet static kmem_cache_t	*rpc_buffer_slabp __read_mostly;
39ba89966cSEric Dumazet static mempool_t	*rpc_task_mempool __read_mostly;
40ba89966cSEric Dumazet static mempool_t	*rpc_buffer_mempool __read_mostly;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static void			__rpc_default_timer(struct rpc_task *task);
431da177e4SLinus Torvalds static void			rpciod_killall(void);
441da177e4SLinus Torvalds static void			rpc_async_schedule(void *);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds  * RPC tasks that create another task (e.g. for contacting the portmapper)
481da177e4SLinus Torvalds  * will wait on this queue for their child's completion
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds static RPC_WAITQ(childq, "childq");
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * RPC tasks sit here while waiting for conditions to improve.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds static RPC_WAITQ(delay_queue, "delayq");
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * All RPC tasks are linked into this list
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds static LIST_HEAD(all_tasks);
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * rpciod-related stuff
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds static DECLARE_MUTEX(rpciod_sema);
661da177e4SLinus Torvalds static unsigned int		rpciod_users;
671da177e4SLinus Torvalds static struct workqueue_struct *rpciod_workqueue;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * Spinlock for other critical sections of code.
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_sched_lock);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * Disable the timer for a given RPC task. Should be called with
761da177e4SLinus Torvalds  * queue->lock and bh_disabled in order to avoid races within
771da177e4SLinus Torvalds  * rpc_run_timer().
781da177e4SLinus Torvalds  */
791da177e4SLinus Torvalds static inline void
801da177e4SLinus Torvalds __rpc_disable_timer(struct rpc_task *task)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	dprintk("RPC: %4d disabling timer\n", task->tk_pid);
831da177e4SLinus Torvalds 	task->tk_timeout_fn = NULL;
841da177e4SLinus Torvalds 	task->tk_timeout = 0;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /*
881da177e4SLinus Torvalds  * Run a timeout function.
891da177e4SLinus Torvalds  * We use the callback in order to allow __rpc_wake_up_task()
901da177e4SLinus Torvalds  * and friends to disable the timer synchronously on SMP systems
911da177e4SLinus Torvalds  * without calling del_timer_sync(). The latter could cause a
921da177e4SLinus Torvalds  * deadlock if called while we're holding spinlocks...
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds static void rpc_run_timer(struct rpc_task *task)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	void (*callback)(struct rpc_task *);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	callback = task->tk_timeout_fn;
991da177e4SLinus Torvalds 	task->tk_timeout_fn = NULL;
1001da177e4SLinus Torvalds 	if (callback && RPC_IS_QUEUED(task)) {
1011da177e4SLinus Torvalds 		dprintk("RPC: %4d running timer\n", task->tk_pid);
1021da177e4SLinus Torvalds 		callback(task);
1031da177e4SLinus Torvalds 	}
1041da177e4SLinus Torvalds 	smp_mb__before_clear_bit();
1051da177e4SLinus Torvalds 	clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
1061da177e4SLinus Torvalds 	smp_mb__after_clear_bit();
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * Set up a timer for the current task.
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds static inline void
1131da177e4SLinus Torvalds __rpc_add_timer(struct rpc_task *task, rpc_action timer)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	if (!task->tk_timeout)
1161da177e4SLinus Torvalds 		return;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	dprintk("RPC: %4d setting alarm for %lu ms\n",
1191da177e4SLinus Torvalds 			task->tk_pid, task->tk_timeout * 1000 / HZ);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	if (timer)
1221da177e4SLinus Torvalds 		task->tk_timeout_fn = timer;
1231da177e4SLinus Torvalds 	else
1241da177e4SLinus Torvalds 		task->tk_timeout_fn = __rpc_default_timer;
1251da177e4SLinus Torvalds 	set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
1261da177e4SLinus Torvalds 	mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  * Delete any timer for the current task. Because we use del_timer_sync(),
1311da177e4SLinus Torvalds  * this function should never be called while holding queue->lock.
1321da177e4SLinus Torvalds  */
1331da177e4SLinus Torvalds static void
1341da177e4SLinus Torvalds rpc_delete_timer(struct rpc_task *task)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	if (RPC_IS_QUEUED(task))
1371da177e4SLinus Torvalds 		return;
1381da177e4SLinus Torvalds 	if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
1391da177e4SLinus Torvalds 		del_singleshot_timer_sync(&task->tk_timer);
1401da177e4SLinus Torvalds 		dprintk("RPC: %4d deleting timer\n", task->tk_pid);
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * Add new request to a priority queue.
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct list_head *q;
1501da177e4SLinus Torvalds 	struct rpc_task *t;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&task->u.tk_wait.links);
1531da177e4SLinus Torvalds 	q = &queue->tasks[task->tk_priority];
1541da177e4SLinus Torvalds 	if (unlikely(task->tk_priority > queue->maxpriority))
1551da177e4SLinus Torvalds 		q = &queue->tasks[queue->maxpriority];
1561da177e4SLinus Torvalds 	list_for_each_entry(t, q, u.tk_wait.list) {
1571da177e4SLinus Torvalds 		if (t->tk_cookie == task->tk_cookie) {
1581da177e4SLinus Torvalds 			list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
1591da177e4SLinus Torvalds 			return;
1601da177e4SLinus Torvalds 		}
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 	list_add_tail(&task->u.tk_wait.list, q);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds /*
1661da177e4SLinus Torvalds  * Add new request to wait queue.
1671da177e4SLinus Torvalds  *
1681da177e4SLinus Torvalds  * Swapper tasks always get inserted at the head of the queue.
1691da177e4SLinus Torvalds  * This should avoid many nasty memory deadlocks and hopefully
1701da177e4SLinus Torvalds  * improve overall performance.
1711da177e4SLinus Torvalds  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1781da177e4SLinus Torvalds 		__rpc_add_wait_queue_priority(queue, task);
1791da177e4SLinus Torvalds 	else if (RPC_IS_SWAPPER(task))
1801da177e4SLinus Torvalds 		list_add(&task->u.tk_wait.list, &queue->tasks[0]);
1811da177e4SLinus Torvalds 	else
1821da177e4SLinus Torvalds 		list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
1831da177e4SLinus Torvalds 	task->u.tk_wait.rpc_waitq = queue;
1841da177e4SLinus Torvalds 	rpc_set_queued(task);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	dprintk("RPC: %4d added to queue %p \"%s\"\n",
1871da177e4SLinus Torvalds 				task->tk_pid, queue, rpc_qname(queue));
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Remove request from a priority queue.
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct rpc_task *t;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if (!list_empty(&task->u.tk_wait.links)) {
1981da177e4SLinus Torvalds 		t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
1991da177e4SLinus Torvalds 		list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
2001da177e4SLinus Torvalds 		list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 	list_del(&task->u.tk_wait.list);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  * Remove request from queue.
2071da177e4SLinus Torvalds  * Note: must be called with spin lock held.
2081da177e4SLinus Torvalds  */
2091da177e4SLinus Torvalds static void __rpc_remove_wait_queue(struct rpc_task *task)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct rpc_wait_queue *queue;
2121da177e4SLinus Torvalds 	queue = task->u.tk_wait.rpc_waitq;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
2151da177e4SLinus Torvalds 		__rpc_remove_wait_queue_priority(task);
2161da177e4SLinus Torvalds 	else
2171da177e4SLinus Torvalds 		list_del(&task->u.tk_wait.list);
2181da177e4SLinus Torvalds 	dprintk("RPC: %4d removed from queue %p \"%s\"\n",
2191da177e4SLinus Torvalds 				task->tk_pid, queue, rpc_qname(queue));
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	queue->priority = priority;
2251da177e4SLinus Torvalds 	queue->count = 1 << (priority * 2);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	queue->cookie = cookie;
2311da177e4SLinus Torvalds 	queue->nr = RPC_BATCH_COUNT;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, queue->maxpriority);
2371da177e4SLinus Torvalds 	rpc_set_waitqueue_cookie(queue, 0);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	int i;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	spin_lock_init(&queue->lock);
2451da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
2461da177e4SLinus Torvalds 		INIT_LIST_HEAD(&queue->tasks[i]);
2471da177e4SLinus Torvalds 	queue->maxpriority = maxprio;
2481da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
2491da177e4SLinus Torvalds #ifdef RPC_DEBUG
2501da177e4SLinus Torvalds 	queue->name = qname;
2511da177e4SLinus Torvalds #endif
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	__rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	__rpc_init_priority_wait_queue(queue, qname, 0);
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds EXPORT_SYMBOL(rpc_init_wait_queue);
2641da177e4SLinus Torvalds 
26544c28873STrond Myklebust static int rpc_wait_bit_interruptible(void *word)
26644c28873STrond Myklebust {
26744c28873STrond Myklebust 	if (signal_pending(current))
26844c28873STrond Myklebust 		return -ERESTARTSYS;
26944c28873STrond Myklebust 	schedule();
27044c28873STrond Myklebust 	return 0;
27144c28873STrond Myklebust }
27244c28873STrond Myklebust 
27344c28873STrond Myklebust /*
27444c28873STrond Myklebust  * Mark an RPC call as having completed by clearing the 'active' bit
27544c28873STrond Myklebust  */
27644c28873STrond Myklebust static inline void rpc_mark_complete_task(struct rpc_task *task)
27744c28873STrond Myklebust {
27844c28873STrond Myklebust 	rpc_clear_active(task);
27944c28873STrond Myklebust 	wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
28044c28873STrond Myklebust }
28144c28873STrond Myklebust 
28244c28873STrond Myklebust /*
28344c28873STrond Myklebust  * Allow callers to wait for completion of an RPC call
28444c28873STrond Myklebust  */
28544c28873STrond Myklebust int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
28644c28873STrond Myklebust {
28744c28873STrond Myklebust 	if (action == NULL)
28844c28873STrond Myklebust 		action = rpc_wait_bit_interruptible;
28944c28873STrond Myklebust 	return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
29044c28873STrond Myklebust 			action, TASK_INTERRUPTIBLE);
29144c28873STrond Myklebust }
29244c28873STrond Myklebust EXPORT_SYMBOL(__rpc_wait_for_completion_task);
29344c28873STrond Myklebust 
2941da177e4SLinus Torvalds /*
2951da177e4SLinus Torvalds  * Make an RPC task runnable.
2961da177e4SLinus Torvalds  *
2971da177e4SLinus Torvalds  * Note: If the task is ASYNC, this must be called with
2981da177e4SLinus Torvalds  * the spinlock held to protect the wait queue operation.
2991da177e4SLinus Torvalds  */
3001da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	int do_ret;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	BUG_ON(task->tk_timeout_fn);
3051da177e4SLinus Torvalds 	do_ret = rpc_test_and_set_running(task);
3061da177e4SLinus Torvalds 	rpc_clear_queued(task);
3071da177e4SLinus Torvalds 	if (do_ret)
3081da177e4SLinus Torvalds 		return;
3091da177e4SLinus Torvalds 	if (RPC_IS_ASYNC(task)) {
3101da177e4SLinus Torvalds 		int status;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
3131da177e4SLinus Torvalds 		status = queue_work(task->tk_workqueue, &task->u.tk_work);
3141da177e4SLinus Torvalds 		if (status < 0) {
3151da177e4SLinus Torvalds 			printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
3161da177e4SLinus Torvalds 			task->tk_status = status;
3171da177e4SLinus Torvalds 			return;
3181da177e4SLinus Torvalds 		}
3191da177e4SLinus Torvalds 	} else
32096651ab3STrond Myklebust 		wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /*
3241da177e4SLinus Torvalds  * Place a newly initialized task on the workqueue.
3251da177e4SLinus Torvalds  */
3261da177e4SLinus Torvalds static inline void
3271da177e4SLinus Torvalds rpc_schedule_run(struct rpc_task *task)
3281da177e4SLinus Torvalds {
32944c28873STrond Myklebust 	rpc_set_active(task);
3301da177e4SLinus Torvalds 	rpc_make_runnable(task);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /*
3341da177e4SLinus Torvalds  * Prepare for sleeping on a wait queue.
3351da177e4SLinus Torvalds  * By always appending tasks to the list we ensure FIFO behavior.
3361da177e4SLinus Torvalds  * NB: An RPC task will only receive interrupt-driven events as long
3371da177e4SLinus Torvalds  * as it's on a wait queue.
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3401da177e4SLinus Torvalds 			rpc_action action, rpc_action timer)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
3431da177e4SLinus Torvalds 				rpc_qname(q), jiffies);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
3461da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
3471da177e4SLinus Torvalds 		return;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	/* Mark the task as being activated if so needed */
35144c28873STrond Myklebust 	rpc_set_active(task);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	__rpc_add_wait_queue(q, task);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	BUG_ON(task->tk_callback != NULL);
3561da177e4SLinus Torvalds 	task->tk_callback = action;
3571da177e4SLinus Torvalds 	__rpc_add_timer(task, timer);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3611da177e4SLinus Torvalds 				rpc_action action, rpc_action timer)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	/*
3641da177e4SLinus Torvalds 	 * Protect the queue operations.
3651da177e4SLinus Torvalds 	 */
3661da177e4SLinus Torvalds 	spin_lock_bh(&q->lock);
3671da177e4SLinus Torvalds 	__rpc_sleep_on(q, task, action, timer);
3681da177e4SLinus Torvalds 	spin_unlock_bh(&q->lock);
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /**
3721da177e4SLinus Torvalds  * __rpc_do_wake_up_task - wake up a single rpc_task
3731da177e4SLinus Torvalds  * @task: task to be woken up
3741da177e4SLinus Torvalds  *
3751da177e4SLinus Torvalds  * Caller must hold queue->lock, and have cleared the task queued flag.
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds static void __rpc_do_wake_up_task(struct rpc_task *task)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds #ifdef RPC_DEBUG
3821da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
3831da177e4SLinus Torvalds #endif
3841da177e4SLinus Torvalds 	/* Has the task been executed yet? If not, we cannot wake it up! */
3851da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task)) {
3861da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
3871da177e4SLinus Torvalds 		return;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	__rpc_disable_timer(task);
3911da177e4SLinus Torvalds 	__rpc_remove_wait_queue(task);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	rpc_make_runnable(task);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	dprintk("RPC:      __rpc_wake_up_task done\n");
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  * Wake up the specified task
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds static void __rpc_wake_up_task(struct rpc_task *task)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	if (rpc_start_wakeup(task)) {
4041da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(task))
4051da177e4SLinus Torvalds 			__rpc_do_wake_up_task(task);
4061da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  * Default timeout handler if none specified by user
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds static void
4141da177e4SLinus Torvalds __rpc_default_timer(struct rpc_task *task)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
4171da177e4SLinus Torvalds 	task->tk_status = -ETIMEDOUT;
4181da177e4SLinus Torvalds 	rpc_wake_up_task(task);
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds /*
4221da177e4SLinus Torvalds  * Wake up the specified task
4231da177e4SLinus Torvalds  */
4241da177e4SLinus Torvalds void rpc_wake_up_task(struct rpc_task *task)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	if (rpc_start_wakeup(task)) {
4271da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(task)) {
4281da177e4SLinus Torvalds 			struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 			spin_lock_bh(&queue->lock);
4311da177e4SLinus Torvalds 			__rpc_do_wake_up_task(task);
4321da177e4SLinus Torvalds 			spin_unlock_bh(&queue->lock);
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*
4391da177e4SLinus Torvalds  * Wake up the next task on a priority queue.
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	struct list_head *q;
4441da177e4SLinus Torvalds 	struct rpc_task *task;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/*
4471da177e4SLinus Torvalds 	 * Service a batch of tasks from a single cookie.
4481da177e4SLinus Torvalds 	 */
4491da177e4SLinus Torvalds 	q = &queue->tasks[queue->priority];
4501da177e4SLinus Torvalds 	if (!list_empty(q)) {
4511da177e4SLinus Torvalds 		task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4521da177e4SLinus Torvalds 		if (queue->cookie == task->tk_cookie) {
4531da177e4SLinus Torvalds 			if (--queue->nr)
4541da177e4SLinus Torvalds 				goto out;
4551da177e4SLinus Torvalds 			list_move_tail(&task->u.tk_wait.list, q);
4561da177e4SLinus Torvalds 		}
4571da177e4SLinus Torvalds 		/*
4581da177e4SLinus Torvalds 		 * Check if we need to switch queues.
4591da177e4SLinus Torvalds 		 */
4601da177e4SLinus Torvalds 		if (--queue->count)
4611da177e4SLinus Torvalds 			goto new_cookie;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	/*
4651da177e4SLinus Torvalds 	 * Service the next queue.
4661da177e4SLinus Torvalds 	 */
4671da177e4SLinus Torvalds 	do {
4681da177e4SLinus Torvalds 		if (q == &queue->tasks[0])
4691da177e4SLinus Torvalds 			q = &queue->tasks[queue->maxpriority];
4701da177e4SLinus Torvalds 		else
4711da177e4SLinus Torvalds 			q = q - 1;
4721da177e4SLinus Torvalds 		if (!list_empty(q)) {
4731da177e4SLinus Torvalds 			task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4741da177e4SLinus Torvalds 			goto new_queue;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 	} while (q != &queue->tasks[queue->priority]);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
4791da177e4SLinus Torvalds 	return NULL;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds new_queue:
4821da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
4831da177e4SLinus Torvalds new_cookie:
4841da177e4SLinus Torvalds 	rpc_set_waitqueue_cookie(queue, task->tk_cookie);
4851da177e4SLinus Torvalds out:
4861da177e4SLinus Torvalds 	__rpc_wake_up_task(task);
4871da177e4SLinus Torvalds 	return task;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /*
4911da177e4SLinus Torvalds  * Wake up the next task on the wait queue.
4921da177e4SLinus Torvalds  */
4931da177e4SLinus Torvalds struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct rpc_task	*task = NULL;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	dprintk("RPC:      wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
4981da177e4SLinus Torvalds 	spin_lock_bh(&queue->lock);
4991da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
5001da177e4SLinus Torvalds 		task = __rpc_wake_up_next_priority(queue);
5011da177e4SLinus Torvalds 	else {
5021da177e4SLinus Torvalds 		task_for_first(task, &queue->tasks[0])
5031da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	spin_unlock_bh(&queue->lock);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	return task;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /**
5111da177e4SLinus Torvalds  * rpc_wake_up - wake up all rpc_tasks
5121da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5131da177e4SLinus Torvalds  *
5141da177e4SLinus Torvalds  * Grabs queue->lock
5151da177e4SLinus Torvalds  */
5161da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	struct rpc_task *task;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	struct list_head *head;
5211da177e4SLinus Torvalds 	spin_lock_bh(&queue->lock);
5221da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5231da177e4SLinus Torvalds 	for (;;) {
5241da177e4SLinus Torvalds 		while (!list_empty(head)) {
5251da177e4SLinus Torvalds 			task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
5261da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5271da177e4SLinus Torvalds 		}
5281da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5291da177e4SLinus Torvalds 			break;
5301da177e4SLinus Torvalds 		head--;
5311da177e4SLinus Torvalds 	}
5321da177e4SLinus Torvalds 	spin_unlock_bh(&queue->lock);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds /**
5361da177e4SLinus Torvalds  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
5371da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5381da177e4SLinus Torvalds  * @status: status value to set
5391da177e4SLinus Torvalds  *
5401da177e4SLinus Torvalds  * Grabs queue->lock
5411da177e4SLinus Torvalds  */
5421da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	struct list_head *head;
5451da177e4SLinus Torvalds 	struct rpc_task *task;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	spin_lock_bh(&queue->lock);
5481da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5491da177e4SLinus Torvalds 	for (;;) {
5501da177e4SLinus Torvalds 		while (!list_empty(head)) {
5511da177e4SLinus Torvalds 			task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
5521da177e4SLinus Torvalds 			task->tk_status = status;
5531da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5541da177e4SLinus Torvalds 		}
5551da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5561da177e4SLinus Torvalds 			break;
5571da177e4SLinus Torvalds 		head--;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 	spin_unlock_bh(&queue->lock);
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  * Run a task at a later time
5641da177e4SLinus Torvalds  */
5651da177e4SLinus Torvalds static void	__rpc_atrun(struct rpc_task *);
5661da177e4SLinus Torvalds void
5671da177e4SLinus Torvalds rpc_delay(struct rpc_task *task, unsigned long delay)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	task->tk_timeout = delay;
5701da177e4SLinus Torvalds 	rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds static void
5741da177e4SLinus Torvalds __rpc_atrun(struct rpc_task *task)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	task->tk_status = 0;
5771da177e4SLinus Torvalds 	rpc_wake_up_task(task);
5781da177e4SLinus Torvalds }
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 {
5854ce70adaSTrond Myklebust 	task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
5864ce70adaSTrond Myklebust }
5874ce70adaSTrond Myklebust 
5884ce70adaSTrond Myklebust /*
589963d8fe5STrond Myklebust  * Helper that calls task->tk_ops->rpc_call_done if it exists
590d05fdb0cSTrond Myklebust  */
591abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task)
592d05fdb0cSTrond Myklebust {
593abbcf28fSTrond Myklebust 	task->tk_action = NULL;
594963d8fe5STrond Myklebust 	if (task->tk_ops->rpc_call_done != NULL) {
595963d8fe5STrond Myklebust 		task->tk_ops->rpc_call_done(task, task->tk_calldata);
596d05fdb0cSTrond Myklebust 		if (task->tk_action != NULL) {
597abbcf28fSTrond Myklebust 			WARN_ON(RPC_ASSASSINATED(task));
598abbcf28fSTrond Myklebust 			/* Always release the RPC slot and buffer memory */
599d05fdb0cSTrond Myklebust 			xprt_release(task);
600d05fdb0cSTrond Myklebust 		}
601d05fdb0cSTrond Myklebust 	}
602d05fdb0cSTrond Myklebust }
603abbcf28fSTrond Myklebust EXPORT_SYMBOL(rpc_exit_task);
604d05fdb0cSTrond Myklebust 
605d05fdb0cSTrond Myklebust /*
6061da177e4SLinus Torvalds  * This is the RPC `scheduler' (or rather, the finite state machine).
6071da177e4SLinus Torvalds  */
6081da177e4SLinus Torvalds static int __rpc_execute(struct rpc_task *task)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	int		status = 0;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	dprintk("RPC: %4d rpc_execute flgs %x\n",
6131da177e4SLinus Torvalds 				task->tk_pid, task->tk_flags);
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	BUG_ON(RPC_IS_QUEUED(task));
6161da177e4SLinus Torvalds 
617d05fdb0cSTrond Myklebust 	for (;;) {
6181da177e4SLinus Torvalds 		/*
6191da177e4SLinus Torvalds 		 * Garbage collection of pending timers...
6201da177e4SLinus Torvalds 		 */
6211da177e4SLinus Torvalds 		rpc_delete_timer(task);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 		/*
6241da177e4SLinus Torvalds 		 * Execute any pending callback.
6251da177e4SLinus Torvalds 		 */
6261da177e4SLinus Torvalds 		if (RPC_DO_CALLBACK(task)) {
6271da177e4SLinus Torvalds 			/* Define a callback save pointer */
6281da177e4SLinus Torvalds 			void (*save_callback)(struct rpc_task *);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 			/*
6311da177e4SLinus Torvalds 			 * If a callback exists, save it, reset it,
6321da177e4SLinus Torvalds 			 * call it.
6331da177e4SLinus Torvalds 			 * The save is needed to stop from resetting
6341da177e4SLinus Torvalds 			 * another callback set within the callback handler
6351da177e4SLinus Torvalds 			 * - Dave
6361da177e4SLinus Torvalds 			 */
6371da177e4SLinus Torvalds 			save_callback=task->tk_callback;
6381da177e4SLinus Torvalds 			task->tk_callback=NULL;
6391da177e4SLinus Torvalds 			lock_kernel();
6401da177e4SLinus Torvalds 			save_callback(task);
6411da177e4SLinus Torvalds 			unlock_kernel();
6421da177e4SLinus Torvalds 		}
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 		/*
6451da177e4SLinus Torvalds 		 * Perform the next FSM step.
6461da177e4SLinus Torvalds 		 * tk_action may be NULL when the task has been killed
6471da177e4SLinus Torvalds 		 * by someone else.
6481da177e4SLinus Torvalds 		 */
6491da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task)) {
650abbcf28fSTrond Myklebust 			if (task->tk_action == NULL)
651abbcf28fSTrond Myklebust 				break;
6521da177e4SLinus Torvalds 			lock_kernel();
6531da177e4SLinus Torvalds 			task->tk_action(task);
6541da177e4SLinus Torvalds 			unlock_kernel();
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		/*
6581da177e4SLinus Torvalds 		 * Lockless check for whether task is sleeping or not.
6591da177e4SLinus Torvalds 		 */
6601da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task))
6611da177e4SLinus Torvalds 			continue;
6621da177e4SLinus Torvalds 		rpc_clear_running(task);
6631da177e4SLinus Torvalds 		if (RPC_IS_ASYNC(task)) {
6641da177e4SLinus Torvalds 			/* Careful! we may have raced... */
6651da177e4SLinus Torvalds 			if (RPC_IS_QUEUED(task))
6661da177e4SLinus Torvalds 				return 0;
6671da177e4SLinus Torvalds 			if (rpc_test_and_set_running(task))
6681da177e4SLinus Torvalds 				return 0;
6691da177e4SLinus Torvalds 			continue;
6701da177e4SLinus Torvalds 		}
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 		/* sync task: sleep here */
6731da177e4SLinus Torvalds 		dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
67496651ab3STrond Myklebust 		/* Note: Caller should be using rpc_clnt_sigmask() */
67596651ab3STrond Myklebust 		status = out_of_line_wait_on_bit(&task->tk_runstate,
67696651ab3STrond Myklebust 				RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
67796651ab3STrond Myklebust 				TASK_INTERRUPTIBLE);
67896651ab3STrond Myklebust 		if (status == -ERESTARTSYS) {
6791da177e4SLinus Torvalds 			/*
6801da177e4SLinus Torvalds 			 * When a sync task receives a signal, it exits with
6811da177e4SLinus Torvalds 			 * -ERESTARTSYS. In order to catch any callbacks that
6821da177e4SLinus Torvalds 			 * clean up after sleeping on some queue, we don't
6831da177e4SLinus Torvalds 			 * break the loop here, but go around once more.
6841da177e4SLinus Torvalds 			 */
6851da177e4SLinus Torvalds 			dprintk("RPC: %4d got signal\n", task->tk_pid);
6861da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_KILLED;
6871da177e4SLinus Torvalds 			rpc_exit(task, -ERESTARTSYS);
6881da177e4SLinus Torvalds 			rpc_wake_up_task(task);
6891da177e4SLinus Torvalds 		}
6901da177e4SLinus Torvalds 		rpc_set_running(task);
6911da177e4SLinus Torvalds 		dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
6921da177e4SLinus Torvalds 	}
6931da177e4SLinus Torvalds 
694e60859acSTrond Myklebust 	dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
69544c28873STrond Myklebust 	/* Wake up anyone who is waiting for task completion */
69644c28873STrond Myklebust 	rpc_mark_complete_task(task);
6971da177e4SLinus Torvalds 	/* Release all resources associated with the task */
6981da177e4SLinus Torvalds 	rpc_release_task(task);
6991da177e4SLinus Torvalds 	return status;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7031da177e4SLinus Torvalds  * User-visible entry point to the scheduler.
7041da177e4SLinus Torvalds  *
7051da177e4SLinus Torvalds  * This may be called recursively if e.g. an async NFS task updates
7061da177e4SLinus Torvalds  * the attributes and finds that dirty pages must be flushed.
7071da177e4SLinus Torvalds  * NOTE: Upon exit of this function the task is guaranteed to be
7081da177e4SLinus Torvalds  *	 released. In particular note that tk_release() will have
7091da177e4SLinus Torvalds  *	 been called, so your task memory may have been freed.
7101da177e4SLinus Torvalds  */
7111da177e4SLinus Torvalds int
7121da177e4SLinus Torvalds rpc_execute(struct rpc_task *task)
7131da177e4SLinus Torvalds {
71444c28873STrond Myklebust 	rpc_set_active(task);
7151da177e4SLinus Torvalds 	rpc_set_running(task);
7161da177e4SLinus Torvalds 	return __rpc_execute(task);
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds static void rpc_async_schedule(void *arg)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	__rpc_execute((struct rpc_task *)arg);
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
724*02107148SChuck Lever /**
725*02107148SChuck Lever  * rpc_malloc - allocate an RPC buffer
726*02107148SChuck Lever  * @task: RPC task that will use this buffer
727*02107148SChuck Lever  * @size: requested byte size
7281da177e4SLinus Torvalds  *
7291da177e4SLinus Torvalds  * We try to ensure that some NFS reads and writes can always proceed
7301da177e4SLinus Torvalds  * by using a mempool when allocating 'small' buffers.
7311da177e4SLinus Torvalds  * In order to avoid memory starvation triggering more writebacks of
7321da177e4SLinus Torvalds  * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
7331da177e4SLinus Torvalds  */
734*02107148SChuck Lever void * rpc_malloc(struct rpc_task *task, size_t size)
7351da177e4SLinus Torvalds {
736*02107148SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
737dd0fc66fSAl Viro 	gfp_t	gfp;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	if (task->tk_flags & RPC_TASK_SWAPPER)
7401da177e4SLinus Torvalds 		gfp = GFP_ATOMIC;
7411da177e4SLinus Torvalds 	else
7421da177e4SLinus Torvalds 		gfp = GFP_NOFS;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	if (size > RPC_BUFFER_MAXSIZE) {
745*02107148SChuck Lever 		req->rq_buffer = kmalloc(size, gfp);
746*02107148SChuck Lever 		if (req->rq_buffer)
747*02107148SChuck Lever 			req->rq_bufsize = size;
7481da177e4SLinus Torvalds 	} else {
749*02107148SChuck Lever 		req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
750*02107148SChuck Lever 		if (req->rq_buffer)
751*02107148SChuck Lever 			req->rq_bufsize = RPC_BUFFER_MAXSIZE;
7521da177e4SLinus Torvalds 	}
753*02107148SChuck Lever 	return req->rq_buffer;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
756*02107148SChuck Lever /**
757*02107148SChuck Lever  * rpc_free - free buffer allocated via rpc_malloc
758*02107148SChuck Lever  * @task: RPC task with a buffer to be freed
759*02107148SChuck Lever  *
760*02107148SChuck Lever  */
761*02107148SChuck Lever void rpc_free(struct rpc_task *task)
7621da177e4SLinus Torvalds {
763*02107148SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
764*02107148SChuck Lever 
765*02107148SChuck Lever 	if (req->rq_buffer) {
766*02107148SChuck Lever 		if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
767*02107148SChuck Lever 			mempool_free(req->rq_buffer, rpc_buffer_mempool);
7681da177e4SLinus Torvalds 		else
769*02107148SChuck Lever 			kfree(req->rq_buffer);
770*02107148SChuck Lever 		req->rq_buffer = NULL;
771*02107148SChuck Lever 		req->rq_bufsize = 0;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /*
7761da177e4SLinus Torvalds  * Creation and deletion of RPC task structures
7771da177e4SLinus Torvalds  */
778963d8fe5STrond Myklebust void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	memset(task, 0, sizeof(*task));
7811da177e4SLinus Torvalds 	init_timer(&task->tk_timer);
7821da177e4SLinus Torvalds 	task->tk_timer.data     = (unsigned long) task;
7831da177e4SLinus Torvalds 	task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
78444c28873STrond Myklebust 	atomic_set(&task->tk_count, 1);
7851da177e4SLinus Torvalds 	task->tk_client = clnt;
7861da177e4SLinus Torvalds 	task->tk_flags  = flags;
787963d8fe5STrond Myklebust 	task->tk_ops = tk_ops;
7884ce70adaSTrond Myklebust 	if (tk_ops->rpc_call_prepare != NULL)
7894ce70adaSTrond Myklebust 		task->tk_action = rpc_prepare_task;
790963d8fe5STrond Myklebust 	task->tk_calldata = calldata;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* Initialize retry counters */
7931da177e4SLinus Torvalds 	task->tk_garb_retry = 2;
7941da177e4SLinus Torvalds 	task->tk_cred_retry = 2;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	task->tk_priority = RPC_PRIORITY_NORMAL;
7971da177e4SLinus Torvalds 	task->tk_cookie = (unsigned long)current;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* Initialize workqueue for async tasks */
8001da177e4SLinus Torvalds 	task->tk_workqueue = rpciod_workqueue;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	if (clnt) {
8031da177e4SLinus Torvalds 		atomic_inc(&clnt->cl_users);
8041da177e4SLinus Torvalds 		if (clnt->cl_softrtry)
8051da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_SOFT;
8061da177e4SLinus Torvalds 		if (!clnt->cl_intr)
8071da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_NOINTR;
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds #ifdef RPC_DEBUG
8111da177e4SLinus Torvalds 	task->tk_magic = RPC_TASK_MAGIC_ID;
8121da177e4SLinus Torvalds 	task->tk_pid = rpc_task_id++;
8131da177e4SLinus Torvalds #endif
8141da177e4SLinus Torvalds 	/* Add to global list of all tasks */
8151da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
8161da177e4SLinus Torvalds 	list_add_tail(&task->tk_task, &all_tasks);
8171da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
8181da177e4SLinus Torvalds 
819963d8fe5STrond Myklebust 	BUG_ON(task->tk_ops == NULL);
820963d8fe5STrond Myklebust 
8211da177e4SLinus Torvalds 	dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
8221da177e4SLinus Torvalds 				current->pid);
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds static struct rpc_task *
8261da177e4SLinus Torvalds rpc_alloc_task(void)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
831963d8fe5STrond Myklebust static void rpc_free_task(struct rpc_task *task)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	dprintk("RPC: %4d freeing task\n", task->tk_pid);
8341da177e4SLinus Torvalds 	mempool_free(task, rpc_task_mempool);
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds /*
8381da177e4SLinus Torvalds  * Create a new task for the specified client.  We have to
8391da177e4SLinus Torvalds  * clean up after an allocation failure, as the client may
8401da177e4SLinus Torvalds  * have specified "oneshot".
8411da177e4SLinus Torvalds  */
842963d8fe5STrond Myklebust struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	struct rpc_task	*task;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	task = rpc_alloc_task();
8471da177e4SLinus Torvalds 	if (!task)
8481da177e4SLinus Torvalds 		goto cleanup;
8491da177e4SLinus Torvalds 
850963d8fe5STrond Myklebust 	rpc_init_task(task, clnt, flags, tk_ops, calldata);
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	dprintk("RPC: %4d allocated task\n", task->tk_pid);
8531da177e4SLinus Torvalds 	task->tk_flags |= RPC_TASK_DYNAMIC;
8541da177e4SLinus Torvalds out:
8551da177e4SLinus Torvalds 	return task;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds cleanup:
8581da177e4SLinus Torvalds 	/* Check whether to release the client */
8591da177e4SLinus Torvalds 	if (clnt) {
8601da177e4SLinus Torvalds 		printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
8611da177e4SLinus Torvalds 			atomic_read(&clnt->cl_users), clnt->cl_oneshot);
8621da177e4SLinus Torvalds 		atomic_inc(&clnt->cl_users); /* pretend we were used ... */
8631da177e4SLinus Torvalds 		rpc_release_client(clnt);
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 	goto out;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds void rpc_release_task(struct rpc_task *task)
8691da177e4SLinus Torvalds {
870963d8fe5STrond Myklebust 	const struct rpc_call_ops *tk_ops = task->tk_ops;
871963d8fe5STrond Myklebust 	void *calldata = task->tk_calldata;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds #ifdef RPC_DEBUG
8741da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
8751da177e4SLinus Torvalds #endif
87644c28873STrond Myklebust 	if (!atomic_dec_and_test(&task->tk_count))
87744c28873STrond Myklebust 		return;
87844c28873STrond Myklebust 	dprintk("RPC: %4d release task\n", task->tk_pid);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	/* Remove from global task list */
8811da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
8821da177e4SLinus Torvalds 	list_del(&task->tk_task);
8831da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	/* Synchronously delete any running timer */
8881da177e4SLinus Torvalds 	rpc_delete_timer(task);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* Release resources */
8911da177e4SLinus Torvalds 	if (task->tk_rqstp)
8921da177e4SLinus Torvalds 		xprt_release(task);
8931da177e4SLinus Torvalds 	if (task->tk_msg.rpc_cred)
8941da177e4SLinus Torvalds 		rpcauth_unbindcred(task);
8951da177e4SLinus Torvalds 	if (task->tk_client) {
8961da177e4SLinus Torvalds 		rpc_release_client(task->tk_client);
8971da177e4SLinus Torvalds 		task->tk_client = NULL;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds #ifdef RPC_DEBUG
9011da177e4SLinus Torvalds 	task->tk_magic = 0;
9021da177e4SLinus Torvalds #endif
903963d8fe5STrond Myklebust 	if (task->tk_flags & RPC_TASK_DYNAMIC)
904963d8fe5STrond Myklebust 		rpc_free_task(task);
905963d8fe5STrond Myklebust 	if (tk_ops->rpc_release)
906963d8fe5STrond Myklebust 		tk_ops->rpc_release(calldata);
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds /**
91044c28873STrond Myklebust  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
91144c28873STrond Myklebust  * @clnt - pointer to RPC client
91244c28873STrond Myklebust  * @flags - RPC flags
91344c28873STrond Myklebust  * @ops - RPC call ops
91444c28873STrond Myklebust  * @data - user call data
91544c28873STrond Myklebust  */
91644c28873STrond Myklebust struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
91744c28873STrond Myklebust 					const struct rpc_call_ops *ops,
91844c28873STrond Myklebust 					void *data)
91944c28873STrond Myklebust {
92044c28873STrond Myklebust 	struct rpc_task *task;
92144c28873STrond Myklebust 	task = rpc_new_task(clnt, flags, ops, data);
92244c28873STrond Myklebust 	if (task == NULL)
92344c28873STrond Myklebust 		return ERR_PTR(-ENOMEM);
92444c28873STrond Myklebust 	atomic_inc(&task->tk_count);
92544c28873STrond Myklebust 	rpc_execute(task);
92644c28873STrond Myklebust 	return task;
92744c28873STrond Myklebust }
92844c28873STrond Myklebust EXPORT_SYMBOL(rpc_run_task);
92944c28873STrond Myklebust 
93044c28873STrond Myklebust /**
9311da177e4SLinus Torvalds  * rpc_find_parent - find the parent of a child task.
9321da177e4SLinus Torvalds  * @child: child task
9331da177e4SLinus Torvalds  *
9341da177e4SLinus Torvalds  * Checks that the parent task is still sleeping on the
9351da177e4SLinus Torvalds  * queue 'childq'. If so returns a pointer to the parent.
9361da177e4SLinus Torvalds  * Upon failure returns NULL.
9371da177e4SLinus Torvalds  *
9381da177e4SLinus Torvalds  * Caller must hold childq.lock
9391da177e4SLinus Torvalds  */
940963d8fe5STrond Myklebust static inline struct rpc_task *rpc_find_parent(struct rpc_task *child, struct rpc_task *parent)
9411da177e4SLinus Torvalds {
942963d8fe5STrond Myklebust 	struct rpc_task	*task;
9431da177e4SLinus Torvalds 	struct list_head *le;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	task_for_each(task, le, &childq.tasks[0])
9461da177e4SLinus Torvalds 		if (task == parent)
9471da177e4SLinus Torvalds 			return parent;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	return NULL;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
952963d8fe5STrond Myklebust static void rpc_child_exit(struct rpc_task *child, void *calldata)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	struct rpc_task	*parent;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	spin_lock_bh(&childq.lock);
957963d8fe5STrond Myklebust 	if ((parent = rpc_find_parent(child, calldata)) != NULL) {
9581da177e4SLinus Torvalds 		parent->tk_status = child->tk_status;
9591da177e4SLinus Torvalds 		__rpc_wake_up_task(parent);
9601da177e4SLinus Torvalds 	}
9611da177e4SLinus Torvalds 	spin_unlock_bh(&childq.lock);
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
964963d8fe5STrond Myklebust static const struct rpc_call_ops rpc_child_ops = {
965963d8fe5STrond Myklebust 	.rpc_call_done = rpc_child_exit,
966963d8fe5STrond Myklebust };
967963d8fe5STrond Myklebust 
9681da177e4SLinus Torvalds /*
9691da177e4SLinus Torvalds  * Note: rpc_new_task releases the client after a failure.
9701da177e4SLinus Torvalds  */
9711da177e4SLinus Torvalds struct rpc_task *
9721da177e4SLinus Torvalds rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	struct rpc_task	*task;
9751da177e4SLinus Torvalds 
976963d8fe5STrond Myklebust 	task = rpc_new_task(clnt, RPC_TASK_ASYNC | RPC_TASK_CHILD, &rpc_child_ops, parent);
9771da177e4SLinus Torvalds 	if (!task)
9781da177e4SLinus Torvalds 		goto fail;
9791da177e4SLinus Torvalds 	return task;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds fail:
9821da177e4SLinus Torvalds 	parent->tk_status = -ENOMEM;
9831da177e4SLinus Torvalds 	return NULL;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	spin_lock_bh(&childq.lock);
9891da177e4SLinus Torvalds 	/* N.B. Is it possible for the child to have already finished? */
9901da177e4SLinus Torvalds 	__rpc_sleep_on(&childq, task, func, NULL);
9911da177e4SLinus Torvalds 	rpc_schedule_run(child);
9921da177e4SLinus Torvalds 	spin_unlock_bh(&childq.lock);
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
9961da177e4SLinus Torvalds  * Kill all tasks for the given client.
9971da177e4SLinus Torvalds  * XXX: kill their descendants as well?
9981da177e4SLinus Torvalds  */
9991da177e4SLinus Torvalds void rpc_killall_tasks(struct rpc_clnt *clnt)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	struct rpc_task	*rovr;
10021da177e4SLinus Torvalds 	struct list_head *le;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	dprintk("RPC:      killing all tasks for client %p\n", clnt);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	/*
10071da177e4SLinus Torvalds 	 * Spin lock all_tasks to prevent changes...
10081da177e4SLinus Torvalds 	 */
10091da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
10101da177e4SLinus Torvalds 	alltask_for_each(rovr, le, &all_tasks) {
10111da177e4SLinus Torvalds 		if (! RPC_IS_ACTIVATED(rovr))
10121da177e4SLinus Torvalds 			continue;
10131da177e4SLinus Torvalds 		if (!clnt || rovr->tk_client == clnt) {
10141da177e4SLinus Torvalds 			rovr->tk_flags |= RPC_TASK_KILLED;
10151da177e4SLinus Torvalds 			rpc_exit(rovr, -EIO);
10161da177e4SLinus Torvalds 			rpc_wake_up_task(rovr);
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 	}
10191da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds static DECLARE_MUTEX_LOCKED(rpciod_running);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds static void rpciod_killall(void)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	unsigned long flags;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	while (!list_empty(&all_tasks)) {
10291da177e4SLinus Torvalds 		clear_thread_flag(TIF_SIGPENDING);
10301da177e4SLinus Torvalds 		rpc_killall_tasks(NULL);
10311da177e4SLinus Torvalds 		flush_workqueue(rpciod_workqueue);
10321da177e4SLinus Torvalds 		if (!list_empty(&all_tasks)) {
10331da177e4SLinus Torvalds 			dprintk("rpciod_killall: waiting for tasks to exit\n");
10341da177e4SLinus Torvalds 			yield();
10351da177e4SLinus Torvalds 		}
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
10391da177e4SLinus Torvalds 	recalc_sigpending();
10401da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds /*
10441da177e4SLinus Torvalds  * Start up the rpciod process if it's not already running.
10451da177e4SLinus Torvalds  */
10461da177e4SLinus Torvalds int
10471da177e4SLinus Torvalds rpciod_up(void)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct workqueue_struct *wq;
10501da177e4SLinus Torvalds 	int error = 0;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	down(&rpciod_sema);
10531da177e4SLinus Torvalds 	dprintk("rpciod_up: users %d\n", rpciod_users);
10541da177e4SLinus Torvalds 	rpciod_users++;
10551da177e4SLinus Torvalds 	if (rpciod_workqueue)
10561da177e4SLinus Torvalds 		goto out;
10571da177e4SLinus Torvalds 	/*
10581da177e4SLinus Torvalds 	 * If there's no pid, we should be the first user.
10591da177e4SLinus Torvalds 	 */
10601da177e4SLinus Torvalds 	if (rpciod_users > 1)
10611da177e4SLinus Torvalds 		printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
10621da177e4SLinus Torvalds 	/*
10631da177e4SLinus Torvalds 	 * Create the rpciod thread and wait for it to start.
10641da177e4SLinus Torvalds 	 */
10651da177e4SLinus Torvalds 	error = -ENOMEM;
10661da177e4SLinus Torvalds 	wq = create_workqueue("rpciod");
10671da177e4SLinus Torvalds 	if (wq == NULL) {
10681da177e4SLinus Torvalds 		printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
10691da177e4SLinus Torvalds 		rpciod_users--;
10701da177e4SLinus Torvalds 		goto out;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 	rpciod_workqueue = wq;
10731da177e4SLinus Torvalds 	error = 0;
10741da177e4SLinus Torvalds out:
10751da177e4SLinus Torvalds 	up(&rpciod_sema);
10761da177e4SLinus Torvalds 	return error;
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds void
10801da177e4SLinus Torvalds rpciod_down(void)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	down(&rpciod_sema);
10831da177e4SLinus Torvalds 	dprintk("rpciod_down sema %d\n", rpciod_users);
10841da177e4SLinus Torvalds 	if (rpciod_users) {
10851da177e4SLinus Torvalds 		if (--rpciod_users)
10861da177e4SLinus Torvalds 			goto out;
10871da177e4SLinus Torvalds 	} else
10881da177e4SLinus Torvalds 		printk(KERN_WARNING "rpciod_down: no users??\n");
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if (!rpciod_workqueue) {
10911da177e4SLinus Torvalds 		dprintk("rpciod_down: Nothing to do!\n");
10921da177e4SLinus Torvalds 		goto out;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 	rpciod_killall();
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	destroy_workqueue(rpciod_workqueue);
10971da177e4SLinus Torvalds 	rpciod_workqueue = NULL;
10981da177e4SLinus Torvalds  out:
10991da177e4SLinus Torvalds 	up(&rpciod_sema);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds #ifdef RPC_DEBUG
11031da177e4SLinus Torvalds void rpc_show_tasks(void)
11041da177e4SLinus Torvalds {
11051da177e4SLinus Torvalds 	struct list_head *le;
11061da177e4SLinus Torvalds 	struct rpc_task *t;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
11091da177e4SLinus Torvalds 	if (list_empty(&all_tasks)) {
11101da177e4SLinus Torvalds 		spin_unlock(&rpc_sched_lock);
11111da177e4SLinus Torvalds 		return;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds 	printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1114963d8fe5STrond Myklebust 		"-rpcwait -action- ---ops--\n");
11151da177e4SLinus Torvalds 	alltask_for_each(t, le, &all_tasks) {
11161da177e4SLinus Torvalds 		const char *rpc_waitq = "none";
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(t))
11191da177e4SLinus Torvalds 			rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 		printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
11221da177e4SLinus Torvalds 			t->tk_pid,
11231da177e4SLinus Torvalds 			(t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
11241da177e4SLinus Torvalds 			t->tk_flags, t->tk_status,
11251da177e4SLinus Torvalds 			t->tk_client,
11261da177e4SLinus Torvalds 			(t->tk_client ? t->tk_client->cl_prog : 0),
11271da177e4SLinus Torvalds 			t->tk_rqstp, t->tk_timeout,
11281da177e4SLinus Torvalds 			rpc_waitq,
1129963d8fe5STrond Myklebust 			t->tk_action, t->tk_ops);
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds #endif
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds void
11361da177e4SLinus Torvalds rpc_destroy_mempool(void)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	if (rpc_buffer_mempool)
11391da177e4SLinus Torvalds 		mempool_destroy(rpc_buffer_mempool);
11401da177e4SLinus Torvalds 	if (rpc_task_mempool)
11411da177e4SLinus Torvalds 		mempool_destroy(rpc_task_mempool);
11421da177e4SLinus Torvalds 	if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
11431da177e4SLinus Torvalds 		printk(KERN_INFO "rpc_task: not all structures were freed\n");
11441da177e4SLinus Torvalds 	if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
11451da177e4SLinus Torvalds 		printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds int
11491da177e4SLinus Torvalds rpc_init_mempool(void)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	rpc_task_slabp = kmem_cache_create("rpc_tasks",
11521da177e4SLinus Torvalds 					     sizeof(struct rpc_task),
11531da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
11541da177e4SLinus Torvalds 					     NULL, NULL);
11551da177e4SLinus Torvalds 	if (!rpc_task_slabp)
11561da177e4SLinus Torvalds 		goto err_nomem;
11571da177e4SLinus Torvalds 	rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
11581da177e4SLinus Torvalds 					     RPC_BUFFER_MAXSIZE,
11591da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
11601da177e4SLinus Torvalds 					     NULL, NULL);
11611da177e4SLinus Torvalds 	if (!rpc_buffer_slabp)
11621da177e4SLinus Torvalds 		goto err_nomem;
11631da177e4SLinus Torvalds 	rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
11641da177e4SLinus Torvalds 					    mempool_alloc_slab,
11651da177e4SLinus Torvalds 					    mempool_free_slab,
11661da177e4SLinus Torvalds 					    rpc_task_slabp);
11671da177e4SLinus Torvalds 	if (!rpc_task_mempool)
11681da177e4SLinus Torvalds 		goto err_nomem;
11691da177e4SLinus Torvalds 	rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
11701da177e4SLinus Torvalds 					    mempool_alloc_slab,
11711da177e4SLinus Torvalds 					    mempool_free_slab,
11721da177e4SLinus Torvalds 					    rpc_buffer_slabp);
11731da177e4SLinus Torvalds 	if (!rpc_buffer_mempool)
11741da177e4SLinus Torvalds 		goto err_nomem;
11751da177e4SLinus Torvalds 	return 0;
11761da177e4SLinus Torvalds err_nomem:
11771da177e4SLinus Torvalds 	rpc_destroy_mempool();
11781da177e4SLinus Torvalds 	return -ENOMEM;
11791da177e4SLinus Torvalds }
1180