qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Stefan Hajnoczi <stefanha@gmail.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	Avi Kivity <avi@redhat.com>,
	Anthony Liguori <anthony@codemonkey.ws>,
	Jan Kiszka <jan.kiszka@siemens.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [patch v4 07/16] memory: make mmio dispatch able to be out of biglock
Date: Mon, 22 Oct 2012 17:23:50 +0800	[thread overview]
Message-ID: <1350897839-29593-8-git-send-email-pingfank@linux.vnet.ibm.com> (raw)
In-Reply-To: <1350897839-29593-1-git-send-email-pingfank@linux.vnet.ibm.com>

Without biglock, we try to protect the mr by increase refcnt.
If we can inc refcnt, go backward and resort to biglock.

Another point is memory radix-tree can be flushed by another
thread, so we should get the copy of terminal mr to survive
from such issue.

Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
---
 exec.c |  125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 117 insertions(+), 8 deletions(-)

diff --git a/exec.c b/exec.c
index 5834766..91b859b 100644
--- a/exec.c
+++ b/exec.c
@@ -200,6 +200,8 @@ struct PhysPageEntry {
     uint16_t ptr : 15;
 };
 
+static QemuMutex mem_map_lock;
+
 /* Simple allocator for PhysPageEntry nodes */
 static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
 static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;
@@ -212,6 +214,8 @@ static PhysPageEntry phys_map = { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
 
 static void io_mem_init(void);
 static void memory_map_init(void);
+static int phys_page_lookup(target_phys_addr_t addr, MemoryRegionSection *mrs);
+
 
 static MemoryRegion io_mem_watch;
 #endif
@@ -2245,6 +2249,7 @@ static void register_subpage(MemoryRegionSection *section)
     subpage_t *subpage;
     target_phys_addr_t base = section->offset_within_address_space
         & TARGET_PAGE_MASK;
+    /* Already under the protection of mem_map_lock */
     MemoryRegionSection *existing = phys_page_find(base >> TARGET_PAGE_BITS);
     MemoryRegionSection subsection = {
         .offset_within_address_space = base,
@@ -3165,6 +3170,8 @@ static void io_mem_init(void)
 
 static void core_begin(MemoryListener *listener)
 {
+    /* protect the updating process of mrs in memory core agaist readers */
+    qemu_mutex_lock(&mem_map_lock);
     destroy_all_mappings();
     phys_sections_clear();
     phys_map.ptr = PHYS_MAP_NODE_NIL;
@@ -3184,17 +3191,32 @@ static void core_commit(MemoryListener *listener)
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
         tlb_flush(env, 1);
     }
+    qemu_mutex_unlock(&mem_map_lock);
 }
 
 static void core_region_add(MemoryListener *listener,
                             MemoryRegionSection *section)
 {
+    MemoryRegion *mr = section->mr;
+
+    if (mr->ops) {
+        if (mr->ops->ref) {
+            mr->ops->ref(mr);
+        }
+    }
     cpu_register_physical_memory_log(section, section->readonly);
 }
 
 static void core_region_del(MemoryListener *listener,
                             MemoryRegionSection *section)
 {
+    MemoryRegion *mr = section->mr;
+
+    if (mr->ops) {
+        if (mr->ops->unref) {
+            mr->ops->unref(mr);
+        }
+    }
 }
 
 static void core_region_nop(MemoryListener *listener,
@@ -3348,6 +3370,8 @@ static void memory_map_init(void)
     memory_region_init(system_io, "io", 65536);
     set_system_io_map(system_io);
 
+    qemu_mutex_init(&mem_map_lock);
+
     memory_listener_register(&core_memory_listener, system_memory);
     memory_listener_register(&io_memory_listener, system_io);
 }
@@ -3406,6 +3430,58 @@ int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
 }
 
 #else
