Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp877756pxf; Thu, 1 Apr 2021 16:39:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxxfZQIfAoqWbOvruAwoulkQhYLxBJXFvqvQr3QUnBCSqjEMbpgUB7VQjZA59I2URlsLtg X-Received: by 2002:a17:906:845b:: with SMTP id e27mr11249578ejy.487.1617320386423; Thu, 01 Apr 2021 16:39:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617320386; cv=none; d=google.com; s=arc-20160816; b=id47A4RfzSWXTKcq4RYH9EpigVYNWQVeqCjfFT/K6EVxggBIHMIKZNicP3pGxH47wW CD3OujsOYJ7reDa9coFZX2zb+KdB5NkOh71H6r8L7DXOp8F+swfUhTZFH9wOIEd/lmTI ezQpnYHpVlfb0iMtQDCzniCZOA4LgHbTfMOJfUpajuqVLUXvRn5qcO4A9iNLkPFqtteM xHC3NV/KtCCodaHpUztM+cd16dX99yDgpHWQzu265BT3jgS1FAzJLWaWc41L/LwZOBro Wxy5mr6J2In+T0LTImPosg/kHpo+36kABingWWpNHsWhooLP15dzZhW8a0KS0NbyFDDo p/ig== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=jyvW4LNHh147mdThm9gosLlXYiBlNPi18fgfUMETNdI=; b=0JcsHQ4mfHWIDKaToHQSJNH0uol+mufrMn5sLiP+a9gqXbAfmmEnFa3mpnItIDSScW w2wm9maYF4EcqpWeREdO2Imf/ULJWGXjxSUbcKvvbIuMX1XIul14556uiHMvAMcm8MaO bavvkv1udyGgc/WbuDQIHu38w2qp38zAznEp6BADuulArOnUfV1nMQLMe1rcIWqocO6G E0Ab24JAha41A6ZVB5QuyM1Ybg8717JMfXLdJS/euivMlqF/LKeJX+rSrjojluGln6pc jYiFm9fDgNsLPJaMJdFqsMw4w5UZUTqiBN1y4SXmuyx3apErOo8qOgdTCjt42n0okaoc HVWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ls53YaRS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g24si4935623ejw.478.2021.04.01.16.39.23; Thu, 01 Apr 2021 16:39:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ls53YaRS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S236468AbhDAXi1 (ORCPT + 99 others); Thu, 1 Apr 2021 19:38:27 -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-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