All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Dr . David Alan Gilbert" <dgilbert@redhat.com>,
	Peter Xu <peterx@redhat.com>
Subject: [PULL 15/20] KVM: Provide helper to sync dirty bitmap from slot to ramblock
Date: Mon, 17 May 2021 07:19:56 -0400	[thread overview]
Message-ID: <20210517112001.2564006-16-pbonzini@redhat.com> (raw)
In-Reply-To: <20210517112001.2564006-1-pbonzini@redhat.com>

From: Peter Xu <peterx@redhat.com>

kvm_physical_sync_dirty_bitmap() calculates the ramblock offset in an
awkward way from the MemoryRegionSection that passed in from the
caller.  The truth is for each KVMSlot the ramblock offset never
change for the lifecycle.  Cache the ramblock offset for each KVMSlot
into the structure when the KVMSlot is created.

With that, we can further simplify kvm_physical_sync_dirty_bitmap()
with a helper to sync KVMSlot dirty bitmap to the ramblock dirty
bitmap of a specific KVMSlot.

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Message-Id: <20210506160549.130416-6-peterx@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 accel/kvm/kvm-all.c      | 37 +++++++++++++++++--------------------
 include/sysemu/kvm_int.h |  2 ++
 2 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index caaa2a5c98..7031dd4250 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -573,15 +573,12 @@ static void kvm_log_stop(MemoryListener *listener,
 }
 
 /* get kvm's dirty pages bitmap and update qemu's */
-static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
-                                         unsigned long *bitmap)
+static void kvm_slot_sync_dirty_pages(KVMSlot *slot)
 {
-    ram_addr_t start = section->offset_within_region +
-                       memory_region_get_ram_addr(section->mr);
-    ram_addr_t pages = int128_get64(section->size) / qemu_real_host_page_size;
+    ram_addr_t start = slot->ram_start_offset;
+    ram_addr_t pages = slot->memory_size / qemu_real_host_page_size;
 
-    cpu_physical_memory_set_dirty_lebitmap(bitmap, start, pages);
-    return 0;
+    cpu_physical_memory_set_dirty_lebitmap(slot->dirty_bmap, start, pages);
 }
 
 #define ALIGN(x, y)  (((x)+(y)-1) & ~((y)-1))
@@ -656,26 +653,19 @@ static void kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml,
     KVMState *s = kvm_state;
     KVMSlot *mem;
     hwaddr start_addr, size;
-    hwaddr slot_size, slot_offset = 0;
+    hwaddr slot_size;
 
     size = kvm_align_section(section, &start_addr);
     while (size) {
-        MemoryRegionSection subsection = *section;
-
         slot_size = MIN(kvm_max_slot_size, size);
         mem = kvm_lookup_matching_slot(kml, start_addr, slot_size);
         if (!mem) {
             /* We don't have a slot if we want to trap every access. */
             return;
         }
-
         if (kvm_slot_get_dirty_log(s, mem)) {
-            subsection.offset_within_region += slot_offset;
-            subsection.size = int128_make64(slot_size);
-            kvm_get_dirty_pages_log_range(&subsection, d.dirty_bitmap);
+            kvm_slot_sync_dirty_pages(mem);
         }
-
-        slot_offset += slot_size;
         start_addr += slot_size;
         size -= slot_size;
     }
@@ -1134,7 +1124,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
     int err;
     MemoryRegion *mr = section->mr;
     bool writeable = !mr->readonly && !mr->rom_device;
-    hwaddr start_addr, size, slot_size;
+    hwaddr start_addr, size, slot_size, mr_offset;
+    ram_addr_t ram_start_offset;
     void *ram;
 
     if (!memory_region_is_ram(mr)) {
@@ -1152,9 +1143,13 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
         return;
     }
 
-    /* use aligned delta to align the ram address */
-    ram = memory_region_get_ram_ptr(mr) + section->offset_within_region +
-          (start_addr - section->offset_within_address_space);
+    /* The offset of the kvmslot within the memory region */
+    mr_offset = section->offset_within_region + start_addr -
+        section->offset_within_address_space;
+
+    /* use aligned delta to align the ram address and offset */
+    ram = memory_region_get_ram_ptr(mr) + mr_offset;
+    ram_start_offset = memory_region_get_ram_addr(mr) + mr_offset;
 
     kvm_slots_lock();
 
