Skip to contentSkip to conversation

powerpc: Fix endless loop that occurs without a device tree

PATCH In review Thomas Huth 4 participants 9 messages May 30, 2017 – May 31, 2017 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: Fix endless loop that occurs without a device tree

When running a powerpc kvm-unit-test, and there is accidentially no device tree available, the test ends up in an endless loop, spamming the console with "rtas_node: /rtas: FDT_ERR_BADMAGIC"
messages: Somewhere the code calls abort() due to the missing device tree, and abort() calls exit() which in turn tries to shut down the VM with rtas_power_off(). rtas_power_off() needs the device tree again to look up the right RTAS token and we then end up in the next iteration.
Fix it by adding some proper checks to rtas_power_off() and rtas_token().

---
 lib/powerpc/rtas.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/powerpc/rtas.c +12 -1
index 3407e25..a1c560b 100644
@@ -74,6 +74,9 @@ int rtas_token(const char *service)
 	const struct fdt_property *prop;
 	u32 *token;
 
+	if (!dt_available())
+		return RTAS_UNKNOWN_SERVICE;
+
 	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
 	if (prop) {
 		token = (u32 *)prop->data;
@@ -116,6 +119,14 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
 void rtas_power_off(void)
 {
-	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {
+		puts("RTAS power-off not available\n");
+		return;
+	}
+
+	ret = rtas_call(token, 2, 1, NULL, -1, -1);
 	printf("RTAS power-off returned %d\n", ret);
 }
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 128

Quoted 8 lines · Thomas Huth

, NULL, -1, -1);

lib/powerpc/rtas.c +7 -0
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {
+		puts("RTAS power-off not available\n");
+		return;
+	}

Should this do some kind of infinite loop (Linux arch/x86 has play_dead which does cli;hlt, not sure if there's something similar for sPAPR)?

Thanks,

Paolo

#2 lore raw
Quoted 14 lines · Paolo Bonzini

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 128

Quoted 8 lines · Thomas Huth

, NULL, -1, -1);

lib/powerpc/rtas.c +7 -0
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {
+		puts("RTAS power-off not available\n");
+		return;
+	}

Should this do some kind of infinite loop (Linux arch/x86 has play_dead which does cli;hlt, not sure if there's something similar for sPAPR)?

The exit() function in lib/powerpc/io.c already calls halt() after trying to run rtas_power_off() ... I think that should be enough here.

Thomas

maintainer
#3 lore raw
Quoted 16 lines · Thomas Huth
Quoted 14 lines · Paolo Bonzini

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 128

Quoted 8 lines · Thomas Huth

, NULL, -1, -1);

lib/powerpc/rtas.c +7 -0
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {
+		puts("RTAS power-off not available\n");
+		return;
+	}

