virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] virtio: release virtio index when fail to device_register
@ 2017-11-28 15:43 weiping zhang
  0 siblings, 0 replies; 8+ messages in thread
From: weiping zhang @ 2017-11-28 15:43 UTC (permalink / raw)
  To: mst, jasowang; +Cc: virtualization

index can be reused by other virtio device.

Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
---
 drivers/virtio/virtio.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 48230a5..bf7ff39 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -333,6 +333,8 @@ int register_virtio_device(struct virtio_device *dev)
 	/* device_register() causes the bus infrastructure to look for a
 	 * matching driver. */
 	err = device_register(&dev->dev);
+	if (err)
+		ida_simple_remove(&virtio_index_ida, dev->index);
 out:
 	if (err)
 		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
-- 
2.9.4

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH] virtio: release virtio index when fail to device_register
@ 2017-11-29  1:23 weiping zhang
  2017-11-29  9:50 ` Cornelia Huck
  2017-11-29 13:55 ` Michael S. Tsirkin
  0 siblings, 2 replies; 8+ messages in thread
From: weiping zhang @ 2017-11-29  1:23 UTC (permalink / raw)
  To: mst, jasowang; +Cc: virtualization

index can be reused by other virtio device.

Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
---
 drivers/virtio/virtio.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 48230a5..bf7ff39 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -333,6 +333,8 @@ int register_virtio_device(struct virtio_device *dev)
 	/* device_register() causes the bus infrastructure to look for a
 	 * matching driver. */
 	err = device_register(&dev->dev);
+	if (err)
+		ida_simple_remove(&virtio_index_ida, dev->index);
 out:
 	if (err)
 		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
-- 
2.9.4

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] virtio: release virtio index when fail to device_register
  2017-11-29  1:23 [PATCH] virtio: release virtio index when fail to device_register weiping zhang
@ 2017-11-29  9:50 ` Cornelia Huck
  2017-12-01 16:55   ` weiping zhang
  2017-11-29 13:55 ` Michael S. Tsirkin
  1 sibling, 1 reply; 8+ messages in thread
From: Cornelia Huck @ 2017-11-29  9:50 UTC (permalink / raw)
  To: weiping zhang; +Cc: virtualization, mst

On Wed, 29 Nov 2017 09:23:01 +0800
weiping zhang <zwp10758@gmail.com> wrote:

> index can be reused by other virtio device.
> 
> Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>

Reviewed-by: Cornelia Huck <cohuck@redhat.com>

> ---
>  drivers/virtio/virtio.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index 48230a5..bf7ff39 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -333,6 +333,8 @@ int register_virtio_device(struct virtio_device *dev)
>  	/* device_register() causes the bus infrastructure to look for a
>  	 * matching driver. */
>  	err = device_register(&dev->dev);
> +	if (err)
> +		ida_simple_remove(&virtio_index_ida, dev->index);
>  out:
>  	if (err)
>  		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);

I think your patch is correct (giving up the index if we failed to
register), but this made me look at our error handling if a virtio
device failed to register.

We hold an extra reference to the struct device, even after a failed
register, and it is the responsibility of the caller to give up that
reference once no longer needed. As callers toregister_virtio_device()
embed the struct virtio_device, it needs to be their responsibility.
Looking at the existing callers,

- ccw does a put_device
- pci, mmio and remoteproc do nothing, causing a leak
- vop does a free on the embedding structure, which is a big no-no

Thoughts?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] virtio: release virtio index when fail to device_register
  2017-11-29  1:23 [PATCH] virtio: release virtio index when fail to device_register weiping zhang
  2017-11-29  9:50 ` Cornelia Huck
@ 2017-11-29 13:55 ` Michael S. Tsirkin
  1 sibling, 0 replies; 8+ messages in thread
From: Michael S. Tsirkin @ 2017-11-29 13:55 UTC (permalink / raw)
  To: weiping zhang; +Cc: stable, virtualization

On Wed, Nov 29, 2017 at 09:23:01AM +0800, weiping zhang wrote:
> index can be reused by other virtio device.
> 
> Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>

Thanks!
I've queued this up, this is needed on stable as well.


> ---
>  drivers/virtio/virtio.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index 48230a5..bf7ff39 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -333,6 +333,8 @@ int register_virtio_device(struct virtio_device *dev)
>  	/* device_register() causes the bus infrastructure to look for a
>  	 * matching driver. */
>  	err = device_register(&dev->dev);
> +	if (err)
> +		ida_simple_remove(&virtio_index_ida, dev->index);
>  out:
>  	if (err)
>  		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
> -- 
> 2.9.4

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] virtio: release virtio index when fail to device_register
  2017-11-29  9:50 ` Cornelia Huck
