Skip to contentSkip to conversation

ARM: MCPM: don't explode if invoked without being initialized first

PATCH New Nicolas Pitre 2 participants 5 messages Sep 23, 2013 – Sep 24, 2013 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: MCPM: don't explode if invoked without being initialized first

Currently mcpm_cpu_power_down() and mcpm_cpu_suspend() trigger BUG() if mcpm_platform_register() is not called beforehand. This may occur for many reasons such as some incomplete device tree passed to the kernel or the like.

Let's be nicer to users and avoid killing the kernel if that happens by logging a warning and returning to the caller. The mcpm_cpu_suspend() user is already set to deal with this situation, and so is cpu_die() invoking mcpm_cpu_die().

The problematic case would have been the B.L switcher's usage of mcpm_cpu_power_down(), however it has to call mcpm_cpu_power_up() first which is already set to catch an error resulting from a missing mcpm_platform_register() call.

arch/arm/common/mcpm_entry.c +4 -2
index 370236dd1a..3fd43f1fd2 100644
@@ -51,7 +51,8 @@ void mcpm_cpu_power_down(void)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops))
+		return;
 	BUG_ON(!irqs_disabled());
 
 	/*
@@ -93,7 +94,8 @@ void mcpm_cpu_suspend(u64 expected_residency)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops))
+		return;
 	BUG_ON(!irqs_disabled());
 
 	/* Very similar to mcpm_cpu_power_down() */
arch/arm/include/asm/mcpm.h +10 -4
index 0f7b7620e9..fc82a88f5b 100644
@@ -76,8 +76,11 @@ int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_power_down(void);
 
@@ -98,8 +101,11 @@ void mcpm_cpu_power_down(void);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_suspend(u64 expected_residency);
 
#1 lore raw

PATCH 1/1 ARM: MCPM: don't explode if invoked without being initialized first

Quoted 23 lines · Nicolas Pitre

Currently mcpm_cpu_power_down() and mcpm_cpu_suspend() trigger BUG() if mcpm_platform_register() is not called beforehand. This may occur for many reasons such as some incomplete device tree passed to the kernel or the like.

Let's be nicer to users and avoid killing the kernel if that happens by logging a warning and returning to the caller. The mcpm_cpu_suspend() user is already set to deal with this situation, and so is cpu_die() invoking mcpm_cpu_die().

The problematic case would have been the B.L switcher's usage of mcpm_cpu_power_down(), however it has to call mcpm_cpu_power_up() first which is already set to catch an error resulting from a missing mcpm_platform_register() call.

arch/arm/common/mcpm_entry.c +0 -0
index 370236dd1a..3fd43f1fd2 100644

arch/arm/common/mcpm_entry.c void mcpm_cpu_power_down(void) · line 56

Quoted 7 lines
arch/arm/common/mcpm_entry.c +2 -1
@@ -51,7 +51,8 @@ void mcpm_cpu_power_down(void)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops))
+		return;
 	BUG_ON(!irqs_disabled());

I think it should be:

if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))

	return;

since even if platform_ops has been initialized we might still be end up with a NULL function pointer.

Probably the test for the function pointer belongs in:

mcpm_platform_register()

arch/arm/common/mcpm_entry.c void mcpm_cpu_suspend(u64 expected_residency) · line 98

Quoted 9 lines
arch/arm/common/mcpm_entry.c +2 -1
 	/*
@@ -93,7 +94,8 @@ void mcpm_cpu_suspend(u64 expected_residency)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops))
+		return;

Ditto.

Thanks,
Lorenzo

Quoted 36 lines
arch/arm/common/mcpm_entry.c +0 -0
 	BUG_ON(!irqs_disabled());
 
 	/* Very similar to mcpm_cpu_power_down() */
arch/arm/include/asm/mcpm.h +10 -4
index 0f7b7620e9..fc82a88f5b 100644
@@ -76,8 +76,11 @@ int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_power_down(void);
 
@@ -98,8 +101,11 @@ void mcpm_cpu_power_down(void);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_suspend(u64 expected_residency);
 
#2 lore raw

PATCH 1/1 ARM: MCPM: don't explode if invoked without being initialized first

Quoted 34 lines · Lorenzo Pieralisi
Quoted 23 lines · Nicolas Pitre

Currently mcpm_cpu_power_down() and mcpm_cpu_suspend() trigger BUG() if mcpm_platform_register() is not called beforehand. This may occur for many reasons such as some incomplete device tree passed to the kernel or the like.

Let's be nicer to users and avoid killing the kernel if that happens by logging a warning and returning to the caller. The mcpm_cpu_suspend() user is already set to deal with this situation, and so is cpu_die() invoking mcpm_cpu_die().

