From nobody Sun Nov 24 02:04:13 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1728060210; cv=none; d=zohomail.com; s=zohoarc; b=WCUhvM3ad6YiSJ/Wh1GacRbFItWCnSQIoFxdxMF2Bs/Z0v1JWIATH8byBSXphdmLwmeRgbyRP/h89Iaq9CKOSMiseEtEUBdZeBITS6wT+xd5yLkSK3wmSTmhEX8Q3J/TaT67WdN6jLmaoU6HlavRIGHgcPTF6zzwYBG/wJ9WORo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1728060210; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=MHCtYjHZqWBXvAFkbnga8RDrSxeZpUx1dCLsimTB34c=; b=nDKFCG1lfgTVdEZcFLENZW23XbgVCTF8HbRYOgXgEqhHA7iRcNobDwTK4nDP/yYndiVnquy06sKKtXmxpcqTcw8aOpe5Yd2GaKh51aS2DTJi6c9jRaVZ7oP2nTIpCEWPsePKBdHdh1UFZfkgxhlxsMGTFTfZGGmYTC0Q74Z0Dkg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1728060210286903.989812535309; Fri, 4 Oct 2024 09:43:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1swlI0-0001VE-9R; Fri, 04 Oct 2024 12:36:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1swlGy-00086k-1u for qemu-devel@nongnu.org; Fri, 04 Oct 2024 12:35:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1swlGw-0006Rc-AF for qemu-devel@nongnu.org; Fri, 04 Oct 2024 12:35:19 -0400 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-104-7bv196j8NrCXbkK40Ji2yw-1; Fri, 04 Oct 2024 12:35:16 -0400 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42f89a68c29so1416195e9.1 for ; Fri, 04 Oct 2024 09:35:16 -0700 (PDT) Received: from avogadro.local ([151.95.43.71]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d1691fff0sm35017f8f.51.2024.10.04.09.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2024 09:35:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728059717; 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=MHCtYjHZqWBXvAFkbnga8RDrSxeZpUx1dCLsimTB34c=; b=WutKUpyHZnlK/GL9QgwYmX9gJLSp5+0Fsho0jUOiWAZx9fuM4DMpAM9qOVuL1IuVVftqme iWaxqsbE4hwOq9RpKkdjDBYX/HvGcG5NcqWhPEdDdA4CsI+LKgFqHoutZgygOAUDuzhoUT 2NUOjf3uU27FuVxvRuo2X8/U5shxUs8= X-MC-Unique: 7bv196j8NrCXbkK40Ji2yw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728059714; x=1728664514; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MHCtYjHZqWBXvAFkbnga8RDrSxeZpUx1dCLsimTB34c=; b=AfWDdyQfE2w/pu/zmtovBsn+yKOsa4KZZfCelEyU4Buwcy6KM5l413Nti3ZP+bAvHG H0ChBt+QMUXmPGq2MQWyHaIi0DwgkZOXV1iWS+NkBPBo7udG7FGQoFEcQlRVOyBAWjVb CcpmXAeNYI6qLX7jSgUHXPV6D6hzrv0rp3NA1WNZSB1MsUvUBjmtMIaogxGNPEDlVUFC /E0RhmzobEVK21WoZng9pYMK5E9wYMTgLF4ORu2i803150zvdfpxEBE2xffCLT104wBs 809/lHHGul69/oTeWYz6OKaB5YYXTy0wC2vffXle9b7McVPiYud9kf+5ZfCMQDj/mvyk qNsQ== X-Gm-Message-State: AOJu0YxOW420yy9Tj6YKZQbSkMoEeHWd/TuM04IySSqv4f3DG2GZlEU+ U7R292sPH8vEozymgoWEMRGxoTi/jg0LT9CvvlnvRMNJXlmPoFKwjouBt6ictybGqIBYiOoqk+o eHw0ksRT0JZujZr+WK0Ycx3Nt9S4S/qOPVFFGas42U+7bbSaQOAK46pGZLXg7CC/g0l/oaObS3u YUHMk9B8jlAr37w+/yGPICQnfVNXUw+18hiMxMbgc= X-Received: by 2002:a5d:5306:0:b0:37c:c842:a16e with SMTP id ffacd0b85a97d-37d0e6f28a5mr2251527f8f.5.1728059714059; Fri, 04 Oct 2024 09:35:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH/Hy6Sdyxc915rkTtKJcvJ5h+axZmR6OEQFFkAHW1qxMFwNhFqm3SYIrvsQ7K7MRn6WoRlOQ== X-Received: by 2002:a5d:5306:0:b0:37c:c842:a16e with SMTP id ffacd0b85a97d-37d0e6f28a5mr2251505f8f.5.1728059713547; Fri, 04 Oct 2024 09:35:13 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Ani Sinha Subject: [PULL 18/23] accel/kvm: refactor dirty ring setup Date: Fri, 4 Oct 2024 18:34:10 +0200 Message-ID: <20241004163415.951106-19-pbonzini@redhat.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241004163415.951106-1-pbonzini@redhat.com> References: <20241004163415.951106-1-pbonzini@redhat.com> MIME-Version: 1.0 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=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.146, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1728060211999116600 Content-Type: text/plain; charset="utf-8" From: Ani Sinha Refactor setting up of dirty ring code in kvm_init() so that is can be reused in the future patchsets. Signed-off-by: Ani Sinha Link: https://lore.kernel.org/r/20240912061838.4501-1-anisinha@redhat.com Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 88 +++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index e06cf3a4157..e57492fda5c 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2435,6 +2435,55 @@ static int find_kvm_machine_type(MachineState *ms) return type; } =20 +static int kvm_setup_dirty_ring(KVMState *s) +{ + uint64_t dirty_log_manual_caps; + int ret; + + /* + * Enable KVM dirty ring if supported, otherwise fall back to + * dirty logging mode + */ + ret =3D kvm_dirty_ring_init(s); + if (ret < 0) { + return ret; + } + + /* + * 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. + * + * 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_size) { + 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; + } + } + } + + return 0; +} + static int kvm_init(MachineState *ms) { MachineClass *mc =3D MACHINE_GET_CLASS(ms); @@ -2454,7 +2503,6 @@ static int kvm_init(MachineState *ms) const KVMCapabilityInfo *missing_cap; int ret; int type; - uint64_t dirty_log_manual_caps; =20 qemu_mutex_init(&kml_slots_lock); =20 @@ -2566,47 +2614,11 @@ 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 - */ - ret =3D kvm_dirty_ring_init(s); + ret =3D kvm_setup_dirty_ring(s); if (ret < 0) { goto err; } =20 - /* - * 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. - * - * 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_size) { - 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; - } - } - } - #ifdef KVM_CAP_VCPU_EVENTS s->vcpu_events =3D kvm_check_extension(s, KVM_CAP_VCPU_EVENTS); #endif --=20 2.46.1