All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oliver Upton <oliver.upton@linux.dev>
To: Sean Christopherson <seanjc@google.com>
Cc: Colton Lewis <coltonlewis@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	David Matlack <dmatlack@google.com>,
	Vipin Sharma <vipinsh@google.com>,
	Andrew Jones <andrew.jones@linux.dev>,
	Marc Zyngier <maz@kernel.org>, Ben Gardon <bgardon@google.com>,
	Ricardo Koller <ricarkol@google.com>,
	kvm@vger.kernel.org
Subject: Re: [PATCH v3 2/2] KVM: selftests: Print summary stats of memory latency distribution
Date: Thu, 1 Jun 2023 05:51:40 +0000	[thread overview]
Message-ID: <ZHgx7GpYNoF/Go8O@linux.dev> (raw)
In-Reply-To: <ZHe1wEIYC6qsgupI@google.com>

On Wed, May 31, 2023 at 02:01:52PM -0700, Sean Christopherson wrote:
> On Mon, Mar 27, 2023, Colton Lewis wrote:
> > diff --git a/tools/testing/selftests/kvm/include/aarch64/processor.h b/tools/testing/selftests/kvm/include/aarch64/processor.h
> > index f65e491763e0..d441f485e9c6 100644
> > --- a/tools/testing/selftests/kvm/include/aarch64/processor.h
> > +++ b/tools/testing/selftests/kvm/include/aarch64/processor.h
> > @@ -219,4 +219,14 @@ uint32_t guest_get_vcpuid(void);
> >  uint64_t cycles_read(void);
> >  uint64_t cycles_to_ns(struct kvm_vcpu *vcpu, uint64_t cycles);
> > 
> > +#define MEASURE_CYCLES(x)			\
> > +	({					\
> > +		uint64_t start;			\
> > +		start = cycles_read();		\
> > +		isb();				\
> 
> Would it make sense to put the necessary barriers inside the cycles_read() (or
> whatever we end up calling it)?  Or does that not make sense on ARM?

+1. Additionally, the function should have a name that implies ordering,
like read_system_counter_ordered() or similar.

> > +		x;				\
> > +		dsb(nsh);			\

I assume you're doing this because you want to wait for outstanding
loads and stores to complete due to 'x', right?

My knee-jerk reaction was that you could just do an mb() and share the
implementation between arches, but it would seem the tools/ flavor of
the barrier demotes to a DMB because... reasons.

> > +		cycles_read() - start;		\
> > +	})
> > +
> >  #endif /* SELFTEST_KVM_PROCESSOR_H */
> ...
> 
> > diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
> > index 5d977f95d5f5..7352e02db4ee 100644
> > --- a/tools/testing/selftests/kvm/include/x86_64/processor.h
> > +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
> > @@ -1137,4 +1137,14 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
> >  uint64_t cycles_read(void);
> >  uint64_t cycles_to_ns(struct kvm_vcpu *vcpu, uint64_t cycles);
> > 
> > +#define MEASURE_CYCLES(x)			\
> > +	({					\
> > +		uint64_t start;			\
> > +		start = cycles_read();		\
> > +		asm volatile("mfence");		\
> 
> This is incorrect as placing the barrier after the RDTSC allows the RDTSC to be
> executed before earlier loads, e.g. could measure memory accesses from whatever
> was before MEASURE_CYCLES().  And per the kernel's rdtsc_ordered(), it sounds like
> RDTSC can only be hoisted before prior loads, i.e. will be ordered with respect
> to future loads and stores.

Same thing goes for the arm64 variant of the function... You want to
insert an isb() immediately _before_ you read the counter register to
avoid speculation.

arch_timer_read_cntvct_el0() back over in the kernel is a good example of
this. You can very likely ignore the ECV alternative for now.

-- 
Thanks,
Oliver

  parent reply	other threads:[~2023-06-01  5:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-27 21:26 [PATCH v3 0/2] KVM: selftests: Calculate memory latency stats Colton Lewis
2023-03-27 21:26 ` [PATCH v3 1/2] KVM: selftests: Provide generic way to read system counter Colton Lewis
2023-03-27 21:26 ` [PATCH v3 2/2] KVM: selftests: Print summary stats of memory latency distribution Colton Lewis
     [not found]   ` <ZHe1wEIYC6qsgupI@google.com>
2023-06-01  5:51     ` Oliver Upton [this message]
2023-06-01 18:12       ` Colton Lewis

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=ZHgx7GpYNoF/Go8O@linux.dev \
    --to=oliver.upton@linux.dev \
    --cc=andrew.jones@linux.dev \
    --cc=bgardon@google.com \
    --cc=coltonlewis@google.com \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=ricarkol@google.com \
    --cc=seanjc@google.com \
    --cc=vipinsh@google.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.