From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81B58E7D0A2 for ; Thu, 21 Sep 2023 20:37:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232054AbjIUUhn (ORCPT ); Thu, 21 Sep 2023 16:37:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231978AbjIUUh0 (ORCPT ); Thu, 21 Sep 2023 16:37:26 -0400 Received: from mail-ot1-x34a.google.com (mail-ot1-x34a.google.com [IPv6:2607:f8b0:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C92E48A210 for ; Thu, 21 Sep 2023 10:40:32 -0700 (PDT) Received: by mail-ot1-x34a.google.com with SMTP id 46e09a7af769-6c0f110ee3bso1424158a34.2 for ; Thu, 21 Sep 2023 10:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695318032; x=1695922832; 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=4hX9qb1Iw7SkuB3FcSZYopfwd6y9xnz247DDm3mrX40=; b=hu/uWs7XhlQi8guUnDILe1X70U5jof6e2d3yyr+enrl5MpjY+lVysVWww2M8QxlnrP Depsbpi016z0GRR/5oMO7Zi9mECrCJS27omm3E6gVkWE3hSxQLcB7lqh0J5mZSlNFE9d zu6u3GttVAra5/O67X8czY0NJxJJhZY2s56tYkA6fIH0cUcUUBUKQpnal1O7RWpCSrLz 6z20Z0o9g2fLvmUN4q3xWV0673GqWgg7dB8SD+DA4AWppd9J8gIjpNYGyQ5aVVWxKunb EG0xlkyY5aq2rwC3gkhdJPHRM3FbEy0ZPXFlai+6IVoLj0xGc6v7y0V6ReV4wFTt9Y1x 99bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695318032; x=1695922832; 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=Bo/3oajtGDtM8uqSD8GzFCI+29ZeKOVGuMi8RaZUzhBiBtoyqB0j+dG4aizLGEvumH qsYNzUBQiPjEyFyWv2IkBsAUkEW3HNqGnIc06rEi6HVDGPYg7Irg3MXHI5NHv/IGXbzt VGI40Dv9KlYBNP+v25xrm/ISw53ks1vHooLxVqjzZkWXH+5D94KkVVb3oUQuzohPMmYA MODrSJVh4hvSEFlmKMg6KWmPg/oNI/szkbTEvivObPmRY0Jjdt3u1/b+Olwwtf9Rquz8 hQwHyX+LFBGrKYirgxx6j3Q06LaLg5EFtTo8avzALpFo4WkjNu+tkRO5soCx/U5o96Fs 7xxg== X-Gm-Message-State: AOJu0Yz2GckBSWc50d1LTgK109pKg4pwObpSk528SkYnq1v4hgCvr4TR Sj1GWbHNqNyqVMr9ekuJ3lYn3Ptwm2Y= 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: 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.