Re: shared anonymous memory

Roger Espel Llima (espel@llaic.u-clermont1.fr)
Sat, 10 Jan 1998 18:36:04 +0100


Linus wrote:
> Note that when I'm saying "UNIX anonymous shared memory" as opposed to
> "SysV shmem" I'm talking about something like this:
>
> char * addr = mmap(NULL, 8192,
> PROT_READ | PROT_WRITE,
> MAP_SHARED | MAP_ANONYMOUS,
> -1, 0);
>
> And I thought that _everything_ shared the same page pool with no
> protection checks (ie different processes that aren't related would get
> the same shared pages).
>
> But if the above only gets a "private" shared memory area (ie only passed
> down as a shared memory area through fork(), not accessible through other
> unrelated processes doing the same mmap()), then it is not as braindamaged
> as I thought it was.

Right, it is only shared via fork. The fd can be -1, or a rw fd on
/dev/zero; some systems support it with /dev/zero but not with -1. If
the fd is not -1, then the MAP_ANONYMOUS flag is not needed.

Actually, the BSD folks who came up with this interface had it planned
so that unrelated processes could have a way to rendez-vous and set up a
shared memory zone. As far as I can understand, they were supposed to
use the file descriptor as a meeting point, possibly by passing it
through a Unix-domain socket (or at least, I can't think of any other
way that makes sense).

A paper provided with the BSD4.4 sources (/usr/share/doc/papers/newvm*
on FreeBSD) reads:

Unlike a region that is mapped from a file, the contents of an
anonymous region will never be read from or written to a disk
unless memory is short and part of the region must be paged to a
swap area. If one of these regions is mapped shared, then all
processes see the changes in the region. [...]

If several processes wish to share a region, then they must have
some way of rendezvousing. For a mapped file this is easy; the
name of the file is used as the rendezvous point. However,
processes may not need the semantics of mapped files nor be
willing to pay the overhead associated with them. For anonymous
memory they must use some other rendezvous point. Our current
interface allows processes to associate a descriptor with a region,
which it may then pass to other processes that wish to attach to
the region.

It is not very clear here how the initiating process was supposed to get
this fd to begin with, since no mention of /dev/zero is made anywhere in
that document. Anyway it never got implemented; the BSD kernel sources
very clearly disallow any call to mmap with an fd other than -1 and with
MAP_ANON(YMOUS).

It would be neat to implement in Linux, if it's not too hard, what BSD
had planned but never came around to, i.e allowing a mmapped region of
/dev/zero to be shared by unrelated processes by passing the fd through
a Unix-domain socket and having the second process do its own mmap on
the received fd. It would make a better alternative to SysV shm, if
only because it automatically cleans up when the last process using it
dies.

-- 
Roger Espel Llima
espel@llaic.u-clermont1.fr, espel@unix.bigots.org
http://www.eleves.ens.fr:8080/home/espel/index.html