cpuset.c 74 KB

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