pids.rst 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. =========================
  2. Process Number Controller
  3. =========================
  4. Abstract
  5. --------
  6. The process number controller is used to allow a cgroup hierarchy to stop any
  7. new tasks from being fork()'d or clone()'d after a certain limit is reached.
  8. Since it is trivial to hit the task limit without hitting any kmemcg limits in
  9. place, PIDs are a fundamental resource. As such, PID exhaustion must be
  10. preventable in the scope of a cgroup hierarchy by allowing resource limiting of
  11. the number of tasks in a cgroup.
  12. Usage
  13. -----
  14. In order to use the `pids` controller, set the maximum number of tasks in
  15. pids.max (this is not available in the root cgroup for obvious reasons). The
  16. number of processes currently in the cgroup is given by pids.current.
  17. Organisational operations are not blocked by cgroup policies, so it is possible
  18. to have pids.current > pids.max. This can be done by either setting the limit to
  19. be smaller than pids.current, or attaching enough processes to the cgroup such
  20. that pids.current > pids.max. However, it is not possible to violate a cgroup
  21. policy through fork() or clone(). fork() and clone() will return -EAGAIN if the
  22. creation of a new process would cause a cgroup policy to be violated.
  23. To set a cgroup to have no limit, set pids.max to "max". This is the default for
  24. all new cgroups (N.B. that PID limits are hierarchical, so the most stringent
  25. limit in the hierarchy is followed).
  26. pids.current tracks all child cgroup hierarchies, so parent/pids.current is a
  27. superset of parent/child/pids.current.
  28. The pids.events file contains event counters:
  29. - max: Number of times fork failed because limit was hit.
  30. Example
  31. -------
  32. First, we mount the pids controller::
  33. # mkdir -p /sys/fs/cgroup/pids
  34. # mount -t cgroup -o pids none /sys/fs/cgroup/pids
  35. Then we create a hierarchy, set limits and attach processes to it::
  36. # mkdir -p /sys/fs/cgroup/pids/parent/child
  37. # echo 2 > /sys/fs/cgroup/pids/parent/pids.max
  38. # echo $$ > /sys/fs/cgroup/pids/parent/cgroup.procs
  39. # cat /sys/fs/cgroup/pids/parent/pids.current
  40. 2
  41. #
  42. It should be noted that attempts to overcome the set limit (2 in this case) will
  43. fail::
  44. # cat /sys/fs/cgroup/pids/parent/pids.current
  45. 2
  46. # ( /bin/echo "Here's some processes for you." | cat )
  47. sh: fork: Resource temporary unavailable
  48. #
  49. Even if we migrate to a child cgroup (which doesn't have a set limit), we will
  50. not be able to overcome the most stringent limit in the hierarchy (in this case,
  51. parent's)::
  52. # echo $$ > /sys/fs/cgroup/pids/parent/child/cgroup.procs
  53. # cat /sys/fs/cgroup/pids/parent/pids.current
  54. 2
  55. # cat /sys/fs/cgroup/pids/parent/child/pids.current
  56. 2
  57. # cat /sys/fs/cgroup/pids/parent/child/pids.max
  58. max
  59. # ( /bin/echo "Here's some processes for you." | cat )
  60. sh: fork: Resource temporary unavailable
  61. #
  62. We can set a limit that is smaller than pids.current, which will stop any new
  63. processes from being forked at all (note that the shell itself counts towards
  64. pids.current)::
  65. # echo 1 > /sys/fs/cgroup/pids/parent/pids.max
  66. # /bin/echo "We can't even spawn a single process now."
  67. sh: fork: Resource temporary unavailable
  68. # echo 0 > /sys/fs/cgroup/pids/parent/pids.max
  69. # /bin/echo "We can't even spawn a single process now."
  70. sh: fork: Resource temporary unavailable
  71. #