xref: /linux/net/sunrpc/sched.c (revision dd0fc66fb33cd610bc1a5db8a5e232d34879b4d7)
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_free(struct rpc_task *task);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static void			rpc_async_schedule(void *);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * RPC tasks that create another task (e.g. for contacting the portmapper)
501da177e4SLinus Torvalds  * will wait on this queue for their child's completion
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds static RPC_WAITQ(childq, "childq");
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * RPC tasks sit here while waiting for conditions to improve.
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds static RPC_WAITQ(delay_queue, "delayq");
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * All RPC tasks are linked into this list
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds static LIST_HEAD(all_tasks);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * rpciod-related stuff
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds static DECLARE_MUTEX(rpciod_sema);
681da177e4SLinus Torvalds static unsigned int		rpciod_users;
691da177e4SLinus Torvalds static struct workqueue_struct *rpciod_workqueue;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  * Spinlock for other critical sections of code.
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_sched_lock);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds  * Disable the timer for a given RPC task. Should be called with
781da177e4SLinus Torvalds  * queue->lock and bh_disabled in order to avoid races within
791da177e4SLinus Torvalds  * rpc_run_timer().
801da177e4SLinus Torvalds  */
811da177e4SLinus Torvalds static inline void
821da177e4SLinus Torvalds __rpc_disable_timer(struct rpc_task *task)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	dprintk("RPC: %4d disabling timer\n", task->tk_pid);
851da177e4SLinus Torvalds 	task->tk_timeout_fn = NULL;
861da177e4SLinus Torvalds 	task->tk_timeout = 0;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /*
901da177e4SLinus Torvalds  * Run a timeout function.
911da177e4SLinus Torvalds  * We use the callback in order to allow __rpc_wake_up_task()
921da177e4SLinus Torvalds  * and friends to disable the timer synchronously on SMP systems
931da177e4SLinus Torvalds  * without calling del_timer_sync(). The latter could cause a
941da177e4SLinus Torvalds  * deadlock if called while we're holding spinlocks...
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds static void rpc_run_timer(struct rpc_task *task)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	void (*callback)(struct rpc_task *);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	callback = task->tk_timeout_fn;
1011da177e4SLinus Torvalds 	task->tk_timeout_fn = NULL;
1021da177e4SLinus Torvalds 	if (callback && RPC_IS_QUEUED(task)) {
1031da177e4SLinus Torvalds 		dprintk("RPC: %4d running timer\n", task->tk_pid);
1041da177e4SLinus Torvalds 		callback(task);
1051da177e4SLinus Torvalds 	}
1061da177e4SLinus Torvalds 	smp_mb__before_clear_bit();
1071da177e4SLinus Torvalds 	clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
1081da177e4SLinus Torvalds 	smp_mb__after_clear_bit();
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * Set up a timer for the current task.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static inline void
1151da177e4SLinus Torvalds __rpc_add_timer(struct rpc_task *task, rpc_action timer)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	if (!task->tk_timeout)
1181da177e4SLinus Torvalds 		return;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	dprintk("RPC: %4d setting alarm for %lu ms\n",
1211da177e4SLinus Torvalds 			task->tk_pid, task->tk_timeout * 1000 / HZ);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (timer)
1241da177e4SLinus Torvalds 		task->tk_timeout_fn = timer;
1251da177e4SLinus Torvalds 	else
1261da177e4SLinus Torvalds 		task->tk_timeout_fn = __rpc_default_timer;
1271da177e4SLinus Torvalds 	set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
1281da177e4SLinus Torvalds 	mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*
1321da177e4SLinus Torvalds  * Delete any timer for the current task. Because we use del_timer_sync(),
1331da177e4SLinus Torvalds  * this function should never be called while holding queue->lock.
1341da177e4SLinus Torvalds  */
1351da177e4SLinus Torvalds static void
1361da177e4SLinus Torvalds rpc_delete_timer(struct rpc_task *task)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	if (RPC_IS_QUEUED(task))
1391da177e4SLinus Torvalds 		return;
1401da177e4SLinus Torvalds 	if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
1411da177e4SLinus Torvalds 		del_singleshot_timer_sync(&task->tk_timer);
1421da177e4SLinus Torvalds 		dprintk("RPC: %4d deleting timer\n", task->tk_pid);
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
1471da177e4SLinus Torvalds  * Add new request to a priority queue.
1481da177e4SLinus Torvalds  */
1491da177e4SLinus Torvalds static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct list_head *q;
1521da177e4SLinus Torvalds 	struct rpc_task *t;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	INIT_LIST_HEAD(&task->u.tk_wait.links);
1551da177e4SLinus Torvalds 	q = &queue->tasks[task->tk_priority];
1561da177e4SLinus Torvalds 	if (unlikely(task->tk_priority > queue->maxpriority))
1571da177e4SLinus Torvalds 		q = &queue->tasks[queue->maxpriority];
1581da177e4SLinus Torvalds 	list_for_each_entry(t, q, u.tk_wait.list) {
1591da177e4SLinus Torvalds 		if (t->tk_cookie == task->tk_cookie) {
1601da177e4SLinus Torvalds 			list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
1611da177e4SLinus Torvalds 			return;
1621da177e4SLinus Torvalds 		}
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 	list_add_tail(&task->u.tk_wait.list, q);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds  * Add new request to wait queue.
1691da177e4SLinus Torvalds  *
1701da177e4SLinus Torvalds  * Swapper tasks always get inserted at the head of the queue.
1711da177e4SLinus Torvalds  * This should avoid many nasty memory deadlocks and hopefully
1721da177e4SLinus Torvalds  * improve overall performance.
1731da177e4SLinus Torvalds  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
1801da177e4SLinus Torvalds 		__rpc_add_wait_queue_priority(queue, task);
1811da177e4SLinus Torvalds 	else if (RPC_IS_SWAPPER(task))
1821da177e4SLinus Torvalds 		list_add(&task->u.tk_wait.list, &queue->tasks[0]);
1831da177e4SLinus Torvalds 	else
1841da177e4SLinus Torvalds 		list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
1851da177e4SLinus Torvalds 	task->u.tk_wait.rpc_waitq = queue;
1861da177e4SLinus Torvalds 	rpc_set_queued(task);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	dprintk("RPC: %4d added to queue %p \"%s\"\n",
1891da177e4SLinus Torvalds 				task->tk_pid, queue, rpc_qname(queue));
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  * Remove request from a priority queue.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct rpc_task *t;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (!list_empty(&task->u.tk_wait.links)) {
2001da177e4SLinus Torvalds 		t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
2011da177e4SLinus Torvalds 		list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
2021da177e4SLinus Torvalds 		list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 	list_del(&task->u.tk_wait.list);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  * Remove request from queue.
2091da177e4SLinus Torvalds  * Note: must be called with spin lock held.
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds static void __rpc_remove_wait_queue(struct rpc_task *task)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	struct rpc_wait_queue *queue;
2141da177e4SLinus Torvalds 	queue = task->u.tk_wait.rpc_waitq;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
2171da177e4SLinus Torvalds 		__rpc_remove_wait_queue_priority(task);
2181da177e4SLinus Torvalds 	else
2191da177e4SLinus Torvalds 		list_del(&task->u.tk_wait.list);
2201da177e4SLinus Torvalds 	dprintk("RPC: %4d removed from queue %p \"%s\"\n",
2211da177e4SLinus Torvalds 				task->tk_pid, queue, rpc_qname(queue));
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	queue->priority = priority;
2271da177e4SLinus Torvalds 	queue->count = 1 << (priority * 2);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	queue->cookie = cookie;
2331da177e4SLinus Torvalds 	queue->nr = RPC_BATCH_COUNT;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, queue->maxpriority);
2391da177e4SLinus Torvalds 	rpc_set_waitqueue_cookie(queue, 0);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	int i;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	spin_lock_init(&queue->lock);
2471da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
2481da177e4SLinus Torvalds 		INIT_LIST_HEAD(&queue->tasks[i]);
2491da177e4SLinus Torvalds 	queue->maxpriority = maxprio;
2501da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
2511da177e4SLinus Torvalds #ifdef RPC_DEBUG
2521da177e4SLinus Torvalds 	queue->name = qname;
2531da177e4SLinus Torvalds #endif
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	__rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	__rpc_init_priority_wait_queue(queue, qname, 0);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds EXPORT_SYMBOL(rpc_init_wait_queue);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds /*
2681da177e4SLinus Torvalds  * Make an RPC task runnable.
2691da177e4SLinus Torvalds  *
2701da177e4SLinus Torvalds  * Note: If the task is ASYNC, this must be called with
2711da177e4SLinus Torvalds  * the spinlock held to protect the wait queue operation.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	int do_ret;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	BUG_ON(task->tk_timeout_fn);
2781da177e4SLinus Torvalds 	do_ret = rpc_test_and_set_running(task);
2791da177e4SLinus Torvalds 	rpc_clear_queued(task);
2801da177e4SLinus Torvalds 	if (do_ret)
2811da177e4SLinus Torvalds 		return;
2821da177e4SLinus Torvalds 	if (RPC_IS_ASYNC(task)) {
2831da177e4SLinus Torvalds 		int status;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 		INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
2861da177e4SLinus Torvalds 		status = queue_work(task->tk_workqueue, &task->u.tk_work);
2871da177e4SLinus Torvalds 		if (status < 0) {
2881da177e4SLinus Torvalds 			printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
2891da177e4SLinus Torvalds 			task->tk_status = status;
2901da177e4SLinus Torvalds 			return;
2911da177e4SLinus Torvalds 		}
2921da177e4SLinus Torvalds 	} else
29396651ab3STrond Myklebust 		wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * Place a newly initialized task on the workqueue.
2981da177e4SLinus Torvalds  */
2991da177e4SLinus Torvalds static inline void
3001da177e4SLinus Torvalds rpc_schedule_run(struct rpc_task *task)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	/* Don't run a child twice! */
3031da177e4SLinus Torvalds 	if (RPC_IS_ACTIVATED(task))
3041da177e4SLinus Torvalds 		return;
3051da177e4SLinus Torvalds 	task->tk_active = 1;
3061da177e4SLinus Torvalds 	rpc_make_runnable(task);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /*
3101da177e4SLinus Torvalds  * Prepare for sleeping on a wait queue.
3111da177e4SLinus Torvalds  * By always appending tasks to the list we ensure FIFO behavior.
3121da177e4SLinus Torvalds  * NB: An RPC task will only receive interrupt-driven events as long
3131da177e4SLinus Torvalds  * as it's on a wait queue.
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3161da177e4SLinus Torvalds 			rpc_action action, rpc_action timer)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
3191da177e4SLinus Torvalds 				rpc_qname(q), jiffies);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
3221da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
3231da177e4SLinus Torvalds 		return;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Mark the task as being activated if so needed */
3271da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task))
3281da177e4SLinus Torvalds 		task->tk_active = 1;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	__rpc_add_wait_queue(q, task);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	BUG_ON(task->tk_callback != NULL);
3331da177e4SLinus Torvalds 	task->tk_callback = action;
3341da177e4SLinus Torvalds 	__rpc_add_timer(task, timer);
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
3381da177e4SLinus Torvalds 				rpc_action action, rpc_action timer)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	/*
3411da177e4SLinus Torvalds 	 * Protect the queue operations.
3421da177e4SLinus Torvalds 	 */
3431da177e4SLinus Torvalds 	spin_lock_bh(&q->lock);
3441da177e4SLinus Torvalds 	__rpc_sleep_on(q, task, action, timer);
3451da177e4SLinus Torvalds 	spin_unlock_bh(&q->lock);
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /**
3491da177e4SLinus Torvalds  * __rpc_do_wake_up_task - wake up a single rpc_task
3501da177e4SLinus Torvalds  * @task: task to be woken up
3511da177e4SLinus Torvalds  *
3521da177e4SLinus Torvalds  * Caller must hold queue->lock, and have cleared the task queued flag.
3531da177e4SLinus Torvalds  */
3541da177e4SLinus Torvalds static void __rpc_do_wake_up_task(struct rpc_task *task)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds #ifdef RPC_DEBUG
3591da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
3601da177e4SLinus Torvalds #endif
3611da177e4SLinus Torvalds 	/* Has the task been executed yet? If not, we cannot wake it up! */
3621da177e4SLinus Torvalds 	if (!RPC_IS_ACTIVATED(task)) {
3631da177e4SLinus Torvalds 		printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
3641da177e4SLinus Torvalds 		return;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	__rpc_disable_timer(task);
3681da177e4SLinus Torvalds 	__rpc_remove_wait_queue(task);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	rpc_make_runnable(task);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	dprintk("RPC:      __rpc_wake_up_task done\n");
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /*
3761da177e4SLinus Torvalds  * Wake up the specified task
3771da177e4SLinus Torvalds  */
3781da177e4SLinus Torvalds static void __rpc_wake_up_task(struct rpc_task *task)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	if (rpc_start_wakeup(task)) {
3811da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(task))
3821da177e4SLinus Torvalds 			__rpc_do_wake_up_task(task);
3831da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * Default timeout handler if none specified by user
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds static void
3911da177e4SLinus Torvalds __rpc_default_timer(struct rpc_task *task)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
3941da177e4SLinus Torvalds 	task->tk_status = -ETIMEDOUT;
3951da177e4SLinus Torvalds 	rpc_wake_up_task(task);
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  * Wake up the specified task
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds 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 			struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 			spin_lock_bh(&queue->lock);
4081da177e4SLinus Torvalds 			__rpc_do_wake_up_task(task);
4091da177e4SLinus Torvalds 			spin_unlock_bh(&queue->lock);
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 		rpc_finish_wakeup(task);
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds  * Wake up the next task on a priority queue.
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	struct list_head *q;
4211da177e4SLinus Torvalds 	struct rpc_task *task;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	/*
4241da177e4SLinus Torvalds 	 * Service a batch of tasks from a single cookie.
4251da177e4SLinus Torvalds 	 */
4261da177e4SLinus Torvalds 	q = &queue->tasks[queue->priority];
4271da177e4SLinus Torvalds 	if (!list_empty(q)) {
4281da177e4SLinus Torvalds 		task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4291da177e4SLinus Torvalds 		if (queue->cookie == task->tk_cookie) {
4301da177e4SLinus Torvalds 			if (--queue->nr)
4311da177e4SLinus Torvalds 				goto out;
4321da177e4SLinus Torvalds 			list_move_tail(&task->u.tk_wait.list, q);
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 		/*
4351da177e4SLinus Torvalds 		 * Check if we need to switch queues.
4361da177e4SLinus Torvalds 		 */
4371da177e4SLinus Torvalds 		if (--queue->count)
4381da177e4SLinus Torvalds 			goto new_cookie;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	/*
4421da177e4SLinus Torvalds 	 * Service the next queue.
4431da177e4SLinus Torvalds 	 */
4441da177e4SLinus Torvalds 	do {
4451da177e4SLinus Torvalds 		if (q == &queue->tasks[0])
4461da177e4SLinus Torvalds 			q = &queue->tasks[queue->maxpriority];
4471da177e4SLinus Torvalds 		else
4481da177e4SLinus Torvalds 			q = q - 1;
4491da177e4SLinus Torvalds 		if (!list_empty(q)) {
4501da177e4SLinus Torvalds 			task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
4511da177e4SLinus Torvalds 			goto new_queue;
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 	} while (q != &queue->tasks[queue->priority]);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	rpc_reset_waitqueue_priority(queue);
4561da177e4SLinus Torvalds 	return NULL;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds new_queue:
4591da177e4SLinus Torvalds 	rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
4601da177e4SLinus Torvalds new_cookie:
4611da177e4SLinus Torvalds 	rpc_set_waitqueue_cookie(queue, task->tk_cookie);
4621da177e4SLinus Torvalds out:
4631da177e4SLinus Torvalds 	__rpc_wake_up_task(task);
4641da177e4SLinus Torvalds 	return task;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
4681da177e4SLinus Torvalds  * Wake up the next task on the wait queue.
4691da177e4SLinus Torvalds  */
4701da177e4SLinus Torvalds struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	struct rpc_task	*task = NULL;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	dprintk("RPC:      wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
4751da177e4SLinus Torvalds 	spin_lock_bh(&queue->lock);
4761da177e4SLinus Torvalds 	if (RPC_IS_PRIORITY(queue))
4771da177e4SLinus Torvalds 		task = __rpc_wake_up_next_priority(queue);
4781da177e4SLinus Torvalds 	else {
4791da177e4SLinus Torvalds 		task_for_first(task, &queue->tasks[0])
4801da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	spin_unlock_bh(&queue->lock);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	return task;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /**
4881da177e4SLinus Torvalds  * rpc_wake_up - wake up all rpc_tasks
4891da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
4901da177e4SLinus Torvalds  *
4911da177e4SLinus Torvalds  * Grabs queue->lock
4921da177e4SLinus Torvalds  */
4931da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct rpc_task *task;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	struct list_head *head;
4981da177e4SLinus Torvalds 	spin_lock_bh(&queue->lock);
4991da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5001da177e4SLinus Torvalds 	for (;;) {
5011da177e4SLinus Torvalds 		while (!list_empty(head)) {
5021da177e4SLinus Torvalds 			task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
5031da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5041da177e4SLinus Torvalds 		}
5051da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5061da177e4SLinus Torvalds 			break;
5071da177e4SLinus Torvalds 		head--;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 	spin_unlock_bh(&queue->lock);
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /**
5131da177e4SLinus Torvalds  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
5141da177e4SLinus Torvalds  * @queue: rpc_wait_queue on which the tasks are sleeping
5151da177e4SLinus Torvalds  * @status: status value to set
5161da177e4SLinus Torvalds  *
5171da177e4SLinus Torvalds  * Grabs queue->lock
5181da177e4SLinus Torvalds  */
5191da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	struct list_head *head;
5221da177e4SLinus Torvalds 	struct rpc_task *task;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	spin_lock_bh(&queue->lock);
5251da177e4SLinus Torvalds 	head = &queue->tasks[queue->maxpriority];
5261da177e4SLinus Torvalds 	for (;;) {
5271da177e4SLinus Torvalds 		while (!list_empty(head)) {
5281da177e4SLinus Torvalds 			task = list_entry(head->next, struct rpc_task, u.tk_wait.list);
5291da177e4SLinus Torvalds 			task->tk_status = status;
5301da177e4SLinus Torvalds 			__rpc_wake_up_task(task);
5311da177e4SLinus Torvalds 		}
5321da177e4SLinus Torvalds 		if (head == &queue->tasks[0])
5331da177e4SLinus Torvalds 			break;
5341da177e4SLinus Torvalds 		head--;
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 	spin_unlock_bh(&queue->lock);
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /*
5401da177e4SLinus Torvalds  * Run a task at a later time
5411da177e4SLinus Torvalds  */
5421da177e4SLinus Torvalds static void	__rpc_atrun(struct rpc_task *);
5431da177e4SLinus Torvalds void
5441da177e4SLinus Torvalds rpc_delay(struct rpc_task *task, unsigned long delay)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	task->tk_timeout = delay;
5471da177e4SLinus Torvalds 	rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static void
5511da177e4SLinus Torvalds __rpc_atrun(struct rpc_task *task)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	task->tk_status = 0;
5541da177e4SLinus Torvalds 	rpc_wake_up_task(task);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
558d05fdb0cSTrond Myklebust  * Helper that calls task->tk_exit if it exists and then returns
559d05fdb0cSTrond Myklebust  * true if we should exit __rpc_execute.
560d05fdb0cSTrond Myklebust  */
561d05fdb0cSTrond Myklebust static inline int __rpc_do_exit(struct rpc_task *task)
562d05fdb0cSTrond Myklebust {
563d05fdb0cSTrond Myklebust 	if (task->tk_exit != NULL) {
564d05fdb0cSTrond Myklebust 		lock_kernel();
565d05fdb0cSTrond Myklebust 		task->tk_exit(task);
566d05fdb0cSTrond Myklebust 		unlock_kernel();
567d05fdb0cSTrond Myklebust 		/* If tk_action is non-null, we should restart the call */
568d05fdb0cSTrond Myklebust 		if (task->tk_action != NULL) {
569d05fdb0cSTrond Myklebust 			if (!RPC_ASSASSINATED(task)) {
570d05fdb0cSTrond Myklebust 				/* Release RPC slot and buffer memory */
571d05fdb0cSTrond Myklebust 				xprt_release(task);
572d05fdb0cSTrond Myklebust 				rpc_free(task);
573d05fdb0cSTrond Myklebust 				return 0;
574d05fdb0cSTrond Myklebust 			}
575d05fdb0cSTrond Myklebust 			printk(KERN_ERR "RPC: dead task tried to walk away.\n");
576d05fdb0cSTrond Myklebust 		}
577d05fdb0cSTrond Myklebust 	}
578d05fdb0cSTrond Myklebust 	return 1;
579d05fdb0cSTrond Myklebust }
580d05fdb0cSTrond Myklebust 
58196651ab3STrond Myklebust static int rpc_wait_bit_interruptible(void *word)
58296651ab3STrond Myklebust {
58396651ab3STrond Myklebust 	if (signal_pending(current))
58496651ab3STrond Myklebust 		return -ERESTARTSYS;
58596651ab3STrond Myklebust 	schedule();
58696651ab3STrond Myklebust 	return 0;
58796651ab3STrond Myklebust }
58896651ab3STrond Myklebust 
589d05fdb0cSTrond Myklebust /*
5901da177e4SLinus Torvalds  * This is the RPC `scheduler' (or rather, the finite state machine).
5911da177e4SLinus Torvalds  */
5921da177e4SLinus Torvalds static int __rpc_execute(struct rpc_task *task)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	int		status = 0;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	dprintk("RPC: %4d rpc_execute flgs %x\n",
5971da177e4SLinus Torvalds 				task->tk_pid, task->tk_flags);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	BUG_ON(RPC_IS_QUEUED(task));
6001da177e4SLinus Torvalds 
601d05fdb0cSTrond Myklebust 	for (;;) {
6021da177e4SLinus Torvalds 		/*
6031da177e4SLinus Torvalds 		 * Garbage collection of pending timers...
6041da177e4SLinus Torvalds 		 */
6051da177e4SLinus Torvalds 		rpc_delete_timer(task);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		/*
6081da177e4SLinus Torvalds 		 * Execute any pending callback.
6091da177e4SLinus Torvalds 		 */
6101da177e4SLinus Torvalds 		if (RPC_DO_CALLBACK(task)) {
6111da177e4SLinus Torvalds 			/* Define a callback save pointer */
6121da177e4SLinus Torvalds 			void (*save_callback)(struct rpc_task *);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 			/*
6151da177e4SLinus Torvalds 			 * If a callback exists, save it, reset it,
6161da177e4SLinus Torvalds 			 * call it.
6171da177e4SLinus Torvalds 			 * The save is needed to stop from resetting
6181da177e4SLinus Torvalds 			 * another callback set within the callback handler
6191da177e4SLinus Torvalds 			 * - Dave
6201da177e4SLinus Torvalds 			 */
6211da177e4SLinus Torvalds 			save_callback=task->tk_callback;
6221da177e4SLinus Torvalds 			task->tk_callback=NULL;
6231da177e4SLinus Torvalds 			lock_kernel();
6241da177e4SLinus Torvalds 			save_callback(task);
6251da177e4SLinus Torvalds 			unlock_kernel();
6261da177e4SLinus Torvalds 		}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 		/*
6291da177e4SLinus Torvalds 		 * Perform the next FSM step.
6301da177e4SLinus Torvalds 		 * tk_action may be NULL when the task has been killed
6311da177e4SLinus Torvalds 		 * by someone else.
6321da177e4SLinus Torvalds 		 */
6331da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task)) {
634d05fdb0cSTrond Myklebust 			if (task->tk_action != NULL) {
6351da177e4SLinus Torvalds 				lock_kernel();
6361da177e4SLinus Torvalds 				task->tk_action(task);
6371da177e4SLinus Torvalds 				unlock_kernel();
638d05fdb0cSTrond Myklebust 			} else if (__rpc_do_exit(task))
639d05fdb0cSTrond Myklebust 				break;
6401da177e4SLinus Torvalds 		}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 		/*
6431da177e4SLinus Torvalds 		 * Lockless check for whether task is sleeping or not.
6441da177e4SLinus Torvalds 		 */
6451da177e4SLinus Torvalds 		if (!RPC_IS_QUEUED(task))
6461da177e4SLinus Torvalds 			continue;
6471da177e4SLinus Torvalds 		rpc_clear_running(task);
6481da177e4SLinus Torvalds 		if (RPC_IS_ASYNC(task)) {
6491da177e4SLinus Torvalds 			/* Careful! we may have raced... */
6501da177e4SLinus Torvalds 			if (RPC_IS_QUEUED(task))
6511da177e4SLinus Torvalds 				return 0;
6521da177e4SLinus Torvalds 			if (rpc_test_and_set_running(task))
6531da177e4SLinus Torvalds 				return 0;
6541da177e4SLinus Torvalds 			continue;
6551da177e4SLinus Torvalds 		}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		/* sync task: sleep here */
6581da177e4SLinus Torvalds 		dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
65996651ab3STrond Myklebust 		/* Note: Caller should be using rpc_clnt_sigmask() */
66096651ab3STrond Myklebust 		status = out_of_line_wait_on_bit(&task->tk_runstate,
66196651ab3STrond Myklebust 				RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
66296651ab3STrond Myklebust 				TASK_INTERRUPTIBLE);
66396651ab3STrond Myklebust 		if (status == -ERESTARTSYS) {
6641da177e4SLinus Torvalds 			/*
6651da177e4SLinus Torvalds 			 * When a sync task receives a signal, it exits with
6661da177e4SLinus Torvalds 			 * -ERESTARTSYS. In order to catch any callbacks that
6671da177e4SLinus Torvalds 			 * clean up after sleeping on some queue, we don't
6681da177e4SLinus Torvalds 			 * break the loop here, but go around once more.
6691da177e4SLinus Torvalds 			 */
6701da177e4SLinus Torvalds 			dprintk("RPC: %4d got signal\n", task->tk_pid);
6711da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_KILLED;
6721da177e4SLinus Torvalds 			rpc_exit(task, -ERESTARTSYS);
6731da177e4SLinus Torvalds 			rpc_wake_up_task(task);
6741da177e4SLinus Torvalds 		}
6751da177e4SLinus Torvalds 		rpc_set_running(task);
6761da177e4SLinus Torvalds 		dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	dprintk("RPC: %4d exit() = %d\n", task->tk_pid, task->tk_status);
6801da177e4SLinus Torvalds 	status = task->tk_status;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	/* Release all resources associated with the task */
6831da177e4SLinus Torvalds 	rpc_release_task(task);
6841da177e4SLinus Torvalds 	return status;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds /*
6881da177e4SLinus Torvalds  * User-visible entry point to the scheduler.
6891da177e4SLinus Torvalds  *
6901da177e4SLinus Torvalds  * This may be called recursively if e.g. an async NFS task updates
6911da177e4SLinus Torvalds  * the attributes and finds that dirty pages must be flushed.
6921da177e4SLinus Torvalds  * NOTE: Upon exit of this function the task is guaranteed to be
6931da177e4SLinus Torvalds  *	 released. In particular note that tk_release() will have
6941da177e4SLinus Torvalds  *	 been called, so your task memory may have been freed.
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds int
6971da177e4SLinus Torvalds rpc_execute(struct rpc_task *task)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	BUG_ON(task->tk_active);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	task->tk_active = 1;
7021da177e4SLinus Torvalds 	rpc_set_running(task);
7031da177e4SLinus Torvalds 	return __rpc_execute(task);
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static void rpc_async_schedule(void *arg)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	__rpc_execute((struct rpc_task *)arg);
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds /*
7121da177e4SLinus Torvalds  * Allocate memory for RPC purposes.
7131da177e4SLinus Torvalds  *
7141da177e4SLinus Torvalds  * We try to ensure that some NFS reads and writes can always proceed
7151da177e4SLinus Torvalds  * by using a mempool when allocating 'small' buffers.
7161da177e4SLinus Torvalds  * In order to avoid memory starvation triggering more writebacks of
7171da177e4SLinus Torvalds  * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds void *
7201da177e4SLinus Torvalds rpc_malloc(struct rpc_task *task, size_t size)
7211da177e4SLinus Torvalds {
722*dd0fc66fSAl Viro 	gfp_t	gfp;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if (task->tk_flags & RPC_TASK_SWAPPER)
7251da177e4SLinus Torvalds 		gfp = GFP_ATOMIC;
7261da177e4SLinus Torvalds 	else
7271da177e4SLinus Torvalds 		gfp = GFP_NOFS;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	if (size > RPC_BUFFER_MAXSIZE) {
7301da177e4SLinus Torvalds 		task->tk_buffer =  kmalloc(size, gfp);
7311da177e4SLinus Torvalds 		if (task->tk_buffer)
7321da177e4SLinus Torvalds 			task->tk_bufsize = size;
7331da177e4SLinus Torvalds 	} else {
7341da177e4SLinus Torvalds 		task->tk_buffer =  mempool_alloc(rpc_buffer_mempool, gfp);
7351da177e4SLinus Torvalds 		if (task->tk_buffer)
7361da177e4SLinus Torvalds 			task->tk_bufsize = RPC_BUFFER_MAXSIZE;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 	return task->tk_buffer;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds static void
7421da177e4SLinus Torvalds rpc_free(struct rpc_task *task)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	if (task->tk_buffer) {
7451da177e4SLinus Torvalds 		if (task->tk_bufsize == RPC_BUFFER_MAXSIZE)
7461da177e4SLinus Torvalds 			mempool_free(task->tk_buffer, rpc_buffer_mempool);
7471da177e4SLinus Torvalds 		else
7481da177e4SLinus Torvalds 			kfree(task->tk_buffer);
7491da177e4SLinus Torvalds 		task->tk_buffer = NULL;
7501da177e4SLinus Torvalds 		task->tk_bufsize = 0;
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /*
7551da177e4SLinus Torvalds  * Creation and deletion of RPC task structures
7561da177e4SLinus Torvalds  */
7571da177e4SLinus Torvalds void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, rpc_action callback, int flags)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	memset(task, 0, sizeof(*task));
7601da177e4SLinus Torvalds 	init_timer(&task->tk_timer);
7611da177e4SLinus Torvalds 	task->tk_timer.data     = (unsigned long) task;
7621da177e4SLinus Torvalds 	task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
7631da177e4SLinus Torvalds 	task->tk_client = clnt;
7641da177e4SLinus Torvalds 	task->tk_flags  = flags;
7651da177e4SLinus Torvalds 	task->tk_exit   = callback;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/* Initialize retry counters */
7681da177e4SLinus Torvalds 	task->tk_garb_retry = 2;
7691da177e4SLinus Torvalds 	task->tk_cred_retry = 2;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	task->tk_priority = RPC_PRIORITY_NORMAL;
7721da177e4SLinus Torvalds 	task->tk_cookie = (unsigned long)current;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	/* Initialize workqueue for async tasks */
7751da177e4SLinus Torvalds 	task->tk_workqueue = rpciod_workqueue;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	if (clnt) {
7781da177e4SLinus Torvalds 		atomic_inc(&clnt->cl_users);
7791da177e4SLinus Torvalds 		if (clnt->cl_softrtry)
7801da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_SOFT;
7811da177e4SLinus Torvalds 		if (!clnt->cl_intr)
7821da177e4SLinus Torvalds 			task->tk_flags |= RPC_TASK_NOINTR;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds #ifdef RPC_DEBUG
7861da177e4SLinus Torvalds 	task->tk_magic = RPC_TASK_MAGIC_ID;
7871da177e4SLinus Torvalds 	task->tk_pid = rpc_task_id++;
7881da177e4SLinus Torvalds #endif
7891da177e4SLinus Torvalds 	/* Add to global list of all tasks */
7901da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
7911da177e4SLinus Torvalds 	list_add_tail(&task->tk_task, &all_tasks);
7921da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
7951da177e4SLinus Torvalds 				current->pid);
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds static struct rpc_task *
7991da177e4SLinus Torvalds rpc_alloc_task(void)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds static void
8051da177e4SLinus Torvalds rpc_default_free_task(struct rpc_task *task)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	dprintk("RPC: %4d freeing task\n", task->tk_pid);
8081da177e4SLinus Torvalds 	mempool_free(task, rpc_task_mempool);
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds /*
8121da177e4SLinus Torvalds  * Create a new task for the specified client.  We have to
8131da177e4SLinus Torvalds  * clean up after an allocation failure, as the client may
8141da177e4SLinus Torvalds  * have specified "oneshot".
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds struct rpc_task *
8171da177e4SLinus Torvalds rpc_new_task(struct rpc_clnt *clnt, rpc_action callback, int flags)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	struct rpc_task	*task;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	task = rpc_alloc_task();
8221da177e4SLinus Torvalds 	if (!task)
8231da177e4SLinus Torvalds 		goto cleanup;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	rpc_init_task(task, clnt, callback, flags);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/* Replace tk_release */
8281da177e4SLinus Torvalds 	task->tk_release = rpc_default_free_task;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	dprintk("RPC: %4d allocated task\n", task->tk_pid);
8311da177e4SLinus Torvalds 	task->tk_flags |= RPC_TASK_DYNAMIC;
8321da177e4SLinus Torvalds out:
8331da177e4SLinus Torvalds 	return task;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds cleanup:
8361da177e4SLinus Torvalds 	/* Check whether to release the client */
8371da177e4SLinus Torvalds 	if (clnt) {
8381da177e4SLinus Torvalds 		printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
8391da177e4SLinus Torvalds 			atomic_read(&clnt->cl_users), clnt->cl_oneshot);
8401da177e4SLinus Torvalds 		atomic_inc(&clnt->cl_users); /* pretend we were used ... */
8411da177e4SLinus Torvalds 		rpc_release_client(clnt);
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 	goto out;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds void rpc_release_task(struct rpc_task *task)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	dprintk("RPC: %4d release task\n", task->tk_pid);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds #ifdef RPC_DEBUG
8511da177e4SLinus Torvalds 	BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
8521da177e4SLinus Torvalds #endif
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	/* Remove from global task list */
8551da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
8561da177e4SLinus Torvalds 	list_del(&task->tk_task);
8571da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	BUG_ON (RPC_IS_QUEUED(task));
8601da177e4SLinus Torvalds 	task->tk_active = 0;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	/* Synchronously delete any running timer */
8631da177e4SLinus Torvalds 	rpc_delete_timer(task);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/* Release resources */
8661da177e4SLinus Torvalds 	if (task->tk_rqstp)
8671da177e4SLinus Torvalds 		xprt_release(task);
8681da177e4SLinus Torvalds 	if (task->tk_msg.rpc_cred)
8691da177e4SLinus Torvalds 		rpcauth_unbindcred(task);
8701da177e4SLinus Torvalds 	rpc_free(task);
8711da177e4SLinus Torvalds 	if (task->tk_client) {
8721da177e4SLinus Torvalds 		rpc_release_client(task->tk_client);
8731da177e4SLinus Torvalds 		task->tk_client = NULL;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds #ifdef RPC_DEBUG
8771da177e4SLinus Torvalds 	task->tk_magic = 0;
8781da177e4SLinus Torvalds #endif
8791da177e4SLinus Torvalds 	if (task->tk_release)
8801da177e4SLinus Torvalds 		task->tk_release(task);
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds /**
8841da177e4SLinus Torvalds  * rpc_find_parent - find the parent of a child task.
8851da177e4SLinus Torvalds  * @child: child task
8861da177e4SLinus Torvalds  *
8871da177e4SLinus Torvalds  * Checks that the parent task is still sleeping on the
8881da177e4SLinus Torvalds  * queue 'childq'. If so returns a pointer to the parent.
8891da177e4SLinus Torvalds  * Upon failure returns NULL.
8901da177e4SLinus Torvalds  *
8911da177e4SLinus Torvalds  * Caller must hold childq.lock
8921da177e4SLinus Torvalds  */
8931da177e4SLinus Torvalds static inline struct rpc_task *rpc_find_parent(struct rpc_task *child)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	struct rpc_task	*task, *parent;
8961da177e4SLinus Torvalds 	struct list_head *le;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	parent = (struct rpc_task *) child->tk_calldata;
8991da177e4SLinus Torvalds 	task_for_each(task, le, &childq.tasks[0])
9001da177e4SLinus Torvalds 		if (task == parent)
9011da177e4SLinus Torvalds 			return parent;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	return NULL;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds static void rpc_child_exit(struct rpc_task *child)
9071da177e4SLinus Torvalds {
9081da177e4SLinus Torvalds 	struct rpc_task	*parent;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	spin_lock_bh(&childq.lock);
9111da177e4SLinus Torvalds 	if ((parent = rpc_find_parent(child)) != NULL) {
9121da177e4SLinus Torvalds 		parent->tk_status = child->tk_status;
9131da177e4SLinus Torvalds 		__rpc_wake_up_task(parent);
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 	spin_unlock_bh(&childq.lock);
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds /*
9191da177e4SLinus Torvalds  * Note: rpc_new_task releases the client after a failure.
9201da177e4SLinus Torvalds  */
9211da177e4SLinus Torvalds struct rpc_task *
9221da177e4SLinus Torvalds rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds 	struct rpc_task	*task;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	task = rpc_new_task(clnt, NULL, RPC_TASK_ASYNC | RPC_TASK_CHILD);
9271da177e4SLinus Torvalds 	if (!task)
9281da177e4SLinus Torvalds 		goto fail;
9291da177e4SLinus Torvalds 	task->tk_exit = rpc_child_exit;
9301da177e4SLinus Torvalds 	task->tk_calldata = parent;
9311da177e4SLinus Torvalds 	return task;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds fail:
9341da177e4SLinus Torvalds 	parent->tk_status = -ENOMEM;
9351da177e4SLinus Torvalds 	return NULL;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds void rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 	spin_lock_bh(&childq.lock);
9411da177e4SLinus Torvalds 	/* N.B. Is it possible for the child to have already finished? */
9421da177e4SLinus Torvalds 	__rpc_sleep_on(&childq, task, func, NULL);
9431da177e4SLinus Torvalds 	rpc_schedule_run(child);
9441da177e4SLinus Torvalds 	spin_unlock_bh(&childq.lock);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds /*
9481da177e4SLinus Torvalds  * Kill all tasks for the given client.
9491da177e4SLinus Torvalds  * XXX: kill their descendants as well?
9501da177e4SLinus Torvalds  */
9511da177e4SLinus Torvalds void rpc_killall_tasks(struct rpc_clnt *clnt)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct rpc_task	*rovr;
9541da177e4SLinus Torvalds 	struct list_head *le;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	dprintk("RPC:      killing all tasks for client %p\n", clnt);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/*
9591da177e4SLinus Torvalds 	 * Spin lock all_tasks to prevent changes...
9601da177e4SLinus Torvalds 	 */
9611da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
9621da177e4SLinus Torvalds 	alltask_for_each(rovr, le, &all_tasks) {
9631da177e4SLinus Torvalds 		if (! RPC_IS_ACTIVATED(rovr))
9641da177e4SLinus Torvalds 			continue;
9651da177e4SLinus Torvalds 		if (!clnt || rovr->tk_client == clnt) {
9661da177e4SLinus Torvalds 			rovr->tk_flags |= RPC_TASK_KILLED;
9671da177e4SLinus Torvalds 			rpc_exit(rovr, -EIO);
9681da177e4SLinus Torvalds 			rpc_wake_up_task(rovr);
9691da177e4SLinus Torvalds 		}
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds static DECLARE_MUTEX_LOCKED(rpciod_running);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds static void rpciod_killall(void)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	unsigned long flags;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	while (!list_empty(&all_tasks)) {
9811da177e4SLinus Torvalds 		clear_thread_flag(TIF_SIGPENDING);
9821da177e4SLinus Torvalds 		rpc_killall_tasks(NULL);
9831da177e4SLinus Torvalds 		flush_workqueue(rpciod_workqueue);
9841da177e4SLinus Torvalds 		if (!list_empty(&all_tasks)) {
9851da177e4SLinus Torvalds 			dprintk("rpciod_killall: waiting for tasks to exit\n");
9861da177e4SLinus Torvalds 			yield();
9871da177e4SLinus Torvalds 		}
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
9911da177e4SLinus Torvalds 	recalc_sigpending();
9921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
9961da177e4SLinus Torvalds  * Start up the rpciod process if it's not already running.
9971da177e4SLinus Torvalds  */
9981da177e4SLinus Torvalds int
9991da177e4SLinus Torvalds rpciod_up(void)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	struct workqueue_struct *wq;
10021da177e4SLinus Torvalds 	int error = 0;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	down(&rpciod_sema);
10051da177e4SLinus Torvalds 	dprintk("rpciod_up: users %d\n", rpciod_users);
10061da177e4SLinus Torvalds 	rpciod_users++;
10071da177e4SLinus Torvalds 	if (rpciod_workqueue)
10081da177e4SLinus Torvalds 		goto out;
10091da177e4SLinus Torvalds 	/*
10101da177e4SLinus Torvalds 	 * If there's no pid, we should be the first user.
10111da177e4SLinus Torvalds 	 */
10121da177e4SLinus Torvalds 	if (rpciod_users > 1)
10131da177e4SLinus Torvalds 		printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
10141da177e4SLinus Torvalds 	/*
10151da177e4SLinus Torvalds 	 * Create the rpciod thread and wait for it to start.
10161da177e4SLinus Torvalds 	 */
10171da177e4SLinus Torvalds 	error = -ENOMEM;
10181da177e4SLinus Torvalds 	wq = create_workqueue("rpciod");
10191da177e4SLinus Torvalds 	if (wq == NULL) {
10201da177e4SLinus Torvalds 		printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
10211da177e4SLinus Torvalds 		rpciod_users--;
10221da177e4SLinus Torvalds 		goto out;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 	rpciod_workqueue = wq;
10251da177e4SLinus Torvalds 	error = 0;
10261da177e4SLinus Torvalds out:
10271da177e4SLinus Torvalds 	up(&rpciod_sema);
10281da177e4SLinus Torvalds 	return error;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds void
10321da177e4SLinus Torvalds rpciod_down(void)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	down(&rpciod_sema);
10351da177e4SLinus Torvalds 	dprintk("rpciod_down sema %d\n", rpciod_users);
10361da177e4SLinus Torvalds 	if (rpciod_users) {
10371da177e4SLinus Torvalds 		if (--rpciod_users)
10381da177e4SLinus Torvalds 			goto out;
10391da177e4SLinus Torvalds 	} else
10401da177e4SLinus Torvalds 		printk(KERN_WARNING "rpciod_down: no users??\n");
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	if (!rpciod_workqueue) {
10431da177e4SLinus Torvalds 		dprintk("rpciod_down: Nothing to do!\n");
10441da177e4SLinus Torvalds 		goto out;
10451da177e4SLinus Torvalds 	}
10461da177e4SLinus Torvalds 	rpciod_killall();
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	destroy_workqueue(rpciod_workqueue);
10491da177e4SLinus Torvalds 	rpciod_workqueue = NULL;
10501da177e4SLinus Torvalds  out:
10511da177e4SLinus Torvalds 	up(&rpciod_sema);
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds #ifdef RPC_DEBUG
10551da177e4SLinus Torvalds void rpc_show_tasks(void)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct list_head *le;
10581da177e4SLinus Torvalds 	struct rpc_task *t;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	spin_lock(&rpc_sched_lock);
10611da177e4SLinus Torvalds 	if (list_empty(&all_tasks)) {
10621da177e4SLinus Torvalds 		spin_unlock(&rpc_sched_lock);
10631da177e4SLinus Torvalds 		return;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds 	printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
10661da177e4SLinus Torvalds 		"-rpcwait -action- --exit--\n");
10671da177e4SLinus Torvalds 	alltask_for_each(t, le, &all_tasks) {
10681da177e4SLinus Torvalds 		const char *rpc_waitq = "none";
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 		if (RPC_IS_QUEUED(t))
10711da177e4SLinus Torvalds 			rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 		printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
10741da177e4SLinus Torvalds 			t->tk_pid,
10751da177e4SLinus Torvalds 			(t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
10761da177e4SLinus Torvalds 			t->tk_flags, t->tk_status,
10771da177e4SLinus Torvalds 			t->tk_client,
10781da177e4SLinus Torvalds 			(t->tk_client ? t->tk_client->cl_prog : 0),
10791da177e4SLinus Torvalds 			t->tk_rqstp, t->tk_timeout,
10801da177e4SLinus Torvalds 			rpc_waitq,
10811da177e4SLinus Torvalds 			t->tk_action, t->tk_exit);
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 	spin_unlock(&rpc_sched_lock);
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds #endif
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds void
10881da177e4SLinus Torvalds rpc_destroy_mempool(void)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	if (rpc_buffer_mempool)
10911da177e4SLinus Torvalds 		mempool_destroy(rpc_buffer_mempool);
10921da177e4SLinus Torvalds 	if (rpc_task_mempool)
10931da177e4SLinus Torvalds 		mempool_destroy(rpc_task_mempool);
10941da177e4SLinus Torvalds 	if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
10951da177e4SLinus Torvalds 		printk(KERN_INFO "rpc_task: not all structures were freed\n");
10961da177e4SLinus Torvalds 	if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
10971da177e4SLinus Torvalds 		printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds int
11011da177e4SLinus Torvalds rpc_init_mempool(void)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	rpc_task_slabp = kmem_cache_create("rpc_tasks",
11041da177e4SLinus Torvalds 					     sizeof(struct rpc_task),
11051da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
11061da177e4SLinus Torvalds 					     NULL, NULL);
11071da177e4SLinus Torvalds 	if (!rpc_task_slabp)
11081da177e4SLinus Torvalds 		goto err_nomem;
11091da177e4SLinus Torvalds 	rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
11101da177e4SLinus Torvalds 					     RPC_BUFFER_MAXSIZE,
11111da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
11121da177e4SLinus Torvalds 					     NULL, NULL);
11131da177e4SLinus Torvalds 	if (!rpc_buffer_slabp)
11141da177e4SLinus Torvalds 		goto err_nomem;
11151da177e4SLinus Torvalds 	rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
11161da177e4SLinus Torvalds 					    mempool_alloc_slab,
11171da177e4SLinus Torvalds 					    mempool_free_slab,
11181da177e4SLinus Torvalds 					    rpc_task_slabp);
11191da177e4SLinus Torvalds 	if (!rpc_task_mempool)
11201da177e4SLinus Torvalds 		goto err_nomem;
11211da177e4SLinus Torvalds 	rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
11221da177e4SLinus Torvalds 					    mempool_alloc_slab,
11231da177e4SLinus Torvalds 					    mempool_free_slab,
11241da177e4SLinus Torvalds 					    rpc_buffer_slabp);
11251da177e4SLinus Torvalds 	if (!rpc_buffer_mempool)
11261da177e4SLinus Torvalds 		goto err_nomem;
11271da177e4SLinus Torvalds 	return 0;
11281da177e4SLinus Torvalds err_nomem:
11291da177e4SLinus Torvalds 	rpc_destroy_mempool();
11301da177e4SLinus Torvalds 	return -ENOMEM;
11311da177e4SLinus Torvalds }
1132