diff mbox series

initcall: break loop immediately on failure

Message ID 20240219183519.2183405-1-caleb.connolly@linaro.org
State Accepted
Commit 7554388c1d10e3b44714cb8e4cbcc17f20141eec
Headers show
Series initcall: break loop immediately on failure | expand

Commit Message

Caleb Connolly Feb. 19, 2024, 6:35 p.m. UTC
The current ordering always results in func pointing to the next
function in the init_sequence. e.g. if fdtdec_setup() fails, ret will
be set to the error code, then func will be updated to point to
initf_malloc(), only then is ret checked and the loop broken. The end
result of this is that the "initcall failed at ..." error will point you
to initf_malloc(), when the error actually occured in fdtdec_setup()!

This can be quite confusing and result in a lot of time wasted debugging
code that has nothing to do with the failure (ask me how I know :P).

Adjust the for loop to check ret immediately after the call and break
early so that func will correctly reference the failed function.

Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
---
 lib/initcall.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Dan Carpenter Feb. 20, 2024, 5:30 p.m. UTC | #1
On Mon, Feb 19, 2024 at 06:35:03PM +0000, Caleb Connolly wrote:
> The current ordering always results in func pointing to the next
> function in the init_sequence. e.g. if fdtdec_setup() fails, ret will
> be set to the error code, then func will be updated to point to
> initf_malloc(), only then is ret checked and the loop broken. The end
> result of this is that the "initcall failed at ..." error will point you
> to initf_malloc(), when the error actually occured in fdtdec_setup()!
> 
> This can be quite confusing and result in a lot of time wasted debugging
> code that has nothing to do with the failure (ask me how I know :P).

Heh.  Subtle.

> 
> Adjust the for loop to check ret immediately after the call and break
> early so that func will correctly reference the failed function.
> 
> Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
> ---
>  lib/initcall.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/initcall.c b/lib/initcall.c
> index ce317af213ab..128242e5ff55 100644
> --- a/lib/initcall.c
> +++ b/lib/initcall.c
> @@ -52,11 +52,11 @@ int initcall_run_list(const init_fnc_t init_sequence[])
>  	ulong reloc_ofs = calc_reloc_ofs();
>  	const init_fnc_t *ptr;
>  	enum event_t type;
>  	init_fnc_t func;
> -	int ret = 0;
> +	int ret;

We need to keep this initialization.  The bug would be when we're
building for CONFIG_ARC and this is called from board_init_f_r().  The
array is empty except for the NULL sentinal at the end.  We don't
enter the loop in that case.

regards,
dan carpenter

>  
> -	for (ptr = init_sequence; func = *ptr, !ret && func; ptr++) {
> +	for (ptr = init_sequence; func = *ptr, func; ptr++) {
>  		type = initcall_is_event(func);
>  
>  		if (type) {
>  			if (!CONFIG_IS_ENABLED(EVENT))
> @@ -70,8 +70,10 @@ int initcall_run_list(const init_fnc_t init_sequence[])
>  			debug("initcall: %p\n", (char *)func - reloc_ofs);
>  		}
>  
>  		ret = type ? event_notify_null(type) : func();
> +		if (ret)
> +			break;
>  	}
>  
>  	if (ret) {
>  		if (CONFIG_IS_ENABLED(EVENT)) {
> -- 
> 2.43.1
>
diff mbox series

Patch

diff --git a/lib/initcall.c b/lib/initcall.c
index ce317af213ab..128242e5ff55 100644
--- a/lib/initcall.c
+++ b/lib/initcall.c
@@ -52,11 +52,11 @@  int initcall_run_list(const init_fnc_t init_sequence[])
 	ulong reloc_ofs = calc_reloc_ofs();
 	const init_fnc_t *ptr;
 	enum event_t type;
 	init_fnc_t func;
-	int ret = 0;
+	int ret;
 
-	for (ptr = init_sequence; func = *ptr, !ret && func; ptr++) {
+	for (ptr = init_sequence; func = *ptr, func; ptr++) {
 		type = initcall_is_event(func);
 
 		if (type) {
 			if (!CONFIG_IS_ENABLED(EVENT))
@@ -70,8 +70,10 @@  int initcall_run_list(const init_fnc_t init_sequence[])
 			debug("initcall: %p\n", (char *)func - reloc_ofs);
 		}
 
 		ret = type ? event_notify_null(type) : func();
+		if (ret)
+			break;
 	}
 
 	if (ret) {
 		if (CONFIG_IS_ENABLED(EVENT)) {