Message ID | 20230211014626.3659152-1-vipinsh@google.com |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1279645wrn; Fri, 10 Feb 2023 17:50:55 -0800 (PST) X-Google-Smtp-Source: AK7set/k2eG7lNK8QUWPNuKyAW4oTdOKqtlZcL7aDwRq63BVYL4rAf75Yo+klYjmK6ht1Ukmtjrt X-Received: by 2002:a17:906:db0d:b0:878:61d8:d7c2 with SMTP id xj13-20020a170906db0d00b0087861d8d7c2mr23984671ejb.39.1676080255732; Fri, 10 Feb 2023 17:50:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676080255; cv=none; d=google.com; s=arc-20160816; b=WDx1UlYMtwWQPkJgnXkgs/zcNce2qT7XCys+c/El+JStvsHnhER84SZY/B0VixC9eh S5v4A98kozrNYlEfYBGLf6jXNaChsGwLfIjoLT0xcbqA0rRcSY3POYjOflbINKRh4tBg 1hEIWiav+PwjylVQg+smBdLeXZUPMSR7wns/vwUCTaTGgeA+JFyFJKxfkfAQXTrCXeD1 IooRyLz3Yve0oRL56NM5zJhXm/LN4EUMoM0Hu6jmHIEDz9cOaFWSYA3C3LTjT/EQWj+a 7b7pRyvJsmglymOJXsCqVi2pgp2zFYOMmnoN23Te/f481or+nAYDsN1Paw+5woNbcq8D TmZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=hkehzV3PgH5ghzIyq+2UQQQuaUD65ZdlOHTF9Rixj6U=; b=kmALuWpuZD7tL2oFrnsGN+xcvSjcIF8eb2Xmya3LZdt/yO2c2c428I7JQqWMgnZR06 N7VogQUouwNYKS1kOaK9t6jpXCqjY8vhJ2nUPzI39Wcxna9FNTmac6pjDnsR1yQtMjil zXaLCCYqe4+2LP/ZuOfNemPnawWdDoGN1i0I+J6zApFgZJprw76t68+qSjYWw6zYxdXC AwuK6YzYhA6Br5/jGKIY2fBV2OAKM7Kd2mErUSjHuWnJoakZP+/jiUhwSZliFCnUtJDk 6uu+sZeWkrPntlS8rPcGbNBPUhWUDm0kUG8F7W/hWja6C26G1VoRphzNb376xkoo72cS 8CUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EiPQ4pAH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c3-20020a17090620c300b008af42607e97si6753384ejc.379.2023.02.10.17.50.32; Fri, 10 Feb 2023 17:50:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EiPQ4pAH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229866AbjBKBqg (ORCPT <rfc822;ybw1215001957@gmail.com> + 99 others); Fri, 10 Feb 2023 20:46:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229889AbjBKBqd (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 10 Feb 2023 20:46:33 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E17E96ADD1 for <linux-kernel@vger.kernel.org>; Fri, 10 Feb 2023 17:46:32 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-507aac99fdfso66631067b3.11 for <linux-kernel@vger.kernel.org>; Fri, 10 Feb 2023 17:46:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=hkehzV3PgH5ghzIyq+2UQQQuaUD65ZdlOHTF9Rixj6U=; b=EiPQ4pAHrHy0B8yMqZ8yaO953edDmUSUzQj/xwAVtNYLDVQBdrP1aQyjZmq12LyB0o EQMYlzm0Esy/1xnxItCR2hr1ZOxRpoSJMdTb2h2MUAhEoTl/EmYqpiJYK3PssnuhbMOy G5CoXRTLEqmTXCJ2LCgAtHHzG2Ghij/qNtJ4lpWvIwMLHras06ZdUdHD6KCg2rTQVEEe pCVgSaRmHw3iav/061EaxrWaKtG5m9LrkyIa0nQqKTZpImolErRK8plBsl2kAvk3co0y uPxga+EwGqi4Vhpfuxef2EjKvWFseLoZAkZMoiVsP0WXFWWjCNGGk6Ung6GKU5H7DQvd UjJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hkehzV3PgH5ghzIyq+2UQQQuaUD65ZdlOHTF9Rixj6U=; b=tera9aWooCXdaSAQUDZ5aGEWeAd7OgGuc0knhVATrxwLovC5jgzFKpv1/Fml/BkKJS f+NC/IljoFYVZsveWLUkZiWhx017v6luZRZEqY68IenXQNUhBB940JmrI5hO0QeqIjkt q8qD9tV7BQDRB/1HIh2NbQ+Bs3SemBMcrw/sH6QF86uxn6toqmbNmPDkvWEQ2nEswOmx 1I0PHaUCCBOPwIGVT8OBYni5+/xftEygYuhYo97t9XUkbJEdw+eOvq1af7all7wdVQi2 VQ2D94o2MuPy31idXzXCUAUmKsrf13N+f1rBdlnnWfmaVliwf4G+990HCLFHI1hZxeIV u7AQ== X-Gm-Message-State: AO0yUKVeSvzBZ8xkQbXKe4HjASC7zjbNJQnJ9/t4X28vyzGaFccfnE7C tHDzAB2X8rJFom3If+b1V/9LTmoBBMjM X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a05:6902:1024:b0:8be:8c5f:9d55 with SMTP id x4-20020a056902102400b008be8c5f9d55mr178ybt.3.1676079992025; Fri, 10 Feb 2023 17:46:32 -0800 (PST) Date: Fri, 10 Feb 2023 17:46:19 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230211014626.3659152-1-vipinsh@google.com> Subject: [Patch v3 0/7] Optimize clear dirty log From: Vipin Sharma <vipinsh@google.com> To: seanjc@google.com, pbonzini@redhat.com, bgardon@google.com, dmatlack@google.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma <vipinsh@google.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757497530186715261?= X-GMAIL-MSGID: =?utf-8?q?1757497530186715261?= |
Series |
Optimize clear dirty log
|
|
Message
Vipin Sharma
Feb. 11, 2023, 1:46 a.m. UTC
Hi, This patch series has optimized control flow of clearing dirty log and improved its performance by ~40% (2% more than v2). It also got rid of many variants of the handle_changed_spte family of functions and converged logic to one handle_changed_spte() function. It also remove tdp_mmu_set_spte_no_[acc_track|dirty_log] and various booleans for controlling them. Thanks, Vipin v3: - Tried to do better job at writing commit messages. - Made kvm_tdp_mmu_clear_spte_bits() similar to the kvm_tdp_mmu_write_spte(). - clear_dirty_pt_masked() evaluates mask for the bit to be cleared outside the loop and use that for all of the SPTEs instead of calculating for each SPTE. - Some naming changes based on the feedbacks. - Split out the dead code clean from the optimization code. v2: https://lore.kernel.org/lkml/20230203192822.106773-1-vipinsh@google.com/ - Clear dirty log and age gfn range does not go through handle_changed_spte, they handle their SPTE changes locally to improve their speed. - Clear only specific bits atomically when updating SPTEs in clearing dirty log and aging gfn range functions. - Removed tdp_mmu_set_spte_no_[acc_track|dirty_log] APIs. - Converged all handle_changed_spte related functions to one place. v1: https://lore.kernel.org/lkml/20230125213857.824959-1-vipinsh@google.com/ Vipin Sharma (7): KVM: x86/mmu: Add a helper function to check if an SPTE needs atomic write KVM: x86/mmu: Atomically clear SPTE dirty state in the clear-dirty-log flow KVM: x86/mmu: Remove "record_dirty_log" in __tdp_mmu_set_spte() KVM: x86/mmu: Optimize SPTE change for aging gfn range KVM: x86/mmu: Remove "record_acc_track" in __tdp_mmu_set_spte() KVM: x86/mmu: Remove handle_changed_spte_dirty_log() KVM: x86/mmu: Merge all handle_changed_pte* functions. arch/x86/kvm/mmu/tdp_iter.h | 48 ++++++--- arch/x86/kvm/mmu/tdp_mmu.c | 190 ++++++++++++------------------------ 2 files changed, 96 insertions(+), 142 deletions(-)
Comments
On Fri, Feb 10, 2023, Vipin Sharma wrote: > This patch series has optimized control flow of clearing dirty log and > improved its performance by ~40% (2% more than v2). > > It also got rid of many variants of the handle_changed_spte family of > functions and converged logic to one handle_changed_spte() function. It > also remove tdp_mmu_set_spte_no_[acc_track|dirty_log] and various > booleans for controlling them. > > v3: > - Tried to do better job at writing commit messages. LOL, that's the spirit! Did a cursory glance, looks good. I'll do a more thorough pass next week and get it queued up if all goes well. No need for a v4 at this point, I'll fixup David's various nits when applying. I'll also add a link in patch 2 to the discussion about why we determined that bypassing __tdp_mmu_set_spte() is safe; that's critical information that isn't captured in the changelog.
On Fri, Mar 17, 2023 at 3:57 PM Sean Christopherson <seanjc@google.com> wrote: > > On Fri, Feb 10, 2023, Vipin Sharma wrote: > > This patch series has optimized control flow of clearing dirty log and > > improved its performance by ~40% (2% more than v2). > > > > It also got rid of many variants of the handle_changed_spte family of > > functions and converged logic to one handle_changed_spte() function. It > > also remove tdp_mmu_set_spte_no_[acc_track|dirty_log] and various > > booleans for controlling them. > > > > v3: > > - Tried to do better job at writing commit messages. > > LOL, that's the spirit! > > Did a cursory glance, looks good. I'll do a more thorough pass next week and get > it queued up if all goes well. No need for a v4 at this point, I'll fixup David's > various nits when applying. I'll also add a link in patch 2 to the discussion Yeah, he is too demanding! :p > about why we determined that bypassing __tdp_mmu_set_spte() is safe; that's critical > information that isn't captured in the changelog. Thanks!
On Fri, Mar 17, 2023, Sean Christopherson wrote: > On Fri, Feb 10, 2023, Vipin Sharma wrote: > > This patch series has optimized control flow of clearing dirty log and > > improved its performance by ~40% (2% more than v2). > > > > It also got rid of many variants of the handle_changed_spte family of > > functions and converged logic to one handle_changed_spte() function. It > > also remove tdp_mmu_set_spte_no_[acc_track|dirty_log] and various > > booleans for controlling them. > > > > v3: > > - Tried to do better job at writing commit messages. > > LOL, that's the spirit! > > Did a cursory glance, looks good. I'll do a more thorough pass next week and get > it queued up if all goes well. No need for a v4 at this point, I'll fixup David's > various nits when applying. Ooof, that ended up being painful. In hindsight, I should have asked for a v4, but damage done, and it's my fault for throwing you a big blob of code in the first place. I ended up splitting the "interesting" patches into three each: 1. Switch to the atomic-AND 2. Drop the access-tracking / dirty-logging (as appropriate) 3. Drop the call to __handle_changed_spte() because logically they are three different things (although obviously related). I have pushed the result to kvm-x86/mmu, but haven't merged to kvm-x86/next or sent thanks because it's not yet tested. I'll do testing tomorrow, but if you can take a look in the meantime to make sure I didn't do something completely boneheaded, it'd be much appreciated.
On Mon, Mar 20, 2023 at 5:41 PM Sean Christopherson <seanjc@google.com> wrote: > > On Fri, Mar 17, 2023, Sean Christopherson wrote: > > Did a cursory glance, looks good. I'll do a more thorough pass next week and get > > it queued up if all goes well. No need for a v4 at this point, I'll fixup David's > > various nits when applying. > > Ooof, that ended up being painful. In hindsight, I should have asked for a v4, > but damage done, and it's my fault for throwing you a big blob of code in the > first place. > > I ended up splitting the "interesting" patches into three each: > > 1. Switch to the atomic-AND > 2. Drop the access-tracking / dirty-logging (as appropriate) > 3. Drop the call to __handle_changed_spte() > > because logically they are three different things (although obviously related). > > I have pushed the result to kvm-x86/mmu, but haven't merged to kvm-x86/next or > sent thanks because it's not yet tested. I'll do testing tomorrow, but if you > can take a look in the meantime to make sure I didn't do something completely > boneheaded, it'd be much appreciated. Thanks for refactoring the patches. I reviewed the commits, no obvious red flags from my side. Few small nits I found: commit e534a94eac07 ("KVM: x86/mmu: Use kvm_ad_enabled() to determine if TDP MMU SPTEs need wrprot") - kvm_ad_enabled() should be outside the loop. commit 69032b5d71ef (" KVM: x86/mmu: Atomically clear SPTE dirty state in the clear-dirty-log flow") - MMU_WARN_ON(kvm_ad_enabled() && spte_ad_need_write_protect(iter.old_spte) should be after if(iter.level > PG_LEVEL_4k...) commit 93c375bb6aea ("KVM: x86/mmu: Bypass __handle_changed_spte() when clearing TDP MMU dirty bits") - Needs new performance numbers. Adding MMU_WARN_ON() might change numbers. I will run a perf test on your mmu branch and see if something changes a lot.
On Tue, Mar 21, 2023, Vipin Sharma wrote: > On Mon, Mar 20, 2023 at 5:41 PM Sean Christopherson <seanjc@google.com> wrote: > > > > On Fri, Mar 17, 2023, Sean Christopherson wrote: > > > Did a cursory glance, looks good. I'll do a more thorough pass next week and get > > > it queued up if all goes well. No need for a v4 at this point, I'll fixup David's > > > various nits when applying. > > > > Ooof, that ended up being painful. In hindsight, I should have asked for a v4, > > but damage done, and it's my fault for throwing you a big blob of code in the > > first place. > > > > I ended up splitting the "interesting" patches into three each: > > > > 1. Switch to the atomic-AND > > 2. Drop the access-tracking / dirty-logging (as appropriate) > > 3. Drop the call to __handle_changed_spte() > > > > because logically they are three different things (although obviously related). > > > > I have pushed the result to kvm-x86/mmu, but haven't merged to kvm-x86/next or > > sent thanks because it's not yet tested. I'll do testing tomorrow, but if you > > can take a look in the meantime to make sure I didn't do something completely > > boneheaded, it'd be much appreciated. > > > Thanks for refactoring the patches. I reviewed the commits, no obvious > red flags from my side. Few small nits I found: > > commit e534a94eac07 ("KVM: x86/mmu: Use kvm_ad_enabled() to determine > if TDP MMU SPTEs need wrprot") > - kvm_ad_enabled() should be outside the loop. Hmm, I deliberately left it inside the loop, but I agree that it would be better to hoist it out in that commit. > commit 69032b5d71ef (" KVM: x86/mmu: Atomically clear SPTE dirty state > in the clear-dirty-log flow") > - MMU_WARN_ON(kvm_ad_enabled() && > spte_ad_need_write_protect(iter.old_spte) should be after > if(iter.level > PG_LEVEL_4k...) Ah, hrm. This was also deliberate, but looking at the diff I agree that relative to the diff, it's an unnecessary/unrelated change. I think what I'll do is land the assertion above the "if (iter.level > PG_LEVEL_4K ||" in the above commit that switches to kvm_ad_enabled(). That way there shouldn't be any change for the assertion in this commit. > commit 93c375bb6aea ("KVM: x86/mmu: Bypass __handle_changed_spte() > when clearing TDP MMU dirty bits") > - Needs new performance numbers. Adding MMU_WARN_ON() might change > numbers. I will run a perf test on your mmu branch and see if > something changes a lot. It won't. MMU_WARN_ON() is dead code without manual modification to define MMU_DEBUG. Part of the reason I used MMU_WARN_ON() was to remind myself to send a patch/series to overhaul MMU_WARN_ON[*]. My thought/hope is that a Kconfig will allow developers and testers to run with a pile of assertions and sanity checks without impacting the runtime overhead for production builds. [*] https://lore.kernel.org/all/Yz4Qi7cn7TWTWQjj@google.com/
On Tue, Mar 21, 2023, Sean Christopherson wrote: > On Tue, Mar 21, 2023, Vipin Sharma wrote: > > On Mon, Mar 20, 2023 at 5:41 PM Sean Christopherson <seanjc@google.com> wrote: > > > > > > On Fri, Mar 17, 2023, Sean Christopherson wrote: > > > > Did a cursory glance, looks good. I'll do a more thorough pass next week and get > > > > it queued up if all goes well. No need for a v4 at this point, I'll fixup David's > > > > various nits when applying. > > > > > > Ooof, that ended up being painful. In hindsight, I should have asked for a v4, > > > but damage done, and it's my fault for throwing you a big blob of code in the > > > first place. > > > > > > I ended up splitting the "interesting" patches into three each: > > > > > > 1. Switch to the atomic-AND > > > 2. Drop the access-tracking / dirty-logging (as appropriate) > > > 3. Drop the call to __handle_changed_spte() > > > > > > because logically they are three different things (although obviously related). > > > > > > I have pushed the result to kvm-x86/mmu, but haven't merged to kvm-x86/next or > > > sent thanks because it's not yet tested. I'll do testing tomorrow, but if you > > > can take a look in the meantime to make sure I didn't do something completely > > > boneheaded, it'd be much appreciated. > > > > > > Thanks for refactoring the patches. I reviewed the commits, no obvious > > red flags from my side. Few small nits I found: > > > > commit e534a94eac07 ("KVM: x86/mmu: Use kvm_ad_enabled() to determine > > if TDP MMU SPTEs need wrprot") > > - kvm_ad_enabled() should be outside the loop. > > Hmm, I deliberately left it inside the loop, but I agree that it would be better > to hoist it out in that commit. > > > commit 69032b5d71ef (" KVM: x86/mmu: Atomically clear SPTE dirty state > > in the clear-dirty-log flow") > > - MMU_WARN_ON(kvm_ad_enabled() && > > spte_ad_need_write_protect(iter.old_spte) should be after > > if(iter.level > PG_LEVEL_4k...) > > Ah, hrm. This was also deliberate, but looking at the diff I agree that relative > to the diff, it's an unnecessary/unrelated change. I think what I'll do is > land the assertion above the "if (iter.level > PG_LEVEL_4K ||" in the above > commit that switches to kvm_ad_enabled(). That way there shouldn't be any change > for the assertion in this commit. Aha! Even better, split this into yet one more patch to dedup the guts before switching to the atomic-AND, and give clear_dirty_gfn_range() the same treatment. That further isolates the changes, provides solid justification for hoisting the kvm_ad_enabled() check out of the loop (it's basically guaranteed to be a single memory read that hits the L1), and keeps clear_dirty_gfn_range() and clear_dirty_pt_masked() as similar as is reasonably possible. Speaking of which, I'll send a patch to remove the redundant is_shadow_present_pte() check in clear_dirty_gfn_range(), that's already handled by tdp_root_for_each_leaf_pte().
On Tue, Mar 21, 2023, Sean Christopherson wrote: > It won't. MMU_WARN_ON() is dead code without manual modification to define MMU_DEBUG. > Part of the reason I used MMU_WARN_ON() was to remind myself to send a patch/series > to overhaul MMU_WARN_ON[*]. My thought/hope is that a Kconfig will allow developers > and testers to run with a pile of assertions and sanity checks without impacting > the runtime overhead for production builds. > > [*] https://lore.kernel.org/all/Yz4Qi7cn7TWTWQjj@google.com/ Ugh, I'm definitely sending that patch, MMU_DEBUG has bitrotted and broken the build yet again. arch/x86/kvm/mmu/mmu.c: In function ‘kvm_mmu_free_shadow_page’: arch/x86/kvm/mmu/mmu.c:1738:15: error: implicit declaration of function ‘is_empty_shadow_page’; did you mean ‘to_shadow_page’? [-Werror=implicit-function-declaration] 1738 | MMU_WARN_ON(!is_empty_shadow_page(sp->spt)); | ^~~~~~~~~~~~~~~~~~~~ include/asm-generic/bug.h:110:25: note: in definition of macro ‘WARN_ON_ONCE’ 110 | int __ret_warn_on = !!(condition); \ | ^~~~~~~~~ arch/x86/kvm/mmu/mmu.c:1738:2: note: in expansion of macro ‘MMU_WARN_ON’ 1738 | MMU_WARN_ON(!is_empty_shadow_page(sp->spt)); | ^~~~~~~~~~~