From patchwork Thu May 6 16:05:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1475150 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NtxlOmxf; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fbdsg5ztdz9sWW for ; Fri, 7 May 2021 02:13:11 +1000 (AEST) Received: from localhost ([::1]:52028 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legca-0002gN-0Q for incoming@patchwork.ozlabs.org; Thu, 06 May 2021 12:13:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59746) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVw-0004LS-Ec for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33242) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVr-0003cB-7S for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317165; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VOVx1SRqMzRYQu3cs/ZILn2Fj3r7/l2ieWtt+maJF9Q=; b=NtxlOmxfXmcxZefsrvnw82LgPjRA8P7rogT/pdzOoSHLR7n5H6odX2IQJb/fOqtJJhfu+B WRx3J8o6i47WaP3+AZIbDfdDZi5tSuvE28pcvSjTd0ecc/HerRFS5IO6pahGMaOLf/Lm+a nv/d9enPp+roepTLv+rsFE/Z2aT4XCI= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-431-ch8cywOmNOOSc9CgKH-c2Q-1; Thu, 06 May 2021 12:06:04 -0400 X-MC-Unique: ch8cywOmNOOSc9CgKH-c2Q-1 Received: by mail-qt1-f199.google.com with SMTP id o5-20020ac872c50000b02901c32e7e3c21so3879120qtp.8 for ; Thu, 06 May 2021 09:06:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VOVx1SRqMzRYQu3cs/ZILn2Fj3r7/l2ieWtt+maJF9Q=; b=dvx4TPeJlpnpmOnl/U+44oU8RUc+szKYLPCaa1VzSb56irbveXX7m9JVZ1J3s0sJGn nfEV7jWFhGSiUb/FZwi5ZPN+unibkul9+Q5Q4lpgaT7rXajlZ/RFuAsp1uFaJP/Pfrzu PJ49eO62yjffnLOrRLE08qQaWu3Po7gFiw4mE/RktfxH1IiKSRjrL8/qbLmnp8SeUzOc g7xbbDewj3aC6m1FAwXzLPCaf230lCxfZFieENM0pvrpr2R3oIQOXVGhssgJPpKsKqlX dCB3Qn6zuuvLkHzNOqZjVBu8vKaQG112J2l4NKhTklSqD3fOwImsvs19ZxcaVwe7AZM9 rxrA== X-Gm-Message-State: AOAM532g6qK5S6N4kTfqKj7UymeyyhRwLIzh4/iZ+NIcCg2zlNttN9jA wHsc2YtvJmalF29z2RzKa0VGzQt1yKpt9t0VooJ3pUDAf94x2e2Ek2LXR1nTEwXfrjaxK/M2Nl1 FLYbkqz/bCXMIIo/0Hee1hy5PwX3MXAsZFFIyIJ4okO9F+4eYZ8sc1uAVxz14UlWr X-Received: by 2002:ae9:ddc2:: with SMTP id r185mr1855157qkf.85.1620317162180; Thu, 06 May 2021 09:06:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9QmNPbSw5E0PNe91dk5j7h8EPei2Rqeu4+vHchRWjfFgHq1g8tpWcnuMaCnRfVMYrm+HmJQ== X-Received: by 2002:ae9:ddc2:: with SMTP id r185mr1855106qkf.85.1620317161864; Thu, 06 May 2021 09:06:01 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:00 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 05/10] KVM: Provide helper to sync dirty bitmap from slot to ramblock Date: Thu, 6 May 2021 12:05:44 -0400 Message-Id: <20210506160549.130416-6-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass client-ip=216.205.24.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 Signed-off-by: Peter Xu --- 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 4ed5b4a3631..1025701f557 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 e13075f738a..ab09a150e19 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 {