pids.txt 3.0 KB

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