From patchwork Thu Jul 25 19:33:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13742102 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8B5FBC3DA49 for ; Thu, 25 Jul 2024 19:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:Date:Subject:Cc:To:From:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=Se9rtGe0gsx2t28vr9Hga6+vIF8IF05glvFWcgoTFuw=; b=Y6L0K3qgitwSghOZqVkjVdNDJs FZsGhkWzTatCK+ym+bnL7Dym+B1hMn09ObDhyworFKBM89mf1kXXK/JuOrzdJcaaoVTQrr8H4InMt Rge2IROcPKIFD3GzyCV42t5a8y7jh7a0NtfBSRgMOZS6lbSJEvCppfEWiiykNr7C/mkYEp2mFT1W0 k2bSaudNZnbOJxI8ZXo+WHS5amynk7db4CVsXAFf5UVylbdv7/kmA7J4A1H8loYZb7tBb10IwJbx3 OLWMkBgNar4M4dPihczSivYXPz8lK4q7OwgEZbXte0BKfVThKf6vMl1Co10/OYUSLIjL2NT7ivwam jAmx3iMw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sX4Ee-00000001uZ8-3qB4; Thu, 25 Jul 2024 19:34:44 +0000 Received: from mail-northeuropeazlp170110001.outbound.protection.outlook.com ([2a01:111:f403:c200::1] helo=DB3PR0202CU003.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sX4EF-00000001uRQ-0w4z for linux-arm-kernel@lists.infradead.org; Thu, 25 Jul 2024 19:34:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=SrWRSJWoNp6wXDwfV9e3VvT6j23u/U1Nxu5iCDjGlreLUbJjl4MEOW13VVA/rCB1nu/W1VplpAaiXfXtelQcZ4UNJg7UGrGkK9SwzUlT/LbYEraZhElRhUr1nMCEb62hywtMB4i58vFk/CIKFylyHxf1dMTbAehgO9Nov7yQFTclIP6IvrOuZs4WbWto4h9BmkgLZoNwkIhhRKmQIDNkzYncRV9+zCDPmqLYamwLpPQ0ouwaLtSUOJFsuQ3fTIf0z4buio7eB8/fNG/RfJP7heKonpXEyN84ir87akwye10enUmGAz9qu3M587XbSCrBmfBCXR2YjYJACLMg2YG0sQ== 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=Se9rtGe0gsx2t28vr9Hga6+vIF8IF05glvFWcgoTFuw=; b=t45UWM/hbnGQ2YmQ/1BclzmgyxvB1F8HwuiyoaxJr9/hyUIzOocni5de4ZeoK38lB7bbIHLaonqMOo7jwT136F6+rkgYFmLyY7DgEwJtB879VJEkXj0hSPQfz1FAc/fy0pgcoyJB0g28Kaw7cf4MJqqE2l5w+aVe0rQ412WWT6Otyl4eZr3sJU4yu7FcoAROW1Mw7PsEvexlrack+oFCz6bPXZMltKv6NmVP/9bntYNlIngNpUOOgcwWBdHzxht08aoTRLRZLQkfXD537bzZN/5IZSUfcp6ITEP6nO6NdVvTdczbagnWRkBRx0T0EoxhLEVmmfpapdqOMKJSqVUNfQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Se9rtGe0gsx2t28vr9Hga6+vIF8IF05glvFWcgoTFuw=; b=EJZ8RF1O9f2/FOY5D5guaoTHW3UqaA5k8dhcTA8LKd6jOvRSmIDGRl8Gu5T56ziScRzLvLi24gf6ARccv5/d3x+dMjaV29WzEJ946v44H7JB4cuhbEZyHMmSSuo36qYuzw3SGVaSp4YlA1nCcotWbODrxgQq7UtZNeeBYteKP6/fGVJpP5JlG1VyeE+/oyccxtSNTmWVLtQalInaGKApzCorHv/MvPzUmKmIX85ZkG8M5NQTTcnxYKmbR+6PZGqlEGPURRn3jbmmNQZpk7an11CCsYWiSQJT++7v8YVKJgl1HOW6EbZA971jRhS2ZM2a7L02hnVDpg9Lf2ayMGBfWA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by DU2PR04MB8791.eurprd04.prod.outlook.com (2603:10a6:10:2e2::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.28; Thu, 25 Jul 2024 19:34:15 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%5]) with mapi id 15.20.7784.017; Thu, 25 Jul 2024 19:34:15 +0000 From: Frank Li To: Daniel Lezcano , Thomas Gleixner , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Dong Aisheng , linux-kernel@vger.kernel.org (open list:CLOCKSOURCE, CLOCKEVENT DRIVERS), imx@lists.linux.dev (open list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE), linux-arm-kernel@lists.infradead.org (moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE) Cc: imx@lists.linux.dev Subject: [PATCH 1/2] clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX Date: Thu, 25 Jul 2024 15:33:54 -0400 Message-Id: <20240725193355.1436005-1-Frank.Li@nxp.com> X-Mailer: git-send-email 2.34.1 X-ClientProxiedBy: BYAPR02CA0062.namprd02.prod.outlook.com (2603:10b6:a03:54::39) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|DU2PR04MB8791:EE_ X-MS-Office365-Filtering-Correlation-Id: 61d9fc54-e00a-4336-08ef-08dcace0c46c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|376014|366016|921020|38350700014; X-Microsoft-Antispam-Message-Info: 4G2Q9Sp+C1qrVElvvdMZC0hEfSqS1zb9UTXDlNcdFZk5Ge+ALF1T6bkomNA8ZNrHpa11ZNfbo5QI9w7MFQSnlmea9Hn3pyrvofrBTC+gE6FbdIBBGa207NVQZfCZ0Z5Ur5YKpnD4daN3be4Fw4v5Ao44vt6voIjsFWLtIb4L6lKWKyr1LzSiUpKcnybPeQv9oaMCfHXZkej0aIFWYRKIbsU/0BqNXj/MRagOwCWIuwW4GkhQaseVfC/TjkKovJZcr5C0wVUt160S2JyENdeMRql9rqQXx7+5kH04T3iq5N7W7kgZbFrCoU38je7r7fW2uh6iHJo23zi/s/c0BGhecMdZ//2Adqo936q5AD3bT3+4aiO54jMGy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efront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(376014)(366016)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ASg/prKSCNXmZmYSKDtQF3KB7S4uLP9wG3WWGlXYi2cf6LsKg0+i8g0VxayWRMsyBsfT2vdotHFuA17G6n0eZhrCD9IufzsLH9SUwjFEvyZPPgjMZ5SgeM9D0TzUMI/ay26qIrz/PSYUrXZ77ojW/+w/gZOLwZZ03x+Pc3V69VwtZ27QEJ4iH9suk0Cd4IDd6bVEOxl/aZVd320BBZVxdHdBhAC1UtqhBdby96VOdGS6a8nFAKs0hzDmKVKutUq8fqS+xXDS4mlzrwIIv9rqVlUbMU0cU/swM8Rv1LBX8tCVYfwJpBYukCgqr6th511GTvYTkM9cvi4JxYdqicowK8WjvN4rix3mjIK9h5l0rynmXUq82BskuicOb3VGlaIzEte9MLr5TpScBNKiIv+9Xu8+MK+f/Ox1tz6nWwscDTYMCxtEDHGLL3EYtDHCVTRuulX2z38D56hVu0xMx8wgVVOmeO0HeaE9gWc40gvCNl3zfFazMEH2s8qsY/GfIDOKvTiIv8zN0wwf1X0YWQXXngD/soWECD9PX2m/xhGvlEhiPnzj4/Xu6G7YFefp+/awtATCtXUKLiiCdDt7P1IZuzOwMZ77/qhF+W1JrXHDYYkD/o+woQEt46g/QAxEHIApiU0hmXF5DpoyWzaE3RokTiUN5UMN+fD+AehcbsWVRFhEz5r0vdaK/jdePp6Ayy2dqOx5OTTsoaJh0rq+tNcICUdJ0v3m4Zlh2s12B0J+bAZHWx2i8tpxtVylriqJ6HJPDm8mtH0EiNAwijgFKr9f4oayyl6I3G+y2kKgv3FwEomXNX9GRyL4vedZnHOauqs7t6E2hcXoxS1g49/yYAufjQ4YByJic67vjETaaZHSzQ4nEPaCrk0WnlpBr3EstvGTOAc9GmMmATi6Ndzjx5i0A1Kf/on2/YywV4ypSUWDIyjB6vi3cyoxnV/JFiHtsqJXlgfICC/2E6au4NMUhVHHOZsu6x3G8jY0PWzLWOFCRgvE6jRZ00OtD/AYjrCwdjty8Y/O1V01xx1iqVqX9NGnDFiFldJhfe/cDtUoY7Ej7RlZF8dIzWXGhH6VQvfwhh6bFf7nhctqNSWY2siaLIb9lM9/69gxstbs7Szm3J7IJOUD897GaxpNuXOKBKKI6N92q8rI4HoKAPOl1JwTR4AjAeOrcZMDhDLcLJsIAC5qfcvs00lPYqOPo6bdfSrCJ8EmAxC8KsW3wFpgBpnjCVVUIfCH9hPrXROwweFsghjt4vg1YqMmLjJIfDWbiRBFDssPf72le+ShwakNZXrrLd5Gu0r65Irp9SGxhe4ewCNGGzUyB8quve31vlSVicIrJ63iiokuvV7M8ZuPgfjaTqLtdgGOpLuUM7SVadHetou0LaREVcSGIv7yu4rH/536dVhOonZNICEBXRZd1eYcRG7DfVlY8qhRw4BX7KgoKWoSv+FCNDl2dFmt3l7mipFVhymRJiGyFGAsS/KIRWGciZNO0e+MOxH6TXtu/RVWqJpSvKMR5iHBb3ild3IYIUboa8/1aQMoTUnOBCZzzBkwnG2QMS6LQ5CWhKSBmqusOcEBQRwIH4nR8P8yLc9zrSw3I//w X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 61d9fc54-e00a-4336-08ef-08dcace0c46c X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jul 2024 19:34:14.9666 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: eRT0HLDDcNE75z2z2ZK8D6JNVQ17SQ04WekjT5KO/kDuyyf13D3d6rT4Zsp6F/RpC1nkmGotqz/lXNmvh7xFAA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8791 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240725_123419_336306_48391ED3 X-CRM114-Status: GOOD ( 13.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Jacky Bai In tpm_set_next_event(delta), return -ETIME by wrong cast to int when delta is larger than INT_MAX. For example: tpm_set_next_event(delta = 0xffff_fffe) { ... next = tpm_read_counter(); // assume next is 0x10 next += delta; // next will 0xffff_fffe + 0x10 = 0x1_0000_000e now = tpm_read_counter(); // now is 0x10 ... return (int)(next - now) <= 0 ? -ETIME : 0; ^^^^^^^^^^ 0x1_0000_000e - 0x10 = 0xffff_fffe, which is -2 when cast to int. So return -ETIME. } To fix this, introduce a 'prev' variable and check if 'now - prev' is larger than delta. Cc: Fixes: 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support") Signed-off-by: Jacky Bai Reviewed-by: Peng Fan Reviewed-by: Ye Li Reviewed-by: Jason Liu Signed-off-by: Frank Li --- drivers/clocksource/timer-imx-tpm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/clocksource/timer-imx-tpm.c b/drivers/clocksource/timer-imx-tpm.c index bd64a8a8427f3..cd23caf1e5999 100644 --- a/drivers/clocksource/timer-imx-tpm.c +++ b/drivers/clocksource/timer-imx-tpm.c @@ -83,10 +83,10 @@ static u64 notrace tpm_read_sched_clock(void) static int tpm_set_next_event(unsigned long delta, struct clock_event_device *evt) { - unsigned long next, now; + unsigned long next, prev, now; - next = tpm_read_counter(); - next += delta; + prev = tpm_read_counter(); + next = prev + delta; writel(next, timer_base + TPM_C0V); now = tpm_read_counter(); @@ -96,7 +96,7 @@ static int tpm_set_next_event(unsigned long delta, * of writing CNT registers which may cause the min_delta event got * missed, so we need add a ETIME check here in case it happened. */ - return (int)(next - now) <= 0 ? -ETIME : 0; + return (now - prev) >= delta ? -ETIME : 0; } static int tpm_set_state_oneshot(struct clock_event_device *evt)