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