Skip to contentSkip to conversation

init: fix -Wmissing-variable-declarations warning

RFC PATCH In review Justin Stitt 3 participants 4 messages Aug 29, 2023 – Aug 31, 2023 VFS

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 init: fix -Wmissing-variable-declarations warning

Hi all,

I was looking to get some help on solving this -Wmissing-variable-declarations warning as there is some hope to turn it on for W=1 soon [1].

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, };
|       |       ^

It seems like the obvious solution is to just add the static keyword and be done with it. I suspect, however, that it is not so simple for the following reasons:

Firstly, envp_init is surrounded by two other variables that have been explicitly marked as static which leads me to believe that this one was intentionally _not_ marked as static for some reason:

| static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
| static const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| static const char *panic_later, *panic_param;

Secondly, there exists this extern declaration for envp_init:
| init/do_mounts_initrd.c
| 90: extern char *envp_init[];

This one is tricky because it seems like I can rename (effectively remove) this extern symbol entirely and still build a kernel image.

If it truly is the case that this extern declaration works then why does Clang produce the warning at all?

FWIW, I've tried moving extern char *envp_init[]; to top-level scope inside of do_mounts_initrd.c which did _not_ work in fixing the warning.

So all in all, it looks like just adding static fixes the warning (which it does) but I am not sure about the other ramifications of this patch especially considering the second point I brought up above regarding the extern declaration already existing (but seemingly not doing anything).

Any help here would be appreciated!

---
 init/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
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: 706a741595047797872e669b3101429ab8d378ef
change-id: 20230829-missingvardecl-init-main-c-1af2e128419e

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

#1 lore raw
kernel test robot noticed the following build errors:

Hi Justin,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on 706a741595047797872e669b3101429ab8d378ef]

url:    https://github.com/intel-lab-lkp/linux/commits/Justin-Stitt/init-fix-Wmissing-variable-declarations-warning/20230830-074037
base:   706a741595047797872e669b3101429ab8d378ef
patch link:    https://lore.kernel.org/r/20230829-missingvardecl-init-main-c-v1-1-ddf0f1a71215%40google.com
patch subject: [PATCH RFC] init: fix -Wmissing-variable-declarations warning
config: s390-defconfig (https://download.01.org/0day-ci/archive/20230830/[email protected]/config)
compiler: s390-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230830/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

s390-linux-ld: init/do_mounts_initrd.o: in function `handle_initrd':

>> init/do_mounts_initrd.c:103:(.init.text+0x21e): undefined reference to `envp_init'

vim +103 init/do_mounts_initrd.c

^1da177e4c3f41 Linus Torvalds     2005-04-16   85  
c8643c72bc4278 Christoph Hellwig  2023-05-31   86  static void __init handle_initrd(char *root_device_name)
^1da177e4c3f41 Linus Torvalds     2005-04-16   87  {
907ed1328d2a74 Lucas De Marchi    2013-04-30   88  	struct subprocess_info *info;
ba4df2808a86f8 Al Viro            2012-10-02   89  	static char *argv[] = { "linuxrc", NULL, };
ba4df2808a86f8 Al Viro            2012-10-02   90  	extern char *envp_init[];
^1da177e4c3f41 Linus Torvalds     2005-04-16   91  	int error;
^1da177e4c3f41 Linus Torvalds     2005-04-16   92  
9acc17baf1fd63 Christoph Hellwig  2020-07-08   93  	pr_warn("using deprecated initrd support, will be removed in 2021.\n");
9acc17baf1fd63 Christoph Hellwig  2020-07-08   94  
^1da177e4c3f41 Linus Torvalds     2005-04-16   95  	real_root_dev = new_encode_dev(ROOT_DEV);
bdaf8529385d51 Greg Kroah-Hartman 2005-06-20   96  	create_dev("/dev/root.old", Root_RAM0);
^1da177e4c3f41 Linus Torvalds     2005-04-16   97  	/* mount initrd on rootfs' /root */
c8643c72bc4278 Christoph Hellwig  2023-05-31   98  	mount_root_generic("/dev/root.old", root_device_name,
c8643c72bc4278 Christoph Hellwig  2023-05-31   99  			   root_mountflags & ~MS_RDONLY);
83ff98c3e9cd2b Christoph Hellwig  2020-07-22  100  	init_mkdir("/old", 0700);
db63f1e3153845 Christoph Hellwig  2020-07-22  101  	init_chdir("/old");
^1da177e4c3f41 Linus Torvalds     2005-04-16  102  
907ed1328d2a74 Lucas De Marchi    2013-04-30 @103  	info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
907ed1328d2a74 Lucas De Marchi    2013-04-30  104  					 GFP_KERNEL, init_linuxrc, NULL, NULL);
907ed1328d2a74 Lucas De Marchi    2013-04-30  105  	if (!info)
907ed1328d2a74 Lucas De Marchi    2013-04-30  106  		return;
1fbcaa923ce2d7 Peter Zijlstra     2022-08-22  107  	call_usermodehelper_exec(info, UMH_WAIT_PROC|UMH_FREEZABLE);
^1da177e4c3f41 Linus Torvalds     2005-04-16  108  
^1da177e4c3f41 Linus Torvalds     2005-04-16  109  	/* move initrd to rootfs' /old */
c60166f04283ff Christoph Hellwig  2020-07-21  110  	init_mount("..", ".", NULL, MS_MOVE, NULL);
^1da177e4c3f41 Linus Torvalds     2005-04-16  111  	/* switch root and cwd back to / of rootfs */
4b7ca5014cbef5 Christoph Hellwig  2020-07-22  112  	init_chroot("..");
^1da177e4c3f41 Linus Torvalds     2005-04-16  113  
^1da177e4c3f41 Linus Torvalds     2005-04-16  114  	if (new_decode_dev(real_root_dev) == Root_RAM0) {
db63f1e3153845 Christoph Hellwig  2020-07-22  115  		init_chdir("/old");
^1da177e4c3f41 Linus Torvalds     2005-04-16  116  		return;
^1da177e4c3f41 Linus Torvalds     2005-04-16  117  	}
^1da177e4c3f41 Linus Torvalds     2005-04-16  118  
db63f1e3153845 Christoph Hellwig  2020-07-22  119  	init_chdir("/");
^1da177e4c3f41 Linus Torvalds     2005-04-16  120  	ROOT_DEV = new_decode_dev(real_root_dev);
c8643c72bc4278 Christoph Hellwig  2023-05-31  121  	mount_root(root_device_name);
^1da177e4c3f41 Linus Torvalds     2005-04-16  122  
^1da177e4c3f41 Linus Torvalds     2005-04-16  123  	printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
c60166f04283ff Christoph Hellwig  2020-07-21  124  	error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
^1da177e4c3f41 Linus Torvalds     2005-04-16  125  	if (!error)
^1da177e4c3f41 Linus Torvalds     2005-04-16  126  		printk("okay\n");
^1da177e4c3f41 Linus Torvalds     2005-04-16  127  	else {
f220ab2a5162c3 Jay Lan            2005-06-30  128  		if (error == -ENOENT)
f220ab2a5162c3 Jay Lan            2005-06-30  129  			printk("/initrd does not exist. Ignored.\n");
f220ab2a5162c3 Jay Lan            2005-06-30  130  		else
^1da177e4c3f41 Linus Torvalds     2005-04-16  131  			printk("failed\n");
^1da177e4c3f41 Linus Torvalds     2005-04-16  132  		printk(KERN_NOTICE "Unmounting old root\n");
09267defa36aaf Christoph Hellwig  2020-07-23  133  		init_umount("/old", MNT_DETACH);
^1da177e4c3f41 Linus Torvalds     2005-04-16  134  	}
^1da177e4c3f41 Linus Torvalds     2005-04-16  135  }
^1da177e4c3f41 Linus Torvalds     2005-04-16  136  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Justin Stitt
kernel test robot noticed the following build errors:

