linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yu Liao <liaoyu15@huawei.com>
To: <alexandre.belloni@bootlin.com>, <a.zummo@towertech.it>
Cc: <liaoyu15@huawei.com>, <liwei391@huawei.com>,
	<linux-kernel@vger.kernel.org>, <linux-rtc@vger.kernel.org>
Subject: [RFC PATCH 1/2] rtc: add lockless rtc_update_irq_enable
Date: Wed, 26 Oct 2022 11:33:47 +0800	[thread overview]
Message-ID: <20221026033348.1660732-2-liaoyu15@huawei.com> (raw)
In-Reply-To: <20221026033348.1660732-1-liaoyu15@huawei.com>

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 <liaoyu15@huawei.com>
---
 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 @@
 
 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
+static int __rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled);
 
 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;
 }
 
+/* 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, unsigned int enabled)
 }
 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
 
-int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
+static int __rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
 {
-	int err;
-
-	err = mutex_lock_interruptible(&rtc->ops_lock);
-	if (err)
-		return err;
+	int err = 0;
 
 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
 	if (enabled == 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 == enabled)
-		goto out;
+		return err;
 
 	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, unsigned int enabled)
 
 		err = __rtc_read_time(rtc, &tm);
 		if (err)
-			goto out;
+			return err;
 		onesec = ktime_set(1, 0);
 		now = rtc_tm_to_ktime(tm);
 		rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
@@ -597,7 +593,19 @@ int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
 		rtc_timer_remove(rtc, &rtc->uie_rtctimer);
 	}
 
-out:
+	return err;
+}
+
+int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
+{
+	int err;
+
+	err = mutex_lock_interruptible(&rtc->ops_lock);
+	if (err)
+		return err;
+
+	err = __rtc_update_irq_enable(rtc, enabled);
+
 	mutex_unlock(&rtc->ops_lock);
 
 	return err;
-- 
2.25.1


  reply	other threads:[~2022-10-26  3:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26  3:33 [RFC PATCH 0/2] rtc: fix race condition between uie enable and rtc set time Yu Liao
2022-10-26  3:33 ` Yu Liao [this message]
2022-10-26  3:33 ` [RFC PATCH 2/2] rtc: fix race condition in rtc_set_time() Yu Liao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221026033348.1660732-2-liaoyu15@huawei.com \
    --to=liaoyu15@huawei.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=liwei391@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).