From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7966C4727D for ; Sun, 4 Oct 2020 08:42:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9128320829 for ; Sun, 4 Oct 2020 08:42:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725915AbgJDImS (ORCPT ); Sun, 4 Oct 2020 04:42:18 -0400 Received: from relay12.mail.gandi.net ([217.70.178.232]:34141 "EHLO relay12.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725825AbgJDImS (ORCPT ); Sun, 4 Oct 2020 04:42:18 -0400 Received: from localhost (lfbn-lyo-1-1908-165.w90-65.abo.wanadoo.fr [90.65.88.165]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 88518200002; Sun, 4 Oct 2020 08:42:09 +0000 (UTC) Date: Sun, 4 Oct 2020 10:42:09 +0200 From: Alexandre Belloni To: Jonathan =?iso-8859-1?Q?Neusch=E4fer?= Cc: Uwe =?iso-8859-1?Q?Kleine-K=F6nig?= , linux-kernel@vger.kernel.org, Heiko Stuebner , linux-pwm@vger.kernel.org, Linus Walleij , Thierry Reding , Fabio Estevam , linux-rtc@vger.kernel.org, Arnd Bergmann , Mauro Carvalho Chehab , Sam Ravnborg , Daniel Palmer , Andy Shevchenko , Andreas Kemnade , NXP Linux Team , devicetree@vger.kernel.org, Stephan Gerhold , allen , Sascha Hauer , Lubomir Rintel , Rob Herring , Lee Jones , linux-arm-kernel@lists.infradead.org, Alessandro Zummo , Mark Brown , Pengutronix Kernel Team , Heiko Stuebner , Josua Mayer , Shawn Guo , "David S. Miller" Subject: Re: [PATCH v3 5/7] rtc: New driver for RTC in Netronix embedded controller Message-ID: <20201004084209.GV2804081@piout.net> References: <20200924192455.2484005-1-j.neuschaefer@gmx.net> <20200924192455.2484005-6-j.neuschaefer@gmx.net> <20200925054424.snlr3lggnsv575wu@pengutronix.de> <20201004014323.GD500800@latitude> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20201004014323.GD500800@latitude> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04/10/2020 03:43:23+0200, Jonathan Neuschäfer wrote: > > > +static int ntxec_set_time(struct device *dev, struct rtc_time *tm) > > > +{ > > > + struct ntxec_rtc *rtc = dev_get_drvdata(dev); > > > + int res = 0; > > > + > > > + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_YEAR, ntxec_reg8(tm->tm_year - 100)); > > > + if (res) > > > + return res; > > > + > > > + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MONTH, ntxec_reg8(tm->tm_mon + 1)); > > > + if (res) > > > + return res; > > > + > > > + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_DAY, ntxec_reg8(tm->tm_mday)); > > > + if (res) > > > + return res; > > > + > > > + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_HOUR, ntxec_reg8(tm->tm_hour)); > > > + if (res) > > > + return res; > > > + > > > + res = regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MINUTE, ntxec_reg8(tm->tm_min)); > > > + if (res) > > > + return res; > > > + > > > + return regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, ntxec_reg8(tm->tm_sec)); > > > > I wonder: Is this racy? If you write minute, does the seconds reset to > > zero or something like that? Or can it happen, that after writing the > > minute register and before writing the second register the seconds > > overflow and you end up with the time set to a minute later than > > intended? If so it might be worth to set the seconds to 0 at the start > > of the function (with an explaining comment). > > The setting the minutes does not reset the seconds, so I think this race > condition is possible. I'll add the workaround. > Are you sure this happens? Usually, the seconds are not reset but the internal 32768kHz counter is so you have a full second to write all the registers. > > .read_time has a similar race. What happens if minutes overflow between > > reading NTXEC_REG_READ_DH and NTXEC_REG_READ_MS? > > Yes, we get read tearing in that case. It could even propagate all the > way to the year/month field, for example when the following time rolls > over: > A | B | C > 2020-10-31 23:59:59 > 2020-11-01 00:00:00 > > - If the increment happens after reading C, we get 2020-10-31 23:59:59 > - If the increment happens between reading B and C, we get 2020-10-31 23:00:00 > - If the increment happens between reading A and B, we get 2020-10-01 00:00:00 > - If the increment happens before reading A, we get 2020-11-01 00:00:00 > > ... both of which are far from correct. > > To mitigate this issue, I think something like the following is needed: > > - Read year/month > - Read day/hour > - Read minute/second > - Read day/hour, compare with previously read value, restart on mismatch > - Read year/month, compare with previously read value, restart on mismatch > > The order of the last two steps doesn't matter, as far as I can see, but > if I remove one of them, I can't catch all cases of read tearing. > Are you also sure this happens? Only one comparison is necessary, the correct order would be: - Read minute/second - Read day/hour - Read year/month - Read minute/second, compare If day/hour changes but not minute/second, it would mean that it took at least an hour to read all the registers. At this point, I think you have other problems and the exact time doesn't matter anymore. -- Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DD33C4727D for ; Sun, 4 Oct 2020 08:44:04 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7A0F020735 for ; Sun, 4 Oct 2020 08:44:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Mh4febE8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7A0F020735 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bootlin.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=VNbOnkoFJKPxWx/4QZd/I+Jt3Ck6Sbu8wVyORPWTwNU=; b=Mh4febE82YMPqMa/eRrBgDGGK O5QcYnJ1pb3LdG9Oms21i8UMO+UM7rQDEHDYwmGzlEampaVxfbBim3GFZYaGidYM+bZbCBn0SPH3f LB4mS+bybqIY9hcnN8elS2jpDVE3SL4edOpbAjxoZfby2fQhoBTbZ/3+LF7SUMZwSqiz/dC0ynePT YO3ZpZP2wsBqqknVxzqqlKow2590wcdDgc5V445Y8j8bIp9dlfJb2054Rx7mPRS1ZEhldG25hhLvD uCxJos4DyhMCzPXvzpsSa9Ykda93eZruP8wGSjXAcGUHuQ7R/QtLuFCz1eTN6w+KatDDcGTI2En/P cvEHbvbWQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kOzb3-0001OE-IF; Sun, 04 Oct 2020 08:42:21 +0000 Received: from relay12.mail.gandi.net ([217.70.178.232]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kOzb0-0001NX-My for linux-arm-kernel@lists.infradead.org; Sun, 04 Oct 2020 08:42:20 +0000 Received: from localhost (lfbn-lyo-1-1908-165.w90-65.abo.wanadoo.fr [90.65.88.165]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 88518200002; Sun, 4 Oct 2020 08:42:09 +0000 (UTC) Date: Sun, 4 Oct 2020 10:42:09 +0200 From: Alexandre Belloni To: Jonathan =?iso-8859-1?Q?Neusch=E4fer?= Subject: Re: [PATCH v3 5/7] rtc: New driver for RTC in Netronix embedded controller Message-ID: <20201004084209.GV2804081@piout.net> References: <20200924192455.2484005-1-j.neuschaefer@gmx.net> <20200924192455.2484005-6-j.neuschaefer@gmx.net> <20200925054424.snlr3lggnsv575wu@pengutronix.de> <20201004014323.GD500800@latitude> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20201004014323.GD500800@latitude> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201004_044218_925320_4348A104 X-CRM114-Status: GOOD ( 31.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Heiko Stuebner , devicetree@vger.kernel.org, Linus Walleij , Thierry Reding , Sam Ravnborg , linux-rtc@vger.kernel.org, Arnd Bergmann , Mauro Carvalho Chehab , Lee Jones , Daniel Palmer , Andy Shevchenko , Andreas Kemnade , NXP Linux Team , Uwe =?iso-8859-1?Q?Kleine-K=F6nig?= , linux-pwm@vger.kernel.org, Stephan Gerhold , allen , Sascha Hauer , Lubomir Rintel , Rob Herring , Fabio Estevam , linux-arm-kernel@lists.infradead.org, Alessandro Zummo , linux-kernel@vger.kernel.org, Mark Brown , Pengutronix Kernel Team , Heiko Stuebner , Josua Mayer , Shawn Guo , "David S. Miller" Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 04/10/2020 03:43:23+0200, Jonathan Neusch=E4fer wrote: > > > +static int ntxec_set_time(struct device *dev, struct rtc_time *tm) > > > +{ > > > + struct ntxec_rtc *rtc =3D dev_get_drvdata(dev); > > > + int res =3D 0; > > > + > > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_YEAR, ntxec_r= eg8(tm->tm_year - 100)); > > > + if (res) > > > + return res; > > > + > > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MONTH, ntxec_= reg8(tm->tm_mon + 1)); > > > + if (res) > > > + return res; > > > + > > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_DAY, ntxec_re= g8(tm->tm_mday)); > > > + if (res) > > > + return res; > > > + > > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_HOUR, ntxec_r= eg8(tm->tm_hour)); > > > + if (res) > > > + return res; > > > + > > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MINUTE, ntxec= _reg8(tm->tm_min)); > > > + if (res) > > > + return res; > > > + > > > + return regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, ntxec_= reg8(tm->tm_sec)); > > = > > I wonder: Is this racy? If you write minute, does the seconds reset to > > zero or something like that? Or can it happen, that after writing the > > minute register and before writing the second register the seconds > > overflow and you end up with the time set to a minute later than > > intended? If so it might be worth to set the seconds to 0 at the start > > of the function (with an explaining comment). > = > The setting the minutes does not reset the seconds, so I think this race > condition is possible. I'll add the workaround. > = Are you sure this happens? Usually, the seconds are not reset but the internal 32768kHz counter is so you have a full second to write all the registers. > > .read_time has a similar race. What happens if minutes overflow between > > reading NTXEC_REG_READ_DH and NTXEC_REG_READ_MS? > = > Yes, we get read tearing in that case. It could even propagate all the > way to the year/month field, for example when the following time rolls > over: > A | B | C > 2020-10-31 23:59:59 > 2020-11-01 00:00:00 > = > - If the increment happens after reading C, we get 2020-10-31 23:= 59:59 > - If the increment happens between reading B and C, we get 2020-10-31 23:= 00:00 > - If the increment happens between reading A and B, we get 2020-10-01 00:= 00:00 > - If the increment happens before reading A, we get 2020-11-01 00:= 00:00 > = > ... both of which are far from correct. > = > To mitigate this issue, I think something like the following is needed: > = > - Read year/month > - Read day/hour > - Read minute/second > - Read day/hour, compare with previously read value, restart on mismatch > - Read year/month, compare with previously read value, restart on mismatch > = > The order of the last two steps doesn't matter, as far as I can see, but > if I remove one of them, I can't catch all cases of read tearing. > = Are you also sure this happens? Only one comparison is necessary, the correct order would be: - Read minute/second - Read day/hour - Read year/month - Read minute/second, compare If day/hour changes but not minute/second, it would mean that it took at least an hour to read all the registers. At this point, I think you have other problems and the exact time doesn't matter anymore. -- = Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel