qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org
Subject: [Qemu-devel] [PATCH v2 01/17] memory: add getter/setter for owner
Date: Tue,  4 Jun 2013 14:13:45 +0200	[thread overview]
Message-ID: <1370348041-6768-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1370348041-6768-1-git-send-email-pbonzini@redhat.com>

Whenever memory regions are accessed outside the BQL, they need to be
preserved against hot-unplug.  MemoryRegions actually do not have their
own reference count; they piggyback on a QOM object, their "owner".
Add two functions to retrieve and specify the owner.

The setter function will affect the owner recursively on a whole tree
of contained regions, but without crossing (a) aliases (b) regions that
are already owned by another device.  This is so that a device can create
a complex tree of regions and a single call to memory_region_set_owner
will affect the entire tree.

In turn, this lets buses (usually through a bus-specific function, e.g.
pci_register_bar) set the owner for regions that are managed by the bus.
The device must set the owner itself only if the device plays directly
with address_space_memory/io (which shouldn't happen except in special
cases) or if regions are added/deleted after passing the container to
the bus (for example dynamically while the device runs).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/exec/memory.h | 36 ++++++++++++++++++++++++++++++++++++
 memory.c              | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)

diff --git a/include/exec/memory.h b/include/exec/memory.h
index 3598c4f..e51f30f 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -150,6 +150,7 @@ struct MemoryRegion {
     const MemoryRegionIOMMUOps *iommu_ops;
     void *opaque;
     MemoryRegion *parent;
+    struct Object *owner;
     Int128 size;
     hwaddr addr;
     void (*destructor)(MemoryRegion *mr);
@@ -388,6 +389,41 @@ void memory_region_init_iommu(MemoryRegion *mr,
 void memory_region_destroy(MemoryRegion *mr);
 
 /**
+ * memory_region_owner: get a memory region's owner.
+ *
+ * @mr: the memory region being queried.
+ */
+struct Object *memory_region_owner(MemoryRegion *mr);
+
+/**
+ * memory_region_set_owner: set the owner for a memory region and all
+ * the unowned regions below it.
+ *
+ * The owner of a region is an object that must be preserved together
+ * with the region itself while the region is being accessed.  This
+ * is useful whenever a region is accessed while the big QEMU lock is
+ * not held, even in the simplest case of accessing RAM from
+ * asynchronous block device I/O.
+ *
+ * This function will affect the owner recursively on a whole tree
+ * of contained regions (not aliases), but without crossing regions that
+ * are already owned by another device.  This is so that a device can create
+ * a complex tree of regions and a single call to memory_region_set_owner
+ * will affect the entire tree.
+ *
+ * This function will usually be called through a bus-specific function, e.g.
+ * pci_register_bar or sysbus_init_mmio.  The device must set the owner itself
+ * only if it uses memory_region_add_subregion directly on some address space,
+ * or after the parent region is passed to the bus (for example dynamically
+ * while the device runs).
+ *
+ * @mr: the memory region being set.
+ * @owner: the object that acts as the owner
+ */
+void memory_region_set_owner(MemoryRegion *mr,
+                             struct Object *owner);
+
+/**
  * memory_region_size: get a memory region's size.
  *
  * @mr: the memory region being queried.
diff --git a/memory.c b/memory.c
index c500d8d..b40cdde 100644
--- a/memory.c
+++ b/memory.c
@@ -823,6 +823,7 @@ void memory_region_init(MemoryRegion *mr,
     mr->opaque = NULL;
     mr->iommu_ops = NULL;
     mr->parent = NULL;
+    mr->owner = NULL;
     mr->size = int128_make64(size);
     if (size == UINT64_MAX) {
         mr->size = int128_2_64();
@@ -1089,6 +1090,50 @@ void memory_region_destroy(MemoryRegion *mr)
     g_free(mr->ioeventfds);
 }
 
+Object *memory_region_owner(MemoryRegion *mr)
+{
+    return mr->owner;
+}
+
+void memory_region_set_owner(MemoryRegion *mr,
+                             Object *owner)
+{
+    MemoryRegion *child;
+    Object *old_owner;
+
+    old_owner = mr->owner;
+    assert(old_owner == NULL || old_owner == owner);
+
+    if (owner != NULL && old_owner == NULL) {
+        object_ref(owner);
+    }
+    mr->owner = owner;
+
+    QTAILQ_FOREACH(child, &mr->subregions, subregions_link) {
+        Object *child_owner = child->owner;
+        if (child_owner == NULL || child_owner == owner) {
+            /* Balance the reference that would have been added in
+             * memory_region_add_subregion.  Same below for
+             * memory_region_del_subregion.
+             */
+            if (owner != NULL && child_owner == NULL) {
+                memory_region_ref(child);
+            }
+            memory_region_set_owner(child, owner);
+            if (owner == NULL && child_owner != NULL) {
+                memory_region_unref(child);
+            }
+        }
+    }
+
+    /* Do not unref until all child regions have been processed,
+     * or the old owner might disappear.
+     */
+    if (owner == NULL && old_owner != NULL) {
+        object_unref(old_owner);
+    }
+}
+
 uint64_t memory_region_size(MemoryRegion *mr)
 {
     if (int128_eq(mr->size, int128_2_64())) {
-- 
1.8.1.4

  reply	other threads:[~2013-06-04 12:14 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-04 12:13 [Qemu-devel] [PATCH v2 00/17] Memory/IOMMU patches part 4: region ownership Paolo Bonzini
2013-06-04 12:13 ` Paolo Bonzini [this message]
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 02/17] memory: add ref/unref Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 03/17] memory: add ref/unref calls Paolo Bonzini
2013-06-13  6:28   ` Alexey Kardashevskiy
2013-06-13  9:02     ` Alexey Kardashevskiy
2013-06-14 10:09       ` Alexey Kardashevskiy
2013-06-14 13:56         ` Paolo Bonzini
2013-06-14 15:04           ` Alexey Kardashevskiy
2013-06-25  8:49         ` Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 04/17] exec: add a reference to the region returned by address_space_translate Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 05/17] pci: set owner for BARs Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 06/17] sysbus: add sysbus_pass_mmio Paolo Bonzini
2013-06-04 12:24   ` Peter Maydell
2013-06-04 12:31     ` Paolo Bonzini
2013-06-04 12:36       ` Peter Maydell
2013-06-04 13:24         ` Paolo Bonzini
2013-06-04 14:11           ` Peter Maydell
2013-06-04 14:27             ` Paolo Bonzini
2013-06-04 14:56               ` Peter Maydell
2013-06-04 15:06                 ` Paolo Bonzini
2013-06-04 16:50           ` Paolo Bonzini
2013-06-04 17:47             ` Alex Williamson
2013-06-04 19:11               ` Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 07/17] sysbus: set owner for MMIO regions Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 08/17] acpi: add memory_region_set_owner calls Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 09/17] misc: " Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 10/17] isa/portio: allow setting an owner Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 11/17] vga: add memory_region_set_owner calls Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 12/17] pci-assign: " Paolo Bonzini
2013-06-04 16:42   ` Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 13/17] vfio: " Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 14/17] exec: check MRU in qemu_ram_addr_from_host Paolo Bonzini
2013-06-04 12:13 ` [Qemu-devel] [PATCH v2 15/17] exec: move qemu_ram_addr_from_host_nofail to cputlb.c Paolo Bonzini
2013-06-04 12:14 ` [Qemu-devel] [PATCH v2 16/17] memory: return MemoryRegion from qemu_ram_addr_from_host Paolo Bonzini
2013-06-04 12:14 ` [Qemu-devel] [PATCH v2 17/17] memory: ref/unref memory across address_space_map/unmap Paolo Bonzini

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=1370348041-6768-2-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.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 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).