From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39853) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ai1uB-00006T-31 for qemu-devel@nongnu.org; Mon, 21 Mar 2016 11:38:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ai1u7-0004sP-UC for qemu-devel@nongnu.org; Mon, 21 Mar 2016 11:38:07 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58727) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ai1u7-0004sJ-M8 for qemu-devel@nongnu.org; Mon, 21 Mar 2016 11:38:03 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 2F2FA46214 for ; Mon, 21 Mar 2016 15:38:03 +0000 (UTC) References: <1456151945-11225-1-git-send-email-pbonzini@redhat.com> <1456151945-11225-3-git-send-email-pbonzini@redhat.com> <874mbz26uk.fsf@blackfin.pond.sub.org> From: Paolo Bonzini Message-ID: <56F01557.9090203@redhat.com> Date: Mon, 21 Mar 2016 16:37:59 +0100 MIME-Version: 1.0 In-Reply-To: <874mbz26uk.fsf@blackfin.pond.sub.org> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 2/3] block: keep BlockBackend alive until device finalize time List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz On 21/03/2016 16:22, Markus Armbruster wrote: > Paolo Bonzini writes: >=20 >> While the next patch will anticipate the death of the DriveInfo >> data structure, the BlockBackend must survive after unrealize, >> for example in case there are outstanding operations on it. >> The good thing is that we can just use reference counting to >> do it. >> >> Signed-off-by: Paolo Bonzini >> --- >> hw/core/qdev-properties-system.c | 19 ++++++++++++------- >> 1 file changed, 12 insertions(+), 7 deletions(-) >> >> diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-propertie= s-system.c >> index 469ba8a..5e84b55 100644 >> --- a/hw/core/qdev-properties-system.c >> +++ b/hw/core/qdev-properties-system.c >> @@ -93,6 +93,7 @@ static void parse_drive(DeviceState *dev, const char= *str, void **ptr, > if (blk_attach_dev(blk, dev) < 0) { > DriveInfo *dinfo =3D blk_legacy_dinfo(blk); >=20 > if (dinfo->type !=3D IF_NONE) { > error_setg(errp, "Drive '%s' is already in use because " > "it has been automatically connected to anoth= er " > "device (did you need 'if=3Dnone' in the driv= e options?)", > str); > } else { > error_setg(errp, "Drive '%s' is already in use by anothe= r device", > str); > } >> return; >> } >> *ptr =3D blk; >> + blk_ref(blk); >=20 > blk_attach_dev() already takes a reference. I'm not sure I understand > why you need to take a second one. You say "in case there are > outstanding operations on it." What operations could that be? There could be asynchronous I/O operations which are still active after unrealize. The device would not be finalized until they are completed. > And shouldn't they take their own reference? Generally the block layer doesn't try to ref/unref on every use. It assumes that someone else does it for you. A better justification for this patch is that blk_attach_dev/blk_detach_dev actually does not need to take a reference, so we can add it to parse_drive/release_drive and remove it from blk_attach_dev/blk_detach_dev instead. Paolo > I hasten to add that I'm not going to demand you fix them to take their > own references. It's okay to take a hacky second reference here, then > fix "them" at our leisure. But I need to understand what exactly this > second reference protects. It probably needs to be explained in the > source, too. >=20 >> } >> =20 >> static void release_drive(Object *obj, const char *name, void *opaque= ) >> @@ -101,13 +102,17 @@ static void release_drive(Object *obj, const cha= r *name, void *opaque) >> Property *prop =3D opaque; >> BlockBackend **ptr =3D qdev_get_prop_ptr(dev, prop); >> =20 >> - if (*ptr && blk_get_attached_dev(*ptr) !=3D NULL) { >> - /* Unrealize has already called blk_detach_dev and blockdev_d= el_drive >> - * if the device has been realized; in that case, blk_get_att= ached_dev >> - * returns NULL. Thus, we get here if the device failed to r= ealize, >> - * and the -drive must not be released. >> - */ >> - blk_detach_dev(*ptr, dev); >> + if (*ptr) { >> + if (blk_get_attached_dev(*ptr) !=3D NULL) { >> + /* Unrealize has already called blk_detach_dev and >> + * blockdev_del_drive if the device has been realized; >> + * in that case, blk_get_attached_dev returns NULL. Thus= , >> + * we get here if the device failed to realize, and the >> + * -drive must not be released. >> + */ >> + blk_detach_dev(*ptr, dev); >> + } >> + blk_unref(*ptr); >> } >> }