From patchwork Mon Mar 9 21:44:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 11428125 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2DC82139A for ; Mon, 9 Mar 2020 21:44:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0161F24655 for ; Mon, 9 Mar 2020 21:44:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KfggPge5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727088AbgCIVod (ORCPT ); Mon, 9 Mar 2020 17:44:33 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:60605 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726266AbgCIVod (ORCPT ); Mon, 9 Mar 2020 17:44:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583790271; 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; bh=+ICtR2v0xNomH1WLkex46Nm+YSPs83aCqTW2zuVdMA4=; b=KfggPge5R51zEnrozMLwxsTSWUAjVuCTznd3SN1LJhpj8GOs8BtmvloEFdB4wlXOasRvCZ 0CFcakKCEkA8Yu9xMNlfzd3o/YoyMTLlZafN53U9S9xBddwjHWNVogRt00dKC+eUrbDC0b jkU29E7vuTNPHqEswmktUx8X/2dsHeQ= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-321-02TW3aEKNF-67w_EybCobQ-1; Mon, 09 Mar 2020 17:44:27 -0400 X-MC-Unique: 02TW3aEKNF-67w_EybCobQ-1 Received: by mail-qt1-f198.google.com with SMTP id b10so3710100qto.21 for ; Mon, 09 Mar 2020 14:44:27 -0700 (PDT) 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:mime-version :content-transfer-encoding; bh=+ICtR2v0xNomH1WLkex46Nm+YSPs83aCqTW2zuVdMA4=; b=AqGhFe7oj97K/d1UQVwpQ5PNCQcydKoc0OJkXkVEg2AFdc0qYkjzJ8j7Ti7NS38p8c GNlwZrXnO8VkXd4IZASPQCKU4TyIzocq238lD3jD+IbfVeizOReAea1SQpRNh9w8W5FE tO8oLafbFq8RK1mwqdOFaUkM2lqLws5v5zH5FuNBF51SdGm5zAqiBikrgSpc3GrZrnif rXJHaL7/IOihG4S/lO6dV8RbWqRQCjeBFmsRpL+yGSKKiGq6UfiFxQOd+pdHtoNDb0H+ OiN3+jiVM7gMOe5YdAK9whbZJUsW6U7Sj650gvpfD0SCTYZNqaNXzRV/IcYhiHhP5nXi bOUg== X-Gm-Message-State: ANhLgQ26Rg1LVcw7+32Lc7Nt3sjuyqdYOF7yUwbwbQ/J+qsQ56Vi5i25 8UePRWW1omx0kMsXJ77PMifpqeBkzH8/u7+hDaUNnZuIeLYybFEAJAbNuYBcidtwEdKWUDCYKd1 SwFM8+0yGZYSQ X-Received: by 2002:a37:4ec1:: with SMTP id c184mr2019112qkb.0.1583790266997; Mon, 09 Mar 2020 14:44:26 -0700 (PDT) X-Google-Smtp-Source: ADFU+vstAS1kAHk1yzYK0Qv2/bUTpVaVnkqEMcxpVeMGOT8PFcgZlWN3qpoiuGO6oI0meC5+JHYWaw== X-Received: by 2002:a37:4ec1:: with SMTP id c184mr2019077qkb.0.1583790266685; Mon, 09 Mar 2020 14:44:26 -0700 (PDT) Received: from xz-x1.redhat.com ([2607:9880:19c0:32::2]) by smtp.gmail.com with ESMTPSA id 82sm10504502qko.91.2020.03.09.14.44.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2020 14:44:25 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Yan Zhao , Jason Wang , Alex Williamson , peterx@redhat.com, Vitaly Kuznetsov , "Dr . David Alan Gilbert" , Christophe de Dinechin , "Michael S . Tsirkin" , Kevin Tian , Paolo Bonzini , Sean Christopherson Subject: [PATCH v6 00/14] KVM: Dirty ring interface Date: Mon, 9 Mar 2020 17:44:10 -0400 Message-Id: <20200309214424.330363-1-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM branch: https://github.com/xzpeter/linux/tree/kvm-dirty-ring QEMU branch for testing: https://github.com/xzpeter/qemu/tree/kvm-dirty-ring v6: - fix typo in test case (LOG_MODE_CLERA_LOG) [Dave] - fix s390 build (again), by moving kvm_reset_dirty_gfn into kvm_dirty_ring.c, with some macro movements [syzbot] For previous versions, please refer to: V1: https://lore.kernel.org/kvm/20191129213505.18472-1-peterx@redhat.com V2: https://lore.kernel.org/kvm/20191221014938.58831-1-peterx@redhat.com V3: https://lore.kernel.org/kvm/20200109145729.32898-1-peterx@redhat.com V4: https://lore.kernel.org/kvm/20200205025105.367213-1-peterx@redhat.com V5: https://lore.kernel.org/kvm/20200304174947.69595-1-peterx@redhat.com Overview ============ This is a continued work from Lei Cao and Paolo Bonzini on the KVM dirty ring interface. The new dirty ring interface is another way to collect dirty pages for the virtual machines. It is different from the existing dirty logging interface in a few ways, majorly: - Data format: The dirty data was in a ring format rather than a bitmap format, so dirty bits to sync for dirty logging does not depend on the size of guest memory any more, but speed of dirtying. Also, the dirty ring is per-vcpu, while the dirty bitmap is per-vm. - Data copy: The sync of dirty pages does not need data copy any more, but instead the ring is shared between the userspace and kernel by page sharings (mmap() on vcpu fd) - Interface: Instead of using the old KVM_GET_DIRTY_LOG, KVM_CLEAR_DIRTY_LOG interfaces, the new ring uses the new KVM_RESET_DIRTY_RINGS ioctl when we want to reset the collected dirty pages to protected mode again (works like KVM_CLEAR_DIRTY_LOG, but ring based). To collecting dirty bits, we only need to read the ring data, no ioctl is needed. Ring Layout =========== KVM dirty ring is per-vcpu. Each ring is an array of kvm_dirty_gfn defined as: struct kvm_dirty_gfn { __u32 flags; __u32 slot; /* as_id | slot_id */ __u64 offset; }; Each GFN is a state machine itself. The state is embeded in the flags field, as defined in the uapi header: /* * 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. */ Testing ======= This series provided both the implementation of the KVM dirty ring and the test case. Also I've implemented the QEMU counterpart that can run with the new KVM, link can be found at the top of the cover letter. However that's still a very initial version which is prone to change and future optimizations. I did some measurement with the new method with 24G guest running some dirty workload, I don't see any speedup so far, even in some heavy dirty load it'll be slower (e.g., when 800MB/s random dirty rate, kvm dirty ring takes average of ~73s to complete migration while dirty logging only needs average of ~55s). However that's understandable because 24G guest means only 1M dirty bitmap, that's still a suitable case for dirty logging. Meanwhile heavier workload means worst case for dirty ring. More tests are welcomed if there's bigger host/guest, especially on COLO-like workload. Please review, thanks. Peter Xu (14): KVM: X86: Change parameter for fast_page_fault tracepoint KVM: Cache as_id in kvm_memory_slot KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR] KVM: Pass in kvm pointer into mark_page_dirty_in_slot() KVM: X86: Implement ring-based dirty memory tracking KVM: Make dirty ring exclusive to dirty bitmap log KVM: Don't allocate dirty bitmap if dirty ring is enabled KVM: selftests: Always clear dirty bitmap after iteration KVM: selftests: Sync uapi/linux/kvm.h to tools/ KVM: selftests: Use a single binary for dirty/clear log test KVM: selftests: Introduce after_vcpu_run hook for dirty log test KVM: selftests: Add dirty ring buffer test KVM: selftests: Let dirty_log_test async for dirty ring test KVM: selftests: Add "-c" parameter to dirty log test Documentation/virt/kvm/api.rst | 123 +++++ arch/x86/include/asm/kvm_host.h | 6 +- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/mmu/mmu.c | 10 +- arch/x86/kvm/mmutrace.h | 9 +- arch/x86/kvm/svm.c | 9 +- arch/x86/kvm/vmx/vmx.c | 85 +-- arch/x86/kvm/x86.c | 49 +- include/linux/kvm_dirty_ring.h | 103 ++++ include/linux/kvm_host.h | 19 + include/trace/events/kvm.h | 78 +++ include/uapi/linux/kvm.h | 53 ++ tools/include/uapi/linux/kvm.h | 44 ++ tools/testing/selftests/kvm/Makefile | 2 - .../selftests/kvm/clear_dirty_log_test.c | 2 - tools/testing/selftests/kvm/dirty_log_test.c | 488 ++++++++++++++++-- .../testing/selftests/kvm/include/kvm_util.h | 4 + tools/testing/selftests/kvm/lib/kvm_util.c | 67 +++ .../selftests/kvm/lib/kvm_util_internal.h | 4 + virt/kvm/dirty_ring.c | 195 +++++++ virt/kvm/kvm_main.c | 164 +++++- 22 files changed, 1393 insertions(+), 125 deletions(-) create mode 100644 include/linux/kvm_dirty_ring.h delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c create mode 100644 virt/kvm/dirty_ring.c