Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2369651rdb; Mon, 12 Feb 2024 02:32:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX+O6jNTpJj4YamzwKiWz6RhzuAOU8cpkQ80qmQrKE8MLeRHu1O4YbUDY02b+zKS/CdgRRPtepuk/OUpvSwi9GUdyzU0zxCfV9SKLxgzQ== X-Google-Smtp-Source: AGHT+IERBOjb3TOWYyCv7hHGnKzTpg/PPVFCTTI/y785W110ZSsOtpj3LHu3HfgdqDgmCruDVebr X-Received: by 2002:a05:6358:7329:b0:178:a7de:2e17 with SMTP id d41-20020a056358732900b00178a7de2e17mr9926713rwg.7.1707733935486; Mon, 12 Feb 2024 02:32:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707733935; cv=pass; d=google.com; s=arc-20160816; b=TgxeE5sNLKZjbEycnBW8z8y+aAI1GMtp5W2NgScH0LILYcfi4+vV97x02GSUsdGyQC vVoR8kBIeQcknbl5caXTWQNSpgZzj4uvj/4DQukRu7lJ/WsEbmuHxvoDjqJu9gVb9T+G yLZ+Tcso5ne4Dysp4ST70IY8fN7xMSIyiqw79oD39aJtNJL2ZcM8YiylncH1n0k3w26q /wsBid8/IQ9jh9zH+w1DfViBGe6qwvhNH9ZDfYz4mXgB21cEDcQlQs/W9Q1P2BB8fJso k8/z/Bb+tAN0cbbICQ33uInsFYC6VeOsZ/uabiik+8TDcjqtQTEvjUjek5kE87p4UMoV hW6Q== 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=1t318SRTxLpDaLWn2G/SymzKj/FNZHT6EAXxG8/aWeU=; fh=vMb2EXJSR05akRR/cjp2N5ozcx/Fe/sowHEPHGh/NyM=; b=i7+1eNYBi4/dyL+K8IRhXWwyRlGOJan0haBowC7tcUl1QR3tA5RpQ4w83CHXUy9crP ntGzLFMpZPl9ihxqyOcsRtdc6OFVd4HFTRFzJPw5wSLbPD2V0lq7EZzQDjLh7WKuTPpr 0OZZxX1M+dYzlhakgQAcIWrpSQQdFusjF1PiqOA3FOTINt2WfVOnyhU0da9R8Xdml+Ef CB5O8ynSqeoaOKtlKJJZMkwPqANc85SUDATtwSYOAqshXIXFjWDiiFVbgOmKVNQ03LOs QBoTB5XzFIFllp7xgodyvjzrQeQxWp1x8UdGfdFhOa/JRfIPfQb0SO3/rOdVYX4uy1tA d4YA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=T1e6zcaX; 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-61315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61315-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCXH/D7gTCHKURmhcbmygj5v4VRLY2K2EB801aHvNp5sNBG5rXJqt8ZstnMN30VQjEl/0aGRULGkeXuJs/d1cNVGME4XtH0FN25p0BfNJw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id j185-20020a638bc2000000b005cdf37c9c29si43719pge.703.2024.02.12.02.32.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 02:32:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61315-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=@redhat.com header.s=mimecast20190719 header.b=T1e6zcaX; 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-61315-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61315-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 1B8E1281902 for ; Mon, 12 Feb 2024 10:32:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B19638DD2; Mon, 12 Feb 2024 10:31:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="T1e6zcaX" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 2822A383BF for ; Mon, 12 Feb 2024 10:31:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707733903; cv=none; b=JHVR4yQ0HEc54fRbaKCg26u+qK7yoOVX4ydUwYDVd8cMaaxYDxGa128N3e7EtTQMtFxqDQ1VRNVRIcCnxWYCzGf5qbKPGNg5Ji5gD/M3CeLAMM0wLmQ7jsGdHcpkgOZT4OthtOLOndSdfd2fYA3s+IyoV/sixfftqg8Ea/b0FC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707733903; c=relaxed/simple; bh=czUfwp9Vd0O43dhgHlhoxm7n6b/o2RFsBWH5mKBRF/I=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=VHgxQcLFpLB7rycIGyeYjdbVxjUR1DUp2/AMlpe7U3Ec0l2lm7dIy68wR9nkI96kapfZxy2IgwGc0FoUt22i9BWEqKbuVFTSGQnLVp4ZbvsjRA/mJNp0cAIqc4/7hVmnleNXDkwkfDZQ58d0ikZ6L5dQQBWdWFFuDsbo4piv4Qg= 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=T1e6zcaX; arc=none smtp.client-ip=170.10.129.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=1707733901; 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=1t318SRTxLpDaLWn2G/SymzKj/FNZHT6EAXxG8/aWeU=; b=T1e6zcaXAgJNSliltBE0yKqcVvrVpOxCET1og34UAjjYlj6AKG+LG/7XbFuRK3mQIJoT8K oogOyw6dn1idq+JZTX8q2ShiKLXjlRyObW1DlMevnjZQ7xzmffRhMB33PH2l5tQ5ztkcSG TdQ411BRSIoNpQvJSdi8kfc40Pi+Rlk= Received: from mail-ua1-f71.google.com (mail-ua1-f71.google.com [209.85.222.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-281-n8_p2_3MNtmVXpZsUK13nA-1; Mon, 12 Feb 2024 05:31:38 -0500 X-MC-Unique: n8_p2_3MNtmVXpZsUK13nA-1 Received: by mail-ua1-f71.google.com with SMTP id a1e0cc1a2514c-7d2dfe84153so1947439241.3 for ; Mon, 12 Feb 2024 02:31:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707733898; x=1708338698; 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=1t318SRTxLpDaLWn2G/SymzKj/FNZHT6EAXxG8/aWeU=; b=WsYr4mjq+rJh5ohdWYaaHQA0e246qfTQQQJ1YHoosRixg5PlynCqqAoSOjc4JckI6O 1CFAUZcqhTI/4CN8qr0xMq5DtqAICV/K/VEFTSxC0K4UVeV3O39JoclVDnp/0LNucQ9x d7qGk+Wrng7wQQL2W0hp8swEljR2LcTGcl95xBYzD6kOUZtahTz9QbFuO3oF1YJgNqlX Ohrb/RXIwTi4JG86hZAkqbzcJ81ed3G5kUfuAmuaOOtPhFHVvPzBqWLPFzGekwlovOLc 2kYByg/rzvuUyx03Bu74ozroue8l7gxGV5kuMad0nnO0M1kmDpcd0m2NbyCe1g0/d+6A y1SA== X-Forwarded-Encrypted: i=1; AJvYcCW+pNB1Ivwo1bw0993XjaEadptx06A88piPbq1gsWfC3Tc8OIH2lx/XuS91LWq7sdfQP8fJ4odqqcLfpP6kRAmuPyaHIgxj0pzXsppE X-Gm-Message-State: AOJu0YyxwtguGh2dpP66rZkYMzy5BomdAOPqNiUa+Xbmm/u4VQ4EauUB E5f+AbY7eO9Isx/Q+BKeN4hP9T3FDy9106bjulDFMlRcDO//XB/qwV/conHKkmI5cfYXpYN91Mk Q4qXEjYkPkhqgoImBkW6iaNXgxbuTnI5jxfDDVlUw/9YEnWjPntwjm8LC2DvxyHJJIveuNed5QB mAWW+4cR3Bfbp4q0+9HTYfWvWJ1uvROKeI7gPK X-Received: by 2002:a05:6102:3161:b0:46d:240d:438c with SMTP id l1-20020a056102316100b0046d240d438cmr4076021vsm.27.1707733898455; Mon, 12 Feb 2024 02:31:38 -0800 (PST) X-Received: by 2002:a05:6102:3161:b0:46d:240d:438c with SMTP id l1-20020a056102316100b0046d240d438cmr4076001vsm.27.1707733898047; Mon, 12 Feb 2024 02:31:38 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231230172351.574091-1-michael.roth@amd.com> <20231230172351.574091-10-michael.roth@amd.com> In-Reply-To: <20231230172351.574091-10-michael.roth@amd.com> From: Paolo Bonzini Date: Mon, 12 Feb 2024 11:31:26 +0100 Message-ID: Subject: Re: [PATCH v11 09/35] KVM: x86: Determine shared/private faults based on vm_type To: Michael Roth Cc: kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, jroedel@suse.de, thomas.lendacky@amd.com, hpa@zytor.com, ardb@kernel.org, seanjc@google.com, vkuznets@redhat.com, jmattson@google.com, luto@kernel.org, dave.hansen@linux.intel.com, slp@redhat.com, pgonda@google.com, peterz@infradead.org, srinivas.pandruvada@linux.intel.com, rientjes@google.com, dovmurik@linux.ibm.com, tobin@ibm.com, bp@alien8.de, vbabka@suse.cz, kirill@shutemov.name, ak@linux.intel.com, tony.luck@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, alpergun@google.com, jarkko@kernel.org, ashish.kalra@amd.com, nikunj.dadhania@amd.com, pankaj.gupta@amd.com, liam.merwick@oracle.com, zhi.a.wang@intel.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Dec 30, 2023 at 6:24=E2=80=AFPM Michael Roth = wrote: > > For KVM_X86_SNP_VM, only the PFERR_GUEST_ENC_MASK flag is needed to > determine with an #NPF is due to a private/shared access by the guest. > Implement that handling here. Also add handling needed to deal with > SNP guests which in some cases will make MMIO accesses with the > encryption bit. > > Signed-off-by: Michael Roth > --- > arch/x86/kvm/mmu/mmu.c | 12 ++++++++++-- > arch/x86/kvm/mmu/mmu_internal.h | 20 +++++++++++++++++++- > 2 files changed, 29 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index d3fbfe0686a0..61213f6648a1 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -4331,6 +4331,7 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu = *vcpu, > static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_faul= t *fault) > { > struct kvm_memory_slot *slot =3D fault->slot; > + bool private_fault =3D fault->is_private; I think it's nicer to just make the fault !is_private in kvm_mmu_do_page_fault(). > +static bool kvm_mmu_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 err= ) > +{ > + bool private_fault =3D false; > + > + if (kvm_is_vm_type(kvm, KVM_X86_SNP_VM)) { > + private_fault =3D !!(err & PFERR_GUEST_ENC_MASK); > + } else if (kvm_is_vm_type(kvm, KVM_X86_SW_PROTECTED_VM)) { > + /* > + * This handling is for gmem self-tests and guests that t= reat > + * userspace as the authority on whether a fault should b= e > + * private or not. > + */ > + private_fault =3D kvm_mem_is_private(kvm, gpa >> PAGE_SHI= FT); > + } Any reason to remove the is_private page fault that was there in previous versions of the patch? I don't really like having both TDX and SVM-specific code in this function. Paolo > + return private_fault; > +} > + > /* > * Return values of handle_mmio_page_fault(), mmu.page_fault(), fast_pag= e_fault(), > * and of course kvm_mmu_do_page_fault(). > @@ -298,7 +316,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vc= pu *vcpu, gpa_t cr2_or_gpa, > .max_level =3D KVM_MAX_HUGEPAGE_LEVEL, > .req_level =3D PG_LEVEL_4K, > .goal_level =3D PG_LEVEL_4K, > - .is_private =3D kvm_mem_is_private(vcpu->kvm, cr2_or_gpa = >> PAGE_SHIFT), > + .is_private =3D kvm_mmu_fault_is_private(vcpu->kvm, cr2_o= r_gpa, err), > }; > int r; > > -- > 2.25.1 >