cpuset.c 75 KB

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