Skip to contentSkip to conversation

do_mounts: always prefer tmpfs for rootfs when available

PATCH In review Alexander Lobakin 1 participant 2 messages Jul 2, 2021 – Aug 4, 2021 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 do_mounts: always prefer tmpfs for rootfs when available

Inspired by the situation from [0].

The roots of choosing tmpfs/ramfs backend for rootfs go far back in history, and it's unclear at all why it was decided to select full-blown tmpfs when "root=" is not specified and feature-poor ramfs otherwise.
There are several cases when "root=" is not needed at all to work, and it doesn't break anything or make any [negative] sense. On the other hand, such separation is rather counter-intuitive and makes debugging more difficult.
Simply always use tmpfs when it's available -- just like devtmpfs does [for over a decade].

[0] https://lore.kernel.org/kernel-hardening/[email protected]/

---
 fs/namespace.c       |  2 --
 include/linux/init.h |  1 -
 init/do_mounts.c     | 26 +++++++-------------------
 3 files changed, 7 insertions(+), 22 deletions(-)
fs/namespace.c +0 -2
index ab4174a3c802..310ab44fdbe7 100644
@@ -17,7 +17,6 @@
 #include <linux/security.h>
 #include <linux/cred.h>
 #include <linux/idr.h>
-#include <linux/init.h>		/* init_rootfs */
 #include <linux/fs_struct.h>	/* get_fs_root et.al. */
 #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
 #include <linux/file.h>
@@ -4248,7 +4247,6 @@ void __init mnt_init(void)
 	if (!fs_kobj)
 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
 	shmem_init();
-	init_rootfs();
 	init_mount_tree();
 }
 
include/linux/init.h +0 -1
index d82b4b2e1d25..10839922a1d3 100644
@@ -148,7 +148,6 @@ extern unsigned int reset_devices;
 /* used by init/main.c */
 void setup_arch(char **);
 void prepare_namespace(void);
-void __init init_rootfs(void);
 extern struct file_system_type rootfs_fs_type;
 
 #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
init/do_mounts.c +7 -19
index 74aede860de7..c00b05015a66 100644
@@ -611,24 +611,12 @@ void __init prepare_namespace(void)
 	init_chroot(".");
 }
 
-static bool is_tmpfs;
-static int rootfs_init_fs_context(struct fs_context *fc)
-{
-	if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
-		return shmem_init_fs_context(fc);
-
-	return ramfs_init_fs_context(fc);
-}
-
 struct file_system_type rootfs_fs_type = {
-	.name		= "rootfs",
-	.init_fs_context = rootfs_init_fs_context,
-	.kill_sb	= kill_litter_super,
+	.name			= "rootfs",
+#ifdef CONFIG_TMPFS
+	.init_fs_context	= shmem_init_fs_context,
+#else
+	.init_fs_context	= ramfs_init_fs_context,
+#endif
+	.kill_sb		= kill_litter_super,
 };
-
-void __init init_rootfs(void)
-{
-	if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
-		(!root_fs_names || strstr(root_fs_names, "tmpfs")))
-		is_tmpfs = true;
-}

--
2.32.0

#1 lore raw

From: Alexander Lobakin <[email protected]>
Date: Fri, 02 Jul 2021 23:44:35 +0000

Quoted 13 lines

Inspired by the situation from [0].

The roots of choosing tmpfs/ramfs backend for rootfs go far back in history, and it's unclear at all why it was decided to select full-blown tmpfs when "root=" is not specified and feature-poor ramfs otherwise.
There are several cases when "root=" is not needed at all to work, and it doesn't break anything or make any [negative] sense. On the other hand, such separation is rather counter-intuitive and makes debugging more difficult.
Simply always use tmpfs when it's available -- just like devtmpfs does [for over a decade].

Quoted 3 lines

[0] https://lore.kernel.org/kernel-hardening/[email protected]/

Ping?

Quoted 75 lines
---
 fs/namespace.c       |  2 --
 include/linux/init.h |  1 -
 init/do_mounts.c     | 26 +++++++-------------------
 3 files changed, 7 insertions(+), 22 deletions(-)
fs/namespace.c +0 -2
index ab4174a3c802..310ab44fdbe7 100644
@@ -17,7 +17,6 @@
 #include <linux/security.h>
 #include <linux/cred.h>
 #include <linux/idr.h>
-#include <linux/init.h>		/* init_rootfs */
 #include <linux/fs_struct.h>	/* get_fs_root et.al. */
 #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
 #include <linux/file.h>
@@ -4248,7 +4247,6 @@ void __init mnt_init(void)
 	if (!fs_kobj)
 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
 	shmem_init();
-	init_rootfs();
 	init_mount_tree();
 }
 
include/linux/init.h +0 -1
index d82b4b2e1d25..10839922a1d3 100644
@@ -148,7 +148,6 @@ extern unsigned int reset_devices;
 /* used by init/main.c */
 void setup_arch(char **);
 void prepare_namespace(void);
-void __init init_rootfs(void);
 extern struct file_system_type rootfs_fs_type;
 
 #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
init/do_mounts.c +7 -20
index 74aede860de7..c00b05015a66 100644
@@ -611,24 +611,12 @@ void __init prepare_namespace(void)
 	init_chroot(".");
 }
 
-static bool is_tmpfs;
-static int rootfs_init_fs_context(struct fs_context *fc)
-{
-	if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
-		return shmem_init_fs_context(fc);
-
-	return ramfs_init_fs_context(fc);
-}
-
 struct file_system_type rootfs_fs_type = {
-	.name		= "rootfs",
-	.init_fs_context = rootfs_init_fs_context,
-	.kill_sb	= kill_litter_super,
+	.name			= "rootfs",
+#ifdef CONFIG_TMPFS
+	.init_fs_context	= shmem_init_fs_context,
+#else
+	.init_fs_context	= ramfs_init_fs_context,
+#endif
+	.kill_sb		= kill_litter_super,
 };
-
-void __init init_rootfs(void)
-{
-	if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
-		(!root_fs_names || strstr(root_fs_names, "tmpfs")))
-		is_tmpfs = true;
-}
--
Quoted 1 line

2.32.0

Thanks,
Al