cpuset.c 76 KB

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