cpuset.c 77 KB

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