Ian Wienand: Shared memory on Linux
You may have noticed a range of things mounted on a
tmpfs file system.
tmpfs is actually more pervasive than it might appear. It
was created to build a filesystem interface ontop of shared pages of
RAM. This is useful because there are a number of ways to access
shared RAM; notably SysV IPC Shared Memory (shmget,
shmat, etc) and mmap of anonymous memory
(MAP_ANONYMOUS).
In order to have a consistent infrastructure implementing all of
this the kernel abstracts RAM as a filesystem and your mapping as a
file. To this end the tmpfs filesystem was created to
present this abstraction. It deals with issues such as caching,
security and the ability to swap pages in a consistent manner
(i.e. using the same infrastructure every other file system does).
Overall, tmpfs is implemented in mm/shmem.c. In
the initialisation function the file system is mounted internally by
the kernel with vfs_kern_mount so it is always around. It
gets fairly complicated because it has to keep track of swapped out
pages and the general complexity that come with implementing a full
filesystem.
As described above, there are several distinct ways to get access
to this file system.
$ mount grep tmpfs tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
Overall, tmpfs is implemented in mm/shmem.c. In
the initialisation function the file system is mounted internally by
the kernel with vfs_kern_mount so it is always around. It
gets fairly complicated because it has to keep track of swapped out
pages and the general complexity that come with implementing a full
filesystem.
As described above, there are several distinct ways to get access
to this file system.
- The SysV IPC shared memory layer (ipc/shm.c) is a wrapper layer which manages IPC keys and internally plugs itself into the tmpfs code. Essentially creating a key with shmget() ends up calling ipc/shmem.c:newseg() which calls mm/shmem.c:shmem_file_setup() which then creates a struct file for the shared memory. This new file reuses the file operations from the tmpfs filesystem, but is not actually put into the file system (i.e. linked to the superblock). When shat() is called to attach to this memory the SysV IPC layer will internally translate this into a mmap of this file. This is what is used by the Shared Global Area of Oracle and some Java implementations, amongst many other things (see /proc/sysvipc/shm for a comprehensive list).
- The second common thing to do is an anonymous mmap. By
default MAP_ANONYMOUS ignores the file-descriptor, but an
analagous methodology is to explicitly map /dev/zero. If you
look at the implementation of /dev/zero in
drivers/char/mem.c you can see code such as
which ends up backing shared areas with tmpfs.
static int mmap_zero(struct file * file, struct vm_area_struct * vma) int err; if (vma->vm_flags & VM_SHARED) return shmem_zero_setup(vma); - The third way is actually mounting tmpfs and using it from userspace as a normal file system. glibc implements the POSIX shared memory call shm_open by creating and mmaping files on a tmpsfs mount (by default looking in /dev/shm). Other utilities also use the tmpfs semantics and mount it at other points; the obvious one of course being mounting /tmp on it!
If you're ever on Sydney Buses bus number 2660 it can probably
still be seen on the driver side window on the back seat. Maybe what
Itanium needs isn't higher clock speeds, lower power, faster FSB,
etc., but a concerted tagging effort on public transport around the
world!
One potential issue with this style of page-table is with a very
large, very sparse address space (such as in a
To examine this further, consider that we know that each page in
our 24-bit address space above is 4KiB, thanks to the 212 =
4KiB offset. Therefore each of the 16 possible values of the top four
bits selects 1MiB of the 224 = 16MiB address space;
e.g. 0x0 selects the first megabyte, 0x1 the second and so forth. The
second 4 bits selects a 64KiB region of this 1MiB, and the last 4 bits
selects a 4KiB region of this 64KiB region.
This scheme could work quite well if we were using only 16 pages
each separated by 1MiB. Each of these pages would only require an
entry in the top level with an appropriate guard which could then be
marked as final and point directly to a physical frame.
Once things become less sparse however, we need to start filling in
middle levels. If you have 2 pages within a 16MiB region, you need at
least one extra bit of address to tell them apart. If you have 3 or 4
pages, you need at least 2 bits to tell them apart, and so forth.
It would therefore be beneficial to have variable level sizes to
dynamically adapt and create the most efficient lookup possible given
the address space layout of a process. For example, if you knew your
process was only going map pages 0x123 (100100011) and 0x124
(100100100) a good option would be to make your top-level only two
entries (i.e. check the top bit) with a guard on the entry at index
0x1 of 00100. You then need a second level with 8 pointers to
differentiate the remaining 3 bits (each of these would be final and
hence point directly to a physical page).
The more pages you start mapping, the less bits you can discard
with the guard. If you follow this through to a guard of 0 bits you
end up with a linear page-table.
It has been shown (