Should this do some kind of infinite loop (Linux arch/x86 has play_dead which does cli;hlt, not sure if there's something similar for sPAPR)?

Quoted 2 lines

The exit() function in lib/powerpc/io.c already calls halt() after trying to run rtas_power_off() ... I think that should be enough here.

Indeed, thanks!

Paolo

#4 lore raw
In reply to Thomas Huth
Quoted 31 lines · Thomas Huth

When running a powerpc kvm-unit-test, and there is accidentially no device tree available, the test ends up in an endless loop, spamming the console with "rtas_node: /rtas: FDT_ERR_BADMAGIC"
messages: Somewhere the code calls abort() due to the missing device tree, and abort() calls exit() which in turn tries to shut down the VM with rtas_power_off(). rtas_power_off() needs the device tree again to look up the right RTAS token and we then end up in the next iteration.
Fix it by adding some proper checks to rtas_power_off() and rtas_token().

---
 lib/powerpc/rtas.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/powerpc/rtas.c +3 -0
index 3407e25..a1c560b 100644
@@ -74,6 +74,9 @@ int rtas_token(const char *service)
 	const struct fdt_property *prop;
 	u32 *token;
 
+	if (!dt_available())
+		return RTAS_UNKNOWN_SERVICE;
+
 	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
 	if (prop) {
 		token = (u32 *)prop->data;

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 125

Quoted 8 lines
lib/powerpc/rtas.c +4 -1
@@ -116,6 +119,14 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
 void rtas_power_off(void)
 {
-	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {

token = RTAS_UNKNOWN_SERVICE ?

Quoted 10 lines
lib/powerpc/rtas.c +5 -0
+		puts("RTAS power-off not available\n");
+		return;
+	}
+
+	ret = rtas_call(token, 2, 1, NULL, -1, -1);
 	printf("RTAS power-off returned %d\n", ret);
 }
Signature · 1 line
1.8.3.1

Thanks,
drew

#5 lore raw
Quoted 33 lines · Andrew Jones
Quoted 32 lines · Thomas Huth

When running a powerpc kvm-unit-test, and there is accidentially no device tree available, the test ends up in an endless loop, spamming the console with "rtas_node: /rtas: FDT_ERR_BADMAGIC"
messages: Somewhere the code calls abort() due to the missing device tree, and abort() calls exit() which in turn tries to shut down the VM with rtas_power_off(). rtas_power_off() needs the device tree again to look up the right RTAS token and we then end up in the next iteration.
Fix it by adding some proper checks to rtas_power_off() and rtas_token().

---
 lib/powerpc/rtas.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/powerpc/rtas.c +3 -0
index 3407e25..a1c560b 100644
@@ -74,6 +74,9 @@ int rtas_token(const char *service)
 	const struct fdt_property *prop;
 	u32 *token;
 
+	if (!dt_available())
+		return RTAS_UNKNOWN_SERVICE;
+
 	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
 	if (prop) {
 		token = (u32 *)prop->data;
@@ -116,6 +119,14 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
Quoted 9 lines

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 125

Quoted 7 lines
lib/powerpc/rtas.c +4 -1
 void rtas_power_off(void)
 {
-	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {

token = RTAS_UNKNOWN_SERVICE ?

Yes, that's likely better ... though the tokens are normally > 0, I just had a look at the spec and it does not say anything about whether they have to be positive or not, so negative tokens could happen, too. I'll send a v2...

Thanks,
Thomas

Quoted 14 lines
Quoted 10 lines
lib/powerpc/rtas.c +5 -0
+		puts("RTAS power-off not available\n");
+		return;
+	}
+
+	ret = rtas_call(token, 2, 1, NULL, -1, -1);
 	printf("RTAS power-off returned %d\n", ret);
 }
Signature · 1 line
1.8.3.1

Thanks,
drew

#6 lore raw
Quoted 44 lines · Thomas Huth
Quoted 33 lines · Andrew Jones
Quoted 32 lines · Thomas Huth

When running a powerpc kvm-unit-test, and there is accidentially no device tree available, the test ends up in an endless loop, spamming the console with "rtas_node: /rtas: FDT_ERR_BADMAGIC"
messages: Somewhere the code calls abort() due to the missing device tree, and abort() calls exit() which in turn tries to shut down the VM with rtas_power_off(). rtas_power_off() needs the device tree again to look up the right RTAS token and we then end up in the next iteration.
Fix it by adding some proper checks to rtas_power_off() and rtas_token().

---
 lib/powerpc/rtas.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/powerpc/rtas.c +3 -0
index 3407e25..a1c560b 100644
@@ -74,6 +74,9 @@ int rtas_token(const char *service)
 	const struct fdt_property *prop;
 	u32 *token;
 
+	if (!dt_available())
+		return RTAS_UNKNOWN_SERVICE;
+
 	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
 	if (prop) {
 		token = (u32 *)prop->data;
@@ -116,6 +119,14 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
Quoted 9 lines

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 125

Quoted 7 lines
lib/powerpc/rtas.c +4 -1
 void rtas_power_off(void)
 {
-	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {

token = RTAS_UNKNOWN_SERVICE ?

Quoted 4 lines

Yes, that's likely better ... though the tokens are normally > 0, I just had a look at the spec and it does not say anything about whether they have to be positive or not, so negative tokens could happen, too. I'll send a v2...

in rtas_token(), token is u32, so a very big token number can appear as a negative value. But how do you return an error code if the return can be negative?
Do you plan to use something like
"error = rtas_token(&token, "power-off");"?

Thanks,
Laurent

#7 lore raw
Quoted 50 lines · Laurent Vivier
Quoted 44 lines · Thomas Huth
Quoted 33 lines · Andrew Jones
Quoted 32 lines · Thomas Huth

When running a powerpc kvm-unit-test, and there is accidentially no device tree available, the test ends up in an endless loop, spamming the console with "rtas_node: /rtas: FDT_ERR_BADMAGIC"
messages: Somewhere the code calls abort() due to the missing device tree, and abort() calls exit() which in turn tries to shut down the VM with rtas_power_off(). rtas_power_off() needs the device tree again to look up the right RTAS token and we then end up in the next iteration.
Fix it by adding some proper checks to rtas_power_off() and rtas_token().

---
 lib/powerpc/rtas.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/powerpc/rtas.c +3 -0
index 3407e25..a1c560b 100644
@@ -74,6 +74,9 @@ int rtas_token(const char *service)
 	const struct fdt_property *prop;
 	u32 *token;
 
+	if (!dt_available())
+		return RTAS_UNKNOWN_SERVICE;
+
 	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
 	if (prop) {
 		token = (u32 *)prop->data;
@@ -116,6 +119,14 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
Quoted 9 lines

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 125

Quoted 7 lines
lib/powerpc/rtas.c +4 -1
 void rtas_power_off(void)
 {
-	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	int token, ret;
+
+	token = rtas_token("power-off");
+	if (token < 0) {

token = RTAS_UNKNOWN_SERVICE ?

Quoted 4 lines

Yes, that's likely better ... though the tokens are normally > 0, I just had a look at the spec and it does not say anything about whether they have to be positive or not, so negative tokens could happen, too. I'll send a v2...

Quoted 5 lines

in rtas_token(), token is u32, so a very big token number can appear as a negative value. But how do you return an error code if the return can be negative?
Do you plan to use something like
"error = rtas_token(&token, "power-off");"?

Hmmm, though it is very unlikely that we ever encounter an RTAS implementation that uses 0xffffffff as token, it still could theoretically happen.
So I guess I have to bite the bullet, implement something like you suggested and change all spots that currently use rtas_token()...

Thomas

Reviewed-by on v1 1/1
#8 lore raw
In reply to Thomas Huth
Quoted 37 lines · Thomas Huth

When running a powerpc kvm-unit-test, and there is accidentially no device tree available, the test ends up in an endless loop, spamming the console with "rtas_node: /rtas: FDT_ERR_BADMAGIC"
messages: Somewhere the code calls abort() due to the missing device tree, and abort() calls exit() which in turn tries to shut down the VM with rtas_power_off(). rtas_power_off() needs the device tree again to look up the right RTAS token and we then end up in the next iteration.
Fix it by adding some proper checks to rtas_power_off() and rtas_token().

---
 lib/powerpc/rtas.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/powerpc/rtas.c +4 -1
index 3407e25..a1c560b 100644
@@ -74,6 +74,9 @@ int rtas_token(const char *service)
 	const struct fdt_property *prop;
 	u32 *token;
 
+	if (!dt_available())
+		return RTAS_UNKNOWN_SERVICE;
+
 	prop = fdt_get_property(dt_fdt(), rtas_node(), service, NULL);
 	if (prop) {
 		token = (u32 *)prop->data;
@@ -116,6 +119,14 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
 void rtas_power_off(void)
 {
-	int ret = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
+	int token, ret;

lib/powerpc/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 132

Quoted 10 lines
lib/powerpc/rtas.c +8 -0
+
+	token = rtas_token("power-off");
+	if (token < 0) {
+		puts("RTAS power-off not available\n");
+		return;
+	}
+
+	ret = rtas_call(token, 2, 1, NULL, -1, -1);
 	printf("RTAS power-off returned %d\n", ret);
 }