All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] rootmpfs
@ 2013-04-03 12:30 Rob Landley
  2013-04-03 12:32 ` Rob Landley
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Rob Landley @ 2013-04-03 12:30 UTC (permalink / raw)
  To: linux-kernel

Attached is my quick and dirty hack to make rootfs be tmpfs when  
CONFIG_TMPFS is
enabled. It can't be this easy or somebody would have done it in the  
_eight_years_
since https://lkml.org/lkml/2006/7/31/145

Yes, it's got an #ifdef and out of place prototypes. Yes, it manually  
calls a module
init function and compensates by making it reentrant. But it works, and  
when I
"cat /dev/zero > filename" the filesystem fills _up_ instead of  
panicing the kernel.

So now that I've posted the error, would someone please tell me how I  
_should_ have done it?

Rob

P.S. If I actually change the filesystem type to a name other than  
"rootfs", it panics on the way up because various bits of the kernel  
are looking for that magic name. Sigh.

P.P.S. removing MS_NOUSER is actually intentional, there's a local cray  
patch that does the same thing because otherwise you can't --bind mount  
directories out of this filesystem, which is a thing they wanted to do.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-03 12:30 [RFC] rootmpfs Rob Landley
@ 2013-04-03 12:32 ` Rob Landley
  2013-04-05 19:53 ` Byron Stanoszek
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Rob Landley @ 2013-04-03 12:32 UTC (permalink / raw)
  To: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 205 bytes --]

On 04/03/2013 07:30:08 AM, Rob Landley wrote:
> Attached is my quick and dirty hack to make rootfs be tmpfs when  
> CONFIG_TMPFS is
> enabled.

For a somewhat quantum definition of "attached".

Rob

[-- Attachment #2: rootmpfs2.patch --]
[-- Type: text/x-patch, Size: 1140 bytes --]

diff --git a/fs/ramfs/inode.c b/fs/ramfs/inode.c
index c24f1e1..f875bf2 100644
--- a/fs/ramfs/inode.c
+++ b/fs/ramfs/inode.c
@@ -244,10 +244,17 @@ struct dentry *ramfs_mount(struct file_system_type *fs_type,
 	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
 }
 
-static struct dentry *rootfs_mount(struct file_system_type *fs_type,
+int shmem_init(void);
+int shmem_fill_super(struct super_block *sb, void *data, int silent);
+static __init struct dentry *rootfs_mount(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data)
 {
+#ifdef CONFIG_TMPFS
+	if (shmem_init()) return NULL;
+	return mount_nodev(fs_type, flags, data, shmem_fill_super);
+#else
 	return mount_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super);
+#endif
 }
 
 static void ramfs_kill_sb(struct super_block *sb)
diff --git a/mm/shmem.c b/mm/shmem.c
index 1c44af7..d2cb93e 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2785,6 +2785,8 @@ int __init shmem_init(void)
 {
 	int error;
 
+	if (shmem_inode_cachep) return 0;
+
 	error = bdi_init(&shmem_backing_dev_info);
 	if (error)
 		goto out4;

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-03 12:30 [RFC] rootmpfs Rob Landley
  2013-04-03 12:32 ` Rob Landley
@ 2013-04-05 19:53 ` Byron Stanoszek
  2013-04-09 14:52   ` Rob Landley
  2013-04-10 13:43 ` Robin Holt
  2013-04-11 17:25 ` Lauri Kasanen
  3 siblings, 1 reply; 8+ messages in thread
From: Byron Stanoszek @ 2013-04-05 19:53 UTC (permalink / raw)
  To: Rob Landley; +Cc: linux-kernel

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1748 bytes --]

Rob,

FWIW I have a patch to do something like this. It even gives you a rdsize=xxx
tunable kernel parameter that lets you specify the size of the tmpfs, which
acts like the -osize= mount flag (so phrases like 100M or 20% works). So doing
things like 'cat /dev/zero > filename' will not run you out of all available
memory. (Note: If you don't specify rdsize= on the kernel command line, it will
not convert rootfs to tmpfs).

See attached.

  -Byron

On Wed, 3 Apr 2013, Rob Landley wrote:

