Skip to content

Commit ebf7155

Browse files
davidhildenbrandmstsirkin
authored andcommitted
virtio-mem: Add parent resource for all added "System RAM"
Let's add a parent resource, named after the virtio device (inspired by drivers/dax/kmem.c). This allows user space to identify which memory belongs to which virtio-mem device. With this change and two virtio-mem devices: :/# cat /proc/iomem 00000000-00000fff : Reserved 00001000-0009fbff : System RAM [...] 140000000-333ffffff : virtio0 140000000-147ffffff : System RAM 148000000-14fffffff : System RAM 150000000-157ffffff : System RAM [...] 334000000-3033ffffff : virtio1 338000000-33fffffff : System RAM 340000000-347ffffff : System RAM 348000000-34fffffff : System RAM [...] Cc: "Michael S. Tsirkin" <[email protected]> Cc: Pankaj Gupta <[email protected]> Signed-off-by: David Hildenbrand <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]> Reviewed-by: Pankaj Gupta <[email protected]>
1 parent 23e77b5 commit ebf7155

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

drivers/virtio/virtio_mem.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ struct virtio_mem {
9999
/* Id of the next memory bock to prepare when needed. */
100100
unsigned long next_mb_id;
101101

102+
/* The parent resource for all memory added via this device. */
103+
struct resource *parent_resource;
104+
102105
/* Summary of all memory block states. */
103106
unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT];
104107
#define VIRTIO_MEM_NB_OFFLINE_THRESHOLD 10
@@ -1741,6 +1744,44 @@ static int virtio_mem_init(struct virtio_mem *vm)
17411744
return 0;
17421745
}
17431746

1747+
static int virtio_mem_create_resource(struct virtio_mem *vm)
1748+
{
1749+
/*
1750+
* When force-unloading the driver and removing the device, we
1751+
* could have a garbage pointer. Duplicate the string.
1752+
*/
1753+
const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
1754+
1755+
if (!name)
1756+
return -ENOMEM;
1757+
1758+
vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
1759+
name, IORESOURCE_SYSTEM_RAM);
1760+
if (!vm->parent_resource) {
1761+
kfree(name);
1762+
dev_warn(&vm->vdev->dev, "could not reserve device region\n");
1763+
return -EBUSY;
1764+
}
1765+
1766+
/* The memory is not actually busy - make add_memory() work. */
1767+
vm->parent_resource->flags &= ~IORESOURCE_BUSY;
1768+
return 0;
1769+
}
1770+
1771+
static void virtio_mem_delete_resource(struct virtio_mem *vm)
1772+
{
1773+
const char *name;
1774+
1775+
if (!vm->parent_resource)
1776+
return;
1777+
1778+
name = vm->parent_resource->name;
1779+
release_resource(vm->parent_resource);
1780+
kfree(vm->parent_resource);
1781+
kfree(name);
1782+
vm->parent_resource = NULL;
1783+
}
1784+
17441785
static int virtio_mem_probe(struct virtio_device *vdev)
17451786
{
17461787
struct virtio_mem *vm;
@@ -1770,11 +1811,16 @@ static int virtio_mem_probe(struct virtio_device *vdev)
17701811
if (rc)
17711812
goto out_del_vq;
17721813

1814+
/* create the parent resource for all memory */
1815+
rc = virtio_mem_create_resource(vm);
1816+
if (rc)
1817+
goto out_del_vq;
1818+
17731819
/* register callbacks */
17741820
vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
17751821
rc = register_memory_notifier(&vm->memory_notifier);
17761822
if (rc)
1777-
goto out_del_vq;
1823+
goto out_del_resource;
17781824
rc = register_virtio_mem_device(vm);
17791825
if (rc)
17801826
goto out_unreg_mem;
@@ -1788,6 +1834,8 @@ static int virtio_mem_probe(struct virtio_device *vdev)
17881834
return 0;
17891835
out_unreg_mem:
17901836
unregister_memory_notifier(&vm->memory_notifier);
1837+
out_del_resource:
1838+
virtio_mem_delete_resource(vm);
17911839
out_del_vq:
17921840
vdev->config->del_vqs(vdev);
17931841
out_free_vm:
@@ -1848,6 +1896,8 @@ static void virtio_mem_remove(struct virtio_device *vdev)
18481896
vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] ||
18491897
vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE])
18501898
dev_warn(&vdev->dev, "device still has system memory added\n");
1899+
else
1900+
virtio_mem_delete_resource(vm);
18511901

18521902
/* remove all tracking data - no locking needed */
18531903
vfree(vm->mb_state);

0 commit comments

Comments
 (0)