From: Dan Magenheimer Subject: Bizarre pv kernel ultra-high frequency rdtsc?!? Date: Fri, 20 Nov 2009 15:45:08 -0800 (PST) Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="__125876072745965538abhmt009.oracle.com" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Keir Fraser , Jeremy Fitzhardinge Cc: "Xen-Devel (E-mail)" List-Id: xen-devel@lists.xenproject.org --__125876072745965538abhmt009.oracle.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Jeremy/Keir (and any other PV time experts out there) -- Working on tsc_mode stuff I've run into a roadblock where there is some time-related interaction between xen and a PV kernel that I don't understand. I'm hoping you might provide a clue. There's also a reasonable chance that this might be uncovering a significant bug that's been around awhile, but never noticed as other than a barely noticeable vague slowdown because rdtsc was unemulated (and "fast"). The problem: In order to preserve TSC across save/restore/migrate, I have implemented a "tsc offset" (and also a "tsc scale" but that isn't used yet). The result is that the PV kernel starts doing rdtsc's at a VERY high frequency (1 MILLION / sec). I suspect this may be a variation of what Jeremy reported at one point when emulated rdtsc was first in-tree, but seemed to go away. By adding some debug code (and confirmed with xenctx) I can see that the millions of rdtsc's are half in get_nsec_offset() and half in do_gettimeofday() (presumably inlined from get_usec_offset()). This is a 32-bit 2.6.18-based PV kernel, not upstream. Poring through the 2.6.18 PV time code, I can find several places where an essentially infinite loop might happen if the version fields are wacko, but none where the timestamp contents make any difference in control flow, so don't see how modifying these values (by adding the offset) could cause a behavioral change in Linux, but obviously a big change is happening! I can reproduce the problem with a very simple patch on xen-unstable that adds a fake fixed offset in the three places I add the "tsc offset", see attached. By changing BIG_OFFSET to 0, in this patch, the frequency of rdtsc's becomes normal again. Suspecting some interaction with wallclock time, I tried shutting off ntpd and with/without independent wallclock in the PV guest. No difference. I also added debug code to see if the Xen-side code was churning through version numbers... it is not. Any ideas? (And, sorry, I know you're on a trans- hemisphere trip right now.) Thanks, Dan diff -r bec27eb6f72c xen/arch/x86/time.c --- a/xen/arch/x86/time.c=09Sat Nov 14 10:32:59 2009 +0000 +++ b/xen/arch/x86/time.c=09Fri Nov 20 16:58:18 2009 -0500 @@ -813,6 +813,8 @@ s_time_t get_s_time(void) #define version_update_begin(v) (((v)+1)|1) #define version_update_end(v) ((v)+1) =20 +#define BIG_OFFSET 10000000000ULL + static void __update_vcpu_system_time(struct vcpu *v, int force) { struct cpu_time *t; @@ -827,7 +829,7 @@ static void __update_vcpu_system_time(st =20 /* Don't bother unless timestamps have changed or we are forced. */ if ( !force && (u->tsc_timestamp =3D=3D (v->domain->arch.vtsc - ? t->stime_local_stamp + ? t->stime_local_stamp - BIG_OFFS= ET : t->local_tsc_stamp)) ) return; =20 @@ -835,8 +837,8 @@ static void __update_vcpu_system_time(st =20 if ( v->domain->arch.vtsc ) { - _u.tsc_timestamp =3D t->stime_local_stamp; - _u.system_time =3D t->stime_local_stamp; + _u.tsc_timestamp =3D t->stime_local_stamp - BIG_OFFSET; + _u.system_time =3D t->stime_local_stamp - BIG_OFFSET; _u.tsc_to_system_mul =3D 0x80000000u; _u.tsc_shift =3D 1; } @@ -1598,6 +1600,8 @@ void pv_soft_rdtsc(struct vcpu *v, struc =20 spin_unlock(&v->domain->arch.vtsc_lock); =20 + now -=3D BIG_OFFSET; + regs->eax =3D (uint32_t)now; regs->edx =3D (uint32_t)(now >> 32); } --__125876072745965538abhmt009.oracle.com Content-Type: application/octet-stream; name="tsc-bigoffset.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="tsc-bigoffset.patch" ZGlmZiAtciBiZWMyN2ViNmY3MmMgeGVuL2FyY2gveDg2L3RpbWUuYwotLS0gYS94ZW4vYXJjaC94 ODYvdGltZS5jCVNhdCBOb3YgMTQgMTA6MzI6NTkgMjAwOSArMDAwMAorKysgYi94ZW4vYXJjaC94 ODYvdGltZS5jCUZyaSBOb3YgMjAgMTY6NTg6MTggMjAwOS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CAgICAgPSB0LT5zdGltZV9sb2NhbF9zdGFtcDsKLSAgICAgICAgX3Uuc3lz dGVtX3RpbWUgICAgICAgPSB0LT5zdGltZV9sb2NhbF9zdGFtcDsKKyAgICAgICAgX3UudHNjX3Rp bWVzdGFtcCAgICAgPSB0LT5zdGltZV9sb2NhbF9zdGFtcCAtIEJJR19PRkZTRVQ7CisgICAgICAg IF91LnN5c3RlbV90aW1lICAgICAgID0gdC0+c3RpbWVfbG9jYWxfc3RhbXAgLSBCSUdfT0ZGU0VU OwogICAgICAgICBfdS50c2NfdG9fc3lzdGVtX211bCA9IDB4ODAwMDAwMDB1OwogICAgICAgICBf dS50c2Nfc2hpZnQgICAgICAgICA9IDE7CiAgICAgfQpAQCAtMTU5OCw2ICsxNjAwLDggQEAgdm9p ZCBwdl9zb2Z0X3JkdHNjKHN0cnVjdCB2Y3B1ICp2LCBzdHJ1YwogCiAgICAgc3Bpbl91bmxvY2so JnYtPmRvbWFpbi0+YXJjaC52dHNjX2xvY2spOwogCisgICAgbm93IC09IEJJR19PRkZTRVQ7CisK ICAgICByZWdzLT5lYXggPSAodWludDMyX3Qpbm93OwogICAgIHJlZ3MtPmVkeCA9ICh1aW50MzJf dCkobm93ID4+IDMyKTsKIH0K --__125876072745965538abhmt009.oracle.com Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --__125876072745965538abhmt009.oracle.com--