From: Paolo Bonzini <pbonzini@redhat.com>
To: Haozhong Zhang <haozhong.zhang@intel.com>, kvm@vger.kernel.org
Cc: "Gleb Natapov" <gleb@kernel.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ingo Molnar" <mingo@redhat.com>,
"H. Peter Anvin" <hpa@zytor.com>,
x86@kernel.org, "Joerg Roedel" <joro@8bytes.org>,
"Wanpeng Li" <wanpeng.li@linux.intel.com>,
"Xiao Guangrong" <guangrong.xiao@linux.intel.com>,
"Mihai Donțu" <mdontu@bitdefender.com>,
"Andy Lutomirski" <luto@kernel.org>,
"Kai Huang" <kai.huang@linux.intel.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 03/12] KVM: x86: Add a common TSC scaling function
Date: Mon, 28 Sep 2015 22:12:37 +0200 [thread overview]
Message-ID: <56099F35.1040106@redhat.com> (raw)
In-Reply-To: <1443418691-24050-4-git-send-email-haozhong.zhang@intel.com>
On 28/09/2015 07:38, Haozhong Zhang wrote:
>
> -static u64 __scale_tsc(u64 ratio, u64 tsc)
> -{
> - u64 mult, frac, _tsc;
> -
> - mult = ratio >> 32;
> - frac = ratio & ((1ULL << 32) - 1);
> -
> - _tsc = tsc;
> - _tsc *= mult;
> - _tsc += (tsc >> 32) * frac;
> - _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32;
> -
> - return _tsc;
> -}
This is basically
return mul_u64_u64_shr(ratio, tsc,
kvm_tsc_scaling_ratio_frac_bits);
except that Linux has no mul_u64_u64_shr function, only mul_u64_u32_shr.
We should implement that function in include/linux/math64.h instead.
For the x86_64 case (or any other CONFIG_ARCH_SUPPORTS_INT128
architecture) we can just write it directly, as is done already for
mul_u64_u32_shr.
For the 32-bit case, here is an implementation of both the
multiplication and the shift, lifted from QEMU:
static inline void mul64(uint64_t *lo, uint64_t *hi,
uint64_t a, uint64_t b)
{
typedef union {
uint64_t ll;
struct {
#ifdef __BIG_ENDIAN
uint32_t high, low;
#else
uint32_t low, high;
#endif
} l;
} LL;
LL rl, rm, rn, rh, a0, b0;
uint64_t c;
a0.ll = a;
b0.ll = b;
rl.ll = (uint64_t)a0.l.low * b0.l.low;
rm.ll = (uint64_t)a0.l.low * b0.l.high;
rn.ll = (uint64_t)a0.l.high * b0.l.low;
rh.ll = (uint64_t)a0.l.high * b0.l.high;
c = (uint64_t)rl.l.high + rm.l.low + rn.l.low;
rl.l.high = c;
c >>= 32;
c = c + rm.l.high + rn.l.high + rh.l.low;
rh.l.low = c;
rh.l.high += (uint32_t)(c >> 32);
*lo = rl.ll;
*hi = rh.ll;
}
static inline void rshift128(uint64_t *lo, uint64_t *hi, int n)
{
uint64_t h;
if (!n) {
return;
}
h = *hi >> (n & 63);
if (n >= 64) {
*hi = 0;
*lo = h;
} else {
*lo = (*lo >> n) | (*hi << (64 - n));
*hi = h;
}
}
and you can easily reuse this code in Linux with just uintNN_t types
changed to uNN + some extra cleanups when it's placed in a single functions.
Paolo
next prev parent reply other threads:[~2015-09-28 20:12 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-28 5:37 [PATCH 00/12] KVM: x86: add support for VMX TSC scaling Haozhong Zhang
2015-09-28 5:38 ` [PATCH 01/12] KVM: x86: Collect information for setting TSC scaling ratio Haozhong Zhang
2015-09-29 3:28 ` Eric Northup
2015-09-29 4:01 ` Haozhong Zhang
2015-09-28 5:38 ` [PATCH 02/12] KVM: x86: Add a common TSC scaling ratio field in kvm_vcpu_arch Haozhong Zhang
2015-10-05 19:26 ` Radim Krčmář
2015-10-06 1:54 ` Haozhong Zhang
2015-09-28 5:38 ` [PATCH 03/12] KVM: x86: Add a common TSC scaling function Haozhong Zhang
2015-09-28 20:12 ` Paolo Bonzini [this message]
2015-09-29 1:51 ` Haozhong Zhang
2015-09-28 5:38 ` [PATCH 04/12] KVM: x86: Replace call-back set_tsc_khz() with a common function Haozhong Zhang
[not found] ` <CAG7+5M3z2-OKsy0wxxks2oW9+7WmFpO4JQETTx3KsaW-hWMGqw@mail.gmail.com>
2015-09-29 3:47 ` Haozhong Zhang
2015-10-05 19:53 ` Radim Krčmář
2015-10-05 20:46 ` David Matlack
2015-10-06 4:06 ` Haozhong Zhang
2015-10-06 10:40 ` Paolo Bonzini
2015-10-06 11:32 ` Haozhong Zhang
2015-09-28 5:38 ` [PATCH 05/12] KVM: x86: Replace call-back compute_tsc_offset() " Haozhong Zhang
2015-09-28 5:38 ` [PATCH 06/12] KVM: x86: Move TSC scaling logic out of call-back adjust_tsc_offset() Haozhong Zhang
2015-09-28 20:14 ` Paolo Bonzini
2015-09-29 1:47 ` Haozhong Zhang
2015-09-28 5:38 ` [PATCH 07/12] KVM: x86: Move TSC scaling logic out of call-back read_l1_tsc() Haozhong Zhang
2015-09-28 5:38 ` [PATCH 08/12] KVM: x86: Use the correct vcpu's TSC rate to compute time scale Haozhong Zhang
2015-10-05 20:12 ` Radim Krčmář
2015-09-28 5:38 ` [PATCH 09/12] KVM: VMX: Enable and initialize VMX TSC scaling Haozhong Zhang
2015-09-28 5:38 ` [PATCH 10/12] KVM: VMX: Setup TSC scaling ratio when a vcpu is loaded Haozhong Zhang
2015-09-28 5:38 ` [PATCH 11/12] KVM: VMX: Use a scaled host TSC for guest readings of MSR_IA32_TSC Haozhong Zhang
2015-09-28 5:38 ` [PATCH 12/12] KVM: VMX: Dump TSC multiplier in dump_vmcs() Haozhong Zhang
2015-09-29 4:00 ` [PATCH 00/12] KVM: x86: add support for VMX TSC scaling Eric Northup
2015-09-29 4:03 ` Haozhong 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=56099F35.1040106@redhat.com \
--to=pbonzini@redhat.com \
--cc=gleb@kernel.org \
--cc=guangrong.xiao@linux.intel.com \
--cc=haozhong.zhang@intel.com \
--cc=hpa@zytor.com \
--cc=joro@8bytes.org \
--cc=kai.huang@linux.intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mdontu@bitdefender.com \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=wanpeng.li@linux.intel.com \
--cc=x86@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).