From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796028523; b=fSO93bCQNFR289wep66PUoj8s3RH80kqp7YKqMnic3bayj1rzqpN3jzCaA9JjcIfBKu8 FzNCWMBMCDy1LOByuxkNH4NXnIIgCLYlay7XaVPcbTxxQVYP4K5u3R2vP7u80jjcldbN3 BEvO4cfpS5HTmYpHMR/vTkAp8w9Pj2UF6inutM2d//3lfq428TlD2eD9i9bwRi9xisSgZ iX1rQ/uT3QTacUbnsbHG+gABBbd3XEAVSQRoc9mcKT4RLzdJR0lZu/gLIkchaPKHk8J3U WMJqMMiYhHdmGKSa+Lq1ztc9Wn0CFbQtaFmlaHzha1tfmBbtU7fEnT4DRr0GQmTyCcw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796028523; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=9Uu7SJVciUVRBD54igsENm5KVha62oQwpgXU1M0a59Q=; b=nVSz1y11rLkNp1tF9f+OZ7+T/YdUC6K5GuM18ZbBe4z4xqZuQxJQ44Cu63loP2PG3nya 7zWIe2THEM8g09yOUpdm0GvUHIYrIVh5u9RZVd88AOiFits020Xo7H2zYrK1uCxzWdsGH I10bQYBSbiC6UatrHC2N/uV66t2X/1/rY0EGxlio5jRAv0D4SuRS5B51cNenpzZyi1XrG cx1ojd4uWxH4jmSGcAN/GuQkG94N6PXMinLjgy+EtbqLdmiBEkhmkvl9rt1AKB9tKN1Wv M2hbVLyiXR5Q2H3u8t3EF9QWcfH4J3uaHAdrli11iBUghXEFIS7mye6jBljvWiHIG+g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pereq.eu; spf=pass smtp.mailfrom=cryspen.com; dkim=pass header.d=pereq.eu header.i=@pereq.eu header.a=rsa-sha256 header.s=google header.b=BAkVKAyL; arc=none smtp.remote-ip=209.85.128.52 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F985606A6AB for ; Mon, 30 Nov 2026 08:48:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8F985606A6AB Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pereq.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cryspen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pereq.eu header.i=@pereq.eu header.a=rsa-sha256 header.s=google header.b=BAkVKAyL Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4a5177d78c7so104617025e9.2 for ; Mon, 30 Nov 2026 00:48:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pereq.eu; s=google; t=1796028521; x=1796633321; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9Uu7SJVciUVRBD54igsENm5KVha62oQwpgXU1M0a59Q=; b=BAkVKAyL1micY4z5jZII/Pk9VRIeZ3k7/gZNLi7P+VO2Z7fFdhts772Z0nLtihPKK6 zyya0xLZQwfv7C+Cetca2xIG1TAtFF5GTDyeYf47MsLbkehkklxGBLVFXBoLADzuWKa7 ARnah+Wg5n5BClWitaFAZftdpsvHSW1g7omotyxJKELGqWx6XlcFMwU1/A1mwfodolWv YkkwYjmHRxOmhkUFTRq0XF+IxPUubep0e1h225BOcOlZZPrcDxlUCpqNZKlrLo8kuHH7 lwE7oXjyVH44J9/d2iDaPX565LPGyupnkcbPI12HxXQNurB/Y8IkO655yhlofAvGKyCP PnFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796028521; x=1796633321; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=9Uu7SJVciUVRBD54igsENm5KVha62oQwpgXU1M0a59Q=; b=ojiwE72Fvby7qP4Yc1KtuuX0wLF8PW5+RtRXeohL8cAKG/4TSxYuHtzTFTMQDCvEqV vBVoW+AX9EQ2RpSzz9Ok5H4W55+ULICBswOtTQOMyASj2xf6hAXmh1UxtXbQ4g+JhLvD yzqpKGOmCFgjh0g8vU3hZr/vzai1TKw7eTElJ8/JrseeoY5pkm3PYWh+vxv7Oua7jrGF TjKIHMq00988C29wmUTs5W8zA0+JXJVU13sv09NHNaSiaVfAg7407NFbSOlnyT4KjB0I icqi4Eiez7y/CVPUqrHtiVPpz0gVHkbGf5+duXWfUOu2AOpswW+XRWfxW+GyeXMq9qMB Ufaw== X-Forwarded-Encrypted: i=1; AL+hErGPlRdnJKzxJrsePbEjKHYmkRgbLcJbb0BaSMp4KaKO/O9pQUaekn07BK4J5Qc8HKwYad5rzGSZ32+9orU=@vger.kernel.org X-Gm-Message-State: APuU4Z+6IfO3G6ApvtPYP5GmUnT3jNwEpEtR7gPnhvFUFUhOYGlQLE+N 5PIUaFM9iC4iiIB7XAsBbpmhcMO9UCfdxrfmuD2aMONz5bYKL8evbTPKhKo6laSEcvo= X-Gm-Gg: AZFdTZdWsPdyQkgoBrD11bx8gLhIx0Y5uVcQklVpOndlLm1TcWnMvRs3o/k2vWQzmWs aPPGLeB4FvIO5xHJ8+3Og9w8dY8Ett+NXkJF8CHhsIQrY1V9vZnIaOY99Y7lmFJu/I3jzJhAzkA yg6RGO+flOPl+n2ISxAVQPvY4v3ZHo4hrKsiZ6KDvyOfq/s2cPK2FALUmqOh+MOGdrdigJhoppb +Rk3WIFcn8ooddt8NMGBqM2Tg8XaDA0RDxs8M4qNWTWyU6jV4DHsuQLOe5635BHdV6KQd9v+9iD 5pTdz474tKnir2EhB2qqI+vmVtziCrec10L9xTAvtkaLrbzucKylQCnM1VlsNYQIvRN4Gh4gGDS 52xQ8shcxIU/J/0VyHAJRBECzjhIzfanwqIKyQ9TwExyDiddm85L0hIc4eqU1jYcXhl803YRNdC HkG9SCv599f0ld163ezDvPAPmfrxcIVH1n8r6ogOuk/w3waIm4oQDJv8uZ8NngnwNwlA6bf0yCO lDRDS+aUyRWsaa9/yxmqXy8Ob5YxqYEuD6Thh8ryUb1izuwsUNswDL3MMBsVGVbNq1e/w5ldrxo zp5qyjPqhB7N5C6mBI3lVkdBVoPL6NAAFmNEdd9J X-Received: by 2002:a05:600c:e558:10b0:4bb:c9f8:aeb with SMTP id 5b1f17b1804b1-4bbc9f80cb1mr3657735e9.1.1796028520907; Mon, 30 Nov 2026 00:48:40 -0800 (PST) Received: from localhost ([2c0f:3d00:6be:8900:ce5e:9212:ea4b:f30]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8538b3a7sm1019819865e9.3.2026.11.30.00.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 00:48:40 -0800 (PST) From: Dan Carpenter Date: Mon, 30 Nov 2026 11:48:18 +0300 Subject: [PATCH v3 6/9] KVM: arm64: nv: Replace a pending SError when injecting a new one X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-double_exception_v3-v3-6-864856664b26@pereq.eu> References: <20261130-double_exception_v3-v3-0-864856664b26@pereq.eu> In-Reply-To: <20261130-double_exception_v3-v3-0-864856664b26@pereq.eu> To: Marc Zyngier , Oliver Upton , Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Rusty Russell , Marcelo Tosatti , James Morse , Sebastian Ott , Eric Auger , Paolo Bonzini , Sean Christopherson , Shuah Khan Cc: Dan Carpenter , Karthik Bhargavan , =?utf-8?q?Cl=C3=A9ment_Blaudeau?= , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Sashiko , stable@vger.kernel.org X-Mailer: b4 0.16.0 The kvm_inject_serror_esr() function injects SErrors as the name implies. The bug is that it doesn't handle already pending errors correctly in nested virtualization. In vanilla KVM the kvm_inject_serror_esr() function always sets the SError to pending. However with nested KVM, it might set it to pending or it might deliver the SError directly. We can only have one pending error at a time. Imagine we enter the function with an SError pending already. In the current code if we decide to pend the new error that's fine, everything works. However if we decide to deliver it directly then the pend flags should be cleared but they are not. So we deliver the SError directly and then deliver the SError that was pending on the next exit or entry. This bug can be triggered in the current code, however in a later patch in this series "KVM: arm64: nv: Re-inject a stored SError after the exit is handled" the bug will become much easier to trigger. In the current code this can only happen when userspace changes where the SError goes between two KVM_RUN calls. For example, userspace injects an SError with the KVM_SET_VCPU_EVENTS ioctl while the vcpu is at vEL2 with SErrors masked, so KVM sets HCR_EL2.VSE. Then userspace uses KVM_SET_ONE_REG to move the vcpu to L2 and injects a second SError. L1 takes SErrors from L2, so KVM delivers the second SError to L1 straight away, but HCR_EL2.VSE is still set. When L1 unmasks SErrors, it takes the first SError as well. After the "KVM: arm64: nv: Re-inject a stored SError after the exit is handled" commit then kvm_nested_sync_hwstate() only stores a pending SError when the guest exits. Then kvm_nested_flush_hwstate() re-injects the SError before the next entry. In between, KVM handles the exit and may return to userspace. If userspace injects an SError during that time, for example on an MMIO exit, the stored SError is still pending, without any change to the routing or masking. So this patch has to come first before the re-inject path. Clear the pend flags unconditionally at the start of the function. Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-229065@kernel.org/ Fixes: ce66109cec86 ("KVM: arm64: nv: Take "masked" aborts to EL2 when HCRX_EL2.TMEA is set") Fixes: 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v3: Add how the bug is triggered to the commit message v2: New patch --- arch/arm64/kvm/inject_fault.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/arm64/kvm/inject_fault.c b/arch/arm64/kvm/inject_fault.c index aa2011cc0d5a..0f6a89b65074 100644 --- a/arch/arm64/kvm/inject_fault.c +++ b/arch/arm64/kvm/inject_fault.c @@ -361,6 +361,10 @@ int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr) { lockdep_assert_held(&vcpu->mutex); + /* A new SError replaces any that is already pending. */ + vcpu_clear_flag(vcpu, NESTED_SERROR_PENDING); + *vcpu_hcr(vcpu) &= ~HCR_EL2_VSE; + if (is_nested_ctxt(vcpu) && kvm_serror_target_is_el2(vcpu)) return kvm_inject_nested_serror(vcpu, esr); -- 2.53.0