From nobody Thu Apr 2 15:38:01 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=1774622412; cv=none; d=zohomail.com; s=zohoarc; b=G6KmYKHE7i2dKa6U9DUX6J4u7ztgrFE0t/QXHl31kRdtNynkkIlJMtH7Vi0oxANf9AnE8G87l7fmGGjVa2IX7aysK9c4w5VggSdG7/RIfwq7WyfFRFA5hKkuQ0LDu1ayMsP/lyq5TAHt4kWC5UyQnGJTdO6VjphJtFhQFTUBo08= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1774622412; 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=BIapZxRnDOssXgnv5RJRZMWVrnwBRoUenD8zoanxkCE=; b=QqcZ94HXFhS2U6NCHlxUHd8TKAfOhcDrfxwKn+he/46jyoTS+T3aSX+dVEMwLRcwzz9hX4qyym42xp1V86sDfvM2WFWB4necoSiA8lWyJMeR08eSILfmdQEnazFDpQw25a6NVA8G2arVtEIVnSm66pqgpIDgualDuzCTJYZ8DPE= 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 1774622412787702.4672203905677; Fri, 27 Mar 2026 07:40:12 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1265780.1556529 (Exim 4.92) (envelope-from ) id 1w68Li-0004xM-83; Fri, 27 Mar 2026 14:39:46 +0000 Received: by outflank-mailman (output) from mailman id 1265780.1556529; Fri, 27 Mar 2026 14:39:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1w68Li-0004xF-5Z; Fri, 27 Mar 2026 14:39:46 +0000 Received: by outflank-mailman (input) for mailman id 1265780; Fri, 27 Mar 2026 14:39:45 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1w68Lh-0004x7-7U for xen-devel@lists.xenproject.org; Fri, 27 Mar 2026 14:39:45 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1w68Lg-0074aP-J5 for xen-devel@lists.xenproject.org; Fri, 27 Mar 2026 15:39:44 +0100 Received: from [10.42.69.4] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69c696a8-bab6-0a2a0a5309dd-0a2a45048808-22 for ; Fri, 27 Mar 2026 15:39:44 +0100 Received: from [209.85.128.48] (helo=mail-wm1-f48.google.com) by tlsNG-ebf023.mxtls.expurgate.net with ESMTPS (eXpurgate 4.55.2) (envelope-from ) id 69c696b0-c823-0a2a45040019-d1558030c1ed-3 for ; Fri, 27 Mar 2026 15:39:44 +0100 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4838c15e3cbso17927325e9.3 for ; Fri, 27 Mar 2026 07:39:44 -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-48722c9f0afsm174396405e9.10.2026.03.27.07.39.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 27 Mar 2026 07:39:43 -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=1774622384; x=1775227184; 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=BIapZxRnDOssXgnv5RJRZMWVrnwBRoUenD8zoanxkCE=; b=VFX6w1m4CHFk/aRzsH1zQCNrYyUeUys7/TvrdL/W/2xmAYcl/OFCt+X7VDWuDfYz0v cmuzFegYeHJbDOvWqzyhs0x/OJjjLtWQvhF6br76QVc5koBiavcbJISaM6sj48/WXAZC u1QeHI98l9gCkwVVpAYTpYbUjeHykvPcJaIFwGYZifUbexcj5M6h9VTCLhqM1WIIPYqF o+H4E1dN/+KJM4ir/O3sHbfBullzWQ4jr9C2/qEva/YiaY06PsJSUbM9TuatXNUluvYq l8Orrb7kqz+0u4VOF0bVPJBPe4KY41G9tsuVK5ORVu6w7wL5QYuTS3/Dd52ze65/OV9W r1pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774622384; x=1775227184; 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=BIapZxRnDOssXgnv5RJRZMWVrnwBRoUenD8zoanxkCE=; b=bHEX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:3ec6:e634 with SMTP id 5b1f17b1804b1-48727d84100mr43438075e9.15.1774622383708; Fri, 27 Mar 2026 07:39:43 -0700 (PDT) Message-ID: Date: Fri, 27 Mar 2026 15:39:42 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Jan Beulich Subject: [PATCH v2] x86/time: adjust handling of negative delta in stime2tsc() To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= 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-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ebf023/1774622384-B908B9D1-49E60D5D/0/0 X-purgate-type: clean X-purgate-size: 2558 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1774622414217154100 Content-Type: text/plain; charset="utf-8" When we cap negative values to 0 (see code comment as to why), going through scale_delta() is pointless - it'll return 0 anyway. Therefore make the call conditional (and then also the one to scale_reciprocal()), adding a comment as to why there is this capping. Modernize types used while there, and switch to usiong initializers for the local variables. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monn=C3=A9 --- Adding likely() to the conditional here does make a difference. Question is whether to do so, seeing that there looks to be a possibility (of unknown frequency) for the delta to be non-positive. --- v2: Make delta unsigned. Add comment to function decl. Make the function decl also use uint64_t. --- a/xen/arch/x86/include/asm/time.h +++ b/xen/arch/x86/include/asm/time.h @@ -50,7 +50,12 @@ void force_update_vcpu_system_time(struc =20 bool clocksource_is_tsc(void); int host_tsc_is_safe(void); -u64 stime2tsc(s_time_t stime); + +/* + * Note: This function "caps" times ahead of the local CPU's stime stamp, + * supplying the corresponding TSC stamp in that case. + */ +uint64_t stime2tsc(s_time_t stime); =20 struct time_scale; void set_time_scale(struct time_scale *ts, u64 ticks_per_sec); --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -1176,20 +1176,26 @@ uint64_t __init calibrate_apic_timer(voi return elapsed * CALIBRATE_FRAC; } =20 -u64 stime2tsc(s_time_t stime) +uint64_t stime2tsc(s_time_t stime) { - struct cpu_time *t; - struct time_scale sys_to_tsc; - s_time_t stime_delta; + const struct cpu_time *t =3D &this_cpu(cpu_time); + s_time_t stime_delta =3D stime - t->stamp.local_stime; + uint64_t delta =3D 0; =20 - t =3D &this_cpu(cpu_time); - sys_to_tsc =3D scale_reciprocal(t->tsc_scale); + /* + * While for reprogram_timer() the capping at 0 isn't relevant (the re= turned + * value is likely in the past anyway then, by the time it is used), f= or + * cstate_restore_tsc() this is relevant: We need to avoid moving the = TSC + * backwards (relative to when it may last have been read). + */ + if ( stime_delta > 0 ) + { + struct time_scale sys_to_tsc =3D scale_reciprocal(t->tsc_scale); =20 - stime_delta =3D stime - t->stamp.local_stime; - if ( stime_delta < 0 ) - stime_delta =3D 0; + delta =3D scale_delta(stime_delta, &sys_to_tsc); + } =20 - return t->stamp.local_tsc + scale_delta(stime_delta, &sys_to_tsc); + return t->stamp.local_tsc + delta; } =20 void cstate_restore_tsc(void)