From nobody Fri May 3 07:27:54 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1497950391307848.0562406826808; Tue, 20 Jun 2017 02:19:51 -0700 (PDT) Received: from localhost ([::1]:47458 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dNFKA-0002Xg-0P for importer@patchew.org; Tue, 20 Jun 2017 05:19:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52904) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dNFGF-00007S-VN for qemu-devel@nongnu.org; Tue, 20 Jun 2017 05:15:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dNFGB-0007kc-0N for qemu-devel@nongnu.org; Tue, 20 Jun 2017 05:15:47 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:35611) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dNFGA-0007k2-Nt for qemu-devel@nongnu.org; Tue, 20 Jun 2017 05:15:42 -0400 Received: by mail-pf0-x242.google.com with SMTP id s66so21597813pfs.2 for ; Tue, 20 Jun 2017 02:15:42 -0700 (PDT) Received: from eric.tencent.com ([203.205.141.35]) by smtp.gmail.com with ESMTPSA id o12sm26343810pfi.9.2017.06.20.02.15.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Jun 2017 02:15:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Lq6ScNh0ix6W9dBDI2lNiDunExlmmgV3xP60lG4eyGU=; b=TV4EVTn1QOSCKgjz8C3rI8sO3/Wnew46BlzLW8kamypSWp/eSC7HBlAhf33p5qRZlI scjuFLlBbg2HAU9WHNh0c6XypZmVN0a8FxD9n69Njhw7jh2EOzNf56bX9bTCRFiblWwi 2+usPOuXWZA6kSn2EDzp0o6NRdI5ZahIjnMj690iZFfwIKUzzy4Zy33WGV/pnYosjZ2r jaaxZzMVGUuG0FuyOr7diTwCQ8Myf+jfEPnkaD2C9Wpjn+Ob4rXleXKxrn/7kZTVdIAk 6vqY3kUH652HDhtmOQl0oHNn8erTrJ2fq8Hi5ujC2LAFy0Ki2RSzMaJXCtQMO5wf0eVf yxfw== 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=Lq6ScNh0ix6W9dBDI2lNiDunExlmmgV3xP60lG4eyGU=; b=s1fcSs0fVVQZYBoPXL4fYZXr1zIRGn3BwqgrVttmC2AvYRirICOmd+ibtSNTwGqd1r XvGn3h2fc+g8ZyswHc6Bvhjyi9NG19H+uGk1nmQnIpEc+Pko1aPba2t2ocbqQsjOCBKU ONvd9+IIQh/3fZ8Cfv1LK/7RVCneJWlGx8w4xsWamZoceaLWk2/Ot/dXjr+1ms90cXsd DFKHs9jspopXw4LHw7mv6vQ5VtP9QAsWybCn2riNNEXOGEM6wCWd2wCRvemJjiGacInA 83vCu0hSZ72Jtwx+Rz0EnzCyLaRI1QmJyW9Gk3Oh4n6oYnq6Sz0f4fsXl+NbZ5HEm+JN IYAQ== X-Gm-Message-State: AKS2vOz1unDyGX5N4hvvXKAWm/MDm0CcDxyu/0JGhdxAELRPfN+PgXW5 4yYuk7HBZd9khQ== X-Received: by 10.98.43.7 with SMTP id r7mr25659515pfr.202.1497950141595; Tue, 20 Jun 2017 02:15:41 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mtosatti@redhat.com, avi.kivity@gmail.com, rkrcmar@redhat.com Date: Tue, 20 Jun 2017 17:15:19 +0800 Message-Id: <20170620091526.4287-1-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.9.4 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v2 0/7] KVM: MMU: fast write protect X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Xiao Guangrong , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 From: Xiao Guangrong Changelog in v2: thanks to Paolo's review, this version disables write-protect-all if PML is supported Background =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The original idea of this patchset is from Avi who raised it in the mailing list during my vMMU development some years ago This patchset introduces a extremely fast way to write protect all the guest memory. Comparing with the ordinary algorithm which write protects last level sptes based on the rmap one by one, it just simply updates the generation number to ask all vCPUs to reload its root page table, particularly, it can be done out of mmu-lock, so that it does not hurt vMMU's parallel. It is the O(1) algorithm which does not depends on the capacity of guest's memory and the number of guest's vCPUs Implementation =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D When write protect for all guest memory is required, we update the global generation number and ask vCPUs to reload its root page table by calling kvm_reload_remote_mmus(), the global number is protected by slots_lock During reloading its root page table, the vCPU checks root page table's generation number with current global number, if it is not matched, it makes all the entries in the shadow page readonly and directly go to VM. So the read access is still going on smoothly without KVM's involvement and write access triggers page fault If the page fault is triggered by write operation, KVM moves the write protection from the upper level to the lower level page - by making all the entries in the lower page readonly first then make the upper level writable, this operation is repeated until we meet the last spte In order to speed up the process of making all entries readonly, we introduce possible_writable_spte_bitmap which indicates the writable sptes and possiable_writable_sptes which is a counter indicating the number of writable sptes in the shadow page, they work very efficiently as usually only one entry in PML4 ( < 512 G)=EF=BC=8Cfew entries in PDPT (o= ne entry indicates 1G memory), PDEs and PTEs need to be write protected for the worst case. Note, the number of page fault and TLB flush are the same as the ordinary algorithm Performance Data =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Case 1) For a VM which has 3G memory and 12 vCPUs, we noticed that: a: the time required for dirty log (ns) before after 64289121 137654 +46603% b: the performance of memory write after dirty log, i.e, the dirty log path is not parallel with page fault, the time required to write all 3G memory for all vCPUs in the VM (ns): before after 281735017 291150923 -3% We think the impact, 3%, is acceptable, particularly, mmu-lock contention is not take into account in this case Case 2) For a VM which has 30G memory and 8 vCPUs, we do the live migration, at the some time, a test case which greedily and repeatedly writes 3000M memory in the VM. 2.1) for the new booted VM, i.e, page fault is required to map guest memory in, we noticed that: a: the dirty page rate (pages): before after 333092 497266 +49% that means, the performance for the being migrated VM is hugely improved as the contention on mmu-lock is reduced b: the time to complete live migration (ms): before after 12532 18467 -47% not surprise, the time required to complete live migration is increased as the VM is able to generate more dirty pages 2.2) pre-write the VM first, then run the test case and do live migration, i.e, no much page faults are needed to map guest memory in, we noticed that: a: the dirty page rate (pages): before after 447435 449284 +0% =09 b: time time to complete live migration (ms) before after 31068 28310 +10% under this case, we also noticed that the time of dirty log for the first time, before the patchset is 156 ms, after that, only 6 ms is needed =20 The patch applied to QEMU =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The draft patch is attached to enable this functionality in QEMU: diff --git a/kvm-all.c b/kvm-all.c index 90b8573..9ebe1ac 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -122,6 +122,7 @@ bool kvm_direct_msi_allowed; bool kvm_ioeventfd_any_length_allowed; bool kvm_msi_use_devid; static bool kvm_immediate_exit; +static bool kvm_write_protect_all; =20 static const KVMCapabilityInfo kvm_required_capabilites[] =3D { KVM_CAP_INFO(USER_MEMORY), @@ -440,6 +441,26 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionS= ection *section, =20 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) =20 +static bool kvm_write_protect_all_is_supported(KVMState *s) +{ + return kvm_check_extension(s, KVM_CAP_X86_WRITE_PROTECT_ALL_MEM) && + kvm_check_extension(s, KVM_CAP_X86_DIRTY_LOG_WITHOUT_WRITE_PROTECT); +} + +static void kvm_write_protect_all_mem(bool write) +{ + int ret; + + if (!kvm_write_protect_all) + return; + + ret =3D kvm_vm_ioctl(kvm_state, KVM_WRITE_PROTECT_ALL_MEM, !!write); + if (ret < 0) { + printf("ioctl failed %d\n", errno); + abort(); + } +} + /** * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space * This function updates qemu's dirty bitmap using @@ -490,6 +511,7 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryList= ener *kml, memset(d.dirty_bitmap, 0, allocated_size); =20 d.slot =3D mem->slot | (kml->as_id << 16); + d.flags =3D kvm_write_protect_all ? KVM_DIRTY_LOG_WITHOUT_WRITE_PR= OTECT : 0; if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) =3D=3D -1) { DPRINTF("ioctl failed %d\n", errno); ret =3D -1; @@ -1622,6 +1644,9 @@ static int kvm_init(MachineState *ms) } =20 kvm_immediate_exit =3D kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT); + kvm_write_protect_all =3D kvm_write_protect_all_is_supported(s); + printf("Write protect all is %s.\n", kvm_write_protect_all ? "supporte= d" : "unsupported"); + memory_register_write_protect_all(kvm_write_protect_all_mem); s->nr_slots =3D kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS); =20 /* If unspecified, use the default value */ diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h index 4e082a8..7c056ef 100644 --- a/linux-headers/linux/kvm.h +++ b/linux-headers/linux/kvm.h @@ -443,9 +443,12 @@ struct kvm_interrupt { }; =20 /* for KVM_GET_DIRTY_LOG */ + +#define KVM_DIRTY_LOG_WITHOUT_WRITE_PROTECT 0x1 + struct kvm_dirty_log { __u32 slot; - __u32 padding1; + __u32 flags; union { void *dirty_bitmap; /* one bit per page */ __u64 padding2; @@ -884,6 +887,9 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_PPC_MMU_HASH_V3 135 #define KVM_CAP_IMMEDIATE_EXIT 136 =20 +#define KVM_CAP_X86_WRITE_PROTECT_ALL_MEM 144 +#define KVM_CAP_X86_DIRTY_LOG_WITHOUT_WRITE_PROTECT 145 + #ifdef KVM_CAP_IRQ_ROUTING =20 struct kvm_irq_routing_irqchip { @@ -1126,6 +1132,7 @@ enum kvm_device_type { struct kvm_userspace_memory_region) #define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47) #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_WRITE_PROTECT_ALL_MEM _IO(KVMIO, 0x49) =20 /* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping { diff --git a/memory.c b/memory.c index 4c95aaf..b836675 100644 --- a/memory.c +++ b/memory.c @@ -809,6 +809,13 @@ static void address_space_update_ioeventfds(AddressSpa= ce *as) flatview_unref(view); } =20 +static write_protect_all_fn write_func; +void memory_register_write_protect_all(write_protect_all_fn func) +{ + printf("Write function is being registering...\n"); + write_func =3D func; +} + static void address_space_update_topology_pass(AddressSpace *as, const FlatView *old_view, const FlatView *new_view, @@ -859,6 +866,8 @@ static void address_space_update_topology_pass(AddressS= pace *as, MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_= stop, frold->dirty_log_mask, frnew->dirty_log_mask); + if (write_func) + write_func(false); } } =20 @@ -2267,6 +2276,9 @@ void memory_global_dirty_log_sync(void) } flatview_unref(view); } + + if (write_func) + write_func(true); } Xiao Guangrong (7): KVM: MMU: correct the behavior of mmu_spte_update_no_track KVM: MMU: introduce possible_writable_spte_bitmap KVM: MMU: introduce kvm_mmu_write_protect_all_pages KVM: MMU: enable KVM_WRITE_PROTECT_ALL_MEM KVM: MMU: allow dirty log without write protect KVM: MMU: clarify fast_pf_fix_direct_spte KVM: MMU: stop using mmu_spte_get_lockless under mmu-lock arch/x86/include/asm/kvm_host.h | 25 +++- arch/x86/kvm/mmu.c | 267 ++++++++++++++++++++++++++++++++++++= ++-- arch/x86/kvm/mmu.h | 2 + arch/x86/kvm/paging_tmpl.h | 13 +- arch/x86/kvm/x86.c | 22 ++++ include/uapi/linux/kvm.h | 8 +- virt/kvm/kvm_main.c | 15 ++- 7 files changed, 333 insertions(+), 19 deletions(-) --=20 2.9.4