Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp228869pxa; Fri, 14 Aug 2020 02:29:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxya3dWMCUuQOYriltypmSk4Atr55+zo2+iqcXNM8RhpN0koyle9iFpYwoj+bAPgae0YvM4 X-Received: by 2002:a50:e047:: with SMTP id g7mr1310943edl.290.1597397349691; Fri, 14 Aug 2020 02:29:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597397349; cv=none; d=google.com; s=arc-20160816; b=Sr13zAYmh/tbhQv34F/1w7zCGgZmZaEvdUkuSfkYfkuoJWUAYJF36J2JZAe1Qovh1I O+NcMgOx+U04RHPLWK0RYM1mpRGppjMlcccbwLPqSe8mbNIfoZBKqB2lzrS8r7rfYY0f 3zlpw19OcpRTK9IKJ3wqr0htIwcNN9NU8+jUOh9l8fOMCftMn5pHoBvlM/vOKlQbKFBC kvTZ9eekK+Qo0Ui3n8EueupIpgmHW6NbcSZYCHnbEiDO6YnNW9PtLCXndcT0TePpEL9g mxJLZjZH/gZnbUsCbLXJ+o3CdKCUzJbwLr44TIkzae+5GUgbFsJzodHsg1Lf0uCDFrwj qNXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=dTTO8Fv93c+c8SEX0dkxqjnGFIMlASMutNhHUlLK4hQ=; b=j998x9B+fQmXFWNu+h8IX/8/RTdXQJqa3L0YzNEnBgC9eTHOVrsP/HEdOEtkV8s+wV wmk1WdljEP+CP8tC5Vzoxznf99FpGCZyu22SdS6GL4X6Xd+94tj8w7c0ewOVOvxFKLcQ cFXCWea5idY57TtbzynPu1Xx6FoZt0L6f248qGsmiZcXjzsNgTbjvHaGu8GVBC4DYDOb a7e0kzClE63/mSU4Kddlnb9b95ZGDuVYjwpY29wuSBqVa62a3pUuYb1/3uG+yTUVomAP sTActh57HP9ea6mg8i8yLkHjkm9H9ETygVFv42GFAJITH+4xvCJUEu6OrIbFNPmm2rrR Nr2w== 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 ec15si5320605ejb.236.2020.08.14.02.28.47; Fri, 14 Aug 2020 02:29:09 -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 S1727100AbgHNIPf (ORCPT + 99 others); Fri, 14 Aug 2020 04:15:35 -0400 Received: from relay8-d.mail.gandi.net ([217.70.183.201]:36111 "EHLO relay8-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726669AbgHNIPd (ORCPT ); Fri, 14 Aug 2020 04:15:33 -0400 X-Originating-IP: 90.66.108.79 Received: from localhost (lfbn-lyo-1-1932-79.w90-66.abo.wanadoo.fr [90.66.108.79]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id E896F1BF204; Fri, 14 Aug 2020 08:15:30 +0000 (UTC) Date: Fri, 14 Aug 2020 10:15:30 +0200 From: Alexandre Belloni To: Victor Ding Cc: LKML , Alessandro Zummo , linux-rtc@vger.kernel.org Subject: Re: [PATCH] rtc: cmos: initialize rtc time when reading alarm Message-ID: <20200814081530.GA6530@piout.net> References: <20200813154020.1.Iaf7638a2f2a87ff68d85fcb8dec615e41340c97f@changeid> <20200813073325.GK3480@piout.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > > > > > 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