Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp1549053qtg; Tue, 21 Mar 2023 15:02:37 -0700 (PDT) X-Google-Smtp-Source: AK7set91UvYw8sg2cOAf2z3S1sYnCj0VDIJxPrUMFZPgxebID9Qn4kTmYF1o/jXTk6jndk0MICVw X-Received: by 2002:a17:902:7297:b0:197:8e8e:f15 with SMTP id d23-20020a170902729700b001978e8e0f15mr3546052pll.6.1679436157624; Tue, 21 Mar 2023 15:02:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679436157; cv=none; d=google.com; s=arc-20160816; b=OQwQLwtu+H57L42sGSxkBx1qmlhTu5PXvonjDIIa57jVxVeyy5QVmJgoFo3iRhZ2ta UtpVYr06ijKgi5+/tq+yIy6g0JcE09YI1GRFQ8kn9hpJD56Uj3QXE/3y58npFfZnMcpd M7VgauFGzr3gS7SjWdoQ8vP9aRblW00B0GmHmErTZkV1HxTAtA21oAm7fuWmRDhdsJPP rtRaV6mY8mwHuWG4Nte3NIgSRrGvruwSonG8LcH7qQQOyIHm6rF0ZHbkJyu7gdmzwM4o JQMznF4eWZdgZ9yC/n/R4pdvUmmVi/7rHhS6xySYPBnSkMNv+cykPSA/8vudHeJasfP8 9hog== 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:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=l3JzYD0u087M8uWewJDs63iio7s7LCBf0UooyaaQZak=; b=g+snhjdGbEXdCZYv/P+5wG2tWUMP02TzZuKSFTdqsxWkZX6kXQD/N953KjiPDxUe69 cdEqbyBiBjfcPbE2KkrXV1fwJ90QyPKTpuMbVhPoRwEHHOTiQlbngaYygQ9+U+astcqh l5BvW0Sf+CwhEVZu3h8ilNbxbN7coNmWAN3J+MILmnwwKhSRov+wd9fCt42pWwucm/zN 4L4KcSlqoqbv3/hEzCFh/V1CsYX9ilmicpvht/ZSXEcY/2BnemdEX2GAMbCNM4PLlZCu so1adIxTIzHW42Z5iKLdzYtW3HDqjf+sYPgmN2D4Zi0MvCaqDpzU81mO2bgQKDzWknPO yWpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=sPfaqDOE; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k16-20020a170902c41000b001a072aedec9si16749325plk.377.2023.03.21.15.02.24; Tue, 21 Mar 2023 15:02:37 -0700 (PDT) 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=sPfaqDOE; 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 S229789AbjCUWBU (ORCPT + 99 others); Tue, 21 Mar 2023 18:01:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230221AbjCUWBA (ORCPT ); Tue, 21 Mar 2023 18:01:00 -0400 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 76B2858C06 for ; Tue, 21 Mar 2023 15:00:38 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-540e3b152a3so167691837b3.2 for ; Tue, 21 Mar 2023 15:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679436037; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=l3JzYD0u087M8uWewJDs63iio7s7LCBf0UooyaaQZak=; b=sPfaqDOE7yWhSuiUIZf0xd+k0PDvbafdbkdbHy9oZQ50zsLIKXlNo4EApjcM1z4yXY 9/ZRMbMHteBU7UnlaUQlJB83EZFWV6DqQLgv/3l95DfrZ07rsVGjeprJ4Jlt9TGlI6A1 Mz9CElVjeppNfWHMD3wYKaOjvH2MuEeM3SMsArkPXwg3cpCopPUsnPJU0ggZI/oGJMKj t2zLZFRLThgRMWoaLHb3zSCfQKrIbKvLhuYiQXflYK36QWSPTN1q4hq5AH8M3dpCnyW4 hB0Ewf4SzzL44avvLXFFBq2aomaMHTMr3mMskq3FCfPKAsd8Hca1vd6hDtU9ajGSQbby jbtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679436037; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=l3JzYD0u087M8uWewJDs63iio7s7LCBf0UooyaaQZak=; b=YTX8bQc8NFfTmBRrPcEV7V/q31+XPv7VStnnFAUyT2whPIY4RWu5ybB15pIhBanNqi rEwG+2hGb+CY/y4TrM1ndzDSVP6MTsOYmq/Xxn9U2+oz956RaJBoPQhtC8GaoP5QHiAS fTbNDr9pCE4dTrICwVietsMeiGdPz+6gDGix4hEqqZ6eTmGH3eJlhJbygkd+7i2ZsPzf mGzzcZ7CkNM942Iwp0ZrR8zDEHxgB9xeGlQHrZO0DkC7tIqVid8YM76mbaEUCZUGDcRg 7AV34FX4EgCOp7UqIs+BiJFvr4NvhGd9ZwGKtFR9lHuFkzeoOobhPOv/RcxlgdgRtBlk 3RbA== X-Gm-Message-State: AAQBX9cUZuu1TIiF4o8y1pcIdZ19MJfPpdKkJ+qUzBr+0ak57Dmq8nm9 udeqffPX2cp5uqsXD9W0qxWzu4a5ni4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:283:b0:b6e:d788:eba7 with SMTP id v3-20020a056902028300b00b6ed788eba7mr2491158ybh.6.1679436037531; Tue, 21 Mar 2023 15:00:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 21 Mar 2023 15:00:16 -0700 In-Reply-To: <20230321220021.2119033-1-seanjc@google.com> Mime-Version: 1.0 References: <20230321220021.2119033-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <20230321220021.2119033-9-seanjc@google.com> Subject: [PATCH v4 08/13] KVM: x86/mmu: Clear only A-bit (if enabled) when aging TDP MMU SPTEs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma , David Matlack , Ben Gardon Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.7 required=5.0 tests=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=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org From: Vipin Sharma Use tdp_mmu_clear_spte_bits() when clearing the Accessed bit in TDP MMU SPTEs so as to use an atomic-AND instead of XCHG to clear the A-bit. Similar to the D-bit story, this will allow KVM to bypass __handle_changed_spte() by ensuring only the A-bit is modified. Link: https://lore.kernel.org/all/Y9HcHRBShQgjxsQb@google.com Signed-off-by: Vipin Sharma Reviewed-by: David Matlack [sean: massage changelog] Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/tdp_mmu.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 3cc81fa22b7f..adbdfed287cc 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -756,13 +756,6 @@ static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, _tdp_mmu_set_spte(kvm, iter, new_spte, true); } -static inline void tdp_mmu_set_spte_no_acc_track(struct kvm *kvm, - struct tdp_iter *iter, - u64 new_spte) -{ - _tdp_mmu_set_spte(kvm, iter, new_spte, false); -} - #define tdp_root_for_each_pte(_iter, _root, _start, _end) \ for_each_tdp_pte(_iter, _root, _start, _end) @@ -1248,33 +1241,44 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm, /* * Mark the SPTEs range of GFNs [start, end) unaccessed and return non-zero * if any of the GFNs in the range have been accessed. + * + * No need to mark the corresponding PFN as accessed as this call is coming + * from the clear_young() or clear_flush_young() notifier, which uses the + * return value to determine if the page has been accessed. */ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter, struct kvm_gfn_range *range) { - u64 new_spte = 0; + u64 new_spte; /* If we have a non-accessed entry we don't need to change the pte. */ if (!is_accessed_spte(iter->old_spte)) return false; - new_spte = iter->old_spte; - - if (spte_ad_enabled(new_spte)) { - new_spte &= ~shadow_accessed_mask; + if (spte_ad_enabled(iter->old_spte)) { + iter->old_spte = tdp_mmu_clear_spte_bits(iter->sptep, + iter->old_spte, + shadow_accessed_mask, + iter->level); + new_spte = iter->old_spte & ~shadow_accessed_mask; } else { /* * Capture the dirty status of the page, so that it doesn't get * lost when the SPTE is marked for access tracking. */ - if (is_writable_pte(new_spte)) - kvm_set_pfn_dirty(spte_to_pfn(new_spte)); + if (is_writable_pte(iter->old_spte)) + kvm_set_pfn_dirty(spte_to_pfn(iter->old_spte)); - new_spte = mark_spte_for_access_track(new_spte); + new_spte = mark_spte_for_access_track(iter->old_spte); + iter->old_spte = kvm_tdp_mmu_write_spte(iter->sptep, + iter->old_spte, new_spte, + iter->level); } - tdp_mmu_set_spte_no_acc_track(kvm, iter, new_spte); - + __handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte, + new_spte, iter->level, false); + handle_changed_spte_dirty_log(kvm, iter->as_id, iter->gfn, + iter->old_spte, new_spte, iter->level); return true; } -- 2.40.0.rc2.332.ga46443480c-goog