From nobody Tue Nov 26 08:37:08 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8029D1FB3D1 for ; Fri, 11 Oct 2024 02:11:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728612687; cv=none; b=tROObznqdRYa0Tcd1MHn6o47jC9jtX0VSlpPaJwFA+lF1KH3IrxRhivPHTGxn5ayaV/ZyS5tYyENSZ+vTHbKpZziBL8b8LlKl45LsgEPYNgY3J0DVrP91gUTkJBEzior0hVhyWWfn3RCQs7ORWUlJ3J3aIKruA0mmdmdC6xcKRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728612687; c=relaxed/simple; bh=LNXuTGR2S+iPgGGEX03rlcOFXKsH1faw0ZPmJ58DiOE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=L9qmva7jq7pJf7RABVn2MbEZMl00AnfI2jU/47zSvw7kx7GJWFFGd0sA8Fcbp4b4HLDlLscj7vWPVTMlsBe6lF3r7M88ccaLf/WSSNJZLGHA8DlTjXLQR4QHnflz2OCTNu8twcmPnMVsWW3u6TmCiTeYN7JaHM7QP+v3lEnZJsU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FdgOyntl; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FdgOyntl" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7ea38f581c9so1614823a12.0 for ; Thu, 10 Oct 2024 19:11:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728612685; x=1729217485; darn=vger.kernel.org; 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=CEg0IFfdUu4UELrQOXo0lUcfHQKNBNnbMq1djiDkWGQ=; b=FdgOyntliR+R53ku1l+8JOs7WqGHgzY5J/dK5vP4+B/B6lw9kgD+qUduihpl8medjW ms2yX97jx7+MevWVzcwv0I/VWrHae4vpGT5Umxt+PwocoL3karL2rybxy7ihNByAsvT7 cTNCE6S6CdhsYwEIcq5LbRSPEAm3smllOxDeTPuvw405/MbFTfPdsKRgcMS7KFMkkL4J sdD4CEmksfwBvI1oo/NZq+KhmArReJnxzUjI4LEFmtgJid28LVEhAeHD9fC7gqK8Ko4h pQLCcYdRzS6bcYh3GRxkYFg9XyWm4WD+6SInx85SwUnlAP8G4+fqi1tNIsJNleQv2owJ pWuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728612685; x=1729217485; 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=CEg0IFfdUu4UELrQOXo0lUcfHQKNBNnbMq1djiDkWGQ=; b=vHQ7pL0cE65g5DodCDmmDdx4tq6NFh8gMi86tupZiMPQ97vIzwSHxxFFW6AZbYQSpG msEXTmCTNP1G18sgBvI8zPYBom6I1HpcWK6mrOTNTz7Y2KvdS5OfWoQOGqqgn2teRRti lKeqXvIEy4T5+OWuTPzpSkdT/hR0k0+2h7N+aukGp4P6BGJmhfX/oMUisfXvlDXygWo5 ajvOaKcQ+0aNl/A/YE9vHiAgor1TiXC6BzIrygddVCS2X4SR02HBnmHo4PCigx2Z53iv 19z2yq37nEA/Hvt2QZsJcVX8SWJllKkAQhuJYmf1eSIEvm1Ry0iGaAclVYntBP9KRiB7 hflw== X-Forwarded-Encrypted: i=1; AJvYcCV1y/zQJb2N1HWCdnhNIgbXFyrMTQNmA+8UPEya92Ow+SFrMS2u9Mbp2RFk0zUQ+NsowVTrUi+KqSjtmgo=@vger.kernel.org X-Gm-Message-State: AOJu0YzjKatzSZjYJD96KrOpjDhP/U9MBVbGFejRN16rpW3fcByziQZm Rf1Ek5Y2XgnrE2YQ48/gLpD5GVhWY59Mt2WEwh3JxXSkK2KR4N+Fp9fBLvQq8RNTVT1XDYwEbj8 syw== X-Google-Smtp-Source: AGHT+IHOAZfcUyAOkGK8+VjEG0Gb80ABIViSbVLqAW+P+s1wcIQgbwkQ7kkxk7jRjasA/YCkTBO3YYYn+wk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a05:6a02:f8a:b0:7cd:8b5f:2567 with SMTP id 41be03b00d2f7-7ea535255a7mr911a12.4.1728612683541; Thu, 10 Oct 2024 19:11:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 10 Oct 2024 19:10:47 -0700 In-Reply-To: <20241011021051.1557902-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011021051.1557902-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011021051.1557902-16-seanjc@google.com> Subject: [PATCH 15/18] KVM: x86/mmu: Dedup logic for detecting TLB flushes on leaf SPTE changes From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yan Zhao , Sagi Shahar , "=?UTF-8?q?Alex=20Benn=C3=A9e?=" , David Matlack , James Houghton Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that the shadow MMU and TDP MMU have identical logic for detecting required TLB flushes when updating SPTEs, move said logic to a helper so that the TDP MMU code can benefit from the comments that are currently exclusive to the shadow MMU. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 19 +------------------ arch/x86/kvm/mmu/spte.h | 29 +++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 +-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 5be3b5f054f1..f75915ff33be 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -488,23 +488,6 @@ static void mmu_spte_set(u64 *sptep, u64 new_spte) /* Rules for using mmu_spte_update: * Update the state bits, it means the mapped pfn is not changed. * - * If the MMU-writable flag is cleared, i.e. the SPTE is write-protected f= or - * write-tracking, remote TLBs must be flushed, even if the SPTE was read-= only, - * as KVM allows stale Writable TLB entries to exist. When dirty logging,= KVM - * flushes TLBs based on whether or not dirty bitmap/ring entries were rea= ped, - * not whether or not SPTEs were modified, i.e. only the write-tracking ca= se - * needs to flush at the time the SPTEs is modified, before dropping mmu_l= ock. - * - * Don't flush if the Accessed bit is cleared, as access tracking tolerates - * false negatives, and the one path that does care about TLB flushes, - * kvm_mmu_notifier_clear_flush_young(), flushes if a young SPTE is found,= i.e. - * doesn't rely on lower helpers to detect the need to flush. - * - * Lastly, don't flush if the Dirty bit is cleared, as KVM unconditionally - * flushes when enabling dirty logging (see kvm_mmu_slot_apply_flags()), a= nd - * when clearing dirty logs, KVM flushes based on whether or not dirty ent= ries - * were reaped from the bitmap/ring, not whether or not dirty SPTEs were f= ound. - * * Returns true if the TLB needs to be flushed */ static bool mmu_spte_update(u64 *sptep, u64 new_spte) @@ -527,7 +510,7 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte) WARN_ON_ONCE(!is_shadow_present_pte(old_spte) || spte_to_pfn(old_spte) !=3D spte_to_pfn(new_spte)); =20 - return is_mmu_writable_spte(old_spte) && !is_mmu_writable_spte(new_spte); + return is_tlb_flush_required_for_leaf_spte(old_spte, new_spte); } =20 /* diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index c8dc75337c8b..a404279ba731 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -467,6 +467,35 @@ static inline bool is_mmu_writable_spte(u64 spte) return spte & shadow_mmu_writable_mask; } =20 +/* + * If the MMU-writable flag is cleared, i.e. the SPTE is write-protected f= or + * write-tracking, remote TLBs must be flushed, even if the SPTE was read-= only, + * as KVM allows stale Writable TLB entries to exist. When dirty logging,= KVM + * flushes TLBs based on whether or not dirty bitmap/ring entries were rea= ped, + * not whether or not SPTEs were modified, i.e. only the write-tracking ca= se + * needs to flush at the time the SPTEs is modified, before dropping mmu_l= ock. + * + * Don't flush if the Accessed bit is cleared, as access tracking tolerates + * false negatives, and the one path that does care about TLB flushes, + * kvm_mmu_notifier_clear_flush_young(), flushes if a young SPTE is found,= i.e. + * doesn't rely on lower helpers to detect the need to flush. + * + * Lastly, don't flush if the Dirty bit is cleared, as KVM unconditionally + * flushes when enabling dirty logging (see kvm_mmu_slot_apply_flags()), a= nd + * when clearing dirty logs, KVM flushes based on whether or not dirty ent= ries + * were reaped from the bitmap/ring, not whether or not dirty SPTEs were f= ound. + * + * Note, this logic only applies to shadow-present leaf SPTEs. The caller= is + * responsible for checking that the old SPTE is shadow-present, and is al= so + * responsible for determining whether or not a TLB flush is required when + * modifying a shadow-present non-leaf SPTE. + */ +static inline bool is_tlb_flush_required_for_leaf_spte(u64 old_spte, + u64 new_spte) +{ + return is_mmu_writable_spte(old_spte) && !is_mmu_writable_spte(new_spte); +} + static inline u64 get_mmio_spte_generation(u64 spte) { u64 gen; diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index f412bca206c5..615c6a84fd60 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1034,8 +1034,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm= _vcpu *vcpu, return RET_PF_RETRY; else if (is_shadow_present_pte(iter->old_spte) && (!is_last_spte(iter->old_spte, iter->level) || - WARN_ON_ONCE(is_mmu_writable_spte(iter->old_spte) && - !is_mmu_writable_spte(new_spte)))) + WARN_ON_ONCE(is_tlb_flush_required_for_leaf_spte(iter->old_spte, new_s= pte)))) kvm_flush_remote_tlbs_gfn(vcpu->kvm, iter->gfn, iter->level); =20 /* --=20 2.47.0.rc1.288.g06298d1525-goog