RE: [PATCH V2 for-6.10/block 1/2] loop: Fix a race between loop detach and loop open

Gulam Mohamed
2024-05-23T18:39:20Z

Hi,

> -----Original Message-----
> From: Christoph Hellwig <hch@lst.de>
> Sent: Thursday, May 23, 2024 1:42 PM
> To: Gulam Mohamed <gulam.mohamed@oracle.com>
> Cc: linux-block@vger.kernel.org; linux-kernel@vger.kernel.org;
> axboe@kernel.dk; shinichiro.kawasaki@wdc.com; chaitanyak@nvidia.com;
> hch@lst.de
> Subject: Re: [PATCH V2 for-6.10/block 1/2] loop: Fix a race between loop
> detach and loop open
>
> On Tue, May 21, 2024 at 10:42:48PM +0000, Gulam Mohamed wrote:
> > Description
> > ===========
>
> That's a weird way to format a patch. Between this and the odd subject not
> matching patch 2 I was tricked into thinking this was just a cover letter and
> patch 1 was missing for a while. Please take a look at other patches/commit
> and try to word it similarly.
I will take care of this in the next version.
>
> > V1->V2:
> > Added a test case, 010, in blktests in tests/loop/
>
> These kind of patch revision changelogs belong after the --- so that they don't
> go into git history. Or even better into the cover letter, which is missing here.
>
Sure. I will take care of this in the next version.
> > Signed-off-by: Gulam Mohamed <gulam.mohamed@oracle.com>
> > ---
> > drivers/block/loop.c | 19 +++++++++++++++++++
> > 1 file changed, 19 insertions(+)
> >
> > diff --git a/drivers/block/loop.c b/drivers/block/loop.c index
> > 28a95fd366fe..9a235d8c062d 100644
> > --- a/drivers/block/loop.c
> > +++ b/drivers/block/loop.c
> > @@ -1717,6 +1717,24 @@ static int lo_compat_ioctl(struct block_device
> > *bdev, blk_mode_t mode, } #endif
> >
> > +static int lo_open(struct gendisk *disk, blk_mode_t mode) {
> > + struct loop_device *lo = disk->private_data;
> > + int err;
> > +
> > + if (!lo)
> > + return -ENXIO;
>
> ->private_data is never cleared, so the NULL check here doesn't
> make sense.
>
> > + err = mutex_lock_killable(&lo->lo_mutex);
> > + if (err)
> > + return err;
> > +
> > + if (lo->lo_state == Lo_rundown)
> > + err = -ENXIO;
> > + mutex_unlock(&lo->lo_mutex);
>
> What if we race with setting Lo_rundown and that gets set right after we
> unlock here?
Similar race was mentioned by Kuai in his comments. We think these race conditions can be resolved by bringing back the "lo->refcnt" ,
by reverting the commit a0e286b6a5b61d4da01bdf865071c4da417046d6 plus the above Lo_rundown check in lo_open.