linux/block/blk-ioc.c
<<
>>
Prefs
   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 */
  10#include <linux/slab.h>
  11
  12#include "blk.h"
  13
  14/*
  15 * For io context allocations
  16 */
  17static struct kmem_cache *iocontext_cachep;
  18
  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 */
  25void get_io_context(struct io_context *ioc)
  26{
  27        BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
  28        atomic_long_inc(&ioc->refcount);
  29}
  30EXPORT_SYMBOL(get_io_context);
  31
  32static void icq_free_icq_rcu(struct rcu_head *head)
  33{
  34        struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
  35
  36        kmem_cache_free(icq->__rcu_icq_cache, icq);
  37}
  38
  39/*
  40 * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
  41 * and queue locked for legacy.
  42 */
  43static void ioc_exit_icq(struct io_cq *icq)
  44{
  45        struct elevator_type_aux *aux = icq->q->elevator->aux;
  46
  47        if (icq->flags & ICQ_EXITED)
  48                return;
  49
  50        if (aux->uses_mq && aux->ops.mq.exit_icq)
  51                aux->ops.mq.exit_icq(icq);
  52        else if (!aux->uses_mq && aux->ops.sq.elevator_exit_icq_fn)
  53                aux->ops.sq.elevator_exit_icq_fn(icq);
  54
  55        icq->flags |= ICQ_EXITED;
  56}
  57
  58/*
  59 * Release an icq. Called with ioc locked for blk-mq, and with both ioc
  60 * and queue locked for legacy.
  61 */
  62static void ioc_destroy_icq(struct io_cq *icq)
  63{
  64        struct io_context *ioc = icq->ioc;
  65        struct request_queue *q = icq->q;
  66        struct elevator_type *et = q->elevator->type;
  67
  68        lockdep_assert_held(&ioc->lock);
  69
  70        radix_tree_delete(&ioc->icq_tree, icq->q->id);
  71        hlist_del_init(&icq->ioc_node);
  72        list_del_init(&icq->q_node);
  73
  74        /*
  75         * Both setting lookup hint to and clearing it from @icq are done
  76         * under queue_lock.  If it's not pointing to @icq now, it never
  77         * will.  Hint assignment itself can race safely.
  78         */
  79        if (rcu_dereference_raw(ioc->icq_hint) == icq)
  80                rcu_assign_pointer(ioc->icq_hint, NULL);
  81
  82        ioc_exit_icq(icq);
  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
  92/*
  93 * Slow path for ioc release in put_io_context().  Performs double-lock
  94 * dancing to unlink all icq's and then frees ioc.
  95 */
  96static void ioc_release_fn(struct work_struct *work)
  97{
  98        struct io_context *ioc = container_of(work, struct io_context,
  99                                              release_work);
 100        unsigned long flags;
 101
 102        /*
 103         * Exiting icq may call into put_io_context() through elevator
 104         * which will trigger lockdep warning.  The ioc's are guaranteed to
 105         * be different, use a different locking subclass here.  Use
 106         * irqsave variant as there's no spin_lock_irq_nested().
 107         */
 108        spin_lock_irqsave_nested(&ioc->lock, flags, 1);
 109
 110        while (!hlist_empty(&ioc->icq_list)) {
 111                struct io_cq *icq = hlist_entry(ioc->icq_list.first,
 112                                                struct io_cq, ioc_node);
 113                struct request_queue *q = icq->q;
 114
 115                if (spin_trylock(q->queue_lock)) {
 116                        ioc_destroy_icq(icq);
 117                        spin_unlock(q->queue_lock);
 118                } else {
 119                        spin_unlock_irqrestore(&ioc->lock, flags);
 120                        cpu_relax();
 121                        spin_lock_irqsave_nested(&ioc->lock, flags, 1);
 122                }
 123        }
 124
 125        spin_unlock_irqrestore(&ioc->lock, flags);
 126
 127        kmem_cache_free(iocontext_cachep, ioc);
 128}
 129
 130/**
 131 * put_io_context - put a reference of io_context
 132 * @ioc: io_context to put
 133 *
 134 * Decrement reference count of @ioc and release it if the count reaches
 135 * zero.
 136 */
 137void put_io_context(struct io_context *ioc)
 138{
 139        unsigned long flags;
 140        bool free_ioc = false;
 141
 142        if (ioc == NULL)
 143                return;
 144
 145        BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
 146
 147        /*
 148         * Releasing ioc requires reverse order double locking and we may
 149         * already be holding a queue_lock.  Do it asynchronously from wq.
 150         */
 151        if (atomic_long_dec_and_test(&ioc->refcount)) {
 152                spin_lock_irqsave(&ioc->lock, flags);
 153                if (!hlist_empty(&ioc->icq_list))
 154                        schedule_work(&ioc->release_work);
 155                else
 156                        free_ioc = true;
 157                spin_unlock_irqrestore(&ioc->lock, flags);
 158        }
 159
 160        if (free_ioc)
 161                kmem_cache_free(iocontext_cachep, ioc);
 162}
 163EXPORT_SYMBOL(put_io_context);
 164
 165/**
 166 * put_io_context_active - put active reference on ioc
 167 * @ioc: ioc of interest
 168 *
 169 * Undo get_io_context_active().  If active reference reaches zero after
 170 * put, @ioc can never issue further IOs and ioscheds are notified.
 171 */
 172void put_io_context_active(struct io_context *ioc)
 173{
 174        struct elevator_type *et;
 175        unsigned long flags;
 176        struct io_cq *icq;
 177
 178        if (!atomic_dec_and_test(&ioc->active_ref)) {
 179                put_io_context(ioc);
 180                return;
 181        }
 182
 183        /*
 184         * Need ioc lock to walk icq_list and q lock to exit icq.  Perform
 185         * reverse double locking.  Read comment in ioc_release_fn() for
 186         * explanation on the nested locking annotation.
 187         */
 188retry:
 189        spin_lock_irqsave_nested(&ioc->lock, flags, 1);
 190        hlist_for_each_entry(icq, &ioc->icq_list, ioc_node) {
 191                if (icq->flags & ICQ_EXITED)
 192                        continue;
 193
 194                et = icq->q->elevator->type;
 195                if (icq->q->elevator->aux->uses_mq) {
 196                        ioc_exit_icq(icq);
 197                } else {
 198                        if (spin_trylock(icq->q->queue_lock)) {
 199                                ioc_exit_icq(icq);
 200                                spin_unlock(icq->q->queue_lock);
 201                        } else {
 202                                spin_unlock_irqrestore(&ioc->lock, flags);
 203                                cpu_relax();
 204                                goto retry;
 205                        }
 206                }
 207        }
 208        spin_unlock_irqrestore(&ioc->lock, flags);
 209
 210        put_io_context(ioc);
 211}
 212
 213/* Called by the exiting task */
 214void exit_io_context(struct task_struct *task)
 215{
 216        struct io_context *ioc;
 217
 218        task_lock(task);
 219        ioc = task->io_context;
 220        task->io_context = NULL;
 221        task_unlock(task);
 222
 223        atomic_dec(&ioc->nr_tasks);
 224        put_io_context_active(ioc);
 225}
 226
 227static void __ioc_clear_queue(struct list_head *icq_list)
 228{
 229        unsigned long flags;
 230
 231        while (!list_empty(icq_list)) {
 232                struct io_cq *icq = list_entry(icq_list->next,
 233                                               struct io_cq, q_node);
 234                struct io_context *ioc = icq->ioc;
 235
 236                spin_lock_irqsave(&ioc->lock, flags);
 237                ioc_destroy_icq(icq);
 238                spin_unlock_irqrestore(&ioc->lock, flags);
 239        }
 240}
 241
 242/**
 243 * ioc_clear_queue - break any ioc association with the specified queue
 244 * @q: request_queue being cleared
 245 *
 246 * Walk @q->icq_list and exit all io_cq's.
 247 */
 248void ioc_clear_queue(struct request_queue *q)
 249{
 250        LIST_HEAD(icq_list);
 251
 252        spin_lock_irq(q->queue_lock);
 253        list_splice_init(&q->icq_list, &icq_list);
 254
 255        if (q->mq_ops) {
 256                spin_unlock_irq(q->queue_lock);
 257                __ioc_clear_queue(&icq_list);
 258        } else {
 259                __ioc_clear_queue(&icq_list);
 260                spin_unlock_irq(q->queue_lock);
 261        }
 262}
 263
 264int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
 265{
 266        struct io_context *ioc;
 267        int ret;
 268
 269        ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
 270                                    node);
 271        if (unlikely(!ioc))
 272                return -ENOMEM;
 273
 274        /* initialize */
 275        atomic_long_set(&ioc->refcount, 1);
 276        atomic_set(&ioc->nr_tasks, 1);
 277        atomic_set(&ioc->active_ref, 1);
 278        spin_lock_init(&ioc->lock);
 279        INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
 280        INIT_HLIST_HEAD(&ioc->icq_list);
 281        INIT_WORK(&ioc->release_work, ioc_release_fn);
 282
 283        /*
 284         * Try to install.  ioc shouldn't be installed if someone else
 285         * already did or @task, which isn't %current, is exiting.  Note
 286         * that we need to allow ioc creation on exiting %current as exit
 287         * path may issue IOs from e.g. exit_files().  The exit path is
 288         * responsible for not issuing IO after exit_io_context().
 289         */
 290        task_lock(task);
 291        if (!task->io_context &&
 292            (task == current || !(task->flags & PF_EXITING)))
 293                task->io_context = ioc;
 294        else
 295                kmem_cache_free(iocontext_cachep, ioc);
 296
 297        ret = task->io_context ? 0 : -EBUSY;
 298
 299        task_unlock(task);
 300
 301        return ret;
 302}
 303
 304/**
 305 * get_task_io_context - get io_context of a task
 306 * @task: task of interest
 307 * @gfp_flags: allocation flags, used if allocation is necessary
 308 * @node: allocation node, used if allocation is necessary
 309 *
 310 * Return io_context of @task.  If it doesn't exist, it is created with
 311 * @gfp_flags and @node.  The returned io_context has its reference count
 312 * incremented.
 313 *
 314 * This function always goes through task_lock() and it's better to use
 315 * %current->io_context + get_io_context() for %current.
 316 */
 317struct io_context *get_task_io_context(struct task_struct *task,
 318                                       gfp_t gfp_flags, int node)
 319{
 320        struct io_context *ioc;
 321
 322        might_sleep_if(gfp_flags & __GFP_WAIT);
 323
 324        do {
 325                task_lock(task);
 326                ioc = task->io_context;
 327                if (likely(ioc)) {
 328                        get_io_context(ioc);
 329                        task_unlock(task);
 330                        return ioc;
 331                }
 332                task_unlock(task);
 333        } while (!create_task_io_context(task, gfp_flags, node));
 334
 335        return NULL;
 336}
 337EXPORT_SYMBOL(get_task_io_context);
 338
 339/**
 340 * ioc_lookup_icq - lookup io_cq from ioc
 341 * @ioc: the associated io_context
 342 * @q: the associated request_queue
 343 *
 344 * Look up io_cq associated with @ioc - @q pair from @ioc.  Must be called
 345 * with @q->queue_lock held.
 346 */
 347struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
 348{
 349        struct io_cq *icq;
 350
 351        lockdep_assert_held(q->queue_lock);
 352
 353        /*
 354         * icq's are indexed from @ioc using radix tree and hint pointer,
 355         * both of which are protected with RCU.  All removals are done
 356         * holding both q and ioc locks, and we're holding q lock - if we
 357         * find a icq which points to us, it's guaranteed to be valid.
 358         */
 359        rcu_read_lock();
 360        icq = rcu_dereference(ioc->icq_hint);
 361        if (icq && icq->q == q)
 362                goto out;
 363
 364        icq = radix_tree_lookup(&ioc->icq_tree, q->id);
 365        if (icq && icq->q == q)
 366                rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
 367        else
 368                icq = NULL;
 369out:
 370        rcu_read_unlock();
 371        return icq;
 372}
 373EXPORT_SYMBOL(ioc_lookup_icq);
 374
 375/**
 376 * ioc_create_icq - create and link io_cq
 377 * @ioc: io_context of interest
 378 * @q: request_queue of interest
 379 * @gfp_mask: allocation mask
 380 *
 381 * Make sure io_cq linking @ioc and @q exists.  If icq doesn't exist, they
 382 * will be created using @gfp_mask.
 383 *
 384 * The caller is responsible for ensuring @ioc won't go away and @q is
 385 * alive and will stay alive until this function returns.
 386 */
 387struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
 388                             gfp_t gfp_mask)
 389{
 390        struct elevator_type *et = q->elevator->type;
 391        struct elevator_type_aux *aux = q->elevator->aux;
 392        struct io_cq *icq;
 393
 394        /* allocate stuff */
 395        icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
 396                                    q->node);
 397        if (!icq)
 398                return NULL;
 399
 400        if (radix_tree_maybe_preload(gfp_mask) < 0) {
 401                kmem_cache_free(et->icq_cache, icq);
 402                return NULL;
 403        }
 404
 405        icq->ioc = ioc;
 406        icq->q = q;
 407        INIT_LIST_HEAD(&icq->q_node);
 408        INIT_HLIST_NODE(&icq->ioc_node);
 409
 410        /* lock both q and ioc and try to link @icq */
 411        spin_lock_irq(q->queue_lock);
 412        spin_lock(&ioc->lock);
 413
 414        if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
 415                hlist_add_head(&icq->ioc_node, &ioc->icq_list);
 416                list_add(&icq->q_node, &q->icq_list);
 417                if (aux->uses_mq && aux->ops.mq.init_icq)
 418                        aux->ops.mq.init_icq(icq);
 419                else if (!aux->uses_mq && aux->ops.sq.elevator_init_icq_fn)
 420                        aux->ops.sq.elevator_init_icq_fn(icq);
 421        } else {
 422                kmem_cache_free(et->icq_cache, icq);
 423                icq = ioc_lookup_icq(ioc, q);
 424                if (!icq)
 425                        printk(KERN_ERR "cfq: icq link failed!\n");
 426        }
 427
 428        spin_unlock(&ioc->lock);
 429        spin_unlock_irq(q->queue_lock);
 430        radix_tree_preload_end();
 431        return icq;
 432}
 433
 434static int __init blk_ioc_init(void)
 435{
 436        iocontext_cachep = kmem_cache_create("blkdev_ioc",
 437                        sizeof(struct io_context), 0, SLAB_PANIC, NULL);
 438        return 0;
 439}
 440subsys_initcall(blk_ioc_init);
 441