Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp4431843pxb; Sat, 12 Feb 2022 05:05:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJw3x5oCpcM5iZf4d8Kz+s4TrF9bMu4Arn08Sr6IfUo1LeqgbHCUExUhYttORb0/jjvAaIs6 X-Received: by 2002:a17:90a:741:: with SMTP id s1mr5193383pje.161.1644671135449; Sat, 12 Feb 2022 05:05:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644671135; cv=none; d=google.com; s=arc-20160816; b=vSvYKj6rs8XpQeKapKTmu6NXnJ096OvBxeO9H5hfnUBxIGK9cxXPxrx72TZibr5b3f 7p3BpGKMxtaDjvcbRbjB/QCHCV87RSUy2BmSqZOnvJRC1b4jyP5XrBP/ef9dCk2e2At5 s0K1xu7oyM2hDbEFi+Za+nfn4vYrTs2IvgxirYBoRCe7idqJ+Ea0tJ5503nkOpWzMT3H 7U+oiXVk2o2twBOTlVLcU8pBdWBJPbR3KmL8+sr3jtUekpycbK8OoC3sKWTEHeXwxQi6 NAJX4FR9w+CCA2yWzA4tHN6HCVzJvo/OEhMKpcI92+0QP/+wiDAEM63/3SL+lnkgR7GV 06qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=prpqRHP0dlcg3P32JWEbdgjnfZCD8Jb0vc5TmR7Dks8=; b=wPcwJOJxYfPKRFVmpGgGYuXLQurChHQS2HSFxGSPpfE1w05SjSUugp4SBJA86agy2l hXgO6wlYjPIiDL/o401vZsSp+6cO0JziZrvXkoc7TXntRBAtyysUtBSfjA9RbYpplxh2 AA8v3+2WMiI/APRdpm6jnyR+oNQrsyEPMEYWj+H9wXYIOVGhc3taud/9/YJSrR+26f7k 1dkbC2IEFbjmNdrd2M1GW7T/EUmi6GTu2w4l73eP88u/+KPl81MhpXon3lSfyX+Fifm6 vI/RQwnU5aytKr/0c8kfSoUjCYZXd8hTdGtq2qUvbl+59VueQZMFbuqwVBrP0QvR7iLt PSAg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t7si7604702plg.297.2022.02.12.05.05.22; Sat, 12 Feb 2022 05:05:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350015AbiBKM1E (ORCPT + 93 others); Fri, 11 Feb 2022 07:27:04 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:34294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349992AbiBKM04 (ORCPT ); Fri, 11 Feb 2022 07:26:56 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9BAE9F28; Fri, 11 Feb 2022 04:26:55 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5F5E01042; Fri, 11 Feb 2022 04:26:55 -0800 (PST) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.196.172]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A02483F70D; Fri, 11 Feb 2022 04:26:53 -0800 (PST) From: Andre Przywara To: Maxime Ripard , Chen-Yu Tsai , Jernej Skrabec Cc: Rob Herring , Ondrej Jirman , Icenowy Zheng , Samuel Holland , linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Alessandro Zummo , Alexandre Belloni , linux-rtc@vger.kernel.org Subject: [PATCH v10 03/18] rtc: sun6i: Fix time overflow handling Date: Fri, 11 Feb 2022 12:26:28 +0000 Message-Id: <20220211122643.1343315-4-andre.przywara@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220211122643.1343315-1-andre.przywara@arm.com> References: <20220211122643.1343315-1-andre.przywara@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Using "unsigned long" for UNIX timestamps is never a good idea, and comparing the value of such a variable against U32_MAX does not do anything useful on 32-bit systems. Use the proper time64_t type when dealing with timestamps, and avoid cutting down the time range unnecessarily. This also fixes the flawed check for the alarm time being too far into the future. The check for this condition is actually somewhat theoretical, as the RTC counts till 2033 only anyways, and 2^32 seconds from now is not before the year 2157 - at which point I hope nobody will be using this hardware anymore. Signed-off-by: Andre Przywara Reviewed-by: Jernej Skrabec --- drivers/rtc/rtc-sun6i.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/rtc/rtc-sun6i.c b/drivers/rtc/rtc-sun6i.c index 35b34d14a1db..dc3ae851841c 100644 --- a/drivers/rtc/rtc-sun6i.c +++ b/drivers/rtc/rtc-sun6i.c @@ -139,7 +139,7 @@ struct sun6i_rtc_dev { const struct sun6i_rtc_clk_data *data; void __iomem *base; int irq; - unsigned long alarm; + time64_t alarm; struct clk_hw hw; struct clk_hw *int_osc; @@ -511,10 +511,8 @@ static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm) struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); struct rtc_time *alrm_tm = &wkalrm->time; struct rtc_time tm_now; - unsigned long time_now = 0; - unsigned long time_set = 0; - unsigned long time_gap = 0; - int ret = 0; + time64_t time_now, time_set; + int ret; ret = sun6i_rtc_gettime(dev, &tm_now); if (ret < 0) { @@ -529,9 +527,7 @@ static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm) return -EINVAL; } - time_gap = time_set - time_now; - - if (time_gap > U32_MAX) { + if ((time_set - time_now) > U32_MAX) { dev_err(dev, "Date too far in the future\n"); return -EINVAL; } @@ -540,7 +536,7 @@ static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm) writel(0, chip->base + SUN6I_ALRM_COUNTER); usleep_range(100, 300); - writel(time_gap, chip->base + SUN6I_ALRM_COUNTER); + writel(time_set - time_now, chip->base + SUN6I_ALRM_COUNTER); chip->alarm = time_set; sun6i_rtc_setaie(wkalrm->enabled, chip); -- 2.25.1