cpuset.c 78 KB

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