Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6182296yba; Wed, 1 May 2019 07:30:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqxOO5DQvhPmeGy3mPkYgNBopsUyxh4y6ohf2dqVU6Au+VyT7MXMHiLZZNoBQC3eKOidEsLk X-Received: by 2002:a62:2a97:: with SMTP id q145mr80632575pfq.22.1556721027369; Wed, 01 May 2019 07:30:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556721027; cv=none; d=google.com; s=arc-20160816; b=VnNEBOuxxSv9uinuTGDwEKTskKvmdmZwGyuF9zNYRbilaLA9rUul/ehEibeN8fgAXP L10VLAScXS9H4Xrxrd5TmMUoGd4U4F25bJJCHEzBmILWT8trgL+37PfA4A1h6VlSATRj MnwJZBYwMENIQftKNjRgWtSSImmRQLrltWzf3yVn+p98IPSed4rbYPgMzgnWUgLQY9uR mpm1kZ73ETuQcBw5y+4PQ1xqharQsRokwV8pkRnlYtXPVlRYfZwbC9uM6xUA2C5Tn48C Is0Mfv1YOW6JQMLcXWThKX7SBxcKdqaSglkMtpau7kEFv50GdluEalnAIzWT2Q8Fbie/ +kMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=TU+W6p6AQXL5aukf0RfewkscgySIDjs4Tf74P8JUQ0w=; b=CD1ufWo2gD91CW52fmLUpn3BuRGs5k4oG4v4nzhmd1Ak0iM1H9qRFbooFoFnAhen6q 2knhD+OoTJLqtyDC0P1gxvk2eqadGOb70FNaX41lK9S7gsbNNhbJ9TtMmklKMeOQiPoA rDQbqahD9ePg3NjTL3ovLE1xgEg6LkgnpRdavYUeGdh9gfNV1kb6XTnH3gL8CNnBAWD7 3Dv44+SHj06ZvwUCJxrLwl3O1Yjd8vWk/m0Sg2vMkd5CGlxnEAQkINtIUF0xIXYHjKoA OBEPBkxA/qGgcMkqE41LMD2Ke2m6OQjXqZRur+7ZIYMpNXBocI50jB5/szzo1ENCvOhf xs7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k1si6167111pld.322.2019.05.01.07.30.10; Wed, 01 May 2019 07:30:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726803AbfEAOZQ (ORCPT + 99 others); Wed, 1 May 2019 10:25:16 -0400 Received: from relay9-d.mail.gandi.net ([217.70.183.199]:34295 "EHLO relay9-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726727AbfEAOZQ (ORCPT ); Wed, 1 May 2019 10:25:16 -0400 X-Originating-IP: 90.66.53.80 Received: from localhost (lfbn-1-3034-80.w90-66.abo.wanadoo.fr [90.66.53.80]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 046E3FF803; Wed, 1 May 2019 14:25:13 +0000 (UTC) Date: Wed, 1 May 2019 16:25:13 +0200 From: Alexandre Belloni To: Trent Piepho Cc: "patrice.chotard@st.com" , "linux-rtc@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] rtc: st-lpc: remove unnecessary check Message-ID: <20190501142513.GK11339@piout.net> References: <20190430201834.12634-1-alexandre.belloni@bootlin.com> <1556663479.31309.36.camel@impinj.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1556663479.31309.36.camel@impinj.com> User-Agent: Mutt/1.11.3 (2019-02-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 30/04/2019 22:31:19+0000, Trent Piepho wrote: > On Tue, 2019-04-30 at 22:18 +0200, Alexandre Belloni wrote: > > The RTC core already ensures the alarm is set to a time in the future, it > > is not necessary to check again in the driver. > > My reading of the rtc core code is that it checks if the alarm is in > the future *twice* before handing off the set call to the driver, which > possibly checks a 3rd time (as seen here). > > However, all these checks are done *before* setting the alarm. It > still possible to have a race and set the alarm after the time has > already passed, in which case the alarm will never fire. > I agree the core need to handle that possible race better and this is something I'm planning to work on. > The way to fix the race would be to have the driver check the alarm > *after* setting it. In precisely this order, do these steps: > > 1. Set alarm in RTC, to Talarm > 2. Get time from RTC, as Tcurrent > 3. Get alarm status from RTC > > If Talarm < Tcurrent, alarm was set to future time, no error This should be Talarm > Tcurrent, right? > Else > If status == fired, alarm was set and has since fired, no error > Else status == not fired, alarm was set in past, EINVAL > > This should be race free. > > > > > > - /* Invalid alarm time */ > > - if (now_secs > alarm_secs) > > - return -EINVAL; > > - > > memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm)); > > > > /* Now many secs to fire */ > alarm_secs -= now_secs; > lpa = (unsigned long long)alarm_secs * rtc->clkrate; > > While it's true the time wouldn't normally be in past, it still races, > as describe above. In that case, the math here underflows alarm_secs, > so it probably still makes sense to check. I can't believe you can possibly have more than one second between the check in the core and the check in the driver, it doesn't make much sense to check, even in the current state of the core. -- Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com