Skip to contentSkip to conversation

PM / s2idle: Clear _TIF_POLLING_NRFLAG before suspend to idle

PATCH Superseded Chen Yu 3 participants 6 messages Jun 16, 2020 – Jun 23, 2020 CPU idle time management framework

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

PATCH v2 1/1 PM / s2idle: Clear _TIF_POLLING_NRFLAG before suspend to idle

Suspend to idle was found to not work on Goldmont CPU recently. And the issue was triggered due to:

1. On Goldmont the CPU in idle can only be woken up via IPIs, not POLLING mode:
Commit 08e237fa56a1 ("x86/cpu: Add workaround for MONITOR instruction erratum on Goldmont based CPUs")
2. When the CPU is entering suspend to idle process, the _TIF_POLLING_NRFLAG is kept on, due to cpuidle_enter_s2idle() doesn't properly match call_cpuidle().
3. Commit b2a02fc43a1f ("smp: Optimize send_call_function_single_ipi()") makes use of _TIF_POLLING_NRFLAG to avoid sending IPIs to idle CPUs.
4. As a result, some IPIs related functions might not work well during suspend to idle on Goldmont. For example, one suspected victim:
tick_unfreeze() -> timekeeping_resume() -> hrtimers_resume() -> clock_was_set() -> on_each_cpu() might wait forever, because the IPIs will not be sent to the CPUs which are sleeping with _TIF_POLLING_NRFLAG set, and Goldmont CPU could not be woken up by only setting _TIF_NEED_RESCHED on the monitor address.

I don't find a way in Ubuntu to update the firmware of Goldmont and check if the issue was gone, a fix patch would do no harm. Clear the _TIF_POLLING_NRFLAG flag before entering suspend to idle, and let the driver's enter_s2idle() to decide whether to set _TIF_POLLING_NRFLAG or not. So that to avoid the scenario described above and keep the context consistent with before. Also adjust the naming to be consistent with call_cpuidle().

Fixes b2a02fc43a1f ("smp: Optimize send_call_function_single_ipi()")
Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>

---
v2: According to Peter's review, v1 is racy, if someone already
set TIF_NEED_RESCHED this patch just clear POLLING and go to sleep. Check TIF_NEED_RESCHED before entering suspend to idle and adjust the naming to be consistent with call_cpuidle().
--

 drivers/cpuidle/cpuidle.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/cpuidle/cpuidle.c +13 -3
index c149d9e20dfd..b003767abebd 100644
@@ -13,6 +13,7 @@
 #include <linux/mutex.h>
 #include <linux/sched.h>
 #include <linux/sched/clock.h>
+#include <linux/sched/idle.h>
 #include <linux/notifier.h>
 #include <linux/pm_qos.h>
 #include <linux/cpu.h>
@@ -133,8 +134,8 @@ int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 }
 
 #ifdef CONFIG_SUSPEND
-static void enter_s2idle_proper(struct cpuidle_driver *drv,
-				struct cpuidle_device *dev, int index)
+static void s2idle_enter(struct cpuidle_driver *drv,
+			 struct cpuidle_device *dev, int index)
 {
 	ktime_t time_start, time_end;
 
@@ -168,6 +169,15 @@ static void enter_s2idle_proper(struct cpuidle_driver *drv,
 	dev->states_usage[index].s2idle_usage++;
 }
 
+static int call_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+		       int index)
+{
+	if (!current_clr_polling_and_test())
+		s2idle_enter(drv, dev, index);
+
+	return index;
+}
+
 /**
  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
  * @drv: cpuidle driver for the given CPU.
@@ -187,7 +197,7 @@ int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
 	 */
 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
 	if (index > 0)
-		enter_s2idle_proper(drv, dev, index);
+		call_s2idle(drv, dev, index);
 
 	return index;
 }
Signature · 1 line
2.17.1
#1 lore raw
Quoted 32 lines · Chen Yu

Suspend to idle was found to not work on Goldmont CPU recently. And the issue was triggered due to:

1. On Goldmont the CPU in idle can only be woken up via IPIs, not POLLING mode:
Commit 08e237fa56a1 ("x86/cpu: Add workaround for MONITOR instruction erratum on Goldmont based CPUs")
2. When the CPU is entering suspend to idle process, the _TIF_POLLING_NRFLAG is kept on, due to cpuidle_enter_s2idle() doesn't properly match call_cpuidle().
3. Commit b2a02fc43a1f ("smp: Optimize send_call_function_single_ipi()") makes use of _TIF_POLLING_NRFLAG to avoid sending IPIs to idle CPUs.
4. As a result, some IPIs related functions might not work well during suspend to idle on Goldmont. For example, one suspected victim:
tick_unfreeze() -> timekeeping_resume() -> hrtimers_resume() -> clock_was_set() -> on_each_cpu() might wait forever, because the IPIs will not be sent to the CPUs which are sleeping with _TIF_POLLING_NRFLAG set, and Goldmont CPU could not be woken up by only setting _TIF_NEED_RESCHED on the monitor address.

