From mboxrd@z Thu Jan 1 00:00:00 1970 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 8BAE11E531 for ; Mon, 6 Apr 2026 21:41:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=170.10.129.124 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775511714; cv=pass; b=h2zPnY/+StNGs4lQ5RSbnbPZX+972Xklzz1G3MzmulZTqOUAnhjgIuNllJw4XJW+SNt/v8gMxgJ7xWzbOX/ZEXStzzEaEJDQbKo3EStpNHpWU/DtlIA9xiP1ZaTHKPwjV+7qon80pmgHSmLyEWJmD0p5q3QaRys2lwyoiF+1AkI= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775511714; c=relaxed/simple; bh=LqeWjRAjleQEgVlvFe8IGD4Aykcb3VyJFHWIx7meAD4=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=j9HPhxw5RUDdBHpSP42xOWAW3JMcJ5wbjAfELm0oYy01HvbAFjKjYcZVYGk0OsJil8+C6/Ti3f2Y/VfpJfS3v0wIf2ZYO2JsfdxkX7sfeXMU0Fx1Vsz+BvaMKwgr+fpwYIPmsWSzVoCN3UIWYzAdQM6E149yfs1NA3SO4qzECwg= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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=HcWDexsr; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=dyHOb1xP; arc=pass smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HcWDexsr"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="dyHOb1xP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775511712; 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: in-reply-to:in-reply-to:references:references; bh=eoWSSC8sEj5p74YK+Y0Flq8SEgfEahb8YNR6rLS40LU=; b=HcWDexsruaCK0XTgFGzs21EbTzPCEAHRksmILyNKXKhthz11G81mPTWkCfUWyvGi5BP+J6 6xDXHNXc2NpSOar3FUQTks36upN1JifL/47vkCPC1AGkUtSetg4Rd1jfKJPFjf8cGaIiim XHdwZifjKCWptHbqYHf8sr5ZDB3wwF8= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-255-IYZFFAwkOya2iBUrNYjURg-1; Mon, 06 Apr 2026 17:41:51 -0400 X-MC-Unique: IYZFFAwkOya2iBUrNYjURg-1 X-Mimecast-MFC-AGG-ID: IYZFFAwkOya2iBUrNYjURg_1775511710 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-43b9b8e3af6so4106024f8f.0 for ; Mon, 06 Apr 2026 14:41:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1775511710; cv=none; d=google.com; s=arc-20240605; b=FxJMaOazqO4KBgcxSidTQ0Alyu+89twljH0K1L0S5FcxR5aywH9/oK3bge9yuCH9qJ wYAxDzBEQEcM93Bk4kyCNGZvKz23o0Z7gqzF0V6gBtAfjSxOemueAZA/Ma4icXr0jHo/ XmBzHAyZOCM4uZ9mxiZHnsyCn6jHWeGEXnD4nHKjBJ9hO7rUG7t/WPh0P/y+BmZ1cnFw OCJ0YydfDIxe/Yy5wnsh2v+E1/PPZ8y+d1YPctqG0LV4Ffq2Fh4cL3mjkQl7kvt0VBRV KsqNwU1bpsL+ML5jTtqdAFWlmDleD3fYpNEdrAAUikX61jXd5GzDvfxnG0wLyswY09yX N5rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=eoWSSC8sEj5p74YK+Y0Flq8SEgfEahb8YNR6rLS40LU=; fh=ZzfYQv/iqwvb8jMpgErNrTfvOg8a2U2+T1CDrR7vo6Q=; b=BUBVvtZEua3P1TSccEnXFvKDqtEPXibj1yg88aUMT+Kl3ZvOs0PafYfeWmN4Z1bn2R beGRUR9EqWsWFCbrA0FqGP99fu5t/IGOkoCjqWiv+MLd0KzwExqjXrIQGFaKSLNUNEkm +9K0Wjocpy/pEFrmtlOA3V8+C8dqSIMGLkHyul8WYoJ2vpTNlIEtqYCZH2loBGMnHkk8 476JqftfJejOYxkJLkH6p1xpwh/Z7WJBFiqkmSyIt1SgTkbGD+9ETAAZ8JZXmis7hKBO AGpVevGcj+erPH+dYVTRWEVQVRMJLVey+3E6cGmdKmYI0UPLZy9mjHMPBPIka+OwdjRW FWkA==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1775511710; x=1776116510; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=eoWSSC8sEj5p74YK+Y0Flq8SEgfEahb8YNR6rLS40LU=; b=dyHOb1xPfUE9VvDDNgvYHWmxB6wwXbfmYxU7jaM0b2Gz7s0X6+4iSU+yYEbmu9yeSl hEJwrDMYoIcI60+ZQAA33P68+Xctlt4mfSRunkPjuNYryL0AW6QSr71zYd7uNoFUzfiQ jwdQ3ZcxT2zCZDRYhnHIlB0NG8qCfW7bxWeh6cjfn/hiCfkKASXaiJBx1uQbuSX5Mx8/ l5CijEPuhydY6loYB5bmAoNaKPc7bi+/MkTyLoHHVFa6EZL2XS8m/BAMMBod6GQSEMZE 1dnFc9WqtPfJ+H0+cfTtQO8tMs4CDnz/j2Yu6RRyJ5Dtz9NKAtyIqwl+HVsKqiuMeRjv oXgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775511710; x=1776116510; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=eoWSSC8sEj5p74YK+Y0Flq8SEgfEahb8YNR6rLS40LU=; b=LXlNtXgBjiMDLiv5d5JrDPiirlscE7YDEIwBpuCVyPBsUzs5FuXK7Rte1VkcnyNEYm sPCnVcJdM/s9iiA/2Vij6/IUp/LqqVWLaIbdzlIF1cd61MDlsqeAwrLBLnqiIRKiX6FH xUBuxs+9vqKHZMGSEZnYCSTdpE/OoVqtwA2kIFaxiXhSx6vY09DVRsTFKyFa+BJgiBND WfuDy4nlcP+y+76gfKuDdorrxH7HUa83svrxIvJY8on2x5pd0SaGYQde29o0xxUR0BN0 uQGc6Sah5Pd4Ujz9KU2vetUl1srRZqiP4OqRwT8gl16d2M175fT0N5a4uFNBGp5fwA96 soFg== X-Forwarded-Encrypted: i=1; AJvYcCU4F8H3jmzLP5YW9QOz/ADItNhfDgSJt/UtBYFwcoAr4cueJDZKooUQk2Ix8civ+z0vaPqGU1YaW822UH4=@vger.kernel.org X-Gm-Message-State: AOJu0YyNMyTPzNJFe1sMZrKEjRCg6tnhpRUZj84F/3Y46c1RiGBjaxmn mVqaJugOwktMESMU9s2k/ujn6Jo/Ooz+OeJR5Td35htiyLwHQR6lYJE4UfC7LqSlWzt3oqNO4v7 nmn2QreXfEhONqfWQa4zUg7jZwakqXG3lrHNhXZCCUO7ks/HUCqc9xZl4dwvD1uDF7vKD/SHikM f6GSp9nvRVCffYBAUIbtgWgPzZ7TCohyzjFpRZIjgj X-Gm-Gg: AeBDies/EVQ2XFX1KLZ+onDScKfrZl392Zp/7zJw3JudbfDixsEGXjgJYQ8X2+ieJyG UcllbgZzJgFfCEeNHjzVN31nIMtAC7V0RfZ1CW8ZeX3aJXQY8Bn1F4TKA4+8WqkLGfz+8gXKV2R QfrOiI9ddWSs13HFrYXd4bVKJT5wHfYwo1t1JrZ1/+494STTN/wD/1d9bZT2ze4o1IvpNkUR992 cwyhVRV/dCOtAps7LtlzaPpZik9RsGhb4ilzeUnaoxtkSs7Tsn2PWD3ji+uFOEXOuDFmdVJ8/r6 Honl X-Received: by 2002:a05:6000:26c2:b0:43d:32a:8595 with SMTP id ffacd0b85a97d-43d29294c00mr19904000f8f.20.1775511710203; Mon, 06 Apr 2026 14:41:50 -0700 (PDT) X-Received: by 2002:a05:6000:26c2:b0:43d:32a:8595 with SMTP id ffacd0b85a97d-43d29294c00mr19903969f8f.20.1775511709729; Mon, 06 Apr 2026 14:41:49 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260311003346.2626238-1-seanjc@google.com> <7ec084f8-812e-42f2-8470-e416fa7ee848@redhat.com> <88e9d7f0-35b8-4559-9f4d-c7daf1af6012@redhat.com> In-Reply-To: From: Paolo Bonzini Date: Mon, 6 Apr 2026 23:41:37 +0200 X-Gm-Features: AQROBzARX7cm2pOzcONamsrvF9MI0sPk77RETM76_dTtAevKjRTQ49z83c779Dw Message-ID: Subject: Re: [PATCH 0/7] KVM: x86: APX reg prep work To: Sean Christopherson Cc: "Chang S. Bae" , Kiryl Shutsemau , kvm , "the arch/x86 maintainers" , linux-coco@lists.linux.dev, "Kernel Mailing List, Linux" , Andrew Cooper Content-Type: text/plain; charset="UTF-8" Il lun 6 apr 2026, 17:28 Sean Christopherson ha scritto: > > You're right about fast paths... > > Ya, potential fastpath usage is why I wanted to just context switch around > entry/exit. > > > so something like the attached patch. > > It is not too bad to translate into assembly, where it could use > > alternatives (in the same way as > > RESTORE_GUEST_SPEC_CTRL/RESTORE_GUEST_SPEC_CTRL_BODY) in place of > > static_cpu_has(). Maybe it's best to bite the bullet and do it > > already... > > My strong vote is to context switch in assembly, but _conditionally_ context > switch R16-R31. > > But that second paragraph isn't quite correct, at least not for KVM. Specifically, > "need a branch prior to regaining speculative safety" isn't correct, as that holds > true if and only if "regaining speculative safety" requires executing code that > might access R16-R31. If we massage __vmx_vcpu_run() to restore SPEC_CTRL in > assembly, same as __svm_vcpu_run(), then __{svm,vmx}_vcpu_run() can simply context > switch R16-R31 if and only if APX is enabled in XCR0. I might even have patches for that lying around (the SPEC_CTRL part). > KVM always intercepts XCR0 writes (when XCR0 isn't context switched by "hardware", > i.e. ignoring SEV-ES+ and TDX guests), and IIUC all access to R16-R31 is gated on > XCR0.APX=1 Right, fortunately. > . So unless I'm missing something (or hardware is flawed and lets the > guest speculative consume R16-R31, which would be sad), it's perfectly safe to > run the guest with host state in R16-R31. > > That would avoid pointlessly context switching 16 registers when APX is not being > used by the guest, and would avoid having to write XCR0 in the fastpath. For now yes, but once/if the kernel starts using the registers there's no way out of writing XCR0 for APX-disabled guests in the fast path. If we ignore that, we can keep guest XCR0 all the time for now, and that would be: - move SPEC_CTRL to assembly - not changing XCR0 handling at all - use XCR0 in addition to just static_cpu_has(X86_FEATURE_APX) to make r16-r31 swap conditional > > - if (vcpu->arch.xcr0 != kvm_host.xcr0) > > + /* > > + * Do not load the definitive XCR0 yet; vcpu->arch.early_xcr0 keeps > > + * APX enabled so that the kernel can move to and from r16...r31. > > + */ > > + if (vcpu->arch.early_xcr0 != kvm_host.xcr0) > > xsetbv(XCR_XFEATURE_ENABLED_MASK, > > - load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0); > > + load_guest ? vcpu->arch.early_xcr0 : kvm_host.xcr0); > > Even _if_ we want to play XCR0 games, (which depends on whether we want to be ready for kernel usage of APX, right?) > tracking early_xcr0 is unnecessary. This can be: > > /* > * XCR0 is context switched around VM-Enter/VM-Exit if APX is enabled > * in the host but not in the guest. > */ > if (vcpu->arch.xcr0 != kvm_host.xcr0 && > (!cpu_feature_enabled(X86_FEATURE_APX) || > vcpu->arch.xcr0 & XFEATURE_MASK_APX)) > xsetbv(XCR_XFEATURE_ENABLED_MASK, > load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0); This is a bit more complex however, because in the end early_xcr0 is precomputing the same conditions and optimizations. For example... > > +void __kvm_load_guest_apx(struct kvm_vcpu *vcpu) > > +{ > > + if (vcpu->arch.early_xcr0 != vcpu->arch.xcr0) > > + xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); > > This is wrong. The "real" xcr0 needs to be loaded *after* accessing R16+. ... this is actually the same optimization you mention above: the real xcr0 only needs to be loaded if APX is off in the guest, and in that case you don't need to load r16-r31. So you can load xcr0 first, and then any components (for now only APX) that need to be swapped. > > + if (!(vcpu->arch.xcr0 & XFEATURE_MASK_APX)) > > + return; ... Because the loads are conditional on APX being enabled in the real xcr0. Paolo > > + > > + WARN_ON_ONCE(!irqs_disabled()); > > + > > + asm("mov %[r16], %%r16\n" > > + "mov %[r17], %%r17\n" // ... > > + : : [r16] "m" (vcpu->arch.regs[16]), > > + [r17] "m" (vcpu->arch.regs[17])); > > +} >