xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/cgroup-v1/cgroups.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun==============
2*4882a593SmuzhiyunControl Groups
3*4882a593Smuzhiyun==============
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunWritten by Paul Menage <menage@google.com> based on
6*4882a593SmuzhiyunDocumentation/admin-guide/cgroup-v1/cpusets.rst
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunOriginal copyright statements from cpusets.txt:
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunPortions Copyright (C) 2004 BULL SA.
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunPortions Copyright (c) 2004-2006 Silicon Graphics, Inc.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunModified by Paul Jackson <pj@sgi.com>
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunModified by Christoph Lameter <cl@linux.com>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun.. CONTENTS:
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun	1. Control Groups
21*4882a593Smuzhiyun	1.1 What are cgroups ?
22*4882a593Smuzhiyun	1.2 Why are cgroups needed ?
23*4882a593Smuzhiyun	1.3 How are cgroups implemented ?
24*4882a593Smuzhiyun	1.4 What does notify_on_release do ?
25*4882a593Smuzhiyun	1.5 What does clone_children do ?
26*4882a593Smuzhiyun	1.6 How do I use cgroups ?
27*4882a593Smuzhiyun	2. Usage Examples and Syntax
28*4882a593Smuzhiyun	2.1 Basic Usage
29*4882a593Smuzhiyun	2.2 Attaching processes
30*4882a593Smuzhiyun	2.3 Mounting hierarchies by name
31*4882a593Smuzhiyun	3. Kernel API
32*4882a593Smuzhiyun	3.1 Overview
33*4882a593Smuzhiyun	3.2 Synchronization
34*4882a593Smuzhiyun	3.3 Subsystem API
35*4882a593Smuzhiyun	4. Extended attributes usage
36*4882a593Smuzhiyun	5. Questions
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun1. Control Groups
39*4882a593Smuzhiyun=================
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun1.1 What are cgroups ?
42*4882a593Smuzhiyun----------------------
43*4882a593Smuzhiyun
44*4882a593SmuzhiyunControl Groups provide a mechanism for aggregating/partitioning sets of
45*4882a593Smuzhiyuntasks, and all their future children, into hierarchical groups with
46*4882a593Smuzhiyunspecialized behaviour.
47*4882a593Smuzhiyun
48*4882a593SmuzhiyunDefinitions:
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunA *cgroup* associates a set of tasks with a set of parameters for one
51*4882a593Smuzhiyunor more subsystems.
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunA *subsystem* is a module that makes use of the task grouping
54*4882a593Smuzhiyunfacilities provided by cgroups to treat groups of tasks in
55*4882a593Smuzhiyunparticular ways. A subsystem is typically a "resource controller" that
56*4882a593Smuzhiyunschedules a resource or applies per-cgroup limits, but it may be
57*4882a593Smuzhiyunanything that wants to act on a group of processes, e.g. a
58*4882a593Smuzhiyunvirtualization subsystem.
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunA *hierarchy* is a set of cgroups arranged in a tree, such that
61*4882a593Smuzhiyunevery task in the system is in exactly one of the cgroups in the
62*4882a593Smuzhiyunhierarchy, and a set of subsystems; each subsystem has system-specific
63*4882a593Smuzhiyunstate attached to each cgroup in the hierarchy.  Each hierarchy has
64*4882a593Smuzhiyunan instance of the cgroup virtual filesystem associated with it.
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunAt any one time there may be multiple active hierarchies of task
67*4882a593Smuzhiyuncgroups. Each hierarchy is a partition of all tasks in the system.
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunUser-level code may create and destroy cgroups by name in an
70*4882a593Smuzhiyuninstance of the cgroup virtual file system, specify and query to
71*4882a593Smuzhiyunwhich cgroup a task is assigned, and list the task PIDs assigned to
72*4882a593Smuzhiyuna cgroup. Those creations and assignments only affect the hierarchy
73*4882a593Smuzhiyunassociated with that instance of the cgroup file system.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunOn their own, the only use for cgroups is for simple job
76*4882a593Smuzhiyuntracking. The intention is that other subsystems hook into the generic
77*4882a593Smuzhiyuncgroup support to provide new attributes for cgroups, such as
78*4882a593Smuzhiyunaccounting/limiting the resources which processes in a cgroup can
79*4882a593Smuzhiyunaccess. For example, cpusets (see Documentation/admin-guide/cgroup-v1/cpusets.rst) allow
80*4882a593Smuzhiyunyou to associate a set of CPUs and a set of memory nodes with the
81*4882a593Smuzhiyuntasks in each cgroup.
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun1.2 Why are cgroups needed ?
84*4882a593Smuzhiyun----------------------------
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunThere are multiple efforts to provide process aggregations in the
87*4882a593SmuzhiyunLinux kernel, mainly for resource-tracking purposes. Such efforts
88*4882a593Smuzhiyuninclude cpusets, CKRM/ResGroups, UserBeanCounters, and virtual server
89*4882a593Smuzhiyunnamespaces. These all require the basic notion of a
90*4882a593Smuzhiyungrouping/partitioning of processes, with newly forked processes ending
91*4882a593Smuzhiyunup in the same group (cgroup) as their parent process.
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunThe kernel cgroup patch provides the minimum essential kernel
94*4882a593Smuzhiyunmechanisms required to efficiently implement such groups. It has
95*4882a593Smuzhiyunminimal impact on the system fast paths, and provides hooks for
96*4882a593Smuzhiyunspecific subsystems such as cpusets to provide additional behaviour as
97*4882a593Smuzhiyundesired.
98*4882a593Smuzhiyun
99*4882a593SmuzhiyunMultiple hierarchy support is provided to allow for situations where
100*4882a593Smuzhiyunthe division of tasks into cgroups is distinctly different for
101*4882a593Smuzhiyundifferent subsystems - having parallel hierarchies allows each
102*4882a593Smuzhiyunhierarchy to be a natural division of tasks, without having to handle
103*4882a593Smuzhiyuncomplex combinations of tasks that would be present if several
104*4882a593Smuzhiyununrelated subsystems needed to be forced into the same tree of
105*4882a593Smuzhiyuncgroups.
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunAt one extreme, each resource controller or subsystem could be in a
108*4882a593Smuzhiyunseparate hierarchy; at the other extreme, all subsystems
109*4882a593Smuzhiyunwould be attached to the same hierarchy.
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunAs an example of a scenario (originally proposed by vatsa@in.ibm.com)
112*4882a593Smuzhiyunthat can benefit from multiple hierarchies, consider a large
113*4882a593Smuzhiyununiversity server with various users - students, professors, system
114*4882a593Smuzhiyuntasks etc. The resource planning for this server could be along the
115*4882a593Smuzhiyunfollowing lines::
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun       CPU :          "Top cpuset"
118*4882a593Smuzhiyun                       /       \
119*4882a593Smuzhiyun               CPUSet1         CPUSet2
120*4882a593Smuzhiyun                  |               |
121*4882a593Smuzhiyun               (Professors)    (Students)
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun               In addition (system tasks) are attached to topcpuset (so
124*4882a593Smuzhiyun               that they can run anywhere) with a limit of 20%
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun       Memory : Professors (50%), Students (30%), system (20%)
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun       Disk : Professors (50%), Students (30%), system (20%)
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun       Network : WWW browsing (20%), Network File System (60%), others (20%)
131*4882a593Smuzhiyun                               / \
132*4882a593Smuzhiyun               Professors (15%)  students (5%)
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunBrowsers like Firefox/Lynx go into the WWW network class, while (k)nfsd goes
135*4882a593Smuzhiyuninto the NFS network class.
136*4882a593Smuzhiyun
137*4882a593SmuzhiyunAt the same time Firefox/Lynx will share an appropriate CPU/Memory class
138*4882a593Smuzhiyundepending on who launched it (prof/student).
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunWith the ability to classify tasks differently for different resources
141*4882a593Smuzhiyun(by putting those resource subsystems in different hierarchies),
142*4882a593Smuzhiyunthe admin can easily set up a script which receives exec notifications
143*4882a593Smuzhiyunand depending on who is launching the browser he can::
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun    # echo browser_pid > /sys/fs/cgroup/<restype>/<userclass>/tasks
146*4882a593Smuzhiyun
147*4882a593SmuzhiyunWith only a single hierarchy, he now would potentially have to create
148*4882a593Smuzhiyuna separate cgroup for every browser launched and associate it with
149*4882a593Smuzhiyunappropriate network and other resource class.  This may lead to
150*4882a593Smuzhiyunproliferation of such cgroups.
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunAlso let's say that the administrator would like to give enhanced network
153*4882a593Smuzhiyunaccess temporarily to a student's browser (since it is night and the user
154*4882a593Smuzhiyunwants to do online gaming :))  OR give one of the student's simulation
155*4882a593Smuzhiyunapps enhanced CPU power.
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunWith ability to write PIDs directly to resource classes, it's just a
158*4882a593Smuzhiyunmatter of::
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun       # echo pid > /sys/fs/cgroup/network/<new_class>/tasks
161*4882a593Smuzhiyun       (after some time)
162*4882a593Smuzhiyun       # echo pid > /sys/fs/cgroup/network/<orig_class>/tasks
163*4882a593Smuzhiyun
164*4882a593SmuzhiyunWithout this ability, the administrator would have to split the cgroup into
165*4882a593Smuzhiyunmultiple separate ones and then associate the new cgroups with the
166*4882a593Smuzhiyunnew resource classes.
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun1.3 How are cgroups implemented ?
171*4882a593Smuzhiyun---------------------------------
172*4882a593Smuzhiyun
173*4882a593SmuzhiyunControl Groups extends the kernel as follows:
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun - Each task in the system has a reference-counted pointer to a
176*4882a593Smuzhiyun   css_set.
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun - A css_set contains a set of reference-counted pointers to
179*4882a593Smuzhiyun   cgroup_subsys_state objects, one for each cgroup subsystem
180*4882a593Smuzhiyun   registered in the system. There is no direct link from a task to
181*4882a593Smuzhiyun   the cgroup of which it's a member in each hierarchy, but this
182*4882a593Smuzhiyun   can be determined by following pointers through the
183*4882a593Smuzhiyun   cgroup_subsys_state objects. This is because accessing the
184*4882a593Smuzhiyun   subsystem state is something that's expected to happen frequently
185*4882a593Smuzhiyun   and in performance-critical code, whereas operations that require a
186*4882a593Smuzhiyun   task's actual cgroup assignments (in particular, moving between
187*4882a593Smuzhiyun   cgroups) are less common. A linked list runs through the cg_list
188*4882a593Smuzhiyun   field of each task_struct using the css_set, anchored at
189*4882a593Smuzhiyun   css_set->tasks.
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun - A cgroup hierarchy filesystem can be mounted for browsing and
192*4882a593Smuzhiyun   manipulation from user space.
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun - You can list all the tasks (by PID) attached to any cgroup.
195*4882a593Smuzhiyun
196*4882a593SmuzhiyunThe implementation of cgroups requires a few, simple hooks
197*4882a593Smuzhiyuninto the rest of the kernel, none in performance-critical paths:
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun - in init/main.c, to initialize the root cgroups and initial
200*4882a593Smuzhiyun   css_set at system boot.
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun - in fork and exit, to attach and detach a task from its css_set.
203*4882a593Smuzhiyun
204*4882a593SmuzhiyunIn addition, a new file system of type "cgroup" may be mounted, to
205*4882a593Smuzhiyunenable browsing and modifying the cgroups presently known to the
206*4882a593Smuzhiyunkernel.  When mounting a cgroup hierarchy, you may specify a
207*4882a593Smuzhiyuncomma-separated list of subsystems to mount as the filesystem mount
208*4882a593Smuzhiyunoptions.  By default, mounting the cgroup filesystem attempts to
209*4882a593Smuzhiyunmount a hierarchy containing all registered subsystems.
210*4882a593Smuzhiyun
211*4882a593SmuzhiyunIf an active hierarchy with exactly the same set of subsystems already
212*4882a593Smuzhiyunexists, it will be reused for the new mount. If no existing hierarchy
213*4882a593Smuzhiyunmatches, and any of the requested subsystems are in use in an existing
214*4882a593Smuzhiyunhierarchy, the mount will fail with -EBUSY. Otherwise, a new hierarchy
215*4882a593Smuzhiyunis activated, associated with the requested subsystems.
216*4882a593Smuzhiyun
217*4882a593SmuzhiyunIt's not currently possible to bind a new subsystem to an active
218*4882a593Smuzhiyuncgroup hierarchy, or to unbind a subsystem from an active cgroup
219*4882a593Smuzhiyunhierarchy. This may be possible in future, but is fraught with nasty
220*4882a593Smuzhiyunerror-recovery issues.
221*4882a593Smuzhiyun
222*4882a593SmuzhiyunWhen a cgroup filesystem is unmounted, if there are any
223*4882a593Smuzhiyunchild cgroups created below the top-level cgroup, that hierarchy
224*4882a593Smuzhiyunwill remain active even though unmounted; if there are no
225*4882a593Smuzhiyunchild cgroups then the hierarchy will be deactivated.
226*4882a593Smuzhiyun
227*4882a593SmuzhiyunNo new system calls are added for cgroups - all support for
228*4882a593Smuzhiyunquerying and modifying cgroups is via this cgroup file system.
229*4882a593Smuzhiyun
230*4882a593SmuzhiyunEach task under /proc has an added file named 'cgroup' displaying,
231*4882a593Smuzhiyunfor each active hierarchy, the subsystem names and the cgroup name
232*4882a593Smuzhiyunas the path relative to the root of the cgroup file system.
233*4882a593Smuzhiyun
234*4882a593SmuzhiyunEach cgroup is represented by a directory in the cgroup file system
235*4882a593Smuzhiyuncontaining the following files describing that cgroup:
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun - tasks: list of tasks (by PID) attached to that cgroup.  This list
238*4882a593Smuzhiyun   is not guaranteed to be sorted.  Writing a thread ID into this file
239*4882a593Smuzhiyun   moves the thread into this cgroup.
240*4882a593Smuzhiyun - cgroup.procs: list of thread group IDs in the cgroup.  This list is
241*4882a593Smuzhiyun   not guaranteed to be sorted or free of duplicate TGIDs, and userspace
242*4882a593Smuzhiyun   should sort/uniquify the list if this property is required.
243*4882a593Smuzhiyun   Writing a thread group ID into this file moves all threads in that
244*4882a593Smuzhiyun   group into this cgroup.
245*4882a593Smuzhiyun - notify_on_release flag: run the release agent on exit?
246*4882a593Smuzhiyun - release_agent: the path to use for release notifications (this file
247*4882a593Smuzhiyun   exists in the top cgroup only)
248*4882a593Smuzhiyun
249*4882a593SmuzhiyunOther subsystems such as cpusets may add additional files in each
250*4882a593Smuzhiyuncgroup dir.
251*4882a593Smuzhiyun
252*4882a593SmuzhiyunNew cgroups are created using the mkdir system call or shell
253*4882a593Smuzhiyuncommand.  The properties of a cgroup, such as its flags, are
254*4882a593Smuzhiyunmodified by writing to the appropriate file in that cgroups
255*4882a593Smuzhiyundirectory, as listed above.
256*4882a593Smuzhiyun
257*4882a593SmuzhiyunThe named hierarchical structure of nested cgroups allows partitioning
258*4882a593Smuzhiyuna large system into nested, dynamically changeable, "soft-partitions".
259*4882a593Smuzhiyun
260*4882a593SmuzhiyunThe attachment of each task, automatically inherited at fork by any
261*4882a593Smuzhiyunchildren of that task, to a cgroup allows organizing the work load
262*4882a593Smuzhiyunon a system into related sets of tasks.  A task may be re-attached to
263*4882a593Smuzhiyunany other cgroup, if allowed by the permissions on the necessary
264*4882a593Smuzhiyuncgroup file system directories.
265*4882a593Smuzhiyun
266*4882a593SmuzhiyunWhen a task is moved from one cgroup to another, it gets a new
267*4882a593Smuzhiyuncss_set pointer - if there's an already existing css_set with the
268*4882a593Smuzhiyundesired collection of cgroups then that group is reused, otherwise a new
269*4882a593Smuzhiyuncss_set is allocated. The appropriate existing css_set is located by
270*4882a593Smuzhiyunlooking into a hash table.
271*4882a593Smuzhiyun
272*4882a593SmuzhiyunTo allow access from a cgroup to the css_sets (and hence tasks)
273*4882a593Smuzhiyunthat comprise it, a set of cg_cgroup_link objects form a lattice;
274*4882a593Smuzhiyuneach cg_cgroup_link is linked into a list of cg_cgroup_links for
275*4882a593Smuzhiyuna single cgroup on its cgrp_link_list field, and a list of
276*4882a593Smuzhiyuncg_cgroup_links for a single css_set on its cg_link_list.
277*4882a593Smuzhiyun
278*4882a593SmuzhiyunThus the set of tasks in a cgroup can be listed by iterating over
279*4882a593Smuzhiyuneach css_set that references the cgroup, and sub-iterating over
280*4882a593Smuzhiyuneach css_set's task set.
281*4882a593Smuzhiyun
282*4882a593SmuzhiyunThe use of a Linux virtual file system (vfs) to represent the
283*4882a593Smuzhiyuncgroup hierarchy provides for a familiar permission and name space
284*4882a593Smuzhiyunfor cgroups, with a minimum of additional kernel code.
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun1.4 What does notify_on_release do ?
287*4882a593Smuzhiyun------------------------------------
288*4882a593Smuzhiyun
289*4882a593SmuzhiyunIf the notify_on_release flag is enabled (1) in a cgroup, then
290*4882a593Smuzhiyunwhenever the last task in the cgroup leaves (exits or attaches to
291*4882a593Smuzhiyunsome other cgroup) and the last child cgroup of that cgroup
292*4882a593Smuzhiyunis removed, then the kernel runs the command specified by the contents
293*4882a593Smuzhiyunof the "release_agent" file in that hierarchy's root directory,
294*4882a593Smuzhiyunsupplying the pathname (relative to the mount point of the cgroup
295*4882a593Smuzhiyunfile system) of the abandoned cgroup.  This enables automatic
296*4882a593Smuzhiyunremoval of abandoned cgroups.  The default value of
297*4882a593Smuzhiyunnotify_on_release in the root cgroup at system boot is disabled
298*4882a593Smuzhiyun(0).  The default value of other cgroups at creation is the current
299*4882a593Smuzhiyunvalue of their parents' notify_on_release settings. The default value of
300*4882a593Smuzhiyuna cgroup hierarchy's release_agent path is empty.
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun1.5 What does clone_children do ?
303*4882a593Smuzhiyun---------------------------------
304*4882a593Smuzhiyun
305*4882a593SmuzhiyunThis flag only affects the cpuset controller. If the clone_children
306*4882a593Smuzhiyunflag is enabled (1) in a cgroup, a new cpuset cgroup will copy its
307*4882a593Smuzhiyunconfiguration from the parent during initialization.
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun1.6 How do I use cgroups ?
310*4882a593Smuzhiyun--------------------------
311*4882a593Smuzhiyun
312*4882a593SmuzhiyunTo start a new job that is to be contained within a cgroup, using
313*4882a593Smuzhiyunthe "cpuset" cgroup subsystem, the steps are something like::
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun 1) mount -t tmpfs cgroup_root /sys/fs/cgroup
316*4882a593Smuzhiyun 2) mkdir /sys/fs/cgroup/cpuset
317*4882a593Smuzhiyun 3) mount -t cgroup -ocpuset cpuset /sys/fs/cgroup/cpuset
318*4882a593Smuzhiyun 4) Create the new cgroup by doing mkdir's and write's (or echo's) in
319*4882a593Smuzhiyun    the /sys/fs/cgroup/cpuset virtual file system.
320*4882a593Smuzhiyun 5) Start a task that will be the "founding father" of the new job.
321*4882a593Smuzhiyun 6) Attach that task to the new cgroup by writing its PID to the
322*4882a593Smuzhiyun    /sys/fs/cgroup/cpuset tasks file for that cgroup.
323*4882a593Smuzhiyun 7) fork, exec or clone the job tasks from this founding father task.
324*4882a593Smuzhiyun
325*4882a593SmuzhiyunFor example, the following sequence of commands will setup a cgroup
326*4882a593Smuzhiyunnamed "Charlie", containing just CPUs 2 and 3, and Memory Node 1,
327*4882a593Smuzhiyunand then start a subshell 'sh' in that cgroup::
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun  mount -t tmpfs cgroup_root /sys/fs/cgroup
330*4882a593Smuzhiyun  mkdir /sys/fs/cgroup/cpuset
331*4882a593Smuzhiyun  mount -t cgroup cpuset -ocpuset /sys/fs/cgroup/cpuset
332*4882a593Smuzhiyun  cd /sys/fs/cgroup/cpuset
333*4882a593Smuzhiyun  mkdir Charlie
334*4882a593Smuzhiyun  cd Charlie
335*4882a593Smuzhiyun  /bin/echo 2-3 > cpuset.cpus
336*4882a593Smuzhiyun  /bin/echo 1 > cpuset.mems
337*4882a593Smuzhiyun  /bin/echo $$ > tasks
338*4882a593Smuzhiyun  sh
339*4882a593Smuzhiyun  # The subshell 'sh' is now running in cgroup Charlie
340*4882a593Smuzhiyun  # The next line should display '/Charlie'
341*4882a593Smuzhiyun  cat /proc/self/cgroup
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun2. Usage Examples and Syntax
344*4882a593Smuzhiyun============================
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun2.1 Basic Usage
347*4882a593Smuzhiyun---------------
348*4882a593Smuzhiyun
349*4882a593SmuzhiyunCreating, modifying, using cgroups can be done through the cgroup
350*4882a593Smuzhiyunvirtual filesystem.
351*4882a593Smuzhiyun
352*4882a593SmuzhiyunTo mount a cgroup hierarchy with all available subsystems, type::
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun  # mount -t cgroup xxx /sys/fs/cgroup
355*4882a593Smuzhiyun
356*4882a593SmuzhiyunThe "xxx" is not interpreted by the cgroup code, but will appear in
357*4882a593Smuzhiyun/proc/mounts so may be any useful identifying string that you like.
358*4882a593Smuzhiyun
359*4882a593SmuzhiyunNote: Some subsystems do not work without some user input first.  For instance,
360*4882a593Smuzhiyunif cpusets are enabled the user will have to populate the cpus and mems files
361*4882a593Smuzhiyunfor each new cgroup created before that group can be used.
362*4882a593Smuzhiyun
363*4882a593SmuzhiyunAs explained in section `1.2 Why are cgroups needed?` you should create
364*4882a593Smuzhiyundifferent hierarchies of cgroups for each single resource or group of
365*4882a593Smuzhiyunresources you want to control. Therefore, you should mount a tmpfs on
366*4882a593Smuzhiyun/sys/fs/cgroup and create directories for each cgroup resource or resource
367*4882a593Smuzhiyungroup::
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun  # mount -t tmpfs cgroup_root /sys/fs/cgroup
370*4882a593Smuzhiyun  # mkdir /sys/fs/cgroup/rg1
371*4882a593Smuzhiyun
372*4882a593SmuzhiyunTo mount a cgroup hierarchy with just the cpuset and memory
373*4882a593Smuzhiyunsubsystems, type::
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun  # mount -t cgroup -o cpuset,memory hier1 /sys/fs/cgroup/rg1
376*4882a593Smuzhiyun
377*4882a593SmuzhiyunWhile remounting cgroups is currently supported, it is not recommend
378*4882a593Smuzhiyunto use it. Remounting allows changing bound subsystems and
379*4882a593Smuzhiyunrelease_agent. Rebinding is hardly useful as it only works when the
380*4882a593Smuzhiyunhierarchy is empty and release_agent itself should be replaced with
381*4882a593Smuzhiyunconventional fsnotify. The support for remounting will be removed in
382*4882a593Smuzhiyunthe future.
383*4882a593Smuzhiyun
384*4882a593SmuzhiyunTo Specify a hierarchy's release_agent::
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun  # mount -t cgroup -o cpuset,release_agent="/sbin/cpuset_release_agent" \
387*4882a593Smuzhiyun    xxx /sys/fs/cgroup/rg1
388*4882a593Smuzhiyun
389*4882a593SmuzhiyunNote that specifying 'release_agent' more than once will return failure.
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunNote that changing the set of subsystems is currently only supported
392*4882a593Smuzhiyunwhen the hierarchy consists of a single (root) cgroup. Supporting
393*4882a593Smuzhiyunthe ability to arbitrarily bind/unbind subsystems from an existing
394*4882a593Smuzhiyuncgroup hierarchy is intended to be implemented in the future.
395*4882a593Smuzhiyun
396*4882a593SmuzhiyunThen under /sys/fs/cgroup/rg1 you can find a tree that corresponds to the
397*4882a593Smuzhiyuntree of the cgroups in the system. For instance, /sys/fs/cgroup/rg1
398*4882a593Smuzhiyunis the cgroup that holds the whole system.
399*4882a593Smuzhiyun
400*4882a593SmuzhiyunIf you want to change the value of release_agent::
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun  # echo "/sbin/new_release_agent" > /sys/fs/cgroup/rg1/release_agent
403*4882a593Smuzhiyun
404*4882a593SmuzhiyunIt can also be changed via remount.
405*4882a593Smuzhiyun
406*4882a593SmuzhiyunIf you want to create a new cgroup under /sys/fs/cgroup/rg1::
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun  # cd /sys/fs/cgroup/rg1
409*4882a593Smuzhiyun  # mkdir my_cgroup
410*4882a593Smuzhiyun
411*4882a593SmuzhiyunNow you want to do something with this cgroup:
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun  # cd my_cgroup
414*4882a593Smuzhiyun
415*4882a593SmuzhiyunIn this directory you can find several files::
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun  # ls
418*4882a593Smuzhiyun  cgroup.procs notify_on_release tasks
419*4882a593Smuzhiyun  (plus whatever files added by the attached subsystems)
420*4882a593Smuzhiyun
421*4882a593SmuzhiyunNow attach your shell to this cgroup::
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun  # /bin/echo $$ > tasks
424*4882a593Smuzhiyun
425*4882a593SmuzhiyunYou can also create cgroups inside your cgroup by using mkdir in this
426*4882a593Smuzhiyundirectory::
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun  # mkdir my_sub_cs
429*4882a593Smuzhiyun
430*4882a593SmuzhiyunTo remove a cgroup, just use rmdir::
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun  # rmdir my_sub_cs
433*4882a593Smuzhiyun
434*4882a593SmuzhiyunThis will fail if the cgroup is in use (has cgroups inside, or
435*4882a593Smuzhiyunhas processes attached, or is held alive by other subsystem-specific
436*4882a593Smuzhiyunreference).
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun2.2 Attaching processes
439*4882a593Smuzhiyun-----------------------
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun::
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun  # /bin/echo PID > tasks
444*4882a593Smuzhiyun
445*4882a593SmuzhiyunNote that it is PID, not PIDs. You can only attach ONE task at a time.
446*4882a593SmuzhiyunIf you have several tasks to attach, you have to do it one after another::
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun  # /bin/echo PID1 > tasks
449*4882a593Smuzhiyun  # /bin/echo PID2 > tasks
450*4882a593Smuzhiyun	  ...
451*4882a593Smuzhiyun  # /bin/echo PIDn > tasks
452*4882a593Smuzhiyun
453*4882a593SmuzhiyunYou can attach the current shell task by echoing 0::
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun  # echo 0 > tasks
456*4882a593Smuzhiyun
457*4882a593SmuzhiyunYou can use the cgroup.procs file instead of the tasks file to move all
458*4882a593Smuzhiyunthreads in a threadgroup at once. Echoing the PID of any task in a
459*4882a593Smuzhiyunthreadgroup to cgroup.procs causes all tasks in that threadgroup to be
460*4882a593Smuzhiyunattached to the cgroup. Writing 0 to cgroup.procs moves all tasks
461*4882a593Smuzhiyunin the writing task's threadgroup.
462*4882a593Smuzhiyun
463*4882a593SmuzhiyunNote: Since every task is always a member of exactly one cgroup in each
464*4882a593Smuzhiyunmounted hierarchy, to remove a task from its current cgroup you must
465*4882a593Smuzhiyunmove it into a new cgroup (possibly the root cgroup) by writing to the
466*4882a593Smuzhiyunnew cgroup's tasks file.
467*4882a593Smuzhiyun
468*4882a593SmuzhiyunNote: Due to some restrictions enforced by some cgroup subsystems, moving
469*4882a593Smuzhiyuna process to another cgroup can fail.
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun2.3 Mounting hierarchies by name
472*4882a593Smuzhiyun--------------------------------
473*4882a593Smuzhiyun
474*4882a593SmuzhiyunPassing the name=<x> option when mounting a cgroups hierarchy
475*4882a593Smuzhiyunassociates the given name with the hierarchy.  This can be used when
476*4882a593Smuzhiyunmounting a pre-existing hierarchy, in order to refer to it by name
477*4882a593Smuzhiyunrather than by its set of active subsystems.  Each hierarchy is either
478*4882a593Smuzhiyunnameless, or has a unique name.
479*4882a593Smuzhiyun
480*4882a593SmuzhiyunThe name should match [\w.-]+
481*4882a593Smuzhiyun
482*4882a593SmuzhiyunWhen passing a name=<x> option for a new hierarchy, you need to
483*4882a593Smuzhiyunspecify subsystems manually; the legacy behaviour of mounting all
484*4882a593Smuzhiyunsubsystems when none are explicitly specified is not supported when
485*4882a593Smuzhiyunyou give a subsystem a name.
486*4882a593Smuzhiyun
487*4882a593SmuzhiyunThe name of the subsystem appears as part of the hierarchy description
488*4882a593Smuzhiyunin /proc/mounts and /proc/<pid>/cgroups.
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun3. Kernel API
492*4882a593Smuzhiyun=============
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun3.1 Overview
495*4882a593Smuzhiyun------------
496*4882a593Smuzhiyun
497*4882a593SmuzhiyunEach kernel subsystem that wants to hook into the generic cgroup
498*4882a593Smuzhiyunsystem needs to create a cgroup_subsys object. This contains
499*4882a593Smuzhiyunvarious methods, which are callbacks from the cgroup system, along
500*4882a593Smuzhiyunwith a subsystem ID which will be assigned by the cgroup system.
501*4882a593Smuzhiyun
502*4882a593SmuzhiyunOther fields in the cgroup_subsys object include:
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun- subsys_id: a unique array index for the subsystem, indicating which
505*4882a593Smuzhiyun  entry in cgroup->subsys[] this subsystem should be managing.
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun- name: should be initialized to a unique subsystem name. Should be
508*4882a593Smuzhiyun  no longer than MAX_CGROUP_TYPE_NAMELEN.
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun- early_init: indicate if the subsystem needs early initialization
511*4882a593Smuzhiyun  at system boot.
512*4882a593Smuzhiyun
513*4882a593SmuzhiyunEach cgroup object created by the system has an array of pointers,
514*4882a593Smuzhiyunindexed by subsystem ID; this pointer is entirely managed by the
515*4882a593Smuzhiyunsubsystem; the generic cgroup code will never touch this pointer.
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun3.2 Synchronization
518*4882a593Smuzhiyun-------------------
519*4882a593Smuzhiyun
520*4882a593SmuzhiyunThere is a global mutex, cgroup_mutex, used by the cgroup
521*4882a593Smuzhiyunsystem. This should be taken by anything that wants to modify a
522*4882a593Smuzhiyuncgroup. It may also be taken to prevent cgroups from being
523*4882a593Smuzhiyunmodified, but more specific locks may be more appropriate in that
524*4882a593Smuzhiyunsituation.
525*4882a593Smuzhiyun
526*4882a593SmuzhiyunSee kernel/cgroup.c for more details.
527*4882a593Smuzhiyun
528*4882a593SmuzhiyunSubsystems can take/release the cgroup_mutex via the functions
529*4882a593Smuzhiyuncgroup_lock()/cgroup_unlock().
530*4882a593Smuzhiyun
531*4882a593SmuzhiyunAccessing a task's cgroup pointer may be done in the following ways:
532*4882a593Smuzhiyun- while holding cgroup_mutex
533*4882a593Smuzhiyun- while holding the task's alloc_lock (via task_lock())
534*4882a593Smuzhiyun- inside an rcu_read_lock() section via rcu_dereference()
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun3.3 Subsystem API
537*4882a593Smuzhiyun-----------------
538*4882a593Smuzhiyun
539*4882a593SmuzhiyunEach subsystem should:
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun- add an entry in linux/cgroup_subsys.h
542*4882a593Smuzhiyun- define a cgroup_subsys object called <name>_cgrp_subsys
543*4882a593Smuzhiyun
544*4882a593SmuzhiyunEach subsystem may export the following methods. The only mandatory
545*4882a593Smuzhiyunmethods are css_alloc/free. Any others that are null are presumed to
546*4882a593Smuzhiyunbe successful no-ops.
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun``struct cgroup_subsys_state *css_alloc(struct cgroup *cgrp)``
549*4882a593Smuzhiyun(cgroup_mutex held by caller)
550*4882a593Smuzhiyun
551*4882a593SmuzhiyunCalled to allocate a subsystem state object for a cgroup. The
552*4882a593Smuzhiyunsubsystem should allocate its subsystem state object for the passed
553*4882a593Smuzhiyuncgroup, returning a pointer to the new object on success or a
554*4882a593SmuzhiyunERR_PTR() value. On success, the subsystem pointer should point to
555*4882a593Smuzhiyuna structure of type cgroup_subsys_state (typically embedded in a
556*4882a593Smuzhiyunlarger subsystem-specific object), which will be initialized by the
557*4882a593Smuzhiyuncgroup system. Note that this will be called at initialization to
558*4882a593Smuzhiyuncreate the root subsystem state for this subsystem; this case can be
559*4882a593Smuzhiyunidentified by the passed cgroup object having a NULL parent (since
560*4882a593Smuzhiyunit's the root of the hierarchy) and may be an appropriate place for
561*4882a593Smuzhiyuninitialization code.
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun``int css_online(struct cgroup *cgrp)``
564*4882a593Smuzhiyun(cgroup_mutex held by caller)
565*4882a593Smuzhiyun
566*4882a593SmuzhiyunCalled after @cgrp successfully completed all allocations and made
567*4882a593Smuzhiyunvisible to cgroup_for_each_child/descendant_*() iterators. The
568*4882a593Smuzhiyunsubsystem may choose to fail creation by returning -errno. This
569*4882a593Smuzhiyuncallback can be used to implement reliable state sharing and
570*4882a593Smuzhiyunpropagation along the hierarchy. See the comment on
571*4882a593Smuzhiyuncgroup_for_each_descendant_pre() for details.
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun``void css_offline(struct cgroup *cgrp);``
574*4882a593Smuzhiyun(cgroup_mutex held by caller)
575*4882a593Smuzhiyun
576*4882a593SmuzhiyunThis is the counterpart of css_online() and called iff css_online()
577*4882a593Smuzhiyunhas succeeded on @cgrp. This signifies the beginning of the end of
578*4882a593Smuzhiyun@cgrp. @cgrp is being removed and the subsystem should start dropping
579*4882a593Smuzhiyunall references it's holding on @cgrp. When all references are dropped,
580*4882a593Smuzhiyuncgroup removal will proceed to the next step - css_free(). After this
581*4882a593Smuzhiyuncallback, @cgrp should be considered dead to the subsystem.
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun``void css_free(struct cgroup *cgrp)``
584*4882a593Smuzhiyun(cgroup_mutex held by caller)
585*4882a593Smuzhiyun
586*4882a593SmuzhiyunThe cgroup system is about to free @cgrp; the subsystem should free
587*4882a593Smuzhiyunits subsystem state object. By the time this method is called, @cgrp
588*4882a593Smuzhiyunis completely unused; @cgrp->parent is still valid. (Note - can also
589*4882a593Smuzhiyunbe called for a newly-created cgroup if an error occurs after this
590*4882a593Smuzhiyunsubsystem's create() method has been called for the new cgroup).
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun``int can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)``
593*4882a593Smuzhiyun(cgroup_mutex held by caller)
594*4882a593Smuzhiyun
595*4882a593SmuzhiyunCalled prior to moving one or more tasks into a cgroup; if the
596*4882a593Smuzhiyunsubsystem returns an error, this will abort the attach operation.
597*4882a593Smuzhiyun@tset contains the tasks to be attached and is guaranteed to have at
598*4882a593Smuzhiyunleast one task in it.
599*4882a593Smuzhiyun
600*4882a593SmuzhiyunIf there are multiple tasks in the taskset, then:
601*4882a593Smuzhiyun  - it's guaranteed that all are from the same thread group
602*4882a593Smuzhiyun  - @tset contains all tasks from the thread group whether or not
603*4882a593Smuzhiyun    they're switching cgroups
604*4882a593Smuzhiyun  - the first task is the leader
605*4882a593Smuzhiyun
606*4882a593SmuzhiyunEach @tset entry also contains the task's old cgroup and tasks which
607*4882a593Smuzhiyunaren't switching cgroup can be skipped easily using the
608*4882a593Smuzhiyuncgroup_taskset_for_each() iterator. Note that this isn't called on a
609*4882a593Smuzhiyunfork. If this method returns 0 (success) then this should remain valid
610*4882a593Smuzhiyunwhile the caller holds cgroup_mutex and it is ensured that either
611*4882a593Smuzhiyunattach() or cancel_attach() will be called in future.
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun``void css_reset(struct cgroup_subsys_state *css)``
614*4882a593Smuzhiyun(cgroup_mutex held by caller)
615*4882a593Smuzhiyun
616*4882a593SmuzhiyunAn optional operation which should restore @css's configuration to the
617*4882a593Smuzhiyuninitial state.  This is currently only used on the unified hierarchy
618*4882a593Smuzhiyunwhen a subsystem is disabled on a cgroup through
619*4882a593Smuzhiyun"cgroup.subtree_control" but should remain enabled because other
620*4882a593Smuzhiyunsubsystems depend on it.  cgroup core makes such a css invisible by
621*4882a593Smuzhiyunremoving the associated interface files and invokes this callback so
622*4882a593Smuzhiyunthat the hidden subsystem can return to the initial neutral state.
623*4882a593SmuzhiyunThis prevents unexpected resource control from a hidden css and
624*4882a593Smuzhiyunensures that the configuration is in the initial state when it is made
625*4882a593Smuzhiyunvisible again later.
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun``void cancel_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)``
628*4882a593Smuzhiyun(cgroup_mutex held by caller)
629*4882a593Smuzhiyun
630*4882a593SmuzhiyunCalled when a task attach operation has failed after can_attach() has succeeded.
631*4882a593SmuzhiyunA subsystem whose can_attach() has some side-effects should provide this
632*4882a593Smuzhiyunfunction, so that the subsystem can implement a rollback. If not, not necessary.
633*4882a593SmuzhiyunThis will be called only about subsystems whose can_attach() operation have
634*4882a593Smuzhiyunsucceeded. The parameters are identical to can_attach().
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun``void attach(struct cgroup *cgrp, struct cgroup_taskset *tset)``
637*4882a593Smuzhiyun(cgroup_mutex held by caller)
638*4882a593Smuzhiyun
639*4882a593SmuzhiyunCalled after the task has been attached to the cgroup, to allow any
640*4882a593Smuzhiyunpost-attachment activity that requires memory allocations or blocking.
641*4882a593SmuzhiyunThe parameters are identical to can_attach().
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun``void fork(struct task_struct *task)``
644*4882a593Smuzhiyun
645*4882a593SmuzhiyunCalled when a task is forked into a cgroup.
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun``void exit(struct task_struct *task)``
648*4882a593Smuzhiyun
649*4882a593SmuzhiyunCalled during task exit.
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun``void free(struct task_struct *task)``
652*4882a593Smuzhiyun
653*4882a593SmuzhiyunCalled when the task_struct is freed.
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun``void bind(struct cgroup *root)``
656*4882a593Smuzhiyun(cgroup_mutex held by caller)
657*4882a593Smuzhiyun
658*4882a593SmuzhiyunCalled when a cgroup subsystem is rebound to a different hierarchy
659*4882a593Smuzhiyunand root cgroup. Currently this will only involve movement between
660*4882a593Smuzhiyunthe default hierarchy (which never has sub-cgroups) and a hierarchy
661*4882a593Smuzhiyunthat is being created/destroyed (and hence has no sub-cgroups).
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun4. Extended attribute usage
664*4882a593Smuzhiyun===========================
665*4882a593Smuzhiyun
666*4882a593Smuzhiyuncgroup filesystem supports certain types of extended attributes in its
667*4882a593Smuzhiyundirectories and files.  The current supported types are:
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun	- Trusted (XATTR_TRUSTED)
670*4882a593Smuzhiyun	- Security (XATTR_SECURITY)
671*4882a593Smuzhiyun
672*4882a593SmuzhiyunBoth require CAP_SYS_ADMIN capability to set.
673*4882a593Smuzhiyun
674*4882a593SmuzhiyunLike in tmpfs, the extended attributes in cgroup filesystem are stored
675*4882a593Smuzhiyunusing kernel memory and it's advised to keep the usage at minimum.  This
676*4882a593Smuzhiyunis the reason why user defined extended attributes are not supported, since
677*4882a593Smuzhiyunany user can do it and there's no limit in the value size.
678*4882a593Smuzhiyun
679*4882a593SmuzhiyunThe current known users for this feature are SELinux to limit cgroup usage
680*4882a593Smuzhiyunin containers and systemd for assorted meta data like main PID in a cgroup
681*4882a593Smuzhiyun(systemd creates a cgroup per service).
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun5. Questions
684*4882a593Smuzhiyun============
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun::
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun  Q: what's up with this '/bin/echo' ?
689*4882a593Smuzhiyun  A: bash's builtin 'echo' command does not check calls to write() against
690*4882a593Smuzhiyun     errors. If you use it in the cgroup file system, you won't be
691*4882a593Smuzhiyun     able to tell whether a command succeeded or failed.
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun  Q: When I attach processes, only the first of the line gets really attached !
694*4882a593Smuzhiyun  A: We can only return one error code per call to write(). So you should also
695*4882a593Smuzhiyun     put only ONE PID.
696