cpuset.c 83 KB

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