From nobody Sat May 30 11:15:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1778654714; cv=none; d=zohomail.com; s=zohoarc; b=cJ6Xfkvn6OAAUOWJYI3EzukHoVQqLrnR6CvdzEUB8vCLVeD+glUREpiFYSUcvp0FP3XejOXpcDULILoZ6P6kgxAgPUZnmZOT6FfbyDlVA582dW7AmCxtrogUABH2yhaD45jZgCbpFBdeNaibh4fv6WZF8LIUnNNhuyd0QDoGxQo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1778654714; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ZUrH8i/YVTA1yhjN7VVQDsPDHU1txiZ6leU/AQLk+WA=; b=XyZUU4woz3/U7P8CLFM26TPS3NeRKzXG8/ky+YEnkgaIlaqpyVdePD/xw+ImKpoSYgRidOKR7fMGIyK6cjpJmpfo4E8Lbh5si+0GbKbC9HlGOPd7kMPuSefUkEvKol/FIIEEFUzvcmtp/lj1wzUQ4QHCck50ajUBhJ7DgEzeMsQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1778654714585352.1722325147298; Tue, 12 May 2026 23:45:14 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1307616.1579214 (Exim 4.92) (envelope-from ) id 1wN3Ks-0004ha-9o; Wed, 13 May 2026 06:44:50 +0000 Received: by outflank-mailman (output) from mailman id 1307616.1579214; Wed, 13 May 2026 06:44:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wN3Ks-0004hT-6z; Wed, 13 May 2026 06:44:50 +0000 Received: by outflank-mailman (input) for mailman id 1307616; Wed, 13 May 2026 06:44:49 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wN3Kr-0004hL-Dx for xen-devel@lists.xenproject.org; Wed, 13 May 2026 06:44:49 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wN3Kq-001RW2-CS for xen-devel@lists.xenproject.org; Wed, 13 May 2026 08:44:48 +0200 Received: from [10.42.69.12] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a041dd4-2eae-0a2a0a5409dd-0a2a450c9570-48 for ; Wed, 13 May 2026 08:44:48 +0200 Received: from [209.85.128.52] (helo=mail-wm1-f52.google.com) by tlsNG-d25034.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 6a041de0-62f1-0a2a450c0019-d1558034e181-3 for ; Wed, 13 May 2026 08:44:48 +0200 Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-488a8ca4aadso57796565e9.3 for ; Tue, 12 May 2026 23:44:48 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48fcdf6408bsm29081335e9.3.2026.05.12.23.44.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 May 2026 23:44:47 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Autocrypt:Content-Language:Cc:To:Subject:From:User-Agent:MIME-Version:Date:Message-ID" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1778654688; x=1779259488; darn=lists.xenproject.org; h=content-transfer-encoding:autocrypt:content-language:cc:to:subject :from:user-agent:mime-version:date:message-id:from:to:cc:subject :date:message-id:reply-to; bh=ZUrH8i/YVTA1yhjN7VVQDsPDHU1txiZ6leU/AQLk+WA=; b=auMjt5Z3H3LYAK3irQq1Fxjq6qFNb0ef5GtxP8nDjvOC90Ddx5gyviCAxq7Y/XB0c/ oacaH57TB02ML2lv/ZP0Ta17aTqC2oVRUXBqlkHJvAhyRxrARjLropbPEJnnnLzTe8+1 9hhZK5sCwXk5MRFv3reVupJpKVuQs20pBnCUeDhSJbxa1WJZv/XzjYCjQfrfvour54CL O0VLntkpJr29SojPri5rAF6O+YAXvCY0qhKw5lpg3/+4d+J2yr0dNHp5Nix2XxVUf/Nv HkFhF5AMCveBYsP73g2Pr8zicBrjS+UJ1ewd9dfD/EooD2FVhB5zXm5Zs6Rl4Gz5Jm4l LTiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778654688; x=1779259488; h=content-transfer-encoding:autocrypt:content-language:cc:to:subject :from:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZUrH8i/YVTA1yhjN7VVQDsPDHU1txiZ6leU/AQLk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c with SMTP id 5b1f17b1804b1-48fce9d6283mr15741225e9.12.1778654687560; Tue, 12 May 2026 23:44:47 -0700 (PDT) Message-ID: <68b1607a-f2a1-4f53-84c5-43c61eeb1869@suse.com> Date: Wed, 13 May 2026 08:44:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Jan Beulich Subject: [PATCH v2 RFC] x86/time: avoid early uses of NOW() to return zero To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Teddy Astie Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-d25034/1778654688-DA368CF5-9A085F9D/0/0 X-purgate-type: clean X-purgate-size: 5826 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1778654715767158500 Waiting loops like the one in flush_command_buffer() will degenerate to infinite ones when used early enough for NOW() to still return constant zero. Make sure the returned value at least monotonically increases. When available, use nominal frequency values as initial approximation. Do this only in get_s_time(), as producing a sane value in get_s_time_fixed() for non-zero inputs won't be reasonably possible. Put an assertion there. Reported-by: Roger Pau Monn=C3=A9 Signed-off-by: Jan Beulich --- RFC: This breaks at least the TSM_BOOT case printk_start_of_line(), which checks for NOW() returning 0 (falling back to TSM_RAW in this case). For now I have no idea how to avoid this; perhaps that's tolerable at least in the case where we put in place an early estimate? Should we maybe weaken the fallback condition to take effect for any value below 1=CE=BCs? RFC: While generally the mentioned waiting loops will take longer to time out, on a very fast CPU tight loops may time out too early. RFC: For the AMD/Hygon case, if the "nominal" value isn't available, we could use the "high" one. That would cause NOW() to run too slowly (until the scale is properly set), but maybe that's still better than it returning 0? (As it stands, I can't really test the new code there, as my Rome system only supplies the lo/hi pair of values.) RFC: On the 2nd pass through early_cpu_init() it may be okay to skip the new additions. With "x86/time: set AP's TSC scale estimate earlier" the counter update may not need to be atomic anymore, as then only the BSP can reasonably hit that path. I don't think Fixes: tags should be put here. If we did, we'd have to enumerate all introductions of early uses of NOW() (or get_s_time()), with the exception of those dealing with getting back 0 (which I expect is only printk_start_of_line()). Will want backporting nevertheless (unless deemed too risky). --- v2: Add assertion to get_s_time_fixed(). Use nominal frequencies for very early setting, if available. --- unstable.orig/xen/arch/x86/cpu/common.c 2026-05-13 08:35:28.640503356 += 0200 +++ unstable/xen/arch/x86/cpu/common.c 2026-05-12 12:30:35.475284195 +0200 @@ -19,6 +19,7 @@ #include #include #include +#include #include =20 #include @@ -403,6 +404,25 @@ void __init early_cpu_init(bool verbose) &c->x86_capability[FEATURESET_7d1]); } =20 + if (c->cpuid_level >=3D 0x15) { + cpuid(0x15, &eax, &ebx, &ecx, &edx); + + if (ecx && ebx && eax) + preset_tsc_scale(DIV_ROUND_UP(ecx * 1UL * ebx, eax)); + else if (c->cpuid_level >=3D 0x16) { + /* Assume CPU base freq =E2=89=88 TSC freq. */ + cpuid(0x16, &eax, &ebx, &ecx, &edx); + if (eax) + preset_tsc_scale(eax * 1000000UL); + } + } else if (c->vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) { + unsigned int nom_mhz =3D 0; + + amd_process_freq(c, NULL, &nom_mhz, NULL); + if (nom_mhz) + preset_tsc_scale(nom_mhz * 1000000UL); + } + eax =3D cpuid_eax(0x80000000); if ((eax >> 16) =3D=3D 0x8000 && eax >=3D 0x80000008) { ebx =3D eax >=3D 0x8000001f ? cpuid_ebx(0x8000001f) : 0; --- unstable.orig/xen/arch/x86/include/asm/time.h 2026-05-13 08:35:28.64050= 3356 +0200 +++ unstable/xen/arch/x86/include/asm/time.h 2026-05-12 12:25:14.435489339 = +0200 @@ -23,6 +23,7 @@ mktime (unsigned int year, unsigned int int time_suspend(void); int time_resume(void); =20 +void preset_tsc_scale(unsigned long freq); void init_percpu_time(void); void time_latch_stamps(void); =20 --- unstable.orig/xen/arch/x86/time.c 2026-05-13 08:35:28.640503356 +0200 +++ unstable/xen/arch/x86/time.c 2026-05-13 08:33:54.000000000 +0200 @@ -1655,6 +1655,9 @@ s_time_t get_s_time_fixed(u64 at_tsc) const struct cpu_time *t =3D &this_cpu(cpu_time); u64 tsc, delta; =20 + /* scale_delta() degenerates when the scale wasn't set yet. */ + ASSERT(t->tsc_scale.mul_frac); + if ( at_tsc ) tsc =3D at_tsc; else @@ -1670,6 +1673,20 @@ s_time_t get_s_time_fixed(u64 at_tsc) =20 s_time_t get_s_time(void) { + /* + * Before the TSC scale is set, avoid returning constant 0 (or whatever + * this_cpu(cpu_time).stamp.local_stime is set to). While the returned + * value is in no way representing time, it at least increases + * monotonically, thus avoiding e.g. waiting loops to degenerate to + * entirely infinite ones. + */ + if ( unlikely(!this_cpu(cpu_time).tsc_scale.mul_frac) ) + { + static s_time_t counter; + + return arch_fetch_and_add(&counter, 1); + } + return get_s_time_fixed(0); } =20 @@ -2623,6 +2640,21 @@ int __init init_xen_time(void) return 0; } =20 +/* BSP-only function to pre-set an approximate TSC scale. */ +void __init preset_tsc_scale(unsigned long freq) +{ + struct cpu_time *t =3D &this_cpu(cpu_time); + + /* + * The incoming frequency is only approximate (nominal). Increase it = by + * 1% to make NOW() output rather a little too slow than too fast, thus + * avoiding a possible backwards jump once the final scale is set. + */ + freq +=3D DIV_ROUND_UP(freq, 100); + + set_time_scale(&t->tsc_scale, freq); + t->stamp.local_tsc =3D boot_tsc_stamp; +} =20 /* Early init function. */ void __init early_time_init(void) @@ -2640,6 +2672,9 @@ void __init early_time_init(void) "TSC ADJUST set to %lx on boot CPU - clearing\n", tmp); wrmsrl(MSR_IA32_TSC_ADJUST, 0); boot_tsc_stamp -=3D tmp; + + if ( t->stamp.local_tsc ) + t->stamp.local_tsc -=3D tmp; } } =20