Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1246891rwd; Thu, 1 Jun 2023 12:34:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4q1jMObBY5WrygrPI0P13f47Fcx9/4s1gkNwTVUAbl5gtcbWPHtysbDu2DL7AU+JUfktkf X-Received: by 2002:a17:903:428d:b0:1ad:e633:ee96 with SMTP id ju13-20020a170903428d00b001ade633ee96mr222513plb.55.1685648089652; Thu, 01 Jun 2023 12:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685648089; cv=none; d=google.com; s=arc-20160816; b=U0VxM3dnBlP/73zqN9jatEgKqqnGs1ZxNTRxnwIc1RwAMyLvA8lCE9+dEDHx+gwvlb AFfWi/777px+obIS54ADTlnlD61QIDeJ/xeGVP6LnxJ0KOhPT/PxuVN8MQYA4sGzYLW8 95gza+ihB5gyuC8J3mhT6fsX3CpchpjWCIRp5+m9U+bU0HGO44SfZwRK1XDZSwgjGZtM //cskDaOYPotKPVbgRqMmy+EzXM45Bsbab17fQ6N73oBOIzsucbxkEzslOisp44qSkZ/ jbL5JYE3cn/H8CjC8vFLY1JyyeTWik3jcRsRWgWjbOB7ZPyMphbwcDz+a7aSAe+bkPbq 4nrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:feedback-id:references:in-reply-to :message-id:subject:cc:from:to:dkim-signature:date; bh=lovUSrk93ENXOhw8lq+zt1tvyB45dc+7JHQ1bHcHwCY=; b=QBG/XMJMpyPnz9O9y6oDjUuSGt82NP5R+fiiYrrpgklULM0gWHsJpg39zMCqP/itYs /rB12VXm53E+ZeCCxplyz4TLaDlEPsHIkVN8YNrRIK7z8ZMN2Ap15FzHeV6cPwAdthYx UbQeOvQj8RRYv8uqxTuZOil5qwNSVJLAyp3xQGIMEhQ+hraGb1F/s8mOLufFAI54vQf7 q2E4KO376eClZ+6jaMM/YiMFO62dfrBDuwoFqsOKVr4/jRwiUtLbtXLjHx3V/FB9AmYe 0qFDVu5NTgYF4rAOnBf7BJaESylOzoS92sRkDJKzL7YivCFfJfw9X/PEb91T1eLaYoKO yi8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@uplinklabs.net header.s=protonmail header.b=eqBIFUXy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=uplinklabs.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jb18-20020a170903259200b001b02ce8470dsi3013331plb.543.2023.06.01.12.34.34; Thu, 01 Jun 2023 12:34:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@uplinklabs.net header.s=protonmail header.b=eqBIFUXy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=uplinklabs.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231279AbjFATIN (ORCPT + 99 others); Thu, 1 Jun 2023 15:08:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229693AbjFATIK (ORCPT ); Thu, 1 Jun 2023 15:08:10 -0400 Received: from mail-4317.proton.ch (mail-4317.proton.ch [185.70.43.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F049F184; Thu, 1 Jun 2023 12:08:03 -0700 (PDT) Date: Thu, 01 Jun 2023 19:07:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uplinklabs.net; s=protonmail; t=1685646480; x=1685905680; bh=lovUSrk93ENXOhw8lq+zt1tvyB45dc+7JHQ1bHcHwCY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=eqBIFUXyFnfkuDCUZzZLc82UrgimdrumUq0evWnuDTVgSEMc6wj7aYVVtxBs9tv4l tOETU39au2AkYJEx+aGHwlPBJCMvh8Rx/Gcpw2BnPQz/QFGEOo/Fw2rjmoO81ADhC7 /1ceHpAHBW6pteof5+L/07v30IfL72OyGTOHgiroFlFTL3Uky9nUQwcJSQ4alQhYie 0pNFPSrhzX8jW1sE7gPMNBiS+ZscISyEuEE0ZAdglV5Z/BnmVvVTOeWBOvOLz9+oXk sCtt0lkY7PQglZZhgVu+S6tBjP68oIGt8P/x4YtI/8QvQj0mZp4qIcspNl3d30jMkK RjkZV+mVA7vJw== To: Thomas Gleixner From: Steven Noonan Cc: Muhammad Usama Anjum , Jonathan Corbet , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" , "open list:DOCUMENTATION" , open list , "Guilherme G. Piccoli" , kernel@collabora.com Subject: Re: Direct rdtsc call side-effect Message-ID: In-Reply-To: <87h6rrdoy0.ffs@tglx> References: <6719fb05-382c-8ec4-ccda-72798906a54b@collabora.com> <87mt1jeax1.ffs@tglx> <87h6rrdoy0.ffs@tglx> Feedback-ID: 10620438:user:proton MIME-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha512; boundary="------796aa968aad2e57f8079f70824c4c45caea1f91b291b6bed56b8f34038fce5eb"; charset=utf-8 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------796aa968aad2e57f8079f70824c4c45caea1f91b291b6bed56b8f34038fce5eb Content-Type: multipart/mixed;boundary=---------------------17872929c4a151b04539e62bfec4da60 -----------------------17872929c4a151b04539e62bfec4da60 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain;charset=utf-8 On Thursday, June 1st, 2023 at 11:20 AM, Thomas Gleixner wrote: > Here is an example where it falls flat on its nose. > = > One of the early Ryzen laptops had a broken BIOS which came up with > unsynchronized TSCs. I tried to fix that up, but couldn't get it to sync > on all CPUs because for some stupid reason the TSC write got > arbritrarily delayed (assumably by SMI/SMM). Hah, I remember that. That was actually my laptop. A Lenovo ThinkPad A485 = with a Ryzen 2700U. I've seen the problem since then occasionally on newer= Ryzen laptops (and even desktops). Without the awful "tsc=3Ddirectsync" p= atch I wrote, which I've been carrying for years now in my own kernel buil= ds, it just falls back to HPET. It's not pleasant, but at least it's a sta= ble clock. > After the vendor fixed the BIOS, I tried again and the problem > persisted. > = > So on such a machine the 'fixup time' mechanism would simply render an > otherwise perfectly fine TSC unusable for timekeeping. > = > We asked both Intel and AMD to add TSC_ADJUST probably 15 years > ago. Intel added it with some HSW variants (IIRC) and since SKL all CPUs > have it. I don't know why AMD thought it's not required. That could have > spared a gazillion of bugzilla entries vs. the early Ryzen machines. > Agreed, TSC_ADJUST is the ultimate solution for any of these kinds of issu= es. But last I heard from AMD, it's still several years out in silicon, an= d there's plenty of hardware to maintain compatibility with. Ugh. A software solution would be preferable in the meantime, but I don't know = what options are left at this point. The trap-and-emulate via SIGSEGV approach proposed earlier in the thread i= s unfortunately not likely to be practical, assuming I implemented it prop= erly. One issue is how much overhead it has. This is an instruction that normall= y executes in roughly 50 clock cycles (RDTSC) to 100 clock cycles (RDTSCP)= on Zen 3. Based on a proof-of-concept I wrote, the overhead of trapping a= nd emulating with a signal handler is roughly 100x. On my Zen 3 system, it= goes up to around 10000 clock cycles per trapped read of RDTSCP. Most Win= dows games that use this instruction directly are doing so under the assum= ption that the TSC is faster to read than any of the native Windows API cl= ock sources. If it's suddenly ~100x slower than even the slowest-to-read W= indows clocksource, those games would likely become entirely unplayable, d= epending on how frequently they do TSC reads. (And many do so quite often!= ) Also, my proof-of-concept doesn't actually do the emulation part. It just = traps the instruction and then executes that same instruction in the signa= l handler, putting the results in the right registers. So it's a pass-thro= ugh approach, which is about the best you can do performance wise. Another issue is that the implementation might be tricky. In the case of W= ine, you'd need to enable PR_TSC_SIGSEGV whenever entering the Windows exe= cutable and PR_TSC_ENABLE whenever leaving it. If you don't, any of the no= rmally well-behaved clock sources implemented using the TSC (e.g. CLOCK_MO= NOTONIC_RAW, etc) would also fault on the Wine side. Also, there's some Wi= ndows-specific trickery, in that the Windows registry exposes the TSC freq= uency in a couple of places, so those would need to be replaced with the f= requency of the emulated clocksource. - Steven -----------------------17872929c4a151b04539e62bfec4da60-- --------796aa968aad2e57f8079f70824c4c45caea1f91b291b6bed56b8f34038fce5eb Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: ProtonMail wnUEARYKACcFgmR47GEJkAi2TYeeRSZQFiEE707zOy6TKdatSeTPCLZNh55F JlAAAJDdAQD0c+SXDqA1PASyKgtok2FQ+jAcie8g0u2Rd/Grlp49QwEAzASw DKrwVDCAWWVHMgksqgsdcchcU5d4UJu9AW2nIAk= =lfCX -----END PGP SIGNATURE----- --------796aa968aad2e57f8079f70824c4c45caea1f91b291b6bed56b8f34038fce5eb--