Commit 4ac845a2 authored by Jens Axboe's avatar Jens Axboe

block: cfq: make the io contect sharing lockless

The io context sharing introduced a per-ioc spinlock, that would protect
the cfq io context lookup. That is a regression from the original, since
we never needed any locking there because the ioc/cic were process private.

The cic lookup is changed from an rbtree construct to a radix tree, which
we can then use RCU to make the reader side lockless. That is the performance
critical path, modifying the radix tree is only done on process creation
(when that process first does IO, actually) and on process exit (if that
process has done IO).

As it so happens, radix trees are also much faster for this type of
lookup where the key is a pointer. It's a very sparse tree.
Signed-off-by: default avatarJens Axboe <jens.axboe@oracle.com>
parent 66dac98e
...@@ -200,7 +200,7 @@ CFQ_CFQQ_FNS(sync); ...@@ -200,7 +200,7 @@ CFQ_CFQQ_FNS(sync);
static void cfq_dispatch_insert(struct request_queue *, struct request *); static void cfq_dispatch_insert(struct request_queue *, struct request *);
static struct cfq_queue *cfq_get_queue(struct cfq_data *, int, static struct cfq_queue *cfq_get_queue(struct cfq_data *, int,
struct io_context *, gfp_t); struct io_context *, gfp_t);
static struct cfq_io_context *cfq_cic_rb_lookup(struct cfq_data *, static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
struct io_context *); struct io_context *);
static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic, static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
...@@ -609,7 +609,7 @@ cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) ...@@ -609,7 +609,7 @@ cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
struct cfq_io_context *cic; struct cfq_io_context *cic;
struct cfq_queue *cfqq; struct cfq_queue *cfqq;
cic = cfq_cic_rb_lookup(cfqd, tsk->io_context); cic = cfq_cic_lookup(cfqd, tsk->io_context);
if (!cic) if (!cic)
return NULL; return NULL;
...@@ -721,7 +721,7 @@ static int cfq_allow_merge(struct request_queue *q, struct request *rq, ...@@ -721,7 +721,7 @@ static int cfq_allow_merge(struct request_queue *q, struct request *rq,
* Lookup the cfqq that this bio will be queued with. Allow * Lookup the cfqq that this bio will be queued with. Allow
* merge only if rq is queued there. * merge only if rq is queued there.
*/ */
cic = cfq_cic_rb_lookup(cfqd, current->io_context); cic = cfq_cic_lookup(cfqd, current->io_context);
if (!cic) if (!cic)
return 0; return 0;
...@@ -1170,29 +1170,74 @@ static void cfq_put_queue(struct cfq_queue *cfqq) ...@@ -1170,29 +1170,74 @@ static void cfq_put_queue(struct cfq_queue *cfqq)
kmem_cache_free(cfq_pool, cfqq); kmem_cache_free(cfq_pool, cfqq);
} }
static void cfq_free_io_context(struct io_context *ioc) /*
* Call func for each cic attached to this ioc. Returns number of cic's seen.
*/
#define CIC_GANG_NR 16
static unsigned int
call_for_each_cic(struct io_context *ioc,
void (*func)(struct io_context *, struct cfq_io_context *))
{ {
struct cfq_io_context *__cic; struct cfq_io_context *cics[CIC_GANG_NR];
struct rb_node *n; unsigned long index = 0;
int freed = 0; unsigned int called = 0;
int nr;
ioc->ioc_data = NULL; rcu_read_lock();
spin_lock(&ioc->lock); do {
int i;
while ((n = rb_first(&ioc->cic_root)) != NULL) { /*
__cic = rb_entry(n, struct cfq_io_context, rb_node); * Perhaps there's a better way - this just gang lookups from
rb_erase(&__cic->rb_node, &ioc->cic_root); * 0 to the end, restarting after each CIC_GANG_NR from the
kmem_cache_free(cfq_ioc_pool, __cic); * last key + 1.
freed++; */
} nr = radix_tree_gang_lookup(&ioc->radix_root, (void **) cics,
index, CIC_GANG_NR);
if (!nr)
break;
called += nr;
index = 1 + (unsigned long) cics[nr - 1]->key;
for (i = 0; i < nr; i++)
func(ioc, cics[i]);
} while (nr == CIC_GANG_NR);
rcu_read_unlock();
return called;
}
static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
{
unsigned long flags;
BUG_ON(!cic->dead_key);
spin_lock_irqsave(&ioc->lock, flags);
radix_tree_delete(&ioc->radix_root, cic->dead_key);
spin_unlock_irqrestore(&ioc->lock, flags);
kmem_cache_free(cfq_ioc_pool, cic);
}
static void cfq_free_io_context(struct io_context *ioc)
{
int freed;
/*
* ioc->refcount is zero here, so no more cic's are allowed to be
* linked into this ioc. So it should be ok to iterate over the known
* list, we will see all cic's since no new ones are added.
*/
freed = call_for_each_cic(ioc, cic_free_func);
elv_ioc_count_mod(ioc_count, -freed); elv_ioc_count_mod(ioc_count, -freed);
if (ioc_gone && !elv_ioc_count_read(ioc_count)) if (ioc_gone && !elv_ioc_count_read(ioc_count))
complete(ioc_gone); complete(ioc_gone);
spin_unlock(&ioc->lock);
} }
static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
...@@ -1209,7 +1254,12 @@ static void __cfq_exit_single_io_context(struct cfq_data *cfqd, ...@@ -1209,7 +1254,12 @@ static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
struct cfq_io_context *cic) struct cfq_io_context *cic)
{ {
list_del_init(&cic->queue_list); list_del_init(&cic->queue_list);
/*
* Make sure key == NULL is seen for dead queues
*/
smp_wmb(); smp_wmb();
cic->dead_key = (unsigned long) cic->key;
cic->key = NULL; cic->key = NULL;
if (cic->cfqq[ASYNC]) { if (cic->cfqq[ASYNC]) {
...@@ -1223,16 +1273,18 @@ static void __cfq_exit_single_io_context(struct cfq_data *cfqd, ...@@ -1223,16 +1273,18 @@ static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
} }
} }
static void cfq_exit_single_io_context(struct cfq_io_context *cic) static void cfq_exit_single_io_context(struct io_context *ioc,
struct cfq_io_context *cic)
{ {
struct cfq_data *cfqd = cic->key; struct cfq_data *cfqd = cic->key;
if (cfqd) { if (cfqd) {
struct request_queue *q = cfqd->queue; struct request_queue *q = cfqd->queue;
unsigned long flags;
spin_lock_irq(q->queue_lock); spin_lock_irqsave(q->queue_lock, flags);
__cfq_exit_single_io_context(cfqd, cic); __cfq_exit_single_io_context(cfqd, cic);
spin_unlock_irq(q->queue_lock); spin_unlock_irqrestore(q->queue_lock, flags);
} }
} }
...@@ -1242,24 +1294,8 @@ static void cfq_exit_single_io_context(struct cfq_io_context *cic) ...@@ -1242,24 +1294,8 @@ static void cfq_exit_single_io_context(struct cfq_io_context *cic)
*/ */
static void cfq_exit_io_context(struct io_context *ioc) static void cfq_exit_io_context(struct io_context *ioc)
{ {
struct cfq_io_context *__cic; rcu_assign_pointer(ioc->ioc_data, NULL);
struct rb_node *n; call_for_each_cic(ioc, cfq_exit_single_io_context);
ioc->ioc_data = NULL;
spin_lock(&ioc->lock);
/*
* put the reference this task is holding to the various queues
*/
n = rb_first(&ioc->cic_root);
while (n != NULL) {
__cic = rb_entry(n, struct cfq_io_context, rb_node);
cfq_exit_single_io_context(__cic);
n = rb_next(n);
}
spin_unlock(&ioc->lock);
} }
static struct cfq_io_context * static struct cfq_io_context *
...@@ -1323,7 +1359,8 @@ static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc) ...@@ -1323,7 +1359,8 @@ static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
cfq_clear_cfqq_prio_changed(cfqq); cfq_clear_cfqq_prio_changed(cfqq);
} }
static inline void changed_ioprio(struct cfq_io_context *cic) static inline void changed_ioprio(struct io_context *ioc,
struct cfq_io_context *cic)
{ {
struct cfq_data *cfqd = cic->key; struct cfq_data *cfqd = cic->key;
struct cfq_queue *cfqq; struct cfq_queue *cfqq;
...@@ -1353,22 +1390,8 @@ static inline void changed_ioprio(struct cfq_io_context *cic) ...@@ -1353,22 +1390,8 @@ static inline void changed_ioprio(struct cfq_io_context *cic)
static void cfq_ioc_set_ioprio(struct io_context *ioc) static void cfq_ioc_set_ioprio(struct io_context *ioc)
{ {
struct cfq_io_context *cic; call_for_each_cic(ioc, changed_ioprio);
struct rb_node *n;
spin_lock(&ioc->lock);
ioc->ioprio_changed = 0; ioc->ioprio_changed = 0;
n = rb_first(&ioc->cic_root);
while (n != NULL) {
cic = rb_entry(n, struct cfq_io_context, rb_node);
changed_ioprio(cic);
n = rb_next(n);
}
spin_unlock(&ioc->lock);
} }
static struct cfq_queue * static struct cfq_queue *
...@@ -1379,7 +1402,7 @@ cfq_find_alloc_queue(struct cfq_data *cfqd, int is_sync, ...@@ -1379,7 +1402,7 @@ cfq_find_alloc_queue(struct cfq_data *cfqd, int is_sync,
struct cfq_io_context *cic; struct cfq_io_context *cic;
retry: retry:
cic = cfq_cic_rb_lookup(cfqd, ioc); cic = cfq_cic_lookup(cfqd, ioc);
/* cic always exists here */ /* cic always exists here */
cfqq = cic_to_cfqq(cic, is_sync); cfqq = cic_to_cfqq(cic, is_sync);
...@@ -1480,28 +1503,42 @@ cfq_get_queue(struct cfq_data *cfqd, int is_sync, struct io_context *ioc, ...@@ -1480,28 +1503,42 @@ cfq_get_queue(struct cfq_data *cfqd, int is_sync, struct io_context *ioc,
return cfqq; return cfqq;
} }
static void cfq_cic_free(struct cfq_io_context *cic)
{
kmem_cache_free(cfq_ioc_pool, cic);
elv_ioc_count_dec(ioc_count);
if (ioc_gone && !elv_ioc_count_read(ioc_count))
complete(ioc_gone);
}
/* /*
* We drop cfq io contexts lazily, so we may find a dead one. * We drop cfq io contexts lazily, so we may find a dead one.
*/ */
static void static void
cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic) cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
struct cfq_io_context *cic)
{ {
unsigned long flags;
WARN_ON(!list_empty(&cic->queue_list)); WARN_ON(!list_empty(&cic->queue_list));
spin_lock_irqsave(&ioc->lock, flags);
if (ioc->ioc_data == cic) if (ioc->ioc_data == cic)
ioc->ioc_data = NULL; rcu_assign_pointer(ioc->ioc_data, NULL);
rb_erase(&cic->rb_node, &ioc->cic_root); radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
kmem_cache_free(cfq_ioc_pool, cic); spin_unlock_irqrestore(&ioc->lock, flags);
elv_ioc_count_dec(ioc_count);
cfq_cic_free(cic);
} }
static struct cfq_io_context * static struct cfq_io_context *
cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc) cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
{ {
struct rb_node *n;
struct cfq_io_context *cic; struct cfq_io_context *cic;
void *k, *key = cfqd; void *k;
if (unlikely(!ioc)) if (unlikely(!ioc))
return NULL; return NULL;
...@@ -1509,79 +1546,65 @@ cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc) ...@@ -1509,79 +1546,65 @@ cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
/* /*
* we maintain a last-hit cache, to avoid browsing over the tree * we maintain a last-hit cache, to avoid browsing over the tree
*/ */
cic = ioc->ioc_data; cic = rcu_dereference(ioc->ioc_data);
if (cic && cic->key == cfqd) if (cic && cic->key == cfqd)
return cic; return cic;
spin_lock(&ioc->lock); do {
restart: rcu_read_lock();
n = ioc->cic_root.rb_node; cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
while (n) { rcu_read_unlock();
cic = rb_entry(n, struct cfq_io_context, rb_node); if (!cic)
break;
/* ->key must be copied to avoid race with cfq_exit_queue() */ /* ->key must be copied to avoid race with cfq_exit_queue() */
k = cic->key; k = cic->key;
if (unlikely(!k)) { if (unlikely(!k)) {
cfq_drop_dead_cic(ioc, cic); cfq_drop_dead_cic(cfqd, ioc, cic);
goto restart; continue;
} }
if (key < k) rcu_assign_pointer(ioc->ioc_data, cic);
n = n->rb_left; break;
else if (key > k) } while (1);
n = n->rb_right;
else {
ioc->ioc_data = cic;
spin_unlock(&ioc->lock);
return cic;
}
}
spin_unlock(&ioc->lock); return cic;
return NULL;
} }
static inline void /*
* Add cic into ioc, using cfqd as the search key. This enables us to lookup
* the process specific cfq io context when entered from the block layer.
* Also adds the cic to a per-cfqd list, used when this queue is removed.
*/
static inline int
cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc, cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
struct cfq_io_context *cic) struct cfq_io_context *cic, gfp_t gfp_mask)
{ {
struct rb_node **p;
struct rb_node *parent;
struct cfq_io_context *__cic;
unsigned long flags; unsigned long flags;
void *k; int ret;
spin_lock(&ioc->lock); ret = radix_tree_preload(gfp_mask);
if (!ret) {
cic->ioc = ioc; cic->ioc = ioc;
cic->key = cfqd; cic->key = cfqd;
restart: spin_lock_irqsave(&ioc->lock, flags);
parent = NULL; ret = radix_tree_insert(&ioc->radix_root,
p = &ioc->cic_root.rb_node; (unsigned long) cfqd, cic);
while (*p) { spin_unlock_irqrestore(&ioc->lock, flags);
parent = *p;
__cic = rb_entry(parent, struct cfq_io_context, rb_node);
/* ->key must be copied to avoid race with cfq_exit_queue() */
k = __cic->key;
if (unlikely(!k)) {
cfq_drop_dead_cic(ioc, __cic);
goto restart;
}
if (cic->key < k)
p = &(*p)->rb_left;
else if (cic->key > k)
p = &(*p)->rb_right;
else
BUG();
}
rb_link_node(&cic->rb_node, parent, p); radix_tree_preload_end();
rb_insert_color(&cic->rb_node, &ioc->cic_root);
if (!ret) {
spin_lock_irqsave(cfqd->queue->queue_lock, flags); spin_lock_irqsave(cfqd->queue->queue_lock, flags);
list_add(&cic->queue_list, &cfqd->cic_list); list_add(&cic->queue_list, &cfqd->cic_list);
spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
spin_unlock(&ioc->lock); }
}
if (ret)
printk(KERN_ERR "cfq: cic link failed!\n");
return ret;
} }
/* /*
...@@ -1601,7 +1624,7 @@ cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) ...@@ -1601,7 +1624,7 @@ cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
if (!ioc) if (!ioc)
return NULL; return NULL;
cic = cfq_cic_rb_lookup(cfqd, ioc); cic = cfq_cic_lookup(cfqd, ioc);
if (cic) if (cic)
goto out; goto out;
...@@ -1609,13 +1632,17 @@ cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) ...@@ -1609,13 +1632,17 @@ cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
if (cic == NULL) if (cic == NULL)
goto err; goto err;
cfq_cic_link(cfqd, ioc, cic); if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
goto err_free;
out: out:
smp_read_barrier_depends(); smp_read_barrier_depends();
if (unlikely(ioc->ioprio_changed)) if (unlikely(ioc->ioprio_changed))
cfq_ioc_set_ioprio(ioc); cfq_ioc_set_ioprio(ioc);
return cic; return cic;
err_free:
cfq_cic_free(cic);
err: err:
put_io_context(ioc); put_io_context(ioc);
return NULL; return NULL;
...@@ -1909,7 +1936,7 @@ static int cfq_may_queue(struct request_queue *q, int rw) ...@@ -1909,7 +1936,7 @@ static int cfq_may_queue(struct request_queue *q, int rw)
* so just lookup a possibly existing queue, or return 'may queue' * so just lookup a possibly existing queue, or return 'may queue'
* if that fails * if that fails
*/ */
cic = cfq_cic_rb_lookup(cfqd, tsk->io_context); cic = cfq_cic_lookup(cfqd, tsk->io_context);
if (!cic) if (!cic)
return ELV_MQUEUE_MAY; return ELV_MQUEUE_MAY;
...@@ -2174,7 +2201,7 @@ static int __init cfq_slab_setup(void) ...@@ -2174,7 +2201,7 @@ static int __init cfq_slab_setup(void)
if (!cfq_pool) if (!cfq_pool)
goto fail; goto fail;
cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0); cfq_ioc_pool = KMEM_CACHE(cfq_io_context, SLAB_DESTROY_BY_RCU);
if (!cfq_ioc_pool) if (!cfq_ioc_pool)
goto fail; goto fail;
......
...@@ -3853,6 +3853,21 @@ int __init blk_dev_init(void) ...@@ -3853,6 +3853,21 @@ int __init blk_dev_init(void)
return 0; return 0;
} }
static void cfq_dtor(struct io_context *ioc)
{
struct cfq_io_context *cic[1];
int r;
/*
* We don't have a specific key to lookup with, so use the gang
* lookup to just retrieve the first item stored. The cfq exit
* function will iterate the full tree, so any member will do.
*/
r = radix_tree_gang_lookup(&ioc->radix_root, (void **) cic, 0, 1);
if (r > 0)
cic[0]->dtor(ioc);
}
/* /*
* IO Context helper functions. put_io_context() returns 1 if there are no * IO Context helper functions. put_io_context() returns 1 if there are no
* more users of this io context, 0 otherwise. * more users of this io context, 0 otherwise.
...@@ -3865,18 +3880,11 @@ int put_io_context(struct io_context *ioc) ...@@ -3865,18 +3880,11 @@ int put_io_context(struct io_context *ioc)
BUG_ON(atomic_read(&ioc->refcount) == 0); BUG_ON(atomic_read(&ioc->refcount) == 0);
if (atomic_dec_and_test(&ioc->refcount)) { if (atomic_dec_and_test(&ioc->refcount)) {
struct cfq_io_context *cic;
rcu_read_lock(); rcu_read_lock();
if (ioc->aic && ioc->aic->dtor) if (ioc->aic && ioc->aic->dtor)
ioc->aic->dtor(ioc->aic); ioc->aic->dtor(ioc->aic);
if (ioc->cic_root.rb_node != NULL) {
struct rb_node *n = rb_first(&ioc->cic_root);
cic = rb_entry(n, struct cfq_io_context, rb_node);
cic->dtor(ioc);
}
rcu_read_unlock(); rcu_read_unlock();
cfq_dtor(ioc);
kmem_cache_free(iocontext_cachep, ioc); kmem_cache_free(iocontext_cachep, ioc);
return 1; return 1;
...@@ -3885,11 +3893,26 @@ int put_io_context(struct io_context *ioc) ...@@ -3885,11 +3893,26 @@ int put_io_context(struct io_context *ioc)
} }
EXPORT_SYMBOL(put_io_context); EXPORT_SYMBOL(put_io_context);
static void cfq_exit(struct io_context *ioc)
{
struct cfq_io_context *cic[1];
int r;
rcu_read_lock();
/*
* See comment for cfq_dtor()
*/
r = radix_tree_gang_lookup(&ioc->radix_root, (void **) cic, 0, 1);
rcu_read_unlock();
if (r > 0)
cic[0]->exit(ioc);
}
/* Called by the exitting task */ /* Called by the exitting task */
void exit_io_context(void) void exit_io_context(void)
{ {
struct io_context *ioc; struct io_context *ioc;
struct cfq_io_context *cic;
task_lock(current); task_lock(current);
ioc = current->io_context; ioc = current->io_context;
...@@ -3899,11 +3922,7 @@ void exit_io_context(void) ...@@ -3899,11 +3922,7 @@ void exit_io_context(void)
if (atomic_dec_and_test(&ioc->nr_tasks)) { if (atomic_dec_and_test(&ioc->nr_tasks)) {
if (ioc->aic && ioc->aic->exit) if (ioc->aic && ioc->aic->exit)
ioc->aic->exit(ioc->aic); ioc->aic->exit(ioc->aic);
if (ioc->cic_root.rb_node != NULL) { cfq_exit(ioc);
cic = rb_entry(rb_first(&ioc->cic_root),
struct cfq_io_context, rb_node);
cic->exit(ioc);
}
put_io_context(ioc); put_io_context(ioc);
} }
...@@ -3923,7 +3942,7 @@ struct io_context *alloc_io_context(gfp_t gfp_flags, int node) ...@@ -3923,7 +3942,7 @@ struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
ret->last_waited = jiffies; /* doesn't matter... */ ret->last_waited = jiffies; /* doesn't matter... */
ret->nr_batch_requests = 0; /* because this is 0 */ ret->nr_batch_requests = 0; /* because this is 0 */
ret->aic = NULL; ret->aic = NULL;
ret->cic_root.rb_node = NULL; INIT_RADIX_TREE(&ret->radix_root, GFP_ATOMIC | __GFP_HIGH);
ret->ioc_data = NULL; ret->ioc_data = NULL;
} }
......
#ifndef IOCONTEXT_H #ifndef IOCONTEXT_H
#define IOCONTEXT_H #define IOCONTEXT_H
#include <linux/radix-tree.h>
/* /*
* This is the per-process anticipatory I/O scheduler state. * This is the per-process anticipatory I/O scheduler state.
*/ */
...@@ -29,8 +31,8 @@ struct as_io_context { ...@@ -29,8 +31,8 @@ struct as_io_context {
struct cfq_queue; struct cfq_queue;
struct cfq_io_context { struct cfq_io_context {
struct rb_node rb_node;
void *key; void *key;
unsigned long dead_key;
struct cfq_queue *cfqq[2]; struct cfq_queue *cfqq[2];
...@@ -74,7 +76,7 @@ struct io_context { ...@@ -74,7 +76,7 @@ struct io_context {
int nr_batch_requests; /* Number of requests left in the batch */ int nr_batch_requests; /* Number of requests left in the batch */
struct as_io_context *aic; struct as_io_context *aic;
struct rb_root cic_root; struct radix_tree_root radix_root;
void *ioc_data; void *ioc_data;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment