Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp214954pxa; Fri, 14 Aug 2020 01:58:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNqSmopcCS7spjyUSgE9PED6u/pjrs7Zs4ykVWAa1cF0OUSKD6mlUsNVzpT9ayY6jguPCk X-Received: by 2002:a17:906:1911:: with SMTP id a17mr1311130eje.431.1597395535002; Fri, 14 Aug 2020 01:58:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597395534; cv=none; d=google.com; s=arc-20160816; b=imCF51fEOJrAgz6SdAjyWKDJvU8R4tqO5nOuPwjXEa1ilrx7VIeJBfInLkbFFEidAu O/bApEZF1CEH/otxz/fvPjtlJBsXaiSVt4XgAdh55qrhrCumqI+Wg9fG+EJ3SoXIt+Np v6Z+YzHSYiwdgWxP80v1txbAEuftF8c6cIN5r17vuyd6H+B8tz3NIBQm+J6FeQUrn+6k DuIhiYqYXGQXov19za9jYSsVruGmFL9rd5Nxnp/apEvygX1hgrkiWzV7vdNY0VnhCM6X TqtJyzKE2uUT7s2GktmJTlQ3++WMCK9SDnUsk6rSI0fyEvUwOphzoxZWu9tTF4D9IFTE 6frw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=H74Cx0z7INlxIE76e/bwEKWBIIXIiyO99JQfsOTNz+c=; b=xqMw7eMzd5MlAllT3TsetghpWh86aLk/hnFP3dKjLr3c9izxDwMWaObfT5jFbRQD0F k6fUfhhI+kbaFI1H6G376D1gYi1lKY+ZegSA0Kp1hs4C1J6nN3Yclh26mu5J1zLV/wKw 4THMsRGhvT+9fGTDdhikQpCgsF9keOsSMC2KzL9v2bj//dfRo6Y5WTwSbgdvj4n/WLx8 4lY3IXpB64Ra0fl96ZiHQqYLdtUmz5ytfXMdu9rbk57ZEgbKhpmAznt8YPoBG1SUJKUf QCEy7h1yP7m13Sw74GEBhhi9pEvy/axD+MT8bRhv2Q3pN2t1v/15XpEwhfIvlpTIIttU VxyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fC2m851E; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q17si5150999edg.164.2020.08.14.01.58.32; Fri, 14 Aug 2020 01:58:54 -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=@google.com header.s=20161025 header.b=fC2m851E; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727771AbgHNI4l (ORCPT + 99 others); Fri, 14 Aug 2020 04:56:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726050AbgHNI4l (ORCPT ); Fri, 14 Aug 2020 04:56:41 -0400 Received: from mail-lf1-x142.google.com (mail-lf1-x142.google.com [IPv6:2a00:1450:4864:20::142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D09D7C061383 for ; Fri, 14 Aug 2020 01:56:40 -0700 (PDT) Received: by mail-lf1-x142.google.com with SMTP id b11so4438033lfe.10 for ; Fri, 14 Aug 2020 01:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=H74Cx0z7INlxIE76e/bwEKWBIIXIiyO99JQfsOTNz+c=; b=fC2m851EepP6y3+PUFFwoHaW8xxT6TI6FbzNMNPsrg/JesnhLqHkvJaxd5tFQycNf0 V8FaaoydQbWXbrjgIO/bP5wYHxHfaqDANieNPmkvl/tXmwVOcxu5EnmicJvDUU7sPKpW P7iP6es/T3PsAln32FalesydnX2NRu0tcyI8TSRjKskSLY7FnpPbGJm7Pu9HkQ+menwx HN6z15CxIiQLGAI9aaUwCXe/3m7FQSGW8MNcMnntUVoKCKrtBp+05mvNQYu/jJDc1jjL 4zAwt2obtcCp1SPvas74t295EvGSCo6p3m51vCtedKH4GOfCx2rTv03zEdTQfZ6EJPW/ h1Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=H74Cx0z7INlxIE76e/bwEKWBIIXIiyO99JQfsOTNz+c=; b=MjmfYIL74VjqkbyjGnVGPak0ygQ0wxEtayU/yOtRhZ5+Qi+LMEhrtBdRvbm9Q1kQvv 9fDA6yaKXvyGsqed7wKORancJCQ52J9PwnWvHWP54eVePwsJoiQHfv9OC2cMFdn5OUwC 4Y6O3UYm9OidYxiWHIv7CmkRYulIwt8F2uqC0xVkgfiRfnFxulBnUMgiram+QyW1WzE4 T7pTeEegdnLFonzOSFv6BXJNB8hv3mx+LswUkhHqJt5YFMbQT7TlCH9hx2IrdSsSYo+S QJ6vgIoITs/Caobk17+GTGw2EFkk9Z93QNTRTBt9TcoVb3gzN/9FFgJ3gXbLYqj2TkmX /2Sw== X-Gm-Message-State: AOAM531h9dXbDlH8wQ60+Sir7sHPHh7lVfL6ScPBkNeYWHnXbcsTFPi0 36eeDEHoc8Pw16qhFKgMsmJHqErZMnAC1tvHsWqeQA== X-Received: by 2002:a19:8644:: with SMTP id i65mr802101lfd.20.1597395399029; Fri, 14 Aug 2020 01:56:39 -0700 (PDT) MIME-Version: 1.0 References: <20200813154020.1.Iaf7638a2f2a87ff68d85fcb8dec615e41340c97f@changeid> <20200813073325.GK3480@piout.net> <20200814081530.GA6530@piout.net> In-Reply-To: <20200814081530.GA6530@piout.net> From: Victor Ding Date: Fri, 14 Aug 2020 18:56:02 +1000 Message-ID: Subject: Re: [PATCH] rtc: cmos: initialize rtc time when reading alarm To: Alexandre Belloni Cc: LKML , Alessandro Zummo , linux-rtc@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Alexandre, On Fri, Aug 14, 2020 at 6:15 PM Alexandre Belloni wrote: > > On 14/08/2020 16:10:13+1000, Victor Ding wrote: > > Hi Alexandre, > > > > On Thu, Aug 13, 2020 at 5:33 PM Alexandre Belloni > > wrote: > > > > > > Hi, > > > > > > On 13/08/2020 15:41:34+1000, Victor Ding wrote: > > > > cmos_read_alarm() may leave certain fields of a struct rtc_time > > > > untouched; therefore, these fields contain garbage if not properly > > > > initialized, leading to inconsistent values when converting into > > > > time64_t. > > > > This patch to set all fields of a struct rtc_time to -1 before calling > > > > cmos_read_alarm(). > > > > > > > > > > I don't think this actually helps with the conversion as mktime64 > > > is taking unsigned int so I would think you need the whole logic that is > > > in __rtc_read_alarm > > > > It's true that this change does not produce a correct time64_t; however, > > it isn't the intention either. The proposed change only produces a > > consistent value: calling obtaining identical struct rtc_time if the CMOS > > wakealarm is unchanged. In the case of suspend/resume, a correct value > > time64_t is not necessary; a consistent value is sufficient to correctly > > perform an equality test for `t_current_expires` and `t_saved_expires`. > > Logic to deduce a correct time64_t is expensive and hence I would like to > > avoid __rtc_read_alarm's logic here. > > > > Prior to this patch, the struct rtc_time is uninitialized. After calling > > cmos_read_alarm(), the tm_year field is always left untouched and hence > > contains only garbage. On platforms without enhanced RTC timers, the > > tm_mon and tm_mday fields are left with garbage as well. Therefore, > > `t_current_expires` and `t_saved_expires` from garbage data, which leads > > to incorrect equality test results. > > > > Seeing that saved_wkalrm is initialized to zero, wouldn't it be > sufficient to initialize current_alarm to 0? This can be done simply at > the declaration. I personally find the -1 to be confusing especially > since the result ends up being architecture dependent. > Good point. Initializing the struct to 0 is also sufficient; I'll update the patch to initialize the fields to 0 and submit the updated version. Note that both `saved_wkalrm` and `current_alarm` must be initialized. `cmos_suspend` may be called multiple times; `cmos_read_alarm` may or may not update `time.tm_mon` or `time.tm_mday` depends on `cmos->day_alrm` and `cmos->mon_alrm`. Both `day_alrm` and `mon_alrm` could theoretically change. Say if at first `day_alrm` is enabled, `cmos_read_alarm` would fill `time.tm_mday`. Later on, if `day_alrm` becomes disabled, `cmos_read_alarm` would not update `time.tm_mday` and hence leaves its value as garbage. > > > > > > > Signed-off-by: Victor Ding > > > > --- > > > > > > > > drivers/rtc/rtc-cmos.c | 2 ++ > > > > 1 file changed, 2 insertions(+) > > > > > > > > diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c > > > > index bcc96ab7793f..c99af567780d 100644 > > > > --- a/drivers/rtc/rtc-cmos.c > > > > +++ b/drivers/rtc/rtc-cmos.c > > > > @@ -1006,6 +1006,7 @@ static int cmos_suspend(struct device *dev) > > > > enable_irq_wake(cmos->irq); > > > > } > > > > > > > > + memset(&cmos->saved_wkalrm.time, -1, sizeof(struct rtc_time)); > > > > cmos_read_alarm(dev, &cmos->saved_wkalrm); > > > > > > > > dev_dbg(dev, "suspend%s, ctrl %02x\n", > > > > @@ -1054,6 +1055,7 @@ static void cmos_check_wkalrm(struct device *dev) > > > > return; > > > > } > > > > > > > > + memset(¤t_alarm.time, -1, sizeof(struct rtc_time)); > > > > cmos_read_alarm(dev, ¤t_alarm); > > > > t_current_expires = rtc_tm_to_time64(¤t_alarm.time); > > > > t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time); > > > > -- > > > > 2.28.0.236.gb10cc79966-goog > > > > > > > > > > -- > > > Alexandre Belloni, Bootlin > > > Embedded Linux and Kernel engineering > > > https://bootlin.com > > > > Best regards, > > Victor Ding > > -- > Alexandre Belloni, Bootlin > Embedded Linux and Kernel engineering > https://bootlin.com Best regards, Victor Ding