Skip to contentSkip to conversation

x86, hotplug: Use hlt instead of mwait when resuming from hibernation

RFC PATCH In review Chen Yu 6 participants 21 messages Jun 25, 2016 – Oct 18, 2016

Subsystems (4)
lore
Share & tools
lore

Chen Yu

author
#0 lore raw

v2 cover x86, hotplug: Use hlt instead of mwait when resuming from hibernation

Currently it is reported that, when system is trying to resume from hibernation, the nonboot CPUs might be incorrectly woken up and hang there. The reason for this is because of inconsistent page tables across hibernation resume. To avoid this situation, use hlt instead of mwait to put nonboot CPUs in a more safe state and just let them watch the boot CPU to do all the things and wakes them up later.

The first three patches are preparation for the fourth one, please refer to [4/4] for detail.

Chen Yu (4):
PM / sleep: Avoid accessing frozen_cpus if it is NULL
PM / sleep: Introduce arch-specific hook for disable/enable nonboot
cpus
PM / hibernate: introduce a flag to indicate resuming from hibernation x86, hotplug: Use hlt instead of mwait when resuming from hibernation

 arch/x86/kernel/smpboot.c | 15 +++++++++++++++
 include/linux/cpu.h       |  2 ++
 include/linux/suspend.h   |  7 +++++++
 kernel/cpu.c              | 38 ++++++++++++++++++++++++++++++++++++++
 kernel/power/hibernate.c  |  3 +++
 5 files changed, 65 insertions(+)
Signature · 1 line
2.7.4
author
#1 lore raw

RFC PATCH v2 1/4 PM / sleep: Avoid accessing frozen_cpus if it is NULL

frozen_cpus might be NULL if the allocation in previous alloc_frozen_cpus failed, when CONFIG_CPUMASK_OFFSTACK is set.

This patch avoid accessing this cpumask if it is NULL.

---
 kernel/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/cpu.c +4 -0
index d948e44..d25266e 100644
@@ -1021,6 +1021,8 @@ int disable_nonboot_cpus(void)
 {
 	int cpu, first_cpu, error = 0;
 
+	if (frozen_cpus == NULL)
+		return -ENOMEM;
 	cpu_maps_update_begin();
 	first_cpu = cpumask_first(cpu_online_mask);
 	/*
@@ -1072,6 +1074,8 @@ void enable_nonboot_cpus(void)
 {
 	int cpu, error;
 
+	if (frozen_cpus == NULL)
+		return;
 	/* Allow everyone to use the CPU hotplug again */
 	cpu_maps_update_begin();
 	WARN_ON(--cpu_hotplug_disabled < 0);
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 1/4][RFC v2] PM / sleep: Avoid accessing frozen_cpus if it is NULL

Quoted 16 lines · Chen Yu

frozen_cpus might be NULL if the allocation in previous alloc_frozen_cpus failed, when CONFIG_CPUMASK_OFFSTACK is set.

This patch avoid accessing this cpumask if it is NULL.

---
 kernel/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/cpu.c +0 -0
index d948e44..d25266e 100644

kernel/cpu.c int disable_nonboot_cpus(void) · line 1028