@ 2017-12-01 16:55   ` weiping zhang
  2017-12-01 17:30     ` weiping zhang
  2017-12-04  9:38     ` Cornelia Huck
  0 siblings, 2 replies; 8+ messages in thread
From: weiping zhang @ 2017-12-01 16:55 UTC (permalink / raw)
  To: Cornelia Huck; +Cc: virtualization, mst

On Wed, Nov 29, 2017 at 10:50:44AM +0100, Cornelia Huck wrote:
> On Wed, 29 Nov 2017 09:23:01 +0800
> weiping zhang <zwp10758@gmail.com> wrote:
> 
> > index can be reused by other virtio device.
> > 
> > Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
> 
> Reviewed-by: Cornelia Huck <cohuck@redhat.com>
> 
> > ---
> >  drivers/virtio/virtio.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> > index 48230a5..bf7ff39 100644
> > --- a/drivers/virtio/virtio.c
> > +++ b/drivers/virtio/virtio.c
> > @@ -333,6 +333,8 @@ int register_virtio_device(struct virtio_device *dev)
> >  	/* device_register() causes the bus infrastructure to look for a
> >  	 * matching driver. */
> >  	err = device_register(&dev->dev);
> > +	if (err)
> > +		ida_simple_remove(&virtio_index_ida, dev->index);
> >  out:
> >  	if (err)
> >  		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
> 
> I think your patch is correct (giving up the index if we failed to
> register), but this made me look at our error handling if a virtio
> device failed to register.
> 
> We hold an extra reference to the struct device, even after a failed
> register, and it is the responsibility of the caller to give up that
> reference once no longer needed. As callers toregister_virtio_device()
> embed the struct virtio_device, it needs to be their responsibility.
> Looking at the existing callers,
> 
> - ccw does a put_device
> - pci, mmio and remoteproc do nothing, causing a leak
> - vop does a free on the embedding structure, which is a big no-no
> 
> Thoughts?
Sorry to relay late and thanks for your review.
Do you mean the "extra reference to the struct device" caused by the
following code?
 
err = device_register(&dev->dev);
	device_add(dev)
		get_device(dev)
If I'm understand right, I think there is no extra reference if we fail
virtio_register_device, because if device_register we don't get a
reference.

--
Thanks
weiping

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] virtio: release virtio index when fail to device_register
  2017-12-01 16:55   ` weiping zhang
@ 2017-12-01 17:30     ` weiping zhang
  2017-12-04  9:38     ` Cornelia Huck
  1 sibling, 0 replies; 8+ messages in thread
From: weiping zhang @ 2017-12-01 17:30 UTC (permalink / raw)
  To: Cornelia Huck; +Cc: virtualization, mst

2017-12-02 0:55 GMT+08:00 weiping zhang <zwp10758@gmail.com>:
> On Wed, Nov 29, 2017 at 10:50:44AM +0100, Cornelia Huck wrote:
>> On Wed, 29 Nov 2017 09:23:01 +0800
>> weiping zhang <zwp10758@gmail.com> wrote:
>>
>> > index can be reused by other virtio device.
>> >
>> > Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
>>
>> Reviewed-by: Cornelia Huck <cohuck@redhat.com>
>>
>> > ---
>> >  drivers/virtio/virtio.c | 2 ++
>> >  1 file changed, 2 insertions(+)
>> >
>> > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
>> > index 48230a5..bf7ff39 100644
>> > --- a/drivers/virtio/virtio.c
>> > +++ b/drivers/virtio/virtio.c
>> > @@ -333,6 +333,8 @@ int register_virtio_device(struct virtio_device *dev)
>> >     /* device_register() causes the bus infrastructure to look for a
>> >      * matching driver. */
>> >     err = device_register(&dev->dev);
>> > +   if (err)
>> > +           ida_simple_remove(&virtio_index_ida, dev->index);
>> >  out:
>> >     if (err)
>> >             virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
>>
>> I think your patch is correct (giving up the index if we failed to
>> register), but this made me look at our error handling if a virtio
>> device failed to register.
>>
>> We hold an extra reference to the struct device, even after a failed
>> register, and it is the responsibility of the caller to give up that
>> reference once no longer needed. As callers toregister_virtio_device()
>> embed the struct virtio_device, it needs to be their responsibility.
>> Looking at the existing callers,
>>
>> - ccw does a put_device
>> - pci, mmio and remoteproc do nothing, causing a leak
>> - vop does a free on the embedding structure, which is a big no-no
>>
>> Thoughts?
> Sorry to relay late and thanks for your review.
> Do you mean the "extra reference to the struct device" caused by the
> following code?
>
> err = device_register(&dev->dev);
>         device_add(dev)
>                 get_device(dev)
> If I'm understand right, I think there is no extra reference if we fail
> virtio_register_device, because if device_register we don't get a
> reference.

