Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3210686rdb; Tue, 6 Feb 2024 10:10:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBTWM5/a+w26qFwGlDsQf4Dq1SYkHyPoCx1HTIl567lbDGRR4FFpv0937KavV8agzNXma/ X-Received: by 2002:a17:902:d2c2:b0:1d9:d300:a69d with SMTP id n2-20020a170902d2c200b001d9d300a69dmr2721944plc.11.1707243058638; Tue, 06 Feb 2024 10:10:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707243058; cv=pass; d=google.com; s=arc-20160816; b=o36SvGROnH5QiAwaSeBC3+jHp2cC1gLjw76tPD7JaDjhx8on63eiS1QfXmwmxsDooi Z15GrJVTR/GQ+yTFF7fgvenPoKSnMNAzKIvVxxACyo6DFCXks36Kh7Mqcis5pC2HNgk7 rBeNXZ7Q6ANyFuUI+bCmcuMa0nEel1md6SLwSenWGS6SYgJFDsq0uS8Y1Yn/gkalxjTU 58uTzIF5cYfMVjBR3Cc2//oR6WdyiIBzTxLT/ZpwIoSXNczAaSw8yK1niCCxIQbe2t4x 1ywGUfeEQleeLWSpw2z11oaj1XMn9n/07NuxaTtavHAyNnTyproPy6/C3PyjULG1/ldH DofQ== 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=l5PpeFFqdP626r1RWYoCpd0vGeqNqbDwh/mrIllBBs0=; fh=o6XNnceyeBejqt4aV3OXCUF6R5yF1ciIIzf8qogqoPY=; b=Masary1UZEbuATsnRLup4hVwcGvJ1jxj1rm/Wgha9ByaZyY1H9ocPVjcC5IIWKHakx xB77sNx+y9VnC//2XHriDsZuN5r9h5rZGSvOx13mWVXnsR3GaQxtMvMDSP0sw5RXivQl GpKa3f/CCcZVmUHO0VEtsf4uIf/YhyEPbnfAdoBZGU3ZI8z8lhVXshvFg2+DiFroG7CU u9CmcGAyppGXleMN2uv7Oh5VLlaVemUH4Fn++Hz3KTmJmHe2yVKQBLLxze/sp1jinros xI+F5bBbIGKgcnxgNjGftE3DGxPJkNgAUcg/Q6T3Hp9hWNgvpH/lKzOpoHq9w1DoLYdu I9UA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=FgvvNVaG; 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-55442-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55442-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; AJvYcCUcP48cHf4kiZ8YBAM7Ix9PS3lNMcWZVBijx35xks+dcucMQAu3csN1RPpu1O36RU302glHnPNvfUxOH44aQknO8189aXa6BADgsM8ZXg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s1-20020a170902c64100b001d9af7357a0si1939866pls.622.2024.02.06.10.10.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 10:10:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55442-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=FgvvNVaG; 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-55442-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55442-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 45A82284A48 for ; Tue, 6 Feb 2024 18:10:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F3B4125B6; Tue, 6 Feb 2024 18:10:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FgvvNVaG" Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 5665BDF42 for ; Tue, 6 Feb 2024 18:10:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707243047; cv=none; b=a07XImC1kunEVCtGDvnGPMz5zIK/HZDSJQGh/LBqAgAFBhmy5if2DCgCAc9XWoOUUptI0CGBjKJOypRcQki5Hk7jTftCoxnIPYWW7wszqAL4CfiZlFtn32EYmQgcAjtP6ZqQ7XLzhz8SAcJp83ptuOA22tPzzsBLgmnU4oBVtxs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707243047; c=relaxed/simple; bh=+K4hkLAVOGFO0w9WP+eWJkb0HlQJo+4K5nbl+fFFDd0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T4+7lolLD/9JnmvF+G95ZYgvCCh68Uau1rZrU0+l3k0RTEy+05KGc2J96nEQ1NtsFsduiL43TupwNjhEoc9f11/lnr6kHcfmuQPBFjmGMkSYh0W3Adzxyf3pw09RJKZzlgSFlqn6Lufu7jb0Hm0SG+6bV49CSNadq27vz7JzK+Q= 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=FgvvNVaG; arc=none smtp.client-ip=209.85.215.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-pg1-f201.google.com with SMTP id 41be03b00d2f7-5ca5b61c841so4213989a12.3 for ; Tue, 06 Feb 2024 10:10:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707243045; x=1707847845; 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=l5PpeFFqdP626r1RWYoCpd0vGeqNqbDwh/mrIllBBs0=; b=FgvvNVaGjmIXER1pWm0mJz7IZdvsjRiBc9IijX8SzzB5iXmJxhRuVTtQ96w0lkrBBN rkt4C9H9+nYzsWpbuSOIEhuwRW1egFwyWTwx/+MyddsDqR5OKHKqweN3ryWaGxx2elBZ RW00EZFQdTIkOwQRmCJRxdQEGO21sqaTsoi25wHt71o5XtkCBQnC3p1qPk0GJHcItP0S YCkV7VVOh5QTexBKz/x2rXYpd9+/POp/bT2eeNWhaE61+znQCAkwVLmH1px/OVeJ7doP 9Fj4XLU5tyMuWNQnwWT1jbkdrA69rwoiqMYiEV4pFTto1Xe/+H660r1V0GHf/MTaBH+Q 1PdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707243045; x=1707847845; 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=l5PpeFFqdP626r1RWYoCpd0vGeqNqbDwh/mrIllBBs0=; b=XnVPHmvsA1xgaSUUHPSWHsJMjbQ+ynMEVw7UKXnKCbAc1v62ooOLcXjs5sBLTERi2e cDywT5U25tuTtaF68eCWzP0EuL3B0FVmhOOq5j+dY6vHKgy1lMH7w+s5Ov62o4qyz8nc PYgwi7rnqoYRtzxwhd+x3wxRzcdSK978TXXQM8lQdddu+e02wJkqxYcPJYpBLLfGpsKq w3bIIwiyv3k1ySsC/uXlsAq6M78d98sumR29JoFaT/JPXdFes0yGABaH35Y3W/Phwlmv NZ+WwuNUMR0yLBvavPvD7+iX0HU7ecLwzIkVgEbbefqW/I7ZVz+lEDvMrtPgJj5KKzRE Oc+A== X-Gm-Message-State: AOJu0YxEVjQhmkciSPUX7AQQdjxueykDeo3+CmtaMSdrf9/Tqjly7xrA 4aIqCoX41yJNKo9VkPEDdnMk+1qXbtj8ZUxOfJMuzH0dB0psCzWyWPisoCGawfyLm25C6mwhRZT SRA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:4e8d:b0:295:c117:d9e9 with SMTP id sr13-20020a17090b4e8d00b00295c117d9e9mr597pjb.4.1707243045534; Tue, 06 Feb 2024 10:10:45 -0800 (PST) Date: Tue, 6 Feb 2024 10:10:44 -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-8-seanjc@google.com> Message-ID: Subject: Re: [PATCH 7/8] KVM: x86/mmu: Alloc TDP MMU roots 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 Wed, Jan 10, 2024 at 06:00:47PM -0800, Sean Christopherson wrote: > > --- > > arch/x86/kvm/mmu/tdp_mmu.c | 55 +++++++++++++++----------------------- > > 1 file changed, 22 insertions(+), 33 deletions(-) > > > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > > index 9a8250a14fc1..d078157e62aa 100644 > > --- a/arch/x86/kvm/mmu/tdp_mmu.c > > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > > @@ -223,51 +223,42 @@ static void tdp_mmu_init_child_sp(struct kvm_mmu_page *child_sp, > > tdp_mmu_init_sp(child_sp, iter->sptep, iter->gfn, role); > > } > > > > -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; > > - > > - for_each_valid_tdp_mmu_root_yield_safe(kvm, root, as_id) { > > - if (root->role.word == role.word) > > - return root; > > - } > > - > > - return NULL; > > -} > > - > > int kvm_tdp_mmu_alloc_root(struct kvm_vcpu *vcpu) > > { > > struct kvm_mmu *mmu = vcpu->arch.mmu; > > union kvm_mmu_page_role role = mmu->root_role; > > + int as_id = kvm_mmu_role_as_id(role); > > struct kvm *kvm = vcpu->kvm; > > struct kvm_mmu_page *root; > > > > /* > > - * Check for an existing root while holding mmu_lock for read to avoid > > + * Check for an existing root before acquiring the pages lock to avoid > > * unnecessary serialization if multiple vCPUs are loading a new root. > > * E.g. when bringing up secondary vCPUs, KVM will already have created > > * a valid root on behalf of the primary vCPU. > > */ > > read_lock(&kvm->mmu_lock); > > - root = kvm_tdp_mmu_try_get_root(vcpu); > > - read_unlock(&kvm->mmu_lock); > > > > - if (root) > > - goto out; > > + for_each_valid_tdp_mmu_root_yield_safe(kvm, root, as_id) { > > + if (root->role.word == role.word) > > + goto out_read_unlock; > > + } > > > > - write_lock(&kvm->mmu_lock); > > It seems really complex to me... > > I failed to understand why the following KVM_BUG_ON() could be avoided > without the mmu_lock for write. I thought a valid root could be added > during zapping. > > void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm) > { > struct kvm_mmu_page *root; > > read_lock(&kvm->mmu_lock); > > for_each_tdp_mmu_root_yield_safe(kvm, root) { > if (!root->tdp_mmu_scheduled_root_to_zap) > continue; > > root->tdp_mmu_scheduled_root_to_zap = false; > KVM_BUG_ON(!root->role.invalid, kvm); tdp_mmu_scheduled_root_to_zap is set only when mmu_lock is held for write, i.e. it's mutually exclusive with allocating a new root. And tdp_mmu_scheduled_root_to_zap is cleared if and only if kvm_tdp_mmu_zap_invalidated_roots is already set, and is only processed by kvm_tdp_mmu_zap_invalidated_roots(), which runs under slots_lock (a mutex). So a new, valid root can be added, but it won't have tdp_mmu_scheduled_root_to_zap set, at least not until the current "fast zap" completes and a new one beings, which as above requires taking mmu_lock for write.