overcommit-accounting.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. .. _overcommit_accounting:
  2. =====================
  3. Overcommit Accounting
  4. =====================
  5. The Linux kernel supports the following overcommit handling modes
  6. 0
  7. Heuristic overcommit handling. Obvious overcommits of address
  8. space are refused. Used for a typical system. It ensures a
  9. seriously wild allocation fails while allowing overcommit to
  10. reduce swap usage. root is allowed to allocate slightly more
  11. memory in this mode. This is the default.
  12. 1
  13. Always overcommit. Appropriate for some scientific
  14. applications. Classic example is code using sparse arrays and
  15. just relying on the virtual memory consisting almost entirely
  16. of zero pages.
  17. 2
  18. Don't overcommit. The total address space commit for the
  19. system is not permitted to exceed swap + a configurable amount
  20. (default is 50%) of physical RAM. Depending on the amount you
  21. use, in most situations this means a process will not be
  22. killed while accessing pages but will receive errors on memory
  23. allocation as appropriate.
  24. Useful for applications that want to guarantee their memory
  25. allocations will be available in the future without having to
  26. initialize every page.
  27. The overcommit policy is set via the sysctl ``vm.overcommit_memory``.
  28. The overcommit amount can be set via ``vm.overcommit_ratio`` (percentage)
  29. or ``vm.overcommit_kbytes`` (absolute value).
  30. The current overcommit limit and amount committed are viewable in
  31. ``/proc/meminfo`` as CommitLimit and Committed_AS respectively.
  32. Gotchas
  33. =======
  34. The C language stack growth does an implicit mremap. If you want absolute
  35. guarantees and run close to the edge you MUST mmap your stack for the
  36. largest size you think you will need. For typical stack usage this does
  37. not matter much but it's a corner case if you really really care
  38. In mode 2 the MAP_NORESERVE flag is ignored.
  39. How It Works
  40. ============
  41. The overcommit is based on the following rules
  42. For a file backed map
  43. | SHARED or READ-only - 0 cost (the file is the map not swap)
  44. | PRIVATE WRITABLE - size of mapping per instance
  45. For an anonymous or ``/dev/zero`` map
  46. | SHARED - size of mapping
  47. | PRIVATE READ-only - 0 cost (but of little use)
  48. | PRIVATE WRITABLE - size of mapping per instance
  49. Additional accounting
  50. | Pages made writable copies by mmap
  51. | shmfs memory drawn from the same pool
  52. Status
  53. ======
  54. * We account mmap memory mappings
  55. * We account mprotect changes in commit
  56. * We account mremap changes in size
  57. * We account brk
  58. * We account munmap
  59. * We report the commit status in /proc
  60. * Account and check on fork
  61. * Review stack handling/building on exec
  62. * SHMfs accounting
  63. * Implement actual limit enforcement
  64. To Do
  65. =====
  66. * Account ptrace pages (this is hard)