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 | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 47 | static void icq_free_icq_rcu(struct rcu_head *head) |
| 48 | { |
| 49 | struct io_cq *icq = container_of(head, struct io_cq, __rcu_head); |
| 50 | |
| 51 | kmem_cache_free(icq->__rcu_icq_cache, icq); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Exit and free an icq. Called with both ioc and q locked. |
| 56 | */ |
| 57 | static void ioc_exit_icq(struct io_cq *icq) |
| 58 | { |
| 59 | struct io_context *ioc = icq->ioc; |
| 60 | struct request_queue *q = icq->q; |
| 61 | struct elevator_type *et = q->elevator->type; |
| 62 | |
| 63 | lockdep_assert_held(&ioc->lock); |
| 64 | lockdep_assert_held(q->queue_lock); |
| 65 | |
| 66 | radix_tree_delete(&ioc->icq_tree, icq->q->id); |
| 67 | hlist_del_init(&icq->ioc_node); |
| 68 | list_del_init(&icq->q_node); |
| 69 | |
| 70 | /* |
| 71 | * Both setting lookup hint to and clearing it from @icq are done |
| 72 | * under queue_lock. If it's not pointing to @icq now, it never |
| 73 | * will. Hint assignment itself can race safely. |
| 74 | */ |
| 75 | if (rcu_dereference_raw(ioc->icq_hint) == icq) |
| 76 | rcu_assign_pointer(ioc->icq_hint, NULL); |
| 77 | |
| 78 | if (et->ops.elevator_exit_icq_fn) { |
| 79 | ioc_release_depth_inc(q); |
| 80 | et->ops.elevator_exit_icq_fn(icq); |
| 81 | ioc_release_depth_dec(q); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * @icq->q might have gone away by the time RCU callback runs |
| 86 | * making it impossible to determine icq_cache. Record it in @icq. |
| 87 | */ |
| 88 | icq->__rcu_icq_cache = et->icq_cache; |
| 89 | call_rcu(&icq->__rcu_head, icq_free_icq_rcu); |
| 90 | } |
| 91 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 92 | /* |
| 93 | * Slow path for ioc release in put_io_context(). Performs double-lock |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 94 | * dancing to unlink all icq's and then frees ioc. |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 95 | */ |
| 96 | static void ioc_release_fn(struct work_struct *work) |
| 97 | { |
| 98 | struct io_context *ioc = container_of(work, struct io_context, |
| 99 | release_work); |
| 100 | struct request_queue *last_q = NULL; |
| 101 | |
| 102 | spin_lock_irq(&ioc->lock); |
| 103 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 104 | while (!hlist_empty(&ioc->icq_list)) { |
| 105 | struct io_cq *icq = hlist_entry(ioc->icq_list.first, |
| 106 | struct io_cq, ioc_node); |
| 107 | struct request_queue *this_q = icq->q; |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 108 | |
| 109 | if (this_q != last_q) { |
| 110 | /* |
| 111 | * Need to switch to @this_q. Once we release |
| 112 | * @ioc->lock, it can go away along with @cic. |
| 113 | * Hold on to it. |
| 114 | */ |
| 115 | __blk_get_queue(this_q); |
| 116 | |
| 117 | /* |
| 118 | * blk_put_queue() might sleep thanks to kobject |
| 119 | * idiocy. Always release both locks, put and |
| 120 | * restart. |
| 121 | */ |
| 122 | if (last_q) { |
| 123 | spin_unlock(last_q->queue_lock); |
| 124 | spin_unlock_irq(&ioc->lock); |
| 125 | blk_put_queue(last_q); |
| 126 | } else { |
| 127 | spin_unlock_irq(&ioc->lock); |
| 128 | } |
| 129 | |
| 130 | last_q = this_q; |
| 131 | spin_lock_irq(this_q->queue_lock); |
| 132 | spin_lock(&ioc->lock); |
| 133 | continue; |
| 134 | } |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 135 | ioc_exit_icq(icq); |
Jens Axboe | ffc4e75 | 2008-02-19 10:02:29 +0100 | [diff] [blame] | 136 | } |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 137 | |
| 138 | if (last_q) { |
| 139 | spin_unlock(last_q->queue_lock); |
| 140 | spin_unlock_irq(&ioc->lock); |
| 141 | blk_put_queue(last_q); |
| 142 | } else { |
| 143 | spin_unlock_irq(&ioc->lock); |
| 144 | } |
| 145 | |
| 146 | kmem_cache_free(iocontext_cachep, ioc); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 149 | /** |
| 150 | * put_io_context - put a reference of io_context |
| 151 | * @ioc: io_context to put |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 152 | * @locked_q: request_queue the caller is holding queue_lock of (hint) |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 153 | * |
| 154 | * Decrement reference count of @ioc and release it if the count reaches |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 155 | * zero. If the caller is holding queue_lock of a queue, it can indicate |
| 156 | * that with @locked_q. This is an optimization hint and the caller is |
| 157 | * 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] | 158 | */ |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 159 | 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] | 160 | { |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 161 | struct request_queue *last_q = locked_q; |
| 162 | unsigned long flags; |
| 163 | |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 164 | if (ioc == NULL) |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 165 | return; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 166 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 167 | BUG_ON(atomic_long_read(&ioc->refcount) <= 0); |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 168 | if (locked_q) |
| 169 | lockdep_assert_held(locked_q->queue_lock); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 170 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 171 | if (!atomic_long_dec_and_test(&ioc->refcount)) |
| 172 | return; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 173 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 174 | /* |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 175 | * Destroy @ioc. This is a bit messy because icq's are chained |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 176 | * from both ioc and queue, and ioc->lock nests inside queue_lock. |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 177 | * The inner ioc->lock should be held to walk our icq_list and then |
| 178 | * for each icq the outer matching queue_lock should be grabbed. |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 179 | * ie. We need to do reverse-order double lock dancing. |
| 180 | * |
| 181 | * Another twist is that we are often called with one of the |
| 182 | * matching queue_locks held as indicated by @locked_q, which |
| 183 | * prevents performing double-lock dance for other queues. |
| 184 | * |
| 185 | * So, we do it in two stages. The fast path uses the queue_lock |
| 186 | * the caller is holding and, if other queues need to be accessed, |
| 187 | * uses trylock to avoid introducing locking dependency. This can |
| 188 | * handle most cases, especially if @ioc was performing IO on only |
| 189 | * single device. |
| 190 | * |
| 191 | * If trylock doesn't cut it, we defer to @ioc->release_work which |
| 192 | * can do all the double-locking dancing. |
| 193 | */ |
| 194 | spin_lock_irqsave_nested(&ioc->lock, flags, |
| 195 | ioc_release_depth(locked_q)); |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 196 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 197 | while (!hlist_empty(&ioc->icq_list)) { |
| 198 | struct io_cq *icq = hlist_entry(ioc->icq_list.first, |
| 199 | struct io_cq, ioc_node); |
| 200 | struct request_queue *this_q = icq->q; |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 201 | |
| 202 | if (this_q != last_q) { |
| 203 | if (last_q && last_q != locked_q) |
| 204 | spin_unlock(last_q->queue_lock); |
| 205 | last_q = NULL; |
| 206 | |
| 207 | if (!spin_trylock(this_q->queue_lock)) |
| 208 | break; |
| 209 | last_q = this_q; |
| 210 | continue; |
| 211 | } |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 212 | ioc_exit_icq(icq); |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (last_q && last_q != locked_q) |
| 216 | spin_unlock(last_q->queue_lock); |
| 217 | |
| 218 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 219 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 220 | /* if no icq is left, we're done; otherwise, kick release_work */ |
| 221 | if (hlist_empty(&ioc->icq_list)) |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 222 | kmem_cache_free(iocontext_cachep, ioc); |
| 223 | else |
| 224 | schedule_work(&ioc->release_work); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 225 | } |
| 226 | EXPORT_SYMBOL(put_io_context); |
| 227 | |
Bart Van Assche | 27667c9 | 2010-12-21 15:07:45 +0100 | [diff] [blame] | 228 | /* Called by the exiting task */ |
Louis Rilling | b69f229 | 2009-12-04 14:52:42 +0100 | [diff] [blame] | 229 | void exit_io_context(struct task_struct *task) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 230 | { |
| 231 | struct io_context *ioc; |
| 232 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 233 | /* PF_EXITING prevents new io_context from being attached to @task */ |
| 234 | WARN_ON_ONCE(!(current->flags & PF_EXITING)); |
| 235 | |
Louis Rilling | b69f229 | 2009-12-04 14:52:42 +0100 | [diff] [blame] | 236 | task_lock(task); |
| 237 | ioc = task->io_context; |
| 238 | task->io_context = NULL; |
| 239 | task_unlock(task); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 240 | |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 241 | atomic_dec(&ioc->nr_tasks); |
| 242 | put_io_context(ioc, NULL); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 245 | /** |
| 246 | * ioc_clear_queue - break any ioc association with the specified queue |
| 247 | * @q: request_queue being cleared |
| 248 | * |
| 249 | * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked. |
| 250 | */ |
| 251 | void ioc_clear_queue(struct request_queue *q) |
| 252 | { |
| 253 | lockdep_assert_held(q->queue_lock); |
| 254 | |
| 255 | while (!list_empty(&q->icq_list)) { |
| 256 | struct io_cq *icq = list_entry(q->icq_list.next, |
| 257 | struct io_cq, q_node); |
| 258 | struct io_context *ioc = icq->ioc; |
| 259 | |
| 260 | spin_lock(&ioc->lock); |
| 261 | ioc_exit_icq(icq); |
| 262 | spin_unlock(&ioc->lock); |
| 263 | } |
| 264 | } |
| 265 | |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 266 | void create_io_context_slowpath(struct task_struct *task, gfp_t gfp_flags, |
| 267 | int node) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 268 | { |
Paul Bolle | df41565 | 2011-06-06 05:11:34 +0200 | [diff] [blame] | 269 | struct io_context *ioc; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 270 | |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 271 | ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO, |
| 272 | node); |
| 273 | if (unlikely(!ioc)) |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 274 | return; |
Tejun Heo | 42ec57a | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 275 | |
| 276 | /* initialize */ |
| 277 | atomic_long_set(&ioc->refcount, 1); |
| 278 | atomic_set(&ioc->nr_tasks, 1); |
| 279 | spin_lock_init(&ioc->lock); |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 280 | INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH); |
| 281 | INIT_HLIST_HEAD(&ioc->icq_list); |
Tejun Heo | b2efa05 | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 282 | INIT_WORK(&ioc->release_work, ioc_release_fn); |
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 | /* try to install, somebody might already have beaten us to it */ |
| 285 | task_lock(task); |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 286 | if (!task->io_context && !(task->flags & PF_EXITING)) |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 287 | task->io_context = ioc; |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 288 | else |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 289 | kmem_cache_free(iocontext_cachep, ioc); |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 290 | task_unlock(task); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 291 | } |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 292 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 293 | /** |
| 294 | * get_task_io_context - get io_context of a task |
| 295 | * @task: task of interest |
| 296 | * @gfp_flags: allocation flags, used if allocation is necessary |
| 297 | * @node: allocation node, used if allocation is necessary |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 298 | * |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 299 | * Return io_context of @task. If it doesn't exist, it is created with |
| 300 | * @gfp_flags and @node. The returned io_context has its reference count |
| 301 | * incremented. |
| 302 | * |
| 303 | * This function always goes through task_lock() and it's better to use |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 304 | * %current->io_context + get_io_context() for %current. |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 305 | */ |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 306 | struct io_context *get_task_io_context(struct task_struct *task, |
| 307 | gfp_t gfp_flags, int node) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 308 | { |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 309 | struct io_context *ioc; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 310 | |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 311 | might_sleep_if(gfp_flags & __GFP_WAIT); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 312 | |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 313 | do { |
| 314 | task_lock(task); |
| 315 | ioc = task->io_context; |
| 316 | if (likely(ioc)) { |
| 317 | get_io_context(ioc); |
| 318 | task_unlock(task); |
| 319 | return ioc; |
| 320 | } |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 321 | task_unlock(task); |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 322 | } while (create_io_context(task, gfp_flags, node)); |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 323 | |
Tejun Heo | f2dbd76 | 2011-12-14 00:33:40 +0100 | [diff] [blame] | 324 | return NULL; |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 325 | } |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 326 | EXPORT_SYMBOL(get_task_io_context); |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 327 | |
Tejun Heo | 47fdd4c | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 328 | /** |
| 329 | * ioc_lookup_icq - lookup io_cq from ioc |
| 330 | * @ioc: the associated io_context |
| 331 | * @q: the associated request_queue |
| 332 | * |
| 333 | * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called |
| 334 | * with @q->queue_lock held. |
| 335 | */ |
| 336 | struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q) |
| 337 | { |
| 338 | struct io_cq *icq; |
| 339 | |
| 340 | lockdep_assert_held(q->queue_lock); |
| 341 | |
| 342 | /* |
| 343 | * icq's are indexed from @ioc using radix tree and hint pointer, |
| 344 | * both of which are protected with RCU. All removals are done |
| 345 | * holding both q and ioc locks, and we're holding q lock - if we |
| 346 | * find a icq which points to us, it's guaranteed to be valid. |
| 347 | */ |
| 348 | rcu_read_lock(); |
| 349 | icq = rcu_dereference(ioc->icq_hint); |
| 350 | if (icq && icq->q == q) |
| 351 | goto out; |
| 352 | |
| 353 | icq = radix_tree_lookup(&ioc->icq_tree, q->id); |
| 354 | if (icq && icq->q == q) |
| 355 | rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */ |
| 356 | else |
| 357 | icq = NULL; |
| 358 | out: |
| 359 | rcu_read_unlock(); |
| 360 | return icq; |
| 361 | } |
| 362 | EXPORT_SYMBOL(ioc_lookup_icq); |
| 363 | |
Tejun Heo | f1f8cc9 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 364 | /** |
| 365 | * ioc_create_icq - create and link io_cq |
| 366 | * @q: request_queue of interest |
| 367 | * @gfp_mask: allocation mask |
| 368 | * |
| 369 | * Make sure io_cq linking %current->io_context and @q exists. If either |
| 370 | * io_context and/or icq don't exist, they will be created using @gfp_mask. |
| 371 | * |
| 372 | * The caller is responsible for ensuring @ioc won't go away and @q is |
| 373 | * alive and will stay alive until this function returns. |
| 374 | */ |
| 375 | struct io_cq *ioc_create_icq(struct request_queue *q, gfp_t gfp_mask) |
| 376 | { |
| 377 | struct elevator_type *et = q->elevator->type; |
| 378 | struct io_context *ioc; |
| 379 | struct io_cq *icq; |
| 380 | |
| 381 | /* allocate stuff */ |
| 382 | ioc = create_io_context(current, gfp_mask, q->node); |
| 383 | if (!ioc) |
| 384 | return NULL; |
| 385 | |
| 386 | icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO, |
| 387 | q->node); |
| 388 | if (!icq) |
| 389 | return NULL; |
| 390 | |
| 391 | if (radix_tree_preload(gfp_mask) < 0) { |
| 392 | kmem_cache_free(et->icq_cache, icq); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | icq->ioc = ioc; |
| 397 | icq->q = q; |
| 398 | INIT_LIST_HEAD(&icq->q_node); |
| 399 | INIT_HLIST_NODE(&icq->ioc_node); |
| 400 | |
| 401 | /* lock both q and ioc and try to link @icq */ |
| 402 | spin_lock_irq(q->queue_lock); |
| 403 | spin_lock(&ioc->lock); |
| 404 | |
| 405 | if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) { |
| 406 | hlist_add_head(&icq->ioc_node, &ioc->icq_list); |
| 407 | list_add(&icq->q_node, &q->icq_list); |
| 408 | if (et->ops.elevator_init_icq_fn) |
| 409 | et->ops.elevator_init_icq_fn(icq); |
| 410 | } else { |
| 411 | kmem_cache_free(et->icq_cache, icq); |
| 412 | icq = ioc_lookup_icq(ioc, q); |
| 413 | if (!icq) |
| 414 | printk(KERN_ERR "cfq: icq link failed!\n"); |
| 415 | } |
| 416 | |
| 417 | spin_unlock(&ioc->lock); |
| 418 | spin_unlock_irq(q->queue_lock); |
| 419 | radix_tree_preload_end(); |
| 420 | return icq; |
| 421 | } |
| 422 | |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 423 | void ioc_set_changed(struct io_context *ioc, int which) |
| 424 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 425 | struct io_cq *icq; |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 426 | struct hlist_node *n; |
| 427 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 428 | hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node) |
| 429 | set_bit(which, &icq->changed); |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
| 433 | * ioc_ioprio_changed - notify ioprio change |
| 434 | * @ioc: io_context of interest |
| 435 | * @ioprio: new ioprio |
| 436 | * |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 437 | * @ioc's ioprio has changed to @ioprio. Set %ICQ_IOPRIO_CHANGED for all |
| 438 | * icq's. iosched is responsible for checking the bit and applying it on |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 439 | * request issue path. |
| 440 | */ |
| 441 | void ioc_ioprio_changed(struct io_context *ioc, int ioprio) |
| 442 | { |
| 443 | unsigned long flags; |
| 444 | |
| 445 | spin_lock_irqsave(&ioc->lock, flags); |
| 446 | ioc->ioprio = ioprio; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 447 | ioc_set_changed(ioc, ICQ_IOPRIO_CHANGED); |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 448 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * ioc_cgroup_changed - notify cgroup change |
| 453 | * @ioc: io_context of interest |
| 454 | * |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 455 | * @ioc's cgroup has changed. Set %ICQ_CGROUP_CHANGED for all icq's. |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 456 | * iosched is responsible for checking the bit and applying it on request |
| 457 | * issue path. |
| 458 | */ |
| 459 | void ioc_cgroup_changed(struct io_context *ioc) |
| 460 | { |
| 461 | unsigned long flags; |
| 462 | |
| 463 | spin_lock_irqsave(&ioc->lock, flags); |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 464 | ioc_set_changed(ioc, ICQ_CGROUP_CHANGED); |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 465 | spin_unlock_irqrestore(&ioc->lock, flags); |
| 466 | } |
Jens Axboe | 64c4299 | 2011-12-19 10:36:44 +0100 | [diff] [blame^] | 467 | EXPORT_SYMBOL(ioc_cgroup_changed); |
Tejun Heo | dc86900 | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 468 | |
Adrian Bunk | 1334159 | 2008-02-18 13:45:53 +0100 | [diff] [blame] | 469 | static int __init blk_ioc_init(void) |
Jens Axboe | 86db1e2 | 2008-01-29 14:53:40 +0100 | [diff] [blame] | 470 | { |
| 471 | iocontext_cachep = kmem_cache_create("blkdev_ioc", |
| 472 | sizeof(struct io_context), 0, SLAB_PANIC, NULL); |
| 473 | return 0; |
| 474 | } |
| 475 | subsys_initcall(blk_ioc_init); |