+
+static MemoryRegionSection *subpage_get_terminal(subpage_t *mmio,
+    target_phys_addr_t addr)
+{
+    MemoryRegionSection *section;
+    unsigned int idx = SUBPAGE_IDX(addr);
+
+    section = &phys_sections[mmio->sub_section[idx]];
+    return section;
+}
+
+static int memory_region_section_ref(MemoryRegionSection *mrs)
+{
+    MemoryRegion *mr;
+    int ret = 0;
+
+    mr = mrs->mr;
+    if (mr->ops) {
+        if (mr->ops->ref) {
+            ret = mr->ops->ref(mr);
+        }
+    }
+    return ret;
+}
+
+static void memory_region_section_unref(MemoryRegionSection *mrs)
+{
+    MemoryRegion *mr;
+
+    mr = mrs->mr;
+    if (mr->ops) {
+        if (mr->ops->unref) {
+            mr->ops->unref(mr);
+        }
+    }
+}
+
+static int phys_page_lookup(target_phys_addr_t addr, MemoryRegionSection *mrs)
+{
+    MemoryRegionSection *section;
+    int ret;
+
+    section = phys_page_find(addr >> TARGET_PAGE_BITS);
+    if (section->mr->subpage) {
+        section = subpage_get_terminal(section->mr->opaque, addr);
+    }
+    *mrs = *section;
+    ret = memory_region_section_ref(mrs);
+
+    return ret;
+}
+
 void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                             int len, int is_write)
 {
@@ -3413,14 +3489,28 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
     uint8_t *ptr;
     uint32_t val;
     target_phys_addr_t page;
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, obj_mrs;
+    int safe_ref;
 
     while (len > 0) {
         page = addr & TARGET_PAGE_MASK;
         l = (page + TARGET_PAGE_SIZE) - addr;
         if (l > len)
             l = len;
-        section = phys_page_find(page >> TARGET_PAGE_BITS);
+        qemu_mutex_lock(&mem_map_lock);
+        safe_ref = phys_page_lookup(page, &obj_mrs);
+        qemu_mutex_unlock(&mem_map_lock);
+        if (safe_ref == 0) {
+            qemu_mutex_lock_iothread();
+            qemu_mutex_lock(&mem_map_lock);
+            /* At the 2nd try, mem map can change, so need to judge it again */
+            safe_ref = phys_page_lookup(page, &obj_mrs);
+            qemu_mutex_unlock(&mem_map_lock);
+            if (safe_ref > 0) {
+                qemu_mutex_unlock_iothread();
+            }
+        }
+        section = &obj_mrs;
 
         if (is_write) {
             if (!memory_region_is_ram(section->mr)) {
@@ -3491,10 +3581,16 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                 qemu_put_ram_ptr(ptr);
             }
         }
+
+        memory_region_section_unref(&obj_mrs);
         len -= l;
         buf += l;
         addr += l;
+        if (safe_ref == 0) {
+            qemu_mutex_unlock_iothread();
+        }
     }
+
 }
 
 /* used for ROM loading : can write in RAM and ROM */
