Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3128774pxb; Tue, 13 Apr 2021 20:11:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwHW3KoJgRDFkJNznJYy+EDHynekWTdvnKdtssa5aycWmWq1MGgKnQ3yob/YrsLxJ2Hk2c X-Received: by 2002:a63:5fd2:: with SMTP id t201mr34429305pgb.116.1618369889546; Tue, 13 Apr 2021 20:11:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618369889; cv=none; d=google.com; s=arc-20160816; b=QlQMmgXPLH2S7etYX5msDgsWoBPBM5LLgV5F9BSWmqVmrf31BUjWmuu/9fx9OtvZ4R zBwnkbFSiP7HgUmCR74/gQubJ1O5NWc1D7uu56f2QxfZkxJZ0aoy9MJA7B3Adz+3RM7D cri53qewRH7b1KwmBWFoIrI/7QvZuXCK3/bWyD4KWoXL79TPzoAFwM01RTqLSF35IHrG 7uyNVsemcMJ0I3L11f1O2o/bNbpCLOrzyZMvLEadQYbHiC4U+M7wbBLYdn0l2Bbxgcbu 52fPUJIDhZFgp+28RrsuFG3ecHXYkdi9MkeXk1Nm8hE4ov0bO+Cyxn1Sa+oUNsfll7XJ AWeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=iW2orkpn3/bzDHR5jPZkG2zO1Q6UU2wcOtmPep0vycU=; b=DsAG3ULgoJXF5bbMM0P4uR1fmBeijUmSRRQo/NugrtcNitbLgP69Clucmq1nfamHZ6 f9pkdMjyGi8VMMwyJAPZZYiKet5ScJp4XmX5JEkKmeweFdO+kJCvzG5veu7pDMt2wYsK uu6oPrTtcH8pdU3efDuGRzBQ7/Ff4c6AtLuXSYX2yZbxjRfFxSM8pWJkESi6Pd+YXlYA BnMX1Ob3j6cX8K7NWQcNco6BSeYZyDEcGsPNvdGm3STelZLhDvOrDHdNSwYn2Utgb3ZV +hg8m8oA6U7y3S1nz3qTDGCGeDJbaLdBY6utidtJEW6ZFmDaZ1pqcbp4/mY1abX/2jOM aohQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ptKyptyd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m3si19671871plx.227.2021.04.13.20.11.14; Tue, 13 Apr 2021 20:11:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ptKyptyd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344025AbhDMQp1 (ORCPT + 99 others); Tue, 13 Apr 2021 12:45:27 -0400 Received: from mail-io1-f45.google.com ([209.85.166.45]:36459 "EHLO mail-io1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232225AbhDMQpZ (ORCPT ); Tue, 13 Apr 2021 12:45:25 -0400 Received: by mail-io1-f45.google.com with SMTP id d5so8288003iof.3 for ; Tue, 13 Apr 2021 09:45:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iW2orkpn3/bzDHR5jPZkG2zO1Q6UU2wcOtmPep0vycU=; b=ptKyptyd2M1R0uP2kg7qbRB0HFHBiTngZVZp76hLjgU8pntkRy/yfJhGlG6fZXFUUX Z2Mx8p6wcLsvoG4FFjuLw08Cho9ybo0HMb0eHuUnIvtDRzqbI6n/bGALY4bOATp/WIrI o7WmoJ5gcVbyKVsF6dZed5p+peWEuSaHuAPh3sO0w4wgxXNXQtc2JkR6ziEiGzoJbaL0 v4GsqH6hMD6qg1Z1mHesX0XcqBJ/0tdkLRvQTamAXl0nQBrIkrUOTz4q3TtPPApEmYDj D8aOUXTM35MulbndOdkKUX2OiTLHiKVIzUfqrBLFbXziVtLyaMO0oKW79jUBbxI0RP2z Z96w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iW2orkpn3/bzDHR5jPZkG2zO1Q6UU2wcOtmPep0vycU=; b=dFfTW8Lja5SyBnzacnkQWdNS55d+CLkSU360CB5T7RMc0WCtWx+CDeiXR5d4297Uhr qmVZROoSRY6Ar8Db/g/P2gKlBC1nQKPO6ipX2TXKYCRVAGXYw8ZdNPF02R2/QTpC41B2 QxUF+Gr46BxdOKI1Qe/Adx16snHSOovUY3fvUr2eyTjAUPGhs2whODi0gb96s/XOgzn/ /B0uyeN+XX6vrPZquU9KnG943SGA8U2mdKs3G6dDTrPuY1GsGEWril+6zssR020KgjxF 1v+JIS77AU+T8JxUNTljjirPS6fJiOXGZAZjXbEZP/voMlra5EfpBYrADMeMMU4goV1i MguA== X-Gm-Message-State: AOAM532pqyZqnfSPXqHC4MHbUx+g+70WHcg3rPJ8XEEzIkHX69v74kAc fHpxbdmSs8Q6Doi9B6ZvF8wvfXRd7EsL5glRp0cRbw== X-Received: by 2002:a05:6638:1648:: with SMTP id a8mr34063909jat.25.1618332245231; Tue, 13 Apr 2021 09:44:05 -0700 (PDT) MIME-Version: 1.0 References: <20200828081157.15748-1-zhukeqian1@huawei.com> <107696eb-755f-7807-a484-da63aad01ce4@huawei.com> In-Reply-To: From: Ben Gardon Date: Tue, 13 Apr 2021 09:43:54 -0700 Message-ID: Subject: Re: [RFC PATCH] KVM: x86: Support write protect huge pages lazily To: Keqian Zhu Cc: Sean Christopherson , kvm , LKML , Paolo Bonzini , wanghaibin.wang@huawei.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 13, 2021 at 2:39 AM Keqian Zhu wrote: > > > > On 2021/4/13 1:19, Ben Gardon wrote: > > On Tue, Apr 6, 2021 at 4:42 PM Sean Christopherson wrote: > >> > >> +Ben > >> > >> On Tue, Apr 06, 2021, Keqian Zhu wrote: > >>> Hi Paolo, > >>> > >>> I plan to rework this patch and do full test. What do you think about this idea > >>> (enable dirty logging for huge pages lazily)? > >> > >> Ben, don't you also have something similar (or maybe the exact opposite?) in the > >> hopper? This sounds very familiar, but I can't quite connect the dots that are > >> floating around my head... > > > > Sorry for the late response, I was out of office last week. > Never mind, Sean has told to me. :) > > > > > Yes, we have two relevant features I'd like to reconcile somehow: > > 1.) Large page shattering - Instead of clearing a large TDP mapping, > > flushing the TLBs, then replacing it with an empty TDP page table, go > > straight from the large mapping to a fully pre-populated table. This > > is slightly slower because the table needs to be pre-populated, but it > > saves many vCPU page faults. > > 2.) Eager page splitting - split all large mappings down to 4k when > > enabling dirty logging, using large page shattering. This makes > > enabling dirty logging much slower, but speeds up the first round (and > > later rounds) of gathering / clearing the dirty log and reduces the > > number of vCPU page faults. We've prefered to do this when enabling > > dirty logging because it's a little less perf-sensitive than the later > > passes where latency and convergence are critical. > OK, I see. I think the lock stuff is an important part, so one question is that > the shattering process is designed to be locked (i.e., protect mapping) or lock-less? > > If it's locked, vCPU thread may be blocked for a long time (For arm, there is a > mmu_lock per VM). If it's lock-less, how can we ensure the synchronization of > mapping? The TDP MMU for x86 could do it under the MMU read lock, but the legacy / shadow x86 MMU and other architectures would need the whole MMU lock. While we do increase the time required to address a large SPTE, we can completely avoid the vCPU needing the MMU lock on an access to that SPTE as the translation goes straight from a large, writable SPTE, to a 4k spte with either the d bit cleared or write protected. If it's write protected, the fault can (at least on x86) be resolved without the MMU lock. When I'm able to put together a large page shattering series, I'll do some performance analysis and see how it changes things, but that part is sort of orthogonal to this change. The more I think about it, the better the init-all-set approach for large pages sounds, compared to eager splitting. I'm definitely in support of this patch and am happy to help review when you send out the v2 with TDP MMU support and such. > > > > > Large page shattering can happen in the NPT page fault handler or the > > thread enabling dirty logging / clearing the dirty log, so it's > > more-or-less orthogonal to this patch. > > > > Eager page splitting on the other hand takes the opposite approach to > > this patch, frontloading as much of the work to enable dirty logging > > as possible. Which approach is better is going to depend a lot on the > > guest workload, your live migration constraints, and how the > > user-space hypervisor makes use of KVM's growing number of dirty > > logging options. In our case, the time to migrate a VM is usually less > > of a concern than the performance degradation the guest experiences, > > so we want to do everything we can to minimize vCPU exits and exit > > latency. > Yes, make sense to me. > > > > > I think this is a reasonable change in principle if we're not write > > protecting 4k pages already, but it's hard to really validate all the > > performance implications. With this change we'd move pretty much all > > the work to the first pass of clearing the dirty log, which is > > probably an improvement since it's much more granular. The downside is > Yes, at least split large page lazily is better than current logic. > > > that we do more work when we'd really like to be converging the dirty > > set as opposed to earlier when we know all pages are dirty anyway. > I think the dirty collecting procedure is not affected, do I miss something? Oh yeah, good point. Since the splitting of large SPTEs is happening in the vCPU threads it wouldn't slow dirty log collection at all. We would have to do slightly more work to write protect the large SPTEs that weren't written to, but that's a relatively small amount of work. > > > > >> > >>> PS: As dirty log of TDP MMU has been supported, I should add more code. > >>> > >>> On 2020/8/28 16:11, Keqian Zhu wrote: > >>>> Currently during enable dirty logging, if we're with init-all-set, > >>>> we just write protect huge pages and leave normal pages untouched, > >>>> for that we can enable dirty logging for these pages lazily. > >>>> > >>>> It seems that enable dirty logging lazily for huge pages is feasible > >>>> too, which not only reduces the time of start dirty logging, also > >>>> greatly reduces side-effect on guest when there is high dirty rate. > > > > The side effect on the guest would also be greatly reduced with large > > page shattering above. > Sure. > > > > >>>> > >>>> (These codes are not tested, for RFC purpose :-) ). > >>>> > >>>> Signed-off-by: Keqian Zhu > >>>> --- > >>>> arch/x86/include/asm/kvm_host.h | 3 +- > >>>> arch/x86/kvm/mmu/mmu.c | 65 ++++++++++++++++++++++++++------- > >>>> arch/x86/kvm/vmx/vmx.c | 3 +- > >>>> arch/x86/kvm/x86.c | 22 +++++------ > >>>> 4 files changed, 62 insertions(+), 31 deletions(-) > >>>> > >>>> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > >>>> index 5303dbc5c9bc..201a068cf43d 100644 > >>>> --- a/arch/x86/include/asm/kvm_host.h > >>>> +++ b/arch/x86/include/asm/kvm_host.h > >>>> @@ -1296,8 +1296,7 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, > >>>> > >>>> void kvm_mmu_reset_context(struct kvm_vcpu *vcpu); > >>>> void kvm_mmu_slot_remove_write_access(struct kvm *kvm, > >>>> - struct kvm_memory_slot *memslot, > >>>> - int start_level); > >>>> + struct kvm_memory_slot *memslot); > >>>> void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm, > >>>> const struct kvm_memory_slot *memslot); > >>>> void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, > >>>> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > >>>> index 43fdb0c12a5d..4b7d577de6cd 100644 > >>>> --- a/arch/x86/kvm/mmu/mmu.c > >>>> +++ b/arch/x86/kvm/mmu/mmu.c > >>>> @@ -1625,14 +1625,45 @@ static bool __rmap_set_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head) > >>>> } > >>>> > >>>> /** > >>>> - * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages > >>>> + * kvm_mmu_write_protect_largepage_masked - write protect selected largepages > >>>> * @kvm: kvm instance > >>>> * @slot: slot to protect > >>>> * @gfn_offset: start of the BITS_PER_LONG pages we care about > >>>> * @mask: indicates which pages we should protect > >>>> * > >>>> - * Used when we do not need to care about huge page mappings: e.g. during dirty > >>>> - * logging we do not have any such mappings. > >>>> + * @ret: true if all pages are write protected > >>>> + */ > >>>> +static bool kvm_mmu_write_protect_largepage_masked(struct kvm *kvm, > >>>> + struct kvm_memory_slot *slot, > >>>> + gfn_t gfn_offset, unsigned long mask) > >>>> +{ > >>>> + struct kvm_rmap_head *rmap_head; > >>>> + bool protected, all_protected; > >>>> + gfn_t start_gfn = slot->base_gfn + gfn_offset; > >>>> + int i; > >>>> + > >>>> + all_protected = true; > >>>> + while (mask) { > >>>> + protected = false; > >>>> + for (i = PG_LEVEL_2M; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) { > >>>> + rmap_head = __gfn_to_rmap(start_gfn + __ffs(mask), i, slot); > >>>> + protectd |= __rmap_write_protect(kvm, rmap_head, false); > >>>> + } > >>>> + > >>>> + all_protected &= protectd; > >>>> + /* clear the first set bit */ > >>>> + mask &= mask - 1; > > > > I'm a little confused by the use of mask in this function. If > > gfn_offset is aligned to some multiple of 64, which I think it is, all > > the bits in the mask will be part of the same large page, so I don't > > think the mask adds anything. > Right. We just need to consider the first set bit. Thanks for your careful review. > > > I'm also not sure this function compiles since I think the use of > > protectd above will result in an error. > Yep, my fault. > > And as I mentioned before, I should add more code to support TDP... > > Thanks, > Keqian