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