* [PATCH] virtio: Don't access index after unregister.
@ 2012-11-08 10:43 Cornelia Huck
2012-11-08 11:50 ` Sjur Brændeland
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Cornelia Huck @ 2012-11-08 10:43 UTC (permalink / raw)
To: Rusty Russell, Michael S. Tsirkin; +Cc: linux-kernel, virtualization
Virtio wants to release used indices after the corresponding
virtio device has been unregistered. However, virtio does not
hold an extra reference, giving up its last reference with
device_unregister(), making accessing dev->index afterwards
invalid.
I actually saw problems when testing my (not-yet-merged)
virtio-ccw code:
- device_add virtio-net,id=xxx
-> creates device virtio<n> with n>0
- device_del xxx
-> deletes virtio<n>, but calls ida_simple_remove with an
index of 0
- device_add virtio-net,id=xxx
-> tries to add virtio0, which is still in use...
So let's save the index we want to release before calling
device_unregister().
Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
---
drivers/virtio/virtio.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 1e8659c..809b0de 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -225,8 +225,10 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
void unregister_virtio_device(struct virtio_device *dev)
{
+ int index = dev->index; /* save for after device release */
+
device_unregister(&dev->dev);
- ida_simple_remove(&virtio_index_ida, dev->index);
+ ida_simple_remove(&virtio_index_ida, index);
}
EXPORT_SYMBOL_GPL(unregister_virtio_device);
--
1.7.12.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] virtio: Don't access index after unregister.
2012-11-08 10:43 [PATCH] virtio: Don't access index after unregister Cornelia Huck
@ 2012-11-08 11:50 ` Sjur Brændeland
2012-11-09 4:24 ` Rusty Russell
2012-11-09 5:14 ` Michael S. Tsirkin
2 siblings, 0 replies; 4+ messages in thread
From: Sjur Brændeland @ 2012-11-08 11:50 UTC (permalink / raw)
To: Cornelia Huck; +Cc: virtualization, linux-kernel, Michael S. Tsirkin
On Thu, Nov 8, 2012 at 11:43 AM, Cornelia Huck <cornelia.huck@de.ibm.com> wrote:
> Virtio wants to release used indices after the corresponding
> virtio device has been unregistered. However, virtio does not
> hold an extra reference, giving up its last reference with
> device_unregister(), making accessing dev->index afterwards
> invalid.
>
> I actually saw problems when testing my (not-yet-merged)
> virtio-ccw code:
>
> - device_add virtio-net,id=xxx
> -> creates device virtio<n> with n>0
>
> - device_del xxx
> -> deletes virtio<n>, but calls ida_simple_remove with an
> index of 0
>
> - device_add virtio-net,id=xxx
> -> tries to add virtio0, which is still in use...
>
> So let's save the index we want to release before calling
> device_unregister().
>
> Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
> ---
> drivers/virtio/virtio.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index 1e8659c..809b0de 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -225,8 +225,10 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
>
> void unregister_virtio_device(struct virtio_device *dev)
> {
> + int index = dev->index; /* save for after device release */
> +
> device_unregister(&dev->dev);
> - ida_simple_remove(&virtio_index_ida, dev->index);
> + ida_simple_remove(&virtio_index_ida, index);
> }
> EXPORT_SYMBOL_GPL(unregister_virtio_device);
Acked-by: Sjur Brændeland <sjur.brandeland@stericsson.com>
Great minds think alike! I discovered issues with this implementation
a while back and Michael suggested an identical patch:
https://lkml.org/lkml/2012/9/4/173
https://lkml.org/lkml/2012/9/7/105
The issue I ran into was that when virtio devices are created by remoteproc
the device memory might be freed when calling device_unregister(), and
the value of dev->index is then undefined. So this bug bites when
unregistering a Virtio devices from remoteproc with CONFIG_DEBUG_SLAB
enabled. However this bug is not triggered by virtio_pci as it implements a
non-standard device release-function that does not free the device memory.
Thanks,
Sjur
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] virtio: Don't access index after unregister.
2012-11-08 10:43 [PATCH] virtio: Don't access index after unregister Cornelia Huck
2012-11-08 11:50 ` Sjur Brændeland
@ 2012-11-09 4:24 ` Rusty Russell
2012-11-09 5:14 ` Michael S. Tsirkin
2 siblings, 0 replies; 4+ messages in thread
From: Rusty Russell @ 2012-11-09 4:24 UTC (permalink / raw)
To: Cornelia Huck, Michael S. Tsirkin; +Cc: linux-kernel, virtualization
Cornelia Huck <cornelia.huck@de.ibm.com> writes:
> Virtio wants to release used indices after the corresponding
> virtio device has been unregistered. However, virtio does not
> hold an extra reference, giving up its last reference with
> device_unregister(), making accessing dev->index afterwards
> invalid.
>
> I actually saw problems when testing my (not-yet-merged)
> virtio-ccw code:
>
> - device_add virtio-net,id=xxx
> -> creates device virtio<n> with n>0
>
> - device_del xxx
> -> deletes virtio<n>, but calls ida_simple_remove with an
> index of 0
>
> - device_add virtio-net,id=xxx
> -> tries to add virtio0, which is still in use...
>
> So let's save the index we want to release before calling
> device_unregister().
Great catch! I've add a CC:stable.
Applied,
Rusty.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] virtio: Don't access index after unregister.
2012-11-08 10:43 [PATCH] virtio: Don't access index after unregister Cornelia Huck
2012-11-08 11:50 ` Sjur Brændeland
2012-11-09 4:24 ` Rusty Russell
@ 2012-11-09 5:14 ` Michael S. Tsirkin
2 siblings, 0 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2012-11-09 5:14 UTC (permalink / raw)
To: Cornelia Huck; +Cc: linux-kernel, virtualization
On Thu, Nov 08, 2012 at 11:43:47AM +0100, Cornelia Huck wrote:
> Virtio wants to release used indices after the corresponding
> virtio device has been unregistered. However, virtio does not
> hold an extra reference, giving up its last reference with
> device_unregister(), making accessing dev->index afterwards
> invalid.
>
> I actually saw problems when testing my (not-yet-merged)
> virtio-ccw code:
>
> - device_add virtio-net,id=xxx
> -> creates device virtio<n> with n>0
>
> - device_del xxx
> -> deletes virtio<n>, but calls ida_simple_remove with an
> index of 0
>
> - device_add virtio-net,id=xxx
> -> tries to add virtio0, which is still in use...
>
> So let's save the index we want to release before calling
> device_unregister().
>
> Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
> ---
> drivers/virtio/virtio.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index 1e8659c..809b0de 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -225,8 +225,10 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
>
> void unregister_virtio_device(struct virtio_device *dev)
> {
> + int index = dev->index; /* save for after device release */
It's obvious from code that we safe for after release,
I think a better comment would explain *why* we do this.
Something like
/*
device_unregister drops reference to device so put_device could
invoke release callback. In case that callback will free the device,
make sure we don't access device after this call.
*/
int index = dev->index;
?
> +
> device_unregister(&dev->dev);
> - ida_simple_remove(&virtio_index_ida, dev->index);
> + ida_simple_remove(&virtio_index_ida, index);
> }
> EXPORT_SYMBOL_GPL(unregister_virtio_device);
>
> --
> 1.7.12.4
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-11-09 5:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-08 10:43 [PATCH] virtio: Don't access index after unregister Cornelia Huck
2012-11-08 11:50 ` Sjur Brændeland
2012-11-09 4:24 ` Rusty Russell
2012-11-09 5:14 ` Michael S. Tsirkin
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).