[PATCH] do_mounts: always prefer tmpfs for rootfs when available

From: Alexander Lobakin
Date: Fri Jul 02 2021 - 19:45:02 EST


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/20210701234807.50453-1-alobakin@xxxxx/

Signed-off-by: Alexander Lobakin <alobakin@xxxxx>
---
fs/namespace.c | 2 --
include/linux/init.h | 1 -
init/do_mounts.c | 26 +++++++-------------------
3 files changed, 7 insertions(+), 22 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index ab4174a3c802..310ab44fdbe7 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -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();
}

diff --git a/include/linux/init.h b/include/linux/init.h
index d82b4b2e1d25..10839922a1d3 100644
--- a/include/linux/init.h
+++ b/include/linux/init.h
@@ -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)
diff --git a/init/do_mounts.c b/init/do_mounts.c
index 74aede860de7..c00b05015a66 100644
--- a/init/do_mounts.c
+++ b/init/do_mounts.c
@@ -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