cpuset.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  1. /*
  2. * kernel/cpuset.c
  3. *
  4. * Processor and Memory placement constraints for sets of tasks.
  5. *
  6. * Copyright (C) 2003 BULL SA.
  7. * Copyright (C) 2004-2007 Silicon Graphics, Inc.
  8. * Copyright (C) 2006 Google, Inc
  9. *
  10. * Portions derived from Patrick Mochel's sysfs code.
  11. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  12. *
  13. * 2003-10-10 Written by Simon Derr.
  14. * 2003-10-22 Updates by Stephen Hemminger.
  15. * 2004 May-July Rework by Paul Jackson.
  16. * 2006 Rework by Paul Menage to use generic cgroups
  17. * 2008 Rework of the scheduler domains and CPU hotplug handling
  18. * by Max Krasnyansky
  19. *
  20. * This file is subject to the terms and conditions of the GNU General Public
  21. * License. See the file COPYING in the main directory of the Linux
  22. * distribution for more details.
  23. */
  24. #include <linux/cpu.h>
  25. #include <linux/cpumask.h>
  26. #include <linux/cpuset.h>
  27. #include <linux/err.h>
  28. #include <linux/errno.h>
  29. #include <linux/file.h>
  30. #include <linux/fs.h>
  31. #include <linux/init.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kernel.h>
  34. #include <linux/kmod.h>
  35. #include <linux/list.h>
  36. #include <linux/mempolicy.h>
  37. #include <linux/mm.h>
  38. #include <linux/module.h>
  39. #include <linux/mount.h>
  40. #include <linux/namei.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/rcupdate.h>
  44. #include <linux/sched.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/security.h>
  47. #include <linux/slab.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/stat.h>
  50. #include <linux/string.h>
  51. #include <linux/time.h>
  52. #include <linux/backing-dev.h>
  53. #include <linux/sort.h>
  54. #include <asm/uaccess.h>
  55. #include <asm/atomic.h>
  56. #include <linux/mutex.h>
  57. #include <linux/workqueue.h>
  58. #include <linux/cgroup.h>
  59. /*
  60. * Tracks how many cpusets are currently defined in system.
  61. * When there is only one cpuset (the root cpuset) we can
  62. * short circuit some hooks.
  63. */
  64. int number_of_cpusets __read_mostly;
  65. /* Forward declare cgroup structures */
  66. struct cgroup_subsys cpuset_subsys;
  67. struct cpuset;
  68. /* See "Frequency meter" comments, below. */
  69. struct fmeter {
  70. int cnt; /* unprocessed events count */
  71. int val; /* most recent output value */
  72. time_t time; /* clock (secs) when val computed */
  73. spinlock_t lock; /* guards read or write of above */
  74. };
  75. struct cpuset {
  76. struct cgroup_subsys_state css;
  77. unsigned long flags; /* "unsigned long" so bitops work */
  78. cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
  79. nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
  80. struct cpuset *parent; /* my parent */
  81. /*
  82. * Copy of global cpuset_mems_generation as of the most
  83. * recent time this cpuset changed its mems_allowed.
  84. */
  85. int mems_generation;
  86. struct fmeter fmeter; /* memory_pressure filter */
  87. /* partition number for rebuild_sched_domains() */
  88. int pn;
  89. /* for custom sched domain */
  90. int relax_domain_level;
  91. /* used for walking a cpuset heirarchy */
  92. struct list_head stack_list;
  93. };
  94. /* Retrieve the cpuset for a cgroup */
  95. static inline struct cpuset *cgroup_cs(struct cgroup *cont)
  96. {
  97. return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
  98. struct cpuset, css);
  99. }
  100. /* Retrieve the cpuset for a task */
  101. static inline struct cpuset *task_cs(struct task_struct *task)
  102. {
  103. return container_of(task_subsys_state(task, cpuset_subsys_id),
  104. struct cpuset, css);
  105. }
  106. struct cpuset_hotplug_scanner {
  107. struct cgroup_scanner scan;
  108. struct cgroup *to;
  109. };
  110. /* bits in struct cpuset flags field */
  111. typedef enum {
  112. CS_CPU_EXCLUSIVE,
  113. CS_MEM_EXCLUSIVE,
  114. CS_MEM_HARDWALL,
  115. CS_MEMORY_MIGRATE,
  116. CS_SCHED_LOAD_BALANCE,
  117. CS_SPREAD_PAGE,
  118. CS_SPREAD_SLAB,
  119. } cpuset_flagbits_t;
  120. /* convenient tests for these bits */
  121. static inline int is_cpu_exclusive(const struct cpuset *cs)
  122. {
  123. return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  124. }
  125. static inline int is_mem_exclusive(const struct cpuset *cs)
  126. {
  127. return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
  128. }
  129. static inline int is_mem_hardwall(const struct cpuset *cs)
  130. {
  131. return test_bit(CS_MEM_HARDWALL, &cs->flags);
  132. }
  133. static inline int is_sched_load_balance(const struct cpuset *cs)
  134. {
  135. return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  136. }
  137. static inline int is_memory_migrate(const struct cpuset *cs)
  138. {
  139. return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
  140. }
  141. static inline int is_spread_page(const struct cpuset *cs)
  142. {
  143. return test_bit(CS_SPREAD_PAGE, &cs->flags);
  144. }
  145. static inline int is_spread_slab(const struct cpuset *cs)
  146. {
  147. return test_bit(CS_SPREAD_SLAB, &cs->flags);
  148. }
  149. /*
  150. * Increment this integer everytime any cpuset changes its
  151. * mems_allowed value. Users of cpusets can track this generation
  152. * number, and avoid having to lock and reload mems_allowed unless
  153. * the cpuset they're using changes generation.
  154. *
  155. * A single, global generation is needed because cpuset_attach_task() could
  156. * reattach a task to a different cpuset, which must not have its
  157. * generation numbers aliased with those of that tasks previous cpuset.
  158. *
  159. * Generations are needed for mems_allowed because one task cannot
  160. * modify another's memory placement. So we must enable every task,
  161. * on every visit to __alloc_pages(), to efficiently check whether
  162. * its current->cpuset->mems_allowed has changed, requiring an update
  163. * of its current->mems_allowed.
  164. *
  165. * Since writes to cpuset_mems_generation are guarded by the cgroup lock
  166. * there is no need to mark it atomic.
  167. */
  168. static int cpuset_mems_generation;
  169. static struct cpuset top_cpuset = {
  170. .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
  171. .cpus_allowed = CPU_MASK_ALL,
  172. .mems_allowed = NODE_MASK_ALL,
  173. };
  174. /*
  175. * There are two global mutexes guarding cpuset structures. The first
  176. * is the main control groups cgroup_mutex, accessed via
  177. * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
  178. * callback_mutex, below. They can nest. It is ok to first take
  179. * cgroup_mutex, then nest callback_mutex. We also require taking
  180. * task_lock() when dereferencing a task's cpuset pointer. See "The
  181. * task_lock() exception", at the end of this comment.
  182. *
  183. * A task must hold both mutexes to modify cpusets. If a task
  184. * holds cgroup_mutex, then it blocks others wanting that mutex,
  185. * ensuring that it is the only task able to also acquire callback_mutex
  186. * and be able to modify cpusets. It can perform various checks on
  187. * the cpuset structure first, knowing nothing will change. It can
  188. * also allocate memory while just holding cgroup_mutex. While it is
  189. * performing these checks, various callback routines can briefly
  190. * acquire callback_mutex to query cpusets. Once it is ready to make
  191. * the changes, it takes callback_mutex, blocking everyone else.
  192. *
  193. * Calls to the kernel memory allocator can not be made while holding
  194. * callback_mutex, as that would risk double tripping on callback_mutex
  195. * from one of the callbacks into the cpuset code from within
  196. * __alloc_pages().
  197. *
  198. * If a task is only holding callback_mutex, then it has read-only
  199. * access to cpusets.
  200. *
  201. * The task_struct fields mems_allowed and mems_generation may only
  202. * be accessed in the context of that task, so require no locks.
  203. *
  204. * The cpuset_common_file_read() handlers only hold callback_mutex across
  205. * small pieces of code, such as when reading out possibly multi-word
  206. * cpumasks and nodemasks.
  207. *
  208. * Accessing a task's cpuset should be done in accordance with the
  209. * guidelines for accessing subsystem state in kernel/cgroup.c
  210. */
  211. static DEFINE_MUTEX(callback_mutex);
  212. /*
  213. * This is ugly, but preserves the userspace API for existing cpuset
  214. * users. If someone tries to mount the "cpuset" filesystem, we
  215. * silently switch it to mount "cgroup" instead
  216. */
  217. static int cpuset_get_sb(struct file_system_type *fs_type,
  218. int flags, const char *unused_dev_name,
  219. void *data, struct vfsmount *mnt)
  220. {
  221. struct file_system_type *cgroup_fs = get_fs_type("cgroup");
  222. int ret = -ENODEV;
  223. if (cgroup_fs) {
  224. char mountopts[] =
  225. "cpuset,noprefix,"
  226. "release_agent=/sbin/cpuset_release_agent";
  227. ret = cgroup_fs->get_sb(cgroup_fs, flags,
  228. unused_dev_name, mountopts, mnt);
  229. put_filesystem(cgroup_fs);
  230. }
  231. return ret;
  232. }
  233. static struct file_system_type cpuset_fs_type = {
  234. .name = "cpuset",
  235. .get_sb = cpuset_get_sb,
  236. };
  237. /*
  238. * Return in *pmask the portion of a cpusets's cpus_allowed that
  239. * are online. If none are online, walk up the cpuset hierarchy
  240. * until we find one that does have some online cpus. If we get
  241. * all the way to the top and still haven't found any online cpus,
  242. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  243. * task, return cpu_online_map.
  244. *
  245. * One way or another, we guarantee to return some non-empty subset
  246. * of cpu_online_map.
  247. *
  248. * Call with callback_mutex held.
  249. */
  250. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  251. {
  252. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  253. cs = cs->parent;
  254. if (cs)
  255. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  256. else
  257. *pmask = cpu_online_map;
  258. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  259. }
  260. /*
  261. * Return in *pmask the portion of a cpusets's mems_allowed that
  262. * are online, with memory. If none are online with memory, walk
  263. * up the cpuset hierarchy until we find one that does have some
  264. * online mems. If we get all the way to the top and still haven't
  265. * found any online mems, return node_states[N_HIGH_MEMORY].
  266. *
  267. * One way or another, we guarantee to return some non-empty subset
  268. * of node_states[N_HIGH_MEMORY].
  269. *
  270. * Call with callback_mutex held.
  271. */
  272. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  273. {
  274. while (cs && !nodes_intersects(cs->mems_allowed,
  275. node_states[N_HIGH_MEMORY]))
  276. cs = cs->parent;
  277. if (cs)
  278. nodes_and(*pmask, cs->mems_allowed,
  279. node_states[N_HIGH_MEMORY]);
  280. else
  281. *pmask = node_states[N_HIGH_MEMORY];
  282. BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
  283. }
  284. /**
  285. * cpuset_update_task_memory_state - update task memory placement
  286. *
  287. * If the current tasks cpusets mems_allowed changed behind our
  288. * backs, update current->mems_allowed, mems_generation and task NUMA
  289. * mempolicy to the new value.
  290. *
  291. * Task mempolicy is updated by rebinding it relative to the
  292. * current->cpuset if a task has its memory placement changed.
  293. * Do not call this routine if in_interrupt().
  294. *
  295. * Call without callback_mutex or task_lock() held. May be
  296. * called with or without cgroup_mutex held. Thanks in part to
  297. * 'the_top_cpuset_hack', the task's cpuset pointer will never
  298. * be NULL. This routine also might acquire callback_mutex during
  299. * call.
  300. *
  301. * Reading current->cpuset->mems_generation doesn't need task_lock
  302. * to guard the current->cpuset derefence, because it is guarded
  303. * from concurrent freeing of current->cpuset using RCU.
  304. *
  305. * The rcu_dereference() is technically probably not needed,
  306. * as I don't actually mind if I see a new cpuset pointer but
  307. * an old value of mems_generation. However this really only
  308. * matters on alpha systems using cpusets heavily. If I dropped
  309. * that rcu_dereference(), it would save them a memory barrier.
  310. * For all other arch's, rcu_dereference is a no-op anyway, and for
  311. * alpha systems not using cpusets, another planned optimization,
  312. * avoiding the rcu critical section for tasks in the root cpuset
  313. * which is statically allocated, so can't vanish, will make this
  314. * irrelevant. Better to use RCU as intended, than to engage in
  315. * some cute trick to save a memory barrier that is impossible to
  316. * test, for alpha systems using cpusets heavily, which might not
  317. * even exist.
  318. *
  319. * This routine is needed to update the per-task mems_allowed data,
  320. * within the tasks context, when it is trying to allocate memory
  321. * (in various mm/mempolicy.c routines) and notices that some other
  322. * task has been modifying its cpuset.
  323. */
  324. void cpuset_update_task_memory_state(void)
  325. {
  326. int my_cpusets_mem_gen;
  327. struct task_struct *tsk = current;
  328. struct cpuset *cs;
  329. if (task_cs(tsk) == &top_cpuset) {
  330. /* Don't need rcu for top_cpuset. It's never freed. */
  331. my_cpusets_mem_gen = top_cpuset.mems_generation;
  332. } else {
  333. rcu_read_lock();
  334. my_cpusets_mem_gen = task_cs(tsk)->mems_generation;
  335. rcu_read_unlock();
  336. }
  337. if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
  338. mutex_lock(&callback_mutex);
  339. task_lock(tsk);
  340. cs = task_cs(tsk); /* Maybe changed when task not locked */
  341. guarantee_online_mems(cs, &tsk->mems_allowed);
  342. tsk->cpuset_mems_generation = cs->mems_generation;
  343. if (is_spread_page(cs))
  344. tsk->flags |= PF_SPREAD_PAGE;
  345. else
  346. tsk->flags &= ~PF_SPREAD_PAGE;
  347. if (is_spread_slab(cs))
  348. tsk->flags |= PF_SPREAD_SLAB;
  349. else
  350. tsk->flags &= ~PF_SPREAD_SLAB;
  351. task_unlock(tsk);
  352. mutex_unlock(&callback_mutex);
  353. mpol_rebind_task(tsk, &tsk->mems_allowed);
  354. }
  355. }
  356. /*
  357. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  358. *
  359. * One cpuset is a subset of another if all its allowed CPUs and
  360. * Memory Nodes are a subset of the other, and its exclusive flags
  361. * are only set if the other's are set. Call holding cgroup_mutex.
  362. */
  363. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  364. {
  365. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  366. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  367. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  368. is_mem_exclusive(p) <= is_mem_exclusive(q);
  369. }
  370. /*
  371. * validate_change() - Used to validate that any proposed cpuset change
  372. * follows the structural rules for cpusets.
  373. *
  374. * If we replaced the flag and mask values of the current cpuset
  375. * (cur) with those values in the trial cpuset (trial), would
  376. * our various subset and exclusive rules still be valid? Presumes
  377. * cgroup_mutex held.
  378. *
  379. * 'cur' is the address of an actual, in-use cpuset. Operations
  380. * such as list traversal that depend on the actual address of the
  381. * cpuset in the list must use cur below, not trial.
  382. *
  383. * 'trial' is the address of bulk structure copy of cur, with
  384. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  385. * or flags changed to new, trial values.
  386. *
  387. * Return 0 if valid, -errno if not.
  388. */
  389. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  390. {
  391. struct cgroup *cont;
  392. struct cpuset *c, *par;
  393. /* Each of our child cpusets must be a subset of us */
  394. list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
  395. if (!is_cpuset_subset(cgroup_cs(cont), trial))
  396. return -EBUSY;
  397. }
  398. /* Remaining checks don't apply to root cpuset */
  399. if (cur == &top_cpuset)
  400. return 0;
  401. par = cur->parent;
  402. /* We must be a subset of our parent cpuset */
  403. if (!is_cpuset_subset(trial, par))
  404. return -EACCES;
  405. /*
  406. * If either I or some sibling (!= me) is exclusive, we can't
  407. * overlap
  408. */
  409. list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
  410. c = cgroup_cs(cont);
  411. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  412. c != cur &&
  413. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  414. return -EINVAL;
  415. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  416. c != cur &&
  417. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  418. return -EINVAL;
  419. }
  420. /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
  421. if (cgroup_task_count(cur->css.cgroup)) {
  422. if (cpus_empty(trial->cpus_allowed) ||
  423. nodes_empty(trial->mems_allowed)) {
  424. return -ENOSPC;
  425. }
  426. }
  427. return 0;
  428. }
  429. /*
  430. * Helper routine for generate_sched_domains().
  431. * Do cpusets a, b have overlapping cpus_allowed masks?
  432. */
  433. static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
  434. {
  435. return cpus_intersects(a->cpus_allowed, b->cpus_allowed);
  436. }
  437. static void
  438. update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
  439. {
  440. if (dattr->relax_domain_level < c->relax_domain_level)
  441. dattr->relax_domain_level = c->relax_domain_level;
  442. return;
  443. }
  444. static void
  445. update_domain_attr_tree(struct sched_domain_attr *dattr, struct cpuset *c)
  446. {
  447. LIST_HEAD(q);
  448. list_add(&c->stack_list, &q);
  449. while (!list_empty(&q)) {
  450. struct cpuset *cp;
  451. struct cgroup *cont;
  452. struct cpuset *child;
  453. cp = list_first_entry(&q, struct cpuset, stack_list);
  454. list_del(q.next);
  455. if (cpus_empty(cp->cpus_allowed))
  456. continue;
  457. if (is_sched_load_balance(cp))
  458. update_domain_attr(dattr, cp);
  459. list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
  460. child = cgroup_cs(cont);
  461. list_add_tail(&child->stack_list, &q);
  462. }
  463. }
  464. }
  465. /*
  466. * generate_sched_domains()
  467. *
  468. * This function builds a partial partition of the systems CPUs
  469. * A 'partial partition' is a set of non-overlapping subsets whose
  470. * union is a subset of that set.
  471. * The output of this function needs to be passed to kernel/sched.c
  472. * partition_sched_domains() routine, which will rebuild the scheduler's
  473. * load balancing domains (sched domains) as specified by that partial
  474. * partition.
  475. *
  476. * See "What is sched_load_balance" in Documentation/cpusets.txt
  477. * for a background explanation of this.
  478. *
  479. * Does not return errors, on the theory that the callers of this
  480. * routine would rather not worry about failures to rebuild sched
  481. * domains when operating in the severe memory shortage situations
  482. * that could cause allocation failures below.
  483. *
  484. * Must be called with cgroup_lock held.
  485. *
  486. * The three key local variables below are:
  487. * q - a linked-list queue of cpuset pointers, used to implement a
  488. * top-down scan of all cpusets. This scan loads a pointer
  489. * to each cpuset marked is_sched_load_balance into the
  490. * array 'csa'. For our purposes, rebuilding the schedulers
  491. * sched domains, we can ignore !is_sched_load_balance cpusets.
  492. * csa - (for CpuSet Array) Array of pointers to all the cpusets
  493. * that need to be load balanced, for convenient iterative
  494. * access by the subsequent code that finds the best partition,
  495. * i.e the set of domains (subsets) of CPUs such that the
  496. * cpus_allowed of every cpuset marked is_sched_load_balance
  497. * is a subset of one of these domains, while there are as
  498. * many such domains as possible, each as small as possible.
  499. * doms - Conversion of 'csa' to an array of cpumasks, for passing to
  500. * the kernel/sched.c routine partition_sched_domains() in a
  501. * convenient format, that can be easily compared to the prior
  502. * value to determine what partition elements (sched domains)
  503. * were changed (added or removed.)
  504. *
  505. * Finding the best partition (set of domains):
  506. * The triple nested loops below over i, j, k scan over the
  507. * load balanced cpusets (using the array of cpuset pointers in
  508. * csa[]) looking for pairs of cpusets that have overlapping
  509. * cpus_allowed, but which don't have the same 'pn' partition
  510. * number and gives them in the same partition number. It keeps
  511. * looping on the 'restart' label until it can no longer find
  512. * any such pairs.
  513. *
  514. * The union of the cpus_allowed masks from the set of
  515. * all cpusets having the same 'pn' value then form the one
  516. * element of the partition (one sched domain) to be passed to
  517. * partition_sched_domains().
  518. */
  519. static int generate_sched_domains(cpumask_t **domains,
  520. struct sched_domain_attr **attributes)
  521. {
  522. LIST_HEAD(q); /* queue of cpusets to be scanned */
  523. struct cpuset *cp; /* scans q */
  524. struct cpuset **csa; /* array of all cpuset ptrs */
  525. int csn; /* how many cpuset ptrs in csa so far */
  526. int i, j, k; /* indices for partition finding loops */
  527. cpumask_t *doms; /* resulting partition; i.e. sched domains */
  528. struct sched_domain_attr *dattr; /* attributes for custom domains */
  529. int ndoms; /* number of sched domains in result */
  530. int nslot; /* next empty doms[] cpumask_t slot */
  531. doms = NULL;
  532. dattr = NULL;
  533. csa = NULL;
  534. /* Special case for the 99% of systems with one, full, sched domain */
  535. if (is_sched_load_balance(&top_cpuset)) {
  536. doms = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
  537. if (!doms)
  538. goto done;
  539. dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
  540. if (dattr) {
  541. *dattr = SD_ATTR_INIT;
  542. update_domain_attr_tree(dattr, &top_cpuset);
  543. }
  544. *doms = top_cpuset.cpus_allowed;
  545. ndoms = 1;
  546. goto done;
  547. }
  548. csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
  549. if (!csa)
  550. goto done;
  551. csn = 0;
  552. list_add(&top_cpuset.stack_list, &q);
  553. while (!list_empty(&q)) {
  554. struct cgroup *cont;
  555. struct cpuset *child; /* scans child cpusets of cp */
  556. cp = list_first_entry(&q, struct cpuset, stack_list);
  557. list_del(q.next);
  558. if (cpus_empty(cp->cpus_allowed))
  559. continue;
  560. /*
  561. * All child cpusets contain a subset of the parent's cpus, so
  562. * just skip them, and then we call update_domain_attr_tree()
  563. * to calc relax_domain_level of the corresponding sched
  564. * domain.
  565. */
  566. if (is_sched_load_balance(cp)) {
  567. csa[csn++] = cp;
  568. continue;
  569. }
  570. list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
  571. child = cgroup_cs(cont);
  572. list_add_tail(&child->stack_list, &q);
  573. }
  574. }
  575. for (i = 0; i < csn; i++)
  576. csa[i]->pn = i;
  577. ndoms = csn;
  578. restart:
  579. /* Find the best partition (set of sched domains) */
  580. for (i = 0; i < csn; i++) {
  581. struct cpuset *a = csa[i];
  582. int apn = a->pn;
  583. for (j = 0; j < csn; j++) {
  584. struct cpuset *b = csa[j];
  585. int bpn = b->pn;
  586. if (apn != bpn && cpusets_overlap(a, b)) {
  587. for (k = 0; k < csn; k++) {
  588. struct cpuset *c = csa[k];
  589. if (c->pn == bpn)
  590. c->pn = apn;
  591. }
  592. ndoms--; /* one less element */
  593. goto restart;
  594. }
  595. }
  596. }
  597. /*
  598. * Now we know how many domains to create.
  599. * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
  600. */
  601. doms = kmalloc(ndoms * sizeof(cpumask_t), GFP_KERNEL);
  602. if (!doms)
  603. goto done;
  604. /*
  605. * The rest of the code, including the scheduler, can deal with
  606. * dattr==NULL case. No need to abort if alloc fails.
  607. */
  608. dattr = kmalloc(ndoms * sizeof(struct sched_domain_attr), GFP_KERNEL);
  609. for (nslot = 0, i = 0; i < csn; i++) {
  610. struct cpuset *a = csa[i];
  611. cpumask_t *dp;
  612. int apn = a->pn;
  613. if (apn < 0) {
  614. /* Skip completed partitions */
  615. continue;
  616. }
  617. dp = doms + nslot;
  618. if (nslot == ndoms) {
  619. static int warnings = 10;
  620. if (warnings) {
  621. printk(KERN_WARNING
  622. "rebuild_sched_domains confused:"
  623. " nslot %d, ndoms %d, csn %d, i %d,"
  624. " apn %d\n",
  625. nslot, ndoms, csn, i, apn);
  626. warnings--;
  627. }
  628. continue;
  629. }
  630. cpus_clear(*dp);
  631. if (dattr)
  632. *(dattr + nslot) = SD_ATTR_INIT;
  633. for (j = i; j < csn; j++) {
  634. struct cpuset *b = csa[j];
  635. if (apn == b->pn) {
  636. cpus_or(*dp, *dp, b->cpus_allowed);
  637. if (dattr)
  638. update_domain_attr_tree(dattr + nslot, b);
  639. /* Done with this partition */
  640. b->pn = -1;
  641. }
  642. }
  643. nslot++;
  644. }
  645. BUG_ON(nslot != ndoms);
  646. done:
  647. kfree(csa);
  648. /*
  649. * Fallback to the default domain if kmalloc() failed.
  650. * See comments in partition_sched_domains().
  651. */
  652. if (doms == NULL)
  653. ndoms = 1;
  654. *domains = doms;
  655. *attributes = dattr;
  656. return ndoms;
  657. }
  658. /*
  659. * Rebuild scheduler domains.
  660. *
  661. * Call with neither cgroup_mutex held nor within get_online_cpus().
  662. * Takes both cgroup_mutex and get_online_cpus().
  663. *
  664. * Cannot be directly called from cpuset code handling changes
  665. * to the cpuset pseudo-filesystem, because it cannot be called
  666. * from code that already holds cgroup_mutex.
  667. */
  668. static void do_rebuild_sched_domains(struct work_struct *unused)
  669. {
  670. struct sched_domain_attr *attr;
  671. cpumask_t *doms;
  672. int ndoms;
  673. get_online_cpus();
  674. /* Generate domain masks and attrs */
  675. cgroup_lock();
  676. ndoms = generate_sched_domains(&doms, &attr);
  677. cgroup_unlock();
  678. /* Have scheduler rebuild the domains */
  679. partition_sched_domains(ndoms, doms, attr);
  680. put_online_cpus();
  681. }
  682. static DECLARE_WORK(rebuild_sched_domains_work, do_rebuild_sched_domains);
  683. /*
  684. * Rebuild scheduler domains, asynchronously via workqueue.
  685. *
  686. * If the flag 'sched_load_balance' of any cpuset with non-empty
  687. * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
  688. * which has that flag enabled, or if any cpuset with a non-empty
  689. * 'cpus' is removed, then call this routine to rebuild the
  690. * scheduler's dynamic sched domains.
  691. *
  692. * The rebuild_sched_domains() and partition_sched_domains()
  693. * routines must nest cgroup_lock() inside get_online_cpus(),
  694. * but such cpuset changes as these must nest that locking the
  695. * other way, holding cgroup_lock() for much of the code.
  696. *
  697. * So in order to avoid an ABBA deadlock, the cpuset code handling
  698. * these user changes delegates the actual sched domain rebuilding
  699. * to a separate workqueue thread, which ends up processing the
  700. * above do_rebuild_sched_domains() function.
  701. */
  702. static void async_rebuild_sched_domains(void)
  703. {
  704. schedule_work(&rebuild_sched_domains_work);
  705. }
  706. /*
  707. * Accomplishes the same scheduler domain rebuild as the above
  708. * async_rebuild_sched_domains(), however it directly calls the
  709. * rebuild routine synchronously rather than calling it via an
  710. * asynchronous work thread.
  711. *
  712. * This can only be called from code that is not holding
  713. * cgroup_mutex (not nested in a cgroup_lock() call.)
  714. */
  715. void rebuild_sched_domains(void)
  716. {
  717. do_rebuild_sched_domains(NULL);
  718. }
  719. /**
  720. * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
  721. * @tsk: task to test
  722. * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
  723. *
  724. * Call with cgroup_mutex held. May take callback_mutex during call.
  725. * Called for each task in a cgroup by cgroup_scan_tasks().
  726. * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
  727. * words, if its mask is not equal to its cpuset's mask).
  728. */
  729. static int cpuset_test_cpumask(struct task_struct *tsk,
  730. struct cgroup_scanner *scan)
  731. {
  732. return !cpus_equal(tsk->cpus_allowed,
  733. (cgroup_cs(scan->cg))->cpus_allowed);
  734. }
  735. /**
  736. * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
  737. * @tsk: task to test
  738. * @scan: struct cgroup_scanner containing the cgroup of the task
  739. *
  740. * Called by cgroup_scan_tasks() for each task in a cgroup whose
  741. * cpus_allowed mask needs to be changed.
  742. *
  743. * We don't need to re-check for the cgroup/cpuset membership, since we're
  744. * holding cgroup_lock() at this point.
  745. */
  746. static void cpuset_change_cpumask(struct task_struct *tsk,
  747. struct cgroup_scanner *scan)
  748. {
  749. set_cpus_allowed_ptr(tsk, &((cgroup_cs(scan->cg))->cpus_allowed));
  750. }
  751. /**
  752. * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
  753. * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
  754. * @heap: if NULL, defer allocating heap memory to cgroup_scan_tasks()
  755. *
  756. * Called with cgroup_mutex held
  757. *
  758. * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
  759. * calling callback functions for each.
  760. *
  761. * No return value. It's guaranteed that cgroup_scan_tasks() always returns 0
  762. * if @heap != NULL.
  763. */
  764. static void update_tasks_cpumask(struct cpuset *cs, struct ptr_heap *heap)
  765. {
  766. struct cgroup_scanner scan;
  767. scan.cg = cs->css.cgroup;
  768. scan.test_task = cpuset_test_cpumask;
  769. scan.process_task = cpuset_change_cpumask;
  770. scan.heap = heap;
  771. cgroup_scan_tasks(&scan);
  772. }
  773. /**
  774. * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
  775. * @cs: the cpuset to consider
  776. * @buf: buffer of cpu numbers written to this cpuset
  777. */
  778. static int update_cpumask(struct cpuset *cs, const char *buf)
  779. {
  780. struct ptr_heap heap;
  781. struct cpuset trialcs;
  782. int retval;
  783. int is_load_balanced;
  784. /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
  785. if (cs == &top_cpuset)
  786. return -EACCES;
  787. trialcs = *cs;
  788. /*
  789. * An empty cpus_allowed is ok only if the cpuset has no tasks.
  790. * Since cpulist_parse() fails on an empty mask, we special case
  791. * that parsing. The validate_change() call ensures that cpusets
  792. * with tasks have cpus.
  793. */
  794. if (!*buf) {
  795. cpus_clear(trialcs.cpus_allowed);
  796. } else {
  797. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  798. if (retval < 0)
  799. return retval;
  800. if (!cpus_subset(trialcs.cpus_allowed, cpu_online_map))
  801. return -EINVAL;
  802. }
  803. retval = validate_change(cs, &trialcs);
  804. if (retval < 0)
  805. return retval;
  806. /* Nothing to do if the cpus didn't change */
  807. if (cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed))
  808. return 0;
  809. retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, NULL);
  810. if (retval)
  811. return retval;
  812. is_load_balanced = is_sched_load_balance(&trialcs);
  813. mutex_lock(&callback_mutex);
  814. cs->cpus_allowed = trialcs.cpus_allowed;
  815. mutex_unlock(&callback_mutex);
  816. /*
  817. * Scan tasks in the cpuset, and update the cpumasks of any
  818. * that need an update.
  819. */
  820. update_tasks_cpumask(cs, &heap);
  821. heap_free(&heap);
  822. if (is_load_balanced)
  823. async_rebuild_sched_domains();
  824. return 0;
  825. }
  826. /*
  827. * cpuset_migrate_mm
  828. *
  829. * Migrate memory region from one set of nodes to another.
  830. *
  831. * Temporarilly set tasks mems_allowed to target nodes of migration,
  832. * so that the migration code can allocate pages on these nodes.
  833. *
  834. * Call holding cgroup_mutex, so current's cpuset won't change
  835. * during this call, as manage_mutex holds off any cpuset_attach()
  836. * calls. Therefore we don't need to take task_lock around the
  837. * call to guarantee_online_mems(), as we know no one is changing
  838. * our task's cpuset.
  839. *
  840. * Hold callback_mutex around the two modifications of our tasks
  841. * mems_allowed to synchronize with cpuset_mems_allowed().
  842. *
  843. * While the mm_struct we are migrating is typically from some
  844. * other task, the task_struct mems_allowed that we are hacking
  845. * is for our current task, which must allocate new pages for that
  846. * migrating memory region.
  847. *
  848. * We call cpuset_update_task_memory_state() before hacking
  849. * our tasks mems_allowed, so that we are assured of being in
  850. * sync with our tasks cpuset, and in particular, callbacks to
  851. * cpuset_update_task_memory_state() from nested page allocations
  852. * won't see any mismatch of our cpuset and task mems_generation
  853. * values, so won't overwrite our hacked tasks mems_allowed
  854. * nodemask.
  855. */
  856. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  857. const nodemask_t *to)
  858. {
  859. struct task_struct *tsk = current;
  860. cpuset_update_task_memory_state();
  861. mutex_lock(&callback_mutex);
  862. tsk->mems_allowed = *to;
  863. mutex_unlock(&callback_mutex);
  864. do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
  865. mutex_lock(&callback_mutex);
  866. guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
  867. mutex_unlock(&callback_mutex);
  868. }
  869. static void *cpuset_being_rebound;
  870. /**
  871. * update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
  872. * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
  873. * @oldmem: old mems_allowed of cpuset cs
  874. *
  875. * Called with cgroup_mutex held
  876. * Return 0 if successful, -errno if not.
  877. */
  878. static int update_tasks_nodemask(struct cpuset *cs, const nodemask_t *oldmem)
  879. {
  880. struct task_struct *p;
  881. struct mm_struct **mmarray;
  882. int i, n, ntasks;
  883. int migrate;
  884. int fudge;
  885. struct cgroup_iter it;
  886. int retval;
  887. cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
  888. fudge = 10; /* spare mmarray[] slots */
  889. fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
  890. retval = -ENOMEM;
  891. /*
  892. * Allocate mmarray[] to hold mm reference for each task
  893. * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
  894. * tasklist_lock. We could use GFP_ATOMIC, but with a
  895. * few more lines of code, we can retry until we get a big
  896. * enough mmarray[] w/o using GFP_ATOMIC.
  897. */
  898. while (1) {
  899. ntasks = cgroup_task_count(cs->css.cgroup); /* guess */
  900. ntasks += fudge;
  901. mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
  902. if (!mmarray)
  903. goto done;
  904. read_lock(&tasklist_lock); /* block fork */
  905. if (cgroup_task_count(cs->css.cgroup) <= ntasks)
  906. break; /* got enough */
  907. read_unlock(&tasklist_lock); /* try again */
  908. kfree(mmarray);
  909. }
  910. n = 0;
  911. /* Load up mmarray[] with mm reference for each task in cpuset. */
  912. cgroup_iter_start(cs->css.cgroup, &it);
  913. while ((p = cgroup_iter_next(cs->css.cgroup, &it))) {
  914. struct mm_struct *mm;
  915. if (n >= ntasks) {
  916. printk(KERN_WARNING
  917. "Cpuset mempolicy rebind incomplete.\n");
  918. break;
  919. }
  920. mm = get_task_mm(p);
  921. if (!mm)
  922. continue;
  923. mmarray[n++] = mm;
  924. }
  925. cgroup_iter_end(cs->css.cgroup, &it);
  926. read_unlock(&tasklist_lock);
  927. /*
  928. * Now that we've dropped the tasklist spinlock, we can
  929. * rebind the vma mempolicies of each mm in mmarray[] to their
  930. * new cpuset, and release that mm. The mpol_rebind_mm()
  931. * call takes mmap_sem, which we couldn't take while holding
  932. * tasklist_lock. Forks can happen again now - the mpol_dup()
  933. * cpuset_being_rebound check will catch such forks, and rebind
  934. * their vma mempolicies too. Because we still hold the global
  935. * cgroup_mutex, we know that no other rebind effort will
  936. * be contending for the global variable cpuset_being_rebound.
  937. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  938. * is idempotent. Also migrate pages in each mm to new nodes.
  939. */
  940. migrate = is_memory_migrate(cs);
  941. for (i = 0; i < n; i++) {
  942. struct mm_struct *mm = mmarray[i];
  943. mpol_rebind_mm(mm, &cs->mems_allowed);
  944. if (migrate)
  945. cpuset_migrate_mm(mm, oldmem, &cs->mems_allowed);
  946. mmput(mm);
  947. }
  948. /* We're done rebinding vmas to this cpuset's new mems_allowed. */
  949. kfree(mmarray);
  950. cpuset_being_rebound = NULL;
  951. retval = 0;
  952. done:
  953. return retval;
  954. }
  955. /*
  956. * Handle user request to change the 'mems' memory placement
  957. * of a cpuset. Needs to validate the request, update the
  958. * cpusets mems_allowed and mems_generation, and for each
  959. * task in the cpuset, rebind any vma mempolicies and if
  960. * the cpuset is marked 'memory_migrate', migrate the tasks
  961. * pages to the new memory.
  962. *
  963. * Call with cgroup_mutex held. May take callback_mutex during call.
  964. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  965. * lock each such tasks mm->mmap_sem, scan its vma's and rebind
  966. * their mempolicies to the cpusets new mems_allowed.
  967. */
  968. static int update_nodemask(struct cpuset *cs, const char *buf)
  969. {
  970. struct cpuset trialcs;
  971. nodemask_t oldmem;
  972. int retval;
  973. /*
  974. * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
  975. * it's read-only
  976. */
  977. if (cs == &top_cpuset)
  978. return -EACCES;
  979. trialcs = *cs;
  980. /*
  981. * An empty mems_allowed is ok iff there are no tasks in the cpuset.
  982. * Since nodelist_parse() fails on an empty mask, we special case
  983. * that parsing. The validate_change() call ensures that cpusets
  984. * with tasks have memory.
  985. */
  986. if (!*buf) {
  987. nodes_clear(trialcs.mems_allowed);
  988. } else {
  989. retval = nodelist_parse(buf, trialcs.mems_allowed);
  990. if (retval < 0)
  991. goto done;
  992. if (!nodes_subset(trialcs.mems_allowed,
  993. node_states[N_HIGH_MEMORY]))
  994. return -EINVAL;
  995. }
  996. oldmem = cs->mems_allowed;
  997. if (nodes_equal(oldmem, trialcs.mems_allowed)) {
  998. retval = 0; /* Too easy - nothing to do */
  999. goto done;
  1000. }
  1001. retval = validate_change(cs, &trialcs);
  1002. if (retval < 0)
  1003. goto done;
  1004. mutex_lock(&callback_mutex);
  1005. cs->mems_allowed = trialcs.mems_allowed;
  1006. cs->mems_generation = cpuset_mems_generation++;
  1007. mutex_unlock(&callback_mutex);
  1008. retval = update_tasks_nodemask(cs, &oldmem);
  1009. done:
  1010. return retval;
  1011. }
  1012. int current_cpuset_is_being_rebound(void)
  1013. {
  1014. return task_cs(current) == cpuset_being_rebound;
  1015. }
  1016. static int update_relax_domain_level(struct cpuset *cs, s64 val)
  1017. {
  1018. if (val < -1 || val >= SD_LV_MAX)
  1019. return -EINVAL;
  1020. if (val != cs->relax_domain_level) {
  1021. cs->relax_domain_level = val;
  1022. if (!cpus_empty(cs->cpus_allowed) && is_sched_load_balance(cs))
  1023. async_rebuild_sched_domains();
  1024. }
  1025. return 0;
  1026. }
  1027. /*
  1028. * update_flag - read a 0 or a 1 in a file and update associated flag
  1029. * bit: the bit to update (see cpuset_flagbits_t)
  1030. * cs: the cpuset to update
  1031. * turning_on: whether the flag is being set or cleared
  1032. *
  1033. * Call with cgroup_mutex held.
  1034. */
  1035. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
  1036. int turning_on)
  1037. {
  1038. struct cpuset trialcs;
  1039. int err;
  1040. int balance_flag_changed;
  1041. trialcs = *cs;
  1042. if (turning_on)
  1043. set_bit(bit, &trialcs.flags);
  1044. else
  1045. clear_bit(bit, &trialcs.flags);
  1046. err = validate_change(cs, &trialcs);
  1047. if (err < 0)
  1048. return err;
  1049. balance_flag_changed = (is_sched_load_balance(cs) !=
  1050. is_sched_load_balance(&trialcs));
  1051. mutex_lock(&callback_mutex);
  1052. cs->flags = trialcs.flags;
  1053. mutex_unlock(&callback_mutex);
  1054. if (!cpus_empty(trialcs.cpus_allowed) && balance_flag_changed)
  1055. async_rebuild_sched_domains();
  1056. return 0;
  1057. }
  1058. /*
  1059. * Frequency meter - How fast is some event occurring?
  1060. *
  1061. * These routines manage a digitally filtered, constant time based,
  1062. * event frequency meter. There are four routines:
  1063. * fmeter_init() - initialize a frequency meter.
  1064. * fmeter_markevent() - called each time the event happens.
  1065. * fmeter_getrate() - returns the recent rate of such events.
  1066. * fmeter_update() - internal routine used to update fmeter.
  1067. *
  1068. * A common data structure is passed to each of these routines,
  1069. * which is used to keep track of the state required to manage the
  1070. * frequency meter and its digital filter.
  1071. *
  1072. * The filter works on the number of events marked per unit time.
  1073. * The filter is single-pole low-pass recursive (IIR). The time unit
  1074. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  1075. * simulate 3 decimal digits of precision (multiplied by 1000).
  1076. *
  1077. * With an FM_COEF of 933, and a time base of 1 second, the filter
  1078. * has a half-life of 10 seconds, meaning that if the events quit
  1079. * happening, then the rate returned from the fmeter_getrate()
  1080. * will be cut in half each 10 seconds, until it converges to zero.
  1081. *
  1082. * It is not worth doing a real infinitely recursive filter. If more
  1083. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  1084. * just compute FM_MAXTICKS ticks worth, by which point the level
  1085. * will be stable.
  1086. *
  1087. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  1088. * arithmetic overflow in the fmeter_update() routine.
  1089. *
  1090. * Given the simple 32 bit integer arithmetic used, this meter works
  1091. * best for reporting rates between one per millisecond (msec) and
  1092. * one per 32 (approx) seconds. At constant rates faster than one
  1093. * per msec it maxes out at values just under 1,000,000. At constant
  1094. * rates between one per msec, and one per second it will stabilize
  1095. * to a value N*1000, where N is the rate of events per second.
  1096. * At constant rates between one per second and one per 32 seconds,
  1097. * it will be choppy, moving up on the seconds that have an event,
  1098. * and then decaying until the next event. At rates slower than
  1099. * about one in 32 seconds, it decays all the way back to zero between
  1100. * each event.
  1101. */
  1102. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  1103. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  1104. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  1105. #define FM_SCALE 1000 /* faux fixed point scale */
  1106. /* Initialize a frequency meter */
  1107. static void fmeter_init(struct fmeter *fmp)
  1108. {
  1109. fmp->cnt = 0;
  1110. fmp->val = 0;
  1111. fmp->time = 0;
  1112. spin_lock_init(&fmp->lock);
  1113. }
  1114. /* Internal meter update - process cnt events and update value */
  1115. static void fmeter_update(struct fmeter *fmp)
  1116. {
  1117. time_t now = get_seconds();
  1118. time_t ticks = now - fmp->time;
  1119. if (ticks == 0)
  1120. return;
  1121. ticks = min(FM_MAXTICKS, ticks);
  1122. while (ticks-- > 0)
  1123. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  1124. fmp->time = now;
  1125. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  1126. fmp->cnt = 0;
  1127. }
  1128. /* Process any previous ticks, then bump cnt by one (times scale). */
  1129. static void fmeter_markevent(struct fmeter *fmp)
  1130. {
  1131. spin_lock(&fmp->lock);
  1132. fmeter_update(fmp);
  1133. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  1134. spin_unlock(&fmp->lock);
  1135. }
  1136. /* Process any previous ticks, then return current value. */
  1137. static int fmeter_getrate(struct fmeter *fmp)
  1138. {
  1139. int val;
  1140. spin_lock(&fmp->lock);
  1141. fmeter_update(fmp);
  1142. val = fmp->val;
  1143. spin_unlock(&fmp->lock);
  1144. return val;
  1145. }
  1146. /* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
  1147. static int cpuset_can_attach(struct cgroup_subsys *ss,
  1148. struct cgroup *cont, struct task_struct *tsk)
  1149. {
  1150. struct cpuset *cs = cgroup_cs(cont);
  1151. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  1152. return -ENOSPC;
  1153. if (tsk->flags & PF_THREAD_BOUND) {
  1154. cpumask_t mask;
  1155. mutex_lock(&callback_mutex);
  1156. mask = cs->cpus_allowed;
  1157. mutex_unlock(&callback_mutex);
  1158. if (!cpus_equal(tsk->cpus_allowed, mask))
  1159. return -EINVAL;
  1160. }
  1161. return security_task_setscheduler(tsk, 0, NULL);
  1162. }
  1163. static void cpuset_attach(struct cgroup_subsys *ss,
  1164. struct cgroup *cont, struct cgroup *oldcont,
  1165. struct task_struct *tsk)
  1166. {
  1167. cpumask_t cpus;
  1168. nodemask_t from, to;
  1169. struct mm_struct *mm;
  1170. struct cpuset *cs = cgroup_cs(cont);
  1171. struct cpuset *oldcs = cgroup_cs(oldcont);
  1172. int err;
  1173. mutex_lock(&callback_mutex);
  1174. guarantee_online_cpus(cs, &cpus);
  1175. err = set_cpus_allowed_ptr(tsk, &cpus);
  1176. mutex_unlock(&callback_mutex);
  1177. if (err)
  1178. return;
  1179. from = oldcs->mems_allowed;
  1180. to = cs->mems_allowed;
  1181. mm = get_task_mm(tsk);
  1182. if (mm) {
  1183. mpol_rebind_mm(mm, &to);
  1184. if (is_memory_migrate(cs))
  1185. cpuset_migrate_mm(mm, &from, &to);
  1186. mmput(mm);
  1187. }
  1188. }
  1189. /* The various types of files and directories in a cpuset file system */
  1190. typedef enum {
  1191. FILE_MEMORY_MIGRATE,
  1192. FILE_CPULIST,
  1193. FILE_MEMLIST,
  1194. FILE_CPU_EXCLUSIVE,
  1195. FILE_MEM_EXCLUSIVE,
  1196. FILE_MEM_HARDWALL,
  1197. FILE_SCHED_LOAD_BALANCE,
  1198. FILE_SCHED_RELAX_DOMAIN_LEVEL,
  1199. FILE_MEMORY_PRESSURE_ENABLED,
  1200. FILE_MEMORY_PRESSURE,
  1201. FILE_SPREAD_PAGE,
  1202. FILE_SPREAD_SLAB,
  1203. } cpuset_filetype_t;
  1204. static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1205. {
  1206. int retval = 0;
  1207. struct cpuset *cs = cgroup_cs(cgrp);
  1208. cpuset_filetype_t type = cft->private;
  1209. if (!cgroup_lock_live_group(cgrp))
  1210. return -ENODEV;
  1211. switch (type) {
  1212. case FILE_CPU_EXCLUSIVE:
  1213. retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
  1214. break;
  1215. case FILE_MEM_EXCLUSIVE:
  1216. retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
  1217. break;
  1218. case FILE_MEM_HARDWALL:
  1219. retval = update_flag(CS_MEM_HARDWALL, cs, val);
  1220. break;
  1221. case FILE_SCHED_LOAD_BALANCE:
  1222. retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
  1223. break;
  1224. case FILE_MEMORY_MIGRATE:
  1225. retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
  1226. break;
  1227. case FILE_MEMORY_PRESSURE_ENABLED:
  1228. cpuset_memory_pressure_enabled = !!val;
  1229. break;
  1230. case FILE_MEMORY_PRESSURE:
  1231. retval = -EACCES;
  1232. break;
  1233. case FILE_SPREAD_PAGE:
  1234. retval = update_flag(CS_SPREAD_PAGE, cs, val);
  1235. cs->mems_generation = cpuset_mems_generation++;
  1236. break;
  1237. case FILE_SPREAD_SLAB:
  1238. retval = update_flag(CS_SPREAD_SLAB, cs, val);
  1239. cs->mems_generation = cpuset_mems_generation++;
  1240. break;
  1241. default:
  1242. retval = -EINVAL;
  1243. break;
  1244. }
  1245. cgroup_unlock();
  1246. return retval;
  1247. }
  1248. static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
  1249. {
  1250. int retval = 0;
  1251. struct cpuset *cs = cgroup_cs(cgrp);
  1252. cpuset_filetype_t type = cft->private;
  1253. if (!cgroup_lock_live_group(cgrp))
  1254. return -ENODEV;
  1255. switch (type) {
  1256. case FILE_SCHED_RELAX_DOMAIN_LEVEL:
  1257. retval = update_relax_domain_level(cs, val);
  1258. break;
  1259. default:
  1260. retval = -EINVAL;
  1261. break;
  1262. }
  1263. cgroup_unlock();
  1264. return retval;
  1265. }
  1266. /*
  1267. * Common handling for a write to a "cpus" or "mems" file.
  1268. */
  1269. static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
  1270. const char *buf)
  1271. {
  1272. int retval = 0;
  1273. if (!cgroup_lock_live_group(cgrp))
  1274. return -ENODEV;
  1275. switch (cft->private) {
  1276. case FILE_CPULIST:
  1277. retval = update_cpumask(cgroup_cs(cgrp), buf);
  1278. break;
  1279. case FILE_MEMLIST:
  1280. retval = update_nodemask(cgroup_cs(cgrp), buf);
  1281. break;
  1282. default:
  1283. retval = -EINVAL;
  1284. break;
  1285. }
  1286. cgroup_unlock();
  1287. return retval;
  1288. }
  1289. /*
  1290. * These ascii lists should be read in a single call, by using a user
  1291. * buffer large enough to hold the entire map. If read in smaller
  1292. * chunks, there is no guarantee of atomicity. Since the display format
  1293. * used, list of ranges of sequential numbers, is variable length,
  1294. * and since these maps can change value dynamically, one could read
  1295. * gibberish by doing partial reads while a list was changing.
  1296. * A single large read to a buffer that crosses a page boundary is
  1297. * ok, because the result being copied to user land is not recomputed
  1298. * across a page fault.
  1299. */
  1300. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1301. {
  1302. cpumask_t mask;
  1303. mutex_lock(&callback_mutex);
  1304. mask = cs->cpus_allowed;
  1305. mutex_unlock(&callback_mutex);
  1306. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  1307. }
  1308. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1309. {
  1310. nodemask_t mask;
  1311. mutex_lock(&callback_mutex);
  1312. mask = cs->mems_allowed;
  1313. mutex_unlock(&callback_mutex);
  1314. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  1315. }
  1316. static ssize_t cpuset_common_file_read(struct cgroup *cont,
  1317. struct cftype *cft,
  1318. struct file *file,
  1319. char __user *buf,
  1320. size_t nbytes, loff_t *ppos)
  1321. {
  1322. struct cpuset *cs = cgroup_cs(cont);
  1323. cpuset_filetype_t type = cft->private;
  1324. char *page;
  1325. ssize_t retval = 0;
  1326. char *s;
  1327. if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
  1328. return -ENOMEM;
  1329. s = page;
  1330. switch (type) {
  1331. case FILE_CPULIST:
  1332. s += cpuset_sprintf_cpulist(s, cs);
  1333. break;
  1334. case FILE_MEMLIST:
  1335. s += cpuset_sprintf_memlist(s, cs);
  1336. break;
  1337. default:
  1338. retval = -EINVAL;
  1339. goto out;
  1340. }
  1341. *s++ = '\n';
  1342. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1343. out:
  1344. free_page((unsigned long)page);
  1345. return retval;
  1346. }
  1347. static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
  1348. {
  1349. struct cpuset *cs = cgroup_cs(cont);
  1350. cpuset_filetype_t type = cft->private;
  1351. switch (type) {
  1352. case FILE_CPU_EXCLUSIVE:
  1353. return is_cpu_exclusive(cs);
  1354. case FILE_MEM_EXCLUSIVE:
  1355. return is_mem_exclusive(cs);
  1356. case FILE_MEM_HARDWALL:
  1357. return is_mem_hardwall(cs);
  1358. case FILE_SCHED_LOAD_BALANCE:
  1359. return is_sched_load_balance(cs);
  1360. case FILE_MEMORY_MIGRATE:
  1361. return is_memory_migrate(cs);
  1362. case FILE_MEMORY_PRESSURE_ENABLED:
  1363. return cpuset_memory_pressure_enabled;
  1364. case FILE_MEMORY_PRESSURE:
  1365. return fmeter_getrate(&cs->fmeter);
  1366. case FILE_SPREAD_PAGE:
  1367. return is_spread_page(cs);
  1368. case FILE_SPREAD_SLAB:
  1369. return is_spread_slab(cs);
  1370. default:
  1371. BUG();
  1372. }
  1373. /* Unreachable but makes gcc happy */
  1374. return 0;
  1375. }
  1376. static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
  1377. {
  1378. struct cpuset *cs = cgroup_cs(cont);
  1379. cpuset_filetype_t type = cft->private;
  1380. switch (type) {
  1381. case FILE_SCHED_RELAX_DOMAIN_LEVEL:
  1382. return cs->relax_domain_level;
  1383. default:
  1384. BUG();
  1385. }
  1386. /* Unrechable but makes gcc happy */
  1387. return 0;
  1388. }
  1389. /*
  1390. * for the common functions, 'private' gives the type of file
  1391. */
  1392. static struct cftype files[] = {
  1393. {
  1394. .name = "cpus",
  1395. .read = cpuset_common_file_read,
  1396. .write_string = cpuset_write_resmask,
  1397. .max_write_len = (100U + 6 * NR_CPUS),
  1398. .private = FILE_CPULIST,
  1399. },
  1400. {
  1401. .name = "mems",
  1402. .read = cpuset_common_file_read,
  1403. .write_string = cpuset_write_resmask,
  1404. .max_write_len = (100U + 6 * MAX_NUMNODES),
  1405. .private = FILE_MEMLIST,
  1406. },
  1407. {
  1408. .name = "cpu_exclusive",
  1409. .read_u64 = cpuset_read_u64,
  1410. .write_u64 = cpuset_write_u64,
  1411. .private = FILE_CPU_EXCLUSIVE,
  1412. },
  1413. {
  1414. .name = "mem_exclusive",
  1415. .read_u64 = cpuset_read_u64,
  1416. .write_u64 = cpuset_write_u64,
  1417. .private = FILE_MEM_EXCLUSIVE,
  1418. },
  1419. {
  1420. .name = "mem_hardwall",
  1421. .read_u64 = cpuset_read_u64,
  1422. .write_u64 = cpuset_write_u64,
  1423. .private = FILE_MEM_HARDWALL,
  1424. },
  1425. {
  1426. .name = "sched_load_balance",
  1427. .read_u64 = cpuset_read_u64,
  1428. .write_u64 = cpuset_write_u64,
  1429. .private = FILE_SCHED_LOAD_BALANCE,
  1430. },
  1431. {
  1432. .name = "sched_relax_domain_level",
  1433. .read_s64 = cpuset_read_s64,
  1434. .write_s64 = cpuset_write_s64,
  1435. .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
  1436. },
  1437. {
  1438. .name = "memory_migrate",
  1439. .read_u64 = cpuset_read_u64,
  1440. .write_u64 = cpuset_write_u64,
  1441. .private = FILE_MEMORY_MIGRATE,
  1442. },
  1443. {
  1444. .name = "memory_pressure",
  1445. .read_u64 = cpuset_read_u64,
  1446. .write_u64 = cpuset_write_u64,
  1447. .private = FILE_MEMORY_PRESSURE,
  1448. },
  1449. {
  1450. .name = "memory_spread_page",
  1451. .read_u64 = cpuset_read_u64,
  1452. .write_u64 = cpuset_write_u64,
  1453. .private = FILE_SPREAD_PAGE,
  1454. },
  1455. {
  1456. .name = "memory_spread_slab",
  1457. .read_u64 = cpuset_read_u64,
  1458. .write_u64 = cpuset_write_u64,
  1459. .private = FILE_SPREAD_SLAB,
  1460. },
  1461. };
  1462. static struct cftype cft_memory_pressure_enabled = {
  1463. .name = "memory_pressure_enabled",
  1464. .read_u64 = cpuset_read_u64,
  1465. .write_u64 = cpuset_write_u64,
  1466. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1467. };
  1468. static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  1469. {
  1470. int err;
  1471. err = cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
  1472. if (err)
  1473. return err;
  1474. /* memory_pressure_enabled is in root cpuset only */
  1475. if (!cont->parent)
  1476. err = cgroup_add_file(cont, ss,
  1477. &cft_memory_pressure_enabled);
  1478. return err;
  1479. }
  1480. /*
  1481. * post_clone() is called at the end of cgroup_clone().
  1482. * 'cgroup' was just created automatically as a result of
  1483. * a cgroup_clone(), and the current task is about to
  1484. * be moved into 'cgroup'.
  1485. *
  1486. * Currently we refuse to set up the cgroup - thereby
  1487. * refusing the task to be entered, and as a result refusing
  1488. * the sys_unshare() or clone() which initiated it - if any
  1489. * sibling cpusets have exclusive cpus or mem.
  1490. *
  1491. * If this becomes a problem for some users who wish to
  1492. * allow that scenario, then cpuset_post_clone() could be
  1493. * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
  1494. * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
  1495. * held.
  1496. */
  1497. static void cpuset_post_clone(struct cgroup_subsys *ss,
  1498. struct cgroup *cgroup)
  1499. {
  1500. struct cgroup *parent, *child;
  1501. struct cpuset *cs, *parent_cs;
  1502. parent = cgroup->parent;
  1503. list_for_each_entry(child, &parent->children, sibling) {
  1504. cs = cgroup_cs(child);
  1505. if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
  1506. return;
  1507. }
  1508. cs = cgroup_cs(cgroup);
  1509. parent_cs = cgroup_cs(parent);
  1510. cs->mems_allowed = parent_cs->mems_allowed;
  1511. cs->cpus_allowed = parent_cs->cpus_allowed;
  1512. return;
  1513. }
  1514. /*
  1515. * cpuset_create - create a cpuset
  1516. * ss: cpuset cgroup subsystem
  1517. * cont: control group that the new cpuset will be part of
  1518. */
  1519. static struct cgroup_subsys_state *cpuset_create(
  1520. struct cgroup_subsys *ss,
  1521. struct cgroup *cont)
  1522. {
  1523. struct cpuset *cs;
  1524. struct cpuset *parent;
  1525. if (!cont->parent) {
  1526. /* This is early initialization for the top cgroup */
  1527. top_cpuset.mems_generation = cpuset_mems_generation++;
  1528. return &top_cpuset.css;
  1529. }
  1530. parent = cgroup_cs(cont->parent);
  1531. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1532. if (!cs)
  1533. return ERR_PTR(-ENOMEM);
  1534. cpuset_update_task_memory_state();
  1535. cs->flags = 0;
  1536. if (is_spread_page(parent))
  1537. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1538. if (is_spread_slab(parent))
  1539. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1540. set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  1541. cpus_clear(cs->cpus_allowed);
  1542. nodes_clear(cs->mems_allowed);
  1543. cs->mems_generation = cpuset_mems_generation++;
  1544. fmeter_init(&cs->fmeter);
  1545. cs->relax_domain_level = -1;
  1546. cs->parent = parent;
  1547. number_of_cpusets++;
  1548. return &cs->css ;
  1549. }
  1550. /*
  1551. * If the cpuset being removed has its flag 'sched_load_balance'
  1552. * enabled, then simulate turning sched_load_balance off, which
  1553. * will call async_rebuild_sched_domains().
  1554. */
  1555. static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  1556. {
  1557. struct cpuset *cs = cgroup_cs(cont);
  1558. cpuset_update_task_memory_state();
  1559. if (is_sched_load_balance(cs))
  1560. update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
  1561. number_of_cpusets--;
  1562. kfree(cs);
  1563. }
  1564. struct cgroup_subsys cpuset_subsys = {
  1565. .name = "cpuset",
  1566. .create = cpuset_create,
  1567. .destroy = cpuset_destroy,
  1568. .can_attach = cpuset_can_attach,
  1569. .attach = cpuset_attach,
  1570. .populate = cpuset_populate,
  1571. .post_clone = cpuset_post_clone,
  1572. .subsys_id = cpuset_subsys_id,
  1573. .early_init = 1,
  1574. };
  1575. /*
  1576. * cpuset_init_early - just enough so that the calls to
  1577. * cpuset_update_task_memory_state() in early init code
  1578. * are harmless.
  1579. */
  1580. int __init cpuset_init_early(void)
  1581. {
  1582. top_cpuset.mems_generation = cpuset_mems_generation++;
  1583. return 0;
  1584. }
  1585. /**
  1586. * cpuset_init - initialize cpusets at system boot
  1587. *
  1588. * Description: Initialize top_cpuset and the cpuset internal file system,
  1589. **/
  1590. int __init cpuset_init(void)
  1591. {
  1592. int err = 0;
  1593. cpus_setall(top_cpuset.cpus_allowed);
  1594. nodes_setall(top_cpuset.mems_allowed);
  1595. fmeter_init(&top_cpuset.fmeter);
  1596. top_cpuset.mems_generation = cpuset_mems_generation++;
  1597. set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
  1598. top_cpuset.relax_domain_level = -1;
  1599. err = register_filesystem(&cpuset_fs_type);
  1600. if (err < 0)
  1601. return err;
  1602. number_of_cpusets = 1;
  1603. return 0;
  1604. }
  1605. /**
  1606. * cpuset_do_move_task - move a given task to another cpuset
  1607. * @tsk: pointer to task_struct the task to move
  1608. * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
  1609. *
  1610. * Called by cgroup_scan_tasks() for each task in a cgroup.
  1611. * Return nonzero to stop the walk through the tasks.
  1612. */
  1613. static void cpuset_do_move_task(struct task_struct *tsk,
  1614. struct cgroup_scanner *scan)
  1615. {
  1616. struct cpuset_hotplug_scanner *chsp;
  1617. chsp = container_of(scan, struct cpuset_hotplug_scanner, scan);
  1618. cgroup_attach_task(chsp->to, tsk);
  1619. }
  1620. /**
  1621. * move_member_tasks_to_cpuset - move tasks from one cpuset to another
  1622. * @from: cpuset in which the tasks currently reside
  1623. * @to: cpuset to which the tasks will be moved
  1624. *
  1625. * Called with cgroup_mutex held
  1626. * callback_mutex must not be held, as cpuset_attach() will take it.
  1627. *
  1628. * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
  1629. * calling callback functions for each.
  1630. */
  1631. static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
  1632. {
  1633. struct cpuset_hotplug_scanner scan;
  1634. scan.scan.cg = from->css.cgroup;
  1635. scan.scan.test_task = NULL; /* select all tasks in cgroup */
  1636. scan.scan.process_task = cpuset_do_move_task;
  1637. scan.scan.heap = NULL;
  1638. scan.to = to->css.cgroup;
  1639. if (cgroup_scan_tasks(&scan.scan))
  1640. printk(KERN_ERR "move_member_tasks_to_cpuset: "
  1641. "cgroup_scan_tasks failed\n");
  1642. }
  1643. /*
  1644. * If CPU and/or memory hotplug handlers, below, unplug any CPUs
  1645. * or memory nodes, we need to walk over the cpuset hierarchy,
  1646. * removing that CPU or node from all cpusets. If this removes the
  1647. * last CPU or node from a cpuset, then move the tasks in the empty
  1648. * cpuset to its next-highest non-empty parent.
  1649. *
  1650. * Called with cgroup_mutex held
  1651. * callback_mutex must not be held, as cpuset_attach() will take it.
  1652. */
  1653. static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
  1654. {
  1655. struct cpuset *parent;
  1656. /*
  1657. * The cgroup's css_sets list is in use if there are tasks
  1658. * in the cpuset; the list is empty if there are none;
  1659. * the cs->css.refcnt seems always 0.
  1660. */
  1661. if (list_empty(&cs->css.cgroup->css_sets))
  1662. return;
  1663. /*
  1664. * Find its next-highest non-empty parent, (top cpuset
  1665. * has online cpus, so can't be empty).
  1666. */
  1667. parent = cs->parent;
  1668. while (cpus_empty(parent->cpus_allowed) ||
  1669. nodes_empty(parent->mems_allowed))
  1670. parent = parent->parent;
  1671. move_member_tasks_to_cpuset(cs, parent);
  1672. }
  1673. /*
  1674. * Walk the specified cpuset subtree and look for empty cpusets.
  1675. * The tasks of such cpuset must be moved to a parent cpuset.
  1676. *
  1677. * Called with cgroup_mutex held. We take callback_mutex to modify
  1678. * cpus_allowed and mems_allowed.
  1679. *
  1680. * This walk processes the tree from top to bottom, completing one layer
  1681. * before dropping down to the next. It always processes a node before
  1682. * any of its children.
  1683. *
  1684. * For now, since we lack memory hot unplug, we'll never see a cpuset
  1685. * that has tasks along with an empty 'mems'. But if we did see such
  1686. * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
  1687. */
  1688. static void scan_for_empty_cpusets(struct cpuset *root)
  1689. {
  1690. LIST_HEAD(queue);
  1691. struct cpuset *cp; /* scans cpusets being updated */
  1692. struct cpuset *child; /* scans child cpusets of cp */
  1693. struct cgroup *cont;
  1694. nodemask_t oldmems;
  1695. list_add_tail((struct list_head *)&root->stack_list, &queue);
  1696. while (!list_empty(&queue)) {
  1697. cp = list_first_entry(&queue, struct cpuset, stack_list);
  1698. list_del(queue.next);
  1699. list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
  1700. child = cgroup_cs(cont);
  1701. list_add_tail(&child->stack_list, &queue);
  1702. }
  1703. /* Continue past cpusets with all cpus, mems online */
  1704. if (cpus_subset(cp->cpus_allowed, cpu_online_map) &&
  1705. nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
  1706. continue;
  1707. oldmems = cp->mems_allowed;
  1708. /* Remove offline cpus and mems from this cpuset. */
  1709. mutex_lock(&callback_mutex);
  1710. cpus_and(cp->cpus_allowed, cp->cpus_allowed, cpu_online_map);
  1711. nodes_and(cp->mems_allowed, cp->mems_allowed,
  1712. node_states[N_HIGH_MEMORY]);
  1713. mutex_unlock(&callback_mutex);
  1714. /* Move tasks from the empty cpuset to a parent */
  1715. if (cpus_empty(cp->cpus_allowed) ||
  1716. nodes_empty(cp->mems_allowed))
  1717. remove_tasks_in_empty_cpuset(cp);
  1718. else {
  1719. update_tasks_cpumask(cp, NULL);
  1720. update_tasks_nodemask(cp, &oldmems);
  1721. }
  1722. }
  1723. }
  1724. /*
  1725. * The top_cpuset tracks what CPUs and Memory Nodes are online,
  1726. * period. This is necessary in order to make cpusets transparent
  1727. * (of no affect) on systems that are actively using CPU hotplug
  1728. * but making no active use of cpusets.
  1729. *
  1730. * This routine ensures that top_cpuset.cpus_allowed tracks
  1731. * cpu_online_map on each CPU hotplug (cpuhp) event.
  1732. *
  1733. * Called within get_online_cpus(). Needs to call cgroup_lock()
  1734. * before calling generate_sched_domains().
  1735. */
  1736. static int cpuset_track_online_cpus(struct notifier_block *unused_nb,
  1737. unsigned long phase, void *unused_cpu)
  1738. {
  1739. struct sched_domain_attr *attr;
  1740. cpumask_t *doms;
  1741. int ndoms;
  1742. switch (phase) {
  1743. case CPU_ONLINE:
  1744. case CPU_ONLINE_FROZEN:
  1745. case CPU_DEAD:
  1746. case CPU_DEAD_FROZEN:
  1747. break;
  1748. default:
  1749. return NOTIFY_DONE;
  1750. }
  1751. cgroup_lock();
  1752. top_cpuset.cpus_allowed = cpu_online_map;
  1753. scan_for_empty_cpusets(&top_cpuset);
  1754. ndoms = generate_sched_domains(&doms, &attr);
  1755. cgroup_unlock();
  1756. /* Have scheduler rebuild the domains */
  1757. partition_sched_domains(ndoms, doms, attr);
  1758. return NOTIFY_OK;
  1759. }
  1760. #ifdef CONFIG_MEMORY_HOTPLUG
  1761. /*
  1762. * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
  1763. * Call this routine anytime after node_states[N_HIGH_MEMORY] changes.
  1764. * See also the previous routine cpuset_track_online_cpus().
  1765. */
  1766. void cpuset_track_online_nodes(void)
  1767. {
  1768. cgroup_lock();
  1769. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1770. scan_for_empty_cpusets(&top_cpuset);
  1771. cgroup_unlock();
  1772. }
  1773. #endif
  1774. /**
  1775. * cpuset_init_smp - initialize cpus_allowed
  1776. *
  1777. * Description: Finish top cpuset after cpu, node maps are initialized
  1778. **/
  1779. void __init cpuset_init_smp(void)
  1780. {
  1781. top_cpuset.cpus_allowed = cpu_online_map;
  1782. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1783. hotcpu_notifier(cpuset_track_online_cpus, 0);
  1784. }
  1785. /**
  1786. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1787. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1788. * @pmask: pointer to cpumask_t variable to receive cpus_allowed set.
  1789. *
  1790. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1791. * attached to the specified @tsk. Guaranteed to return some non-empty
  1792. * subset of cpu_online_map, even if this means going outside the
  1793. * tasks cpuset.
  1794. **/
  1795. void cpuset_cpus_allowed(struct task_struct *tsk, cpumask_t *pmask)
  1796. {
  1797. mutex_lock(&callback_mutex);
  1798. cpuset_cpus_allowed_locked(tsk, pmask);
  1799. mutex_unlock(&callback_mutex);
  1800. }
  1801. /**
  1802. * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
  1803. * Must be called with callback_mutex held.
  1804. **/
  1805. void cpuset_cpus_allowed_locked(struct task_struct *tsk, cpumask_t *pmask)
  1806. {
  1807. task_lock(tsk);
  1808. guarantee_online_cpus(task_cs(tsk), pmask);
  1809. task_unlock(tsk);
  1810. }
  1811. void cpuset_init_current_mems_allowed(void)
  1812. {
  1813. nodes_setall(current->mems_allowed);
  1814. }
  1815. /**
  1816. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  1817. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  1818. *
  1819. * Description: Returns the nodemask_t mems_allowed of the cpuset
  1820. * attached to the specified @tsk. Guaranteed to return some non-empty
  1821. * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
  1822. * tasks cpuset.
  1823. **/
  1824. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  1825. {
  1826. nodemask_t mask;
  1827. mutex_lock(&callback_mutex);
  1828. task_lock(tsk);
  1829. guarantee_online_mems(task_cs(tsk), &mask);
  1830. task_unlock(tsk);
  1831. mutex_unlock(&callback_mutex);
  1832. return mask;
  1833. }
  1834. /**
  1835. * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
  1836. * @nodemask: the nodemask to be checked
  1837. *
  1838. * Are any of the nodes in the nodemask allowed in current->mems_allowed?
  1839. */
  1840. int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
  1841. {
  1842. return nodes_intersects(*nodemask, current->mems_allowed);
  1843. }
  1844. /*
  1845. * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
  1846. * mem_hardwall ancestor to the specified cpuset. Call holding
  1847. * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
  1848. * (an unusual configuration), then returns the root cpuset.
  1849. */
  1850. static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
  1851. {
  1852. while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
  1853. cs = cs->parent;
  1854. return cs;
  1855. }
  1856. /**
  1857. * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node?
  1858. * @z: is this zone on an allowed node?
  1859. * @gfp_mask: memory allocation flags
  1860. *
  1861. * If we're in interrupt, yes, we can always allocate. If
  1862. * __GFP_THISNODE is set, yes, we can always allocate. If zone
  1863. * z's node is in our tasks mems_allowed, yes. If it's not a
  1864. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1865. * hardwalled cpuset ancestor to this tasks cpuset, yes.
  1866. * If the task has been OOM killed and has access to memory reserves
  1867. * as specified by the TIF_MEMDIE flag, yes.
  1868. * Otherwise, no.
  1869. *
  1870. * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall()
  1871. * reduces to cpuset_zone_allowed_hardwall(). Otherwise,
  1872. * cpuset_zone_allowed_softwall() might sleep, and might allow a zone
  1873. * from an enclosing cpuset.
  1874. *
  1875. * cpuset_zone_allowed_hardwall() only handles the simpler case of
  1876. * hardwall cpusets, and never sleeps.
  1877. *
  1878. * The __GFP_THISNODE placement logic is really handled elsewhere,
  1879. * by forcibly using a zonelist starting at a specified node, and by
  1880. * (in get_page_from_freelist()) refusing to consider the zones for
  1881. * any node on the zonelist except the first. By the time any such
  1882. * calls get to this routine, we should just shut up and say 'yes'.
  1883. *
  1884. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1885. * and do not allow allocations outside the current tasks cpuset
  1886. * unless the task has been OOM killed as is marked TIF_MEMDIE.
  1887. * GFP_KERNEL allocations are not so marked, so can escape to the
  1888. * nearest enclosing hardwalled ancestor cpuset.
  1889. *
  1890. * Scanning up parent cpusets requires callback_mutex. The
  1891. * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
  1892. * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
  1893. * current tasks mems_allowed came up empty on the first pass over
  1894. * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
  1895. * cpuset are short of memory, might require taking the callback_mutex
  1896. * mutex.
  1897. *
  1898. * The first call here from mm/page_alloc:get_page_from_freelist()
  1899. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
  1900. * so no allocation on a node outside the cpuset is allowed (unless
  1901. * in interrupt, of course).
  1902. *
  1903. * The second pass through get_page_from_freelist() doesn't even call
  1904. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  1905. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  1906. * in alloc_flags. That logic and the checks below have the combined
  1907. * affect that:
  1908. * in_interrupt - any node ok (current task context irrelevant)
  1909. * GFP_ATOMIC - any node ok
  1910. * TIF_MEMDIE - any node ok
  1911. * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
  1912. * GFP_USER - only nodes in current tasks mems allowed ok.
  1913. *
  1914. * Rule:
  1915. * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you
  1916. * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
  1917. * the code that might scan up ancestor cpusets and sleep.
  1918. */
  1919. int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask)
  1920. {
  1921. int node; /* node that zone z is on */
  1922. const struct cpuset *cs; /* current cpuset ancestors */
  1923. int allowed; /* is allocation in zone z allowed? */
  1924. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  1925. return 1;
  1926. node = zone_to_nid(z);
  1927. might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
  1928. if (node_isset(node, current->mems_allowed))
  1929. return 1;
  1930. /*
  1931. * Allow tasks that have access to memory reserves because they have
  1932. * been OOM killed to get memory anywhere.
  1933. */
  1934. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  1935. return 1;
  1936. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1937. return 0;
  1938. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1939. return 1;
  1940. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1941. mutex_lock(&callback_mutex);
  1942. task_lock(current);
  1943. cs = nearest_hardwall_ancestor(task_cs(current));
  1944. task_unlock(current);
  1945. allowed = node_isset(node, cs->mems_allowed);
  1946. mutex_unlock(&callback_mutex);
  1947. return allowed;
  1948. }
  1949. /*
  1950. * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node?
  1951. * @z: is this zone on an allowed node?
  1952. * @gfp_mask: memory allocation flags
  1953. *
  1954. * If we're in interrupt, yes, we can always allocate.
  1955. * If __GFP_THISNODE is set, yes, we can always allocate. If zone
  1956. * z's node is in our tasks mems_allowed, yes. If the task has been
  1957. * OOM killed and has access to memory reserves as specified by the
  1958. * TIF_MEMDIE flag, yes. Otherwise, no.
  1959. *
  1960. * The __GFP_THISNODE placement logic is really handled elsewhere,
  1961. * by forcibly using a zonelist starting at a specified node, and by
  1962. * (in get_page_from_freelist()) refusing to consider the zones for
  1963. * any node on the zonelist except the first. By the time any such
  1964. * calls get to this routine, we should just shut up and say 'yes'.
  1965. *
  1966. * Unlike the cpuset_zone_allowed_softwall() variant, above,
  1967. * this variant requires that the zone be in the current tasks
  1968. * mems_allowed or that we're in interrupt. It does not scan up the
  1969. * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
  1970. * It never sleeps.
  1971. */
  1972. int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask)
  1973. {
  1974. int node; /* node that zone z is on */
  1975. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  1976. return 1;
  1977. node = zone_to_nid(z);
  1978. if (node_isset(node, current->mems_allowed))
  1979. return 1;
  1980. /*
  1981. * Allow tasks that have access to memory reserves because they have
  1982. * been OOM killed to get memory anywhere.
  1983. */
  1984. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  1985. return 1;
  1986. return 0;
  1987. }
  1988. /**
  1989. * cpuset_lock - lock out any changes to cpuset structures
  1990. *
  1991. * The out of memory (oom) code needs to mutex_lock cpusets
  1992. * from being changed while it scans the tasklist looking for a
  1993. * task in an overlapping cpuset. Expose callback_mutex via this
  1994. * cpuset_lock() routine, so the oom code can lock it, before
  1995. * locking the task list. The tasklist_lock is a spinlock, so
  1996. * must be taken inside callback_mutex.
  1997. */
  1998. void cpuset_lock(void)
  1999. {
  2000. mutex_lock(&callback_mutex);
  2001. }
  2002. /**
  2003. * cpuset_unlock - release lock on cpuset changes
  2004. *
  2005. * Undo the lock taken in a previous cpuset_lock() call.
  2006. */
  2007. void cpuset_unlock(void)
  2008. {
  2009. mutex_unlock(&callback_mutex);
  2010. }
  2011. /**
  2012. * cpuset_mem_spread_node() - On which node to begin search for a page
  2013. *
  2014. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  2015. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  2016. * and if the memory allocation used cpuset_mem_spread_node()
  2017. * to determine on which node to start looking, as it will for
  2018. * certain page cache or slab cache pages such as used for file
  2019. * system buffers and inode caches, then instead of starting on the
  2020. * local node to look for a free page, rather spread the starting
  2021. * node around the tasks mems_allowed nodes.
  2022. *
  2023. * We don't have to worry about the returned node being offline
  2024. * because "it can't happen", and even if it did, it would be ok.
  2025. *
  2026. * The routines calling guarantee_online_mems() are careful to
  2027. * only set nodes in task->mems_allowed that are online. So it
  2028. * should not be possible for the following code to return an
  2029. * offline node. But if it did, that would be ok, as this routine
  2030. * is not returning the node where the allocation must be, only
  2031. * the node where the search should start. The zonelist passed to
  2032. * __alloc_pages() will include all nodes. If the slab allocator
  2033. * is passed an offline node, it will fall back to the local node.
  2034. * See kmem_cache_alloc_node().
  2035. */
  2036. int cpuset_mem_spread_node(void)
  2037. {
  2038. int node;
  2039. node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
  2040. if (node == MAX_NUMNODES)
  2041. node = first_node(current->mems_allowed);
  2042. current->cpuset_mem_spread_rotor = node;
  2043. return node;
  2044. }
  2045. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  2046. /**
  2047. * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
  2048. * @tsk1: pointer to task_struct of some task.
  2049. * @tsk2: pointer to task_struct of some other task.
  2050. *
  2051. * Description: Return true if @tsk1's mems_allowed intersects the
  2052. * mems_allowed of @tsk2. Used by the OOM killer to determine if
  2053. * one of the task's memory usage might impact the memory available
  2054. * to the other.
  2055. **/
  2056. int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  2057. const struct task_struct *tsk2)
  2058. {
  2059. return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
  2060. }
  2061. /*
  2062. * Collection of memory_pressure is suppressed unless
  2063. * this flag is enabled by writing "1" to the special
  2064. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  2065. */
  2066. int cpuset_memory_pressure_enabled __read_mostly;
  2067. /**
  2068. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  2069. *
  2070. * Keep a running average of the rate of synchronous (direct)
  2071. * page reclaim efforts initiated by tasks in each cpuset.
  2072. *
  2073. * This represents the rate at which some task in the cpuset
  2074. * ran low on memory on all nodes it was allowed to use, and
  2075. * had to enter the kernels page reclaim code in an effort to
  2076. * create more free memory by tossing clean pages or swapping
  2077. * or writing dirty pages.
  2078. *
  2079. * Display to user space in the per-cpuset read-only file
  2080. * "memory_pressure". Value displayed is an integer
  2081. * representing the recent rate of entry into the synchronous
  2082. * (direct) page reclaim by any task attached to the cpuset.
  2083. **/
  2084. void __cpuset_memory_pressure_bump(void)
  2085. {
  2086. task_lock(current);
  2087. fmeter_markevent(&task_cs(current)->fmeter);
  2088. task_unlock(current);
  2089. }
  2090. #ifdef CONFIG_PROC_PID_CPUSET
  2091. /*
  2092. * proc_cpuset_show()
  2093. * - Print tasks cpuset path into seq_file.
  2094. * - Used for /proc/<pid>/cpuset.
  2095. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  2096. * doesn't really matter if tsk->cpuset changes after we read it,
  2097. * and we take cgroup_mutex, keeping cpuset_attach() from changing it
  2098. * anyway.
  2099. */
  2100. static int proc_cpuset_show(struct seq_file *m, void *unused_v)
  2101. {
  2102. struct pid *pid;
  2103. struct task_struct *tsk;
  2104. char *buf;
  2105. struct cgroup_subsys_state *css;
  2106. int retval;
  2107. retval = -ENOMEM;
  2108. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2109. if (!buf)
  2110. goto out;
  2111. retval = -ESRCH;
  2112. pid = m->private;
  2113. tsk = get_pid_task(pid, PIDTYPE_PID);
  2114. if (!tsk)
  2115. goto out_free;
  2116. retval = -EINVAL;
  2117. cgroup_lock();
  2118. css = task_subsys_state(tsk, cpuset_subsys_id);
  2119. retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
  2120. if (retval < 0)
  2121. goto out_unlock;
  2122. seq_puts(m, buf);
  2123. seq_putc(m, '\n');
  2124. out_unlock:
  2125. cgroup_unlock();
  2126. put_task_struct(tsk);
  2127. out_free:
  2128. kfree(buf);
  2129. out:
  2130. return retval;
  2131. }
  2132. static int cpuset_open(struct inode *inode, struct file *file)
  2133. {
  2134. struct pid *pid = PROC_I(inode)->pid;
  2135. return single_open(file, proc_cpuset_show, pid);
  2136. }
  2137. const struct file_operations proc_cpuset_operations = {
  2138. .open = cpuset_open,
  2139. .read = seq_read,
  2140. .llseek = seq_lseek,
  2141. .release = single_release,
  2142. };
  2143. #endif /* CONFIG_PROC_PID_CPUSET */
  2144. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  2145. void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
  2146. {
  2147. seq_printf(m, "Cpus_allowed:\t");
  2148. seq_cpumask(m, &task->cpus_allowed);
  2149. seq_printf(m, "\n");
  2150. seq_printf(m, "Cpus_allowed_list:\t");
  2151. seq_cpumask_list(m, &task->cpus_allowed);
  2152. seq_printf(m, "\n");
  2153. seq_printf(m, "Mems_allowed:\t");
  2154. seq_nodemask(m, &task->mems_allowed);
  2155. seq_printf(m, "\n");
  2156. seq_printf(m, "Mems_allowed_list:\t");
  2157. seq_nodemask_list(m, &task->mems_allowed);
  2158. seq_printf(m, "\n");
  2159. }