I don't find a way in Ubuntu to update the firmware of Goldmont and check if the issue was gone, a fix patch would do no harm. Clear the _TIF_POLLING_NRFLAG flag before entering suspend to idle, and let the driver's enter_s2idle() to decide whether to set _TIF_POLLING_NRFLAG or not. So that to avoid the scenario described above and keep the context consistent with before. Also adjust the naming to be consistent with call_cpuidle().

Quoted 7 lines
Fixes b2a02fc43a1f ("smp: Optimize send_call_function_single_ipi()")
Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>

Peter, any more comments here?

Quoted 33 lines

---
v2: According to Peter's review, v1 is racy, if someone already
set TIF_NEED_RESCHED this patch just clear POLLING and go to sleep. Check TIF_NEED_RESCHED before entering suspend to idle and adjust the naming to be consistent with call_cpuidle().
--

 drivers/cpuidle/cpuidle.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/cpuidle/cpuidle.c +3 -2
index c149d9e20dfd..b003767abebd 100644
@@ -13,6 +13,7 @@
 #include <linux/mutex.h>
 #include <linux/sched.h>
 #include <linux/sched/clock.h>
+#include <linux/sched/idle.h>
 #include <linux/notifier.h>
 #include <linux/pm_qos.h>
 #include <linux/cpu.h>
@@ -133,8 +134,8 @@ int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 }
 
 #ifdef CONFIG_SUSPEND
-static void enter_s2idle_proper(struct cpuidle_driver *drv,
-                               struct cpuidle_device *dev, int index)
+static void s2idle_enter(struct cpuidle_driver *drv,
+                        struct cpuidle_device *dev, int index)
 {
        ktime_t time_start, time_end;
 

drivers/cpuidle/cpuidle.c static void enter_s2idle_proper(struct cpuidle_driver *drv, · line 178

Quoted 10 lines
drivers/cpuidle/cpuidle.c +7 -0
@@ -168,6 +169,15 @@ static void enter_s2idle_proper(struct cpuidle_driver *drv,
        dev->states_usage[index].s2idle_usage++;
 }
 
+static int call_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+                      int index)
+{
+       if (!current_clr_polling_and_test())
+               s2idle_enter(drv, dev, index);
+
+       return index;

Is the value returned here used at all?

drivers/cpuidle/cpuidle.c int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) · line 200

Quoted 11 lines
drivers/cpuidle/cpuidle.c +3 -1
+}
+
 /**
  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
  * @drv: cpuidle driver for the given CPU.
@@ -187,7 +197,7 @@ int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
         */
        index = find_deepest_state(drv, dev, U64_MAX, 0, true);
        if (index > 0)
-               enter_s2idle_proper(drv, dev, index);
+               call_s2idle(drv, dev, index);

I'm wondering why this can't be

    if (index > 0 && !current_clr_polling_and_test())
            enter_s2idle_proper(drv, dev, index);
Quoted 3 lines
drivers/cpuidle/cpuidle.c +0 -1
        return index;
 }
--
author
#2 lore raw

Hi Rafael,
On Mon, Jun 22, 2020 at 06:19:35PM +0200, Rafael J. Wysocki wrote:
[cut]

Quoted 8 lines

drivers/cpuidle/cpuidle.c static void enter_s2idle_proper(struct cpuidle_driver *drv, · line 178

Quoted 5 lines
drivers/cpuidle/cpuidle.c +5 -0
+{
+       if (!current_clr_polling_and_test())
+               s2idle_enter(drv, dev, index);
+
+       return index;

Is the value returned here used at all?

It is not used for now IMO.

Quoted 11 lines

drivers/cpuidle/cpuidle.c int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) · line 200

Quoted 5 lines
         */
        index = find_deepest_state(drv, dev, U64_MAX, 0, true);
        if (index > 0)
drivers/cpuidle/cpuidle.c +1 -1
-               enter_s2idle_proper(drv, dev, index);
+               call_s2idle(drv, dev, index);

I'm wondering why this can't be

    if (index > 0 && !current_clr_polling_and_test())
            enter_s2idle_proper(drv, dev, index);

Yes, it should be simpler, but I guess Peter was trying to make call_s2idle() consistent with call_cpuidle(), and also s2idle_enter() is analogous to cpuidle_enter().

Thanks,
Chenyu

#5 lore raw
In reply to Rafael J. Wysocki
Quoted 9 lines · Rafael J. Wysocki
Quoted 7 lines
Fixes b2a02fc43a1f ("smp: Optimize send_call_function_single_ipi()")
Cc "Rafael J. Wysocki" <[email protected]>
Cc Len Brown <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>

Peter, any more comments here?

Only that the whole s2idle stuff could do with a cleanup :-)

