Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp3660332pxu; Sun, 11 Oct 2020 19:11:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk71lsYv2fzRbIno9XpEXJ2m6fHOgAArQBPn0qB8qbJhDVYKy/yLiI3tK3Piya9LooIuLf X-Received: by 2002:a17:906:d8b6:: with SMTP id qc22mr26342923ejb.196.1602468681132; Sun, 11 Oct 2020 19:11:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602468681; cv=none; d=google.com; s=arc-20160816; b=kwpdo5dHEpcJZMjpCvY1l+Kirobvca8eU8wxBH/o1N2VCShlvGp5RuuexPy9ZUfXWi LgIwlXn+M9xRSoLnS9aCJ6RE3eKsOHxR56A7BqaOMnX9AW9Usmyqc4kQYARMDnb6cy8v yjRL5wfClw7tbb3grYJG48NAwuUHv/736TgzQZzsA486PoYpyokDzLSh5ab2ev3cIH6b ltZutNWtRCWy+qLl+PJzNlL3qsiZWR+bHPSMvDF+HnvPNn2uqC8udkGUJLIKfGn0cUcS sLTeY8hlV8XR+O0XYhaL1fNZUK2L+08DT1OLrOOVFznN5HwVMfi2hBwzdR7Ywk//kHla XzBA== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=lghg+r+PYWHIStOOePJqfiofQHXW27fcjPPxjvwd4sY=; b=rdMLLtTnD9CEi6ryDOVhzmo/AWAZoA7U8pALdbN9MaoDS2Lq8nZRuUunCb+zrURzJh FqgfZR0XuvD/Wo3NApL5ulQ4hzc6rdCkXdxf9DzbtDhBkPgdc6HFjxitSX785qAetOhJ /5nAONGm7Z1etDJlaJvNF1qmpc3onHO/y9I2RlEyTlN1J3dQKnYL+dskS4zGABnflx/q UB508mN17w5RShadlfpHW56DZdpcC+A4EB9hLCgIKmuP2iqKPjfYu8ORjy784hnyQloY OiBj5ZOQln9gCDAcfeGM+14rkwPAE+w5dGmu/BkrRT0YgVhR+ixP6Xvq/lgE4RGZo+J0 kkTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=ZdwHoKYL; 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 l23si14643596ejb.488.2020.10.11.19.10.57; Sun, 11 Oct 2020 19:11:21 -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; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=ZdwHoKYL; 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 S1728743AbgJKTKP (ORCPT + 99 others); Sun, 11 Oct 2020 15:10:15 -0400 Received: from mout.gmx.net ([212.227.15.15]:34633 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728236AbgJKTKP (ORCPT ); Sun, 11 Oct 2020 15:10:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1602443357; bh=4tiCguk2G4daQO6UyDdSbqh3sXV+E4fT80cf7CrV5AY=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:References:In-Reply-To; b=ZdwHoKYLT6P2T0uwwc/eglY00QK5rz5GCKVpP7P5+ihulRsEn9bdWFuMTqMyK8ujR qZYR5/AWRlNaDB7cRYiIVciYb1X0cfY78GPDNLyPQCP+PWMM2GbbpdqTPQ87a2jhhb Z2Fsp/q3mTYgVIViyodVsLYgJCyjTJl8/OPhuiic= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from longitude ([37.201.214.162]) by mail.gmx.com (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MCsU6-1kaWQG2skq-008rQY; Sun, 11 Oct 2020 21:09:16 +0200 Date: Sun, 11 Oct 2020 21:09:10 +0200 From: Jonathan =?utf-8?Q?Neusch=C3=A4fer?= To: Alexandre Belloni Cc: Jonathan =?utf-8?Q?Neusch=C3=A4fer?= , Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= , 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: <20201011190910.GE500800@latitude> References: <20200924192455.2484005-1-j.neuschaefer@gmx.net> <20200924192455.2484005-6-j.neuschaefer@gmx.net> <20200925054424.snlr3lggnsv575wu@pengutronix.de> <20201004014323.GD500800@latitude> <20201004084209.GV2804081@piout.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="2hMgfIw2X+zgXrFs" Content-Disposition: inline In-Reply-To: <20201004084209.GV2804081@piout.net> X-Provags-ID: V03:K1:7K8Md1pZqbYIW8A9iut38o8lntAa5HGcKgmIuw+DcTCDMPBJz0o fIz/Bi/xrZp3TgfNoxDkvxWMtHjJQPAt+yjh2XCdaU2BZF1gMs8giJXBnWRLBJH8AcAAYRl tjzTY3jkWaRJxT3OK6Iz0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N8roD1qfaMSrpHQ5a2E3qUcRU7Fn5 O8O+9US2zPGSiMB9jTmREq95D69Zv9zUbMFD6YEmYGP/cVR/xiI3Z5kYCKMEV5MBFiVzaPz1+ Y/eIHVG6NHROpdvbizPNp0IkoqTE4YY3IbgiiF6VvluZJHNKcy6ditGKXGsQ1u5O5IY3CTiw8 Fsp277vtJq3kUY/Tpo48gUsMWfOgaSUkloUW1/ddfaWvrifNzpYUegs4oYUAc84jxGgCt2u0I YuyZqSvkQzo3o5d9m5LFnlTGuPOcCcICHxj4iqdh01JRCNX8WkXSZqicmKVoYpZuFDhP6jeco qu1422B2ijs+bGQj9WZJ8CVqZ7+WwUeStnXfThw== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --2hMgfIw2X+zgXrFs Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Oct 04, 2020 at 10:42:09AM +0200, Alexandre Belloni wrote: > On 04/10/2020 03:43:23+0200, Jonathan Neusch=C3=A4fer wrote: > > > > +static int ntxec_set_time(struct device *dev, struct rtc_time *tm) =2E... > > > > + res =3D regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_MINUTE, ntx= ec_reg8(tm->tm_min)); > > > > + if (res) > > > > + return res; > > > > + > > > > + return regmap_write(rtc->ec->regmap, NTXEC_REG_WRITE_SECOND, ntxe= c_reg8(tm->tm_sec)); > > >=20 > > > 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). > >=20 > > The setting the minutes does not reset the seconds, so I think this race > > condition is possible. I'll add the workaround. > >=20 >=20 > 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. I just checked it, and on this RTC, the phase / sub-second part is not reset when the time is set. > > > .read_time has a similar race. What happens if minutes overflow betwe= en > > > reading NTXEC_REG_READ_DH and NTXEC_REG_READ_MS? > >=20 > > 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 > >=20 > > - If the increment happens after reading C, we get 2020-10-31 2= 3:59:59 > > - If the increment happens between reading B and C, we get 2020-10-31 2= 3:00:00 > > - If the increment happens between reading A and B, we get 2020-10-01 0= 0:00:00 > > - If the increment happens before reading A, we get 2020-11-01 0= 0:00:00 > >=20 > > ... both of which are far from correct. > >=20 > > To mitigate this issue, I think something like the following is needed: > >=20 > > - 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 misma= tch > >=20 > > 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. > >=20 >=20 > Are you also sure this happens? >=20 > Only one comparison is necessary, the correct order would be: >=20 > - Read minute/second > - Read day/hour > - Read year/month > - Read minute/second, compare With this order, every one-second increment is detected, which I previously tried to avoid. But I suppose it's fine because it simplifies the logic and the window from first to last read should be short enough anyway to be relatively unlikely to hit, and thus not cause a lot of retrie= s. > 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. Indeed. Thanks, Jonathan Neusch=C3=A4fer --2hMgfIw2X+zgXrFs Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEvHAHGBBjQPVy+qvDCDBEmo7zX9sFAl+DWEYACgkQCDBEmo7z X9uepA//fs6TCspmRMOe/zMk+MiAPbG25ReDry1BglGSs1eeSHmE96PH7J5nrOId wQvmP/r+Sf3MqaIie02JZpoj3J223zgb0dfY3fUZ1FwsUpm7lbRJRSmuPEcBcdFd 6VcvgCL9/xASAYe0ShQg2Gni6Gk6zzGLRxx0C+PTDvar9pzutLZKngTAPDM6yDwO 4xlZkGWjQ0jxVhcJnUiNF3ooj/PFbkOLGkHlQfWHToDBXWKDkfDiMGFREdnN6lTI qzMGwUUCbjEQaGlM/GDMKI+Oq9oqgizZKx+hJ+2hay94cDz/Bh+hqFe5j4TCrvVG QCnOixWBQs80Zwoei8owdgszzKasJPFTQ08tcnYGsJvdOTHjTPWY1XUPWhZaxdHO 57Pg/t3JpU10VjkEIzdg/qLmdlK7yqZ5BPk1WCeLfobzgr72EKslIveFP+7TmqHG j9EP5NewELC3Zt5sYvcTy6A7rxNT4TpRr0I3N9As1MNereNq2YV78ylqHDN8xj80 Meo726ga70Tr2nWQ5/VyPabbvsXsuQGjIpcFs3XjfnvpCRDolGUnjEpy3W9EFMqt WFW5XUa9tw+snOi2VkMxmsVa3nNbpH0dLO22JN+C/Y4yEzpdQBPmLDkQDv4rIDqf UvXNBVGffn1fxxS3UX71T/KtwxDsGT6+QopIL47DURXw8yqX14Y= =GuMr -----END PGP SIGNATURE----- --2hMgfIw2X+zgXrFs--