Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF50EC4332F for ; Thu, 23 Dec 2021 22:24:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350538AbhLWWYd (ORCPT ); Thu, 23 Dec 2021 17:24:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350582AbhLWWYE (ORCPT ); Thu, 23 Dec 2021 17:24:04 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C0F2C061763 for ; Thu, 23 Dec 2021 14:24:04 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id i3-20020a628703000000b004ba462357d6so3977743pfe.23 for ; Thu, 23 Dec 2021 14:24:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=BNNkx5iDOAmq+UI4vOkeZBthRbL3U3mJe4quYYQ4WIc=; b=ELA1VF+P7266N3tFYQZiX57UD7CJ9nzv9z97D9a7u4zYcnIW7U/YemfMb25FGYe3mH DRcZ5+3JvJ3mc5zka3nwe/0efPQHWwqiH2XmUenaRFwXPha7bT4tkZkZ+wpVlrdhuIyI 52KVymQHUuzdgNJIfAjF2sZZzraeV3GlVvPdMK0AbxuVVvCRA+bA4oC3rXrqu0FpUzMz Sshre2GWrf7S/JYqmudAL+Yr8BmDPGAX7GNqyuUzioByhRgT7qLgIZG8yuq5T4uie9AL kAGV03e4SN2X10if6CXLT4Z8o09/HhDJvZKfIzigw1YxZIW0KXN8X8EYd6xUjFIdd9fh oQeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=BNNkx5iDOAmq+UI4vOkeZBthRbL3U3mJe4quYYQ4WIc=; b=DyENjzR0d1M09kRCoYW8oHbKSMkAMV8SCq5jw9lF4s0aL4LzF2xUjRGixZyPaX06yd OvqsNUBmAk0W1WnWFflyuvxLVW1qfpOfVTGxbpgdlCAaLNkix/mPMdlxNams3Y3RvUZL CTxsyVhcGY/yGua5oVWw1/ENW4BqTXrgFdt9Vh2uf6AWQHbAruzQ0cxeurNRe1R2xTl4 DNaLK5m49w2iSNGFCfpMKniVR8Azqr4CsMJSYUakmaIYkzvT6U7oC2j7DjIl1lKY4XKH VW/G8+YX9ywZqKPl1PgggXhM/u6h5AhMDZfGjkUfV04qpFgB6ePHjJcwxxhMGGFiU6kG F0kg== X-Gm-Message-State: AOAM530QPrYoTJ3Ozjug55q5H2yKFG0nWVkBn8wuYVgnGuDj49mbH/R5 QaTmgzDVS+Ktd8kA1ijloc1R6vbJR3M= X-Google-Smtp-Source: ABdhPJxrCGl+X5LAgyGiaOibHR8XaD5zaKzAM30Bk0/2KTJtfA6wwRp3IzYbzyk0a1nr52rei86LKPIdARQ= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:90a:3d42:: with SMTP id o2mr738615pjf.1.1640298243595; Thu, 23 Dec 2021 14:24:03 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 23 Dec 2021 22:23:02 +0000 In-Reply-To: <20211223222318.1039223-1-seanjc@google.com> Message-Id: <20211223222318.1039223-15-seanjc@google.com> Mime-Version: 1.0 References: <20211223222318.1039223-1-seanjc@google.com> X-Mailer: git-send-email 2.34.1.448.ga2b2bfdf31-goog Subject: [PATCH v2 14/30] KVM: x86/mmu: Add helpers to read/write TDP MMU SPTEs and document RCU From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , David Matlack , Mingwei Zhang Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add helpers to read and write TDP MMU SPTEs instead of open coding rcu_dereference() all over the place, and to provide a convenient location to document why KVM doesn't exempt holding mmu_lock for write from having to hold RCU (and any future changes to the rules). No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Ben Gardon --- arch/x86/kvm/mmu/tdp_iter.c | 6 +++--- arch/x86/kvm/mmu/tdp_iter.h | 16 ++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 14 +++++++------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c index caa96c270b95..de31f3e68668 100644 --- a/arch/x86/kvm/mmu/tdp_iter.c +++ b/arch/x86/kvm/mmu/tdp_iter.c @@ -12,7 +12,7 @@ static void tdp_iter_refresh_sptep(struct tdp_iter *iter) { iter->sptep = iter->pt_path[iter->level - 1] + SHADOW_PT_INDEX(iter->gfn << PAGE_SHIFT, iter->level); - iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep)); + iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep); } static gfn_t round_gfn_for_level(gfn_t gfn, int level) @@ -87,7 +87,7 @@ static bool try_step_down(struct tdp_iter *iter) * Reread the SPTE before stepping down to avoid traversing into page * tables that are no longer linked from this entry. */ - iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep)); + iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep); child_pt = spte_to_child_pt(iter->old_spte, iter->level); if (!child_pt) @@ -121,7 +121,7 @@ static bool try_step_side(struct tdp_iter *iter) iter->gfn += KVM_PAGES_PER_HPAGE(iter->level); iter->next_last_level_gfn = iter->gfn; iter->sptep++; - iter->old_spte = READ_ONCE(*rcu_dereference(iter->sptep)); + iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep); return true; } diff --git a/arch/x86/kvm/mmu/tdp_iter.h b/arch/x86/kvm/mmu/tdp_iter.h index e19cabbcb65c..3cdfaf391a49 100644 --- a/arch/x86/kvm/mmu/tdp_iter.h +++ b/arch/x86/kvm/mmu/tdp_iter.h @@ -9,6 +9,22 @@ typedef u64 __rcu *tdp_ptep_t; +/* + * TDP MMU SPTEs are RCU protected to allow paging structures (non-leaf SPTEs) + * to be zapped while holding mmu_lock for read. Holding RCU isn't required for + * correctness if mmu_lock is held for write, but plumbing "struct kvm" down to + * the lower* depths of the TDP MMU just to make lockdep happy is a nightmare, + * so all* accesses to SPTEs are must be done under RCU protection. + */ +static inline u64 kvm_tdp_mmu_read_spte(tdp_ptep_t sptep) +{ + return READ_ONCE(*rcu_dereference(sptep)); +} +static inline void kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 val) +{ + WRITE_ONCE(*rcu_dereference(sptep), val); +} + /* * A TDP iterator performs a pre-order walk over a TDP paging structure. */ diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 47424e22a681..41c3a1cff3e7 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -603,7 +603,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm, * here since the SPTE is going from non-present * to non-present. */ - WRITE_ONCE(*rcu_dereference(iter->sptep), 0); + kvm_tdp_mmu_write_spte(iter->sptep, 0); return true; } @@ -642,7 +642,7 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, */ WARN_ON(is_removed_spte(iter->old_spte)); - WRITE_ONCE(*rcu_dereference(iter->sptep), new_spte); + kvm_tdp_mmu_write_spte(iter->sptep, new_spte); __handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte, new_spte, iter->level, false); @@ -807,7 +807,7 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, * The iter must explicitly re-read the SPTE because * the atomic cmpxchg failed. */ - iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep)); + iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep); goto retry; } } @@ -1011,7 +1011,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) * because the new value informs the !present * path below. */ - iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep)); + iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep); } if (!is_shadow_present_pte(iter.old_spte)) { @@ -1217,7 +1217,7 @@ static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, * The iter must explicitly re-read the SPTE because * the atomic cmpxchg failed. */ - iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep)); + iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep); goto retry; } spte_set = true; @@ -1288,7 +1288,7 @@ static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, * The iter must explicitly re-read the SPTE because * the atomic cmpxchg failed. */ - iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep)); + iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep); goto retry; } spte_set = true; @@ -1419,7 +1419,7 @@ static void zap_collapsible_spte_range(struct kvm *kvm, * The iter must explicitly re-read the SPTE because * the atomic cmpxchg failed. */ - iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep)); + iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep); goto retry; } } -- 2.34.1.448.ga2b2bfdf31-goog