From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611444; cv=none; d=zohomail.com; s=zohoarc; b=XRtbmfWmxCQV05pnUB/UAcupDSA+m4yuOedXn/LJKlJH0IPDn1RM9EMaCOoG7DRWJrX15j26ExF1N1HyVETa/MHRIPlK3sLfbM0dNxw+YMTm4OzcpTBNsghuE6Tw3s/nf3o2KOoevwMhmL2vIxqbVQ7JiBVSqUJ8IimWA1J0rjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611444; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=rC712/DPw5bCFyqsNpzp3vrOKQtQcPcqk0rALenCWhY=; b=iX5plIgSSdyG6oubojIZ549+QS2Im/sUSbNycaU67icdslmzzCWWCqeGUfVGG7JGBjXpK9eqMaOWyj8tOwHt0VKtdpbcAStropOIZkET49pXYNoRroRvTi0+PchROrhZzk8ZaB32Yfo2u8L2hubbdoD6zLToUSjGTUGtjVoTeOI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611444219973.5524061171924; Wed, 24 Mar 2021 11:44:04 -0700 (PDT) Received: from localhost ([::1]:46498 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8U6-0001TT-Oe for importer@patchew.org; Wed, 24 Mar 2021 14:44:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QJ-0007HF-J4 for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:07 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:60684) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lP8QF-0006W3-GT for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:06 -0400 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-564-r54RtOjAP6K2GfLl7b2OqQ-1; Wed, 24 Mar 2021 14:40:00 -0400 Received: by mail-qk1-f200.google.com with SMTP id y22so2252073qkb.23 for ; Wed, 24 Mar 2021 11:40:00 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.39.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:39:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611201; 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=rC712/DPw5bCFyqsNpzp3vrOKQtQcPcqk0rALenCWhY=; b=X0/Po8N+j+ooCNw0/SXFLemp3ZCtnqoE5oJpuWMO373hsx2izuSEGhZdksbKSRWVe8l9hf dfVDb7C52ZaTQiKGdrr/Sn651PCbYXRsH0tE4FN8aQtCathc52TM4Az+6pUDGCvZeULtfe 4iHQ2JcpdfpK0nehSdyDkPleZ4o6p68= X-MC-Unique: r54RtOjAP6K2GfLl7b2OqQ-1 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=rC712/DPw5bCFyqsNpzp3vrOKQtQcPcqk0rALenCWhY=; b=FPXIW30y1eBUPeuEhUr+j2iKIamePID3020m+c/EmuYsRs8kFX/7aF4FD2Yr6VKaAF CMYCpustw986P4LqlxYwR/zxhglF3ODPi/qs5zh/DfG9DIYERV7Fex/QzTmqUqsHvdfY LqN2oIhmPBZKdHY+tr1Q6tPB9TVoaJFddYnhmBRq/X6DUWDaME+DIRy2vng+qHj/h8h2 Ihj1iMKt0Bs+dpdO9r+gi4cSgAzM7xBP4cRyOj7M4XLc7Mr1LNooJ8G4+i/eazIlQfOP 2vjl7BsXpZY7S3Cb+Ew7vsV9nHx6zM0Z/m5EHOChmxXixQ95/U+fV45nm8G+qBM0Tb5r B6ew== X-Gm-Message-State: AOAM5338dXy8Pzc+i41sKhlKass7YXWPhYnW8X/TzLLjzqybUsp/wtUL Rhy1gf3eREGOxeFpWnYCoMkSYw5H7T9swnqj2V1aflqcEyKxowxPxHICzD2a6tWJaMXYVtXMMtb OXJ9HKRFwSwIVjKnl7ZpQ5x//OLpcP6UXimJul1YeUOp3hyMs8TvyFEShJRxya/zq X-Received: by 2002:a37:a785:: with SMTP id q127mr4278297qke.425.1616611199243; Wed, 24 Mar 2021 11:39:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweYeb91rQVVwEQWufN0a2uPDHOjzryD/NMJyiKTkffZtb9bNSLXsidzt7dE2YbPP2nhxq8yg== X-Received: by 2002:a37:a785:: with SMTP id q127mr4278268qke.425.1616611198894; Wed, 24 Mar 2021 11:39:58 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 01/10] memory: Introduce log_sync_global() to memory listener Date: Wed, 24 Mar 2021 14:39:45 -0400 Message-Id: <20210324183954.345629-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Some of the memory listener may want to do log synchronization without being able to specify a range of memory to sync but always globally. Such a memory listener should provide this new method instead of the log_sync() method. Obviously we can also achieve similar thing when we put the global sync logic into a log_sync() handler. However that's not efficient enough because otherwise memory_global_dirty_log_sync() may do the global sync N times, where N is the number of flat ranges in the address space. Make this new method be exclusive to log_sync(). Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Peter Xu --- include/exec/memory.h | 12 ++++++++++++ softmmu/memory.c | 33 +++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 5728a681b27..97de5a8fa1e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -616,6 +616,18 @@ struct MemoryListener { */ void (*log_sync)(MemoryListener *listener, MemoryRegionSection *sectio= n); =20 + /** + * @log_sync_global: + * + * This is the global version of @log_sync when the listener does + * not have a way to synchronize the log with finer granularity. + * When the listener registers with @log_sync_global defined, then + * its @log_sync must be NULL. Vice versa. + * + * @listener: The #MemoryListener. + */ + void (*log_sync_global)(MemoryListener *listener); + /** * @log_clear: * diff --git a/softmmu/memory.c b/softmmu/memory.c index d4493ef9e43..ea36805e2e9 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2057,6 +2057,10 @@ void memory_region_set_dirty(MemoryRegion *mr, hwadd= r addr, memory_region_get_dirty_log_mask(m= r)); } =20 +/* + * If memory region `mr' is NULL, do global sync. Otherwise, sync + * dirty bitmap for the specified memory region. + */ static void memory_region_sync_dirty_bitmap(MemoryRegion *mr) { MemoryListener *listener; @@ -2070,18 +2074,24 @@ static void memory_region_sync_dirty_bitmap(MemoryR= egion *mr) * address space once. */ QTAILQ_FOREACH(listener, &memory_listeners, link) { - if (!listener->log_sync) { - continue; - } - as =3D listener->address_space; - view =3D address_space_get_flatview(as); - FOR_EACH_FLAT_RANGE(fr, view) { - if (fr->dirty_log_mask && (!mr || fr->mr =3D=3D mr)) { - MemoryRegionSection mrs =3D section_from_flat_range(fr, vi= ew); - listener->log_sync(listener, &mrs); + if (listener->log_sync) { + as =3D listener->address_space; + view =3D address_space_get_flatview(as); + FOR_EACH_FLAT_RANGE(fr, view) { + if (fr->dirty_log_mask && (!mr || fr->mr =3D=3D mr)) { + MemoryRegionSection mrs =3D section_from_flat_range(fr= , view); + listener->log_sync(listener, &mrs); + } } + flatview_unref(view); + } else if (listener->log_sync_global) { + /* + * No matter whether MR is specified, what we can do here + * is to do a global sync, because we are not capable to + * sync in a finer granularity. + */ + listener->log_sync_global(listener); } - flatview_unref(view); } } =20 @@ -2769,6 +2779,9 @@ void memory_listener_register(MemoryListener *listene= r, AddressSpace *as) { MemoryListener *other =3D NULL; =20 + /* Only one of them can be defined for a listener */ + assert(!(listener->log_sync && listener->log_sync_global)); + listener->address_space =3D as; if (QTAILQ_EMPTY(&memory_listeners) || listener->priority >=3D QTAILQ_LAST(&memory_listeners)->priorit= y) { --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611680; cv=none; d=zohomail.com; s=zohoarc; b=I8181bGCFo0INzTqZyc5UI42ptXZMIPiMHGZNlo4zwB3FHiY6JexSYk2hPc1gwM2s5sWmUD+Y5JMJAYBEATU/BrcH/G8URMtobfRK9gk0MG+PHzqax4qe4SMCsiS8tQIQOWoMk4MyuFVMeVG8bxGeGbE88zSHDxhqhVhPriJAuc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611680; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=T1Jntt8jjTHAaR26UjFByLuJS6U0C8B8GRy9fsEcFe4=; b=Jw999Kc8eaElFrk0icRa3+pFmm2/Esc+V/M938Nv2UGeFoug7RBHXKi6/ltdXugEHOBVdI7HWGFmw6YJ3KswmRuK57/FqLan6QEUydZS4glFvW34jheEX8gjyKFvnydds2PTr9VEQdUDGC+5UclnGkCE1V6xeQaLZFZkGu/jS6A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611680297464.655491283424; Wed, 24 Mar 2021 11:48:00 -0700 (PDT) Received: from localhost ([::1]:54870 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8Xv-000501-29 for importer@patchew.org; Wed, 24 Mar 2021 14:47:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47062) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QK-0007JP-Vq for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:32463) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QI-0006YO-Iw for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:08 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-314-kQumSyFkO7adtIqTIpPr2Q-1; Wed, 24 Mar 2021 14:40:02 -0400 Received: by mail-qv1-f69.google.com with SMTP id k4so1996916qvf.8 for ; Wed, 24 Mar 2021 11:40:02 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.39.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:39:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611205; 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=T1Jntt8jjTHAaR26UjFByLuJS6U0C8B8GRy9fsEcFe4=; b=g7FE6l6jab0Cano+/IbTGyOdHOtexQgkrLa1o65xiSjzQm/WNCqxxAIWNDD8np/3peuDZq ljDfIsZirhO6F4LIw7gt1XOQkPPOx7rT1Ry3G1Lb/6kmRvCltn/W1/art8cAQHsxIyCPvo YTyarQIU/O12RKHF2hqR4TAKEkXetEY= X-MC-Unique: kQumSyFkO7adtIqTIpPr2Q-1 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=T1Jntt8jjTHAaR26UjFByLuJS6U0C8B8GRy9fsEcFe4=; b=mN5ocTFpH2/0HH4I6CsrR2ca+uYcbuv6DmpjwcAdqLLMJDrTGNdWw/HODanHIfZy68 9LMVsOcvmWf5ykSFbAmCRK+vOxFylMR68IehQTedaSQLAo++WaHf6ZLvtN/z+yBISHHu KBcYdMJytzulRMTyT6W3ebd3+5MQdekT2O5QMzNI1QuHyNiFEV5ugf1Acp56/jHyslx9 aTAGcF1eBAJ/wCt6ofKQOTv303Kob5iXl0lqooGz03a8AI4wKPUNfYGYLLVqmVZAUEfd RvvSNJB3FEbjVdJSF6LFNSpgCCice5XwJ9WwRgKVDsuZWhed83n2KwIY8ZyHNoJbwNLp bkYw== X-Gm-Message-State: AOAM530RiUDf3QGTnwDdsZt971HUIKx3LcIvkR/HYejltvit2q4RjKRS ygw6+CD50INwXlu78bvEUELhmmaugN4CxXhDlwzQRxjLYgsYKt/9y6C654Bcvb+zbFn4f7RUXF+ yAEh6Zh9wpBzpW8BlrcO5LS++i8RRq30aqtM3b1IpAEOjVKm9WUE9eOSjnnSwOusu X-Received: by 2002:ac8:4f4b:: with SMTP id i11mr4292325qtw.284.1616611200774; Wed, 24 Mar 2021 11:40:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWd+asMriWav9qc1AMWEU0GLZUaPzZ8NT7XTaU/4a5Lrn+wAeQrSIeqh5FFZf/vapw5ySb8A== X-Received: by 2002:ac8:4f4b:: with SMTP id i11mr4292301qtw.284.1616611200468; Wed, 24 Mar 2021 11:40:00 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 02/10] KVM: Use a big lock to replace per-kml slots_lock Date: Wed, 24 Mar 2021 14:39:46 -0400 Message-Id: <20210324183954.345629-3-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Per-kml slots_lock will bring some trouble if we want to take all slots_loc= k of all the KMLs, especially when we're in a context that we could have taken s= ome of the KML slots_lock, then we even need to figure out what we've taken and what we need to take. Make this simple by merging all KML slots_lock into a single slots lock. Per-kml slots_lock isn't anything that helpful anyway - so far only x86 has= two address spaces (so, two slots_locks). All the rest archs will be having one address space always, which means there's actually one slots_lock so it wil= l be the same as before. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 33 ++++++++++++++++++--------------- include/sysemu/kvm_int.h | 2 -- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index b6d9f92f151..766a7ad8c72 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -174,8 +174,10 @@ typedef struct KVMResampleFd KVMResampleFd; static QLIST_HEAD(, KVMResampleFd) kvm_resample_fd_list =3D QLIST_HEAD_INITIALIZER(kvm_resample_fd_list); =20 -#define kvm_slots_lock(kml) qemu_mutex_lock(&(kml)->slots_lock) -#define kvm_slots_unlock(kml) qemu_mutex_unlock(&(kml)->slots_lock) +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) =20 static inline void kvm_resample_fd_remove(int gsi) { @@ -241,9 +243,9 @@ bool kvm_has_free_slot(MachineState *ms) bool result; KVMMemoryListener *kml =3D &s->memory_listener; =20 - kvm_slots_lock(kml); + kvm_slots_lock(); result =3D !!kvm_get_free_slot(kml); - kvm_slots_unlock(kml); + kvm_slots_unlock(); =20 return result; } @@ -309,7 +311,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, voi= d *ram, KVMMemoryListener *kml =3D &s->memory_listener; int i, ret =3D 0; =20 - kvm_slots_lock(kml); + kvm_slots_lock(); for (i =3D 0; i < s->nr_slots; i++) { KVMSlot *mem =3D &kml->slots[i]; =20 @@ -319,7 +321,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, voi= d *ram, break; } } - kvm_slots_unlock(kml); + kvm_slots_unlock(); =20 return ret; } @@ -515,7 +517,7 @@ static int kvm_section_update_flags(KVMMemoryListener *= kml, return 0; } =20 - kvm_slots_lock(kml); + kvm_slots_lock(); =20 while (size && !ret) { slot_size =3D MIN(kvm_max_slot_size, size); @@ -531,7 +533,7 @@ static int kvm_section_update_flags(KVMMemoryListener *= kml, } =20 out: - kvm_slots_unlock(kml); + kvm_slots_unlock(); return ret; } =20 @@ -812,7 +814,7 @@ static int kvm_physical_log_clear(KVMMemoryListener *km= l, return ret; } =20 - kvm_slots_lock(kml); + kvm_slots_lock(); =20 for (i =3D 0; i < s->nr_slots; i++) { mem =3D &kml->slots[i]; @@ -838,7 +840,7 @@ static int kvm_physical_log_clear(KVMMemoryListener *km= l, } } =20 - kvm_slots_unlock(kml); + kvm_slots_unlock(); =20 return ret; } @@ -1143,7 +1145,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, ram =3D memory_region_get_ram_ptr(mr) + section->offset_within_region + (start_addr - section->offset_within_address_space); =20 - kvm_slots_lock(kml); + kvm_slots_lock(); =20 if (!add) { do { @@ -1201,7 +1203,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, } while (size); =20 out: - kvm_slots_unlock(kml); + kvm_slots_unlock(); } =20 static void kvm_region_add(MemoryListener *listener, @@ -1228,9 +1230,9 @@ static void kvm_log_sync(MemoryListener *listener, KVMMemoryListener *kml =3D container_of(listener, KVMMemoryListener, l= istener); int r; =20 - kvm_slots_lock(kml); + kvm_slots_lock(); r =3D kvm_physical_sync_dirty_bitmap(kml, section); - kvm_slots_unlock(kml); + kvm_slots_unlock(); if (r < 0) { abort(); } @@ -1330,7 +1332,6 @@ void kvm_memory_listener_register(KVMState *s, KVMMem= oryListener *kml, { int i; =20 - qemu_mutex_init(&kml->slots_lock); kml->slots =3D g_malloc0(s->nr_slots * sizeof(KVMSlot)); kml->as_id =3D as_id; =20 @@ -2003,6 +2004,8 @@ static int kvm_init(MachineState *ms) int type =3D 0; uint64_t dirty_log_manual_caps; =20 + qemu_mutex_init(&kml_slots_lock); + s =3D KVM_STATE(ms->accelerator); =20 /* diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index ccb8869f01b..1da30e18841 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -27,8 +27,6 @@ typedef struct KVMSlot =20 typedef struct KVMMemoryListener { MemoryListener listener; - /* Protects the slots and all inside them */ - QemuMutex slots_lock; KVMSlot *slots; int as_id; } KVMMemoryListener; --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611483; cv=none; d=zohomail.com; s=zohoarc; b=g+2p0sr4IF5/S8s0aLxvU7bP4z9rUDc7FpFIh8aRmKj80PohyQ+QOD0K4+ZyIZgsNZ+t3/jMIftcSR22K8P/DVAK76JdMI+hzWf9amZaMHWnc9uRIBbdFKG+Mfzi1ij/pjqhwBWzAMdmWAPlVnlJbkAFiMWRtWCweBPGWqwQTBg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611483; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Vl1BvGQfceFPZAGFbqFHhEuQ9V+7JVB+FCewYBIoqoc=; b=MF4RS04YbC7r90aKh2M0MrsGLRwqpGw6/dWUwF5B2bjDzls/wpFf0XDATwY1xsPIQs3myVYwVt9g/iTTj53XuiwZTIpLvns11qRGzJw5El3VPmXXYdauA2skuv+OYeHe6g7OmvuMuOFy1S3xIQzODGgnRUGIQYnn2apJp3cX0VM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 161661148312822.59216431325285; Wed, 24 Mar 2021 11:44:43 -0700 (PDT) Received: from localhost ([::1]:47914 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8Uj-00023s-T9 for importer@patchew.org; Wed, 24 Mar 2021 14:44:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47070) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QL-0007JZ-57 for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:45222) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QI-0006YN-E4 for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:08 -0400 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-440-HugiSWf5Nna7UnR3g6H4Bw-1; Wed, 24 Mar 2021 14:40:03 -0400 Received: by mail-qk1-f199.google.com with SMTP id c1so2283976qke.8 for ; Wed, 24 Mar 2021 11:40:03 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611205; 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=Vl1BvGQfceFPZAGFbqFHhEuQ9V+7JVB+FCewYBIoqoc=; b=WKeak5k9q7o9kCCS8tGsQVlLIPl07yddXZD6ufAcwcfJMuUj+7b/PCbHxVRDq3FAl1fngG xxcrJnzRcLaG60vxpj8yZ+XkI5xBqaSqCksp+0TlFT5I/2mPIPWTdDkjuwWz6AjbvKGtK9 xVQEPl/DaWj45xP1e40lNk3qW5a5Bhk= X-MC-Unique: HugiSWf5Nna7UnR3g6H4Bw-1 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=Vl1BvGQfceFPZAGFbqFHhEuQ9V+7JVB+FCewYBIoqoc=; b=e72SgeXNeXXW1s2vbNcJgdPjrdlTjtFYf9TAf8AXKMje85+or+vYKpbbr0q+EhAvjK GFlF9EqOSthCrdAaESfo+PdAOiGbyBB20DGwibfiSeFnOp1vkKKvnTVDtcS3Jgf5GaAT xFTFcHVneih1Llr+LlbYSqZO2e7u2ZCAKnsfy5i7/031Ybhemd7k0VcIIsJcFweCqax4 ZTIHu2WgEdOdj7YjTzazLmJZQqrOEPgSfjzj9JN4cYAkbHh8qnitv2VOuBeMKpQt7yTj MeT2Pm97SkJfOMzgyUyOxwKjrb0t+1+xqCdtHtMtJ4girw/ce5v+RVtpftIaF4P0qaUW 5uTA== X-Gm-Message-State: AOAM533ly+zEo205VGyWBxi5goLTy+gzkuE+qWYJYSDi17S9OXyIuFpb Cfx6NioMlBVdCHEpJf7JR+541OSsE5uOResm3wsTNtTxSihdLWUlOKIJ/Tm7f0dkYBjnZEibyFc Qgwx5HJrIYPntN1cv8OjUfCm9/x9JUq01dBGwL/g87Ur7CGI450edfWge3Csv87e7 X-Received: by 2002:ac8:6690:: with SMTP id d16mr4339653qtp.312.1616611202571; Wed, 24 Mar 2021 11:40:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFR7zfFKEfi/ccyelTPCpMJeQZH6QcUNn4NNjvyGAa0hHuqNdayTxuL9QvggeqcMo/pnqdLA== X-Received: by 2002:ac8:6690:: with SMTP id d16mr4339619qtp.312.1616611202247; Wed, 24 Mar 2021 11:40:02 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 03/10] KVM: Create the KVMSlot dirty bitmap on flag changes Date: Wed, 24 Mar 2021 14:39:47 -0400 Message-Id: <20210324183954.345629-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" 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 --- 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 766a7ad8c72..55ee5f1ad69 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -179,6 +179,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) =20 +static void kvm_slot_init_dirty_bitmap(KVMSlot *mem); + static inline void kvm_resample_fd_remove(int gsi) { KVMResampleFd *rfd; @@ -502,6 +504,7 @@ static int kvm_slot_update_flags(KVMMemoryListener *kml= , KVMSlot *mem, return 0; } =20 + kvm_slot_init_dirty_bitmap(mem); return kvm_set_user_memory_region(kml, mem, false); } =20 @@ -586,8 +589,12 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionS= ection *section, #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) =20 /* 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 @@ -642,11 +649,6 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryLis= tener *kml, goto out; } =20 - if (!mem->dirty_bmap) { - /* Allocate on the first log_sync, once and for all */ - kvm_memslot_init_dirty_bitmap(mem); - } - d.dirty_bitmap =3D mem->dirty_bmap; d.slot =3D mem->slot | (kml->as_id << 16); ret =3D kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); @@ -1183,14 +1185,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->start_addr =3D start_addr; mem->ram =3D ram; mem->flags =3D 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 =3D kvm_set_user_memory_region(kml, mem, true); if (err) { fprintf(stderr, "%s: error registering slot: %s\n", __func__, --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611392; cv=none; d=zohomail.com; s=zohoarc; b=F+1byXY7DP3b4/ceAENrwHhc+mGdiFX5NR6kd2SKTjBaYdD2BJOYkHDDc7SQd6KCRVrG0ndAW1tf4xAxxzLXZjMb+DW94sPz/1/dsne5lIWbKhqwfw07sLYoRfpvJG4QGLhP4Xf6mGpeEDSfk0diiFO6Z9FiSnYURvHx19VvORo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611392; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=AJg3LzsoV0uD8Yhji+O8YBMotRNzJfz+BKtRamGN6Tk=; b=G4bfG53BOMIu7Xo+o+AEHvPP2iyT0jcMqFC/lRhy4wJVZ6ToxU3a8mM6UEVF+EWtoOykEYmmHM2gOjkSruOz28OeQhamsLhFn05oWHL3Dj3QiPEK14ogtWkdY2LQHcvC6pKlsqIE6yXe/Wj3xsnuqkGR/KaMvhxNVObM5XBclpo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611392956606.7783780177159; Wed, 24 Mar 2021 11:43:12 -0700 (PDT) Received: from localhost ([::1]:44678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8TH-0000iv-Sa for importer@patchew.org; Wed, 24 Mar 2021 14:43:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47086) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QN-0007KV-0E for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:51871) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QK-0006ag-UP for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:10 -0400 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-174-21qmTyh1Mvac9OMOFkPWyg-1; Wed, 24 Mar 2021 14:40:06 -0400 Received: by mail-qk1-f200.google.com with SMTP id y9so2264095qki.14 for ; Wed, 24 Mar 2021 11:40:06 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611208; 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=AJg3LzsoV0uD8Yhji+O8YBMotRNzJfz+BKtRamGN6Tk=; b=XpsRPBKy8GJUD4c8i8wlhXGDpyoafKTrg6xGHwy+pfDNg4bIJ8w5ULrVvr9u3DBmJLRxM6 gD28psZ2YkyBxSF6DP6a/ZzAWyMOV0xDgPtk4FeaDsPsQeOwXtt+U6QwB7cQuavNGYtSXv 6V4H2ZdEYMQsaGGFsLY28wJS9G2LoSA= X-MC-Unique: 21qmTyh1Mvac9OMOFkPWyg-1 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=AJg3LzsoV0uD8Yhji+O8YBMotRNzJfz+BKtRamGN6Tk=; b=tSNDuJIocz2dU5BsKl03O16ir3maijpp7k/I8GV8HmwFWFiQpvlEJAiq495ZgxWJWQ XF29SzFM77r1nSklQoB0uz8IjXhq+B5+ox4RTtT4MGmzsoJOeutbhM05n+0RsqISOLUC AoKo65JXGtZnizrUNBeS+9Nqn/fwfU2QJh6npERsMdGnQZv4JEww4TG8fRUlVdl+bmav 9p/vk/+S0FZZotLyc/p/FxTx6cUXPohhZvWdqMM/4TQNxrFoElAjpp7784WiWR7ZQcYR vyw/TYIKEx/2Lq8dmRYgMTr12Hl7kd7UvuTYt2TRdXdHCNwGeg71iNK+ZNOtRkZkwbMo dLfg== X-Gm-Message-State: AOAM533tS9AKUzaIZ0fYouWlxIWxEg8i8bVsUlOBBnHzwW+I3cA7ekFT bKSPQcwSusJ80wUZIDa3OACtLJDcQuxWM16MXIEtT0PCOsdlIllqj1IGpg3VtJUd8bhT/LbCpmF i90Y984PrJh5fnVq5wfUbgaBb2MEsNA3g3XDX1f5bU3mtTJMHRK+UL5q0JyX6txz1 X-Received: by 2002:a05:620a:16dc:: with SMTP id a28mr4488620qkn.442.1616611204057; Wed, 24 Mar 2021 11:40:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwK9tqFDdw8BYGI11hYwChpwWckrv//tqUo0HyopDIL/feLYjDWmj7WeiFl0eyqCkXz/3Ygqg== X-Received: by 2002:a05:620a:16dc:: with SMTP id a28mr4488585qkn.442.1616611203743; Wed, 24 Mar 2021 11:40:03 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 04/10] KVM: Provide helper to get kvm dirty log Date: Wed, 24 Mar 2021 14:39:48 -0400 Message-Id: <20210324183954.345629-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Provide a helper kvm_slot_get_dirty_log() to make the function kvm_physical_sync_dirty_bitmap() clearer. We can even cache the as_id into KVMSlot when it is created, so that we don't even need to pass it down every time. Since at it, remove return value of kvm_physical_sync_dirty_bitmap() because it should never fail. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 52 +++++++++++++++++++++++----------------- include/sysemu/kvm_int.h | 2 ++ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 55ee5f1ad69..27460587a03 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -617,6 +617,30 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) mem->dirty_bmap =3D g_malloc0(bitmap_size); } =20 +/* + * Sync dirty bitmap from kernel to KVMSlot.dirty_bmap, return true if + * succeeded, false otherwise + */ +static bool kvm_slot_get_dirty_log(KVMState *s, KVMSlot *slot) +{ + struct kvm_dirty_log d =3D {}; + int ret; + + d.dirty_bitmap =3D slot->dirty_bmap; + d.slot =3D slot->slot | (slot->as_id << 16); + ret =3D kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); + + if (ret =3D=3D -ENOENT) { + /* kernel does not have dirty bitmap in this slot */ + ret =3D 0; + } + if (ret) { + error_report_once("%s: KVM_GET_DIRTY_LOG failed with %d", + __func__, ret); + } + return ret =3D=3D 0; +} + /** * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space * @@ -628,15 +652,13 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) * @kml: the KVM memory listener object * @section: the memory section to sync the dirty bitmap with */ -static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, - MemoryRegionSection *section) +static void kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, + MemoryRegionSection *section) { KVMState *s =3D kvm_state; - struct kvm_dirty_log d =3D {}; KVMSlot *mem; hwaddr start_addr, size; hwaddr slot_size, slot_offset =3D 0; - int ret =3D 0; =20 size =3D kvm_align_section(section, &start_addr); while (size) { @@ -646,19 +668,10 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryLi= stener *kml, mem =3D kvm_lookup_matching_slot(kml, start_addr, slot_size); if (!mem) { /* We don't have a slot if we want to trap every access. */ - goto out; + return; } =20 - d.dirty_bitmap =3D mem->dirty_bmap; - d.slot =3D mem->slot | (kml->as_id << 16); - ret =3D kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); - if (ret =3D=3D -ENOENT) { - /* kernel does not have dirty bitmap in this slot */ - ret =3D 0; - } else if (ret < 0) { - error_report("ioctl KVM_GET_DIRTY_LOG failed: %d", errno); - goto out; - } else { + if (kvm_slot_get_dirty_log(s, mem)) { subsection.offset_within_region +=3D slot_offset; subsection.size =3D int128_make64(slot_size); kvm_get_dirty_pages_log_range(&subsection, d.dirty_bitmap); @@ -668,8 +681,6 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryList= ener *kml, start_addr +=3D slot_size; size -=3D slot_size; } -out: - return ret; } =20 /* Alignment requirement for KVM_CLEAR_DIRTY_LOG - 64 pages */ @@ -1181,6 +1192,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, do { slot_size =3D MIN(kvm_max_slot_size, size); mem =3D kvm_alloc_slot(kml); + mem->as_id =3D kml->as_id; mem->memory_size =3D slot_size; mem->start_addr =3D start_addr; mem->ram =3D ram; @@ -1223,14 +1235,10 @@ static void kvm_log_sync(MemoryListener *listener, MemoryRegionSection *section) { KVMMemoryListener *kml =3D container_of(listener, KVMMemoryListener, l= istener); - int r; =20 kvm_slots_lock(); - r =3D kvm_physical_sync_dirty_bitmap(kml, section); + kvm_physical_sync_dirty_bitmap(kml, section); kvm_slots_unlock(); - if (r < 0) { - abort(); - } } =20 static void kvm_log_clear(MemoryListener *listener, diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 1da30e18841..e13075f738a 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -23,6 +23,8 @@ typedef struct KVMSlot int old_flags; /* Dirty bitmap cache for the slot */ unsigned long *dirty_bmap; + /* Cache of the address space ID */ + int as_id; } KVMSlot; =20 typedef struct KVMMemoryListener { --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616612072; cv=none; d=zohomail.com; s=zohoarc; b=aLlk2kfog+aGa6B9pXZtTVVr3zU8STlnu+L25viaUFlbmlgWnftRdMUzzleJWmoOj0OprB/KuRCqb/wmdIFPNi91WM7ctNUcAzBHIiOPKG6yLpAjuyvSf8GcDxzWpA/0OwvWsCLwvZdd5yGVKjleJEiq0aZMyennu5HcrfOPBZw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616612072; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mAvwmZHAKk0fCUyn2n5FgIr1Et54/9Gy9vYho7TorJQ=; b=A69NQmgslmFV8PlMaDMHBiLnLKAGAedI+cUagdS7uSPYIJ7hlPgtxbTPxImYMw12CqAAf6fTTzhiqWqIi8OTfAK4ZXkRBmql7iyr3ajEijPANQqPJ4BhkF4aCWBoyCgoFkdpvUTMHLuttYNgT852GiUSfM532Fgfwl4xaxpmfwk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616612071825764.0034501640696; Wed, 24 Mar 2021 11:54:31 -0700 (PDT) Received: from localhost ([::1]:42930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8eE-0003SP-Fo for importer@patchew.org; Wed, 24 Mar 2021 14:54:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47134) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QP-0007NU-GN for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:31525) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QL-0006bE-GV for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:13 -0400 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-45-qNVBPKU0PUGde-XiHNC_dQ-1; Wed, 24 Mar 2021 14:40:07 -0400 Received: by mail-qk1-f199.google.com with SMTP id t24so2293072qkg.3 for ; Wed, 24 Mar 2021 11:40:07 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611208; 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=mAvwmZHAKk0fCUyn2n5FgIr1Et54/9Gy9vYho7TorJQ=; b=ZmziySeY9mzOGidM4Jjn/KkPlSE/R/HEbHmgaYZmkU5SesFl2DN5bYOr1JsbEO8yXAYMAg /2JFNpYX/0I8cydNJ28ccwpI5VM0fooltJd23TVTPrM/NTTWEtcfFQk4NL9zUggXAmjCkc a9qMPCLvNI24j0rtNpCxiTqP6x5DkVI= X-MC-Unique: qNVBPKU0PUGde-XiHNC_dQ-1 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=mAvwmZHAKk0fCUyn2n5FgIr1Et54/9Gy9vYho7TorJQ=; b=TWZfuicBXD5ugnp1qTBgXtdmMoW0kCFfGFo+gDy4IojkMytfXyCx8LUovXKTF6E0BP 49X1RyI0I1orUOfj6iFE1pExNqttg1TywBkh0oUZc+/haMwSy4nz6cRS1vI67K4gD7Yt dLLE7wrgnMq100k9eHLUIjT2d8GTjdQnY8GQnRyJwZ38GV22kEVLakllrjxiI1U6b4x8 ATFjjMimDqK7RYmV1HsdXOZHHgwUKbOEKlL/uWzR4gxUgWj47yJKFBWbuVJpKNTRkUYi S3lPhWBm+WM+/oAs00ZKFch9ZsStEA4BbO7i8ZF/FE9q6bOVmveBA88ylZsx1c6uGHqF aAsg== X-Gm-Message-State: AOAM533E5w5xpAIRdr6i7Bv+iWW/h2b6Xbmd5lF0I65lBn0yJfj7IwOS rp80nwe5+vlDjS5xXERxerU08FGnn3He+RHfeoxRdgSip3L2e7s004jjtNxxA0mtQDqO967sFO9 R6ByYeWL4gt50SCZ358WPAHqXde3b9GUUiuOCt4db6j09J2fOpEVfeE+5/XDlP30/ X-Received: by 2002:a37:a785:: with SMTP id q127mr4278799qke.425.1616611206019; Wed, 24 Mar 2021 11:40:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzNA9qgOpRbUsRxvURFi2tp5K1wyMhASJPGOuODAIvzC7uLrdCxV5aUdwSmOOeHMPS/Hvd2sQ== X-Received: by 2002:a37:a785:: with SMTP id q127mr4278774qke.425.1616611205710; Wed, 24 Mar 2021 11:40:05 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 05/10] KVM: Provide helper to sync dirty bitmap from slot to ramblock Date: Wed, 24 Mar 2021 14:39:49 -0400 Message-Id: <20210324183954.345629-6-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" 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 27460587a03..57a43e6a6b2 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -575,15 +575,12 @@ static void kvm_log_stop(MemoryListener *listener, } =20 /* 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 =3D section->offset_within_region + - memory_region_get_ram_addr(section->mr); - ram_addr_t pages =3D int128_get64(section->size) / qemu_real_host_page= _size; + ram_addr_t start =3D slot->ram_start_offset; + ram_addr_t pages =3D slot->memory_size / qemu_real_host_page_size; =20 - cpu_physical_memory_set_dirty_lebitmap(bitmap, start, pages); - return 0; + cpu_physical_memory_set_dirty_lebitmap(slot->dirty_bmap, start, pages); } =20 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) @@ -658,26 +655,19 @@ static void kvm_physical_sync_dirty_bitmap(KVMMemoryL= istener *kml, KVMState *s =3D kvm_state; KVMSlot *mem; hwaddr start_addr, size; - hwaddr slot_size, slot_offset =3D 0; + hwaddr slot_size; =20 size =3D kvm_align_section(section, &start_addr); while (size) { - MemoryRegionSection subsection =3D *section; - slot_size =3D MIN(kvm_max_slot_size, size); mem =3D 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 +=3D slot_offset; - subsection.size =3D int128_make64(slot_size); - kvm_get_dirty_pages_log_range(&subsection, d.dirty_bitmap); + kvm_slot_sync_dirty_pages(mem); } - - slot_offset +=3D slot_size; start_addr +=3D slot_size; size -=3D slot_size; } @@ -1136,7 +1126,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, int err; MemoryRegion *mr =3D section->mr; bool writeable =3D !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; =20 if (!memory_region_is_ram(mr)) { @@ -1154,9 +1145,13 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, return; } =20 - /* use aligned delta to align the ram address */ - ram =3D 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 =3D section->offset_within_region + start_addr - + section->offset_within_address_space; + + /* use aligned delta to align the ram address and offset */ + ram =3D memory_region_get_ram_ptr(mr) + mr_offset; + ram_start_offset =3D memory_region_get_ram_addr(mr) + mr_offset; =20 kvm_slots_lock(); =20 @@ -1195,6 +1190,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->as_id =3D kml->as_id; mem->memory_size =3D slot_size; mem->start_addr =3D start_addr; + mem->ram_start_offset =3D ram_start_offset; mem->ram =3D ram; mem->flags =3D kvm_mem_flags(mr); kvm_slot_init_dirty_bitmap(mem); @@ -1205,6 +1201,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, abort(); } start_addr +=3D slot_size; + ram_start_offset +=3D slot_size; ram +=3D slot_size; size -=3D 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; =20 typedef struct KVMMemoryListener { --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611912; cv=none; d=zohomail.com; s=zohoarc; b=PFJvCy87WGvYQPX4ayCH5jyVVOJFP353bWTzeI1Mtk11yiLakVfwpZJ86FnwJht8WnnWCeVmRCxd8RvWNaPXsZXKN4wtcjkGFjXQsyAK9NLwgMjw2xoXAjrq96Ijk433WdbXh1jHmxEB2e64Jd0ccN5F0HVrXonzH4YFAHLVJzo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611912; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=E9n43tAu28BWttdNlvE7I8oKA+ER66k2/i43qMD9Z2I=; b=btT1Ni3Hxx8yetK4t//0fTwf0VLAKVhsdKxg6GQh2m4dv/LskvqyvFIuupT5coG/PfZaiowOsSQnsS3bpdLIxy6fPi3Nm7zhkJ9Sg43AIh0nQcGQNFd/7aMo+eaYH8ua0WIvyChIXEqQ0ICUNIRDKjtpsKJPGa80KT3DcTd+udA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611912004526.642816748708; Wed, 24 Mar 2021 11:51:52 -0700 (PDT) Received: from localhost ([::1]:35150 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8be-0000D4-SU for importer@patchew.org; Wed, 24 Mar 2021 14:51:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47122) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QO-0007MU-Sz for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26639) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QN-0006c3-5J for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:12 -0400 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-125-6XcOVNA-PxWtIIe7uLxtbA-1; Wed, 24 Mar 2021 14:40:08 -0400 Received: by mail-qt1-f199.google.com with SMTP id t5so1844725qti.5 for ; Wed, 24 Mar 2021 11:40:08 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611210; 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=E9n43tAu28BWttdNlvE7I8oKA+ER66k2/i43qMD9Z2I=; b=IiZNQwRwI60bG0TDnktV9D6QB0vOP+WZyiTN5OJ0P6/g1/KLjZbg8WjiPPQEPQabUKLMSC 5eS8+DQPdLLzP7YDnRXiT3mJCJmnJCq8QGlUpS8B3C4BGOCcVhJHPxrk8Uw19K77F61mgv K8aU5EV6jJLA+gKXRZHl3X4QwtQr45s= X-MC-Unique: 6XcOVNA-PxWtIIe7uLxtbA-1 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=E9n43tAu28BWttdNlvE7I8oKA+ER66k2/i43qMD9Z2I=; b=iDfcOmMDgqpnyLtJbJpsZrmdeFGOCPKm2IJN6BdIpG+gcXtvhNSKbmfgkPD/ewjB5Q riqyGbqtUgTwLvCiLlMlKRENBp6MSBDeNrTbux6LP4XtsUGnIpL6uEw5bSNZ3ZsIxqNP 0WK3KnhXBGURCAcV4EiSRzn0ikd6KzAztVwOH+FgGbiduql/F6h2C6sA6cdLlZ7H3uxG o86Y63L2q4kNOehyZ3bomk60TYkjkkTs4lEi9kF4IvVapuxESpsXQ+kgDwNQzN3LzzRr iaofFqLyxs/nfY9PpshFMG0/Qgy5TveKEGu9NvBza5hJKMH2C0bMgVGS3dRKONHV+XPf 8Jzg== X-Gm-Message-State: AOAM532hV4CYw1+tOZ0JnOuhuSIHz0TU0xNOhV6RoSM96SZo+9EyPua5 iG6bnxNoCvVw6AHXX9kCVotEjZMuLlmSMA5vB9By3mYSCF6fHIeWPoJSEqrjOmw9v6HjDKx396N d3MN7dBF2R7n34mBaDeWpqHJ4KwCoRG93XsG8n9DLnolk9hrex+fduV9Uf+bNxCNL X-Received: by 2002:a0c:a951:: with SMTP id z17mr4333453qva.1.1616611207211; Wed, 24 Mar 2021 11:40:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz7Hx+1a0Ihr050SJNuJbuRu80/8npsiYZerdQoPmQ699TE0NIezLB6QrmlYOxd8SycshHMSg== X-Received: by 2002:a0c:a951:: with SMTP id z17mr4333422qva.1.1616611206883; Wed, 24 Mar 2021 11:40:06 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 06/10] KVM: Simplify dirty log sync in kvm_set_phys_mem Date: Wed, 24 Mar 2021 14:39:50 -0400 Message-Id: <20210324183954.345629-7-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" kvm_physical_sync_dirty_bitmap() on the whole section is inaccurate, because the section can be a superset of the memslot that we're working on. The re= sult is that if the section covers multiple kvm memslots, we could be doing the synchronization for multiple times for each kvmslot in the section. With the two helpers that we just introduced, it's very easy to do it right= now by calling the helpers. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 57a43e6a6b2..e8795d04f85 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -1163,7 +1163,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, goto out; } if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { - kvm_physical_sync_dirty_bitmap(kml, section); + kvm_slot_get_dirty_log(kvm_state, mem); + kvm_slot_sync_dirty_pages(mem); } =20 /* unregister the slot */ --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611727; cv=none; d=zohomail.com; s=zohoarc; b=LJCqEUpfkUmN6qS+XXxKUqK5UVX+3E/ITiEVz3MszB3N+jam/MKTzhCi6NQ9grnirxI5BRCVR6f4i7vd4ZJ5mmPR3pDRNge6UHldyJm3KCdRkdo2AmeEyEt7F96BYPQ+UqYZXbx+Nda1p+734t8/9aITT1qx1L3iFBae0jw4WFc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611727; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=PM6zkCFLhaS1Spp8GGIRs8/5IlGmsui4ppoii8KqbNo=; b=BbYxMtrgqmt3NxgEh+b5dZ0GTa94oHT1KX4euZYAm6+aGt8LYHZReBfEAuuEijFy09Xop8aqfYlnqLs8ZulRk63L6b2XwBUQzew+GA+RdkDef2BRFk7pBUq3geqt1zFW+zGE0hnZT2seLWTWJPBprv4dW3nXqvCRtJZtUWBOjrw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611727502903.5230917162851; Wed, 24 Mar 2021 11:48:47 -0700 (PDT) Received: from localhost ([::1]:56492 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8Yg-0005fT-B4 for importer@patchew.org; Wed, 24 Mar 2021 14:48:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47136) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QQ-0007Oz-3p for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:45727) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QN-0006cA-DM for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:13 -0400 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-405-ABqf2dIKN2ml8Unophp9og-1; Wed, 24 Mar 2021 14:40:09 -0400 Received: by mail-qk1-f199.google.com with SMTP id b78so2271257qkg.13 for ; Wed, 24 Mar 2021 11:40:09 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611210; 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=PM6zkCFLhaS1Spp8GGIRs8/5IlGmsui4ppoii8KqbNo=; b=FyKAt8dlr1ZtcbaIq+4aSGC9yU87JOgnVj269TlBuByJAwWRCZscf6UAMHRxTPGWofj+Av gB9RnSMsBF9dLOPqbeR6p2Na/elDVsOc+aYrtX8Hvd/UB9InZSj+IR5n+0IK4QqjSMgeZc EBOsbzTnmafrlz0TpEx5ueYzvLh4pLA= X-MC-Unique: ABqf2dIKN2ml8Unophp9og-1 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=PM6zkCFLhaS1Spp8GGIRs8/5IlGmsui4ppoii8KqbNo=; b=f8zfL/DeGEG9HNRFmspijlLYBavykVUks3JTiUXtrUJJmlrnkbqG+C3BufY0b2JRRL 7sPD1gPFInlPc23eeCZyMjtou/Tbgb54Q4TJRGYjaDNlBQTRwJzDU9giatBp3EENihX0 +4hD2kdRysImMfzKkpMkcAk1LRi386b3HoGlDTMDslPculkyhGizYrc70yvFdySTgDCm +GyUtSw5IXL9+LqX4bp4Xp/N+3QhI647hf5y/i+mqdXs8qPwmlQzHrhDMIOOBxwAEQnd 2WjfH6VQVNboSCAYi5FoMEmU3EdH3+Ymzg5g5oxVjXyouMHY4Sw3COS4xDbwjl6gQC7k OdKA== X-Gm-Message-State: AOAM530ZaTBH7elaKDWJm/MXS1X/iVcVc6h505CmLPZ36kNtk1bsi3oF b8ODH7WiOpzieKyGGkTC196wf/kT1/Htpz5vvi4D5Z5IJAAhdYqkWkimsoqyNiP6o0tH7qWPXub PUYOD1SiHQjFqON5CjkOL78TX41VSpzDed7gz405IOkFAJfzOPlUIPW7RzfX/Plms X-Received: by 2002:a0c:e38f:: with SMTP id a15mr4757794qvl.18.1616611208364; Wed, 24 Mar 2021 11:40:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwtR7gy0uTt757G7Yq0xpkzvQUaxVPPHsZy4MEAOM8uH4NUMkC3EkptCZ4RfLBml69Yb8Mkaw== X-Received: by 2002:a0c:e38f:: with SMTP id a15mr4757769qvl.18.1616611208146; Wed, 24 Mar 2021 11:40:08 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 07/10] KVM: Cache kvm slot dirty bitmap size Date: Wed, 24 Mar 2021 14:39:51 -0400 Message-Id: <20210324183954.345629-8-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Cache it too because we'll reference it more frequently in the future. Reviewed-by: Dr. David Alan Gilbert Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 1 + include/sysemu/kvm_int.h | 1 + 2 files changed, 2 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index e8795d04f85..4d7ec955b39 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -612,6 +612,7 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) hwaddr bitmap_size =3D ALIGN(mem->memory_size / qemu_real_host_page_si= ze, /*HOST_LONG_BITS*/ 64) / 8; mem->dirty_bmap =3D g_malloc0(bitmap_size); + mem->dirty_bmap_size =3D bitmap_size; } =20 /* diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index ab09a150e19..c788452cd96 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -23,6 +23,7 @@ typedef struct KVMSlot int old_flags; /* Dirty bitmap cache for the slot */ unsigned long *dirty_bmap; + unsigned long dirty_bmap_size; /* Cache of the address space ID */ int as_id; /* Cache of the offset in ram address space */ --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611938; cv=none; d=zohomail.com; s=zohoarc; b=EqT+HQ1t7yNuQGienfRmqc+k+zklaP/1S+qPebWgN3ojVjIGCLHxH/pyLREXMy9ltr1cp4iwzps1nq7lXXw7FPq3XraIrFN0HlxcjWz+qaSHY20QxD3BqXQY+oFlm5b/kA+zqIicxBHO0kLewLMUt0BxPNOa94DNawgCH3O3kEQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611938; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=DCZwd7psCu0ukoI7qN0T500L4gKfi6+wkLXbVcD12lQ=; b=R/FVMzJkHPWCWehwuYQPHGxCdZTzcwqq295P6xhGScGJyYO87en9JDbl/OdBIF7MKbFtVm2urgzVOYzR+MLRRrWGXwAghzJWVR5ECXh1jjXPHefpp1qoeNoBoUhJx2rR0Zab9CtTdRKVAdeYM8R98tZ9TKHi/SCyuZWpnf6De3E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611938663902.7448808336998; Wed, 24 Mar 2021 11:52:18 -0700 (PDT) Received: from localhost ([::1]:36806 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8c5-0000wt-I3 for importer@patchew.org; Wed, 24 Mar 2021 14:52:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47164) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QS-0007T3-Eb for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:20983) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QQ-0006eI-AT for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:16 -0400 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-398-UNn3ns_MN2erCEBjRnqdKQ-1; Wed, 24 Mar 2021 14:40:12 -0400 Received: by mail-qk1-f199.google.com with SMTP id v136so2280600qkb.9 for ; Wed, 24 Mar 2021 11:40:12 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611213; 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=DCZwd7psCu0ukoI7qN0T500L4gKfi6+wkLXbVcD12lQ=; b=RkFFELpvZh8lMWHU64STMOY/4UF5q9BT+kzJhF75X7e8Es7aNvJa59gIVvVW9P1i9XihRZ EKDnbFvzEGkqnyaqXM1r6WMUPlZh8A/W+83+5ceJxZ287NnbAj3OQjijuuYV2MY1dtrzjL Djsc4lFHxtgLLv+d+Mf8PCdQTXN/yxA= X-MC-Unique: UNn3ns_MN2erCEBjRnqdKQ-1 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=DCZwd7psCu0ukoI7qN0T500L4gKfi6+wkLXbVcD12lQ=; b=rX4orVoFsIXM/o0C+4bF9MqlfBANOie0mKlZo3twoi1FGkvSKB9vN9IKZ/Mxu+8gl0 Mc6kqj1ku8Im6IqruQ0K+QC+IRMeAd8X8xHHP66Aew3EyVMJTsQyqIvHTji/2xo+oa3c lCi6Qn0T/wg0QBo99JpCkGmo1kIU4OVYa8TzY3EBe6tgNPkBNnKaw3uss4N6RJVs25Mg w/Pj7zngcMBBk2HDgjYWY7wyM6u9jlJsacJWbRXq8eyLHOz5oBIpoPjX7ott1zaNBJyD tBPZWP72uzS4pgqiG+fa33y100U1GfHL9qltJ3RcgPqpKhoQdPnCAasjq1tfn69NuroK e1ew== X-Gm-Message-State: AOAM532tGtUDuwhAv2Q67NHr5FgSnLq/7q4QYUlEtCLhpYRySEKjCkE/ pUFpF1gpL5Xsdtj4Xt8lyu3FR2f0AABeX2N/q9UinjTYY3BcRDn4BYNw+eHFsDhkmycfwv3/RnL uOSTPJTsV5LqgsWt2AqR7oDZXiKdgjASGL4hVRy+XbaDpr3jiB3TtIkirZcs39Bl4 X-Received: by 2002:a05:620a:553:: with SMTP id o19mr4502743qko.491.1616611210247; Wed, 24 Mar 2021 11:40:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKxrPnHGijjQT8TQ3ZiPV/2nBer89df0nL3Cf0wJvbF755smBqb7CvAT1Ryin6TBlLnWKW6A== X-Received: by 2002:a05:620a:553:: with SMTP id o19mr4502713qko.491.1616611209887; Wed, 24 Mar 2021 11:40:09 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 08/10] KVM: Add dirty-gfn-count property Date: Wed, 24 Mar 2021 14:39:52 -0400 Message-Id: <20210324183954.345629-9-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add a parameter for dirty gfn count for dirty rings. If zero, dirty ring is disabled. Otherwise dirty ring will be enabled with the per-vcpu gfn count= as specified. If dirty ring cannot be enabled due to unsupported kernel or illegal parameter, it'll fallback to dirty logging. By default, dirty ring is not enabled (dirty-gfn-count default to 0). Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 72 +++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 12 ++++++++ 2 files changed, 84 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 4d7ec955b39..8baa002c009 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -128,6 +128,9 @@ struct KVMState KVMMemoryListener *ml; AddressSpace *as; } *as; + bool kvm_dirty_ring_enabled; /* Whether KVM dirty ring is enabled */ + uint64_t kvm_dirty_ring_size; /* Size of the per-vcpu dirty ring */ + uint32_t kvm_dirty_gfn_count; /* Number of dirty GFNs per ring */ }; =20 KVMState *kvm_state; @@ -2132,6 +2135,40 @@ static int kvm_init(MachineState *ms) s->coalesced_pio =3D s->coalesced_mmio && kvm_check_extension(s, KVM_CAP_COALESCED_PIO); =20 + /* + * Enable KVM dirty ring if supported, otherwise fall back to + * dirty logging mode + */ + if (s->kvm_dirty_gfn_count > 0) { + uint64_t ring_size; + + ring_size =3D s->kvm_dirty_gfn_count * sizeof(struct kvm_dirty_gfn= ); + + /* Read the max supported pages */ + ret =3D kvm_vm_check_extension(s, KVM_CAP_DIRTY_LOG_RING); + if (ret > 0) { + if (ring_size > ret) { + error_report("KVM dirty GFN count %" PRIu32 " too big " + "(maximum is %ld). Please use a smaller valu= e.", + s->kvm_dirty_gfn_count, + ret / sizeof(struct kvm_dirty_gfn)); + ret =3D -EINVAL; + goto err; + } + + ret =3D kvm_vm_enable_cap(s, KVM_CAP_DIRTY_LOG_RING, 0, ring_s= ize); + if (ret) { + error_report("Enabling of KVM dirty ring failed: %d. " + "Suggested mininum value is 1024. " + "Please also make sure it's a power of two.",= ret); + goto err; + } + + s->kvm_dirty_ring_size =3D ring_size; + s->kvm_dirty_ring_enabled =3D true; + } + } + dirty_log_manual_caps =3D kvm_check_extension(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); dirty_log_manual_caps &=3D (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | @@ -3175,6 +3212,33 @@ bool kvm_kernel_irqchip_split(void) return kvm_state->kernel_irqchip_split =3D=3D ON_OFF_AUTO_ON; } =20 +static void kvm_get_dirty_gfn_count(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + KVMState *s =3D KVM_STATE(obj); + uint32_t value =3D s->kvm_dirty_gfn_count; + + visit_type_uint32(v, name, &value, errp); +} + +static void kvm_set_dirty_gfn_count(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + KVMState *s =3D KVM_STATE(obj); + Error *error =3D NULL; + uint32_t value; + + visit_type_uint32(v, name, &value, &error); + if (error) { + error_propagate(errp, error); + return; + } + + s->kvm_dirty_gfn_count =3D value; +} + static void kvm_accel_instance_init(Object *obj) { KVMState *s =3D KVM_STATE(obj); @@ -3182,6 +3246,8 @@ static void kvm_accel_instance_init(Object *obj) s->kvm_shadow_mem =3D -1; s->kernel_irqchip_allowed =3D true; s->kernel_irqchip_split =3D ON_OFF_AUTO_AUTO; + /* KVM dirty ring is by default off */ + s->kvm_dirty_gfn_count =3D 0; } =20 static void kvm_accel_class_init(ObjectClass *oc, void *data) @@ -3203,6 +3269,12 @@ static void kvm_accel_class_init(ObjectClass *oc, vo= id *data) NULL, NULL); object_class_property_set_description(oc, "kvm-shadow-mem", "KVM shadow MMU size"); + + object_class_property_add(oc, "dirty-gfn-count", "uint32", + kvm_get_dirty_gfn_count, kvm_set_dirty_gfn_count, + NULL, NULL); + object_class_property_set_description(oc, "dirty-gfn-count", + "KVM dirty GFN count (=3D0 to disable dirty ring)"); } =20 static const TypeInfo kvm_accel_type =3D { diff --git a/qemu-options.hx b/qemu-options.hx index d60a03d3a97..c7877009408 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -141,6 +141,7 @@ DEF("accel", HAS_ARG, QEMU_OPTION_accel, " kvm-shadow-mem=3Dsize of KVM shadow MMU in bytes\n" " split-wx=3Don|off (enable TCG split w^x mapping)\n" " tb-size=3Dn (TCG translation block cache size)\n" + " dirty-gfn-count=3Dn (KVM dirty ring GFN count, defaul= t 0)\n" " thread=3Dsingle|multi (enable multi-threaded TCG)\n",= QEMU_ARCH_ALL) SRST ``-accel name[,prop=3Dvalue[,...]]`` @@ -181,6 +182,17 @@ SRST where both the back-end and front-ends support it and no incompatible TCG features have been enabled (e.g. icount/replay). + + ``dirty-gfn-count=3Dn`` + When KVM accelerator is used, it controls the per-vcpu KVM dirty r= ing + size (number of entries one dirty ring contains, per-vcpu). It sho= uld + be a value that is power of two, and it should be 1024 or bigger (= but + still less than the maximum value that the kernel supports). 4096 + could be a good initial value if you have no idea which is the bes= t. + Set this value to 0 to disable the feature. By default, this feat= ure + is disabled (dirty-gfn-count=3D0). When enabled, it'll automatica= lly + replace the kvm get dirty log feature. + ERST =20 DEF("smp", HAS_ARG, QEMU_OPTION_smp, --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611639; cv=none; d=zohomail.com; s=zohoarc; b=Wrj203RxkwhDZQcoVJQ3BwgNMfZzFpu+PaGGsC2QE5mhJ/enevcRClTb8GFvwweOTnuuvQs0WHMofCcaq4IKHfLbdGdGmnal5ENW2fNT0dDWy7kBshVBVvq3ILH/TWlJxtZvnb/YF3/PvAo+3JvqvVIzuQOaJi0UesFfKgY9H10= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611639; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=z8Azwhwk/zF/WlHWQkj7ts5ZdeHu5O2AXyHc/eoiIgU=; b=KCKWyARb+ACC3g0+cLgb6zmHknLdL+Cetk3MXWvnu7iYI942NfAShQOCL1MCEaQSRyN08eDCFm6G9cXJEn9LGXCXwOV584ULHhCTWSiRLQcfFO435cQIJN9Fm8alUolWs5QE6RGc8rswbANV28vWxUpWY9CEtiECXmyCq8ZVx6s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611639374190.87550343842986; Wed, 24 Mar 2021 11:47:19 -0700 (PDT) Received: from localhost ([::1]:53132 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8XG-0004Ba-7x for importer@patchew.org; Wed, 24 Mar 2021 14:47:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47168) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QS-0007TB-Gf for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:21461) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8QQ-0006f4-Pm for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:40:16 -0400 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-359-PisMivrAMOGqkaTIDvCtSQ-1; Wed, 24 Mar 2021 14:40:12 -0400 Received: by mail-qk1-f200.google.com with SMTP id j14so2276250qka.7 for ; Wed, 24 Mar 2021 11:40:12 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id t24sm2001083qto.23.2021.03.24.11.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:40:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611214; 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=z8Azwhwk/zF/WlHWQkj7ts5ZdeHu5O2AXyHc/eoiIgU=; b=SfXYpo77DGX1LW/gy3RYCKeScuIb2KgvwzHlWGdhiO6SqtNm6/PBmaql+yrAR/ItGusvQ+ JUW9pGP8ICFlZnO6z80lE2+4uGqN42tAGrfy+9dMtELEIjKkRNrbA0h9Nn34Ei6UQ3co0R ZP7ZC1GyCpN4RfIXBsjka18/SyzU08I= X-MC-Unique: PisMivrAMOGqkaTIDvCtSQ-1 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=z8Azwhwk/zF/WlHWQkj7ts5ZdeHu5O2AXyHc/eoiIgU=; b=d1hQIjGjZ+eM6z7ElM4icILpLx8f1aACFoqwdUADkr3m8yU1/9JKEH0VFRAF9Hug1B L3fO6o1S4l2S5StrWNEaNHEjyo5HAmc5sO6KUO/Ns2pUvfXKfeM7OgNPgaaJQcF2gycO Ya+VohuHLRpLM2MNHBN3ANjnPgtHqWL5oqs1u77pepfJt382krtbtDCvNc3z1vkEkfQX rxjgJs1AAHeCWlI7C7qEReZXZhJhQaKqm/3JtMrWcEaM8fMgAUFOSd6Lqk2yTaeLTTl+ MUUA65p894Zs8T2CFIb1nIW1kRguR7XnRq2KtgBbiCCb8u6xV+iEh1Glm/jjWxKI9DbH cUMQ== X-Gm-Message-State: AOAM532b3mvVlZ+cuZmRKcdRhAgwwGZgCTQj1pQ181/8VqM2rfkcBBed IETb/Zx5ScTv9w0K41aueQFUiZY3tFfMdqMEjLGItfnjI1YTNEl1w6Mf1zYvt1e3KpR28pB3a/w 20RWxhI3awR/dyP00K0gQb6aq/0PJzhfS29dPAUmBbMogcxNh77pnFErwwIVrDZTV X-Received: by 2002:a37:92c4:: with SMTP id u187mr4327465qkd.113.1616611211233; Wed, 24 Mar 2021 11:40:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdhmLcJH3LFs1Q8hRH1pyRSXmlm6Xw7XLz1QHWf4QTfR2pCjYsFBq3K3jwkmpdvv2V5fR4Iw== X-Received: by 2002:a37:92c4:: with SMTP id u187mr4327428qkd.113.1616611210887; Wed, 24 Mar 2021 11:40:10 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 09/10] KVM: Disable manual dirty log when dirty ring enabled Date: Wed, 24 Mar 2021 14:39:53 -0400 Message-Id: <20210324183954.345629-10-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is for KVM_CLEAR_DIRTY_LOG, which is only useful for KVM_GET_DIRTY_LOG. Skip enabling it for kvm dirty ring. More importantly, KVM_DIRTY_LOG_INITIALLY_SET will not wr-protect all the p= ages initially, which is against how kvm dirty ring is used - there's no way for= kvm dirty ring to re-protect a page before it's notified as being written first with a GFN entry in the ring! So when KVM_DIRTY_LOG_INITIALLY_SET is enabl= ed with dirty ring, we'll see silent data loss after migration. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 8baa002c009..daf59836ae2 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2169,20 +2169,29 @@ static int kvm_init(MachineState *ms) } } =20 - dirty_log_manual_caps =3D - kvm_check_extension(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); - dirty_log_manual_caps &=3D (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | - KVM_DIRTY_LOG_INITIALLY_SET); - s->manual_dirty_log_protect =3D dirty_log_manual_caps; - if (dirty_log_manual_caps) { - ret =3D kvm_vm_enable_cap(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, 0, - dirty_log_manual_caps); - if (ret) { - warn_report("Trying to enable capability %"PRIu64" of " - "KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed. " - "Falling back to the legacy mode. ", - dirty_log_manual_caps); - s->manual_dirty_log_protect =3D 0; + /* + * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is not needed when dirty ring is + * enabled. More importantly, KVM_DIRTY_LOG_INITIALLY_SET will assume= no + * page is wr-protected initially, which is against how kvm dirty ring= is + * usage - kvm dirty ring requires all pages are wr-protected at the v= ery + * beginning. Enabling this feature for dirty ring causes data corrup= tion. + */ + if (!s->kvm_dirty_ring_enabled) { + dirty_log_manual_caps =3D + kvm_check_extension(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); + dirty_log_manual_caps &=3D (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | + KVM_DIRTY_LOG_INITIALLY_SET); + s->manual_dirty_log_protect =3D dirty_log_manual_caps; + if (dirty_log_manual_caps) { + ret =3D kvm_vm_enable_cap(s, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2= , 0, + dirty_log_manual_caps); + if (ret) { + warn_report("Trying to enable capability %"PRIu64" of " + "KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed.= " + "Falling back to the legacy mode. ", + dirty_log_manual_caps); + s->manual_dirty_log_protect =3D 0; + } } } =20 --=20 2.26.2 From nobody Fri May 3 13:20:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616611642; cv=none; d=zohomail.com; s=zohoarc; b=ccRN93ciz9B787qrKUdFcMTzQIG8wN7BHOg/66QF/TvSkE7pElS+vOnCdmMs2uHxYViDfPZ3BXsQMeZVjHcubz7QIyG/Z+vI92OaMowKrop0EjUYlLW0a6hxz4v6fznt5p+Vep+k5hMhft8uzWuHEl/g4QPHwKPcDrBeiF4CgE4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616611642; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HTQ1xXHASEKUJ38CBeiKxntsoTr7ObwZvbl8ABzgfzY=; b=VtWcq+Zm0POnIXsfiSogUMXA667PFsK0B6Y4yPi2aOSJURxH0TYL5JuwEnltTrk+fu17w8yd5rkxtD7HdawuEz9MAIIflVmEtgtkRkROQGjW/ejQnoMUpiHog1Ct3I9V6HXPglE+iEY3Y6P/gj3SHBLlcMtXxrJdzsI7fdoy9Vc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1616611642574464.9222336668373; Wed, 24 Mar 2021 11:47:22 -0700 (PDT) Received: from localhost ([::1]:53244 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lP8XJ-0004EY-FW for importer@patchew.org; Wed, 24 Mar 2021 14:47:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47468) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8S3-0000Rz-2t for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:41:55 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47196) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lP8S0-0007hd-Ca for qemu-devel@nongnu.org; Wed, 24 Mar 2021 14:41:54 -0400 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-37-I3rN4l41M4eS1hTXsIVRkg-1; Wed, 24 Mar 2021 14:41:50 -0400 Received: by mail-qv1-f72.google.com with SMTP id e10so1978466qvr.17 for ; Wed, 24 Mar 2021 11:41:50 -0700 (PDT) Received: from xz-x1.redhat.com (bras-base-toroon474qw-grc-82-174-91-135-175.dsl.bell.ca. [174.91.135.175]) by smtp.gmail.com with ESMTPSA id g11sm2379526qkk.5.2021.03.24.11.41.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Mar 2021 11:41:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616611311; 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=HTQ1xXHASEKUJ38CBeiKxntsoTr7ObwZvbl8ABzgfzY=; b=bqQfbFtM4ETxED3PDfFbEq0CH66im+CfK9YT7SXo0DbYzkkYscf2OHLBj739JDIFmaFnhA pnnySWaqVYP7mT3nHcv+yI6RW8M7/nFX2uwgEGtl7U6vhIXzQdd8bEZs3uG4/+zYMX2SFW XE58XOsSg5JsVTD+PDiFfhOC+kBYlXs= X-MC-Unique: I3rN4l41M4eS1hTXsIVRkg-1 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=HTQ1xXHASEKUJ38CBeiKxntsoTr7ObwZvbl8ABzgfzY=; b=dOL8LL/acrkDZ7vtTWoqQUV7vsaWFp0qNxR1Pk7AmRy8U4Of890wk+vAzRuudjotOh 7leZrcgHX4HujpsZenYXykngz+lm4Xy8JZlAOx9viomgz9UXthBX19Ujbrmmk10p//5C cc1ZmJEV5OswC0BFlaW3ZwPpOb7J+TYKP0OxkuelxxSZ3vWTUAxShuZ+J2vb8RlQd+k8 Qd72vfzV+dE1koud5G0V//DYCMzT21GNYj5h4wwiG2ROGJartBWXKmmlwdK3n4LqUp9H A/oeyUu/wCLSdy0Lc8sitVZKxJWqCKRISmuCGAoozyQ4JOsZ2OPXjs2zuDYfFhimCSFy 1XDQ== X-Gm-Message-State: AOAM531EP2nP4PTAKmxAy5bAevqkwOxoOixVwyR1/+Z5ywVAIBap9+Dk 7i5uTqaCnRj5KGwWVP2YfmCtXFiHoCzGZN53fhXEsLSCAtVCwSLg1a7uQe2zUVJDA8xEUdtVbSK c3lLRaCtZn4bb5kr35xIIfmZFjGGw5aocNc9jt5AnyPmG8wwKFTtBHJgLlhH7RzAk X-Received: by 2002:a05:620a:14ac:: with SMTP id x12mr4463567qkj.409.1616611308896; Wed, 24 Mar 2021 11:41:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7K5ptcZs8pXsfiUFUWNIQ1wC6j9hSWBvx79g1t+LAj+Z+lMjR8W1c6ie0nL7xeAMP2hzf/g== X-Received: by 2002:a05:620a:14ac:: with SMTP id x12mr4463524qkj.409.1616611308290; Wed, 24 Mar 2021 11:41:48 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v6 10/10] KVM: Dirty ring support Date: Wed, 24 Mar 2021 14:41:46 -0400 Message-Id: <20210324184146.346073-1-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210324183954.345629-1-peterx@redhat.com> References: <20210324183954.345629-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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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: Hyman , Keqian Zhu , "Dr . David Alan Gilbert" , peterx@redhat.com, Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" KVM dirty ring is a new interface to pass over dirty bits from kernel to the userspace. Instead of using a bitmap for each memory region, the dirty ring contains an array of dirtied GPAs to fetch (in the form of offset in slots). For each vcpu there will be one dirty ring that binds to it. kvm_dirty_ring_reap() is the major function to collect dirty rings. It can= be called either by a standalone reaper thread that runs in the background, collecting dirty pages for the whole VM. It can also be called directly by= any thread that has BQL taken. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 339 ++++++++++++++++++++++++++++++++++++++++- accel/kvm/trace-events | 7 + include/hw/core/cpu.h | 7 + 3 files changed, 350 insertions(+), 3 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index daf59836ae2..ab9e1f7e7ed 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -15,6 +15,7 @@ =20 #include "qemu/osdep.h" #include +#include =20 #include =20 @@ -80,6 +81,25 @@ struct KVMParkedVcpu { QLIST_ENTRY(KVMParkedVcpu) node; }; =20 +enum KVMDirtyRingReaperState { + KVM_DIRTY_RING_REAPER_NONE =3D 0, + /* The reaper is sleeping */ + KVM_DIRTY_RING_REAPER_WAIT, + /* The reaper is reaping for dirty pages */ + KVM_DIRTY_RING_REAPER_REAPING, +}; + +/* + * KVM reaper instance, responsible for collecting the KVM dirty bits + * via the dirty ring. + */ +struct KVMDirtyRingReaper { + /* The reaper thread */ + QemuThread reaper_thr; + volatile uint64_t reaper_iteration; /* iteration number of reaper thr = */ + volatile enum KVMDirtyRingReaperState reaper_state; /* reap thr state = */ +}; + struct KVMState { AccelState parent_obj; @@ -131,6 +151,7 @@ struct KVMState bool kvm_dirty_ring_enabled; /* Whether KVM dirty ring is enabled */ uint64_t kvm_dirty_ring_size; /* Size of the per-vcpu dirty ring */ uint32_t kvm_dirty_gfn_count; /* Number of dirty GFNs per ring */ + struct KVMDirtyRingReaper reaper; }; =20 KVMState *kvm_state; @@ -392,6 +413,13 @@ static int do_kvm_destroy_vcpu(CPUState *cpu) goto err; } =20 + if (cpu->kvm_dirty_gfns) { + ret =3D munmap(cpu->kvm_dirty_gfns, s->kvm_dirty_ring_size); + if (ret < 0) { + goto err; + } + } + vcpu =3D g_malloc0(sizeof(*vcpu)); vcpu->vcpu_id =3D kvm_arch_vcpu_id(cpu); vcpu->kvm_fd =3D cpu->kvm_fd; @@ -468,6 +496,19 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) (void *)cpu->kvm_run + s->coalesced_mmio * PAGE_SIZE; } =20 + if (s->kvm_dirty_ring_enabled) { + /* Use MAP_SHARED to share pages with the kernel */ + cpu->kvm_dirty_gfns =3D mmap(NULL, s->kvm_dirty_ring_size, + PROT_READ | PROT_WRITE, MAP_SHARED, + cpu->kvm_fd, + PAGE_SIZE * KVM_DIRTY_LOG_PAGE_OFFSET); + if (cpu->kvm_dirty_gfns =3D=3D MAP_FAILED) { + ret =3D -errno; + DPRINTF("mmap'ing vcpu dirty gfns failed: %d\n", ret); + goto err; + } + } + ret =3D kvm_arch_init_vcpu(cpu); if (ret < 0) { error_setg_errno(errp, -ret, @@ -586,6 +627,11 @@ static void kvm_slot_sync_dirty_pages(KVMSlot *slot) cpu_physical_memory_set_dirty_lebitmap(slot->dirty_bmap, start, pages); } =20 +static void kvm_slot_reset_dirty_pages(KVMSlot *slot) +{ + memset(slot->dirty_bmap, 0, slot->dirty_bmap_size); +} + #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) =20 /* Allocate the dirty bitmap for a slot */ @@ -642,6 +688,171 @@ static bool kvm_slot_get_dirty_log(KVMState *s, KVMSl= ot *slot) return ret =3D=3D 0; } =20 +/* Should be with all slots_lock held for the address spaces. */ +static void kvm_dirty_ring_mark_page(KVMState *s, uint32_t as_id, + uint32_t slot_id, uint64_t offset) +{ + KVMMemoryListener *kml; + KVMSlot *mem; + + if (as_id >=3D s->nr_as) { + return; + } + + kml =3D s->as[as_id].ml; + mem =3D &kml->slots[slot_id]; + + if (!mem->memory_size || offset >=3D + (mem->memory_size / qemu_real_host_page_size)) { + return; + } + + set_bit(offset, mem->dirty_bmap); +} + +static bool dirty_gfn_is_dirtied(struct kvm_dirty_gfn *gfn) +{ + return gfn->flags =3D=3D KVM_DIRTY_GFN_F_DIRTY; +} + +static void dirty_gfn_set_collected(struct kvm_dirty_gfn *gfn) +{ + gfn->flags =3D KVM_DIRTY_GFN_F_RESET; +} + +/* + * Should be with all slots_lock held for the address spaces. It returns = the + * dirty page we've collected on this dirty ring. + */ +static uint32_t kvm_dirty_ring_reap_one(KVMState *s, CPUState *cpu) +{ + struct kvm_dirty_gfn *dirty_gfns =3D cpu->kvm_dirty_gfns, *cur; + uint32_t gfn_count =3D s->kvm_dirty_gfn_count; + uint32_t count =3D 0, fetch =3D cpu->kvm_fetch_index; + + assert(dirty_gfns && gfn_count); + trace_kvm_dirty_ring_reap_vcpu(cpu->cpu_index); + + while (true) { + cur =3D &dirty_gfns[fetch % gfn_count]; + if (!dirty_gfn_is_dirtied(cur)) { + break; + } + kvm_dirty_ring_mark_page(s, cur->slot >> 16, cur->slot & 0xffff, + cur->offset); + dirty_gfn_set_collected(cur); + trace_kvm_dirty_ring_page(cpu->cpu_index, fetch, cur->offset); + fetch++; + count++; + } + cpu->kvm_fetch_index =3D fetch; + + return count; +} + +/* Must be with slots_lock held */ +static uint64_t kvm_dirty_ring_reap_locked(KVMState *s) +{ + int ret; + CPUState *cpu; + uint64_t total =3D 0; + int64_t stamp; + + stamp =3D get_clock(); + + CPU_FOREACH(cpu) { + total +=3D kvm_dirty_ring_reap_one(s, cpu); + } + + if (total) { + ret =3D kvm_vm_ioctl(s, KVM_RESET_DIRTY_RINGS); + assert(ret =3D=3D total); + } + + stamp =3D get_clock() - stamp; + + if (total) { + trace_kvm_dirty_ring_reap(total, stamp / 1000); + } + + return total; +} + +/* + * Currently for simplicity, we must hold BQL before calling this. We can + * consider to drop the BQL if we're clear with all the race conditions. + */ +static uint64_t kvm_dirty_ring_reap(KVMState *s) +{ + uint64_t total; + + /* + * We need to lock all kvm slots for all address spaces here, + * because: + * + * (1) We need to mark dirty for dirty bitmaps in multiple slots + * and for tons of pages, so it's better to take the lock here + * once rather than once per page. And more importantly, + * + * (2) We must _NOT_ publish dirty bits to the other threads + * (e.g., the migration thread) via the kvm memory slot dirty + * bitmaps before correctly re-protect those dirtied pages. + * Otherwise we can have potential risk of data corruption if + * the page data is read in the other thread before we do + * reset below. + */ + kvm_slots_lock(); + total =3D kvm_dirty_ring_reap_locked(s); + kvm_slots_unlock(); + + return total; +} + +static void do_kvm_cpu_synchronize_kick(CPUState *cpu, run_on_cpu_data arg) +{ + /* No need to do anything */ +} + +/* + * Kick all vcpus out in a synchronized way. When returned, we + * guarantee that every vcpu has been kicked and at least returned to + * userspace once. + */ +static void kvm_cpu_synchronize_kick_all(void) +{ + CPUState *cpu; + + CPU_FOREACH(cpu) { + run_on_cpu(cpu, do_kvm_cpu_synchronize_kick, RUN_ON_CPU_NULL); + } +} + +/* + * Flush all the existing dirty pages to the KVM slot buffers. When + * this call returns, we guarantee that all the touched dirty pages + * before calling this function have been put into the per-kvmslot + * dirty bitmap. + * + * This function must be called with BQL held. + */ +static void kvm_dirty_ring_flush(void) +{ + trace_kvm_dirty_ring_flush(0); + /* + * The function needs to be serialized. Since this function + * should always be with BQL held, serialization is guaranteed. + * However, let's be sure of it. + */ + assert(qemu_mutex_iothread_locked()); + /* + * First make sure to flush the hardware buffers by kicking all + * vcpus out in a synchronous way. + */ + kvm_cpu_synchronize_kick_all(); + kvm_dirty_ring_reap(kvm_state); + trace_kvm_dirty_ring_flush(1); +} + /** * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space * @@ -1167,7 +1378,24 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, goto out; } if (mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { - kvm_slot_get_dirty_log(kvm_state, mem); + /* + * NOTE: We should be aware of the fact that here we're on= ly + * doing a best effort to sync dirty bits. No matter whet= her + * we're using dirty log or dirty ring, we ignored two fac= ts: + * + * (1) dirty bits can reside in hardware buffers (PML) + * + * (2) after we collected dirty bits here, pages can be di= rtied + * again before we do the final KVM_SET_USER_MEMORY_REGION= to + * remove the slot. + * + * Not easy. Let's cross the fingers until it's fixed. + */ + if (kvm_state->kvm_dirty_ring_enabled) { + kvm_dirty_ring_reap_locked(kvm_state); + } else { + kvm_slot_get_dirty_log(kvm_state, mem); + } kvm_slot_sync_dirty_pages(mem); } =20 @@ -1215,6 +1443,51 @@ out: kvm_slots_unlock(); } =20 +static void *kvm_dirty_ring_reaper_thread(void *data) +{ + KVMState *s =3D data; + struct KVMDirtyRingReaper *r =3D &s->reaper; + + rcu_register_thread(); + + trace_kvm_dirty_ring_reaper("init"); + + while (true) { + r->reaper_state =3D KVM_DIRTY_RING_REAPER_WAIT; + trace_kvm_dirty_ring_reaper("wait"); + /* + * TODO: provide a smarter timeout rather than a constant? + */ + sleep(1); + + trace_kvm_dirty_ring_reaper("wakeup"); + r->reaper_state =3D KVM_DIRTY_RING_REAPER_REAPING; + + qemu_mutex_lock_iothread(); + kvm_dirty_ring_reap(s); + qemu_mutex_unlock_iothread(); + + r->reaper_iteration++; + } + + trace_kvm_dirty_ring_reaper("exit"); + + rcu_unregister_thread(); + + return NULL; +} + +static int kvm_dirty_ring_reaper_init(KVMState *s) +{ + struct KVMDirtyRingReaper *r =3D &s->reaper; + + qemu_thread_create(&r->reaper_thr, "kvm-reaper", + kvm_dirty_ring_reaper_thread, + s, QEMU_THREAD_JOINABLE); + + return 0; +} + static void kvm_region_add(MemoryListener *listener, MemoryRegionSection *section) { @@ -1243,6 +1516,36 @@ static void kvm_log_sync(MemoryListener *listener, kvm_slots_unlock(); } =20 +static void kvm_log_sync_global(MemoryListener *l) +{ + KVMMemoryListener *kml =3D container_of(l, KVMMemoryListener, listener= ); + KVMState *s =3D kvm_state; + KVMSlot *mem; + int i; + + /* Flush all kernel dirty addresses into KVMSlot dirty bitmap */ + kvm_dirty_ring_flush(); + + /* + * TODO: make this faster when nr_slots is big while there are + * only a few used slots (small VMs). + */ + kvm_slots_lock(); + for (i =3D 0; i < s->nr_slots; i++) { + mem =3D &kml->slots[i]; + if (mem->memory_size && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) { + kvm_slot_sync_dirty_pages(mem); + /* + * This is not needed by KVM_GET_DIRTY_LOG because the + * ioctl will unconditionally overwrite the whole region. + * However kvm dirty ring has no such side effect. + */ + kvm_slot_reset_dirty_pages(mem); + } + } + kvm_slots_unlock(); +} + static void kvm_log_clear(MemoryListener *listener, MemoryRegionSection *section) { @@ -1348,10 +1651,15 @@ void kvm_memory_listener_register(KVMState *s, KVMM= emoryListener *kml, kml->listener.region_del =3D kvm_region_del; kml->listener.log_start =3D kvm_log_start; kml->listener.log_stop =3D kvm_log_stop; - kml->listener.log_sync =3D kvm_log_sync; - kml->listener.log_clear =3D kvm_log_clear; kml->listener.priority =3D 10; =20 + if (s->kvm_dirty_ring_enabled) { + kml->listener.log_sync_global =3D kvm_log_sync_global; + } else { + kml->listener.log_sync =3D kvm_log_sync; + kml->listener.log_clear =3D kvm_log_clear; + } + memory_listener_register(&kml->listener, as); =20 for (i =3D 0; i < s->nr_as; ++i) { @@ -2175,6 +2483,12 @@ static int kvm_init(MachineState *ms) * page is wr-protected initially, which is against how kvm dirty ring= is * usage - kvm dirty ring requires all pages are wr-protected at the v= ery * beginning. Enabling this feature for dirty ring causes data corrup= tion. + * + * TODO: Without KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 and kvm clear dirty= log, + * we may expect a higher stall time when starting the migration. In = the + * future we can enable KVM_CLEAR_DIRTY_LOG to work with dirty ring to= o: + * instead of clearing dirty bit, it can be a way to explicitly wr-pro= tect + * guest pages. */ if (!s->kvm_dirty_ring_enabled) { dirty_log_manual_caps =3D @@ -2277,6 +2591,14 @@ static int kvm_init(MachineState *ms) ret =3D ram_block_discard_disable(true); assert(!ret); } + + if (s->kvm_dirty_ring_enabled) { + ret =3D kvm_dirty_ring_reaper_init(s); + if (ret) { + goto err; + } + } + return 0; =20 err: @@ -2589,6 +2911,17 @@ int kvm_cpu_exec(CPUState *cpu) case KVM_EXIT_INTERNAL_ERROR: ret =3D kvm_handle_internal_error(cpu, run); break; + case KVM_EXIT_DIRTY_RING_FULL: + /* + * We shouldn't continue if the dirty ring of this vcpu is + * still full. Got kicked by KVM_RESET_DIRTY_RINGS. + */ + trace_kvm_dirty_ring_full(cpu->cpu_index); + qemu_mutex_lock_iothread(); + kvm_dirty_ring_reap(kvm_state); + qemu_mutex_unlock_iothread(); + ret =3D 0; + break; case KVM_EXIT_SYSTEM_EVENT: switch (run->system_event.type) { case KVM_SYSTEM_EVENT_SHUTDOWN: diff --git a/accel/kvm/trace-events b/accel/kvm/trace-events index e15ae8980d3..72a01320a1a 100644 --- a/accel/kvm/trace-events +++ b/accel/kvm/trace-events @@ -18,4 +18,11 @@ kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint32_t va= l, bool assign, uint32_t kvm_set_user_memory(uint32_t slot, uint32_t flags, uint64_t guest_phys_add= r, uint64_t memory_size, uint64_t userspace_addr, int ret) "Slot#%d flags= =3D0x%x gpa=3D0x%"PRIx64 " size=3D0x%"PRIx64 " ua=3D0x%"PRIx64 " ret=3D%d" kvm_clear_dirty_log(uint32_t slot, uint64_t start, uint32_t size) "slot#%"= PRId32" start 0x%"PRIx64" size 0x%"PRIx32 kvm_resample_fd_notify(int gsi) "gsi %d" +kvm_dirty_ring_full(int id) "vcpu %d" +kvm_dirty_ring_reap_vcpu(int id) "vcpu %d" +kvm_dirty_ring_page(int vcpu, uint32_t slot, uint64_t offset) "vcpu %d fet= ch %"PRIu32" offset 0x%"PRIx64 +kvm_dirty_ring_reaper(const char *s) "%s" +kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages (to= ok %"PRIi64" us)" +kvm_dirty_ring_reaper_kick(const char *reason) "%s" +kvm_dirty_ring_flush(int finished) "%d" =20 diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index c68bc3ba8af..19f4528c841 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -323,6 +323,10 @@ struct qemu_work_item; * @ignore_memory_transaction_failures: Cached copy of the MachineState * flag of the same name: allows the board to suppress calling of the * CPU do_transaction_failed hook function. + * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dir= ty + * ring is enabled. + * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU + * dirty ring structure. * * State of one CPU core or thread. */ @@ -394,9 +398,12 @@ struct CPUState { */ uintptr_t mem_io_pc; =20 + /* Only used in KVM */ int kvm_fd; struct KVMState *kvm_state; struct kvm_run *kvm_run; + struct kvm_dirty_gfn *kvm_dirty_gfns; + uint32_t kvm_fetch_index; =20 /* Used for events with 'vcpu' and *without* the 'disabled' properties= */ DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS); --=20 2.26.2