All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org, virtio-dev@lists.oasis-open.org,
	virtualization@lists.linux-foundation.org, kvm@vger.kernel.org,
	Michal Hocko <mhocko@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	David Hildenbrand <david@redhat.com>,
	Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Subject: [PATCH v3 11/15] virtio-mem: Add parent resource for all added "System RAM"
Date: Thu,  7 May 2020 12:31:15 +0200	[thread overview]
Message-ID: <20200507103119.11219-12-david@redhat.com> (raw)
In-Reply-To: <20200507103119.11219-1-david@redhat.com>

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" <mst@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 drivers/virtio/virtio_mem.c | 52 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index eb4c16d634e0..80cdb9e6b3c4 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -99,6 +99,9 @@ struct virtio_mem {
 	/* Id of the next memory bock to prepare when needed. */
 	unsigned long next_mb_id;
 
+	/* The parent resource for all memory added via this device. */
+	struct resource *parent_resource;
+
 	/* Summary of all memory block states. */
 	unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT];
 #define VIRTIO_MEM_NB_OFFLINE_THRESHOLD		10
@@ -1741,6 +1744,44 @@ static int virtio_mem_init(struct virtio_mem *vm)
 	return 0;
 }
 
+static int virtio_mem_create_resource(struct virtio_mem *vm)
+{
+	/*
+	 * When force-unloading the driver and removing the device, we
+	 * could have a garbage pointer. Duplicate the string.
+	 */
+	const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
+
+	if (!name)
+		return -ENOMEM;
+
+	vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
+						   name, IORESOURCE_SYSTEM_RAM);
+	if (!vm->parent_resource) {
+		kfree(name);
+		dev_warn(&vm->vdev->dev, "could not reserve device region\n");
+		return -EBUSY;
+	}
+
+	/* The memory is not actually busy - make add_memory() work. */
+	vm->parent_resource->flags &= ~IORESOURCE_BUSY;
+	return 0;
+}
+
+static void virtio_mem_delete_resource(struct virtio_mem *vm)
+{
+	const char *name;
+
+	if (!vm->parent_resource)
+		return;
+
+	name = vm->parent_resource->name;
+	release_resource(vm->parent_resource);
+	kfree(vm->parent_resource);
+	kfree(name);
+	vm->parent_resource = NULL;
+}
+
 static int virtio_mem_probe(struct virtio_device *vdev)
 {
 	struct virtio_mem *vm;
@@ -1770,11 +1811,16 @@ static int virtio_mem_probe(struct virtio_device *vdev)
 	if (rc)
 		goto out_del_vq;
 
+	/* create the parent resource for all memory */
+	rc = virtio_mem_create_resource(vm);
+	if (rc)
+		goto out_del_vq;
+
 	/* register callbacks */
 	vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
 	rc = register_memory_notifier(&vm->memory_notifier);
 	if (rc)
-		goto out_del_vq;
+		goto out_del_resource;
 	rc = register_virtio_mem_device(vm);
 	if (rc)
 		goto out_unreg_mem;
@@ -1788,6 +1834,8 @@ static int virtio_mem_probe(struct virtio_device *vdev)
 	return 0;
 out_unreg_mem:
 	unregister_memory_notifier(&vm->memory_notifier);
+out_del_resource:
+	virtio_mem_delete_resource(vm);
 out_del_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vm:
@@ -1848,6 +1896,8 @@ static void virtio_mem_remove(struct virtio_device *vdev)
 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] ||
 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE])
 		dev_warn(&vdev->dev, "device still has system memory added\n");
+	else
+		virtio_mem_delete_resource(vm);
 
 	/* remove all tracking data - no locking needed */
 	vfree(vm->mb_state);
-- 
2.25.3


WARNING: multiple messages have this Message-ID (diff)
From: David Hildenbrand <david@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org, virtio-dev@lists.oasis-open.org,
	virtualization@lists.linux-foundation.org, kvm@vger.kernel.org,
	Michal Hocko <mhocko@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	David Hildenbrand <david@redhat.com>,
	Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Subject: [virtio-dev] [PATCH v3 11/15] virtio-mem: Add parent resource for all added "System RAM"
