Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp52816lqq; Wed, 12 Jun 2024 16:31:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV8pQhzMtAGALo+e+08s/QfcEXyh0ehBZcN36wp/Evhl9koH4Zpjln0ONyk7OgbylFD6KdqTqJNCcFL8kvh3fj/1xsfexLsGY14toP1yw== X-Google-Smtp-Source: AGHT+IFGdAndmIzYt9gDcbJRZNm8OH7QR/CcuruI3xzdXM1qPlA1gpiMCvscwW/qiXsZ/h5zlYmX X-Received: by 2002:a17:906:c14a:b0:a6f:1951:5755 with SMTP id a640c23a62f3a-a6f47c7d735mr213632866b.11.1718235111194; Wed, 12 Jun 2024 16:31:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718235111; cv=pass; d=google.com; s=arc-20160816; b=JfYQFGuAh6yeQbHE/SLYgdvR/7GZ9xcwZx/WbbU1jA6G/Qz85plhfrgJb7tJ73B31u 2DEBdS42x68SXElHJRp3SsxOHKOGtpQAo0NyuKYiWwY2ifyNmvjlUGA1pmCnoXM8x855 XUWrh64jI3KOnLJxJeNpq4OmN0AGTm1pB23EeEg/DcmYfz7Lc0+n/cBTYotTJwjPW/5e aARB7eVNbsn4ICHSoFv3NGWbXtJpXtLn9RkEK++aIhZZYCDKHMc42K5lvsvUe2kEXWno itxFV66PoW9i6p22U3Oyu+e7LnVuIrNTY1HpqwEww3w8gmNzbLv66WljK5zBWkTBy4Pp j1gw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=L/SyTTg+Pb2L8jVOL9JfQwGKpPzI3PRlHDd/+KWWSxs=; fh=0nyhhlp4HssM4P5wi4tWgDoPTFz9o7wgVpw1ZnYQ1nI=; b=WdZxl+CRbzdMuMAFaQbS+A2lwW4bo8XLZxbYVeANuXS0FhkDk/Fc/wDnMbaL2NM82F dgAMlzmfhrz8Rmwq4+WPgMjSAB6XSSFVxvjhFqRUq9rYfd9l6AGhWhrdxSZxcjE6Lycm HiuepyneoK2DYn2Yl1QcYfTd34DsYFjiFXpzQC0oexrnCz3SUhSjoOS8bJfHYQbEwFiz AIb7p/mchiKbf9EibkU/RiHKqEnEVFq4aaS9DBefxbHSEisQri+HiPTCTLASwKJAySCT ItqmozGAETv6M7Uo5rG2unZ2k3AwvwH3Pr9uPmIwlgACp8ueAnL8UH7NlDDBzuew3gZc arDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vPnpl7Og; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-212379-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212379-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 a640c23a62f3a-a6f56e3c9b8si5563266b.837.2024.06.12.16.31.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 16:31:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212379-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=@google.com header.s=20230601 header.b=vPnpl7Og; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-212379-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212379-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 B84AB1F22505 for ; Wed, 12 Jun 2024 23:31:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BFF8912D748; Wed, 12 Jun 2024 23:31:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vPnpl7Og" Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DDCF12CDA5 for ; Wed, 12 Jun 2024 23:31:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718235098; cv=none; b=MynDut0NmTSBZWVSsjk6OTHW2s15bb9iZEBy3fbc+QtTQ5xdi7BRakI8VWokRtsp2v4b0qQJ7YBRAHE5HtVxyoM5BWFAEF9EoYZ3a3fe+GM4NbUbtJYqUbmP3M4OT6jwlWneTBuUhgR9XhOdBFGcDNnQO1BRBT+6n1SlQIhgquI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718235098; c=relaxed/simple; bh=4SDKp1waJzXqdTd/ActDvagt62Ph9uX2CozVVnbthGw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OUOjNjseYGZXP3wie/K4XPajA20wV0hDDNJ+pmyufqhiYLlz1+J+MiR2wny1uuYrpTujGVipVy6CuRNqM0QuiL+tr0+p2yQphJpVUydRY5w60O7oSOigf/7E7tzwkOYAPpevcDn30B8zB1GDFRHkoVXBg3EObN3qqXKeqrqN17I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vPnpl7Og; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2c24109ad3fso299422a91.0 for ; Wed, 12 Jun 2024 16:31:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718235097; x=1718839897; 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=L/SyTTg+Pb2L8jVOL9JfQwGKpPzI3PRlHDd/+KWWSxs=; b=vPnpl7OgTw3m1W9f7qd8zuFbhpnIj+Jd34ThUJ7cL7UUofqkzRWLXa51Txwe4/fgxL PLzlDOasbRE+NzMuzxMsbsjUmjMh6HEYZPereLw5PaVNb8BVZzMrebU+Q81WpBJq7CPt GqouZtMhHxB7yb43W4Zs/RcZMGIN1WXLa5N6v64RhHdchIZvE9y2s3HQzP+2zejVkIsE D/qFO96+CjbH0nAd0oZsiCShLpItkPvCrABUdqVUmHILxr55EOKKcRgURxnQ8zF+0vYP xqjDo2Whtgb7BT4Tsq/V0HUpLKSHFphdLrYRrTmQTYmOXlENmugklXmS1yPBJ5Re6sh1 IF3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718235097; x=1718839897; 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=L/SyTTg+Pb2L8jVOL9JfQwGKpPzI3PRlHDd/+KWWSxs=; b=lrK2c/b2iJrRqa3OkWf195zt2J678Es426Zikeu/yKMqZhOZqLm8rMuRyB0TxrVNKK fqIXHKWbsbemL8auSC/1w0fHUuFJcKl0+JAUh6QqMnaYlAYhSTLEFkflXKLIMtMoQIX4 iqpHLQncGG5Z8TSRUfwUL/2lQKQvw6KiJJ+7fMG3oOXZscr0QbJzNzGRPk9QDBYl3dCx KZX1VuwuHU0RwTi5+ZcARFgr/eH5dnwR0dggHbfMvwaw7wjU4g390qFJEc3sqVN1KmYH qk7Avh/DwwyINmwQIitN99H0B6+vzcjeda/JMK3jXL9eYTdERx0mkbyG5fdQlMsT1oqr DOKg== X-Forwarded-Encrypted: i=1; AJvYcCXtfWZCEeju2Am+uoHJNG/9mtaD6xkt+wEt+Jk7nL1QzP55q1Wk726k/SM9zrpmekLHXmGfJCRKHNRZiGvdshm21QlyiFGAuL6hj3Gq X-Gm-Message-State: AOJu0YyRwkBPQ83QPpHT+eIW8iUnMFgNF9PC0YB1tXBjwY5jJ7lRHZQx v7WDKGj794Xnwv+z0OA+4rbdA5+/8hwVs8kStExj4g7Pkqk3VoEQdwIegzvfrNan3KERFvZBW+5 gfw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:62c9:b0:2c2:c65f:52dc with SMTP id 98e67ed59e1d1-2c4a770e8f6mr9058a91.6.1718235096779; Wed, 12 Jun 2024 16:31:36 -0700 (PDT) Date: Wed, 12 Jun 2024 16:31:35 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240207172646.3981-1-xin3.li@intel.com> <20240207172646.3981-13-xin3.li@intel.com> Message-ID: Subject: Re: [PATCH v2 12/25] KVM: VMX: Handle FRED event data From: Sean Christopherson To: Chao Gao Cc: Xin3 Li , "linux-kernel@vger.kernel.org" , "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kselftest@vger.kernel.org" , "pbonzini@redhat.com" , "corbet@lwn.net" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "shuah@kernel.org" , "vkuznets@redhat.com" , "peterz@infradead.org" , Ravi V Shankar , "xin@zytor.com" Content-Type: text/plain; charset="us-ascii" On Sat, May 11, 2024, Chao Gao wrote: > On Fri, May 10, 2024 at 05:36:03PM +0800, Li, Xin3 wrote: > >> >+ if (kvm_is_fred_enabled(vcpu)) { > >> >+ u64 event_data = 0; > >> >+ > >> >+ if (is_debug(intr_info)) > >> >+ /* > >> >+ * Compared to DR6, FRED #DB event data saved on > >> >+ * the stack frame have bits 4 ~ 11 and 16 ~ 31 > >> >+ * inverted, i.e., > >> >+ * fred_db_event_data = dr6 ^ 0xFFFF0FF0UL > >> >+ */ > >> >+ event_data = vcpu->arch.dr6 ^ DR6_RESERVED; > >> >+ else if (is_page_fault(intr_info)) > >> >+ event_data = vcpu->arch.cr2; > >> >+ else if (is_nm_fault(intr_info)) > >> >+ event_data = > >> >+ to_vmx(vcpu)->fred_xfd_event_data; > >> >+ > >> > >> IMO, deriving an event_data from CR2/DR6 is a little short-sighted because the > >> event_data and CR2/DR6 __can__ be different, e.g., L1 VMM __can__ set CR2 to A > >> and event_data field to B (!=A) when injecting #PF. > > > >VMM should guarantee a FRED guest _sees_ consistent values in CR6/DR6 > >and event data. If not it's just a VMM bug that we need to fix. > > I don't get why VMM should. > > I know the hardware will guarantee this. And likely KVM will also do this. > but I don't think it is necessary for KVM to assume L1 VMM will guarantee > this. because as long as L2 guest is enlightened to read event_data from stack > only, the ABI between L1 VMM and L2 guest can be: CR2/DR6 may be out of sync > with the event_data. I am not saying it is good that L1 VMM deviates from the > real hardware behavior. But how L1 VMM defines this ABI with L2 has nothing to > do with KVM as L0. KVM shouldn't make assumptions on that. Right, but in that case the propagation of event_data would be from vmcs12 => vmcs02, which is handled by prepare_vmcs02_early(). For this flow, it specifically handles exception injection from _L0 KVM_, in which case KVM should always follow the architectural behavior. Ahh, but the code in with __vmx_complete_interrupts() is wrong. Overwriting vcpu->arch.{dr6,cr2} is wrong, because theres no telling what was in vmcs02. And even if vmcs02 holds DR6/CR2 values, those might be L2 values, i.e. shouldn't clobber the vCPU state. It's not clear to me that we need to do anything new for FRED in __vmx_complete_interrupts(). The relevant VMCS fields should already hold the correct values, there's no reason to clobber vCPU state. The reason KVM grabs things like instruction length and error code is because that information is visible to other aspects of injection, e.g. to adjust RIP and pushed the error code on the stack.