cpuset.c 74 KB

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