time: alarmtimer: Fix erroneous case of using 0 as an "invalid" initialization value
John Stultz
maintainerRFC PATCH 1/2 time: alarmtimer: Fix erroneous case of using 0 as an "invalid" initialization value
Michael reported seeing an error where alarmtimers would occasionally not wake the system up.
It was found that in alarmtimer_suspend() it was exiting via the:
if (min == 0)
return 0;check. This logic was from one of the early versions of the original alarmtimer patch, where we initialized min to 0, and then this check would exit early if we found no timers to expire (leaving min still at 0).
However, its possible for an alarmtimer to expire as we are checking it, leaving the calculated delta to be zero, and thus setting min to zero.
This is the result of my using 0 as an invalid time value which is clearly erroneous. Instead KTIME_MAX should have been used.
This patch, split out from a change originally suggested by Thomas Gleixner, changes the logic to instead use KTIME_MAX.
[jstultz: Forward ported to 6.2-rc, and split out just the
KTIME_MAX change]
--- kernel/time/alarmtimer.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/time/alarmtimer.c +5 -4
@@ -251,7 +251,7 @@ static int alarmtimer_suspend(struct device *dev) min = freezer_delta; expires = freezer_expires; type = freezer_alarmtype; - freezer_delta =0 ; + freezer_delta =KTIME_MAX ;spin_unlock_irqrestore (&freezer_delta_lock, flags); rtc =alarmtimer_get_rtcdev ();
@@ -271,13 +271,14 @@ static int alarmtimer_suspend(struct device *dev)if (!next)continue ; delta =ktime_sub (next->expires, base->get_ktime ()); -if (!min || (delta < min)) { +if (delta < min) { expires = next->expires; min = delta; type = i; } } -if (min ==0 ) +/* No timers to expire */ +if (min ==KTIME_MAX )return 0 ;if (ktime_to_ns (min) <2 *NSEC_PER_SEC ) {
@@ -503,7 +504,7 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type) delta =ktime_sub (absexp, base->get_ktime ());spin_lock_irqsave (&freezer_delta_lock, flags); -if (!freezer_delta || (delta < freezer_delta)) { +if (delta < freezer_delta) { freezer_delta = delta; freezer_expires = absexp; freezer_alarmtype = type;
Signature · 1 line
2.39.1.581.gbfd45094c4-goog
John Stultz
maintainerRFC PATCH 2/2 time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Instead of trying to handle the freezer waking up tasks from schedule() in nanosleep on alarmtimers explicitly, use TASK_FREEZABLE which marks the task freezable when it goes to schedule, which prevents the signal wakeup.
This allows for the freezer handling to be removed, simplifying the code.
[jstultz: Forward ported to 6.2-rc and split out from a separate
fix.]
--- kernel/time/alarmtimer.c | 53 ++-------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-)
kernel/time/alarmtimer.c +2 -51
@@ -49,14 +49,6 @@ static struct alarm_base {clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ]; -#if defined(CONFIG_POSIX_TIMERS ) || defined(CONFIG_RTC_CLASS ) -/* freezer information to handle clock_nanosleep triggered wakeups */ -static enum alarmtimer_type freezer_alarmtype; -static ktime_t freezer_expires; -static ktime_t freezer_delta; -static DEFINE_SPINLOCK (freezer_delta_lock); -#endif -#ifdef CONFIG_RTC_CLASS /* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer;
@@ -241,19 +233,12 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining);*/ static int alarmtimer_suspend (struct device *dev) { -ktime_t min, now, expires; +ktime_t now, expires, min =KTIME_MAX ;int i, ret, type;struct rtc_device *rtc;unsigned long flags;struct rtc_time tm; -spin_lock_irqsave (&freezer_delta_lock, flags); - min = freezer_delta; - expires = freezer_expires; - type = freezer_alarmtype; - freezer_delta =KTIME_MAX ; -spin_unlock_irqrestore (&freezer_delta_lock, flags); - rtc =alarmtimer_get_rtcdev ();/* If we have no rtcdev, just return */ if (!rtc)
@@ -480,38 +465,6 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)EXPORT_SYMBOL_GPL (alarm_forward_now);#ifdef CONFIG_POSIX_TIMERS - -static void alarmtimer_freezerset (ktime_t absexp,enum alarmtimer_type type) -{ -struct alarm_base *base; -unsigned long flags; -ktime_t delta; - -switch (type) { -case ALARM_REALTIME : - base = &alarm_bases[ALARM_REALTIME ]; - type =ALARM_REALTIME_FREEZER ; -break ; -case ALARM_BOOTTIME : - base = &alarm_bases[ALARM_BOOTTIME ]; - type =ALARM_BOOTTIME_FREEZER ; -break ; -default : -WARN_ONCE (1 ,"Invalid alarm type: %d\n" , type); -return ; - } - - delta =ktime_sub (absexp, base->get_ktime ()); - -spin_lock_irqsave (&freezer_delta_lock, flags); -if (delta < freezer_delta) { - freezer_delta = delta; - freezer_expires = absexp; - freezer_alarmtype = type; - } -spin_unlock_irqrestore (&freezer_delta_lock, flags); -} -/** * clock2alarm - helper that converts from clockid to alarmtypes * @clockid: clockid.
@@ -750,7 +703,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct restart_block *restart; alarm->data = (void *)current; do { - set_current_state(TASK_INTERRUPTIBLE); + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE); alarm_start(alarm, absexp); if (likely(alarm->data)) schedule();
@@ -765,8 +718,6 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,if (!alarm->data)return 0 ; -if (freezing (current)) -alarmtimer_freezerset (absexp, type); restart = ¤t->restart_block;if (restart->nanosleep.type !=TT_NONE ) {struct timespec64 rmt;
Signature · 1 line
2.39.1.581.gbfd45094c4-goog
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi John
Quoted 104 lines · John Stultz
Instead of trying to handle the freezer waking up tasks from
schedule()in nanosleep on alarmtimers explicitly, useTASK_FREEZABLEwhich marks the task freezable when it goes to schedule, which prevents the signal wakeup.This allows for the freezer handling to be removed, simplifying the code.
Cc Thomas Gleixner <[email protected]>Cc Stephen Boyd <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Michael <[email protected]>Cc Michael Trimarchi <[email protected]>Originally-by Thomas Gleixner <[email protected]>[jstultz: Forward ported to 6.2-rc and split out from a separate
fix.]Signed-off-by John Stultz <[email protected]>--- kernel/time/alarmtimer.c | 53 ++-------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-)kernel/time/alarmtimer.c +1
-48@@ -49,14 +49,6 @@ static struct alarm_base {clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ]; -#if defined(CONFIG_POSIX_TIMERS ) || defined(CONFIG_RTC_CLASS ) -/* freezer information to handle clock_nanosleep triggered wakeups */ -static enum alarmtimer_type freezer_alarmtype; -static ktime_t freezer_expires; -static ktime_t freezer_delta; -static DEFINE_SPINLOCK (freezer_delta_lock); -#endif -#ifdef CONFIG_RTC_CLASS /* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer;@@ -241,19 +233,12 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining);*/ static int alarmtimer_suspend (struct device *dev) { -ktime_t min, now, expires; +ktime_t now, expires, min =KTIME_MAX ;int i, ret, type;struct rtc_device *rtc;unsigned long flags;struct rtc_time tm; -spin_lock_irqsave (&freezer_delta_lock, flags); - min = freezer_delta; - expires = freezer_expires; - type = freezer_alarmtype; - freezer_delta =KTIME_MAX ; -spin_unlock_irqrestore (&freezer_delta_lock, flags); - rtc =alarmtimer_get_rtcdev ();/* If we have no rtcdev, just return */ if (!rtc)@@ -480,38 +465,6 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)EXPORT_SYMBOL_GPL (alarm_forward_now);#ifdef CONFIG_POSIX_TIMERS - -static void alarmtimer_freezerset (ktime_t absexp,enum alarmtimer_type type) -{ -struct alarm_base *base; -unsigned long flags; -ktime_t delta; - -switch (type) { -case ALARM_REALTIME : - base = &alarm_bases[ALARM_REALTIME ]; - type =ALARM_REALTIME_FREEZER ; -break ; -case ALARM_BOOTTIME : - base = &alarm_bases[ALARM_BOOTTIME ]; - type =ALARM_BOOTTIME_FREEZER ; -break ; -default : -WARN_ONCE (1 ,"Invalid alarm type: %d\n" , type); -return ; - } - - delta =ktime_sub (absexp, base->get_ktime ()); - -spin_lock_irqsave (&freezer_delta_lock, flags); -if (delta < freezer_delta) { - freezer_delta = delta; - freezer_expires = absexp; - freezer_alarmtype = type; - } -spin_unlock_irqrestore (&freezer_delta_lock, flags); -} -/** * clock2alarm - helper that converts from clockid to alarmtypes * @clockid: clockid.
kernel/time/alarmtimer.c static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp, · line 706
Quoted 5 lines
kernel/time/alarmtimer.c +1
-1@@ -750,7 +703,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct restart_block *restart; alarm->data = (void *)current; do { - set_current_state(TASK_INTERRUPTIBLE); + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE);
For kernel 5.10.x and lts is possible to use freezable_schedule and let this set_current_state as was before.
I have seen patch that introduce the new state but I suppose that in order to be compatible to stable this should be the change. Am I right?
Michael
Quoted 15 lines
kernel/time/alarmtimer.c +0
-3alarm_start (alarm, absexp);if (likely (alarm->data))schedule ();@@ -765,8 +718,6 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,if (!alarm->data)return 0 ; -if (freezing (current)) -alarmtimer_freezerset (absexp, type); restart = ¤t->restart_block;if (restart->nanosleep.type !=TT_NONE ) {struct timespec64 rmt; --2.39.1.581.gbfd45094c4-goog
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 16 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp, · line 706
Quoted 9 lines · John Stultz
/**
* clock2alarm - helper that converts from clockid to alarmtypes
* @clockid: clockid.kernel/time/alarmtimer.c +1
-1@@ -750,7 +703,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct restart_block *restart; alarm->data = (void *)current; do { - set_current_state(TASK_INTERRUPTIBLE); + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE);For kernel 5.10.x and lts is possible to use
freezable_scheduleand let thisset_current_stateas was before.
I have seen patch that introduce the new state but I suppose that in order to be compatible to stable this should be the change. Am I right?
We always work against upstream first and there freezable_schedule() is gone. Backports have to be sorted seperately.
Thanks,
tglx
Quoted 81 lines · John Stultz
Michael reported seeing an error where alarmtimers would occasionally not wake the system up.
It was found that in
alarmtimer_suspend()it was exiting via the:if (min == 0) return 0;check. This logic was from one of the early versions of the original alarmtimer patch, where we initialized min to 0, and then this check would exit early if we found no timers to expire (leaving min still at 0).
However, its possible for an alarmtimer to expire as we are checking it, leaving the calculated delta to be zero, and thus setting min to zero.
This is the result of my using 0 as an invalid time value which is clearly erroneous. Instead
KTIME_MAXshould have been used.This patch, split out from a change originally suggested by Thomas Gleixner, changes the logic to instead use
KTIME_MAX.Cc Thomas Gleixner <[email protected]>Cc Stephen Boyd <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Michael <[email protected]>Cc Michael Trimarchi <[email protected]>Reported-by Michael <[email protected]>Reported-by Michael Trimarchi <[email protected]>Fixes ff3ead96d17f ("timers: Introduce in-kernel alarm-timer interface")Originally-by Thomas Gleixner <[email protected]>[jstultz: Forward ported to 6.2-rc, and split out just the
KTIME_MAXchange]Signed-off-by John Stultz <[email protected]>--- kernel/time/alarmtimer.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)kernel/time/alarmtimer.c +5
-5@@ -251,7 +251,7 @@ static int alarmtimer_suspend(struct device *dev) min = freezer_delta; expires = freezer_expires; type = freezer_alarmtype; - freezer_delta =0 ; + freezer_delta =KTIME_MAX ;spin_unlock_irqrestore (&freezer_delta_lock, flags); rtc =alarmtimer_get_rtcdev ();@@ -271,13 +271,14 @@ static int alarmtimer_suspend(struct device *dev)if (!next)continue ; delta =ktime_sub (next->expires, base->get_ktime ()); -if (!min || (delta < min)) { +if (delta < min) { expires = next->expires; min = delta; type = i; } } -if (min ==0 ) +/* No timers to expire */ +if (min ==KTIME_MAX )return 0 ;if (ktime_to_ns (min) <2 *NSEC_PER_SEC ) {@@ -503,7 +504,7 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type) delta =ktime_sub (absexp, base->get_ktime ());spin_lock_irqsave (&freezer_delta_lock, flags); -if (!freezer_delta || (delta < freezer_delta)) { +if (delta < freezer_delta) { freezer_delta = delta; freezer_expires = absexp; freezer_alarmtype = type; --
Quoted 2 lines
2.39.1.581.gbfd45094c4-goog
I don't find regression on this
Michael
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to John StultzHi John
Quoted 122 lines · John Stultz
Instead of trying to handle the freezer waking up tasks from
schedule()in nanosleep on alarmtimers explicitly, useTASK_FREEZABLEwhich marks the task freezable when it goes to schedule, which prevents the signal wakeup.This allows for the freezer handling to be removed, simplifying the code.
Cc Thomas Gleixner <[email protected]>Cc Stephen Boyd <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Michael <[email protected]>Cc Michael Trimarchi <[email protected]>Originally-by Thomas Gleixner <[email protected]>[jstultz: Forward ported to 6.2-rc and split out from a separate
fix.]Signed-off-by John Stultz <[email protected]>--- kernel/time/alarmtimer.c | 53 ++-------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-)kernel/time/alarmtimer.c +2
-52@@ -49,14 +49,6 @@ static struct alarm_base {clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ]; -#if defined(CONFIG_POSIX_TIMERS ) || defined(CONFIG_RTC_CLASS ) -/* freezer information to handle clock_nanosleep triggered wakeups */ -static enum alarmtimer_type freezer_alarmtype; -static ktime_t freezer_expires; -static ktime_t freezer_delta; -static DEFINE_SPINLOCK (freezer_delta_lock); -#endif -#ifdef CONFIG_RTC_CLASS /* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer;@@ -241,19 +233,12 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining);*/ static int alarmtimer_suspend (struct device *dev) { -ktime_t min, now, expires; +ktime_t now, expires, min =KTIME_MAX ;int i, ret, type;struct rtc_device *rtc;unsigned long flags;struct rtc_time tm; -spin_lock_irqsave (&freezer_delta_lock, flags); - min = freezer_delta; - expires = freezer_expires; - type = freezer_alarmtype; - freezer_delta =KTIME_MAX ; -spin_unlock_irqrestore (&freezer_delta_lock, flags); - rtc =alarmtimer_get_rtcdev ();/* If we have no rtcdev, just return */ if (!rtc)@@ -480,38 +465,6 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)EXPORT_SYMBOL_GPL (alarm_forward_now);#ifdef CONFIG_POSIX_TIMERS - -static void alarmtimer_freezerset (ktime_t absexp,enum alarmtimer_type type) -{ -struct alarm_base *base; -unsigned long flags; -ktime_t delta; - -switch (type) { -case ALARM_REALTIME : - base = &alarm_bases[ALARM_REALTIME ]; - type =ALARM_REALTIME_FREEZER ; -break ; -case ALARM_BOOTTIME : - base = &alarm_bases[ALARM_BOOTTIME ]; - type =ALARM_BOOTTIME_FREEZER ; -break ; -default : -WARN_ONCE (1 ,"Invalid alarm type: %d\n" , type); -return ; - } - - delta =ktime_sub (absexp, base->get_ktime ()); - -spin_lock_irqsave (&freezer_delta_lock, flags); -if (delta < freezer_delta) { - freezer_delta = delta; - freezer_expires = absexp; - freezer_alarmtype = type; - } -spin_unlock_irqrestore (&freezer_delta_lock, flags); -} -/** * clock2alarm - helper that converts from clockid to alarmtypes * @clockid: clockid. @@ -750,7 +703,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct restart_block *restart; alarm->data = (void *)current; do { - set_current_state(TASK_INTERRUPTIBLE); + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE); alarm_start(alarm, absexp); if (likely(alarm->data)) schedule();@@ -765,8 +718,6 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,if (!alarm->data)return 0 ; -if (freezing (current)) -alarmtimer_freezerset (absexp, type); restart = ¤t->restart_block;if (restart->nanosleep.type !=TT_NONE ) {struct timespec64 rmt; --
Quoted 2 lines
2.39.1.581.gbfd45094c4-goog
I have changed the alarm test to check some corner case
periodic_alarm
Start time (CLOCK_REALTIME_ALARM)[ 85.624819] alarmtimer_enqueue: called
: 94:865096467
Setting alarm for every 4 seconds
Starting suspend loops
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds) [ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HERE
I have the dequeue but not an enquee of the periodic alarm. I was thinking that create a periodic time of 4 seconds
and have the first alarm on suspend will always guarantee the re-arm it but it's not working as I expect
Michael
[ 89.767735] Freezing remaining freezable tasks completed (elapsed 0.003 seconds) [ 89.775626] printk: Suspending console(s) (use no_console_suspend to debug)
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 2 lines · Michael Nazzareno Trimarchi
I have changed the alarm test to check some corner case
Could you tell us please which test did you change and what the change is?
Quoted 14 lines
periodic_alarm
Start time (CLOCK_REALTIME_ALARM)[ 85.624819]alarmtimer_enqueue: called
: 94:865096467
Setting alarm for every 4 seconds
Starting suspend loops[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds) [ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HERE
Quoted 4 lines
I have the dequeue but not an enquee of the periodic alarm. I was thinking that create a periodic time of 4 seconds
and have the first alarm on suspend will always guarantee the re-arm it but it's not working as I expect
Again. You are not telling what you expect. It depends on how the timer is set up whether the timer is self rearmed or not.
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi Thomas
Quoted 7 lines · Thomas Gleixner
Quoted 2 lines · Michael Nazzareno Trimarchi
I have changed the alarm test to check some corner case
Could you tell us please which test did you change and what the change is?
if (timer_create(CLOCK_REALTIME_ALARM, &se, &tm1) == -1) {
printf("timer_create failed, %s unsupported?\n",
clockstring(alarm_clock_id));
exit(1);
}
clock_gettime(alarm_clock_id, &start_time);
printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id),
start_time.tv_sec, start_time.tv_nsec);
printf("Setting alarm for every %i seconds\n", SUSPEND_SECS);
its1.it_value = start_time;
its1.it_value.tv_sec += 4;
/* Empiric value for get in between a freeze task and fire of the timer */
its1.it_value.tv_nsec += 132079666;
its1.it_interval.tv_sec = 4;
its1.it_interval.tv_nsec = 0;
timer_settime(tm1, TIMER_ABSTIME, &its1, &its2);
printf("Starting suspend loops\n");
while (1) {
int ret;
sleep(4);
system("echo mem > /sys/power/state");
}Quoted 14 lines
Quoted 14 lines
periodic_alarm
Start time (CLOCK_REALTIME_ALARM)[ 85.624819]alarmtimer_enqueue: called
: 94:865096467
Setting alarm for every 4 seconds
Starting suspend loops[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds) [ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HERE
Quoted 8 lines
Quoted 4 lines
I have the dequeue but not an enquee of the periodic alarm. I was thinking that create a periodic time of 4 seconds
and have the first alarm on suspend will always guarantee the re-arm it but it's not working as I expectAgain. You are not telling what you expect. It depends on how the timer is set up whether the timer is self rearmed or not.
Posted the pseudo code. As far as I understand, the timer periodic is re-armed in get_signal do_work_pending->do_signal()->get_signal(), then in the posix timer code the enqueue_alarm is called. All the timers
used from suspend are coming from the expiration list that contains only the enqueue alarm
My test case is a single core, arm and with only one REAL_TIME_ALARM periodic timer created.
Michael
Quoted 3 lines
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi
Quoted 12 lines · Michael Nazzareno Trimarchi
Hi Thomas
Quoted 7 lines · Thomas Gleixner
Quoted 2 lines · Michael Nazzareno Trimarchi
I have changed the alarm test to check some corner case
Could you tell us please which test did you change and what the change is?
There are no changes in the kernel apart pr_info on enqueue dequeue and fired call
in alarmtimer.c. linux master branch sha 38f8ccde04a3fa317b51b05e63c3cb57e1641931
and both patches applied
time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
time: alarmtimer: Fix erroneous case of using 0 as an "invalid" initialization value
Michael
Quoted 64 lines
if (timer_create(CLOCK_REALTIME_ALARM, &se, &tm1) == -1) { printf("timer_create failed, %s unsupported?\n", clockstring(alarm_clock_id)); exit(1); } clock_gettime(alarm_clock_id, &start_time); printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id), start_time.tv_sec, start_time.tv_nsec); printf("Setting alarm for every %i seconds\n", SUSPEND_SECS); its1.it_value = start_time; its1.it_value.tv_sec += 4; /* Empiric value for get in between a freeze task and fire of the timer */ its1.it_value.tv_nsec += 132079666; its1.it_interval.tv_sec = 4; its1.it_interval.tv_nsec = 0; timer_settime(tm1, TIMER_ABSTIME, &its1, &its2); printf("Starting suspend loops\n"); while (1) { int ret; sleep(4); system("echo mem > /sys/power/state"); }Quoted 14 lines
Quoted 14 lines
periodic_alarm
Start time (CLOCK_REALTIME_ALARM)[ 85.624819]alarmtimer_enqueue: called
: 94:865096467
Setting alarm for every 4 seconds
Starting suspend loops[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds) [ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HEREQuoted 8 lines
Quoted 4 lines
I have the dequeue but not an enquee of the periodic alarm. I was thinking that create a periodic time of 4 seconds
and have the first alarm on suspend will always guarantee the re-arm it but it's not working as I expectAgain. You are not telling what you expect. It depends on how the timer is set up whether the timer is self rearmed or not.
Posted the pseudo code. As far as I understand, the timer periodic is re-armed in
get_signaldo_work_pending->do_signal()->get_signal(), then in the posix timer code theenqueue_alarmis called. All the timers
used from suspend are coming from the expiration list that contains only the enqueue alarmMy test case is a single core, arm and with only one
REAL_TIME_ALARMperiodic timer created.Michael
Quoted 3 lines
Thanks,
tglx
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Michael!
Quoted 18 lines · Michael Nazzareno Trimarchi
Quoted 16 lines · Michael Nazzareno Trimarchi
Quoted 15 lines · Thomas Gleixner
Quoted 14 lines
Starting suspend loops
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds) [ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HEREI have the dequeue but not an enquee of the periodic alarm. I was thinking that create a periodic time of 4 seconds
and have the first alarm on suspend will always guarantee the re-arm it but it's not working as I expect
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Again. You are not telling what you expect. It depends on how the timer is set up whether the timer is self rearmed or not.
Posted the pseudo code. As far as I understand, the timer periodic is re-armed in
get_signaldo_work_pending->do_signal()->get_signal(), then in the posix timer code theenqueue_alarmis called. All the timers used from suspend are coming from the expiration list that contains only the enqueue alarm.
Let me try to decode the above.
Yes, periodic timers are re-armed when the signal is delivered, but that happens way later after the system resumed.
Here is what I observe:
[ 27.349352] alarmtimer_enqueue()
U: Before SUSPEND
[ 31.353228] PM: suspend entry (s2idle) [ 31.388857] Filesystems sync: 0.033 seconds [ 31.418427] Freezing user space processes [ 31.422406] Freezing user space processes completed (elapsed 0.002 seconds) [ 31.425435] OOM killer disabled. [ 31.426833] Freezing remaining freezable tasks [ 31.429838] Freezing remaining freezable tasks completed (elapsed 0.001 seconds) [ 31.432922] printk: Suspending console(s) (use no_console_suspend to debug) [ 31.435912] alarmtimer alarmtimer.0.auto: PM: dpm_run_callback(): platform_pm_suspend+0x0/0x50 returns -16 [ 31.435954] alarmtimer alarmtimer.0.auto: PM: failed to suspend: error -16
That means the RTC interrupt was raised before the system was able to suspend.
[ 31.436077] PM: Some devices failed to suspend, or early wake event detected [ 31.444270] OOM killer enabled. [ 31.445011] Restarting tasks ... done. [ 31.446820] random: crng reseeded on system resumption [ 31.466019] PM: suspend exit
[ 31.480283] alarmtimer_fired() [ 31.481403] alarmtimer_dequeue() <- Signal queued
[ 31.482596] alarmtimer_rearm()_ <- Signal delivery [ 31.483713] alarmtimer_enqueue()
U: ALRM signal received <- User space signal handler
U: Post Suspend <- system("echo .... >") returns
That's 6.2 + John's patches.
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi Thomas
Quoted 53 lines · Thomas Gleixner
Michael!
Quoted 18 lines · Michael Nazzareno Trimarchi
Quoted 16 lines · Michael Nazzareno Trimarchi
Quoted 15 lines · Thomas Gleixner
Quoted 14 lines
Starting suspend loops
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds) [ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HEREI have the dequeue but not an enquee of the periodic alarm. I was thinking that create a periodic time of 4 seconds
and have the first alarm on suspend will always guarantee the re-arm it but it's not working as I expectQuoted 8 lines
Quoted 8 lines
Quoted 2 lines
Again. You are not telling what you expect. It depends on how the timer is set up whether the timer is self rearmed or not.
Posted the pseudo code. As far as I understand, the timer periodic is re-armed in
get_signaldo_work_pending->do_signal()->get_signal(), then in the posix timer code theenqueue_alarmis called. All the timers used from suspend are coming from the expiration list that contains only the enqueue alarm.Let me try to decode the above.
Yes, periodic timers are re-armed when the signal is delivered, but that happens way later after the system resumed.
Here is what I observe:
[ 27.349352] alarmtimer_enqueue()U: Before SUSPEND
[ 31.353228] PM: suspend entry (s2idle) [ 31.388857] Filesystems sync: 0.033 seconds [ 31.418427] Freezing user space processes [ 31.422406] Freezing user space processes completed (elapsed 0.002 seconds) [ 31.425435] OOM killer disabled. [ 31.426833] Freezing remaining freezable tasks [ 31.429838] Freezing remaining freezable tasks completed (elapsed 0.001 seconds) [ 31.432922] printk: Suspending console(s) (use no_console_suspend to debug) [ 31.435912] alarmtimer alarmtimer.0.auto: PM: dpm_run_callback(): platform_pm_suspend+0x0/0x50 returns -16 [ 31.435954] alarmtimer alarmtimer.0.auto: PM: failed to suspend: error -16
Quoted 1 line
That means the RTC interrupt was raised before the system was able to suspend.
if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
return -EBUSY;
}I think that above happens to you. So it means that you are too close to this limit, can be?
Yes but the alarm for me was set to be fired just before freezing. Is this a valid scenario? I can retest on 6.2
Let's say that I set an alarm to be fired just before the userspace freeze happens. If I'm close to
it then then process will not process the signal to enquene again the alarm in the list and then during
alarm suspend the list will be empty for the above.
So your suggestion is that something is wrong with my environment so I will do the same on top of 6.2
Michael
Quoted 22 lines
[ 31.436077] PM: Some devices failed to suspend, or early wake event detected [ 31.444270] OOM killer enabled. [ 31.445011] Restarting tasks ... done. [ 31.446820] random: crng reseeded on system resumption [ 31.466019] PM: suspend exit[ 31.480283] alarmtimer_fired() [ 31.481403] alarmtimer_dequeue() <- Signal queued[ 31.482596] alarmtimer_rearm()_ <- Signal delivery [ 31.483713] alarmtimer_enqueue()U: ALRM signal received <- User space signal handler
U: Post Suspend <- system("echo .... >") returnsThat's 6.2 + John's patches.
Thanks,
tglx
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Michael!
Quoted 18 lines · Michael Nazzareno Trimarchi
Quoted 15 lines · Thomas Gleixner
[ 27.349352] alarmtimer_enqueue()U: Before SUSPEND
[ 31.353228] PM: suspend entry (s2idle) [ 31.388857] Filesystems sync: 0.033 seconds [ 31.418427] Freezing user space processes [ 31.422406] Freezing user space processes completed (elapsed 0.002 seconds) [ 31.425435] OOM killer disabled. [ 31.426833] Freezing remaining freezable tasks [ 31.429838] Freezing remaining freezable tasks completed (elapsed 0.001 seconds) [ 31.432922] printk: Suspending console(s) (use no_console_suspend to debug) [ 31.435912] alarmtimer alarmtimer.0.auto: PM: dpm_run_callback(): platform_pm_suspend+0x0/0x50 returns -16 [ 31.435954] alarmtimer alarmtimer.0.auto: PM: failed to suspend: error -16Quoted 1 line
That means the RTC interrupt was raised before the system was able to suspend.
Quoted 7 lines
if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { pm_wakeup_event(dev, 2 * MSEC_PER_SEC); return -EBUSY; }I think that above happens to you. So it means that you are too close to this limit, can be?
Maybe.
Quoted 2 lines
Yes but the alarm for me was set to be fired just before freezing. Is this a valid scenario?
Sure why not?
Quoted 4 lines
Let's say that I set an alarm to be fired just before the userspace freeze happens. If I'm close to it then then process will not process the signal to enquene again the alarm in the list and then during alarm suspend the list will be empty for the above.
Halfways, but slowly your explanations start to make sense. Here is the dmesg output you provided:
Quoted 4 lines
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds)
User space tasks are frozen now.
Quoted 4 lines
[ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HERE
Here fires the underlying hrtimer before devices are suspended, so the sig_sendqueue() cannot wake up the task because task->state == TASK_FROZEN, which means the signal wont be handled and the timer wont be rearmed until the task is thawed.
And as you correctly observed the alarmtimer_suspend() path won't see a pending timer anymore because it is dequeued.
So precisely the time between freeze(alarmtask) and alarmtimer_suspend() is a gaping hole which guarantees lost wakeups.
That's completely unrelated to Johns patches. That hole has been there forever.
I assume that this horrible freezer hackery was supposed to plug that hole, but that gem is not solving anything as far as I understand what it is doing. I'm still failing to understand what it is supposed to solve, but that's a different story.
Aside of that I can clearly reproduce the issue, now that I understand what you are trying to tell, on plain 6.2 without and with John's cleanup.
Something like the below plugs the hole reliably.
Thanks,
tglx
---
kernel/time/alarmtimer.c +55 -1
@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/suspend.h> #include <linux/time_namespace.h> #include "posix-timers.h"
@@ -176,6 +177,7 @@ static void alarmtimer_dequeue(struct al alarm->state &= ~ALARMTIMER_STATE_ENQUEUED ; } +static atomic_t alarmtimer_wakeup;/** * alarmtimer_fired - Handles alarm hrtimer being fired.
@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +spin_lock_irqsave (&base->lock, flags);alarmtimer_dequeue (base, alarm);spin_unlock_irqrestore (&base->lock, flags);
@@ -244,6 +248,16 @@ static int alarmtimer_suspend(struct devif (!rtc)return 0 ; +/* +* Handle wakeups which happened between the start of suspend and +* now as those wakeups might have tried to wake up a frozen task +* which means they are not longer in the alarm timer list. +*/ +if (atomic_read (&alarmtimer_wakeup)) { +pm_wakeup_event (dev,0 ); +return -EBUSY ; + } +/* Find the soonest timer to expire*/ for (i =0 ; i <ALARM_NUMTYPE ; i++) {struct alarm_base *base = &alarm_bases[i];
@@ -296,6 +310,31 @@ static int alarmtimer_resume(struct devi return 0; } +static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 ); +break ; + } +return NOTIFY_DONE ; +} + +static struct notifier_block alarmtimer_pm_notifier = { + .notifier_call = alarmtimer_pm_notifier_fn, +}; + +static inline int alarmtimer_register_pm_notifier (void ) +{ +return register_pm_notifier (&alarmtimer_pm_notifier); +} + +static inline void alarmtimer_unregister_pm_notifier (void ) +{ +unregister_pm_notifier (&alarmtimer_pm_notifier); +}#else static int alarmtimer_suspend (struct device *dev) {
@@ -306,6 +345,15 @@ static int alarmtimer_resume(struct devi {return 0 ; } + +static inline int alarmtimer_register_pm_notifier (void ) +{ +return 0 ; +} + +static inline void alarmtimer_unregister_pm_notifier (void ) +{ +}#endif static void
@@ -904,11 +952,17 @@ static int __init alarmtimer_init(void)if (error)return error; - error =platform_driver_register (&alarmtimer_driver); + error =alarmtimer_register_pm_notifier ();if (error) goto out_if; + error =platform_driver_register (&alarmtimer_driver); +if (error) + goto out_pm; +return 0 ; +out_pm: +alarmtimer_unregister_pm_notifier (); out_if:alarmtimer_rtc_interface_remove ();return error;
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi
Quoted 62 lines · Thomas Gleixner
Michael!
Quoted 18 lines · Michael Nazzareno Trimarchi
Quoted 15 lines · Thomas Gleixner
[ 27.349352] alarmtimer_enqueue()U: Before SUSPEND
[ 31.353228] PM: suspend entry (s2idle) [ 31.388857] Filesystems sync: 0.033 seconds [ 31.418427] Freezing user space processes [ 31.422406] Freezing user space processes completed (elapsed 0.002 seconds) [ 31.425435] OOM killer disabled. [ 31.426833] Freezing remaining freezable tasks [ 31.429838] Freezing remaining freezable tasks completed (elapsed 0.001 seconds) [ 31.432922] printk: Suspending console(s) (use no_console_suspend to debug) [ 31.435912] alarmtimer alarmtimer.0.auto: PM: dpm_run_callback(): platform_pm_suspend+0x0/0x50 returns -16 [ 31.435954] alarmtimer alarmtimer.0.auto: PM: failed to suspend: error -16Quoted 1 line
That means the RTC interrupt was raised before the system was able to suspend.
Quoted 7 lines
if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { pm_wakeup_event(dev, 2 * MSEC_PER_SEC); return -EBUSY; }I think that above happens to you. So it means that you are too close to this limit, can be?
Maybe.
Quoted 2 lines
Yes but the alarm for me was set to be fired just before freezing. Is this a valid scenario?
Sure why not?
Quoted 4 lines
Let's say that I set an alarm to be fired just before the userspace freeze happens. If I'm close to it then then process will not process the signal to enquene again the alarm in the list and then during alarm suspend the list will be empty for the above.
Halfways, but slowly your explanations start to make sense. Here is the dmesg output you provided:
Quoted 4 lines
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds)User space tasks are frozen now.
Quoted 4 lines
[ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HEREHere fires the underlying hrtimer before devices are suspended, so the
sig_sendqueue()cannot wake up the task becausetask->state==TASK_FROZEN, which means the signal wont be handled and the timer wont be rearmed until the task is thawed.
Quoted 9 lines
And as you correctly observed the
alarmtimer_suspend()path won't see a pending timer anymore because it is dequeued.So precisely the time between freeze(alarmtask) and
alarmtimer_suspend()is a gaping hole which guarantees lost wakeups.That's completely unrelated to Johns patches. That hole has been there forever.
That was clear, but I was trying to test the race here.
Quoted 5 lines
I assume that this horrible freezer hackery was supposed to plug that hole, but that gem is not solving anything as far as I understand what it is doing. I'm still failing to understand what it is supposed to solve, but that's a different story.
Yes, cleaning up was good.
Quoted 6 lines
Aside of that I can clearly reproduce the issue, now that I understand what you are trying to tell, on plain 6.2 without and with John's cleanup.
Something like the below plugs the hole reliably.
Some comments below
Quoted 23 lines
Thanks,
tglx
---kernel/time/alarmtimer.c +2
-0@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/suspend.h> #include <linux/time_namespace.h> #include "posix-timers.h" @@ -176,6 +177,7 @@ static void alarmtimer_dequeue(struct al alarm->state &= ~ALARMTIMER_STATE_ENQUEUED ; } +static atomic_t alarmtimer_wakeup;/** * alarmtimer_fired - Handles alarm hrtimer being fired.
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 5 lines
kernel/time/alarmtimer.c +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +
ptr->it_active = 0;
if (ptr->it_interval) {
atomic_inc(&alarmtimer_wakeup);
si_private = ++ptr->it_requeue_pending;
}Should I not go to the alarm_handle_timer? and only if it's a periodic one?
Michael
Quoted 95 lines
kernel/time/alarmtimer.c +51
-1spin_lock_irqsave (&base->lock, flags);alarmtimer_dequeue (base, alarm);spin_unlock_irqrestore (&base->lock, flags);@@ -244,6 +248,16 @@ static int alarmtimer_suspend(struct devif (!rtc)return 0 ; +/* +* Handle wakeups which happened between the start of suspend and +* now as those wakeups might have tried to wake up a frozen task +* which means they are not longer in the alarm timer list. +*/ +if (atomic_read (&alarmtimer_wakeup)) { +pm_wakeup_event (dev,0 ); +return -EBUSY ; + } +/* Find the soonest timer to expire*/ for (i =0 ; i <ALARM_NUMTYPE ; i++) {struct alarm_base *base = &alarm_bases[i];@@ -296,6 +310,31 @@ static int alarmtimer_resume(struct devi return 0; } +static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 ); +break ; + } +return NOTIFY_DONE ; +} + +static struct notifier_block alarmtimer_pm_notifier = { + .notifier_call = alarmtimer_pm_notifier_fn, +}; + +static inline int alarmtimer_register_pm_notifier (void ) +{ +return register_pm_notifier (&alarmtimer_pm_notifier); +} + +static inline void alarmtimer_unregister_pm_notifier (void ) +{ +unregister_pm_notifier (&alarmtimer_pm_notifier); +}#else static int alarmtimer_suspend (struct device *dev) {@@ -306,6 +345,15 @@ static int alarmtimer_resume(struct devi {return 0 ; } + +static inline int alarmtimer_register_pm_notifier (void ) +{ +return 0 ; +} + +static inline void alarmtimer_unregister_pm_notifier (void ) +{ +}#endif static void@@ -904,11 +952,17 @@ static int __init alarmtimer_init(void)if (error)return error; - error =platform_driver_register (&alarmtimer_driver); + error =alarmtimer_register_pm_notifier ();if (error) goto out_if; + error =platform_driver_register (&alarmtimer_driver); +if (error) + goto out_pm; +return 0 ; +out_pm: +alarmtimer_unregister_pm_notifier (); out_if:alarmtimer_rtc_interface_remove ();return error;
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Michael!
Quoted 15 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 7 lines · Thomas Gleixner
*
alarmtimer_fired- Handles alarm hrtimer being fired.kernel/time/alarmtimer.c +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +ptr->it_active = 0; if (ptr->it_interval) { atomic_inc(&alarmtimer_wakeup); si_private = ++ptr->it_requeue_pending; }
Quoted 2 lines
Should I not go to the
alarm_handle_timer? and only if it's a periodic one?
Why?
Any alarmtimer which hits that window has exactly the same problem.
It's not restricted to periodic timers. Why would a dropped one-shot wakeup be acceptable?
It's neither restricted to posix timers. If a clock_nanosleep(ALARM) expires in that window then the task wake up will just end up in the /dev/null bucket for the very same reason. Why would this be correct?
Hmm?
<GRMBL>
Quoted 3 lines
Michael
Quoted 1 line
kernel/time/alarmtimer.c +0
-0spin_lock_irqsave (&base->lock, flags);
<SNIP>Tons of wasted electrons</SNIP>
Can you please trim your replies?
</GRMBL>
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi
Quoted 19 lines · Thomas Gleixner
Michael!
Quoted 15 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 7 lines · Thomas Gleixner
*
alarmtimer_fired- Handles alarm hrtimer being fired.kernel/time/alarmtimer.c +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +ptr->it_active = 0; if (ptr->it_interval) { atomic_inc(&alarmtimer_wakeup); si_private = ++ptr->it_requeue_pending; }
Quoted 5 lines
Quoted 2 lines
Should I not go to the
alarm_handle_timer? and only if it's a periodic one?Why?
You are right. I will pay more attention to my reply.
Michael
Quoted 25 lines
Any alarmtimer which hits that window has exactly the same problem.
It's not restricted to periodic timers. Why would a dropped one-shot wakeup be acceptable?
It's neither restricted to posix timers. If a
clock_nanosleep(ALARM)expires in that window then the task wake up will just end up in the/dev/nullbucket for the very same reason. Why would this be correct?Hmm?
<GRMBL>
Quoted 3 lines
Michael
Quoted 1 line
kernel/time/alarmtimer.c +0
-0spin_lock_irqsave (&base->lock, flags);<SNIP>Tons of wasted electrons</SNIP>
Can you please trim your replies?
</GRMBL>
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to John StultzHi John
Quoted 122 lines · John Stultz
Instead of trying to handle the freezer waking up tasks from
schedule()in nanosleep on alarmtimers explicitly, useTASK_FREEZABLEwhich marks the task freezable when it goes to schedule, which prevents the signal wakeup.This allows for the freezer handling to be removed, simplifying the code.
Cc Thomas Gleixner <[email protected]>Cc Stephen Boyd <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Michael <[email protected]>Cc Michael Trimarchi <[email protected]>Originally-by Thomas Gleixner <[email protected]>[jstultz: Forward ported to 6.2-rc and split out from a separate
fix.]Signed-off-by John Stultz <[email protected]>--- kernel/time/alarmtimer.c | 53 ++-------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-)kernel/time/alarmtimer.c +2
-52@@ -49,14 +49,6 @@ static struct alarm_base {clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ]; -#if defined(CONFIG_POSIX_TIMERS ) || defined(CONFIG_RTC_CLASS ) -/* freezer information to handle clock_nanosleep triggered wakeups */ -static enum alarmtimer_type freezer_alarmtype; -static ktime_t freezer_expires; -static ktime_t freezer_delta; -static DEFINE_SPINLOCK (freezer_delta_lock); -#endif -#ifdef CONFIG_RTC_CLASS /* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer;@@ -241,19 +233,12 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining);*/ static int alarmtimer_suspend (struct device *dev) { -ktime_t min, now, expires; +ktime_t now, expires, min =KTIME_MAX ;int i, ret, type;struct rtc_device *rtc;unsigned long flags;struct rtc_time tm; -spin_lock_irqsave (&freezer_delta_lock, flags); - min = freezer_delta; - expires = freezer_expires; - type = freezer_alarmtype; - freezer_delta =KTIME_MAX ; -spin_unlock_irqrestore (&freezer_delta_lock, flags); - rtc =alarmtimer_get_rtcdev ();/* If we have no rtcdev, just return */ if (!rtc)@@ -480,38 +465,6 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)EXPORT_SYMBOL_GPL (alarm_forward_now);#ifdef CONFIG_POSIX_TIMERS - -static void alarmtimer_freezerset (ktime_t absexp,enum alarmtimer_type type) -{ -struct alarm_base *base; -unsigned long flags; -ktime_t delta; - -switch (type) { -case ALARM_REALTIME : - base = &alarm_bases[ALARM_REALTIME ]; - type =ALARM_REALTIME_FREEZER ; -break ; -case ALARM_BOOTTIME : - base = &alarm_bases[ALARM_BOOTTIME ]; - type =ALARM_BOOTTIME_FREEZER ; -break ; -default : -WARN_ONCE (1 ,"Invalid alarm type: %d\n" , type); -return ; - } - - delta =ktime_sub (absexp, base->get_ktime ()); - -spin_lock_irqsave (&freezer_delta_lock, flags); -if (delta < freezer_delta) { - freezer_delta = delta; - freezer_expires = absexp; - freezer_alarmtype = type; - } -spin_unlock_irqrestore (&freezer_delta_lock, flags); -} -/** * clock2alarm - helper that converts from clockid to alarmtypes * @clockid: clockid. @@ -750,7 +703,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct restart_block *restart; alarm->data = (void *)current; do { - set_current_state(TASK_INTERRUPTIBLE); + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE); alarm_start(alarm, absexp); if (likely(alarm->data)) schedule();@@ -765,8 +718,6 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,if (!alarm->data)return 0 ; -if (freezing (current)) -alarmtimer_freezerset (absexp, type); restart = ¤t->restart_block;if (restart->nanosleep.type !=TT_NONE ) {struct timespec64 rmt; --
Quoted 2 lines
2.39.1.581.gbfd45094c4-goog
I will test Thomas patch soon
Michael
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Michael Nazzareno TrimarchiHi Thomas
Quoted 23 lines · Michael Nazzareno Trimarchi
Hi
Quoted 19 lines · Thomas Gleixner
Michael!
Quoted 15 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 7 lines · Thomas Gleixner
*
alarmtimer_fired- Handles alarm hrtimer being fired.kernel/time/alarmtimer.c +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +ptr->it_active = 0; if (ptr->it_interval) { atomic_inc(&alarmtimer_wakeup); si_private = ++ptr->it_requeue_pending; }
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Should I not go to the
alarm_handle_timer? and only if it's a periodic one?Why?
You are right. I will pay more attention to my reply.
I get time to test it and if the system suspend to ram we need to catch:
case PM_SUSPEND_PREPARE:
case PM_POST_SUSPEND:
Michael
Quoted 27 lines
Michael
Quoted 25 lines
Any alarmtimer which hits that window has exactly the same problem.
It's not restricted to periodic timers. Why would a dropped one-shot wakeup be acceptable?
It's neither restricted to posix timers. If a
clock_nanosleep(ALARM)expires in that window then the task wake up will just end up in the/dev/nullbucket for the very same reason. Why would this be correct?Hmm?
<GRMBL>
Quoted 3 lines
Michael
Quoted 1 line
kernel/time/alarmtimer.c +0
-0spin_lock_irqsave (&base->lock, flags);<SNIP>Tons of wasted electrons</SNIP>
Can you please trim your replies?
</GRMBL>
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi
Quoted 37 lines · Michael Nazzareno Trimarchi
Hi Thomas
Quoted 23 lines · Michael Nazzareno Trimarchi
Hi
Quoted 19 lines · Thomas Gleixner
Michael!
Quoted 15 lines · Michael Nazzareno Trimarchi
Review static enum hrtimer_restart alarmtimer_f · line 200
Quoted 7 lines · Thomas Gleixner
*
alarmtimer_fired- Handles alarm hrtimer being fired.(quoted diff) +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_f int ret = HRTIMER_NORESTART; int restart = ALARMTIMER_NORESTART; + atomic_inc(&alarmtimer_wakeup); +ptr->it_active = 0; if (ptr->it_interval) { atomic_inc(&alarmtimer_wakeup); si_private = ++ptr->it_requeue_pending; }Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Should I not go to the
alarm_handle_timer? and only if it's a periodic one?Why?
You are right. I will pay more attention to my reply.
Quoted 25 lines
I get time to test it and if the system suspend to ram we need to catch:
case
PM_SUSPEND_PREPARE:
casePM_POST_SUSPEND:Michael
Quoted 18 lines
Michael
Quoted 16 lines
Any alarmtimer which hits that window has exactly the same problem.
It's not restricted to periodic timers. Why would a dropped one-shot wakeup be acceptable?
It's neither restricted to posix timers. If a
clock_nanosleep(ALARM)expires in that window then the task wake up will just end up in the/dev/nullbucket for the very same reason. Why would this be correct?Hmm?
<GRMBL>
Quoted 3 lines
Michael
Quoted 1 line
(quoted diff) +0
-0spin_lock_irqsave(&base->lock, flags);
[snip]
I have something like this
kernel/time/alarmtimer.c +1 -0
@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/suspend.h> #include <linux/time_namespace.h> #include "posix-timers.h"
@@ -176,6 +177,7 @@ static void alarmtimer_dequeue(struct alarm_base
*base, struct alarm *alarm)
alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
}
+static atomic_t alarmtimer_wakeup;
/**
* alarmtimer_fired - Handles alarm hrtimer being fired.
@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
int ret = HRTIMER_NORESTART;
int restart = ALARMTIMER_NORESTART;+ atomic_inc(&alarmtimer_wakeup);
+
spin_lock_irqsave(&base->lock, flags);
alarmtimer_dequeue(base, alarm);
spin_unlock_irqrestore(&base->lock, flags);
@@ -263,8 +267,18 @@ static int alarmtimer_suspend(struct device *dev)
}
}
/* No timers to expire */
- if (min == KTIME_MAX)
+ if (min == KTIME_MAX) {
+ /*
+ * Handle wakeups which happened between the start of suspend and
+ * now as those wakeups might have tried to wake up a frozen task
+ * which means they are no longer in the alarm timer list.
+ */+ if (atomic_read(&alarmtimer_wakeup)) {
+ pm_wakeup_event(dev, 0);
+ return -EBUSY;
+ }
return 0;
+ }
if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
@@ -296,6 +310,30 @@ static int alarmtimer_resume(struct device *dev)
return 0;}
+static int alarmtimer_pm_notifier_fn(struct notifier_block *bl, unsigned long state,
+ void *unused)
+{
+ switch (state) {
+ case PM_POST_SUSPEND:
+ atomic_set(&alarmtimer_wakeup, 0);
+ break;
+ }
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block alarmtimer_pm_notifier = {
+ .notifier_call = alarmtimer_pm_notifier_fn,
+};
+
+static inline int alarmtimer_register_pm_notifier(void) +{
+ return register_pm_notifier(&alarmtimer_pm_notifier);
+}
+
+static inline void alarmtimer_unregister_pm_notifier(void) +{
+ unregister_pm_notifier(&alarmtimer_pm_notifier);
+}
#else
static int alarmtimer_suspend(struct device *dev)
{
@@ -306,6 +344,15 @@ static int alarmtimer_resume(struct device *dev)
{
return 0; }
+
+static inline int alarmtimer_register_pm_notifier(void) +{
+ return 0;
+}
+
+static inline void alarmtimer_unregister_pm_notifier(void) +{
+}
#endif
static void
@@ -904,11 +951,18 @@ static int __init alarmtimer_init(void)
if (error)
return error;- error = platform_driver_register(&alarmtimer_driver);
+ error = alarmtimer_register_pm_notifier();
if (error)
goto out_if;+ error = platform_driver_register(&alarmtimer_driver);
+ if (error)
+ goto out_pm;
+
return 0;
+
+out_pm:
+ alarmtimer_unregister_pm_notifier();
out_if:
alarmtimer_rtc_interface_remove();
return error;Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi
Drop my last email. I have made a wrong assumption, below the fix I have done
Quoted 24 lines · Michael Nazzareno Trimarchi
Hi
Quoted 19 lines · Michael Nazzareno Trimarchi
Hi Thomas
Quoted 14 lines · Michael Nazzareno Trimarchi
Hi
Quoted 10 lines · Thomas Gleixner
Michael!
Quoted 6 lines · Michael Nazzareno Trimarchi
Quoted 5 lines · Thomas Gleixner
*
alarmtimer_fired- Handles alarm hrtimer being fired.(quoted diff) +0
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_f int ret = HRTIMER_NORESTART; int restart = ALARMTIMER_NORESTART;
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review static enum hrtimer_restart alarmtimer_f · line 200
Quoted 2 lines
(quoted diff) +2
-0+ atomic_inc(&alarmtimer_wakeup); +I have something like this
[snip]
Fixed now and tested on my side as below
kernel/time/alarmtimer.c +1 -0
@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/suspend.h> #include <linux/time_namespace.h> #include "posix-timers.h"
@@ -171,11 +172,11 @@ static void alarmtimer_dequeue(struct alarm_base
*base, struct alarm *alarm)
{
if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
return;
timerqueue_del(&base->timerqueue, &alarm->node);
alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;}
+static atomic_t alarmtimer_wakeup;
/**
* alarmtimer_fired - Handles alarm hrtimer being fired.
@@ -194,6 +195,8 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
int ret = HRTIMER_NORESTART;
int restart = ALARMTIMER_NORESTART;+ atomic_inc(&alarmtimer_wakeup);
+
spin_lock_irqsave(&base->lock, flags);
alarmtimer_dequeue(base, alarm);
spin_unlock_irqrestore(&base->lock, flags);
@@ -244,6 +247,16 @@ static int alarmtimer_suspend(struct device *dev)
if (!rtc)
return 0;+ /*
+ * Handle wakeups which happened between the start of suspend and + * now as those wakeups might have tried to wake up a frozen task + * which means they are not longer in the alarm timer list. + */
+ if (atomic_read(&alarmtimer_wakeup)) {
+ pm_wakeup_event(dev, 0);
+ return -EBUSY;
+ }
+
/* Find the soonest timer to expire*/
for (i = 0; i < ALARM_NUMTYPE; i++) {
struct alarm_base *base = &alarm_bases[i];
@@ -296,6 +309,33 @@ static int alarmtimer_resume(struct device *dev)
return 0;}
+static int alarmtimer_pm_notifier_fn(struct notifier_block *bl, unsigned long state,
+ void *unused)
+{
+ switch (state) {
+ case PM_SUSPEND_PREPARE:
+ case PM_POST_SUSPEND:
+ case PM_HIBERNATION_PREPARE:
+ case PM_POST_HIBERNATION:+ atomic_set(&alarmtimer_wakeup, 0);
+ break;
+ }
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block alarmtimer_pm_notifier = {
+ .notifier_call = alarmtimer_pm_notifier_fn,
+};
+
+static inline int alarmtimer_register_pm_notifier(void) +{
+ return register_pm_notifier(&alarmtimer_pm_notifier);
+}
+
+static inline void alarmtimer_unregister_pm_notifier(void) +{
+ unregister_pm_notifier(&alarmtimer_pm_notifier);
+}
#else
static int alarmtimer_suspend(struct device *dev)
{
@@ -306,6 +346,15 @@ static int alarmtimer_resume(struct device *dev)
{
return 0; }
+
+static inline int alarmtimer_register_pm_notifier(void) +{
+ return 0;
+}
+
+static inline void alarmtimer_unregister_pm_notifier(void) +{
+}
#endif
static void
@@ -904,11 +953,18 @@ static int __init alarmtimer_init(void)
if (error)
return error;- error = platform_driver_register(&alarmtimer_driver);
+ error = alarmtimer_register_pm_notifier();
if (error)
goto out_if;+ error = platform_driver_register(&alarmtimer_driver);
+ if (error)
+ goto out_pm;
+
return 0;
+
+out_pm:
+ alarmtimer_unregister_pm_notifier();
out_if:
alarmtimer_rtc_interface_remove();
return error;John Stultz
maintainerRe: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Thomas GleixnerQuoted 18 lines · Thomas Gleixner
Quoted 4 lines · Michael Nazzareno Trimarchi
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds)User space tasks are frozen now.
Quoted 4 lines
[ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HEREHere fires the underlying hrtimer before devices are suspended, so the
sig_sendqueue()cannot wake up the task becausetask->state==TASK_FROZEN, which means the signal wont be handled and the timer wont be rearmed until the task is thawed.
Quoted 6 lines
And as you correctly observed the
alarmtimer_suspend()path won't see a pending timer anymore because it is dequeued.So precisely the time between freeze(alarmtask) and
alarmtimer_suspend()is a gaping hole which guarantees lost wakeups.
Hey Michael, Thomas,
Sorry for being a little slow to respond the last few weeks. :(
So I've managed to reproduce the issue following Michael's instructions and your analysis, Thomas, looks right!
Though I'm a bit confused on the suggested solution:
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 203
Quoted 9 lines
kernel/time/alarmtimer.c +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +spin_lock_irqsave (&base->lock, flags);alarmtimer_dequeue (base, alarm);spin_unlock_irqrestore (&base->lock, flags);
Ok, so here we're incrementing the wakeup counter for each alarmtimer_fired call.
Quoted 7 lines
kernel/time/alarmtimer.c +3
-0@@ -244,6 +248,16 @@ static int alarmtimer_suspend(struct devif (!rtc)return 0 ; +/* +* Handle wakeups which happened between the start of suspend and +* now as those wakeups might have tried to wake up a frozen task
kernel/time/alarmtimer.c static int alarmtimer_suspend(struct dev · line 263
Quoted 10 lines
kernel/time/alarmtimer.c +7
-0+* which means they are not longer in the alarm timer list. +*/ +if (atomic_read (&alarmtimer_wakeup)) { +pm_wakeup_event (dev,0 ); +return -EBUSY ; + } +/* Find the soonest timer to expire*/ for (i =0 ; i <ALARM_NUMTYPE ; i++) {struct alarm_base *base = &alarm_bases[i];
And here we're bailing on alarmtimer_suspend if an alarmtimer_fired happened recently. Still looks okish.
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 10 lines
kernel/time/alarmtimer.c +10
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 ); +break ; + } +return NOTIFY_DONE ;
But here, we're setting the alarmtimer_wakeup count to zero if we get PM_HIBERNATION_PREPARE or PM_POST_HIBERNATION notifications? And Michael noted we need to add PM_SUSPEND_PREPARE and PM_POST_SUSPEND there for this to seemingly work.
This zeroing out the counter here feels a little sloppy, as it seems nothing prevents the notifier from racing with the other added logic.
If the issue is that when we're expiring timers in alarmtimer_fire(), a suspend event may come in midway after the alarmtimer_dequeue(), while the timer is running but before the alarmtimer_enqueue(), causing recurring timers to not be re-armed, it seems we probably should do the accounting fully in alarmtimer_fire(), doing an atomic_dec(&alarmtimer_wakeup) at the end of that function. That way we avoid suspending while running an alarmtimer, so the recurring timers will always be back on the timer list when we do suspend.
This sort of has a risk that if there's timers that run for too long, you'd prevent suspend for that period, but we already avoid suspending if there's a alarmtimer within two seconds in the future, so having alarmtimers recur quickly is inherently going to prevent you from suspending.
I'll send out a reworked patch here in a moment after I validate it as working.
thanks
-john
John Stultz
maintainerRe: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 20 lines · John Stultz
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 11 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +10
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 ); +break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.This zeroing out the counter here feels a little sloppy, as it seems nothing prevents the notifier from racing with the other added logic.
Quoted 8 lines
If the issue is that when we're expiring timers in
alarmtimer_fire(), a suspend event may come in midway after thealarmtimer_dequeue(), while the timer is running but before thealarmtimer_enqueue(), causing recurring timers to not be re-armed, it seems we probably should do the accounting fully inalarmtimer_fire(), doing anatomic_dec(&alarmtimer_wakeup)at the end of that function. That way we avoid suspending while running an alarmtimer, so the recurring timers will always be back on the timer list when we do suspend.
Ah. Scratch that. I was testing with my rework of the patch and still seeing trouble w/ Michael's reproducer.
My apologies, as I was mistaken that the race is in alarmtimer_fired between the dequeue and the enqueue. It's between the alarmtimer_fired() and the posixtimer_rearm() logic, where a suspend inbetween could prevent the timer from being rearmed.
This is messier as we need to allow the timer to fire and re-arm itself and block suspend happening inbetween, and since the re-arming logic is happening at the posixtimers abstraction level above the alarmtimers, its difficult to totally prevent that.
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
My only thought is this feels a little bit like its mirroring what the pm_wakeup_event() logic is supposed to do. Should we be adding a pm_wakeup_event() to alarmtimer_fired() to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?
thanks
-john
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.
Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?
The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
Thanks,
tglx
John Stultz
maintainerRe: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 20 lines · Thomas Gleixner
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.
Quoted 7 lines
Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
That said, it does work. :) In my testing, your approach has been reliable, so it has that going for it.
Quoted 8 lines
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
I took a brief stab at this, and one thing is the test needs to use the /sys/power/wakeup_count dance before suspending. However, I still had some cases where the recurring alarmtimer got lost, so I need to dig a bit more to understand what was going wrong there.
In the meantime, I'm ok with Thomas' approach, but we probably need some comment documentation that suggests it might be reworked in a cleaner way?
thanks
-john
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi John
Quoted 48 lines · John Stultz
Quoted 20 lines · Thomas Gleixner
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.Quoted 7 lines
Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
That said, it does work. :) In my testing, your approach has been reliable, so it has that going for it.
Quoted 8 lines
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
I took a brief stab at this, and one thing is the test needs to use the
/sys/power/wakeup_countdance before suspending. However, I still had some cases where the recurring alarmtimer got lost, so I need to dig a bit more to understand what was going wrong there.
Quoted 6 lines
In the meantime, I'm ok with Thomas' approach, but we probably need some comment documentation that suggests it might be reworked in a cleaner way?
thanks
-john
For now I have pushed to our internal devices this commit message
time: alarmtimer: Fix wakeup lost between freeze(alarmtask) and alarmtimer_suspend()
An alarm timer can happen in between a freeze and alarmtimer_suspend as
below output:
> [ 89.674127] PM: suspend entry (deep)
> [ 89.714916] Filesystems sync: 0.037 seconds
> [ 89.733594] Freezing user space processes
> [ 89.740680] Freezing user space processes completed (elapsed0.002 seconds)
User space tasks are frozen now.
> [ 89.748593] OOM killer disabled.
> [ 89.752257] Freezing remaining freezable tasks
> [ 89.756807] alarmtimer_fired: called
> [ 89.756831] alarmtimer_dequeue: called <---- HERE
Here fires the underlying hrtimer before devices are suspended, so the sig_sendqueue() cannot wake up the task because task->state == TASK_FROZEN, which means the signal won't be handled and the timer won't be rearmed until the task is thawed.
The alarmtimer_suspend() path won't see a pending timer anymore because it is dequeued.
So precisely the time between freeze(alarmtask) and alarmtimer_suspend() is a gaping hole which guarantees lost wakeups.
That hole has been there forever.
The old horrible freezer hackery was supposed to plug that hole, but that gem is not solving anything as far as I understand what it is doing.
Grab a bit from Thomas discussion
Michael
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Thomas GleixnerQuoted 105 lines · Thomas Gleixner
Michael!
Quoted 18 lines · Michael Nazzareno Trimarchi
Quoted 15 lines · Thomas Gleixner
[ 27.349352] alarmtimer_enqueue()U: Before SUSPEND
[ 31.353228] PM: suspend entry (s2idle) [ 31.388857] Filesystems sync: 0.033 seconds [ 31.418427] Freezing user space processes [ 31.422406] Freezing user space processes completed (elapsed 0.002 seconds) [ 31.425435] OOM killer disabled. [ 31.426833] Freezing remaining freezable tasks [ 31.429838] Freezing remaining freezable tasks completed (elapsed 0.001 seconds) [ 31.432922] printk: Suspending console(s) (use no_console_suspend to debug) [ 31.435912] alarmtimer alarmtimer.0.auto: PM: dpm_run_callback(): platform_pm_suspend+0x0/0x50 returns -16 [ 31.435954] alarmtimer alarmtimer.0.auto: PM: failed to suspend: error -16Quoted 1 line
That means the RTC interrupt was raised before the system was able to suspend.
Quoted 7 lines
if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { pm_wakeup_event(dev, 2 * MSEC_PER_SEC); return -EBUSY; }I think that above happens to you. So it means that you are too close to this limit, can be?
Maybe.
Quoted 2 lines
Yes but the alarm for me was set to be fired just before freezing. Is this a valid scenario?
Sure why not?
Quoted 4 lines
Let's say that I set an alarm to be fired just before the userspace freeze happens. If I'm close to it then then process will not process the signal to enquene again the alarm in the list and then during alarm suspend the list will be empty for the above.
Halfways, but slowly your explanations start to make sense. Here is the dmesg output you provided:
Quoted 4 lines
[ 89.674127] PM: suspend entry (deep) [ 89.714916] Filesystems sync: 0.037 seconds [ 89.733594] Freezing user space processes [ 89.740680] Freezing user space processes completed (elapsed 0.002 seconds)User space tasks are frozen now.
Quoted 4 lines
[ 89.748593] OOM killer disabled. [ 89.752257] Freezing remaining freezable tasks [ 89.756807] alarmtimer_fired: called [ 89.756831] alarmtimer_dequeue: called <---- HEREHere fires the underlying hrtimer before devices are suspended, so the
sig_sendqueue()cannot wake up the task becausetask->state==TASK_FROZEN, which means the signal wont be handled and the timer wont be rearmed until the task is thawed.And as you correctly observed the
alarmtimer_suspend()path won't see a pending timer anymore because it is dequeued.So precisely the time between freeze(alarmtask) and
alarmtimer_suspend()is a gaping hole which guarantees lost wakeups.That's completely unrelated to Johns patches. That hole has been there forever.
I assume that this horrible freezer hackery was supposed to plug that hole, but that gem is not solving anything as far as I understand what it is doing. I'm still failing to understand what it is supposed to solve, but that's a different story.
Aside of that I can clearly reproduce the issue, now that I understand what you are trying to tell, on plain 6.2 without and with John's cleanup.
Something like the below plugs the hole reliably.
Thanks,
tglx
---kernel/time/alarmtimer.c +2
-0@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/compat.h> #include <linux/module.h> +#include <linux/suspend.h> #include <linux/time_namespace.h> #include "posix-timers.h" @@ -176,6 +177,7 @@ static void alarmtimer_dequeue(struct al alarm->state &= ~ALARMTIMER_STATE_ENQUEUED ; } +static atomic_t alarmtimer_wakeup;/** * alarmtimer_fired - Handles alarm hrtimer being fired.
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 5 lines
kernel/time/alarmtimer.c +2
-0@@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +
This appears to be still somewhat racy, because the notifier can run at this point AFAICS.
Quoted 87 lines
kernel/time/alarmtimer.c +51
-1spin_lock_irqsave (&base->lock, flags);alarmtimer_dequeue (base, alarm);spin_unlock_irqrestore (&base->lock, flags);@@ -244,6 +248,16 @@ static int alarmtimer_suspend(struct devif (!rtc)return 0 ; +/* +* Handle wakeups which happened between the start of suspend and +* now as those wakeups might have tried to wake up a frozen task +* which means they are not longer in the alarm timer list. +*/ +if (atomic_read (&alarmtimer_wakeup)) { +pm_wakeup_event (dev,0 ); +return -EBUSY ; + } +/* Find the soonest timer to expire*/ for (i =0 ; i <ALARM_NUMTYPE ; i++) {struct alarm_base *base = &alarm_bases[i];@@ -296,6 +310,31 @@ static int alarmtimer_resume(struct devi return 0; } +static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 ); +break ; + } +return NOTIFY_DONE ; +} + +static struct notifier_block alarmtimer_pm_notifier = { + .notifier_call = alarmtimer_pm_notifier_fn, +}; + +static inline int alarmtimer_register_pm_notifier (void ) +{ +return register_pm_notifier (&alarmtimer_pm_notifier); +} + +static inline void alarmtimer_unregister_pm_notifier (void ) +{ +unregister_pm_notifier (&alarmtimer_pm_notifier); +}#else static int alarmtimer_suspend (struct device *dev) {@@ -306,6 +345,15 @@ static int alarmtimer_resume(struct devi {return 0 ; } + +static inline int alarmtimer_register_pm_notifier (void ) +{ +return 0 ; +} + +static inline void alarmtimer_unregister_pm_notifier (void ) +{ +}#endif static void@@ -904,11 +952,17 @@ static int __init alarmtimer_init(void)if (error)return error; - error =platform_driver_register (&alarmtimer_driver); + error =alarmtimer_register_pm_notifier ();if (error) goto out_if; + error =platform_driver_register (&alarmtimer_driver); +if (error) + goto out_pm; +return 0 ; +out_pm: +alarmtimer_unregister_pm_notifier (); out_if:alarmtimer_rtc_interface_remove ();return error;
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Thomas GleixnerQuoted 28 lines · Thomas Gleixner
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
Quoted 8 lines
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
The pm_wakeup_event() doesn't need the timeout, but it is conditional on using /sys/power/wakeup_count.
However, in any case it doesn't guarantee that the target user of the alarm timer will be able to handle the signal on time AFAICS. To my eyes, it is entirely possible for alarmtimer_fired() to run before /sys/power/wakeup_count is written to while the signal will be sent to the given task later, in which case there is no guarantee that the task will not be frozen in the meantime.
Moreover, I'm wondering if all alarm timers should always wake up the system from sleep or abort suspends in progress? If the answer is "no" here, it changes the problem at hand quite a bit.
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 39 lines · Rafael J. Wysocki
Quoted 28 lines · Thomas Gleixner
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
Quoted 8 lines
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
Quoted 7 lines
The
pm_wakeup_event()doesn't need the timeout, but it is conditional on using/sys/power/wakeup_count.However, in any case it doesn't guarantee that the target user of the alarm timer will be able to handle the signal on time AFAICS. To my eyes, it is entirely possible for
alarmtimer_fired()to run before/sys/power/wakeup_countis written to while the signal will be sent to
I actually should have said "read from" instead of "written to" here, sorry.
Quoted 6 lines
the given task later, in which case there is no guarantee that the task will not be frozen in the meantime.
Moreover, I'm wondering if all alarm timers should always wake up the system from sleep or abort suspends in progress? If the answer is "no" here, it changes the problem at hand quite a bit.
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to John StultzQuoted 39 lines · John Stultz
Quoted 20 lines · Thomas Gleixner
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.Quoted 7 lines
Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
That said, it does work. :) In my testing, your approach has been reliable, so it has that going for it.
Quoted 6 lines
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?
Quoted 5 lines
Quoted 2 lines
The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
I took a brief stab at this, and one thing is the test needs to use the
/sys/power/wakeup_countdance before suspending.
That's correct.
Quoted 3 lines
However, I still had some cases where the recurring alarmtimer got lost, so I need to dig a bit more to understand what was going wrong there.
I'm interested in that too, so if you have any conclusions, please let me know.
Quoted 3 lines
In the meantime, I'm ok with Thomas' approach, but we probably need some comment documentation that suggests it might be reworked in a cleaner way?
Well, in theory, the PM notifier can run in parallel with alarmtimer_fired() right after it has incremented the atomic var, can't it?
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Rafael J. WysockiQuoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Thomas Gleixner
kernel/time/alarmtimer.c +1
-0+static atomic_t alarmtimer_wakeup;/** * alarmtimer_fired - Handles alarm hrtimer being fired. @@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ;
Quoted 5 lines
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 2 lines
kernel/time/alarmtimer.c +2
-0+atomic_inc (&alarmtimer_wakeup); +This appears to be still somewhat racy, because the notifier can run at this point AFAICS.
Indeed it is. Let me think more about this.
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Quoted 10 lines · Thomas Gleixner
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Thomas Gleixner
kernel/time/alarmtimer.c +1
-0+static atomic_t alarmtimer_wakeup;/** * alarmtimer_fired - Handles alarm hrtimer being fired. @@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ;
Quoted 7 lines
Quoted 5 lines
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 2 lines
kernel/time/alarmtimer.c +2
-0+atomic_inc (&alarmtimer_wakeup); +This appears to be still somewhat racy, because the notifier can run at this point AFAICS.
Indeed it is. Let me think more about this.
All of this is inherently racy as there is zero feedback whether the event has been consumed or not. Making this feedback based is not necessarily trivial, but let me stare into that.
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Rafael J. WysockiHi Rafael
Quoted 55 lines · Rafael J. Wysocki
Quoted 39 lines · John Stultz
Quoted 20 lines · Thomas Gleixner
Quoted 17 lines · John Stultz
Quoted 8 lines · John Stultz
Quoted 8 lines · Michael Nazzareno Trimarchi
kernel/time/alarmtimer.c +7
-0+static int alarmtimer_pm_notifier_fn (struct notifier_block *bl,unsigned long state, +void *unused) +{ +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_POST_HIBERNATION : +atomic_set (&alarmtimer_wakeup,0 );Quoted 8 lines
kernel/time/alarmtimer.c static int alarmtimer_resume(struct devi · line 322
Quoted 3 lines
kernel/time/alarmtimer.c +3
-0+break ; + } +return NOTIFY_DONE ;But here, we're setting the
alarmtimer_wakeupcount to zero if we getPM_HIBERNATION_PREPAREorPM_POST_HIBERNATIONnotifications? And Michael noted we need to addPM_SUSPEND_PREPAREandPM_POST_SUSPENDthere for this to seemingly work.Quoted 7 lines
Yup. I missed those when sending out that hack.
Quoted 3 lines
So Thomas's notifier method of zeroing at the begining of suspend and tracking any wakeups after that point makes more sense now. It still feels a bit messy, but I'm not sure there's something better.
I'm not enthused about it either.
That said, it does work. :) In my testing, your approach has been reliable, so it has that going for it.
Quoted 6 lines
Quoted 5 lines
My only thought is this feels a little bit like its mirroring what the
pm_wakeup_event()logic is supposed to do. Should we be adding apm_wakeup_event()toalarmtimer_fired()to try to prevent suspend from occuring for 500ms or so after an alarmtimer has fired so there is enough time for it to be re-armed if needed?Quoted 5 lines
Quoted 2 lines
The question is whether this can be called unconditionally and how that interacts with the suspend logic. Rafael?
I took a brief stab at this, and one thing is the test needs to use the
/sys/power/wakeup_countdance before suspending.That's correct.
Quoted 3 lines
However, I still had some cases where the recurring alarmtimer got lost, so I need to dig a bit more to understand what was going wrong there.
I'm interested in that too, so if you have any conclusions, please let me know.
Quoted 7 lines
Quoted 3 lines
In the meantime, I'm ok with Thomas' approach, but we probably need some comment documentation that suggests it might be reworked in a cleaner way?
Well, in theory, the PM notifier can run in parallel with
alarmtimer_fired()right after it has incremented the atomic var, can't it?
Can we add a function like pm_get_state()?
So can we mark the suspend window using some state?
Michael
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to John StultzHi all
Quoted 17 lines · John Stultz
Instead of trying to handle the freezer waking up tasks from
schedule()in nanosleep on alarmtimers explicitly, useTASK_FREEZABLEwhich marks the task freezable when it goes to schedule, which prevents the signal wakeup.This allows for the freezer handling to be removed, simplifying the code.
Cc Thomas Gleixner <[email protected]>Cc Stephen Boyd <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Michael <[email protected]>Cc Michael Trimarchi <[email protected]>Originally-by Thomas Gleixner <[email protected]>
Quoted 4 lines
[jstultz: Forward ported to 6.2-rc and split out from a separate
fix.]Signed-off-by John Stultz <[email protected]>---
Is this land?
Michael
Quoted 103 lines
kernel/time/alarmtimer.c | 53 ++-------------------------------------- 1 file changed, 2 insertions(+), 51 deletions(-)kernel/time/alarmtimer.c +2
-52@@ -49,14 +49,6 @@ static struct alarm_base {clockid_t base_clockid; } alarm_bases[ALARM_NUMTYPE ]; -#if defined(CONFIG_POSIX_TIMERS ) || defined(CONFIG_RTC_CLASS ) -/* freezer information to handle clock_nanosleep triggered wakeups */ -static enum alarmtimer_type freezer_alarmtype; -static ktime_t freezer_expires; -static ktime_t freezer_delta; -static DEFINE_SPINLOCK (freezer_delta_lock); -#endif -#ifdef CONFIG_RTC_CLASS /* rtc timer and device for setting alarm wakeups at suspend */ static struct rtc_timer rtctimer;@@ -241,19 +233,12 @@ EXPORT_SYMBOL_GPL(alarm_expires_remaining);*/ static int alarmtimer_suspend (struct device *dev) { -ktime_t min, now, expires; +ktime_t now, expires, min =KTIME_MAX ;int i, ret, type;struct rtc_device *rtc;unsigned long flags;struct rtc_time tm; -spin_lock_irqsave (&freezer_delta_lock, flags); - min = freezer_delta; - expires = freezer_expires; - type = freezer_alarmtype; - freezer_delta =KTIME_MAX ; -spin_unlock_irqrestore (&freezer_delta_lock, flags); - rtc =alarmtimer_get_rtcdev ();/* If we have no rtcdev, just return */ if (!rtc)@@ -480,38 +465,6 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)EXPORT_SYMBOL_GPL (alarm_forward_now);#ifdef CONFIG_POSIX_TIMERS - -static void alarmtimer_freezerset (ktime_t absexp,enum alarmtimer_type type) -{ -struct alarm_base *base; -unsigned long flags; -ktime_t delta; - -switch (type) { -case ALARM_REALTIME : - base = &alarm_bases[ALARM_REALTIME ]; - type =ALARM_REALTIME_FREEZER ; -break ; -case ALARM_BOOTTIME : - base = &alarm_bases[ALARM_BOOTTIME ]; - type =ALARM_BOOTTIME_FREEZER ; -break ; -default : -WARN_ONCE (1 ,"Invalid alarm type: %d\n" , type); -return ; - } - - delta =ktime_sub (absexp, base->get_ktime ()); - -spin_lock_irqsave (&freezer_delta_lock, flags); -if (delta < freezer_delta) { - freezer_delta = delta; - freezer_expires = absexp; - freezer_alarmtype = type; - } -spin_unlock_irqrestore (&freezer_delta_lock, flags); -} -/** * clock2alarm - helper that converts from clockid to alarmtypes * @clockid: clockid. @@ -750,7 +703,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,struct restart_block *restart; alarm->data = (void *)current; do { - set_current_state(TASK_INTERRUPTIBLE); + set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE); alarm_start(alarm, absexp); if (likely(alarm->data)) schedule();@@ -765,8 +718,6 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,if (!alarm->data)return 0 ; -if (freezing (current)) -alarmtimer_freezerset (absexp, type); restart = ¤t->restart_block;if (restart->nanosleep.type !=TT_NONE ) {struct timespec64 rmt; --2.39.1.581.gbfd45094c4-goog
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
In reply to Thomas GleixnerHi Thomas
Quoted 18 lines · Thomas Gleixner
Quoted 16 lines · Thomas Gleixner
Quoted 14 lines · Rafael J. Wysocki
kernel/time/alarmtimer.c static enum hrtimer_restart alarmtimer_f · line 200
Quoted 10 lines · Thomas Gleixner
kernel/time/alarmtimer.c +3
-0+static atomic_t alarmtimer_wakeup;/** * alarmtimer_fired - Handles alarm hrtimer being fired. @@ -194,6 +196,8 @@ static enum hrtimer_restart alarmtimer_fint ret =HRTIMER_NORESTART ;int restart =ALARMTIMER_NORESTART ; +atomic_inc (&alarmtimer_wakeup); +This appears to be still somewhat racy, because the notifier can run at this point AFAICS.
Quoted 6 lines
Quoted 1 line
Indeed it is. Let me think more about this.
All of this is inherently racy as there is zero feedback whether the event has been consumed or not. Making this feedback based is not necessarily trivial, but let me stare into that.
Sorry to come back on this topic but I would like to know, if we can find a way to be sure
to not lost wakeup or if you have already addressed in some series
Michael
Quoted 4 lines
Thanks,
tglx
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Michael!
Quoted 6 lines · Michael Nazzareno Trimarchi
Quoted 5 lines · Thomas Gleixner
Quoted 4 lines
Quoted 2 lines
This appears to be still somewhat racy, because the notifier can run at this point AFAICS.
Indeed it is. Let me think more about this.
Quoted 7 lines
Quoted 4 lines
All of this is inherently racy as there is zero feedback whether the event has been consumed or not. Making this feedback based is not necessarily trivial, but let me stare into that.
Sorry to come back on this topic but I would like to know, if we can find a way to be sure
to not lost wakeup or if you have already addressed in some series
No. I did not take care of it as I got distracted with other important stuff. Feel free to take up the loose ends of this thread and make it work. I'm happy to help discussing design choices.
Thanks,
tglx
Re: [RFC][PATCH 2/2] time: alarmtimer: Use TASK_FREEZABLE to cleanup freezer handling
Hi Thomas
Quoted 14 lines · Thomas Gleixner
Michael!
Quoted 10 lines · Michael Nazzareno Trimarchi
Quoted 9 lines · Thomas Gleixner
Quoted 4 lines
Quoted 2 lines
This appears to be still somewhat racy, because the notifier can run at this point AFAICS.
Indeed it is. Let me think more about this.
All of this is inherently racy as there is zero feedback whether the event has been consumed or not. Making this feedback based is not necessarily trivial, but let me stare into that.
Quoted 8 lines
Quoted 3 lines
Sorry to come back on this topic but I would like to know, if we can find a way to be sure
to not lost wakeup or if you have already addressed in some seriesNo. I did not take care of it as I got distracted with other important stuff. Feel free to take up the loose ends of this thread and make it work. I'm happy to help discussing design choices.
I will pick it up this week, and see what I can do and propose some way.
Thank you for having time to reply
Michael
Quoted 3 lines
Thanks,
tglx
Signature · 11 lines
Michael Nazzareno Trimarchi Co-Founder & Chief Executive Officer M. +39 347 913 2170 [email protected] __________________________________ Amarula Solutions BV Joop Geesinkweg 125, 1114 AB, Amsterdam, NL T. +31 (0)85 111 9172 [email protected] www.amarulasolutions.com