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, Liu Ping Fan <qemulist@gmail.com>,
	jan.kiszka@siemens.com
Subject: [Qemu-devel] [PATCH 15/15] memory: ref/unref memory across address_space_map/unmap
Date: Sun,  2 Jun 2013 17:43:32 +0200	[thread overview]
Message-ID: <1370187812-13191-16-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1370187812-13191-1-git-send-email-pbonzini@redhat.com>

The iothread mutex might be released between map and unmap, so the
mapped region might disappear.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 exec.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/exec.c b/exec.c
index 39324ba..538aa9e 100644
--- a/exec.c
+++ b/exec.c
@@ -2068,6 +2068,7 @@ void cpu_physical_memory_write_rom(hwaddr addr,
 }
 
 typedef struct {
+    MemoryRegion *mr;
     void *buffer;
     hwaddr addr;
     hwaddr len;
@@ -2165,15 +2166,18 @@ void *address_space_map(AddressSpace *as,
             bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
             bounce.addr = addr;
             bounce.len = l;
+            bounce.mr = mr;
             if (!is_write) {
                 address_space_read(as, addr, bounce.buffer, l);
             }
 
             *plen = l;
+            memory_region_ref(mr);
             return bounce.buffer;
         }
         if (!todo) {
             raddr = memory_region_get_ram_addr(mr) + xlat;
+            memory_region_ref(mr);
         } else {
             if (memory_region_get_ram_addr(mr) + xlat != raddr + todo) {
                 memory_region_unref(mr);
@@ -2200,8 +2204,12 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
                          int is_write, hwaddr access_len)
 {
     if (buffer != bounce.buffer) {
+        MemoryRegion *mr;
+        ram_addr_t addr1;
+
+        mr = qemu_ram_addr_from_host(buffer, &addr1);
+        assert(mr);
         if (is_write) {
-            ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
             while (access_len) {
                 unsigned l;
                 l = TARGET_PAGE_SIZE;
@@ -2215,6 +2223,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
         if (xen_enabled()) {
             xen_invalidate_map_cache_entry(buffer);
         }
+        memory_region_unref(mr);
         return;
     }
     if (is_write) {
@@ -2222,6 +2231,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
     }
     qemu_vfree(bounce.buffer);
     bounce.buffer = NULL;
+    memory_region_unref(bounce.mr);
     cpu_notify_map_clients();
 }
 
-- 
1.8.1.4

  parent reply	other threads:[~2013-06-02 15:44 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-02 15:43 [Qemu-devel] [PATCH 00/15] Memory/IOMMU patches part 4: region ownership Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 01/15] memory: add getter/setter for owner Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 02/15] memory: add ref/unref Paolo Bonzini
2013-06-02 15:58   ` Peter Maydell
2013-06-03  6:49     ` Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 03/15] memory: add ref/unref calls Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 04/15] exec: add a reference to the region returned by address_space_translate Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 05/15] pci: set owner for BARs Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 06/15] sysbus: set owner for MMIO regions Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 07/15] acpi: add memory_region_set_owner calls Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 08/15] misc: " Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 09/15] isa/portio: allow setting an owner Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 10/15] vga: add memory_region_set_owner calls Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 11/15] pci-assign: " Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 12/15] vfio: " Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 13/15] exec: check MRU in qemu_ram_addr_from_host Paolo Bonzini
2013-06-02 15:43 ` [Qemu-devel] [PATCH 14/15] memory: return MemoryRegion from qemu_ram_addr_from_host Paolo Bonzini
2013-06-02 16:04   ` Peter Maydell
2013-06-03  6:40     ` Paolo Bonzini
2013-06-03 10:51       ` Paolo Bonzini
2013-06-02 15:43 ` Paolo Bonzini [this message]
2013-06-02 16:12 ` [Qemu-devel] [PATCH 00/15] Memory/IOMMU patches part 4: region ownership Peter Maydell
2013-06-03  6:47   ` Paolo Bonzini
2013-06-03  9:22     ` Peter Maydell
2013-06-03  9:40       ` Paolo Bonzini
2013-06-03  9:58         ` Peter Maydell
2013-06-03 10:12           ` Paolo Bonzini
2013-06-03 10:25             ` Peter Maydell
2013-06-03 11:05               ` 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=1370187812-13191-16-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemulist@gmail.com \
    /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).