Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1567439rdb; Wed, 24 Jan 2024 22:17:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IFJeHgTL2Q33EuxigJyjVlhzKR3KhBt0fclmfmZuH/eHVW7QHABfuZ1Cy9GM8zOaZEXqHWF X-Received: by 2002:a9d:6a95:0:b0:6e0:d870:9fe9 with SMTP id l21-20020a9d6a95000000b006e0d8709fe9mr434144otq.43.1706163424264; Wed, 24 Jan 2024 22:17:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706163424; cv=pass; d=google.com; s=arc-20160816; b=b41YoWCcAoPT/AJR9CLP+MvHpsnHOJiqoZ4/VxQ/7bkHaTEPPrE2nBMIfH+AaKGVuc gSo+zcgarkpp1cpV2JWXphoxpGQ6vzNpnmuKGealODmrDPdDkSeMMUHHewwS307TTw8/ Mu6MMOxj3ximTErh0+7r+n7ka96D0HBkg8QFsvkxw4A2EJiwFKLIa4L1nIIe+m1lTOKT rq8oMyM1rQfz4EKXuBnxlwJiaf/QYhOHaplAdzuDmSPzm0uslMXYlpCMPcAXKrTe+pRm XHP4066KvOYNUoZhuldgJ2GNlAw9wi04X3EQlS+yE6HHbuKO81Dp6f7gUz+F9mZga8Ih 4kCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=WyjRm70cNhy2UUKebzo39dzYqQUiyuTEJLfCg4UEdPM=; fh=r8hQL/v9RKJEnJiwnC14YLt4Tmv2EWtrhfNI0e5bOKo=; b=QC+bW5n9QEVtGqQ7LjXzSgdy19LYtcjRIiQr2MZEt7hlPwXONzWaISpMw+82FSrMDB ZrWPsE/5Pd/fWRepHN5y5445olqDiNDLr39r+Bt3TcyilxtUlGAir8CwMwvlaG0mLVjH Ur6QOKYynPFWdMiBShNGBwW4NdAW0tEdpYhEcJsvPumUDmEFBBU6VvuCgaap6tIxxan7 PLH+9DwgwUJ/6GsAEdVBglZA7Dv/dfiZVzNcutUiT9dCqHG3ZqKECDgIDX7k5vAiL+Ih Ybk4E4OqIrvRTEnJMEe+T39liw9+o26QCOEvILXjmwfDTHSbvgC4+0bKU+PUAgjkZ3Hw iVrw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ewNVnbA7; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37839-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37839-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l33-20020a635721000000b005c6b4e190b0si12605176pgb.530.2024.01.24.22.17.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:17:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37839-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=@intel.com header.s=Intel header.b=ewNVnbA7; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37839-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37839-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 83B19286886 for ; Thu, 25 Jan 2024 00:58:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA0891860; Thu, 25 Jan 2024 00:57:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ewNVnbA7" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) (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 4E28C193; Thu, 25 Jan 2024 00:57:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706144276; cv=none; b=Ho0t1c0EaxGO/RllcGkLLWpixkCcncDepr5St4Y3VB0kcd5PZxiGR+CP3q+5BNzYuW8tFLEawGpme4kKMBXAa6XqOumL526n9xrQUBot+kkpP/ErTBFZC29nOnvB45+xQlmUMUDZFSQHq759dghAP6deYoJDEyKhgZnRUgyf2sE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706144276; c=relaxed/simple; bh=ooW4EEQBVQlYOcJ+TWB/kkVLUo0DXaIhjEWAqx4cpto=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=FceyXJKsI1HsuDE+8NcG2NRQXnk2LGEKQHYHqE7EsUudw3ORDbj5b6ZJmEeZ5KESJvTjQnbPdcqSeqWV/HWjQpo/omNBPdP0rv9kwoLIJnH6buSOrRtWZOM3nv0BFK0BGvGuQFJPPP75g5qFE9EruiA4XKmAEWCVwrLs8Qi2frY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ewNVnbA7; arc=none smtp.client-ip=198.175.65.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706144274; x=1737680274; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=ooW4EEQBVQlYOcJ+TWB/kkVLUo0DXaIhjEWAqx4cpto=; b=ewNVnbA7cPfVN8B2XZ9oaySGE9LCYAih1SxjbtCLfslylYyqOP1xxqxc HM1u4V7Y3kYzdFYRU8ubx0MJSbBzK41myWwyxRtOwBfmlk0DVaFKdxrdB kQ6N3i7gDND3lPYiFbK2TIi2bxVK5HpEXHDfzEOJlyOyf+S6Ao6oqM3tX nBxl8huIocG8rIaDsHQLkRH7x5MlNrmIRxpklxvBaW9jRmKPI8vsGJ/ic X7Vq/qa9BjgIs+2Qp0oIUEvuaxQ8iAUZUKuap4aPBPMwo5igAAy9Ztsar FCqm0v+tmg1/zwHYokyl6Uom1mwQuYy1tf+qGxKFRk2ZK184GnNp9IjR5 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="9128064" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9128064" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 16:57:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="905800230" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="905800230" Received: from yjiang5-mobl.amr.corp.intel.com (HELO localhost) ([10.212.118.106]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 16:57:51 -0800 Date: Wed, 24 Jan 2024 16:57:50 -0800 From: Yunhong Jiang To: Michal Wilczynski Cc: seanjc@google.com, pbonzini@redhat.com, mlevitsk@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, dedekind1@gmail.com, yuan.yao@intel.com, Zheyu Ma Subject: Re: [PATCH v2] KVM: x86: nSVM/nVMX: Fix handling triple fault on RSM instruction Message-ID: <20240125005710.GA8443@yjiang5-mobl.amr.corp.intel.com> References: <20240123001555.4168188-1-michal.wilczynski@intel.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; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240123001555.4168188-1-michal.wilczynski@intel.com> On Tue, Jan 23, 2024 at 02:15:55AM +0200, Michal Wilczynski wrote: > Syzkaller found a warning triggered in nested_vmx_vmexit(). > vmx->nested.nested_run_pending is non-zero, even though we're in > nested_vmx_vmexit(). Generally, trying to cancel a pending entry is > considered a bug. However in this particular scenario, the kernel > behavior seems correct. > > Syzkaller scenario: > 1) Set up VCPU's > 2) Run some code with KVM_RUN in L2 as a nested guest > 3) Return from KVM_RUN > 4) Inject KVM_SMI into the VCPU > 5) Change the EFER register with KVM_SET_SREGS to value 0x2501 > 6) Run some code on the VCPU using KVM_RUN > 7) Observe following behavior: > > kvm_smm_transition: vcpu 0: entering SMM, smbase 0x30000 > kvm_entry: vcpu 0, rip 0x8000 > kvm_entry: vcpu 0, rip 0x8000 > kvm_entry: vcpu 0, rip 0x8002 > kvm_smm_transition: vcpu 0: leaving SMM, smbase 0x30000 > kvm_nested_vmenter: rip: 0x0000000000008002 vmcs: 0x0000000000007000 > nested_rip: 0x0000000000000000 int_ctl: 0x00000000 > event_inj: 0x00000000 nested_ept=n guest > cr3: 0x0000000000002000 > kvm_nested_vmexit_inject: reason: TRIPLE_FAULT ext_inf1: 0x0000000000000000 > ext_inf2: 0x0000000000000000 ext_int: 0x00000000 > ext_int_err: 0x00000000 > > What happened here is an SMI was injected immediately and the handler was > called at address 0x8000; all is good. Later, an RSM instruction is > executed in an emulator to return to the nested VM. em_rsm() is called, > which leads to emulator_leave_smm(). A part of this function calls VMX/SVM > callback, in this case vmx_leave_smm(). It attempts to set up a pending > reentry to guest VM by calling nested_vmx_enter_non_root_mode() and sets > vmx->nested.nested_run_pending to one. Unfortunately, later in > emulator_leave_smm(), rsm_load_state_64() fails to write invalid EFER to > the MSR. This results in em_rsm() calling triple_fault callback. At this > point it's clear that the KVM should call the vmexit, but > vmx->nested.nested_run_pending is left set to 1. > > Similar flow goes for SVM, as the bug also reproduces on AMD platforms. > > To address this issue, reset the nested_run_pending flag in the > triple_fault handler. However, it's crucial to note that > nested_pending_run cannot be cleared in all cases. It should only be > cleared for the specific instruction requiring hardware VM-Enter to > complete the emulation, such as RSM. Previously, there were instances > where KVM prematurely synthesized a triple fault on nested VM-Enter. In > these cases, it is not appropriate to zero the nested_pending_run. > > To resolve this, introduce a new emulator flag indicating the need for > HW VM-Enter to complete emulating RSM. Based on this flag, a decision can > be made in vendor-specific triple fault handlers about whether > nested_pending_run needs to be cleared. Would it be ok to move the followed emulator_leave_smm() code into vmx_leave_smm, before setting nested_run_pending bit? It avoids changing the generic emulator code. #ifdef CONFIG_X86_64 if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) return rsm_load_state_64(ctxt, &smram.smram64); else #endif return rsm_load_state_32(ctxt, &smram.smram32); --jyh