@@ -3504,14 +3600,18 @@ void cpu_physical_memory_write_rom(target_phys_addr_t addr,
     int l;
     uint8_t *ptr;
     target_phys_addr_t page;
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, mr_obj;
 
     while (len > 0) {
         page = addr & TARGET_PAGE_MASK;
         l = (page + TARGET_PAGE_SIZE) - addr;
         if (l > len)
             l = len;
-        section = phys_page_find(page >> TARGET_PAGE_BITS);
+
+        qemu_mutex_lock(&mem_map_lock);
+        phys_page_lookup(page, &mr_obj);
+        qemu_mutex_unlock(&mem_map_lock);
+        section = &mr_obj;
 
         if (!(memory_region_is_ram(section->mr) ||
               memory_region_is_romd(section->mr))) {
@@ -3528,6 +3628,7 @@ void cpu_physical_memory_write_rom(target_phys_addr_t addr,
         len -= l;
         buf += l;
         addr += l;
+        memory_region_section_unref(&mr_obj);
     }
 }
 
@@ -3592,7 +3693,7 @@ void *cpu_physical_memory_map(target_phys_addr_t addr,
     target_phys_addr_t todo = 0;
     int l;
     target_phys_addr_t page;
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, mr_obj;
     ram_addr_t raddr = RAM_ADDR_MAX;
     ram_addr_t rlen;
     void *ret;
@@ -3602,7 +3703,10 @@ void *cpu_physical_memory_map(target_phys_addr_t addr,
         l = (page + TARGET_PAGE_SIZE) - addr;
         if (l > len)
             l = len;
-        section = phys_page_find(page >> TARGET_PAGE_BITS);
+        qemu_mutex_lock(&mem_map_lock);
+        phys_page_lookup(page, &mr_obj);
+        qemu_mutex_unlock(&mem_map_lock);
+        section = &mr_obj;
 
         if (!(memory_region_is_ram(section->mr) && !section->readonly)) {
             if (todo || bounce.buffer) {
@@ -3616,6 +3720,7 @@ void *cpu_physical_memory_map(target_phys_addr_t addr,
             }
 
             *plen = l;
+            memory_region_section_unref(&mr_obj);
             return bounce.buffer;
         }
         if (!todo) {
@@ -3630,6 +3735,7 @@ void *cpu_physical_memory_map(target_phys_addr_t addr,
     rlen = todo;
     ret = qemu_ram_ptr_length(raddr, &rlen);
     *plen = rlen;
+    memory_region_section_unref(&mr_obj);
     return ret;
 }
 
@@ -4239,9 +4345,12 @@ bool virtio_is_big_endian(void)
 #ifndef CONFIG_USER_ONLY
 bool cpu_physical_memory_is_io(target_phys_addr_t phys_addr)
 {
-    MemoryRegionSection *section;
+    MemoryRegionSection *section, mr_obj;
 
-    section = phys_page_find(phys_addr >> TARGET_PAGE_BITS);
+    qemu_mutex_lock(&mem_map_lock);
+    phys_page_lookup(phys_addr, &mr_obj);
+    qemu_mutex_unlock(&mem_map_lock);
+    section = &mr_obj;
 
     return !(memory_region_is_ram(section->mr) ||
              memory_region_is_romd(section->mr));
-- 
1.7.4.4

  parent reply	other threads:[~2012-10-22  9:24 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-22  9:23 [Qemu-devel] [patch v4 00/16] push mmio dispatch out of big lock Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 01/16] atomic: introduce atomic operations Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 02/16] qom: apply atomic on object's refcount Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 03/16] hotplug: introduce qdev_unplug_complete() to remove device from views Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 04/16] pci: remove pci device from mem view when unplug Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 05/16] memory: introduce ref, unref interface for MemoryRegionOps Liu Ping Fan
2012-10-22  9:38   ` Avi Kivity
2012-10-23 11:51     ` Paolo Bonzini
2012-10-23 11:55       ` Avi Kivity
2012-10-23 11:57         ` Paolo Bonzini
2012-10-23 12:02           ` Avi Kivity
2012-10-23 12:06             ` Paolo Bonzini
2012-10-23 12:15               ` Avi Kivity
2012-10-23 12:32                 ` Paolo Bonzini
2012-10-23 14:49                   ` Avi Kivity
2012-10-23 15:26                     ` Paolo Bonzini
2012-10-23 16:09                       ` Avi Kivity
2012-10-24  7:29                         ` Paolo Bonzini
2012-10-25 16:28                           ` Avi Kivity
2012-10-26 15:05                             ` Paolo Bonzini
2012-10-23 12:04         ` Jan Kiszka
2012-10-23 12:12           ` Paolo Bonzini
2012-10-23 12:16             ` Jan Kiszka
2012-10-23 12:28               ` Avi Kivity
2012-10-23 12:40                 ` Jan Kiszka
2012-10-23 14:37                   ` Avi Kivity
2012-10-22  9:23 ` [Qemu-devel] [patch v4 06/16] memory: document ref, unref interface Liu Ping Fan
2012-10-22  9:23 ` Liu Ping Fan [this message]
2012-10-23 12:12   ` [Qemu-devel] [patch v4 07/16] memory: make mmio dispatch able to be out of biglock Jan Kiszka
2012-10-23 12:36     ` Avi Kivity
2012-10-24  6:31       ` liu ping fan
2012-10-24  6:56         ` liu ping fan
2012-10-25  8:57           ` Avi Kivity
2012-10-22  9:23 ` [Qemu-devel] [patch v4 08/16] QemuThread: make QemuThread as tls to store extra info Liu Ping Fan
2012-10-22  9:30   ` Jan Kiszka
2012-10-22 17:13     ` Peter Maydell
2012-10-23  5:58       ` liu ping fan
2012-10-23 11:48       ` Paolo Bonzini
2012-10-23 11:50         ` Peter Maydell
2012-10-23 11:51           ` Jan Kiszka
2012-10-23 12:00           ` Paolo Bonzini
2012-10-23 12:27             ` Peter Maydell
2012-11-18 10:02             ` Brad Smith
2012-11-18 16:14               ` Paolo Bonzini
2012-11-18 16:15                 ` Paolo Bonzini
2012-10-22  9:23 ` [Qemu-devel] [patch v4 09/16] memory: introduce mmio request pending to anti nested DMA Liu Ping Fan
2012-10-22 10:28   ` Avi Kivity
2012-10-23 12:38   ` Gleb Natapov
2012-10-24  6:31     ` liu ping fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 10/16] memory: introduce lock ops for MemoryRegionOps Liu Ping Fan
2012-10-22 10:30   ` Avi Kivity
2012-10-23  5:53     ` liu ping fan
2012-10-23  8:53       ` Jan Kiszka
2012-10-22  9:23 ` [Qemu-devel] [patch v4 11/16] vcpu: push mmio dispatcher out of big lock Liu Ping Fan
2012-10-22 10:31   ` Avi Kivity
2012-10-22 10:36     ` Jan Kiszka
2012-10-22  9:23 ` [Qemu-devel] [patch v4 12/16] e1000: apply fine lock on e1000 Liu Ping Fan
2012-10-22 10:37   ` Avi Kivity
2012-10-23  9:04   ` Jan Kiszka
2012-10-24  6:31     ` liu ping fan
2012-10-24  7:17       ` Jan Kiszka
2012-10-25  9:01         ` Avi Kivity
2012-10-25  9:31           ` Jan Kiszka
2012-10-25 16:21             ` Avi Kivity
2012-10-25 16:39               ` Jan Kiszka
2012-10-25 17:02                 ` Avi Kivity
2012-10-25 18:48                   ` Jan Kiszka
2012-10-29  5:24                     ` liu ping fan
2012-10-24  7:29     ` liu ping fan
2012-10-25 13:34       ` Jan Kiszka
2012-10-25 16:23         ` Avi Kivity
2012-10-25 16:41           ` Jan Kiszka
2012-10-25 17:03             ` Avi Kivity
2012-10-29  5:24         ` liu ping fan
2012-10-31  7:03           ` Jan Kiszka
2012-10-22  9:23 ` [Qemu-devel] [patch v4 13/16] e1000: add busy flag to anti broken device state Liu Ping Fan
2012-10-22 10:40   ` Avi Kivity
2012-10-23  5:52     ` liu ping fan
2012-10-23  9:06       ` Avi Kivity
2012-10-23  9:07       ` Jan Kiszka
2012-10-23  9:32         ` liu ping fan
2012-10-23  9:37           ` Avi Kivity
2012-10-24  6:36             ` liu ping fan
2012-10-25  8:55               ` Avi Kivity
2012-10-25  9:00             ` Peter Maydell
2012-10-25  9:04               ` Avi Kivity
2012-10-26  3:05                 ` liu ping fan
2012-10-26  3:08                   ` liu ping fan
2012-10-26 10:25                     ` Jan Kiszka
2012-10-29  5:24                       ` liu ping fan
2012-10-29  7:50                         ` Peter Maydell
2012-10-22  9:23 ` [Qemu-devel] [patch v4 14/16] qdev: introduce stopping state Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 15/16] e1000: introduce unmap() to fix unplug issue Liu Ping Fan
2012-10-22  9:23 ` [Qemu-devel] [patch v4 16/16] e1000: implement MemoryRegionOps's ref&lock interface Liu Ping Fan
2012-10-25 14:04 ` [Qemu-devel] [patch v4 00/16] push mmio dispatch out of big lock Peter Maydell
2012-10-25 16:44   ` Jan Kiszka
2012-10-25 17:07   ` Avi Kivity
2012-10-25 17:13     ` Peter Maydell
2012-10-25 18:13       ` Marcelo Tosatti
2012-10-25 19:00         ` Jan Kiszka
2012-10-25 19:06           ` Peter Maydell
2012-10-29 15:24       ` Avi Kivity

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=1350897839-29593-8-git-send-email-pingfank@linux.vnet.ibm.com \
    --to=pingfank@linux.vnet.ibm.com \
    --cc=anthony@codemonkey.ws \
    --cc=avi@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@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).