Date: Thu,  7 May 2020 12:31:15 +0200	[thread overview]
Message-ID: <20200507103119.11219-12-david@redhat.com> (raw)
In-Reply-To: <20200507103119.11219-1-david@redhat.com>

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" <mst@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 drivers/virtio/virtio_mem.c | 52 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index eb4c16d634e0..80cdb9e6b3c4 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -99,6 +99,9 @@ struct virtio_mem {
 	/* Id of the next memory bock to prepare when needed. */
 	unsigned long next_mb_id;
 
+	/* The parent resource for all memory added via this device. */
+	struct resource *parent_resource;
+
 	/* Summary of all memory block states. */
 	unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT];
 #define VIRTIO_MEM_NB_OFFLINE_THRESHOLD		10
@@ -1741,6 +1744,44 @@ static int virtio_mem_init(struct virtio_mem *vm)
 	return 0;
 }
 
+static int virtio_mem_create_resource(struct virtio_mem *vm)
+{
+	/*
+	 * When force-unloading the driver and removing the device, we
+	 * could have a garbage pointer. Duplicate the string.
+	 */
+	const char *name = kstrdup(dev_name(&vm->vdev->dev), GFP_KERNEL);
+
+	if (!name)
+		return -ENOMEM;
+
+	vm->parent_resource = __request_mem_region(vm->addr, vm->region_size,
+						   name, IORESOURCE_SYSTEM_RAM);
+	if (!vm->parent_resource) {
+		kfree(name);
+		dev_warn(&vm->vdev->dev, "could not reserve device region\n");
+		return -EBUSY;
+	}
+
+	/* The memory is not actually busy - make add_memory() work. */
+	vm->parent_resource->flags &= ~IORESOURCE_BUSY;
+	return 0;
+}
+
+static void virtio_mem_delete_resource(struct virtio_mem *vm)
+{
+	const char *name;
+
+	if (!vm->parent_resource)
+		return;
+
+	name = vm->parent_resource->name;
+	release_resource(vm->parent_resource);
+	kfree(vm->parent_resource);
+	kfree(name);
+	vm->parent_resource = NULL;
+}
+
 static int virtio_mem_probe(struct virtio_device *vdev)
 {
 	struct virtio_mem *vm;
@@ -1770,11 +1811,16 @@ static int virtio_mem_probe(struct virtio_device *vdev)
 	if (rc)
 		goto out_del_vq;
 
+	/* create the parent resource for all memory */
+	rc = virtio_mem_create_resource(vm);
+	if (rc)
+		goto out_del_vq;
+
 	/* register callbacks */
 	vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb;
 	rc = register_memory_notifier(&vm->memory_notifier);
 	if (rc)
-		goto out_del_vq;
+		goto out_del_resource;
 	rc = register_virtio_mem_device(vm);
 	if (rc)
 		goto out_unreg_mem;
@@ -1788,6 +1834,8 @@ static int virtio_mem_probe(struct virtio_device *vdev)
 	return 0;
 out_unreg_mem:
 	unregister_memory_notifier(&vm->memory_notifier);
+out_del_resource:
+	virtio_mem_delete_resource(vm);
 out_del_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vm:
@@ -1848,6 +1896,8 @@ static void virtio_mem_remove(struct virtio_device *vdev)
 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] ||
 	    vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE])
 		dev_warn(&vdev->dev, "device still has system memory added\n");
+	else
+		virtio_mem_delete_resource(vm);
 
 	/* remove all tracking data - no locking needed */
 	vfree(vm->mb_state);
-- 
2.25.3


