Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions related to io context handling |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/bio.h> |
| 8 | #include <linux/blkdev.h> |
| 9 | #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 11 | |
| 12 | #include "blk.h" |
| 13 | |
| 14 | /* |
| 15 | * For io context allocations |
| 16 | */ |
| 17 | static struct kmem_cache *iocontext_cachep; |
| 18 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 19 | /** |
| 20 | * get_io_context - increment reference count to io_context |
| 21 | * @ioc: io_context to get |
| 22 | * |
| 23 | * Increment reference count to @ioc. |
| 24 | */ |
| 25 | void get_io_context(struct io_context *ioc) |
| 26 | { |
| 27 | BUG_ON(atomic_long_read(&ioc->refcount) <= 0); |
| 28 | atomic_long_inc(&ioc->refcount); |
| 29 | } |
| 30 | EXPORT_SYMBOL(get_io_context); |
| 31 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 32 | /* |
| 33 | * Releasing ioc may nest into another put_io_context() leading to nested |
| 34 | * fast path release. As the ioc's can't be the same, this is okay but |
| 35 | * makes lockdep whine. Keep track of nesting and use it as subclass. |
| 36 | */ |
| 37 | #ifdef CONFIG_LOCKDEP |
| 38 | #define ioc_release_depth(q) ((q) ? (q)->ioc_release_depth : 0) |
| 39 | #define ioc_release_depth_inc(q) (q)->ioc_release_depth++ |
| 40 | #define ioc_release_depth_dec(q) (q)->ioc_release_depth-- |
| 41 | #else |
| 42 | #define ioc_release_depth(q) 0 |
| 43 | #define ioc_release_depth_inc(q) do { } while (0) |
| 44 | #define ioc_release_depth_dec(q) do { } while (0) |
| 45 | #endif |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 46 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 47 | /* |
| 48 | * Slow path for ioc release in put_io_context(). Performs double-lock |
| 49 | * dancing to unlink all cic's and then frees ioc. |
| 50 | */ |
| 51 | static void ioc_release_fn(struct work_struct *work) |
| 52 | { |
| 53 | struct io_context *ioc = container_of(work, struct io_context, |
| 54 | release_work); |
| 55 | struct request_queue *last_q = NULL; |
| 56 | |
| 57 | spin_lock_irq(&ioc->lock); |
| 58 | |
| 59 | while (!hlist_empty(&ioc->cic_list)) { |
| 60 | struct cfq_io_context *cic = hlist_entry(ioc->cic_list.first, |
| 61 | struct cfq_io_context, |
| 62 | cic_list); |
| 63 | struct request_queue *this_q = cic->q; |
| 64 | |
| 65 | if (this_q != last_q) { |
| 66 | /* |
| 67 | * Need to switch to @this_q. Once we release |
| 68 | * @ioc->lock, it can go away along with @cic. |
| 69 | * Hold on to it. |
| 70 | */ |
| 71 | __blk_get_queue(this_q); |
| 72 | |
| 73 | /* |
| 74 | * blk_put_queue() might sleep thanks to kobject |
| 75 | * idiocy. Always release both locks, put and |
| 76 | * restart. |
| 77 | */ |
| 78 | if (last_q) { |
| 79 | spin_unlock(last_q->queue_lock); |
| 80 | spin_unlock_irq(&ioc->lock); |
| 81 | blk_put_queue(last_q); |
| 82 | } else { |
| 83 | spin_unlock_irq(&ioc->lock); |
| 84 | } |
| 85 | |
| 86 | last_q = this_q; |
| 87 | spin_lock_irq(this_q->queue_lock); |
| 88 | spin_lock(&ioc->lock); |
| 89 | continue; |
| 90 | } |
| 91 | ioc_release_depth_inc(this_q); |
| 92 | cic->exit(cic); |
| 93 | cic->release(cic); |
| 94 | ioc_release_depth_dec(this_q); |
Jens Axboe | ffc4e75 | 2008-02-19 10:02:29 +0100 | [diff] [blame] | 95 | } |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 96 | |
| 97 | if (last_q) { |
| 98 | spin_unlock(last_q->queue_lock); |
| 99 | spin_unlock_irq(&ioc->lock); |
| 100 | blk_put_queue(last_q); |
| 101 | } else { |
| 102 | spin_unlock_irq(&ioc->lock); |
| 103 | } |
| 104 | |
| 105 | kmem_cache_free(iocontext_cachep, ioc); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 108 | /** |
| 109 | * put_io_context - put a reference of io_context |
| 110 | * @ioc: io_context to put |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 111 | * @locked_q: request_queue the caller is holding queue_lock of (hint) |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 112 | * |
| 113 | * Decrement reference count of @ioc and release it if the count reaches |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 114 | * zero. If the caller is holding queue_lock of a queue, it can indicate |
| 115 | * that with @locked_q. This is an optimization hint and the caller is |
| 116 | * allowed to pass in %NULL even when it's holding a queue_lock. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 117 | */ |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 118 | void put_io_context(struct io_context *ioc, struct request_queue *locked_q) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 119 | { |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 120 | struct request_queue *last_q = locked_q; |
| 121 | unsigned long flags; |
| 122 | |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 123 | if (ioc == NULL) |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 124 | return; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 125 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 126 | BUG_ON(atomic_long_read(&ioc->refcount) <= 0); |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 127 | if (locked_q) |
| 128 | lockdep_assert_held(locked_q->queue_lock); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 129 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 130 | if (!atomic_long_dec_and_test(&ioc->refcount)) |
| 131 | return; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 132 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 133 | /* |
| 134 | * Destroy @ioc. This is a bit messy because cic's are chained |
| 135 | * from both ioc and queue, and ioc->lock nests inside queue_lock. |
| 136 | * The inner ioc->lock should be held to walk our cic_list and then |
| 137 | * for each cic the outer matching queue_lock should be grabbed. |
| 138 | * ie. We need to do reverse-order double lock dancing. |
| 139 | * |
| 140 | * Another twist is that we are often called with one of the |
| 141 | * matching queue_locks held as indicated by @locked_q, which |
| 142 | * prevents performing double-lock dance for other queues. |
| 143 | * |
| 144 | * So, we do it in two stages. The fast path uses the queue_lock |
| 145 | * the caller is holding and, if other queues need to be accessed, |
| 146 | * uses trylock to avoid introducing locking dependency. This can |
| 147 | * handle most cases, especially if @ioc was performing IO on only |
| 148 | * single device. |
| 149 | * |
| 150 | * If trylock doesn't cut it, we defer to @ioc->release_work which |
| 151 | * can do all the double-locking dancing. |
| 152 | */ |
| 153 | spin_lock_irqsave_nested(&ioc->lock, flags, |
| 154 | ioc_release_depth(locked_q)); |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 155 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 156 | while (!hlist_empty(&ioc->cic_list)) { |
| 157 | struct cfq_io_context *cic = hlist_entry(ioc->cic_list.first, |
| 158 | struct cfq_io_context, |
| 159 | cic_list); |
| 160 | struct request_queue *this_q = cic->q; |
| 161 | |
| 162 | if (this_q != last_q) { |
| 163 | if (last_q && last_q != locked_q) |
| 164 | spin_unlock(last_q->queue_lock); |
| 165 | last_q = NULL; |
| 166 | |
| 167 | if (!spin_trylock(this_q->queue_lock)) |
| 168 | break; |
| 169 | last_q = this_q; |
| 170 | continue; |
| 171 | } |
| 172 | ioc_release_depth_inc(this_q); |
| 173 | cic->exit(cic); |
| 174 | cic->release(cic); |
| 175 | ioc_release_depth_dec(this_q); |
| 176 | } |
| 177 | |
| 178 | if (last_q && last_q != locked_q) |
| 179 | spin_unlock(last_q->queue_lock); |
| 180 | |
| 181 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 182 | |
| 183 | /* if no cic's left, we're done; otherwise, kick release_work */ |
| 184 | if (hlist_empty(&ioc->cic_list)) |
| 185 | kmem_cache_free(iocontext_cachep, ioc); |
| 186 | else |
| 187 | schedule_work(&ioc->release_work); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 188 | } |
| 189 | EXPORT_SYMBOL(put_io_context); |
| 190 | |
Bart Van Assche | 27667c9 | 2010-12-21 15:07:45 +0100 | [diff] [blame] | 191 | /* Called by the exiting task */ |
Louis Rilling | b69f229 | 2009-12-04 14:52:42 +0100 | [diff] [blame] | 192 | void exit_io_context(struct task_struct *task) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 193 | { |
| 194 | struct io_context *ioc; |
| 195 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 196 | /* PF_EXITING prevents new io_context from being attached to @task */ |
| 197 | WARN_ON_ONCE(!(current->flags & PF_EXITING)); |
| 198 | |
Louis Rilling | b69f229 | 2009-12-04 14:52:42 +0100 | [diff] [blame] | 199 | task_lock(task); |
| 200 | ioc = task->io_context; |
| 201 | task->io_context = NULL; |
| 202 | task_unlock(task); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 203 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 204 | atomic_dec(&ioc->nr_tasks); |
| 205 | put_io_context(ioc, NULL); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 208 | static struct io_context *create_task_io_context(struct task_struct *task, |
| 209 | gfp_t gfp_flags, int node, |
| 210 | bool take_ref) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 211 | { |
Paul Bolle | df41565 | 2011-06-06 05:11:34 +0200 | [diff] [blame] | 212 | struct io_context *ioc; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 213 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 214 | ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO, |
| 215 | node); |
| 216 | if (unlikely(!ioc)) |
| 217 | return NULL; |
| 218 | |
| 219 | /* initialize */ |
| 220 | atomic_long_set(&ioc->refcount, 1); |
| 221 | atomic_set(&ioc->nr_tasks, 1); |
| 222 | spin_lock_init(&ioc->lock); |
| 223 | INIT_RADIX_TREE(&ioc->radix_root, GFP_ATOMIC | __GFP_HIGH); |
| 224 | INIT_HLIST_HEAD(&ioc->cic_list); |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame^] | 225 | INIT_WORK(&ioc->release_work, ioc_release_fn); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 226 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 227 | /* try to install, somebody might already have beaten us to it */ |
| 228 | task_lock(task); |
| 229 | |
| 230 | if (!task->io_context && !(task->flags & PF_EXITING)) { |
| 231 | task->io_context = ioc; |
| 232 | } else { |
| 233 | kmem_cache_free(iocontext_cachep, ioc); |
| 234 | ioc = task->io_context; |
| 235 | } |
| 236 | |
| 237 | if (ioc && take_ref) |
| 238 | get_io_context(ioc); |
| 239 | |
| 240 | task_unlock(task); |
Paul Bolle | df41565 | 2011-06-06 05:11:34 +0200 | [diff] [blame] | 241 | return ioc; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 244 | /** |
| 245 | * current_io_context - get io_context of %current |
| 246 | * @gfp_flags: allocation flags, used if allocation is necessary |
| 247 | * @node: allocation node, used if allocation is necessary |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 248 | * |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 249 | * Return io_context of %current. If it doesn't exist, it is created with |
| 250 | * @gfp_flags and @node. The returned io_context does NOT have its |
| 251 | * reference count incremented. Because io_context is exited only on task |
| 252 | * exit, %current can be sure that the returned io_context is valid and |
| 253 | * alive as long as it is executing. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 254 | */ |
| 255 | struct io_context *current_io_context(gfp_t gfp_flags, int node) |
| 256 | { |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 257 | might_sleep_if(gfp_flags & __GFP_WAIT); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 258 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 259 | if (current->io_context) |
| 260 | return current->io_context; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 261 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 262 | return create_task_io_context(current, gfp_flags, node, false); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 263 | } |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 264 | EXPORT_SYMBOL(current_io_context); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 265 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 266 | /** |
| 267 | * get_task_io_context - get io_context of a task |
| 268 | * @task: task of interest |
| 269 | * @gfp_flags: allocation flags, used if allocation is necessary |
| 270 | * @node: allocation node, used if allocation is necessary |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 271 | * |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 272 | * Return io_context of @task. If it doesn't exist, it is created with |
| 273 | * @gfp_flags and @node. The returned io_context has its reference count |
| 274 | * incremented. |
| 275 | * |
| 276 | * This function always goes through task_lock() and it's better to use |
| 277 | * current_io_context() + get_io_context() for %current. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 278 | */ |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 279 | struct io_context *get_task_io_context(struct task_struct *task, |
| 280 | gfp_t gfp_flags, int node) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 281 | { |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 282 | struct io_context *ioc; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 283 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 284 | might_sleep_if(gfp_flags & __GFP_WAIT); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 285 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 286 | task_lock(task); |
| 287 | ioc = task->io_context; |
| 288 | if (likely(ioc)) { |
| 289 | get_io_context(ioc); |
| 290 | task_unlock(task); |
| 291 | return ioc; |
| 292 | } |
| 293 | task_unlock(task); |
| 294 | |
| 295 | return create_task_io_context(task, gfp_flags, node, true); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 296 | } |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 297 | EXPORT_SYMBOL(get_task_io_context); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 298 | |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 299 | void ioc_set_changed(struct io_context *ioc, int which) |
| 300 | { |
| 301 | struct cfq_io_context *cic; |
| 302 | struct hlist_node *n; |
| 303 | |
| 304 | hlist_for_each_entry(cic, n, &ioc->cic_list, cic_list) |
| 305 | set_bit(which, &cic->changed); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * ioc_ioprio_changed - notify ioprio change |
| 310 | * @ioc: io_context of interest |
| 311 | * @ioprio: new ioprio |
| 312 | * |
| 313 | * @ioc's ioprio has changed to @ioprio. Set %CIC_IOPRIO_CHANGED for all |
| 314 | * cic's. iosched is responsible for checking the bit and applying it on |
| 315 | * request issue path. |
| 316 | */ |
| 317 | void ioc_ioprio_changed(struct io_context *ioc, int ioprio) |
| 318 | { |
| 319 | unsigned long flags; |
| 320 | |
| 321 | spin_lock_irqsave(&ioc->lock, flags); |
| 322 | ioc->ioprio = ioprio; |
| 323 | ioc_set_changed(ioc, CIC_IOPRIO_CHANGED); |
| 324 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * ioc_cgroup_changed - notify cgroup change |
| 329 | * @ioc: io_context of interest |
| 330 | * |
| 331 | * @ioc's cgroup has changed. Set %CIC_CGROUP_CHANGED for all cic's. |
| 332 | * iosched is responsible for checking the bit and applying it on request |
| 333 | * issue path. |
| 334 | */ |
| 335 | void ioc_cgroup_changed(struct io_context *ioc) |
| 336 | { |
| 337 | unsigned long flags; |
| 338 | |
| 339 | spin_lock_irqsave(&ioc->lock, flags); |
| 340 | ioc_set_changed(ioc, CIC_CGROUP_CHANGED); |
| 341 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 342 | } |
| 343 | |
Adrian Bunk | 1334159 | 2008-02-18 13:45:53 +0100 | [diff] [blame] | 344 | static int __init blk_ioc_init(void) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 345 | { |
| 346 | iocontext_cachep = kmem_cache_create("blkdev_ioc", |
| 347 | sizeof(struct io_context), 0, SLAB_PANIC, NULL); |
| 348 | return 0; |
| 349 | } |
| 350 | subsys_initcall(blk_ioc_init); |