The problematic case would have been the B.L switcher's usage of mcpm_cpu_power_down(), however it has to call mcpm_cpu_power_up() first which is already set to catch an error resulting from a missing mcpm_platform_register() call.

arch/arm/common/mcpm_entry.c +0 -0
index 370236dd1a..3fd43f1fd2 100644

arch/arm/common/mcpm_entry.c void mcpm_cpu_power_down(void) · line 56

Quoted 7 lines
arch/arm/common/mcpm_entry.c +2 -1
@@ -51,7 +51,8 @@ void mcpm_cpu_power_down(void)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops))
+		return;
 	BUG_ON(!irqs_disabled());

I think it should be:

Quoted 5 lines

if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))

	return;

since even if platform_ops has been initialized we might still be end up with a NULL function pointer.

Good point.

Quoted 3 lines

Probably the test for the function pointer belongs in:

mcpm_platform_register()

Well, some methods may be optional. For example there is no suspend support possible with the DCSCB backend. And you can't do much in mcpm_platform_register() other than refuse the whole registration even though some methods are still usable.

Amended patch below.

From: Nicolas Pitre <[email protected]>
Date: Mon, 23 Sep 2013 14:08:05 -0400
Subject: [PATCH] ARM: MCPM: don't explode if invoked without being initialized first

Currently mcpm_cpu_power_down() and mcpm_cpu_suspend() trigger BUG() if mcpm_platform_register() is not called beforehand. This may occur for many reasons such as some incomplete device tree passed to the kernel or the like.

Let's be nicer to users and avoid killing the kernel if that happens by logging a warning and returning to the caller. The mcpm_cpu_suspend() user is already set to deal with this situation, and so is cpu_die() invoking mcpm_cpu_die().

The problematic case would have been the B.L switcher's usage of mcpm_cpu_power_down(), however it has to call mcpm_cpu_power_up() first which is already set to catch an error resulting from a missing mcpm_platform_register() call.

arch/arm/common/mcpm_entry.c +4 -2
index 370236dd1a..990250965f 100644
@@ -51,7 +51,8 @@ void mcpm_cpu_power_down(void)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))
+		return;
 	BUG_ON(!irqs_disabled());
 
 	/*
@@ -93,7 +94,8 @@ void mcpm_cpu_suspend(u64 expected_residency)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops || !platform_ops->suspend))
+		return;
 	BUG_ON(!irqs_disabled());
 
 	/* Very similar to mcpm_cpu_power_down() */
arch/arm/include/asm/mcpm.h +10 -4
index 0f7b7620e9..fc82a88f5b 100644
@@ -76,8 +76,11 @@ int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_power_down(void);
 
@@ -98,8 +101,11 @@ void mcpm_cpu_power_down(void);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_suspend(u64 expected_residency);
 
#3 lore raw

PATCH 1/1 ARM: MCPM: don't explode if invoked without being initialized first

Quoted 121 lines · Nicolas Pitre
Quoted 34 lines · Lorenzo Pieralisi
Quoted 23 lines · Nicolas Pitre

Currently mcpm_cpu_power_down() and mcpm_cpu_suspend() trigger BUG() if mcpm_platform_register() is not called beforehand. This may occur for many reasons such as some incomplete device tree passed to the kernel or the like.

Let's be nicer to users and avoid killing the kernel if that happens by logging a warning and returning to the caller. The mcpm_cpu_suspend() user is already set to deal with this situation, and so is cpu_die() invoking mcpm_cpu_die().

The problematic case would have been the B.L switcher's usage of mcpm_cpu_power_down(), however it has to call mcpm_cpu_power_up() first which is already set to catch an error resulting from a missing mcpm_platform_register() call.

arch/arm/common/mcpm_entry.c +0 -0
index 370236dd1a..3fd43f1fd2 100644

arch/arm/common/mcpm_entry.c void mcpm_cpu_power_down(void) · line 56

Quoted 7 lines
arch/arm/common/mcpm_entry.c +2 -1
@@ -51,7 +51,8 @@ void mcpm_cpu_power_down(void)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops))
+		return;
 	BUG_ON(!irqs_disabled());

I think it should be:

Quoted 5 lines

if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))

	return;

since even if platform_ops has been initialized we might still be end up with a NULL function pointer.

Good point.

Quoted 3 lines

Probably the test for the function pointer belongs in:

mcpm_platform_register()

Well, some methods may be optional. For example there is no suspend support possible with the DCSCB backend. And you can't do much in mcpm_platform_register() other than refuse the whole registration even though some methods are still usable.

Amended patch below.

From: Nicolas Pitre <[email protected]>
Date: Mon, 23 Sep 2013 14:08:05 -0400
Subject: [PATCH] ARM: MCPM: don't explode if invoked without being initialized first

