cpuset.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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-2006 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. /*
  56. * Tracks how many cpusets are currently defined in system.
  57. * When there is only one cpuset (the root cpuset) we can
  58. * short circuit some hooks.
  59. */
  60. int number_of_cpusets __read_mostly;
  61. /* Retrieve the cpuset from a cgroup */
  62. struct cgroup_subsys cpuset_subsys;
  63. struct cpuset;
  64. /* See "Frequency meter" comments, below. */
  65. struct fmeter {
  66. int cnt; /* unprocessed events count */
  67. int val; /* most recent output value */
  68. time_t time; /* clock (secs) when val computed */
  69. spinlock_t lock; /* guards read or write of above */
  70. };
  71. struct cpuset {
  72. struct cgroup_subsys_state css;
  73. unsigned long flags; /* "unsigned long" so bitops work */
  74. cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
  75. nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
  76. struct cpuset *parent; /* my parent */
  77. /*
  78. * Copy of global cpuset_mems_generation as of the most
  79. * recent time this cpuset changed its mems_allowed.
  80. */
  81. int mems_generation;
  82. struct fmeter fmeter; /* memory_pressure filter */
  83. };
  84. /* Retrieve the cpuset for a cgroup */
  85. static inline struct cpuset *cgroup_cs(struct cgroup *cont)
  86. {
  87. return container_of(cgroup_subsys_state(cont, cpuset_subsys_id),
  88. struct cpuset, css);
  89. }
  90. /* Retrieve the cpuset for a task */
  91. static inline struct cpuset *task_cs(struct task_struct *task)
  92. {
  93. return container_of(task_subsys_state(task, cpuset_subsys_id),
  94. struct cpuset, css);
  95. }
  96. /* bits in struct cpuset flags field */
  97. typedef enum {
  98. CS_CPU_EXCLUSIVE,
  99. CS_MEM_EXCLUSIVE,
  100. CS_MEMORY_MIGRATE,
  101. CS_SPREAD_PAGE,
  102. CS_SPREAD_SLAB,
  103. } cpuset_flagbits_t;
  104. /* convenient tests for these bits */
  105. static inline int is_cpu_exclusive(const struct cpuset *cs)
  106. {
  107. return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  108. }
  109. static inline int is_mem_exclusive(const struct cpuset *cs)
  110. {
  111. return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
  112. }
  113. static inline int is_memory_migrate(const struct cpuset *cs)
  114. {
  115. return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
  116. }
  117. static inline int is_spread_page(const struct cpuset *cs)
  118. {
  119. return test_bit(CS_SPREAD_PAGE, &cs->flags);
  120. }
  121. static inline int is_spread_slab(const struct cpuset *cs)
  122. {
  123. return test_bit(CS_SPREAD_SLAB, &cs->flags);
  124. }
  125. /*
  126. * Increment this integer everytime any cpuset changes its
  127. * mems_allowed value. Users of cpusets can track this generation
  128. * number, and avoid having to lock and reload mems_allowed unless
  129. * the cpuset they're using changes generation.
  130. *
  131. * A single, global generation is needed because attach_task() could
  132. * reattach a task to a different cpuset, which must not have its
  133. * generation numbers aliased with those of that tasks previous cpuset.
  134. *
  135. * Generations are needed for mems_allowed because one task cannot
  136. * modify anothers memory placement. So we must enable every task,
  137. * on every visit to __alloc_pages(), to efficiently check whether
  138. * its current->cpuset->mems_allowed has changed, requiring an update
  139. * of its current->mems_allowed.
  140. *
  141. * Since cpuset_mems_generation is guarded by manage_mutex,
  142. * there is no need to mark it atomic.
  143. */
  144. static int cpuset_mems_generation;
  145. static struct cpuset top_cpuset = {
  146. .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
  147. .cpus_allowed = CPU_MASK_ALL,
  148. .mems_allowed = NODE_MASK_ALL,
  149. };
  150. /*
  151. * We have two global cpuset mutexes below. They can nest.
  152. * It is ok to first take manage_mutex, then nest callback_mutex. We also
  153. * require taking task_lock() when dereferencing a tasks cpuset pointer.
  154. * See "The task_lock() exception", at the end of this comment.
  155. *
  156. * A task must hold both mutexes to modify cpusets. If a task
  157. * holds manage_mutex, then it blocks others wanting that mutex,
  158. * ensuring that it is the only task able to also acquire callback_mutex
  159. * and be able to modify cpusets. It can perform various checks on
  160. * the cpuset structure first, knowing nothing will change. It can
  161. * also allocate memory while just holding manage_mutex. While it is
  162. * performing these checks, various callback routines can briefly
  163. * acquire callback_mutex to query cpusets. Once it is ready to make
  164. * the changes, it takes callback_mutex, blocking everyone else.
  165. *
  166. * Calls to the kernel memory allocator can not be made while holding
  167. * callback_mutex, as that would risk double tripping on callback_mutex
  168. * from one of the callbacks into the cpuset code from within
  169. * __alloc_pages().
  170. *
  171. * If a task is only holding callback_mutex, then it has read-only
  172. * access to cpusets.
  173. *
  174. * The task_struct fields mems_allowed and mems_generation may only
  175. * be accessed in the context of that task, so require no locks.
  176. *
  177. * Any task can increment and decrement the count field without lock.
  178. * So in general, code holding manage_mutex or callback_mutex can't rely
  179. * on the count field not changing. However, if the count goes to
  180. * zero, then only attach_task(), which holds both mutexes, can
  181. * increment it again. Because a count of zero means that no tasks
  182. * are currently attached, therefore there is no way a task attached
  183. * to that cpuset can fork (the other way to increment the count).
  184. * So code holding manage_mutex or callback_mutex can safely assume that
  185. * if the count is zero, it will stay zero. Similarly, if a task
  186. * holds manage_mutex or callback_mutex on a cpuset with zero count, it
  187. * knows that the cpuset won't be removed, as cpuset_rmdir() needs
  188. * both of those mutexes.
  189. *
  190. * The cpuset_common_file_write handler for operations that modify
  191. * the cpuset hierarchy holds manage_mutex across the entire operation,
  192. * single threading all such cpuset modifications across the system.
  193. *
  194. * The cpuset_common_file_read() handlers only hold callback_mutex across
  195. * small pieces of code, such as when reading out possibly multi-word
  196. * cpumasks and nodemasks.
  197. *
  198. * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
  199. * (usually) take either mutex. These are the two most performance
  200. * critical pieces of code here. The exception occurs on cpuset_exit(),
  201. * when a task in a notify_on_release cpuset exits. Then manage_mutex
  202. * is taken, and if the cpuset count is zero, a usermode call made
  203. * to /sbin/cpuset_release_agent with the name of the cpuset (path
  204. * relative to the root of cpuset file system) as the argument.
  205. *
  206. * A cpuset can only be deleted if both its 'count' of using tasks
  207. * is zero, and its list of 'children' cpusets is empty. Since all
  208. * tasks in the system use _some_ cpuset, and since there is always at
  209. * least one task in the system (init), therefore, top_cpuset
  210. * always has either children cpusets and/or using tasks. So we don't
  211. * need a special hack to ensure that top_cpuset cannot be deleted.
  212. *
  213. * The above "Tale of Two Semaphores" would be complete, but for:
  214. *
  215. * The task_lock() exception
  216. *
  217. * The need for this exception arises from the action of attach_task(),
  218. * which overwrites one tasks cpuset pointer with another. It does
  219. * so using both mutexes, however there are several performance
  220. * critical places that need to reference task->cpuset without the
  221. * expense of grabbing a system global mutex. Therefore except as
  222. * noted below, when dereferencing or, as in attach_task(), modifying
  223. * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
  224. * (task->alloc_lock) already in the task_struct routinely used for
  225. * such matters.
  226. *
  227. * P.S. One more locking exception. RCU is used to guard the
  228. * update of a tasks cpuset pointer by attach_task() and the
  229. * access of task->cpuset->mems_generation via that pointer in
  230. * the routine cpuset_update_task_memory_state().
  231. */
  232. static DEFINE_MUTEX(callback_mutex);
  233. /* This is ugly, but preserves the userspace API for existing cpuset
  234. * users. If someone tries to mount the "cpuset" filesystem, we
  235. * silently switch it to mount "cgroup" instead */
  236. static int cpuset_get_sb(struct file_system_type *fs_type,
  237. int flags, const char *unused_dev_name,
  238. void *data, struct vfsmount *mnt)
  239. {
  240. struct file_system_type *cgroup_fs = get_fs_type("cgroup");
  241. int ret = -ENODEV;
  242. if (cgroup_fs) {
  243. char mountopts[] =
  244. "cpuset,noprefix,"
  245. "release_agent=/sbin/cpuset_release_agent";
  246. ret = cgroup_fs->get_sb(cgroup_fs, flags,
  247. unused_dev_name, mountopts, mnt);
  248. put_filesystem(cgroup_fs);
  249. }
  250. return ret;
  251. }
  252. static struct file_system_type cpuset_fs_type = {
  253. .name = "cpuset",
  254. .get_sb = cpuset_get_sb,
  255. };
  256. /*
  257. * Return in *pmask the portion of a cpusets's cpus_allowed that
  258. * are online. If none are online, walk up the cpuset hierarchy
  259. * until we find one that does have some online cpus. If we get
  260. * all the way to the top and still haven't found any online cpus,
  261. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  262. * task, return cpu_online_map.
  263. *
  264. * One way or another, we guarantee to return some non-empty subset
  265. * of cpu_online_map.
  266. *
  267. * Call with callback_mutex held.
  268. */
  269. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  270. {
  271. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  272. cs = cs->parent;
  273. if (cs)
  274. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  275. else
  276. *pmask = cpu_online_map;
  277. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  278. }
  279. /*
  280. * Return in *pmask the portion of a cpusets's mems_allowed that
  281. * are online, with memory. If none are online with memory, walk
  282. * up the cpuset hierarchy until we find one that does have some
  283. * online mems. If we get all the way to the top and still haven't
  284. * found any online mems, return node_states[N_HIGH_MEMORY].
  285. *
  286. * One way or another, we guarantee to return some non-empty subset
  287. * of node_states[N_HIGH_MEMORY].
  288. *
  289. * Call with callback_mutex held.
  290. */
  291. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  292. {
  293. while (cs && !nodes_intersects(cs->mems_allowed,
  294. node_states[N_HIGH_MEMORY]))
  295. cs = cs->parent;
  296. if (cs)
  297. nodes_and(*pmask, cs->mems_allowed,
  298. node_states[N_HIGH_MEMORY]);
  299. else
  300. *pmask = node_states[N_HIGH_MEMORY];
  301. BUG_ON(!nodes_intersects(*pmask, node_states[N_HIGH_MEMORY]));
  302. }
  303. /**
  304. * cpuset_update_task_memory_state - update task memory placement
  305. *
  306. * If the current tasks cpusets mems_allowed changed behind our
  307. * backs, update current->mems_allowed, mems_generation and task NUMA
  308. * mempolicy to the new value.
  309. *
  310. * Task mempolicy is updated by rebinding it relative to the
  311. * current->cpuset if a task has its memory placement changed.
  312. * Do not call this routine if in_interrupt().
  313. *
  314. * Call without callback_mutex or task_lock() held. May be
  315. * called with or without manage_mutex held. Thanks in part to
  316. * 'the_top_cpuset_hack', the tasks cpuset pointer will never
  317. * be NULL. This routine also might acquire callback_mutex and
  318. * current->mm->mmap_sem during call.
  319. *
  320. * Reading current->cpuset->mems_generation doesn't need task_lock
  321. * to guard the current->cpuset derefence, because it is guarded
  322. * from concurrent freeing of current->cpuset by attach_task(),
  323. * using RCU.
  324. *
  325. * The rcu_dereference() is technically probably not needed,
  326. * as I don't actually mind if I see a new cpuset pointer but
  327. * an old value of mems_generation. However this really only
  328. * matters on alpha systems using cpusets heavily. If I dropped
  329. * that rcu_dereference(), it would save them a memory barrier.
  330. * For all other arch's, rcu_dereference is a no-op anyway, and for
  331. * alpha systems not using cpusets, another planned optimization,
  332. * avoiding the rcu critical section for tasks in the root cpuset
  333. * which is statically allocated, so can't vanish, will make this
  334. * irrelevant. Better to use RCU as intended, than to engage in
  335. * some cute trick to save a memory barrier that is impossible to
  336. * test, for alpha systems using cpusets heavily, which might not
  337. * even exist.
  338. *
  339. * This routine is needed to update the per-task mems_allowed data,
  340. * within the tasks context, when it is trying to allocate memory
  341. * (in various mm/mempolicy.c routines) and notices that some other
  342. * task has been modifying its cpuset.
  343. */
  344. void cpuset_update_task_memory_state(void)
  345. {
  346. int my_cpusets_mem_gen;
  347. struct task_struct *tsk = current;
  348. struct cpuset *cs;
  349. if (task_cs(tsk) == &top_cpuset) {
  350. /* Don't need rcu for top_cpuset. It's never freed. */
  351. my_cpusets_mem_gen = top_cpuset.mems_generation;
  352. } else {
  353. rcu_read_lock();
  354. my_cpusets_mem_gen = task_cs(current)->mems_generation;
  355. rcu_read_unlock();
  356. }
  357. if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
  358. mutex_lock(&callback_mutex);
  359. task_lock(tsk);
  360. cs = task_cs(tsk); /* Maybe changed when task not locked */
  361. guarantee_online_mems(cs, &tsk->mems_allowed);
  362. tsk->cpuset_mems_generation = cs->mems_generation;
  363. if (is_spread_page(cs))
  364. tsk->flags |= PF_SPREAD_PAGE;
  365. else
  366. tsk->flags &= ~PF_SPREAD_PAGE;
  367. if (is_spread_slab(cs))
  368. tsk->flags |= PF_SPREAD_SLAB;
  369. else
  370. tsk->flags &= ~PF_SPREAD_SLAB;
  371. task_unlock(tsk);
  372. mutex_unlock(&callback_mutex);
  373. mpol_rebind_task(tsk, &tsk->mems_allowed);
  374. }
  375. }
  376. /*
  377. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  378. *
  379. * One cpuset is a subset of another if all its allowed CPUs and
  380. * Memory Nodes are a subset of the other, and its exclusive flags
  381. * are only set if the other's are set. Call holding manage_mutex.
  382. */
  383. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  384. {
  385. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  386. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  387. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  388. is_mem_exclusive(p) <= is_mem_exclusive(q);
  389. }
  390. /*
  391. * validate_change() - Used to validate that any proposed cpuset change
  392. * follows the structural rules for cpusets.
  393. *
  394. * If we replaced the flag and mask values of the current cpuset
  395. * (cur) with those values in the trial cpuset (trial), would
  396. * our various subset and exclusive rules still be valid? Presumes
  397. * manage_mutex held.
  398. *
  399. * 'cur' is the address of an actual, in-use cpuset. Operations
  400. * such as list traversal that depend on the actual address of the
  401. * cpuset in the list must use cur below, not trial.
  402. *
  403. * 'trial' is the address of bulk structure copy of cur, with
  404. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  405. * or flags changed to new, trial values.
  406. *
  407. * Return 0 if valid, -errno if not.
  408. */
  409. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  410. {
  411. struct cgroup *cont;
  412. struct cpuset *c, *par;
  413. /* Each of our child cpusets must be a subset of us */
  414. list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
  415. if (!is_cpuset_subset(cgroup_cs(cont), trial))
  416. return -EBUSY;
  417. }
  418. /* Remaining checks don't apply to root cpuset */
  419. if (cur == &top_cpuset)
  420. return 0;
  421. par = cur->parent;
  422. /* We must be a subset of our parent cpuset */
  423. if (!is_cpuset_subset(trial, par))
  424. return -EACCES;
  425. /* If either I or some sibling (!= me) is exclusive, we can't overlap */
  426. list_for_each_entry(cont, &par->css.cgroup->children, sibling) {
  427. c = cgroup_cs(cont);
  428. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  429. c != cur &&
  430. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  431. return -EINVAL;
  432. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  433. c != cur &&
  434. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. /*
  440. * Call with manage_mutex held. May take callback_mutex during call.
  441. */
  442. static int update_cpumask(struct cpuset *cs, char *buf)
  443. {
  444. struct cpuset trialcs;
  445. int retval;
  446. /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
  447. if (cs == &top_cpuset)
  448. return -EACCES;
  449. trialcs = *cs;
  450. /*
  451. * We allow a cpuset's cpus_allowed to be empty; if it has attached
  452. * tasks, we'll catch it later when we validate the change and return
  453. * -ENOSPC.
  454. */
  455. if (!buf[0] || (buf[0] == '\n' && !buf[1])) {
  456. cpus_clear(trialcs.cpus_allowed);
  457. } else {
  458. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  459. if (retval < 0)
  460. return retval;
  461. }
  462. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  463. /* cpus_allowed cannot be empty for a cpuset with attached tasks. */
  464. if (cgroup_task_count(cs->css.cgroup) &&
  465. cpus_empty(trialcs.cpus_allowed))
  466. return -ENOSPC;
  467. retval = validate_change(cs, &trialcs);
  468. if (retval < 0)
  469. return retval;
  470. mutex_lock(&callback_mutex);
  471. cs->cpus_allowed = trialcs.cpus_allowed;
  472. mutex_unlock(&callback_mutex);
  473. return 0;
  474. }
  475. /*
  476. * cpuset_migrate_mm
  477. *
  478. * Migrate memory region from one set of nodes to another.
  479. *
  480. * Temporarilly set tasks mems_allowed to target nodes of migration,
  481. * so that the migration code can allocate pages on these nodes.
  482. *
  483. * Call holding manage_mutex, so our current->cpuset won't change
  484. * during this call, as manage_mutex holds off any attach_task()
  485. * calls. Therefore we don't need to take task_lock around the
  486. * call to guarantee_online_mems(), as we know no one is changing
  487. * our tasks cpuset.
  488. *
  489. * Hold callback_mutex around the two modifications of our tasks
  490. * mems_allowed to synchronize with cpuset_mems_allowed().
  491. *
  492. * While the mm_struct we are migrating is typically from some
  493. * other task, the task_struct mems_allowed that we are hacking
  494. * is for our current task, which must allocate new pages for that
  495. * migrating memory region.
  496. *
  497. * We call cpuset_update_task_memory_state() before hacking
  498. * our tasks mems_allowed, so that we are assured of being in
  499. * sync with our tasks cpuset, and in particular, callbacks to
  500. * cpuset_update_task_memory_state() from nested page allocations
  501. * won't see any mismatch of our cpuset and task mems_generation
  502. * values, so won't overwrite our hacked tasks mems_allowed
  503. * nodemask.
  504. */
  505. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  506. const nodemask_t *to)
  507. {
  508. struct task_struct *tsk = current;
  509. cpuset_update_task_memory_state();
  510. mutex_lock(&callback_mutex);
  511. tsk->mems_allowed = *to;
  512. mutex_unlock(&callback_mutex);
  513. do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
  514. mutex_lock(&callback_mutex);
  515. guarantee_online_mems(task_cs(tsk),&tsk->mems_allowed);
  516. mutex_unlock(&callback_mutex);
  517. }
  518. /*
  519. * Handle user request to change the 'mems' memory placement
  520. * of a cpuset. Needs to validate the request, update the
  521. * cpusets mems_allowed and mems_generation, and for each
  522. * task in the cpuset, rebind any vma mempolicies and if
  523. * the cpuset is marked 'memory_migrate', migrate the tasks
  524. * pages to the new memory.
  525. *
  526. * Call with manage_mutex held. May take callback_mutex during call.
  527. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  528. * lock each such tasks mm->mmap_sem, scan its vma's and rebind
  529. * their mempolicies to the cpusets new mems_allowed.
  530. */
  531. static void *cpuset_being_rebound;
  532. static int update_nodemask(struct cpuset *cs, char *buf)
  533. {
  534. struct cpuset trialcs;
  535. nodemask_t oldmem;
  536. struct task_struct *p;
  537. struct mm_struct **mmarray;
  538. int i, n, ntasks;
  539. int migrate;
  540. int fudge;
  541. int retval;
  542. struct cgroup_iter it;
  543. /*
  544. * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
  545. * it's read-only
  546. */
  547. if (cs == &top_cpuset)
  548. return -EACCES;
  549. trialcs = *cs;
  550. /*
  551. * We allow a cpuset's mems_allowed to be empty; if it has attached
  552. * tasks, we'll catch it later when we validate the change and return
  553. * -ENOSPC.
  554. */
  555. if (!buf[0] || (buf[0] == '\n' && !buf[1])) {
  556. nodes_clear(trialcs.mems_allowed);
  557. } else {
  558. retval = nodelist_parse(buf, trialcs.mems_allowed);
  559. if (retval < 0)
  560. goto done;
  561. if (!nodes_intersects(trialcs.mems_allowed,
  562. node_states[N_HIGH_MEMORY])) {
  563. /*
  564. * error if only memoryless nodes specified.
  565. */
  566. retval = -ENOSPC;
  567. goto done;
  568. }
  569. }
  570. /*
  571. * Exclude memoryless nodes. We know that trialcs.mems_allowed
  572. * contains at least one node with memory.
  573. */
  574. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed,
  575. node_states[N_HIGH_MEMORY]);
  576. oldmem = cs->mems_allowed;
  577. if (nodes_equal(oldmem, trialcs.mems_allowed)) {
  578. retval = 0; /* Too easy - nothing to do */
  579. goto done;
  580. }
  581. /* mems_allowed cannot be empty for a cpuset with attached tasks. */
  582. if (cgroup_task_count(cs->css.cgroup) &&
  583. nodes_empty(trialcs.mems_allowed)) {
  584. retval = -ENOSPC;
  585. goto done;
  586. }
  587. retval = validate_change(cs, &trialcs);
  588. if (retval < 0)
  589. goto done;
  590. mutex_lock(&callback_mutex);
  591. cs->mems_allowed = trialcs.mems_allowed;
  592. cs->mems_generation = cpuset_mems_generation++;
  593. mutex_unlock(&callback_mutex);
  594. cpuset_being_rebound = cs; /* causes mpol_copy() rebind */
  595. fudge = 10; /* spare mmarray[] slots */
  596. fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
  597. retval = -ENOMEM;
  598. /*
  599. * Allocate mmarray[] to hold mm reference for each task
  600. * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
  601. * tasklist_lock. We could use GFP_ATOMIC, but with a
  602. * few more lines of code, we can retry until we get a big
  603. * enough mmarray[] w/o using GFP_ATOMIC.
  604. */
  605. while (1) {
  606. ntasks = cgroup_task_count(cs->css.cgroup); /* guess */
  607. ntasks += fudge;
  608. mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
  609. if (!mmarray)
  610. goto done;
  611. read_lock(&tasklist_lock); /* block fork */
  612. if (cgroup_task_count(cs->css.cgroup) <= ntasks)
  613. break; /* got enough */
  614. read_unlock(&tasklist_lock); /* try again */
  615. kfree(mmarray);
  616. }
  617. n = 0;
  618. /* Load up mmarray[] with mm reference for each task in cpuset. */
  619. cgroup_iter_start(cs->css.cgroup, &it);
  620. while ((p = cgroup_iter_next(cs->css.cgroup, &it))) {
  621. struct mm_struct *mm;
  622. if (n >= ntasks) {
  623. printk(KERN_WARNING
  624. "Cpuset mempolicy rebind incomplete.\n");
  625. break;
  626. }
  627. mm = get_task_mm(p);
  628. if (!mm)
  629. continue;
  630. mmarray[n++] = mm;
  631. }
  632. cgroup_iter_end(cs->css.cgroup, &it);
  633. read_unlock(&tasklist_lock);
  634. /*
  635. * Now that we've dropped the tasklist spinlock, we can
  636. * rebind the vma mempolicies of each mm in mmarray[] to their
  637. * new cpuset, and release that mm. The mpol_rebind_mm()
  638. * call takes mmap_sem, which we couldn't take while holding
  639. * tasklist_lock. Forks can happen again now - the mpol_copy()
  640. * cpuset_being_rebound check will catch such forks, and rebind
  641. * their vma mempolicies too. Because we still hold the global
  642. * cpuset manage_mutex, we know that no other rebind effort will
  643. * be contending for the global variable cpuset_being_rebound.
  644. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  645. * is idempotent. Also migrate pages in each mm to new nodes.
  646. */
  647. migrate = is_memory_migrate(cs);
  648. for (i = 0; i < n; i++) {
  649. struct mm_struct *mm = mmarray[i];
  650. mpol_rebind_mm(mm, &cs->mems_allowed);
  651. if (migrate)
  652. cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
  653. mmput(mm);
  654. }
  655. /* We're done rebinding vma's to this cpusets new mems_allowed. */
  656. kfree(mmarray);
  657. cpuset_being_rebound = NULL;
  658. retval = 0;
  659. done:
  660. return retval;
  661. }
  662. int current_cpuset_is_being_rebound(void)
  663. {
  664. return task_cs(current) == cpuset_being_rebound;
  665. }
  666. /*
  667. * Call with manage_mutex held.
  668. */
  669. static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
  670. {
  671. if (simple_strtoul(buf, NULL, 10) != 0)
  672. cpuset_memory_pressure_enabled = 1;
  673. else
  674. cpuset_memory_pressure_enabled = 0;
  675. return 0;
  676. }
  677. /*
  678. * update_flag - read a 0 or a 1 in a file and update associated flag
  679. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  680. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
  681. * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
  682. * cs: the cpuset to update
  683. * buf: the buffer where we read the 0 or 1
  684. *
  685. * Call with manage_mutex held.
  686. */
  687. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  688. {
  689. int turning_on;
  690. struct cpuset trialcs;
  691. int err;
  692. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  693. trialcs = *cs;
  694. if (turning_on)
  695. set_bit(bit, &trialcs.flags);
  696. else
  697. clear_bit(bit, &trialcs.flags);
  698. err = validate_change(cs, &trialcs);
  699. if (err < 0)
  700. return err;
  701. mutex_lock(&callback_mutex);
  702. cs->flags = trialcs.flags;
  703. mutex_unlock(&callback_mutex);
  704. return 0;
  705. }
  706. /*
  707. * Frequency meter - How fast is some event occurring?
  708. *
  709. * These routines manage a digitally filtered, constant time based,
  710. * event frequency meter. There are four routines:
  711. * fmeter_init() - initialize a frequency meter.
  712. * fmeter_markevent() - called each time the event happens.
  713. * fmeter_getrate() - returns the recent rate of such events.
  714. * fmeter_update() - internal routine used to update fmeter.
  715. *
  716. * A common data structure is passed to each of these routines,
  717. * which is used to keep track of the state required to manage the
  718. * frequency meter and its digital filter.
  719. *
  720. * The filter works on the number of events marked per unit time.
  721. * The filter is single-pole low-pass recursive (IIR). The time unit
  722. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  723. * simulate 3 decimal digits of precision (multiplied by 1000).
  724. *
  725. * With an FM_COEF of 933, and a time base of 1 second, the filter
  726. * has a half-life of 10 seconds, meaning that if the events quit
  727. * happening, then the rate returned from the fmeter_getrate()
  728. * will be cut in half each 10 seconds, until it converges to zero.
  729. *
  730. * It is not worth doing a real infinitely recursive filter. If more
  731. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  732. * just compute FM_MAXTICKS ticks worth, by which point the level
  733. * will be stable.
  734. *
  735. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  736. * arithmetic overflow in the fmeter_update() routine.
  737. *
  738. * Given the simple 32 bit integer arithmetic used, this meter works
  739. * best for reporting rates between one per millisecond (msec) and
  740. * one per 32 (approx) seconds. At constant rates faster than one
  741. * per msec it maxes out at values just under 1,000,000. At constant
  742. * rates between one per msec, and one per second it will stabilize
  743. * to a value N*1000, where N is the rate of events per second.
  744. * At constant rates between one per second and one per 32 seconds,
  745. * it will be choppy, moving up on the seconds that have an event,
  746. * and then decaying until the next event. At rates slower than
  747. * about one in 32 seconds, it decays all the way back to zero between
  748. * each event.
  749. */
  750. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  751. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  752. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  753. #define FM_SCALE 1000 /* faux fixed point scale */
  754. /* Initialize a frequency meter */
  755. static void fmeter_init(struct fmeter *fmp)
  756. {
  757. fmp->cnt = 0;
  758. fmp->val = 0;
  759. fmp->time = 0;
  760. spin_lock_init(&fmp->lock);
  761. }
  762. /* Internal meter update - process cnt events and update value */
  763. static void fmeter_update(struct fmeter *fmp)
  764. {
  765. time_t now = get_seconds();
  766. time_t ticks = now - fmp->time;
  767. if (ticks == 0)
  768. return;
  769. ticks = min(FM_MAXTICKS, ticks);
  770. while (ticks-- > 0)
  771. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  772. fmp->time = now;
  773. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  774. fmp->cnt = 0;
  775. }
  776. /* Process any previous ticks, then bump cnt by one (times scale). */
  777. static void fmeter_markevent(struct fmeter *fmp)
  778. {
  779. spin_lock(&fmp->lock);
  780. fmeter_update(fmp);
  781. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  782. spin_unlock(&fmp->lock);
  783. }
  784. /* Process any previous ticks, then return current value. */
  785. static int fmeter_getrate(struct fmeter *fmp)
  786. {
  787. int val;
  788. spin_lock(&fmp->lock);
  789. fmeter_update(fmp);
  790. val = fmp->val;
  791. spin_unlock(&fmp->lock);
  792. return val;
  793. }
  794. static int cpuset_can_attach(struct cgroup_subsys *ss,
  795. struct cgroup *cont, struct task_struct *tsk)
  796. {
  797. struct cpuset *cs = cgroup_cs(cont);
  798. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  799. return -ENOSPC;
  800. return security_task_setscheduler(tsk, 0, NULL);
  801. }
  802. static void cpuset_attach(struct cgroup_subsys *ss,
  803. struct cgroup *cont, struct cgroup *oldcont,
  804. struct task_struct *tsk)
  805. {
  806. cpumask_t cpus;
  807. nodemask_t from, to;
  808. struct mm_struct *mm;
  809. struct cpuset *cs = cgroup_cs(cont);
  810. struct cpuset *oldcs = cgroup_cs(oldcont);
  811. mutex_lock(&callback_mutex);
  812. guarantee_online_cpus(cs, &cpus);
  813. set_cpus_allowed(tsk, cpus);
  814. mutex_unlock(&callback_mutex);
  815. from = oldcs->mems_allowed;
  816. to = cs->mems_allowed;
  817. mm = get_task_mm(tsk);
  818. if (mm) {
  819. mpol_rebind_mm(mm, &to);
  820. if (is_memory_migrate(cs))
  821. cpuset_migrate_mm(mm, &from, &to);
  822. mmput(mm);
  823. }
  824. }
  825. /* The various types of files and directories in a cpuset file system */
  826. typedef enum {
  827. FILE_MEMORY_MIGRATE,
  828. FILE_CPULIST,
  829. FILE_MEMLIST,
  830. FILE_CPU_EXCLUSIVE,
  831. FILE_MEM_EXCLUSIVE,
  832. FILE_MEMORY_PRESSURE_ENABLED,
  833. FILE_MEMORY_PRESSURE,
  834. FILE_SPREAD_PAGE,
  835. FILE_SPREAD_SLAB,
  836. } cpuset_filetype_t;
  837. static ssize_t cpuset_common_file_write(struct cgroup *cont,
  838. struct cftype *cft,
  839. struct file *file,
  840. const char __user *userbuf,
  841. size_t nbytes, loff_t *unused_ppos)
  842. {
  843. struct cpuset *cs = cgroup_cs(cont);
  844. cpuset_filetype_t type = cft->private;
  845. char *buffer;
  846. int retval = 0;
  847. /* Crude upper limit on largest legitimate cpulist user might write. */
  848. if (nbytes > 100 + 6 * max(NR_CPUS, MAX_NUMNODES))
  849. return -E2BIG;
  850. /* +1 for nul-terminator */
  851. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  852. return -ENOMEM;
  853. if (copy_from_user(buffer, userbuf, nbytes)) {
  854. retval = -EFAULT;
  855. goto out1;
  856. }
  857. buffer[nbytes] = 0; /* nul-terminate */
  858. cgroup_lock();
  859. if (cgroup_is_removed(cont)) {
  860. retval = -ENODEV;
  861. goto out2;
  862. }
  863. switch (type) {
  864. case FILE_CPULIST:
  865. retval = update_cpumask(cs, buffer);
  866. break;
  867. case FILE_MEMLIST:
  868. retval = update_nodemask(cs, buffer);
  869. break;
  870. case FILE_CPU_EXCLUSIVE:
  871. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  872. break;
  873. case FILE_MEM_EXCLUSIVE:
  874. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  875. break;
  876. case FILE_MEMORY_MIGRATE:
  877. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  878. break;
  879. case FILE_MEMORY_PRESSURE_ENABLED:
  880. retval = update_memory_pressure_enabled(cs, buffer);
  881. break;
  882. case FILE_MEMORY_PRESSURE:
  883. retval = -EACCES;
  884. break;
  885. case FILE_SPREAD_PAGE:
  886. retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
  887. cs->mems_generation = cpuset_mems_generation++;
  888. break;
  889. case FILE_SPREAD_SLAB:
  890. retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
  891. cs->mems_generation = cpuset_mems_generation++;
  892. break;
  893. default:
  894. retval = -EINVAL;
  895. goto out2;
  896. }
  897. if (retval == 0)
  898. retval = nbytes;
  899. out2:
  900. cgroup_unlock();
  901. out1:
  902. kfree(buffer);
  903. return retval;
  904. }
  905. /*
  906. * These ascii lists should be read in a single call, by using a user
  907. * buffer large enough to hold the entire map. If read in smaller
  908. * chunks, there is no guarantee of atomicity. Since the display format
  909. * used, list of ranges of sequential numbers, is variable length,
  910. * and since these maps can change value dynamically, one could read
  911. * gibberish by doing partial reads while a list was changing.
  912. * A single large read to a buffer that crosses a page boundary is
  913. * ok, because the result being copied to user land is not recomputed
  914. * across a page fault.
  915. */
  916. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  917. {
  918. cpumask_t mask;
  919. mutex_lock(&callback_mutex);
  920. mask = cs->cpus_allowed;
  921. mutex_unlock(&callback_mutex);
  922. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  923. }
  924. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  925. {
  926. nodemask_t mask;
  927. mutex_lock(&callback_mutex);
  928. mask = cs->mems_allowed;
  929. mutex_unlock(&callback_mutex);
  930. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  931. }
  932. static ssize_t cpuset_common_file_read(struct cgroup *cont,
  933. struct cftype *cft,
  934. struct file *file,
  935. char __user *buf,
  936. size_t nbytes, loff_t *ppos)
  937. {
  938. struct cpuset *cs = cgroup_cs(cont);
  939. cpuset_filetype_t type = cft->private;
  940. char *page;
  941. ssize_t retval = 0;
  942. char *s;
  943. if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
  944. return -ENOMEM;
  945. s = page;
  946. switch (type) {
  947. case FILE_CPULIST:
  948. s += cpuset_sprintf_cpulist(s, cs);
  949. break;
  950. case FILE_MEMLIST:
  951. s += cpuset_sprintf_memlist(s, cs);
  952. break;
  953. case FILE_CPU_EXCLUSIVE:
  954. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  955. break;
  956. case FILE_MEM_EXCLUSIVE:
  957. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  958. break;
  959. case FILE_MEMORY_MIGRATE:
  960. *s++ = is_memory_migrate(cs) ? '1' : '0';
  961. break;
  962. case FILE_MEMORY_PRESSURE_ENABLED:
  963. *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
  964. break;
  965. case FILE_MEMORY_PRESSURE:
  966. s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
  967. break;
  968. case FILE_SPREAD_PAGE:
  969. *s++ = is_spread_page(cs) ? '1' : '0';
  970. break;
  971. case FILE_SPREAD_SLAB:
  972. *s++ = is_spread_slab(cs) ? '1' : '0';
  973. break;
  974. default:
  975. retval = -EINVAL;
  976. goto out;
  977. }
  978. *s++ = '\n';
  979. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  980. out:
  981. free_page((unsigned long)page);
  982. return retval;
  983. }
  984. /*
  985. * for the common functions, 'private' gives the type of file
  986. */
  987. static struct cftype cft_cpus = {
  988. .name = "cpus",
  989. .read = cpuset_common_file_read,
  990. .write = cpuset_common_file_write,
  991. .private = FILE_CPULIST,
  992. };
  993. static struct cftype cft_mems = {
  994. .name = "mems",
  995. .read = cpuset_common_file_read,
  996. .write = cpuset_common_file_write,
  997. .private = FILE_MEMLIST,
  998. };
  999. static struct cftype cft_cpu_exclusive = {
  1000. .name = "cpu_exclusive",
  1001. .read = cpuset_common_file_read,
  1002. .write = cpuset_common_file_write,
  1003. .private = FILE_CPU_EXCLUSIVE,
  1004. };
  1005. static struct cftype cft_mem_exclusive = {
  1006. .name = "mem_exclusive",
  1007. .read = cpuset_common_file_read,
  1008. .write = cpuset_common_file_write,
  1009. .private = FILE_MEM_EXCLUSIVE,
  1010. };
  1011. static struct cftype cft_memory_migrate = {
  1012. .name = "memory_migrate",
  1013. .read = cpuset_common_file_read,
  1014. .write = cpuset_common_file_write,
  1015. .private = FILE_MEMORY_MIGRATE,
  1016. };
  1017. static struct cftype cft_memory_pressure_enabled = {
  1018. .name = "memory_pressure_enabled",
  1019. .read = cpuset_common_file_read,
  1020. .write = cpuset_common_file_write,
  1021. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1022. };
  1023. static struct cftype cft_memory_pressure = {
  1024. .name = "memory_pressure",
  1025. .read = cpuset_common_file_read,
  1026. .write = cpuset_common_file_write,
  1027. .private = FILE_MEMORY_PRESSURE,
  1028. };
  1029. static struct cftype cft_spread_page = {
  1030. .name = "memory_spread_page",
  1031. .read = cpuset_common_file_read,
  1032. .write = cpuset_common_file_write,
  1033. .private = FILE_SPREAD_PAGE,
  1034. };
  1035. static struct cftype cft_spread_slab = {
  1036. .name = "memory_spread_slab",
  1037. .read = cpuset_common_file_read,
  1038. .write = cpuset_common_file_write,
  1039. .private = FILE_SPREAD_SLAB,
  1040. };
  1041. static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  1042. {
  1043. int err;
  1044. if ((err = cgroup_add_file(cont, ss, &cft_cpus)) < 0)
  1045. return err;
  1046. if ((err = cgroup_add_file(cont, ss, &cft_mems)) < 0)
  1047. return err;
  1048. if ((err = cgroup_add_file(cont, ss, &cft_cpu_exclusive)) < 0)
  1049. return err;
  1050. if ((err = cgroup_add_file(cont, ss, &cft_mem_exclusive)) < 0)
  1051. return err;
  1052. if ((err = cgroup_add_file(cont, ss, &cft_memory_migrate)) < 0)
  1053. return err;
  1054. if ((err = cgroup_add_file(cont, ss, &cft_memory_pressure)) < 0)
  1055. return err;
  1056. if ((err = cgroup_add_file(cont, ss, &cft_spread_page)) < 0)
  1057. return err;
  1058. if ((err = cgroup_add_file(cont, ss, &cft_spread_slab)) < 0)
  1059. return err;
  1060. /* memory_pressure_enabled is in root cpuset only */
  1061. if (err == 0 && !cont->parent)
  1062. err = cgroup_add_file(cont, ss,
  1063. &cft_memory_pressure_enabled);
  1064. return 0;
  1065. }
  1066. /*
  1067. * post_clone() is called at the end of cgroup_clone().
  1068. * 'cgroup' was just created automatically as a result of
  1069. * a cgroup_clone(), and the current task is about to
  1070. * be moved into 'cgroup'.
  1071. *
  1072. * Currently we refuse to set up the cgroup - thereby
  1073. * refusing the task to be entered, and as a result refusing
  1074. * the sys_unshare() or clone() which initiated it - if any
  1075. * sibling cpusets have exclusive cpus or mem.
  1076. *
  1077. * If this becomes a problem for some users who wish to
  1078. * allow that scenario, then cpuset_post_clone() could be
  1079. * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
  1080. * (and likewise for mems) to the new cgroup.
  1081. */
  1082. static void cpuset_post_clone(struct cgroup_subsys *ss,
  1083. struct cgroup *cgroup)
  1084. {
  1085. struct cgroup *parent, *child;
  1086. struct cpuset *cs, *parent_cs;
  1087. parent = cgroup->parent;
  1088. list_for_each_entry(child, &parent->children, sibling) {
  1089. cs = cgroup_cs(child);
  1090. if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
  1091. return;
  1092. }
  1093. cs = cgroup_cs(cgroup);
  1094. parent_cs = cgroup_cs(parent);
  1095. cs->mems_allowed = parent_cs->mems_allowed;
  1096. cs->cpus_allowed = parent_cs->cpus_allowed;
  1097. return;
  1098. }
  1099. /*
  1100. * cpuset_create - create a cpuset
  1101. * parent: cpuset that will be parent of the new cpuset.
  1102. * name: name of the new cpuset. Will be strcpy'ed.
  1103. * mode: mode to set on new inode
  1104. *
  1105. * Must be called with the mutex on the parent inode held
  1106. */
  1107. static struct cgroup_subsys_state *cpuset_create(
  1108. struct cgroup_subsys *ss,
  1109. struct cgroup *cont)
  1110. {
  1111. struct cpuset *cs;
  1112. struct cpuset *parent;
  1113. if (!cont->parent) {
  1114. /* This is early initialization for the top cgroup */
  1115. top_cpuset.mems_generation = cpuset_mems_generation++;
  1116. return &top_cpuset.css;
  1117. }
  1118. parent = cgroup_cs(cont->parent);
  1119. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1120. if (!cs)
  1121. return ERR_PTR(-ENOMEM);
  1122. cpuset_update_task_memory_state();
  1123. cs->flags = 0;
  1124. if (is_spread_page(parent))
  1125. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1126. if (is_spread_slab(parent))
  1127. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1128. cs->cpus_allowed = CPU_MASK_NONE;
  1129. cs->mems_allowed = NODE_MASK_NONE;
  1130. cs->mems_generation = cpuset_mems_generation++;
  1131. fmeter_init(&cs->fmeter);
  1132. cs->parent = parent;
  1133. number_of_cpusets++;
  1134. return &cs->css ;
  1135. }
  1136. static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  1137. {
  1138. struct cpuset *cs = cgroup_cs(cont);
  1139. cpuset_update_task_memory_state();
  1140. number_of_cpusets--;
  1141. kfree(cs);
  1142. }
  1143. struct cgroup_subsys cpuset_subsys = {
  1144. .name = "cpuset",
  1145. .create = cpuset_create,
  1146. .destroy = cpuset_destroy,
  1147. .can_attach = cpuset_can_attach,
  1148. .attach = cpuset_attach,
  1149. .populate = cpuset_populate,
  1150. .post_clone = cpuset_post_clone,
  1151. .subsys_id = cpuset_subsys_id,
  1152. .early_init = 1,
  1153. };
  1154. /*
  1155. * cpuset_init_early - just enough so that the calls to
  1156. * cpuset_update_task_memory_state() in early init code
  1157. * are harmless.
  1158. */
  1159. int __init cpuset_init_early(void)
  1160. {
  1161. top_cpuset.mems_generation = cpuset_mems_generation++;
  1162. return 0;
  1163. }
  1164. /**
  1165. * cpuset_init - initialize cpusets at system boot
  1166. *
  1167. * Description: Initialize top_cpuset and the cpuset internal file system,
  1168. **/
  1169. int __init cpuset_init(void)
  1170. {
  1171. int err = 0;
  1172. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1173. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1174. fmeter_init(&top_cpuset.fmeter);
  1175. top_cpuset.mems_generation = cpuset_mems_generation++;
  1176. err = register_filesystem(&cpuset_fs_type);
  1177. if (err < 0)
  1178. return err;
  1179. number_of_cpusets = 1;
  1180. return 0;
  1181. }
  1182. /*
  1183. * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs
  1184. * or memory nodes, we need to walk over the cpuset hierarchy,
  1185. * removing that CPU or node from all cpusets. If this removes the
  1186. * last CPU or node from a cpuset, then the guarantee_online_cpus()
  1187. * or guarantee_online_mems() code will use that emptied cpusets
  1188. * parent online CPUs or nodes. Cpusets that were already empty of
  1189. * CPUs or nodes are left empty.
  1190. *
  1191. * This routine is intentionally inefficient in a couple of regards.
  1192. * It will check all cpusets in a subtree even if the top cpuset of
  1193. * the subtree has no offline CPUs or nodes. It checks both CPUs and
  1194. * nodes, even though the caller could have been coded to know that
  1195. * only one of CPUs or nodes needed to be checked on a given call.
  1196. * This was done to minimize text size rather than cpu cycles.
  1197. *
  1198. * Call with both manage_mutex and callback_mutex held.
  1199. *
  1200. * Recursive, on depth of cpuset subtree.
  1201. */
  1202. static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
  1203. {
  1204. struct cgroup *cont;
  1205. struct cpuset *c;
  1206. /* Each of our child cpusets mems must be online */
  1207. list_for_each_entry(cont, &cur->css.cgroup->children, sibling) {
  1208. c = cgroup_cs(cont);
  1209. guarantee_online_cpus_mems_in_subtree(c);
  1210. if (!cpus_empty(c->cpus_allowed))
  1211. guarantee_online_cpus(c, &c->cpus_allowed);
  1212. if (!nodes_empty(c->mems_allowed))
  1213. guarantee_online_mems(c, &c->mems_allowed);
  1214. }
  1215. }
  1216. /*
  1217. * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track
  1218. * cpu_online_map and node_states[N_HIGH_MEMORY]. Force the top cpuset to
  1219. * track what's online after any CPU or memory node hotplug or unplug
  1220. * event.
  1221. *
  1222. * To ensure that we don't remove a CPU or node from the top cpuset
  1223. * that is currently in use by a child cpuset (which would violate
  1224. * the rule that cpusets must be subsets of their parent), we first
  1225. * call the recursive routine guarantee_online_cpus_mems_in_subtree().
  1226. *
  1227. * Since there are two callers of this routine, one for CPU hotplug
  1228. * events and one for memory node hotplug events, we could have coded
  1229. * two separate routines here. We code it as a single common routine
  1230. * in order to minimize text size.
  1231. */
  1232. static void common_cpu_mem_hotplug_unplug(void)
  1233. {
  1234. cgroup_lock();
  1235. mutex_lock(&callback_mutex);
  1236. guarantee_online_cpus_mems_in_subtree(&top_cpuset);
  1237. top_cpuset.cpus_allowed = cpu_online_map;
  1238. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1239. mutex_unlock(&callback_mutex);
  1240. cgroup_unlock();
  1241. }
  1242. /*
  1243. * The top_cpuset tracks what CPUs and Memory Nodes are online,
  1244. * period. This is necessary in order to make cpusets transparent
  1245. * (of no affect) on systems that are actively using CPU hotplug
  1246. * but making no active use of cpusets.
  1247. *
  1248. * This routine ensures that top_cpuset.cpus_allowed tracks
  1249. * cpu_online_map on each CPU hotplug (cpuhp) event.
  1250. */
  1251. static int cpuset_handle_cpuhp(struct notifier_block *nb,
  1252. unsigned long phase, void *cpu)
  1253. {
  1254. if (phase == CPU_DYING || phase == CPU_DYING_FROZEN)
  1255. return NOTIFY_DONE;
  1256. common_cpu_mem_hotplug_unplug();
  1257. return 0;
  1258. }
  1259. #ifdef CONFIG_MEMORY_HOTPLUG
  1260. /*
  1261. * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
  1262. * Call this routine anytime after you change
  1263. * node_states[N_HIGH_MEMORY].
  1264. * See also the previous routine cpuset_handle_cpuhp().
  1265. */
  1266. void cpuset_track_online_nodes(void)
  1267. {
  1268. common_cpu_mem_hotplug_unplug();
  1269. }
  1270. #endif
  1271. /**
  1272. * cpuset_init_smp - initialize cpus_allowed
  1273. *
  1274. * Description: Finish top cpuset after cpu, node maps are initialized
  1275. **/
  1276. void __init cpuset_init_smp(void)
  1277. {
  1278. top_cpuset.cpus_allowed = cpu_online_map;
  1279. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1280. hotcpu_notifier(cpuset_handle_cpuhp, 0);
  1281. }
  1282. /**
  1283. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1284. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1285. *
  1286. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1287. * attached to the specified @tsk. Guaranteed to return some non-empty
  1288. * subset of cpu_online_map, even if this means going outside the
  1289. * tasks cpuset.
  1290. **/
  1291. cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
  1292. {
  1293. cpumask_t mask;
  1294. mutex_lock(&callback_mutex);
  1295. task_lock(tsk);
  1296. guarantee_online_cpus(task_cs(tsk), &mask);
  1297. task_unlock(tsk);
  1298. mutex_unlock(&callback_mutex);
  1299. return mask;
  1300. }
  1301. void cpuset_init_current_mems_allowed(void)
  1302. {
  1303. current->mems_allowed = NODE_MASK_ALL;
  1304. }
  1305. /**
  1306. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  1307. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  1308. *
  1309. * Description: Returns the nodemask_t mems_allowed of the cpuset
  1310. * attached to the specified @tsk. Guaranteed to return some non-empty
  1311. * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
  1312. * tasks cpuset.
  1313. **/
  1314. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  1315. {
  1316. nodemask_t mask;
  1317. mutex_lock(&callback_mutex);
  1318. task_lock(tsk);
  1319. guarantee_online_mems(task_cs(tsk), &mask);
  1320. task_unlock(tsk);
  1321. mutex_unlock(&callback_mutex);
  1322. return mask;
  1323. }
  1324. /**
  1325. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1326. * @zl: the zonelist to be checked
  1327. *
  1328. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1329. */
  1330. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1331. {
  1332. int i;
  1333. for (i = 0; zl->zones[i]; i++) {
  1334. int nid = zone_to_nid(zl->zones[i]);
  1335. if (node_isset(nid, current->mems_allowed))
  1336. return 1;
  1337. }
  1338. return 0;
  1339. }
  1340. /*
  1341. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1342. * ancestor to the specified cpuset. Call holding callback_mutex.
  1343. * If no ancestor is mem_exclusive (an unusual configuration), then
  1344. * returns the root cpuset.
  1345. */
  1346. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1347. {
  1348. while (!is_mem_exclusive(cs) && cs->parent)
  1349. cs = cs->parent;
  1350. return cs;
  1351. }
  1352. /**
  1353. * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node?
  1354. * @z: is this zone on an allowed node?
  1355. * @gfp_mask: memory allocation flags
  1356. *
  1357. * If we're in interrupt, yes, we can always allocate. If
  1358. * __GFP_THISNODE is set, yes, we can always allocate. If zone
  1359. * z's node is in our tasks mems_allowed, yes. If it's not a
  1360. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1361. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1362. * If the task has been OOM killed and has access to memory reserves
  1363. * as specified by the TIF_MEMDIE flag, yes.
  1364. * Otherwise, no.
  1365. *
  1366. * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall()
  1367. * reduces to cpuset_zone_allowed_hardwall(). Otherwise,
  1368. * cpuset_zone_allowed_softwall() might sleep, and might allow a zone
  1369. * from an enclosing cpuset.
  1370. *
  1371. * cpuset_zone_allowed_hardwall() only handles the simpler case of
  1372. * hardwall cpusets, and never sleeps.
  1373. *
  1374. * The __GFP_THISNODE placement logic is really handled elsewhere,
  1375. * by forcibly using a zonelist starting at a specified node, and by
  1376. * (in get_page_from_freelist()) refusing to consider the zones for
  1377. * any node on the zonelist except the first. By the time any such
  1378. * calls get to this routine, we should just shut up and say 'yes'.
  1379. *
  1380. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1381. * and do not allow allocations outside the current tasks cpuset
  1382. * unless the task has been OOM killed as is marked TIF_MEMDIE.
  1383. * GFP_KERNEL allocations are not so marked, so can escape to the
  1384. * nearest enclosing mem_exclusive ancestor cpuset.
  1385. *
  1386. * Scanning up parent cpusets requires callback_mutex. The
  1387. * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
  1388. * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
  1389. * current tasks mems_allowed came up empty on the first pass over
  1390. * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
  1391. * cpuset are short of memory, might require taking the callback_mutex
  1392. * mutex.
  1393. *
  1394. * The first call here from mm/page_alloc:get_page_from_freelist()
  1395. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
  1396. * so no allocation on a node outside the cpuset is allowed (unless
  1397. * in interrupt, of course).
  1398. *
  1399. * The second pass through get_page_from_freelist() doesn't even call
  1400. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  1401. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  1402. * in alloc_flags. That logic and the checks below have the combined
  1403. * affect that:
  1404. * in_interrupt - any node ok (current task context irrelevant)
  1405. * GFP_ATOMIC - any node ok
  1406. * TIF_MEMDIE - any node ok
  1407. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1408. * GFP_USER - only nodes in current tasks mems allowed ok.
  1409. *
  1410. * Rule:
  1411. * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you
  1412. * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
  1413. * the code that might scan up ancestor cpusets and sleep.
  1414. */
  1415. int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask)
  1416. {
  1417. int node; /* node that zone z is on */
  1418. const struct cpuset *cs; /* current cpuset ancestors */
  1419. int allowed; /* is allocation in zone z allowed? */
  1420. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  1421. return 1;
  1422. node = zone_to_nid(z);
  1423. might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
  1424. if (node_isset(node, current->mems_allowed))
  1425. return 1;
  1426. /*
  1427. * Allow tasks that have access to memory reserves because they have
  1428. * been OOM killed to get memory anywhere.
  1429. */
  1430. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  1431. return 1;
  1432. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1433. return 0;
  1434. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1435. return 1;
  1436. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1437. mutex_lock(&callback_mutex);
  1438. task_lock(current);
  1439. cs = nearest_exclusive_ancestor(task_cs(current));
  1440. task_unlock(current);
  1441. allowed = node_isset(node, cs->mems_allowed);
  1442. mutex_unlock(&callback_mutex);
  1443. return allowed;
  1444. }
  1445. /*
  1446. * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node?
  1447. * @z: is this zone on an allowed node?
  1448. * @gfp_mask: memory allocation flags
  1449. *
  1450. * If we're in interrupt, yes, we can always allocate.
  1451. * If __GFP_THISNODE is set, yes, we can always allocate. If zone
  1452. * z's node is in our tasks mems_allowed, yes. If the task has been
  1453. * OOM killed and has access to memory reserves as specified by the
  1454. * TIF_MEMDIE flag, yes. Otherwise, no.
  1455. *
  1456. * The __GFP_THISNODE placement logic is really handled elsewhere,
  1457. * by forcibly using a zonelist starting at a specified node, and by
  1458. * (in get_page_from_freelist()) refusing to consider the zones for
  1459. * any node on the zonelist except the first. By the time any such
  1460. * calls get to this routine, we should just shut up and say 'yes'.
  1461. *
  1462. * Unlike the cpuset_zone_allowed_softwall() variant, above,
  1463. * this variant requires that the zone be in the current tasks
  1464. * mems_allowed or that we're in interrupt. It does not scan up the
  1465. * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
  1466. * It never sleeps.
  1467. */
  1468. int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask)
  1469. {
  1470. int node; /* node that zone z is on */
  1471. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  1472. return 1;
  1473. node = zone_to_nid(z);
  1474. if (node_isset(node, current->mems_allowed))
  1475. return 1;
  1476. /*
  1477. * Allow tasks that have access to memory reserves because they have
  1478. * been OOM killed to get memory anywhere.
  1479. */
  1480. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  1481. return 1;
  1482. return 0;
  1483. }
  1484. /**
  1485. * cpuset_lock - lock out any changes to cpuset structures
  1486. *
  1487. * The out of memory (oom) code needs to mutex_lock cpusets
  1488. * from being changed while it scans the tasklist looking for a
  1489. * task in an overlapping cpuset. Expose callback_mutex via this
  1490. * cpuset_lock() routine, so the oom code can lock it, before
  1491. * locking the task list. The tasklist_lock is a spinlock, so
  1492. * must be taken inside callback_mutex.
  1493. */
  1494. void cpuset_lock(void)
  1495. {
  1496. mutex_lock(&callback_mutex);
  1497. }
  1498. /**
  1499. * cpuset_unlock - release lock on cpuset changes
  1500. *
  1501. * Undo the lock taken in a previous cpuset_lock() call.
  1502. */
  1503. void cpuset_unlock(void)
  1504. {
  1505. mutex_unlock(&callback_mutex);
  1506. }
  1507. /**
  1508. * cpuset_mem_spread_node() - On which node to begin search for a page
  1509. *
  1510. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  1511. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  1512. * and if the memory allocation used cpuset_mem_spread_node()
  1513. * to determine on which node to start looking, as it will for
  1514. * certain page cache or slab cache pages such as used for file
  1515. * system buffers and inode caches, then instead of starting on the
  1516. * local node to look for a free page, rather spread the starting
  1517. * node around the tasks mems_allowed nodes.
  1518. *
  1519. * We don't have to worry about the returned node being offline
  1520. * because "it can't happen", and even if it did, it would be ok.
  1521. *
  1522. * The routines calling guarantee_online_mems() are careful to
  1523. * only set nodes in task->mems_allowed that are online. So it
  1524. * should not be possible for the following code to return an
  1525. * offline node. But if it did, that would be ok, as this routine
  1526. * is not returning the node where the allocation must be, only
  1527. * the node where the search should start. The zonelist passed to
  1528. * __alloc_pages() will include all nodes. If the slab allocator
  1529. * is passed an offline node, it will fall back to the local node.
  1530. * See kmem_cache_alloc_node().
  1531. */
  1532. int cpuset_mem_spread_node(void)
  1533. {
  1534. int node;
  1535. node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
  1536. if (node == MAX_NUMNODES)
  1537. node = first_node(current->mems_allowed);
  1538. current->cpuset_mem_spread_rotor = node;
  1539. return node;
  1540. }
  1541. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  1542. /**
  1543. * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
  1544. * @tsk1: pointer to task_struct of some task.
  1545. * @tsk2: pointer to task_struct of some other task.
  1546. *
  1547. * Description: Return true if @tsk1's mems_allowed intersects the
  1548. * mems_allowed of @tsk2. Used by the OOM killer to determine if
  1549. * one of the task's memory usage might impact the memory available
  1550. * to the other.
  1551. **/
  1552. int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  1553. const struct task_struct *tsk2)
  1554. {
  1555. return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
  1556. }
  1557. /*
  1558. * Collection of memory_pressure is suppressed unless
  1559. * this flag is enabled by writing "1" to the special
  1560. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  1561. */
  1562. int cpuset_memory_pressure_enabled __read_mostly;
  1563. /**
  1564. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  1565. *
  1566. * Keep a running average of the rate of synchronous (direct)
  1567. * page reclaim efforts initiated by tasks in each cpuset.
  1568. *
  1569. * This represents the rate at which some task in the cpuset
  1570. * ran low on memory on all nodes it was allowed to use, and
  1571. * had to enter the kernels page reclaim code in an effort to
  1572. * create more free memory by tossing clean pages or swapping
  1573. * or writing dirty pages.
  1574. *
  1575. * Display to user space in the per-cpuset read-only file
  1576. * "memory_pressure". Value displayed is an integer
  1577. * representing the recent rate of entry into the synchronous
  1578. * (direct) page reclaim by any task attached to the cpuset.
  1579. **/
  1580. void __cpuset_memory_pressure_bump(void)
  1581. {
  1582. task_lock(current);
  1583. fmeter_markevent(&task_cs(current)->fmeter);
  1584. task_unlock(current);
  1585. }
  1586. #ifdef CONFIG_PROC_PID_CPUSET
  1587. /*
  1588. * proc_cpuset_show()
  1589. * - Print tasks cpuset path into seq_file.
  1590. * - Used for /proc/<pid>/cpuset.
  1591. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  1592. * doesn't really matter if tsk->cpuset changes after we read it,
  1593. * and we take manage_mutex, keeping attach_task() from changing it
  1594. * anyway. No need to check that tsk->cpuset != NULL, thanks to
  1595. * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
  1596. * cpuset to top_cpuset.
  1597. */
  1598. static int proc_cpuset_show(struct seq_file *m, void *v)
  1599. {
  1600. struct pid *pid;
  1601. struct task_struct *tsk;
  1602. char *buf;
  1603. struct cgroup_subsys_state *css;
  1604. int retval;
  1605. retval = -ENOMEM;
  1606. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1607. if (!buf)
  1608. goto out;
  1609. retval = -ESRCH;
  1610. pid = m->private;
  1611. tsk = get_pid_task(pid, PIDTYPE_PID);
  1612. if (!tsk)
  1613. goto out_free;
  1614. retval = -EINVAL;
  1615. cgroup_lock();
  1616. css = task_subsys_state(tsk, cpuset_subsys_id);
  1617. retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
  1618. if (retval < 0)
  1619. goto out_unlock;
  1620. seq_puts(m, buf);
  1621. seq_putc(m, '\n');
  1622. out_unlock:
  1623. cgroup_unlock();
  1624. put_task_struct(tsk);
  1625. out_free:
  1626. kfree(buf);
  1627. out:
  1628. return retval;
  1629. }
  1630. static int cpuset_open(struct inode *inode, struct file *file)
  1631. {
  1632. struct pid *pid = PROC_I(inode)->pid;
  1633. return single_open(file, proc_cpuset_show, pid);
  1634. }
  1635. const struct file_operations proc_cpuset_operations = {
  1636. .open = cpuset_open,
  1637. .read = seq_read,
  1638. .llseek = seq_lseek,
  1639. .release = single_release,
  1640. };
  1641. #endif /* CONFIG_PROC_PID_CPUSET */
  1642. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1643. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1644. {
  1645. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1646. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1647. buffer += sprintf(buffer, "\n");
  1648. buffer += sprintf(buffer, "Mems_allowed:\t");
  1649. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1650. buffer += sprintf(buffer, "\n");
  1651. return buffer;
  1652. }