cpuset.c 76 KB

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