From patchwork Thu May 20 08:22:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12269441 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BECA9C433ED for ; Thu, 20 May 2021 08:30:27 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 6D8FC610A2 for ; Thu, 20 May 2021 08:30:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6D8FC610A2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:38878 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lje4Y-0003hE-Ik for qemu-devel@archiver.kernel.org; Thu, 20 May 2021 04:30:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50464) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljdxf-0005OE-9R for qemu-devel@nongnu.org; Thu, 20 May 2021 04:23:20 -0400 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]:46900) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ljdxY-0001Wv-32 for qemu-devel@nongnu.org; Thu, 20 May 2021 04:23:15 -0400 Received: by mail-wr1-x430.google.com with SMTP id y14so14589746wrm.13 for ; Thu, 20 May 2021 01:23:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Cb/EIE1W0YCsIKhSOhonZiq7HmU1guIPixtbSVuS12k=; b=BDHDaNyaL7OjEsv/MBUph3q0vw88a4QIvhWZtA30u5/au6lzhLnvwk4/ZnU9BEM0xh a3NNM+9ZcinwqRZ7yCRSywlDBzDoTpxx3Y8ephVWVpnZiTTk0JCse/86UmGSqglp044x i3p91sebkC5nN7iXjIlbPPLfXwBB4t33ZWMqJUpm8eSNdymzMf7aVMHiHvfxUOPtyyK/ bPuHp03iKWM8O3+hMDcs6/MbMF0IcpYEtq7WNHGVmpALVSbCX/IEDTI8wbFGy1O50+9s JVJngP9dj+7srtd7zd7wYf0Hg4EibFj3YlKfebuqtJIELv2P1kPceKtBr/RZm6OTQmcc jQYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Cb/EIE1W0YCsIKhSOhonZiq7HmU1guIPixtbSVuS12k=; b=E/Wwihshr2Xg2CVeV2xhT+24PS2dloYTnjWqem5O1nfVlb5AMt6tX3c9bsIsQsE+WQ zQuJNpT+WxvdHMLN4s2zkml8vczGkOL3DwxcFgF93Aqzp8g74UIT/G+s27SW3L8Lnqzx tQJmDBr0FpBN7PSTlegKHgFFTDTt6AWzLYmV/M4lrIGNa04S+U/q3Y5yPOpM1sZQnkBl KFTM7gCoUrdm5dfnzmQjbmWS/XkRXLz5EyL73lYJqFs532lfRgnbTCuA1wiJw+o/PwVF uxs6alji1kVSni3mHlDPIieRbteLMFYPn8fvyr9evvHYzzjrAkGvAU0oIvlvL6KVtsg0 HIbA== X-Gm-Message-State: AOAM532NMa/EGAlmsVbDhr6GhZwMR1ZdSAItbv3erQ7vUHVAUz5ioTmm JZCMqRbRaUmyZRRQvIPFDHk1DnopasE4Ow== X-Google-Smtp-Source: ABdhPJyGZg92jyLdJ6jpyk8vrMFb41UeD+amYNCkqQy2pSfnDSlO9I0/zpl2BuMgQkB8Iu8LcG+l/Q== X-Received: by 2002:a5d:4408:: with SMTP id z8mr3057137wrq.2.1621498986395; Thu, 20 May 2021 01:23:06 -0700 (PDT) Received: from avogadro.redhat.com ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id v10sm2603506wrq.0.2021.05.20.01.23.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 01:23:06 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 12/26] KVM: Create the KVMSlot dirty bitmap on flag changes Date: Thu, 20 May 2021 10:22:43 +0200 Message-Id: <20210520082257.187061-13-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520082257.187061-1-pbonzini@redhat.com> References: <20210520082257.187061-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::430; envelope-from=paolo.bonzini@gmail.com; helo=mail-wr1-x430.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.25, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: "Dr . David Alan Gilbert" , Peter Xu Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Peter Xu Previously we have two places that will create the per KVMSlot dirty bitmap: 1. When a newly created KVMSlot has dirty logging enabled, 2. When the first log_sync() happens for a memory slot. The 2nd case is lazy-init, while the 1st case is not (which is a fix of what the 2nd case missed). To do explicit initialization of dirty bitmaps, what we're missing is to create the dirty bitmap when the slot changed from not-dirty-track to dirty-track. Do that in kvm_slot_update_flags(). With that, we can safely remove the 2nd lazy-init. This change will be needed for kvm dirty ring because kvm dirty ring does not use the log_sync() interface at all. Also move all the pre-checks into kvm_slot_init_dirty_bitmap(). Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Peter Xu Message-Id: <20210506160549.130416-4-peterx@redhat.com> Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index f8980e155b..5bc40fd71b 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -177,6 +177,8 @@ static QemuMutex kml_slots_lock; #define kvm_slots_lock() qemu_mutex_lock(&kml_slots_lock) #define kvm_slots_unlock() qemu_mutex_unlock(&kml_slots_lock) +static void kvm_slot_init_dirty_bitmap(KVMSlot *mem); + static inline void kvm_resample_fd_remove(int gsi) { KVMResampleFd *rfd; @@ -500,6 +502,7 @@ static int kvm_slot_update_flags(KVMMemoryListener *kml, KVMSlot *mem, return 0; } + kvm_slot_init_dirty_bitmap(mem); return kvm_set_user_memory_region(kml, mem, false); } @@ -584,8 +587,12 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section, #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) /* Allocate the dirty bitmap for a slot */ -static void kvm_memslot_init_dirty_bitmap(KVMSlot *mem) +static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) { + if (!(mem->flags & KVM_MEM_LOG_DIRTY_PAGES) || mem->dirty_bmap) { + return; + } + /* * XXX bad kernel interface alert * For dirty bitmap, kernel allocates array of size aligned to @@ -640,11 +647,6 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, goto out; } - if (!mem->dirty_bmap) { - /* Allocate on the first log_sync, once and for all */ - kvm_memslot_init_dirty_bitmap(mem); - } - d.dirty_bitmap = mem->dirty_bmap; d.slot = mem->slot | (kml->as_id << 16); ret = kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); @@ -1181,14 +1183,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->start_addr = start_addr; mem->ram = ram; mem->flags = kvm_mem_flags(mr); - - if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { - /* - * Reallocate the bmap; it means it doesn't disappear in - * middle of a migrate. - */ - kvm_memslot_init_dirty_bitmap(mem); - } + kvm_slot_init_dirty_bitmap(mem); err = kvm_set_user_memory_region(kml, mem, true); if (err) { fprintf(stderr, "%s: error registering slot: %s\n", __func__,