@@ -1193,6 +1188,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
         mem->as_id = kml->as_id;
         mem->memory_size = slot_size;
         mem->start_addr = start_addr;
+        mem->ram_start_offset = ram_start_offset;
         mem->ram = ram;
         mem->flags = kvm_mem_flags(mr);
         kvm_slot_init_dirty_bitmap(mem);
@@ -1203,6 +1199,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,
             abort();
         }
         start_addr += slot_size;
+        ram_start_offset += slot_size;
         ram += slot_size;
         size -= slot_size;
     } while (size);
diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h
index e13075f738..ab09a150e1 100644
--- a/include/sysemu/kvm_int.h
+++ b/include/sysemu/kvm_int.h
@@ -25,6 +25,8 @@ typedef struct KVMSlot
     unsigned long *dirty_bmap;
     /* Cache of the address space ID */
     int as_id;
+    /* Cache of the offset in ram address space */
+    ram_addr_t ram_start_offset;
 } KVMSlot;
 
 typedef struct KVMMemoryListener {
-- 
2.27.0




  parent reply	other threads:[~2021-05-17 11:59 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17 11:19 [PULL 00/20] Misc patches for 2020-05-17 Paolo Bonzini
2021-05-17 11:19 ` [PULL 01/20] configure: Only clone softfloat-3 repositories if TCG is enabled Paolo Bonzini
2021-05-17 11:19 ` [PULL 02/20] configure: check for submodules if --with-git-submodules=ignore Paolo Bonzini
2021-05-17 11:19 ` [PULL 03/20] configure: simplify assignment to GIT_SUBMODULES Paolo Bonzini
2021-05-17 11:19 ` [PULL 04/20] hw/mem/nvdimm: Use Kconfig 'imply' instead of 'depends on' Paolo Bonzini
2021-05-17 11:19 ` [PULL 05/20] backends/tpm: Replace qemu_mutex_lock calls with QEMU_LOCK_GUARD Paolo Bonzini
2021-05-17 11:19 ` [PULL 06/20] i386/cpu: Expose AVX_VNNI instruction to guest Paolo Bonzini
2021-05-17 11:19 ` [PULL 07/20] meson: bump submodule to 0.57.2 Paolo Bonzini
2021-05-17 11:19 ` [PULL 08/20] object: add more commands to preconfig mode Paolo Bonzini
2021-05-17 11:19 ` [PULL 09/20] qtest: add a QOM object for qtest Paolo Bonzini
2021-05-17 11:19 ` [PULL 10/20] KVM: do not allow setting properties at runtime Paolo Bonzini
2021-05-17 11:19 ` [PULL 11/20] memory: Introduce log_sync_global() to memory listener Paolo Bonzini
2021-05-17 11:19 ` [PULL 12/20] KVM: Use a big lock to replace per-kml slots_lock Paolo Bonzini
2021-05-17 11:19 ` [PULL 13/20] KVM: Create the KVMSlot dirty bitmap on flag changes Paolo Bonzini
2021-05-17 11:19 ` [PULL 14/20] KVM: Provide helper to get kvm dirty log Paolo Bonzini
2021-05-17 11:19 ` Paolo Bonzini [this message]
2021-05-17 11:19 ` [PULL 16/20] KVM: Simplify dirty log sync in kvm_set_phys_mem Paolo Bonzini
2021-05-17 11:19 ` [PULL 17/20] KVM: Cache kvm slot dirty bitmap size Paolo Bonzini
2021-05-17 11:19 ` [PULL 18/20] KVM: Add dirty-ring-size property Paolo Bonzini
2021-05-17 11:20 ` [PULL 19/20] KVM: Disable manual dirty log when dirty ring enabled Paolo Bonzini
2021-05-17 11:20 ` [PULL 20/20] KVM: Dirty ring support Paolo Bonzini
2021-05-17 12:15 ` [PULL 00/20] Misc patches for 2020-05-17 no-reply
2021-05-18 15:16 ` Peter Maydell

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=20210517112001.2564006-16-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=peterx@redhat.com \
    --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 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.