Commit 489405fe authored by Gregory CLEMENT's avatar Gregory CLEMENT Committed by Linus Torvalds

rtc: armada38x: fix concurrency access in armada38x_rtc_set_time

While setting the time, the RTC TIME register should not be accessed.
However due to hardware constraints, setting the RTC time involves
sleeping during 100ms.  This sleep was done outside the critical section
protected by the spinlock, so it was possible to read the RTC TIME
register and get an incorrect value.  This patch introduces a mutex for
protecting the RTC TIME access, unlike the spinlock it is allowed to
sleep in a critical section protected by a mutex.

The RTC STATUS register can still be used from the interrupt handler but
it has no effect on setting the time.
Signed-off-by: default avatarGregory CLEMENT <gregory.clement@free-electrons.com>
Acked-by: default avatarAlexandre Belloni <alexandre.belloni@free-electrons.com>
Acked-by: default avatarAndrew Lunn <andrew@lunn.ch>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: <stable@vger.kernel.org>	[4.0]
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent b1432a2a
...@@ -40,6 +40,13 @@ struct armada38x_rtc { ...@@ -40,6 +40,13 @@ struct armada38x_rtc {
void __iomem *regs; void __iomem *regs;
void __iomem *regs_soc; void __iomem *regs_soc;
spinlock_t lock; spinlock_t lock;
/*
* While setting the time, the RTC TIME register should not be
* accessed. Setting the RTC time involves sleeping during
* 100ms, so a mutex instead of a spinlock is used to protect
* it
*/
struct mutex mutex_time;
int irq; int irq;
}; };
...@@ -59,8 +66,7 @@ static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm) ...@@ -59,8 +66,7 @@ static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
struct armada38x_rtc *rtc = dev_get_drvdata(dev); struct armada38x_rtc *rtc = dev_get_drvdata(dev);
unsigned long time, time_check, flags; unsigned long time, time_check, flags;
spin_lock_irqsave(&rtc->lock, flags); mutex_lock(&rtc->mutex_time);
time = readl(rtc->regs + RTC_TIME); time = readl(rtc->regs + RTC_TIME);
/* /*
* WA for failing time set attempts. As stated in HW ERRATA if * WA for failing time set attempts. As stated in HW ERRATA if
...@@ -71,7 +77,7 @@ static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm) ...@@ -71,7 +77,7 @@ static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
if ((time_check - time) > 1) if ((time_check - time) > 1)
time_check = readl(rtc->regs + RTC_TIME); time_check = readl(rtc->regs + RTC_TIME);
spin_unlock_irqrestore(&rtc->lock, flags); mutex_unlock(&rtc->mutex_time);
rtc_time_to_tm(time_check, tm); rtc_time_to_tm(time_check, tm);
...@@ -94,19 +100,12 @@ static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm) ...@@ -94,19 +100,12 @@ static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
* then wait for 100ms before writing to the time register to be * then wait for 100ms before writing to the time register to be
* sure that the data will be taken into account. * sure that the data will be taken into account.
*/ */
spin_lock_irqsave(&rtc->lock, flags); mutex_lock(&rtc->mutex_time);
rtc_delayed_write(0, rtc, RTC_STATUS); rtc_delayed_write(0, rtc, RTC_STATUS);
spin_unlock_irqrestore(&rtc->lock, flags);
msleep(100); msleep(100);
spin_lock_irqsave(&rtc->lock, flags);
rtc_delayed_write(time, rtc, RTC_TIME); rtc_delayed_write(time, rtc, RTC_TIME);
mutex_unlock(&rtc->mutex_time);
spin_unlock_irqrestore(&rtc->lock, flags);
out: out:
return ret; return ret;
} }
...@@ -230,6 +229,7 @@ static __init int armada38x_rtc_probe(struct platform_device *pdev) ...@@ -230,6 +229,7 @@ static __init int armada38x_rtc_probe(struct platform_device *pdev)
return -ENOMEM; return -ENOMEM;
spin_lock_init(&rtc->lock); spin_lock_init(&rtc->lock);
mutex_init(&rtc->mutex_time);
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc"); res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
rtc->regs = devm_ioremap_resource(&pdev->dev, res); rtc->regs = devm_ioremap_resource(&pdev->dev, res);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment