blob: 29b1c1441f4de0b653d9a8eded7337d3a0bd9dd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/sched.c
3 *
4 * Scheduling for synchronous and asynchronous RPC requests.
5 *
6 * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10 */
11
12#include <linux/module.h>
13
14#include <linux/sched.h>
15#include <linux/interrupt.h>
16#include <linux/slab.h>
17#include <linux/mempool.h>
18#include <linux/smp.h>
19#include <linux/smp_lock.h>
20#include <linux/spinlock.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#ifdef RPC_DEBUG
26#define RPCDBG_FACILITY RPCDBG_SCHED
27#define RPC_TASK_MAGIC_ID 0xf00baa
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29
30/*
31 * RPC slabs and memory pools
32 */
33#define RPC_BUFFER_MAXSIZE (2048)
34#define RPC_BUFFER_POOLSIZE (8)
35#define RPC_TASK_POOLSIZE (8)
Christoph Lametere18b8902006-12-06 20:33:20 -080036static struct kmem_cache *rpc_task_slabp __read_mostly;
37static struct kmem_cache *rpc_buffer_slabp __read_mostly;
Eric Dumazetba899662005-08-26 12:05:31 -070038static mempool_t *rpc_task_mempool __read_mostly;
39static mempool_t *rpc_buffer_mempool __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Howells65f27f32006-11-22 14:55:48 +000041static void rpc_async_schedule(struct work_struct *);
Trond Myklebustbde8f002007-01-24 11:54:53 -080042static void rpc_release_task(struct rpc_task *task);
Trond Myklebust36df9aa2007-07-18 16:18:52 -040043static void __rpc_queue_timer_fn(unsigned long ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * RPC tasks sit here while waiting for conditions to improve.
47 */
Trond Myklebusta4a87492007-07-18 13:24:19 -040048static struct rpc_wait_queue delay_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * rpciod-related stuff
52 */
Trond Myklebust24c5d9d2006-03-20 13:44:08 -050053struct workqueue_struct *rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * Disable the timer for a given RPC task. Should be called with
57 * queue->lock and bh_disabled in order to avoid races within
58 * rpc_run_timer().
59 */
Trond Myklebust5d008372008-02-22 16:34:17 -050060static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070061__rpc_disable_timer(struct rpc_task *task)
62{
Trond Myklebust36df9aa2007-07-18 16:18:52 -040063 if (task->tk_timeout == 0)
64 return;
Chuck Lever46121cf2007-01-31 12:14:08 -050065 dprintk("RPC: %5u disabling timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 task->tk_timeout = 0;
Trond Myklebust36df9aa2007-07-18 16:18:52 -040067 list_del(&task->u.tk_wait.timer_list);
68}
69
70static void
71rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
72{
73 queue->timer_list.expires = expires;
74 mod_timer(&queue->timer_list.timer, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Set up a timer for the current task.
79 */
Trond Myklebust5d008372008-02-22 16:34:17 -050080static void
81__rpc_add_timer(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 if (!task->tk_timeout)
84 return;
85
Chuck Lever46121cf2007-01-31 12:14:08 -050086 dprintk("RPC: %5u setting alarm for %lu ms\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 task->tk_pid, task->tk_timeout * 1000 / HZ);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
90 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
91}
92
93/*
94 * Delete any timer for the current task. Because we use del_timer_sync(),
95 * this function should never be called while holding queue->lock.
96 */
97static void
98rpc_delete_timer(struct rpc_task *task)
99{
100 if (RPC_IS_QUEUED(task))
101 return;
102 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
103 del_singleshot_timer_sync(&task->tk_timer);
Chuck Lever46121cf2007-01-31 12:14:08 -0500104 dprintk("RPC: %5u deleting timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106}
107
108/*
109 * Add new request to a priority queue.
110 */
111static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
112{
113 struct list_head *q;
114 struct rpc_task *t;
115
116 INIT_LIST_HEAD(&task->u.tk_wait.links);
117 q = &queue->tasks[task->tk_priority];
118 if (unlikely(task->tk_priority > queue->maxpriority))
119 q = &queue->tasks[queue->maxpriority];
120 list_for_each_entry(t, q, u.tk_wait.list) {
Trond Myklebust3ff75762007-07-14 15:40:00 -0400121 if (t->tk_owner == task->tk_owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
123 return;
124 }
125 }
126 list_add_tail(&task->u.tk_wait.list, q);
127}
128
129/*
130 * Add new request to wait queue.
131 *
132 * Swapper tasks always get inserted at the head of the queue.
133 * This should avoid many nasty memory deadlocks and hopefully
134 * improve overall performance.
135 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
136 */
137static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
138{
139 BUG_ON (RPC_IS_QUEUED(task));
140
141 if (RPC_IS_PRIORITY(queue))
142 __rpc_add_wait_queue_priority(queue, task);
143 else if (RPC_IS_SWAPPER(task))
144 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
145 else
146 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500147 task->tk_waitqueue = queue;
Chuck Levere19b63d2006-03-20 13:44:15 -0500148 queue->qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 rpc_set_queued(task);
150
Chuck Lever46121cf2007-01-31 12:14:08 -0500151 dprintk("RPC: %5u added to queue %p \"%s\"\n",
152 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
156 * Remove request from a priority queue.
157 */
158static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
159{
160 struct rpc_task *t;
161
162 if (!list_empty(&task->u.tk_wait.links)) {
163 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
164 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
165 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/*
170 * Remove request from queue.
171 * Note: must be called with spin lock held.
172 */
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500173static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Trond Myklebust36df9aa2007-07-18 16:18:52 -0400175 __rpc_disable_timer(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (RPC_IS_PRIORITY(queue))
177 __rpc_remove_wait_queue_priority(task);
Trond Myklebust36df9aa2007-07-18 16:18:52 -0400178 list_del(&task->u.tk_wait.list);
Chuck Levere19b63d2006-03-20 13:44:15 -0500179 queue->qlen--;
Chuck Lever46121cf2007-01-31 12:14:08 -0500180 dprintk("RPC: %5u removed from queue %p \"%s\"\n",
181 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
185{
186 queue->priority = priority;
187 queue->count = 1 << (priority * 2);
188}
189
Trond Myklebust3ff75762007-07-14 15:40:00 -0400190static inline void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Trond Myklebust3ff75762007-07-14 15:40:00 -0400192 queue->owner = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 queue->nr = RPC_BATCH_COUNT;
194}
195
196static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
197{
198 rpc_set_waitqueue_priority(queue, queue->maxpriority);
Trond Myklebust3ff75762007-07-14 15:40:00 -0400199 rpc_set_waitqueue_owner(queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Trond Myklebust3ff75762007-07-14 15:40:00 -0400202static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int i;
205
206 spin_lock_init(&queue->lock);
207 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
208 INIT_LIST_HEAD(&queue->tasks[i]);
Trond Myklebust3ff75762007-07-14 15:40:00 -0400209 queue->maxpriority = nr_queues - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 rpc_reset_waitqueue_priority(queue);
Trond Myklebust36df9aa2007-07-18 16:18:52 -0400211 queue->qlen = 0;
212 setup_timer(&queue->timer_list.timer, __rpc_queue_timer_fn, (unsigned long)queue);
213 INIT_LIST_HEAD(&queue->timer_list.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#ifdef RPC_DEBUG
215 queue->name = qname;
216#endif
217}
218
219void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
220{
Trond Myklebust3ff75762007-07-14 15:40:00 -0400221 __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
225{
Trond Myklebust3ff75762007-07-14 15:40:00 -0400226 __rpc_init_priority_wait_queue(queue, qname, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400228EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500230void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
231{
Trond Myklebust36df9aa2007-07-18 16:18:52 -0400232 del_timer_sync(&queue->timer_list.timer);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500233}
234EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
235
Matthew Wilcox150030b2007-12-06 16:24:39 -0500236static int rpc_wait_bit_killable(void *word)
Trond Myklebust44c28872006-01-03 09:55:06 +0100237{
Matthew Wilcox150030b2007-12-06 16:24:39 -0500238 if (fatal_signal_pending(current))
Trond Myklebust44c28872006-01-03 09:55:06 +0100239 return -ERESTARTSYS;
240 schedule();
241 return 0;
242}
243
Trond Myklebustc44fe702007-06-16 14:17:01 -0400244#ifdef RPC_DEBUG
245static void rpc_task_set_debuginfo(struct rpc_task *task)
246{
247 static atomic_t rpc_pid;
248
249 task->tk_magic = RPC_TASK_MAGIC_ID;
250 task->tk_pid = atomic_inc_return(&rpc_pid);
251}
252#else
253static inline void rpc_task_set_debuginfo(struct rpc_task *task)
254{
255}
256#endif
257
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500258static void rpc_set_active(struct rpc_task *task)
259{
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400260 struct rpc_clnt *clnt;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500261 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
262 return;
Trond Myklebustc44fe702007-06-16 14:17:01 -0400263 rpc_task_set_debuginfo(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500264 /* Add to global list of all tasks */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400265 clnt = task->tk_client;
266 if (clnt != NULL) {
267 spin_lock(&clnt->cl_lock);
268 list_add_tail(&task->tk_task, &clnt->cl_tasks);
269 spin_unlock(&clnt->cl_lock);
270 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500271}
272
Trond Myklebust44c28872006-01-03 09:55:06 +0100273/*
274 * Mark an RPC call as having completed by clearing the 'active' bit
275 */
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500276static void rpc_mark_complete_task(struct rpc_task *task)
Trond Myklebust44c28872006-01-03 09:55:06 +0100277{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500278 smp_mb__before_clear_bit();
279 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
280 smp_mb__after_clear_bit();
Trond Myklebust44c28872006-01-03 09:55:06 +0100281 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
282}
283
284/*
285 * Allow callers to wait for completion of an RPC call
286 */
287int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
288{
289 if (action == NULL)
Matthew Wilcox150030b2007-12-06 16:24:39 -0500290 action = rpc_wait_bit_killable;
Trond Myklebust44c28872006-01-03 09:55:06 +0100291 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
Matthew Wilcox150030b2007-12-06 16:24:39 -0500292 action, TASK_KILLABLE);
Trond Myklebust44c28872006-01-03 09:55:06 +0100293}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400294EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
Trond Myklebust44c28872006-01-03 09:55:06 +0100295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * Make an RPC task runnable.
298 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800299 * Note: If the task is ASYNC, this must be called with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * the spinlock held to protect the wait queue operation.
301 */
302static void rpc_make_runnable(struct rpc_task *task)
303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 rpc_clear_queued(task);
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100305 if (rpc_test_and_set_running(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return;
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100307 /* We might have raced */
308 if (RPC_IS_QUEUED(task)) {
309 rpc_clear_running(task);
310 return;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (RPC_IS_ASYNC(task)) {
313 int status;
314
David Howells65f27f32006-11-22 14:55:48 +0000315 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
Trond Myklebust32bfb5c2008-02-19 20:04:21 -0500316 status = queue_work(rpciod_workqueue, &task->u.tk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (status < 0) {
318 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
319 task->tk_status = status;
320 return;
321 }
322 } else
Trond Myklebust96651ab2005-06-22 17:16:21 +0000323 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Prepare for sleeping on a wait queue.
328 * By always appending tasks to the list we ensure FIFO behavior.
329 * NB: An RPC task will only receive interrupt-driven events as long
330 * as it's on a wait queue.
331 */
332static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
Trond Myklebust5d008372008-02-22 16:34:17 -0500333 rpc_action action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Chuck Lever46121cf2007-01-31 12:14:08 -0500335 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
336 task->tk_pid, rpc_qname(q), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
339 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
340 return;
341 }
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 __rpc_add_wait_queue(q, task);
344
345 BUG_ON(task->tk_callback != NULL);
346 task->tk_callback = action;
Trond Myklebust5d008372008-02-22 16:34:17 -0500347 __rpc_add_timer(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
Trond Myklebust5d008372008-02-22 16:34:17 -0500351 rpc_action action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500353 /* Mark the task as being activated if so needed */
354 rpc_set_active(task);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /*
357 * Protect the queue operations.
358 */
359 spin_lock_bh(&q->lock);
Trond Myklebust5d008372008-02-22 16:34:17 -0500360 __rpc_sleep_on(q, task, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock_bh(&q->lock);
362}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400363EXPORT_SYMBOL_GPL(rpc_sleep_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365/**
366 * __rpc_do_wake_up_task - wake up a single rpc_task
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500367 * @queue: wait queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * @task: task to be woken up
369 *
370 * Caller must hold queue->lock, and have cleared the task queued flag.
371 */
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500372static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Chuck Lever46121cf2007-01-31 12:14:08 -0500374 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
375 task->tk_pid, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377#ifdef RPC_DEBUG
378 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
379#endif
380 /* Has the task been executed yet? If not, we cannot wake it up! */
381 if (!RPC_IS_ACTIVATED(task)) {
382 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
383 return;
384 }
385
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500386 __rpc_remove_wait_queue(queue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 rpc_make_runnable(task);
389
Chuck Lever46121cf2007-01-31 12:14:08 -0500390 dprintk("RPC: __rpc_wake_up_task done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500394 * Wake up a queued task while the queue lock is being held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500396static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500398 if (!RPC_IS_QUEUED(task) || task->tk_waitqueue != queue)
399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (rpc_start_wakeup(task)) {
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500401 __rpc_do_wake_up_task(queue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 rpc_finish_wakeup(task);
403 }
404}
405
406/*
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500407 * Wake up a task on a specific queue
408 */
409void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
410{
411 rcu_read_lock_bh();
412 spin_lock(&queue->lock);
413 rpc_wake_up_task_queue_locked(queue, task);
414 spin_unlock(&queue->lock);
415 rcu_read_unlock_bh();
416}
417EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
418
419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * Wake up the specified task
421 */
Trond Myklebustfda13932008-02-22 16:34:12 -0500422static void rpc_wake_up_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500424 rpc_wake_up_queued_task(task->tk_waitqueue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/*
428 * Wake up the next task on a priority queue.
429 */
430static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
431{
432 struct list_head *q;
433 struct rpc_task *task;
434
435 /*
Trond Myklebust3ff75762007-07-14 15:40:00 -0400436 * Service a batch of tasks from a single owner.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
438 q = &queue->tasks[queue->priority];
439 if (!list_empty(q)) {
440 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
Trond Myklebust3ff75762007-07-14 15:40:00 -0400441 if (queue->owner == task->tk_owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (--queue->nr)
443 goto out;
444 list_move_tail(&task->u.tk_wait.list, q);
445 }
446 /*
447 * Check if we need to switch queues.
448 */
449 if (--queue->count)
Trond Myklebust3ff75762007-07-14 15:40:00 -0400450 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
453 /*
454 * Service the next queue.
455 */
456 do {
457 if (q == &queue->tasks[0])
458 q = &queue->tasks[queue->maxpriority];
459 else
460 q = q - 1;
461 if (!list_empty(q)) {
462 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
463 goto new_queue;
464 }
465 } while (q != &queue->tasks[queue->priority]);
466
467 rpc_reset_waitqueue_priority(queue);
468 return NULL;
469
470new_queue:
471 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
Trond Myklebust3ff75762007-07-14 15:40:00 -0400472new_owner:
473 rpc_set_waitqueue_owner(queue, task->tk_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474out:
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500475 rpc_wake_up_task_queue_locked(queue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return task;
477}
478
479/*
480 * Wake up the next task on the wait queue.
481 */
482struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
483{
484 struct rpc_task *task = NULL;
485
Chuck Lever46121cf2007-01-31 12:14:08 -0500486 dprintk("RPC: wake_up_next(%p \"%s\")\n",
487 queue, rpc_qname(queue));
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500488 rcu_read_lock_bh();
489 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (RPC_IS_PRIORITY(queue))
491 task = __rpc_wake_up_next_priority(queue);
492 else {
493 task_for_first(task, &queue->tasks[0])
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500494 rpc_wake_up_task_queue_locked(queue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500496 spin_unlock(&queue->lock);
497 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 return task;
500}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400501EXPORT_SYMBOL_GPL(rpc_wake_up_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503/**
504 * rpc_wake_up - wake up all rpc_tasks
505 * @queue: rpc_wait_queue on which the tasks are sleeping
506 *
507 * Grabs queue->lock
508 */
509void rpc_wake_up(struct rpc_wait_queue *queue)
510{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800511 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct list_head *head;
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800513
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500514 rcu_read_lock_bh();
515 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 head = &queue->tasks[queue->maxpriority];
517 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800518 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500519 rpc_wake_up_task_queue_locked(queue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (head == &queue->tasks[0])
521 break;
522 head--;
523 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500524 spin_unlock(&queue->lock);
525 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400527EXPORT_SYMBOL_GPL(rpc_wake_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529/**
530 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
531 * @queue: rpc_wait_queue on which the tasks are sleeping
532 * @status: status value to set
533 *
534 * Grabs queue->lock
535 */
536void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
537{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800538 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500541 rcu_read_lock_bh();
542 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 head = &queue->tasks[queue->maxpriority];
544 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800545 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 task->tk_status = status;
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500547 rpc_wake_up_task_queue_locked(queue, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 if (head == &queue->tasks[0])
550 break;
551 head--;
552 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500553 spin_unlock(&queue->lock);
554 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400556EXPORT_SYMBOL_GPL(rpc_wake_up_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Trond Myklebustfde95c72008-02-22 15:09:26 -0500558/*
559 * Run a timeout function.
560 */
561static void rpc_run_timer(unsigned long ptr)
562{
563 struct rpc_task *task = (struct rpc_task *)ptr;
Trond Myklebust5d008372008-02-22 16:34:17 -0500564 struct rpc_wait_queue *queue = task->tk_waitqueue;
Trond Myklebustfde95c72008-02-22 15:09:26 -0500565
Trond Myklebust5d008372008-02-22 16:34:17 -0500566 spin_lock(&queue->lock);
567 if (RPC_IS_QUEUED(task) && task->tk_waitqueue == queue) {
568 dprintk("RPC: %5u timeout\n", task->tk_pid);
569 task->tk_status = -ETIMEDOUT;
Trond Myklebust96ef13b2008-02-22 15:46:41 -0500570 rpc_wake_up_task_queue_locked(queue, task);
Trond Myklebustfde95c72008-02-22 15:09:26 -0500571 }
Trond Myklebust5d008372008-02-22 16:34:17 -0500572 spin_unlock(&queue->lock);
Trond Myklebustfde95c72008-02-22 15:09:26 -0500573 smp_mb__before_clear_bit();
574 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
575 smp_mb__after_clear_bit();
576}
577
Trond Myklebust36df9aa2007-07-18 16:18:52 -0400578static void __rpc_queue_timer_fn(unsigned long ptr)
579{
580 struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
581 struct rpc_task *task, *n;
582 unsigned long expires, now, timeo;
583
584 spin_lock(&queue->lock);
585 expires = now = jiffies;
586 list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
587 timeo = task->u.tk_wait.expires;
588 if (time_after_eq(now, timeo)) {
589 list_del_init(&task->u.tk_wait.timer_list);
590 dprintk("RPC: %5u timeout\n", task->tk_pid);
591 task->tk_status = -ETIMEDOUT;
592 rpc_wake_up_task_queue_locked(queue, task);
593 continue;
594 }
595 if (expires == now || time_after(expires, timeo))
596 expires = timeo;
597 }
598 if (!list_empty(&queue->timer_list.list))
599 rpc_set_queue_timer(queue, expires);
600 spin_unlock(&queue->lock);
601}
602
Trond Myklebust80147932006-08-31 18:24:08 -0400603static void __rpc_atrun(struct rpc_task *task)
604{
Trond Myklebust5d008372008-02-22 16:34:17 -0500605 task->tk_status = 0;
Trond Myklebust80147932006-08-31 18:24:08 -0400606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Run a task at a later time
610 */
Trond Myklebust80147932006-08-31 18:24:08 -0400611void rpc_delay(struct rpc_task *task, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 task->tk_timeout = delay;
Trond Myklebust5d008372008-02-22 16:34:17 -0500614 rpc_sleep_on(&delay_queue, task, __rpc_atrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400616EXPORT_SYMBOL_GPL(rpc_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/*
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100619 * Helper to call task->tk_ops->rpc_call_prepare
620 */
621static void rpc_prepare_task(struct rpc_task *task)
622{
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400623 lock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100624 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400625 unlock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100626}
627
628/*
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100629 * Helper that calls task->tk_ops->rpc_call_done if it exists
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000630 */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100631void rpc_exit_task(struct rpc_task *task)
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000632{
Trond Myklebustabbcf282006-01-03 09:55:03 +0100633 task->tk_action = NULL;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100634 if (task->tk_ops->rpc_call_done != NULL) {
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400635 lock_kernel();
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100636 task->tk_ops->rpc_call_done(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400637 unlock_kernel();
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000638 if (task->tk_action != NULL) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100639 WARN_ON(RPC_ASSASSINATED(task));
640 /* Always release the RPC slot and buffer memory */
641 xprt_release(task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000642 }
643 }
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000644}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400645EXPORT_SYMBOL_GPL(rpc_exit_task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000646
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400647void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
648{
649 if (ops->rpc_release != NULL) {
650 lock_kernel();
651 ops->rpc_release(calldata);
652 unlock_kernel();
653 }
654}
655
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000656/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * This is the RPC `scheduler' (or rather, the finite state machine).
658 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800659static void __rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int status = 0;
662
Chuck Lever46121cf2007-01-31 12:14:08 -0500663 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
664 task->tk_pid, task->tk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 BUG_ON(RPC_IS_QUEUED(task));
667
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000668 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * Garbage collection of pending timers...
671 */
672 rpc_delete_timer(task);
673
674 /*
675 * Execute any pending callback.
676 */
677 if (RPC_DO_CALLBACK(task)) {
678 /* Define a callback save pointer */
679 void (*save_callback)(struct rpc_task *);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800680
681 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * If a callback exists, save it, reset it,
683 * call it.
684 * The save is needed to stop from resetting
685 * another callback set within the callback handler
686 * - Dave
687 */
688 save_callback=task->tk_callback;
689 task->tk_callback=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 save_callback(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 /*
694 * Perform the next FSM step.
695 * tk_action may be NULL when the task has been killed
696 * by someone else.
697 */
698 if (!RPC_IS_QUEUED(task)) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100699 if (task->tk_action == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
Trond Myklebustabbcf282006-01-03 09:55:03 +0100701 task->tk_action(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
704 /*
705 * Lockless check for whether task is sleeping or not.
706 */
707 if (!RPC_IS_QUEUED(task))
708 continue;
709 rpc_clear_running(task);
710 if (RPC_IS_ASYNC(task)) {
711 /* Careful! we may have raced... */
712 if (RPC_IS_QUEUED(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (rpc_test_and_set_running(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800715 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 continue;
717 }
718
719 /* sync task: sleep here */
Chuck Lever46121cf2007-01-31 12:14:08 -0500720 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000721 status = out_of_line_wait_on_bit(&task->tk_runstate,
Matthew Wilcox150030b2007-12-06 16:24:39 -0500722 RPC_TASK_QUEUED, rpc_wait_bit_killable,
723 TASK_KILLABLE);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000724 if (status == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * When a sync task receives a signal, it exits with
727 * -ERESTARTSYS. In order to catch any callbacks that
728 * clean up after sleeping on some queue, we don't
729 * break the loop here, but go around once more.
730 */
Chuck Lever46121cf2007-01-31 12:14:08 -0500731 dprintk("RPC: %5u got signal\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000732 task->tk_flags |= RPC_TASK_KILLED;
733 rpc_exit(task, -ERESTARTSYS);
734 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736 rpc_set_running(task);
Chuck Lever46121cf2007-01-31 12:14:08 -0500737 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Chuck Lever46121cf2007-01-31 12:14:08 -0500740 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
741 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* Release all resources associated with the task */
743 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/*
747 * User-visible entry point to the scheduler.
748 *
749 * This may be called recursively if e.g. an async NFS task updates
750 * the attributes and finds that dirty pages must be flushed.
751 * NOTE: Upon exit of this function the task is guaranteed to be
752 * released. In particular note that tk_release() will have
753 * been called, so your task memory may have been freed.
754 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800755void rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Trond Myklebust44c28872006-01-03 09:55:06 +0100757 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 rpc_set_running(task);
Trond Myklebust2efef832007-02-03 13:38:41 -0800759 __rpc_execute(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
David Howells65f27f32006-11-22 14:55:48 +0000762static void rpc_async_schedule(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
David Howells65f27f32006-11-22 14:55:48 +0000764 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400767struct rpc_buffer {
768 size_t len;
769 char data[];
770};
771
Chuck Lever02107142006-01-03 09:55:49 +0100772/**
773 * rpc_malloc - allocate an RPC buffer
774 * @task: RPC task that will use this buffer
775 * @size: requested byte size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400777 * To prevent rpciod from hanging, this allocator never sleeps,
778 * returning NULL if the request cannot be serviced immediately.
779 * The caller can arrange to sleep in a way that is safe for rpciod.
780 *
781 * Most requests are 'small' (under 2KiB) and can be serviced from a
782 * mempool, ensuring that NFS reads and writes can always proceed,
783 * and that there is good locality of reference for these buffers.
784 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * In order to avoid memory starvation triggering more writebacks of
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400786 * NFS requests, we avoid using GFP_KERNEL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400788void *rpc_malloc(struct rpc_task *task, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400790 struct rpc_buffer *buf;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400791 gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400793 size += sizeof(struct rpc_buffer);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400794 if (size <= RPC_BUFFER_MAXSIZE)
795 buf = mempool_alloc(rpc_buffer_mempool, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 else
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400797 buf = kmalloc(size, gfp);
Peter Zijlstraddce40d2007-05-09 08:30:11 +0200798
799 if (!buf)
800 return NULL;
801
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400802 buf->len = size;
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200803 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400804 task->tk_pid, size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400805 return &buf->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400807EXPORT_SYMBOL_GPL(rpc_malloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Chuck Lever02107142006-01-03 09:55:49 +0100809/**
810 * rpc_free - free buffer allocated via rpc_malloc
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400811 * @buffer: buffer to free
Chuck Lever02107142006-01-03 09:55:49 +0100812 *
813 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400814void rpc_free(void *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400816 size_t size;
817 struct rpc_buffer *buf;
Chuck Lever02107142006-01-03 09:55:49 +0100818
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400819 if (!buffer)
820 return;
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400821
822 buf = container_of(buffer, struct rpc_buffer, data);
823 size = buf->len;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400824
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200825 dprintk("RPC: freeing buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400826 size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400827
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400828 if (size <= RPC_BUFFER_MAXSIZE)
829 mempool_free(buf, rpc_buffer_mempool);
830 else
831 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400833EXPORT_SYMBOL_GPL(rpc_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835/*
836 * Creation and deletion of RPC task structures
837 */
Trond Myklebust47fe0642007-10-25 18:42:55 -0400838static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 memset(task, 0, sizeof(*task));
Trond Myklebustfde95c72008-02-22 15:09:26 -0500841 setup_timer(&task->tk_timer, rpc_run_timer, (unsigned long)task);
Trond Myklebust44c28872006-01-03 09:55:06 +0100842 atomic_set(&task->tk_count, 1);
Trond Myklebust84115e12007-07-14 15:39:59 -0400843 task->tk_flags = task_setup_data->flags;
844 task->tk_ops = task_setup_data->callback_ops;
845 task->tk_calldata = task_setup_data->callback_data;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400846 INIT_LIST_HEAD(&task->tk_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /* Initialize retry counters */
849 task->tk_garb_retry = 2;
850 task->tk_cred_retry = 2;
851
Trond Myklebust3ff75762007-07-14 15:40:00 -0400852 task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
853 task->tk_owner = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Initialize workqueue for async tasks */
Trond Myklebust32bfb5c2008-02-19 20:04:21 -0500856 task->tk_workqueue = task_setup_data->workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Trond Myklebust84115e12007-07-14 15:39:59 -0400858 task->tk_client = task_setup_data->rpc_client;
859 if (task->tk_client != NULL) {
860 kref_get(&task->tk_client->cl_kref);
861 if (task->tk_client->cl_softrtry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 task->tk_flags |= RPC_TASK_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Trond Myklebust84115e12007-07-14 15:39:59 -0400865 if (task->tk_ops->rpc_call_prepare != NULL)
866 task->tk_action = rpc_prepare_task;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100867
Trond Myklebustb3ef8b32007-10-25 18:32:34 -0400868 if (task_setup_data->rpc_message != NULL) {
869 memcpy(&task->tk_msg, task_setup_data->rpc_message, sizeof(task->tk_msg));
870 /* Bind the user cred */
871 if (task->tk_msg.rpc_cred != NULL)
872 rpcauth_holdcred(task);
873 else
874 rpcauth_bindcred(task);
875 if (task->tk_action == NULL)
876 rpc_call_start(task);
877 }
878
Chuck Leveref759a22006-03-20 13:44:17 -0500879 /* starting timestamp */
880 task->tk_start = jiffies;
881
Chuck Lever46121cf2007-01-31 12:14:08 -0500882 dprintk("RPC: new task initialized, procpid %u\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700883 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static struct rpc_task *
887rpc_alloc_task(void)
888{
889 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
890}
891
Trond Myklebust32bfb5c2008-02-19 20:04:21 -0500892static void rpc_free_task_rcu(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500894 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
Chuck Lever46121cf2007-01-31 12:14:08 -0500895 dprintk("RPC: %5u freeing task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 mempool_free(task, rpc_task_mempool);
897}
898
899/*
Trond Myklebust90c57552007-06-09 19:49:36 -0400900 * Create a new task for the specified client.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Trond Myklebust84115e12007-07-14 15:39:59 -0400902struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Trond Myklebuste8f5d772007-10-25 18:42:53 -0400904 struct rpc_task *task = setup_data->task;
905 unsigned short flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Trond Myklebuste8f5d772007-10-25 18:42:53 -0400907 if (task == NULL) {
908 task = rpc_alloc_task();
909 if (task == NULL)
910 goto out;
911 flags = RPC_TASK_DYNAMIC;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Trond Myklebust84115e12007-07-14 15:39:59 -0400914 rpc_init_task(task, setup_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Trond Myklebuste8f5d772007-10-25 18:42:53 -0400916 task->tk_flags |= flags;
Chuck Lever46121cf2007-01-31 12:14:08 -0500917 dprintk("RPC: allocated task %p\n", task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918out:
919 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebust32bfb5c2008-02-19 20:04:21 -0500922static void rpc_free_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100924 const struct rpc_call_ops *tk_ops = task->tk_ops;
925 void *calldata = task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Trond Myklebust32bfb5c2008-02-19 20:04:21 -0500927 if (task->tk_flags & RPC_TASK_DYNAMIC)
928 call_rcu_bh(&task->u.tk_rcu, rpc_free_task_rcu);
929 rpc_release_calldata(tk_ops, calldata);
930}
931
932static void rpc_async_release(struct work_struct *work)
933{
934 rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
935}
936
937void rpc_put_task(struct rpc_task *task)
938{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500939 if (!atomic_dec_and_test(&task->tk_count))
940 return;
941 /* Release resources */
942 if (task->tk_rqstp)
943 xprt_release(task);
944 if (task->tk_msg.rpc_cred)
945 rpcauth_unbindcred(task);
946 if (task->tk_client) {
947 rpc_release_client(task->tk_client);
948 task->tk_client = NULL;
949 }
Trond Myklebust32bfb5c2008-02-19 20:04:21 -0500950 if (task->tk_workqueue != NULL) {
951 INIT_WORK(&task->u.tk_work, rpc_async_release);
952 queue_work(task->tk_workqueue, &task->u.tk_work);
953 } else
954 rpc_free_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500955}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400956EXPORT_SYMBOL_GPL(rpc_put_task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500957
Trond Myklebustbde8f002007-01-24 11:54:53 -0800958static void rpc_release_task(struct rpc_task *task)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500959{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960#ifdef RPC_DEBUG
961 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
962#endif
Chuck Lever46121cf2007-01-31 12:14:08 -0500963 dprintk("RPC: %5u release task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Trond Myklebust6529eba2007-06-14 16:40:14 -0400965 if (!list_empty(&task->tk_task)) {
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400966 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400967 /* Remove from client task list */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400968 spin_lock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400969 list_del(&task->tk_task);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400970 spin_unlock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 BUG_ON (RPC_IS_QUEUED(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /* Synchronously delete any running timer */
975 rpc_delete_timer(task);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#ifdef RPC_DEBUG
978 task->tk_magic = 0;
979#endif
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500980 /* Wake up anyone who is waiting for task completion */
981 rpc_mark_complete_task(task);
982
983 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * Kill all tasks for the given client.
988 * XXX: kill their descendants as well?
989 */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400990void rpc_killall_tasks(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct rpc_task *rovr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400995 if (list_empty(&clnt->cl_tasks))
996 return;
997 dprintk("RPC: killing all tasks for client %p\n", clnt);
998 /*
999 * Spin lock all_tasks to prevent changes...
1000 */
1001 spin_lock(&clnt->cl_lock);
1002 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (! RPC_IS_ACTIVATED(rovr))
1004 continue;
Trond Myklebust6529eba2007-06-14 16:40:14 -04001005 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 rovr->tk_flags |= RPC_TASK_KILLED;
1007 rpc_exit(rovr, -EIO);
1008 rpc_wake_up_task(rovr);
1009 }
1010 }
Trond Myklebust4bef61f2007-06-16 14:17:01 -04001011 spin_unlock(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001013EXPORT_SYMBOL_GPL(rpc_killall_tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001015int rpciod_up(void)
1016{
1017 return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1018}
1019
1020void rpciod_down(void)
1021{
1022 module_put(THIS_MODULE);
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001026 * Start up the rpciod workqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001028static int rpciod_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct workqueue_struct *wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * Create the rpciod thread and wait for it to start.
1034 */
Trond Myklebustab418d72007-06-14 17:08:36 -04001035 dprintk("RPC: creating workqueue rpciod\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 wq = create_workqueue("rpciod");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 rpciod_workqueue = wq;
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001038 return rpciod_workqueue != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001041static void rpciod_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001043 struct workqueue_struct *wq = NULL;
Trond Myklebustab418d72007-06-14 17:08:36 -04001044
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001045 if (rpciod_workqueue == NULL)
1046 return;
Trond Myklebustab418d72007-06-14 17:08:36 -04001047 dprintk("RPC: destroying workqueue rpciod\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001049 wq = rpciod_workqueue;
1050 rpciod_workqueue = NULL;
1051 destroy_workqueue(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054void
1055rpc_destroy_mempool(void)
1056{
Trond Myklebustb247bbf2007-07-19 16:32:20 -04001057 rpciod_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (rpc_buffer_mempool)
1059 mempool_destroy(rpc_buffer_mempool);
1060 if (rpc_task_mempool)
1061 mempool_destroy(rpc_task_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001062 if (rpc_task_slabp)
1063 kmem_cache_destroy(rpc_task_slabp);
1064 if (rpc_buffer_slabp)
1065 kmem_cache_destroy(rpc_buffer_slabp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -05001066 rpc_destroy_wait_queue(&delay_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069int
1070rpc_init_mempool(void)
1071{
Trond Myklebustf6a1cc82008-02-22 17:06:55 -05001072 /*
1073 * The following is not strictly a mempool initialisation,
1074 * but there is no harm in doing it here
1075 */
1076 rpc_init_wait_queue(&delay_queue, "delayq");
1077 if (!rpciod_start())
1078 goto err_nomem;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1081 sizeof(struct rpc_task),
1082 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001083 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!rpc_task_slabp)
1085 goto err_nomem;
1086 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1087 RPC_BUFFER_MAXSIZE,
1088 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001089 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!rpc_buffer_slabp)
1091 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001092 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1093 rpc_task_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!rpc_task_mempool)
1095 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001096 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1097 rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!rpc_buffer_mempool)
1099 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return 0;
1101err_nomem:
1102 rpc_destroy_mempool();
1103 return -ENOMEM;
1104}