From nobody Mon Feb 9 16:51:01 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.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 64EA833984E for ; Mon, 26 Jan 2026 10:44:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769424294; cv=none; b=cW11jt/nwd+JcZ8OGLPBasJCQ93pjEpx6fFar0PUtjBDjSLwCIwMspbh6cJ6YdMR77L88tARM0JWAmxT2fNb3zUAnrmTM3kqqVise4eRiJtZDuy7tWbRmdEfTTfMkUBeE79jTxnROiybhOs1oAOguU/XurGOOt1HkeBVvkRmzkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769424294; c=relaxed/simple; bh=IEHoas6fg4+rHvu4ApD3tBhREacTshdHwr1AnsNE2hI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H94ldD+fiaqE9NsUOOBW07nYu5yyoWVBcrEj/ObkBK7oIS/tRaxt5Lq0qoXJ1z0yLilQNhbTe7P8fjQTVpdKCY2lJ0TPV/x0g9gVX2QZShRAVX/0SZAc/oJLimwQUlvE0KuoUI+HcYfg4GPbq/JPN6RqaPsoSTTiLyni1YzncQM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2oH1wDGv; arc=none smtp.client-ip=209.85.128.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--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2oH1wDGv" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-47ee3dd7fc8so47164765e9.3 for ; Mon, 26 Jan 2026 02:44:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769424289; x=1770029089; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GrrY1Bo4caR7Yg7iMIVZCactU50F+u23O+ceOpCGr/k=; b=2oH1wDGvBiQkMzqeMSneLzZmyw5y3LIn3Uail1OOrhSrbsZzH/DnPPqUraKgvPI/Cg U/IvLfVLwOOGQWbAF2UsBO1f2ccLKiA/zL8fgMflVWbuB27Xer58VygDuzfZGLZlX+uk 7bRtBVo+RPF2bVL3o5uN5qAZ1zcDriiFAUfrxzgGOO8tWlxYT2+q7u3zEskuwthBV4XA XT9BZoCl44V+udi7nzO3zOMOQB69Bhi4OqiCvn0DIEg5vWezZylbZ66SvQboI/o3tUpH xvxuCND6qABzRfvgvXXQDdngSyMUZmydcbYvMv4a9k+qa26F4oTr9bIDp+VZDQyoavy5 1+iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769424289; x=1770029089; h=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=GrrY1Bo4caR7Yg7iMIVZCactU50F+u23O+ceOpCGr/k=; b=fDiCLnG93ujhKd7acKaUNyOpI9BZl983x6WCAbN4RLoqH4pLYDzymyAcoHCrN+yIK9 1NAHJwJynejmctlHok7bEZYuQdIx7lZraVD9XBa5LABY/8VNdWxASg/gsmtf1CNG28ox bsyg6Y5tMXsFtMQFgkteJXytjdEDQPvSem1VapTaHYYsEQF3jPXmO40SeqM9UWSvc8l4 H4hWPObI2n2oWbzQ/v+CyQ7dY94OPyIrLqXbFkkT4TLFqoaVZVnvzl6c66T9DaVGprJO EeXiHziRoRBrf71AtDjTNtKDkvIfDKLpo5Bzsr35joktiObZ97Qi7f72ibJ7YGrCRIAG +O+g== X-Forwarded-Encrypted: i=1; AJvYcCUDrUXBKLkBdaUedkGKEYmyoAwKHRZakrLZeio3WQJ8tUVY71I/6shrNw2fasTgfpRKW6PTTu8imOVSFb8=@vger.kernel.org X-Gm-Message-State: AOJu0YyE1v3xC/iBWkhhTxzl6/+b4k5aDKKjguFKIycQp/18kmcRRn7O HnN7HCSAOre8oL8WwzSh9lH5fkUy18ZieSFyVVLzjjoblq8Zb89jX89VNIzaaGqQvDPGaViiBK8 gUj5nEq2JmFEsxcI+PoZF/g== X-Received: from wmbjj24.prod.google.com ([2002:a05:600c:6a18:b0:480:4a03:7b79]) (user=vdonnefort job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:b95:b0:477:55ce:f3c2 with SMTP id 5b1f17b1804b1-4805ce3faf5mr68562295e9.14.1769424288788; Mon, 26 Jan 2026 02:44:48 -0800 (PST) Date: Mon, 26 Jan 2026 10:44:14 +0000 In-Reply-To: <20260126104419.1649811-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260126104419.1649811-1-vdonnefort@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260126104419.1649811-26-vdonnefort@google.com> Subject: [PATCH v10 25/30] KVM: arm64: Sync boot clock with the nVHE/pKVM hyp From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, linux-trace-kernel@vger.kernel.org, maz@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, jstultz@google.com, qperret@google.com, will@kernel.org, aneesh.kumar@kernel.org, kernel-team@android.com, linux-kernel@vger.kernel.org, Vincent Donnefort , Thomas Gleixner , Stephen Boyd , "Christopher S. Hall" , Richard Cochran Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Configure the hypervisor tracing clock with the kernel boot clock. For tracing purposes, the boot clock is interesting: it doesn't stop on suspend. However, it is corrected on a regular basis, which implies the need to re-evaluate it every once in a while. Cc: John Stultz Cc: Thomas Gleixner Cc: Stephen Boyd Cc: Christopher S. Hall Cc: Richard Cochran Signed-off-by: Vincent Donnefort diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_= asm.h index c3a7fc939f42..352ebbedaab2 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -93,6 +93,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___tracing_unload, __KVM_HOST_SMCCC_FUNC___tracing_enable, __KVM_HOST_SMCCC_FUNC___tracing_swap_reader, + __KVM_HOST_SMCCC_FUNC___tracing_update_clock, }; =20 #define DECLARE_KVM_VHE_SYM(sym) extern char sym[] diff --git a/arch/arm64/kvm/hyp/include/nvhe/trace.h b/arch/arm64/kvm/hyp/i= nclude/nvhe/trace.h index 7da8788ce527..fd641e1b1c23 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/trace.h +++ b/arch/arm64/kvm/hyp/include/nvhe/trace.h @@ -11,6 +11,7 @@ int __tracing_load(unsigned long desc_va, size_t desc_siz= e); void __tracing_unload(void); int __tracing_enable(bool enable); int __tracing_swap_reader(unsigned int cpu); +void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_c= yc); #else static inline void *tracing_reserve_entry(unsigned long length) { return N= ULL; } static inline void tracing_commit_entry(void) { } @@ -19,5 +20,6 @@ static inline int __tracing_load(unsigned long desc_va, s= ize_t desc_size) { retu static inline void __tracing_unload(void) { } static inline int __tracing_enable(bool enable) { return -ENODEV; } static inline int __tracing_swap_reader(unsigned int cpu) { return -ENODEV= ; } +static inline void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_n= s, u64 epoch_cyc) { } #endif #endif diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/h= yp-main.c index eddbf5df5d13..9df0d37a494b 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -615,6 +615,16 @@ static void handle___tracing_swap_reader(struct kvm_cp= u_context *host_ctxt) cpu_reg(host_ctxt, 1) =3D __tracing_swap_reader(cpu); } =20 +static void handle___tracing_update_clock(struct kvm_cpu_context *host_ctx= t) +{ + DECLARE_REG(u32, mult, host_ctxt, 1); + DECLARE_REG(u32, shift, host_ctxt, 2); + DECLARE_REG(u64, epoch_ns, host_ctxt, 3); + DECLARE_REG(u64, epoch_cyc, host_ctxt, 4); + + __tracing_update_clock(mult, shift, epoch_ns, epoch_cyc); +} + typedef void (*hcall_t)(struct kvm_cpu_context *); =20 #define HANDLE_FUNC(x) [__KVM_HOST_SMCCC_FUNC_##x] =3D (hcall_t)handle_##x @@ -660,6 +670,7 @@ static const hcall_t host_hcall[] =3D { HANDLE_FUNC(__tracing_unload), HANDLE_FUNC(__tracing_enable), HANDLE_FUNC(__tracing_swap_reader), + HANDLE_FUNC(__tracing_update_clock), }; =20 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp/nvhe/trace.c b/arch/arm64/kvm/hyp/nvhe/trac= e.c index df9d66fcb3c9..97e9f6c1a52c 100644 --- a/arch/arm64/kvm/hyp/nvhe/trace.c +++ b/arch/arm64/kvm/hyp/nvhe/trace.c @@ -271,3 +271,19 @@ int __tracing_swap_reader(unsigned int cpu) =20 return ret; } + +void __tracing_update_clock(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_c= yc) +{ + int cpu; + + /* After this loop, all CPUs are observing the new bank... */ + for (cpu =3D 0; cpu < hyp_nr_cpus; cpu++) { + struct simple_rb_per_cpu *simple_rb =3D per_cpu_ptr(trace_buffer.simple_= rbs, cpu); + + while (READ_ONCE(simple_rb->status) =3D=3D SIMPLE_RB_WRITING) + ; + } + + /* ...we can now override the old one and swap. */ + trace_clock_update(mult, shift, epoch_ns, epoch_cyc); +} diff --git a/arch/arm64/kvm/hyp_trace.c b/arch/arm64/kvm/hyp_trace.c index 2866effe28ec..1e5fc27f0e9d 100644 --- a/arch/arm64/kvm/hyp_trace.c +++ b/arch/arm64/kvm/hyp_trace.c @@ -4,15 +4,133 @@ * Author: Vincent Donnefort */ =20 +#include #include +#include #include =20 +#include #include #include #include =20 #include "hyp_trace.h" =20 +/* Same 10min used by clocksource when width is more than 32-bits */ +#define CLOCK_MAX_CONVERSION_S 600 +/* + * Time to give for the clock init. Long enough to get a good mult/shift + * estimation. Short enough to not delay the tracing start too much. + */ +#define CLOCK_INIT_MS 100 +/* + * Time between clock checks. Must be small enough to catch clock deviatio= n when + * it is still tiny. + */ +#define CLOCK_UPDATE_MS 500 + +static struct hyp_trace_clock { + u64 cycles; + u64 cyc_overflow64; + u64 boot; + u32 mult; + u32 shift; + struct delayed_work work; + struct completion ready; + struct mutex lock; + bool running; +} hyp_clock; + +static void __hyp_clock_work(struct work_struct *work) +{ + struct delayed_work *dwork =3D to_delayed_work(work); + struct hyp_trace_clock *hyp_clock; + struct system_time_snapshot snap; + u64 rate, delta_cycles; + u64 boot, delta_boot; + + hyp_clock =3D container_of(dwork, struct hyp_trace_clock, work); + + ktime_get_snapshot(&snap); + boot =3D ktime_to_ns(snap.boot); + + delta_boot =3D boot - hyp_clock->boot; + delta_cycles =3D snap.cycles - hyp_clock->cycles; + + /* Compare hyp clock with the kernel boot clock */ + if (hyp_clock->mult) { + u64 err, cur =3D delta_cycles; + + if (WARN_ON_ONCE(cur >=3D hyp_clock->cyc_overflow64)) { + __uint128_t tmp =3D (__uint128_t)cur * hyp_clock->mult; + + cur =3D tmp >> hyp_clock->shift; + } else { + cur *=3D hyp_clock->mult; + cur >>=3D hyp_clock->shift; + } + cur +=3D hyp_clock->boot; + + err =3D abs_diff(cur, boot); + /* No deviation, only update epoch if necessary */ + if (!err) { + if (delta_cycles >=3D (hyp_clock->cyc_overflow64 >> 1)) + goto fast_forward; + + goto resched; + } + + /* Warn if the error is above tracing precision (1us) */ + if (err > NSEC_PER_USEC) + pr_warn_ratelimited("hyp trace clock off by %lluus\n", + err / NSEC_PER_USEC); + } + + rate =3D div64_u64(delta_cycles * NSEC_PER_SEC, delta_boot); + + clocks_calc_mult_shift(&hyp_clock->mult, &hyp_clock->shift, + rate, NSEC_PER_SEC, CLOCK_MAX_CONVERSION_S); + + /* Add a comfortable 50% margin */ + hyp_clock->cyc_overflow64 =3D (U64_MAX / hyp_clock->mult) >> 1; + +fast_forward: + hyp_clock->cycles =3D snap.cycles; + hyp_clock->boot =3D boot; + kvm_call_hyp_nvhe(__tracing_update_clock, hyp_clock->mult, + hyp_clock->shift, hyp_clock->boot, hyp_clock->cycles); + complete(&hyp_clock->ready); + +resched: + schedule_delayed_work(&hyp_clock->work, + msecs_to_jiffies(CLOCK_UPDATE_MS)); +} + +static void hyp_trace_clock_enable(struct hyp_trace_clock *hyp_clock, bool= enable) +{ + struct system_time_snapshot snap; + + if (hyp_clock->running =3D=3D enable) + return; + + if (!enable) { + cancel_delayed_work_sync(&hyp_clock->work); + hyp_clock->running =3D false; + } + + ktime_get_snapshot(&snap); + + hyp_clock->boot =3D ktime_to_ns(snap.boot); + hyp_clock->cycles =3D snap.cycles; + hyp_clock->mult =3D 0; + + init_completion(&hyp_clock->ready); + INIT_DELAYED_WORK(&hyp_clock->work, __hyp_clock_work); + schedule_delayed_work(&hyp_clock->work, msecs_to_jiffies(CLOCK_INIT_MS)); + wait_for_completion(&hyp_clock->ready); + hyp_clock->running =3D true; +} + /* Access to this struct within the trace_remote_callbacks are protected b= y the trace_remote lock */ static struct hyp_trace_buffer { struct hyp_trace_desc *desc; @@ -183,6 +301,8 @@ static void hyp_trace_unload(struct trace_buffer_desc *= desc, void *priv) =20 static int hyp_trace_enable_tracing(bool enable, void *priv) { + hyp_trace_clock_enable(&hyp_clock, enable); + return kvm_call_hyp_nvhe(__tracing_enable, enable); } =20 @@ -201,7 +321,22 @@ static int hyp_trace_enable_event(unsigned short id, b= ool enable, void *priv) return 0; } =20 +static int hyp_trace_clock_show(struct seq_file *m, void *v) +{ + seq_puts(m, "[boot]\n"); + + return 0; +} +DEFINE_SHOW_ATTRIBUTE(hyp_trace_clock); + +static int hyp_trace_init_tracefs(struct dentry *d, void *priv) +{ + return tracefs_create_file("trace_clock", 0440, d, NULL, &hyp_trace_clock= _fops) ? + 0 : -ENOMEM; +} + static struct trace_remote_callbacks trace_remote_callbacks =3D { + .init =3D hyp_trace_init_tracefs, .load_trace_buffer =3D hyp_trace_load, .unload_trace_buffer =3D hyp_trace_unload, .enable_tracing =3D hyp_trace_enable_tracing, @@ -212,8 +347,22 @@ static struct trace_remote_callbacks trace_remote_call= backs =3D { =20 int __init kvm_hyp_trace_init(void) { + int cpu; + if (is_kernel_in_hyp_mode()) return 0; =20 +#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND + for_each_possible_cpu(cpu) { + const struct arch_timer_erratum_workaround *wa =3D + per_cpu(timer_unstable_counter_workaround, cpu); + + if (wa && wa->read_cntvct_el0) { + pr_warn("hyp trace can't handle CNTVCT workaround '%s'\n", wa->desc); + return -EOPNOTSUPP; + } + } +#endif + return trace_remote_register("hypervisor", &trace_remote_callbacks, &trac= e_buffer, NULL, 0); } --=20 2.52.0.457.g6b5491de43-goog