Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * 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 Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #ifdef RPC_DEBUG |
| 26 | #define RPCDBG_FACILITY RPCDBG_SCHED |
| 27 | #define RPC_TASK_MAGIC_ID 0xf00baa |
| 28 | static int rpc_task_id; |
| 29 | #endif |
| 30 | |
| 31 | /* |
| 32 | * RPC slabs and memory pools |
| 33 | */ |
| 34 | #define RPC_BUFFER_MAXSIZE (2048) |
| 35 | #define RPC_BUFFER_POOLSIZE (8) |
| 36 | #define RPC_TASK_POOLSIZE (8) |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 37 | static struct kmem_cache *rpc_task_slabp __read_mostly; |
| 38 | static struct kmem_cache *rpc_buffer_slabp __read_mostly; |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 39 | static mempool_t *rpc_task_mempool __read_mostly; |
| 40 | static mempool_t *rpc_buffer_mempool __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | static void __rpc_default_timer(struct rpc_task *task); |
| 43 | static void rpciod_killall(void); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 44 | static void rpc_async_schedule(struct work_struct *); |
Trond Myklebust | bde8f00 | 2007-01-24 11:54:53 -0800 | [diff] [blame] | 45 | static void rpc_release_task(struct rpc_task *task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * RPC tasks sit here while waiting for conditions to improve. |
| 49 | */ |
| 50 | static RPC_WAITQ(delay_queue, "delayq"); |
| 51 | |
| 52 | /* |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 53 | * All RPC clients are linked into this list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 55 | static LIST_HEAD(all_clients); |
| 56 | static DECLARE_WAIT_QUEUE_HEAD(client_kill_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * rpciod-related stuff |
| 60 | */ |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 61 | static DEFINE_MUTEX(rpciod_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static unsigned int rpciod_users; |
Trond Myklebust | 24c5d9d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 63 | struct workqueue_struct *rpciod_workqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Spinlock for other critical sections of code. |
| 67 | */ |
| 68 | static DEFINE_SPINLOCK(rpc_sched_lock); |
| 69 | |
| 70 | /* |
| 71 | * Disable the timer for a given RPC task. Should be called with |
| 72 | * queue->lock and bh_disabled in order to avoid races within |
| 73 | * rpc_run_timer(). |
| 74 | */ |
| 75 | static inline void |
| 76 | __rpc_disable_timer(struct rpc_task *task) |
| 77 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 78 | dprintk("RPC: %5u disabling timer\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | task->tk_timeout_fn = NULL; |
| 80 | task->tk_timeout = 0; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Run a timeout function. |
| 85 | * We use the callback in order to allow __rpc_wake_up_task() |
| 86 | * and friends to disable the timer synchronously on SMP systems |
| 87 | * without calling del_timer_sync(). The latter could cause a |
| 88 | * deadlock if called while we're holding spinlocks... |
| 89 | */ |
| 90 | static void rpc_run_timer(struct rpc_task *task) |
| 91 | { |
| 92 | void (*callback)(struct rpc_task *); |
| 93 | |
| 94 | callback = task->tk_timeout_fn; |
| 95 | task->tk_timeout_fn = NULL; |
| 96 | if (callback && RPC_IS_QUEUED(task)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 97 | dprintk("RPC: %5u running timer\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | callback(task); |
| 99 | } |
| 100 | smp_mb__before_clear_bit(); |
| 101 | clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate); |
| 102 | smp_mb__after_clear_bit(); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Set up a timer for the current task. |
| 107 | */ |
| 108 | static inline void |
| 109 | __rpc_add_timer(struct rpc_task *task, rpc_action timer) |
| 110 | { |
| 111 | if (!task->tk_timeout) |
| 112 | return; |
| 113 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 114 | dprintk("RPC: %5u setting alarm for %lu ms\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | task->tk_pid, task->tk_timeout * 1000 / HZ); |
| 116 | |
| 117 | if (timer) |
| 118 | task->tk_timeout_fn = timer; |
| 119 | else |
| 120 | task->tk_timeout_fn = __rpc_default_timer; |
| 121 | set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate); |
| 122 | mod_timer(&task->tk_timer, jiffies + task->tk_timeout); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Delete any timer for the current task. Because we use del_timer_sync(), |
| 127 | * this function should never be called while holding queue->lock. |
| 128 | */ |
| 129 | static void |
| 130 | rpc_delete_timer(struct rpc_task *task) |
| 131 | { |
| 132 | if (RPC_IS_QUEUED(task)) |
| 133 | return; |
| 134 | if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) { |
| 135 | del_singleshot_timer_sync(&task->tk_timer); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 136 | dprintk("RPC: %5u deleting timer\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Add new request to a priority queue. |
| 142 | */ |
| 143 | static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task) |
| 144 | { |
| 145 | struct list_head *q; |
| 146 | struct rpc_task *t; |
| 147 | |
| 148 | INIT_LIST_HEAD(&task->u.tk_wait.links); |
| 149 | q = &queue->tasks[task->tk_priority]; |
| 150 | if (unlikely(task->tk_priority > queue->maxpriority)) |
| 151 | q = &queue->tasks[queue->maxpriority]; |
| 152 | list_for_each_entry(t, q, u.tk_wait.list) { |
| 153 | if (t->tk_cookie == task->tk_cookie) { |
| 154 | list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links); |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | list_add_tail(&task->u.tk_wait.list, q); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Add new request to wait queue. |
| 163 | * |
| 164 | * Swapper tasks always get inserted at the head of the queue. |
| 165 | * This should avoid many nasty memory deadlocks and hopefully |
| 166 | * improve overall performance. |
| 167 | * Everyone else gets appended to the queue to ensure proper FIFO behavior. |
| 168 | */ |
| 169 | static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task) |
| 170 | { |
| 171 | BUG_ON (RPC_IS_QUEUED(task)); |
| 172 | |
| 173 | if (RPC_IS_PRIORITY(queue)) |
| 174 | __rpc_add_wait_queue_priority(queue, task); |
| 175 | else if (RPC_IS_SWAPPER(task)) |
| 176 | list_add(&task->u.tk_wait.list, &queue->tasks[0]); |
| 177 | else |
| 178 | list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]); |
| 179 | task->u.tk_wait.rpc_waitq = queue; |
Chuck Lever | e19b63d | 2006-03-20 13:44:15 -0500 | [diff] [blame] | 180 | queue->qlen++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | rpc_set_queued(task); |
| 182 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 183 | dprintk("RPC: %5u added to queue %p \"%s\"\n", |
| 184 | task->tk_pid, queue, rpc_qname(queue)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Remove request from a priority queue. |
| 189 | */ |
| 190 | static void __rpc_remove_wait_queue_priority(struct rpc_task *task) |
| 191 | { |
| 192 | struct rpc_task *t; |
| 193 | |
| 194 | if (!list_empty(&task->u.tk_wait.links)) { |
| 195 | t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list); |
| 196 | list_move(&t->u.tk_wait.list, &task->u.tk_wait.list); |
| 197 | list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links); |
| 198 | } |
| 199 | list_del(&task->u.tk_wait.list); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Remove request from queue. |
| 204 | * Note: must be called with spin lock held. |
| 205 | */ |
| 206 | static void __rpc_remove_wait_queue(struct rpc_task *task) |
| 207 | { |
| 208 | struct rpc_wait_queue *queue; |
| 209 | queue = task->u.tk_wait.rpc_waitq; |
| 210 | |
| 211 | if (RPC_IS_PRIORITY(queue)) |
| 212 | __rpc_remove_wait_queue_priority(task); |
| 213 | else |
| 214 | list_del(&task->u.tk_wait.list); |
Chuck Lever | e19b63d | 2006-03-20 13:44:15 -0500 | [diff] [blame] | 215 | queue->qlen--; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 216 | dprintk("RPC: %5u removed from queue %p \"%s\"\n", |
| 217 | task->tk_pid, queue, rpc_qname(queue)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority) |
| 221 | { |
| 222 | queue->priority = priority; |
| 223 | queue->count = 1 << (priority * 2); |
| 224 | } |
| 225 | |
| 226 | static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie) |
| 227 | { |
| 228 | queue->cookie = cookie; |
| 229 | queue->nr = RPC_BATCH_COUNT; |
| 230 | } |
| 231 | |
| 232 | static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue) |
| 233 | { |
| 234 | rpc_set_waitqueue_priority(queue, queue->maxpriority); |
| 235 | rpc_set_waitqueue_cookie(queue, 0); |
| 236 | } |
| 237 | |
| 238 | static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio) |
| 239 | { |
| 240 | int i; |
| 241 | |
| 242 | spin_lock_init(&queue->lock); |
| 243 | for (i = 0; i < ARRAY_SIZE(queue->tasks); i++) |
| 244 | INIT_LIST_HEAD(&queue->tasks[i]); |
| 245 | queue->maxpriority = maxprio; |
| 246 | rpc_reset_waitqueue_priority(queue); |
| 247 | #ifdef RPC_DEBUG |
| 248 | queue->name = qname; |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname) |
| 253 | { |
| 254 | __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH); |
| 255 | } |
| 256 | |
| 257 | void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname) |
| 258 | { |
| 259 | __rpc_init_priority_wait_queue(queue, qname, 0); |
| 260 | } |
| 261 | EXPORT_SYMBOL(rpc_init_wait_queue); |
| 262 | |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 263 | static int rpc_wait_bit_interruptible(void *word) |
| 264 | { |
| 265 | if (signal_pending(current)) |
| 266 | return -ERESTARTSYS; |
| 267 | schedule(); |
| 268 | return 0; |
| 269 | } |
| 270 | |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 271 | static void rpc_set_active(struct rpc_task *task) |
| 272 | { |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 273 | struct rpc_clnt *clnt; |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 274 | if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0) |
| 275 | return; |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 276 | #ifdef RPC_DEBUG |
| 277 | task->tk_magic = RPC_TASK_MAGIC_ID; |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 278 | spin_lock(&rpc_sched_lock); |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 279 | task->tk_pid = rpc_task_id++; |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 280 | spin_unlock(&rpc_sched_lock); |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 281 | #endif |
| 282 | /* Add to global list of all tasks */ |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 283 | clnt = task->tk_client; |
| 284 | if (clnt != NULL) { |
| 285 | spin_lock(&clnt->cl_lock); |
| 286 | list_add_tail(&task->tk_task, &clnt->cl_tasks); |
| 287 | spin_unlock(&clnt->cl_lock); |
| 288 | } |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 291 | /* |
| 292 | * Mark an RPC call as having completed by clearing the 'active' bit |
| 293 | */ |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 294 | static void rpc_mark_complete_task(struct rpc_task *task) |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 295 | { |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 296 | smp_mb__before_clear_bit(); |
| 297 | clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate); |
| 298 | smp_mb__after_clear_bit(); |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 299 | wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE); |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Allow callers to wait for completion of an RPC call |
| 304 | */ |
| 305 | int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *)) |
| 306 | { |
| 307 | if (action == NULL) |
| 308 | action = rpc_wait_bit_interruptible; |
| 309 | return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE, |
| 310 | action, TASK_INTERRUPTIBLE); |
| 311 | } |
| 312 | EXPORT_SYMBOL(__rpc_wait_for_completion_task); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | /* |
| 315 | * Make an RPC task runnable. |
| 316 | * |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 317 | * Note: If the task is ASYNC, this must be called with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | * the spinlock held to protect the wait queue operation. |
| 319 | */ |
| 320 | static void rpc_make_runnable(struct rpc_task *task) |
| 321 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | BUG_ON(task->tk_timeout_fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | rpc_clear_queued(task); |
Christophe Saout | cc4dc59 | 2006-11-05 18:42:48 +0100 | [diff] [blame] | 324 | if (rpc_test_and_set_running(task)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return; |
Christophe Saout | cc4dc59 | 2006-11-05 18:42:48 +0100 | [diff] [blame] | 326 | /* We might have raced */ |
| 327 | if (RPC_IS_QUEUED(task)) { |
| 328 | rpc_clear_running(task); |
| 329 | return; |
| 330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (RPC_IS_ASYNC(task)) { |
| 332 | int status; |
| 333 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 334 | INIT_WORK(&task->u.tk_work, rpc_async_schedule); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | status = queue_work(task->tk_workqueue, &task->u.tk_work); |
| 336 | if (status < 0) { |
| 337 | printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status); |
| 338 | task->tk_status = status; |
| 339 | return; |
| 340 | } |
| 341 | } else |
Trond Myklebust | 96651ab | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 342 | wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * Prepare for sleeping on a wait queue. |
| 347 | * By always appending tasks to the list we ensure FIFO behavior. |
| 348 | * NB: An RPC task will only receive interrupt-driven events as long |
| 349 | * as it's on a wait queue. |
| 350 | */ |
| 351 | static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, |
| 352 | rpc_action action, rpc_action timer) |
| 353 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 354 | dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n", |
| 355 | task->tk_pid, rpc_qname(q), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) { |
| 358 | printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n"); |
| 359 | return; |
| 360 | } |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | __rpc_add_wait_queue(q, task); |
| 363 | |
| 364 | BUG_ON(task->tk_callback != NULL); |
| 365 | task->tk_callback = action; |
| 366 | __rpc_add_timer(task, timer); |
| 367 | } |
| 368 | |
| 369 | void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, |
| 370 | rpc_action action, rpc_action timer) |
| 371 | { |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 372 | /* Mark the task as being activated if so needed */ |
| 373 | rpc_set_active(task); |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* |
| 376 | * Protect the queue operations. |
| 377 | */ |
| 378 | spin_lock_bh(&q->lock); |
| 379 | __rpc_sleep_on(q, task, action, timer); |
| 380 | spin_unlock_bh(&q->lock); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * __rpc_do_wake_up_task - wake up a single rpc_task |
| 385 | * @task: task to be woken up |
| 386 | * |
| 387 | * Caller must hold queue->lock, and have cleared the task queued flag. |
| 388 | */ |
| 389 | static void __rpc_do_wake_up_task(struct rpc_task *task) |
| 390 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 391 | dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n", |
| 392 | task->tk_pid, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | #ifdef RPC_DEBUG |
| 395 | BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID); |
| 396 | #endif |
| 397 | /* Has the task been executed yet? If not, we cannot wake it up! */ |
| 398 | if (!RPC_IS_ACTIVATED(task)) { |
| 399 | printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | __rpc_disable_timer(task); |
| 404 | __rpc_remove_wait_queue(task); |
| 405 | |
| 406 | rpc_make_runnable(task); |
| 407 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 408 | dprintk("RPC: __rpc_wake_up_task done\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Wake up the specified task |
| 413 | */ |
| 414 | static void __rpc_wake_up_task(struct rpc_task *task) |
| 415 | { |
| 416 | if (rpc_start_wakeup(task)) { |
| 417 | if (RPC_IS_QUEUED(task)) |
| 418 | __rpc_do_wake_up_task(task); |
| 419 | rpc_finish_wakeup(task); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Default timeout handler if none specified by user |
| 425 | */ |
| 426 | static void |
| 427 | __rpc_default_timer(struct rpc_task *task) |
| 428 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 429 | dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | task->tk_status = -ETIMEDOUT; |
| 431 | rpc_wake_up_task(task); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Wake up the specified task |
| 436 | */ |
| 437 | void rpc_wake_up_task(struct rpc_task *task) |
| 438 | { |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 439 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (rpc_start_wakeup(task)) { |
| 441 | if (RPC_IS_QUEUED(task)) { |
| 442 | struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq; |
| 443 | |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 444 | /* Note: we're already in a bh-safe context */ |
| 445 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | __rpc_do_wake_up_task(task); |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 447 | spin_unlock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | rpc_finish_wakeup(task); |
| 450 | } |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 451 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Wake up the next task on a priority queue. |
| 456 | */ |
| 457 | static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue) |
| 458 | { |
| 459 | struct list_head *q; |
| 460 | struct rpc_task *task; |
| 461 | |
| 462 | /* |
| 463 | * Service a batch of tasks from a single cookie. |
| 464 | */ |
| 465 | q = &queue->tasks[queue->priority]; |
| 466 | if (!list_empty(q)) { |
| 467 | task = list_entry(q->next, struct rpc_task, u.tk_wait.list); |
| 468 | if (queue->cookie == task->tk_cookie) { |
| 469 | if (--queue->nr) |
| 470 | goto out; |
| 471 | list_move_tail(&task->u.tk_wait.list, q); |
| 472 | } |
| 473 | /* |
| 474 | * Check if we need to switch queues. |
| 475 | */ |
| 476 | if (--queue->count) |
| 477 | goto new_cookie; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Service the next queue. |
| 482 | */ |
| 483 | do { |
| 484 | if (q == &queue->tasks[0]) |
| 485 | q = &queue->tasks[queue->maxpriority]; |
| 486 | else |
| 487 | q = q - 1; |
| 488 | if (!list_empty(q)) { |
| 489 | task = list_entry(q->next, struct rpc_task, u.tk_wait.list); |
| 490 | goto new_queue; |
| 491 | } |
| 492 | } while (q != &queue->tasks[queue->priority]); |
| 493 | |
| 494 | rpc_reset_waitqueue_priority(queue); |
| 495 | return NULL; |
| 496 | |
| 497 | new_queue: |
| 498 | rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0])); |
| 499 | new_cookie: |
| 500 | rpc_set_waitqueue_cookie(queue, task->tk_cookie); |
| 501 | out: |
| 502 | __rpc_wake_up_task(task); |
| 503 | return task; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Wake up the next task on the wait queue. |
| 508 | */ |
| 509 | struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue) |
| 510 | { |
| 511 | struct rpc_task *task = NULL; |
| 512 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 513 | dprintk("RPC: wake_up_next(%p \"%s\")\n", |
| 514 | queue, rpc_qname(queue)); |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 515 | rcu_read_lock_bh(); |
| 516 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | if (RPC_IS_PRIORITY(queue)) |
| 518 | task = __rpc_wake_up_next_priority(queue); |
| 519 | else { |
| 520 | task_for_first(task, &queue->tasks[0]) |
| 521 | __rpc_wake_up_task(task); |
| 522 | } |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 523 | spin_unlock(&queue->lock); |
| 524 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | return task; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * rpc_wake_up - wake up all rpc_tasks |
| 531 | * @queue: rpc_wait_queue on which the tasks are sleeping |
| 532 | * |
| 533 | * Grabs queue->lock |
| 534 | */ |
| 535 | void rpc_wake_up(struct rpc_wait_queue *queue) |
| 536 | { |
Trond Myklebust | e6d83d5 | 2006-03-13 21:20:48 -0800 | [diff] [blame] | 537 | struct rpc_task *task, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | struct list_head *head; |
Trond Myklebust | e6d83d5 | 2006-03-13 21:20:48 -0800 | [diff] [blame] | 539 | |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 540 | rcu_read_lock_bh(); |
| 541 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | head = &queue->tasks[queue->maxpriority]; |
| 543 | for (;;) { |
Trond Myklebust | e6d83d5 | 2006-03-13 21:20:48 -0800 | [diff] [blame] | 544 | list_for_each_entry_safe(task, next, head, u.tk_wait.list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | __rpc_wake_up_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (head == &queue->tasks[0]) |
| 547 | break; |
| 548 | head--; |
| 549 | } |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 550 | spin_unlock(&queue->lock); |
| 551 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
| 555 | * rpc_wake_up_status - wake up all rpc_tasks and set their status value. |
| 556 | * @queue: rpc_wait_queue on which the tasks are sleeping |
| 557 | * @status: status value to set |
| 558 | * |
| 559 | * Grabs queue->lock |
| 560 | */ |
| 561 | void rpc_wake_up_status(struct rpc_wait_queue *queue, int status) |
| 562 | { |
Trond Myklebust | e6d83d5 | 2006-03-13 21:20:48 -0800 | [diff] [blame] | 563 | struct rpc_task *task, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 566 | rcu_read_lock_bh(); |
| 567 | spin_lock(&queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | head = &queue->tasks[queue->maxpriority]; |
| 569 | for (;;) { |
Trond Myklebust | e6d83d5 | 2006-03-13 21:20:48 -0800 | [diff] [blame] | 570 | list_for_each_entry_safe(task, next, head, u.tk_wait.list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | task->tk_status = status; |
| 572 | __rpc_wake_up_task(task); |
| 573 | } |
| 574 | if (head == &queue->tasks[0]) |
| 575 | break; |
| 576 | head--; |
| 577 | } |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 578 | spin_unlock(&queue->lock); |
| 579 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Trond Myklebust | 8014793 | 2006-08-31 18:24:08 -0400 | [diff] [blame] | 582 | static void __rpc_atrun(struct rpc_task *task) |
| 583 | { |
| 584 | rpc_wake_up_task(task); |
| 585 | } |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | /* |
| 588 | * Run a task at a later time |
| 589 | */ |
Trond Myklebust | 8014793 | 2006-08-31 18:24:08 -0400 | [diff] [blame] | 590 | void rpc_delay(struct rpc_task *task, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
| 592 | task->tk_timeout = delay; |
| 593 | rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun); |
| 594 | } |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | /* |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 597 | * Helper to call task->tk_ops->rpc_call_prepare |
| 598 | */ |
| 599 | static void rpc_prepare_task(struct rpc_task *task) |
| 600 | { |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 601 | lock_kernel(); |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 602 | task->tk_ops->rpc_call_prepare(task, task->tk_calldata); |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 603 | unlock_kernel(); |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 607 | * Helper that calls task->tk_ops->rpc_call_done if it exists |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 608 | */ |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 609 | void rpc_exit_task(struct rpc_task *task) |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 610 | { |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 611 | task->tk_action = NULL; |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 612 | if (task->tk_ops->rpc_call_done != NULL) { |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 613 | lock_kernel(); |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 614 | task->tk_ops->rpc_call_done(task, task->tk_calldata); |
Trond Myklebust | 6d5fcb5 | 2006-10-18 16:01:06 -0400 | [diff] [blame] | 615 | unlock_kernel(); |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 616 | if (task->tk_action != NULL) { |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 617 | WARN_ON(RPC_ASSASSINATED(task)); |
| 618 | /* Always release the RPC slot and buffer memory */ |
| 619 | xprt_release(task); |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 622 | } |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 623 | EXPORT_SYMBOL(rpc_exit_task); |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 624 | |
Trond Myklebust | bbd5a1f | 2006-10-18 16:01:05 -0400 | [diff] [blame] | 625 | void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) |
| 626 | { |
| 627 | if (ops->rpc_release != NULL) { |
| 628 | lock_kernel(); |
| 629 | ops->rpc_release(calldata); |
| 630 | unlock_kernel(); |
| 631 | } |
| 632 | } |
| 633 | |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 634 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | * This is the RPC `scheduler' (or rather, the finite state machine). |
| 636 | */ |
Trond Myklebust | 2efef83 | 2007-02-03 13:38:41 -0800 | [diff] [blame] | 637 | static void __rpc_execute(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
| 639 | int status = 0; |
| 640 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 641 | dprintk("RPC: %5u __rpc_execute flags=0x%x\n", |
| 642 | task->tk_pid, task->tk_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | BUG_ON(RPC_IS_QUEUED(task)); |
| 645 | |
Trond Myklebust | d05fdb0 | 2005-06-22 17:16:19 +0000 | [diff] [blame] | 646 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | /* |
| 648 | * Garbage collection of pending timers... |
| 649 | */ |
| 650 | rpc_delete_timer(task); |
| 651 | |
| 652 | /* |
| 653 | * Execute any pending callback. |
| 654 | */ |
| 655 | if (RPC_DO_CALLBACK(task)) { |
| 656 | /* Define a callback save pointer */ |
| 657 | void (*save_callback)(struct rpc_task *); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 658 | |
| 659 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * If a callback exists, save it, reset it, |
| 661 | * call it. |
| 662 | * The save is needed to stop from resetting |
| 663 | * another callback set within the callback handler |
| 664 | * - Dave |
| 665 | */ |
| 666 | save_callback=task->tk_callback; |
| 667 | task->tk_callback=NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | save_callback(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Perform the next FSM step. |
| 673 | * tk_action may be NULL when the task has been killed |
| 674 | * by someone else. |
| 675 | */ |
| 676 | if (!RPC_IS_QUEUED(task)) { |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 677 | if (task->tk_action == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | break; |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 679 | task->tk_action(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Lockless check for whether task is sleeping or not. |
| 684 | */ |
| 685 | if (!RPC_IS_QUEUED(task)) |
| 686 | continue; |
| 687 | rpc_clear_running(task); |
| 688 | if (RPC_IS_ASYNC(task)) { |
| 689 | /* Careful! we may have raced... */ |
| 690 | if (RPC_IS_QUEUED(task)) |
Trond Myklebust | 2efef83 | 2007-02-03 13:38:41 -0800 | [diff] [blame] | 691 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | if (rpc_test_and_set_running(task)) |
Trond Myklebust | 2efef83 | 2007-02-03 13:38:41 -0800 | [diff] [blame] | 693 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | continue; |
| 695 | } |
| 696 | |
| 697 | /* sync task: sleep here */ |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 698 | dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid); |
Trond Myklebust | 96651ab | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 699 | /* Note: Caller should be using rpc_clnt_sigmask() */ |
| 700 | status = out_of_line_wait_on_bit(&task->tk_runstate, |
| 701 | RPC_TASK_QUEUED, rpc_wait_bit_interruptible, |
| 702 | TASK_INTERRUPTIBLE); |
| 703 | if (status == -ERESTARTSYS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | /* |
| 705 | * When a sync task receives a signal, it exits with |
| 706 | * -ERESTARTSYS. In order to catch any callbacks that |
| 707 | * clean up after sleeping on some queue, we don't |
| 708 | * break the loop here, but go around once more. |
| 709 | */ |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 710 | dprintk("RPC: %5u got signal\n", task->tk_pid); |
Trond Myklebust | 96651ab | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 711 | task->tk_flags |= RPC_TASK_KILLED; |
| 712 | rpc_exit(task, -ERESTARTSYS); |
| 713 | rpc_wake_up_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | rpc_set_running(task); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 716 | dprintk("RPC: %5u sync task resuming\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 719 | dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status, |
| 720 | task->tk_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | /* Release all resources associated with the task */ |
| 722 | rpc_release_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /* |
| 726 | * User-visible entry point to the scheduler. |
| 727 | * |
| 728 | * This may be called recursively if e.g. an async NFS task updates |
| 729 | * the attributes and finds that dirty pages must be flushed. |
| 730 | * NOTE: Upon exit of this function the task is guaranteed to be |
| 731 | * released. In particular note that tk_release() will have |
| 732 | * been called, so your task memory may have been freed. |
| 733 | */ |
Trond Myklebust | 2efef83 | 2007-02-03 13:38:41 -0800 | [diff] [blame] | 734 | void rpc_execute(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 736 | rpc_set_active(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | rpc_set_running(task); |
Trond Myklebust | 2efef83 | 2007-02-03 13:38:41 -0800 | [diff] [blame] | 738 | __rpc_execute(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
| 740 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 741 | static void rpc_async_schedule(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 743 | __rpc_execute(container_of(work, struct rpc_task, u.tk_work)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 746 | struct rpc_buffer { |
| 747 | size_t len; |
| 748 | char data[]; |
| 749 | }; |
| 750 | |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 751 | /** |
| 752 | * rpc_malloc - allocate an RPC buffer |
| 753 | * @task: RPC task that will use this buffer |
| 754 | * @size: requested byte size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | * |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 756 | * To prevent rpciod from hanging, this allocator never sleeps, |
| 757 | * returning NULL if the request cannot be serviced immediately. |
| 758 | * The caller can arrange to sleep in a way that is safe for rpciod. |
| 759 | * |
| 760 | * Most requests are 'small' (under 2KiB) and can be serviced from a |
| 761 | * mempool, ensuring that NFS reads and writes can always proceed, |
| 762 | * and that there is good locality of reference for these buffers. |
| 763 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | * In order to avoid memory starvation triggering more writebacks of |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 765 | * NFS requests, we avoid using GFP_KERNEL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | */ |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 767 | void *rpc_malloc(struct rpc_task *task, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | { |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 769 | struct rpc_buffer *buf; |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 770 | gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 772 | size += sizeof(struct rpc_buffer); |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 773 | if (size <= RPC_BUFFER_MAXSIZE) |
| 774 | buf = mempool_alloc(rpc_buffer_mempool, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | else |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 776 | buf = kmalloc(size, gfp); |
Peter Zijlstra | ddce40d | 2007-05-09 08:30:11 +0200 | [diff] [blame] | 777 | |
| 778 | if (!buf) |
| 779 | return NULL; |
| 780 | |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 781 | buf->len = size; |
Geert Uytterhoeven | 215d067 | 2007-05-08 11:37:26 +0200 | [diff] [blame] | 782 | dprintk("RPC: %5u allocated buffer of size %zu at %p\n", |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 783 | task->tk_pid, size, buf); |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 784 | return &buf->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 787 | /** |
| 788 | * rpc_free - free buffer allocated via rpc_malloc |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 789 | * @buffer: buffer to free |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 790 | * |
| 791 | */ |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 792 | void rpc_free(void *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 794 | size_t size; |
| 795 | struct rpc_buffer *buf; |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 796 | |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 797 | if (!buffer) |
| 798 | return; |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 799 | |
| 800 | buf = container_of(buffer, struct rpc_buffer, data); |
| 801 | size = buf->len; |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 802 | |
Geert Uytterhoeven | 215d067 | 2007-05-08 11:37:26 +0200 | [diff] [blame] | 803 | dprintk("RPC: freeing buffer of size %zu at %p\n", |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 804 | size, buf); |
Chuck Lever | aa3d1fa | 2007-05-08 18:23:28 -0400 | [diff] [blame] | 805 | |
Chuck Lever | c5a4dd8 | 2007-03-29 16:47:58 -0400 | [diff] [blame] | 806 | if (size <= RPC_BUFFER_MAXSIZE) |
| 807 | mempool_free(buf, rpc_buffer_mempool); |
| 808 | else |
| 809 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Creation and deletion of RPC task structures |
| 814 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 815 | void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
| 817 | memset(task, 0, sizeof(*task)); |
| 818 | init_timer(&task->tk_timer); |
| 819 | task->tk_timer.data = (unsigned long) task; |
| 820 | task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer; |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 821 | atomic_set(&task->tk_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | task->tk_client = clnt; |
| 823 | task->tk_flags = flags; |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 824 | task->tk_ops = tk_ops; |
Trond Myklebust | 4ce70ad | 2006-01-03 09:55:05 +0100 | [diff] [blame] | 825 | if (tk_ops->rpc_call_prepare != NULL) |
| 826 | task->tk_action = rpc_prepare_task; |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 827 | task->tk_calldata = calldata; |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 828 | INIT_LIST_HEAD(&task->tk_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | /* Initialize retry counters */ |
| 831 | task->tk_garb_retry = 2; |
| 832 | task->tk_cred_retry = 2; |
| 833 | |
| 834 | task->tk_priority = RPC_PRIORITY_NORMAL; |
| 835 | task->tk_cookie = (unsigned long)current; |
| 836 | |
| 837 | /* Initialize workqueue for async tasks */ |
| 838 | task->tk_workqueue = rpciod_workqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | if (clnt) { |
| 841 | atomic_inc(&clnt->cl_users); |
| 842 | if (clnt->cl_softrtry) |
| 843 | task->tk_flags |= RPC_TASK_SOFT; |
| 844 | if (!clnt->cl_intr) |
| 845 | task->tk_flags |= RPC_TASK_NOINTR; |
| 846 | } |
| 847 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 848 | BUG_ON(task->tk_ops == NULL); |
| 849 | |
Chuck Lever | ef759a2 | 2006-03-20 13:44:17 -0500 | [diff] [blame] | 850 | /* starting timestamp */ |
| 851 | task->tk_start = jiffies; |
| 852 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 853 | dprintk("RPC: new task initialized, procpid %u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | current->pid); |
| 855 | } |
| 856 | |
| 857 | static struct rpc_task * |
| 858 | rpc_alloc_task(void) |
| 859 | { |
| 860 | return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS); |
| 861 | } |
| 862 | |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 863 | static void rpc_free_task(struct rcu_head *rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 865 | struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 866 | dprintk("RPC: %5u freeing task\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | mempool_free(task, rpc_task_mempool); |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Create a new task for the specified client. We have to |
| 872 | * clean up after an allocation failure, as the client may |
| 873 | * have specified "oneshot". |
| 874 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 875 | struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | { |
| 877 | struct rpc_task *task; |
| 878 | |
| 879 | task = rpc_alloc_task(); |
| 880 | if (!task) |
| 881 | goto cleanup; |
| 882 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 883 | rpc_init_task(task, clnt, flags, tk_ops, calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 885 | dprintk("RPC: allocated task %p\n", task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | task->tk_flags |= RPC_TASK_DYNAMIC; |
| 887 | out: |
| 888 | return task; |
| 889 | |
| 890 | cleanup: |
| 891 | /* Check whether to release the client */ |
| 892 | if (clnt) { |
| 893 | printk("rpc_new_task: failed, users=%d, oneshot=%d\n", |
| 894 | atomic_read(&clnt->cl_users), clnt->cl_oneshot); |
| 895 | atomic_inc(&clnt->cl_users); /* pretend we were used ... */ |
| 896 | rpc_release_client(clnt); |
| 897 | } |
| 898 | goto out; |
| 899 | } |
| 900 | |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 901 | |
| 902 | void rpc_put_task(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 904 | const struct rpc_call_ops *tk_ops = task->tk_ops; |
| 905 | void *calldata = task->tk_calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 907 | if (!atomic_dec_and_test(&task->tk_count)) |
| 908 | return; |
| 909 | /* Release resources */ |
| 910 | if (task->tk_rqstp) |
| 911 | xprt_release(task); |
| 912 | if (task->tk_msg.rpc_cred) |
| 913 | rpcauth_unbindcred(task); |
| 914 | if (task->tk_client) { |
| 915 | rpc_release_client(task->tk_client); |
| 916 | task->tk_client = NULL; |
| 917 | } |
| 918 | if (task->tk_flags & RPC_TASK_DYNAMIC) |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 919 | call_rcu_bh(&task->u.tk_rcu, rpc_free_task); |
Trond Myklebust | bbd5a1f | 2006-10-18 16:01:05 -0400 | [diff] [blame] | 920 | rpc_release_calldata(tk_ops, calldata); |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 921 | } |
| 922 | EXPORT_SYMBOL(rpc_put_task); |
| 923 | |
Trond Myklebust | bde8f00 | 2007-01-24 11:54:53 -0800 | [diff] [blame] | 924 | static void rpc_release_task(struct rpc_task *task) |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 925 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | #ifdef RPC_DEBUG |
| 927 | BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID); |
| 928 | #endif |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 929 | dprintk("RPC: %5u release task\n", task->tk_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 931 | if (!list_empty(&task->tk_task)) { |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 932 | struct rpc_clnt *clnt = task->tk_client; |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 933 | /* Remove from client task list */ |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 934 | spin_lock(&clnt->cl_lock); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 935 | list_del(&task->tk_task); |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 936 | spin_unlock(&clnt->cl_lock); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | BUG_ON (RPC_IS_QUEUED(task)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | /* Synchronously delete any running timer */ |
| 941 | rpc_delete_timer(task); |
| 942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | #ifdef RPC_DEBUG |
| 944 | task->tk_magic = 0; |
| 945 | #endif |
Trond Myklebust | e6b3c4d | 2006-11-11 22:18:03 -0500 | [diff] [blame] | 946 | /* Wake up anyone who is waiting for task completion */ |
| 947 | rpc_mark_complete_task(task); |
| 948 | |
| 949 | rpc_put_task(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /** |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 953 | * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it |
Martin Waitz | 99acf04 | 2006-02-01 03:06:56 -0800 | [diff] [blame] | 954 | * @clnt: pointer to RPC client |
| 955 | * @flags: RPC flags |
| 956 | * @ops: RPC call ops |
| 957 | * @data: user call data |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 958 | */ |
| 959 | struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags, |
| 960 | const struct rpc_call_ops *ops, |
| 961 | void *data) |
| 962 | { |
| 963 | struct rpc_task *task; |
| 964 | task = rpc_new_task(clnt, flags, ops, data); |
Trond Myklebust | 7a1218a | 2006-03-20 18:11:10 -0500 | [diff] [blame] | 965 | if (task == NULL) { |
Trond Myklebust | bbd5a1f | 2006-10-18 16:01:05 -0400 | [diff] [blame] | 966 | rpc_release_calldata(ops, data); |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 967 | return ERR_PTR(-ENOMEM); |
Trond Myklebust | 7a1218a | 2006-03-20 18:11:10 -0500 | [diff] [blame] | 968 | } |
Trond Myklebust | 44c2887 | 2006-01-03 09:55:06 +0100 | [diff] [blame] | 969 | atomic_inc(&task->tk_count); |
| 970 | rpc_execute(task); |
| 971 | return task; |
| 972 | } |
| 973 | EXPORT_SYMBOL(rpc_run_task); |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* |
| 976 | * Kill all tasks for the given client. |
| 977 | * XXX: kill their descendants as well? |
| 978 | */ |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 979 | void rpc_killall_tasks(struct rpc_clnt *clnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | { |
| 981 | struct rpc_task *rovr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 984 | if (list_empty(&clnt->cl_tasks)) |
| 985 | return; |
| 986 | dprintk("RPC: killing all tasks for client %p\n", clnt); |
| 987 | /* |
| 988 | * Spin lock all_tasks to prevent changes... |
| 989 | */ |
| 990 | spin_lock(&clnt->cl_lock); |
| 991 | list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | if (! RPC_IS_ACTIVATED(rovr)) |
| 993 | continue; |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 994 | if (!(rovr->tk_flags & RPC_TASK_KILLED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | rovr->tk_flags |= RPC_TASK_KILLED; |
| 996 | rpc_exit(rovr, -EIO); |
| 997 | rpc_wake_up_task(rovr); |
| 998 | } |
| 999 | } |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 1000 | spin_unlock(&clnt->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | static void rpciod_killall(void) |
| 1004 | { |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1005 | struct rpc_clnt *clnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | unsigned long flags; |
| 1007 | |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1008 | for(;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | clear_thread_flag(TIF_SIGPENDING); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1010 | |
| 1011 | spin_lock(&rpc_sched_lock); |
| 1012 | list_for_each_entry(clnt, &all_clients, cl_clients) |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 1013 | rpc_killall_tasks(clnt); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1014 | spin_unlock(&rpc_sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | flush_workqueue(rpciod_workqueue); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1016 | if (!list_empty(&all_clients)) |
| 1017 | break; |
| 1018 | dprintk("RPC: rpciod_killall: waiting for tasks " |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1019 | "to exit\n"); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1020 | wait_event_timeout(client_kill_wait, |
| 1021 | list_empty(&all_clients), 1*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 1025 | recalc_sigpending(); |
| 1026 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 1027 | } |
| 1028 | |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1029 | void rpc_register_client(struct rpc_clnt *clnt) |
| 1030 | { |
| 1031 | spin_lock(&rpc_sched_lock); |
| 1032 | list_add(&clnt->cl_clients, &all_clients); |
| 1033 | spin_unlock(&rpc_sched_lock); |
| 1034 | } |
| 1035 | |
| 1036 | void rpc_unregister_client(struct rpc_clnt *clnt) |
| 1037 | { |
| 1038 | spin_lock(&rpc_sched_lock); |
| 1039 | list_del(&clnt->cl_clients); |
| 1040 | if (list_empty(&all_clients)) |
| 1041 | wake_up(&client_kill_wait); |
| 1042 | spin_unlock(&rpc_sched_lock); |
| 1043 | } |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | /* |
| 1046 | * Start up the rpciod process if it's not already running. |
| 1047 | */ |
| 1048 | int |
| 1049 | rpciod_up(void) |
| 1050 | { |
| 1051 | struct workqueue_struct *wq; |
| 1052 | int error = 0; |
| 1053 | |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 1054 | mutex_lock(&rpciod_mutex); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1055 | dprintk("RPC: rpciod_up: users %u\n", rpciod_users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | rpciod_users++; |
| 1057 | if (rpciod_workqueue) |
| 1058 | goto out; |
| 1059 | /* |
| 1060 | * If there's no pid, we should be the first user. |
| 1061 | */ |
| 1062 | if (rpciod_users > 1) |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1063 | printk(KERN_WARNING "rpciod_up: no workqueue, %u users??\n", rpciod_users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | /* |
| 1065 | * Create the rpciod thread and wait for it to start. |
| 1066 | */ |
| 1067 | error = -ENOMEM; |
| 1068 | wq = create_workqueue("rpciod"); |
| 1069 | if (wq == NULL) { |
| 1070 | printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error); |
| 1071 | rpciod_users--; |
| 1072 | goto out; |
| 1073 | } |
| 1074 | rpciod_workqueue = wq; |
| 1075 | error = 0; |
| 1076 | out: |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 1077 | mutex_unlock(&rpciod_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | return error; |
| 1079 | } |
| 1080 | |
| 1081 | void |
| 1082 | rpciod_down(void) |
| 1083 | { |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 1084 | mutex_lock(&rpciod_mutex); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1085 | dprintk("RPC: rpciod_down sema %u\n", rpciod_users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | if (rpciod_users) { |
| 1087 | if (--rpciod_users) |
| 1088 | goto out; |
| 1089 | } else |
| 1090 | printk(KERN_WARNING "rpciod_down: no users??\n"); |
| 1091 | |
| 1092 | if (!rpciod_workqueue) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1093 | dprintk("RPC: rpciod_down: Nothing to do!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | goto out; |
| 1095 | } |
| 1096 | rpciod_killall(); |
| 1097 | |
| 1098 | destroy_workqueue(rpciod_workqueue); |
| 1099 | rpciod_workqueue = NULL; |
| 1100 | out: |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 1101 | mutex_unlock(&rpciod_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | #ifdef RPC_DEBUG |
| 1105 | void rpc_show_tasks(void) |
| 1106 | { |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1107 | struct rpc_clnt *clnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | struct rpc_task *t; |
| 1109 | |
| 1110 | spin_lock(&rpc_sched_lock); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1111 | if (list_empty(&all_clients)) |
| 1112 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout " |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1114 | "-rpcwait -action- ---ops--\n"); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1115 | list_for_each_entry(clnt, &all_clients, cl_clients) { |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 1116 | if (list_empty(&clnt->cl_tasks)) |
| 1117 | continue; |
| 1118 | spin_lock(&clnt->cl_lock); |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1119 | list_for_each_entry(t, &clnt->cl_tasks, tk_task) { |
| 1120 | const char *rpc_waitq = "none"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1122 | if (RPC_IS_QUEUED(t)) |
| 1123 | rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1125 | printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n", |
| 1126 | t->tk_pid, |
| 1127 | (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1), |
| 1128 | t->tk_flags, t->tk_status, |
| 1129 | t->tk_client, |
| 1130 | (t->tk_client ? t->tk_client->cl_prog : 0), |
| 1131 | t->tk_rqstp, t->tk_timeout, |
| 1132 | rpc_waitq, |
| 1133 | t->tk_action, t->tk_ops); |
| 1134 | } |
Trond Myklebust | 4bef61f | 2007-06-16 14:17:01 -0400 | [diff] [blame^] | 1135 | spin_unlock(&clnt->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
Trond Myklebust | 6529eba | 2007-06-14 16:40:14 -0400 | [diff] [blame] | 1137 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | spin_unlock(&rpc_sched_lock); |
| 1139 | } |
| 1140 | #endif |
| 1141 | |
| 1142 | void |
| 1143 | rpc_destroy_mempool(void) |
| 1144 | { |
| 1145 | if (rpc_buffer_mempool) |
| 1146 | mempool_destroy(rpc_buffer_mempool); |
| 1147 | if (rpc_task_mempool) |
| 1148 | mempool_destroy(rpc_task_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1149 | if (rpc_task_slabp) |
| 1150 | kmem_cache_destroy(rpc_task_slabp); |
| 1151 | if (rpc_buffer_slabp) |
| 1152 | kmem_cache_destroy(rpc_buffer_slabp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | int |
| 1156 | rpc_init_mempool(void) |
| 1157 | { |
| 1158 | rpc_task_slabp = kmem_cache_create("rpc_tasks", |
| 1159 | sizeof(struct rpc_task), |
| 1160 | 0, SLAB_HWCACHE_ALIGN, |
| 1161 | NULL, NULL); |
| 1162 | if (!rpc_task_slabp) |
| 1163 | goto err_nomem; |
| 1164 | rpc_buffer_slabp = kmem_cache_create("rpc_buffers", |
| 1165 | RPC_BUFFER_MAXSIZE, |
| 1166 | 0, SLAB_HWCACHE_ALIGN, |
| 1167 | NULL, NULL); |
| 1168 | if (!rpc_buffer_slabp) |
| 1169 | goto err_nomem; |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1170 | rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE, |
| 1171 | rpc_task_slabp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | if (!rpc_task_mempool) |
| 1173 | goto err_nomem; |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1174 | rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE, |
| 1175 | rpc_buffer_slabp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | if (!rpc_buffer_mempool) |
| 1177 | goto err_nomem; |
| 1178 | return 0; |
| 1179 | err_nomem: |
| 1180 | rpc_destroy_mempool(); |
| 1181 | return -ENOMEM; |
| 1182 | } |