Patchwork [7/8] xen_disk: simplify blk_disconnect with refcnt

login
register
mail settings
Submitter Fam Zheng
Date July 25, 2013, 9:01 a.m.
Message ID <1374742906-4489-8-git-send-email-famz@redhat.com>
Download mbox | patch
Permalink /patch/261635/
State New
Headers show

Comments

Fam Zheng - July 25, 2013, 9:01 a.m.
We call bdrv_attach_dev when initializing whether or not bs is created
locally, so call bdrv_detach_dev and let the refcnt handle the
lifecycle.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 hw/block/xen_disk.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
Jeff Cody - July 25, 2013, 12:56 p.m.
On Thu, Jul 25, 2013 at 05:01:45PM +0800, Fam Zheng wrote:
> We call bdrv_attach_dev when initializing whether or not bs is created
> locally, so call bdrv_detach_dev and let the refcnt handle the
> lifecycle.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  hw/block/xen_disk.c | 7 +------
>  1 file changed, 1 insertion(+), 6 deletions(-)
> 
> diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
> index 99537e8..3ec4bd2 100644
> --- a/hw/block/xen_disk.c
> +++ b/hw/block/xen_disk.c
> @@ -910,12 +910,7 @@ static void blk_disconnect(struct XenDevice *xendev)
>      struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
>  
>      if (blkdev->bs) {
> -        if (!blkdev->dinfo) {
> -            /* close/delete only if we created it ourself */
> -            bdrv_close(blkdev->bs);
> -            bdrv_detach_dev(blkdev->bs, blkdev);
> -            bdrv_unref(blkdev->bs);
> -        }
> +        bdrv_detach_dev(blkdev->bs, blkdev);
>          blkdev->bs = NULL;

If the refcnt for blkdev->bs is > 1, then this leaks blkdev->bs, and
doesn't call bdrv_close().

>      }
>      xen_be_unbind_evtchn(&blkdev->xendev);
> -- 
> 1.8.3.2
> 
>
Jeff Cody - July 26, 2013, 1:30 a.m.
On Thu, Jul 25, 2013 at 08:56:41AM -0400, Jeff Cody wrote:
> On Thu, Jul 25, 2013 at 05:01:45PM +0800, Fam Zheng wrote:
> > We call bdrv_attach_dev when initializing whether or not bs is created
> > locally, so call bdrv_detach_dev and let the refcnt handle the
> > lifecycle.
> > 
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> > ---
> >  hw/block/xen_disk.c | 7 +------
> >  1 file changed, 1 insertion(+), 6 deletions(-)
> > 
> > diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
> > index 99537e8..3ec4bd2 100644
> > --- a/hw/block/xen_disk.c
> > +++ b/hw/block/xen_disk.c
> > @@ -910,12 +910,7 @@ static void blk_disconnect(struct XenDevice *xendev)
> >      struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
> >  
> >      if (blkdev->bs) {
> > -        if (!blkdev->dinfo) {
> > -            /* close/delete only if we created it ourself */
> > -            bdrv_close(blkdev->bs);
> > -            bdrv_detach_dev(blkdev->bs, blkdev);
> > -            bdrv_unref(blkdev->bs);
> > -        }
> > +        bdrv_detach_dev(blkdev->bs, blkdev);
> >          blkdev->bs = NULL;
> 
> If the refcnt for blkdev->bs is > 1, then this leaks blkdev->bs, and
> doesn't call bdrv_close().
> 

Same with this, I don't think it will leak this either.

> >      }
> >      xen_be_unbind_evtchn(&blkdev->xendev);
> > -- 
> > 1.8.3.2
> > 
> >

Patch

diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
index 99537e8..3ec4bd2 100644
--- a/hw/block/xen_disk.c
+++ b/hw/block/xen_disk.c
@@ -910,12 +910,7 @@  static void blk_disconnect(struct XenDevice *xendev)
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
 
     if (blkdev->bs) {
-        if (!blkdev->dinfo) {
-            /* close/delete only if we created it ourself */
-            bdrv_close(blkdev->bs);
-            bdrv_detach_dev(blkdev->bs, blkdev);
-            bdrv_unref(blkdev->bs);
-        }
+        bdrv_detach_dev(blkdev->bs, blkdev);
         blkdev->bs = NULL;
     }
     xen_be_unbind_evtchn(&blkdev->xendev);