]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - block/blk-ioc.c
block, cfq: unlink cfq_io_context's immediately
[karo-tx-linux.git] / block / blk-ioc.c
index 6f9bbd978653e631f4fdd5cb36eee160d6045031..fb23965595da6a34f5965074004ba73887476561 100644 (file)
  */
 static struct kmem_cache *iocontext_cachep;
 
-static void cfq_dtor(struct io_context *ioc)
+/**
+ * get_io_context - increment reference count to io_context
+ * @ioc: io_context to get
+ *
+ * Increment reference count to @ioc.
+ */
+void get_io_context(struct io_context *ioc)
 {
-       if (!hlist_empty(&ioc->cic_list)) {
-               struct cfq_io_context *cic;
-
-               cic = hlist_entry(ioc->cic_list.first, struct cfq_io_context,
-                                                               cic_list);
-               cic->dtor(ioc);
-       }
+       BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
+       atomic_long_inc(&ioc->refcount);
 }
+EXPORT_SYMBOL(get_io_context);
+
+/*
+ * Releasing ioc may nest into another put_io_context() leading to nested
+ * fast path release.  As the ioc's can't be the same, this is okay but
+ * makes lockdep whine.  Keep track of nesting and use it as subclass.
+ */
+#ifdef CONFIG_LOCKDEP
+#define ioc_release_depth(q)           ((q) ? (q)->ioc_release_depth : 0)
+#define ioc_release_depth_inc(q)       (q)->ioc_release_depth++
+#define ioc_release_depth_dec(q)       (q)->ioc_release_depth--
+#else
+#define ioc_release_depth(q)           0
+#define ioc_release_depth_inc(q)       do { } while (0)
+#define ioc_release_depth_dec(q)       do { } while (0)
+#endif
 
 /*
- * IO Context helper functions. put_io_context() returns 1 if there are no
- * more users of this io context, 0 otherwise.
+ * Slow path for ioc release in put_io_context().  Performs double-lock
+ * dancing to unlink all cic's and then frees ioc.
  */
