cpuset.c 75 KB

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