Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp67284lqp; Fri, 12 Apr 2024 10:36:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUVJLwGVdAKmFELiGNxJes2UceK8t6xcYBQ0qNMZDaDh0I5PoQqABafE3BRhRSmDSe0/fDBO21NT5uOhcY9sNoxM79s+zZzLhDyZyVijA== X-Google-Smtp-Source: AGHT+IGiiglvyyBdj2ERkKk4kJxzscVAes+Equr9IKlBSSKqZZ1XyPJL9WsmCHnpfphljRPUDczG X-Received: by 2002:a17:906:1395:b0:a52:2ca3:aae7 with SMTP id f21-20020a170906139500b00a522ca3aae7mr2126690ejc.13.1712943360381; Fri, 12 Apr 2024 10:36:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712943360; cv=pass; d=google.com; s=arc-20160816; b=wTQ9mk981XFvoipidKo0i/Y70kixcMLpvG5ep6CoKiPOcTicA4SdlBwy4y7kx8zhhc uSb8h8Om1EN2EMExUiotT3EBsr7uoCxA9fzq4bvKsa+2MPgqewuEqQIMbMhMhmC+n/Wy STAgAQfKahiERluV2cIUpz4SM7G1ZpZUSrmEK4rc2EtwJLzU6FAR0jLC/jKgWLwGru8C JNage/c86wREwbac5LHkc3WX3mqB7tiBh1ohLXXzvNNVdBqLnGNZxL/TjgrlFnv5ARdV WU22iGVpuDqSK5HdzXIBbZzYPJHtEhF5KQjtmUGqhvFK7+ib98+32xD9KCWLI9kTH4LE N0Bg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:to:from:dkim-signature; bh=VpeDD5FA3om+hsK2jspL++DDgX73rZcz5eJV48XG4Vk=; fh=4N0ilCeWNPUMy3p+fwQEcpDcSktLq8IMxbqwLAweA1k=; b=cRFC1bxhDn70SYLxBMaC0h/S54akIaTfT7mhjodvF21i5oiN/Gy8Pq5S7TYB/NDm78 7OiF3EqFlTr4LZEeiTY+FtGy0T5+FZEVpNejMFAR0g7Q2ssh6gPmt/ljqqY3uoDcZbSa Wrt+u9KIj/5BSIwNBJLveSlxO60ZKg4QrEQOXsTMAW7Cxc8w+JtwmBwJRJGbSgdEQNoN wmryPs2jdAz8p63AE/wnZ5A7xZ7Lj+G8mBXlvZQVlpLkkORpqk31Q3lmrEcd8eHivrq8 mzkN71W9z7CIT0zfvMPbNTg1/LJIjDEwFWIeT5tJ28Fh9pnDR41fVno43njzOXGwjSFK 6esg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zw8jMsg1; 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-143132-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143132-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id lt12-20020a170906fa8c00b00a51d6337b52si1935475ejb.910.2024.04.12.10.36.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 10:36:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-143132-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zw8jMsg1; 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-143132-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143132-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1DB381F23D0A for ; Fri, 12 Apr 2024 17:36:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E846714EC6E; Fri, 12 Apr 2024 17:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zw8jMsg1" 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 959A014C596 for ; Fri, 12 Apr 2024 17:35:37 +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=1712943339; cv=none; b=JbGstjY7gP/Q33mAS4tY0BlM2fSvEZZg2MIIx2Mad7ReAI3r0hZVrnAyiwSAxoD0EwzYggy53go4bQRotf7kSJMVNDxJNY7wCJtRZMVYTfpnYtC57+ibYie7p1nCKwFj9d86fjyh8j9pljVU7PNjPUlBzB/WI0+2HulB0HeksX4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712943339; c=relaxed/simple; bh=m6cvDpKwQxw/+1HX6xn29yBbQlMJwxJA/GOc2nFuhwM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rC27ATsBwRixE/AN+3/mwVQK0o2V/Y88+4Edprfxw5ACdgCIYfgXNx8zTSdwsN7AGlhuFZynK7szFWWilQDz6V+pWdLdhBaYy69xDZSqJOveYS9wowIS4CTX5JeG9VExbJDeifOd7k/pONC0gWmXv1EQP9mjd979+20MmBHhQr0= 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=Zw8jMsg1; 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=1712943336; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VpeDD5FA3om+hsK2jspL++DDgX73rZcz5eJV48XG4Vk=; b=Zw8jMsg1HaIQ0KrTnUZwYCsCoowXto64vOXtfmAVjPokKSUWsI/1AVvel/49RjWdk1gys1 /yzL7Iqis/HdrsTXN9iNv//xh8RfnKsJ5amWBfpYsbH/5HWMihPPVxoCbdKRx2dmDYmuni ZcZ+4ayJL+xiRwm+VS9kbCsl1Gf4l/I= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-489-pGbX_jt3NbysqDWKIcvPAA-1; Fri, 12 Apr 2024 13:35:35 -0400 X-MC-Unique: pGbX_jt3NbysqDWKIcvPAA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DE3291807ACE; Fri, 12 Apr 2024 17:35:34 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id C64B32166B31; Fri, 12 Apr 2024 17:35:34 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 10/10] KVM: x86/mmu: check for invalid async page faults involving private memory Date: Fri, 12 Apr 2024 13:35:32 -0400 Message-ID: <20240412173532.3481264-11-pbonzini@redhat.com> In-Reply-To: <20240412173532.3481264-1-pbonzini@redhat.com> References: <20240412173532.3481264-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 Right now the error code is not used when an async page fault is completed. This is not a problem in the current code, but it is untidy. For protected VMs, we will also need to check that the page attributes match the current state of the page, because asynchronous page faults can only occur on shared pages (private pages go through kvm_faultin_pfn_private() instead of __gfn_to_pfn_memslot()). Start by piping the error code from kvm_arch_setup_async_pf() to kvm_arch_async_page_ready() via the architecture-specific async page fault data. For now, it can be used to assert that there are no async page faults on private memory. Extracted from a patch by Isaku Yamahata. Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 0b73f78dd70a..cf5f28dcda06 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1854,6 +1854,7 @@ struct kvm_arch_async_pf { gfn_t gfn; unsigned long cr3; bool direct_map; + u64 error_code; }; extern u32 __read_mostly kvm_nr_uret_msrs; diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0fb175ad6b9b..7b0d671cf696 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4207,24 +4207,27 @@ static u32 alloc_apf_token(struct kvm_vcpu *vcpu) return (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; } -static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, - gfn_t gfn) +static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) { struct kvm_arch_async_pf arch; arch.token = alloc_apf_token(vcpu); - arch.gfn = gfn; + arch.gfn = fault->gfn; arch.direct_map = vcpu->arch.mmu->root_role.direct; arch.cr3 = kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu); - return kvm_setup_async_pf(vcpu, cr2_or_gpa, - kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch); + return kvm_setup_async_pf(vcpu, fault->addr, + kvm_vcpu_gfn_to_hva(vcpu, fault->gfn), &arch); } void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) { int r; + if (WARN_ON_ONCE(work->arch.error_code & PFERR_GUEST_ENC_MASK)) + return; + if ((vcpu->arch.mmu->root_role.direct != work->arch.direct_map) || work->wakeup_all) return; @@ -4237,7 +4240,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) work->arch.cr3 != kvm_mmu_get_guest_pgd(vcpu, vcpu->arch.mmu)) return; - kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true, NULL); + kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code, true, NULL); } static inline u8 kvm_max_level_for_order(int order) @@ -4350,7 +4353,7 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault trace_kvm_async_pf_repeated_fault(fault->addr, fault->gfn); kvm_make_request(KVM_REQ_APF_HALT, vcpu); return RET_PF_RETRY; - } else if (kvm_arch_setup_async_pf(vcpu, fault->addr, fault->gfn)) { + } else if (kvm_arch_setup_async_pf(vcpu, fault)) { return RET_PF_RETRY; } } -- 2.43.0