> Attached is my quick and dirty hack to make rootfs be tmpfs when CONFIG_TMPFS 
> is
> enabled. It can't be this easy or somebody would have done it in the 
> _eight_years_
> since https://lkml.org/lkml/2006/7/31/145
>
> Yes, it's got an #ifdef and out of place prototypes. Yes, it manually calls a 
> module
> init function and compensates by making it reentrant. But it works, and when 
> I
> "cat /dev/zero > filename" the filesystem fills _up_ instead of panicing the 
> kernel.
>
> So now that I've posted the error, would someone please tell me how I 
> _should_ have done it?
>
> Rob
>
> P.S. If I actually change the filesystem type to a name other than "rootfs", 
> it panics on the way up because various bits of the kernel are looking for 
> that magic name. Sigh.
>
> P.P.S. removing MS_NOUSER is actually intentional, there's a local cray patch 
> that does the same thing because otherwise you can't --bind mount directories 
> out of this filesystem, which is a thing they wanted to do.--
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: TEXT/x-diff; name=root-tmpfs.patch, Size: 1592 bytes --]

--- linux/init/initramfs.c.orig	2012-12-05 21:39:09.000000000 -0500
+++ linux/init/initramfs.c	2012-12-10 12:15:49.000000000 -0500
@@ -569,14 +569,56 @@
 }
 #endif
 
