Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3216674rdb; Tue, 6 Feb 2024 10:21:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGhKXk6BB1xviVMemCzQ3sln+Q7Gv9wcAe3WKKjqzE4c/k+zS+X6u1s45XT7k6vOeBiXL9m X-Received: by 2002:a05:6359:6399:b0:176:8332:c661 with SMTP id sg25-20020a056359639900b001768332c661mr361652rwb.25.1707243672341; Tue, 06 Feb 2024 10:21:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707243672; cv=pass; d=google.com; s=arc-20160816; b=Qu22lUG+FMlofDi55j6YX4o3vLCg6VQz4bYCeH65U7wiH9qj2fBKKiYua6uB49KaiR jmE/dRcoaMoUT3T6TuoNFGuPns5wOGVSiE9s2/WWhbpiIQX3IVUYdKAkon9nNC1dS88m lxF9m8zas+q13SUIVehejnoWEfm6DozYPy4wIvo4QE2Dd7h307rt9tx9v0t5iFsj3zoK URjjadpLlXtBnY48WzFem6IDZgZLHrPo1+oGJ8bFrpg5DkSbtaJsCc20GLGevR5hB2et DTZYSs01YMx0Cg5xUfDbKy2ukxlTOYC2OvrXJBUQrLmuyFw4GzUtIbHCF5Po1tVOCASE 08Hw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=YWkxJlkXZn2XKzfrRn210wVpvl5epIQvB1ytCNVqVwI=; fh=hV42idRGT944iqzfNzSozijDAmuWcKSDoyadQ7z3nec=; b=Z/rGyPo8kbIN3M45mqygT3C7QPFG7pVTzWzvoJvSsM1iTsS6X0XetSdsORbgIeQLL2 5soYbH6s70T/EV90JpDp0HkGvS1WlpYmp1GCNZQ5zLHY9wJ59E7imqP0jBDYMHwciY8P gQRWXYp96RMBOeL0psar2ZQUFucTJmsvMLopXQX+zBeqxr/UyFUw3NPzSCpdfIyKeTsK GPCPdvJ1pKXsV6PeeoC/+1RSBZgJ5tl1F9LLXMnvL/mYWYZtLkd3A4ojGT3YZpbMtpG5 +RjG/XDDUI/D/gtchjvOIo7yvc5axJS2v4bU6kw8HnPJrGkCAFxbFyBJ/5JlgIqHj6f3 pSaQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4fBZle52; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-55448-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55448-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXSniXC7Q4x28sNES6lMQU/KgrNjVNSCQRUztP07Kl0c16sxXPHkuQc+zLJuyi2y38e+T0cD0mb9sv9YkZ2mERsCVUe6/W5FNxm4Q3qHA== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id u21-20020a62d455000000b006e0500ee05dsi1958386pfl.160.2024.02.06.10.21.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 10:21:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55448-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4fBZle52; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-55448-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55448-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id EC8F1285E9A for ; Tue, 6 Feb 2024 18:21:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E2EB612B8B; Tue, 6 Feb 2024 18:21:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4fBZle52" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 5DD14C142 for ; Tue, 6 Feb 2024 18:21:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707243665; cv=none; b=hWgFS4ux/Y52B2Vx1jwFyZ8Td0Iol2rnmmxI4J0c93htkxugXjw1B6BB7TK/RIAvSGjXXqH9tNc4RANutk1WKW1jK5dvo7mE9DLFRctO+I+wILoSUGea6/wT4xNesN8VAGADTF6v/w+3y74M8OXxrxoyXsv33WR7prLtaEnM6EQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707243665; c=relaxed/simple; bh=/MlM5rAxErSc52f8uAboDn9Px2mtKuGXMTnYEUz69g4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u7qz7G2A0Sc9GxpyTvkgUJi1y0KHxjEawbTQ+d+/HwVu1wd/WWe8RWBEvQ7tVA/HGRnCV1i5HWSB9ny8zTSGzjJ6iGki5NZAChADhqGnl3W+WYStTD9rWkRkTr0H/NzafYJ45jn1eXvRv38pAeh3RbuSDwIrFPklXftDn9M7iDQ= 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=4fBZle52; arc=none smtp.client-ip=209.85.128.201 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60481d2c9d8so12815977b3.0 for ; Tue, 06 Feb 2024 10:21:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707243662; x=1707848462; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YWkxJlkXZn2XKzfrRn210wVpvl5epIQvB1ytCNVqVwI=; b=4fBZle52k1ZTsQ8Q3ingUJouXzR8PGGblLmjqm6RzuUG4W0ZVXp3+DaQN6gcyP4VPh eFdAlcYllbAeAOr3ZyzGCByoXNY/2Z9t8Fd8mUzWRhvWG94u/+r2sZZ2NpWTj3hjGEEO Q38Z7PdStGh9m+ryqLOS6d5/QrRbeaVUGxAaI5dnC/hgxJOH8Hs0uEes6mzWFOvepmIM cqD0ZiO2AemIxClFff/LUYP7prWT48ooiL9H2jMj7BQvmHfj7jGe+Z2anKiDT0Ebo64t C9lVb5c9nV13a/QbxlRwPRAAugjdnIfHw7uj6ay06eVcAanbMt1aktjFXEN1615bjAKy D74g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707243662; x=1707848462; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YWkxJlkXZn2XKzfrRn210wVpvl5epIQvB1ytCNVqVwI=; b=w4AJD+1iws6+Ym0w9Lf6shSSqUta5TknvpOSDKneMwyFa+ooMFyXoC0scLpXCGI+LJ 7Cp/tZHyW/KrMRp4ZUKUk7N7YNPNqQSUG7GUNz63QvHSu8viq+ts1e/ijZhIrL2PbaLM TN9SuE0AiV5XXf7b6y7WifeRxC0QQXx2GjpJvXDD0gG/g0vheDuwk2jIT8zxL4y67ijx xMzhms/DEZB7jNzq1VdITor7NfPIzPvHrEMzqbtjW8t7NhPiX7FdTsze8MlI8DW6qedB CS+un+OQsj4qZHV6dAJgIzWrA/UANJWYsHvbrDokE0yF8dPbT9Y24BJe//fpHeHI4sjN S7Qg== X-Gm-Message-State: AOJu0Yxhui0a6u02Zi+rJTzP8UwZsj62wIBHaFJH6HPAXrW4gBm6Z9HR /ZarHLszP/E3gztiQQ59XjQjfYIEiJOiAcuLUM1EvFkz6ag74FqQPcorLCEjTOn/CXjHGf+vDY1 Jmg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2842:b0:dc6:519b:5425 with SMTP id ee2-20020a056902284200b00dc6519b5425mr612720ybb.11.1707243662363; Tue, 06 Feb 2024 10:21:02 -0800 (PST) Date: Tue, 6 Feb 2024 10:21:00 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240111020048.844847-1-seanjc@google.com> <20240111020048.844847-7-seanjc@google.com> Message-ID: Subject: Re: [PATCH 6/8] KVM: x86/mmu: Check for usable TDP MMU root while holding mmu_lock for read From: Sean Christopherson To: Xu Yilun Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Pattara Teerapong Content-Type: text/plain; charset="us-ascii" On Tue, Feb 06, 2024, Xu Yilun wrote: > On Tue, Feb 06, 2024 at 06:09:18PM +0800, Xu Yilun wrote: > > On Wed, Jan 10, 2024 at 06:00:46PM -0800, Sean Christopherson wrote: > > > When allocating a new TDP MMU root, check for a usable root while holding > > > mmu_lock for read and only acquire mmu_lock for write if a new root needs > > > to be created. There is no need to serialize other MMU operations if a > > > vCPU is simply grabbing a reference to an existing root, holding mmu_lock > > > for write is "necessary" (spoiler alert, it's not strictly necessary) only > > > to ensure KVM doesn't end up with duplicate roots. > > > > > > Allowing vCPUs to get "new" roots in parallel is beneficial to VM boot and > > > to setups that frequently delete memslots, i.e. which force all vCPUs to > > > reload all roots. > > > > > > Signed-off-by: Sean Christopherson > > > --- > > > arch/x86/kvm/mmu/mmu.c | 8 ++--- > > > arch/x86/kvm/mmu/tdp_mmu.c | 60 +++++++++++++++++++++++++++++++------- > > > arch/x86/kvm/mmu/tdp_mmu.h | 2 +- > > > 3 files changed, 55 insertions(+), 15 deletions(-) > > > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > index 3c844e428684..ea18aca23196 100644 > > > --- a/arch/x86/kvm/mmu/mmu.c > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > @@ -3693,15 +3693,15 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) > > > unsigned i; > > > int r; > > > > > > + if (tdp_mmu_enabled) > > > + return kvm_tdp_mmu_alloc_root(vcpu); > > > + > > > write_lock(&vcpu->kvm->mmu_lock); > > > r = make_mmu_pages_available(vcpu); > > > if (r < 0) > > > goto out_unlock; > > > > > > - if (tdp_mmu_enabled) { > > > - root = kvm_tdp_mmu_get_vcpu_root_hpa(vcpu); > > > - mmu->root.hpa = root; > > > - } else if (shadow_root_level >= PT64_ROOT_4LEVEL) { > > > + if (shadow_root_level >= PT64_ROOT_4LEVEL) { > > > root = mmu_alloc_root(vcpu, 0, 0, shadow_root_level); > > > mmu->root.hpa = root; > > > } else if (shadow_root_level == PT32E_ROOT_LEVEL) { > > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > > > index e0a8343f66dc..9a8250a14fc1 100644 > > > --- a/arch/x86/kvm/mmu/tdp_mmu.c > > > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > > > @@ -223,21 +223,52 @@ static void tdp_mmu_init_child_sp(struct kvm_mmu_page *child_sp, > > > tdp_mmu_init_sp(child_sp, iter->sptep, iter->gfn, role); > > > } > > > > > > -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) > > > +static struct kvm_mmu_page *kvm_tdp_mmu_try_get_root(struct kvm_vcpu *vcpu) > > > { > > > union kvm_mmu_page_role role = vcpu->arch.mmu->root_role; > > > + int as_id = kvm_mmu_role_as_id(role); > > > struct kvm *kvm = vcpu->kvm; > > > struct kvm_mmu_page *root; > > > > > > - lockdep_assert_held_write(&kvm->mmu_lock); > > > - > > > - /* Check for an existing root before allocating a new one. */ > > > - for_each_valid_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) { > > > - if (root->role.word == role.word && > > > - kvm_tdp_mmu_get_root(root)) > > > - goto out; > > > + for_each_valid_tdp_mmu_root_yield_safe(kvm, root, as_id) { > > > > No lock yielding attempt in this loop, why change to _yield_safe version? Because variants that don't allow yielding, i.e. for_each_valid_tdp_mmu_root() as of this patch, require mmu_lock be held for write. Holding mmu_lock for write is necessary because that simpler version uses list_for_each_entry() and doesn't grab a reference to the root, i.e. entries on the list could be freed, e.g. by kvm_tdp_mmu_zap_invalidated_roots(). The _yield_safe() versions don't require the user to want to yield. The naming is _yield_safe() because the yield-safe iterators can run with mmu_lock held for read *or* right. > Oh, I assume you just want to early exit the loop with the reference to > root hold. But I feel it makes harder for us to have a clear > understanding of the usage of _yield_safe and non _yield_safe helpers. > > Maybe change it back? No. There's even a comment above for_each_tdp_mmu_root() (which is for_each_valid_tdp_mmu_root() as of this patch) that explains the difference. The rule is essentially, use the yield-safe variant unless there's a good reason not to. /* * Iterate over all TDP MMU roots. Requires that mmu_lock be held for write, * the implication being that any flow that holds mmu_lock for read is * inherently yield-friendly and should use the yield-safe variant above. * Holding mmu_lock for write obviates the need for RCU protection as the list * is guaranteed to be stable. */