Quoted 24 lines

drivers/cpuidle/cpuidle.c static void enter_s2idle_proper(struct cpuidle_driver *drv, · line 178

Quoted 7 lines
drivers/cpuidle/cpuidle.c +7 -0
+static int call_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+                      int index)
+{
+       if (!current_clr_polling_and_test())
+               s2idle_enter(drv, dev, index);
+
+       return index;

Is the value returned here used at all?

drivers/cpuidle/cpuidle.c int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) · line 200

Quoted 11 lines
drivers/cpuidle/cpuidle.c +3 -1
+}
+
 /**
  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
  * @drv: cpuidle driver for the given CPU.
@@ -187,7 +197,7 @@ int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
         */
        index = find_deepest_state(drv, dev, U64_MAX, 0, true);
        if (index > 0)
-               enter_s2idle_proper(drv, dev, index);
+               call_s2idle(drv, dev, index);

I'm wondering why this can't be

Quoted 2 lines
    if (index > 0 && !current_clr_polling_and_test())
            enter_s2idle_proper(drv, dev, index);

Works for me. Some Wysocki guy wrote much of it, best ask him :-)

The thing that confused me is that all this is way different from the normal idle path and didn't keep the invariants.

Ideally; much of that gets folded back into the normal patch somehow.

#3 lore raw
In reply to Chen Yu
Quoted 19 lines · Chen Yu

Hi Rafael,
On Mon, Jun 22, 2020 at 06:19:35PM +0200, Rafael J. Wysocki wrote:
[cut]

Quoted 8 lines

drivers/cpuidle/cpuidle.c static void enter_s2idle_proper(struct cpuidle_driver *drv, · line 178

Quoted 5 lines
drivers/cpuidle/cpuidle.c +5 -0
+{
+       if (!current_clr_polling_and_test())
+               s2idle_enter(drv, dev, index);
+
+       return index;

Is the value returned here used at all?

It is not used for now IMO.

Quoted 6 lines
Quoted 5 lines
         */
        index = find_deepest_state(drv, dev, U64_MAX, 0, true);
        if (index > 0)
drivers/cpuidle/cpuidle.c +1 -1
-               enter_s2idle_proper(drv, dev, index);
+               call_s2idle(drv, dev, index);
Quoted 8 lines
Quoted 5 lines

I'm wondering why this can't be

    if (index > 0 && !current_clr_polling_and_test())
            enter_s2idle_proper(drv, dev, index);

Yes, it should be simpler, but I guess Peter was trying to make call_s2idle() consistent with call_cpuidle(), and also s2idle_enter() is analogous to cpuidle_enter().

So IMO it would be better to do the simplest fix first and then do the cleanup on top of it.

Thanks!

author
#4 lore raw
Quoted 25 lines · Rafael J. Wysocki
Quoted 24 lines · Chen Yu

Hi Rafael,
On Mon, Jun 22, 2020 at 06:19:35PM +0200, Rafael J. Wysocki wrote:
[cut]

Quoted 8 lines

drivers/cpuidle/cpuidle.c static void enter_s2idle_proper(struct cpuidle_driver *drv, · line 178

Quoted 5 lines
drivers/cpuidle/cpuidle.c +5 -0
+{
+       if (!current_clr_polling_and_test())
+               s2idle_enter(drv, dev, index);
+
+       return index;

Is the value returned here used at all?

It is not used for now IMO.

Quoted 11 lines

drivers/cpuidle/cpuidle.c int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) · line 200

Quoted 5 lines
         */
        index = find_deepest_state(drv, dev, U64_MAX, 0, true);
        if (index > 0)
drivers/cpuidle/cpuidle.c +1 -1
-               enter_s2idle_proper(drv, dev, index);
+               call_s2idle(drv, dev, index);

I'm wondering why this can't be

    if (index > 0 && !current_clr_polling_and_test())
            enter_s2idle_proper(drv, dev, index);
Quoted 7 lines
Quoted 3 lines

Yes, it should be simpler, but I guess Peter was trying to make call_s2idle() consistent with call_cpuidle(), and also s2idle_enter() is analogous to cpuidle_enter().

So IMO it would be better to do the simplest fix first and then do the cleanup on top of it.

Okay, I'll do that.

Thanks,
Chenyu