Linux:如何为RAMFS/TMPFS启用就地执行(XIP) [英] Linux: How to enable Execute in place (XIP) for RAMFS/TMPFS

查看:181
本文介绍了Linux:如何为RAMFS/TMPFS启用就地执行(XIP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用init进程在tmpfs分区中构造rootfs的嵌入式系统上. rootfs完成后,它将做一个透视根并开始生成rootfs中的生成过程.

但是XIP似乎不适用于我们的tmpfs,因此所有应用程序都两次加载到ram中(在tmpfs中,加载时再次加载到ram中).

这真的可以吗?

我在 https://ez.analog.com/thread/45262

上找到了一个旧的讨论线程 a>描述与我所见相同的问题.

如何为内存中的文件系统实现XIP?

解决方案

您正在尝试做的事情确实应该可以实现(尽管我自己还没有尝试过).问题是您根本没有正确的方法.如果您使用Block RAM设备("brd"),则可以创建实际上是RAM表示为Block设备的Block设备.要在您的内核上启用此功能(对不起,您不说您拥有哪个内核,所以我将只在4.14内核上使用),您需要在内核配置中启用CONFIG_BLK_DEV_RAM以及CONFIG_BLK_DEV_RAM_DAX.它们都在设备驱动程序"->块设备"下.然后,您创建这样一个支持RAM的块设备,然后在其上创建ext2或ext4或XFS文件系统,然后将rootfs准备到该文件系统中,然后将其透视根.现在,您正在具有XIP(现在已由DAX代替)功能的RAM支持的文件系统中执行,因此,执行应用程序现在至少在理论上应该可以正常工作,而无需创建数据副本,而只需在块的RAM页面中运行它即可. RAM设备.

请注意,这种方法有局限性,例如内核模块本身仍将被复制到RAM中,get_user_pages()可能不起作用,O_DIRECT可能不起作用,RDMA,sendfile()和splice()均不起作用

要注意的一些相关事项包括:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/block/Kconfig?h=v3.19#n359

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/block/Kconfig?h=v3.19#n396

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/blockdev/ramdisk.txt?h=v3.19

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/xip.txt?h=v3.19

注意XIP从4.0内核开始被DAX取代,因此请参见:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/dax.txt?h=4.14

还请注意,从内核4.15的Block RAM驱动程序中删除了对DAX的支持,因此一旦移至内核4.15及更高版本,您将不再能够执行此操作...请参阅commit https://ez.analog.com/thread/45262 which describe the same issue as I'm seeing.

How can I achieve XIP for a file-system located in memory?

解决方案

What you are attempting to do should be indeed possible (though I haven't tried it myself). The problem is simply you are not going about it the correct way. If you use the block RAM device ("brd") you can create a block device that is actually RAM presented as a block device. To enable this on your kernel (sorry you do not say which kernel you have so I will just go with the kernel 4.14), you need to enable CONFIG_BLK_DEV_RAM as well as CONFIG_BLK_DEV_RAM_DAX in your kernel configuration. They are both under "Device Drivers" -> "Block Devices". Then you create such a RAM backed block device and then create for example an ext2 or ext4 or XFS file system on it and then prepare your rootfs into that file system and then pivot-root into it. Now you are executing in a RAM backed file system which has XIP (now replaced by DAX) functionality thus executing applications should now at least in theory work correctly without creating a copy of the data and simply running it out of the RAM pages of the block RAM device.

Please do beware that such approach has limitation such as for example that kernel modules themselves will still be copied into RAM, get_user_pages() may not work, O_DIRECT may not work, and neither might RDMA, sendfile() and splice().

Some relevant things to look at include:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/block/Kconfig?h=v3.19#n359

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/block/Kconfig?h=v3.19#n396

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/blockdev/ramdisk.txt?h=v3.19

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/xip.txt?h=v3.19

Note XIP was replaced by DAX since 4.0 kernel so there see:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/dax.txt?h=4.14

Also note that support for DAX was removed from block RAM driver with kernel 4.15 so you will no longer be able to do this once you move to kernel 4.15 and later... See commit 7a862fbbdec665190c5ef298c0c6ec9f3915cf45 for the reasoning behind removing the functionality.

I hope this is enough to set you on the right track and sorry about the bad news that the functionality has been removed since 4.15 kernel...

这篇关于Linux:如何为RAMFS/TMPFS启用就地执行(XIP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