From: Vincent Donnefort <vdonnefort@google.com>
To: John Stultz <jstultz@google.com>
Cc: rostedt@goodmis.org, mhiramat@kernel.org,
linux-trace-kernel@vger.kernel.org, maz@kernel.org,
oliver.upton@linux.dev, kvmarm@lists.linux.dev, will@kernel.org,
qperret@google.com, kernel-team@android.com,
linux-kernel@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Stephen Boyd <sboyd@kernel.org>,
"Christopher S. Hall" <christopher.s.hall@intel.com>,
Richard Cochran <richardcochran@gmail.com>,
Lakshmi Sowjanya D <lakshmi.sowjanya.d@intel.com>
Subject: Re: [PATCH 09/13] KVM: arm64: Add clock for hyp tracefs
Date: Mon, 16 Sep 2024 13:39:11 +0100 [thread overview]
Message-ID: <Zugm77Z47-kal5rf@google.com> (raw)
In-Reply-To: <CANDhNCqvwn7W8JgdzY=2PrEk0unm3E0Kso7h2CCZBPO+hzLaOw@mail.gmail.com>
On Fri, Sep 13, 2024 at 04:21:05PM -0700, 'John Stultz' via kernel-team wrote:
> On Wed, Sep 11, 2024 at 2:31 AM Vincent Donnefort <vdonnefort@google.com> wrote:
> >
> > Configure the hypervisor tracing clock before starting tracing. For
> > tracing purpose, the boot clock is interesting as it doesn't stop on
> > suspend. However, it is corrected on a regular basis, which implies we
> > need to re-evaluate it every once in a while.
> >
> > Cc: John Stultz <jstultz@google.com>
> > Cc: Thomas Gleixner <tglx@linutronix.de>
> > Cc: Stephen Boyd <sboyd@kernel.org>
> > Cc: Christopher S. Hall <christopher.s.hall@intel.com>
> > Cc: Richard Cochran <richardcochran@gmail.com>
> > Cc: Lakshmi Sowjanya D <lakshmi.sowjanya.d@intel.com>
> > Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> >
> ...
> > +static void __hyp_clock_work(struct work_struct *work)
> > +{
> > + struct delayed_work *dwork = to_delayed_work(work);
> > + struct hyp_trace_buffer *hyp_buffer;
> > + struct hyp_trace_clock *hyp_clock;
> > + struct system_time_snapshot snap;
> > + u64 rate, delta_cycles;
> > + u64 boot, delta_boot;
> > + u64 err = 0;
> > +
> > + hyp_clock = container_of(dwork, struct hyp_trace_clock, work);
> > + hyp_buffer = container_of(hyp_clock, struct hyp_trace_buffer, clock);
> > +
> > + ktime_get_snapshot(&snap);
> > + boot = ktime_to_ns(snap.boot);
> > +
> > + delta_boot = boot - hyp_clock->boot;
> > + delta_cycles = snap.cycles - hyp_clock->cycles;
> > +
> > + /* Compare hyp clock with the kernel boot clock */
> > + if (hyp_clock->mult) {
> > + u64 cur = delta_cycles;
> > +
> > + cur *= hyp_clock->mult;
>
> Mult overflow protection (I see you already have a max_delta value) is
> probably needed here.
That should never happen really with the max_delta. But I could add a WARN_ON
and fallback to a 128-bits compute instead here too?
>
> > + cur >>= hyp_clock->shift;
> > + cur += hyp_clock->boot;
> > +
> > + err = abs_diff(cur, boot);
> > +
> > + /* No deviation, only update epoch if necessary */
> > + if (!err) {
> > + if (delta_cycles >= hyp_clock->max_delta)
> > + goto update_hyp;
> > +
> > + goto resched;
> > + }
> > +
> > + /* Warn if the error is above tracing precision (1us) */
> > + if (hyp_buffer->tracing_on && err > NSEC_PER_USEC)
> > + pr_warn_ratelimited("hyp trace clock off by %lluus\n",
> > + err / NSEC_PER_USEC);
>
> I'm curious in practice, does this come up often? If so, does it
> converge down nicely? Have you done much disruption testing using
> adjtimex?
So far, I haven't seen any error above ~100 ns on the machine I have tested
with, but that's a good point, I'll check how it looks when the boot clock is
less stable.
>
> > + }
> > +
> > + if (delta_boot > U32_MAX) {
> > + do_div(delta_boot, NSEC_PER_SEC);
> > + rate = delta_cycles;
> > + } else {
> > + rate = delta_cycles * NSEC_PER_SEC;
> > + }
> > +
> > + do_div(rate, delta_boot);
> > +
> > + clocks_calc_mult_shift(&hyp_clock->mult, &hyp_clock->shift,
> > + rate, NSEC_PER_SEC, CLOCK_MAX_CONVERSION_S);
> > +
> > +update_hyp:
> > + hyp_clock->max_delta = (U64_MAX / hyp_clock->mult) >> 1;
> > + hyp_clock->cycles = snap.cycles;
> > + hyp_clock->boot = boot;
> > + kvm_call_hyp_nvhe(__pkvm_update_clock_tracing, hyp_clock->mult,
> > + hyp_clock->shift, hyp_clock->boot, hyp_clock->cycles);
> > + complete(&hyp_clock->ready);
>
> I'm very forgetful, so maybe it's unnecessary, but for future-you or
> just other's like me, it might be worth adding some extra comments to
> clarify the assumptions in these calculations.
Ack.
>
>
> thanks
> -john
Thanks for your time!
--
Vincent
next prev parent reply other threads:[~2024-09-16 12:39 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-11 9:30 [PATCH 00/13] Tracefs support for pKVM Vincent Donnefort
2024-09-11 9:30 ` [PATCH 01/13] ring-buffer: Check for empty ring-buffer with rb_num_of_entries() Vincent Donnefort
2024-09-11 9:30 ` [PATCH 02/13] ring-buffer: Introducing ring-buffer writer Vincent Donnefort
2024-09-12 12:55 ` kernel test robot
2024-09-11 9:30 ` [PATCH 03/13] ring-buffer: Expose buffer_data_page material Vincent Donnefort
2024-09-11 9:30 ` [PATCH 04/13] timekeeping: Add the boot clock to system time snapshot Vincent Donnefort
2024-09-13 22:28 ` John Stultz
2024-10-02 15:15 ` Thomas Gleixner
2024-09-11 9:30 ` [PATCH 05/13] KVM: arm64: Support unaligned fixmap in the nVHE hyp Vincent Donnefort
2024-09-11 9:30 ` [PATCH 06/13] KVM: arm64: Add clock support " Vincent Donnefort
2024-09-13 22:41 ` John Stultz
2024-09-16 12:26 ` Vincent Donnefort
2024-09-11 9:30 ` [PATCH 07/13] KVM: arm64: Add tracing support for the pKVM hyp Vincent Donnefort
2024-09-11 9:30 ` [PATCH 08/13] KVM: arm64: Add hyp tracing to tracefs Vincent Donnefort
2024-09-11 9:30 ` [PATCH 09/13] KVM: arm64: Add clock for hyp tracefs Vincent Donnefort
2024-09-13 23:21 ` John Stultz
2024-09-16 12:39 ` Vincent Donnefort [this message]
2024-09-11 9:30 ` [PATCH 10/13] KVM: arm64: Add raw interface " Vincent Donnefort
2024-09-11 9:30 ` [PATCH 11/13] KVM: arm64: Add trace " Vincent Donnefort
2024-09-11 9:30 ` [PATCH 12/13] KVM: arm64: Add support for hyp events Vincent Donnefort
2024-09-11 9:30 ` [PATCH 13/13] KVM: arm64: Add kselftest for tracefs hyp tracefs Vincent Donnefort
2025-01-07 19:54 ` [PATCH 00/13] Tracefs support for pKVM Steven Rostedt
2025-01-07 21:46 ` Vincent Donnefort
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Zugm77Z47-kal5rf@google.com \
--to=vdonnefort@google.com \
--cc=christopher.s.hall@intel.com \
--cc=jstultz@google.com \
--cc=kernel-team@android.com \
--cc=kvmarm@lists.linux.dev \
--cc=lakshmi.sowjanya.d@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=maz@kernel.org \
--cc=mhiramat@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=qperret@google.com \
--cc=richardcochran@gmail.com \
--cc=rostedt@goodmis.org \
--cc=sboyd@kernel.org \
--cc=tglx@linutronix.de \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).