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 X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD0C9C433B4 for ; Thu, 1 Apr 2021 23:38:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A44B961106 for ; Thu, 1 Apr 2021 23:38:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236464AbhDAXi3 (ORCPT ); Thu, 1 Apr 2021 19:38:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236406AbhDAXiS (ORCPT ); Thu, 1 Apr 2021 19:38:18 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43457C0617A7 for ; Thu, 1 Apr 2021 16:38:11 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id lj2so532641pjb.1 for ; Thu, 01 Apr 2021 16:38:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=jyvW4LNHh147mdThm9gosLlXYiBlNPi18fgfUMETNdI=; b=Ls53YaRSYleBvtnGSnOtnGhpJ+KAGk+cAjcd6YVP/n1m+LchacpRgKeT3kK2zR8rfI w4ph7/7nn7Oo/slE/ICq8USQGPcNNLOWpHko7IsGkAwKaSE2fizCBw+EkZWa6STw6OeJ wDwd6hx59PKT0yqYjwxfm9TPEeDA582DN1F8kRcudjoqevO7NxCPcXN/Dytliaz8Z2tg U/24p0P6U+CiIvCzCBl8YWSgqkmvqo44c98AB1Niv9JraoS2SeB5peAUh2feiu8sm25X X24W/lgHQEqHVevHOMTc86AzOiqBHnZ0wsoRPaX9oWHgicSnZ/0HiZAJ+c1YQVUwlFvv 0AQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jyvW4LNHh147mdThm9gosLlXYiBlNPi18fgfUMETNdI=; b=ZW2g34W6V3FWQSCTRgpvZIZhaWrDNiqi6OWu72RF82IZe3xxBynwJKEMtUgqskIVhF wSHb3auntmafYTh9awMLVWeymQTS9arehgCJ8MbWkRq8L0UaxI+kCGc/7VhNdpls/OHx nZ7fR2YYdLHkRLAgcgeV2Ra1632Q2F3NOoD5UhoPaTRpQmBtVUJrky+2DMHsgteUM+V+ V6fGo/XO1ckkTfS8bc01R+bvWTRA9JEMEWGD+CrXzjyzWaVz431WihVB/GRZ6lhLKjkp FlAXOq1dRYa1QuxVo2N1mOl7AiWrN8cCczPTD0Q5g2jLbIPx8tnToPegKNEVPmqq7exG HYIg== X-Gm-Message-State: AOAM533Gc/E2R803Fwkx898GHpYkAotqRlQkXYp9FYsAYFvWaGIGGiRv APSXFgtEDZtW4uh/OGg/1mOwQb6rcrjqAsHn5gE8mH+viLuQxr9VzLLLQ5Xq5xkvpLzzKxbMZWM 9uS9zp7ckNB+IGC7G53llcT1bxx0/ci+cgqhj0T6WQHfsdtkrGWSCqWQx6UXVAdRweQ7/+r8a X-Google-Smtp-Source: ABdhPJxQDpBYMeHhgFid5KdIpSvqSiyYK3tQXEPJMuHmLxfp8z14DglaxX1A9e7pcuiYwLUiiF4Od+d8RuU9 X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:e088:88b8:ea4a:22b6]) (user=bgardon job=sendgmr) by 2002:a17:902:d351:b029:e8:ba10:e6f5 with SMTP id l17-20020a170902d351b02900e8ba10e6f5mr1117239plk.82.1617320290640; Thu, 01 Apr 2021 16:38:10 -0700 (PDT) Date: Thu, 1 Apr 2021 16:37:30 -0700 In-Reply-To: <20210401233736.638171-1-bgardon@google.com> Message-Id: <20210401233736.638171-8-bgardon@google.com> Mime-Version: 1.0 References: <20210401233736.638171-1-bgardon@google.com> X-Mailer: git-send-email 2.31.0.208.g409f899ff0-goog Subject: [PATCH v2 07/13] KVM: x86/mmu: handle cmpxchg failure in kvm_tdp_mmu_get_root From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , Peter Shier , Peter Feiner , Junaid Shahid , Jim Mattson , Yulei Zhang , Wanpeng Li , Vitaly Kuznetsov , Xiao Guangrong , Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To reduce dependence on the MMU write lock, don't rely on the assumption that the atomic operation in kvm_tdp_mmu_get_root will always succeed. By not relying on that assumption, threads do not need to hold the MMU lock in write mode in order to take a reference on a TDP MMU root. In the root iterator, this change means that some roots might have to be skipped if they are found to have a zero refcount. This will still never happen as of this patch, but a future patch will need that flexibility to make the root iterator safe under the MMU read lock. Signed-off-by: Ben Gardon --- arch/x86/kvm/mmu/tdp_mmu.c | 11 ++++++----- arch/x86/kvm/mmu/tdp_mmu.h | 13 +++---------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 697ea882a3e4..886bc170f2a5 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -88,10 +88,12 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, next_root = list_first_entry(&kvm->arch.tdp_mmu_roots, typeof(*next_root), link); + while (!list_entry_is_head(next_root, &kvm->arch.tdp_mmu_roots, link) && + !kvm_tdp_mmu_get_root(kvm, next_root)) + next_root = list_next_entry(next_root, link); + if (list_entry_is_head(next_root, &kvm->arch.tdp_mmu_roots, link)) next_root = NULL; - else - kvm_tdp_mmu_get_root(kvm, next_root); if (prev_root) kvm_tdp_mmu_put_root(kvm, prev_root); @@ -161,10 +163,9 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) /* Check for an existing root before allocating a new one. */ 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); + if (root->role.word == role.word && + kvm_tdp_mmu_get_root(kvm, root)) goto out; - } } root = alloc_tdp_mmu_page(vcpu, 0, vcpu->arch.mmu->shadow_root_level); diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h index 1ec7914ecff9..f0a26214e999 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.h +++ b/arch/x86/kvm/mmu/tdp_mmu.h @@ -7,17 +7,10 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu); -static inline void kvm_tdp_mmu_get_root(struct kvm *kvm, - struct kvm_mmu_page *root) +__must_check static inline bool kvm_tdp_mmu_get_root(struct kvm *kvm, + struct kvm_mmu_page *root) { - lockdep_assert_held_write(&kvm->mmu_lock); - - /* - * This should never fail since roots are removed from the roots - * list under the MMU write lock when their reference count falls - * to zero. - */ - refcount_inc_not_zero(&root->tdp_mmu_root_count); + return refcount_inc_not_zero(&root->tdp_mmu_root_count); } void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root); -- 2.31.0.208.g409f899ff0-goog