1

In embedded Linux, memory is precious. In my case, I have an executable in a tmpfs ram-disk. In fact, the ram-disk contains only one file, the executable.

I want to run the executable for once only. That executable runs in a very long time. When it is running, I want to unmount the ram-disk, to release some memory. Thus I deleted the executable, and then umount the ram-disk. It just showed a device busy error message: the umount failed.

How can I achieve that? I am not talking about Lazy umount. I want a real unmount, to release the memory! It should be possible, since the running process already has its image loaded in RAM. It should not rely on the ram-disk file system.

If it is not possible for general Linux kernel for now, I welcome answers pointing me where I can hack the kernel to enable this feature.

If possible, answers in programmatic way are preferred. Though answers in script way are also welcome (I can convert scripts to some executable anyway, by looking at the shell script source code).

My kernel version is 3.10.14

--Update-- Another thought: My purpose is to release some memory, since my application is memory-hogging. Also the binary size is large, compared to the total RAM. My original thought is that, the ramdisk have a copy of the file, and the running image is another copy. But maybe Linux keep just one copy than two? Anyone knows how elf executables been kept in both file system and the running image? Does Linux just keep one copy, or perhaps some meta data + 1 copy, not really keep 2 copies? If that's true, I don't need to do the unmount.

Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
  • If you are only running it once, why load it into a ramdisk at all? Are you downloading it from a network or something? – Bob Shaffer Mar 05 '19 at 05:16
  • For example, it may come from network or may be encrypted due to the security requirement. decrypt in ram is better than on disk for security. – Robin Hsu Mar 05 '19 at 06:43
  • If it is a `tmpfs` filesystem, you could remount it at a smaller size (`-o remount,size=NEWSIZE`) after removing its contents. – Ian Abbott Mar 05 '19 at 11:41
  • Perhaps this question is also relevant: https://stackoverflow.com/questions/37317493/linux-how-to-enable-execute-in-place-xip-for-ramfs-tmpfs – Ian Abbott Mar 05 '19 at 11:47
  • @lan Abbott: Thanks for the info. Kernel 3.x can use CONFIG_BLK_DEV_XIP, while kernel 4.x can use CONFIG_BLK_DEV_RAM_DAX. Let me study it for a while. – Robin Hsu Mar 06 '19 at 07:35

1 Answers1

1

It should be possible, since the running process already has its image loaded in RAM.

This isn't possible.

If a system is under memory pressure, unused text pages of executable images can be paged out. Instead of writing these pages to swap, the kernel "swaps" these pages by removing them from core, with the understanding that they can be read back from disk if they are needed.

As such, you cannot unmount a filesystem containing executable code which is in use. This isn't specific to executable binaries; it also applies to libraries.