* [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr()
@ 2026-06-04 12:34 Qiang Ma
2026-06-05 1:41 ` Bibo Mao
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Qiang Ma @ 2026-06-04 12:34 UTC (permalink / raw)
To: zhaotianrui, maobibo, chenhuacai, kernel
Cc: kvm, loongarch, linux-kernel, Qiang Ma, stable
The LoongArch CSRXCHG instruction returns the full old CSR value in rd
after applying the masked update. kvm_emu_xchg_csr() currently masks
the saved value before returning it to the guest, so rd receives only
the bits selected by the write mask.
That breaks the architectural behavior and makes a zero mask return 0
instead of the previous CSR value. Keep the masked CSR update, but
return the unmodified old CSR value.
Fixes: da50f5a693ff ("LoongArch: KVM: Implement handle csr exception")
Cc: stable@vger.kernel.org
Signed-off-by: Qiang Ma <maqianga@uniontech.com>
---
arch/loongarch/kvm/exit.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c
index 3b95cd0f989b..264813d45cbe 100644
--- a/arch/loongarch/kvm/exit.c
+++ b/arch/loongarch/kvm/exit.c
@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid,
old = kvm_read_sw_gcsr(csr, csrid);
val = (old & ~csr_mask) | (val & csr_mask);
kvm_write_sw_gcsr(csr, csrid, val);
- old = old & csr_mask;
} else
pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", csrid, vcpu->arch.pc);
--
2.20.1
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() 2026-06-04 12:34 [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() Qiang Ma @ 2026-06-05 1:41 ` Bibo Mao [not found] ` <4CD61A5AB07B2388+7fa119a1-af5f-4def-b2c4-6073fc397b10@uniontech.com> 2026-06-05 7:19 ` Bibo Mao 2026-06-07 4:40 ` Huacai Chen 2 siblings, 1 reply; 6+ messages in thread From: Bibo Mao @ 2026-06-05 1:41 UTC (permalink / raw) To: Qiang Ma, zhaotianrui, chenhuacai, kernel Cc: kvm, loongarch, linux-kernel, stable On 2026/6/4 下午8:34, Qiang Ma wrote: > The LoongArch CSRXCHG instruction returns the full old CSR value in rd > after applying the masked update. kvm_emu_xchg_csr() currently masks > the saved value before returning it to the guest, so rd receives only > the bits selected by the write mask. > > That breaks the architectural behavior and makes a zero mask return 0 > instead of the previous CSR value. Keep the masked CSR update, but > return the unmodified old CSR value. > > Fixes: da50f5a693ff ("LoongArch: KVM: Implement handle csr exception") > Cc: stable@vger.kernel.org > Signed-off-by: Qiang Ma <maqianga@uniontech.com> > --- > arch/loongarch/kvm/exit.c | 1 - > 1 file changed, 1 deletion(-) > > diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c > index 3b95cd0f989b..264813d45cbe 100644 > --- a/arch/loongarch/kvm/exit.c > +++ b/arch/loongarch/kvm/exit.c > @@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, > old = kvm_read_sw_gcsr(csr, csrid); > val = (old & ~csr_mask) | (val & csr_mask); > kvm_write_sw_gcsr(csr, csrid, val); > - old = old & csr_mask; Hi Qiang Ma This is correct from the manual. Is there any test case or problem in practice? I want to evaluate severity about this problem. Regards Bibo Mao > } else > pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", csrid, vcpu->arch.pc); > > ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <4CD61A5AB07B2388+7fa119a1-af5f-4def-b2c4-6073fc397b10@uniontech.com>]
* Re: [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() [not found] ` <4CD61A5AB07B2388+7fa119a1-af5f-4def-b2c4-6073fc397b10@uniontech.com> @ 2026-06-05 6:27 ` Bibo Mao 2026-06-05 6:50 ` Qiang Ma 0 siblings, 1 reply; 6+ messages in thread From: Bibo Mao @ 2026-06-05 6:27 UTC (permalink / raw) To: Qiang Ma, zhaotianrui, chenhuacai, kernel Cc: kvm, loongarch, linux-kernel, stable On 2026/6/5 下午2:08, Qiang Ma wrote: > > 在 2026/6/5 09:41, Bibo Mao 写道: >> >> >> On 2026/6/4 下午8:34, Qiang Ma wrote: >>> The LoongArch CSRXCHG instruction returns the full old CSR value in rd >>> after applying the masked update. kvm_emu_xchg_csr() currently masks >>> the saved value before returning it to the guest, so rd receives only >>> the bits selected by the write mask. >>> >>> That breaks the architectural behavior and makes a zero mask return 0 >>> instead of the previous CSR value. Keep the masked CSR update, but >>> return the unmodified old CSR value. >>> >>> Fixes: da50f5a693ff ("LoongArch: KVM: Implement handle csr exception") >>> Cc: stable@vger.kernel.org >>> Signed-off-by: Qiang Ma <maqianga@uniontech.com> >>> --- >>> arch/loongarch/kvm/exit.c | 1 - >>> 1 file changed, 1 deletion(-) >>> >>> diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c >>> index 3b95cd0f989b..264813d45cbe 100644 >>> --- a/arch/loongarch/kvm/exit.c >>> +++ b/arch/loongarch/kvm/exit.c >>> @@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct >>> kvm_vcpu *vcpu, int csrid, >>> old = kvm_read_sw_gcsr(csr, csrid); >>> val = (old & ~csr_mask) | (val & csr_mask); >>> kvm_write_sw_gcsr(csr, csrid, val); >>> - old = old & csr_mask; >> >> Hi Qiang Ma >> >> This is correct from the manual. Is there any test case or problem in >> practice? I want to evaluate severity about this problem. > > Yes, I have written a selftest for this. Below are the test results > comparing without and with the patch. > > I have not encountered this problem in an actual operating environment. > > without this patch: > > [root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing > guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 > (implementation-specific control 1)Initial guest CSR value: > 0x10000100Checking that CSRXCHG updates the CSR per mask and returnsthe > full old CSR value in rd. > > Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned > old CSR value : 0expected old CSR value : 0x10000100CSR value after > update : 0x10000100expected CSR after update: 0x10000100result : FAIL > > Case: partial-maskwrite value : 0write mask : 0x100returned old CSR > value : 0x100expected old CSR value : 0x10000100CSR value after update : > 0x10000000expected CSR after update: 0x10000000result : FAIL > > CSRXCHG test FAILED > > with this patch: > > [root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing > guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 > (implementation-specific control 1)Initial guest CSR value: > 0x10000100Checking that CSRXCHG updates the CSR per mask and returnsthe > full old CSR value in rd. > > Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned > old CSR value : 0x10000100expected old CSR value : 0x10000100CSR value > after update : 0x10000100expected CSR after update: 0x10000100result : PASS > > Case: partial-maskwrite value : 0write mask : 0x100returned old CSR > value : 0x10000100expected old CSR value : 0x10000100CSR value after > update : 0x10000000expected CSR after update: 0x10000000result : PASS > > CSRXCHG test PASSED > > Should this selftest case be included as a patch and sent along with > version 2? No, it is not necessary. I test csrxchg instruction by myself, the manual is right, return value should be the whole old value. My meaning is that what is the scenery where CSR register is SW emulated in KVM mode, or this problem is found by code browsing. Regards Bibo Mao > >> >> Regards >> Bibo Mao >>> } else >>> pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", >>> csrid, vcpu->arch.pc); >>> >> >> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() 2026-06-05 6:27 ` Bibo Mao @ 2026-06-05 6:50 ` Qiang Ma 0 siblings, 0 replies; 6+ messages in thread From: Qiang Ma @ 2026-06-05 6:50 UTC (permalink / raw) To: Bibo Mao, zhaotianrui, chenhuacai, kernel Cc: kvm, loongarch, linux-kernel, stable 在 2026/6/5 14:27, Bibo Mao 写道: > > > On 2026/6/5 下午2:08, Qiang Ma wrote: >> >> 在 2026/6/5 09:41, Bibo Mao 写道: >>> >>> >>> On 2026/6/4 下午8:34, Qiang Ma wrote: >>>> The LoongArch CSRXCHG instruction returns the full old CSR value in rd >>>> after applying the masked update. kvm_emu_xchg_csr() currently masks >>>> the saved value before returning it to the guest, so rd receives only >>>> the bits selected by the write mask. >>>> >>>> That breaks the architectural behavior and makes a zero mask return 0 >>>> instead of the previous CSR value. Keep the masked CSR update, but >>>> return the unmodified old CSR value. >>>> >>>> Fixes: da50f5a693ff ("LoongArch: KVM: Implement handle csr exception") >>>> Cc: stable@vger.kernel.org >>>> Signed-off-by: Qiang Ma <maqianga@uniontech.com> >>>> --- >>>> arch/loongarch/kvm/exit.c | 1 - >>>> 1 file changed, 1 deletion(-) >>>> >>>> diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c >>>> index 3b95cd0f989b..264813d45cbe 100644 >>>> --- a/arch/loongarch/kvm/exit.c >>>> +++ b/arch/loongarch/kvm/exit.c >>>> @@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct >>>> kvm_vcpu *vcpu, int csrid, >>>> old = kvm_read_sw_gcsr(csr, csrid); >>>> val = (old & ~csr_mask) | (val & csr_mask); >>>> kvm_write_sw_gcsr(csr, csrid, val); >>>> - old = old & csr_mask; >>> >>> Hi Qiang Ma >>> >>> This is correct from the manual. Is there any test case or problem >>> in practice? I want to evaluate severity about this problem. >> >> Yes, I have written a selftest for this. Below are the test results >> comparing without and with the patch. >> >> I have not encountered this problem in an actual operating environment. >> >> without this patch: >> >> [root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing >> guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 >> (implementation-specific control 1)Initial guest CSR value: >> 0x10000100Checking that CSRXCHG updates the CSR per mask and >> returnsthe full old CSR value in rd. >> >> Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned >> old CSR value : 0expected old CSR value : 0x10000100CSR value after >> update : 0x10000100expected CSR after update: 0x10000100result : FAIL >> >> Case: partial-maskwrite value : 0write mask : 0x100returned old CSR >> value : 0x100expected old CSR value : 0x10000100CSR value after >> update : 0x10000000expected CSR after update: 0x10000000result : FAIL >> >> CSRXCHG test FAILED >> >> with this patch: >> >> [root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing >> guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 >> (implementation-specific control 1)Initial guest CSR value: >> 0x10000100Checking that CSRXCHG updates the CSR per mask and >> returnsthe full old CSR value in rd. >> >> Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned >> old CSR value : 0x10000100expected old CSR value : 0x10000100CSR >> value after update : 0x10000100expected CSR after update: >> 0x10000100result : PASS >> >> Case: partial-maskwrite value : 0write mask : 0x100returned old CSR >> value : 0x10000100expected old CSR value : 0x10000100CSR value after >> update : 0x10000000expected CSR after update: 0x10000000result : PASS >> >> CSRXCHG test PASSED >> >> Should this selftest case be included as a patch and sent along with >> version 2? > No, it is not necessary. I test csrxchg instruction by myself, the > manual is right, return value should be the whole old value. > > My meaning is that what is the scenery where CSR register is SW > emulated in KVM mode, or this problem is found by code browsing. Understood. This was discovered while browsing the code. > > Regards > Bibo Mao > >> >>> >>> Regards >>> Bibo Mao >>>> } else >>>> pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", >>>> csrid, vcpu->arch.pc); >>>> >>> >>> > > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() 2026-06-04 12:34 [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() Qiang Ma 2026-06-05 1:41 ` Bibo Mao @ 2026-06-05 7:19 ` Bibo Mao 2026-06-07 4:40 ` Huacai Chen 2 siblings, 0 replies; 6+ messages in thread From: Bibo Mao @ 2026-06-05 7:19 UTC (permalink / raw) To: Qiang Ma, zhaotianrui, chenhuacai, kernel Cc: kvm, loongarch, linux-kernel, stable On 2026/6/4 下午8:34, Qiang Ma wrote: > The LoongArch CSRXCHG instruction returns the full old CSR value in rd > after applying the masked update. kvm_emu_xchg_csr() currently masks > the saved value before returning it to the guest, so rd receives only > the bits selected by the write mask. > > That breaks the architectural behavior and makes a zero mask return 0 > instead of the previous CSR value. Keep the masked CSR update, but > return the unmodified old CSR value. > > Fixes: da50f5a693ff ("LoongArch: KVM: Implement handle csr exception") > Cc: stable@vger.kernel.org > Signed-off-by: Qiang Ma <maqianga@uniontech.com> > --- > arch/loongarch/kvm/exit.c | 1 - > 1 file changed, 1 deletion(-) > > diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c > index 3b95cd0f989b..264813d45cbe 100644 > --- a/arch/loongarch/kvm/exit.c > +++ b/arch/loongarch/kvm/exit.c > @@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, > old = kvm_read_sw_gcsr(csr, csrid); > val = (old & ~csr_mask) | (val & csr_mask); > kvm_write_sw_gcsr(csr, csrid, val); > - old = old & csr_mask; > } else > pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", csrid, vcpu->arch.pc); > > Reviewed-by: Bibo Mao <maobibo@loongson.cn> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() 2026-06-04 12:34 [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() Qiang Ma 2026-06-05 1:41 ` Bibo Mao 2026-06-05 7:19 ` Bibo Mao @ 2026-06-07 4:40 ` Huacai Chen 2 siblings, 0 replies; 6+ messages in thread From: Huacai Chen @ 2026-06-07 4:40 UTC (permalink / raw) To: Qiang Ma Cc: zhaotianrui, maobibo, kernel, kvm, loongarch, linux-kernel, stable Applied, thanks. Huacai On Thu, Jun 4, 2026 at 8:47 PM Qiang Ma <maqianga@uniontech.com> wrote: > > The LoongArch CSRXCHG instruction returns the full old CSR value in rd > after applying the masked update. kvm_emu_xchg_csr() currently masks > the saved value before returning it to the guest, so rd receives only > the bits selected by the write mask. > > That breaks the architectural behavior and makes a zero mask return 0 > instead of the previous CSR value. Keep the masked CSR update, but > return the unmodified old CSR value. > > Fixes: da50f5a693ff ("LoongArch: KVM: Implement handle csr exception") > Cc: stable@vger.kernel.org > Signed-off-by: Qiang Ma <maqianga@uniontech.com> > --- > arch/loongarch/kvm/exit.c | 1 - > 1 file changed, 1 deletion(-) > > diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c > index 3b95cd0f989b..264813d45cbe 100644 > --- a/arch/loongarch/kvm/exit.c > +++ b/arch/loongarch/kvm/exit.c > @@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, > old = kvm_read_sw_gcsr(csr, csrid); > val = (old & ~csr_mask) | (val & csr_mask); > kvm_write_sw_gcsr(csr, csrid, val); > - old = old & csr_mask; > } else > pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", csrid, vcpu->arch.pc); > > -- > 2.20.1 > ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-06-07 4:40 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-04 12:34 [PATCH] LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr() Qiang Ma
2026-06-05 1:41 ` Bibo Mao
[not found] ` <4CD61A5AB07B2388+7fa119a1-af5f-4def-b2c4-6073fc397b10@uniontech.com>
2026-06-05 6:27 ` Bibo Mao
2026-06-05 6:50 ` Qiang Ma
2026-06-05 7:19 ` Bibo Mao
2026-06-07 4:40 ` Huacai Chen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®