cpuset.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  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. *
  18. * This file is subject to the terms and conditions of the GNU General Public
  19. * License. See the file COPYING in the main directory of the Linux
  20. * distribution for more details.
  21. */
  22. #include <linux/cpu.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/cpuset.h>
  25. #include <linux/err.h>
  26. #include <linux/errno.h>
  27. #include <linux/file.h>
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kmod.h>
  33. #include <linux/list.h>
  34. #include <linux/mempolicy.h>
  35. #include <linux/mm.h>
  36. #include <linux/module.h>
  37. #include <linux/mount.h>
  38. #include <linux/namei.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/sched.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/security.h>
  45. #include <linux/slab.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/stat.h>
  48. #include <linux/string.h>
  49. #include <linux/time.h>
  50. #include <linux/backing-dev.h>
  51. #include <linux/sort.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/atomic.h>
  54. #include <linux/mutex.h>
  55. #include <linux/kfifo.h>
  56. #include <linux/workqueue.h>
  57. #include <linux/cgroup.h>
  58. /*
  59. * Tracks how many cpusets are currently defined in system.
  60. * When there is only one cpuset (the root cpuset) we can
  61. * short circuit some hooks.
  62. */
  63. int number_of_cpusets __read_mostly;
  64. /* Forward declare cgroup structures */
  65. struct cgroup_subsys cpuset_subsys;
  66. struct cpuset;
  67. /* See "Frequency meter" comments, below. */
  68. struct fmeter {
  69. int cnt; /* unprocessed events count */
  70. int val; /* most recent output value */
  71. time_t time; /* clock (secs) when val computed */
  72. spinlock_t lock; /* guards read or write of above */
  73. };
  74. struct cpuset {
  75. struct cgroup_subsys_state css;
  76. unsigned long flags; /* "unsigned long" so bitops work */
  77. cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
  78. nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
  79. struct cpuset *parent; /* my parent */
  80. /*
  81. * Copy of global cpuset_mems_generation as of the most
  82. * recent time this cpuset changed its mems_allowed.
  83. */
  84. int mems_generation;
  85. struct fmeter fmeter; /* memory_pressure filter */
  86. /* partition number for rebuild_sched_domains() */
  87. int pn;
  88. /* used for walking a cpuset heirarchy */
  89. struct list_head stack_list;
  90. };
  91. /* Retrieve the cpuset for a cgroup */
  92. static inline struct cpuset *cgroup_cs(struct cgroup *cont)
  93. {
  94. return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
  95. struct cpuset, css);
  96. }
  97. /* Retrieve the cpuset for a task */
  98. static inline struct cpuset *task_cs(struct task_struct *task)
  99. {
  100. return container_of(task_subsys_state(task, cpuset_subsys_id),
  101. struct cpuset, css);
  102. }
  103. struct cpuset_hotplug_scanner {
  104. struct cgroup_scanner scan;
  105. struct cgroup *to;
  106. };
  107. /* bits in struct cpuset flags field */
  108. typedef enum {
  109. CS_CPU_EXCLUSIVE,
  110. CS_MEM_EXCLUSIVE,
  111. CS_MEMORY_MIGRATE,
  112. CS_SCHED_LOAD_BALANCE,
  113. CS_SPREAD_PAGE,
  114. CS_SPREAD_SLAB,
  115. } cpuset_flagbits_t;
  116. /* convenient tests for these bits */
  117. static inline int is_cpu_exclusive(const struct cpuset *cs)
  118. {
  119. return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  120. }
  121. static inline int is_mem_exclusive(const struct cpuset *cs)
  122. {
  123. return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
  124. }
  125. static inline int is_sched_load_balance(const struct cpuset *cs)
  126. {
  127. return test_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  128. }
  129. static inline int is_memory_migrate(const struct cpuset *cs)
  130. {
  131. return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
  132. }
  133. static inline int is_spread_page(const struct cpuset *cs)
  134. {
  135. return test_bit(CS_SPREAD_PAGE, &cs->flags);
  136. }
  137. static inline int is_spread_slab(const struct cpuset *cs)
  138. {
  139. return test_bit(CS_SPREAD_SLAB, &cs->flags);
  140. }
  141. /*
  142. * Increment this integer everytime any cpuset changes its
  143. * mems_allowed value. Users of cpusets can track this generation
  144. * number, and avoid having to lock and reload mems_allowed unless
  145. * the cpuset they're using changes generation.
  146. *
  147. * A single, global generation is needed because cpuset_attach_task() could
  148. * reattach a task to a different cpuset, which must not have its
  149. * generation numbers aliased with those of that tasks previous cpuset.
  150. *
  151. * Generations are needed for mems_allowed because one task cannot
  152. * modify another's memory placement. So we must enable every task,
  153. * on every visit to __alloc_pages(), to efficiently check whether
  154. * its current->cpuset->mems_allowed has changed, requiring an update
  155. * of its current->mems_allowed.
  156. *
  157. * Since writes to cpuset_mems_generation are guarded by the cgroup lock
  158. * there is no need to mark it atomic.
  159. */
  160. static int cpuset_mems_generation;
  161. static struct cpuset top_cpuset = {
  162. .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
  163. .cpus_allowed = CPU_MASK_ALL,
  164. .mems_allowed = NODE_MASK_ALL,
  165. };
  166. /*
  167. * There are two global mutexes guarding cpuset structures. The first
  168. * is the main control groups cgroup_mutex, accessed via
  169. * cgroup_lock()/cgroup_unlock(). The second is the cpuset-specific
  170. * callback_mutex, below. They can nest. It is ok to first take
  171. * cgroup_mutex, then nest callback_mutex. We also require taking
  172. * task_lock() when dereferencing a task's cpuset pointer. See "The
  173. * task_lock() exception", at the end of this comment.
  174. *
  175. * A task must hold both mutexes to modify cpusets. If a task
  176. * holds cgroup_mutex, then it blocks others wanting that mutex,
  177. * ensuring that it is the only task able to also acquire callback_mutex
  178. * and be able to modify cpusets. It can perform various checks on
  179. * the cpuset structure first, knowing nothing will change. It can
  180. * also allocate memory while just holding cgroup_mutex. While it is
  181. * performing these checks, various callback routines can briefly
  182. * acquire callback_mutex to query cpusets. Once it is ready to make
  183. * the changes, it takes callback_mutex, blocking everyone else.
  184. *
  185. * Calls to the kernel memory allocator can not be made while holding
  186. * callback_mutex, as that would risk double tripping on callback_mutex
  187. * from one of the callbacks into the cpuset code from within
  188. * __alloc_pages().
  189. *
  190. * If a task is only holding callback_mutex, then it has read-only
  191. * access to cpusets.
  192. *
  193. * The task_struct fields mems_allowed and mems_generation may only
  194. * be accessed in the context of that task, so require no locks.
  195. *
  196. * The cpuset_common_file_write handler for operations that modify
  197. * the cpuset hierarchy holds cgroup_mutex across the entire operation,
  198. * single threading all such cpuset modifications across the system.
  199. *
  200. * The cpuset_common_file_read() handlers only hold callback_mutex across
  201. * small pieces of code, such as when reading out possibly multi-word
  202. * cpumasks and nodemasks.
  203. *
  204. * Accessing a task's cpuset should be done in accordance with the
  205. * guidelines for accessing subsystem state in kernel/cgroup.c
  206. */
  207. static DEFINE_MUTEX(callback_mutex);
  208. /* This is ugly, but preserves the userspace API for existing cpuset
  209. * users. If someone tries to mount the "cpuset" filesystem, we
  210. * silently switch it to mount "cgroup" instead */
  211. static int cpuset_get_sb(struct file_system_type *fs_type,
  212. int flags, const char *unused_dev_name,
  213. void *data, struct vfsmount *mnt)
  214. {
  215. struct file_system_type *cgroup_fs = get_fs_type("cgroup");
  216. int ret = -ENODEV;
  217. if (cgroup_fs) {
  218. char mountopts[] =
  219. "cpuset,noprefix,"
  220. "release_agent=/sbin/cpuset_release_agent";
  221. ret = cgroup_fs->get_sb(cgroup_fs, flags,
  222. unused_dev_name, mountopts, mnt);
  223. put_filesystem(cgroup_fs);
  224. }
  225. return ret;
  226. }
  227. static struct file_system_type cpuset_fs_type = {
  228. .name = "cpuset",
  229. .get_sb = cpuset_get_sb,
  230. };
  231. /*
  232. * Return in *pmask the portion of a cpusets's cpus_allowed that
  233. * are online. If none are online, walk up the cpuset hierarchy
  234. * until we find one that does have some online cpus. If we get
  235. * all the way to the top and still haven't found any online cpus,
  236. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  237. * task, return cpu_online_map.
  238. *
  239. * One way or another, we guarantee to return some non-empty subset
  240. * of cpu_online_map.
  241. *
  242. * Call with callback_mutex held.
  243. */
  244. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  245. {
  246. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  247. cs = cs->parent;
  248. if (cs)
  249. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  250. else
  251. *pmask = cpu_online_map;
  252. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  253. }
  254. /*
  255. * Return in *pmask the portion of a cpusets's mems_allowed that
  256. * are online, with memory. If none are online with memory, walk
  257. * up the cpuset hierarchy until we find one that does have some
  258. * online mems. If we get all the way to the top and still haven't
  259. * found any online mems, return node_states[N_HIGH_MEMORY].
  260. *
  261. * One way or another, we guarantee to return some non-empty subset
  262. * of node_states[N_HIGH_MEMORY].
  263. *
  264. * Call with callback_mutex held.
  265. */
  266. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  267. {
  268. while (cs && !nodes_intersects(cs->mems_allowed,
  269. node_states[N_HIGH_MEMORY]))
  270. cs = cs->parent;
  271. if (cs)
  272. nodes_and(*pmask, cs->mems_allowed,
  273. node_states[N_HIGH_MEMORY]);
  274. else
  275. *pmask = node_states[N_HIGH_MEMORY];
  276. BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
  277. }
  278. /**
  279. * cpuset_update_task_memory_state - update task memory placement
  280. *
  281. * If the current tasks cpusets mems_allowed changed behind our
  282. * backs, update current->mems_allowed, mems_generation and task NUMA
  283. * mempolicy to the new value.
  284. *
  285. * Task mempolicy is updated by rebinding it relative to the
  286. * current->cpuset if a task has its memory placement changed.
  287. * Do not call this routine if in_interrupt().
  288. *
  289. * Call without callback_mutex or task_lock() held. May be
  290. * called with or without cgroup_mutex held. Thanks in part to
  291. * 'the_top_cpuset_hack', the task's cpuset pointer will never
  292. * be NULL. This routine also might acquire callback_mutex during
  293. * call.
  294. *
  295. * Reading current->cpuset->mems_generation doesn't need task_lock
  296. * to guard the current->cpuset derefence, because it is guarded
  297. * from concurrent freeing of current->cpuset using RCU.
  298. *
  299. * The rcu_dereference() is technically probably not needed,
  300. * as I don't actually mind if I see a new cpuset pointer but
  301. * an old value of mems_generation. However this really only
  302. * matters on alpha systems using cpusets heavily. If I dropped
  303. * that rcu_dereference(), it would save them a memory barrier.
  304. * For all other arch's, rcu_dereference is a no-op anyway, and for
  305. * alpha systems not using cpusets, another planned optimization,
  306. * avoiding the rcu critical section for tasks in the root cpuset
  307. * which is statically allocated, so can't vanish, will make this
  308. * irrelevant. Better to use RCU as intended, than to engage in
  309. * some cute trick to save a memory barrier that is impossible to
  310. * test, for alpha systems using cpusets heavily, which might not
  311. * even exist.
  312. *
  313. * This routine is needed to update the per-task mems_allowed data,
  314. * within the tasks context, when it is trying to allocate memory
  315. * (in various mm/mempolicy.c routines) and notices that some other
  316. * task has been modifying its cpuset.
  317. */
  318. void cpuset_update_task_memory_state(void)
  319. {
  320. int my_cpusets_mem_gen;
  321. struct task_struct *tsk = current;
  322. struct cpuset *cs;
  323. if (task_cs(tsk) == &top_cpuset) {
  324. /* Don't need rcu for top_cpuset. It's never freed. */
  325. my_cpusets_mem_gen = top_cpuset.mems_generation;
  326. } else {
  327. rcu_read_lock();
  328. my_cpusets_mem_gen = task_cs(current)->mems_generation;
  329. rcu_read_unlock();
  330. }
  331. if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
  332. mutex_lock(&callback_mutex);
  333. task_lock(tsk);
  334. cs = task_cs(tsk); /* Maybe changed when task not locked */
  335. guarantee_online_mems(cs, &tsk->mems_allowed);
  336. tsk->cpuset_mems_generation = cs->mems_generation;
  337. if (is_spread_page(cs))
  338. tsk->flags |= PF_SPREAD_PAGE;
  339. else
  340. tsk->flags &= ~PF_SPREAD_PAGE;
  341. if (is_spread_slab(cs))
  342. tsk->flags |= PF_SPREAD_SLAB;
  343. else
  344. tsk->flags &= ~PF_SPREAD_SLAB;
  345. task_unlock(tsk);
  346. mutex_unlock(&callback_mutex);
  347. mpol_rebind_task(tsk, &tsk->mems_allowed);
  348. }
  349. }
  350. /*
  351. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  352. *
  353. * One cpuset is a subset of another if all its allowed CPUs and
  354. * Memory Nodes are a subset of the other, and its exclusive flags
  355. * are only set if the other's are set. Call holding cgroup_mutex.
  356. */
  357. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  358. {
  359. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  360. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  361. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  362. is_mem_exclusive(p) <= is_mem_exclusive(q);
  363. }
  364. /*
  365. * validate_change() - Used to validate that any proposed cpuset change
  366. * follows the structural rules for cpusets.
  367. *
  368. * If we replaced the flag and mask values of the current cpuset
  369. * (cur) with those values in the trial cpuset (trial), would
  370. * our various subset and exclusive rules still be valid? Presumes
  371. * cgroup_mutex held.
  372. *
  373. * 'cur' is the address of an actual, in-use cpuset. Operations
  374. * such as list traversal that depend on the actual address of the
  375. * cpuset in the list must use cur below, not trial.
  376. *
  377. * 'trial' is the address of bulk structure copy of cur, with
  378. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  379. * or flags changed to new, trial values.
  380. *
  381. * Return 0 if valid, -errno if not.
  382. */
  383. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  384. {
  385. struct cgroup *cont;
  386. struct cpuset *c, *par;
  387. /* Each of our child cpusets must be a subset of us */
  388. list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
  389. if (!is_cpuset_subset(cgroup_cs(cont), trial))
  390. return -EBUSY;
  391. }
  392. /* Remaining checks don't apply to root cpuset */
  393. if (cur == &top_cpuset)
  394. return 0;
  395. par = cur->parent;
  396. /* We must be a subset of our parent cpuset */
  397. if (!is_cpuset_subset(trial, par))
  398. return -EACCES;
  399. /*
  400. * If either I or some sibling (!= me) is exclusive, we can't
  401. * overlap
  402. */
  403. list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
  404. c = cgroup_cs(cont);
  405. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  406. c != cur &&
  407. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  408. return -EINVAL;
  409. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  410. c != cur &&
  411. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  412. return -EINVAL;
  413. }
  414. /* Cpusets with tasks can't have empty cpus_allowed or mems_allowed */
  415. if (cgroup_task_count(cur->css.cgroup)) {
  416. if (cpus_empty(trial->cpus_allowed) ||
  417. nodes_empty(trial->mems_allowed)) {
  418. return -ENOSPC;
  419. }
  420. }
  421. return 0;
  422. }
  423. /*
  424. * Helper routine for rebuild_sched_domains().
  425. * Do cpusets a, b have overlapping cpus_allowed masks?
  426. */
  427. static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
  428. {
  429. return cpus_intersects(a->cpus_allowed, b->cpus_allowed);
  430. }
  431. /*
  432. * rebuild_sched_domains()
  433. *
  434. * If the flag 'sched_load_balance' of any cpuset with non-empty
  435. * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
  436. * which has that flag enabled, or if any cpuset with a non-empty
  437. * 'cpus' is removed, then call this routine to rebuild the
  438. * scheduler's dynamic sched domains.
  439. *
  440. * This routine builds a partial partition of the systems CPUs
  441. * (the set of non-overlappping cpumask_t's in the array 'part'
  442. * below), and passes that partial partition to the kernel/sched.c
  443. * partition_sched_domains() routine, which will rebuild the
  444. * schedulers load balancing domains (sched domains) as specified
  445. * by that partial partition. A 'partial partition' is a set of
  446. * non-overlapping subsets whose union is a subset of that set.
  447. *
  448. * See "What is sched_load_balance" in Documentation/cpusets.txt
  449. * for a background explanation of this.
  450. *
  451. * Does not return errors, on the theory that the callers of this
  452. * routine would rather not worry about failures to rebuild sched
  453. * domains when operating in the severe memory shortage situations
  454. * that could cause allocation failures below.
  455. *
  456. * Call with cgroup_mutex held. May take callback_mutex during
  457. * call due to the kfifo_alloc() and kmalloc() calls. May nest
  458. * a call to the get_online_cpus()/put_online_cpus() pair.
  459. * Must not be called holding callback_mutex, because we must not
  460. * call get_online_cpus() while holding callback_mutex. Elsewhere
  461. * the kernel nests callback_mutex inside get_online_cpus() calls.
  462. * So the reverse nesting would risk an ABBA deadlock.
  463. *
  464. * The three key local variables below are:
  465. * q - a kfifo queue of cpuset pointers, used to implement a
  466. * top-down scan of all cpusets. This scan loads a pointer
  467. * to each cpuset marked is_sched_load_balance into the
  468. * array 'csa'. For our purposes, rebuilding the schedulers
  469. * sched domains, we can ignore !is_sched_load_balance cpusets.
  470. * csa - (for CpuSet Array) Array of pointers to all the cpusets
  471. * that need to be load balanced, for convenient iterative
  472. * access by the subsequent code that finds the best partition,
  473. * i.e the set of domains (subsets) of CPUs such that the
  474. * cpus_allowed of every cpuset marked is_sched_load_balance
  475. * is a subset of one of these domains, while there are as
  476. * many such domains as possible, each as small as possible.
  477. * doms - Conversion of 'csa' to an array of cpumasks, for passing to
  478. * the kernel/sched.c routine partition_sched_domains() in a
  479. * convenient format, that can be easily compared to the prior
  480. * value to determine what partition elements (sched domains)
  481. * were changed (added or removed.)
  482. *
  483. * Finding the best partition (set of domains):
  484. * The triple nested loops below over i, j, k scan over the
  485. * load balanced cpusets (using the array of cpuset pointers in
  486. * csa[]) looking for pairs of cpusets that have overlapping
  487. * cpus_allowed, but which don't have the same 'pn' partition
  488. * number and gives them in the same partition number. It keeps
  489. * looping on the 'restart' label until it can no longer find
  490. * any such pairs.
  491. *
  492. * The union of the cpus_allowed masks from the set of
  493. * all cpusets having the same 'pn' value then form the one
  494. * element of the partition (one sched domain) to be passed to
  495. * partition_sched_domains().
  496. */
  497. static void rebuild_sched_domains(void)
  498. {
  499. struct kfifo *q; /* queue of cpusets to be scanned */
  500. struct cpuset *cp; /* scans q */
  501. struct cpuset **csa; /* array of all cpuset ptrs */
  502. int csn; /* how many cpuset ptrs in csa so far */
  503. int i, j, k; /* indices for partition finding loops */
  504. cpumask_t *doms; /* resulting partition; i.e. sched domains */
  505. int ndoms; /* number of sched domains in result */
  506. int nslot; /* next empty doms[] cpumask_t slot */
  507. q = NULL;
  508. csa = NULL;
  509. doms = NULL;
  510. /* Special case for the 99% of systems with one, full, sched domain */
  511. if (is_sched_load_balance(&top_cpuset)) {
  512. ndoms = 1;
  513. doms = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
  514. if (!doms)
  515. goto rebuild;
  516. *doms = top_cpuset.cpus_allowed;
  517. goto rebuild;
  518. }
  519. q = kfifo_alloc(number_of_cpusets * sizeof(cp), GFP_KERNEL, NULL);
  520. if (IS_ERR(q))
  521. goto done;
  522. csa = kmalloc(number_of_cpusets * sizeof(cp), GFP_KERNEL);
  523. if (!csa)
  524. goto done;
  525. csn = 0;
  526. cp = &top_cpuset;
  527. __kfifo_put(q, (void *)&cp, sizeof(cp));
  528. while (__kfifo_get(q, (void *)&cp, sizeof(cp))) {
  529. struct cgroup *cont;
  530. struct cpuset *child; /* scans child cpusets of cp */
  531. if (is_sched_load_balance(cp))
  532. csa[csn++] = cp;
  533. list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
  534. child = cgroup_cs(cont);
  535. __kfifo_put(q, (void *)&child, sizeof(cp));
  536. }
  537. }
  538. for (i = 0; i < csn; i++)
  539. csa[i]->pn = i;
  540. ndoms = csn;
  541. restart:
  542. /* Find the best partition (set of sched domains) */
  543. for (i = 0; i < csn; i++) {
  544. struct cpuset *a = csa[i];
  545. int apn = a->pn;
  546. for (j = 0; j < csn; j++) {
  547. struct cpuset *b = csa[j];
  548. int bpn = b->pn;
  549. if (apn != bpn && cpusets_overlap(a, b)) {
  550. for (k = 0; k < csn; k++) {
  551. struct cpuset *c = csa[k];
  552. if (c->pn == bpn)
  553. c->pn = apn;
  554. }
  555. ndoms--; /* one less element */
  556. goto restart;
  557. }
  558. }
  559. }
  560. /* Convert <csn, csa> to <ndoms, doms> */
  561. doms = kmalloc(ndoms * sizeof(cpumask_t), GFP_KERNEL);
  562. if (!doms)
  563. goto rebuild;
  564. for (nslot = 0, i = 0; i < csn; i++) {
  565. struct cpuset *a = csa[i];
  566. int apn = a->pn;
  567. if (apn >= 0) {
  568. cpumask_t *dp = doms + nslot;
  569. if (nslot == ndoms) {
  570. static int warnings = 10;
  571. if (warnings) {
  572. printk(KERN_WARNING
  573. "rebuild_sched_domains confused:"
  574. " nslot %d, ndoms %d, csn %d, i %d,"
  575. " apn %d\n",
  576. nslot, ndoms, csn, i, apn);
  577. warnings--;
  578. }
  579. continue;
  580. }
  581. cpus_clear(*dp);
  582. for (j = i; j < csn; j++) {
  583. struct cpuset *b = csa[j];
  584. if (apn == b->pn) {
  585. cpus_or(*dp, *dp, b->cpus_allowed);
  586. b->pn = -1;
  587. }
  588. }
  589. nslot++;
  590. }
  591. }
  592. BUG_ON(nslot != ndoms);
  593. rebuild:
  594. /* Have scheduler rebuild sched domains */
  595. get_online_cpus();
  596. partition_sched_domains(ndoms, doms);
  597. put_online_cpus();
  598. done:
  599. if (q && !IS_ERR(q))
  600. kfifo_free(q);
  601. kfree(csa);
  602. /* Don't kfree(doms) -- partition_sched_domains() does that. */
  603. }
  604. static inline int started_after_time(struct task_struct *t1,
  605. struct timespec *time,
  606. struct task_struct *t2)
  607. {
  608. int start_diff = timespec_compare(&t1->start_time, time);
  609. if (start_diff > 0) {
  610. return 1;
  611. } else if (start_diff < 0) {
  612. return 0;
  613. } else {
  614. /*
  615. * Arbitrarily, if two processes started at the same
  616. * time, we'll say that the lower pointer value
  617. * started first. Note that t2 may have exited by now
  618. * so this may not be a valid pointer any longer, but
  619. * that's fine - it still serves to distinguish
  620. * between two tasks started (effectively)
  621. * simultaneously.
  622. */
  623. return t1 > t2;
  624. }
  625. }
  626. static inline int started_after(void *p1, void *p2)
  627. {
  628. struct task_struct *t1 = p1;
  629. struct task_struct *t2 = p2;
  630. return started_after_time(t1, &t2->start_time, t2);
  631. }
  632. /**
  633. * cpuset_test_cpumask - test a task's cpus_allowed versus its cpuset's
  634. * @tsk: task to test
  635. * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
  636. *
  637. * Call with cgroup_mutex held. May take callback_mutex during call.
  638. * Called for each task in a cgroup by cgroup_scan_tasks().
  639. * Return nonzero if this tasks's cpus_allowed mask should be changed (in other
  640. * words, if its mask is not equal to its cpuset's mask).
  641. */
  642. int cpuset_test_cpumask(struct task_struct *tsk, struct cgroup_scanner *scan)
  643. {
  644. return !cpus_equal(tsk->cpus_allowed,
  645. (cgroup_cs(scan->cg))->cpus_allowed);
  646. }
  647. /**
  648. * cpuset_change_cpumask - make a task's cpus_allowed the same as its cpuset's
  649. * @tsk: task to test
  650. * @scan: struct cgroup_scanner containing the cgroup of the task
  651. *
  652. * Called by cgroup_scan_tasks() for each task in a cgroup whose
  653. * cpus_allowed mask needs to be changed.
  654. *
  655. * We don't need to re-check for the cgroup/cpuset membership, since we're
  656. * holding cgroup_lock() at this point.
  657. */
  658. void cpuset_change_cpumask(struct task_struct *tsk, struct cgroup_scanner *scan)
  659. {
  660. set_cpus_allowed(tsk, (cgroup_cs(scan->cg))->cpus_allowed);
  661. }
  662. /**
  663. * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
  664. * @cs: the cpuset to consider
  665. * @buf: buffer of cpu numbers written to this cpuset
  666. */
  667. static int update_cpumask(struct cpuset *cs, char *buf)
  668. {
  669. struct cpuset trialcs;
  670. struct cgroup_scanner scan;
  671. struct ptr_heap heap;
  672. int retval;
  673. int is_load_balanced;
  674. /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
  675. if (cs == &top_cpuset)
  676. return -EACCES;
  677. trialcs = *cs;
  678. /*
  679. * An empty cpus_allowed is ok only if the cpuset has no tasks.
  680. * Since cpulist_parse() fails on an empty mask, we special case
  681. * that parsing. The validate_change() call ensures that cpusets
  682. * with tasks have cpus.
  683. */
  684. buf = strstrip(buf);
  685. if (!*buf) {
  686. cpus_clear(trialcs.cpus_allowed);
  687. } else {
  688. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  689. if (retval < 0)
  690. return retval;
  691. }
  692. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  693. retval = validate_change(cs, &trialcs);
  694. if (retval < 0)
  695. return retval;
  696. /* Nothing to do if the cpus didn't change */
  697. if (cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed))
  698. return 0;
  699. retval = heap_init(&heap, PAGE_SIZE, GFP_KERNEL, &started_after);
  700. if (retval)
  701. return retval;
  702. is_load_balanced = is_sched_load_balance(&trialcs);
  703. mutex_lock(&callback_mutex);
  704. cs->cpus_allowed = trialcs.cpus_allowed;
  705. mutex_unlock(&callback_mutex);
  706. /*
  707. * Scan tasks in the cpuset, and update the cpumasks of any
  708. * that need an update.
  709. */
  710. scan.cg = cs->css.cgroup;
  711. scan.test_task = cpuset_test_cpumask;
  712. scan.process_task = cpuset_change_cpumask;
  713. scan.heap = &heap;
  714. cgroup_scan_tasks(&scan);
  715. heap_free(&heap);
  716. if (is_load_balanced)
  717. rebuild_sched_domains();
  718. return 0;
  719. }
  720. /*
  721. * cpuset_migrate_mm
  722. *
  723. * Migrate memory region from one set of nodes to another.
  724. *
  725. * Temporarilly set tasks mems_allowed to target nodes of migration,
  726. * so that the migration code can allocate pages on these nodes.
  727. *
  728. * Call holding cgroup_mutex, so current's cpuset won't change
  729. * during this call, as manage_mutex holds off any cpuset_attach()
  730. * calls. Therefore we don't need to take task_lock around the
  731. * call to guarantee_online_mems(), as we know no one is changing
  732. * our task's cpuset.
  733. *
  734. * Hold callback_mutex around the two modifications of our tasks
  735. * mems_allowed to synchronize with cpuset_mems_allowed().
  736. *
  737. * While the mm_struct we are migrating is typically from some
  738. * other task, the task_struct mems_allowed that we are hacking
  739. * is for our current task, which must allocate new pages for that
  740. * migrating memory region.
  741. *
  742. * We call cpuset_update_task_memory_state() before hacking
  743. * our tasks mems_allowed, so that we are assured of being in
  744. * sync with our tasks cpuset, and in particular, callbacks to
  745. * cpuset_update_task_memory_state() from nested page allocations
  746. * won't see any mismatch of our cpuset and task mems_generation
  747. * values, so won't overwrite our hacked tasks mems_allowed
  748. * nodemask.
  749. */
  750. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  751. const nodemask_t *to)
  752. {
  753. struct task_struct *tsk = current;
  754. cpuset_update_task_memory_state();
  755. mutex_lock(&callback_mutex);
  756. tsk->mems_allowed = *to;
  757. mutex_unlock(&callback_mutex);
  758. do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
  759. mutex_lock(&callback_mutex);
  760. guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
  761. mutex_unlock(&callback_mutex);
  762. }
  763. /*
  764. * Handle user request to change the 'mems' memory placement
  765. * of a cpuset. Needs to validate the request, update the
  766. * cpusets mems_allowed and mems_generation, and for each
  767. * task in the cpuset, rebind any vma mempolicies and if
  768. * the cpuset is marked 'memory_migrate', migrate the tasks
  769. * pages to the new memory.
  770. *
  771. * Call with cgroup_mutex held. May take callback_mutex during call.
  772. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  773. * lock each such tasks mm->mmap_sem, scan its vma's and rebind
  774. * their mempolicies to the cpusets new mems_allowed.
  775. */
  776. static void *cpuset_being_rebound;
  777. static int update_nodemask(struct cpuset *cs, char *buf)
  778. {
  779. struct cpuset trialcs;
  780. nodemask_t oldmem;
  781. struct task_struct *p;
  782. struct mm_struct **mmarray;
  783. int i, n, ntasks;
  784. int migrate;
  785. int fudge;
  786. int retval;
  787. struct cgroup_iter it;
  788. /*
  789. * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
  790. * it's read-only
  791. */
  792. if (cs == &top_cpuset)
  793. return -EACCES;
  794. trialcs = *cs;
  795. /*
  796. * An empty mems_allowed is ok iff there are no tasks in the cpuset.
  797. * Since nodelist_parse() fails on an empty mask, we special case
  798. * that parsing. The validate_change() call ensures that cpusets
  799. * with tasks have memory.
  800. */
  801. buf = strstrip(buf);
  802. if (!*buf) {
  803. nodes_clear(trialcs.mems_allowed);
  804. } else {
  805. retval = nodelist_parse(buf, trialcs.mems_allowed);
  806. if (retval < 0)
  807. goto done;
  808. }
  809. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed,
  810. node_states[N_HIGH_MEMORY]);
  811. oldmem = cs->mems_allowed;
  812. if (nodes_equal(oldmem, trialcs.mems_allowed)) {
  813. retval = 0; /* Too easy - nothing to do */
  814. goto done;
  815. }
  816. retval = validate_change(cs, &trialcs);
  817. if (retval < 0)
  818. goto done;
  819. mutex_lock(&callback_mutex);
  820. cs->mems_allowed = trialcs.mems_allowed;
  821. cs->mems_generation = cpuset_mems_generation++;
  822. mutex_unlock(&callback_mutex);
  823. cpuset_being_rebound = cs; /* causes mpol_copy() rebind */
  824. fudge = 10; /* spare mmarray[] slots */
  825. fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
  826. retval = -ENOMEM;
  827. /*
  828. * Allocate mmarray[] to hold mm reference for each task
  829. * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
  830. * tasklist_lock. We could use GFP_ATOMIC, but with a
  831. * few more lines of code, we can retry until we get a big
  832. * enough mmarray[] w/o using GFP_ATOMIC.
  833. */
  834. while (1) {
  835. ntasks = cgroup_task_count(cs->css.cgroup); /* guess */
  836. ntasks += fudge;
  837. mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
  838. if (!mmarray)
  839. goto done;
  840. read_lock(&tasklist_lock); /* block fork */
  841. if (cgroup_task_count(cs->css.cgroup) <= ntasks)
  842. break; /* got enough */
  843. read_unlock(&tasklist_lock); /* try again */
  844. kfree(mmarray);
  845. }
  846. n = 0;
  847. /* Load up mmarray[] with mm reference for each task in cpuset. */
  848. cgroup_iter_start(cs->css.cgroup, &it);
  849. while ((p = cgroup_iter_next(cs->css.cgroup, &it))) {
  850. struct mm_struct *mm;
  851. if (n >= ntasks) {
  852. printk(KERN_WARNING
  853. "Cpuset mempolicy rebind incomplete.\n");
  854. break;
  855. }
  856. mm = get_task_mm(p);
  857. if (!mm)
  858. continue;
  859. mmarray[n++] = mm;
  860. }
  861. cgroup_iter_end(cs->css.cgroup, &it);
  862. read_unlock(&tasklist_lock);
  863. /*
  864. * Now that we've dropped the tasklist spinlock, we can
  865. * rebind the vma mempolicies of each mm in mmarray[] to their
  866. * new cpuset, and release that mm. The mpol_rebind_mm()
  867. * call takes mmap_sem, which we couldn't take while holding
  868. * tasklist_lock. Forks can happen again now - the mpol_copy()
  869. * cpuset_being_rebound check will catch such forks, and rebind
  870. * their vma mempolicies too. Because we still hold the global
  871. * cgroup_mutex, we know that no other rebind effort will
  872. * be contending for the global variable cpuset_being_rebound.
  873. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  874. * is idempotent. Also migrate pages in each mm to new nodes.
  875. */
  876. migrate = is_memory_migrate(cs);
  877. for (i = 0; i < n; i++) {
  878. struct mm_struct *mm = mmarray[i];
  879. mpol_rebind_mm(mm, &cs->mems_allowed);
  880. if (migrate)
  881. cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
  882. mmput(mm);
  883. }
  884. /* We're done rebinding vmas to this cpuset's new mems_allowed. */
  885. kfree(mmarray);
  886. cpuset_being_rebound = NULL;
  887. retval = 0;
  888. done:
  889. return retval;
  890. }
  891. int current_cpuset_is_being_rebound(void)
  892. {
  893. return task_cs(current) == cpuset_being_rebound;
  894. }
  895. /*
  896. * Call with cgroup_mutex held.
  897. */
  898. static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
  899. {
  900. if (simple_strtoul(buf, NULL, 10) != 0)
  901. cpuset_memory_pressure_enabled = 1;
  902. else
  903. cpuset_memory_pressure_enabled = 0;
  904. return 0;
  905. }
  906. /*
  907. * update_flag - read a 0 or a 1 in a file and update associated flag
  908. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  909. * CS_SCHED_LOAD_BALANCE,
  910. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
  911. * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
  912. * cs: the cpuset to update
  913. * buf: the buffer where we read the 0 or 1
  914. *
  915. * Call with cgroup_mutex held.
  916. */
  917. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  918. {
  919. int turning_on;
  920. struct cpuset trialcs;
  921. int err;
  922. int cpus_nonempty, balance_flag_changed;
  923. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  924. trialcs = *cs;
  925. if (turning_on)
  926. set_bit(bit, &trialcs.flags);
  927. else
  928. clear_bit(bit, &trialcs.flags);
  929. err = validate_change(cs, &trialcs);
  930. if (err < 0)
  931. return err;
  932. cpus_nonempty = !cpus_empty(trialcs.cpus_allowed);
  933. balance_flag_changed = (is_sched_load_balance(cs) !=
  934. is_sched_load_balance(&trialcs));
  935. mutex_lock(&callback_mutex);
  936. cs->flags = trialcs.flags;
  937. mutex_unlock(&callback_mutex);
  938. if (cpus_nonempty && balance_flag_changed)
  939. rebuild_sched_domains();
  940. return 0;
  941. }
  942. /*
  943. * Frequency meter - How fast is some event occurring?
  944. *
  945. * These routines manage a digitally filtered, constant time based,
  946. * event frequency meter. There are four routines:
  947. * fmeter_init() - initialize a frequency meter.
  948. * fmeter_markevent() - called each time the event happens.
  949. * fmeter_getrate() - returns the recent rate of such events.
  950. * fmeter_update() - internal routine used to update fmeter.
  951. *
  952. * A common data structure is passed to each of these routines,
  953. * which is used to keep track of the state required to manage the
  954. * frequency meter and its digital filter.
  955. *
  956. * The filter works on the number of events marked per unit time.
  957. * The filter is single-pole low-pass recursive (IIR). The time unit
  958. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  959. * simulate 3 decimal digits of precision (multiplied by 1000).
  960. *
  961. * With an FM_COEF of 933, and a time base of 1 second, the filter
  962. * has a half-life of 10 seconds, meaning that if the events quit
  963. * happening, then the rate returned from the fmeter_getrate()
  964. * will be cut in half each 10 seconds, until it converges to zero.
  965. *
  966. * It is not worth doing a real infinitely recursive filter. If more
  967. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  968. * just compute FM_MAXTICKS ticks worth, by which point the level
  969. * will be stable.
  970. *
  971. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  972. * arithmetic overflow in the fmeter_update() routine.
  973. *
  974. * Given the simple 32 bit integer arithmetic used, this meter works
  975. * best for reporting rates between one per millisecond (msec) and
  976. * one per 32 (approx) seconds. At constant rates faster than one
  977. * per msec it maxes out at values just under 1,000,000. At constant
  978. * rates between one per msec, and one per second it will stabilize
  979. * to a value N*1000, where N is the rate of events per second.
  980. * At constant rates between one per second and one per 32 seconds,
  981. * it will be choppy, moving up on the seconds that have an event,
  982. * and then decaying until the next event. At rates slower than
  983. * about one in 32 seconds, it decays all the way back to zero between
  984. * each event.
  985. */
  986. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  987. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  988. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  989. #define FM_SCALE 1000 /* faux fixed point scale */
  990. /* Initialize a frequency meter */
  991. static void fmeter_init(struct fmeter *fmp)
  992. {
  993. fmp->cnt = 0;
  994. fmp->val = 0;
  995. fmp->time = 0;
  996. spin_lock_init(&fmp->lock);
  997. }
  998. /* Internal meter update - process cnt events and update value */
  999. static void fmeter_update(struct fmeter *fmp)
  1000. {
  1001. time_t now = get_seconds();
  1002. time_t ticks = now - fmp->time;
  1003. if (ticks == 0)
  1004. return;
  1005. ticks = min(FM_MAXTICKS, ticks);
  1006. while (ticks-- > 0)
  1007. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  1008. fmp->time = now;
  1009. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  1010. fmp->cnt = 0;
  1011. }
  1012. /* Process any previous ticks, then bump cnt by one (times scale). */
  1013. static void fmeter_markevent(struct fmeter *fmp)
  1014. {
  1015. spin_lock(&fmp->lock);
  1016. fmeter_update(fmp);
  1017. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  1018. spin_unlock(&fmp->lock);
  1019. }
  1020. /* Process any previous ticks, then return current value. */
  1021. static int fmeter_getrate(struct fmeter *fmp)
  1022. {
  1023. int val;
  1024. spin_lock(&fmp->lock);
  1025. fmeter_update(fmp);
  1026. val = fmp->val;
  1027. spin_unlock(&fmp->lock);
  1028. return val;
  1029. }
  1030. /* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
  1031. static int cpuset_can_attach(struct cgroup_subsys *ss,
  1032. struct cgroup *cont, struct task_struct *tsk)
  1033. {
  1034. struct cpuset *cs = cgroup_cs(cont);
  1035. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  1036. return -ENOSPC;
  1037. return security_task_setscheduler(tsk, 0, NULL);
  1038. }
  1039. static void cpuset_attach(struct cgroup_subsys *ss,
  1040. struct cgroup *cont, struct cgroup *oldcont,
  1041. struct task_struct *tsk)
  1042. {
  1043. cpumask_t cpus;
  1044. nodemask_t from, to;
  1045. struct mm_struct *mm;
  1046. struct cpuset *cs = cgroup_cs(cont);
  1047. struct cpuset *oldcs = cgroup_cs(oldcont);
  1048. mutex_lock(&callback_mutex);
  1049. guarantee_online_cpus(cs, &cpus);
  1050. set_cpus_allowed(tsk, cpus);
  1051. mutex_unlock(&callback_mutex);
  1052. from = oldcs->mems_allowed;
  1053. to = cs->mems_allowed;
  1054. mm = get_task_mm(tsk);
  1055. if (mm) {
  1056. mpol_rebind_mm(mm, &to);
  1057. if (is_memory_migrate(cs))
  1058. cpuset_migrate_mm(mm, &from, &to);
  1059. mmput(mm);
  1060. }
  1061. }
  1062. /* The various types of files and directories in a cpuset file system */
  1063. typedef enum {
  1064. FILE_MEMORY_MIGRATE,
  1065. FILE_CPULIST,
  1066. FILE_MEMLIST,
  1067. FILE_CPU_EXCLUSIVE,
  1068. FILE_MEM_EXCLUSIVE,
  1069. FILE_SCHED_LOAD_BALANCE,
  1070. FILE_MEMORY_PRESSURE_ENABLED,
  1071. FILE_MEMORY_PRESSURE,
  1072. FILE_SPREAD_PAGE,
  1073. FILE_SPREAD_SLAB,
  1074. } cpuset_filetype_t;
  1075. static ssize_t cpuset_common_file_write(struct cgroup *cont,
  1076. struct cftype *cft,
  1077. struct file *file,
  1078. const char __user *userbuf,
  1079. size_t nbytes, loff_t *unused_ppos)
  1080. {
  1081. struct cpuset *cs = cgroup_cs(cont);
  1082. cpuset_filetype_t type = cft->private;
  1083. char *buffer;
  1084. int retval = 0;
  1085. /* Crude upper limit on largest legitimate cpulist user might write. */
  1086. if (nbytes > 100U + 6 * max(NR_CPUS, MAX_NUMNODES))
  1087. return -E2BIG;
  1088. /* +1 for nul-terminator */
  1089. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  1090. return -ENOMEM;
  1091. if (copy_from_user(buffer, userbuf, nbytes)) {
  1092. retval = -EFAULT;
  1093. goto out1;
  1094. }
  1095. buffer[nbytes] = 0; /* nul-terminate */
  1096. cgroup_lock();
  1097. if (cgroup_is_removed(cont)) {
  1098. retval = -ENODEV;
  1099. goto out2;
  1100. }
  1101. switch (type) {
  1102. case FILE_CPULIST:
  1103. retval = update_cpumask(cs, buffer);
  1104. break;
  1105. case FILE_MEMLIST:
  1106. retval = update_nodemask(cs, buffer);
  1107. break;
  1108. case FILE_CPU_EXCLUSIVE:
  1109. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  1110. break;
  1111. case FILE_MEM_EXCLUSIVE:
  1112. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  1113. break;
  1114. case FILE_SCHED_LOAD_BALANCE:
  1115. retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, buffer);
  1116. break;
  1117. case FILE_MEMORY_MIGRATE:
  1118. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  1119. break;
  1120. case FILE_MEMORY_PRESSURE_ENABLED:
  1121. retval = update_memory_pressure_enabled(cs, buffer);
  1122. break;
  1123. case FILE_MEMORY_PRESSURE:
  1124. retval = -EACCES;
  1125. break;
  1126. case FILE_SPREAD_PAGE:
  1127. retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
  1128. cs->mems_generation = cpuset_mems_generation++;
  1129. break;
  1130. case FILE_SPREAD_SLAB:
  1131. retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
  1132. cs->mems_generation = cpuset_mems_generation++;
  1133. break;
  1134. default:
  1135. retval = -EINVAL;
  1136. goto out2;
  1137. }
  1138. if (retval == 0)
  1139. retval = nbytes;
  1140. out2:
  1141. cgroup_unlock();
  1142. out1:
  1143. kfree(buffer);
  1144. return retval;
  1145. }
  1146. /*
  1147. * These ascii lists should be read in a single call, by using a user
  1148. * buffer large enough to hold the entire map. If read in smaller
  1149. * chunks, there is no guarantee of atomicity. Since the display format
  1150. * used, list of ranges of sequential numbers, is variable length,
  1151. * and since these maps can change value dynamically, one could read
  1152. * gibberish by doing partial reads while a list was changing.
  1153. * A single large read to a buffer that crosses a page boundary is
  1154. * ok, because the result being copied to user land is not recomputed
  1155. * across a page fault.
  1156. */
  1157. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1158. {
  1159. cpumask_t mask;
  1160. mutex_lock(&callback_mutex);
  1161. mask = cs->cpus_allowed;
  1162. mutex_unlock(&callback_mutex);
  1163. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  1164. }
  1165. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1166. {
  1167. nodemask_t mask;
  1168. mutex_lock(&callback_mutex);
  1169. mask = cs->mems_allowed;
  1170. mutex_unlock(&callback_mutex);
  1171. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  1172. }
  1173. static ssize_t cpuset_common_file_read(struct cgroup *cont,
  1174. struct cftype *cft,
  1175. struct file *file,
  1176. char __user *buf,
  1177. size_t nbytes, loff_t *ppos)
  1178. {
  1179. struct cpuset *cs = cgroup_cs(cont);
  1180. cpuset_filetype_t type = cft->private;
  1181. char *page;
  1182. ssize_t retval = 0;
  1183. char *s;
  1184. if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
  1185. return -ENOMEM;
  1186. s = page;
  1187. switch (type) {
  1188. case FILE_CPULIST:
  1189. s += cpuset_sprintf_cpulist(s, cs);
  1190. break;
  1191. case FILE_MEMLIST:
  1192. s += cpuset_sprintf_memlist(s, cs);
  1193. break;
  1194. case FILE_CPU_EXCLUSIVE:
  1195. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  1196. break;
  1197. case FILE_MEM_EXCLUSIVE:
  1198. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  1199. break;
  1200. case FILE_SCHED_LOAD_BALANCE:
  1201. *s++ = is_sched_load_balance(cs) ? '1' : '0';
  1202. break;
  1203. case FILE_MEMORY_MIGRATE:
  1204. *s++ = is_memory_migrate(cs) ? '1' : '0';
  1205. break;
  1206. case FILE_MEMORY_PRESSURE_ENABLED:
  1207. *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
  1208. break;
  1209. case FILE_MEMORY_PRESSURE:
  1210. s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
  1211. break;
  1212. case FILE_SPREAD_PAGE:
  1213. *s++ = is_spread_page(cs) ? '1' : '0';
  1214. break;
  1215. case FILE_SPREAD_SLAB:
  1216. *s++ = is_spread_slab(cs) ? '1' : '0';
  1217. break;
  1218. default:
  1219. retval = -EINVAL;
  1220. goto out;
  1221. }
  1222. *s++ = '\n';
  1223. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1224. out:
  1225. free_page((unsigned long)page);
  1226. return retval;
  1227. }
  1228. /*
  1229. * for the common functions, 'private' gives the type of file
  1230. */
  1231. static struct cftype cft_cpus = {
  1232. .name = "cpus",
  1233. .read = cpuset_common_file_read,
  1234. .write = cpuset_common_file_write,
  1235. .private = FILE_CPULIST,
  1236. };
  1237. static struct cftype cft_mems = {
  1238. .name = "mems",
  1239. .read = cpuset_common_file_read,
  1240. .write = cpuset_common_file_write,
  1241. .private = FILE_MEMLIST,
  1242. };
  1243. static struct cftype cft_cpu_exclusive = {
  1244. .name = "cpu_exclusive",
  1245. .read = cpuset_common_file_read,
  1246. .write = cpuset_common_file_write,
  1247. .private = FILE_CPU_EXCLUSIVE,
  1248. };
  1249. static struct cftype cft_mem_exclusive = {
  1250. .name = "mem_exclusive",
  1251. .read = cpuset_common_file_read,
  1252. .write = cpuset_common_file_write,
  1253. .private = FILE_MEM_EXCLUSIVE,
  1254. };
  1255. static struct cftype cft_sched_load_balance = {
  1256. .name = "sched_load_balance",
  1257. .read = cpuset_common_file_read,
  1258. .write = cpuset_common_file_write,
  1259. .private = FILE_SCHED_LOAD_BALANCE,
  1260. };
  1261. static struct cftype cft_memory_migrate = {
  1262. .name = "memory_migrate",
  1263. .read = cpuset_common_file_read,
  1264. .write = cpuset_common_file_write,
  1265. .private = FILE_MEMORY_MIGRATE,
  1266. };
  1267. static struct cftype cft_memory_pressure_enabled = {
  1268. .name = "memory_pressure_enabled",
  1269. .read = cpuset_common_file_read,
  1270. .write = cpuset_common_file_write,
  1271. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1272. };
  1273. static struct cftype cft_memory_pressure = {
  1274. .name = "memory_pressure",
  1275. .read = cpuset_common_file_read,
  1276. .write = cpuset_common_file_write,
  1277. .private = FILE_MEMORY_PRESSURE,
  1278. };
  1279. static struct cftype cft_spread_page = {
  1280. .name = "memory_spread_page",
  1281. .read = cpuset_common_file_read,
  1282. .write = cpuset_common_file_write,
  1283. .private = FILE_SPREAD_PAGE,
  1284. };
  1285. static struct cftype cft_spread_slab = {
  1286. .name = "memory_spread_slab",
  1287. .read = cpuset_common_file_read,
  1288. .write = cpuset_common_file_write,
  1289. .private = FILE_SPREAD_SLAB,
  1290. };
  1291. static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  1292. {
  1293. int err;
  1294. if ((err = cgroup_add_file(cont, ss, &cft_cpus)) < 0)
  1295. return err;
  1296. if ((err = cgroup_add_file(cont, ss, &cft_mems)) < 0)
  1297. return err;
  1298. if ((err = cgroup_add_file(cont, ss, &cft_cpu_exclusive)) < 0)
  1299. return err;
  1300. if ((err = cgroup_add_file(cont, ss, &cft_mem_exclusive)) < 0)
  1301. return err;
  1302. if ((err = cgroup_add_file(cont, ss, &cft_memory_migrate)) < 0)
  1303. return err;
  1304. if ((err = cgroup_add_file(cont, ss, &cft_sched_load_balance)) < 0)
  1305. return err;
  1306. if ((err = cgroup_add_file(cont, ss, &cft_memory_pressure)) < 0)
  1307. return err;
  1308. if ((err = cgroup_add_file(cont, ss, &cft_spread_page)) < 0)
  1309. return err;
  1310. if ((err = cgroup_add_file(cont, ss, &cft_spread_slab)) < 0)
  1311. return err;
  1312. /* memory_pressure_enabled is in root cpuset only */
  1313. if (err == 0 && !cont->parent)
  1314. err = cgroup_add_file(cont, ss,
  1315. &cft_memory_pressure_enabled);
  1316. return 0;
  1317. }
  1318. /*
  1319. * post_clone() is called at the end of cgroup_clone().
  1320. * 'cgroup' was just created automatically as a result of
  1321. * a cgroup_clone(), and the current task is about to
  1322. * be moved into 'cgroup'.
  1323. *
  1324. * Currently we refuse to set up the cgroup - thereby
  1325. * refusing the task to be entered, and as a result refusing
  1326. * the sys_unshare() or clone() which initiated it - if any
  1327. * sibling cpusets have exclusive cpus or mem.
  1328. *
  1329. * If this becomes a problem for some users who wish to
  1330. * allow that scenario, then cpuset_post_clone() could be
  1331. * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
  1332. * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
  1333. * held.
  1334. */
  1335. static void cpuset_post_clone(struct cgroup_subsys *ss,
  1336. struct cgroup *cgroup)
  1337. {
  1338. struct cgroup *parent, *child;
  1339. struct cpuset *cs, *parent_cs;
  1340. parent = cgroup->parent;
  1341. list_for_each_entry(child, &parent->children, sibling) {
  1342. cs = cgroup_cs(child);
  1343. if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
  1344. return;
  1345. }
  1346. cs = cgroup_cs(cgroup);
  1347. parent_cs = cgroup_cs(parent);
  1348. cs->mems_allowed = parent_cs->mems_allowed;
  1349. cs->cpus_allowed = parent_cs->cpus_allowed;
  1350. return;
  1351. }
  1352. /*
  1353. * cpuset_create - create a cpuset
  1354. * ss: cpuset cgroup subsystem
  1355. * cont: control group that the new cpuset will be part of
  1356. */
  1357. static struct cgroup_subsys_state *cpuset_create(
  1358. struct cgroup_subsys *ss,
  1359. struct cgroup *cont)
  1360. {
  1361. struct cpuset *cs;
  1362. struct cpuset *parent;
  1363. if (!cont->parent) {
  1364. /* This is early initialization for the top cgroup */
  1365. top_cpuset.mems_generation = cpuset_mems_generation++;
  1366. return &top_cpuset.css;
  1367. }
  1368. parent = cgroup_cs(cont->parent);
  1369. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1370. if (!cs)
  1371. return ERR_PTR(-ENOMEM);
  1372. cpuset_update_task_memory_state();
  1373. cs->flags = 0;
  1374. if (is_spread_page(parent))
  1375. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1376. if (is_spread_slab(parent))
  1377. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1378. set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  1379. cs->cpus_allowed = CPU_MASK_NONE;
  1380. cs->mems_allowed = NODE_MASK_NONE;
  1381. cs->mems_generation = cpuset_mems_generation++;
  1382. fmeter_init(&cs->fmeter);
  1383. cs->parent = parent;
  1384. number_of_cpusets++;
  1385. return &cs->css ;
  1386. }
  1387. /*
  1388. * Locking note on the strange update_flag() call below:
  1389. *
  1390. * If the cpuset being removed has its flag 'sched_load_balance'
  1391. * enabled, then simulate turning sched_load_balance off, which
  1392. * will call rebuild_sched_domains(). The get_online_cpus()
  1393. * call in rebuild_sched_domains() must not be made while holding
  1394. * callback_mutex. Elsewhere the kernel nests callback_mutex inside
  1395. * get_online_cpus() calls. So the reverse nesting would risk an
  1396. * ABBA deadlock.
  1397. */
  1398. static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  1399. {
  1400. struct cpuset *cs = cgroup_cs(cont);
  1401. cpuset_update_task_memory_state();
  1402. if (is_sched_load_balance(cs))
  1403. update_flag(CS_SCHED_LOAD_BALANCE, cs, "0");
  1404. number_of_cpusets--;
  1405. kfree(cs);
  1406. }
  1407. struct cgroup_subsys cpuset_subsys = {
  1408. .name = "cpuset",
  1409. .create = cpuset_create,
  1410. .destroy = cpuset_destroy,
  1411. .can_attach = cpuset_can_attach,
  1412. .attach = cpuset_attach,
  1413. .populate = cpuset_populate,
  1414. .post_clone = cpuset_post_clone,
  1415. .subsys_id = cpuset_subsys_id,
  1416. .early_init = 1,
  1417. };
  1418. /*
  1419. * cpuset_init_early - just enough so that the calls to
  1420. * cpuset_update_task_memory_state() in early init code
  1421. * are harmless.
  1422. */
  1423. int __init cpuset_init_early(void)
  1424. {
  1425. top_cpuset.mems_generation = cpuset_mems_generation++;
  1426. return 0;
  1427. }
  1428. /**
  1429. * cpuset_init - initialize cpusets at system boot
  1430. *
  1431. * Description: Initialize top_cpuset and the cpuset internal file system,
  1432. **/
  1433. int __init cpuset_init(void)
  1434. {
  1435. int err = 0;
  1436. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1437. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1438. fmeter_init(&top_cpuset.fmeter);
  1439. top_cpuset.mems_generation = cpuset_mems_generation++;
  1440. set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
  1441. err = register_filesystem(&cpuset_fs_type);
  1442. if (err < 0)
  1443. return err;
  1444. number_of_cpusets = 1;
  1445. return 0;
  1446. }
  1447. /**
  1448. * cpuset_do_move_task - move a given task to another cpuset
  1449. * @tsk: pointer to task_struct the task to move
  1450. * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
  1451. *
  1452. * Called by cgroup_scan_tasks() for each task in a cgroup.
  1453. * Return nonzero to stop the walk through the tasks.
  1454. */
  1455. void cpuset_do_move_task(struct task_struct *tsk, struct cgroup_scanner *scan)
  1456. {
  1457. struct cpuset_hotplug_scanner *chsp;
  1458. chsp = container_of(scan, struct cpuset_hotplug_scanner, scan);
  1459. cgroup_attach_task(chsp->to, tsk);
  1460. }
  1461. /**
  1462. * move_member_tasks_to_cpuset - move tasks from one cpuset to another
  1463. * @from: cpuset in which the tasks currently reside
  1464. * @to: cpuset to which the tasks will be moved
  1465. *
  1466. * Called with cgroup_mutex held
  1467. * callback_mutex must not be held, as cpuset_attach() will take it.
  1468. *
  1469. * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
  1470. * calling callback functions for each.
  1471. */
  1472. static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
  1473. {
  1474. struct cpuset_hotplug_scanner scan;
  1475. scan.scan.cg = from->css.cgroup;
  1476. scan.scan.test_task = NULL; /* select all tasks in cgroup */
  1477. scan.scan.process_task = cpuset_do_move_task;
  1478. scan.scan.heap = NULL;
  1479. scan.to = to->css.cgroup;
  1480. if (cgroup_scan_tasks((struct cgroup_scanner *)&scan))
  1481. printk(KERN_ERR "move_member_tasks_to_cpuset: "
  1482. "cgroup_scan_tasks failed\n");
  1483. }
  1484. /*
  1485. * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs
  1486. * or memory nodes, we need to walk over the cpuset hierarchy,
  1487. * removing that CPU or node from all cpusets. If this removes the
  1488. * last CPU or node from a cpuset, then move the tasks in the empty
  1489. * cpuset to its next-highest non-empty parent.
  1490. *
  1491. * Called with cgroup_mutex held
  1492. * callback_mutex must not be held, as cpuset_attach() will take it.
  1493. */
  1494. static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
  1495. {
  1496. struct cpuset *parent;
  1497. /*
  1498. * The cgroup's css_sets list is in use if there are tasks
  1499. * in the cpuset; the list is empty if there are none;
  1500. * the cs->css.refcnt seems always 0.
  1501. */
  1502. if (list_empty(&cs->css.cgroup->css_sets))
  1503. return;
  1504. /*
  1505. * Find its next-highest non-empty parent, (top cpuset
  1506. * has online cpus, so can't be empty).
  1507. */
  1508. parent = cs->parent;
  1509. while (cpus_empty(parent->cpus_allowed) ||
  1510. nodes_empty(parent->mems_allowed))
  1511. parent = parent->parent;
  1512. move_member_tasks_to_cpuset(cs, parent);
  1513. }
  1514. /*
  1515. * Walk the specified cpuset subtree and look for empty cpusets.
  1516. * The tasks of such cpuset must be moved to a parent cpuset.
  1517. *
  1518. * Called with cgroup_mutex held. We take callback_mutex to modify
  1519. * cpus_allowed and mems_allowed.
  1520. *
  1521. * This walk processes the tree from top to bottom, completing one layer
  1522. * before dropping down to the next. It always processes a node before
  1523. * any of its children.
  1524. *
  1525. * For now, since we lack memory hot unplug, we'll never see a cpuset
  1526. * that has tasks along with an empty 'mems'. But if we did see such
  1527. * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
  1528. */
  1529. static void scan_for_empty_cpusets(const struct cpuset *root)
  1530. {
  1531. struct cpuset *cp; /* scans cpusets being updated */
  1532. struct cpuset *child; /* scans child cpusets of cp */
  1533. struct list_head queue;
  1534. struct cgroup *cont;
  1535. INIT_LIST_HEAD(&queue);
  1536. list_add_tail((struct list_head *)&root->stack_list, &queue);
  1537. while (!list_empty(&queue)) {
  1538. cp = container_of(queue.next, struct cpuset, stack_list);
  1539. list_del(queue.next);
  1540. list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
  1541. child = cgroup_cs(cont);
  1542. list_add_tail(&child->stack_list, &queue);
  1543. }
  1544. cont = cp->css.cgroup;
  1545. /* Continue past cpusets with all cpus, mems online */
  1546. if (cpus_subset(cp->cpus_allowed, cpu_online_map) &&
  1547. nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
  1548. continue;
  1549. /* Remove offline cpus and mems from this cpuset. */
  1550. mutex_lock(&callback_mutex);
  1551. cpus_and(cp->cpus_allowed, cp->cpus_allowed, cpu_online_map);
  1552. nodes_and(cp->mems_allowed, cp->mems_allowed,
  1553. node_states[N_HIGH_MEMORY]);
  1554. mutex_unlock(&callback_mutex);
  1555. /* Move tasks from the empty cpuset to a parent */
  1556. if (cpus_empty(cp->cpus_allowed) ||
  1557. nodes_empty(cp->mems_allowed))
  1558. remove_tasks_in_empty_cpuset(cp);
  1559. }
  1560. }
  1561. /*
  1562. * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track
  1563. * cpu_online_map and node_states[N_HIGH_MEMORY]. Force the top cpuset to
  1564. * track what's online after any CPU or memory node hotplug or unplug event.
  1565. *
  1566. * Since there are two callers of this routine, one for CPU hotplug
  1567. * events and one for memory node hotplug events, we could have coded
  1568. * two separate routines here. We code it as a single common routine
  1569. * in order to minimize text size.
  1570. */
  1571. static void common_cpu_mem_hotplug_unplug(void)
  1572. {
  1573. cgroup_lock();
  1574. top_cpuset.cpus_allowed = cpu_online_map;
  1575. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1576. scan_for_empty_cpusets(&top_cpuset);
  1577. cgroup_unlock();
  1578. }
  1579. /*
  1580. * The top_cpuset tracks what CPUs and Memory Nodes are online,
  1581. * period. This is necessary in order to make cpusets transparent
  1582. * (of no affect) on systems that are actively using CPU hotplug
  1583. * but making no active use of cpusets.
  1584. *
  1585. * This routine ensures that top_cpuset.cpus_allowed tracks
  1586. * cpu_online_map on each CPU hotplug (cpuhp) event.
  1587. */
  1588. static int cpuset_handle_cpuhp(struct notifier_block *unused_nb,
  1589. unsigned long phase, void *unused_cpu)
  1590. {
  1591. if (phase == CPU_DYING || phase == CPU_DYING_FROZEN)
  1592. return NOTIFY_DONE;
  1593. common_cpu_mem_hotplug_unplug();
  1594. return 0;
  1595. }
  1596. #ifdef CONFIG_MEMORY_HOTPLUG
  1597. /*
  1598. * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
  1599. * Call this routine anytime after you change
  1600. * node_states[N_HIGH_MEMORY].
  1601. * See also the previous routine cpuset_handle_cpuhp().
  1602. */
  1603. void cpuset_track_online_nodes(void)
  1604. {
  1605. common_cpu_mem_hotplug_unplug();
  1606. }
  1607. #endif
  1608. /**
  1609. * cpuset_init_smp - initialize cpus_allowed
  1610. *
  1611. * Description: Finish top cpuset after cpu, node maps are initialized
  1612. **/
  1613. void __init cpuset_init_smp(void)
  1614. {
  1615. top_cpuset.cpus_allowed = cpu_online_map;
  1616. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1617. hotcpu_notifier(cpuset_handle_cpuhp, 0);
  1618. }
  1619. /**
  1620. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1621. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1622. *
  1623. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1624. * attached to the specified @tsk. Guaranteed to return some non-empty
  1625. * subset of cpu_online_map, even if this means going outside the
  1626. * tasks cpuset.
  1627. **/
  1628. cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
  1629. {
  1630. cpumask_t mask;
  1631. mutex_lock(&callback_mutex);
  1632. mask = cpuset_cpus_allowed_locked(tsk);
  1633. mutex_unlock(&callback_mutex);
  1634. return mask;
  1635. }
  1636. /**
  1637. * cpuset_cpus_allowed_locked - return cpus_allowed mask from a tasks cpuset.
  1638. * Must be called with callback_mutex held.
  1639. **/
  1640. cpumask_t cpuset_cpus_allowed_locked(struct task_struct *tsk)
  1641. {
  1642. cpumask_t mask;
  1643. task_lock(tsk);
  1644. guarantee_online_cpus(task_cs(tsk), &mask);
  1645. task_unlock(tsk);
  1646. return mask;
  1647. }
  1648. void cpuset_init_current_mems_allowed(void)
  1649. {
  1650. current->mems_allowed = NODE_MASK_ALL;
  1651. }
  1652. /**
  1653. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  1654. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  1655. *
  1656. * Description: Returns the nodemask_t mems_allowed of the cpuset
  1657. * attached to the specified @tsk. Guaranteed to return some non-empty
  1658. * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
  1659. * tasks cpuset.
  1660. **/
  1661. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  1662. {
  1663. nodemask_t mask;
  1664. mutex_lock(&callback_mutex);
  1665. task_lock(tsk);
  1666. guarantee_online_mems(task_cs(tsk), &mask);
  1667. task_unlock(tsk);
  1668. mutex_unlock(&callback_mutex);
  1669. return mask;
  1670. }
  1671. /**
  1672. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1673. * @zl: the zonelist to be checked
  1674. *
  1675. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1676. */
  1677. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1678. {
  1679. int i;
  1680. for (i = 0; zl->zones[i]; i++) {
  1681. int nid = zone_to_nid(zl->zones[i]);
  1682. if (node_isset(nid, current->mems_allowed))
  1683. return 1;
  1684. }
  1685. return 0;
  1686. }
  1687. /*
  1688. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1689. * ancestor to the specified cpuset. Call holding callback_mutex.
  1690. * If no ancestor is mem_exclusive (an unusual configuration), then
  1691. * returns the root cpuset.
  1692. */
  1693. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1694. {
  1695. while (!is_mem_exclusive(cs) && cs->parent)
  1696. cs = cs->parent;
  1697. return cs;
  1698. }
  1699. /**
  1700. * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node?
  1701. * @z: is this zone on an allowed node?
  1702. * @gfp_mask: memory allocation flags
  1703. *
  1704. * If we're in interrupt, yes, we can always allocate. If
  1705. * __GFP_THISNODE is set, yes, we can always allocate. If zone
  1706. * z's node is in our tasks mems_allowed, yes. If it's not a
  1707. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1708. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1709. * If the task has been OOM killed and has access to memory reserves
  1710. * as specified by the TIF_MEMDIE flag, yes.
  1711. * Otherwise, no.
  1712. *
  1713. * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall()
  1714. * reduces to cpuset_zone_allowed_hardwall(). Otherwise,
  1715. * cpuset_zone_allowed_softwall() might sleep, and might allow a zone
  1716. * from an enclosing cpuset.
  1717. *
  1718. * cpuset_zone_allowed_hardwall() only handles the simpler case of
  1719. * hardwall cpusets, and never sleeps.
  1720. *
  1721. * The __GFP_THISNODE placement logic is really handled elsewhere,
  1722. * by forcibly using a zonelist starting at a specified node, and by
  1723. * (in get_page_from_freelist()) refusing to consider the zones for
  1724. * any node on the zonelist except the first. By the time any such
  1725. * calls get to this routine, we should just shut up and say 'yes'.
  1726. *
  1727. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1728. * and do not allow allocations outside the current tasks cpuset
  1729. * unless the task has been OOM killed as is marked TIF_MEMDIE.
  1730. * GFP_KERNEL allocations are not so marked, so can escape to the
  1731. * nearest enclosing mem_exclusive ancestor cpuset.
  1732. *
  1733. * Scanning up parent cpusets requires callback_mutex. The
  1734. * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
  1735. * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
  1736. * current tasks mems_allowed came up empty on the first pass over
  1737. * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
  1738. * cpuset are short of memory, might require taking the callback_mutex
  1739. * mutex.
  1740. *
  1741. * The first call here from mm/page_alloc:get_page_from_freelist()
  1742. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
  1743. * so no allocation on a node outside the cpuset is allowed (unless
  1744. * in interrupt, of course).
  1745. *
  1746. * The second pass through get_page_from_freelist() doesn't even call
  1747. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  1748. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  1749. * in alloc_flags. That logic and the checks below have the combined
  1750. * affect that:
  1751. * in_interrupt - any node ok (current task context irrelevant)
  1752. * GFP_ATOMIC - any node ok
  1753. * TIF_MEMDIE - any node ok
  1754. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1755. * GFP_USER - only nodes in current tasks mems allowed ok.
  1756. *
  1757. * Rule:
  1758. * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you
  1759. * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
  1760. * the code that might scan up ancestor cpusets and sleep.
  1761. */
  1762. int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask)
  1763. {
  1764. int node; /* node that zone z is on */
  1765. const struct cpuset *cs; /* current cpuset ancestors */
  1766. int allowed; /* is allocation in zone z allowed? */
  1767. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  1768. return 1;
  1769. node = zone_to_nid(z);
  1770. might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
  1771. if (node_isset(node, current->mems_allowed))
  1772. return 1;
  1773. /*
  1774. * Allow tasks that have access to memory reserves because they have
  1775. * been OOM killed to get memory anywhere.
  1776. */
  1777. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  1778. return 1;
  1779. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1780. return 0;
  1781. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1782. return 1;
  1783. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1784. mutex_lock(&callback_mutex);
  1785. task_lock(current);
  1786. cs = nearest_exclusive_ancestor(task_cs(current));
  1787. task_unlock(current);
  1788. allowed = node_isset(node, cs->mems_allowed);
  1789. mutex_unlock(&callback_mutex);
  1790. return allowed;
  1791. }
  1792. /*
  1793. * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node?
  1794. * @z: is this zone on an allowed node?
  1795. * @gfp_mask: memory allocation flags
  1796. *
  1797. * If we're in interrupt, yes, we can always allocate.
  1798. * If __GFP_THISNODE is set, yes, we can always allocate. If zone
  1799. * z's node is in our tasks mems_allowed, yes. If the task has been
  1800. * OOM killed and has access to memory reserves as specified by the
  1801. * TIF_MEMDIE flag, yes. Otherwise, no.
  1802. *
  1803. * The __GFP_THISNODE placement logic is really handled elsewhere,
  1804. * by forcibly using a zonelist starting at a specified node, and by
  1805. * (in get_page_from_freelist()) refusing to consider the zones for
  1806. * any node on the zonelist except the first. By the time any such
  1807. * calls get to this routine, we should just shut up and say 'yes'.
  1808. *
  1809. * Unlike the cpuset_zone_allowed_softwall() variant, above,
  1810. * this variant requires that the zone be in the current tasks
  1811. * mems_allowed or that we're in interrupt. It does not scan up the
  1812. * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
  1813. * It never sleeps.
  1814. */
  1815. int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask)
  1816. {
  1817. int node; /* node that zone z is on */
  1818. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  1819. return 1;
  1820. node = zone_to_nid(z);
  1821. if (node_isset(node, current->mems_allowed))
  1822. return 1;
  1823. /*
  1824. * Allow tasks that have access to memory reserves because they have
  1825. * been OOM killed to get memory anywhere.
  1826. */
  1827. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  1828. return 1;
  1829. return 0;
  1830. }
  1831. /**
  1832. * cpuset_lock - lock out any changes to cpuset structures
  1833. *
  1834. * The out of memory (oom) code needs to mutex_lock cpusets
  1835. * from being changed while it scans the tasklist looking for a
  1836. * task in an overlapping cpuset. Expose callback_mutex via this
  1837. * cpuset_lock() routine, so the oom code can lock it, before
  1838. * locking the task list. The tasklist_lock is a spinlock, so
  1839. * must be taken inside callback_mutex.
  1840. */
  1841. void cpuset_lock(void)
  1842. {
  1843. mutex_lock(&callback_mutex);
  1844. }
  1845. /**
  1846. * cpuset_unlock - release lock on cpuset changes
  1847. *
  1848. * Undo the lock taken in a previous cpuset_lock() call.
  1849. */
  1850. void cpuset_unlock(void)
  1851. {
  1852. mutex_unlock(&callback_mutex);
  1853. }
  1854. /**
  1855. * cpuset_mem_spread_node() - On which node to begin search for a page
  1856. *
  1857. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  1858. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  1859. * and if the memory allocation used cpuset_mem_spread_node()
  1860. * to determine on which node to start looking, as it will for
  1861. * certain page cache or slab cache pages such as used for file
  1862. * system buffers and inode caches, then instead of starting on the
  1863. * local node to look for a free page, rather spread the starting
  1864. * node around the tasks mems_allowed nodes.
  1865. *
  1866. * We don't have to worry about the returned node being offline
  1867. * because "it can't happen", and even if it did, it would be ok.
  1868. *
  1869. * The routines calling guarantee_online_mems() are careful to
  1870. * only set nodes in task->mems_allowed that are online. So it
  1871. * should not be possible for the following code to return an
  1872. * offline node. But if it did, that would be ok, as this routine
  1873. * is not returning the node where the allocation must be, only
  1874. * the node where the search should start. The zonelist passed to
  1875. * __alloc_pages() will include all nodes. If the slab allocator
  1876. * is passed an offline node, it will fall back to the local node.
  1877. * See kmem_cache_alloc_node().
  1878. */
  1879. int cpuset_mem_spread_node(void)
  1880. {
  1881. int node;
  1882. node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
  1883. if (node == MAX_NUMNODES)
  1884. node = first_node(current->mems_allowed);
  1885. current->cpuset_mem_spread_rotor = node;
  1886. return node;
  1887. }
  1888. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  1889. /**
  1890. * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
  1891. * @tsk1: pointer to task_struct of some task.
  1892. * @tsk2: pointer to task_struct of some other task.
  1893. *
  1894. * Description: Return true if @tsk1's mems_allowed intersects the
  1895. * mems_allowed of @tsk2. Used by the OOM killer to determine if
  1896. * one of the task's memory usage might impact the memory available
  1897. * to the other.
  1898. **/
  1899. int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  1900. const struct task_struct *tsk2)
  1901. {
  1902. return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
  1903. }
  1904. /*
  1905. * Collection of memory_pressure is suppressed unless
  1906. * this flag is enabled by writing "1" to the special
  1907. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  1908. */
  1909. int cpuset_memory_pressure_enabled __read_mostly;
  1910. /**
  1911. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  1912. *
  1913. * Keep a running average of the rate of synchronous (direct)
  1914. * page reclaim efforts initiated by tasks in each cpuset.
  1915. *
  1916. * This represents the rate at which some task in the cpuset
  1917. * ran low on memory on all nodes it was allowed to use, and
  1918. * had to enter the kernels page reclaim code in an effort to
  1919. * create more free memory by tossing clean pages or swapping
  1920. * or writing dirty pages.
  1921. *
  1922. * Display to user space in the per-cpuset read-only file
  1923. * "memory_pressure". Value displayed is an integer
  1924. * representing the recent rate of entry into the synchronous
  1925. * (direct) page reclaim by any task attached to the cpuset.
  1926. **/
  1927. void __cpuset_memory_pressure_bump(void)
  1928. {
  1929. task_lock(current);
  1930. fmeter_markevent(&task_cs(current)->fmeter);
  1931. task_unlock(current);
  1932. }
  1933. #ifdef CONFIG_PROC_PID_CPUSET
  1934. /*
  1935. * proc_cpuset_show()
  1936. * - Print tasks cpuset path into seq_file.
  1937. * - Used for /proc/<pid>/cpuset.
  1938. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  1939. * doesn't really matter if tsk->cpuset changes after we read it,
  1940. * and we take cgroup_mutex, keeping cpuset_attach() from changing it
  1941. * anyway.
  1942. */
  1943. static int proc_cpuset_show(struct seq_file *m, void *unused_v)
  1944. {
  1945. struct pid *pid;
  1946. struct task_struct *tsk;
  1947. char *buf;
  1948. struct cgroup_subsys_state *css;
  1949. int retval;
  1950. retval = -ENOMEM;
  1951. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1952. if (!buf)
  1953. goto out;
  1954. retval = -ESRCH;
  1955. pid = m->private;
  1956. tsk = get_pid_task(pid, PIDTYPE_PID);
  1957. if (!tsk)
  1958. goto out_free;
  1959. retval = -EINVAL;
  1960. cgroup_lock();
  1961. css = task_subsys_state(tsk, cpuset_subsys_id);
  1962. retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
  1963. if (retval < 0)
  1964. goto out_unlock;
  1965. seq_puts(m, buf);
  1966. seq_putc(m, '\n');
  1967. out_unlock:
  1968. cgroup_unlock();
  1969. put_task_struct(tsk);
  1970. out_free:
  1971. kfree(buf);
  1972. out:
  1973. return retval;
  1974. }
  1975. static int cpuset_open(struct inode *inode, struct file *file)
  1976. {
  1977. struct pid *pid = PROC_I(inode)->pid;
  1978. return single_open(file, proc_cpuset_show, pid);
  1979. }
  1980. const struct file_operations proc_cpuset_operations = {
  1981. .open = cpuset_open,
  1982. .read = seq_read,
  1983. .llseek = seq_lseek,
  1984. .release = single_release,
  1985. };
  1986. #endif /* CONFIG_PROC_PID_CPUSET */
  1987. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1988. void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
  1989. {
  1990. seq_printf(m, "Cpus_allowed:\t");
  1991. m->count += cpumask_scnprintf(m->buf + m->count, m->size - m->count,
  1992. task->cpus_allowed);
  1993. seq_printf(m, "\n");
  1994. seq_printf(m, "Mems_allowed:\t");
  1995. m->count += nodemask_scnprintf(m->buf + m->count, m->size - m->count,
  1996. task->mems_allowed);
  1997. seq_printf(m, "\n");
  1998. }