* [PATCH] virtio: decrement dev_index when device is unregistered
[not found] <1457566934.641329.1301903526622.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
@ 2011-04-05 4:49 ` Takuma Umeya
2011-04-05 6:21 ` Stefan Hajnoczi
0 siblings, 1 reply; 4+ messages in thread
From: Takuma Umeya @ 2011-04-05 4:49 UTC (permalink / raw)
To: virtualization; +Cc: Stephen Hemminger, Rusty Russell, linux-kernel
When virtio device is removed, dev_index does not get decremented.
The next device hotplug event results in consuming the next pci to
the one that is suppose to be available.
Signed-off-by: Takuma Umeya <tumeya@redhat.com>
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index efb35aa..67fe71d 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -216,6 +216,7 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
void unregister_virtio_device(struct virtio_device *dev)
{
device_unregister(&dev->dev);
+ dev_index--;
}
EXPORT_SYMBOL_GPL(unregister_virtio_device);
--
Umeya, Takuma
Technical Account Manager
Red Hat GSS APAC
+81.3.5798.8584 (direct)
tumeya@redhat.com
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] virtio: decrement dev_index when device is unregistered
2011-04-05 4:49 ` [PATCH] virtio: decrement dev_index when device is unregistered Takuma Umeya
@ 2011-04-05 6:21 ` Stefan Hajnoczi
2011-04-11 9:11 ` Takuma Umeya
0 siblings, 1 reply; 4+ messages in thread
From: Stefan Hajnoczi @ 2011-04-05 6:21 UTC (permalink / raw)
To: Takuma Umeya; +Cc: virtualization, Stephen Hemminger, linux-kernel
On Tue, Apr 5, 2011 at 5:49 AM, Takuma Umeya <tumeya@redhat.com> wrote:
> When virtio device is removed, dev_index does not get decremented.
> The next device hotplug event results in consuming the next pci to
> the one that is suppose to be available.
>
> Signed-off-by: Takuma Umeya <tumeya@redhat.com>
>
> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> index efb35aa..67fe71d 100644
> --- a/drivers/virtio/virtio.c
> +++ b/drivers/virtio/virtio.c
> @@ -216,6 +216,7 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
> void unregister_virtio_device(struct virtio_device *dev)
> {
> device_unregister(&dev->dev);
> + dev_index--;
I don't think there is any guarantee that virtio devices are
added/removed in first-in-last-out order.
That means I could add a virtio-net device (index 0) followed by a
virtio-blk device (index 1). Now I remove the virtio-net device
(index 0) which causes me to decrement dev_index and hand index 1 out
again to the next device. This leaves us with virtio-blk (index 1)
and the new device with index 1, which is not unique.
Perhaps I missed a constraint which prevents this from occurring?
Stefan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] virtio: decrement dev_index when device is unregistered
2011-04-05 6:21 ` Stefan Hajnoczi
@ 2011-04-11 9:11 ` Takuma Umeya
2011-04-12 9:09 ` Stefan Hajnoczi
0 siblings, 1 reply; 4+ messages in thread
From: Takuma Umeya @ 2011-04-11 9:11 UTC (permalink / raw)
To: Stefan Hajnoczi; +Cc: virtualization, Stephen Hemminger, linux-kernel
----- Original Message -----
> On Tue, Apr 5, 2011 at 5:49 AM, Takuma Umeya <tumeya@redhat.com>
> wrote:
> > When virtio device is removed, dev_index does not get decremented.
> > The next device hotplug event results in consuming the next pci to
> > the one that is suppose to be available.
> >
> > Signed-off-by: Takuma Umeya <tumeya@redhat.com>
> >
> > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
> > index efb35aa..67fe71d 100644
> > --- a/drivers/virtio/virtio.c
> > +++ b/drivers/virtio/virtio.c
> > @@ -216,6 +216,7 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
> > void unregister_virtio_device(struct virtio_device *dev)
> > {
> > device_unregister(&dev->dev);
> > + dev_index--;
>
> I don't think there is any guarantee that virtio devices are
> added/removed in first-in-last-out order.
>
> That means I could add a virtio-net device (index 0) followed by a
> virtio-blk device (index 1). Now I remove the virtio-net device
> (index 0) which causes me to decrement dev_index and hand index 1 out
> again to the next device. This leaves us with virtio-blk (index 1)
> and the new device with index 1, which is not unique.
>
> Perhaps I missed a constraint which prevents this from occurring?
I believe the address is assigned up to 1f so using u32 value
to track use/free. This should make the code immune to the scenario.
Would this be adequate?
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index efb35aa..0c73507 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -3,7 +3,7 @@
#include <linux/virtio_config.h>
/* Unique numbering for virtio devices. */
-static unsigned int dev_index;
+static u32 dev_index;
static ssize_t device_show(struct device *_d,
struct device_attribute *attr, char *buf)
@@ -187,12 +187,23 @@ EXPORT_SYMBOL_GPL(unregister_virtio_driver);
int register_virtio_device(struct virtio_device *dev)
{
- int err;
+ int err, count;
+ u32 testbit;
+
+ count = 0;
dev->dev.bus = &virtio_bus;
/* Assign a unique device index and hence name. */
- dev->index = dev_index++;
+ while (count < 32){
+ testbit = 1UL << count;
+ if(!(dev_index & testbit)){
+ dev->index = count;
+ dev_index |= testbit;
+ break;
+ }
+ count++;
+ }
dev_set_name(&dev->dev, "virtio%u", dev->index);
/* We always start by resetting the device, in case a previous
@@ -215,7 +226,13 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
void unregister_virtio_device(struct virtio_device *dev)
{
+ u32 removebit;
+
+ removebit = 1UL << (dev->index);
+
device_unregister(&dev->dev);
+
+ dev_index ^= removebit;
}
EXPORT_SYMBOL_GPL(unregister_virtio_device);
>
> Stefan
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] virtio: decrement dev_index when device is unregistered
2011-04-11 9:11 ` Takuma Umeya
@ 2011-04-12 9:09 ` Stefan Hajnoczi
0 siblings, 0 replies; 4+ messages in thread
From: Stefan Hajnoczi @ 2011-04-12 9:09 UTC (permalink / raw)
To: Takuma Umeya; +Cc: virtualization, Stephen Hemminger, linux-kernel
On Mon, Apr 11, 2011 at 10:11 AM, Takuma Umeya <tumeya@redhat.com> wrote:
> ----- Original Message -----
>> On Tue, Apr 5, 2011 at 5:49 AM, Takuma Umeya <tumeya@redhat.com>
>> wrote:
>> > When virtio device is removed, dev_index does not get decremented.
>> > The next device hotplug event results in consuming the next pci to
>> > the one that is suppose to be available.
>> >
>> > Signed-off-by: Takuma Umeya <tumeya@redhat.com>
>> >
>> > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
>> > index efb35aa..67fe71d 100644
>> > --- a/drivers/virtio/virtio.c
>> > +++ b/drivers/virtio/virtio.c
>> > @@ -216,6 +216,7 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
>> > void unregister_virtio_device(struct virtio_device *dev)
>> > {
>> > device_unregister(&dev->dev);
>> > + dev_index--;
>>
>> I don't think there is any guarantee that virtio devices are
>> added/removed in first-in-last-out order.
>>
>> That means I could add a virtio-net device (index 0) followed by a
>> virtio-blk device (index 1). Now I remove the virtio-net device
>> (index 0) which causes me to decrement dev_index and hand index 1 out
>> again to the next device. This leaves us with virtio-blk (index 1)
>> and the new device with index 1, which is not unique.
>>
>> Perhaps I missed a constraint which prevents this from occurring?
> I believe the address is assigned up to 1f so using u32 value
> to track use/free. This should make the code immune to the scenario.
> Would this be adequate?
This issue was also brought up by Jens Axboe on your other patch for
virtio block devices. He suggested using idr. I think that would be
a nicer solution than a u32 bitfield.
I'm not sure where you got 0x1f from but that seems like an artifical
limitation. Nothing should stop us from having more virtio devices.
Stefan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-04-12 9:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <1457566934.641329.1301903526622.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com>
2011-04-05 4:49 ` [PATCH] virtio: decrement dev_index when device is unregistered Takuma Umeya
2011-04-05 6:21 ` Stefan Hajnoczi
2011-04-11 9:11 ` Takuma Umeya
2011-04-12 9:09 ` Stefan Hajnoczi
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).