Skip to contentSkip to conversation

init: fix -Wmissing-variable-declarations clang warning

PATCH In review Justin Stitt 2 participants 3 messages Sep 5, 2023 – Sep 11, 2023 VFS

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 init: fix -Wmissing-variable-declarations clang warning

When building x86/defconfig with Clang-18 I encounter the following warning:

| init/main.c:189:13: warning: no previous extern declaration for non-static variable 'envp_init' [-Wmissing-variable-declarations]
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| init/main.c:189:7: note: declare 'static' if the variable is not intended to be used outside of this translation unit
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };

Make envp_init static and provide handle_initrd its own copy.

This silences the warning and makes the code more readable as you no longer have to track down extern definitions inside of handle_initrd. It is now more self-contained.

---
The kernel test robot didn't like v1 of this patch and I prefer Kees' approach to simplifying this code anyways so I'm sending this v2. Hopefully the CI builds clean (as I am locally using their repro).

Changes in v2:
- Make envp_init static and provide handle_initrd() with a copy (thanks Kees)
- Rebase onto mainline (2dde18cd1d8fa)
- Link to v1: https://lore.kernel.org/r/20230830-missingvardecl2-init-main-c-v1-1-59007a637259@google.com
---
Note: build-tested only.

It should be noted that handle_initrd has been marked as deprecated and perhaps the entire thing can be removed as per it's own message:
| using deprecated initrd support, will be removed in 2021.

---
 init/do_mounts_initrd.c | 4 ++--
 init/main.c             | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
