* [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code
@ 2025-02-12 17:34 Marc Zyngier
2025-02-12 21:01 ` Oliver Upton
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Marc Zyngier @ 2025-02-12 17:34 UTC (permalink / raw)
To: kvmarm, linux-arm-kernel
Cc: Joey Gouly, Suzuki K Poulose, Oliver Upton, Zenghui Yu,
Linux Kernel Functional Testing
Now that EL2 has gained some early timer emulation, it accesses
the offsets pointed to by the timer structure, both of which
live in the KVM structure.
Of course, these are *kernel* pointers, so the dereferencing
of these pointers in non-kernel code must be itself be offset.
Given switch.h its own version of timer_get_offset() and use that
instead.
Fixes: b86fc215dc26d ("KVM: arm64: Handle counter access early in non-HYP context")
Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
Signed-off-by: Marc Zyngier <maz@kernel.org>
---
arch/arm64/kvm/hyp/include/hyp/switch.h | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h
index f5e882a358e2d..23bbe28eaaf95 100644
--- a/arch/arm64/kvm/hyp/include/hyp/switch.h
+++ b/arch/arm64/kvm/hyp/include/hyp/switch.h
@@ -581,9 +581,22 @@ static inline bool handle_tx2_tvm(struct kvm_vcpu *vcpu)
return true;
}
+/* Open-coded version of timer_get_offset() to allow for kern_hyp_va() */
+static inline u64 hyp_timer_get_offset(struct arch_timer_context *ctxt)
+{
+ u64 offset = 0;
+
+ if (ctxt->offset.vm_offset)
+ offset += *kern_hyp_va(ctxt->offset.vm_offset);
+ if (ctxt->offset.vcpu_offset)
+ offset += *kern_hyp_va(ctxt->offset.vcpu_offset);
+
+ return offset;
+}
+
static inline u64 compute_counter_value(struct arch_timer_context *ctxt)
{
- return arch_timer_read_cntpct_el0() - timer_get_offset(ctxt);
+ return arch_timer_read_cntpct_el0() - hyp_timer_get_offset(ctxt);
}
static bool kvm_handle_cntxct(struct kvm_vcpu *vcpu)
--
2.39.2
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code
2025-02-12 17:34 [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code Marc Zyngier
@ 2025-02-12 21:01 ` Oliver Upton
2025-02-13 9:59 ` Anders Roxell
2025-02-13 11:06 ` Marc Zyngier
2 siblings, 0 replies; 4+ messages in thread
From: Oliver Upton @ 2025-02-12 21:01 UTC (permalink / raw)
To: Marc Zyngier
Cc: kvmarm, linux-arm-kernel, Joey Gouly, Suzuki K Poulose,
Zenghui Yu, Linux Kernel Functional Testing
On Wed, Feb 12, 2025 at 05:34:54PM +0000, Marc Zyngier wrote:
> Now that EL2 has gained some early timer emulation, it accesses
> the offsets pointed to by the timer structure, both of which
> live in the KVM structure.
>
> Of course, these are *kernel* pointers, so the dereferencing
> of these pointers in non-kernel code must be itself be offset.
>
> Given switch.h its own version of timer_get_offset() and use that
> instead.
>
> Fixes: b86fc215dc26d ("KVM: arm64: Handle counter access early in non-HYP context")
> Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
> Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code
2025-02-12 17:34 [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code Marc Zyngier
2025-02-12 21:01 ` Oliver Upton
@ 2025-02-13 9:59 ` Anders Roxell
2025-02-13 11:06 ` Marc Zyngier
2 siblings, 0 replies; 4+ messages in thread
From: Anders Roxell @ 2025-02-13 9:59 UTC (permalink / raw)
To: Marc Zyngier
Cc: kvmarm, linux-arm-kernel, Joey Gouly, Suzuki K Poulose,
Oliver Upton, Zenghui Yu, Linux Kernel Functional Testing
On Wed, 12 Feb 2025 at 18:35, 'Marc Zyngier' via lkft <lkft@linaro.org> wrote:
>
> Now that EL2 has gained some early timer emulation, it accesses
> the offsets pointed to by the timer structure, both of which
> live in the KVM structure.
>
> Of course, these are *kernel* pointers, so the dereferencing
> of these pointers in non-kernel code must be itself be offset.
>
> Given switch.h its own version of timer_get_offset() and use that
> instead.
>
> Fixes: b86fc215dc26d ("KVM: arm64: Handle counter access early in non-HYP context")
> Reported-by: Linux Kernel Functional Testing <lkft@linaro.org>
> Signed-off-by: Marc Zyngier <maz@kernel.org>
Tested-by: Anders Roxell <anders.roxell@linaro.org>
Build this patch ontop of next-20250212 and ran kvm-unit-tests on a rockpi4.
And it ran fine without any kernel panic.
Cheers,
Anders
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code
2025-02-12 17:34 [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code Marc Zyngier
2025-02-12 21:01 ` Oliver Upton
2025-02-13 9:59 ` Anders Roxell
@ 2025-02-13 11:06 ` Marc Zyngier
2 siblings, 0 replies; 4+ messages in thread
From: Marc Zyngier @ 2025-02-13 11:06 UTC (permalink / raw)
To: kvmarm, linux-arm-kernel, Marc Zyngier
Cc: Joey Gouly, Suzuki K Poulose, Oliver Upton, Zenghui Yu,
Linux Kernel Functional Testing
On Wed, 12 Feb 2025 17:34:54 +0000, Marc Zyngier wrote:
> Now that EL2 has gained some early timer emulation, it accesses
> the offsets pointed to by the timer structure, both of which
> live in the KVM structure.
>
> Of course, these are *kernel* pointers, so the dereferencing
> of these pointers in non-kernel code must be itself be offset.
>
> [...]
Applied to fixes, thanks!
[1/1] KVM: arm64: Convert timer offset VA when accessed in HYP code
commit: 18ec0be672a4a27aa045cf692eeb1e180a202e0c
Cheers,
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-02-13 11:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-12 17:34 [PATCH] KVM: arm64: Convert timer offset VA when accessed in HYP code Marc Zyngier
2025-02-12 21:01 ` Oliver Upton
2025-02-13 9:59 ` Anders Roxell
2025-02-13 11:06 ` Marc Zyngier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).