Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp213376lqb; Tue, 16 Apr 2024 13:23:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWkXCHeqN5T03CbXl08W8ugkeMg27u290IJGE/Ml1rrOJJ47t4Qh+VFaKCHIg8GegAJvETMfhOanEvwxOaMtIX4A31I0S7fa5+sCqu0oQ== X-Google-Smtp-Source: AGHT+IH/rl+m8deoreeq2C6i2BemuPJq5Ok9dCbs57VaIoUIaGzuCPkU20w4fvmn7ecyYAYXBlpu X-Received: by 2002:a17:90a:1fca:b0:2a5:270c:38cc with SMTP id z10-20020a17090a1fca00b002a5270c38ccmr13464573pjz.43.1713299017088; Tue, 16 Apr 2024 13:23:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713299017; cv=pass; d=google.com; s=arc-20160816; b=IdqkEfUCX7vxn1GFN6SGGctTK7ly5gauSUeXPA+uFzmyLKSWuNsy1OchJqzZen/7oS IEgXI7fvGkum0455CBWa3A7nT8cGzVW+drh+8+GVej1oD2mqdgHvlm1pgUwQGt1YVRu8 v7r36NZGCmRjli1FsZBAvin2UBwxjGbqDhvzgnFqYyqVq9bE0YyM0ga5O9LUixKqiHVe LtGXXEVgm7dVtSyEraJ7tK5pTjW/KVCOUW9s6j6ZZptnwuSfjmT2IoQS8CTH/OWRTNRR EDHabgaotmEs6BAeTGBYhb3U7++J5C5mOOl6GLe5/aZSjBN6TgPTFmtmjeCTp8egR18t x0NQ== 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:cc:to:from:dkim-signature; bh=wKHBMFyUvxqFiQQY3mVtCcupwWGc2q6fnOXY0GMQChE=; fh=kM0aiaSc/0bcKmeaNecUcPBkSFAAdFQ8DFGcHuH09bk=; b=cqujrf3ptLNnZlzA6aE28t7qSPCV3L7LwcddayVSq/fHuVMMfKNQv7k0HIyFxr3JWG DvbI4cYEHqlsSy/Jlh9tyA0M/9didGqvrvgc+dv76xd0JB/2lYktbv2yC2ORtRAWHlti ijGJhKAfwTtqxA2uKFuru4Z+1bNZi6QRELOK+UttimzFr9odVySQgMSu8lu/bIBOgncp BRI1bb43RbnnzBGoR9p5x+2K3weONkoZ6Td7HvVo3glWPE3bf69ORzlrhp00JQ4c555z qRSgkJiERS/Z1IC3/awdUUZ3txLGg0/p8aAO13TIMw/4WYAK44AuJUKG2MmWVLdNtL1R 6OYw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Xh6HdFG9; 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-147511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-147511-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. [147.75.48.161]) by mx.google.com with ESMTPS id ot1-20020a17090b3b4100b0029ff777c440si9514pjb.7.2024.04.16.13.23.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 13:23:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-147511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Xh6HdFG9; 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-147511-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-147511-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 57AB0B22F2B for ; Tue, 16 Apr 2024 20:22:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D466513C818; Tue, 16 Apr 2024 20:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Xh6HdFG9" 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 BBDC0139D17 for ; Tue, 16 Apr 2024 20:19:41 +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=1713298784; cv=none; b=kQKFqOwfTOk9D1VzyZFR2fnHv8iaN7DdhcMTl3kpvz4g9XEdUt3+6H9afC2YDWw/kSZslmz7DLf5FGot0VWV8cNck+gh/fzLw+tg1ZDKxcKVi42P45XKxZ+d9HI6HEorhx5qA7V+bLOzfdxpR1xt1G0IYscKrqZUOV/xb9peiv0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713298784; c=relaxed/simple; bh=czdR7Th8MzHIEsGl2kG3zA2zrMvBtsVZocV83LzMCJ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JfXQYoFb6rvtsPkF8ZlohGcpNsD7eMbMYah6qO4Jn5r6AIIHosVXJumqF/ik9TsbFn3NDPzQdrMggLe4yfUtxDNBnZOugMGw1yJO96uwhITkMhrxeVYarBcWN67Iate8McgpNniWqPwqDJGo4lQp3Xo5igd7bHDGN4d9jom2/3M= 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=Xh6HdFG9; 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=1713298780; 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=wKHBMFyUvxqFiQQY3mVtCcupwWGc2q6fnOXY0GMQChE=; b=Xh6HdFG9gp84wFJHIbzgLFY2wN2x25yEd55OyDgkIklDt3X1K5CngkiJp5PCs2q8hyjLYm G7Y42vUFGATB5UD1CVG4EOOiyzMXtduHz++xGHdalwt/1Rw+Wd9NTLZWnE9Mci6BUz9Jy3 pbsNp+EJq8PA3U3j4f/C6KlQZiJhoJ4= 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-594-u1d24xX6M9u_JjXAYGDRMw-1; Tue, 16 Apr 2024 16:19:39 -0400 X-MC-Unique: u1d24xX6M9u_JjXAYGDRMw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 0B5FF88CE80; Tue, 16 Apr 2024 20:19:39 +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 D353440B497B; Tue, 16 Apr 2024 20:19:38 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: isaku.yamahata@intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com, chao.gao@intel.com Subject: [PATCH v2 10/10] KVM: x86/mmu: check for invalid async page faults involving private memory Date: Tue, 16 Apr 2024 16:19:35 -0400 Message-ID: <20240416201935.3525739-11-pbonzini@redhat.com> In-Reply-To: <20240416201935.3525739-1-pbonzini@redhat.com> References: <20240416201935.3525739-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.2 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 7c73952b6f4e..57ec96bd4221 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1850,6 +1850,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 33aea47dce8b..402d04aa5423 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) @@ -4342,7 +4345,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