Currently mcpm_cpu_power_down() and mcpm_cpu_suspend() trigger BUG() if mcpm_platform_register() is not called beforehand. This may occur for many reasons such as some incomplete device tree passed to the kernel or the like.

Let's be nicer to users and avoid killing the kernel if that happens by logging a warning and returning to the caller. The mcpm_cpu_suspend() user is already set to deal with this situation, and so is cpu_die() invoking mcpm_cpu_die().

The problematic case would have been the B.L switcher's usage of mcpm_cpu_power_down(), however it has to call mcpm_cpu_power_up() first which is already set to catch an error resulting from a missing mcpm_platform_register() call.

arch/arm/common/mcpm_entry.c +4 -2
index 370236dd1a..990250965f 100644
@@ -51,7 +51,8 @@ void mcpm_cpu_power_down(void)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops || !platform_ops->power_down))
+		return;
 	BUG_ON(!irqs_disabled());
 
 	/*
@@ -93,7 +94,8 @@ void mcpm_cpu_suspend(u64 expected_residency)
 {
 	phys_reset_t phys_reset;
 
-	BUG_ON(!platform_ops);
+	if (WARN_ON_ONCE(!platform_ops || !platform_ops->suspend))
+		return;
 	BUG_ON(!irqs_disabled());
 
 	/* Very similar to mcpm_cpu_power_down() */
arch/arm/include/asm/mcpm.h +5 -2
index 0f7b7620e9..fc82a88f5b 100644
@@ -76,8 +76,11 @@ int mcpm_cpu_power_up(unsigned int cpu, unsigned int cluster);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_power_down(void);
 
@@ -98,8 +101,11 @@ void mcpm_cpu_power_down(void);
  *
  * This must be called with interrupts disabled.

arch/arm/include/asm/mcpm.h void mcpm_cpu_power_down(void); · line 110

Quoted 10 lines
arch/arm/include/asm/mcpm.h +5 -2
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
  */
 void mcpm_cpu_suspend(u64 expected_residency);

Ok, it is fine as it is, but at the risk of sounding pedantic, I thought about it and I reckon that it would be nicer to return an error value instead of just returning. In the bigger picture we might not want to always resume from the reset entry point, I think that adding a return value is worth the hassle, given that it is a trivial change. Resuming through the reset vector implies a successful cpu_suspend call (even if eg TC2 wfi is skipped), we may want for instance to monitor aborted shutdowns or the like.

Again, a return value gives us flexibility and it costs nothing, you can postpone it till the API is refactored though (residency parameter, etc).

Thanks,
Lorenzo

#4 lore raw

PATCH 1/1 ARM: MCPM: don't explode if invoked without being initialized first

Quoted 12 lines · Lorenzo Pieralisi
Quoted 11 lines · Nicolas Pitre
arch/arm/include/asm/mcpm.h +5 -2
@@ -98,8 +101,11 @@ void mcpm_cpu_power_down(void);
  *
  * This must be called with interrupts disabled.
  *
- * This does not return.  Re-entry in the kernel is expected via
- * mcpm_entry_point.
+ * On success this does not return.  Re-entry in the kernel is expected
+ * via mcpm_entry_point.
+ *
+ * This will return if mcpm_platform_register() has not been called
+ * previously in which case the caller should take appropriate action.
Quoted 8 lines

arch/arm/include/asm/mcpm.h void mcpm_cpu_power_down(void); · line 110

Quoted 2 lines
arch/arm/include/asm/mcpm.h +0 -0
  */
 void mcpm_cpu_suspend(u64 expected_residency);

Ok, it is fine as it is, but at the risk of sounding pedantic, I thought about it and I reckon that it would be nicer to return an error value instead of just returning. In the bigger picture we might not want to always resume from the reset entry point, I think that adding a return value is worth the hassle, given that it is a trivial change.

And given it is trivial, this can be postponed until there is a clear meaning for a return value. Right now there would be none.

Quoted 3 lines

Resuming through the reset vector implies a successful cpu_suspend call (even if eg TC2 wfi is skipped), we may want for instance to monitor aborted shutdowns or the like.

OTOH, adding a return value now is on the verge of over engineering.

Quoted 2 lines

Again, a return value gives us flexibility and it costs nothing, you can postpone it till the API is refactored though (residency parameter, etc).

Absolutely. Until there is clear semantics for those return values, I don't see the point of having any. The flexibility Linux gives us is not in the existence of a return value, but rather in the ability to add one when we really need it. And right now we don't.

So unless there is a fundamental objection to this patch I'll submit it as is and we can augment the code with return values later.

Nicolas