From mboxrd@z Thu Jan 1 00:00:00 1970 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 BAB1529D270 for ; Fri, 7 Nov 2025 19:09:05 +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=1762542547; cv=none; b=m7yb823fkmKh1bjfZaOmXMRy3yJHpuVX5iJgHmPvocn3odFKvuTyWEgJJKnd8/sCROGuHpXLlD3fmrGVx6KzgRThUSWEiKl2+VVypNN/LBhDxB33TMjaoZEsAuvjmXhGdCh6/fkeCGSuYcMzduRDhVnV7VQZRTqHatqiFCjPSqY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762542547; c=relaxed/simple; bh=8Sh01qsQnFqXTjo/cJT7q03gWfer8OL1PrCWFV7VHIA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c5dPPQSvgFwmUOtS2se/AOI57zGuzns0EYAj2hj4S5pl4ykbXVTIw5SL2PTqwMNPmd94Wms7Cra9HSJPdJItLTPhynzrEHXxA+eAj7ex523GBgqEpwDgZ6hqfDxIlbL+V95wj/e28fjCOmCoa1bAD89o5ltwdX5hzJl2MCPhfXo= 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=w9jQ924h; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="w9jQ924h" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-340d3b1baafso1761015a91.3 for ; Fri, 07 Nov 2025 11:09:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1762542545; x=1763147345; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=X77mgdPoU1VHaudK74YxkC46spvrpO5NvEWTm3wV9vw=; b=w9jQ924hxBWCZu74pfwvP2w2UALLbIuD8d/t+P0BXcMZ+g5GYzf6FEpau8MYO0etbv Bizl4Dvtw7fi7vLeb/b1eUueeaLevKmQ/BZqeCSWc0JBLWd8pqm3hBJXteFtVvJvOVzw OUAppJqvrazDwOvBXWyrPsPNXt7W4hxH0kNOc+xXgR3jso67oHQfVwlP52XwHdj6q+Uh Ot7B5w34uyoAVA7hmgxRvtlvaLvXduLRJ0ToJFdXBXajD47iZgBqqKs2W/58/wSndkgD kdRn3bdmWcqTACG/pF57ncxnPG2l7cMTArwFghG4NHLoLtmNrWExh+EWKotJFPP11Dz3 chWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762542545; x=1763147345; h=content-transfer-encoding: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=X77mgdPoU1VHaudK74YxkC46spvrpO5NvEWTm3wV9vw=; b=N3zjgZRPFruTx5xBAtOk9Boi2yIDgYJeJSN4VoxNDwI0LZHjIv6zUutvcgmYdeoCi6 pmEB7gCFFh/+qtWQiv/zB84ri6yhSIO+1bsRehLYVBAItV1/wLeu1DzRs5MobFNTu1yW I3HRYCN3o6sURt96qK2RfU5WHOn5hqDXGmxc7LBu3yUr8nLhM0ynQmhP6G/GpLfiIGuq WXVVSn245ctbq9S9usywHZZrttPEVVKTnMJdSBlsc05JlHYovhyAtsFQaXFGV5Mx5yqH koYr98tDQiymnbQMChWMaFurhf9YQxWlPZvI+alCWIwvuUwjgQTbwpps4vFJe/Y759G6 uVdw== X-Forwarded-Encrypted: i=1; AJvYcCXv+y+hiMCXfXImvaxTkLbiZNTRVM+uhP+sfFSvPmL/KXjFGTy645vRNXcUHuIS2HlxfsfgTsdhGKn9piY=@vger.kernel.org X-Gm-Message-State: AOJu0YxeJ6ImDvPPa/xbK46Am8cwyOTT6mmbfgetMov0xropLpMQZjdp 46rTo9NKuvhY/bxlWxn5iqknz32DU7Pf9MBD7BMsq4YwUCu9UyfGchjlJuX4j48r7tYTiHAHUUg uLWsMIQ== X-Google-Smtp-Source: AGHT+IGH7LL6GV2gLbWjUzWfRR4A8LykKgi383PZhU1U0IWxftK363fN1GI5ZstiN7+YwdfTqgRZ/o9SlrQ= X-Received: from pjbdt5.prod.google.com ([2002:a17:90a:fa45:b0:341:7640:eb1e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3811:b0:340:29ce:f7fa with SMTP id 98e67ed59e1d1-3436cb7ae1emr293287a91.7.1762542545053; Fri, 07 Nov 2025 11:09:05 -0800 (PST) Date: Fri, 7 Nov 2025 11:09:03 -0800 In-Reply-To: <599eb00e-a034-4809-8f5a-893597016133@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251106101138.2756175-1-binbin.wu@linux.intel.com> <599eb00e-a034-4809-8f5a-893597016133@linux.intel.com> Message-ID: Subject: Re: [PATCH] KVM: x86: Add a help to dedup loading guest/host XCR0 and XSS From: Sean Christopherson To: Binbin Wu Cc: Chao Gao , pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Fri, Nov 07, 2025, Binbin Wu wrote: >=20 >=20 > On 11/7/2025 11:14 AM, Chao Gao wrote: > > s/help/helper in the subject. > >=20 > > On Thu, Nov 06, 2025 at 06:11:38PM +0800, Binbin Wu wrote: > > > Add and use a helper, kvm_load_xfeatures(), to dedup the code that lo= ads > > > guest/host xfeatures by passing XCR0 and XSS values accordingly. > > >=20 > > > No functional change intended. > > >=20 > > > Signed-off-by: Binbin Wu > > Reviewed-by: Chao Gao > >=20 > > > >=20 > > > @@ -11406,7 +11391,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *= vcpu) > > > vcpu->mode =3D OUTSIDE_GUEST_MODE; > > > smp_wmb(); > > >=20 > > > - kvm_load_host_xfeatures(vcpu); > > > + kvm_load_xfeatures(vcpu, kvm_host.xcr0, kvm_host.xss); > > Nit: given that xcr0/xss are either guest or host values, would it be s= lightly > > better for this helper to accept a boolean (e.g., bool load_guest) to c= onvey > > that the API loads guest (or host) values rather than arbitrary xcr0/xs= s > > values? like fpu_swap_kvm_fpstate(). >=20 > Make sense. I don't love passing true/false, but I Xiaoyao does make a good a point tha= t subtly requiring the caller to pass vcpu->arch.xcr0 vs. kvm_host.xcr0 is we= ird and confusing. > > static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest) > > { > > u64 xcr0 =3D load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0; > > u64 xss =3D load_guest ? vcpu->arch.ia32_xss : kvm_host.xss; >=20 > Since they are only used once, I even want to open code them as:=20 +100, I find your version much more intuitive. > static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest) > { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (vcpu->arch.guest_state_protected) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return; >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)= ) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (vcpu->arch.xc= r0 !=3D kvm_host.xcr0) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 xsetbv(XCR_XFEATURE_ENABLED_MASK, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0load_guest ? vcpu->arch.xcr0 : kvm_ho= st.xcr0); >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (guest_cpu_cap= _has(vcpu, X86_FEATURE_XSAVES) && > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 vcp= u->arch.ia32_xss !=3D kvm_host.xss) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 wrmsrq(MSR_IA32_XSS, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0load_guest ? vcpu->arch.ia32_xss : kv= m_host.xss); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 } > } >=20 > >=20 > > if (vcpu->arch.guest_state_protected) > > return; > >=20 > > > /* > > > * Sync xfd before calling handle_exit_irqoff() which may > > >=20 > > > base-commit: a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a > > > prerequisite-patch-id: 9aafd634f0ab2033d7b032e227d356777469e046 > > > prerequisite-patch-id: 656ce1f5aa97c77a9cf6125713707a5007b2c7ba > > > prerequisite-patch-id: d6328b8c0fdb8593bb534ab7378821edcf9f639d > > > prerequisite-patch-id: c7f36d1cedc4ae6416223d2225460944629b3d4f > > > --=20 > > > 2.46.0 > > >=20 > > >=20 >=20