Hi Justin,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on 706a741595047797872e669b3101429ab8d378ef]

url:    https://github.com/intel-lab-lkp/linux/commits/Justin-Stitt/init-fix-Wmissing-variable-declarations-warning/20230830-074037
base:   706a741595047797872e669b3101429ab8d378ef
patch link:    https://lore.kernel.org/r/20230829-missingvardecl-init-main-c-v1-1-ddf0f1a71215%40google.com
patch subject: [PATCH RFC] init: fix -Wmissing-variable-declarations warning
config: arm-defconfig (https://download.01.org/0day-ci/archive/20230830/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230830/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

arm-linux-gnueabi-ld: init/do_mounts_initrd.o: in function `handle_initrd':

>> do_mounts_initrd.c:(.init.text+0x1ec): undefined reference to `envp_init'
>> arm-linux-gnueabi-ld: do_mounts_initrd.c:(.init.text+0x1f0): undefined reference to `envp_init'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Justin Stitt
Quoted 17 lines · Justin Stitt

Hi all,

I was looking to get some help on solving this -Wmissing-variable-declarations warning as there is some hope to turn it on for W=1 soon [1].

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, };
|       |       ^

It seems like the obvious solution is to just add the static keyword and be done with it. I suspect, however, that it is not so simple for the following reasons:

Quoted 6 lines

Firstly, envp_init is surrounded by two other variables that have been explicitly marked as static which leads me to believe that this one was intentionally _not_ marked as static for some reason:

| static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
| static const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
| static const char *panic_later, *panic_param;

I went looking to see the history here and it goes beyond git history. :)

Quoted 4 lines

Secondly, there exists this extern declaration for envp_init:
| init/do_mounts_initrd.c
| 90: extern char *envp_init[];

This is the only user of call_usermodehelper_setup() that doesn't make its own envp. And there's a deprecation warning in that function too:

9acc17baf1fd6 (Christoph Hellwig 2020-07-08 18:18:54 +0200 93) pr_warn("using deprecated initrd support, will be removed in 2021.\n");

Quoted 1 line

Any help here would be appreciated!

I recommend making it static and giving handle_initrd() its own copy:

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;
Signature · 1 line
Kees Cook