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