[nsd-users] Remove needless memory zeroing

Michael McConville mmcco at mykolab.com
Tue Jan 19 19:09:24 UTC 2016

Stephane Bortzmeyer wrote:
> On Mon, Jan 18, 2016 at 10:46:03PM -0500,
>  Michael McConville <mmcco at mykolab.com> wrote 
>  a message of 24 lines which said:
> > Unless I'm misunderstanding something, anonymously mmapped memory is
> > always zeroed by the kernel
> This is written in Linux mmap man page:
> > its contents are initialized to zero
> But not in FreeBSD mmap man page:
> >     MAP_ANON           Map anonymous memory not associated with any specific
> >                        file.  The file descriptor used for creating MAP_ANON
> >                        must be -1.  The offset argument must be 0.
> So, portability seems to require mmset.

I know that OpenBSD zeroes anonymously mmapped pages. This assumption is
used throughout the codebase, but it isn't mentioned in the man page
(see below).

I just looked at FreeBSD code. It seems that it also zeroes and that
core code depends on that. For example, see pages_map() called through
chunk_alloc_mmap_slow() in jemalloc. Interestingly, I also found some
apparently needless memset() calls elsewhere.

It seems that software that uses anonymous mmapping often depends on it
being zeroed, so there isn't much of a choice. For example, both
jemalloc and Firefox contain the idiom of using VirtualAlloc (which
zeroes) on Windows and anonymous mmapping on Unix.

Regardless, remember that this memory is coming from the kernel. If it
weren't zeroed, it would be likely to contain sensitive memory from the
kernel or other processes. I've been told that that's why it doesn't
appear in the OpenBSD man page: it's considered implied.

More information about the nsd-users mailing list