cpuset.c 64 KB

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