From nobody Wed Apr 8 12:14:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49F57C38A02 for ; Wed, 26 Oct 2022 03:35:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232488AbiJZDfF (ORCPT ); Tue, 25 Oct 2022 23:35:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230345AbiJZDex (ORCPT ); Tue, 25 Oct 2022 23:34:53 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE1B231DC5; Tue, 25 Oct 2022 20:34:50 -0700 (PDT) Received: from dggpeml500025.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MxvSn55RlzVjB5; Wed, 26 Oct 2022 11:30:01 +0800 (CST) Received: from dggpeml500003.china.huawei.com (7.185.36.200) by dggpeml500025.china.huawei.com (7.185.36.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 26 Oct 2022 11:34:48 +0800 Received: from huawei.com (10.175.103.91) by dggpeml500003.china.huawei.com (7.185.36.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 26 Oct 2022 11:34:48 +0800 From: Yu Liao To: , CC: , , , Subject: [RFC PATCH 1/2] rtc: add lockless rtc_update_irq_enable Date: Wed, 26 Oct 2022 11:33:47 +0800 Message-ID: <20221026033348.1660732-2-liaoyu15@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221026033348.1660732-1-liaoyu15@huawei.com> References: <20221026033348.1660732-1-liaoyu15@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500003.china.huawei.com (7.185.36.200) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Split out a function that does not acquire rtc->ops_lock from rtc_update_irq_enable, in preparation for fixing race condition in rtc_set_time. Signed-off-by: Yu Liao --- drivers/rtc/interface.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 9edd662c69ac..bc55dd31bece 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -19,6 +19,7 @@ =20 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *tim= er); static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *tim= er); +static int __rtc_update_irq_enable(struct rtc_device *rtc, unsigned int en= abled); =20 static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm) { @@ -81,6 +82,7 @@ static int rtc_valid_range(struct rtc_device *rtc, struct= rtc_time *tm) return 0; } =20 +/* This function must be called with rtc->ops_lock held */ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) { int err; @@ -553,27 +555,21 @@ int rtc_alarm_irq_enable(struct rtc_device *rtc, unsi= gned int enabled) } EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable); =20 -int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) +static int __rtc_update_irq_enable(struct rtc_device *rtc, unsigned int en= abled) { - int err; - - err =3D mutex_lock_interruptible(&rtc->ops_lock); - if (err) - return err; + int err =3D 0; =20 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL if (enabled =3D=3D 0 && rtc->uie_irq_active) { - mutex_unlock(&rtc->ops_lock); return rtc_dev_update_irq_enable_emul(rtc, 0); } #endif /* make sure we're changing state */ if (rtc->uie_rtctimer.enabled =3D=3D enabled) - goto out; + return err; =20 if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) || !test_bit(RTC_FEATURE_ALARM, rtc->features)) { - mutex_unlock(&rtc->ops_lock); #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL return rtc_dev_update_irq_enable_emul(rtc, enabled); #else @@ -587,7 +583,7 @@ int rtc_update_irq_enable(struct rtc_device *rtc, unsig= ned int enabled) =20 err =3D __rtc_read_time(rtc, &tm); if (err) - goto out; + return err; onesec =3D ktime_set(1, 0); now =3D rtc_tm_to_ktime(tm); rtc->uie_rtctimer.node.expires =3D ktime_add(now, onesec); @@ -597,7 +593,19 @@ int rtc_update_irq_enable(struct rtc_device *rtc, unsi= gned int enabled) rtc_timer_remove(rtc, &rtc->uie_rtctimer); } =20 -out: + return err; +} + +int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) +{ + int err; + + err =3D mutex_lock_interruptible(&rtc->ops_lock); + if (err) + return err; + + err =3D __rtc_update_irq_enable(rtc, enabled); + mutex_unlock(&rtc->ops_lock); =20 return err; --=20 2.25.1 From nobody Wed Apr 8 12:14:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 163FBC38A02 for ; Wed, 26 Oct 2022 03:35:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233058AbiJZDfL (ORCPT ); Tue, 25 Oct 2022 23:35:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232981AbiJZDe6 (ORCPT ); Tue, 25 Oct 2022 23:34:58 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98F8A33E0A; Tue, 25 Oct 2022 20:34:51 -0700 (PDT) Received: from dggpeml500020.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MxvVN1ckzzpVrr; Wed, 26 Oct 2022 11:31:24 +0800 (CST) Received: from dggpeml500003.china.huawei.com (7.185.36.200) by dggpeml500020.china.huawei.com (7.185.36.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 26 Oct 2022 11:34:49 +0800 Received: from huawei.com (10.175.103.91) by dggpeml500003.china.huawei.com (7.185.36.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 26 Oct 2022 11:34:49 +0800 From: Yu Liao To: , CC: , , , Subject: [RFC PATCH 2/2] rtc: fix race condition in rtc_set_time() Date: Wed, 26 Oct 2022 11:33:48 +0800 Message-ID: <20221026033348.1660732-3-liaoyu15@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221026033348.1660732-1-liaoyu15@huawei.com> References: <20221026033348.1660732-1-liaoyu15@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500003.china.huawei.com (7.185.36.200) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Commit 7e7c005b4b1f ("rtc: disable uie before setting time and enable after") was introduced to solve problem that rtc_timer_do_work will loop for a while, when setting the time in the future with the uie timer enabled. But reading uie timer state and setting rtc time are not in a critical section, a race condition may occur in rtc_set_time which has two issues: 1) In the above scenario, rtc_timer_do_work may still loop for a while. For example consider the following sequence: CPU0 CPU1 ---- ---- ioctl(RTC_SET_TIME) ioctl(RTC_UIE_ON) uie =3D rtc->uie_rtctimer.enabled; [ assume uie is 0 ] if (uie) rtc_update_irq_enable(rtc, 0); rtc_update_irq_enable(rtc, 1); [ uie is enabled ] rtc->ops->set_time(); [ set rtc time in the future ] 2) A thread try to turn off uie, however rtc_settime called by another thread turns on uie when they run in parallel. Consider the following sequence: CPU0 CPU1 ---- ---- ioctl(RTC_SET_TIME) ioctl(RTC_UIE_OFF) rtc->ops->set_time(); rtc_update_irq_enable(rtc, 0); rtc_update_irq_enable(rtc, 1); Fix it by guaranteeing that reading uie timer state, setting rtc time and enabling uie timer within a critical section. Fixes: 7e7c005b4b1f ("rtc: disable uie before setting time and enable after= ") Signed-off-by: Yu Liao --- drivers/rtc/interface.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index bc55dd31bece..00a6173d8895 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -139,21 +139,21 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_t= ime *tm) =20 rtc_subtract_offset(rtc, tm); =20 + err =3D mutex_lock_interruptible(&rtc->ops_lock); + if (err) + return err; + #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL uie =3D rtc->uie_rtctimer.enabled || rtc->uie_irq_active; #else uie =3D rtc->uie_rtctimer.enabled; #endif if (uie) { - err =3D rtc_update_irq_enable(rtc, 0); + err =3D __rtc_update_irq_enable(rtc, 0); if (err) return err; } =20 - err =3D mutex_lock_interruptible(&rtc->ops_lock); - if (err) - return err; - if (!rtc->ops) err =3D -ENODEV; else if (rtc->ops->set_time) @@ -162,15 +162,15 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_t= ime *tm) err =3D -EINVAL; =20 pm_stay_awake(rtc->dev.parent); - mutex_unlock(&rtc->ops_lock); /* A timer might have just expired */ schedule_work(&rtc->irqwork); =20 if (uie) { - err =3D rtc_update_irq_enable(rtc, 1); + err =3D __rtc_update_irq_enable(rtc, 1); if (err) return err; } + mutex_unlock(&rtc->ops_lock); =20 trace_rtc_set_time(rtc_tm_to_time64(tm), err); return err; --=20 2.25.1