From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 3F3939475 for ; Thu, 21 Sep 2023 14:59:13 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-58f9db8bc1dso14226067b3.3 for ; Thu, 21 Sep 2023 07:59:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695308352; x=1695913152; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4hX9qb1Iw7SkuB3FcSZYopfwd6y9xnz247DDm3mrX40=; b=j9g9hg14HoJ9pv99VRfPeMXNDBlasYEzjCwU8sqYnP3eYInU7KifLZV48XrEo+hQ4Y malPfGiYQDCs44FunqzRpY2ei0MpBqmtFsMRwNgyEOzV9AXpsmwzfu/Rk0kX19MTEH3V 1LBZqB/rmMv2lp29T9oFr9PVjysA5HK89Ip6M34tPe/O37B0w+OutEIeDr7mNSUiEvyB vN/aU4XrrNRQ1xhIe3jFM7j/D6zTS5hVukLzvyxXmXqgp93Y/hZSwqOus2ytqmzQMlid sIt4CRxDKlnX69QgZxvUOrNHMJ7ySQwMC4cG07p+oWv59MUUYVBZucbQ9svh2A+syspa cqyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695308352; x=1695913152; 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=4hX9qb1Iw7SkuB3FcSZYopfwd6y9xnz247DDm3mrX40=; b=qJ+UpQkOq1rrUyA25G5dZyl/yE+j/dCZcMo9v5gR1zU9cOO8Qr5hq5XWE4VHesz+Rb g3FVyNbPsSFl1iHyotcA6uVljRYLTR+9RbNcCbDQrSyH9iRdBSHsuYEBJU/EHacE/6DM b2k8Yh+UkufMXKjtq3ELCyu3J9Kt3PqUuZ38V601febXvwIT7PnO3aQ3O41UNXCJvTF3 GfsUQcBQNtW6/YjJ6KEaiRKMu5d6DpFDoC3GNBM9icBrLdQ72+unqtQR6f0SF52Wkf89 P8IK6uiCKHk3ALdLuFtZoniTtqkyDsxvpsXzBoN3S9IY0WpccUmsqJPxf3bIMrfF2mrx D57Q== X-Gm-Message-State: AOJu0YwCCXaL7zR2yLuEn62/zv+hCYt8mL+OVXXvpLfF+oJ8MnwR/1m8 3yw//93Zp2lwH1zmv5DSEESPt0G/jbY= X-Google-Smtp-Source: AGHT+IGMlI3r5pTKtUH2X22CRcKkDXwZa01FQ/zuLj/x6iC6Lpx1DAvznI7oMbumBcX5i3FsE4fH+yzcjSE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:2906:b0:59e:ee51:52a1 with SMTP id eg6-20020a05690c290600b0059eee5152a1mr76581ywb.10.1695308352199; Thu, 21 Sep 2023 07:59:12 -0700 (PDT) Date: Thu, 21 Sep 2023 07:59:10 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> <20230914015531.1419405-19-seanjc@google.com> Message-ID: Subject: Re: [RFC PATCH v12 18/33] KVM: x86/mmu: Handle page fault for private memory From: Sean Christopherson To: Yan Zhao Cc: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="us-ascii" On Mon, Sep 18, 2023, Yan Zhao wrote: > On Fri, Sep 15, 2023 at 07:26:16AM -0700, Sean Christopherson wrote: > > On Fri, Sep 15, 2023, Yan Zhao wrote: > > > > static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) > > > > { > > > > struct kvm_memory_slot *slot = fault->slot; > > > > @@ -4293,6 +4356,14 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault > > > > return RET_PF_EMULATE; > > > > } > > > > > > > > + if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { > > > In patch 21, > > > fault->is_private is set as: > > > ".is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT)", > > > then, the inequality here means memory attribute has been updated after > > > last check. > > > So, why an exit to user space for converting is required instead of a mere retry? > > > > > > Or, is it because how .is_private is assigned in patch 21 is subjected to change > > > in future? > > > > This. Retrying on SNP or TDX would hang the guest. I suppose we could special > Is this because if the guest access a page in private way (e.g. via > private key in TDX), the returned page must be a private page? Yes, the returned page must be private, because the GHCI (TDX) and GHCB (SNP) require that the host allow implicit conversions. I.e. if the guest accesses memory as private (or shared), then the host must map memory as private (or shared). Simply resuming the guest will not change the guest access, nor will it change KVM's memory attributes. Ideally (IMO), implicit conversions would be disallowed, but even if implicit conversions weren't a thing, retrying would still be wrong as KVM would either inject an exception into the guest or exit to userspace to let userspace handle the illegal access. > > case VMs where .is_private is derived from the memory attributes, but the > > SW_PROTECTED_VM type is primary a development vehicle at this point. I'd like to > > have it mimic SNP/TDX as much as possible; performance is a secondary concern. > Ok. But this mimic is somewhat confusing as it may be problematic in below scenario, > though sane guest should ensure no one is accessing a page before doing memory > conversion. > > > CPU 0 CPU 1 > access GFN A in private way > fault->is_private=true > convert GFN A to shared > set memory attribute of A to shared > > faultin, mismatch and exit > set memory attribute of A > to private > > vCPU access GFN A in shared way > fault->is_private = true > faultin, match and map a private PFN B > > vCPU accesses private PFN B in shared way If this is a TDX or SNP VM, then the private vs. shared information comes from the guest itself, e.g. this sequence vCPU access GFN A in shared way fault->is_private = true cannot happen because is_private will be false based on the error code (SNP) or the GPA (TDX). And when hardware doesn't generate page faults based on private vs. shared, i.e. for non-TDX/SNP VMs, from a fault handling perspective there is no concept of the guest accessing a GFN in a "private way" or a "shared way". I.e. there are no implicit conversions. For SEV and SEV-ES, the guest can access memory as private vs. shared, but the and the host VMM absolutely must be in agreement and synchronized with respect to the state of a page, otherwise guest memory will be corrupted. But that has nothing to do with the fault handling, e.g. creating aliases in the guest to access a single GFN as shared and private from two CPUs will create incoherent cache entries and/or corrupt data without any involvement from KVM. In other words, the above isn't possible for TDX/SNP, and for all other types, the conflict between CPU0 and CPU1 is unequivocally a guest bug.