[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] x86/time: adjust handling of negative delta in stime2tsc()


  • To: Jan Beulich <jbeulich@xxxxxxxx>
  • From: Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Date: Wed, 25 Mar 2026 18:57:19 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vDI5HUHEtewLgS8n/ZdbVrbOmqMLBLVgjnZksf+/KaM=; b=DxN/Vd3DYW3WW3sj1riV28FzA+WkBMA7U0neD7+cz9TtA0g/ZlREmTdV3XDOtqDORysPtrx0xnwRWtjHNCAocpbm0aNCx5+2Q9dyKf76TFbNAEnlO9K79ltOL6ac7ECRVTa8qnkGF/syzGJLopDBsaV4DlFrL1KI7MXXsZU08BiGqllFWyRnqjmFiX764bGb/3km5wcj/2+LsMPJmg5fiAgl2jSa0IJPl9QZ75fVtMbpko4RZBlUd4XUmqlpsBKz8881cvKZIGrvrpAJ8SAdM9rgEWAovHEKwkAdoTRbm/e7A/D7s9z6PjHPgJ4Ywxapk3JktdrEnmoRh3WkvAssqA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=FpA0x0MiyKe+UFkaZE24Px6Y705K0b0BBqnGYrlctsJ7LwcWBXLMTMOa1Wa2g5b+/Gi2ivIfLynBRtTkp1rZHv+8W/SY8ZB9aMgJE3uFIP2ZyJc4YYaluHhNjljX7R0C3nQ0Ks7Kq/JezpEfrIwMWmKIDI2G2IAbwpK/9+rlayVpwgJXAsyi/Mq9vP4E2EDxX7/k/WE9LBUb2PiJgPMy8pWF1bzQfzDlvR2Lf4gWub5zczXUVryYuxzgRz4/qLIZV/tB26Exvr8It4Z/YmB30ZTfXX8NB5RjfO638vLac5cJj1bgm98zF72YS88fakctQLPmTyTxfHJbfvwHCC71sg==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Delivery-date: Wed, 25 Mar 2026 17:57:40 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On Tue, Feb 10, 2026 at 11:04:59AM +0100, Jan Beulich wrote:
> 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 <jbeulich@xxxxxxxx>
> ---
> 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.

Hm, what I've done lately with {un}likely() is not attempting to
optimize for the most taken path, but rather use it to force the
compiler to optimize the fast path, if the function has one.  The slow
path will be slow anyway, and hence any compiler optimization should
be towards making the fast path possibly faster IMO.

This function doesn't seem to have any fast (or slow) paths, so I
would leave it as-is.

> 
> --- 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;
>  }
>  
> -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 = &this_cpu(cpu_time);
> +    s_time_t stime_delta = stime - t->stamp.local_stime;
> +    int64_t delta = 0;

Why do you make delta a signed integer, the value returned by
scale_delta() is unsigned.

>  
> -    t = &this_cpu(cpu_time);
> -    sys_to_tsc = scale_reciprocal(t->tsc_scale);
> +    /*
> +     * While for reprogram_timer() the capping at 0 isn't relevant (the 
> returned

The capping might want mentioning in the function prototype, as maybe
new users expect stime2tsc() to return TSC values from times in the
past.

Otherwise LGTM.

Thanks, Roger.



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.