From: Sean Christopherson <seanjc@google.com>
To: Tao Lyu <tao.lyu@epfl.ch>
Cc: Dongli Zhang <dongli.zhang@oracle.com>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"akalita@cs.stonybrook.edu" <akalita@cs.stonybrook.edu>
Subject: Re: obtain the timestamp counter of physical/host machine inside the VMs.
Date: Tue, 2 Jan 2024 09:53:27 -0800 [thread overview]
Message-ID: <ZZRNly2jIIVyC5F6@google.com> (raw)
In-Reply-To: <a31d33cb6eb14ddda272b9d291c5ae00@epfl.ch>
On Tue, Jan 02, 2024, Tao Lyu wrote:
>
> Hi Dongli,
>
> > On 1/1/24 14:06, Tao Lyu wrote:
> >> Hello Arnabjyoti, Sean, and everyone,
> >>
> >> I'm having a similiar but slightly differnt issue about the rdtsc in KVM.
> >>
> >> I want to obtain the timestamp counter of physical/host machine inside the VMs.
> >>
> >> Acccording to the previous threads, I know I need to disable the offsetting, VM exit, and scaling.
> >> I specify the correspoding parameters in the qemu arguments.
> >> The booting command is listed below:
> >>
> >> qemu-system-x86_64 -m 10240 -smp 4 -chardev socket,id=SOCKSYZ,server=on,nowait,host=localhost,port=3258 -mon chardev=SOCKSYZ,mode=control -display none -serial stdio -device virtio-rng-pci -enable-kvm -cpu host,migratable=off,tsc=on,rdtscp=on,vmx-tsc-offset=off,vmx-rdtsc-exit=off,tsc-scale=off,tsc-adjust=off,vmx-rdtscp-exit=off -netdev bridge,id=hn40 -device virtio-net,netdev=hn40,mac=e6:c8:ff:09:76:38 -hda XXX -kernel XXX -append "root=/dev/sda console=ttyS0"
> >>
> >>
> >> But the rdtsc still returns the adjusted tsc.
> >> The vmxcap script shows the TSC settings as below:
> >>
> >> Use TSC offsetting no
> >> RDTSC exiting no
> >> Enable RDTSCP no
> >> TSC scaling yes
> >>
> >>
> >> I would really appreciate it if anyone can tell me whether and how I can get the tsc of physical machine insdie the VM.
>
> > If the objective is to obtain the same tsc at both VM and host side (that is, to
> > avoid any offset or scaling), I can obtain quite close tsc at both VM and host
> > side with the below linux-6.6 change.
>
> > My env does not use tsc scaling.
>
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 41cce50..b102dcd 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> >@@ -2723,7 +2723,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64
> data)
> > bool synchronizing = false;
> >
> > raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
> > - offset = kvm_compute_l1_tsc_offset(vcpu, data);
> > + offset = 0;
> > ns = get_kvmclock_base_ns();
> > elapsed = ns - kvm->arch.last_tsc_nsec;
> >
> > Dongli Zhang
>
>
> Hi Dongli,
>
> Thank you so much for the explanation and for providing a patch.
> It works for me now.
Yeah, during vCPU creation KVM sets a target guest TSC of '0', i.e. sets the TSC
offset to "0 - HOST_TSC". As of commit 828ca89628bf ("KVM: x86: Expose TSC offset
controls to userspace"), userspace can explicitly set an offset of '0' via
KVM_VCPU_TSC_CTRL+KVM_VCPU_TSC_OFFSET, but AFAIK QEMU doesn't support that API.
All the other methods for setting the TSC offset are indirect, i.e. userspace
provides the target TSC and KVM computes the offset. So even if QEMU provides a
way to specify an explicit TSC (or offset), there will be a healthy amount of slop.
next prev parent reply other threads:[~2024-01-02 17:53 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-17 4:54 Intercepting RDTSC instruction by causing a VMEXIT Arnabjyoti Kalita
2021-04-19 16:46 ` Sean Christopherson
2021-04-20 2:33 ` Arnabjyoti Kalita
[not found] ` <CAJGDS+GT1mKHz6K=qHQf54S_97ym=nRP12MfO6OSEOpLYGht=A@mail.gmail.com>
2021-04-26 1:51 ` Arnabjyoti Kalita
2021-04-26 16:05 ` Sean Christopherson
2021-04-27 2:19 ` Arnabjyoti Kalita
2021-04-30 17:16 ` Arnabjyoti Kalita
2024-01-01 22:06 ` obtain the timestamp counter of physical/host machine inside the VMs Tao Lyu
2024-01-02 5:11 ` Dongli Zhang
2024-01-02 10:10 ` Tao Lyu
2024-01-02 17:53 ` Sean Christopherson [this message]
2024-01-02 18:20 ` Tao Lyu
2024-01-02 19:36 ` Dongli Zhang
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=ZZRNly2jIIVyC5F6@google.com \
--to=seanjc@google.com \
--cc=akalita@cs.stonybrook.edu \
--cc=dongli.zhang@oracle.com \
--cc=kvm@vger.kernel.org \
--cc=tao.lyu@epfl.ch \
/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).