cpuset.c 75 KB

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