cpuset.c 77 KB

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