cpuset.c 73 KB

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