Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp8615365ybc; Fri, 29 Nov 2019 13:40:33 -0800 (PST) X-Google-Smtp-Source: APXvYqxA1qGFjCde2iokzpiPnXo3FIjeG3Z/+5b69KyQuFmHy7Q1bJ1yn162gD9XvLah04JZHWfo X-Received: by 2002:a17:906:891:: with SMTP id n17mr60948918eje.230.1575063633877; Fri, 29 Nov 2019 13:40:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575063633; cv=none; d=google.com; s=arc-20160816; b=pV6JuGcIq8sJhru1nt4POZSEHdIrp2HWVhhsRNd05ecJ2C3c89jpvbRLIZlX4kSHZW vwa4oWtBM9iZ9DvAVa4pBgJIYeQ4Il4Sz3YUDmGWsIW9nuW2sC09G9aw3pmvsF+7ry1S bky4GfO0Hy3dSfMrZbkuWPbPDSHw0Z0aIjMZVMVEOw9ebS8IsNj0trNSZcWgbuSV1N/m nsipzf7R9YnmAuOHN4KDYZ6+YwdWFJHxPC5pSpUz6PQq3PeQVrkjxVKF9r4z7H5l4598 ULCclevudVATqJTLX2FU3q3wIacAdwRMyJKW1QCZkJD6lzVt6Tn82674Aqkh0pfw0it2 romw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rb+6DP+izG5ltISMKHwwwOgrlsplIg1b9aT7OhqAVdI=; b=FOXE6rSWQLxc9lQV2W/k/7XNpjdR5rpanS4iX6iQphYg/RrEjL/ZAEqPK0GJNCb86e ARm0vdS15AYtcTjS9cBTzwZEYnjV4/tNoBWSKZO1jUVc6wgNktqzGn7GV9ddjdTNpICO HsNnOGIY3is7KaiwnkJo+Q6G0RG6Fnc2QK2lb2ybN8EWW2xRGASHZT+hIVreyeLz3LJJ xv+xyg67mIUTX17tWsClf0E4S92h8XQN3l3e9+mwSmMRUhQVkPMN/7gg2LBWlLnuuPle 81eAwoh/IiXAkdkFXNXLhbVR25MXcu1Fhcm89wfjqpygZ9LlTfT1d3tOHqk+5ukmgh8J /DrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LxZkIhF1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f11si5960896edn.424.2019.11.29.13.40.09; Fri, 29 Nov 2019 13:40:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LxZkIhF1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727446AbfK2Vf0 (ORCPT + 99 others); Fri, 29 Nov 2019 16:35:26 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:42492 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727385AbfK2VfX (ORCPT ); Fri, 29 Nov 2019 16:35:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575063322; 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=rb+6DP+izG5ltISMKHwwwOgrlsplIg1b9aT7OhqAVdI=; b=LxZkIhF12KpKao3v/6U+tXVfQtVQVUINEKQQkA+TYUszcP7PYTZdzK9UVoeWrVSmO7It1n g+YY8Wwr/gFdbzFWDTUsYAH76o+oslM/MSrr9IbO/IqKSI2kPMCcWlJYAeqB2A20jyc/A9 R5HPIAfP37LJW9SbBXfJdPooqBEO7Dw= 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-327-mZHLb78kPV-Vtj_NFP3I8g-1; Fri, 29 Nov 2019 16:35:21 -0500 Received: by mail-qk1-f200.google.com with SMTP id o204so7616724qke.22 for ; Fri, 29 Nov 2019 13:35:21 -0800 (PST) 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=TqT6uWauQhM+3HI2ETpe/nnUa6snC1aBt/hCGN1iX+A=; b=nESljyw2Xwkodw23/9/ChgLwoSOBcTN4MDRanoQU/NHErT+rgFS8/Z0HTloLvWz87D 1a4QomZCyATmG37jTKsSecD/04t9lqDypclleG+y1nKz+FxkcHy8CbyMhPQTx57wbb5k qYTwJ6vB5beqIsK+QqAtJXyE8NjNN4E6xsfYeCbWjnQ92jij/cWn6mKChagylALBp29D 9WkFc/gQ3WTcIvduMORAaHXBOqzdF14XCJ8uSytHQTHBlR+SGByb8lD5akluTub66/Kv TxOjO0H5akn2AUBzEcaJutF1yiWo1301xmK/TiegzxjQBBMGguuQsvRFSdBYwwURb02P 2nWA== X-Gm-Message-State: APjAAAWdWyonYfL/Wp7UcvopjKk51t/e/41Qihwq18MDiNSKLh+W4fiL HxEwdNAUFsqGcNjBDW3NNbNAspAMUiGN8EDxRL75S7JBgOIHj94vuer+A71igJnjtQCVdqVNuX/ /BiV9DLWQRR4k3Znmq7kwdjib X-Received: by 2002:ac8:4645:: with SMTP id f5mr6786387qto.38.1575063320072; Fri, 29 Nov 2019 13:35:20 -0800 (PST) X-Received: by 2002:ac8:4645:: with SMTP id f5mr6786365qto.38.1575063319872; Fri, 29 Nov 2019 13:35:19 -0800 (PST) Received: from xz-x1.yyz.redhat.com ([104.156.64.74]) by smtp.gmail.com with ESMTPSA id h186sm10679046qkf.64.2019.11.29.13.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Nov 2019 13:35:18 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Paolo Bonzini , "Dr . David Alan Gilbert" , peterx@redhat.com, Vitaly Kuznetsov Subject: [PATCH RFC 07/15] KVM: X86: Implement ring-based dirty memory tracking Date: Fri, 29 Nov 2019 16:34:57 -0500 Message-Id: <20191129213505.18472-8-peterx@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191129213505.18472-1-peterx@redhat.com> References: <20191129213505.18472-1-peterx@redhat.com> MIME-Version: 1.0 X-MC-Unique: mZHLb78kPV-Vtj_NFP3I8g-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Cao, Lei" Add new KVM exit reason KVM_EXIT_DIRTY_LOG_FULL and connect KVM_REQ_DIRTY_LOG_FULL to it. Signed-off-by: Lei Cao Signed-off-by: Paolo Bonzini [peterx: rebase, return 0 instead of -EINTR for user exits, emul_insn before exit to userspace] Signed-off-by: Peter Xu --- arch/x86/include/asm/kvm_host.h | 5 +++++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/mmu/mmu.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 7 +++++++ arch/x86/kvm/x86.c | 12 ++++++++++++ 5 files changed, 31 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index b79cd6aa4075..67521627f9e4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -49,6 +49,8 @@ =20 #define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS =20 +#define KVM_DIRTY_RING_VERSION 1 + /* x86-specific vcpu->requests bit members */ #define KVM_REQ_MIGRATE_TIMER=09=09KVM_ARCH_REQ(0) #define KVM_REQ_REPORT_TPR_ACCESS=09KVM_ARCH_REQ(1) @@ -1176,6 +1178,7 @@ struct kvm_x86_ops { =09=09=09=09=09 struct kvm_memory_slot *slot, =09=09=09=09=09 gfn_t offset, unsigned long mask); =09int (*write_log_dirty)(struct kvm_vcpu *vcpu); +=09int (*cpu_dirty_log_size)(void); =20 =09/* pmu operations of sub-arch */ =09const struct kvm_pmu_ops *pmu_ops; @@ -1661,4 +1664,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu) #define GET_SMSTATE(type, buf, offset)=09=09\ =09(*(type *)((buf) + (offset) - 0x7e00)) =20 +int kvm_cpu_dirty_log_size(void); + #endif /* _ASM_X86_KVM_HOST_H */ diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 503d3f42da16..b59bf356c478 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/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/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 6f92b40d798c..f7efb69b089e 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1818,7 +1818,13 @@ int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu) { =09if (kvm_x86_ops->write_log_dirty) =09=09return kvm_x86_ops->write_log_dirty(vcpu); +=09return 0; +} =20 +int kvm_cpu_dirty_log_size(void) +{ +=09if (kvm_x86_ops->cpu_dirty_log_size) +=09=09return kvm_x86_ops->cpu_dirty_log_size(); =09return 0; } =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d175429c91b0..871489d92d3c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7710,6 +7710,7 @@ static __init int hardware_setup(void) =09=09kvm_x86_ops->slot_disable_log_dirty =3D NULL; =09=09kvm_x86_ops->flush_log_dirty =3D NULL; =09=09kvm_x86_ops->enable_log_dirty_pt_masked =3D NULL; +=09=09kvm_x86_ops->cpu_dirty_log_size =3D NULL; =09} =20 =09if (!cpu_has_vmx_preemption_timer()) @@ -7774,6 +7775,11 @@ static __exit void hardware_unsetup(void) =09free_kvm_area(); } =20 +static int vmx_cpu_dirty_log_size(void) +{ +=09return enable_pml ? PML_ENTITY_NUM : 0; +} + static struct kvm_x86_ops vmx_x86_ops __ro_after_init =3D { =09.cpu_has_kvm_support =3D cpu_has_kvm_support, =09.disabled_by_bios =3D vmx_disabled_by_bios, @@ -7896,6 +7902,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init= =3D { =09.flush_log_dirty =3D vmx_flush_log_dirty, =09.enable_log_dirty_pt_masked =3D vmx_enable_log_dirty_pt_masked, =09.write_log_dirty =3D vmx_write_pml_buffer, +=09.cpu_dirty_log_size =3D vmx_cpu_dirty_log_size, =20 =09.pre_block =3D vmx_pre_block, =09.post_block =3D vmx_post_block, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3ed167e039e5..03ff34783fa1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8094,6 +8094,18 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) =09=09 */ =09=09if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) =09=09=09kvm_hv_process_stimers(vcpu); + +=09=09if (kvm_check_request(KVM_REQ_DIRTY_RING_FULL, vcpu)) { +=09=09=09vcpu->run->exit_reason =3D KVM_EXIT_DIRTY_RING_FULL; +=09=09=09/* +=09=09=09 * If this is requested, it means that we've +=09=09=09 * marked the dirty bit in the dirty ring BUT +=09=09=09 * we've not written the date. Do it now. +=09=09=09 */ +=09=09=09r =3D kvm_emulate_instruction(vcpu, 0); +=09=09=09r =3D r >=3D 0 ? 0 : r; +=09=09=09goto out; +=09=09} =09} =20 =09if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { --=20 2.21.0