From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 EA27C2772D for ; Mon, 22 Jun 2026 08:49:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782118196; cv=none; b=il3uIPTZF6XNXKJvfT2w7Iy4oaozoBQvfqsdix4DELHHYaVcTy7CYPwr+N3U59c+iZ0C94PjMYe9AGldmiDXNiBjz2ieQSKpoXOdEqDZklQ97x87LC5HQkaoh+d0BKAuxcK3fefN5mbDAITVH/UGJF3s7mdUWyozgvs6cZVg9j4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782118196; c=relaxed/simple; bh=v6b9tIPottpOhVi5BOdy+hR4NQy2BK0lyl88HmGRHug=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TQQU0JplPP39iEEm3kJrf+kiay7nxVGC4TkkVXuRM3beJng9DuGcCug9r8+jYKJK1MyseQuEEiIuZkemWR08ZNe1zl3HfFb4FGePMvLfKCcB7sIk15kJnp3irS4ftIgvD2DvsDluujynbQwFBnuS5Kr4DdC9qq8dKd4OBFi6XwM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W0qkl//k; arc=none smtp.client-ip=209.85.128.53 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=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W0qkl//k" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-490b3637b90so31382385e9.3 for ; Mon, 22 Jun 2026 01:49:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782118193; x=1782722993; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=G4aLRA0sObdyQwkUt9lWTZbbvG2GGrfM8jRkE/wqXXI=; b=W0qkl//kd/51u+tkYW+efK1q7L5/iosWsAJ7m+Z82cnfzzRtRzk6XKXTEAEInNzw7J aHu0DP3apFDVPJOrW3p8g6ho8OdmUYg6NsNjKPdE8gid8sbl5Z1Bll7Vbiogquxbqyah mGGBZ/Q7829euHdd68yCSi0fwKCmaZFMZMoKl0kf6/4wz0OEHJu7GKId3FkJWtejQ6X/ Erau8UR0a2sbzV8bunFyKlPnVi/J3VL7kX71mwTYvuyw2b76Xvcs6AGw7ewumIFXe/Kt YMPBKE1JYVvP9uyrNVGlfP/iFa9yzaVIC9CkZGcBLm8oyMCI8++Gpa698dJ0gatzH3ss gQcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782118193; x=1782722993; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=G4aLRA0sObdyQwkUt9lWTZbbvG2GGrfM8jRkE/wqXXI=; b=SJlqBeLhAbarXVvfdrata7NT3iUtczMKjbUfcwpiyftmhNIoeOO14cUF8Jm3LTTKkr oO9ZdM2i8PsiFogORfCP0lT9RlsxemVc1k0EY2GdhzsJZZY5audyQ7y8MbdEovVq8vdY k3M8lBBJR7JoKeiYDe/RSukCq6WitLnIVGzLK2dD8cNi64OJAGusJDNhzSFd4DKvVSW5 Y2HYFc1VTeRKNltiLBz4MHVEc7L4x9yWCvJiD9IYkdg//NKjGc/0Uhxyr0t5FReNcz7x vlQs+3wMok1pMDysQhw0HQv2hQZmVuJ2c6j08C3lytBABseennA5JJ1GYagXIJF0k2RL GOFw== X-Forwarded-Encrypted: i=1; AFNElJ/c7K5KqEHC+xOTwd+IsqHA/aqa2kit8YR5VcWZo70plAFKJxHtPc5oI7q0MICS8LHvSCGsKACFBggbvPk=@vger.kernel.org X-Gm-Message-State: AOJu0YwlHOxBMRgImnVPe1ff977xSwVYahFODh1H4wPb6n6kzu8l8G5g N/5pLCcpaAXPL/rl2NuVeaOlVHidUTCUYUthHjJflV916kAPLaxSDrmhzzppfED/hg== X-Gm-Gg: AfdE7clN68aOCdTH7Lc21XvQkDilBsLhqvaTMz4MQNGuMD2yktqwmXzTNGRv7fUZl80 k4Kjhq9LyX2uYrydLQDnz+mx8e8Gtc033U+Wj7AMCBmHaaShVQUNVrYXWmGqXJk4wGilATSt9T8 l3eV/kT7xK47yYSY/e9OzBzQcTuCAsgYQLD+q5RYIC8xu6nZxRgdWCo1teb5zMvtUh4yenYvb9j FsDz/1bKeQIBqM9vdDziZmVmbK/VeOFbgjpmDaBawg5H09XiGcvZoaH0VVQXjJlAEOvXuD5HKQJ S208R1cB035N9GMgRdWe4nE3jtYlLDMmM9LLM9/vCLhWhdJU2cjfR3yOwOVjKY9r4AjRv5uC4RZ NMRqM7soqGTuhwQ29bgMQWslVj3nNrPiTgcYTNau8uH1+HNYGbpvpbDA0SoaXtVFTpBy2ZBDF6H peDj8xnw2IQ+jQDRkFpLgLXpVfTVAUe1ADN0I0/sJL2y2WdzvMNYPnIrR2 X-Received: by 2002:a05:600c:19c8:b0:492:4cc7:de73 with SMTP id 5b1f17b1804b1-4924cc7df3dmr124550365e9.31.1782118192788; Mon, 22 Jun 2026 01:49:52 -0700 (PDT) Received: from google.com (135.91.155.104.bc.googleusercontent.com. [104.155.91.135]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-492492338dasm195354755e9.1.2026.06.22.01.49.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2026 01:49:51 -0700 (PDT) Date: Mon, 22 Jun 2026 09:49:47 +0100 From: Vincent Donnefort To: Fuad Tabba Cc: Marc Zyngier , Oliver Upton , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Quentin Perret , Sebastian Ene , Hyunwoo Kim Subject: Re: [PATCH v2 8/8] KVM: arm64: Implement lazy vCPU state sync for non-protected guests Message-ID: References: <20260619070719.812227-1-tabba@google.com> <20260619070719.812227-9-tabba@google.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: [...] > > > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > > > index 54aedf93c78b..8963621bcdd1 100644 > > > --- a/arch/arm64/kvm/handle_exit.c > > > +++ b/arch/arm64/kvm/handle_exit.c > > > @@ -422,6 +422,20 @@ static int handle_trap_exceptions(struct kvm_vcpu *vcpu) > > > { > > > int handled; > > > > > > + /* > > > + * If we run a non-protected VM when protection is enabled > > > + * system-wide, resync the state from the hypervisor and mark > > > + * it as dirty on the host side if it wasn't dirty already > > > + * (which could happen if preemption has taken place). > > > + */ > > > + if (is_protected_kvm_enabled() && !kvm_vm_is_protected(vcpu->kvm)) { > > > + guard(preempt)(); > > > + if (!(vcpu_get_flag(vcpu, PKVM_HOST_STATE_DIRTY))) { > > > + kvm_call_hyp_nvhe(__pkvm_vcpu_sync_state); > > > + vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY); > > > + } > > > + } > > > + > > > > Could we remove this update here and let handle_exit_early() do the sync > > regardless of the SError injection? One of the main point of handle_exit_early() > > is to do things under !prempt(). > > Agreed on the move: handle_exit_early() is already preempt-off, so the > guard() goes away. Not on every exit though. handle_exit_early() runs > on every exit, and sync_hyp_vcpu() only copies PC/PSTATE/fault back > for a non-protected guest; the GPRs and sysregs cross solely via > __pkvm_vcpu_sync_state. Syncing unconditionally would pull the full > context back on plain IRQ exits, which is the copy this patch avoids. > So I will gate it on trap-or-SError and drop the > handle_trap_exceptions() block. > > > > > > > > /* > > > * See ARM ARM B1.14.1: "Hyp traps on instructions > > > * that fail their condition code check" > > > @@ -489,6 +503,22 @@ int handle_exit(struct kvm_vcpu *vcpu, int exception_index) > > > /* For exit types that need handling before we can be preempted */ > > > void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index) > > > { > > > + bool inject_serror = ARM_SERROR_PENDING(exception_index) || > > > + ARM_EXCEPTION_CODE(exception_index) == ARM_EXCEPTION_EL1_SERROR; > > > + > > > + /* > > > + * An SError injected below writes the host ctxt; for a non-protected > > > + * guest, sync from the hyp vCPU and keep it dirty so it isn't dropped. > > > + */ > > > + if (is_protected_kvm_enabled()) { > > > > Should we test !kvm_vm_is_protected(vcpu->kvm) here, as the > > PKVM_HOST_STATE_DIRTY is only updated for p-guests everywhere else? > > Yes. The flag is only ever set for non-protected guests, so clearing it > for a protected one is a no-op, but gating it matches the invariant. > > Both fold into one block in handle_exit_early(): > > if (is_protected_kvm_enabled() && !kvm_vm_is_protected(vcpu->kvm)) { > if (inject_serror || > ARM_EXCEPTION_CODE(exception_index) == ARM_EXCEPTION_TRAP) { > kvm_call_hyp_nvhe(__pkvm_vcpu_sync_state); > vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY); > } else { > vcpu_clear_flag(vcpu, PKVM_HOST_STATE_DIRTY); > } > } > > I will fold this into the next respin. Ah yes of course, I was hoping we could just have a switch here, just like handle_exit() does, but that's not possible because of ARM_SERROR_PENDING(). Perhaps it would look cleaner if done in a separate function handle_exit_pkvm_state()? > > Thanks for the reviews! > /fuad > > > > > > + vcpu_clear_flag(vcpu, PKVM_HOST_STATE_DIRTY); > > > + > > > + if (inject_serror && !kvm_vm_is_protected(vcpu->kvm)) { > > > + kvm_call_hyp_nvhe(__pkvm_vcpu_sync_state); > > > + vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY); > > > + } > > > + } > > > + > > > if (ARM_SERROR_PENDING(exception_index)) { > > > if (this_cpu_has_cap(ARM64_HAS_RAS_EXTN)) { > > > u64 disr = kvm_vcpu_get_disr(vcpu); > > > > [...]