cpuset.c 78 KB

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