diff options
| author | Thomas Gleixner <tglx@linutronix.de> | 2011-07-22 05:12:51 -0400 |
|---|---|---|
| committer | John Stultz <john.stultz@linaro.org> | 2011-07-26 17:49:59 -0400 |
| commit | 3c8bb90efb6e3105206e4aaa9127395feeda5492 (patch) | |
| tree | 9ff9771503c219677671bea7f4b2740661d1c5e9 | |
| parent | 53cc2820acbdbcc768675bfaff321f3a8680a317 (diff) | |
rtc: Fix hrtimer deadlock
Ben reported a lockup related to rtc. The lockup happens due to:
CPU0 CPU1
rtc_irq_set_state() __run_hrtimer()
spin_lock_irqsave(&rtc->irq_task_lock) rtc_handle_legacy_irq();
spin_lock(&rtc->irq_task_lock);
hrtimer_cancel()
while (callback_running);
So the running callback never finishes as it's blocked on
rtc->irq_task_lock.
Use hrtimer_try_to_cancel() instead and drop rtc->irq_task_lock while
waiting for the callback. Fix this for both rtc_irq_set_state() and
rtc_irq_set_freq().
Cc: stable@kernel.org
Reported-by: Ben Greear <greearb@candelatech.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Stultz <john.stultz@linaro.org>
| -rw-r--r-- | drivers/rtc/interface.c | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index b6bf57f25cc9..a1ba2caa8308 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c | |||
| @@ -636,6 +636,29 @@ void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) | |||
| 636 | } | 636 | } |
| 637 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); | 637 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); |
| 638 | 638 | ||
| 639 | static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled) | ||
| 640 | { | ||
| 641 | /* | ||
| 642 | * We always cancel the timer here first, because otherwise | ||
| 643 | * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); | ||
| 644 | * when we manage to start the timer before the callback | ||
| 645 | * returns HRTIMER_RESTART. | ||
| 646 | * | ||
| 647 | * We cannot use hrtimer_cancel() here as a running callback | ||
| 648 | * could be blocked on rtc->irq_task_lock and hrtimer_cancel() | ||
| 649 | * would spin forever. | ||
| 650 | */ | ||
| 651 | if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0) | ||
| 652 | return -1; | ||
| 653 | |||
| 654 | if (enabled) { | ||
| 655 | ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq); | ||
| 656 | |||
| 657 | hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); | ||
| 658 | } | ||
| 659 | return 0; | ||
| 660 | } | ||
| 661 | |||
| 639 | /** | 662 | /** |
| 640 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs | 663 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs |
| 641 | * @rtc: the rtc device | 664 | * @rtc: the rtc device |
| @@ -651,24 +674,21 @@ int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled | |||
| 651 | int err = 0; | 674 | int err = 0; |
| 652 | unsigned long flags; | 675 | unsigned long flags; |
| 653 | 676 | ||
| 677 | retry: | ||
| 654 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | 678 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
| 655 | if (rtc->irq_task != NULL && task == NULL) | 679 | if (rtc->irq_task != NULL && task == NULL) |
| 656 | err = -EBUSY; | 680 | err = -EBUSY; |
| 657 | if (rtc->irq_task != task) | 681 | if (rtc->irq_task != task) |
| 658 | err = -EACCES; | 682 | err = -EACCES; |
| 659 | if (err) | 683 | if (!err) { |
| 660 | goto out; | 684 | if (rtc_update_hrtimer(rtc, enabled) < 0) { |
| 661 | 685 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | |
| 662 | if (enabled) { | 686 | cpu_relax(); |
| 663 | ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); | 687 | goto retry; |
| 664 | hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); | 688 | } |
| 665 | } else { | 689 | rtc->pie_enabled = enabled; |
| 666 | hrtimer_cancel(&rtc->pie_timer); | ||
| 667 | } | 690 | } |
| 668 | rtc->pie_enabled = enabled; | ||
| 669 | out: | ||
| 670 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | 691 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 671 | |||
| 672 | return err; | 692 | return err; |
| 673 | } | 693 | } |
| 674 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); | 694 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); |
| @@ -690,20 +710,18 @@ int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq) | |||
| 690 | 710 | ||
| 691 | if (freq <= 0) | 711 | if (freq <= 0) |
| 692 | return -EINVAL; | 712 | return -EINVAL; |
| 693 | 713 | retry: | |
| 694 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | 714 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
| 695 | if (rtc->irq_task != NULL && task == NULL) | 715 | if (rtc->irq_task != NULL && task == NULL) |
| 696 | err = -EBUSY; | 716 | err = -EBUSY; |
| 697 | if (rtc->irq_task != task) | 717 | if (rtc->irq_task != task) |
| 698 | err = -EACCES; | 718 | err = -EACCES; |
| 699 | if (err == 0) { | 719 | if (!err) { |
| 700 | rtc->irq_freq = freq; | 720 | rtc->irq_freq = freq; |
| 701 | if (rtc->pie_enabled) { | 721 | if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) { |
| 702 | ktime_t period; | 722 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 703 | hrtimer_cancel(&rtc->pie_timer); | 723 | cpu_relax(); |
| 704 | period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); | 724 | goto retry; |
| 705 | hrtimer_start(&rtc->pie_timer, period, | ||
| 706 | HRTIMER_MODE_REL); | ||
| 707 | } | 725 | } |
| 708 | } | 726 | } |
| 709 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | 727 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