---------------------------------------------------------------------
To unsubscribe, e-mail: virtio-dev-unsubscribe@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-help@lists.oasis-open.org


  parent reply	other threads:[~2020-05-07 10:33 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-07 10:31 [PATCH v3 00/15] virtio-mem: paravirtualized memory David Hildenbrand
2020-05-07 10:31 ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31 ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 01/15] virtio-mem: Paravirtualized memory hotplug David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 02/15] virtio-mem: Allow to specify an ACPI PXM as nid David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 03/15] virtio-mem: Paravirtualized memory hotunplug part 1 David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 04/15] virtio-mem: Paravirtualized memory hotunplug part 2 David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 05/15] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:48   ` Michael S. Tsirkin
2020-05-07 10:48     ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 10:48     ` Michael S. Tsirkin
2020-05-07 10:31 ` [PATCH v3 06/15] virtio-mem: Allow to offline partially unplugged memory blocks David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 07/15] mm/memory_hotplug: Introduce offline_and_remove_memory() David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:46   ` Michael S. Tsirkin
2020-05-07 10:46     ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 11:24     ` David Hildenbrand
2020-05-07 11:24       ` [virtio-dev] " David Hildenbrand
2020-05-07 11:33       ` David Hildenbrand
2020-05-07 11:33         ` [virtio-dev] " David Hildenbrand
2020-05-07 11:34         ` Michael S. Tsirkin
2020-05-07 11:34           ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 11:37           ` David Hildenbrand
2020-05-07 11:37             ` [virtio-dev] " David Hildenbrand
2020-05-07 12:11             ` Michael S. Tsirkin
2020-05-07 12:11               ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 12:24               ` David Hildenbrand
2020-05-07 12:24                 ` [virtio-dev] " David Hildenbrand
2020-05-07 11:33       ` Michael S. Tsirkin
2020-05-07 11:33         ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 10:31 ` [PATCH v3 08/15] virtio-mem: Offline and remove completely unplugged memory blocks David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 09/15] virtio-mem: Better retry handling David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 10/15] MAINTAINERS: Add myself as virtio-mem maintainer David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:47   ` Michael S. Tsirkin
2020-05-07 10:47     ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 11:25     ` David Hildenbrand
2020-05-07 11:25       ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31 ` David Hildenbrand [this message]
2020-05-07 10:31   ` [virtio-dev] [PATCH v3 11/15] virtio-mem: Add parent resource for all added "System RAM" David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 12/15] virtio-mem: Drop manual check for already present memory David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 13/15] virtio-mem: Unplug subblocks right-to-left David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 14/15] virtio-mem: Use -ETXTBSY as error code if the device is busy David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:31   ` David Hildenbrand
2020-05-07 10:31 ` [PATCH v3 15/15] virtio-mem: Try to unplug the complete online memory block first David Hildenbrand
2020-05-07 10:31   ` [virtio-dev] " David Hildenbrand
2020-05-07 10:48 ` [PATCH v3 00/15] virtio-mem: paravirtualized memory Michael S. Tsirkin
2020-05-07 10:48   ` [virtio-dev] " Michael S. Tsirkin
2020-05-07 10:48   ` Michael S. Tsirkin
2020-05-07 11:23   ` David Hildenbrand
2020-05-07 11:23     ` [virtio-dev] " David Hildenbrand
2020-05-07 11:23     ` David Hildenbrand
2020-05-14  6:44 ` [virtio-dev] " teawater
2020-05-14  6:44   ` teawater
2020-05-14  6:44   ` teawater
2020-05-14  8:48   ` [virtio-dev] " David Hildenbrand
2020-05-14  8:48     ` David Hildenbrand
2020-05-14  8:48     ` David Hildenbrand
2020-05-14 10:02     ` [virtio-dev] " teawater
2020-05-14 10:02       ` teawater
2020-05-14 10:02       ` teawater
2020-05-14 10:12       ` [virtio-dev] " David Hildenbrand
2020-05-14 10:12         ` David Hildenbrand
2020-05-14 10:12         ` David Hildenbrand
2020-05-14 11:10         ` [virtio-dev] " David Hildenbrand
2020-05-14 11:10           ` David Hildenbrand
2020-05-14 11:10           ` David Hildenbrand
2020-05-14 11:47           ` [virtio-dev] " David Hildenbrand
2020-05-14 11:47             ` David Hildenbrand
2020-05-14 11:47             ` David Hildenbrand
2020-05-14 12:19             ` [virtio-dev] " David Hildenbrand
2020-05-14 12:19               ` David Hildenbrand
2020-05-14 12:19               ` David Hildenbrand
2020-05-15  2:58               ` [virtio-dev] " teawater
2020-05-15  2:58                 ` teawater
2020-05-15  2:58                 ` teawater

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200507103119.11219-12-david@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mst@redhat.com \
    --cc=pankaj.gupta.linux@gmail.com \
    --cc=virtio-dev@lists.oasis-open.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.