cpuset.c 74 KB

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