-int put_io_context(struct io_context *ioc)
+static void ioc_release_fn(struct work_struct *work)
 {
-       if (ioc == NULL)
-               return 1;
+       struct io_context *ioc = container_of(work, struct io_context,
+                                             release_work);
+       struct request_queue *last_q = NULL;
 
-       BUG_ON(atomic_long_read(&ioc->refcount) == 0);
+       spin_lock_irq(&ioc->lock);
 
-       if (atomic_long_dec_and_test(&ioc->refcount)) {
-               rcu_read_lock();
-               cfq_dtor(ioc);
-               rcu_read_unlock();
+       while (!hlist_empty(&ioc->cic_list)) {
+               struct cfq_io_context *cic = hlist_entry(ioc->cic_list.first,
+                                                        struct cfq_io_context,
+                                                        cic_list);
+               struct request_queue *this_q = cic->q;
 
-               kmem_cache_free(iocontext_cachep, ioc);
-               return 1;
+               if (this_q != last_q) {
+                       /*
+                        * Need to switch to @this_q.  Once we release
+                        * @ioc->lock, it can go away along with @cic.
+                        * Hold on to it.
+                        */
+                       __blk_get_queue(this_q);
+
+                       /*
+                        * blk_put_queue() might sleep thanks to kobject
+                        * idiocy.  Always release both locks, put and
+                        * restart.
+                        */
+                       if (last_q) {
+                               spin_unlock(last_q->queue_lock);
+                               spin_unlock_irq(&ioc->lock);
+                               blk_put_queue(last_q);
+                       } else {
+                               spin_unlock_irq(&ioc->lock);
+                       }
+
+                       last_q = this_q;
+                       spin_lock_irq(this_q->queue_lock);
+                       spin_lock(&ioc->lock);
+                       continue;
+               }
+               ioc_release_depth_inc(this_q);
+               cic->exit(cic);
+               cic->release(cic);
+               ioc_release_depth_dec(this_q);
        }
-       return 0;
+
+       if (last_q) {
+               spin_unlock(last_q->queue_lock);
+               spin_unlock_irq(&ioc->lock);
+               blk_put_queue(last_q);
+       } else {
+               spin_unlock_irq(&ioc->lock);
+       }
+
+       kmem_cache_free(iocontext_cachep, ioc);
 }
-EXPORT_SYMBOL(put_io_context);
 
-static void cfq_exit(struct io_context *ioc)
+/**
+ * put_io_context - put a reference of io_context
+ * @ioc: io_context to put
+ * @locked_q: request_queue the caller is holding queue_lock of (hint)
+ *
+ * Decrement reference count of @ioc and release it if the count reaches
+ * zero.  If the caller is holding queue_lock of a queue, it can indicate
+ * that with @locked_q.  This is an optimization hint and the caller is
+ * allowed to pass in %NULL even when it's holding a queue_lock.
+ */
+void put_io_context(struct io_context *ioc, struct request_queue *locked_q)
 {
-       rcu_read_lock();
+       struct request_queue *last_q = locked_q;
+       unsigned long flags;
+
+       if (ioc == NULL)
+               return;
+
+       BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
+       if (locked_q)
+               lockdep_assert_held(locked_q->queue_lock);
+
+       if (!atomic_long_dec_and_test(&ioc->refcount))
+               return;
+
+       /*
+        * Destroy @ioc.  This is a bit messy because cic's are chained
+        * from both ioc and queue, and ioc->lock nests inside queue_lock.
+        * The inner ioc->lock should be held to walk our cic_list and then
+        * for each cic the outer matching queue_lock should be grabbed.
+        * ie. We need to do reverse-order double lock dancing.
+        *
+        * Another twist is that we are often called with one of the
+        * matching queue_locks held as indicated by @locked_q, which
+        * prevents performing double-lock dance for other queues.
+        *
+        * So, we do it in two stages.  The fast path uses the queue_lock
+        * the caller is holding and, if other queues need to be accessed,
+        * uses trylock to avoid introducing locking dependency.  This can
+        * handle most cases, especially if @ioc was performing IO on only
+        * single device.
+        *
+        * If trylock doesn't cut it, we defer to @ioc->release_work which
+        * can do all the double-locking dancing.
+        */
+       spin_lock_irqsave_nested(&ioc->lock, flags,
+                                ioc_release_depth(locked_q));
 
-       if (!hlist_empty(&ioc->cic_list)) {
-               struct cfq_io_context *cic;
+       while (!hlist_empty(&ioc->cic_list)) {
+               struct cfq_io_context *cic = hlist_entry(ioc->cic_list.first,
+                                                        struct cfq_io_context,
+                                                        cic_list);
+               struct request_queue *this_q = cic->q;
 
-               cic = hlist_entry(ioc->cic_list.first, struct cfq_io_context,
-                                                               cic_list);
-               cic->exit(ioc);
+               if (this_q != last_q) {
+                       if (last_q && last_q != locked_q)
+                               spin_unlock(last_q->queue_lock);
+                       last_q = NULL;
+
+                       if (!spin_trylock(this_q->queue_lock))
+                               break;
+                       last_q = this_q;
+                       continue;
+               }
+               ioc_release_depth_inc(this_q);
+               cic->exit(cic);
+               cic->release(cic);
+               ioc_release_depth_dec(this_q);
        }
-       rcu_read_unlock();
+
+       if (last_q && last_q != locked_q)
+               spin_unlock(last_q->queue_lock);
+
+       spin_unlock_irqrestore(&ioc->lock, flags);
+
+       /* if no cic's left, we're done; otherwise, kick release_work */
+       if (hlist_empty(&ioc->cic_list))
+               kmem_cache_free(iocontext_cachep, ioc);
+       else
+               schedule_work(&ioc->release_work);
 }
+EXPORT_SYMBOL(put_io_context);
 
 /* Called by the exiting task */
 void exit_io_context(struct task_struct *task)
 {
        struct io_context *ioc;
 
+       /* PF_EXITING prevents new io_context from being attached to @task */
+       WARN_ON_ONCE(!(current->flags & PF_EXITING));
+
        task_lock(task);
        ioc = task->io_context;
        task->io_context = NULL;
        task_unlock(task);
 
-       if (atomic_dec_and_test(&ioc->nr_tasks))
-               cfq_exit(ioc);
-
-       put_io_context(ioc);
+       atomic_dec(&ioc->nr_tasks);
+       put_io_context(ioc, NULL);
 }
 
-struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
+static struct io_context *create_task_io_context(struct task_struct *task,
+                                                gfp_t gfp_flags, int node,
+                                                bool take_ref)
 {
        struct io_context *ioc;
 
-       ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node);
-       if (ioc) {
-               atomic_long_set(&ioc->refcount, 1);
-               atomic_set(&ioc->nr_tasks, 1);
-               spin_lock_init(&ioc->lock);
-               ioc->ioprio_changed = 0;
-               ioc->ioprio = 0;
-               ioc->last_waited = 0; /* doesn't matter... */
-               ioc->nr_batch_requests = 0; /* because this is 0 */
-               INIT_RADIX_TREE(&ioc->radix_root, GFP_ATOMIC | __GFP_HIGH);
-               INIT_HLIST_HEAD(&ioc->cic_list);
-               ioc->ioc_data = NULL;
-#if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
-               ioc->cgroup_changed = 0;
-#endif
+       ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
+                                   node);
+       if (unlikely(!ioc))
+               return NULL;
+
+       /* initialize */
+       atomic_long_set(&ioc->refcount, 1);
+       atomic_set(&ioc->nr_tasks, 1);
+       spin_lock_init(&ioc->lock);
+       INIT_RADIX_TREE(&ioc->radix_root, GFP_ATOMIC | __GFP_HIGH);
+       INIT_HLIST_HEAD(&ioc->cic_list);
+       INIT_WORK(&ioc->release_work, ioc_release_fn);
+
+       /* try to install, somebody might already have beaten us to it */
+       task_lock(task);
+
+       if (!task->io_context && !(task->flags & PF_EXITING)) {
+               task->io_context = ioc;
+       } else {
+               kmem_cache_free(iocontext_cachep, ioc);
+               ioc = task->io_context;
        }
 
+       if (ioc && take_ref)
+               get_io_context(ioc);
+
+       task_unlock(task);
        return ioc;
 }
 
-/*
- * If the current task has no IO context then create one and initialise it.
- * Otherwise, return its existing IO context.
+/**
+ * current_io_context - get io_context of %current
+ * @gfp_flags: allocation flags, used if allocation is necessary
+ * @node: allocation node, used if allocation is necessary
  *
- * This returned IO context doesn't have a specifically elevated refcount,
- * but since the current task itself holds a reference, the context can be
- * used in general code, so long as it stays within `current` context.
+ * Return io_context of %current.  If it doesn't exist, it is created with
+ * @gfp_flags and @node.  The returned io_context does NOT have its
+ * reference count incremented.  Because io_context is exited only on task
+ * exit, %current can be sure that the returned io_context is valid and
+ * alive as long as it is executing.
  */
 struct io_context *current_io_context(gfp_t gfp_flags, int node)
 {
-       struct task_struct *tsk = current;
-       struct io_context *ret;
-
-       ret = tsk->io_context;
-       if (likely(ret))
-               return ret;
-
-       ret = alloc_io_context(gfp_flags, node);
-       if (ret) {
-               /* make sure set_task_ioprio() sees the settings above */
-               smp_wmb();
-               tsk->io_context = ret;
+       might_sleep_if(gfp_flags & __GFP_WAIT);
+
+       if (current->io_context)
+               return current->io_context;
+
+       return create_task_io_context(current, gfp_flags, node, false);
+}
+EXPORT_SYMBOL(current_io_context);
+
+/**
+ * get_task_io_context - get io_context of a task
+ * @task: task of interest
+ * @gfp_flags: allocation flags, used if allocation is necessary
+ * @node: allocation node, used if allocation is necessary
+ *
+ * Return io_context of @task.  If it doesn't exist, it is created with
+ * @gfp_flags and @node.  The returned io_context has its reference count
+ * incremented.
+ *
+ * This function always goes through task_lock() and it's better to use
+ * current_io_context() + get_io_context() for %current.
+ */
+struct io_context *get_task_io_context(struct task_struct *task,
+                                      gfp_t gfp_flags, int node)
+{
+       struct io_context *ioc;
+
+       might_sleep_if(gfp_flags & __GFP_WAIT);
+
+       task_lock(task);
+       ioc = task->io_context;
+       if (likely(ioc)) {
+               get_io_context(ioc);
+               task_unlock(task);
+               return ioc;
        }
+       task_unlock(task);
 
-       return ret;
+       return create_task_io_context(task, gfp_flags, node, true);
 }
+EXPORT_SYMBOL(get_task_io_context);
 
-/*
- * If the current task has no IO context then create one and initialise it.
- * If it does have a context, take a ref on it.
+void ioc_set_changed(struct io_context *ioc, int which)
+{
+       struct cfq_io_context *cic;
+       struct hlist_node *n;
+
+       hlist_for_each_entry(cic, n, &ioc->cic_list, cic_list)
+               set_bit(which, &cic->changed);
+}
+
+/**
+ * ioc_ioprio_changed - notify ioprio change
+ * @ioc: io_context of interest
+ * @ioprio: new ioprio
  *
- * This is always called in the context of the task which submitted the I/O.
+ * @ioc's ioprio has changed to @ioprio.  Set %CIC_IOPRIO_CHANGED for all
+ * cic's.  iosched is responsible for checking the bit and applying it on
+ * request issue path.
  */
-struct io_context *get_io_context(gfp_t gfp_flags, int node)
+void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
 {
-       struct io_context *ioc = NULL;
+       unsigned long flags;
 
-       /*
-        * Check for unlikely race with exiting task. ioc ref count is
-        * zero when ioc is being detached.
-        */
-       do {
-               ioc = current_io_context(gfp_flags, node);
-               if (unlikely(!ioc))
-                       break;
-       } while (!atomic_long_inc_not_zero(&ioc->refcount));
+       spin_lock_irqsave(&ioc->lock, flags);
+       ioc->ioprio = ioprio;
+       ioc_set_changed(ioc, CIC_IOPRIO_CHANGED);
+       spin_unlock_irqrestore(&ioc->lock, flags);
+}
 
-       return ioc;
+/**
+ * ioc_cgroup_changed - notify cgroup change
+ * @ioc: io_context of interest
+ *
+ * @ioc's cgroup has changed.  Set %CIC_CGROUP_CHANGED for all cic's.
+ * iosched is responsible for checking the bit and applying it on request
+ * issue path.
+ */
+void ioc_cgroup_changed(struct io_context *ioc)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&ioc->lock, flags);
+       ioc_set_changed(ioc, CIC_CGROUP_CHANGED);
+       spin_unlock_irqrestore(&ioc->lock, flags);
 }
-EXPORT_SYMBOL(get_io_context);
 
 static int __init blk_ioc_init(void)
 {