cpuset.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  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. /* Called by cgroups to determine if a cpuset is usable; cgroup_mutex held */
  1218. static int cpuset_can_attach(struct cgroup_subsys *ss, struct cgroup *cont,
  1219. struct task_struct *tsk)
  1220. {
  1221. struct cpuset *cs = cgroup_cs(cont);
  1222. if (cpumask_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  1223. return -ENOSPC;
  1224. /*
  1225. * Kthreads bound to specific cpus cannot be moved to a new cpuset; we
  1226. * cannot change their cpu affinity and isolating such threads by their
  1227. * set of allowed nodes is unnecessary. Thus, cpusets are not
  1228. * applicable for such threads. This prevents checking for success of
  1229. * set_cpus_allowed_ptr() on all attached tasks before cpus_allowed may
  1230. * be changed.
  1231. */
  1232. if (tsk->flags & PF_THREAD_BOUND)
  1233. return -EINVAL;
  1234. return 0;
  1235. }
  1236. static int cpuset_can_attach_task(struct cgroup *cgrp, struct task_struct *task)
  1237. {
  1238. return security_task_setscheduler(task);
  1239. }
  1240. /*
  1241. * Protected by cgroup_lock. The nodemasks must be stored globally because
  1242. * dynamically allocating them is not allowed in pre_attach, and they must
  1243. * persist among pre_attach, attach_task, and attach.
  1244. */
  1245. static cpumask_var_t cpus_attach;
  1246. static nodemask_t cpuset_attach_nodemask_from;
  1247. static nodemask_t cpuset_attach_nodemask_to;
  1248. /* Set-up work for before attaching each task. */
  1249. static void cpuset_pre_attach(struct cgroup *cont)
  1250. {
  1251. struct cpuset *cs = cgroup_cs(cont);
  1252. if (cs == &top_cpuset)
  1253. cpumask_copy(cpus_attach, cpu_possible_mask);
  1254. else
  1255. guarantee_online_cpus(cs, cpus_attach);
  1256. guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
  1257. }
  1258. /* Per-thread attachment work. */
  1259. static void cpuset_attach_task(struct cgroup *cont, struct task_struct *tsk)
  1260. {
  1261. int err;
  1262. struct cpuset *cs = cgroup_cs(cont);
  1263. /*
  1264. * can_attach beforehand should guarantee that this doesn't fail.
  1265. * TODO: have a better way to handle failure here
  1266. */
  1267. err = set_cpus_allowed_ptr(tsk, cpus_attach);
  1268. WARN_ON_ONCE(err);
  1269. cpuset_change_task_nodemask(tsk, &cpuset_attach_nodemask_to);
  1270. cpuset_update_task_spread_flag(cs, tsk);
  1271. }
  1272. static void cpuset_attach(struct cgroup_subsys *ss, struct cgroup *cont,
  1273. struct cgroup *oldcont, struct task_struct *tsk)
  1274. {
  1275. struct mm_struct *mm;
  1276. struct cpuset *cs = cgroup_cs(cont);
  1277. struct cpuset *oldcs = cgroup_cs(oldcont);
  1278. /*
  1279. * Change mm, possibly for multiple threads in a threadgroup. This is
  1280. * expensive and may sleep.
  1281. */
  1282. cpuset_attach_nodemask_from = oldcs->mems_allowed;
  1283. cpuset_attach_nodemask_to = cs->mems_allowed;
  1284. mm = get_task_mm(tsk);
  1285. if (mm) {
  1286. mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
  1287. if (is_memory_migrate(cs))
  1288. cpuset_migrate_mm(mm, &cpuset_attach_nodemask_from,
  1289. &cpuset_attach_nodemask_to);
  1290. mmput(mm);
  1291. }
  1292. }
  1293. /* The various types of files and directories in a cpuset file system */
  1294. typedef enum {
  1295. FILE_MEMORY_MIGRATE,
  1296. FILE_CPULIST,
  1297. FILE_MEMLIST,
  1298. FILE_CPU_EXCLUSIVE,
  1299. FILE_MEM_EXCLUSIVE,
  1300. FILE_MEM_HARDWALL,
  1301. FILE_SCHED_LOAD_BALANCE,
  1302. FILE_SCHED_RELAX_DOMAIN_LEVEL,
  1303. FILE_MEMORY_PRESSURE_ENABLED,
  1304. FILE_MEMORY_PRESSURE,
  1305. FILE_SPREAD_PAGE,
  1306. FILE_SPREAD_SLAB,
  1307. } cpuset_filetype_t;
  1308. static int cpuset_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1309. {
  1310. int retval = 0;
  1311. struct cpuset *cs = cgroup_cs(cgrp);
  1312. cpuset_filetype_t type = cft->private;
  1313. if (!cgroup_lock_live_group(cgrp))
  1314. return -ENODEV;
  1315. switch (type) {
  1316. case FILE_CPU_EXCLUSIVE:
  1317. retval = update_flag(CS_CPU_EXCLUSIVE, cs, val);
  1318. break;
  1319. case FILE_MEM_EXCLUSIVE:
  1320. retval = update_flag(CS_MEM_EXCLUSIVE, cs, val);
  1321. break;
  1322. case FILE_MEM_HARDWALL:
  1323. retval = update_flag(CS_MEM_HARDWALL, cs, val);
  1324. break;
  1325. case FILE_SCHED_LOAD_BALANCE:
  1326. retval = update_flag(CS_SCHED_LOAD_BALANCE, cs, val);
  1327. break;
  1328. case FILE_MEMORY_MIGRATE:
  1329. retval = update_flag(CS_MEMORY_MIGRATE, cs, val);
  1330. break;
  1331. case FILE_MEMORY_PRESSURE_ENABLED:
  1332. cpuset_memory_pressure_enabled = !!val;
  1333. break;
  1334. case FILE_MEMORY_PRESSURE:
  1335. retval = -EACCES;
  1336. break;
  1337. case FILE_SPREAD_PAGE:
  1338. retval = update_flag(CS_SPREAD_PAGE, cs, val);
  1339. break;
  1340. case FILE_SPREAD_SLAB:
  1341. retval = update_flag(CS_SPREAD_SLAB, cs, val);
  1342. break;
  1343. default:
  1344. retval = -EINVAL;
  1345. break;
  1346. }
  1347. cgroup_unlock();
  1348. return retval;
  1349. }
  1350. static int cpuset_write_s64(struct cgroup *cgrp, struct cftype *cft, s64 val)
  1351. {
  1352. int retval = 0;
  1353. struct cpuset *cs = cgroup_cs(cgrp);
  1354. cpuset_filetype_t type = cft->private;
  1355. if (!cgroup_lock_live_group(cgrp))
  1356. return -ENODEV;
  1357. switch (type) {
  1358. case FILE_SCHED_RELAX_DOMAIN_LEVEL:
  1359. retval = update_relax_domain_level(cs, val);
  1360. break;
  1361. default:
  1362. retval = -EINVAL;
  1363. break;
  1364. }
  1365. cgroup_unlock();
  1366. return retval;
  1367. }
  1368. /*
  1369. * Common handling for a write to a "cpus" or "mems" file.
  1370. */
  1371. static int cpuset_write_resmask(struct cgroup *cgrp, struct cftype *cft,
  1372. const char *buf)
  1373. {
  1374. int retval = 0;
  1375. struct cpuset *cs = cgroup_cs(cgrp);
  1376. struct cpuset *trialcs;
  1377. if (!cgroup_lock_live_group(cgrp))
  1378. return -ENODEV;
  1379. trialcs = alloc_trial_cpuset(cs);
  1380. if (!trialcs) {
  1381. retval = -ENOMEM;
  1382. goto out;
  1383. }
  1384. switch (cft->private) {
  1385. case FILE_CPULIST:
  1386. retval = update_cpumask(cs, trialcs, buf);
  1387. break;
  1388. case FILE_MEMLIST:
  1389. retval = update_nodemask(cs, trialcs, buf);
  1390. break;
  1391. default:
  1392. retval = -EINVAL;
  1393. break;
  1394. }
  1395. free_trial_cpuset(trialcs);
  1396. out:
  1397. cgroup_unlock();
  1398. return retval;
  1399. }
  1400. /*
  1401. * These ascii lists should be read in a single call, by using a user
  1402. * buffer large enough to hold the entire map. If read in smaller
  1403. * chunks, there is no guarantee of atomicity. Since the display format
  1404. * used, list of ranges of sequential numbers, is variable length,
  1405. * and since these maps can change value dynamically, one could read
  1406. * gibberish by doing partial reads while a list was changing.
  1407. * A single large read to a buffer that crosses a page boundary is
  1408. * ok, because the result being copied to user land is not recomputed
  1409. * across a page fault.
  1410. */
  1411. static size_t cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1412. {
  1413. size_t count;
  1414. mutex_lock(&callback_mutex);
  1415. count = cpulist_scnprintf(page, PAGE_SIZE, cs->cpus_allowed);
  1416. mutex_unlock(&callback_mutex);
  1417. return count;
  1418. }
  1419. static size_t cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1420. {
  1421. size_t count;
  1422. mutex_lock(&callback_mutex);
  1423. count = nodelist_scnprintf(page, PAGE_SIZE, cs->mems_allowed);
  1424. mutex_unlock(&callback_mutex);
  1425. return count;
  1426. }
  1427. static ssize_t cpuset_common_file_read(struct cgroup *cont,
  1428. struct cftype *cft,
  1429. struct file *file,
  1430. char __user *buf,
  1431. size_t nbytes, loff_t *ppos)
  1432. {
  1433. struct cpuset *cs = cgroup_cs(cont);
  1434. cpuset_filetype_t type = cft->private;
  1435. char *page;
  1436. ssize_t retval = 0;
  1437. char *s;
  1438. if (!(page = (char *)__get_free_page(GFP_TEMPORARY)))
  1439. return -ENOMEM;
  1440. s = page;
  1441. switch (type) {
  1442. case FILE_CPULIST:
  1443. s += cpuset_sprintf_cpulist(s, cs);
  1444. break;
  1445. case FILE_MEMLIST:
  1446. s += cpuset_sprintf_memlist(s, cs);
  1447. break;
  1448. default:
  1449. retval = -EINVAL;
  1450. goto out;
  1451. }
  1452. *s++ = '\n';
  1453. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1454. out:
  1455. free_page((unsigned long)page);
  1456. return retval;
  1457. }
  1458. static u64 cpuset_read_u64(struct cgroup *cont, struct cftype *cft)
  1459. {
  1460. struct cpuset *cs = cgroup_cs(cont);
  1461. cpuset_filetype_t type = cft->private;
  1462. switch (type) {
  1463. case FILE_CPU_EXCLUSIVE:
  1464. return is_cpu_exclusive(cs);
  1465. case FILE_MEM_EXCLUSIVE:
  1466. return is_mem_exclusive(cs);
  1467. case FILE_MEM_HARDWALL:
  1468. return is_mem_hardwall(cs);
  1469. case FILE_SCHED_LOAD_BALANCE:
  1470. return is_sched_load_balance(cs);
  1471. case FILE_MEMORY_MIGRATE:
  1472. return is_memory_migrate(cs);
  1473. case FILE_MEMORY_PRESSURE_ENABLED:
  1474. return cpuset_memory_pressure_enabled;
  1475. case FILE_MEMORY_PRESSURE:
  1476. return fmeter_getrate(&cs->fmeter);
  1477. case FILE_SPREAD_PAGE:
  1478. return is_spread_page(cs);
  1479. case FILE_SPREAD_SLAB:
  1480. return is_spread_slab(cs);
  1481. default:
  1482. BUG();
  1483. }
  1484. /* Unreachable but makes gcc happy */
  1485. return 0;
  1486. }
  1487. static s64 cpuset_read_s64(struct cgroup *cont, struct cftype *cft)
  1488. {
  1489. struct cpuset *cs = cgroup_cs(cont);
  1490. cpuset_filetype_t type = cft->private;
  1491. switch (type) {
  1492. case FILE_SCHED_RELAX_DOMAIN_LEVEL:
  1493. return cs->relax_domain_level;
  1494. default:
  1495. BUG();
  1496. }
  1497. /* Unrechable but makes gcc happy */
  1498. return 0;
  1499. }
  1500. /*
  1501. * for the common functions, 'private' gives the type of file
  1502. */
  1503. static struct cftype files[] = {
  1504. {
  1505. .name = "cpus",
  1506. .read = cpuset_common_file_read,
  1507. .write_string = cpuset_write_resmask,
  1508. .max_write_len = (100U + 6 * NR_CPUS),
  1509. .private = FILE_CPULIST,
  1510. },
  1511. {
  1512. .name = "mems",
  1513. .read = cpuset_common_file_read,
  1514. .write_string = cpuset_write_resmask,
  1515. .max_write_len = (100U + 6 * MAX_NUMNODES),
  1516. .private = FILE_MEMLIST,
  1517. },
  1518. {
  1519. .name = "cpu_exclusive",
  1520. .read_u64 = cpuset_read_u64,
  1521. .write_u64 = cpuset_write_u64,
  1522. .private = FILE_CPU_EXCLUSIVE,
  1523. },
  1524. {
  1525. .name = "mem_exclusive",
  1526. .read_u64 = cpuset_read_u64,
  1527. .write_u64 = cpuset_write_u64,
  1528. .private = FILE_MEM_EXCLUSIVE,
  1529. },
  1530. {
  1531. .name = "mem_hardwall",
  1532. .read_u64 = cpuset_read_u64,
  1533. .write_u64 = cpuset_write_u64,
  1534. .private = FILE_MEM_HARDWALL,
  1535. },
  1536. {
  1537. .name = "sched_load_balance",
  1538. .read_u64 = cpuset_read_u64,
  1539. .write_u64 = cpuset_write_u64,
  1540. .private = FILE_SCHED_LOAD_BALANCE,
  1541. },
  1542. {
  1543. .name = "sched_relax_domain_level",
  1544. .read_s64 = cpuset_read_s64,
  1545. .write_s64 = cpuset_write_s64,
  1546. .private = FILE_SCHED_RELAX_DOMAIN_LEVEL,
  1547. },
  1548. {
  1549. .name = "memory_migrate",
  1550. .read_u64 = cpuset_read_u64,
  1551. .write_u64 = cpuset_write_u64,
  1552. .private = FILE_MEMORY_MIGRATE,
  1553. },
  1554. {
  1555. .name = "memory_pressure",
  1556. .read_u64 = cpuset_read_u64,
  1557. .write_u64 = cpuset_write_u64,
  1558. .private = FILE_MEMORY_PRESSURE,
  1559. .mode = S_IRUGO,
  1560. },
  1561. {
  1562. .name = "memory_spread_page",
  1563. .read_u64 = cpuset_read_u64,
  1564. .write_u64 = cpuset_write_u64,
  1565. .private = FILE_SPREAD_PAGE,
  1566. },
  1567. {
  1568. .name = "memory_spread_slab",
  1569. .read_u64 = cpuset_read_u64,
  1570. .write_u64 = cpuset_write_u64,
  1571. .private = FILE_SPREAD_SLAB,
  1572. },
  1573. };
  1574. static struct cftype cft_memory_pressure_enabled = {
  1575. .name = "memory_pressure_enabled",
  1576. .read_u64 = cpuset_read_u64,
  1577. .write_u64 = cpuset_write_u64,
  1578. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1579. };
  1580. static int cpuset_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  1581. {
  1582. int err;
  1583. err = cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
  1584. if (err)
  1585. return err;
  1586. /* memory_pressure_enabled is in root cpuset only */
  1587. if (!cont->parent)
  1588. err = cgroup_add_file(cont, ss,
  1589. &cft_memory_pressure_enabled);
  1590. return err;
  1591. }
  1592. /*
  1593. * post_clone() is called during cgroup_create() when the
  1594. * clone_children mount argument was specified. The cgroup
  1595. * can not yet have any tasks.
  1596. *
  1597. * Currently we refuse to set up the cgroup - thereby
  1598. * refusing the task to be entered, and as a result refusing
  1599. * the sys_unshare() or clone() which initiated it - if any
  1600. * sibling cpusets have exclusive cpus or mem.
  1601. *
  1602. * If this becomes a problem for some users who wish to
  1603. * allow that scenario, then cpuset_post_clone() could be
  1604. * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
  1605. * (and likewise for mems) to the new cgroup. Called with cgroup_mutex
  1606. * held.
  1607. */
  1608. static void cpuset_post_clone(struct cgroup_subsys *ss,
  1609. struct cgroup *cgroup)
  1610. {
  1611. struct cgroup *parent, *child;
  1612. struct cpuset *cs, *parent_cs;
  1613. parent = cgroup->parent;
  1614. list_for_each_entry(child, &parent->children, sibling) {
  1615. cs = cgroup_cs(child);
  1616. if (is_mem_exclusive(cs) || is_cpu_exclusive(cs))
  1617. return;
  1618. }
  1619. cs = cgroup_cs(cgroup);
  1620. parent_cs = cgroup_cs(parent);
  1621. mutex_lock(&callback_mutex);
  1622. cs->mems_allowed = parent_cs->mems_allowed;
  1623. cpumask_copy(cs->cpus_allowed, parent_cs->cpus_allowed);
  1624. mutex_unlock(&callback_mutex);
  1625. return;
  1626. }
  1627. /*
  1628. * cpuset_create - create a cpuset
  1629. * ss: cpuset cgroup subsystem
  1630. * cont: control group that the new cpuset will be part of
  1631. */
  1632. static struct cgroup_subsys_state *cpuset_create(
  1633. struct cgroup_subsys *ss,
  1634. struct cgroup *cont)
  1635. {
  1636. struct cpuset *cs;
  1637. struct cpuset *parent;
  1638. if (!cont->parent) {
  1639. return &top_cpuset.css;
  1640. }
  1641. parent = cgroup_cs(cont->parent);
  1642. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1643. if (!cs)
  1644. return ERR_PTR(-ENOMEM);
  1645. if (!alloc_cpumask_var(&cs->cpus_allowed, GFP_KERNEL)) {
  1646. kfree(cs);
  1647. return ERR_PTR(-ENOMEM);
  1648. }
  1649. cs->flags = 0;
  1650. if (is_spread_page(parent))
  1651. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1652. if (is_spread_slab(parent))
  1653. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1654. set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  1655. cpumask_clear(cs->cpus_allowed);
  1656. nodes_clear(cs->mems_allowed);
  1657. fmeter_init(&cs->fmeter);
  1658. cs->relax_domain_level = -1;
  1659. cs->parent = parent;
  1660. number_of_cpusets++;
  1661. return &cs->css ;
  1662. }
  1663. /*
  1664. * If the cpuset being removed has its flag 'sched_load_balance'
  1665. * enabled, then simulate turning sched_load_balance off, which
  1666. * will call async_rebuild_sched_domains().
  1667. */
  1668. static void cpuset_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
  1669. {
  1670. struct cpuset *cs = cgroup_cs(cont);
  1671. if (is_sched_load_balance(cs))
  1672. update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
  1673. number_of_cpusets--;
  1674. free_cpumask_var(cs->cpus_allowed);
  1675. kfree(cs);
  1676. }
  1677. struct cgroup_subsys cpuset_subsys = {
  1678. .name = "cpuset",
  1679. .create = cpuset_create,
  1680. .destroy = cpuset_destroy,
  1681. .can_attach = cpuset_can_attach,
  1682. .can_attach_task = cpuset_can_attach_task,
  1683. .pre_attach = cpuset_pre_attach,
  1684. .attach_task = cpuset_attach_task,
  1685. .attach = cpuset_attach,
  1686. .populate = cpuset_populate,
  1687. .post_clone = cpuset_post_clone,
  1688. .subsys_id = cpuset_subsys_id,
  1689. .early_init = 1,
  1690. };
  1691. /**
  1692. * cpuset_init - initialize cpusets at system boot
  1693. *
  1694. * Description: Initialize top_cpuset and the cpuset internal file system,
  1695. **/
  1696. int __init cpuset_init(void)
  1697. {
  1698. int err = 0;
  1699. if (!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL))
  1700. BUG();
  1701. cpumask_setall(top_cpuset.cpus_allowed);
  1702. nodes_setall(top_cpuset.mems_allowed);
  1703. fmeter_init(&top_cpuset.fmeter);
  1704. set_bit(CS_SCHED_LOAD_BALANCE, &top_cpuset.flags);
  1705. top_cpuset.relax_domain_level = -1;
  1706. err = register_filesystem(&cpuset_fs_type);
  1707. if (err < 0)
  1708. return err;
  1709. if (!alloc_cpumask_var(&cpus_attach, GFP_KERNEL))
  1710. BUG();
  1711. number_of_cpusets = 1;
  1712. return 0;
  1713. }
  1714. /**
  1715. * cpuset_do_move_task - move a given task to another cpuset
  1716. * @tsk: pointer to task_struct the task to move
  1717. * @scan: struct cgroup_scanner contained in its struct cpuset_hotplug_scanner
  1718. *
  1719. * Called by cgroup_scan_tasks() for each task in a cgroup.
  1720. * Return nonzero to stop the walk through the tasks.
  1721. */
  1722. static void cpuset_do_move_task(struct task_struct *tsk,
  1723. struct cgroup_scanner *scan)
  1724. {
  1725. struct cgroup *new_cgroup = scan->data;
  1726. cgroup_attach_task(new_cgroup, tsk);
  1727. }
  1728. /**
  1729. * move_member_tasks_to_cpuset - move tasks from one cpuset to another
  1730. * @from: cpuset in which the tasks currently reside
  1731. * @to: cpuset to which the tasks will be moved
  1732. *
  1733. * Called with cgroup_mutex held
  1734. * callback_mutex must not be held, as cpuset_attach() will take it.
  1735. *
  1736. * The cgroup_scan_tasks() function will scan all the tasks in a cgroup,
  1737. * calling callback functions for each.
  1738. */
  1739. static void move_member_tasks_to_cpuset(struct cpuset *from, struct cpuset *to)
  1740. {
  1741. struct cgroup_scanner scan;
  1742. scan.cg = from->css.cgroup;
  1743. scan.test_task = NULL; /* select all tasks in cgroup */
  1744. scan.process_task = cpuset_do_move_task;
  1745. scan.heap = NULL;
  1746. scan.data = to->css.cgroup;
  1747. if (cgroup_scan_tasks(&scan))
  1748. printk(KERN_ERR "move_member_tasks_to_cpuset: "
  1749. "cgroup_scan_tasks failed\n");
  1750. }
  1751. /*
  1752. * If CPU and/or memory hotplug handlers, below, unplug any CPUs
  1753. * or memory nodes, we need to walk over the cpuset hierarchy,
  1754. * removing that CPU or node from all cpusets. If this removes the
  1755. * last CPU or node from a cpuset, then move the tasks in the empty
  1756. * cpuset to its next-highest non-empty parent.
  1757. *
  1758. * Called with cgroup_mutex held
  1759. * callback_mutex must not be held, as cpuset_attach() will take it.
  1760. */
  1761. static void remove_tasks_in_empty_cpuset(struct cpuset *cs)
  1762. {
  1763. struct cpuset *parent;
  1764. /*
  1765. * The cgroup's css_sets list is in use if there are tasks
  1766. * in the cpuset; the list is empty if there are none;
  1767. * the cs->css.refcnt seems always 0.
  1768. */
  1769. if (list_empty(&cs->css.cgroup->css_sets))
  1770. return;
  1771. /*
  1772. * Find its next-highest non-empty parent, (top cpuset
  1773. * has online cpus, so can't be empty).
  1774. */
  1775. parent = cs->parent;
  1776. while (cpumask_empty(parent->cpus_allowed) ||
  1777. nodes_empty(parent->mems_allowed))
  1778. parent = parent->parent;
  1779. move_member_tasks_to_cpuset(cs, parent);
  1780. }
  1781. /*
  1782. * Walk the specified cpuset subtree and look for empty cpusets.
  1783. * The tasks of such cpuset must be moved to a parent cpuset.
  1784. *
  1785. * Called with cgroup_mutex held. We take callback_mutex to modify
  1786. * cpus_allowed and mems_allowed.
  1787. *
  1788. * This walk processes the tree from top to bottom, completing one layer
  1789. * before dropping down to the next. It always processes a node before
  1790. * any of its children.
  1791. *
  1792. * For now, since we lack memory hot unplug, we'll never see a cpuset
  1793. * that has tasks along with an empty 'mems'. But if we did see such
  1794. * a cpuset, we'd handle it just like we do if its 'cpus' was empty.
  1795. */
  1796. static void scan_for_empty_cpusets(struct cpuset *root)
  1797. {
  1798. LIST_HEAD(queue);
  1799. struct cpuset *cp; /* scans cpusets being updated */
  1800. struct cpuset *child; /* scans child cpusets of cp */
  1801. struct cgroup *cont;
  1802. static nodemask_t oldmems; /* protected by cgroup_mutex */
  1803. list_add_tail((struct list_head *)&root->stack_list, &queue);
  1804. while (!list_empty(&queue)) {
  1805. cp = list_first_entry(&queue, struct cpuset, stack_list);
  1806. list_del(queue.next);
  1807. list_for_each_entry(cont, &cp->css.cgroup->children, sibling) {
  1808. child = cgroup_cs(cont);
  1809. list_add_tail(&child->stack_list, &queue);
  1810. }
  1811. /* Continue past cpusets with all cpus, mems online */
  1812. if (cpumask_subset(cp->cpus_allowed, cpu_active_mask) &&
  1813. nodes_subset(cp->mems_allowed, node_states[N_HIGH_MEMORY]))
  1814. continue;
  1815. oldmems = cp->mems_allowed;
  1816. /* Remove offline cpus and mems from this cpuset. */
  1817. mutex_lock(&callback_mutex);
  1818. cpumask_and(cp->cpus_allowed, cp->cpus_allowed,
  1819. cpu_active_mask);
  1820. nodes_and(cp->mems_allowed, cp->mems_allowed,
  1821. node_states[N_HIGH_MEMORY]);
  1822. mutex_unlock(&callback_mutex);
  1823. /* Move tasks from the empty cpuset to a parent */
  1824. if (cpumask_empty(cp->cpus_allowed) ||
  1825. nodes_empty(cp->mems_allowed))
  1826. remove_tasks_in_empty_cpuset(cp);
  1827. else {
  1828. update_tasks_cpumask(cp, NULL);
  1829. update_tasks_nodemask(cp, &oldmems, NULL);
  1830. }
  1831. }
  1832. }
  1833. /*
  1834. * The top_cpuset tracks what CPUs and Memory Nodes are online,
  1835. * period. This is necessary in order to make cpusets transparent
  1836. * (of no affect) on systems that are actively using CPU hotplug
  1837. * but making no active use of cpusets.
  1838. *
  1839. * This routine ensures that top_cpuset.cpus_allowed tracks
  1840. * cpu_active_mask on each CPU hotplug (cpuhp) event.
  1841. *
  1842. * Called within get_online_cpus(). Needs to call cgroup_lock()
  1843. * before calling generate_sched_domains().
  1844. */
  1845. void cpuset_update_active_cpus(void)
  1846. {
  1847. struct sched_domain_attr *attr;
  1848. cpumask_var_t *doms;
  1849. int ndoms;
  1850. cgroup_lock();
  1851. mutex_lock(&callback_mutex);
  1852. cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
  1853. mutex_unlock(&callback_mutex);
  1854. scan_for_empty_cpusets(&top_cpuset);
  1855. ndoms = generate_sched_domains(&doms, &attr);
  1856. cgroup_unlock();
  1857. /* Have scheduler rebuild the domains */
  1858. partition_sched_domains(ndoms, doms, attr);
  1859. }
  1860. #ifdef CONFIG_MEMORY_HOTPLUG
  1861. /*
  1862. * Keep top_cpuset.mems_allowed tracking node_states[N_HIGH_MEMORY].
  1863. * Call this routine anytime after node_states[N_HIGH_MEMORY] changes.
  1864. * See also the previous routine cpuset_track_online_cpus().
  1865. */
  1866. static int cpuset_track_online_nodes(struct notifier_block *self,
  1867. unsigned long action, void *arg)
  1868. {
  1869. static nodemask_t oldmems; /* protected by cgroup_mutex */
  1870. cgroup_lock();
  1871. switch (action) {
  1872. case MEM_ONLINE:
  1873. oldmems = top_cpuset.mems_allowed;
  1874. mutex_lock(&callback_mutex);
  1875. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1876. mutex_unlock(&callback_mutex);
  1877. update_tasks_nodemask(&top_cpuset, &oldmems, NULL);
  1878. break;
  1879. case MEM_OFFLINE:
  1880. /*
  1881. * needn't update top_cpuset.mems_allowed explicitly because
  1882. * scan_for_empty_cpusets() will update it.
  1883. */
  1884. scan_for_empty_cpusets(&top_cpuset);
  1885. break;
  1886. default:
  1887. break;
  1888. }
  1889. cgroup_unlock();
  1890. return NOTIFY_OK;
  1891. }
  1892. #endif
  1893. /**
  1894. * cpuset_init_smp - initialize cpus_allowed
  1895. *
  1896. * Description: Finish top cpuset after cpu, node maps are initialized
  1897. **/
  1898. void __init cpuset_init_smp(void)
  1899. {
  1900. cpumask_copy(top_cpuset.cpus_allowed, cpu_active_mask);
  1901. top_cpuset.mems_allowed = node_states[N_HIGH_MEMORY];
  1902. hotplug_memory_notifier(cpuset_track_online_nodes, 10);
  1903. cpuset_wq = create_singlethread_workqueue("cpuset");
  1904. BUG_ON(!cpuset_wq);
  1905. }
  1906. /**
  1907. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1908. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1909. * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
  1910. *
  1911. * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
  1912. * attached to the specified @tsk. Guaranteed to return some non-empty
  1913. * subset of cpu_online_map, even if this means going outside the
  1914. * tasks cpuset.
  1915. **/
  1916. void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
  1917. {
  1918. mutex_lock(&callback_mutex);
  1919. task_lock(tsk);
  1920. guarantee_online_cpus(task_cs(tsk), pmask);
  1921. task_unlock(tsk);
  1922. mutex_unlock(&callback_mutex);
  1923. }
  1924. int cpuset_cpus_allowed_fallback(struct task_struct *tsk)
  1925. {
  1926. const struct cpuset *cs;
  1927. int cpu;
  1928. rcu_read_lock();
  1929. cs = task_cs(tsk);
  1930. if (cs)
  1931. do_set_cpus_allowed(tsk, cs->cpus_allowed);
  1932. rcu_read_unlock();
  1933. /*
  1934. * We own tsk->cpus_allowed, nobody can change it under us.
  1935. *
  1936. * But we used cs && cs->cpus_allowed lockless and thus can
  1937. * race with cgroup_attach_task() or update_cpumask() and get
  1938. * the wrong tsk->cpus_allowed. However, both cases imply the
  1939. * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
  1940. * which takes task_rq_lock().
  1941. *
  1942. * If we are called after it dropped the lock we must see all
  1943. * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
  1944. * set any mask even if it is not right from task_cs() pov,
  1945. * the pending set_cpus_allowed_ptr() will fix things.
  1946. */
  1947. cpu = cpumask_any_and(&tsk->cpus_allowed, cpu_active_mask);
  1948. if (cpu >= nr_cpu_ids) {
  1949. /*
  1950. * Either tsk->cpus_allowed is wrong (see above) or it
  1951. * is actually empty. The latter case is only possible
  1952. * if we are racing with remove_tasks_in_empty_cpuset().
  1953. * Like above we can temporary set any mask and rely on
  1954. * set_cpus_allowed_ptr() as synchronization point.
  1955. */
  1956. do_set_cpus_allowed(tsk, cpu_possible_mask);
  1957. cpu = cpumask_any(cpu_active_mask);
  1958. }
  1959. return cpu;
  1960. }
  1961. void cpuset_init_current_mems_allowed(void)
  1962. {
  1963. nodes_setall(current->mems_allowed);
  1964. }
  1965. /**
  1966. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  1967. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  1968. *
  1969. * Description: Returns the nodemask_t mems_allowed of the cpuset
  1970. * attached to the specified @tsk. Guaranteed to return some non-empty
  1971. * subset of node_states[N_HIGH_MEMORY], even if this means going outside the
  1972. * tasks cpuset.
  1973. **/
  1974. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  1975. {
  1976. nodemask_t mask;
  1977. mutex_lock(&callback_mutex);
  1978. task_lock(tsk);
  1979. guarantee_online_mems(task_cs(tsk), &mask);
  1980. task_unlock(tsk);
  1981. mutex_unlock(&callback_mutex);
  1982. return mask;
  1983. }
  1984. /**
  1985. * cpuset_nodemask_valid_mems_allowed - check nodemask vs. curremt mems_allowed
  1986. * @nodemask: the nodemask to be checked
  1987. *
  1988. * Are any of the nodes in the nodemask allowed in current->mems_allowed?
  1989. */
  1990. int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
  1991. {
  1992. return nodes_intersects(*nodemask, current->mems_allowed);
  1993. }
  1994. /*
  1995. * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
  1996. * mem_hardwall ancestor to the specified cpuset. Call holding
  1997. * callback_mutex. If no ancestor is mem_exclusive or mem_hardwall
  1998. * (an unusual configuration), then returns the root cpuset.
  1999. */
  2000. static const struct cpuset *nearest_hardwall_ancestor(const struct cpuset *cs)
  2001. {
  2002. while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && cs->parent)
  2003. cs = cs->parent;
  2004. return cs;
  2005. }
  2006. /**
  2007. * cpuset_node_allowed_softwall - Can we allocate on a memory node?
  2008. * @node: is this an allowed node?
  2009. * @gfp_mask: memory allocation flags
  2010. *
  2011. * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
  2012. * set, yes, we can always allocate. If node is in our task's mems_allowed,
  2013. * yes. If it's not a __GFP_HARDWALL request and this node is in the nearest
  2014. * hardwalled cpuset ancestor to this task's cpuset, yes. If the task has been
  2015. * OOM killed and has access to memory reserves as specified by the TIF_MEMDIE
  2016. * flag, yes.
  2017. * Otherwise, no.
  2018. *
  2019. * If __GFP_HARDWALL is set, cpuset_node_allowed_softwall() reduces to
  2020. * cpuset_node_allowed_hardwall(). Otherwise, cpuset_node_allowed_softwall()
  2021. * might sleep, and might allow a node from an enclosing cpuset.
  2022. *
  2023. * cpuset_node_allowed_hardwall() only handles the simpler case of hardwall
  2024. * cpusets, and never sleeps.
  2025. *
  2026. * The __GFP_THISNODE placement logic is really handled elsewhere,
  2027. * by forcibly using a zonelist starting at a specified node, and by
  2028. * (in get_page_from_freelist()) refusing to consider the zones for
  2029. * any node on the zonelist except the first. By the time any such
  2030. * calls get to this routine, we should just shut up and say 'yes'.
  2031. *
  2032. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  2033. * and do not allow allocations outside the current tasks cpuset
  2034. * unless the task has been OOM killed as is marked TIF_MEMDIE.
  2035. * GFP_KERNEL allocations are not so marked, so can escape to the
  2036. * nearest enclosing hardwalled ancestor cpuset.
  2037. *
  2038. * Scanning up parent cpusets requires callback_mutex. The
  2039. * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
  2040. * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
  2041. * current tasks mems_allowed came up empty on the first pass over
  2042. * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
  2043. * cpuset are short of memory, might require taking the callback_mutex
  2044. * mutex.
  2045. *
  2046. * The first call here from mm/page_alloc:get_page_from_freelist()
  2047. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
  2048. * so no allocation on a node outside the cpuset is allowed (unless
  2049. * in interrupt, of course).
  2050. *
  2051. * The second pass through get_page_from_freelist() doesn't even call
  2052. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  2053. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  2054. * in alloc_flags. That logic and the checks below have the combined
  2055. * affect that:
  2056. * in_interrupt - any node ok (current task context irrelevant)
  2057. * GFP_ATOMIC - any node ok
  2058. * TIF_MEMDIE - any node ok
  2059. * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
  2060. * GFP_USER - only nodes in current tasks mems allowed ok.
  2061. *
  2062. * Rule:
  2063. * Don't call cpuset_node_allowed_softwall if you can't sleep, unless you
  2064. * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
  2065. * the code that might scan up ancestor cpusets and sleep.
  2066. */
  2067. int __cpuset_node_allowed_softwall(int node, gfp_t gfp_mask)
  2068. {
  2069. const struct cpuset *cs; /* current cpuset ancestors */
  2070. int allowed; /* is allocation in zone z allowed? */
  2071. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  2072. return 1;
  2073. might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
  2074. if (node_isset(node, current->mems_allowed))
  2075. return 1;
  2076. /*
  2077. * Allow tasks that have access to memory reserves because they have
  2078. * been OOM killed to get memory anywhere.
  2079. */
  2080. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  2081. return 1;
  2082. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  2083. return 0;
  2084. if (current->flags & PF_EXITING) /* Let dying task have memory */
  2085. return 1;
  2086. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  2087. mutex_lock(&callback_mutex);
  2088. task_lock(current);
  2089. cs = nearest_hardwall_ancestor(task_cs(current));
  2090. task_unlock(current);
  2091. allowed = node_isset(node, cs->mems_allowed);
  2092. mutex_unlock(&callback_mutex);
  2093. return allowed;
  2094. }
  2095. /*
  2096. * cpuset_node_allowed_hardwall - Can we allocate on a memory node?
  2097. * @node: is this an allowed node?
  2098. * @gfp_mask: memory allocation flags
  2099. *
  2100. * If we're in interrupt, yes, we can always allocate. If __GFP_THISNODE is
  2101. * set, yes, we can always allocate. If node is in our task's mems_allowed,
  2102. * yes. If the task has been OOM killed and has access to memory reserves as
  2103. * specified by the TIF_MEMDIE flag, yes.
  2104. * Otherwise, no.
  2105. *
  2106. * The __GFP_THISNODE placement logic is really handled elsewhere,
  2107. * by forcibly using a zonelist starting at a specified node, and by
  2108. * (in get_page_from_freelist()) refusing to consider the zones for
  2109. * any node on the zonelist except the first. By the time any such
  2110. * calls get to this routine, we should just shut up and say 'yes'.
  2111. *
  2112. * Unlike the cpuset_node_allowed_softwall() variant, above,
  2113. * this variant requires that the node be in the current task's
  2114. * mems_allowed or that we're in interrupt. It does not scan up the
  2115. * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
  2116. * It never sleeps.
  2117. */
  2118. int __cpuset_node_allowed_hardwall(int node, gfp_t gfp_mask)
  2119. {
  2120. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  2121. return 1;
  2122. if (node_isset(node, current->mems_allowed))
  2123. return 1;
  2124. /*
  2125. * Allow tasks that have access to memory reserves because they have
  2126. * been OOM killed to get memory anywhere.
  2127. */
  2128. if (unlikely(test_thread_flag(TIF_MEMDIE)))
  2129. return 1;
  2130. return 0;
  2131. }
  2132. /**
  2133. * cpuset_unlock - release lock on cpuset changes
  2134. *
  2135. * Undo the lock taken in a previous cpuset_lock() call.
  2136. */
  2137. void cpuset_unlock(void)
  2138. {
  2139. mutex_unlock(&callback_mutex);
  2140. }
  2141. /**
  2142. * cpuset_mem_spread_node() - On which node to begin search for a file page
  2143. * cpuset_slab_spread_node() - On which node to begin search for a slab page
  2144. *
  2145. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  2146. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  2147. * and if the memory allocation used cpuset_mem_spread_node()
  2148. * to determine on which node to start looking, as it will for
  2149. * certain page cache or slab cache pages such as used for file
  2150. * system buffers and inode caches, then instead of starting on the
  2151. * local node to look for a free page, rather spread the starting
  2152. * node around the tasks mems_allowed nodes.
  2153. *
  2154. * We don't have to worry about the returned node being offline
  2155. * because "it can't happen", and even if it did, it would be ok.
  2156. *
  2157. * The routines calling guarantee_online_mems() are careful to
  2158. * only set nodes in task->mems_allowed that are online. So it
  2159. * should not be possible for the following code to return an
  2160. * offline node. But if it did, that would be ok, as this routine
  2161. * is not returning the node where the allocation must be, only
  2162. * the node where the search should start. The zonelist passed to
  2163. * __alloc_pages() will include all nodes. If the slab allocator
  2164. * is passed an offline node, it will fall back to the local node.
  2165. * See kmem_cache_alloc_node().
  2166. */
  2167. static int cpuset_spread_node(int *rotor)
  2168. {
  2169. int node;
  2170. node = next_node(*rotor, current->mems_allowed);
  2171. if (node == MAX_NUMNODES)
  2172. node = first_node(current->mems_allowed);
  2173. *rotor = node;
  2174. return node;
  2175. }
  2176. int cpuset_mem_spread_node(void)
  2177. {
  2178. if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
  2179. current->cpuset_mem_spread_rotor =
  2180. node_random(&current->mems_allowed);
  2181. return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
  2182. }
  2183. int cpuset_slab_spread_node(void)
  2184. {
  2185. if (current->cpuset_slab_spread_rotor == NUMA_NO_NODE)
  2186. current->cpuset_slab_spread_rotor =
  2187. node_random(&current->mems_allowed);
  2188. return cpuset_spread_node(&current->cpuset_slab_spread_rotor);
  2189. }
  2190. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  2191. /**
  2192. * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
  2193. * @tsk1: pointer to task_struct of some task.
  2194. * @tsk2: pointer to task_struct of some other task.
  2195. *
  2196. * Description: Return true if @tsk1's mems_allowed intersects the
  2197. * mems_allowed of @tsk2. Used by the OOM killer to determine if
  2198. * one of the task's memory usage might impact the memory available
  2199. * to the other.
  2200. **/
  2201. int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  2202. const struct task_struct *tsk2)
  2203. {
  2204. return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
  2205. }
  2206. /**
  2207. * cpuset_print_task_mems_allowed - prints task's cpuset and mems_allowed
  2208. * @task: pointer to task_struct of some task.
  2209. *
  2210. * Description: Prints @task's name, cpuset name, and cached copy of its
  2211. * mems_allowed to the kernel log. Must hold task_lock(task) to allow
  2212. * dereferencing task_cs(task).
  2213. */
  2214. void cpuset_print_task_mems_allowed(struct task_struct *tsk)
  2215. {
  2216. struct dentry *dentry;
  2217. dentry = task_cs(tsk)->css.cgroup->dentry;
  2218. spin_lock(&cpuset_buffer_lock);
  2219. snprintf(cpuset_name, CPUSET_NAME_LEN,
  2220. dentry ? (const char *)dentry->d_name.name : "/");
  2221. nodelist_scnprintf(cpuset_nodelist, CPUSET_NODELIST_LEN,
  2222. tsk->mems_allowed);
  2223. printk(KERN_INFO "%s cpuset=%s mems_allowed=%s\n",
  2224. tsk->comm, cpuset_name, cpuset_nodelist);
  2225. spin_unlock(&cpuset_buffer_lock);
  2226. }
  2227. /*
  2228. * Collection of memory_pressure is suppressed unless
  2229. * this flag is enabled by writing "1" to the special
  2230. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  2231. */
  2232. int cpuset_memory_pressure_enabled __read_mostly;
  2233. /**
  2234. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  2235. *
  2236. * Keep a running average of the rate of synchronous (direct)
  2237. * page reclaim efforts initiated by tasks in each cpuset.
  2238. *
  2239. * This represents the rate at which some task in the cpuset
  2240. * ran low on memory on all nodes it was allowed to use, and
  2241. * had to enter the kernels page reclaim code in an effort to
  2242. * create more free memory by tossing clean pages or swapping
  2243. * or writing dirty pages.
  2244. *
  2245. * Display to user space in the per-cpuset read-only file
  2246. * "memory_pressure". Value displayed is an integer
  2247. * representing the recent rate of entry into the synchronous
  2248. * (direct) page reclaim by any task attached to the cpuset.
  2249. **/
  2250. void __cpuset_memory_pressure_bump(void)
  2251. {
  2252. task_lock(current);
  2253. fmeter_markevent(&task_cs(current)->fmeter);
  2254. task_unlock(current);
  2255. }
  2256. #ifdef CONFIG_PROC_PID_CPUSET
  2257. /*
  2258. * proc_cpuset_show()
  2259. * - Print tasks cpuset path into seq_file.
  2260. * - Used for /proc/<pid>/cpuset.
  2261. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  2262. * doesn't really matter if tsk->cpuset changes after we read it,
  2263. * and we take cgroup_mutex, keeping cpuset_attach() from changing it
  2264. * anyway.
  2265. */
  2266. static int proc_cpuset_show(struct seq_file *m, void *unused_v)
  2267. {
  2268. struct pid *pid;
  2269. struct task_struct *tsk;
  2270. char *buf;
  2271. struct cgroup_subsys_state *css;
  2272. int retval;
  2273. retval = -ENOMEM;
  2274. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2275. if (!buf)
  2276. goto out;
  2277. retval = -ESRCH;
  2278. pid = m->private;
  2279. tsk = get_pid_task(pid, PIDTYPE_PID);
  2280. if (!tsk)
  2281. goto out_free;
  2282. retval = -EINVAL;
  2283. cgroup_lock();
  2284. css = task_subsys_state(tsk, cpuset_subsys_id);
  2285. retval = cgroup_path(css->cgroup, buf, PAGE_SIZE);
  2286. if (retval < 0)
  2287. goto out_unlock;
  2288. seq_puts(m, buf);
  2289. seq_putc(m, '\n');
  2290. out_unlock:
  2291. cgroup_unlock();
  2292. put_task_struct(tsk);
  2293. out_free:
  2294. kfree(buf);
  2295. out:
  2296. return retval;
  2297. }
  2298. static int cpuset_open(struct inode *inode, struct file *file)
  2299. {
  2300. struct pid *pid = PROC_I(inode)->pid;
  2301. return single_open(file, proc_cpuset_show, pid);
  2302. }
  2303. const struct file_operations proc_cpuset_operations = {
  2304. .open = cpuset_open,
  2305. .read = seq_read,
  2306. .llseek = seq_lseek,
  2307. .release = single_release,
  2308. };
  2309. #endif /* CONFIG_PROC_PID_CPUSET */
  2310. /* Display task mems_allowed in /proc/<pid>/status file. */
  2311. void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
  2312. {
  2313. seq_printf(m, "Mems_allowed:\t");
  2314. seq_nodemask(m, &task->mems_allowed);
  2315. seq_printf(m, "\n");
  2316. seq_printf(m, "Mems_allowed_list:\t");
  2317. seq_nodemask_list(m, &task->mems_allowed);
  2318. seq_printf(m, "\n");
  2319. }