Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2503902pxk; Sun, 4 Oct 2020 01:45:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw90bpeMK6jkR6GOf6dNaf2ZPrLKXDjg0+XGaCjxUF1uzxwvsFkRVqz/z+1vQPrRPcU2GZQ X-Received: by 2002:a50:fd83:: with SMTP id o3mr11274950edt.176.1601801149894; Sun, 04 Oct 2020 01:45:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601801149; cv=none; d=google.com; s=arc-20160816; b=si2/sm7j97J4ESQauMvgqxd/+fesmXpPWMLtS2tgUdXvgMKwoq5Tj80rZUKv1S11zE SJ3bup6LR0Lkm8pHhQbSx/ewPaOSBqiMkrjJhWeLEnN1KwUbjUJSg34QPwADSk9DRBjg VJ92d3Ezs00taMCtVXxVjhCYaNVKVKlrXBImnh/R8m7mBRXa4C9mK1qeuBePGDw/3Pz9 PBxSwq7t0GSMz1tSJbpXNb/U/7WvxgZAAEIgj1grskyStQeFvLrsDdANTB5pKKkLFhqz WK2MSTVjg6WDGdET+g9wEC3pB7swv1xb30dYHk/9MupcEKJYzza7cGdY9tqeyIHDv9bB Co9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=gUoNlwsT4oM3eErqIEOVR9ta1BFYxBWToE5s6nVloIs=; b=NOKpi/GrCKV12w7QE4rHJYmYYqbT0UzDYYsFoqnU/joBeMG1IRe2tTlnhPnBF8WKiC MXgjlHc9H27u7/ASP+YDCbpFXDrbSy3px4Hl5TyZYwYYLGWWutMMZx65P/j9+u/ObS+a OKDWpsIdjtzfVbK7otlsg47xxUe7dg7R5ViJyKJA4YBpGKpYiV8cUke+L2H9atiEy1C4 UQPvAF/cpAjGc4pjXvScsFByAwZcOrKsZETvTa9UWX56BR9IPWuvg4rSkHlW188tLkMS GExKjOM0hqFSoLRxYCeMRDL1diOi/pUDZ7482Km79X2kPTIqJOIl3Z+oB2Y1ojzmi82m Lgdg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pg3si4842184ejb.94.2020.10.04.01.45.26; Sun, 04 Oct 2020 01:45:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725909AbgJDImS (ORCPT + 99 others); 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