From nobody Fri May 17 04:59:54 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=1620317929; cv=none; d=zohomail.com; s=zohoarc; b=UQCf6Kh9zDXGtnMSr195K123bNdte9TWNokLJeRIu2BbVWAPNDgR16/TZFTSMsVBAMkJmHXhiTIAUf1aK6xKyg66M1hXpVYWxVlysLsG0Ezq48jG8Qgm2zSxd6mwHsCMOzFTM7L4FTe2vDU2dwuHT75mKHxmU1c69nNnP4CS+iA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620317929; 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=+5EH2tQeYOTzoLawqab1A0H8FWNrS+FmcJTYEsoxHII=; b=DXwCIwJRoZK3Ott1N2vLydHnFXClvnaXXD5wxfzjlZ5WRUkRcaaMmT2O2+/y3tbMgAA27dC5uFbO18zMLOZjQwgY0/gVvCNSryQgRvlYY+F8uQ1MJnE+t8hmocr5eNKlYBQS5su8raG2KCXXqG5KHs+7gZMv40ch4o27GKinPRs= 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 1620317929130187.33769381584545; Thu, 6 May 2021 09:18:49 -0700 (PDT) Received: from localhost ([::1]:37590 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legi7-0000hf-Ez for importer@patchew.org; Thu, 06 May 2021 12:18:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59630) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVl-0004B1-7D for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53272) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVi-0003Xd-85 for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:00 -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-263-PFiOQoYNMkyZiR7eHKholw-1; Thu, 06 May 2021 12:05:55 -0400 Received: by mail-qk1-f200.google.com with SMTP id i141-20020a379f930000b02902e94f6d938dso3799202qke.5 for ; Thu, 06 May 2021 09:05:55 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:05:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317157; 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=+5EH2tQeYOTzoLawqab1A0H8FWNrS+FmcJTYEsoxHII=; b=Coe4AFCYJaatj3t3B69V1P0w5QsmLsGbngFHqsedzEnU7cQoZh6+yqS8PhDr4Hgx4VXrpd iRZcb/uRHDh7H2QH9Ez3LBOmKb4yR4VNnzoRfRKbuS/2dKb7xcw/IJPYu4kPwAfKGXON2Y yWgeJnfVRar19xnfkO+M4o7WakzkjqY= X-MC-Unique: PFiOQoYNMkyZiR7eHKholw-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=+5EH2tQeYOTzoLawqab1A0H8FWNrS+FmcJTYEsoxHII=; b=BIvmAWR/ouZvDhVtcxc+AJCfldQ9betseuvoyhKOgEumz2JpWtI8O6oo8pcvWMFi61 S6qpSvKj1zhysSofdC7jZNcRqTNMRLcQW4M96euzKcxHxO2S6T+xLtDuLCHBO9PBqHD+ 33u81M9GNPZxmm5nFyjSI+D7NKZsRIP8UIe7/xfXjLD2K84hTXPw+aNvbSDgCjX+iNtH F+eMjr8lcXlAwHysc6TN98Z2AKfo9gtHD2UN3lDEenXPirr2GSPneRSQhrtYy7Z1B6IC iVgFNA9H1c3WYBsIAC+6lhpIPoS/l027Hm9GAMrVuRX7ByFIm6M1vF6cx4pBtAG4P9CG 2d/w== X-Gm-Message-State: AOAM531Vzo9NjialgX900vYcy6C9RWjO5nVorAmH2Einh3Ke4v9EI075 HRDiucDeuwjmBXurby8DzzYSOar+QBOAiwFcf2BbJ+Y7gJ4oWP+0VEsBqTF0djUatiSfW0jy0yv djfKRfHMSK+dQeKkF+JgbRkrVxEavgO3dw6ABX7YL2FgxghT5vis4WrwxBOx+dNc8 X-Received: by 2002:ac8:5810:: with SMTP id g16mr4827089qtg.135.1620317154490; Thu, 06 May 2021 09:05:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3ytoHf79YW0zAU7xlwv8PyqCiwCuCjEi1h+rFUoLq+8aHxmjgrwbC5tJtSnXwmFkWyR8Zwg== X-Received: by 2002:ac8:5810:: with SMTP id g16mr4827049qtg.135.1620317154093; Thu, 06 May 2021 09:05:54 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 01/10] memory: Introduce log_sync_global() to memory listener Date: Thu, 6 May 2021 12:05:40 -0400 Message-Id: <20210506160549.130416-2-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 3bb533c0bce..c19b0be6b11 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2055,6 +2055,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; @@ -2068,18 +2072,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 @@ -2767,6 +2777,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.31.1 From nobody Fri May 17 04:59:54 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=1620318119; cv=none; d=zohomail.com; s=zohoarc; b=MPcuaKa8q7WQeEFOxfvpltNs7QkzcjnpmEGEYZPIVdleVZE2Yk1BH1zL4wzdxNnm4hnIYGfcfHfqFmI/hqKvYHd78hsudKIeGGI765TwPwvf2e+QgcqCFsh3mOND5aYEQnxWxIbjU0TM7ZGoGqx752GIz88KFw833kEB4twRT+8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318119; 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=OM2XyTzzC86YAJbdG9JEgthtoxhW4weKyjnb13GxWdA=; b=TFTmXrTMuAqGM7U/NRnIdwqkIuPE2Fq9QWuKl7mGFMLB+pVF8/TPDl24ghFdMMBf/7CeUFbNkgZVwVg8ZPw5UxPV7vY2hatR/HbcNylPjX+R2PffKJzOwmY2ennu3K1Msp88/ilbOBSqVEwYXUfXT9SyfiTZQsea4+oaMq5KGMQ= 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 162031811949053.0895079887398; Thu, 6 May 2021 09:21:59 -0700 (PDT) Received: from localhost ([::1]:46180 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1leglB-0004Q8-Fd for importer@patchew.org; Thu, 06 May 2021 12:21:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59678) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVo-0004HU-G2 for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:51547) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVk-0003ZC-Md for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:04 -0400 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-7-VeLatu4UNtehf8mT6Z_Zqg-1; Thu, 06 May 2021 12:05:57 -0400 Received: by mail-qv1-f71.google.com with SMTP id b10-20020a0cf04a0000b02901bda1df3afbso4486679qvl.13 for ; Thu, 06 May 2021 09:05:57 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.05.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:05:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317160; 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=OM2XyTzzC86YAJbdG9JEgthtoxhW4weKyjnb13GxWdA=; b=c+YIc7iAK4uarVMm9NcQgg1IdK9ebfmdgWcVKmoJP4UmFCj1Q6S0ys506eMa479VGTd+OA TfRQK5pdE//1cC5fKO5rN3dnjHW2IPt2aG8LsHG28GAe2OR6IwOz7R/89jS149p4jQktFy kw4P4fQUxEYuCzS69LZHe6/fRL6Tzeg= X-MC-Unique: VeLatu4UNtehf8mT6Z_Zqg-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=OM2XyTzzC86YAJbdG9JEgthtoxhW4weKyjnb13GxWdA=; b=NXS7eeaZhfH6zI3U9bzsnrbPM3/q0zBaPfw4cuDm7vS+l3uJUJPi/0nvDl6xMij9tl R1hWhLcavFl0SPyxp1FOa+7j52qH8qsSKf+5/8uUUG0UU/5lxI8LBdKCwGMuV9M14nBN IErxrrvUu110icC0HPj4spXL8qFuCJA27UAjSv7R6Op0m+gZ5gLqrYqmT5qmmANnh7yI dWzAsIVeHR+tVWtBZAHkk+AZK8hrsLGMO2xV6BCHr/MOEdyvrIOQacp9+kXoQdG89OeV vAD2kusmVrXblHFmoiFMx5+TOokJwAaNmWKLECG9M/ZHPEz5tZDMecQbZhEVIU2hTqzc Bo6g== X-Gm-Message-State: AOAM530CjpGcxH29IQIQ9zNJu90AwkK9NP377n9iR1PUntGoUIuXtB/g xJdbk6NE4AKwK/iOrWwo/PknWu84UP7kH5F19wC6V5Ww5kObwHsPOGkqRKlm2Fk+WVWjsXyBwwX DzfqkVJzgrFxLOwDRRccnNA4uKbmvNRijK0Apq3hCzqX4FZhpVNwXdNTlQl8iTUPF X-Received: by 2002:a37:d205:: with SMTP id f5mr4748302qkj.244.1620317156248; Thu, 06 May 2021 09:05:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+8zGk4G6Q+FN8o8nDTrBAaUi8+Avl3U08N93bZ6ReOcCTBQFwZl3dGtGFVc+/t8dmLgKALg== X-Received: by 2002:a37:d205:: with SMTP id f5mr4748265qkj.244.1620317155903; Thu, 06 May 2021 09:05:55 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 02/10] KVM: Use a big lock to replace per-kml slots_lock Date: Thu, 6 May 2021 12:05:41 -0400 Message-Id: <20210506160549.130416-3-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 4e0168e88b9..d335e1ec380 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -172,8 +172,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) { @@ -239,9 +241,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; } @@ -307,7 +309,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 @@ -317,7 +319,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, voi= d *ram, break; } } - kvm_slots_unlock(kml); + kvm_slots_unlock(); =20 return ret; } @@ -513,7 +515,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); @@ -529,7 +531,7 @@ static int kvm_section_update_flags(KVMMemoryListener *= kml, } =20 out: - kvm_slots_unlock(kml); + kvm_slots_unlock(); return ret; } =20 @@ -810,7 +812,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]; @@ -836,7 +838,7 @@ static int kvm_physical_log_clear(KVMMemoryListener *km= l, } } =20 - kvm_slots_unlock(kml); + kvm_slots_unlock(); =20 return ret; } @@ -1141,7 +1143,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 { @@ -1199,7 +1201,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, @@ -1226,9 +1228,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(); } @@ -1328,7 +1330,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 @@ -2001,6 +2002,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.31.1 From nobody Fri May 17 04:59:54 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=1620318418; cv=none; d=zohomail.com; s=zohoarc; b=YvdD7mroeDw0uvaf05jki9t9fEZsPgwH3BlEh2kpMyb2Zo5W4SsDCAYI79AVkdPIqAaPvK57M5JZdZqYXp79Eq7VuO75Fnr2Id/xRr+ZLXwX4J8Pro6bgW7hch7ZxCyJJ+Jma3zV1QSvBoHmTS7k2h+7RhAxq1BfS4PIwLb5Eow= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318418; 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=1KRsB3dZd2ZuwXeR581le5W9+VnyOI+ccWnxFXDSIGU=; b=AsDwD9fkOnXSljDkRI58hsbupYq3huDly78HinrIkVbOeTf1PAZFzgtZ6Mr7Z54Kh68PDDN3A567gyZ0Pf2KKAVoQfoZUIjPpFROx7gTjZ4eRaF8oa2qQOX52BLd2K2ZaqmfyjA7iFmiCcuqA3K6TpA84f6pRSUfXTLU2mgWgZE= 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 1620318418852551.7575747956726; Thu, 6 May 2021 09:26:58 -0700 (PDT) Received: from localhost ([::1]:54946 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legq1-0008NB-I8 for importer@patchew.org; Thu, 06 May 2021 12:26:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59808) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW1-0004Ts-M0 for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:30629) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVr-0003c7-6k for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:17 -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-492-SeRJ0tDuMiWajvFqLu_uww-1; Thu, 06 May 2021 12:06:02 -0400 Received: by mail-qt1-f199.google.com with SMTP id s4-20020ac85cc40000b02901b59d9c0986so3859663qta.19 for ; Thu, 06 May 2021 09:06:02 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.05.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317165; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1KRsB3dZd2ZuwXeR581le5W9+VnyOI+ccWnxFXDSIGU=; b=aXc9e75bkDWlHcZl3JZVAv6oSb7qcqqeZVbdmA08cZoPLQ+vdc3h4ky7BjKtXs2bWC0DH7 bjMonPz2oaHhy7jsWBsH9bb/0o0X6zZ+b5CfW6gfa0LWNXKf1YOyD2gMvHtqEfyH7Csr6W UDNijdBsfR9zGsWuiSeUlGhcu8d3K2A= X-MC-Unique: SeRJ0tDuMiWajvFqLu_uww-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=1KRsB3dZd2ZuwXeR581le5W9+VnyOI+ccWnxFXDSIGU=; b=fpf672SMWub8LMEHe2t/I5gG/RH4eR50jczRt1/iD8okNplSsND1s/QN0KAsnIGcoP l2BafI1mP9zGui3XYvRKtFWPm9LYTuMIV9USzl/3WSy2TcCoFyL7OV6nJZvsH0okPE/j VU0eud4V5dFe+Beh/DbU/EzUJqgLfSqxcNcAf01VjT8xHTvVEo5xFDkSbLB5OulhLH64 hHqyxb9CbuZKsMS92iwpvrcVcb0StXjVWqptyUgyfmfNbGxt42o2amryCVIoOMkD1RS8 1FCjB/WQjmAI+1EmoRLElAghdozfT9dJN1RxhswEe2OMX2oDPBULDq+PsNrFfxw5RFjQ /aFg== X-Gm-Message-State: AOAM5335bAcz/RAF06nXeJWs0Hv+5bXKRedpAsqy0t3B3ldcjy5195De W058RBnZRlY0vvguoWr3kVtoASeSZ1+TV2Ji3NgvulBxQKBJMDJ1lIe4V1iLtTywSsWw2UGAkMK TB80eyDSH21aj0KuDTB/iEAwsWA0ffbeDPeEyofZwE6dfz5zW/fSIUxIvzia6QsyX X-Received: by 2002:a05:620a:cca:: with SMTP id b10mr4733783qkj.136.1620317159965; Thu, 06 May 2021 09:05:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5oZNS+YKpEWcgNqfNZvKUS0XfC3nDKmLEe7S6G5ZuMEI+nCw7qj5PI7r4fRRT/lbwlXx91A== X-Received: by 2002:a05:620a:cca:: with SMTP id b10mr4733605qkj.136.1620317158129; Thu, 06 May 2021 09:05:58 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 03/10] KVM: Create the KVMSlot dirty bitmap on flag changes Date: Thu, 6 May 2021 12:05:42 -0400 Message-Id: <20210506160549.130416-4-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 d335e1ec380..84272d18553 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -177,6 +177,8 @@ static QemuMutex kml_slots_lock; #define kvm_slots_lock() qemu_mutex_lock(&kml_slots_lock) #define kvm_slots_unlock() qemu_mutex_unlock(&kml_slots_lock) =20 +static void kvm_slot_init_dirty_bitmap(KVMSlot *mem); + static inline void kvm_resample_fd_remove(int gsi) { KVMResampleFd *rfd; @@ -500,6 +502,7 @@ static int kvm_slot_update_flags(KVMMemoryListener *kml= , KVMSlot *mem, return 0; } =20 + kvm_slot_init_dirty_bitmap(mem); return kvm_set_user_memory_region(kml, mem, false); } =20 @@ -584,8 +587,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 @@ -640,11 +647,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); @@ -1181,14 +1183,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.31.1 From nobody Fri May 17 04:59:54 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=1620318028; cv=none; d=zohomail.com; s=zohoarc; b=VbgxB9cxexbvvSFsILlbv/J2H4WULp+ZS19Rcx0WTVisVk14SKwcXRw4EobZcoVR7C63F7vYXin5lAOFa3RsCplNXyrF9ekhz1h7KWNBGoZCbKXjxkkb2N7WTVw2IGfjPeog0hj7NQbRUiLtd4bVZ+oQCglIg3qoMG977ass/r8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318028; 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=DZYX5Y52f1JWYi6nZzE3uGv6I3Ja2831c2co8CAQUwY=; b=QlLo2PUUO84ck2+s0tA8xv8brhpmkBE/mUyx+XDT/KQHM3fXjjnSizhtiJoR6++PIQ1vHBBZ3Efaa4/is/raKUmpRiYMeIQ7t5Hp745tCdqxOEObqD4UD8lm+qdhLon4MtW4svyZFOSf0SydvkhfnRJ6ShyJdqEApNCuic2APDM= 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 1620318028286949.4429067317999; Thu, 6 May 2021 09:20:28 -0700 (PDT) Received: from localhost ([::1]:42538 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legjh-0002li-RH for importer@patchew.org; Thu, 06 May 2021 12:20:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59706) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVr-0004JO-GZ for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:07 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27317) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVo-0003au-0N for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:07 -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-251-Ct342NnWO7amjNR3FPPp-w-1; Thu, 06 May 2021 12:06:01 -0400 Received: by mail-qv1-f69.google.com with SMTP id b10-20020a0cf04a0000b02901bda1df3afbso4486854qvl.13 for ; Thu, 06 May 2021 09:06:01 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:05:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317163; 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=DZYX5Y52f1JWYi6nZzE3uGv6I3Ja2831c2co8CAQUwY=; b=GJEvvLLZ24Lk5H9W8I21Hem2xQiZUnaU4q8Rc83ZbGztQtwsX56AH5GUFgSxIRZ9JpKniS GO69P7tSFCjcbk6lzR+HraYdbBHaG+Lcv8oAAM9lT6NjL4eEF/CMTropSAo8C1NmcJzuLL XIV0M0m/YPcKPG34hlKVJm/slcpFAzk= X-MC-Unique: Ct342NnWO7amjNR3FPPp-w-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=DZYX5Y52f1JWYi6nZzE3uGv6I3Ja2831c2co8CAQUwY=; b=CENZz2uP6LwsuqgylNgFLpmH19egaJA8T8sw2Yur47PzL++Q7e6lvhB9IDM60r6lLu xh+TgH16CdX+lrMGcxYlLIKGcrEca+qjwrf/tGY+3BUZEPl6VEaJUvR6xVh1QYytUDdB Be5owzgz4O0XBtpDy8BanJ4Ul3VxvI2/byZb4cK6cSNQqBD7/alB/MPe/lUnTSh5r8MJ nktDvKliRTiRocVrDTc15wM4YDi+k9Li9xQ7iH+SpPRS9IfML8lkyzuaYxeKXMODsa6O /J6xVCIneJe8gnxxu9fbR0Ve9qp7EmZ6fOPdfifq88wRJ1/pFg7cp3ydj9J1eTzkCHsp 9Iww== X-Gm-Message-State: AOAM531NP8N99Uuv+LWzrqF1dp/hBmfX8g8Zatu8eKqPaGcJDq+7AwEb kYLZ/iFCL9rk4KNRTdDyeoAkOo38dJThzLrQh1u86z7w+aj2jOXgb5QCieVDJYBXjKIa4sn+KYD jOVCSJoecrbAbYVTn1Z7fbDlMOuPWxnCWVKYX9PXiLmn05jio0DDXeUIsJ7f1MTrQ X-Received: by 2002:a05:620a:4043:: with SMTP id i3mr4554778qko.380.1620317160504; Thu, 06 May 2021 09:06:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFNG2svdwxFIyKGWm20slhPETseEoS2bnbCWfgELVWIL8JYIlN4QKzUG36a4i4nPu1VHR2dQ== X-Received: by 2002:a05:620a:4043:: with SMTP id i3mr4554737qko.380.1620317160196; Thu, 06 May 2021 09:06:00 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 04/10] KVM: Provide helper to get kvm dirty log Date: Thu, 6 May 2021 12:05:43 -0400 Message-Id: <20210506160549.130416-5-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 84272d18553..4ed5b4a3631 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -615,6 +615,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 * @@ -626,15 +650,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) { @@ -644,19 +666,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); @@ -666,8 +679,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 */ @@ -1179,6 +1190,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; @@ -1221,14 +1233,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.31.1 From nobody Fri May 17 04:59:54 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=1620317671; cv=none; d=zohomail.com; s=zohoarc; b=cEvRW8VyoZ0RZp4UJeo/CFBtGE/1LzgxeYxVB22kGYxOwz+hrsnmzuzYSJccMkdfcufXLOmfh/6YMM072Qjl5AQfGFw8LANnwDmWWdTj7ARvap2EmQNtnXa9fGKJLvCXbdUPAj8IMdKnTQKFq0G2C7GKZEbPqJOSAxY3IYP3i/U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620317671; 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=VOVx1SRqMzRYQu3cs/ZILn2Fj3r7/l2ieWtt+maJF9Q=; b=ZLv80H7IFHsPOdC+cceKkBVKcWVJF370faHd0BwWbaPU16yrx+xOaFLXk2qmgdbtHQwyw3RsqZm+fnFCIEortBJGI9t9XflnZTfwz5X3fWK6TXKSUReaM9tjiBaVEC1cgUP+lv8Moiy17ZaqCYLdhHr1po3v3tfCJD6i6e5mHHA= 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 1620317671103767.4261132340071; Thu, 6 May 2021 09:14:31 -0700 (PDT) Received: from localhost ([::1]:56336 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legdw-0004X5-V3 for importer@patchew.org; Thu, 06 May 2021 12:14:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59746) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVw-0004LS-Ec for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33242) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVr-0003cB-7S for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:12 -0400 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-431-ch8cywOmNOOSc9CgKH-c2Q-1; Thu, 06 May 2021 12:06:04 -0400 Received: by mail-qt1-f199.google.com with SMTP id o5-20020ac872c50000b02901c32e7e3c21so3879120qtp.8 for ; Thu, 06 May 2021 09:06:03 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317165; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VOVx1SRqMzRYQu3cs/ZILn2Fj3r7/l2ieWtt+maJF9Q=; b=NtxlOmxfXmcxZefsrvnw82LgPjRA8P7rogT/pdzOoSHLR7n5H6odX2IQJb/fOqtJJhfu+B WRx3J8o6i47WaP3+AZIbDfdDZi5tSuvE28pcvSjTd0ecc/HerRFS5IO6pahGMaOLf/Lm+a nv/d9enPp+roepTLv+rsFE/Z2aT4XCI= X-MC-Unique: ch8cywOmNOOSc9CgKH-c2Q-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=VOVx1SRqMzRYQu3cs/ZILn2Fj3r7/l2ieWtt+maJF9Q=; b=dvx4TPeJlpnpmOnl/U+44oU8RUc+szKYLPCaa1VzSb56irbveXX7m9JVZ1J3s0sJGn nfEV7jWFhGSiUb/FZwi5ZPN+unibkul9+Q5Q4lpgaT7rXajlZ/RFuAsp1uFaJP/Pfrzu PJ49eO62yjffnLOrRLE08qQaWu3Po7gFiw4mE/RktfxH1IiKSRjrL8/qbLmnp8SeUzOc g7xbbDewj3aC6m1FAwXzLPCaf230lCxfZFieENM0pvrpr2R3oIQOXVGhssgJPpKsKqlX dCB3Qn6zuuvLkHzNOqZjVBu8vKaQG112J2l4NKhTklSqD3fOwImsvs19ZxcaVwe7AZM9 rxrA== X-Gm-Message-State: AOAM532g6qK5S6N4kTfqKj7UymeyyhRwLIzh4/iZ+NIcCg2zlNttN9jA wHsc2YtvJmalF29z2RzKa0VGzQt1yKpt9t0VooJ3pUDAf94x2e2Ek2LXR1nTEwXfrjaxK/M2Nl1 FLYbkqz/bCXMIIo/0Hee1hy5PwX3MXAsZFFIyIJ4okO9F+4eYZ8sc1uAVxz14UlWr X-Received: by 2002:ae9:ddc2:: with SMTP id r185mr1855157qkf.85.1620317162180; Thu, 06 May 2021 09:06:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9QmNPbSw5E0PNe91dk5j7h8EPei2Rqeu4+vHchRWjfFgHq1g8tpWcnuMaCnRfVMYrm+HmJQ== X-Received: by 2002:ae9:ddc2:: with SMTP id r185mr1855106qkf.85.1620317161864; Thu, 06 May 2021 09:06:01 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 05/10] KVM: Provide helper to sync dirty bitmap from slot to ramblock Date: Thu, 6 May 2021 12:05:44 -0400 Message-Id: <20210506160549.130416-6-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 4ed5b4a3631..1025701f557 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -573,15 +573,12 @@ static void kvm_log_stop(MemoryListener *listener, } =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)) @@ -656,26 +653,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; } @@ -1134,7 +1124,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)) { @@ -1152,9 +1143,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 @@ -1193,6 +1188,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); @@ -1203,6 +1199,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.31.1 From nobody Fri May 17 04:59:54 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=1620318264; cv=none; d=zohomail.com; s=zohoarc; b=m+vaURwvJQSuMP83KjPqvJAIFO8Hrb02DOaQ+51kPeYV8RzSzW2jI44RMA3N6xj5VpXIFUYwVO4S8zBaqNIxVKx2vFw3x5ZXBWv9Pavz37MkflmNhueCxQv2J6EO4K966c/JNi/+GupuHyjFOkAhm/K4g1Vnckg7sS1yNr8lFu4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318264; 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=4rCrAN+h0wgJgjBJic1rm3cOtCEsNSz3UJTGf5unUhs=; b=nR46IH/nAUm6fOB/a4T7/s5LQHdVBDw2JuE5ewzUZVGmpAtT83nJCzYRJFw5KAoYx2vo3Y5ts7pGWj8zQL6YyUoLJq1qr9bqUMZ02ICYQmcpaMSz3Ugj7mbB31Qu9rjB96+faEyS8J0heZN9dI6CnT/uSSl0+8PC1S3mBhyqj30= 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 1620318264940940.0991740487975; Thu, 6 May 2021 09:24:24 -0700 (PDT) Received: from localhost ([::1]:51058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legnX-0006Pd-7Z for importer@patchew.org; Thu, 06 May 2021 12:24:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59812) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW1-0004Tt-PB for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:36081) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVt-0003d8-EA for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:17 -0400 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-295-hfXF7RSLMQOFRSa_3WJWtQ-1; Thu, 06 May 2021 12:06:06 -0400 Received: by mail-qt1-f200.google.com with SMTP id h4-20020ac858440000b029019d657b9f21so3869645qth.9 for ; Thu, 06 May 2021 09:06:06 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317168; 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=4rCrAN+h0wgJgjBJic1rm3cOtCEsNSz3UJTGf5unUhs=; b=RBIafxBSWjKLObUUVFRzvSIylGYfRNAoH6Tb3JxlnTj+br19lHYrIA/X6fEJy4DxmfXIHB /YCIVAfEStBcMT2Jeq98bjakyqu0ARLdS2OKRiQdaUgXHn87nnScuxqFwLXs3QwAUF34LC lFFnpDjv06rT9AxMzpYSJmXNIyZBbxM= X-MC-Unique: hfXF7RSLMQOFRSa_3WJWtQ-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=4rCrAN+h0wgJgjBJic1rm3cOtCEsNSz3UJTGf5unUhs=; b=gCFhpKVYWmGSj1zRLjTZHiy+n7Qbd7xbZo7MPUw5xgI033HYhWa3VwUL1Q2qFi2B62 9S00Stk9u3efiJsBbM1h74FzoEKhU6tBAsHNNccuO2Yr918pOHWE4Kz9utsy9opWpKcb BRv1q59/zSSxeVwZuzblL2T9MfAX5MoHr7OtbKaNMpJOJa5XdftywEDGjg0W/kPcKzzc +joQ7OmCxJKUlvDlmx3EGDMEnYA36URPXXUKI5FvYIXPq0Fm1B3ZkSGgzZFxQ1VhBgwD blgtzwqnAHPpy7Dpiha3EzfXH9T35B2KXipqym2jp8AdzlQ/VKcbLImA8eVW7BhHdOYM Jzfw== X-Gm-Message-State: AOAM533J7Bv8NBZUDA0wsMm347xa3KxlmO8SJ23IbqeQmafw7B6RRmyP QHHRtfd1DayqZUBWT9S/e6XIdB+a9tMYoAA7o07gDN+/Dkqar5bbNZYGPqZdcI0E1/euVMckVn7 tUQmBY4DhePlIpNNkb4DaDlNqcrrjj7/aa6s7lNObfuKxOKJl+HtH15i0ZIyftOWz X-Received: by 2002:a37:a604:: with SMTP id p4mr4605255qke.215.1620317165090; Thu, 06 May 2021 09:06:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxs8CIHKxm4yPk38+sq3ELsDBvLBk7WBncz0U4cuUYJexUUR0+qcEBWlUnw64Uh414BksFqtA== X-Received: by 2002:a37:a604:: with SMTP id p4mr4605191qke.215.1620317164493; Thu, 06 May 2021 09:06:04 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 06/10] KVM: Simplify dirty log sync in kvm_set_phys_mem Date: Thu, 6 May 2021 12:05:45 -0400 Message-Id: <20210506160549.130416-7-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 1025701f557..4b786240a04 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -1161,7 +1161,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.31.1 From nobody Fri May 17 04:59:54 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=1620318820; cv=none; d=zohomail.com; s=zohoarc; b=nJGZKzEUXIoOWLIZVHKC/0Qw5CJH8B1VJwnc02GexwAMTDJHX65oAWlUOv9dby0lVkadZY6nygIiUDgNzIxdxUQVYItHmqF+2QCLIHo5d+3ZtRy12N9jW+3ZnsByJuAalw6ArCqJm5SRhli3Xt5TSv2btFZJFtDHGpKjnBMas+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318820; 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=3hEMDIt72dK79rkwjKZwzBlNWlqk9Cujj6vHcXwHvIY=; b=hHLlY6/rappnJlEy03LvnCoYdP8vyGqmlmusCXvPjJwYHaQndvkcyrT/pLuCwHAadCgiUg9HWlnB0b98BAGHj0ZI8hqBsbLzOyekZrNEGotRWUCMn60SZrrLjv3OCUPK/dNrtWVPuiOg10jva6C54e4zZu24HYepyyeT45Y5wJc= 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 1620318820537744.8039047127803; Thu, 6 May 2021 09:33:40 -0700 (PDT) Received: from localhost ([::1]:40208 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legwV-0005g6-74 for importer@patchew.org; Thu, 06 May 2021 12:33:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59848) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW5-0004Xf-9s for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:43552) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVz-0003eS-Nj for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:21 -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-283-MnXuZ3lDOqWJqiGoLrebYA-1; Thu, 06 May 2021 12:06:13 -0400 Received: by mail-qk1-f200.google.com with SMTP id e4-20020a37b5040000b02902df9a0070efso3783726qkf.18 for ; Thu, 06 May 2021 09:06:13 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317175; 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=3hEMDIt72dK79rkwjKZwzBlNWlqk9Cujj6vHcXwHvIY=; b=fdFG8E8cOJQUyxY/lh3iKl9rI2PbtiyU3ToFyV9sNTZ+XXZTVQxYfoyB/5RJKOq0KfKUUj H/C1wedU9thZxGZdUpwjy6gDhxYqf0v6MB2VNYwE40ogZrgrUNy7P57sWT8/7P0O2KNIV0 GPvb5mF7NZt0ZAUg4fzJscY9x+MIbnI= X-MC-Unique: MnXuZ3lDOqWJqiGoLrebYA-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=3hEMDIt72dK79rkwjKZwzBlNWlqk9Cujj6vHcXwHvIY=; b=ljpMv8GR4bGy1LyIXDWX4zTSMb5wcS1wFZM8hu7QlzXy7Wb/Z8FWT+g8Mer2plaH+d CLxzoE7iXpON96xskJ7to1E9126C5Oh2L6RN1BYgfBYZ/us2lkwVE2vI28AC9bMeqBPL Hk0cqUfwzzj69tcvgyh8xTCtnQDp7hLoRag1GH/ghp+XqgFBSH5AA3GwgNUpcTzVxONr jQeeE/gzGLh0KJnhbbUShNLuCBpppcj/bs4R74YUM6t3Z9wZ/VxUHuZVLAeL4Wn/m4V+ 4JBr1CQRT8jwKMhDlVBE0J9NW5p4t25Bo+DDLFv2bNWmiv7boQ0hQ/oFZn5TBuHmE6sJ WarA== X-Gm-Message-State: AOAM531d+T2MIVzzaZ2zsaAOug+QJBAnJFkZszzj2NHUojuUcq8WbX3M jZVBzgAvppLSwwaXYtHcH8HHcnYTwRG9UJXIsJ26tXRYEvMb1TNg+VanwpufGPAzoSazE11H5Hj Xus5uRugCc7imvESbbQtpmZB++O+nGEYizQmSfoZECCj5ceNd47VrlUS09/Krt694 X-Received: by 2002:a05:620a:cca:: with SMTP id b10mr4734802qkj.136.1620317171540; Thu, 06 May 2021 09:06:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxh+pWGMI9EBgVfGYEA/xDFXXiDokXkmQ5vyNU0pyVmtKfkF6JEhkQZN3plI4F7aLL1RwmOlQ== X-Received: by 2002:a05:620a:cca:: with SMTP id b10mr4734369qkj.136.1620317166825; Thu, 06 May 2021 09:06:06 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 07/10] KVM: Cache kvm slot dirty bitmap size Date: Thu, 6 May 2021 12:05:46 -0400 Message-Id: <20210506160549.130416-8-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 4b786240a04..4af61734e37 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -610,6 +610,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.31.1 From nobody Fri May 17 04:59:54 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=1620318522; cv=none; d=zohomail.com; s=zohoarc; b=fYf8Q4g+EnYAd6sjr1HPCC8zQyG3rXw8OeLvbtWnZFkW0XZbAZQ9gGAPqk4mkezZnmT/25GG2z3rhNZMPac36yfxMclXR4kSmHd1nAg69zfxhpI2nwxa4RpRb66ZgaXYa2ez2O3Q5tjj2xtVTE6Ac84bXdrRRqtMti5KNDCGWTo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318522; 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=XD6i3dbssdJ2dHzVQAQl0vHdXYxv6CZKwIRpTh9uJlg=; b=KJQslwNBeDNyJ/LVi3p2aD3ovf3p+C3lEJPazVtJ8VJPRV3muBt2Z3qi4Rqmr5kXlNolskuYXogIwvzjMXtlcuJiSH9gJXnueyEad/qvGg8zsnagjc74FPrV3bh4ugw5MF96l5NzimnlRY2HfXk5AcsBFkLJCMjeej1eEFHww6o= 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 1620318522038181.1388133631848; Thu, 6 May 2021 09:28:42 -0700 (PDT) Received: from localhost ([::1]:59818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legrg-0001x7-RO for importer@patchew.org; Thu, 06 May 2021 12:28:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59838) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW3-0004Uc-UR for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35605) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legVx-0003e6-GQ for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:19 -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-376-izZIXL02PzauZDdBzq5trg-1; Thu, 06 May 2021 12:06:11 -0400 Received: by mail-qv1-f69.google.com with SMTP id b10-20020a0cf04a0000b02901bda1df3afbso4487303qvl.13 for ; Thu, 06 May 2021 09:06:11 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317172; 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=XD6i3dbssdJ2dHzVQAQl0vHdXYxv6CZKwIRpTh9uJlg=; b=B8FmuQEV9DdkP0K9BlPnW18EFQmAkdMh3/WM8ZTodPpIXaLv/F5F0soP5dPyZEHhn/wA9f W/s+SnNK/y1UqxWUXu/5NMJDUHa4VGh7W3fQP5tdj0jxnr37pVYugrPyPDRpZBH/TFrfjG 6KpkwUKHaDmY4JXBGBeGABx7iO+1HKE= X-MC-Unique: izZIXL02PzauZDdBzq5trg-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=XD6i3dbssdJ2dHzVQAQl0vHdXYxv6CZKwIRpTh9uJlg=; b=saXfuDwJBvacT8rWte/9/XJSwhKhcw4xzTAAfau5Bp5i9mrSlrprIhTI8MGkcKCrYP SMQFOvXFAogMvgb8dfSyKtBMZcX0W19FZZx6K6RAgXmf0zl8eKKDNFoOhux1zFDCKjG4 rVnnZ74aQ6u2VSLVrdOfZSToWfYNSghpml3N5vvG+rCrZ4cG6qoOj5Gdbb2XIvD0V3D4 AYD2ENzTzLlVYm81BuCOdP6oTWIgB6FnFNdNxB077T811k8YRGCnDwz4qqejfNwlrBxm ZHtk8JtnrP7omM5A2+nTvGPj09BDg0sgvr8qoTVb0BYgSZPwn3EC56UN5m4I8o86fD4I Htsw== X-Gm-Message-State: AOAM530A/HTsY0CaYHN7AjhJlK8JBbwMNYnVcZ7AgVLL+/JpzQ92crd5 oyhf66NzSquVrayF6LlxD9L2mK18x8To2GKkTCMEVePEGRruKpKLef3vjuNkJUY22WWmPN++jk4 3heLYgzkcDbhwgQhuGd+ti1oOpC9uSEqMClpZ43kkLVRO0D92fNc2syPzcG0I7Z9d X-Received: by 2002:a05:620a:12b1:: with SMTP id x17mr4735728qki.362.1620317170136; Thu, 06 May 2021 09:06:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwt1RurjpJEpHLksuB2DV1aBR8tTr+FPU7QmIJPgfecdgtgnNqFI6bAMIxCEmtrYCW3Vqne6g== X-Received: by 2002:a05:620a:12b1:: with SMTP id x17mr4735683qki.362.1620317169764; Thu, 06 May 2021 09:06:09 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 08/10] KVM: Add dirty-gfn-count property Date: Thu, 6 May 2021 12:05:47 -0400 Message-Id: <20210506160549.130416-9-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 4af61734e37..e438e5d26ad 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -126,6 +126,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; @@ -2130,6 +2133,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 | @@ -3173,6 +3210,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); @@ -3180,6 +3244,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) @@ -3201,6 +3267,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 635dc8a6243..e40c17fb761 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.31.1 From nobody Fri May 17 04:59:54 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=1620319023; cv=none; d=zohomail.com; s=zohoarc; b=XktLuggcB40GXbsprpJgfzbtpakMUXQEpkkzSZkPwVVJdqLxpsW+SVFfc/DHcLflV7dozI0PzmEXkd0BYleVPQ9hRluSFUbLrC3orU9JeZTvYSMffpjlhH6uiu9kxoYGg3W/uUPbitLmXK6mPsQC48WlCEbXuAB+dP6myNL9f5Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620319023; 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=H1vFXx+gF0iNExiOY7cdBT8+TWNFWXHTAf1t0sfNuhI=; b=JQ+Uo6Bk2t3aGmNDlWvfZZYmQCCWk3+QjYDwLuJ9MIEheysWhVOKGSdqU49MaRw7dzDX+fx4Puztm8IwUKd5/Lg504BatjlGjRngSnIFxbwQhJhU2egNoIp4eb4nq1Nzjs6JZtLcb0ssJaPulpHg15EhmUvV5rltZ0OW1vcHLOc= 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 162031902345395.4344413303586; Thu, 6 May 2021 09:37:03 -0700 (PDT) Received: from localhost ([::1]:49006 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1legzl-00016E-Ts for importer@patchew.org; Thu, 06 May 2021 12:37:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59856) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW6-0004Yw-0C for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35743) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW1-0003fK-Fk for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:21 -0400 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-432-TiYWgXnSOCON3rGPDdXq4g-1; Thu, 06 May 2021 12:06:14 -0400 Received: by mail-qv1-f70.google.com with SMTP id a18-20020a0cca920000b02901d3c6996bb7so4497219qvk.6 for ; Thu, 06 May 2021 09:06:13 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317176; 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=H1vFXx+gF0iNExiOY7cdBT8+TWNFWXHTAf1t0sfNuhI=; b=Npeo6kPngg8lP5b5fMp4LTUSDfMi3R+UPyXyFXvydbKYd/Y3wNPgxKy65nbu4sef0yl1AJ Qp7F3SrMMKFKuHADONs8napXk3hjCxmCNdDBowDV3XsP96sUndN3ZgdTaQsAIK10vpmsSy juRNuhu1fwwLz3g1O4YQGWCHTxktD2w= X-MC-Unique: TiYWgXnSOCON3rGPDdXq4g-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=H1vFXx+gF0iNExiOY7cdBT8+TWNFWXHTAf1t0sfNuhI=; b=TPqE9VgFlXN6jTgE/vP4k5A8ltTNMVV8LpT19ngtDbJ4UV5TaG6whenDgIjxyRUxFl gcEW1xx/RjlKpN/22+oawas+OFNQP6UccrvpOMRCxhm77LBw0iqHHDsQpLzgcXaTItLT CRFsUQPCqzlZ1NPQxMM2wzMlUE41ypVaNEWJ43sqLFwVA7lTrmENH1+ZMGyJyVOoIthV lEgBnhLOfCEf5E1HVeM47JROdytAsFYFJ+ppNPy04gHp6TWiPLb/UevF+ksI7ng+jj+B +Qfb4Q7TMbQh5ED7HMij2T6sjttFUIEnKdwl7kdzmon19kxfxN+ahK7KXCv3Cgk307Kd 9Lig== X-Gm-Message-State: AOAM533lYiVRBnJLE6TYrXc1/jCZd3tt0GXKTmHBp+kkyOBs6uMQt+ui pOdrKF/yJXXO9uuT1w/+Tj93aRDHYiWAlJAfT1ASMBN2LDfo68jNhyhtSe1pSo+4HIWnrZ2D9o5 KT6M65WuOcHRITdUE0SODYAbkH4GQRVa0ATePKKq1ABB5ovzza9yRBWlhDjoGKvCT X-Received: by 2002:a0c:d6cc:: with SMTP id l12mr5426492qvi.47.1620317172389; Thu, 06 May 2021 09:06:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhCc5fPz+AJvb9qn+/+oJxVVEVfVb99vKQ3TCoVOaTwbeuowOvTwM5euzqSfc/9CloJHIy4Q== X-Received: by 2002:a0c:d6cc:: with SMTP id l12mr5426458qvi.47.1620317172126; Thu, 06 May 2021 09:06:12 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 09/10] KVM: Disable manual dirty log when dirty ring enabled Date: Thu, 6 May 2021 12:05:48 -0400 Message-Id: <20210506160549.130416-10-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 e438e5d26ad..2404c941784 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2167,20 +2167,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.31.1 From nobody Fri May 17 04:59:54 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=1620318679; cv=none; d=zohomail.com; s=zohoarc; b=hYR9KUznoacazb70E/koAIi6Jk23YSWw6EiF+ulEFxXF3o3QQ+MmZh/Q/OV1NZJciDNJ3Muq6nzLDL96vrPrP//drZWXn+Ipgq3cT8Rn7ZbTs/H4RPktqQg69eAhYCJK+fgBugGg5oT6qgXU+H4yYXGjGxGT80tuEatYI75pVVI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620318679; 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=aVh0xAoOOMX/NGM3qPIyw+U3h4B6uYQ5rF/MtNkkk4g=; b=gA6Xlg6bAxTgUmLMBuUqyP2vs59HnvIMtqGXen3laiCIHZoOAkUX1Pjf5jsZ6eeumDMKagKR86fduxheMYtktsvP7L/kht8o61XqtWqgw3Ukyuhz8ybNiEuXgw4UWWi+htqsqraih68KaJp+R+bYG48Vh6bT3Nk8SfcDkq3tpP8= 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 1620318679668726.989753873752; Thu, 6 May 2021 09:31:19 -0700 (PDT) Received: from localhost ([::1]:35396 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1leguE-0003X3-1I for importer@patchew.org; Thu, 06 May 2021 12:31:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59858) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW6-0004bB-QI for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59977) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1legW3-0003fY-SH for qemu-devel@nongnu.org; Thu, 06 May 2021 12:06:22 -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-311-hLw0z3VENEePhv8TeMtRiA-1; Thu, 06 May 2021 12:06:16 -0400 Received: by mail-qv1-f72.google.com with SMTP id a18-20020a0cca920000b02901d3c6996bb7so4497356qvk.6 for ; Thu, 06 May 2021 09:06:16 -0700 (PDT) Received: from t490s.redhat.com (bras-base-toroon474qw-grc-72-184-145-4-219.dsl.bell.ca. [184.145.4.219]) by smtp.gmail.com with ESMTPSA id y84sm2194131qkb.134.2021.05.06.09.06.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 09:06:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620317177; 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=aVh0xAoOOMX/NGM3qPIyw+U3h4B6uYQ5rF/MtNkkk4g=; b=OpqO5utFU1DtQt2vXegswBHgVjU+FcgmD75eV2VkqvSJWyFqYTJ29K3msJGjhzTyw+0e23 9IjczV5FePGKtM9aMmuPo7nRl5V+IRKJSD/D4TqW7vLzjv6woEL4Qgf82jtq+VOocPVAw8 u/PqrTcssosjBRHHsCSGF7dDRMWSn+s= X-MC-Unique: hLw0z3VENEePhv8TeMtRiA-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=aVh0xAoOOMX/NGM3qPIyw+U3h4B6uYQ5rF/MtNkkk4g=; b=ivR2KMrqdYC3uhumwpBXThAzINQAPYh5jvRma389QgCxtgepBUZtcuAO4V/HKqQWc0 NLZ+1kINXIb+hhg/Ajo816A5aLSXgpW/Jzwbq/zXe8X8/WL55/bDHJ2A95Qk6JsAz/Ku KBk99P3Dv27+DC5pMd5I1QOOTX3akVKYD+m2nefGRfPFwL4Hf3s4z8OMouw0t3/m3WAM Bp1r6F+xdeZXaqD2oPdCZ58gRKAqFDzvnYMJPhpzayDpRfOpVV7li5emzwjDy8hLviPL ztKRgTrcLnygwAonoIIfxUw9OfUhWmfwFogMfAMGrTS9nqiQmX6Ma+LJoG0bo/qcQKkJ Hf1Q== X-Gm-Message-State: AOAM533DaDOSOthAk9HtQGMfNNvP6e0+vfhSsaxKaLkEQe75tSVOP4lS 01QN6EQOT+uBojB+Q9sJ3X0Vqxsk4mN6VPilsn/dkHML9HYqZnt1AWYYI6lyZHwv/5BA1Dhe9FV ioWhbp1DMMNYfWmOiK0XbdCKpW3CzLJkG0b3/nOV7ik4KCrMcGj5UBBCWOUurRM3b X-Received: by 2002:ad4:4dc8:: with SMTP id cw8mr5013301qvb.16.1620317174533; Thu, 06 May 2021 09:06:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYertuHb3u4xdt1Wkyb7WUqgGdA1Fhirn7FKintJSZUAl2+qvezb7j2kFdOwNJyvrzHPWs8g== X-Received: by 2002:ad4:4dc8:: with SMTP id cw8mr5013239qvb.16.1620317173923; Thu, 06 May 2021 09:06:13 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v7 10/10] KVM: Dirty ring support Date: Thu, 6 May 2021 12:05:49 -0400 Message-Id: <20210506160549.130416-11-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210506160549.130416-1-peterx@redhat.com> References: <20210506160549.130416-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.69, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Keqian Zhu , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+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 2404c941784..fd8ce2e0b2c 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 @@ -78,6 +79,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; @@ -129,6 +149,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; @@ -390,6 +411,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; @@ -466,6 +494,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, @@ -584,6 +625,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 */ @@ -640,6 +686,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 * @@ -1165,7 +1376,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 @@ -1213,6 +1441,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) { @@ -1241,6 +1514,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) { @@ -1346,10 +1649,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) { @@ -2173,6 +2481,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 @@ -2275,6 +2589,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: @@ -2587,6 +2909,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.31.1