Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp159774pxu; Wed, 2 Dec 2020 18:13:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYRJqWepQncMjPaXPW1MntDngjKZdn3UhfOIv6zbjnc7IZtAY3gytZf0VS1qRqHS+hbDWZ X-Received: by 2002:a17:906:eb49:: with SMTP id mc9mr586339ejb.487.1606961638736; Wed, 02 Dec 2020 18:13:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606961638; cv=none; d=google.com; s=arc-20160816; b=0UTso8ZofVYhBDKLZ0qY5cqStA58pnZ5k41UN80pMdCrKPI6v4AkwbWmfW4iDaQJ56 X/XELli6hfZ344KNVhiNXXVlSPZiJ4awGo/8v8IduK9s7qmMapvhvHcCtdXevbY9zQap g27AHtfp/LN2vn3BEcNu+7Qsh5BhIU3QUwG6OJLnB6KbaXdPrUsSYEQiHun/LNqO4Bfj zF5hLtUELtp2qEY5m550fP3YgY7uPdCOcgxod64yFC8xvsEEXeKS+Snv4kUnXb8POehZ oEB6Q/J9BVxaXVp4Y/tL6lEO8aJP/wgH+Breb0o+d9vWl6VSaKS5RXoSoghhOU+ZiK4H SarQ== 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=UV54pa9B2mAhux9iyIUm6cVLC4TOtGf4MqR+kZq6HmU=; b=rvddnsgYIfmGfB1bF1dQeHWxc0ssQvhmhLwKL+GmIm54A+RlbcIUrCw6i9f3XDbAC/ 7rl5EAmP1NpjN1sjNNUKPULeSr8icvNLybazg00kQg5Z/xchDGNH+P1v8/R2kyTgH60O 5ngaGuW2Y42s5q4+fy1c12BMseA0BdTyanz+5rgFwzmHTFQgEEVxoicrb5qFRtIimZEU jdedk2Nj2IY5dL8qQHXMQ1B0QSvnCxSt25xIXeWDGDPeEJxN+IkPystj9snvsPShsBzB kWg39m/i7vPz8+BaWkZJtiqIJB4CvZ8wWOQlLRc/O8QC5Hd3KxNWlakaSNvnfVonBPI4 rC+Q== 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 v3si103501edt.166.2020.12.02.18.13.36; Wed, 02 Dec 2020 18:13:58 -0800 (PST) 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 S2387527AbgLCCLb (ORCPT + 99 others); Wed, 2 Dec 2020 21:11:31 -0500 Received: from relay12.mail.gandi.net ([217.70.178.232]:37779 "EHLO relay12.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725893AbgLCCLb (ORCPT ); Wed, 2 Dec 2020 21:11:31 -0500 Received: from localhost (lfbn-lyo-1-997-19.w86-194.abo.wanadoo.fr [86.194.74.19]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay12.mail.gandi.net (Postfix) with ESMTPSA id F102B200002; Thu, 3 Dec 2020 02:10:47 +0000 (UTC) Date: Thu, 3 Dec 2020 03:10:47 +0100 From: Alexandre Belloni To: Thomas Gleixner Cc: Jason Gunthorpe , Miroslav Lichvar , linux-kernel@vger.kernel.org, John Stultz , Prarit Bhargava , Alessandro Zummo , linux-rtc@vger.kernel.org, Peter Zijlstra Subject: Re: [PATCH] rtc: adapt allowed RTC update error Message-ID: <20201203021047.GG3544@piout.net> References: <20201201143835.2054508-1-mlichvar@redhat.com> <20201201161224.GF5487@ziepe.ca> <20201201171420.GN1900232@localhost> <20201201173540.GH5487@ziepe.ca> <87mtywe2zu.fsf@nanos.tec.linutronix.de> <20201202162723.GJ5487@ziepe.ca> <87a6uwdnfn.fsf@nanos.tec.linutronix.de> <20201202205418.GN5487@ziepe.ca> <874kl3eu8p.fsf@nanos.tec.linutronix.de> <87zh2vd72z.fsf@nanos.tec.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87zh2vd72z.fsf@nanos.tec.linutronix.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Thomas, I'll take more time to reply more in depth to the whole email but... On 03/12/2020 02:14:12+0100, Thomas Gleixner wrote: > Aside of that the magic correction of the time which is written to the > RTC is completely bogus. Lets start with the interface and the two > callers of it: > > static inline bool rtc_tv_nsec_ok(s64 set_offset_nsec, > struct timespec64 *to_set, > const struct timespec64 *now) > > The callers are: > > sync_cmos_clock() /* The legacy RTC cruft */ > struct timespec64 now; > struct timespec64 adjust; > long target_nsec = NSEC_PER_SEC / 2; > > ktime_get_real_ts64(&now); > if (rtc_tv_nsec_ok(-1 * target_nsec, &adjust, &now)) { > if (persistent_clock_is_local) > adjust.tv_sec -= (sys_tz.tz_minuteswest * 60); > rc = update_persistent_clock64(adjust); > } > > sync_rtc_clock() > unsigned long target_nsec; <- Signed unsigned .... > struct timespec64 adjust, now; > > ktime_get_real_ts64(&now); > > adjust = now; <- Why the difference to the above? > > if (persistent_clock_is_local) <- Again, why is the ordering different? > adjust.tv_sec -= (sys_tz.tz_minuteswest * 60); > > rc = rtc_set_ntp_time(adjust, &target_nsec) > // int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec) > > struct timespec64 to_set; > > set_normalized_timespec64(&to_set, 0, -rtc->set_offset_nsec); > *target_nsec = to_set.tv_nsec; <- target_nsec = rtc->set_offset_nsec > because the timespec is normalized > ergo == rtc->set_offset_nsec > unless the set_offset_nsec would > be negative which makes at all. > > if (rtc_tv_nsec_ok(rtc->set_offset_nsec, &to_set, &now)) > update_rtc(...); > > So sync_cmos_clock hands in -(NSEC_PER_SEC/2) and the rtc cruft hands in > NSEC_PER_SEC/2 by default. The comment in drivers/rtc/class.c says: > > drivers/rtc/class.c- /* Drivers can revise this default after allocating the device. */ > drivers/rtc/class.c: rtc->set_offset_nsec = NSEC_PER_SEC / 2; > > but no driver ever bothered to change that value. Also the idea of > having this offset as type s64 is beyond my understanding. Why the heck > would any RTC require to set an offset which is _after_ the second > transition. > This (no driver making use of set_offset_nsec) happened because it got applied without me agreeing to the change. I did complain at the time and RMK walked away. [...] > That said, can somebody answer the one million dollar question which > problem is solved by all of this magic nonsense? > The goal was to remove the 500ms offset for all the RTCs but the MC146818 because there are RTC that will reset properly their counter when setting the time, meaning they can be set very precisely. IIRC, used in conjunction with rtc_hctosys which also adds inconditionnaly 500ms this can ends up with the system time being one second away from the wall clock time which NTP will take quite some time to remove. > If anyone involved seriously believes that any of this solves a real > world problem, then please come forth an make your case. > > If not, all of this illusionary attempts to be "correct" can be removed > for good and the whole thing reduced to a > > update_rtc_plus_minus_a_second() > > mechanism, which is exactly what we have today just without the code > which pretends to be *exact* or whatever. > Coincidentally, I was going to revert those patches for v5.11. Also, honestly, I still don't understand why the kernel needs to set the RTC while userspace is very well equipped to do that. chrony is able to set the RTC time and it can do so precisely. It can even compute how that RTC is time drifting and that value can already be used to adjust the RTC crystal. From my tests, with some efforts, userspace can set the RTC time with a 20?s precision, even on low end systems. To do so, it doesn't need set_offset_nsec. I also don't like hctosys, it is currently wrong but I can see use cases and now systemd relies on its presence so my plan is to fix it. -- Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com