From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912364745543.9822535129363; Wed, 5 Feb 2020 06:19:24 -0800 (PST) Received: from localhost ([::1]:47906 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLWV-0002rD-BM for importer@patchew.org; Wed, 05 Feb 2020 09:19:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46229) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLV9-0001HQ-IE for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLV8-0003kA-JP for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:17:59 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:20491 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLV8-0003iH-FF for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:17:58 -0500 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-298-IZdwwqIkNUaD9xpu-mq9KA-1; Wed, 05 Feb 2020 09:17:56 -0500 Received: by mail-qt1-f199.google.com with SMTP id a13so1419635qtp.8 for ; Wed, 05 Feb 2020 06:17:56 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.17.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:17:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912278; 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=gwnOjzlxPiLrUtXS9/Js+Kwd14a7kfPZ/UhB56CISFs=; b=HoHelwjghXicfJkw8nVdth8YmzH0952GVIDH6q+rIjz+D4N4ChyHbs1Fib41w99w78/cE7 SxCiHTGhgIPKfvUWmkJT3uUaEJTat3ebTjdx8fhorXmQkQlxyiiogi/yg7NgbQgfC873WH 8PcqYleCDxWqeGwyXPt2aTQxCbE/Va0= 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=vPwF7koEOtFK/v1GMHKbe9mr0XFSrZCmlysXssYQcTU=; b=uEieijiNqNzvwnhkV57EhPPceZYShI/WwYKNFb3FsmqsSz14ZGWFWkLUjEI43nI60k TlixF93JwUfSLU7MtRMv3KWWu6bl8Cvg1GviMpZslJAZi3G5Pj/hw/tsbmamOjguhDND pIVlhjsY696jOwRP2+12g21HIjnqQNMl9y2Y/nZHUmQVJq6mSwwzVxiBq5aYZycEoXtX EcrSNhhWUR10tJ5Ra6St1w4oF+lhGRHfneBXvIfm6zZTpbhTLYuEfeJtp8quPDnVwZEq H5qG5y3OqnlL8TeF/bEuVRcDtjibusEVvw5+kbfoX/A9kMb0FHbUVeVcZqCwEusVZ7jz 3GMQ== X-Gm-Message-State: APjAAAWzqjSYftJZrHU76C61/5lO3JDEfJu8PkfIHsTfEmexszhfKvT4 pZPWEdh6jS6wF4wpuLP3ZrUb7VogCCMBoa4iHsR0OwPRMn0acvjhxzH4dW+KlezEFqVl882Erkc OwdCemSwosKgbOis= X-Received: by 2002:a37:bfc5:: with SMTP id p188mr30637316qkf.283.1580912275968; Wed, 05 Feb 2020 06:17:55 -0800 (PST) X-Google-Smtp-Source: APXvYqyd9oiXw+azTzKyugkFI9dxzMAUXdBlv4TSa5kfh+LkumO2XqNNYevrYWNKCPVxWMhOJJKjHg== X-Received: by 2002:a37:bfc5:: with SMTP id p188mr30637294qkf.283.1580912275744; Wed, 05 Feb 2020 06:17:55 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 1/9] KVM: Fixup kvm_log_clear_one_slot() ioctl return check Date: Wed, 5 Feb 2020 09:17:41 -0500 Message-Id: <20200205141749.378044-2-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: IZdwwqIkNUaD9xpu-mq9KA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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_vm_ioctl() handles the errno trick already for ioctl() on returning -1 for errors. Fix this. Signed-off-by: Peter Xu Reviewed-by: Dr. David Alan Gilbert --- accel/kvm/kvm-all.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index c111312dfd..4be3cd2352 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -688,14 +688,13 @@ static int kvm_log_clear_one_slot(KVMSlot *mem, int a= s_id, uint64_t start, d.num_pages =3D bmap_npages; d.slot =3D mem->slot | (as_id << 16); =20 - if (kvm_vm_ioctl(s, KVM_CLEAR_DIRTY_LOG, &d) =3D=3D -1) { - ret =3D -errno; + ret =3D kvm_vm_ioctl(s, KVM_CLEAR_DIRTY_LOG, &d); + if (ret) { error_report("%s: KVM_CLEAR_DIRTY_LOG failed, slot=3D%d, " "start=3D0x%"PRIx64", size=3D0x%"PRIx32", errno=3D%d", __func__, d.slot, (uint64_t)d.first_page, (uint32_t)d.num_pages, ret); } else { - ret =3D 0; trace_kvm_clear_dirty_log(d.slot, d.first_page, d.num_pages); } =20 --=20 2.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912376224237.5923936896313; Wed, 5 Feb 2020 06:19:36 -0800 (PST) Received: from localhost ([::1]:47912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLWg-0003NA-UZ for importer@patchew.org; Wed, 05 Feb 2020 09:19:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46294) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVB-0001Jv-Oi for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVA-0003zP-MF for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:01 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:57376 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVA-0003v5-HD for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:00 -0500 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-112-48hf_5G-Otm8bQCOPFgzCg-1; Wed, 05 Feb 2020 09:17:58 -0500 Received: by mail-qv1-f70.google.com with SMTP id cn2so1577977qvb.1 for ; Wed, 05 Feb 2020 06:17:58 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:17:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912279; 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=W5N3viiIpDvgmz5NQ3C0n0h5DAYjZJzdSGyqyhpF2Hg=; b=TPVjmunvALcso55hbhl5ErXYhLlXdXXMxFdjHE93T120q7FA4YepyJRnLaxr+HxxMZBD2O XGURr+aKcTOZVjHtDfz6kG2cD+6pdV9PMtIbwW1sUG124RS8b72GcEPyIzlnKjzVVft/0o WnhMTBCTAawelKc/OU7JgVYK1VEATtc= 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=gXkPt6N6Af8+FqxhOe2WmGrZCOt+N/6RyewFCHsP9v8=; b=oGEY15sAswAnQ+q3ChZwjLwGPhAvbOFjAGMOcDqk7MfWZ4UgmjieloDvlNmEHxTLl5 lTfYdCl77JAGkS/vZ4G4beNdG8DtxB5+Q4Q+7r1I46mol/kvKmaXNOnHXNFYdM4g3boZ BLQ6X6dss1vvQhpiUQeH3MbygDcEFqe2LR4fc5swDJIGDzMvv1IJ/z05SOysOVRu7/xp PvHD4+QOo/KeJ234QUP3vPYSSHCki1dEDIVK0RYpE4FzYvc6Wp0FNDcvcUC1+5JTbAgK NfMpCcrAHyBx2HaK3Y8MFpDLQ+4efLmPnCoCfHtNDyF4ucjZZEqcaw/3ZmLK4ay5Va+a tnlA== X-Gm-Message-State: APjAAAV5i8uHMwhmFUDbva2ob58lGQdJ+seMy6Os5FbT3tG0m7/j5Qjt kajE+rdFIQP6gdXVJdeGbNVx0Lzf7v6/uKAlucrtXLkAfvca5U0bLreMjp4d4wIo1WD+sUANWWv MQ0iffdxjTmG/u3A= X-Received: by 2002:a37:9e09:: with SMTP id h9mr32940195qke.289.1580912277657; Wed, 05 Feb 2020 06:17:57 -0800 (PST) X-Google-Smtp-Source: APXvYqzTV29qcKFHl49Yf2MzK5ByW+swLFvny2QzaMu3Ejx6PF1SvGk1dyxb616O8LbulOY3tjzWCw== X-Received: by 2002:a37:9e09:: with SMTP id h9mr32940177qke.289.1580912277419; Wed, 05 Feb 2020 06:17:57 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 2/9] linux-headers: Update Date: Wed, 5 Feb 2020 09:17:42 -0500 Message-Id: <20200205141749.378044-3-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: 48hf_5G-Otm8bQCOPFgzCg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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" Signed-off-by: Peter Xu --- linux-headers/asm-x86/kvm.h | 1 + linux-headers/linux/kvm.h | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/linux-headers/asm-x86/kvm.h b/linux-headers/asm-x86/kvm.h index 503d3f42da..b59bf356c4 100644 --- a/linux-headers/asm-x86/kvm.h +++ b/linux-headers/asm-x86/kvm.h @@ -12,6 +12,7 @@ =20 #define KVM_PIO_PAGE_OFFSET 1 #define KVM_COALESCED_MMIO_PAGE_OFFSET 2 +#define KVM_DIRTY_LOG_PAGE_OFFSET 64 =20 #define DE_VECTOR 0 #define DB_VECTOR 1 diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h index 9d647fad76..c5a6c6e0a6 100644 --- a/linux-headers/linux/kvm.h +++ b/linux-headers/linux/kvm.h @@ -236,6 +236,7 @@ struct kvm_hyperv_exit { #define KVM_EXIT_IOAPIC_EOI 26 #define KVM_EXIT_HYPERV 27 #define KVM_EXIT_ARM_NISV 28 +#define KVM_EXIT_DIRTY_RING_FULL 29 =20 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -1009,6 +1010,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_PPC_GUEST_DEBUG_SSTEP 176 #define KVM_CAP_ARM_NISV_TO_USER 177 #define KVM_CAP_ARM_INJECT_EXT_DABT 178 +#define KVM_CAP_DIRTY_LOG_RING 179 =20 #ifdef KVM_CAP_IRQ_ROUTING =20 @@ -1473,6 +1475,9 @@ struct kvm_enc_region { /* Available with KVM_CAP_ARM_SVE */ #define KVM_ARM_VCPU_FINALIZE _IOW(KVMIO, 0xc2, int) =20 +/* Available with KVM_CAP_DIRTY_LOG_RING */ +#define KVM_RESET_DIRTY_RINGS _IO(KVMIO, 0xc3) + /* Secure Encrypted Virtualization command */ enum sev_cmd_id { /* Guest initialization commands */ @@ -1623,4 +1628,43 @@ struct kvm_hyperv_eventfd { #define KVM_HYPERV_CONN_ID_MASK 0x00ffffff #define KVM_HYPERV_EVENTFD_DEASSIGN (1 << 0) =20 +/* + * KVM dirty GFN flags, defined as: + * + * |---------------+---------------+--------------| + * | bit 1 (reset) | bit 0 (dirty) | Status | + * |---------------+---------------+--------------| + * | 0 | 0 | Invalid GFN | + * | 0 | 1 | Dirty GFN | + * | 1 | X | GFN to reset | + * |---------------+---------------+--------------| + * + * Lifecycle of a dirty GFN goes like: + * + * dirtied collected reset + * 00 -----------> 01 -------------> 1X -------+ + * ^ | + * | | + * +------------------------------------------+ + * + * The userspace program is only responsible for the 01->1X state + * conversion (to collect dirty bits). Also, it must not skip any + * dirty bits so that dirty bits are always collected in sequence. + */ +#define KVM_DIRTY_GFN_F_DIRTY BIT(0) +#define KVM_DIRTY_GFN_F_RESET BIT(1) +#define KVM_DIRTY_GFN_F_MASK 0x3 + +/* + * KVM dirty rings should be mapped at KVM_DIRTY_LOG_PAGE_OFFSET of + * per-vcpu mmaped regions as an array of struct kvm_dirty_gfn. The + * size of the gfn buffer is decided by the first argument when + * enabling KVM_CAP_DIRTY_LOG_RING. + */ +struct kvm_dirty_gfn { + __u32 flags; + __u32 slot; + __u64 offset; +}; + #endif /* __LINUX_KVM_H */ --=20 2.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912368079489.8384762871997; Wed, 5 Feb 2020 06:19:28 -0800 (PST) Received: from localhost ([::1]:47908 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLWY-00031P-Vu for importer@patchew.org; Wed, 05 Feb 2020 09:19:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46335) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVD-0001Mh-Ik for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVC-00048Q-95 for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:03 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:21816 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVC-00045v-4I for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:02 -0500 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-140-btPD_UBINaKLfnQhYu0_BA-1; Wed, 05 Feb 2020 09:18:00 -0500 Received: by mail-qt1-f199.google.com with SMTP id c10so1391257qtk.18 for ; Wed, 05 Feb 2020 06:18:00 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.17.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:17:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912281; 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=Y8fsBRwMolg42syNt6jxBKLMK101VeBYIo5tMXgljYk=; b=Z25WADoaKfxvXw0VJMwgSJjtIV1TMI3hgodWZQw81FXGUo0+pRIXbWeDGpPuSfSrucgqR6 icO8DFlanA/BX9gxROYWrlH/OHKGiGNtmkBX5jn5foWYPXG1xHNSpRQWj9PX7bARiD1+tS sf+3t9G9r9DZowRr9nNIRll2OGzgLDw= 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=ZwCAJ7+Bi8HYROxL4ZxVz+2gGyWK5YxztwwQiXiRp10=; b=cQfiDbwd2opPtnKqJUFOvOuVR7vCZbyfV2w0wUgB6Dqx2u3POPjjr0FWo6yH1hJ6Bs EdEl+ui5r4/nHWGtp++T8/8gy3v6yuz4IjTg8t/4v54EVH4j3vA+Iz1MnJUKDQMu7gu9 KSaWrilqKcS8BUIUR3DdVk7lrCcv7NaZEUV3H7Uzapre02GR5cLNko4TygsamunFjUtU utr2YfoFUzZw5vhRf/OZq0Dez7TbS2X7rMB8l2TPijDADJn5XJHQ2/iLmRizZEYSYtQw hVILc4xXyfqQSLu9hPHeP9Xe7UgzPy86jhaBJ3TVC5oe4BznGNmdUaP9eFI4V37n2QhK j/0Q== X-Gm-Message-State: APjAAAVQ+7Y8Wai8GZegvrXnjLq1BT5TQCikiviRbl00vsAQqF8ZMESr Ju+Ugkp4jqFZaie5orLj/JNO7lJMFpsTr1wUfnoC/mCapTzeth3DdDE3d4q/1mnnBHvW7NSZa4S sS8Nlcl0VuFNuuZc= X-Received: by 2002:a05:6214:10cb:: with SMTP id r11mr31379338qvs.59.1580912279561; Wed, 05 Feb 2020 06:17:59 -0800 (PST) X-Google-Smtp-Source: APXvYqxBvXZgeGjML3JG/YZe80kwx1+vFa9Qd7H9HJpqhLhYYNtCtw1qU4fgg7lOY0sE7yFPWT4KRw== X-Received: by 2002:a05:6214:10cb:: with SMTP id r11mr31379314qvs.59.1580912279255; Wed, 05 Feb 2020 06:17:59 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 3/9] memory: Introduce log_sync_global() to memory listener Date: Wed, 5 Feb 2020 09:17:43 -0500 Message-Id: <20200205141749.378044-4-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: btPD_UBINaKLfnQhYu0_BA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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 views. Make this new method be exclusive to log_sync(). Signed-off-by: Peter Xu Reviewed-by: Dr. David Alan Gilbert --- include/exec/memory.h | 12 ++++++++++++ memory.c | 33 +++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index e85b7de99a..c4427094bb 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -533,6 +533,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/memory.c b/memory.c index aeaa8dcc9e..53828ba00c 100644 --- a/memory.c +++ b/memory.c @@ -2016,6 +2016,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; @@ -2029,18 +2033,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 @@ -2727,6 +2737,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.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912372075169.49440068397348; Wed, 5 Feb 2020 06:19:32 -0800 (PST) Received: from localhost ([::1]:47910 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLWc-0003CK-Rx for importer@patchew.org; Wed, 05 Feb 2020 09:19:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46490) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVJ-0001Te-PL for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVH-0004SV-5q for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:09 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:31490 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVH-0004MX-0X for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:07 -0500 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-165-olJw1sO-Mlicati2qONzTw-1; Wed, 05 Feb 2020 09:18:01 -0500 Received: by mail-qt1-f199.google.com with SMTP id o24so1399971qtr.17 for ; Wed, 05 Feb 2020 06:18:01 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.17.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:18:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912284; 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=/l92mY2pAFJF/yUP+iL03ANFNUP9y0kgsvuaoTh1kgg=; b=W5mN7gbGM0WaW51FO1lHXF2nuKKV56vCV2n/O4fb1oCKtCDVNfgj1CqPQrFe0lXtVlLjGI hYwuivdE/W3O4d1VuzgNqO0VBh22kmVS6P1JR6/rfoVxXTh7fyqy56gBO3oEn8u/eD8t5H g8Xy6lltR8REmnz7p8Bgz306O/bkbWE= 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=Ha3PiyjeE7m3n0zH2bYefx34bQnP0jpPs+XZBPTQxTg=; b=Bn45GjqR4mPL3LUD36k5TUxjWgUfcaow7rIxEXIL9nGySzeSbJooRsJitWzkC5mh9n BtKE2noVVHZ1hK0attK0NX4ygkrdlMsOFqOWPDFr6nN6EuH5JxY1bLNrVm6CDjNn349b 6bP1yfnZJWMQH6SuRhi+BbUMwMPLl0WklA3D9st0cGgkHWU2Gzjse5C17OSUvebETl9H n/HD5daTXcbbacnE2BCViMpkHnN8MAdXnZA05HF1VP/tNpC3My+8+xbn4MXI0sLU4+W2 WZpvfk8ncN8D5eT+w/coluoAFIWy04mNZbZQ5+k3fBwW3b6fBaltp0zueRrJwPGMTiVs ykLA== X-Gm-Message-State: APjAAAXQWjw9x6m+88UIg3XQnRSBoLfEBMKZEXaAMTS15hLm8QVDbuIU hSx9EE2gxOQ77ti8WN3cQxEx88odXsNeqMXuDzSrdNmn9xnhqmdGiI5fA2uwy4Vf+ORA/8V5Hbk bgUvRhw8mQL3UfjI= X-Received: by 2002:a05:6214:927:: with SMTP id dk7mr29250966qvb.200.1580912281038; Wed, 05 Feb 2020 06:18:01 -0800 (PST) X-Google-Smtp-Source: APXvYqwUHB+zXjrTNJiKFKHsixhMD6GxCR9nO43ARJwlOz+J40yHrsh+S7U+cT3j7zlX8nyxKc8lMw== X-Received: by 2002:a05:6214:927:: with SMTP id dk7mr29250949qvb.200.1580912280774; Wed, 05 Feb 2020 06:18:00 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 4/9] KVM: Create the KVMSlot dirty bitmap on flag changes Date: Wed, 5 Feb 2020 09:17:44 -0500 Message-Id: <20200205141749.378044-5-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: olJw1sO-Mlicati2qONzTw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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. Since at it, move all the pre-checks into kvm_slot_init_dirty_bitmap(). Signed-off-by: Peter Xu Reviewed-by: Dr. David Alan Gilbert --- 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 4be3cd2352..bb635c775f 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -162,6 +162,8 @@ static NotifierList kvm_irqchip_change_notifiers =3D #define kvm_slots_lock(kml) qemu_mutex_lock(&(kml)->slots_lock) #define kvm_slots_unlock(kml) qemu_mutex_unlock(&(kml)->slots_lock) =20 +static void kvm_slot_init_dirty_bitmap(KVMSlot *mem); + int kvm_get_max_memslots(void) { KVMState *s =3D KVM_STATE(current_accel()); @@ -442,6 +444,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 @@ -526,8 +529,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 @@ -578,11 +585,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); if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) =3D=3D -1) { @@ -1079,14 +1081,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.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912486824353.56726606323764; Wed, 5 Feb 2020 06:21:26 -0800 (PST) Received: from localhost ([::1]:47950 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLYT-0006id-Lo for importer@patchew.org; Wed, 05 Feb 2020 09:21:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46559) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVL-0001W8-2l for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVJ-0004cw-Ka for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:11 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:39579 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVJ-0004X9-Eq for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:09 -0500 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-430-q0a-nXkZPxCQ5uvwHiZXpQ-1; Wed, 05 Feb 2020 09:18:03 -0500 Received: by mail-qk1-f199.google.com with SMTP id p3so1362463qkd.11 for ; Wed, 05 Feb 2020 06:18:03 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:18:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912287; 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=SDqF6ADXTmGX6aI+C0w8LxHxtAanmII+L5XlPUFEpeE=; b=G3XqW3nnYwOznqk12GZkd8tMeOEA1rmy2+bj+z2tGArpoB07TDJ8pOuDJu6c8j961LeGL2 3DIF7hwMGVLDaVqHl3NTToSIo5erY6rHrT17txj+3VKraGdnFZ6qywNjjQKNOsqRbwtGZ2 pVt/N5wE0OoJFcndoa2MxKFLEydKut8= 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=suTJCLiAp4aNTkjDp91lv6dxS1uEjlqRGFRV/Jyu20w=; b=Lt936RrpY8fGTYVgzOuZV0+eyVAkOrqttQCx/FPcVN2oJec/412M5YP8By3LgkywJR AJO57iW+FPjzBE6VVQsIo2eT8Mj2KzNPcZXI3pqB6M8pRs/sAcRjw/tNc5P50QlQjJ8H Pe1G+OGxIcSyExzUO6DdYDx361m6Uafp+3MpFeIOOOmL+/nGtotkJ3+o9E3kFao/d5zv Wt/uzMZor6b2aFfghseCRGhisw5AzJEf3nCtp9gN95TH/Q8Get6rYd4ZVOQJAaL9Mrfg p2X/RqBm2AAK8FfwG4D81ISDyxY1SszyoDMSlepmFvXI96XGA4lTB190HD8og5PgZ/26 XaJA== X-Gm-Message-State: APjAAAVpRWRpz+JCvDzHOmZ6pjlTxO5tKphKIxgP8XL6XCPAIUlB6Uu0 nDsnq6ReOmBRq5+G74AeWK7FNW0nS3syTbwMwgfNAdZzrxkLHE1CMsE+CPPJycgZ3XXU9xQI2vz Q+lIgJH2AQoaZeH8= X-Received: by 2002:ac8:33f8:: with SMTP id d53mr33253067qtb.86.1580912283004; Wed, 05 Feb 2020 06:18:03 -0800 (PST) X-Google-Smtp-Source: APXvYqzcbm8HkUJDeCCw26VzHR2l/nqbL93dUcIf5feYF8YCoB9201RCglEcEzYwHHxGqIzo9d1l5g== X-Received: by 2002:ac8:33f8:: with SMTP id d53mr33253047qtb.86.1580912282723; Wed, 05 Feb 2020 06:18:02 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 5/9] KVM: Provide helper to get kvm dirty log Date: Wed, 5 Feb 2020 09:17:45 -0500 Message-Id: <20200205141749.378044-6-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: q0a-nXkZPxCQ5uvwHiZXpQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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 | 39 +++++++++++++++++++-------------------- include/sysemu/kvm_int.h | 2 ++ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index bb635c775f..608216fd53 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -553,6 +553,18 @@ 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 */ +static void 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); + assert(ret !=3D -1); +} + /** * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space * @@ -564,15 +576,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) { @@ -582,27 +592,19 @@ 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); - if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) =3D=3D -1) { - DPRINTF("ioctl failed %d\n", errno); - ret =3D -1; - goto out; - } + kvm_slot_get_dirty_log(s, mem); =20 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_get_dirty_pages_log_range(&subsection, mem->dirty_bmap); =20 slot_offset +=3D slot_size; start_addr +=3D slot_size; size -=3D slot_size; } -out: - return ret; } =20 /* Alignment requirement for KVM_CLEAR_DIRTY_LOG - 64 pages */ @@ -1077,6 +1079,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; @@ -1119,14 +1122,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(kml); - r =3D kvm_physical_sync_dirty_bitmap(kml, section); + kvm_physical_sync_dirty_bitmap(kml, section); kvm_slots_unlock(kml); - 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 ac2d1f8b56..4434e15ec7 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.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912489180316.1869616585326; Wed, 5 Feb 2020 06:21:29 -0800 (PST) Received: from localhost ([::1]:47952 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLYW-0006oz-1E for importer@patchew.org; Wed, 05 Feb 2020 09:21:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46473) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVJ-0001Sj-BE for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVH-0004XC-SV for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:09 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:42153 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVH-0004U0-Nb for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:07 -0500 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-363-5Lh30yNVOZSmvoexAYJ14w-1; Wed, 05 Feb 2020 09:18:05 -0500 Received: by mail-qk1-f200.google.com with SMTP id z1so1349505qkl.15 for ; Wed, 05 Feb 2020 06:18:05 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:18:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912287; 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=mRBblZHE0HNFkgTdufGMQbPMoE8LbUW6M6/u+V/IrNM=; b=FzZfBvSdDvzBa9GmytQR1V80Hi2cCCcAMfl5oYEBa1/I432ulaGhrlok78TRlv1AFxxGdB /01eHZLtDkvccI4l0e72GYIw+6BSWc9dd0khy5cgoAO297dMeJcIfn3ng+OWdMffUgzBuV xAPQdOEAmtn5JWq9NuynpTmo4djX/go= 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=glkVyCmPn2lU4x4NusHdwtelEkS4tZnSFXZS1Ih0SOQ=; b=tc5vmBvJ6pvj3oVfHpwIR/S/jG3ZzmqL9LXqvAz8cCwucOj1PPonQzqjOyvN6EjVg6 zKvuDVbuAYRdjceTb+DItvH1jf3tqe4dGSqwGyhJBMujbVuHvlAE6brK+9/+jHdGQ756 CDnl1T5FBhm6q4PcJr2P/x0BLoleyqrqC+Ry3/4knzf8+ZjmBFrCfULLmnap2eEt7dqz 4rZaY1e67Xe8dy1g85pBMduWPij9V6wdym/7dRfMtedsJ/yngmbkFbZHqYhdft4fPkKi ZW5Pdj/PbhfcWp+jOclf2aLiHj33ScO08mZJtkQzjQLRdY9SEyfTwr5fPwKJ89zEO49g 80Hw== X-Gm-Message-State: APjAAAUO00OeNrZ+32jfEPzFYU5updqkJaGP1+FrWT4WFpw9/xvAxEIS OVGHJqCfwIbXzckJcb2OZ8ZC4SjtNdzZ2iq01TW9pYkgoolBY26d25FjdS737yi0FjQ6Ab/biGb Jhu+ndfx1xpgQdkc= X-Received: by 2002:a05:6214:3aa:: with SMTP id m10mr32708727qvy.125.1580912284698; Wed, 05 Feb 2020 06:18:04 -0800 (PST) X-Google-Smtp-Source: APXvYqwbucul0ut7yKSwQCUGPQ59+flo+W/439eC14lRi3Q1dcVcSYkAR4yU2Q9YFB62+X7yRKb4ZQ== X-Received: by 2002:a05:6214:3aa:: with SMTP id m10mr32708712qvy.125.1580912284461; Wed, 05 Feb 2020 06:18:04 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 6/9] KVM: Provide helper to sync dirty bitmap from slot to ramblock Date: Wed, 5 Feb 2020 09:17:46 -0500 Message-Id: <20200205141749.378044-7-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: 5Lh30yNVOZSmvoexAYJ14w-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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. Signed-off-by: Peter Xu Reviewed-by: Dr. David Alan Gilbert --- 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 608216fd53..f81e7a644b 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -515,15 +515,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)) @@ -582,12 +579,10 @@ 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) { @@ -596,12 +591,7 @@ static void kvm_physical_sync_dirty_bitmap(KVMMemoryLi= stener *kml, } =20 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, mem->dirty_bmap); - - slot_offset +=3D slot_size; + kvm_slot_sync_dirty_pages(mem); start_addr +=3D slot_size; size -=3D slot_size; } @@ -1023,7 +1013,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)) { @@ -1041,9 +1032,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(kml); =20 @@ -1082,6 +1077,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); @@ -1092,6 +1088,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 4434e15ec7..1a19bfef80 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.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912475086886.2224619250719; Wed, 5 Feb 2020 06:21:15 -0800 (PST) Received: from localhost ([::1]:47946 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLYH-0006IW-Vl for importer@patchew.org; Wed, 05 Feb 2020 09:21:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46535) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVK-0001V0-IU for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVJ-0004bh-F3 for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:10 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:31202 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVJ-0004ZE-6p for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:09 -0500 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-397-8ivQtaRsMkG4ZUZn0yupCg-1; Wed, 05 Feb 2020 09:18:07 -0500 Received: by mail-qt1-f200.google.com with SMTP id l1so1388735qtp.21 for ; Wed, 05 Feb 2020 06:18:07 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:18:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912288; 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=yE+CLXdD02TVbOQfaDBkHUdhwWAp/SC/thW2yzCYvNk=; b=d+zyoS+onTQ4TY84jNeAW6uYpwrq/yO24tiia9pyyq0k3KUQ+pefArfh1B8IcF4zI0RM1o I3ao8LgFos80EIGtpx7pj/Jx6nKaVnw7R2Wq1gGjqJ4Gg/+QUkJhZFHj6l4idFhp4kOiAO hmqlwrLYGtGA84DxKSW0O6T4n3Dkfuc= 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=uArLOZV6r+y65XCWsRz0zJYx4XWcyehI0dFRirWsE7g=; b=HVtPuv4iK0BVKhS8776pOT21aw/s6vMTz56Z/psiydMeijyltRVz6y38JMIzBtMhOT ObweQkKuSCf+suYT+gJqRwIRB//N5UhG8rd4w+MWOgHCpky+BZK3JGOfexT8BhrWBgQP R43qFwMMTb2plL9yBRGY/jwf6hlrPtQe0dbzulLSQXVRgG+qTQtHIJXA/KzYjpIKMQFj 7b6Jy2+o0r4WAsn+JdDeGa8Tfo2eaLbMaPGlLgk2jMEeikE9cjcVHkEGTGlc/zSqTEa5 5YmUIt4A8DHIo8VDU+ztcT0ZbAo/ElpkaWfdHaWEOxVRtdPqRmKtwsQRS7NYMvLuvCCy ggPQ== X-Gm-Message-State: APjAAAWM8tPD/nZxw7xEnywcuuGK9zr1UdM1phlpiYq4uQksUX2wnpzt 64GA4fCnmyy8Ul9gql+/w2E2I/e9OXEZjBjJAW5elBqe60iR4viHGNWPOxr8ucUYLVgl6sT70lW B+27z6j4YqCewIbU= X-Received: by 2002:ad4:4f8f:: with SMTP id em15mr31121385qvb.169.1580912286372; Wed, 05 Feb 2020 06:18:06 -0800 (PST) X-Google-Smtp-Source: APXvYqzaB6mRUfa48JlO2cVzQtIGgb1NCVBzRWV4Q7P6Y7qWcch1VE5US1O7KVcwmetKwddH3XeM8A== X-Received: by 2002:ad4:4f8f:: with SMTP id em15mr31121368qvb.169.1580912286191; Wed, 05 Feb 2020 06:18:06 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 7/9] KVM: Cache kvm slot dirty bitmap size Date: Wed, 5 Feb 2020 09:17:47 -0500 Message-Id: <20200205141749.378044-8-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: 8ivQtaRsMkG4ZUZn0yupCg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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. Signed-off-by: Peter Xu Reviewed-by: Dr. David Alan Gilbert --- 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 f81e7a644b..ea7b8f7ca5 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -548,6 +548,7 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) hwaddr bitmap_size =3D ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS), /*HOST_LONG_BITS*/ 64) / 8; mem->dirty_bmap =3D g_malloc0(bitmap_size); + mem->dirty_bmap_size =3D bitmap_size; } =20 /* Sync dirty bitmap from kernel to KVMSlot.dirty_bmap */ diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 1a19bfef80..71c9946ecf 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.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912506925523.0223504778761; Wed, 5 Feb 2020 06:21:46 -0800 (PST) Received: from localhost ([::1]:47954 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLYn-0007et-Oc for importer@patchew.org; Wed, 05 Feb 2020 09:21:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46591) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVN-0001Y4-0I for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVL-0004om-Ok for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:12 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:37623 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVL-0004ln-K2 for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:11 -0500 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-324-DC4CotIQOo-M7wdC33_lMQ-1; Wed, 05 Feb 2020 09:18:09 -0500 Received: by mail-qk1-f198.google.com with SMTP id a132so1366021qkg.5 for ; Wed, 05 Feb 2020 06:18:09 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.18.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:18:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912291; 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=fz3NNX5AQ85UcY6yKGiJK1sZmDXeAQTTmBMXqMLh684=; b=R+BneKJBDucE9RFoA4zUIF5yKb0stZV92pcajhxQ4M/AqX5FzewZF3/xK3hWaMKXrIFaDc FyWdKXS2+ou2PLY7qQNmyeCRuY+7k0ebctaA908P0CfiUyaAmL0iPv9Gsk4yDQM3O7a27G MYAdQ9Cptk+c3Abg7zwm6w3sjGFIo7k= 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=Rx27vFBWamWKMWsj2aJzAbu6Jt3ncPu4wuVyMsyAlAQ=; b=azhuGRi76uueVOW0W4/asNOJmCuXuzWbo10KTowfT2fOe6tUh6P83fnMnTqRB/+/a9 y5pvL1SyamiwCQQjqwnHkzUnHU2h5HtIwR88V55kH7Xc2LEQgzcZ4+RQMuspWAD/ZEpl OAykaEnhDfvD9mHQgjQ9AAItEdErtsJMNYqlMmw3jMzijjdxqmV33EKN3PR/s1BEiI/R E3FdZIuWNSwH9JEJONVNEOvuhrLGRhQ3wqPGAVfdYEmnhrY07Bcps5hgKhzNvlHH5KGv 2aOZZ6BoZBKyGAuFrYvL/irr8oi+5GSs/8NoKV9tMIgKe6YB4gXL2e8NfW4tmjIHRxzO gndQ== X-Gm-Message-State: APjAAAWTg0jXUo3RCaajHB4plwujjVNxBi0egLN0tPLo4TVABUTqtEdQ GRTNl/716o7NGXm1EoblqlM8XMH/WUIz1DKi0wNd22O9xUHu2PQXkvX2y5q+/aEWs+40M0MX2pl tCp33KfUZ08G43QU= X-Received: by 2002:a05:620a:1112:: with SMTP id o18mr32769346qkk.126.1580912288750; Wed, 05 Feb 2020 06:18:08 -0800 (PST) X-Google-Smtp-Source: APXvYqw/x/dJ4OZGUf2SQESxUQiKdI8ZLgPdImKUIXqQVipxlQkM4ZKAfQcJh6Hd1PUgIRFjx8LDdw== X-Received: by 2002:a05:620a:1112:: with SMTP id o18mr32769315qkk.126.1580912288407; Wed, 05 Feb 2020 06:18:08 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 8/9] KVM: Add dirty-ring-size property Date: Wed, 5 Feb 2020 09:17:48 -0500 Message-Id: <20200205141749.378044-9-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: DC4CotIQOo-M7wdC33_lMQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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 size of dirty ring. If zero, dirty ring is disabled. Otherwise dirty ring will be enabled with the per-vcpu size as specified. If dirty ring cannot be enabled due to unsupported kernel, it'll fallback to dirty logging. By default, dirty ring is not enabled (dirty-ring-size=3D=3D0). Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 3 +++ 2 files changed, 67 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index ea7b8f7ca5..6d145a8b98 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -127,6 +127,8 @@ struct KVMState KVMMemoryListener *ml; AddressSpace *as; } *as; + int kvm_dirty_ring_size; + int kvm_dirty_gfn_count; /* If nonzero, then kvm dirty ring enabled= */ }; =20 KVMState *kvm_state; @@ -2077,6 +2079,33 @@ static int kvm_init(MachineState *ms) s->memory_listener.listener.coalesced_io_add =3D kvm_coalesce_mmio_reg= ion; s->memory_listener.listener.coalesced_io_del =3D kvm_uncoalesce_mmio_r= egion; =20 + /* + * Enable KVM dirty ring if supported, otherwise fall back to + * dirty logging mode + */ + if (s->kvm_dirty_ring_size > 0) { + /* Read the max supported pages */ + ret =3D kvm_vm_check_extension(kvm_state, KVM_CAP_DIRTY_LOG_RING); + if (ret > 0) { + if (s->kvm_dirty_ring_size > ret) { + error_report("KVM dirty ring size %d too big (maximum is %= d). " + "Please use a smaller value.", + s->kvm_dirty_ring_size, ret); + goto err; + } + + ret =3D kvm_vm_enable_cap(s, KVM_CAP_DIRTY_LOG_RING, 0, + s->kvm_dirty_ring_size); + if (ret) { + error_report("Enabling of KVM dirty ring failed: %d", ret); + goto err; + } + + s->kvm_dirty_gfn_count =3D + s->kvm_dirty_ring_size / sizeof(struct kvm_dirty_gfn); + } + } + kvm_memory_listener_register(s, &s->memory_listener, &address_space_memory, 0); memory_listener_register(&kvm_io_listener, @@ -3037,6 +3066,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_ring_size(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + KVMState *s =3D KVM_STATE(obj); + int64_t value =3D s->kvm_dirty_ring_size; + + visit_type_int(v, name, &value, errp); +} + +static void kvm_set_dirty_ring_size(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + KVMState *s =3D KVM_STATE(obj); + Error *error =3D NULL; + int64_t value; + + visit_type_int(v, name, &value, &error); + if (error) { + error_propagate(errp, error); + return; + } + + s->kvm_dirty_ring_size =3D value; +} + static void kvm_accel_instance_init(Object *obj) { KVMState *s =3D KVM_STATE(obj); @@ -3044,6 +3100,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; + /* By default off */ + s->kvm_dirty_ring_size =3D 0; } =20 static void kvm_accel_class_init(ObjectClass *oc, void *data) @@ -3065,6 +3123,12 @@ static void kvm_accel_class_init(ObjectClass *oc, vo= id *data) NULL, NULL, &error_abort); object_class_property_set_description(oc, "kvm-shadow-mem", "KVM shadow MMU size", &error_abort); + + object_class_property_add(oc, "dirty-ring-size", "int", + kvm_get_dirty_ring_size, kvm_set_dirty_ring_size, + NULL, NULL, &error_abort); + object_class_property_set_description(oc, "dirty-ring-size", + "KVM dirty ring size (<=3D0 to disable)", &error_abort); } =20 static const TypeInfo kvm_accel_type =3D { diff --git a/qemu-options.hx b/qemu-options.hx index 224a8e8712..140bd38726 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -119,6 +119,7 @@ DEF("accel", HAS_ARG, QEMU_OPTION_accel, " kernel-irqchip=3Don|off|split controls accelerated ir= qchip support (default=3Don)\n" " kvm-shadow-mem=3Dsize of KVM shadow MMU in bytes\n" " tb-size=3Dn (TCG translation block cache size)\n" + " dirty-ring-size=3Dn (KVM dirty ring size in Bytes)\n" " thread=3Dsingle|multi (enable multi-threaded TCG)\n",= QEMU_ARCH_ALL) STEXI @item -accel @var{name}[,prop=3D@var{value}[,...]] @@ -140,6 +141,8 @@ irqchip completely is not recommended except for debugg= ing purposes. Defines the size of the KVM shadow MMU. @item tb-size=3D@var{n} Controls the size (in MiB) of the TCG translation block cache. +@item dirty-ring-size=3D@val{n} +Controls the size (in Bytes) of KVM dirty ring (<=3D0 to disable). @item thread=3Dsingle|multi Controls number of TCG threads. When the TCG is multi-threaded there will = be one thread per vCPU therefor taking advantage of additional host cores. The de= fault --=20 2.24.1 From nobody Wed May 22 00:41:04 2024 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580912551296826.0904900571551; Wed, 5 Feb 2020 06:22:31 -0800 (PST) Received: from localhost ([::1]:47990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLZW-0000wc-45 for importer@patchew.org; Wed, 05 Feb 2020 09:22:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46731) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izLVT-0001oG-9e for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izLVQ-00056u-FA for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:19 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:53049 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1izLVQ-000544-9D for qemu-devel@nongnu.org; Wed, 05 Feb 2020 09:18:16 -0500 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-31-fuUAeVh1OlGhVm8wfVw-Gw-1; Wed, 05 Feb 2020 09:18:14 -0500 Received: by mail-qk1-f199.google.com with SMTP id q2so1340261qkq.19 for ; Wed, 05 Feb 2020 06:18:13 -0800 (PST) Received: from xz-x1.redhat.com ([2607:9880:19c8:32::2]) by smtp.gmail.com with ESMTPSA id q5sm12833489qkf.14.2020.02.05.06.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2020 06:18:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580912295; 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=1VI52KZlt+ao9WHLYQ9vWkU+VhHl4b1xPKv4e1ex0x0=; b=Nj/jJ7k0S3rXWtBZOr3Tkj7zdUibWvAu78/C3oI5mEqNUspWy7QOI3HCW/7OqCUK5xvxHe dvkYZRvwmEt9vXScf02tx//BA5JcGpwDrv29eq0881QMOCeyoNLFwWL5staymdPDlweFuT GlH8cuUpbGQbZFTz8YV3r7l9AyBHfGs= 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=r+4u5Q23LtSaRnaJymGafsaMoCQc5SoJPUpqQXYIgTA=; b=fKk0xQZ9Swr29t9SbjDRYq3jajh0PxwvL2miKbH7xP50ZPq7Qkr386V8+K/WmOCXXC J5zDfsKhECE693W7GOKos0v0aYZWm9PgGOn+LY3HYRvFywPuOqVoAwHYTK7DI9czJvHU KbavfXVMpt8djHBcqNUkEj+pLYdq8EIQtgq9bSGGzy5iRUQt7Qg+3E/nVKBkfkMMHbZv iCC9Pf36zgFQk2ELO17KDNHbEWqoBn0MW59aqzI2+ztpnfEQk4K3gWe1pN06iLxcYKmw O33HyxvWDNoLAPvEE5lCs1tbTdSCBXVTqAtbK1dD2O1OXSp21AobEkpadM/xelEk9HkF qcPw== X-Gm-Message-State: APjAAAX1orO5yEnldv+BUTDKJ/p6BnKHEPwtd056R3fDLf0YhVHLAtU7 qOJcUxXCq4xhoUW31uvtT2d9JePTJXegPpHth+i8h4dDcshjbH0UAhJ6ZYbFtE8aqybFCB5T/r0 s25BNX7DmFRUGKMc= X-Received: by 2002:a05:620a:2104:: with SMTP id l4mr30960082qkl.240.1580912292994; Wed, 05 Feb 2020 06:18:12 -0800 (PST) X-Google-Smtp-Source: APXvYqzguD2cP1VeWaNGLxpKGZXZ2k4LWRxhuskMTrPg/rJZpOlruMI7j5BMxv85r82+JetWr5MvSw== X-Received: by 2002:a05:620a:2104:: with SMTP id l4mr30960043qkl.240.1580912292445; Wed, 05 Feb 2020 06:18:12 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH RFC 9/9] KVM: Dirty ring support Date: Wed, 5 Feb 2020 09:17:49 -0500 Message-Id: <20200205141749.378044-10-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200205141749.378044-1-peterx@redhat.com> References: <20200205141749.378044-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: fuUAeVh1OlGhVm8wfVw-Gw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 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 , "Dr . David Alan Gilbert" , peterx@redhat.com, "Michael S . Tsirkin" 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. For each vcpu there will be one dirty ring that binds to it. There're a few major changes comparing to how the old dirty logging interface would work: - Granularity of dirty bits KVM dirty ring interface does not offer memory region level granularity to collect dirty bits (i.e., per KVM memory slot). Instead the dirty bit is collected globally for all the vcpus at once. The major effect is on VGA part because VGA dirty tracking is enabled as long as the device is created, also it was in memory region granularity. Now that operation will be amplified to a VM sync. Maybe there's smarter way to do the same thing in VGA with the new interface, but so far I don't see it affects much at least on regular VMs. - Collection of dirty bits The old dirty logging interface collects KVM dirty bits when synchronizing dirty bits. KVM dirty ring interface instead used a standalone thread to do that. So when the other thread (e.g., the migration thread) wants to synchronize the dirty bits, it simply kick the thread and wait until it flushes all the dirty bits to the ramblock dirty bitmap. For more information please refer to the comments in the code. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 426 ++++++++++++++++++++++++++++++++++++++++- accel/kvm/trace-events | 7 + include/hw/core/cpu.h | 10 + 3 files changed, 440 insertions(+), 3 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 6d145a8b98..201617bbb7 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 @@ -75,6 +76,47 @@ struct KVMParkedVcpu { QLIST_ENTRY(KVMParkedVcpu) node; }; =20 +enum KVMReaperState { + KVM_REAPER_NONE =3D 0, + /* The reaper is sleeping */ + KVM_REAPER_WAIT, + /* The reaper is reaping for dirty pages */ + KVM_REAPER_REAPING, +}; + +/* + * KVM reaper instance, responsible for collecting the KVM dirty bits + * via the dirty ring. + */ +struct KVMDirtyRingReaper { + /* The reaper thread */ + QemuThread reaper_thr; + /* + * Telling the reaper thread to wakeup. This should be used as a + * generic interface to kick the reaper thread, like, in vcpu + * threads where it gets a exit due to ring full. + */ + EventNotifier reaper_event; + /* + * This should only be used when someone wants to do synchronous + * flush of the dirty ring buffers. Logically we can achieve this + * even with the reaper_event only, however that'll make things + * complicated. This extra event can make the sync procedure easy + * and clean. + */ + EventNotifier reaper_flush_event; + /* + * Used in pair with reaper_flush_event, that the sem will be + * posted to notify that the previous flush event is handled by + * the reaper thread. + */ + QemuSemaphore reaper_flush_sem; + /* Iteration number of the reaper thread */ + volatile uint64_t reaper_iteration; + /* Status of the reaper thread */ + volatile enum KVMReaperState reaper_state; +}; + struct KVMState { AccelState parent_obj; @@ -121,7 +163,6 @@ struct KVMState void *memcrypt_handle; int (*memcrypt_encrypt_data)(void *handle, uint8_t *ptr, uint64_t len); =20 - /* For "info mtree -f" to tell if an MR is registered in KVM */ int nr_as; struct KVMAs { KVMMemoryListener *ml; @@ -129,6 +170,7 @@ struct KVMState } *as; int kvm_dirty_ring_size; int kvm_dirty_gfn_count; /* If nonzero, then kvm dirty ring enabled= */ + struct KVMDirtyRingReaper reaper; }; =20 KVMState *kvm_state; @@ -348,6 +390,11 @@ int kvm_destroy_vcpu(CPUState *cpu) goto err; } =20 + 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; @@ -391,6 +438,7 @@ int kvm_init_vcpu(CPUState *cpu) cpu->kvm_fd =3D ret; cpu->kvm_state =3D s; cpu->vcpu_dirty =3D true; + qemu_sem_init(&cpu->kvm_dirty_ring_avail, 0); =20 mmap_size =3D kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); if (mmap_size < 0) { @@ -412,6 +460,18 @@ int kvm_init_vcpu(CPUState *cpu) (void *)cpu->kvm_run + s->coalesced_mmio * PAGE_SIZE; } =20 + if (s->kvm_dirty_gfn_count) { + 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\n"); + goto err; + } + } + ret =3D kvm_arch_init_vcpu(cpu); err: return ret; @@ -525,6 +585,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 */ @@ -1100,6 +1165,305 @@ out: kvm_slots_unlock(kml); } =20 +static void kvm_dirty_ring_reaper_kick(void) +{ + trace_kvm_dirty_ring_reaper_kick("any"); + event_notifier_set(&kvm_state->reaper.reaper_event); +} + +static void kvm_dirty_ring_reaper_kick_flush(void) +{ + trace_kvm_dirty_ring_reaper_kick("flush"); + event_notifier_set(&kvm_state->reaper.reaper_flush_event); +} + +/* 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; + + assert(as_id < s->nr_as); + /* These fields shouldn't change after VM inits */ + kml =3D s->as[as_id].ml; + mem =3D &kml->slots[slot_id]; + 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 */ +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; + } + trace_kvm_dirty_ring_page(cpu->cpu_index, fetch, cur->offset); + kvm_dirty_ring_mark_page(s, cur->slot >> 16, cur->slot & 0xffff, + cur->offset); + dirty_gfn_set_collected(cur); + fetch++; + count++; + } + cpu->kvm_fetch_index =3D fetch; + + return count; +} + +static uint64_t kvm_dirty_ring_reap(KVMState *s) +{ + KVMMemoryListener *kml; + int ret, i, locked_count =3D s->nr_as; + CPUState *cpu; + uint64_t total =3D 0; + + /* + * 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. + */ + for (i =3D 0; i < s->nr_as; i++) { + kml =3D s->as[i].ml; + if (!kml) { + /* + * This is tricky - we grow s->as[] dynamically now. Take + * care of that case. We also assumed the as[] will fill + * one by one starting from zero. Without this, we race + * with register_smram_listener. + * + * TODO: make all these prettier... + */ + locked_count =3D i; + break; + } + kvm_slots_lock(kml); + } + + 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); + } + + /* Unlock whatever locks that we have locked */ + for (i =3D 0; i < locked_count; i++) { + kvm_slots_unlock(s->as[i].ml); + } + + CPU_FOREACH(cpu) { + if (cpu->kvm_dirty_ring_full) { + qemu_sem_post(&cpu->kvm_dirty_ring_avail); + } + } + + 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. + * + * To achieve this, we need to: + * + * (1) Kick all vcpus out, this will make sure that we flush all the + * dirty buffers that potentially in the hardware (PML) into the + * dirty rings, after that, + * + * (2) Kick the reaper thread and make sure it reaps all the dirty + * page that is in the dirty rings. + * + * This function must be called with BQL held. + */ +static void kvm_dirty_ring_flush(struct KVMDirtyRingReaper *r) +{ + uint64_t iteration; + + 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(); + + iteration =3D r->reaper_iteration; + + /* + * Kick the reaper to collect data. Here we must make sure that + * it goes over a complete WAIT->REAPING->WAIT period so that we + * know the reaper has collected all the dirty pages even in the + * hardware buffers we just flushed. To achieve this, we kick the + * flush_event. + */ + kvm_dirty_ring_reaper_kick_flush(); + qemu_sem_wait(&r->reaper_flush_sem); + + /* When reach here, we must have finished at least one iteration */ + assert(r->reaper_iteration > iteration); + + trace_kvm_dirty_ring_flush(1); +} + +static void *kvm_dirty_ring_reaper_thread(void *data) +{ + KVMState *s =3D data; + struct KVMDirtyRingReaper *r =3D &s->reaper; + struct pollfd *pfd =3D g_new0(struct pollfd, 2); + uint64_t count; + int64_t stamp; + int ret; + + rcu_register_thread(); + + trace_kvm_dirty_ring_reaper("init"); + + pfd[0].fd =3D event_notifier_get_fd(&r->reaper_event); + pfd[0].events =3D POLLIN; + pfd[1].fd =3D event_notifier_get_fd(&r->reaper_flush_event); + pfd[1].events =3D POLLIN; + + while (true) { + bool flush_requested =3D false; + + r->reaper_state =3D KVM_REAPER_WAIT; + trace_kvm_dirty_ring_reaper("wait"); + /* + * TODO: provide a smarter timeout rather than a constant? If + * this timeout is too small it could eat a lot of CPU + * resource, however if too big then VGA could be less + * responsive. 30ms is a value that is not too small so it + * won't eat much CPU, while the VGA can still get ~30Hz + * refresh rate. + */ + ret =3D poll(pfd, 2, 30); + trace_kvm_dirty_ring_reaper("wakeup"); + r->reaper_state =3D KVM_REAPER_REAPING; + + if (ret =3D=3D -1) { + error_report("%s: poll() failed: %s", __func__, strerror(errno= )); + break; + } + + /* + * Note: we only handle one request at a time. Also, we'll + * clear the event flag before we reap, so each SET to the + * event will guarantee that another full-reap procedure will + * happen. + */ + if (pfd[0].revents) { + ret =3D event_notifier_test_and_clear(&r->reaper_event); + assert(ret); + } else if (pfd[1].revents) { + ret =3D event_notifier_test_and_clear(&r->reaper_flush_event); + assert(ret); + flush_requested =3D true; + } + + stamp =3D get_clock(); + count =3D kvm_dirty_ring_reap(s); + stamp =3D get_clock() - stamp; + + r->reaper_iteration++; + + if (count) { + trace_kvm_dirty_ring_reaper_iterate(r->reaper_iteration, + count, stamp / 1000); + } + + /* + * If this iteration is to handle a flush event, wakeup the + * requester of the flush + */ + if (flush_requested) { + qemu_sem_post(&r->reaper_flush_sem); + } + } + + 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; + int ret; + + ret =3D event_notifier_init(&r->reaper_event, false); + assert(ret =3D=3D 0); + ret =3D event_notifier_init(&r->reaper_flush_event, false); + assert(ret =3D=3D 0); + qemu_sem_init(&r->reaper_flush_sem, 0); + + 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) { @@ -1128,6 +1492,36 @@ static void kvm_log_sync(MemoryListener *listener, kvm_slots_unlock(kml); } =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(&s->reaper); + + /* + * TODO: make this faster when nr_slots is big while there are + * only a few used slots (small VMs). + */ + kvm_slots_lock(kml); + 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(kml); +} + static void kvm_log_clear(MemoryListener *listener, MemoryRegionSection *section) { @@ -1234,10 +1628,17 @@ 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_gfn_count) { + /* KVM dirty ring enabled */ + kml->listener.log_sync_global =3D kvm_log_sync_global; + } else { + /* KVM dirty logging enabled */ + 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) { @@ -2120,6 +2521,13 @@ static int kvm_init(MachineState *ms) qemu_balloon_inhibit(true); } =20 + if (s->kvm_dirty_gfn_count) { + ret =3D kvm_dirty_ring_reaper_init(s); + if (ret) { + goto err; + } + } + return 0; =20 err: @@ -2427,6 +2835,18 @@ 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); + cpu->kvm_dirty_ring_full =3D true; + kvm_dirty_ring_reaper_kick(); + qemu_sem_wait(&cpu->kvm_dirty_ring_avail); + cpu->kvm_dirty_ring_full =3D false; + 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 4fb6e59d19..17d6b6a154 100644 --- a/accel/kvm/trace-events +++ b/accel/kvm/trace-events @@ -16,4 +16,11 @@ kvm_set_ioeventfd_mmio(int fd, uint64_t addr, uint32_t v= al, bool assign, uint32_ kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint32_t val, bool assign, ui= nt32_t size, bool datamatch) "fd: %d @0x%x val=3D0x%x assign: %d size: %d m= atch: %d" 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_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_reaper_iterate(uint64_t iter, uint64_t count, int64_t t) "i= teration %"PRIu64" reaped %"PRIu64" pages (took %"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 73e9a869a4..a7cddb7b40 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -342,6 +342,11 @@ 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_ring_full: + * Whether the kvm dirty ring of this vcpu is soft-full. + * @kvm_dirty_ring_avail: + * Semaphore to be posted when the kvm dirty ring of the vcpu is + * available again. * * State of one CPU core or thread. */ @@ -409,9 +414,14 @@ 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; + QemuSemaphore kvm_dirty_ring_avail; + bool kvm_dirty_ring_full; =20 /* Used for events with 'vcpu' and *without* the 'disabled' properties= */ DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS); --=20 2.24.1