kernel - Implement sbrk(), change low-address mmap hinting
commit4b5665564ef37dc939a3a9ffbafaab9894c18885
authorMatthew Dillon <dillon@apollo.backplane.com>
Sun, 17 Feb 2019 00:41:25 +0000 (16 16:41 -0800)
committerMatthew Dillon <dillon@apollo.backplane.com>
Mon, 18 Feb 2019 01:48:24 +0000 (17 17:48 -0800)
tree10d566fe01d714e63776b49cc5d1f1d4cd564a3a
parent8618d94a0e2ff8303ad93c123a3fa598c26a116e
kernel - Implement sbrk(), change low-address mmap hinting

* Change mmap()'s internal lower address bound from dmax (32GB)
  to RLIMIT_DATA's current value.  This allows the rlimit to be
  e.g. reduced and for hinted mmap()s to then map space below
  the 4GB mark.  The default data rlimit is 32GB.

  This change is needed to support several languages, at least
  lua and probably another one or two, who use mmap hinting
  under the assumption that it can map space below the 4GB
  address mark.  The data limit must be lowered with a limit command
  too, which can be scripted or patched for such programs.

* Implement the sbrk() system call.  This system call was already
  present but just returned EOPNOTSUPP and libc previously had its
  own shim for sbrk() which used the ancient break() system call.
  (Note that the prior implementation did not ENOSYS or signal).

  sbrk() in the kernel is thread-safe for positive increments and
  is also byte-granular (the old libc sbrk() was only page-granular).

  sbrk() in the kernel does not implement negative increments and
  will return EOPNOTSUPP if asked to.  Negative increments were
  historically designed to be able to 'free' memory allocated with
  sbrk(), but it is not possible to implement the case in a modern
  VM system due to the mmap changes above.

  (1) Because the new mmap hinting changes make it possible for
  normal mmap()s to have mapped space prior to the RLIMIT_DATA resource
  limit being increased, causing intermingling of sbrk() and user mmap()d
  regions.  (2) because negative increments are not even remotely
  thread-safe.

* Note the previous commit refactored libc to use the kernel sbrk()
  and fall-back to its previous emulation code on failure, so libc
  supports both new and old kernels.

* Remove the brk() shim from libc.  brk() is not implemented by the
  kernel.  Symbol removed.  Requires testing against ports so we may
  have to add it back in but basically there is no way to implement
  brk() properly with the mmap() hinting fix

* Adjust manual pages.
20 files changed:
include/unistd.h
lib/libc/sys/brk.2
sys/kern/imgact_aout.c
sys/kern/imgact_elf.c
sys/kern/imgact_gzip.c
sys/kern/init_main.c
sys/kern/kern_checkpoint.c
sys/kern/kern_clock.c
sys/kern/kern_exec.c
sys/kern/kern_fork.c
sys/kern/kern_kinfo.c
sys/kern/syscalls.master
sys/libkern/arc4random.c
sys/sys/ckpt.h
sys/sys/libkern.h
sys/sys/param.h
sys/vm/vm_map.c
sys/vm/vm_map.h
sys/vm/vm_mmap.c
sys/vm/vm_unix.c