cpuset.c 76 KB

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