init/do_mounts_initrd.c +2 -2
index 425f4bcf4b77..154bd0de85a6 100644
@@ -87,7 +87,7 @@ static void __init handle_initrd(char *root_device_name)
 {
 	struct subprocess_info *info;
 	static char *argv[] = { "linuxrc", NULL, };
-	extern char *envp_init[];
+	static char *envp[] = { "HOME=/", "TERM=linux", NULL, };
 	int error;
 
 	pr_warn("using deprecated initrd support, will be removed in 2021.\n");
@@ -100,7 +100,7 @@ static void __init handle_initrd(char *root_device_name)
 	init_mkdir("/old", 0700);
 	init_chdir("/old");
 
-	info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
+	info = call_usermodehelper_setup("/linuxrc", argv, envp,
 					 GFP_KERNEL, init_linuxrc, NULL, NULL);
 	if (!info)
 		return;
init/main.c +1 -1
index ad920fac325c..9a473107bb8f 100644
@@ -186,7 +186,7 @@ static int __init set_reset_devices(char *str)
 __setup("reset_devices", set_reset_devices);
 
 static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
-const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
+static const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
 static const char *panic_later, *panic_param;
 
 static bool __init obsolete_checksetup(char *line)

---
base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
change-id: 20230830-missingvardecl2-init-main-c-93dc1013ff8a

Best regards,
--
Justin Stitt <[email protected]>

#1 lore raw
Quoted 7 lines · Justin Stitt

When building x86/defconfig with Clang-18 I encounter the following warning:

| init/main.c:189:13: warning: no previous extern declaration for non-static variable 'envp_init' [-Wmissing-variable-declarations]
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| init/main.c:189:7: note: declare 'static' if the variable is not intended to be used outside of this translation unit
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
Quoted 5 lines

Make envp_init static and provide handle_initrd its own copy.

This silences the warning and makes the code more readable as you no longer have to track down extern definitions inside of handle_initrd. It is now more self-contained.

But this is now creating duplicate string arrays in the kernel image.

Why can't this be:
1. declared in include/linux/initrd.h
2. defined in init/main.c
3. referenced in init/do_mounts.c

The extern declaration in init/do_mounts_initrd.c isn't even correct! It's missing a const! That's the kind of monkey business this warning is trying to help avoid. C has headers for this reason, let's use them.

Perhaps users of call_usermodehelper_setup need an update, or envp_init actually should not be const.

Quoted 67 lines

---
The kernel test robot didn't like v1 of this patch and I prefer Kees' approach to simplifying this code anyways so I'm sending this v2. Hopefully the CI builds clean (as I am locally using their repro).

Changes in v2:
- Make envp_init static and provide handle_initrd() with a copy (thanks Kees)
- Rebase onto mainline (2dde18cd1d8fa)
- Link to v1: https://lore.kernel.org/r/20230830-missingvardecl2-init-main-c-v1-1-59007a637259@google.com
---
Note: build-tested only.

It should be noted that handle_initrd has been marked as deprecated and perhaps the entire thing can be removed as per it's own message:
| using deprecated initrd support, will be removed in 2021.

---
 init/do_mounts_initrd.c | 4 ++--
 init/main.c             | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
init/do_mounts_initrd.c +2 -2
index 425f4bcf4b77..154bd0de85a6 100644
@@ -87,7 +87,7 @@ static void __init handle_initrd(char *root_device_name)
 {
        struct subprocess_info *info;
        static char *argv[] = { "linuxrc", NULL, };
-       extern char *envp_init[];
+       static char *envp[] = { "HOME=/", "TERM=linux", NULL, };
        int error;
 
        pr_warn("using deprecated initrd support, will be removed in 2021.\n");
@@ -100,7 +100,7 @@ static void __init handle_initrd(char *root_device_name)
        init_mkdir("/old", 0700);
        init_chdir("/old");
 
-       info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
+       info = call_usermodehelper_setup("/linuxrc", argv, envp,
                                         GFP_KERNEL, init_linuxrc, NULL, NULL);
        if (!info)
                return;
init/main.c +1 -2
index ad920fac325c..9a473107bb8f 100644
@@ -186,7 +186,7 @@ static int __init set_reset_devices(char *str)
 __setup("reset_devices", set_reset_devices);
 
 static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
-const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
+static const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
 static const char *panic_later, *panic_param;
 
 static bool __init obsolete_checksetup(char *line)
 
---

base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
change-id: 20230830-missingvardecl2-init-main-c-93dc1013ff8a

Best regards,
--
Justin Stitt <[email protected]>

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 22 lines · Nick Desaulniers
Quoted 7 lines · Justin Stitt

When building x86/defconfig with Clang-18 I encounter the following warning:

| init/main.c:189:13: warning: no previous extern declaration for non-static variable 'envp_init' [-Wmissing-variable-declarations]
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| init/main.c:189:7: note: declare 'static' if the variable is not intended to be used outside of this translation unit
|   189 | const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
Quoted 5 lines

Make envp_init static and provide handle_initrd its own copy.

This silences the warning and makes the code more readable as you no longer have to track down extern definitions inside of handle_initrd. It is now more self-contained.

But this is now creating duplicate string arrays in the kernel image.

Why can't this be:
1. declared in include/linux/initrd.h
2. defined in init/main.c
3. referenced in init/do_mounts.c

Quoted 7 lines

The extern declaration in init/do_mounts_initrd.c isn't even correct! It's missing a const! That's the kind of monkey business this warning is trying to help avoid. C has headers for this reason, let's use them.

Perhaps users of call_usermodehelper_setup need an update, or envp_init actually should not be const.

I prefer v1 as the basis; though as kbuild test robot noted to you, there are other references to envp_init that now need to be cleaned up in addition.

When you say you build tested v1, I assume you only built init/main.o and not vmlinux. Make sure to build test ALL files which refer to variables you've modified.

call_usermodehelper_setup and its callers either need to be cleaned up, or we should consider dropping const.

Quoted 73 lines
Quoted 64 lines

---
The kernel test robot didn't like v1 of this patch and I prefer Kees' approach to simplifying this code anyways so I'm sending this v2. Hopefully the CI builds clean (as I am locally using their repro).

Changes in v2:
- Make envp_init static and provide handle_initrd() with a copy (thanks Kees)
- Rebase onto mainline (2dde18cd1d8fa)
- Link to v1: https://lore.kernel.org/r/20230830-missingvardecl2-init-main-c-v1-1-59007a637259@google.com
---
Note: build-tested only.

It should be noted that handle_initrd has been marked as deprecated and perhaps the entire thing can be removed as per it's own message:
| using deprecated initrd support, will be removed in 2021.

---
 init/do_mounts_initrd.c | 4 ++--
 init/main.c             | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
init/do_mounts_initrd.c +2 -2
index 425f4bcf4b77..154bd0de85a6 100644
@@ -87,7 +87,7 @@ static void __init handle_initrd(char *root_device_name)
 {
        struct subprocess_info *info;
        static char *argv[] = { "linuxrc", NULL, };
-       extern char *envp_init[];
+       static char *envp[] = { "HOME=/", "TERM=linux", NULL, };
        int error;
 
        pr_warn("using deprecated initrd support, will be removed in 2021.\n");
@@ -100,7 +100,7 @@ static void __init handle_initrd(char *root_device_name)
        init_mkdir("/old", 0700);
        init_chdir("/old");
 
-       info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
+       info = call_usermodehelper_setup("/linuxrc", argv, envp,
                                         GFP_KERNEL, init_linuxrc, NULL, NULL);
        if (!info)
                return;
init/main.c +1 -2
index ad920fac325c..9a473107bb8f 100644
@@ -186,7 +186,7 @@ static int __init set_reset_devices(char *str)
 __setup("reset_devices", set_reset_devices);
 
 static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
-const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
+static const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
 static const char *panic_later, *panic_param;
 
 static bool __init obsolete_checksetup(char *line)
 
---

base-commit: 2dde18cd1d8fac735875f2e4987f11817cc0bc2c
change-id: 20230830-missingvardecl2-init-main-c-93dc1013ff8a

Quoted 4 lines

Best regards,
--
Justin Stitt <[email protected]>

--
Thanks,
~Nick Desaulniers

Signature · 2 lines
Thanks,
~Nick Desaulniers