Commit 90e03207 authored by Asias He's avatar Asias He Committed by Rusty Russell

virtio: Use ida to allocate virtio index

Current index allocation in virtio is based on a monotonically
increasing variable "index". This means we'll run out of numbers
after a while. E.g. someone crazy doing this in host side.

while(1) {
	hot-plug a virtio device
	hot-unplug the virito devcie
}
Signed-off-by: default avatarAsias He <asias@redhat.com>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent c877bab5
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/virtio_config.h> #include <linux/virtio_config.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/idr.h>
/* Unique numbering for virtio devices. */ /* Unique numbering for virtio devices. */
static unsigned int dev_index; static DEFINE_IDA(virtio_index_ida);
static ssize_t device_show(struct device *_d, static ssize_t device_show(struct device *_d,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
...@@ -193,7 +194,11 @@ int register_virtio_device(struct virtio_device *dev) ...@@ -193,7 +194,11 @@ int register_virtio_device(struct virtio_device *dev)
dev->dev.bus = &virtio_bus; dev->dev.bus = &virtio_bus;
/* Assign a unique device index and hence name. */ /* Assign a unique device index and hence name. */
dev->index = dev_index++; err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
if (err < 0)
goto out;
dev->index = err;
dev_set_name(&dev->dev, "virtio%u", dev->index); dev_set_name(&dev->dev, "virtio%u", dev->index);
/* We always start by resetting the device, in case a previous /* We always start by resetting the device, in case a previous
...@@ -208,6 +213,7 @@ int register_virtio_device(struct virtio_device *dev) ...@@ -208,6 +213,7 @@ int register_virtio_device(struct virtio_device *dev)
/* device_register() causes the bus infrastructure to look for a /* device_register() causes the bus infrastructure to look for a
* matching driver. */ * matching driver. */
err = device_register(&dev->dev); err = device_register(&dev->dev);
out:
if (err) if (err)
add_status(dev, VIRTIO_CONFIG_S_FAILED); add_status(dev, VIRTIO_CONFIG_S_FAILED);
return err; return err;
...@@ -217,6 +223,7 @@ EXPORT_SYMBOL_GPL(register_virtio_device); ...@@ -217,6 +223,7 @@ EXPORT_SYMBOL_GPL(register_virtio_device);
void unregister_virtio_device(struct virtio_device *dev) void unregister_virtio_device(struct virtio_device *dev)
{ {
device_unregister(&dev->dev); device_unregister(&dev->dev);
ida_simple_remove(&virtio_index_ida, dev->index);
} }
EXPORT_SYMBOL_GPL(unregister_virtio_device); EXPORT_SYMBOL_GPL(unregister_virtio_device);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment