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