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