From nobody Thu Mar 28 17:14:47 2024 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; arc=fail (BodyHash is different from the expected one); dmarc=pass(p=quarantine dis=none) header.from=suse.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1641977746075343.7262110298501; Wed, 12 Jan 2022 00:55:46 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.256391.439930 (Exim 4.92) (envelope-from ) id 1n7ZPh-0007KL-KJ; Wed, 12 Jan 2022 08:55:25 +0000 Received: by outflank-mailman (output) from mailman id 256391.439930; Wed, 12 Jan 2022 08:55:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7ZPh-0007KE-HI; Wed, 12 Jan 2022 08:55:25 +0000 Received: by outflank-mailman (input) for mailman id 256391; Wed, 12 Jan 2022 08:55:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7ZPg-0007Jn-6e for xen-devel@lists.xenproject.org; Wed, 12 Jan 2022 08:55:24 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 5ff1c5e2-7385-11ec-9ce5-af14b9085ebd; Wed, 12 Jan 2022 09:55:22 +0100 (CET) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04lp2052.outbound.protection.outlook.com [104.47.12.52]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-32--BBvSMtZOzSgAIix_KULFA-1; Wed, 12 Jan 2022 09:55:20 +0100 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) by VI1PR04MB7024.eurprd04.prod.outlook.com (2603:10a6:800:124::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.7; Wed, 12 Jan 2022 08:55:19 +0000 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::5951:a489:1cf0:19fe]) by VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::5951:a489:1cf0:19fe%6]) with mapi id 15.20.4867.012; Wed, 12 Jan 2022 08:55:19 +0000 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" X-Inumbo-ID: 5ff1c5e2-7385-11ec-9ce5-af14b9085ebd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1641977721; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C2Vuior8PYuOVJP0shUnoF3HfrsoCqa7CjfiATJmeS8=; b=Duk2CCeN7omw8ZHPl53KkyRZp0A8t9Haah1NxCC42lBpY+Ep3tG3YqtW3gg5IaJL7bp+8I 2U4pFIRPgAwlw/T7QWgzsgZsgmeGg06CIWgGYi/PsKVnUH2NZJLMCu0UFf5bJLsYa+8ijU oVzUMKdR5JUxQLa4C2SODILKhWpoa4o= X-MC-Unique: -BBvSMtZOzSgAIix_KULFA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lMsmSVviw7eC0ArQ1pfS5UaY7FtzzUCD3PM4HCIojZ8oc85dllixiF1WKvRsHIxeKJK9ghZPIOMxMAxA1i3pmAO6+aNIljhqzLgxI5hzyMbGp5jaMfZpZ4Ln8fn8qM+2NzhYgbe4fQvO0roxWtJaqqZepWNeeF56LCBLe9U2GAv7cO18lKEE8K5HIfAdVmIPKV5ve1A7YDKuXOK7Z5WHM72W11BjAuIgmg0RrQOyycgZQDYvZsdJcDyPjJECs52T4CUdwSpyTjOGPIX2FFf4nu9TWOWW6WHHMUqkmNQov2HaKaFNbb61yWGIDxLEY3nf0ROyBNhdQ4PvqMUWyS+fqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=wxh9IDBUC18qTBjUbBaFY6EpgeSBeQZi1rjPikAJ6H0=; b=NVi5BzrFbF0CenDYt7fvIi7aYfWkMLe52X826PC0kMYcRxEA0ZQ1upDUzRhJkcu8n8XFp586xmF2y9UGMQ2X0iauvQipwIotlf8BmkgKa5Mpi5i9sq6dxjvYItqBXB4D5m59bdaND8WYerbktO/txhqt+Bt4LcrX8T0oq6cycqsCuGE9N6fX4hapzRb+q+8uQIFm/W3NIx10UiK7NItSmHtBeYZ5MbTZegICC/16o3HU3nb7I6iztIznjbk4xC1WZw7/Jb1ThWYyJgNHOjL/LtWYzXa6vXhGBPxMouv12OP8FmBSrRX3cWHgZ/X/gQyECJXxWF/O2ONXnheMcMIqeA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <34662095-6f58-4471-8bbf-1bdf67650fb2@suse.com> Date: Wed, 12 Jan 2022 09:55:17 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Subject: [PATCH 1/2] x86/time: use relative counts in calibration loops Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= References: <595b3447-0eba-dfc8-fa24-6bb8b964a137@suse.com> In-Reply-To: <595b3447-0eba-dfc8-fa24-6bb8b964a137@suse.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: AM6PR02CA0012.eurprd02.prod.outlook.com (2603:10a6:20b:6e::25) To VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 64af267b-de62-4aa0-ccca-08d9d5a942b5 X-MS-TrafficTypeDiagnostic: VI1PR04MB7024:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4KWMo+4j7GCf3VEvcLfQt+0vpqeAIhte+Q3aThnj60TzMW9wiyjU1HYZHPRnVfGASObEpZ69c3rRekvJf7H7YNQ9TSIsoJ0L75HtvvcfhabOLUuP2pWALwEHjrS7GTiMBdSkr1Uut5ml6mqY+3njCcp2J3tlfbIzviYYsI+lj+ea5mPiBxvElF6uN9yeHSnBmlNABLbVoAHRfMeK+0yBo1CK8lXxx3a2VsSsNUjqwIAPbS6+L3lG/dEYnvvFmoAEplf5AzgP8zvBT1HlALOs8b4A+0codCljns4MUDJ2RBiS/YgLQN05/d3v/oOGj1yAsYM36AhfvPx872eSPri+7qHKS6/ys97bMoSojK6K9MUwXPrmGN6Pyg/D7EvF2aQyTCSG5VuzURcPa5G45q2BX7h1cAJMvqvjFvxkV6Zjtyes3k+jJ9bkShQwO/3o7f1ZHszoVjj80IhnuOrRUxOD7eF3xiJ4s1hge+4EM0imzDjptU3ZRVRJaZvorfl6OVSpdoT+F+FAP22jhquU5jwHR36d9CyIewSF2ppQs4kP6ZDeWkQkb3HlHkavQYxi+1qqsO92X1VjBLlGnurb2JkaMV/BWAuOGFWdw3F4Zhji5O+2dtPKpRNddk59ncB67z2imgc/i3GHYi/KwUR2Oi48iIZ5120AKmwuPdJjHpzH2x0PBunuoFGTdNRDANnX3i1xSD8PPfkkJqJAyh4xAVsWL0BTQpEls9676kzlerKsyPy1a66bNjhL2rQnkJF0yV83 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR04MB5600.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(2906002)(2616005)(31696002)(6916009)(8936002)(36756003)(54906003)(186003)(26005)(6506007)(31686004)(86362001)(38100700002)(4326008)(5660300002)(8676002)(6486002)(6512007)(508600001)(66946007)(66476007)(83380400001)(316002)(66556008)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?crhOxdcn/P4SEMiZ257hkHGLLke1hTDn8EuoXVRURyPzqTVMS4028ErFqUKW?= =?us-ascii?Q?GqLkJpg7/m8wHdRYJ7nhS/EVWHaIpmwQwQ7Gupw+t+gFY5htOxyyj7hcL0Dc?= =?us-ascii?Q?2raGwwyAi2UVKC+cg+4ggTqzBuPNqS8iM04FgAhtztIJXDMP860XTh4cE2Oo?= =?us-ascii?Q?3Tc2xCtx/4IcsOSs2xxr1MUYV4ZEjgu33oT+qerhE6rHrZFtv4+seiIK2r0v?= =?us-ascii?Q?rODihZuA7oE9f+kzK31Sa2rluWI9FzJhCJ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GmZ8hxQZmxkcAv1fyiWGVeBIyshiSDyOBkaElo9jbINGd1MhhC?= =?us-ascii?Q?lSzZ/3FSA/HWVwAtEImUlRR+9jdn5WdBsOP6K+ce6pwHQFPfYg9tCODvjNrQ?= =?us-ascii?Q?wm5tz2KDH4mg+gjcBmcH5X4eah6p6Ww5wEsQ5DFdpl3L8eYDtpt6wwpl6c0z?= =?us-ascii?Q?/XsFjgoak8LPGBszsnAwRA4mRBaC5aArLM5zo2LegZexu1geH7AHq2Fc9HMt?= =?us-ascii?Q?y/A4qZcwVwFG2DToNwBCZsfrWIRGoRqVdpgAQCy1GkZrPPoV96OsGMAQKWns?= =?us-ascii?Q?+nagjpJq5jO9TozTo2p90OKIOKAubLl8i4FPRdGadUaGYz3XPp0d2PG9gWP3?= =?us-ascii?Q?ua7J/Pb93zMXYIQM++V2Cwmv5ispaMpcVClAnZHiLjmBvASCeM75OVBrwMeC?= =?us-ascii?Q?0OE+jOBfzAR+kDCrlxmUuUF04dnEvUhWjEG3cCzK8/L6s6EvvuVEG8yFEoIA?= =?us-ascii?Q?h/4ObserZhlmFz5N9bQ0UJo=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 64af267b-de62-4aa0-ccca-08d9d5a942b5 X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5600.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2022 08:55:19.6355 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RbFkoUFW46h17xWlUjCTub7vZR+KdZZ1nNx1jou9NnlkyTeN+WutT9xeWP3PwXq9v4uV6oVaIGg8GrfL8xKigQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB7024 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1641977747820100001 Content-Type: text/plain; charset="utf-8" Looping until reaching/exceeding a certain value is error prone: If the target value is close enough to the wrapping point, the loop may not terminate at all. Switch to using delta values, which then allows to fold the two loops each into just one. Fixes: 93340297802b ("x86/time: calibrate TSC against platform timer") Reported-by: Roger Pau Monn=C3=A9 Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monn=C3=A9 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -378,7 +378,7 @@ static u64 read_hpet_count(void) static int64_t __init init_hpet(struct platform_timesource *pts) { uint64_t hpet_rate, start; - uint32_t count, target; + uint32_t count, target, elapsed; /* * Allow HPET to be setup, but report a frequency of 0 so it's not sel= ected * as a timer source. This is required so it can be used in legacy @@ -451,11 +451,8 @@ static int64_t __init init_hpet(struct p =20 count =3D hpet_read32(HPET_COUNTER); start =3D rdtsc_ordered(); - target =3D count + CALIBRATE_VALUE(hpet_rate); - if ( target < count ) - while ( hpet_read32(HPET_COUNTER) >=3D count ) - continue; - while ( hpet_read32(HPET_COUNTER) < target ) + target =3D CALIBRATE_VALUE(hpet_rate); + while ( (elapsed =3D hpet_read32(HPET_COUNTER) - count) < target ) continue; =20 return (rdtsc_ordered() - start) * CALIBRATE_FRAC; @@ -493,8 +490,8 @@ static u64 read_pmtimer_count(void) =20 static s64 __init init_pmtimer(struct platform_timesource *pts) { - u64 start; - u32 count, target, mask; + uint64_t start; + uint32_t count, target, mask, elapsed; =20 if ( !pmtmr_ioport || (pmtmr_width !=3D 24 && pmtmr_width !=3D 32) ) return 0; @@ -504,11 +501,8 @@ static s64 __init init_pmtimer(struct pl =20 count =3D inl(pmtmr_ioport) & mask; start =3D rdtsc_ordered(); - target =3D count + CALIBRATE_VALUE(ACPI_PM_FREQUENCY); - if ( target < count ) - while ( (inl(pmtmr_ioport) & mask) >=3D count ) - continue; - while ( (inl(pmtmr_ioport) & mask) < target ) + target =3D CALIBRATE_VALUE(ACPI_PM_FREQUENCY); + while ( (elapsed =3D (inl(pmtmr_ioport) & mask) - count) < target ) continue; =20 return (rdtsc_ordered() - start) * CALIBRATE_FRAC; From nobody Thu Mar 28 17:14:47 2024 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; arc=fail (BodyHash is different from the expected one); dmarc=pass(p=quarantine dis=none) header.from=suse.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 164197779931196.59511394166202; Wed, 12 Jan 2022 00:56:39 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.256396.439941 (Exim 4.92) (envelope-from ) id 1n7ZQc-0007wJ-38; Wed, 12 Jan 2022 08:56:22 +0000 Received: by outflank-mailman (output) from mailman id 256396.439941; Wed, 12 Jan 2022 08:56:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7ZQb-0007wC-W8; Wed, 12 Jan 2022 08:56:21 +0000 Received: by outflank-mailman (input) for mailman id 256396; Wed, 12 Jan 2022 08:56:20 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n7ZQa-0007w0-Nk for xen-devel@lists.xenproject.org; Wed, 12 Jan 2022 08:56:20 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 819810c0-7385-11ec-81c1-a30af7de8005; Wed, 12 Jan 2022 09:56:18 +0100 (CET) Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03lp2059.outbound.protection.outlook.com [104.47.9.59]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-14-p82mavivOleOJj7mYFiXnQ-1; Wed, 12 Jan 2022 09:56:17 +0100 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) by VI1PR04MB5167.eurprd04.prod.outlook.com (2603:10a6:803:5b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.11; Wed, 12 Jan 2022 08:56:14 +0000 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::5951:a489:1cf0:19fe]) by VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::5951:a489:1cf0:19fe%6]) with mapi id 15.20.4867.012; Wed, 12 Jan 2022 08:56:14 +0000 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" X-Inumbo-ID: 819810c0-7385-11ec-81c1-a30af7de8005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1641977778; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gaTdc3QeNtdfPvaqCygV/XiXFfQlC7iAK7RBUOO8E4A=; b=WQzO68eqf45OKRKFfzI5xLTKx2foZ/9c6d4i/pZzAR2q4GkM9T3LaSMNSm1tpqzIr+uLYk RVf1rZdMQFVlAbZ2qmg8r6w83QQbXccl04jxLULkRhELcWDTepuqLZICKfm1kk78qIiOQI Sj6s9JapgB/3+KxtGoAw4nMy0CuZmR0= X-MC-Unique: p82mavivOleOJj7mYFiXnQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dSu5wwu85WB6OGmOTOFh2QdNYJUPPulcSE+GJJizoReuemDQ9grSWezEg+xr4d+xFpO1qW19AnJjUOmFTSDoX9ynOOMrn6veOO4QIoREQslQCS/ESS/+UhTr3Ea9o04hGkIR1bOm1uD3J9PapHTnu1fZf8MlWJZf23ER5+DEEKuoesBt6XUAY/ZbnMHXEAulQ/5buYzRgnzVo6rZlSMnLRTFimNrIQaJG9W/6vIy+8uuyUJBX3wEJyhkIDxBodFV38goY5gqVGTLC2agsLqNubTl353JWdvsBNIDiNnGojtNaWwgdMzPb1hRmh6VKjdjzb0bG30uViYaplJNTliIPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=CrmwLre874s9XIXxDVJv2DhxFGuz4XuU3it6eTatltY=; b=gKV6mR8F211AHdHy9m9IWMBiutM22Yo2/gCZXBj/WmrpD1rnpEhlqKl3UpN8rJzROopVNwOzUNl26L4nqbBBmXtcyq0yjmmi2J+cv4uCeNY00CcOaHGNAfW4EEAucjSeW3+UJGTrVwKtAybrUMmfogruL578cyc5Lq2nhwSsiYBaUNoz7MxiDOm79Ykn9XDr/nCK/M0RngjlAOJmeWKhn1QeGmDQVyY5AN01Oyk9Myt9vwir8p/48S6enOXHCxCj19Tn25stLJLBMTq7X+ioB/6e4gKwkl6SWhwZ+/GfktEaVAEBTMVNVcJVYjll0TyRtbUsZ2q6VOFUo45S23woIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <85939a58-ab7f-31ea-1014-91cabe746046@suse.com> Date: Wed, 12 Jan 2022 09:56:12 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Subject: [PATCH 2/2] x86/time: improve TSC / CPU freq calibration accuracy Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= References: <595b3447-0eba-dfc8-fa24-6bb8b964a137@suse.com> In-Reply-To: <595b3447-0eba-dfc8-fa24-6bb8b964a137@suse.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: AM6PR02CA0020.eurprd02.prod.outlook.com (2603:10a6:20b:6e::33) To VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4362bd33-7722-4b6a-147f-08d9d5a96360 X-MS-TrafficTypeDiagnostic: VI1PR04MB5167:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: owatKwgEyiBtNdCyDPDrRAQAV/REuZpsAE966cAji0RMaMvRwE/dMEo1TEUxSeSY3RxJw4wkaaQ2eRVmYNzoYw8UJV9lAgKWSd5NMd322ImaS84Od99b7WdB1hGPGx6q8V/I/GJXtXuqIxSmXZ0Gb8kVMKObakqMwhNQUDeCbOCw/R/NxUqKwpFfzFey/mQH+PzTB3zOh0ju8GyYD6xAa1AdtOVACJB4oycePJgAZF2t4IKFnF/3o+eMT6cvdgWuIXqVOEuv1leKKfeZgVw2KhxCLQc4JjhBTbg5AwYsli4MiT1E01K3dgJqnlSd0+jqr43NkzV3RKbR/b0bWHlkfls/IuF2Ol2oUK75CpF3rFIa/vdEX24ST3/jmkA0wIN+pHKcq1MpwSFjcYnAphTYr335csnPDB0js6ra3PX2xm2UPgw5asS224rZZA/WpOfBTubTThvCR6WRayyh82DmrD7GjbsrK9N3KDDgEBwYp5ZQXR7aT6AV2r/MB074Wr8t+RpDpN2xk+5JhQeq4V/oh31LYoQS9kazpB8XDwM4nYf48cd3pY/S+rB2EDSx2oDoMQgIiSLy+MPz1GbkZWFtSoXOVONWTQkO/UGNLUfhKSpN4l0Ce6XkiOaFT3YyAHrAGG/jqA5ICA8wT27YoK4VDVjQwl7KAd7IFjbVfDOHfKIuNS6NaYDbCuC2ZswfhhhgusDvlLnQ3ATzHa7akyX821eolZemZPA56O8VOfJOFFqr/HhqMSILXx78bbc5LAG9 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR04MB5600.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(8936002)(26005)(316002)(36756003)(31696002)(6506007)(186003)(5660300002)(2906002)(86362001)(2616005)(6486002)(8676002)(83380400001)(4326008)(6512007)(66476007)(54906003)(6916009)(31686004)(66556008)(66946007)(508600001)(38100700002)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?lo+RVx5dFUtjEPN//JsCtB/QWB81TUhzKk7T5reFlCpytbJHc/DKjTyoW3b1?= =?us-ascii?Q?keIbhyP5r/6/YN9t2zVpg+gsRJcmtOyMU3yeTQSxnqiX2gbkE9ewGU6+XcSh?= =?us-ascii?Q?7kiF1Q6Y5RLE8ZEZX67R7/hBsxWuPmmYBcfQBXD4rLs9GUm3dCHKoSpA8jLu?= =?us-ascii?Q?q1eXbFEdvh8Snu8IMuslZ0jnDaOcefD/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us-ascii?Q?+mEMUkyhgiSAWTFtc9zKWNSERcO2Gj+UHVzyJoi5LyE4xF+V9aFib/7emnFr?= =?us-ascii?Q?+nO+m2I/G8GF3yk4HED0JTY+mRbaYcIGCUhDUZGc+P+mL+J0qf+vtp/7Tb2E?= =?us-ascii?Q?/C1LCEynoTK4GIfDYGwlvGpRFnCzN/R6/68ELl9CVc5SCCxkv5wlKMg3rda7?= =?us-ascii?Q?uPWVn/zYHTEph8vLn8Q08jlt/UwYP4aWOQDLoFb+ztugJdxzuYwWRf+pwzVt?= =?us-ascii?Q?o6N6EoBDnggCeEZS3VrjsSLxBVxJSMEF8juejhpzF54p3/HHnrnWI8Ccs1am?= =?us-ascii?Q?yzLPm9J+U3JXGSuScSdZuymA+pvYfRSp270KtYUx4i8cqPe+Ayx7+9+EQAZ9?= =?us-ascii?Q?rdViCxifv9RxB0jcGOvRlutsMVWFrKTehwzAvYu+6wkirLZGUTrot46H7MOK?= =?us-ascii?Q?z1207qrnQ13EXFthD8/aHaYLVj67xPEhxkaypJbws2y+L1sFKBAwWUu+9Tz2?= =?us-ascii?Q?RbUsYvMslAi4KZCMlxVohqmK+/K3DayI9BsyIfYdtcrWGzSdbNn38MJCHBkx?= =?us-ascii?Q?uS3rcWWn/TgPCM4h2nNjwo8=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4362bd33-7722-4b6a-147f-08d9d5a96360 X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5600.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2022 08:56:14.4415 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: IuJVm1vhOhb4wDjYoP5SfL82/ecbb/88TLeWJkkjhHTD9/BAL9MzNqwRjiteDEBWPqNy5NO+IIF+kSXPXZ24gg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB5167 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1641977801411100001 Content-Type: text/plain; charset="utf-8" While the problem report was for extreme errors, even smaller ones would better be avoided: The calculated period to run calibration loops over can (and usually will) be shorter than the actual time elapsed between first and last platform timer and TSC reads. Adjust values returned from the init functions accordingly. On a Skylake system I've tested this on accuracy (using HPET) went from detecting in some cases more than 220kHz too high a value to about =C2=B12kHz. On other systems (or on this system, but with PMTMR) the origin= al error range was much smaller, with less (in some cases only very little) improvement. Reported-by: James Dingwall Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monn=C3=A9 --- There's still a time window for the issue to occur between the final HPET/PMTMR read and the following TSC read. Improving this will be the subject of yet another patch. TBD: Accuracy could be slightly further improved by using a (to be introduced) rounding variant of muldiv64(). TBD: I'm not entirely sure how useful the conditional is - there shouldn't be any inaccuracy from the division when actual equals target (upon entry to the conditional), as then the divisor is what the original value was just multiplied by. And as per the logic in the callers actual can't be smaller than target. TBD: I'm also no longer sure that the helper function is warranted anymore. It started out with more contents, but is now effectively only the [conditional] muldiv64() invocation. I'm afraid I don't see a way to deal with the same issue in init_pit(). In particular the (multiple) specs I have to hand don't make clear whether the counter would continue counting after having reached zero. Obviously it wouldn't help to check this on a few systems, as their behavior could still be implementation specific. --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -287,6 +287,23 @@ static char *freq_string(u64 freq) return s; } =20 +static uint64_t adjust_elapsed(uint64_t elapsed, uint32_t actual, + uint32_t target) +{ + if ( likely(actual > target) ) + { + /* + * A (perhaps significant) delay before the last timer read (e.g. = due + * to a SMI or NMI) can lead to (perhaps severe) inaccuracy if not + * accounting for the time elapsed beyond the originally calculated + * duration of the calibration interval. + */ + elapsed =3D muldiv64(elapsed, target, actual); + } + + return elapsed * CALIBRATE_FRAC; +} + /************************************************************ * PLATFORM TIMER 1: PROGRAMMABLE INTERVAL TIMER (LEGACY PIT) */ @@ -455,7 +472,7 @@ static int64_t __init init_hpet(struct p while ( (elapsed =3D hpet_read32(HPET_COUNTER) - count) < target ) continue; =20 - return (rdtsc_ordered() - start) * CALIBRATE_FRAC; + return adjust_elapsed(rdtsc_ordered() - start, elapsed, target); } =20 static void resume_hpet(struct platform_timesource *pts) @@ -505,7 +522,7 @@ static s64 __init init_pmtimer(struct pl while ( (elapsed =3D (inl(pmtmr_ioport) & mask) - count) < target ) continue; =20 - return (rdtsc_ordered() - start) * CALIBRATE_FRAC; + return adjust_elapsed(rdtsc_ordered() - start, elapsed, target); } =20 static struct platform_timesource __initdata plt_pmtimer =3D