cpuset.c 67 KB

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