Quoted 8 lines
kernel/cpu.c +2 -0
@@ -1021,6 +1021,8 @@ int disable_nonboot_cpus(void)
 {
 	int cpu, first_cpu, error = 0;
 
+	if (frozen_cpus == NULL)
+		return -ENOMEM;
 	cpu_maps_update_begin();
 	first_cpu = cpumask_first(cpu_online_mask);
 	/*

I'd say that whoever allocates frozen_cpus should just abort the hibernation if there's not enough memory for the operation...? This seems like checking for the problem too late.

Best regards,
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
author
#3 lore raw

RE: [PATCH 1/4][RFC v2] PM / sleep: Avoid accessing frozen_cpus if it is NULL

Hi,

Quoted 27 lines

-----Original Message-----
From: Pavel Machek [mailto:[email protected]]
Sent: Sunday, June 26, 2016 12:51 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Rafael J. Wysocki; Len Brown; Peter Zijlstra; H. Peter Anvin; Borislav Petkov; Brian Gerst; Thomas Gleixner; Ingo Molnar; Varun Koyyalagunta; [email protected]
Subject: Re: [PATCH 1/4][RFC v2] PM / sleep: Avoid accessing frozen_cpus if it is NULL

Quoted 16 lines · Chen Yu

frozen_cpus might be NULL if the allocation in previous alloc_frozen_cpus failed, when CONFIG_CPUMASK_OFFSTACK is set.

This patch avoid accessing this cpumask if it is NULL.

---
 kernel/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/cpu.c +0 -0
@@ -1021,6 +1021,8 @@ int disable_nonboot_cpus(void)  {
 	int cpu, first_cpu, error = 0;
 
Quoted 10 lines

kernel/cpu.c int disable_nonboot_cpus(void) · line 1028

Quoted 5 lines
kernel/cpu.c +2 -0
+	if (frozen_cpus == NULL)
+		return -ENOMEM;
 	cpu_maps_update_begin();
 	first_cpu = cpumask_first(cpu_online_mask);
 	/*

I'd say that whoever allocates frozen_cpus should just abort the hibernation if there's not enough memory for the operation...? This seems like checking for the problem too late.

The allocation of frozen_cpus is alloc_frozen_cpus, which is in core_initcall, and do_initcall during boot up seems not to care about the return value from these functions. So I think either we add the check in disable_nonboot_cpus, or we can set noresume = 1 and nohibernate = 1 if alloc_frozen_cpus fails.

thanks,
Yu

author
#4 lore raw

RE: [PATCH 1/4][RFC v2] PM / sleep: Avoid accessing frozen_cpus if it is NULL

In reply to Pavel Machek
Quoted 24 lines

-----Original Message-----
From: Chen, Yu C
Sent: Sunday, June 26, 2016 9:17 AM
To: 'Pavel Machek'
Cc: [email protected]; [email protected]; Rafael J. Wysocki; Len Brown; Peter Zijlstra; H. Peter Anvin; Borislav Petkov; Brian Gerst; Thomas Gleixner; Ingo Molnar; Varun Koyyalagunta; [email protected]
Subject: RE: [PATCH 1/4][RFC v2] PM / sleep: Avoid accessing frozen_cpus if it is NULL

Hi,

Quoted 11 lines

-----Original Message-----
From: Pavel Machek [mailto:[email protected]]
Sent: Sunday, June 26, 2016 12:51 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Rafael J. Wysocki; Len Brown; Peter Zijlstra; H. Peter Anvin; Borislav Petkov; Brian Gerst; Thomas Gleixner; Ingo Molnar; Varun Koyyalagunta; [email protected]
Subject: Re: [PATCH 1/4][RFC v2] PM / sleep: Avoid accessing frozen_cpus if it is NULL

[cut]

Quoted 10 lines
Quoted 5 lines

I'd say that whoever allocates frozen_cpus should just abort the hibernation if there's not enough memory for the operation...? This seems like checking for the problem too late.

The allocation of frozen_cpus is alloc_frozen_cpus, which is in core_initcall, and do_initcall during boot up seems not to care about the return value from these functions. So I think either we add the check in disable_nonboot_cpus, or we can set noresume = 1 and nohibernate = 1 if alloc_frozen_cpus fails.

Besides, disable_nonboot_cpus is not only used for hibernation, but also for suspend to ram, kexec, etc, so it might be better to check before we use it, thus to avoid introducing new flags IMO.

thanks,
Yu

author
#5 lore raw

RFC PATCH v2 2/4 PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

In reply to Chen Yu

There is requirement that we need to do some arch-specific operations before putting the nonboot CPUs offline/online. One of the requirements comes from the hibernation resume process on x86_64, we need to kick all the offlin-CPUs online and offline again, in order to put them in a safe state, thus to avoid possible unwilling wake up during hibernation resume.

---
 kernel/cpu.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
kernel/cpu.c +12 -0
index d25266e..ce6e5e4 100644
@@ -1017,6 +1017,16 @@ EXPORT_SYMBOL_GPL(cpu_up);
 #ifdef CONFIG_PM_SLEEP_SMP
 static cpumask_var_t frozen_cpus;
 
+void __weak arch_disable_nonboot_cpus_pre(void)
+{
+
+}
+
+void __weak arch_enable_nonboot_cpus_pre(void)
+{
+
+}
+
 int disable_nonboot_cpus(void)
 {
 	int cpu, first_cpu, error = 0;
@@ -1024,6 +1034,7 @@ int disable_nonboot_cpus(void)
 	if (frozen_cpus == NULL)
 		return -ENOMEM;
 	cpu_maps_update_begin();
+	arch_disable_nonboot_cpus_pre();
 	first_cpu = cpumask_first(cpu_online_mask);
 	/*
 	 * We take down all of the non-boot CPUs in one shot to avoid races
@@ -1078,6 +1089,7 @@ void enable_nonboot_cpus(void)
 		return;
 	/* Allow everyone to use the CPU hotplug again */
 	cpu_maps_update_begin();
+	arch_enable_nonboot_cpus_pre();
 	WARN_ON(--cpu_hotplug_disabled < 0);
 	if (cpumask_empty(frozen_cpus))
 		goto out;
Signature · 1 line
2.7.4
#6 lore raw

Re: [PATCH 2/4][RFC v2] PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

Quoted 7 lines · Chen Yu

There is requirement that we need to do some arch-specific operations before putting the nonboot CPUs offline/online. One of the requirements comes from the hibernation resume process on x86_64, we need to kick all the offlin-CPUs online and offline again, in order to put them in a safe state, thus to avoid possible unwilling wake up during hibernation resume.

You may want to check english here.

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
author
#7 lore raw

RE: [PATCH 2/4][RFC v2] PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

Hi,

Quoted 11 lines

-----Original Message-----
From: Pavel Machek [mailto:[email protected]]
Sent: Sunday, June 26, 2016 12:52 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Rafael J. Wysocki; Len Brown; Peter Zijlstra; H. Peter Anvin; Borislav Petkov; Brian Gerst; Thomas Gleixner; Ingo Molnar; Varun Koyyalagunta; [email protected]
Subject: Re: [PATCH 2/4][RFC v2] PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

On Sun 2016-06-26 00:18:41, Chen Yu wrote:

Quoted 8 lines
Quoted 6 lines

There is requirement that we need to do some arch-specific operations before putting the nonboot CPUs offline/online.
One of the requirements comes from the hibernation resume process on x86_64, we need to kick all the offlin-CPUs online and offline again, in order to put them in a safe state, thus to avoid possible unwilling wake up during hibernation resume.

You may want to check english here.

Ah, sorry, I'll rewrite the log, and [PATCH 4/4] might contain the detail of this issue.

thanks,
Yu

#8 lore raw

Re: [PATCH 2/4][RFC v2] PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

In reply to Chen Yu
Quoted 18 lines · Chen Yu

There is requirement that we need to do some arch-specific operations before putting the nonboot CPUs offline/online. One of the requirements comes from the hibernation resume process on x86_64, we need to kick all the offlin-CPUs online and offline again, in order to put them in a safe state, thus to avoid possible unwilling wake up during hibernation resume.

---
 kernel/cpu.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
kernel/cpu.c +0 -0
index d25266e..ce6e5e4 100644

kernel/cpu.c EXPORT_SYMBOL_GPL(cpu_up); · line 1020

Quoted 4 lines
kernel/cpu.c +1 -0
@@ -1017,6 +1017,16 @@ EXPORT_SYMBOL_GPL(cpu_up);
 #ifdef CONFIG_PM_SLEEP_SMP
 static cpumask_var_t frozen_cpus;
 
+void __weak arch_disable_nonboot_cpus_pre(void)

I don't like using __weak. It penalizes code size on architectures that don't hook these functions. My preferred pattern is:

include/linux/something.h:

#include <asm/something.h>

#ifndef arch_do_xyz
static inline void arch_do_xyz() {}
#endif

arch/whatever/asm/something.h:

extern void arch_do_xyz(); /* or static inline... */ #define arch_do_xyz

This is totally free for architectures that don't have the hooks and it can potentially be inlined on architectures that do have the hooks. Everyone wins except that it's about five additional lines of code.

--Andy

author
#9 lore raw

Re: [PATCH 2/4][RFC v2] PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

Hi Andy,

Quoted 37 lines · Andy Lutomirski
Quoted 18 lines · Chen Yu

There is requirement that we need to do some arch-specific operations before putting the nonboot CPUs offline/online. One of the requirements comes from the hibernation resume process on x86_64, we need to kick all the offlin-CPUs online and offline again, in order to put them in a safe state, thus to avoid possible unwilling wake up during hibernation resume.

---
kernel/cpu.c | 12 ++++++++++++

1 file changed, 12 insertions(+)
kernel/cpu.c +0 -0
index d25266e..ce6e5e4 100644

kernel/cpu.c EXPORT_SYMBOL_GPL(cpu_up); · line 1020

Quoted 4 lines
kernel/cpu.c +1 -0
@@ -1017,6 +1017,16 @@ EXPORT_SYMBOL_GPL(cpu_up);
 #ifdef CONFIG_PM_SLEEP_SMP
 static cpumask_var_t frozen_cpus;
 
+void __weak arch_disable_nonboot_cpus_pre(void)

I don't like using __weak. It penalizes code size on architectures that don't hook these functions. My preferred pattern is:

include/linux/something.h:

#include <asm/something.h>

#ifndef arch_do_xyz
static inline void arch_do_xyz() {}
#endif

arch/whatever/asm/something.h:

Quoted 8 lines

extern void arch_do_xyz(); /* or static inline... */ #define arch_do_xyz

This is totally free for architectures that don't have the hooks and it can potentially be inlined on architectures that do have the hooks. Everyone wins except that it's about five additional lines of code.

Thanks for your guidance, this patch was deprecated previously, but I'll take your advice the next time when writing similar patch.

Quoted 1 line

--Andy

Thanks,
Yu

#10 lore raw

Re: [PATCH 2/4][RFC v2] PM / sleep: Introduce arch-specific hook for disable/enable nonboot cpus

In reply to Andy Lutomirski
Quoted 37 lines · Andy Lutomirski
Quoted 18 lines · Chen Yu

There is requirement that we need to do some arch-specific operations before putting the nonboot CPUs offline/online. One of the requirements comes from the hibernation resume process on x86_64, we need to kick all the offlin-CPUs online and offline again, in order to put them in a safe state, thus to avoid possible unwilling wake up during hibernation resume.

---
kernel/cpu.c | 12 ++++++++++++

1 file changed, 12 insertions(+)
kernel/cpu.c +0 -0
index d25266e..ce6e5e4 100644

kernel/cpu.c EXPORT_SYMBOL_GPL(cpu_up); · line 1020

Quoted 4 lines
kernel/cpu.c +1 -0
@@ -1017,6 +1017,16 @@ EXPORT_SYMBOL_GPL(cpu_up);
 #ifdef CONFIG_PM_SLEEP_SMP
 static cpumask_var_t frozen_cpus;
 
+void __weak arch_disable_nonboot_cpus_pre(void)

I don't like using __weak. It penalizes code size on architectures that don't hook these functions. My preferred pattern is:

include/linux/something.h:

#include <asm/something.h>

#ifndef arch_do_xyz
static inline void arch_do_xyz() {}
#endif

arch/whatever/asm/something.h:

Quoted 7 lines

extern void arch_do_xyz(); /* or static inline... */ #define arch_do_xyz

This is totally free for architectures that don't have the hooks and it can potentially be inlined on architectures that do have the hooks. Everyone wins except that it's about five additional lines of code.

Well... 5 additional lines may be worse than few bytes in the object file...

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
author
#11 lore raw

RFC PATCH v2 3/4 PM / hibernate: introduce a flag to indicate resuming from hibernation

In reply to Chen Yu

Sometime we need to do some operations before resuming from hibernation, so introduce a flag to indicate this stage.

---
 include/linux/suspend.h  | 7 +++++++
 kernel/power/hibernate.c | 3 +++
 2 files changed, 10 insertions(+)
include/linux/suspend.h +7 -0
index 8b6ec7e..422e87a 100644
@@ -384,6 +384,12 @@ extern bool system_entering_hibernation(void);
 extern bool hibernation_available(void);
 asmlinkage int swsusp_save(void);
 extern struct pbe *restore_pblist;
+extern bool in_resume_hibernate;
+
+static inline bool hibernation_in_resume(void)
+{
+	return in_resume_hibernate;
+}
 #else /* CONFIG_HIBERNATION */
 static inline void register_nosave_region(unsigned long b, unsigned long e) {}
 static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
@@ -395,6 +401,7 @@ static inline void hibernation_set_ops(const struct platform_hibernation_ops *op
 static inline int hibernate(void) { return -ENOSYS; }
 static inline bool system_entering_hibernation(void) { return false; }
 static inline bool hibernation_available(void) { return false; }
+static inline bool hibernation_in_resume(void) { return false; }
 #endif /* CONFIG_HIBERNATION */
 
 /* Hibernation and suspend events */
kernel/power/hibernate.c +3 -0
index fca9254..41be909 100644
@@ -43,6 +43,7 @@ static char resume_file[256] = CONFIG_PM_STD_PARTITION;
 dev_t swsusp_resume_device;
 sector_t swsusp_resume_block;
 __visible int in_suspend __nosavedata;
+bool in_resume_hibernate;
 
 enum {
 	HIBERNATION_INVALID,
@@ -433,6 +434,7 @@ static int resume_target_kernel(bool platform_mode)
 	if (error)
 		goto Cleanup;
 
+	in_resume_hibernate = true;
 	error = disable_nonboot_cpus();
 	if (error)
 		goto Enable_cpus;
@@ -474,6 +476,7 @@ static int resume_target_kernel(bool platform_mode)
 	local_irq_enable();
 
  Enable_cpus:
+	in_resume_hibernate = false;
 	enable_nonboot_cpus();
 
  Cleanup:
Signature · 1 line
2.7.4
#12 lore raw

Re: [PATCH 3/4][RFC v2] PM / hibernate: introduce a flag to indicate resuming from hibernation

Quoted 14 lines · Chen Yu

Sometime we need to do some operations before resuming from hibernation, so introduce a flag to indicate this stage.

---
 include/linux/suspend.h  | 7 +++++++
 kernel/power/hibernate.c | 3 +++
 2 files changed, 10 insertions(+)
include/linux/suspend.h +0 -0
index 8b6ec7e..422e87a 100644

include/linux/suspend.h extern bool system_entering_hibernation(void); · line 387

Quoted 4 lines
include/linux/suspend.h +1 -0
@@ -384,6 +384,12 @@ extern bool system_entering_hibernation(void);
 extern bool hibernation_available(void);
 asmlinkage int swsusp_save(void);
 extern struct pbe *restore_pblist;
+extern bool in_resume_hibernate;

in_resume_hibernation? But it is pretty sad if we need another such state function...
Pavel

Quoted 43 lines
include/linux/suspend.h +5 -0
+static inline bool hibernation_in_resume(void)
+{
+	return in_resume_hibernate;
+}
 #else /* CONFIG_HIBERNATION */
 static inline void register_nosave_region(unsigned long b, unsigned long e) {}
 static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
@@ -395,6 +401,7 @@ static inline void hibernation_set_ops(const struct platform_hibernation_ops *op
 static inline int hibernate(void) { return -ENOSYS; }
 static inline bool system_entering_hibernation(void) { return false; }
 static inline bool hibernation_available(void) { return false; }
+static inline bool hibernation_in_resume(void) { return false; }
 #endif /* CONFIG_HIBERNATION */
 
 /* Hibernation and suspend events */
kernel/power/hibernate.c +3 -0
index fca9254..41be909 100644
@@ -43,6 +43,7 @@ static char resume_file[256] = CONFIG_PM_STD_PARTITION;
 dev_t swsusp_resume_device;
 sector_t swsusp_resume_block;
 __visible int in_suspend __nosavedata;
+bool in_resume_hibernate;
 
 enum {
 	HIBERNATION_INVALID,
@@ -433,6 +434,7 @@ static int resume_target_kernel(bool platform_mode)
 	if (error)
 		goto Cleanup;
 
+	in_resume_hibernate = true;
 	error = disable_nonboot_cpus();
 	if (error)
 		goto Enable_cpus;
@@ -474,6 +476,7 @@ static int resume_target_kernel(bool platform_mode)
 	local_irq_enable();
 
  Enable_cpus:
+	in_resume_hibernate = false;
 	enable_nonboot_cpus();
 
  Cleanup:
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
author
#13 lore raw

RE: [PATCH 3/4][RFC v2] PM / hibernate: introduce a flag to indicate resuming from hibernation

Hi,

Quoted 20 lines

-----Original Message-----
From: Pavel Machek [mailto:[email protected]]
Sent: Sunday, June 26, 2016 12:53 AM
To: Chen, Yu C
Cc: [email protected]; [email protected]; Rafael J. Wysocki; Len Brown; Peter Zijlstra; H. Peter Anvin; Borislav Petkov; Brian Gerst; Thomas Gleixner; Ingo Molnar; Varun Koyyalagunta; [email protected]
Subject: Re: [PATCH 3/4][RFC v2] PM / hibernate: introduce a flag to indicate resuming from hibernation

Quoted 9 lines · Chen Yu

Sometime we need to do some operations before resuming from hibernation, so introduce a flag to indicate this stage.

---
 include/linux/suspend.h  | 7 +++++++
 kernel/power/hibernate.c | 3 +++
 2 files changed, 10 insertions(+)
Quoted 11 lines

include/linux/suspend.h extern bool system_entering_hibernation(void); · line 386

Quoted 8 lines
include/linux/suspend.h index renamed from include/linux/suspend.h +0 -0

8b6ec7e..422e87a 100644

include/linux/suspend.h +1 -0
@@ -384,6 +384,12 @@ extern bool system_entering_hibernation(void);
 extern bool hibernation_available(void);  asmlinkage int
 swsusp_save(void);  extern struct pbe *restore_pblist;
+extern bool in_resume_hibernate;

in_resume_hibernation? But it is pretty sad if we need another such state function...

At first I tried not to add any new flags, and just let all the CPUs fall into hlt if it is triggered by disable_nonboot_cpus, but as depicted in [PATCH 4/4],
there is a scenario that, what about the CPUs already offline before
the hibernation? They might be in deep cstate by mwait, and mwait is not safe during resume.
We have to kick all the offline CPUs online again, and then
put them offline to hlt state, thus to avoid the unsafe memory access during resume. (The memory mapping for text code is not safe during resume, so it's hard to fix it in mwait_play_dead, and the kick mechanism seems to be a suitable choice). In order to restrict the impact of the kick mechanism to a minimal scope, I used this flag to make this mechanism only work for hibernation resume phase.

thanks,
Yu

author
#14 lore raw

PATCH v2 4/4 x86, hotplug: Use hlt instead of mwait when resuming from hibernation

In reply to Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

---
 arch/x86/kernel/smpboot.c | 15 +++++++++++++++
 include/linux/cpu.h       |  2 ++
 kernel/cpu.c              | 22 ++++++++++++++++++++++
 3 files changed, 39 insertions(+)
arch/x86/kernel/smpboot.c +15 -0
index fafe8b9..86672be 100644
@@ -53,6 +53,7 @@
 #include <linux/stackprotector.h>
 #include <linux/gfp.h>
 #include <linux/cpuidle.h>
+#include <linux/suspend.h>
 
 #include <asm/acpi.h>
 #include <asm/desc.h>
@@ -1341,6 +1342,18 @@ void arch_enable_nonboot_cpus_end(void)
 	mtrr_aps_init();
 }
 
+void arch_disable_nonboot_cpus_pre(void)
+{
+	if (!hibernation_in_resume())
+		return;
+	kick_offline_cpus(1);
+}
+
+void arch_enable_nonboot_cpus_prev(void)
+{
+	kick_offline_cpus(0);
+}
+
 /*
  * Early setup to make printk work.
  */
@@ -1642,6 +1655,8 @@ void native_play_dead(void)
 	play_dead_common();
 	tboot_shutdown(TB_SHUTDOWN_WFS);
 
+	if (hibernation_in_resume())
+		hlt_play_dead();
 	mwait_play_dead();	/* Only returns on failure */
 	if (cpuidle_play_dead())
 		hlt_play_dead();
include/linux/cpu.h +2 -0
index 21597dc..d408200 100644
@@ -241,9 +241,11 @@ static inline void cpu_hotplug_done(void) {}
 #ifdef CONFIG_PM_SLEEP_SMP
 extern int disable_nonboot_cpus(void);
 extern void enable_nonboot_cpus(void);
+extern int kick_offline_cpus(int first);
 #else /* !CONFIG_PM_SLEEP_SMP */
 static inline int disable_nonboot_cpus(void) { return 0; }
 static inline void enable_nonboot_cpus(void) {}
+static inline int kick_offline_cpus(int first) { return 0; }
 #endif /* !CONFIG_PM_SLEEP_SMP */
 
 void cpu_startup_entry(enum cpuhp_state state);
kernel/cpu.c +22 -0
index ce6e5e4..538a382 100644
@@ -1016,6 +1016,25 @@ EXPORT_SYMBOL_GPL(cpu_up);
 
 #ifdef CONFIG_PM_SLEEP_SMP
 static cpumask_var_t frozen_cpus;
+static cpumask_var_t offline_cpus;
+
+int kick_offline_cpus(int first)
+{
+	int cpu;
+
+	if (offline_cpus == NULL)
+		return -ENOMEM;
+
+	if (first)
+		cpumask_andnot(offline_cpus, cpu_possible_mask, cpu_online_mask);
+
+	for_each_cpu(cpu, offline_cpus) {
+		_cpu_up(cpu, 1, CPUHP_ONLINE);
+		_cpu_down(cpu, 1, CPUHP_OFFLINE);
+	}
+
+	return 0;
+}
 
 void __weak arch_disable_nonboot_cpus_pre(void)
 {
@@ -1120,6 +1139,9 @@ static int __init alloc_frozen_cpus(void)
 {
 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
 		return -ENOMEM;
+	/* No need to free frozen_cpus if failed. */
+	if (!alloc_cpumask_var(&offline_cpus, GFP_KERNEL|__GFP_ZERO))
+		return -ENOMEM;
 	return 0;
 }
 core_initcall(alloc_frozen_cpus);
Signature · 1 line
2.7.4
#15 lore raw
Quoted 43 lines · Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

Quoted 13 lines

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

I realize I'm extremely late to the party, but I must admit that I don't get it. Sure, hibernation resume can spuriously wake the non-boot CPU, but at some point it has to wake up for real. What ensures that the text it was running (native_play_dead or whatever) is still there when it wakes up?

Or does the hibernation resume code actually send the remote CPU an INIT-SIPI sequence a la wakeup_secondary_cpu_via_init()? If so, this seems a bit odd to me. Shouldn't we kick the CPU all the way to the wait-for-SIPI state rather than getting it to play dead via hlt or mwait? Or perhaps there should be a real hibernation trampoline: allocate a safe page, have all CPUs jump there and spin (via hlt or mwait), do the resume, and then kick the other CPUs awake and have them jump back to real code.

--Andy

#16 lore raw
Quoted 59 lines · Andy Lutomirski
Quoted 44 lines · Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

Quoted 13 lines

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

Quoted 3 lines

I realize I'm extremely late to the party, but I must admit that I don't get it. Sure, hibernation resume can spuriously wake the non-boot CPU, but at some point it has to wake up for real.

You mean during resume? We reinit from scratch then.

Quoted 5 lines

What ensures that the text it was
running (native_play_dead or whatever) is still there when it wakes up?

Or does the hibernation resume code actually send the remote CPU an INIT-SIPI sequence a la wakeup_secondary_cpu_via_init()?

That's what happens AFAICS.

Quoted 3 lines

If so, this seems
a bit odd to me. Shouldn't we kick the CPU all the way to the wait-for-SIPI state rather than getting it to play dead via hlt or mwait?

We could do that. It would be a bit cleaner than using the "hlt play dead" thing, but the practical difference would be very small (if observable at all).

Quoted 4 lines

Or perhaps
there should be a real hibernation trampoline: allocate a safe page, have all CPUs jump there and spin (via hlt or mwait), do the resume, and then kick the other CPUs awake and have them jump back to real code.

That would require extra synchronization between the image and restore kernels (the trampoline would have to be the same virtual address and the same code in both), so I'd rather not do that if not absolutely necessary.

The wait-for-SIPI idea seems viable to me though. Do we have code to do that already?

Thanks,
Rafael

#17 lore raw
Quoted 74 lines · Rafael J. Wysocki
Quoted 59 lines · Andy Lutomirski
Quoted 44 lines · Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

Quoted 13 lines

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

Quoted 3 lines

I realize I'm extremely late to the party, but I must admit that I don't get it. Sure, hibernation resume can spuriously wake the non-boot CPU, but at some point it has to wake up for real.

You mean during resume? We reinit from scratch then.

Quoted 5 lines

What ensures that the text it was
running (native_play_dead or whatever) is still there when it wakes up?

Or does the hibernation resume code actually send the remote CPU an INIT-SIPI sequence a la wakeup_secondary_cpu_via_init()?

That's what happens AFAICS.

Quoted 7 lines
Quoted 3 lines

If so, this seems
a bit odd to me. Shouldn't we kick the CPU all the way to the wait-for-SIPI state rather than getting it to play dead via hlt or mwait?

We could do that. It would be a bit cleaner than using the "hlt play dead" thing, but the practical difference would be very small (if observable at all).

Probably true. It might be worth changing the "hlt" path to something like:

asm volatile ("hlt");
WARN(1, "CPU woke directly from halt-for-resume -- should have been woken by SIPI\n");

#18 lore raw
Quoted 83 lines · Andy Lutomirski
Quoted 74 lines · Rafael J. Wysocki
Quoted 59 lines · Andy Lutomirski
Quoted 44 lines · Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

Quoted 13 lines

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

Quoted 3 lines

I realize I'm extremely late to the party, but I must admit that I don't get it. Sure, hibernation resume can spuriously wake the non-boot CPU, but at some point it has to wake up for real.

You mean during resume? We reinit from scratch then.

Quoted 5 lines

What ensures that the text it was
running (native_play_dead or whatever) is still there when it wakes up?

Or does the hibernation resume code actually send the remote CPU an INIT-SIPI sequence a la wakeup_secondary_cpu_via_init()?

That's what happens AFAICS.

Quoted 7 lines
Quoted 3 lines

If so, this seems
a bit odd to me. Shouldn't we kick the CPU all the way to the wait-for-SIPI state rather than getting it to play dead via hlt or mwait?

We could do that. It would be a bit cleaner than using the "hlt play dead" thing, but the practical difference would be very small (if observable at all).

Quoted 5 lines

Probably true. It might be worth changing the "hlt" path to something like:

asm volatile ("hlt");
WARN(1, "CPU woke directly from halt-for-resume -- should have been woken by SIPI\n");

The visibility of that warning would be sort of limited, though, because the only case it might show up is when the system went belly up due to an unhandled page fault.

Thanks,
Rafael

#19 lore raw
Quoted 86 lines · Rafael J. Wysocki
Quoted 85 lines · Andy Lutomirski
Quoted 74 lines · Rafael J. Wysocki
Quoted 59 lines · Andy Lutomirski
Quoted 44 lines · Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

Quoted 13 lines

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

Quoted 3 lines

I realize I'm extremely late to the party, but I must admit that I don't get it. Sure, hibernation resume can spuriously wake the non-boot CPU, but at some point it has to wake up for real.

You mean during resume? We reinit from scratch then.

Quoted 5 lines

What ensures that the text it was
running (native_play_dead or whatever) is still there when it wakes up?

Or does the hibernation resume code actually send the remote CPU an INIT-SIPI sequence a la wakeup_secondary_cpu_via_init()?

That's what happens AFAICS.

Quoted 7 lines
Quoted 3 lines

If so, this seems
a bit odd to me. Shouldn't we kick the CPU all the way to the wait-for-SIPI state rather than getting it to play dead via hlt or mwait?

We could do that. It would be a bit cleaner than using the "hlt play dead" thing, but the practical difference would be very small (if observable at all).

Probably true. It might be worth changing the "hlt" path to something like:

Quoted 7 lines
Quoted 3 lines

asm volatile ("hlt");
WARN(1, "CPU woke directly from halt-for-resume -- should have been woken by SIPI\n");

The visibility of that warning would be sort of limited, though, because the only case it might show up is when the system went belly up due to an unhandled page fault.

Righto.

Maybe at least add a comment that the hlt isn't intended to ever resume on the next instruction?

--Andy

author
#20 lore raw
In reply to Andy Lutomirski

Hi Andy,

Quoted 58 lines · Andy Lutomirski
Quoted 43 lines · Chen Yu

Here's the story of what the problem is, why this
happened, and why this patch looks like this:

Stress test from Varun Koyyalagunta reports that, the nonboot CPU would hang occasionally, when resuming from hibernation. Further investigation shows that, the precise stage when nonboot CPU hangs, is the time when the nonboot CPU been woken up incorrectly, and tries to monitor the mwait_ptr for the second time, then an exception is triggered due to illegal vaddr access, say, something like, 'Unable to handler kernel address of 0xffff8800ba800010...'

Further investigation shows that, the exception is caused by accessing a page without PRESENT flag, because the pte entry for this vaddr is zero. Here's the scenario how this problem
happens: Page table for direct mapping is allocated dynamically by kernel_physical_mapping_init, it is possible that in the resume process, when the boot CPU is trying to write back pages to their original address, and just right to writes to the monitor mwait_ptr then wakes up one of the nonboot CPUs, since the page table currently used by the nonboot CPU might not the same as it is before the hibernation, an exception might occur due to inconsistent page table.

First try is to get rid of this problem by changing the monitor address from task.flag to zero page, because one one would write to zero page. But this still have problem because of ping-pong wake up situation in mwait_play_dead:

One possible implementation of a clflush is a read-invalidate snoop, which is what a store might look like, so cflush might break the mwait.

1. CPU1 wait at zero page
2. CPU2 cflush zero page, wake CPU1 up, then CPU2 waits at zero page
3. CPU1 is woken up, and invoke cflush zero page, thus wake up CPU2 again. then the nonboot CPUs never sleep for long.

So it's better to monitor different address for each nonboot CPUs, however since there is only one zero page, at most:
PAGE_SIZE/L1_CACHE_LINE CPUs are satisfied, which is usually 64 on a x86_64, apparently it's not enough for servers, maybe more zero pages are required.

Quoted 13 lines

So choose the solution as Brian suggested, to put the nonboot CPUs into hlt before resuming. But Rafael has mentioned that, if some of the CPUs have already been offline before hibernation, then the problem is still there. So this patch tries to kick the already offline CPUs woken up and fall into hlt, and then put the rest online CPUs into hlt. In this way, all the nonboot CPUs will wait at a safe state, without touching any memory during s/r. (It's not safe to modify mwait_play_dead, because once previous offline CPUs are woken up, it will either access text code, whose page table is not safe anymore across hibernation, due to:
Commit ab76f7b4ab23 ("x86/mm: Set NX on gap between __ex_table and rodata").

Quoted 4 lines

I realize I'm extremely late to the party, but I must admit that I don't get it. Sure, hibernation resume can spuriously wake the non-boot CPU, but at some point it has to wake up for real. What ensures that the text it was running (native_play_dead or whatever) is still there when it wakes up?

BTW, If I understand correctly, when the nonboot CPUs are woken up from hlt by INIT,INIT,STARTUP sequence, these CPUs do not need the text mapping, as Rafael mentioned, they are reinitialized from scratch(react in realmode after received STARTUP).

Quoted 2 lines

--Andy

Thanks,
Yu