Use kref for reference counter of io_context. This patch also removes BUG_ON() for freeing unreferenced io_context. But kref can detect it if CONFIG_DEBUG_SLAB is enabled. Signed-off-by: Akinobu Mita CC: Jens Axboe CC: Greg KH block/cfq-iosched.c | 2 +- block/ll_rw_blk.c | 43 +++++++++++++++++++++---------------------- include/linux/blkdev.h | 2 +- 3 files changed, 23 insertions(+), 24 deletions(-) Index: 2.6-git/block/ll_rw_blk.c =================================================================== --- 2.6-git.orig/block/ll_rw_blk.c +++ 2.6-git/block/ll_rw_blk.c @@ -3536,29 +3536,29 @@ int __init blk_dev_init(void) /* * IO Context helper functions */ +static void release_io_context(struct kref *kref) +{ + struct io_context *ioc = container_of(kref, struct io_context, kref); + struct cfq_io_context *cic; + + rcu_read_lock(); + if (ioc->aic && ioc->aic->dtor) + 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(); + kmem_cache_free(iocontext_cachep, ioc); +} + void put_io_context(struct io_context *ioc) { if (ioc == NULL) return; - BUG_ON(atomic_read(&ioc->refcount) == 0); - - if (atomic_dec_and_test(&ioc->refcount)) { - struct cfq_io_context *cic; - - rcu_read_lock(); - if (ioc->aic && ioc->aic->dtor) - 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(); - - kmem_cache_free(iocontext_cachep, ioc); - } + kref_put(&ioc->kref, release_io_context); } EXPORT_SYMBOL(put_io_context); @@ -3606,7 +3606,7 @@ struct io_context *current_io_context(gf ret = kmem_cache_alloc(iocontext_cachep, gfp_flags); if (ret) { - atomic_set(&ret->refcount, 1); + kref_init(&ret->kref); ret->task = current; ret->set_ioprio = NULL; ret->last_waited = jiffies; /* doesn't matter... */ @@ -3631,7 +3631,7 @@ struct io_context *get_io_context(gfp_t struct io_context *ret; ret = current_io_context(gfp_flags); if (likely(ret)) - atomic_inc(&ret->refcount); + kref_get(&ret->kref); return ret; } EXPORT_SYMBOL(get_io_context); @@ -3642,8 +3642,7 @@ void copy_io_context(struct io_context * struct io_context *dst = *pdst; if (src) { - BUG_ON(atomic_read(&src->refcount) == 0); - atomic_inc(&src->refcount); + kref_get(&src->kref); put_io_context(dst); *pdst = src; } Index: 2.6-git/include/linux/blkdev.h =================================================================== --- 2.6-git.orig/include/linux/blkdev.h +++ 2.6-git/include/linux/blkdev.h @@ -88,7 +88,7 @@ struct cfq_io_context { * (apart from the atomic refcount), so require no locking. */ struct io_context { - atomic_t refcount; + struct kref kref; struct task_struct *task; int (*set_ioprio)(struct io_context *, unsigned int); Index: 2.6-git/block/cfq-iosched.c =================================================================== --- 2.6-git.orig/block/cfq-iosched.c +++ 2.6-git/block/cfq-iosched.c @@ -1068,7 +1068,7 @@ __cfq_dispatch_requests(struct cfq_data dispatched++; if (!cfqd->active_cic) { - atomic_inc(&crq->io_context->ioc->refcount); + kref_get(&crq->io_context->ioc->kref); cfqd->active_cic = crq->io_context; } --