Commit 90e03207f468e84258270ad07095ef50f925c17d
Committed by
Rusty Russell
1 parent
c877bab507
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
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: Asias He <asias@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Showing 1 changed file with 9 additions and 2 deletions Side-by-side Diff
drivers/virtio/virtio.c
... | ... | @@ -2,9 +2,10 @@ |
2 | 2 | #include <linux/spinlock.h> |
3 | 3 | #include <linux/virtio_config.h> |
4 | 4 | #include <linux/module.h> |
5 | +#include <linux/idr.h> | |
5 | 6 | |
6 | 7 | /* Unique numbering for virtio devices. */ |
7 | -static unsigned int dev_index; | |
8 | +static DEFINE_IDA(virtio_index_ida); | |
8 | 9 | |
9 | 10 | static ssize_t device_show(struct device *_d, |
10 | 11 | struct device_attribute *attr, char *buf) |
... | ... | @@ -193,7 +194,11 @@ |
193 | 194 | dev->dev.bus = &virtio_bus; |
194 | 195 | |
195 | 196 | /* Assign a unique device index and hence name. */ |
196 | - dev->index = dev_index++; | |
197 | + err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL); | |
198 | + if (err < 0) | |
199 | + goto out; | |
200 | + | |
201 | + dev->index = err; | |
197 | 202 | dev_set_name(&dev->dev, "virtio%u", dev->index); |
198 | 203 | |
199 | 204 | /* We always start by resetting the device, in case a previous |
... | ... | @@ -208,6 +213,7 @@ |
208 | 213 | /* device_register() causes the bus infrastructure to look for a |
209 | 214 | * matching driver. */ |
210 | 215 | err = device_register(&dev->dev); |
216 | +out: | |
211 | 217 | if (err) |
212 | 218 | add_status(dev, VIRTIO_CONFIG_S_FAILED); |
213 | 219 | return err; |
... | ... | @@ -217,6 +223,7 @@ |
217 | 223 | void unregister_virtio_device(struct virtio_device *dev) |
218 | 224 | { |
219 | 225 | device_unregister(&dev->dev); |
226 | + ida_simple_remove(&virtio_index_ida, dev->index); | |
220 | 227 | } |
221 | 228 | EXPORT_SYMBOL_GPL(unregister_virtio_device); |
222 | 229 |