From mboxrd@z Thu Jan 1 00:00:00 1970 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 DBB16C433F5 for ; Thu, 23 Dec 2021 22:23:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350442AbhLWWXx (ORCPT ); Thu, 23 Dec 2021 17:23:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350417AbhLWWXp (ORCPT ); Thu, 23 Dec 2021 17:23:45 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7420FC061757 for ; Thu, 23 Dec 2021 14:23:45 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id k21-20020a63f015000000b0033db7baf101so3863612pgh.19 for ; Thu, 23 Dec 2021 14:23:45 -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=O0B5yHs9mrLguToeJUNtqwnxH46d2zShhuTlEqWnd2Y=; b=j3f476DX6cFUy1LeHxbgKU/39XYmfhXM7YCJPoGXYJXG4tzh/TIg4LDroE2Eh8ExsZ AszwXw0eWIPsVr7PnWBZcgNh6EpXOAOPb0XC3CmRJXBSR0HJsp62GWyPgDvuFruj8vHG PfjgOdZhKq9insutYDBhPQnerzAdn3MEPZpEXni76KbXEiDUAu39kuYLUx949LrpK2l1 s/7T1AusdxAnqAKm683N6/qpuVdUd+mEwrFZpFkZm23/wPMb7dnPnFgoASe+Av8Kv9gF KGyCf757+Ogsd5v6vCHUBpuzle1TjAWrsR3KpYnAOnCNESSYhkGC/FZIGaJCqBdVbDsS uxWA== 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=O0B5yHs9mrLguToeJUNtqwnxH46d2zShhuTlEqWnd2Y=; b=ECA/t4rhc4Wsh+OtZdXGiqIj/Z7NKINwXQsC/O6yujrwrNnO14uXxACLZ5CZcH4gDO JsnfdbWTJYFnl17JCFfom5Q30/tUzmpZSlqtlYpsGQ1WnGkBmSao3zAYgnbgy4EuTNck uTrVoXlnkxnQ3l/5G9GDX+MEA68H3/lTCIEt5fiCXOdjO1wnMFLPPJ7jYn8UdCWicGH1 lrW7uV4Zvo3J5nb7IvK/R0qZgJUiuw0oCoZu66/N+7HYAz2m0EIEFe6aZIgSAsytHkMb T/0/yt+MVvdqAuapR3DxdXq2Ory71GbOmMMTRHmquDlxqQHnKIuAXGkibzrqwDzV6zYS nbIA== X-Gm-Message-State: AOAM530WLH6yFGyN077uH4cfhx0eryVMS0ImMwtghiP7NvaAC0WWAtXF Qze6jZ1yZLYjXrl0hndhErYgkXiWv9U= X-Google-Smtp-Source: ABdhPJwz12iTb+zG1EDREi+ibfO4OLS6nmunCF6taJTygfFDtz9t2fFNJG5TmDYp4Fppj1Dyf3gqG6psTDE= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2187:b0:4a7:fedf:8ff9 with SMTP id h7-20020a056a00218700b004a7fedf8ff9mr4129022pfi.9.1640298224993; Thu, 23 Dec 2021 14:23:44 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 23 Dec 2021 22:22:50 +0000 In-Reply-To: <20211223222318.1039223-1-seanjc@google.com> Message-Id: <20211223222318.1039223-3-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 02/30] KVM: x86/mmu: Move "invalid" check out of kvm_tdp_mmu_get_root() 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 Move the check for an invalid root out kvm_tdp_mmu_get_root() and into the one place it actually matters, tdp_mmu_next_root(), as the other user already has an implicit validity check. A future bug fix will need to get references to invalid roots to honor mmu_notifier requests, there's no point in forcing what will be a common path to open code getting a reference to a root. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/tdp_mmu.c | 12 ++++++++++-- arch/x86/kvm/mmu/tdp_mmu.h | 3 --- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index d320b56d5cd7..200001190fcf 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -121,9 +121,14 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, next_root = list_first_or_null_rcu(&kvm->arch.tdp_mmu_roots, typeof(*next_root), link); - while (next_root && !kvm_tdp_mmu_get_root(kvm, next_root)) + while (next_root) { + if (!next_root->role.invalid && + kvm_tdp_mmu_get_root(kvm, next_root)) + break; + next_root = list_next_or_null_rcu(&kvm->arch.tdp_mmu_roots, &next_root->link, typeof(*next_root), link); + } rcu_read_unlock(); @@ -200,7 +205,10 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) role = page_role_for_level(vcpu, vcpu->arch.mmu->shadow_root_level); - /* Check for an existing root before allocating a new one. */ + /* + * Check for an existing root before allocating a new one. Note, the + * role check prevents consuming an invalid root. + */ for_each_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) { if (root->role.word == role.word && kvm_tdp_mmu_get_root(kvm, root)) diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h index 3899004a5d91..08c917511fed 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.h +++ b/arch/x86/kvm/mmu/tdp_mmu.h @@ -10,9 +10,6 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu); __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm *kvm, struct kvm_mmu_page *root) { - if (root->role.invalid) - return false; - return refcount_inc_not_zero(&root->tdp_mmu_root_count); } -- 2.34.1.448.ga2b2bfdf31-goog