+static char * __initdata ramdisk_size;
+
+static int __init rdsize_setup(char *str)
+{
+	ramdisk_size = str;
+	return 1;
+}
+__setup("rdsize=", rdsize_setup);
+
+static int __init change_root_to_tmpfs(void)
+{
+	char size[38], *s;
+
+	sprintf(size, "size=%.20s,nr_inodes=0", ramdisk_size);
+	if ((s = strchr(size, ',')))
+		*s = '\0';
+
+	if (!sys_mkdir("/root", 0700) &&
+	    !sys_mount("/dev/root", "/root", "tmpfs", 0, size) &&
+	    !sys_chdir("/root") &&
+	    !sys_mount(".", "/", NULL, MS_MOVE, NULL) &&
+	    !sys_chroot("."))
+	        return 0;
+
+	panic("Failed to mount tmpfs as root filesystem");
+}
+
+
 static int __init populate_rootfs(void)
 {
-	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
+	char *err;
+
+	if (ramdisk_size)
+		change_root_to_tmpfs();
+
+	err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
 	if (err)
 		panic(err);	/* Failed to decompress INTERNAL initramfs */
 	if (initrd_start) {
 #ifdef CONFIG_BLK_DEV_RAM
 		int fd;
+		if (ramdisk_size) {
+			printk(KERN_INFO "Unpacking initramfs...\n");
+			err = unpack_to_rootfs((char *)initrd_start,
+				initrd_end - initrd_start);
+			if (err)
+				panic(err);
+			free_initrd();
+			return 0;
+		}
 		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
 		err = unpack_to_rootfs((char *)initrd_start,
 			initrd_end - initrd_start);

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-05 19:53 ` Byron Stanoszek
@ 2013-04-09 14:52   ` Rob Landley
  2013-04-09 17:28     ` Randy Dunlap
  0 siblings, 1 reply; 8+ messages in thread
From: Rob Landley @ 2013-04-09 14:52 UTC (permalink / raw)
  To: Byron Stanoszek; +Cc: linux-kernel

On 04/05/2013 02:53:12 PM, Byron Stanoszek wrote:
> Rob,
> 
> FWIW I have a patch to do something like this. It even gives you a  
> rdsize=xxx
> tunable kernel parameter that lets you specify the size of the tmpfs,  
> which
> acts like the -osize= mount flag (so phrases like 100M or 20% works).  
> So doing
> things like 'cat /dev/zero > filename' will not run you out of all  
> available
> memory. (Note: If you don't specify rdsize= on the kernel command  
> line, it will
> not convert rootfs to tmpfs).

In init/do_mounts.c the boot infrastructure already has kernel command  
line options "rootflags=" and "rootfstype=", so the logical thing to do  
is probably to hook those up to rootfs. (That way instead of special  
casing a new option we use the existing tmpfs option parsing.)

The default tmpfs size is 50%, which solves the "trivial to exhaust  
memory and panic a kernel running under rootfs" problem. Having one  
tmpfs also fixes the case that multiple tmpfs mounts (for /home and  
/var, for example,) have separate memory limits that don't coordinate  
with each other, so if /home can use 30% and /var can use 30%, that's  
60% plus whatever rootfs is already using, so you can easily squeeze  
the kernel against the wall without meaning to. (Yes, you can make one  
tmpfs mount and --bind mount from there to elsewhere, I've seen that  
done. Having rootfs just _be_ tmpfs makes this much easier to track.)

> See attached.

You're not actually changing the type of rootfs, you're overmounting it  
with a second filesystem instance. (Mine hasn't got a "change", it just  
mounts it correctly the first time, and there's just one rootfs  
instance.)

What _is_ wrong with my version is that if you select tmpfs as a module  
bad things happen; it tries to use code that's not there. I dunno of an  
#ifdef that distinguishes between module and builtin, so I think I have  
to add another kconfig symbol...

I'll poke at it.

Rob

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-09 14:52   ` Rob Landley
@ 2013-04-09 17:28     ` Randy Dunlap
  2013-04-10 17:23       ` Rob Landley
  0 siblings, 1 reply; 8+ messages in thread
From: Randy Dunlap @ 2013-04-09 17:28 UTC (permalink / raw)
  To: Rob Landley; +Cc: Byron Stanoszek, linux-kernel

On 04/09/13 07:52, Rob Landley wrote:
> On 04/05/2013 02:53:12 PM, Byron Stanoszek wrote:
>> Rob,
>>
>> FWIW I have a patch to do something like this. It even gives you a rdsize=xxx
>> tunable kernel parameter that lets you specify the size of the tmpfs, which
>> acts like the -osize= mount flag (so phrases like 100M or 20% works). So doing
>> things like 'cat /dev/zero > filename' will not run you out of all available
>> memory. (Note: If you don't specify rdsize= on the kernel command line, it will
>> not convert rootfs to tmpfs).
> 
> In init/do_mounts.c the boot infrastructure already has kernel command line options "rootflags=" and "rootfstype=", so the logical thing to do is probably to hook those up to rootfs. (That way instead of special casing a new option we use the existing tmpfs option parsing.)
> 
> The default tmpfs size is 50%, which solves the "trivial to exhaust memory and panic a kernel running under rootfs" problem. Having one tmpfs also fixes the case that multiple tmpfs mounts (for /home and /var, for example,) have separate memory limits that don't coordinate with each other, so if /home can use 30% and /var can use 30%, that's 60% plus whatever rootfs is already using, so you can easily squeeze the kernel against the wall without meaning to. (Yes, you can make one tmpfs mount and --bind mount from there to elsewhere, I've seen that done. Having rootfs just _be_ tmpfs makes this much easier to track.)
> 
>> See attached.
> 
> You're not actually changing the type of rootfs, you're overmounting it with a second filesystem instance. (Mine hasn't got a "change", it just mounts it correctly the first time, and there's just one rootfs instance.)
> 
> What _is_ wrong with my version is that if you select tmpfs as a module bad things happen; it tries to use code that's not there. I dunno of an #ifdef that distinguishes between module and builtin, so I think I have to add another kconfig symbol...

See include/linux/kconfig.h:  IS_MODULE() and IS_BUILTIN().

> 
> I'll poke at it.


-- 
~Randy

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-03 12:30 [RFC] rootmpfs Rob Landley
  2013-04-03 12:32 ` Rob Landley
  2013-04-05 19:53 ` Byron Stanoszek
@ 2013-04-10 13:43 ` Robin Holt
  2013-04-11 17:25 ` Lauri Kasanen
  3 siblings, 0 replies; 8+ messages in thread
From: Robin Holt @ 2013-04-10 13:43 UTC (permalink / raw)
  To: Rob Landley; +Cc: linux-kernel

On Wed, Apr 03, 2013 at 07:30:08AM -0500, Rob Landley wrote:
> Attached is my quick and dirty hack to make rootfs be tmpfs when
> CONFIG_TMPFS is
> enabled. It can't be this easy or somebody would have done it in the

I don't see anything attached, but...

I have been running using tmpfs as my root for years.  I tweaked the
debian initrd scripts to mount a tmpfs filesystem, cpio recover a
compressed cpio image from the disk drive and then pivotroot.  Did not
require any kernel tweaks.

Thanks,
Robin

> _eight_years_
> since https://lkml.org/lkml/2006/7/31/145
> 
> Yes, it's got an #ifdef and out of place prototypes. Yes, it
> manually calls a module
> init function and compensates by making it reentrant. But it works,
> and when I
> "cat /dev/zero > filename" the filesystem fills _up_ instead of
> panicing the kernel.
> 
> So now that I've posted the error, would someone please tell me how
> I _should_ have done it?
> 
> Rob
> 
> P.S. If I actually change the filesystem type to a name other than
> "rootfs", it panics on the way up because various bits of the kernel
> are looking for that magic name. Sigh.
> 
> P.P.S. removing MS_NOUSER is actually intentional, there's a local
> cray patch that does the same thing because otherwise you can't
> --bind mount directories out of this filesystem, which is a thing
> they wanted to do.--
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-09 17:28     ` Randy Dunlap
@ 2013-04-10 17:23       ` Rob Landley
  0 siblings, 0 replies; 8+ messages in thread
From: Rob Landley @ 2013-04-10 17:23 UTC (permalink / raw)
  To: Randy Dunlap; +Cc: Byron Stanoszek, linux-kernel

On 04/09/2013 12:28:21 PM, Randy Dunlap wrote:
> On 04/09/13 07:52, Rob Landley wrote:
> > On 04/05/2013 02:53:12 PM, Byron Stanoszek wrote:
> >> Rob,
> >>
> >> FWIW I have a patch to do something like this. It even gives you a  
> rdsize=xxx
> >> tunable kernel parameter that lets you specify the size of the  
> tmpfs, which
> >> acts like the -osize= mount flag (so phrases like 100M or 20%  
> works). So doing
> >> things like 'cat /dev/zero > filename' will not run you out of all  
> available
> >> memory. (Note: If you don't specify rdsize= on the kernel command  
> line, it will
> >> not convert rootfs to tmpfs).
> >
> > In init/do_mounts.c the boot infrastructure already has kernel  
> command line options "rootflags=" and "rootfstype=", so the logical  
> thing to do is probably to hook those up to rootfs. (That way instead  
> of special casing a new option we use the existing tmpfs option  
> parsing.)
> >
> > The default tmpfs size is 50%, which solves the "trivial to exhaust  
> memory and panic a kernel running under rootfs" problem. Having one  
> tmpfs also fixes the case that multiple tmpfs mounts (for /home and  
> /var, for example,) have separate memory limits that don't coordinate  
> with each other, so if /home can use 30% and /var can use 30%, that's  
> 60% plus whatever rootfs is already using, so you can easily squeeze  
> the kernel against the wall without meaning to. (Yes, you can make  
> one tmpfs mount and --bind mount from there to elsewhere, I've seen  
> that done. Having rootfs just _be_ tmpfs makes this much easier to  
> track.)
> >
> >> See attached.
> >
> > You're not actually changing the type of rootfs, you're  
> overmounting it with a second filesystem instance. (Mine hasn't got a  
> "change", it just mounts it correctly the first time, and there's  
> just one rootfs instance.)
> >
> > What _is_ wrong with my version is that if you select tmpfs as a  
> module bad things happen; it tries to use code that's not there. I  
> dunno of an #ifdef that distinguishes between module and builtin, so  
> I think I have to add another kconfig symbol...
> 
> See include/linux/kconfig.h:  IS_MODULE() and IS_BUILTIN().

Good to know, thanks.

(It turns out I was looking at a distro kernel directory and vanilla  
only lets TMPFS be static anyway, but I should still use that in case  
it changes, and I think I still need to wire up a rootfsflags argument.)

Rob

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC] rootmpfs
  2013-04-03 12:30 [RFC] rootmpfs Rob Landley
                   ` (2 preceding siblings ...)
  2013-04-10 13:43 ` Robin Holt
@ 2013-04-11 17:25 ` Lauri Kasanen
  3 siblings, 0 replies; 8+ messages in thread
From: Lauri Kasanen @ 2013-04-11 17:25 UTC (permalink / raw)
  To: linux-kernel

Rob Landley <rob <at> landley.net> writes:

> Attached is my quick and dirty hack to make rootfs be tmpfs when  
> CONFIG_TMPFS is
> enabled. It can't be this easy or somebody would have done it in the  
> _eight_years_
> since https://lkml.org/lkml/2006/7/31/145

Hi Rob,

We've been using a variation of the 2006 patch in TinyCore for years. It's 
a fairly essential piece for us, as using it can save several seconds of 
boot time for us.

Since we run in the initramfs, to get size limits and swappability, we used 
to tar the files and switch_root. Both unnecessary steps when the kernel 
could directly unpack to tmpfs.

So consider this a +1 "we use this".

- Lauri


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-04-11 17:30 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-03 12:30 [RFC] rootmpfs Rob Landley
2013-04-03 12:32 ` Rob Landley
2013-04-05 19:53 ` Byron Stanoszek
2013-04-09 14:52   ` Rob Landley
2013-04-09 17:28     ` Randy Dunlap
2013-04-10 17:23       ` Rob Landley
2013-04-10 13:43 ` Robin Holt
2013-04-11 17:25 ` Lauri Kasanen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.