cpuset.c 77 KB

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