cpuset.c 75 KB

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