Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3496981pxb; Tue, 20 Apr 2021 09:31:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeb4Vd3fuqJNsnBowHA3uc19oT4/48RtR3wDMhcapZ6KtRll2le3oKR/11TiRYl+5OSFCY X-Received: by 2002:a17:902:8b81:b029:eb:5a4:9cae with SMTP id ay1-20020a1709028b81b02900eb05a49caemr30029563plb.13.1618936306626; Tue, 20 Apr 2021 09:31:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618936306; cv=none; d=google.com; s=arc-20160816; b=u8InTy75vLHa+Mxjsa1IVTRiXJJdiJRQLhCJnUAEo9aMBaSxnAh6mlRda+JPTXygYl RLrcQPq/7jw6+raVnBpoKI9wQt6ouowltemuK7GfaHPwJ5eKyIs/357YhKYIbS4dw7Pt xkItXI3PvrotGuHBkPNqW2NXVv8+rLiORLc+Ykd7YGhpTSEgfnJ6asTsBXL0S1zxR0jx jRW7QX/ZxPxjGqQRG3l54e+nk7OU0vjdsn4quJjd0RDRMlSyjM1F+2J6fUW0wJCYOUlb ZW5gyIyb68aFjwei2kTHXZXqwUxfx/8bSOVaD9WIL5hh+0sgqawydaO6MXB99S411KQv fu5Q== 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=deNOr07l2wkHhud4xGx8hrdhodJ3UZWUT/QKiiXv/EI=; b=jwMhS4VwySXoXdfxn7LQjQ9drEg5GlW4V3I2M9JBqzDVEnFMjhY4XmHI0mmbBJVCpG RkQAwYkRxmaAel4Rhu0JqXLaUD3I5iTR8t2q6g9aKF54Pr0iS0NMY3qj/IV/OtuhYu4E IbJHrgS+iNzkVrV8DM7q0s1oJout1c1Ren7gTdxv1mYvjmTwSFVRnXRU7tSZjPN611AA R6xNCZsMC3d+NBdagWDwwjBAQ8qUK3zK7e5NU1jlfXGSC3LZD2YjRgSb8npA1XXIJKEH 1JGFJOMdd042fjBgt5T8y7BhCVDtxyf8Tr/Nh3xREZRQXot7taXdGczplCKkQoaWgs03 +yDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hzCmI+CW; 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 h3si20902390pfe.97.2021.04.20.09.31.33; Tue, 20 Apr 2021 09:31:46 -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=hzCmI+CW; 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 S232940AbhDTQba (ORCPT + 99 others); Tue, 20 Apr 2021 12:31:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232473AbhDTQb3 (ORCPT ); Tue, 20 Apr 2021 12:31:29 -0400 Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7BBBC06174A for ; Tue, 20 Apr 2021 09:30:56 -0700 (PDT) Received: by mail-il1-x12c.google.com with SMTP id y10so10659813ilv.0 for ; Tue, 20 Apr 2021 09:30:56 -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=deNOr07l2wkHhud4xGx8hrdhodJ3UZWUT/QKiiXv/EI=; b=hzCmI+CWUw1aC+FvafEX4nl8l+cXuSmkdoNGlWYa6ol9zGeXEBFP7sqpqI34idPhxF AJnDGzkz38AJUkMfLKsqZ+etP9IkV/QCskIABVk43IScLGX+ziYjhP2Ksc+zPUTSfSP3 9SIZhcXSMUUPAePGl/EPpiDy/5PGmKb5dKNnwwzJnDX/f2D1tKkgx/h87YmFHbTNGGGY UCZ+NhfU8BMkWh3M4PT200+l8leEl2UA592Ej0d8/MIn2pqg803AYvriIhAKci8ST+U4 CBDUHOXATSt3QM3RBsZvUkNqEf8ku48+kCNxjfoFwITVPLxLWxB6KLsfr6zNWewIWH9k k4oQ== 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=deNOr07l2wkHhud4xGx8hrdhodJ3UZWUT/QKiiXv/EI=; b=MgwLoD4dgxAyaJnotuvvNDDiWjCLLKbfKWHHcbw06Q0jrDwPcLBhtxmfC/CTNnnsp0 qv4yThGMPC9WuWzZ9/QiPMotA/Lt3H4xwEqo166RKbM/Mfk33te+9u4DTiVMJuLXHDpV NLAt3CRuis0t+Ko2Pj72aET0D7WNZEydQH5tPVdVqvHG19FGTKpya6zNi8FDAsAzujgg gQ2UpyzQNCM0i2VEdSkwe6HtwcQQOhoP4IODH+++G0+Q7aFnmPHaT7oJs4HrqDciFu1R ViBYiSUv4KDppzdIy7CPGiN+60ALYOelHlcplK/MkObt3n/KTcCGyhUfO+ra9ahr7n2g ADeg== X-Gm-Message-State: AOAM530dlEfpLOeokO/A56oC4Qcfc9lW9XXDxtRlAj3FgCcBrhXv2xSh CKYAOiWeosMniYGHLknZlT1MoCKvgFjMNshMCve+Yw== X-Received: by 2002:a92:7f03:: with SMTP id a3mr22488135ild.203.1618936256045; Tue, 20 Apr 2021 09:30:56 -0700 (PDT) MIME-Version: 1.0 References: <20210416082511.2856-1-zhukeqian1@huawei.com> <20210416082511.2856-3-zhukeqian1@huawei.com> <49e6bf4f-0142-c9ea-a8c1-7cfe211c8d7b@huawei.com> In-Reply-To: <49e6bf4f-0142-c9ea-a8c1-7cfe211c8d7b@huawei.com> From: Ben Gardon Date: Tue, 20 Apr 2021 09:30:45 -0700 Message-ID: Subject: Re: [RFC PATCH v2 2/2] KVM: x86: Not wr-protect huge page with init_all_set dirty log To: Keqian Zhu Cc: LKML , kvm , Paolo Bonzini , Sean Christopherson , 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 20, 2021 at 12:49 AM Keqian Zhu wrote: > > Hi Ben, > > On 2021/4/20 3:20, Ben Gardon wrote: > > On Fri, Apr 16, 2021 at 1:25 AM Keqian Zhu wrote: > >> > >> Currently during start dirty logging, if we're with init-all-set, > >> we write protect huge pages and leave normal pages untouched, for > >> that we can enable dirty logging for these pages lazily. > >> > >> Actually 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. > >> > >> Signed-off-by: Keqian Zhu > >> --- > >> arch/x86/kvm/mmu/mmu.c | 48 ++++++++++++++++++++++++++++++++++++++---- > >> arch/x86/kvm/x86.c | 37 +++++++++----------------------- > >> 2 files changed, 54 insertions(+), 31 deletions(-) > >> > >> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > >> index 2ce5bc2ea46d..98fa25172b9a 100644 > >> --- a/arch/x86/kvm/mmu/mmu.c > >> +++ b/arch/x86/kvm/mmu/mmu.c > >> @@ -1188,8 +1188,7 @@ static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head, > >> * @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. > >> + * Used when we do not need to care about huge page mappings. > >> */ > >> static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm, > >> struct kvm_memory_slot *slot, > >> @@ -1246,13 +1245,54 @@ static void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm, > >> * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to > >> * enable dirty logging for them. > >> * > >> - * Used when we do not need to care about huge page mappings: e.g. during dirty > >> - * logging we do not have any such mappings. > >> + * We need to care about huge page mappings: e.g. during dirty logging we may > >> + * have any such mappings. > >> */ > >> void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm, > >> struct kvm_memory_slot *slot, > >> gfn_t gfn_offset, unsigned long mask) > >> { > >> + gfn_t start, end; > >> + > >> + /* > >> + * Huge pages are NOT write protected when we start dirty log with > >> + * init-all-set, so we must write protect them at here. > >> + * > >> + * The gfn_offset is guaranteed to be aligned to 64, but the base_gfn > >> + * of memslot has no such restriction, so the range can cross two large > >> + * pages. > >> + */ > >> + if (kvm_dirty_log_manual_protect_and_init_set(kvm)) { > >> + start = slot->base_gfn + gfn_offset + __ffs(mask); > >> + end = slot->base_gfn + gfn_offset + __fls(mask); > >> + kvm_mmu_slot_gfn_write_protect(kvm, slot, start, PG_LEVEL_2M); > >> + > >> + /* Cross two large pages? */ > >> + if (ALIGN(start << PAGE_SHIFT, PMD_SIZE) != > >> + ALIGN(end << PAGE_SHIFT, PMD_SIZE)) > >> + kvm_mmu_slot_gfn_write_protect(kvm, slot, end, > >> + PG_LEVEL_2M); > >> + } > >> + > >> + /* > >> + * RFC: > >> + * > >> + * 1. I don't return early when kvm_mmu_slot_gfn_write_protect() returns > >> + * true, because I am not very clear about the relationship between > >> + * legacy mmu and tdp mmu. AFAICS, the code logic is NOT an if/else > >> + * manner. > >> + * > >> + * The kvm_mmu_slot_gfn_write_protect() returns true when we hit a > >> + * writable large page mapping in legacy mmu mapping or tdp mmu mapping. > >> + * Do we still have normal mapping in that case? (e.g. We have large > >> + * mapping in legacy mmu and normal mapping in tdp mmu). > > > > Right, we can't return early because the two MMUs could map the page > > in different ways, but each MMU could also map the page in multiple > > ways independently. > > For example, if the legacy MMU was being used and we were running a > > nested VM, a page could be mapped 2M in EPT01 and 4K in EPT02, so we'd > > still need kvm_mmu_slot_gfn_write_protect calls for both levels. > > I don't think there's a case where we can return early here with the > > information that the first calls to kvm_mmu_slot_gfn_write_protect > > access. > Thanks for the detailed explanation. > > > > >> + * > >> + * 2. kvm_mmu_slot_gfn_write_protect() doesn't tell us whether the large > >> + * page mapping exist. If it exists but is clean, we can return early. > >> + * However, we have to do invasive change. > > > > What do you mean by invasive change? > We need the kvm_mmu_slot_gfn_write_protect to report whether all mapping are large > and clean, so we can return early. However it's not a part of semantics of this function. > > If this is the final code, compared to old code, we have an extra gfn_write_protect(), > I don't whether it's acceptable? Ah, I see. Please correct me if I'm wrong, but I think that in order to check that the only mappings on the GFN range are large, we'd still have to go over the rmap for the 4k mappings, at least for the legacy MMU. In that case, we're doing about as much work as the extra gfn_write_protect and I don't think that we'd get any efficiency gain for the change in semantics. Likewise for the TDP MMU, if the GFN range is mapped both large and 4k, it would have to be in different TDP structures, so the efficiency gains would again not be very big. I'm really just guessing about those performance characteristics though. It would definitely help to have some performance data to back all this up. Even just a few runs of the dirty_log_perf_test (in selftests) could provide some interesting results, and I'd be happy to help review any improvements you might make to that test. Regardless, I'd be inclined to keep this change as simple as possible for now and the early return optimization could happen in a follow-up patch. I think the extra gfn_write_protect is acceptable, especially if you can show that it doesn't cause a big hit in performance when running the dirty_log_perf_test with 4k and 2m backing memory. > > Thanks, > Keqian > > > > > >> + */ > >> + > >> + /* Then we can handle the PT level pages */ > >> if (kvm_x86_ops.cpu_dirty_log_size) > >> kvm_mmu_clear_dirty_pt_masked(kvm, slot, gfn_offset, mask); > >> else > >> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > >> index eca63625aee4..dfd676ffa7da 100644 > >> --- a/arch/x86/kvm/x86.c > >> +++ b/arch/x86/kvm/x86.c > >> @@ -10888,36 +10888,19 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm, > >> */ > >> kvm_mmu_zap_collapsible_sptes(kvm, new); > >> } else { > >> - /* By default, write-protect everything to log writes. */ > >> - int level = PG_LEVEL_4K; > >> + /* > >> + * If we're with initial-all-set, we don't need to write protect > >> + * any page because they're reported as dirty already. > >> + */ > >> + if (kvm_dirty_log_manual_protect_and_init_set(kvm)) > >> + return; > >> > >> if (kvm_x86_ops.cpu_dirty_log_size) { > >> - /* > >> - * Clear all dirty bits, unless pages are treated as > >> - * dirty from the get-go. > >> - */ > >> - if (!kvm_dirty_log_manual_protect_and_init_set(kvm)) > >> - kvm_mmu_slot_leaf_clear_dirty(kvm, new); > >> - > >> - /* > >> - * Write-protect large pages on write so that dirty > >> - * logging happens at 4k granularity. No need to > >> - * write-protect small SPTEs since write accesses are > >> - * logged by the CPU via dirty bits. > >> - */ > >> - level = PG_LEVEL_2M; > >> - } else if (kvm_dirty_log_manual_protect_and_init_set(kvm)) { > >> - /* > >> - * If we're with initial-all-set, we don't need > >> - * to write protect any small page because > >> - * they're reported as dirty already. However > >> - * we still need to write-protect huge pages > >> - * so that the page split can happen lazily on > >> - * the first write to the huge page. > >> - */ > >> - level = PG_LEVEL_2M; > >> + kvm_mmu_slot_leaf_clear_dirty(kvm, new); > >> + kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_2M); > >> + } else { > >> + kvm_mmu_slot_remove_write_access(kvm, new, PG_LEVEL_4K); > >> } > >> - kvm_mmu_slot_remove_write_access(kvm, new, level); > >> } > >> } > >> > >> -- > >> 2.23.0 > >> > > . > >