Re: [PATCH v2] block: fix ioc leak in put_io_context

From: Xiaotian Feng
Date: Tue Mar 13 2012 - 18:44:22 EST


On Tue, Mar 13, 2012 at 11:49 PM, Tejun Heo <tj@xxxxxxxxxx> wrote:
> On Tue, Mar 13, 2012 at 10:28:20AM -0400, Vivek Goyal wrote:
>> On Tue, Mar 13, 2012 at 01:21:06PM -0400, Xiaotian Feng wrote:
>> > diff --git a/block/blk-ioc.c b/block/blk-ioc.c
>> > index 8b782a6..9690f27 100644
>> > --- a/block/blk-ioc.c
>> > +++ b/block/blk-ioc.c
>> > @@ -145,6 +145,7 @@ static void ioc_release_fn(struct work_struct *work)
>> > Âvoid put_io_context(struct io_context *ioc)
>> > Â{
>> > Â Â unsigned long flags;
>> > + Â bool free_ioc = false;
>> >
>> > Â Â if (ioc == NULL)
>> > Â Â Â Â Â Â return;
>> > @@ -159,8 +160,13 @@ void put_io_context(struct io_context *ioc)
>> > Â Â Â Â Â Â spin_lock_irqsave(&ioc->lock, flags);
>> > Â Â Â Â Â Â if (!hlist_empty(&ioc->icq_list))
>> > Â Â Â Â Â Â Â Â Â Â schedule_work(&ioc->release_work);
>> > + Â Â Â Â Â else
>> > + Â Â Â Â Â Â Â Â Â free_ioc = true;
>
> Calling kmem_cache_free() here directly is probably better.

I did this on my first try, but I got a kernel warning with the
following spin_unlock on ioc->lock :(
We'll hit a use after free bug then...

>
>> > Â Â Â Â Â Â spin_unlock_irqrestore(&ioc->lock, flags);
>> > Â Â }
>> > +
>> > + Â if (free_ioc)
>> > + Â Â Â Â Â kmem_cache_free(iocontext_cachep, ioc);
>> > Â}
>> > ÂEXPORT_SYMBOL(put_io_context);
>>
>> This one looks good to me. Tejun?
>>
>> Acked-by: Vivek Goyal <vgoyal@xxxxxxxxxx>
>
> Acked-by: Tejun Heo <tj@xxxxxxxxxx>
>
> Thanks.
>
> --
> tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/