I go through these callers,  virtio_mmio_probe should do more cleanup.
I'll sent a patch fix that.

> --
> Thanks
> weiping

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] virtio: release virtio index when fail to device_register
  2017-12-01 16:55   ` weiping zhang
  2017-12-01 17:30     ` weiping zhang
@ 2017-12-04  9:38     ` Cornelia Huck
  2017-12-05  1:30       ` weiping zhang
  1 sibling, 1 reply; 8+ messages in thread
From: Cornelia Huck @ 2017-12-04  9:38 UTC (permalink / raw)
  To: weiping zhang; +Cc: virtualization, mst

On Sat, 2 Dec 2017 00:55:39 +0800
weiping zhang <zwp10758@gmail.com> wrote:

> On Wed, Nov 29, 2017 at 10:50:44AM +0100, Cornelia Huck wrote:

> > We hold an extra reference to the struct device, even after a failed
> > register, and it is the responsibility of the caller to give up that
> > reference once no longer needed. As callers toregister_virtio_device()
> > embed the struct virtio_device, it needs to be their responsibility.
> > Looking at the existing callers,
> > 
> > - ccw does a put_device
> > - pci, mmio and remoteproc do nothing, causing a leak
> > - vop does a free on the embedding structure, which is a big no-no
> > 
> > Thoughts?  
> Sorry to relay late and thanks for your review.
> Do you mean the "extra reference to the struct device" caused by the
> following code?
>  
> err = device_register(&dev->dev);
> 	device_add(dev)
> 		get_device(dev)
> If I'm understand right, I think there is no extra reference if we fail
> virtio_register_device, because if device_register we don't get a
> reference.

The device_initialize() already gives you a reference. If device_add()
fails, it has cleaned up any additional reference it might have
obtained, but the initial reference is still there and needs to be
released by the caller.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] virtio: release virtio index when fail to device_register
  2017-12-04  9:38     ` Cornelia Huck
@ 2017-12-05  1:30       ` weiping zhang
  0 siblings, 0 replies; 8+ messages in thread
From: weiping zhang @ 2017-12-05  1:30 UTC (permalink / raw)
  To: Cornelia Huck; +Cc: virtualization, mst

2017-12-04 17:38 GMT+08:00 Cornelia Huck <cohuck@redhat.com>:
> On Sat, 2 Dec 2017 00:55:39 +0800
> weiping zhang <zwp10758@gmail.com> wrote:
>
>> On Wed, Nov 29, 2017 at 10:50:44AM +0100, Cornelia Huck wrote:
>
>> > We hold an extra reference to the struct device, even after a failed
>> > register, and it is the responsibility of the caller to give up that
>> > reference once no longer needed. As callers toregister_virtio_device()
>> > embed the struct virtio_device, it needs to be their responsibility.
>> > Looking at the existing callers,
>> >
>> > - ccw does a put_device
>> > - pci, mmio and remoteproc do nothing, causing a leak
>> > - vop does a free on the embedding structure, which is a big no-no
>> >
>> > Thoughts?
>> Sorry to relay late and thanks for your review.
>> Do you mean the "extra reference to the struct device" caused by the
>> following code?
>>
>> err = device_register(&dev->dev);
>>       device_add(dev)
>>               get_device(dev)
>> If I'm understand right, I think there is no extra reference if we fail
>> virtio_register_device, because if device_register we don't get a
>> reference.
>
> The device_initialize() already gives you a reference. If device_add()
> fails, it has cleaned up any additional reference it might have
> obtained, but the initial reference is still there and needs to be
> released by the caller.

Thanks your clarify, I also notice the comments at device_register,
device_initialize, device_add,

 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up the
 * reference initialized in this function instead.

--
Thanks
weiping

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-12-05  1:30 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-29  1:23 [PATCH] virtio: release virtio index when fail to device_register weiping zhang
2017-11-29  9:50 ` Cornelia Huck
2017-12-01 16:55   ` weiping zhang
2017-12-01 17:30     ` weiping zhang
2017-12-04  9:38     ` Cornelia Huck
2017-12-05  1:30       ` weiping zhang
2017-11-29 13:55 ` Michael S. Tsirkin
  -- strict thread matches above, loose matches on Subject: below --
2017-11-28 15:43 weiping zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).