Skip to contentSkip to conversation

init: Enable might_sleep() and smp_processor_id() debugging early

PATCH Superseded Thomas Gleixner 11 participants 47 messages May 14, 2017 – May 16, 2017

Subsystems (16)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#3 lore raw

PATCH 2/18 arm: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in ipi_cpu_stop() to handle the extra states.

Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 arch/arm/kernel/smp.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/kernel/smp.c +1 -2
@@ -555,8 +555,7 @@ static DEFINE_RAW_SPINLOCK(stop_lock);
  */
 static void ipi_cpu_stop(unsigned int cpu)
 {
-	if (system_state == SYSTEM_BOOTING ||
-	    system_state == SYSTEM_RUNNING) {
+	if (system_state <= SYSTEM_RUNNING) {
 		raw_spin_lock(&stop_lock);
 		pr_crit("CPU%u: stopping\n", cpu);
 		dump_stack();
#4 lore raw

PATCH 3/18 arm64: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in smp_send_stop() to handle the extra states.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 arch/arm64/kernel/smp.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/kernel/smp.c +1 -2
@@ -961,8 +961,7 @@ void smp_send_stop(void)
 		cpumask_copy(&mask, cpu_online_mask);
 		cpumask_clear_cpu(smp_processor_id(), &mask);
 
-		if (system_state == SYSTEM_BOOTING ||
-		    system_state == SYSTEM_RUNNING)
+		if (system_state <= SYSTEM_RUNNING)
 			pr_crit("SMP: stopping secondary CPUs\n");
 		smp_cross_call(&mask, IPI_CPU_STOP);
 	}
#7 lore raw

PATCH 5/18 metag: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in stop_this_cpu() to handle the extra states.

---
 arch/metag/kernel/smp.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/metag/kernel/smp.c +1 -2
@@ -567,8 +567,7 @@ static void stop_this_cpu(void *data)
 {
 	unsigned int cpu = smp_processor_id();
 
-	if (system_state == SYSTEM_BOOTING ||
-	    system_state == SYSTEM_RUNNING) {
+	if (system_state <= SYSTEM_RUNNING) {
 		spin_lock(&stop_lock);
 		pr_crit("CPU%u: stopping\n", cpu);
 		dump_stack();
#10 lore raw

PATCH 7/18 ACPI: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Make the decision wether a pci root is hotplugged depend on SYSTEM_RUNNING instead of !SYSTEM_BOOTING.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 drivers/acpi/pci_root.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/pci_root.c +1 -1
@@ -523,7 +523,7 @@ static int acpi_pci_root_add(struct acpi
 	struct acpi_pci_root *root;
 	acpi_handle handle = device->handle;
 	int no_aspm = 0;
-	bool hotadd = system_state != SYSTEM_BOOTING;
+	bool hotadd = system_state == SYSTEM_RUNNING;
 
 	root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
 	if (!root)
#21 lore raw

PATCH 10/18 iommu/vt-d: Adjust system_state checks

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state checks in dmar_parse_one_atsr() and dmar_iommu_notify_scope_dev() to handle the extra states.

Cc David Woodhouse <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
---
 drivers/iommu/intel-iommu.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/intel-iommu.c +2 -2
@@ -4312,7 +4312,7 @@ int dmar_parse_one_atsr(struct acpi_dmar
 	struct acpi_dmar_atsr *atsr;
 	struct dmar_atsr_unit *atsru;
 
-	if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
+	if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
 		return 0;
 
 	atsr = container_of(hdr, struct acpi_dmar_atsr, header);
@@ -4562,7 +4562,7 @@ int dmar_iommu_notify_scope_dev(struct d
 	struct acpi_dmar_atsr *atsr;
 	struct acpi_dmar_reserved_memory *rmrr;
 
-	if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
+	if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
 		return 0;
 
 	list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
#23 lore raw

PATCH 11/18 iommu/of: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in of_iommu_driver_present() to handle the extra states.

Cc Joerg Roedel <[email protected]>
Cc iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
---
 drivers/iommu/of_iommu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/of_iommu.c +1 -1
@@ -103,7 +103,7 @@ static bool of_iommu_driver_present(stru
 	 * it never will be. We don't want to defer indefinitely, nor attempt
 	 * to dereference __iommu_of_table after it's been freed.
 	 */
-	if (system_state > SYSTEM_BOOTING)
+	if (system_state >= SYSTEM_RUNNING)
 		return false;
 
 	return of_match_node(&__iommu_of_table, np);
#0 lore raw

cover init: Enable might_sleep() and smp_processor_id() debugging early

We recentlty discovered a call path which takes a mutex from the low level secondary CPU bringup code and wondered why this was not caught by might_sleep().

The reason is that both debug facilities depend on system_state == SYSTEM_RUNNING, which is set after init memory is freed.

That means that the SMP bootup and the builtin driver initialization is not covered by these checks at all.

The patch series addresses this by adding two intermediate states. might_sleep() debugging is enabled right when scheduling starts, i.e. the boot CPU idle task schedules the first time. smp_processor_id() debugging is enabled right before SMP bringup happens.

Thanks,

tglx
----

 arch/arm/kernel/smp.c            |    3 +--
 arch/metag/kernel/smp.c          |    3 +--
 arch/x86/kernel/smpboot.c        |    2 +-
 b/arch/arm64/kernel/smp.c        |    3 +--
 b/arch/powerpc/kernel/smp.c      |    2 +-
 drivers/acpi/pci_root.c          |    2 +-
 drivers/base/node.c              |    2 +-
 drivers/cpufreq/pasemi-cpufreq.c |    2 +-
 drivers/iommu/intel-iommu.c      |    4 ++--
 drivers/iommu/of_iommu.c         |    2 +-
 drivers/xen/manage.c             |    2 ++
 include/linux/kernel.h           |    2 ++
 init/main.c                      |   12 ++++++++++--
 kernel/async.c                   |    8 ++++----
 kernel/extable.c                 |    2 +-
 kernel/printk/printk.c           |    2 +-
 kernel/sched/core.c              |    4 +++-
 lib/smp_processor_id.c           |    2 +-
 mm/vmscan.c                      |    2 +-
 19 files changed, 36 insertions(+), 25 deletions(-)
#1 lore raw

PATCH 1/18 init: Pin init task to boot cpu initially

Some of the boot code in init_kernel_freeable() which runs before SMP bringup assumes (rightfully) that it runs on the boot cpu and therefor can use smp_processor_id() in preemptible context.

That works so far because the smp_processor_id() check starts to be effective after smp bringup. That's just wrong. Starting with SMP bringup and the ability to move threads around, smp_processor_id() in preemptible context is broken.

Aside of that it does not make sense to allow init to run on all cpus before sched_smp_init() has been run.

Pin the init to the boot cpu so the existing code can continue to use smp_processor_id() without triggering the checks when the enabling of those checks starts earlier.

---
 init/main.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
init/main.c +5 -2
@@ -1015,10 +1015,13 @@ static noinline void __init kernel_init_
 	 * init can allocate pages on any node
 	 */
 	set_mems_allowed(node_states[N_MEMORY]);
+
 	/*
-	 * init can run on any cpu.
+	 * Pin init on the boot cpu. Task migration is not properly working
+	 * until sched_init_smp() has been run. It will set the allowed
+	 * cpus for init to the non isolated cpus.
 	 */
-	set_cpus_allowed_ptr(current, cpu_all_mask);
+	set_cpus_allowed_ptr(current, cpumask_of(raw_smp_processor_id()));
 
 	cad_pid = task_pid(current);
 
#18 lore raw

PATCH 9/18 cpufreq/pasemi: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in pas_cpufreq_cpu_exit() to handle the extra states.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
---
 drivers/cpufreq/pasemi-cpufreq.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/pasemi-cpufreq.c +1 -1
@@ -226,7 +226,7 @@ static int pas_cpufreq_cpu_exit(struct c
 	 * We don't support CPU hotplug. Don't unmap after the system
 	 * has already made it to a running state.
 	 */
-	if (system_state != SYSTEM_BOOTING)
+	if (system_state >= SYSTEM_RUNNING)
 		return 0;
 
 	if (sdcasr_mapbase)
#8 lore raw

PATCH 6/18 powerpc: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in smp_generic_cpu_bootable() to handle the extra states.

Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
---
 arch/powerpc/kernel/smp.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/smp.c +1 -1
@@ -97,7 +97,7 @@ int smp_generic_cpu_bootable(unsigned in
 	/* Special case - we inhibit secondary thread startup
 	 * during boot if the user requests it.
 	 */
-	if (system_state == SYSTEM_BOOTING && cpu_has_feature(CPU_FTR_SMT)) {
+	if (system_state < SYSTEM_RUNNING && cpu_has_feature(CPU_FTR_SMT)) {
 		if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
 			return 0;
 		if (smt_enabled_at_boot
#30 lore raw

PATCH 14/18 printk: Adjust system_state checks

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in boot_delay_msec() to handle the extra states.

---
 kernel/printk/printk.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
@@ -1176,7 +1176,7 @@ static void boot_delay_msec(int level)
 	unsigned long long k;
 	unsigned long timeout;
 
-	if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
+	if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING)
 		|| suppress_message_printing(level)) {
 		return;
 	}
#37 lore raw

PATCH 17/18 sched: Enable might_sleep() checks early

In reply to Thomas Gleixner

might_sleep() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Enable it right when the scheduler starts working, i.e. when init task and kthreadd have been created and right before the idle task enables preemption.

---
 init/main.c         |    2 ++
 kernel/sched/core.c |    4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
init/main.c +2 -0
@@ -410,6 +410,8 @@ static noinline void __ref rest_init(voi
 	 * at least once to get things moving:
 	 */
 	init_idle_bootup_task(current);
+	/* Enable might_sleep() checks */
+	system_state = SYSTEM_BOOTING_UP;
 	schedule_preempt_disabled();
 	/* Call into cpu_idle with preempt disabled */
 	cpu_startup_entry(CPUHP_ONLINE);
kernel/sched/core.c +3 -1
@@ -6226,8 +6226,10 @@ void ___might_sleep(const char *file, in
 
 	if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
 	     !is_idle_task(current)) ||
-	    system_state != SYSTEM_RUNNING || oops_in_progress)
+	    system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
+	    oops_in_progress)
 		return;
+
 	if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
 		return;
 	prev_jiffy = jiffies;
#34 lore raw

PATCH 16/18 init: Introduce SYSTEM_BOOTING_UP/SMP states

In reply to Thomas Gleixner

might_sleep() debugging should be active right after the scheduler starts working and smp_processor_id() debugging right before the first non boot cpu is brought up.

Add two new states which allow to enable those checks earlier and add them to the xen do_poweroff() function.

No functional change.

---
 drivers/xen/manage.c   |    2 ++
 include/linux/kernel.h |    2 ++
 2 files changed, 4 insertions(+)
drivers/xen/manage.c +2 -0
@@ -190,6 +190,8 @@ static void do_poweroff(void)
 {
 	switch (system_state) {
 	case SYSTEM_BOOTING:
+	case SYSTEM_BOOTING_UP:
+	case SYSTEM_BOOTING_SMP:
 		orderly_poweroff(true);
 		break;
 	case SYSTEM_RUNNING:
include/linux/kernel.h +2 -0
@@ -493,6 +493,8 @@ extern bool early_boot_irqs_disabled;
 /* Values used for system_state */
 extern enum system_states {
 	SYSTEM_BOOTING,
+	SYSTEM_BOOTING_UP,
+	SYSTEM_BOOTING_SMP,
 	SYSTEM_RUNNING,
 	SYSTEM_HALT,
 	SYSTEM_POWER_OFF,
#32 lore raw

PATCH 15/18 mm/vmscan: Adjust system_state checks

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in kswapd_run() to handle the extra states.

Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Vlastimil Babka <[email protected]>
---
 mm/vmscan.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
@@ -3643,7 +3643,7 @@ int kswapd_run(int nid)
 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
 	if (IS_ERR(pgdat->kswapd)) {
 		/* failure at boot is fatal */
-		BUG_ON(system_state == SYSTEM_BOOTING);
+		BUG_ON(system_state < SYSTEM_RUNNING);
 		pr_err("Failed to start kswapd on node %d\n", nid);
 		ret = PTR_ERR(pgdat->kswapd);
 		pgdat->kswapd = NULL;
#43 lore raw

PATCH 18/18 sched: Enable smp_processor_id() checks early

In reply to Thomas Gleixner

smp_processor_id() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Enable it right before the first non-boot CPU is brought up.

---
 init/main.c            |    3 +++
 lib/smp_processor_id.c |    2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
---
 init/main.c            |    3 +++
 lib/smp_processor_id.c |    2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
init/main.c +3 -0
@@ -1025,6 +1025,9 @@ static noinline void __init kernel_init_
 	 */
 	set_cpus_allowed_ptr(current, cpumask_of(raw_smp_processor_id()));
 
+	/* Enable smp_processor_id() checks */
+	system_state = SYSTEM_BOOTING_SMP;
+
 	cad_pid = task_pid(current);
 
 	smp_prepare_cpus(setup_max_cpus);
lib/smp_processor_id.c +1 -1
@@ -28,7 +28,7 @@ notrace static unsigned int check_preemp
 	/*
 	 * It is valid to assume CPU-locality during early bootup:
 	 */
-	if (system_state != SYSTEM_RUNNING)
+	if (system_state < SYSTEM_BOOTING_SMP)
 		goto out;
 
 	/*
#28 lore raw

PATCH 13/18 extable: Adjust system_state checks

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in core_kernel_text() to handle the extra states, i.e. to cover init text up to the point where the system switches to state RUNNING.

---
 kernel/extable.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/extable.c +1 -1
@@ -75,7 +75,7 @@ int core_kernel_text(unsigned long addr)
 	    addr < (unsigned long)_etext)
 		return 1;
 
-	if (system_state == SYSTEM_BOOTING &&
+	if (system_state < SYSTEM_RUNNING &&
 	    init_kernel_text(addr))
 		return 1;
 	return 0;
#26 lore raw

PATCH 12/18 async: Adjust system_state checks

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in async_run_entry_fn() and async_synchronize_cookie_domain() to handle the extra states.

Cc Arjan van de Ven <[email protected]>
---
 kernel/async.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/async.c +4 -4
@@ -114,14 +114,14 @@ static void async_run_entry_fn(struct wo
 	ktime_t uninitialized_var(calltime), delta, rettime;
 
 	/* 1) run (and print duration) */
-	if (initcall_debug && system_state == SYSTEM_BOOTING) {
+	if (initcall_debug && system_state < SYSTEM_RUNNING) {
 		pr_debug("calling  %lli_%pF @ %i\n",
 			(long long)entry->cookie,
 			entry->func, task_pid_nr(current));
 		calltime = ktime_get();
 	}
 	entry->func(entry->data, entry->cookie);
-	if (initcall_debug && system_state == SYSTEM_BOOTING) {
+	if (initcall_debug && system_state < SYSTEM_RUNNING) {
 		rettime = ktime_get();
 		delta = ktime_sub(rettime, calltime);
 		pr_debug("initcall %lli_%pF returned 0 after %lld usecs\n",
@@ -284,14 +284,14 @@ void async_synchronize_cookie_domain(asy
 {
 	ktime_t uninitialized_var(starttime), delta, endtime;
 
-	if (initcall_debug && system_state == SYSTEM_BOOTING) {
+	if (initcall_debug && system_state < SYSTEM_RUNNING) {
 		pr_debug("async_waiting @ %i\n", task_pid_nr(current));
 		starttime = ktime_get();
 	}
 
 	wait_event(async_done, lowest_in_progress(domain) >= cookie);
 
-	if (initcall_debug && system_state == SYSTEM_BOOTING) {
+	if (initcall_debug && system_state < SYSTEM_RUNNING) {
 		endtime = ktime_get();
 		delta = ktime_sub(endtime, starttime);
 
#27 lore raw

Re: [patch 12/18] async: Adjust system_state checks

On 5/14/2017 11:27 AM, Thomas Gleixner wrote:

looks good .. ack

#15 lore raw

PATCH 8/18 mm: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

get_nid_for_pfn() checks for system_state == BOOTING to decide whether to use early_pfn_to_nid() when CONFIG_DEFERRED_STRUCT_PAGE_INIT=y.

That check is dubious, because the switch to state RUNNING happes way after page_alloc_init_late() has been invoked.

Change the check to less than RUNNING state so it covers the new intermediate states as well.

Cc Mel Gorman <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/node.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/node.c +1 -1
@@ -377,7 +377,7 @@ static int __ref get_nid_for_pfn(unsigne
 	if (!pfn_valid_within(pfn))
 		return -1;
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
-	if (system_state == SYSTEM_BOOTING)
+	if (system_state < SYSTEM_RUNNING)
 		return early_pfn_to_nid(pfn);
 #endif
 	page = pfn_to_page(pfn);
#5 lore raw

PATCH 4/18 x86/smp: Adjust system_state check

In reply to Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in announce_cpu() to handle the extra states.

---
 arch/x86/kernel/smpboot.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/smpboot.c +1 -1
@@ -863,7 +863,7 @@ static void announce_cpu(int cpu, int ap
 	if (cpu == 1)
 		printk(KERN_INFO "x86: Booting SMP configuration:\n");
 
-	if (system_state == SYSTEM_BOOTING) {
+	if (system_state < SYSTEM_RUNNING) {
 		if (node != current_node) {
 			if (current_node > (-1))
 				pr_cont("\n");
maintainer Acked-by on v1 9/18
#19 lore raw

Re: [patch 09/18] cpufreq/pasemi: Adjust system_state check

In reply to Thomas Gleixner
Quoted 17 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in pas_cpufreq_cpu_exit() to handle the extra states.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
---
 drivers/cpufreq/pasemi-cpufreq.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/cpufreq/pasemi-cpufreq.c static int pas_cpufreq_cpu_exit(struct c · line 232

Quoted 8 lines
drivers/cpufreq/pasemi-cpufreq.c +1 -1
@@ -226,7 +226,7 @@ static int pas_cpufreq_cpu_exit(struct c
 	 * We don't support CPU hotplug. Don't unmap after the system
 	 * has already made it to a running state.
 	 */
-	if (system_state != SYSTEM_BOOTING)
+	if (system_state >= SYSTEM_RUNNING)
 		return 0;
 
 	if (sdcasr_mapbase)
Signature · 1 line
viresh
maintainer Acked-by on v1 8/18
#16 lore raw

Re: [patch 08/18] mm: Adjust system_state check

In reply to Thomas Gleixner
Quoted 22 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

get_nid_for_pfn() checks for system_state == BOOTING to decide whether to use early_pfn_to_nid() when CONFIG_DEFERRED_STRUCT_PAGE_INIT=y.

That check is dubious, because the switch to state RUNNING happes way after page_alloc_init_late() has been invoked.

Change the check to less than RUNNING state so it covers the new intermediate states as well.

Cc Mel Gorman <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/node.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/base/node.c static int __ref get_nid_for_pfn(unsigne · line 380

Quoted 5 lines
drivers/base/node.c +1 -1
@@ -377,7 +377,7 @@ static int __ref get_nid_for_pfn(unsigne
 	if (!pfn_valid_within(pfn))
 		return -1;
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
-	if (system_state == SYSTEM_BOOTING)
+	if (system_state < SYSTEM_RUNNING)

Someone better comment the place where these are defined that they need to be in a specific order, I don't think they have been tested this way in the past...

With that change:

thanks,

greg k-h

reviewer Acked-by on v1 6/18
#9 lore raw

Re: [patch 06/18] powerpc: Adjust system_state check

In reply to Thomas Gleixner
Quoted 6 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in smp_generic_cpu_bootable() to handle the extra states.

Quoted 8 lines
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Michael Ellerman <[email protected]>
---
 arch/powerpc/kernel/smp.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

cheers

maintainer Acked-by on v1 16/18
#35 lore raw

Re: [patch 16/18] init: Introduce SYSTEM_BOOTING_UP/SMP states

In reply to Thomas Gleixner
Quoted 11 lines · Thomas Gleixner

might_sleep() debugging should be active right after the scheduler starts working and smp_processor_id() debugging right before the first non boot cpu is brought up.

Add two new states which allow to enable those checks earlier and add them to the xen do_poweroff() function.

No functional change.

Juergen

reviewer
#11 lore raw

Re: [patch 07/18] ACPI: Adjust system_state check

In reply to Thomas Gleixner
Quoted 6 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Make the decision wether a pci root is hotplugged depend on SYSTEM_RUNNING instead of !SYSTEM_BOOTING.

Quoted 8 lines
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 drivers/acpi/pci_root.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This looks sane to me, but I assume this was meant to be Cc'd to the ACPI folk rather than the arm64 folk?

Mark.

Quoted 13 lines
drivers/acpi/pci_root.c +1 -1
@@ -523,7 +523,7 @@ static int acpi_pci_root_add(struct acpi
 	struct acpi_pci_root *root;
 	acpi_handle handle = device->handle;
 	int no_aspm = 0;
-	bool hotadd = system_state != SYSTEM_BOOTING;
+	bool hotadd = system_state == SYSTEM_RUNNING;
 
 	root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
 	if (!root)
#12 lore raw

Re: [patch 07/18] ACPI: Adjust system_state check

Quoted 16 lines · Mark Rutland
Quoted 6 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Make the decision wether a pci root is hotplugged depend on SYSTEM_RUNNING instead of !SYSTEM_BOOTING.

Quoted 8 lines
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 drivers/acpi/pci_root.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 2 lines

This looks sane to me, but I assume this was meant to be Cc'd to the ACPI folk rather than the arm64 folk?

Indeed.

Quoted 16 lines

Mark.

Quoted 13 lines
drivers/acpi/pci_root.c +1 -1
@@ -523,7 +523,7 @@ static int acpi_pci_root_add(struct acpi
 	struct acpi_pci_root *root;
 	acpi_handle handle = device->handle;
 	int no_aspm = 0;
-	bool hotadd = system_state != SYSTEM_BOOTING;
+	bool hotadd = system_state == SYSTEM_RUNNING;
 
 	root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
 	if (!root)
reviewer Acked-by on v1 11/18
#24 lore raw

Re: [patch 11/18] iommu/of: Adjust system_state check

In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in of_iommu_driver_present() to handle the extra states.

FWIW,

Quoted 19 lines
Cc Joerg Roedel <[email protected]>
Cc iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
---
 drivers/iommu/of_iommu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/of_iommu.c +1 -1
@@ -103,7 +103,7 @@ static bool of_iommu_driver_present(stru
 	 * it never will be. We don't want to defer indefinitely, nor attempt
 	 * to dereference __iommu_of_table after it's been freed.
 	 */
-	if (system_state > SYSTEM_BOOTING)
+	if (system_state >= SYSTEM_RUNNING)
 		return false;
 
 	return of_match_node(&__iommu_of_table, np);
reviewer Tested-by on the v1 series
#46 lore raw
In reply to Thomas Gleixner

Hi Thomas,

Quoted 7 lines · Thomas Gleixner

We recentlty discovered a call path which takes a mutex from the low level secondary CPU bringup code and wondered why this was not caught by might_sleep().

The reason is that both debug facilities depend on system_state == SYSTEM_RUNNING, which is set after init memory is freed.

Quoted 7 lines

That means that the SMP bootup and the builtin driver initialization is not covered by these checks at all.

The patch series addresses this by adding two intermediate states. might_sleep() debugging is enabled right when scheduling starts, i.e. the boot CPU idle task schedules the first time. smp_processor_id() debugging is enabled right before SMP bringup happens.

Thanks for putting this together!

I've tested the series on a Juno R1 (arm64) system, with both DT and ACPI.

Patch 17 finds the arm64 mutex issue [1] we discussed previously.

Otherwise, no new issues spotted with this on arm64, just an (existing) ACPI PMU issue for which I've evidently not run sufficient checks on.

So for the bits relevant to arm64 (patches 1, 3, 7, 8, and 12-18):

Thanks,
Mark.

[1] https://lkml.kernel.org/r/20170511125430.GD14766@leverpostej

Quoted 28 lines

Thanks,

tglx
----

 arch/arm/kernel/smp.c            |    3 +--
 arch/metag/kernel/smp.c          |    3 +--
 arch/x86/kernel/smpboot.c        |    2 +-
 b/arch/arm64/kernel/smp.c        |    3 +--
 b/arch/powerpc/kernel/smp.c      |    2 +-
 drivers/acpi/pci_root.c          |    2 +-
 drivers/base/node.c              |    2 +-
 drivers/cpufreq/pasemi-cpufreq.c |    2 +-
 drivers/iommu/intel-iommu.c      |    4 ++--
 drivers/iommu/of_iommu.c         |    2 +-
 drivers/xen/manage.c             |    2 ++
 include/linux/kernel.h           |    2 ++
 init/main.c                      |   12 ++++++++++--
 kernel/async.c                   |    8 ++++----
 kernel/extable.c                 |    2 +-
 kernel/printk/printk.c           |    2 +-
 kernel/sched/core.c              |    4 +++-
 lib/smp_processor_id.c           |    2 +-
 mm/vmscan.c                      |    2 +-
 19 files changed, 36 insertions(+), 25 deletions(-)
reviewer Reviewed-by on v1 1/18
#2 lore raw

Re: [patch 01/18] init: Pin init task to boot cpu initially

In reply to Thomas Gleixner
Quoted 9 lines · On Sun, 14 May 2017 20:27:17 +0200 Thomas Gleixner <[email protected]> wrote:

Some of the boot code in init_kernel_freeable() which runs before SMP bringup assumes (rightfully) that it runs on the boot cpu and therefor can use smp_processor_id() in preemptible context.

That works so far because the smp_processor_id() check starts to be effective after smp bringup. That's just wrong. Starting with SMP bringup and the ability to move threads around, smp_processor_id() in preemptible context is broken.

Quoted 8 lines

Aside of that it does not make sense to allow init to run on all cpus before sched_smp_init() has been run.

Pin the init to the boot cpu so the existing code can continue to use smp_processor_id() without triggering the checks when the enabling of those checks starts earlier.

-- Steve

Quoted 23 lines
---
 init/main.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
init/main.c +5 -2
@@ -1015,10 +1015,13 @@ static noinline void __init kernel_init_
 	 * init can allocate pages on any node
 	 */
 	set_mems_allowed(node_states[N_MEMORY]);
+
 	/*
-	 * init can run on any cpu.
+	 * Pin init on the boot cpu. Task migration is not properly working
+	 * until sched_init_smp() has been run. It will set the allowed
+	 * cpus for init to the non isolated cpus.
 	 */
-	set_cpus_allowed_ptr(current, cpu_all_mask);
+	set_cpus_allowed_ptr(current, cpumask_of(raw_smp_processor_id()));
 
 	cad_pid = task_pid(current);
 
reviewer Reviewed-by on v1 4/18
#6 lore raw

Re: [patch 04/18] x86/smp: Adjust system_state check

In reply to Thomas Gleixner
Quoted 6 lines · On Sun, 14 May 2017 20:27:20 +0200 Thomas Gleixner <[email protected]> wrote:

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in announce_cpu() to handle the extra states.

-- Steve

Quoted 16 lines
---
 arch/x86/kernel/smpboot.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/smpboot.c +1 -1
@@ -863,7 +863,7 @@ static void announce_cpu(int cpu, int ap
 	if (cpu == 1)
 		printk(KERN_INFO "x86: Booting SMP configuration:\n");
 
-	if (system_state == SYSTEM_BOOTING) {
+	if (system_state < SYSTEM_RUNNING) {
 		if (node != current_node) {
 			if (current_node > (-1))
 				pr_cont("\n");
#13 lore raw

Re: [patch 07/18] ACPI: Adjust system_state check

In reply to Thomas Gleixner

[ added linux-acpi and took off arm list ;-) ]

Quoted 18 lines · On Sun, 14 May 2017 20:27:23 +0200 Thomas Gleixner <[email protected]> wrote:

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Make the decision wether a pci root is hotplugged depend on SYSTEM_RUNNING instead of !SYSTEM_BOOTING.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
---
 drivers/acpi/pci_root.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/acpi/pci_root.c static int acpi_pci_root_add(struct acpi · line 526

Quoted 5 lines
drivers/acpi/pci_root.c +1 -1
@@ -523,7 +523,7 @@ static int acpi_pci_root_add(struct acpi
 	struct acpi_pci_root *root;
 	acpi_handle handle = device->handle;
 	int no_aspm = 0;
-	bool hotadd = system_state != SYSTEM_BOOTING;
+	bool hotadd = system_state == SYSTEM_RUNNING;

What about the other boot states greater than SYSTEM_RUNNING? Can this be called then too?

IOW, should it be:

	bool hotadd = system_state >= SYSTEM_RUNNING;

?

-- Steve

Quoted 4 lines
drivers/acpi/pci_root.c +0 -0
 	root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
 	if (!root)
 
#17 lore raw

Re: [patch 08/18] mm: Adjust system_state check

In reply to Greg Kroah-Hartman
Quoted 4 lines · On Mon, 15 May 2017 08:18:26 +0200 Greg Kroah-Hartman…
Quoted 4 lines
drivers/base/node.c +0 -0
@@ -377,7 +377,7 @@ static int __ref get_nid_for_pfn(unsigne
 	if (!pfn_valid_within(pfn))
Quoted 8 lines

drivers/base/node.c static int __ref get_nid_for_pfn(unsigne · line 380

Quoted 4 lines
drivers/base/node.c +1 -1
 		return -1;
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
-	if (system_state == SYSTEM_BOOTING)
+	if (system_state < SYSTEM_RUNNING)  

Someone better comment the place where these are defined that they need to be in a specific order, I don't think they have been tested this way in the past...

Comments never hurt but it has been used before this patch set.

$ git grep  SYSTEM_RUNNING v4.12-rc1 | grep -e '<' -e '>'
v4.12-rc1:arch/powerpc/kernel/smp.c:    if (system_state < SYSTEM_RUNNING)
v4.12-rc1:arch/powerpc/platforms/powernv/eeh-powernv.c:         if (system_state < SYSTEM_RUNNING)
v4.12-rc1:arch/powerpc/platforms/powernv/eeh-powernv.c:         if (system_state < SYSTEM_RUNNING)
v4.12-rc1:drivers/cpuidle/cpuidle-powernv.c:    if (unlikely(system_state < SYSTEM_RUNNING))
v4.12-rc1:drivers/xen/xenbus/xenbus_probe.c:    if (system_state > SYSTEM_RUNNING) {

-- Steve

#20 lore raw

Re: [patch 09/18] cpufreq/pasemi: Adjust system_state check

In reply to Viresh Kumar
Quoted 25 lines · On Mon, 15 May 2017 10:54:03 +0530 Viresh Kumar <[email protected]> wrote:
Quoted 24 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in pas_cpufreq_cpu_exit() to handle the extra states.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
---
 drivers/cpufreq/pasemi-cpufreq.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/pasemi-cpufreq.c +1 -1
@@ -226,7 +226,7 @@ static int pas_cpufreq_cpu_exit(struct c
 	 * We don't support CPU hotplug. Don't unmap after the system
 	 * has already made it to a running state.
 	 */
-	if (system_state != SYSTEM_BOOTING)
+	if (system_state >= SYSTEM_RUNNING)
 		return 0;
 
Quoted 4 lines

drivers/cpufreq/pasemi-cpufreq.c static int pas_cpufreq_cpu_exit(struct c · line 232

Quoted 1 line
drivers/cpufreq/pasemi-cpufreq.c +0 -0
 	if (sdcasr_mapbase)  

This reminds me. My pasemi died. I was trying to see if anyone had an extra one they could send me ;-)

-- Steve

reviewer Reviewed-by on v1 13/18
#29 lore raw

Re: [patch 13/18] extable: Adjust system_state checks

In reply to Thomas Gleixner
Quoted 8 lines · On Sun, 14 May 2017 20:27:29 +0200 Thomas Gleixner <[email protected]> wrote:

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in core_kernel_text() to handle the extra states, i.e. to cover init text up to the point where the system switches to state RUNNING.

-- Steve

Quoted 16 lines
---
 kernel/extable.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/extable.c +1 -1
@@ -75,7 +75,7 @@ int core_kernel_text(unsigned long addr)
 	    addr < (unsigned long)_etext)
 		return 1;
 
-	if (system_state == SYSTEM_BOOTING &&
+	if (system_state < SYSTEM_RUNNING &&
 	    init_kernel_text(addr))
 		return 1;
 	return 0;
maintainer Acked-by on v1 10/18
#22 lore raw

Re: [patch 10/18] iommu/vt-d: Adjust system_state checks

In reply to Thomas Gleixner
Quoted 6 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state checks in dmar_parse_one_atsr() and dmar_iommu_notify_scope_dev() to handle the extra states.

Quoted 7 lines
---
 drivers/iommu/intel-iommu.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 22 lines
drivers/iommu/intel-iommu.c +2 -2
@@ -4312,7 +4312,7 @@ int dmar_parse_one_atsr(struct acpi_dmar
 	struct acpi_dmar_atsr *atsr;
 	struct dmar_atsr_unit *atsru;
 
-	if (system_state != SYSTEM_BOOTING && !intel_iommu_enabled)
+	if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled)
 		return 0;
 
 	atsr = container_of(hdr, struct acpi_dmar_atsr, header);
@@ -4562,7 +4562,7 @@ int dmar_iommu_notify_scope_dev(struct d
 	struct acpi_dmar_atsr *atsr;
 	struct acpi_dmar_reserved_memory *rmrr;
 
-	if (!intel_iommu_enabled && system_state != SYSTEM_BOOTING)
+	if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING)
 		return 0;
 
 	list_for_each_entry(rmrru, &dmar_rmrr_units, list) {
maintainer Acked-by on v1 11/18
#25 lore raw

Re: [patch 11/18] iommu/of: Adjust system_state check

In reply to Thomas Gleixner
Quoted 9 lines · Thomas Gleixner

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in of_iommu_driver_present() to handle the extra states.

Cc Joerg Roedel <[email protected]>
Cc iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
reviewer Reviewed-by on v1 14/18
#31 lore raw

Re: [patch 14/18] printk: Adjust system_state checks

In reply to Thomas Gleixner
Quoted 7 lines · On Sun, 14 May 2017 20:27:30 +0200 Thomas Gleixner <[email protected]> wrote:

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in boot_delay_msec() to handle the extra states.

-- Steve

Quoted 16 lines
---
 kernel/printk/printk.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
@@ -1176,7 +1176,7 @@ static void boot_delay_msec(int level)
 	unsigned long long k;
 	unsigned long timeout;
 
-	if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
+	if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING)
 		|| suppress_message_printing(level)) {
 		return;
 	}
reviewer Reviewed-by on v1 15/18
#33 lore raw

Re: [patch 15/18] mm/vmscan: Adjust system_state checks

In reply to Thomas Gleixner
Quoted 6 lines · On Sun, 14 May 2017 20:27:31 +0200 Thomas Gleixner <[email protected]> wrote:

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Adjust the system_state check in kswapd_run() to handle the extra states.

-- Steve

Quoted 14 lines
Cc Andrew Morton <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Vlastimil Babka <[email protected]>
---
 mm/vmscan.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

mm/vmscan.c int kswapd_run(int nid) · line 3649

Quoted 8 lines
mm/vmscan.c +1 -1
@@ -3643,7 +3643,7 @@ int kswapd_run(int nid)
 	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
 	if (IS_ERR(pgdat->kswapd)) {
 		/* failure at boot is fatal */
-		BUG_ON(system_state == SYSTEM_BOOTING);
+		BUG_ON(system_state < SYSTEM_RUNNING);
 		pr_err("Failed to start kswapd on node %d\n", nid);
 		ret = PTR_ERR(pgdat->kswapd);
 		pgdat->kswapd = NULL;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

reviewer Reviewed-by on v1 16/18
#36 lore raw

Re: [patch 16/18] init: Introduce SYSTEM_BOOTING_UP/SMP states

In reply to Thomas Gleixner
Quoted 10 lines · On Sun, 14 May 2017 20:27:32 +0200 Thomas Gleixner <[email protected]> wrote:

might_sleep() debugging should be active right after the scheduler starts working and smp_processor_id() debugging right before the first non boot cpu is brought up.

Add two new states which allow to enable those checks earlier and add them to the xen do_poweroff() function.

No functional change.

-- Steve

Quoted 29 lines
Cc Boris Ostrovsky <[email protected]>
---
 drivers/xen/manage.c   |    2 ++
 include/linux/kernel.h |    2 ++
 2 files changed, 4 insertions(+)
drivers/xen/manage.c +2 -0
@@ -190,6 +190,8 @@ static void do_poweroff(void)
 {
 	switch (system_state) {
 	case SYSTEM_BOOTING:
+	case SYSTEM_BOOTING_UP:
+	case SYSTEM_BOOTING_SMP:
 		orderly_poweroff(true);
 		break;
 	case SYSTEM_RUNNING:
include/linux/kernel.h +2 -0
@@ -493,6 +493,8 @@ extern bool early_boot_irqs_disabled;
 /* Values used for system_state */
 extern enum system_states {
 	SYSTEM_BOOTING,
+	SYSTEM_BOOTING_UP,
+	SYSTEM_BOOTING_SMP,
 	SYSTEM_RUNNING,
 	SYSTEM_HALT,
 	SYSTEM_POWER_OFF,
#38 lore raw

Re: [patch 17/18] sched: Enable might_sleep() checks early

In reply to Thomas Gleixner
Quoted 6 lines · On Sun, 14 May 2017 20:27:33 +0200 Thomas Gleixner <[email protected]> wrote:

might_sleep() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Enable it right when the scheduler starts working, i.e. when init task and kthreadd have been created and right before the idle task enables preemption.

Looking at commit b433c3d4549ae749, it appears that on very slow machines, there is a possibility that the init task can start running. Should system_state be updated before that complete() is called?

-- Steve

Quoted 33 lines
---
 init/main.c         |    2 ++
 kernel/sched/core.c |    4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
init/main.c +2 -0
@@ -410,6 +410,8 @@ static noinline void __ref rest_init(voi
 	 * at least once to get things moving:
 	 */
 	init_idle_bootup_task(current);
+	/* Enable might_sleep() checks */
+	system_state = SYSTEM_BOOTING_UP;
 	schedule_preempt_disabled();
 	/* Call into cpu_idle with preempt disabled */
 	cpu_startup_entry(CPUHP_ONLINE);
kernel/sched/core.c +3 -1
@@ -6226,8 +6226,10 @@ void ___might_sleep(const char *file, in
 
 	if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
 	     !is_idle_task(current)) ||
-	    system_state != SYSTEM_RUNNING || oops_in_progress)
+	    system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
+	    oops_in_progress)
 		return;
+
 	if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
 		return;
 	prev_jiffy = jiffies;
reviewer Reviewed-by on v1 18/18
#44 lore raw

Re: [patch 18/18] sched: Enable smp_processor_id() checks early

In reply to Thomas Gleixner
Quoted 21 lines · On Sun, 14 May 2017 20:27:34 +0200 Thomas Gleixner <[email protected]> wrote:

smp_processor_id() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Enable it right before the first non-boot CPU is brought up.

---
 init/main.c            |    3 +++
 lib/smp_processor_id.c |    2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
---
 init/main.c            |    3 +++
 lib/smp_processor_id.c |    2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

init/main.c static noinline void __init kernel_init_ · line 1030

Quoted 6 lines
init/main.c +3 -0
@@ -1025,6 +1025,9 @@ static noinline void __init kernel_init_
 	 */
 	set_cpus_allowed_ptr(current, cpumask_of(raw_smp_processor_id()));
 
+	/* Enable smp_processor_id() checks */
+	system_state = SYSTEM_BOOTING_SMP;
+

Just a nit, but why set this here and not just before smp_prepare_cpus()? Anyway...

-- Steve

Quoted 15 lines
init/main.c +0 -0
 	cad_pid = task_pid(current);
 
 	smp_prepare_cpus(setup_max_cpus);
lib/smp_processor_id.c +1 -1
@@ -28,7 +28,7 @@ notrace static unsigned int check_preemp
 	/*
 	 * It is valid to assume CPU-locality during early bootup:
 	 */
-	if (system_state != SYSTEM_RUNNING)
+	if (system_state < SYSTEM_BOOTING_SMP)
 		goto out;
 
 	/*
#45 lore raw

Re: [patch 18/18] sched: Enable smp_processor_id() checks early

Quoted 27 lines · Steven Rostedt
Quoted 24 lines · On Sun, 14 May 2017 20:27:34 +0200 Thomas Gleixner <[email protected]> wrote:

smp_processor_id() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Enable it right before the first non-boot CPU is brought up.

---
 init/main.c            |    3 +++
 lib/smp_processor_id.c |    2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
---
 init/main.c            |    3 +++
 lib/smp_processor_id.c |    2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
init/main.c +0 -0
@@ -1025,6 +1025,9 @@ static noinline void __init kernel_init_
 	 */
 	set_cpus_allowed_ptr(current, cpumask_of(raw_smp_processor_id()));
 
Quoted 6 lines

init/main.c static noinline void __init kernel_init_ · line 1030

Quoted 3 lines
init/main.c +3 -0
+	/* Enable smp_processor_id() checks */
+	system_state = SYSTEM_BOOTING_SMP;
+

Just a nit, but why set this here and not just before smp_prepare_cpus()? Anyway...

Because smp_prepare_cpus() is full of "UP" assumptions. Thinking more about it, we just can set the cpus allowed ptr right after creating it and just have a single state which enables both checks. I'll have a look.

Thanks,

tglx

#39 lore raw

Re: [patch 17/18] sched: Enable might_sleep() checks early

In reply to Steven Rostedt
Quoted 6 lines · Steven Rostedt
Quoted 3 lines · On Sun, 14 May 2017 20:27:33 +0200 Thomas Gleixner <[email protected]> wrote:

might_sleep() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Quoted 7 lines
Quoted 3 lines

Enable it right when the scheduler starts working, i.e. when init task and kthreadd have been created and right before the idle task enables preemption.

Looking at commit b433c3d4549ae749, it appears that on very slow machines, there is a possibility that the init task can start running. Should system_state be updated before that complete() is called?

That commit is magic voodoo with exactly no effect at all.

rest_init() is called with preemption disabled and nothing can schedule there _before_ schedule_preempt_disabled().

Both threads - init task and kthreadd - are only created and woken up. They cannot get on the CPU simply because preemption is disabled. And this was the case back then in 2.6.35 as well.

It does not matter at all whether the machine is slow or not. That completion is pointless.

Peter, can you explain what the heck this patch is actually doing?

Thanks,

tglx

maintainer
#40 lore raw

Re: [patch 17/18] sched: Enable might_sleep() checks early

Quoted 21 lines · Thomas Gleixner
Quoted 6 lines · Steven Rostedt
Quoted 3 lines · On Sun, 14 May 2017 20:27:33 +0200 Thomas Gleixner <[email protected]> wrote:

might_sleep() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Quoted 7 lines
Quoted 3 lines

Enable it right when the scheduler starts working, i.e. when init task and kthreadd have been created and right before the idle task enables preemption.

Looking at commit b433c3d4549ae749, it appears that on very slow machines, there is a possibility that the init task can start running. Should system_state be updated before that complete() is called?

That commit is magic voodoo with exactly no effect at all.

rest_init() is called with preemption disabled and nothing can schedule there _before_ schedule_preempt_disabled().

Quoted 8 lines

Both threads - init task and kthreadd - are only created and woken up. They cannot get on the CPU simply because preemption is disabled. And this was the case back then in 2.6.35 as well.

It does not matter at all whether the machine is slow or not. That completion is pointless.

Peter, can you explain what the heck this patch is actually doing?

Argh.. what a shit Changelog, who wrote that crap!?

So the problem was with PREEMPT_VOLUNTARY (where, as you know, preempt_disable() has no meaning).

Supposedly there's a might_sleep()/cond_resched() point somewhere around there (every alloc in the fork path for example), which will happily reschedule us.

So if we schedule to the kernel_init() task before we set kthreadd_task we'll try and spawn kthreads and OOPS.

#41 lore raw

Re: [patch 17/18] sched: Enable might_sleep() checks early

Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Thomas Gleixner
Quoted 6 lines · Steven Rostedt
Quoted 3 lines · On Sun, 14 May 2017 20:27:33 +0200 Thomas Gleixner <[email protected]> wrote:

might_sleep() checks are enabled after the boot process is done. That hides bugs in the smp bringup and driver initialization code.

Quoted 7 lines
Quoted 3 lines

Enable it right when the scheduler starts working, i.e. when init task and kthreadd have been created and right before the idle task enables preemption.

Looking at commit b433c3d4549ae749, it appears that on very slow machines, there is a possibility that the init task can start running. Should system_state be updated before that complete() is called?

That commit is magic voodoo with exactly no effect at all.

rest_init() is called with preemption disabled and nothing can schedule there _before_ schedule_preempt_disabled().

Both threads - init task and kthreadd - are only created and woken up. They cannot get on the CPU simply because preemption is disabled. And this was the case back then in 2.6.35 as well.

Quoted 6 lines
Quoted 4 lines

It does not matter at all whether the machine is slow or not. That completion is pointless.

Peter, can you explain what the heck this patch is actually doing?

Argh.. what a shit Changelog, who wrote that crap!?

Indeed.

Quoted 6 lines

So the problem was with PREEMPT_VOLUNTARY (where, as you know, preempt_disable() has no meaning).

Supposedly there's a might_sleep()/cond_resched() point somewhere around there (every alloc in the fork path for example), which will happily reschedule us.

Darn, forgot about PREEMPT_VOLUNTARY and that excellent changelog does not mention it either.

Quoted 2 lines

So if we schedule to the kernel_init() task before we set kthreadd_task we'll try and spawn kthreads and OOPS.

So back to Stevens question. No, we can't set the state earlier than right before schedule() simply because with PREEMPT preemption _is_ actually disabled and kernel_kthread() will trigger might_sleep() splats.

What a mess.

Thanks,

tglx

#42 lore raw

Re: [patch 17/18] sched: Enable might_sleep() checks early

Quoted 9 lines · On Tue, 16 May 2017 09:33:52 +0200 (CEST) Thomas Gleixner <[email protected]>…

Darn, forgot about PREEMPT_VOLUNTARY and that excellent changelog does not mention it either.

Quoted 2 lines

So if we schedule to the kernel_init() task before we set kthreadd_task we'll try and spawn kthreads and OOPS.

So back to Stevens question. No, we can't set the state earlier than right before schedule() simply because with PREEMPT preemption _is_ actually disabled and kernel_kthread() will trigger might_sleep() splats.

So a comment in the code mentioning PREEMPT_VOLUNTARY might be advantageous.

Quoted 2 lines

What a mess.

Indeed.

-- Steve

#14 lore raw

Re: [patch 07/18] ACPI: Adjust system_state check

In reply to Steven Rostedt
Quoted 33 lines · Steven Rostedt

[ added linux-acpi and took off arm list ;-) ]

Quoted 18 lines · On Sun, 14 May 2017 20:27:23 +0200 Thomas Gleixner <[email protected]> wrote:

To enable smp_processor_id() and might_sleep() debug checks earlier, it's required to add system states between SYSTEM_BOOTING and SYSTEM_RUNNING.

Make the decision wether a pci root is hotplugged depend on SYSTEM_RUNNING instead of !SYSTEM_BOOTING.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Mark Rutland <[email protected]>
---
 drivers/acpi/pci_root.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

drivers/acpi/pci_root.c static int acpi_pci_root_add(struct acpi · line 526

Quoted 5 lines
drivers/acpi/pci_root.c +1 -1
@@ -523,7 +523,7 @@ static int acpi_pci_root_add(struct acpi
 	struct acpi_pci_root *root;
 	acpi_handle handle = device->handle;
 	int no_aspm = 0;
-	bool hotadd = system_state != SYSTEM_BOOTING;
+	bool hotadd = system_state == SYSTEM_RUNNING;

What about the other boot states greater than SYSTEM_RUNNING? Can this be called then too?

Quoted 3 lines

IOW, should it be:

	bool hotadd = system_state >= SYSTEM_RUNNING;

Greater than RUNNING is HALT,POWEROFF,RESTART. Having a PCI hotplug event there would be interesting :) I'll amend the changelog.

Thanks,

tglx