Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp933776lqt; Fri, 7 Jun 2024 03:12:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+9p0x1oe+yXpVK1Gyjyrg5v8XU4pDIjKhnHvLEnCnxC/Ug1yQTzu6RjHpGUTMkIPwaDysRe6rJ+9jPVA03/FL4aJoQnYYN1LsKwfqWA== X-Google-Smtp-Source: AGHT+IGvK36yzVb1/YLFOgGn+qVnxbdV3SgIFMl6X6oc16iziZzjmel4SxwU4p/6Bd2Glz5uA7KX X-Received: by 2002:a17:902:ea08:b0:1f6:7a48:405c with SMTP id d9443c01a7336-1f6d03a06d0mr23236775ad.48.1717755152415; Fri, 07 Jun 2024 03:12:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717755152; cv=pass; d=google.com; s=arc-20160816; b=ecGGtviBcJ+feg8l/IgTWiW8VW6QkP7cVAcZvWZataPIJ0Ad7uAoP4AzCNo/mHBteA vQcznQ1acqosb4+w6/jglb7hxDL8OSnKIrWsV9iDCoz91zXBL/JTfuduo2PHp1HjyZDV 3RzK4Iv2t7WtML1OgxPcYInNttxUIW1tfMFNS81YN4kD1SpArWqDYYnUsWcIL80wz3HC /q0jdrwv8QwIi6KH2s8e6ov/o2WkGIqc8bbdH8iURTZhd3kvUD14gIkDTI/Q6anXVurp hc2vXBZwsGWzBp3h/mF8B2WcxSRCeQZdecLRbJc/lTj+Q4boiDbIUwYbe8oOvbPw37Se IIPw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=V1PfJJ2X6kfxVeld9qIvWb5WqYVs7ksNnwyBTZFTZh4=; fh=SKeZfxmPkOkm4uoYHbd7GBNxbXCSqulwzHke2aUvFDo=; b=yQvd9qwiNso7cq1ja/lJnqunck3aoG+odvPKdmgq31OFsOM1NImVviy9bTTML/tBwb zteGBz4PJwP479j71RV7htb3AphLK6vWsTp0VGDfREQFQBO0SjSm3mTXP5hqP75UlR+5 G0sMc3oKlZ+EZm1/LKVN4ys25K97mCdUt1JP0cR52h69cHXAsirW6lh4IYo8oU08hlCl PCrvNX7kZuyl/jJAGqcg7zzQ8Z5PnXCs/oWzWy1JdEANmP7lZwyTjaYDTj/ICP/JwyX9 3lJ6xCNamkdExTaZtffDgsNHPL5U48/ubj+Njw/DOW9kdJR38cA63b1kH5ql8kA/XXiM DmOg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ODiAS7do; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-205798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f6bd7d2d4csi7367535ad.286.2024.06.07.03.12.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 03:12:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ODiAS7do; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-205798-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 6F205B209B7 for ; Fri, 7 Jun 2024 10:11:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3DF9315CD65; Fri, 7 Jun 2024 10:10:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ODiAS7do" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 883A813F016 for ; Fri, 7 Jun 2024 10:10:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717755051; cv=none; b=dl5x2JkQJP4256UhfQLYthlgXG5Z30hvBxJZ6gqHo/erLFuGo0y9YBjRamhHb+3hIOpmruswmsmOz1iL1ufdQn05ApDpx5K+d6eLtANvbfY7puv/DHy2dFEtr0AA5wejYVLQfyAj91qz+ti8faJAwBRCYF9BXzwoLb+dP/pw5HI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717755051; c=relaxed/simple; bh=QW+p0LE+OA0sFKpEDoQlNfo/8sXx7142FTJZGgXwPxA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Ft4EOA6yn73lB3Atx+nx79mclJNgD+TLnZ1qJ/ziCdl8LaL0eqr2qPhfXtP+xaiPR8/vdWqlZz7S/4x4R8OYK1vdsRuFqzf+6NnEbca/MsBbHjEMEXH6e2kIogGgNBn1wEKdxVCo1WRlSK4ShmsKtI+VeEZQay8m7Zhnu70ovWM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ODiAS7do; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717755048; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V1PfJJ2X6kfxVeld9qIvWb5WqYVs7ksNnwyBTZFTZh4=; b=ODiAS7dojKLCdPKPxHHlOz2Sece/Y4b8ZhKutb3vm688iEMx9OoijQhAAEKvBTj7EIYgMQ qlyBK44tRE7dNFu9gy1OHCEqF4YlB2yp5JgLsY4fGh1EUrn+Q2+hGOs/bfmIwXeQ9rsrzx 91B72nA7iSQOWthY1exaFvs4jGZxfN4= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-450-LQvbI_4iPoiz85H-v2ySIA-1; Fri, 07 Jun 2024 06:10:47 -0400 X-MC-Unique: LQvbI_4iPoiz85H-v2ySIA-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-35dcd39c6ebso1939957f8f.1 for ; Fri, 07 Jun 2024 03:10:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717755046; x=1718359846; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V1PfJJ2X6kfxVeld9qIvWb5WqYVs7ksNnwyBTZFTZh4=; b=hTqmBuYI4luRaN49xuQOiaoevL9xqjz1FHhhen8Z4ABgO8JpH6njlLi+SEaHcNqiHN +ROta6HUAzjhhy1aXXYc1uPxwr74IfREVIqfxC1CQOXmyt8wrlJHFWWv+SUIlh0n+Bw7 XiWSCnqF5AYlDceMTUDeEr0ANTVc8ku5htZ3qAKFcfD/qDU6pUixRA6aiLBTKbDHfsVx A0O9dz3l1jQFaj/91cPOibPMgNeBiLMBmNzP7RMBalXkej+GEzvVaWB/vVS7PHnSUKPK PBaioQufRrFdq6/9bOo1Yuqa+u/AM2KkxK4JMTeiewgb/PEgKwHwbP+pV77Q2uli/o6e rEnQ== X-Forwarded-Encrypted: i=1; AJvYcCV3SRTeQfqYih0MVu5ePIhNpGo/XIxLkdd5/lzGuQGmxVpiYexDzYuOAT93oH0m3M4gZALeiE3VLdSnIgi5KiDP8sh+bkOU3fkxWjq4 X-Gm-Message-State: AOJu0Yzpi+AXdZv8SsII/H6Jmce750aVJIGm04Uk4Jdc5+avKKGl/WWs GNN9rax4PS8cF5oq5op4TIsYaSN8YtzcdlykbyqihvM0CHLERlpVNR1OCtE8C57GNV53uuUqBV9 eseHhMVL4BWnmUSvRzI5aPCfPzpSA4etb6BsO5X6DDtLRKBJTf/zCpeaI4AjTVtm4krz0FQWDvs 4YlfrH2HK3XaXwH7ae6SFnrSPvgNNyQ41+p5Ur X-Received: by 2002:a5d:4e08:0:b0:34c:fd92:3359 with SMTP id ffacd0b85a97d-35efea29070mr2152890f8f.21.1717755046092; Fri, 07 Jun 2024 03:10:46 -0700 (PDT) X-Received: by 2002:a5d:4e08:0:b0:34c:fd92:3359 with SMTP id ffacd0b85a97d-35efea29070mr2152844f8f.21.1717755045636; Fri, 07 Jun 2024 03:10:45 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240530210714.364118-1-rick.p.edgecombe@intel.com> <20240530210714.364118-11-rick.p.edgecombe@intel.com> In-Reply-To: <20240530210714.364118-11-rick.p.edgecombe@intel.com> From: Paolo Bonzini Date: Fri, 7 Jun 2024 12:10:33 +0200 Message-ID: Subject: Re: [PATCH v2 10/15] KVM: x86/tdp_mmu: Reflect building mirror page tables To: Rick Edgecombe Cc: seanjc@google.com, kvm@vger.kernel.org, kai.huang@intel.com, dmatlack@google.com, erdemaktas@google.com, isaku.yamahata@gmail.com, linux-kernel@vger.kernel.org, sagis@google.com, yan.y.zhao@intel.com, Isaku Yamahata Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, May 30, 2024 at 11:07=E2=80=AFPM Rick Edgecombe wrote: > + /* Update mirrored mapping with page table link */ > + int (*reflect_link_spt)(struct kvm *kvm, gfn_t gfn, enum pg_level= level, > + void *mirrored_spt); > + /* Update the mirrored page table from spte getting set */ > + int (*reflect_set_spte)(struct kvm *kvm, gfn_t gfn, enum pg_level= level, > + kvm_pfn_t pfn); Possibly link_external_spt and set_external_spte, since you'll have to s/mirrored/external/ in the comment. But not a hard request. > +static void *get_mirrored_spt(gfn_t gfn, u64 new_spte, int level) > +{ > + if (is_shadow_present_pte(new_spte) && !is_last_spte(new_spte, le= vel)) { > + struct kvm_mmu_page *sp =3D to_shadow_page(pfn_to_hpa(spt= e_to_pfn(new_spte))); I think this is spte_to_child_sp(new_spte)? > + void *mirrored_spt =3D kvm_mmu_mirrored_spt(sp); > + > + WARN_ON_ONCE(sp->role.level + 1 !=3D level); > + WARN_ON_ONCE(sp->gfn !=3D gfn); > + return mirrored_spt; Based on previous reviews this can be just "return sp->external_spt", removing the not-particularly-interesting kvm_mmu_mirrored_spt() helper. > +static int __must_check reflect_set_spte_present(struct kvm *kvm, tdp_pt= ep_t sptep, tdp_mmu_set_mirror_spte_atomic? > + /* > + * For mirrored page table, callbacks are needed to propagate SPT= E > + * change into the mirrored page table. In order to atomically up= date > + * both the SPTE and the mirrored page tables with callbacks, uti= lize > + * freezing SPTE. > + * - Freeze the SPTE. Set entry to REMOVED_SPTE. > + * - Trigger callbacks for mirrored page tables. > + * - Unfreeze the SPTE. Set the entry to new_spte. > + */ /* * We need to lock out other updates to the SPTE until the external * page table has been modified. Use REMOVED_SPTE similar to * the zapping case. */ Easy peasy. :) We may want to rename REMOVED_SPTE to FROZEN_SPTE; feel free to do it at the head of this series, then it can be picked for 6.11. > -static inline int __tdp_mmu_set_spte_atomic(struct tdp_iter *iter, u64 n= ew_spte) > +static inline int __tdp_mmu_set_spte_atomic(struct kvm *kvm, struct tdp_= iter *iter, u64 new_spte) > { > u64 *sptep =3D rcu_dereference(iter->sptep); > > @@ -571,15 +629,36 @@ static inline int __tdp_mmu_set_spte_atomic(struct = tdp_iter *iter, u64 new_spte) > */ > WARN_ON_ONCE(iter->yielded || is_removed_spte(iter->old_spte)); > > - /* > - * Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs = and > - * does not hold the mmu_lock. On failure, i.e. if a different l= ogical > - * CPU modified the SPTE, try_cmpxchg64() updates iter->old_spte = with > - * the current value, so the caller operates on fresh data, e.g. = if it > - * retries tdp_mmu_set_spte_atomic() > - */ > - if (!try_cmpxchg64(sptep, &iter->old_spte, new_spte)) > - return -EBUSY; > + if (is_mirror_sptep(iter->sptep) && !is_removed_spte(new_spte)) { > + int ret; > + > + /* Don't support atomic zapping for mirrored roots */ The why is hidden in the commit message to patch 11. I wonder if it isn't clearer to simply squash together patches 10 and 11 (your call), and instead split out the addition of the new struct kvm parameters. Anyway, this comment needs a bit more info: /* * Users of atomic zapping don't operate on mirror roots, * so only need to handle present new_spte. */ > + if (KVM_BUG_ON(!is_shadow_present_pte(new_spte), kvm)) > + return -EBUSY; > + /* > + * Populating case. > + * - reflect_set_spte_present() implements > + * 1) Freeze SPTE > + * 2) call hooks to update mirrored page table, > + * 3) update SPTE to new_spte > + * - handle_changed_spte() only updates stats. > + */ Comment not needed (weird I know). > + ret =3D reflect_set_spte_present(kvm, iter->sptep, iter->= gfn, > + iter->old_spte, new_spte, = iter->level); > + if (ret) > + return ret; > + } else { > + /* > + * Note, fast_pf_fix_direct_spte() can also modify TDP MM= U SPTEs > + * and does not hold the mmu_lock. On failure, i.e. if a > + * different logical CPU modified the SPTE, try_cmpxchg64= () > + * updates iter->old_spte with the current value, so the = caller > + * operates on fresh data, e.g. if it retries > + * tdp_mmu_set_spte_atomic() > + */ > + if (!try_cmpxchg64(sptep, &iter->old_spte, new_spte)) > + return -EBUSY; > + } > > return 0; > } > @@ -610,7 +689,7 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm = *kvm, > > lockdep_assert_held_read(&kvm->mmu_lock); > > - ret =3D __tdp_mmu_set_spte_atomic(iter, new_spte); > + ret =3D __tdp_mmu_set_spte_atomic(kvm, iter, new_spte); > if (ret) > return ret; > > @@ -636,7 +715,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm = *kvm, > * Delay processing of the zapped SPTE until after TLBs are flush= ed and > * the REMOVED_SPTE is replaced (see below). > */ > - ret =3D __tdp_mmu_set_spte_atomic(iter, REMOVED_SPTE); > + ret =3D __tdp_mmu_set_spte_atomic(kvm, iter, REMOVED_SPTE); > if (ret) > return ret; > > @@ -698,6 +777,11 @@ static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_= id, tdp_ptep_t sptep, > role =3D sptep_to_sp(sptep)->role; > role.level =3D level; > handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, role, fa= lse); > + > + /* Don't support setting for the non-atomic case */ > + if (is_mirror_sptep(sptep)) > + KVM_BUG_ON(is_shadow_present_pte(new_spte), kvm); > + > return old_spte; > } > > -- > 2.34.1 >