Re: [PATCH v2] init: fix -Wmissing-variable-declarations clang warning

From: Nick Desaulniers
Date: Mon Sep 11 2023 - 16:52:03 EST


On Tue, Sep 5, 2023 at 3:43 PM Justin Stitt <justinstitt@xxxxxxxxxx> wrote:
>
> 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.

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.

>
> Link: https://github.com/ClangBuiltLinux/linux/issues/1920
> Signed-off-by: Justin Stitt <justinstitt@xxxxxxxxxx>
> ---
> 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@xxxxxxxxxx
> ---
> 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(-)
>
> diff --git a/init/do_mounts_initrd.c b/init/do_mounts_initrd.c
> index 425f4bcf4b77..154bd0de85a6 100644
> --- a/init/do_mounts_initrd.c
> +++ b/init/do_mounts_initrd.c
> @@ -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;
> diff --git a/init/main.c b/init/main.c
> index ad920fac325c..9a473107bb8f 100644
> --- a/init/main.c
> +++ b/init/main.c
> @@ -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 <justinstitt@xxxxxxxxxx>
>


--
Thanks,
~Nick Desaulniers