RE: [PATCH 1/3] alarmtimer: Replace the spinlock rtcdev_lock withmutex

From: Liu, Chuansheng
Date: Thu Nov 01 2012 - 23:40:04 EST


> It's safe to do in any code path which wants to use rtcdev:
>
> if (!rtcdev)
> return -ENODEV:
> do_something(rtcdev);
>
Really thanks your analysis and sharing, learn something.
And preparing a new patch with your comments for this point.

Subject: [PATCH] alarmtimer: Removing lock at alarmtimer_get_rtcdev()

[From Thomas comments]
"Protecting" the dereferencing of rtcdev with any kind of lock does
not provide any useful protection at all.

Moreover, alarmtimer_rtc_add_device just set the rtc device ONCE,
and the device will never go away and is never going to change, it's
completely pointless to have a lock in alarmtimer_get_rtcdev() at all.

So this patch remove the lock/unlock actions in alarmtimer_get_rtcdev().

Signed-off-by: liu chuansheng <chuansheng.liu@xxxxxxxxx>
---
kernel/time/alarmtimer.c | 21 +++++++--------------
1 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index f11d83b..eed3b26 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -62,14 +62,7 @@ static DEFINE_SPINLOCK(rtcdev_lock);
*/
struct rtc_device *alarmtimer_get_rtcdev(void)
{
- unsigned long flags;
- struct rtc_device *ret;
-
- spin_lock_irqsave(&rtcdev_lock, flags);
- ret = rtcdev;
- spin_unlock_irqrestore(&rtcdev_lock, flags);
-
- return ret;
+ return rtcdev;
}


@@ -224,10 +217,10 @@ static int alarmtimer_suspend(struct device *dev)
freezer_delta = ktime_set(0, 0);
spin_unlock_irqrestore(&freezer_delta_lock, flags);

- rtc = alarmtimer_get_rtcdev();
/* If we have no rtcdev, just return */
- if (!rtc)
+ if (!rtcdev)
return 0;
+ rtc = rtcdev;

/* Find the soonest timer to expire*/
for (i = 0; i < ALARM_NUMTYPE; i++) {
@@ -444,7 +437,7 @@ static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
{
clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;

- if (!alarmtimer_get_rtcdev())
+ if (!rtcdev)
return -ENOTSUPP;

return hrtimer_get_res(baseid, tp);
@@ -461,7 +454,7 @@ static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
{
struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];

- if (!alarmtimer_get_rtcdev())
+ if (!rtcdev)
return -ENOTSUPP;

*tp = ktime_to_timespec(base->gettime());
@@ -479,7 +472,7 @@ static int alarm_timer_create(struct k_itimer *new_timer)
enum alarmtimer_type type;
struct alarm_base *base;

- if (!alarmtimer_get_rtcdev())
+ if (!rtcdev)
return -ENOTSUPP;

if (!capable(CAP_WAKE_ALARM))
@@ -682,7 +675,7 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
int ret = 0;
struct restart_block *restart;

- if (!alarmtimer_get_rtcdev())
+ if (!rtcdev)
return -ENOTSUPP;

if (!capable(CAP_WAKE_ALARM))
--
1.7.0.4
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/