cpuset.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  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-2006 Silicon Graphics, Inc.
  8. *
  9. * Portions derived from Patrick Mochel's sysfs code.
  10. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  11. *
  12. * 2003-10-10 Written by Simon Derr.
  13. * 2003-10-22 Updates by Stephen Hemminger.
  14. * 2004 May-July Rework by Paul Jackson.
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file COPYING in the main directory of the Linux
  18. * distribution for more details.
  19. */
  20. #include <linux/cpu.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/cpuset.h>
  23. #include <linux/err.h>
  24. #include <linux/errno.h>
  25. #include <linux/file.h>
  26. #include <linux/fs.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kernel.h>
  30. #include <linux/kmod.h>
  31. #include <linux/list.h>
  32. #include <linux/mempolicy.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/mount.h>
  36. #include <linux/namei.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/rcupdate.h>
  40. #include <linux/sched.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/security.h>
  43. #include <linux/slab.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/stat.h>
  47. #include <linux/string.h>
  48. #include <linux/time.h>
  49. #include <linux/backing-dev.h>
  50. #include <linux/sort.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/atomic.h>
  53. #include <linux/mutex.h>
  54. #define CPUSET_SUPER_MAGIC 0x27e0eb
  55. /*
  56. * Tracks how many cpusets are currently defined in system.
  57. * When there is only one cpuset (the root cpuset) we can
  58. * short circuit some hooks.
  59. */
  60. int number_of_cpusets __read_mostly;
  61. /* See "Frequency meter" comments, below. */
  62. struct fmeter {
  63. int cnt; /* unprocessed events count */
  64. int val; /* most recent output value */
  65. time_t time; /* clock (secs) when val computed */
  66. spinlock_t lock; /* guards read or write of above */
  67. };
  68. struct cpuset {
  69. unsigned long flags; /* "unsigned long" so bitops work */
  70. cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
  71. nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
  72. /*
  73. * Count is atomic so can incr (fork) or decr (exit) without a lock.
  74. */
  75. atomic_t count; /* count tasks using this cpuset */
  76. /*
  77. * We link our 'sibling' struct into our parents 'children'.
  78. * Our children link their 'sibling' into our 'children'.
  79. */
  80. struct list_head sibling; /* my parents children */
  81. struct list_head children; /* my children */
  82. struct cpuset *parent; /* my parent */
  83. struct dentry *dentry; /* cpuset fs entry */
  84. /*
  85. * Copy of global cpuset_mems_generation as of the most
  86. * recent time this cpuset changed its mems_allowed.
  87. */
  88. int mems_generation;
  89. struct fmeter fmeter; /* memory_pressure filter */
  90. };
  91. /* bits in struct cpuset flags field */
  92. typedef enum {
  93. CS_CPU_EXCLUSIVE,
  94. CS_MEM_EXCLUSIVE,
  95. CS_MEMORY_MIGRATE,
  96. CS_REMOVED,
  97. CS_NOTIFY_ON_RELEASE,
  98. CS_SPREAD_PAGE,
  99. CS_SPREAD_SLAB,
  100. } cpuset_flagbits_t;
  101. /* convenient tests for these bits */
  102. static inline int is_cpu_exclusive(const struct cpuset *cs)
  103. {
  104. return test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  105. }
  106. static inline int is_mem_exclusive(const struct cpuset *cs)
  107. {
  108. return test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
  109. }
  110. static inline int is_removed(const struct cpuset *cs)
  111. {
  112. return test_bit(CS_REMOVED, &cs->flags);
  113. }
  114. static inline int notify_on_release(const struct cpuset *cs)
  115. {
  116. return test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  117. }
  118. static inline int is_memory_migrate(const struct cpuset *cs)
  119. {
  120. return test_bit(CS_MEMORY_MIGRATE, &cs->flags);
  121. }
  122. static inline int is_spread_page(const struct cpuset *cs)
  123. {
  124. return test_bit(CS_SPREAD_PAGE, &cs->flags);
  125. }
  126. static inline int is_spread_slab(const struct cpuset *cs)
  127. {
  128. return test_bit(CS_SPREAD_SLAB, &cs->flags);
  129. }
  130. /*
  131. * Increment this integer everytime any cpuset changes its
  132. * mems_allowed value. Users of cpusets can track this generation
  133. * number, and avoid having to lock and reload mems_allowed unless
  134. * the cpuset they're using changes generation.
  135. *
  136. * A single, global generation is needed because attach_task() could
  137. * reattach a task to a different cpuset, which must not have its
  138. * generation numbers aliased with those of that tasks previous cpuset.
  139. *
  140. * Generations are needed for mems_allowed because one task cannot
  141. * modify anothers memory placement. So we must enable every task,
  142. * on every visit to __alloc_pages(), to efficiently check whether
  143. * its current->cpuset->mems_allowed has changed, requiring an update
  144. * of its current->mems_allowed.
  145. *
  146. * Since cpuset_mems_generation is guarded by manage_mutex,
  147. * there is no need to mark it atomic.
  148. */
  149. static int cpuset_mems_generation;
  150. static struct cpuset top_cpuset = {
  151. .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
  152. .cpus_allowed = CPU_MASK_ALL,
  153. .mems_allowed = NODE_MASK_ALL,
  154. .count = ATOMIC_INIT(0),
  155. .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
  156. .children = LIST_HEAD_INIT(top_cpuset.children),
  157. };
  158. static struct vfsmount *cpuset_mount;
  159. static struct super_block *cpuset_sb;
  160. /*
  161. * We have two global cpuset mutexes below. They can nest.
  162. * It is ok to first take manage_mutex, then nest callback_mutex. We also
  163. * require taking task_lock() when dereferencing a tasks cpuset pointer.
  164. * See "The task_lock() exception", at the end of this comment.
  165. *
  166. * A task must hold both mutexes to modify cpusets. If a task
  167. * holds manage_mutex, then it blocks others wanting that mutex,
  168. * ensuring that it is the only task able to also acquire callback_mutex
  169. * and be able to modify cpusets. It can perform various checks on
  170. * the cpuset structure first, knowing nothing will change. It can
  171. * also allocate memory while just holding manage_mutex. While it is
  172. * performing these checks, various callback routines can briefly
  173. * acquire callback_mutex to query cpusets. Once it is ready to make
  174. * the changes, it takes callback_mutex, blocking everyone else.
  175. *
  176. * Calls to the kernel memory allocator can not be made while holding
  177. * callback_mutex, as that would risk double tripping on callback_mutex
  178. * from one of the callbacks into the cpuset code from within
  179. * __alloc_pages().
  180. *
  181. * If a task is only holding callback_mutex, then it has read-only
  182. * access to cpusets.
  183. *
  184. * The task_struct fields mems_allowed and mems_generation may only
  185. * be accessed in the context of that task, so require no locks.
  186. *
  187. * Any task can increment and decrement the count field without lock.
  188. * So in general, code holding manage_mutex or callback_mutex can't rely
  189. * on the count field not changing. However, if the count goes to
  190. * zero, then only attach_task(), which holds both mutexes, can
  191. * increment it again. Because a count of zero means that no tasks
  192. * are currently attached, therefore there is no way a task attached
  193. * to that cpuset can fork (the other way to increment the count).
  194. * So code holding manage_mutex or callback_mutex can safely assume that
  195. * if the count is zero, it will stay zero. Similarly, if a task
  196. * holds manage_mutex or callback_mutex on a cpuset with zero count, it
  197. * knows that the cpuset won't be removed, as cpuset_rmdir() needs
  198. * both of those mutexes.
  199. *
  200. * The cpuset_common_file_write handler for operations that modify
  201. * the cpuset hierarchy holds manage_mutex across the entire operation,
  202. * single threading all such cpuset modifications across the system.
  203. *
  204. * The cpuset_common_file_read() handlers only hold callback_mutex across
  205. * small pieces of code, such as when reading out possibly multi-word
  206. * cpumasks and nodemasks.
  207. *
  208. * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
  209. * (usually) take either mutex. These are the two most performance
  210. * critical pieces of code here. The exception occurs on cpuset_exit(),
  211. * when a task in a notify_on_release cpuset exits. Then manage_mutex
  212. * is taken, and if the cpuset count is zero, a usermode call made
  213. * to /sbin/cpuset_release_agent with the name of the cpuset (path
  214. * relative to the root of cpuset file system) as the argument.
  215. *
  216. * A cpuset can only be deleted if both its 'count' of using tasks
  217. * is zero, and its list of 'children' cpusets is empty. Since all
  218. * tasks in the system use _some_ cpuset, and since there is always at
  219. * least one task in the system (init), therefore, top_cpuset
  220. * always has either children cpusets and/or using tasks. So we don't
  221. * need a special hack to ensure that top_cpuset cannot be deleted.
  222. *
  223. * The above "Tale of Two Semaphores" would be complete, but for:
  224. *
  225. * The task_lock() exception
  226. *
  227. * The need for this exception arises from the action of attach_task(),
  228. * which overwrites one tasks cpuset pointer with another. It does
  229. * so using both mutexes, however there are several performance
  230. * critical places that need to reference task->cpuset without the
  231. * expense of grabbing a system global mutex. Therefore except as
  232. * noted below, when dereferencing or, as in attach_task(), modifying
  233. * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
  234. * (task->alloc_lock) already in the task_struct routinely used for
  235. * such matters.
  236. *
  237. * P.S. One more locking exception. RCU is used to guard the
  238. * update of a tasks cpuset pointer by attach_task() and the
  239. * access of task->cpuset->mems_generation via that pointer in
  240. * the routine cpuset_update_task_memory_state().
  241. */
  242. static DEFINE_MUTEX(manage_mutex);
  243. static DEFINE_MUTEX(callback_mutex);
  244. /*
  245. * A couple of forward declarations required, due to cyclic reference loop:
  246. * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
  247. * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
  248. */
  249. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  250. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
  251. static struct backing_dev_info cpuset_backing_dev_info = {
  252. .ra_pages = 0, /* No readahead */
  253. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  254. };
  255. static struct inode *cpuset_new_inode(mode_t mode)
  256. {
  257. struct inode *inode = new_inode(cpuset_sb);
  258. if (inode) {
  259. inode->i_mode = mode;
  260. inode->i_uid = current->fsuid;
  261. inode->i_gid = current->fsgid;
  262. inode->i_blocks = 0;
  263. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  264. inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
  265. }
  266. return inode;
  267. }
  268. static void cpuset_diput(struct dentry *dentry, struct inode *inode)
  269. {
  270. /* is dentry a directory ? if so, kfree() associated cpuset */
  271. if (S_ISDIR(inode->i_mode)) {
  272. struct cpuset *cs = dentry->d_fsdata;
  273. BUG_ON(!(is_removed(cs)));
  274. kfree(cs);
  275. }
  276. iput(inode);
  277. }
  278. static struct dentry_operations cpuset_dops = {
  279. .d_iput = cpuset_diput,
  280. };
  281. static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
  282. {
  283. struct dentry *d = lookup_one_len(name, parent, strlen(name));
  284. if (!IS_ERR(d))
  285. d->d_op = &cpuset_dops;
  286. return d;
  287. }
  288. static void remove_dir(struct dentry *d)
  289. {
  290. struct dentry *parent = dget(d->d_parent);
  291. d_delete(d);
  292. simple_rmdir(parent->d_inode, d);
  293. dput(parent);
  294. }
  295. /*
  296. * NOTE : the dentry must have been dget()'ed
  297. */
  298. static void cpuset_d_remove_dir(struct dentry *dentry)
  299. {
  300. struct list_head *node;
  301. spin_lock(&dcache_lock);
  302. node = dentry->d_subdirs.next;
  303. while (node != &dentry->d_subdirs) {
  304. struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
  305. list_del_init(node);
  306. if (d->d_inode) {
  307. d = dget_locked(d);
  308. spin_unlock(&dcache_lock);
  309. d_delete(d);
  310. simple_unlink(dentry->d_inode, d);
  311. dput(d);
  312. spin_lock(&dcache_lock);
  313. }
  314. node = dentry->d_subdirs.next;
  315. }
  316. list_del_init(&dentry->d_u.d_child);
  317. spin_unlock(&dcache_lock);
  318. remove_dir(dentry);
  319. }
  320. static struct super_operations cpuset_ops = {
  321. .statfs = simple_statfs,
  322. .drop_inode = generic_delete_inode,
  323. };
  324. static int cpuset_fill_super(struct super_block *sb, void *unused_data,
  325. int unused_silent)
  326. {
  327. struct inode *inode;
  328. struct dentry *root;
  329. sb->s_blocksize = PAGE_CACHE_SIZE;
  330. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  331. sb->s_magic = CPUSET_SUPER_MAGIC;
  332. sb->s_op = &cpuset_ops;
  333. cpuset_sb = sb;
  334. inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
  335. if (inode) {
  336. inode->i_op = &simple_dir_inode_operations;
  337. inode->i_fop = &simple_dir_operations;
  338. /* directories start off with i_nlink == 2 (for "." entry) */
  339. inc_nlink(inode);
  340. } else {
  341. return -ENOMEM;
  342. }
  343. root = d_alloc_root(inode);
  344. if (!root) {
  345. iput(inode);
  346. return -ENOMEM;
  347. }
  348. sb->s_root = root;
  349. return 0;
  350. }
  351. static int cpuset_get_sb(struct file_system_type *fs_type,
  352. int flags, const char *unused_dev_name,
  353. void *data, struct vfsmount *mnt)
  354. {
  355. return get_sb_single(fs_type, flags, data, cpuset_fill_super, mnt);
  356. }
  357. static struct file_system_type cpuset_fs_type = {
  358. .name = "cpuset",
  359. .get_sb = cpuset_get_sb,
  360. .kill_sb = kill_litter_super,
  361. };
  362. /* struct cftype:
  363. *
  364. * The files in the cpuset filesystem mostly have a very simple read/write
  365. * handling, some common function will take care of it. Nevertheless some cases
  366. * (read tasks) are special and therefore I define this structure for every
  367. * kind of file.
  368. *
  369. *
  370. * When reading/writing to a file:
  371. * - the cpuset to use in file->f_path.dentry->d_parent->d_fsdata
  372. * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
  373. */
  374. struct cftype {
  375. char *name;
  376. int private;
  377. int (*open) (struct inode *inode, struct file *file);
  378. ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
  379. loff_t *ppos);
  380. int (*write) (struct file *file, const char __user *buf, size_t nbytes,
  381. loff_t *ppos);
  382. int (*release) (struct inode *inode, struct file *file);
  383. };
  384. static inline struct cpuset *__d_cs(struct dentry *dentry)
  385. {
  386. return dentry->d_fsdata;
  387. }
  388. static inline struct cftype *__d_cft(struct dentry *dentry)
  389. {
  390. return dentry->d_fsdata;
  391. }
  392. /*
  393. * Call with manage_mutex held. Writes path of cpuset into buf.
  394. * Returns 0 on success, -errno on error.
  395. */
  396. static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
  397. {
  398. char *start;
  399. start = buf + buflen;
  400. *--start = '\0';
  401. for (;;) {
  402. int len = cs->dentry->d_name.len;
  403. if ((start -= len) < buf)
  404. return -ENAMETOOLONG;
  405. memcpy(start, cs->dentry->d_name.name, len);
  406. cs = cs->parent;
  407. if (!cs)
  408. break;
  409. if (!cs->parent)
  410. continue;
  411. if (--start < buf)
  412. return -ENAMETOOLONG;
  413. *start = '/';
  414. }
  415. memmove(buf, start, buf + buflen - start);
  416. return 0;
  417. }
  418. /*
  419. * Notify userspace when a cpuset is released, by running
  420. * /sbin/cpuset_release_agent with the name of the cpuset (path
  421. * relative to the root of cpuset file system) as the argument.
  422. *
  423. * Most likely, this user command will try to rmdir this cpuset.
  424. *
  425. * This races with the possibility that some other task will be
  426. * attached to this cpuset before it is removed, or that some other
  427. * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
  428. * The presumed 'rmdir' will fail quietly if this cpuset is no longer
  429. * unused, and this cpuset will be reprieved from its death sentence,
  430. * to continue to serve a useful existence. Next time it's released,
  431. * we will get notified again, if it still has 'notify_on_release' set.
  432. *
  433. * The final arg to call_usermodehelper() is 0, which means don't
  434. * wait. The separate /sbin/cpuset_release_agent task is forked by
  435. * call_usermodehelper(), then control in this thread returns here,
  436. * without waiting for the release agent task. We don't bother to
  437. * wait because the caller of this routine has no use for the exit
  438. * status of the /sbin/cpuset_release_agent task, so no sense holding
  439. * our caller up for that.
  440. *
  441. * When we had only one cpuset mutex, we had to call this
  442. * without holding it, to avoid deadlock when call_usermodehelper()
  443. * allocated memory. With two locks, we could now call this while
  444. * holding manage_mutex, but we still don't, so as to minimize
  445. * the time manage_mutex is held.
  446. */
  447. static void cpuset_release_agent(const char *pathbuf)
  448. {
  449. char *argv[3], *envp[3];
  450. int i;
  451. if (!pathbuf)
  452. return;
  453. i = 0;
  454. argv[i++] = "/sbin/cpuset_release_agent";
  455. argv[i++] = (char *)pathbuf;
  456. argv[i] = NULL;
  457. i = 0;
  458. /* minimal command environment */
  459. envp[i++] = "HOME=/";
  460. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  461. envp[i] = NULL;
  462. call_usermodehelper(argv[0], argv, envp, 0);
  463. kfree(pathbuf);
  464. }
  465. /*
  466. * Either cs->count of using tasks transitioned to zero, or the
  467. * cs->children list of child cpusets just became empty. If this
  468. * cs is notify_on_release() and now both the user count is zero and
  469. * the list of children is empty, prepare cpuset path in a kmalloc'd
  470. * buffer, to be returned via ppathbuf, so that the caller can invoke
  471. * cpuset_release_agent() with it later on, once manage_mutex is dropped.
  472. * Call here with manage_mutex held.
  473. *
  474. * This check_for_release() routine is responsible for kmalloc'ing
  475. * pathbuf. The above cpuset_release_agent() is responsible for
  476. * kfree'ing pathbuf. The caller of these routines is responsible
  477. * for providing a pathbuf pointer, initialized to NULL, then
  478. * calling check_for_release() with manage_mutex held and the address
  479. * of the pathbuf pointer, then dropping manage_mutex, then calling
  480. * cpuset_release_agent() with pathbuf, as set by check_for_release().
  481. */
  482. static void check_for_release(struct cpuset *cs, char **ppathbuf)
  483. {
  484. if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
  485. list_empty(&cs->children)) {
  486. char *buf;
  487. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  488. if (!buf)
  489. return;
  490. if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
  491. kfree(buf);
  492. else
  493. *ppathbuf = buf;
  494. }
  495. }
  496. /*
  497. * Return in *pmask the portion of a cpusets's cpus_allowed that
  498. * are online. If none are online, walk up the cpuset hierarchy
  499. * until we find one that does have some online cpus. If we get
  500. * all the way to the top and still haven't found any online cpus,
  501. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  502. * task, return cpu_online_map.
  503. *
  504. * One way or another, we guarantee to return some non-empty subset
  505. * of cpu_online_map.
  506. *
  507. * Call with callback_mutex held.
  508. */
  509. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  510. {
  511. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  512. cs = cs->parent;
  513. if (cs)
  514. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  515. else
  516. *pmask = cpu_online_map;
  517. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  518. }
  519. /*
  520. * Return in *pmask the portion of a cpusets's mems_allowed that
  521. * are online. If none are online, walk up the cpuset hierarchy
  522. * until we find one that does have some online mems. If we get
  523. * all the way to the top and still haven't found any online mems,
  524. * return node_online_map.
  525. *
  526. * One way or another, we guarantee to return some non-empty subset
  527. * of node_online_map.
  528. *
  529. * Call with callback_mutex held.
  530. */
  531. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  532. {
  533. while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
  534. cs = cs->parent;
  535. if (cs)
  536. nodes_and(*pmask, cs->mems_allowed, node_online_map);
  537. else
  538. *pmask = node_online_map;
  539. BUG_ON(!nodes_intersects(*pmask, node_online_map));
  540. }
  541. /**
  542. * cpuset_update_task_memory_state - update task memory placement
  543. *
  544. * If the current tasks cpusets mems_allowed changed behind our
  545. * backs, update current->mems_allowed, mems_generation and task NUMA
  546. * mempolicy to the new value.
  547. *
  548. * Task mempolicy is updated by rebinding it relative to the
  549. * current->cpuset if a task has its memory placement changed.
  550. * Do not call this routine if in_interrupt().
  551. *
  552. * Call without callback_mutex or task_lock() held. May be
  553. * called with or without manage_mutex held. Thanks in part to
  554. * 'the_top_cpuset_hack', the tasks cpuset pointer will never
  555. * be NULL. This routine also might acquire callback_mutex and
  556. * current->mm->mmap_sem during call.
  557. *
  558. * Reading current->cpuset->mems_generation doesn't need task_lock
  559. * to guard the current->cpuset derefence, because it is guarded
  560. * from concurrent freeing of current->cpuset by attach_task(),
  561. * using RCU.
  562. *
  563. * The rcu_dereference() is technically probably not needed,
  564. * as I don't actually mind if I see a new cpuset pointer but
  565. * an old value of mems_generation. However this really only
  566. * matters on alpha systems using cpusets heavily. If I dropped
  567. * that rcu_dereference(), it would save them a memory barrier.
  568. * For all other arch's, rcu_dereference is a no-op anyway, and for
  569. * alpha systems not using cpusets, another planned optimization,
  570. * avoiding the rcu critical section for tasks in the root cpuset
  571. * which is statically allocated, so can't vanish, will make this
  572. * irrelevant. Better to use RCU as intended, than to engage in
  573. * some cute trick to save a memory barrier that is impossible to
  574. * test, for alpha systems using cpusets heavily, which might not
  575. * even exist.
  576. *
  577. * This routine is needed to update the per-task mems_allowed data,
  578. * within the tasks context, when it is trying to allocate memory
  579. * (in various mm/mempolicy.c routines) and notices that some other
  580. * task has been modifying its cpuset.
  581. */
  582. void cpuset_update_task_memory_state(void)
  583. {
  584. int my_cpusets_mem_gen;
  585. struct task_struct *tsk = current;
  586. struct cpuset *cs;
  587. if (tsk->cpuset == &top_cpuset) {
  588. /* Don't need rcu for top_cpuset. It's never freed. */
  589. my_cpusets_mem_gen = top_cpuset.mems_generation;
  590. } else {
  591. rcu_read_lock();
  592. cs = rcu_dereference(tsk->cpuset);
  593. my_cpusets_mem_gen = cs->mems_generation;
  594. rcu_read_unlock();
  595. }
  596. if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
  597. mutex_lock(&callback_mutex);
  598. task_lock(tsk);
  599. cs = tsk->cpuset; /* Maybe changed when task not locked */
  600. guarantee_online_mems(cs, &tsk->mems_allowed);
  601. tsk->cpuset_mems_generation = cs->mems_generation;
  602. if (is_spread_page(cs))
  603. tsk->flags |= PF_SPREAD_PAGE;
  604. else
  605. tsk->flags &= ~PF_SPREAD_PAGE;
  606. if (is_spread_slab(cs))
  607. tsk->flags |= PF_SPREAD_SLAB;
  608. else
  609. tsk->flags &= ~PF_SPREAD_SLAB;
  610. task_unlock(tsk);
  611. mutex_unlock(&callback_mutex);
  612. mpol_rebind_task(tsk, &tsk->mems_allowed);
  613. }
  614. }
  615. /*
  616. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  617. *
  618. * One cpuset is a subset of another if all its allowed CPUs and
  619. * Memory Nodes are a subset of the other, and its exclusive flags
  620. * are only set if the other's are set. Call holding manage_mutex.
  621. */
  622. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  623. {
  624. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  625. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  626. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  627. is_mem_exclusive(p) <= is_mem_exclusive(q);
  628. }
  629. /*
  630. * validate_change() - Used to validate that any proposed cpuset change
  631. * follows the structural rules for cpusets.
  632. *
  633. * If we replaced the flag and mask values of the current cpuset
  634. * (cur) with those values in the trial cpuset (trial), would
  635. * our various subset and exclusive rules still be valid? Presumes
  636. * manage_mutex held.
  637. *
  638. * 'cur' is the address of an actual, in-use cpuset. Operations
  639. * such as list traversal that depend on the actual address of the
  640. * cpuset in the list must use cur below, not trial.
  641. *
  642. * 'trial' is the address of bulk structure copy of cur, with
  643. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  644. * or flags changed to new, trial values.
  645. *
  646. * Return 0 if valid, -errno if not.
  647. */
  648. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  649. {
  650. struct cpuset *c, *par;
  651. /* Each of our child cpusets must be a subset of us */
  652. list_for_each_entry(c, &cur->children, sibling) {
  653. if (!is_cpuset_subset(c, trial))
  654. return -EBUSY;
  655. }
  656. /* Remaining checks don't apply to root cpuset */
  657. if (cur == &top_cpuset)
  658. return 0;
  659. par = cur->parent;
  660. /* We must be a subset of our parent cpuset */
  661. if (!is_cpuset_subset(trial, par))
  662. return -EACCES;
  663. /* If either I or some sibling (!= me) is exclusive, we can't overlap */
  664. list_for_each_entry(c, &par->children, sibling) {
  665. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  666. c != cur &&
  667. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  668. return -EINVAL;
  669. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  670. c != cur &&
  671. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  672. return -EINVAL;
  673. }
  674. return 0;
  675. }
  676. /*
  677. * For a given cpuset cur, partition the system as follows
  678. * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
  679. * exclusive child cpusets
  680. * b. All cpus in the current cpuset's cpus_allowed that are not part of any
  681. * exclusive child cpusets
  682. * Build these two partitions by calling partition_sched_domains
  683. *
  684. * Call with manage_mutex held. May nest a call to the
  685. * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
  686. * Must not be called holding callback_mutex, because we must
  687. * not call lock_cpu_hotplug() while holding callback_mutex.
  688. */
  689. static void update_cpu_domains(struct cpuset *cur)
  690. {
  691. struct cpuset *c, *par = cur->parent;
  692. cpumask_t pspan, cspan;
  693. if (par == NULL || cpus_empty(cur->cpus_allowed))
  694. return;
  695. /*
  696. * Get all cpus from parent's cpus_allowed not part of exclusive
  697. * children
  698. */
  699. pspan = par->cpus_allowed;
  700. list_for_each_entry(c, &par->children, sibling) {
  701. if (is_cpu_exclusive(c))
  702. cpus_andnot(pspan, pspan, c->cpus_allowed);
  703. }
  704. if (!is_cpu_exclusive(cur)) {
  705. cpus_or(pspan, pspan, cur->cpus_allowed);
  706. if (cpus_equal(pspan, cur->cpus_allowed))
  707. return;
  708. cspan = CPU_MASK_NONE;
  709. } else {
  710. if (cpus_empty(pspan))
  711. return;
  712. cspan = cur->cpus_allowed;
  713. /*
  714. * Get all cpus from current cpuset's cpus_allowed not part
  715. * of exclusive children
  716. */
  717. list_for_each_entry(c, &cur->children, sibling) {
  718. if (is_cpu_exclusive(c))
  719. cpus_andnot(cspan, cspan, c->cpus_allowed);
  720. }
  721. }
  722. lock_cpu_hotplug();
  723. partition_sched_domains(&pspan, &cspan);
  724. unlock_cpu_hotplug();
  725. }
  726. /*
  727. * Call with manage_mutex held. May take callback_mutex during call.
  728. */
  729. static int update_cpumask(struct cpuset *cs, char *buf)
  730. {
  731. struct cpuset trialcs;
  732. int retval, cpus_unchanged;
  733. /* top_cpuset.cpus_allowed tracks cpu_online_map; it's read-only */
  734. if (cs == &top_cpuset)
  735. return -EACCES;
  736. trialcs = *cs;
  737. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  738. if (retval < 0)
  739. return retval;
  740. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  741. if (cpus_empty(trialcs.cpus_allowed))
  742. return -ENOSPC;
  743. retval = validate_change(cs, &trialcs);
  744. if (retval < 0)
  745. return retval;
  746. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  747. mutex_lock(&callback_mutex);
  748. cs->cpus_allowed = trialcs.cpus_allowed;
  749. mutex_unlock(&callback_mutex);
  750. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  751. update_cpu_domains(cs);
  752. return 0;
  753. }
  754. /*
  755. * cpuset_migrate_mm
  756. *
  757. * Migrate memory region from one set of nodes to another.
  758. *
  759. * Temporarilly set tasks mems_allowed to target nodes of migration,
  760. * so that the migration code can allocate pages on these nodes.
  761. *
  762. * Call holding manage_mutex, so our current->cpuset won't change
  763. * during this call, as manage_mutex holds off any attach_task()
  764. * calls. Therefore we don't need to take task_lock around the
  765. * call to guarantee_online_mems(), as we know no one is changing
  766. * our tasks cpuset.
  767. *
  768. * Hold callback_mutex around the two modifications of our tasks
  769. * mems_allowed to synchronize with cpuset_mems_allowed().
  770. *
  771. * While the mm_struct we are migrating is typically from some
  772. * other task, the task_struct mems_allowed that we are hacking
  773. * is for our current task, which must allocate new pages for that
  774. * migrating memory region.
  775. *
  776. * We call cpuset_update_task_memory_state() before hacking
  777. * our tasks mems_allowed, so that we are assured of being in
  778. * sync with our tasks cpuset, and in particular, callbacks to
  779. * cpuset_update_task_memory_state() from nested page allocations
  780. * won't see any mismatch of our cpuset and task mems_generation
  781. * values, so won't overwrite our hacked tasks mems_allowed
  782. * nodemask.
  783. */
  784. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  785. const nodemask_t *to)
  786. {
  787. struct task_struct *tsk = current;
  788. cpuset_update_task_memory_state();
  789. mutex_lock(&callback_mutex);
  790. tsk->mems_allowed = *to;
  791. mutex_unlock(&callback_mutex);
  792. do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
  793. mutex_lock(&callback_mutex);
  794. guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed);
  795. mutex_unlock(&callback_mutex);
  796. }
  797. /*
  798. * Handle user request to change the 'mems' memory placement
  799. * of a cpuset. Needs to validate the request, update the
  800. * cpusets mems_allowed and mems_generation, and for each
  801. * task in the cpuset, rebind any vma mempolicies and if
  802. * the cpuset is marked 'memory_migrate', migrate the tasks
  803. * pages to the new memory.
  804. *
  805. * Call with manage_mutex held. May take callback_mutex during call.
  806. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  807. * lock each such tasks mm->mmap_sem, scan its vma's and rebind
  808. * their mempolicies to the cpusets new mems_allowed.
  809. */
  810. static int update_nodemask(struct cpuset *cs, char *buf)
  811. {
  812. struct cpuset trialcs;
  813. nodemask_t oldmem;
  814. struct task_struct *g, *p;
  815. struct mm_struct **mmarray;
  816. int i, n, ntasks;
  817. int migrate;
  818. int fudge;
  819. int retval;
  820. /* top_cpuset.mems_allowed tracks node_online_map; it's read-only */
  821. if (cs == &top_cpuset)
  822. return -EACCES;
  823. trialcs = *cs;
  824. retval = nodelist_parse(buf, trialcs.mems_allowed);
  825. if (retval < 0)
  826. goto done;
  827. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  828. oldmem = cs->mems_allowed;
  829. if (nodes_equal(oldmem, trialcs.mems_allowed)) {
  830. retval = 0; /* Too easy - nothing to do */
  831. goto done;
  832. }
  833. if (nodes_empty(trialcs.mems_allowed)) {
  834. retval = -ENOSPC;
  835. goto done;
  836. }
  837. retval = validate_change(cs, &trialcs);
  838. if (retval < 0)
  839. goto done;
  840. mutex_lock(&callback_mutex);
  841. cs->mems_allowed = trialcs.mems_allowed;
  842. cs->mems_generation = cpuset_mems_generation++;
  843. mutex_unlock(&callback_mutex);
  844. set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
  845. fudge = 10; /* spare mmarray[] slots */
  846. fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
  847. retval = -ENOMEM;
  848. /*
  849. * Allocate mmarray[] to hold mm reference for each task
  850. * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
  851. * tasklist_lock. We could use GFP_ATOMIC, but with a
  852. * few more lines of code, we can retry until we get a big
  853. * enough mmarray[] w/o using GFP_ATOMIC.
  854. */
  855. while (1) {
  856. ntasks = atomic_read(&cs->count); /* guess */
  857. ntasks += fudge;
  858. mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
  859. if (!mmarray)
  860. goto done;
  861. write_lock_irq(&tasklist_lock); /* block fork */
  862. if (atomic_read(&cs->count) <= ntasks)
  863. break; /* got enough */
  864. write_unlock_irq(&tasklist_lock); /* try again */
  865. kfree(mmarray);
  866. }
  867. n = 0;
  868. /* Load up mmarray[] with mm reference for each task in cpuset. */
  869. do_each_thread(g, p) {
  870. struct mm_struct *mm;
  871. if (n >= ntasks) {
  872. printk(KERN_WARNING
  873. "Cpuset mempolicy rebind incomplete.\n");
  874. continue;
  875. }
  876. if (p->cpuset != cs)
  877. continue;
  878. mm = get_task_mm(p);
  879. if (!mm)
  880. continue;
  881. mmarray[n++] = mm;
  882. } while_each_thread(g, p);
  883. write_unlock_irq(&tasklist_lock);
  884. /*
  885. * Now that we've dropped the tasklist spinlock, we can
  886. * rebind the vma mempolicies of each mm in mmarray[] to their
  887. * new cpuset, and release that mm. The mpol_rebind_mm()
  888. * call takes mmap_sem, which we couldn't take while holding
  889. * tasklist_lock. Forks can happen again now - the mpol_copy()
  890. * cpuset_being_rebound check will catch such forks, and rebind
  891. * their vma mempolicies too. Because we still hold the global
  892. * cpuset manage_mutex, we know that no other rebind effort will
  893. * be contending for the global variable cpuset_being_rebound.
  894. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  895. * is idempotent. Also migrate pages in each mm to new nodes.
  896. */
  897. migrate = is_memory_migrate(cs);
  898. for (i = 0; i < n; i++) {
  899. struct mm_struct *mm = mmarray[i];
  900. mpol_rebind_mm(mm, &cs->mems_allowed);
  901. if (migrate)
  902. cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
  903. mmput(mm);
  904. }
  905. /* We're done rebinding vma's to this cpusets new mems_allowed. */
  906. kfree(mmarray);
  907. set_cpuset_being_rebound(NULL);
  908. retval = 0;
  909. done:
  910. return retval;
  911. }
  912. /*
  913. * Call with manage_mutex held.
  914. */
  915. static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
  916. {
  917. if (simple_strtoul(buf, NULL, 10) != 0)
  918. cpuset_memory_pressure_enabled = 1;
  919. else
  920. cpuset_memory_pressure_enabled = 0;
  921. return 0;
  922. }
  923. /*
  924. * update_flag - read a 0 or a 1 in a file and update associated flag
  925. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  926. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
  927. * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
  928. * cs: the cpuset to update
  929. * buf: the buffer where we read the 0 or 1
  930. *
  931. * Call with manage_mutex held.
  932. */
  933. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  934. {
  935. int turning_on;
  936. struct cpuset trialcs;
  937. int err, cpu_exclusive_changed;
  938. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  939. trialcs = *cs;
  940. if (turning_on)
  941. set_bit(bit, &trialcs.flags);
  942. else
  943. clear_bit(bit, &trialcs.flags);
  944. err = validate_change(cs, &trialcs);
  945. if (err < 0)
  946. return err;
  947. cpu_exclusive_changed =
  948. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  949. mutex_lock(&callback_mutex);
  950. cs->flags = trialcs.flags;
  951. mutex_unlock(&callback_mutex);
  952. if (cpu_exclusive_changed)
  953. update_cpu_domains(cs);
  954. return 0;
  955. }
  956. /*
  957. * Frequency meter - How fast is some event occurring?
  958. *
  959. * These routines manage a digitally filtered, constant time based,
  960. * event frequency meter. There are four routines:
  961. * fmeter_init() - initialize a frequency meter.
  962. * fmeter_markevent() - called each time the event happens.
  963. * fmeter_getrate() - returns the recent rate of such events.
  964. * fmeter_update() - internal routine used to update fmeter.
  965. *
  966. * A common data structure is passed to each of these routines,
  967. * which is used to keep track of the state required to manage the
  968. * frequency meter and its digital filter.
  969. *
  970. * The filter works on the number of events marked per unit time.
  971. * The filter is single-pole low-pass recursive (IIR). The time unit
  972. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  973. * simulate 3 decimal digits of precision (multiplied by 1000).
  974. *
  975. * With an FM_COEF of 933, and a time base of 1 second, the filter
  976. * has a half-life of 10 seconds, meaning that if the events quit
  977. * happening, then the rate returned from the fmeter_getrate()
  978. * will be cut in half each 10 seconds, until it converges to zero.
  979. *
  980. * It is not worth doing a real infinitely recursive filter. If more
  981. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  982. * just compute FM_MAXTICKS ticks worth, by which point the level
  983. * will be stable.
  984. *
  985. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  986. * arithmetic overflow in the fmeter_update() routine.
  987. *
  988. * Given the simple 32 bit integer arithmetic used, this meter works
  989. * best for reporting rates between one per millisecond (msec) and
  990. * one per 32 (approx) seconds. At constant rates faster than one
  991. * per msec it maxes out at values just under 1,000,000. At constant
  992. * rates between one per msec, and one per second it will stabilize
  993. * to a value N*1000, where N is the rate of events per second.
  994. * At constant rates between one per second and one per 32 seconds,
  995. * it will be choppy, moving up on the seconds that have an event,
  996. * and then decaying until the next event. At rates slower than
  997. * about one in 32 seconds, it decays all the way back to zero between
  998. * each event.
  999. */
  1000. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  1001. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  1002. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  1003. #define FM_SCALE 1000 /* faux fixed point scale */
  1004. /* Initialize a frequency meter */
  1005. static void fmeter_init(struct fmeter *fmp)
  1006. {
  1007. fmp->cnt = 0;
  1008. fmp->val = 0;
  1009. fmp->time = 0;
  1010. spin_lock_init(&fmp->lock);
  1011. }
  1012. /* Internal meter update - process cnt events and update value */
  1013. static void fmeter_update(struct fmeter *fmp)
  1014. {
  1015. time_t now = get_seconds();
  1016. time_t ticks = now - fmp->time;
  1017. if (ticks == 0)
  1018. return;
  1019. ticks = min(FM_MAXTICKS, ticks);
  1020. while (ticks-- > 0)
  1021. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  1022. fmp->time = now;
  1023. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  1024. fmp->cnt = 0;
  1025. }
  1026. /* Process any previous ticks, then bump cnt by one (times scale). */
  1027. static void fmeter_markevent(struct fmeter *fmp)
  1028. {
  1029. spin_lock(&fmp->lock);
  1030. fmeter_update(fmp);
  1031. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  1032. spin_unlock(&fmp->lock);
  1033. }
  1034. /* Process any previous ticks, then return current value. */
  1035. static int fmeter_getrate(struct fmeter *fmp)
  1036. {
  1037. int val;
  1038. spin_lock(&fmp->lock);
  1039. fmeter_update(fmp);
  1040. val = fmp->val;
  1041. spin_unlock(&fmp->lock);
  1042. return val;
  1043. }
  1044. /*
  1045. * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
  1046. * writing the path of the old cpuset in 'ppathbuf' if it needs to be
  1047. * notified on release.
  1048. *
  1049. * Call holding manage_mutex. May take callback_mutex and task_lock of
  1050. * the task 'pid' during call.
  1051. */
  1052. static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
  1053. {
  1054. pid_t pid;
  1055. struct task_struct *tsk;
  1056. struct cpuset *oldcs;
  1057. cpumask_t cpus;
  1058. nodemask_t from, to;
  1059. struct mm_struct *mm;
  1060. int retval;
  1061. if (sscanf(pidbuf, "%d", &pid) != 1)
  1062. return -EIO;
  1063. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  1064. return -ENOSPC;
  1065. if (pid) {
  1066. read_lock(&tasklist_lock);
  1067. tsk = find_task_by_pid(pid);
  1068. if (!tsk || tsk->flags & PF_EXITING) {
  1069. read_unlock(&tasklist_lock);
  1070. return -ESRCH;
  1071. }
  1072. get_task_struct(tsk);
  1073. read_unlock(&tasklist_lock);
  1074. if ((current->euid) && (current->euid != tsk->uid)
  1075. && (current->euid != tsk->suid)) {
  1076. put_task_struct(tsk);
  1077. return -EACCES;
  1078. }
  1079. } else {
  1080. tsk = current;
  1081. get_task_struct(tsk);
  1082. }
  1083. retval = security_task_setscheduler(tsk, 0, NULL);
  1084. if (retval) {
  1085. put_task_struct(tsk);
  1086. return retval;
  1087. }
  1088. mutex_lock(&callback_mutex);
  1089. task_lock(tsk);
  1090. oldcs = tsk->cpuset;
  1091. /*
  1092. * After getting 'oldcs' cpuset ptr, be sure still not exiting.
  1093. * If 'oldcs' might be the top_cpuset due to the_top_cpuset_hack
  1094. * then fail this attach_task(), to avoid breaking top_cpuset.count.
  1095. */
  1096. if (tsk->flags & PF_EXITING) {
  1097. task_unlock(tsk);
  1098. mutex_unlock(&callback_mutex);
  1099. put_task_struct(tsk);
  1100. return -ESRCH;
  1101. }
  1102. atomic_inc(&cs->count);
  1103. rcu_assign_pointer(tsk->cpuset, cs);
  1104. task_unlock(tsk);
  1105. guarantee_online_cpus(cs, &cpus);
  1106. set_cpus_allowed(tsk, cpus);
  1107. from = oldcs->mems_allowed;
  1108. to = cs->mems_allowed;
  1109. mutex_unlock(&callback_mutex);
  1110. mm = get_task_mm(tsk);
  1111. if (mm) {
  1112. mpol_rebind_mm(mm, &to);
  1113. if (is_memory_migrate(cs))
  1114. cpuset_migrate_mm(mm, &from, &to);
  1115. mmput(mm);
  1116. }
  1117. put_task_struct(tsk);
  1118. synchronize_rcu();
  1119. if (atomic_dec_and_test(&oldcs->count))
  1120. check_for_release(oldcs, ppathbuf);
  1121. return 0;
  1122. }
  1123. /* The various types of files and directories in a cpuset file system */
  1124. typedef enum {
  1125. FILE_ROOT,
  1126. FILE_DIR,
  1127. FILE_MEMORY_MIGRATE,
  1128. FILE_CPULIST,
  1129. FILE_MEMLIST,
  1130. FILE_CPU_EXCLUSIVE,
  1131. FILE_MEM_EXCLUSIVE,
  1132. FILE_NOTIFY_ON_RELEASE,
  1133. FILE_MEMORY_PRESSURE_ENABLED,
  1134. FILE_MEMORY_PRESSURE,
  1135. FILE_SPREAD_PAGE,
  1136. FILE_SPREAD_SLAB,
  1137. FILE_TASKLIST,
  1138. } cpuset_filetype_t;
  1139. static ssize_t cpuset_common_file_write(struct file *file,
  1140. const char __user *userbuf,
  1141. size_t nbytes, loff_t *unused_ppos)
  1142. {
  1143. struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
  1144. struct cftype *cft = __d_cft(file->f_path.dentry);
  1145. cpuset_filetype_t type = cft->private;
  1146. char *buffer;
  1147. char *pathbuf = NULL;
  1148. int retval = 0;
  1149. /* Crude upper limit on largest legitimate cpulist user might write. */
  1150. if (nbytes > 100 + 6 * max(NR_CPUS, MAX_NUMNODES))
  1151. return -E2BIG;
  1152. /* +1 for nul-terminator */
  1153. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  1154. return -ENOMEM;
  1155. if (copy_from_user(buffer, userbuf, nbytes)) {
  1156. retval = -EFAULT;
  1157. goto out1;
  1158. }
  1159. buffer[nbytes] = 0; /* nul-terminate */
  1160. mutex_lock(&manage_mutex);
  1161. if (is_removed(cs)) {
  1162. retval = -ENODEV;
  1163. goto out2;
  1164. }
  1165. switch (type) {
  1166. case FILE_CPULIST:
  1167. retval = update_cpumask(cs, buffer);
  1168. break;
  1169. case FILE_MEMLIST:
  1170. retval = update_nodemask(cs, buffer);
  1171. break;
  1172. case FILE_CPU_EXCLUSIVE:
  1173. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  1174. break;
  1175. case FILE_MEM_EXCLUSIVE:
  1176. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  1177. break;
  1178. case FILE_NOTIFY_ON_RELEASE:
  1179. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  1180. break;
  1181. case FILE_MEMORY_MIGRATE:
  1182. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  1183. break;
  1184. case FILE_MEMORY_PRESSURE_ENABLED:
  1185. retval = update_memory_pressure_enabled(cs, buffer);
  1186. break;
  1187. case FILE_MEMORY_PRESSURE:
  1188. retval = -EACCES;
  1189. break;
  1190. case FILE_SPREAD_PAGE:
  1191. retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
  1192. cs->mems_generation = cpuset_mems_generation++;
  1193. break;
  1194. case FILE_SPREAD_SLAB:
  1195. retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
  1196. cs->mems_generation = cpuset_mems_generation++;
  1197. break;
  1198. case FILE_TASKLIST:
  1199. retval = attach_task(cs, buffer, &pathbuf);
  1200. break;
  1201. default:
  1202. retval = -EINVAL;
  1203. goto out2;
  1204. }
  1205. if (retval == 0)
  1206. retval = nbytes;
  1207. out2:
  1208. mutex_unlock(&manage_mutex);
  1209. cpuset_release_agent(pathbuf);
  1210. out1:
  1211. kfree(buffer);
  1212. return retval;
  1213. }
  1214. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  1215. size_t nbytes, loff_t *ppos)
  1216. {
  1217. ssize_t retval = 0;
  1218. struct cftype *cft = __d_cft(file->f_path.dentry);
  1219. if (!cft)
  1220. return -ENODEV;
  1221. /* special function ? */
  1222. if (cft->write)
  1223. retval = cft->write(file, buf, nbytes, ppos);
  1224. else
  1225. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  1226. return retval;
  1227. }
  1228. /*
  1229. * These ascii lists should be read in a single call, by using a user
  1230. * buffer large enough to hold the entire map. If read in smaller
  1231. * chunks, there is no guarantee of atomicity. Since the display format
  1232. * used, list of ranges of sequential numbers, is variable length,
  1233. * and since these maps can change value dynamically, one could read
  1234. * gibberish by doing partial reads while a list was changing.
  1235. * A single large read to a buffer that crosses a page boundary is
  1236. * ok, because the result being copied to user land is not recomputed
  1237. * across a page fault.
  1238. */
  1239. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1240. {
  1241. cpumask_t mask;
  1242. mutex_lock(&callback_mutex);
  1243. mask = cs->cpus_allowed;
  1244. mutex_unlock(&callback_mutex);
  1245. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  1246. }
  1247. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1248. {
  1249. nodemask_t mask;
  1250. mutex_lock(&callback_mutex);
  1251. mask = cs->mems_allowed;
  1252. mutex_unlock(&callback_mutex);
  1253. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  1254. }
  1255. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  1256. size_t nbytes, loff_t *ppos)
  1257. {
  1258. struct cftype *cft = __d_cft(file->f_path.dentry);
  1259. struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
  1260. cpuset_filetype_t type = cft->private;
  1261. char *page;
  1262. ssize_t retval = 0;
  1263. char *s;
  1264. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  1265. return -ENOMEM;
  1266. s = page;
  1267. switch (type) {
  1268. case FILE_CPULIST:
  1269. s += cpuset_sprintf_cpulist(s, cs);
  1270. break;
  1271. case FILE_MEMLIST:
  1272. s += cpuset_sprintf_memlist(s, cs);
  1273. break;
  1274. case FILE_CPU_EXCLUSIVE:
  1275. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  1276. break;
  1277. case FILE_MEM_EXCLUSIVE:
  1278. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  1279. break;
  1280. case FILE_NOTIFY_ON_RELEASE:
  1281. *s++ = notify_on_release(cs) ? '1' : '0';
  1282. break;
  1283. case FILE_MEMORY_MIGRATE:
  1284. *s++ = is_memory_migrate(cs) ? '1' : '0';
  1285. break;
  1286. case FILE_MEMORY_PRESSURE_ENABLED:
  1287. *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
  1288. break;
  1289. case FILE_MEMORY_PRESSURE:
  1290. s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
  1291. break;
  1292. case FILE_SPREAD_PAGE:
  1293. *s++ = is_spread_page(cs) ? '1' : '0';
  1294. break;
  1295. case FILE_SPREAD_SLAB:
  1296. *s++ = is_spread_slab(cs) ? '1' : '0';
  1297. break;
  1298. default:
  1299. retval = -EINVAL;
  1300. goto out;
  1301. }
  1302. *s++ = '\n';
  1303. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1304. out:
  1305. free_page((unsigned long)page);
  1306. return retval;
  1307. }
  1308. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  1309. loff_t *ppos)
  1310. {
  1311. ssize_t retval = 0;
  1312. struct cftype *cft = __d_cft(file->f_path.dentry);
  1313. if (!cft)
  1314. return -ENODEV;
  1315. /* special function ? */
  1316. if (cft->read)
  1317. retval = cft->read(file, buf, nbytes, ppos);
  1318. else
  1319. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  1320. return retval;
  1321. }
  1322. static int cpuset_file_open(struct inode *inode, struct file *file)
  1323. {
  1324. int err;
  1325. struct cftype *cft;
  1326. err = generic_file_open(inode, file);
  1327. if (err)
  1328. return err;
  1329. cft = __d_cft(file->f_path.dentry);
  1330. if (!cft)
  1331. return -ENODEV;
  1332. if (cft->open)
  1333. err = cft->open(inode, file);
  1334. else
  1335. err = 0;
  1336. return err;
  1337. }
  1338. static int cpuset_file_release(struct inode *inode, struct file *file)
  1339. {
  1340. struct cftype *cft = __d_cft(file->f_path.dentry);
  1341. if (cft->release)
  1342. return cft->release(inode, file);
  1343. return 0;
  1344. }
  1345. /*
  1346. * cpuset_rename - Only allow simple rename of directories in place.
  1347. */
  1348. static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
  1349. struct inode *new_dir, struct dentry *new_dentry)
  1350. {
  1351. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1352. return -ENOTDIR;
  1353. if (new_dentry->d_inode)
  1354. return -EEXIST;
  1355. if (old_dir != new_dir)
  1356. return -EIO;
  1357. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1358. }
  1359. static const struct file_operations cpuset_file_operations = {
  1360. .read = cpuset_file_read,
  1361. .write = cpuset_file_write,
  1362. .llseek = generic_file_llseek,
  1363. .open = cpuset_file_open,
  1364. .release = cpuset_file_release,
  1365. };
  1366. static const struct inode_operations cpuset_dir_inode_operations = {
  1367. .lookup = simple_lookup,
  1368. .mkdir = cpuset_mkdir,
  1369. .rmdir = cpuset_rmdir,
  1370. .rename = cpuset_rename,
  1371. };
  1372. static int cpuset_create_file(struct dentry *dentry, int mode)
  1373. {
  1374. struct inode *inode;
  1375. if (!dentry)
  1376. return -ENOENT;
  1377. if (dentry->d_inode)
  1378. return -EEXIST;
  1379. inode = cpuset_new_inode(mode);
  1380. if (!inode)
  1381. return -ENOMEM;
  1382. if (S_ISDIR(mode)) {
  1383. inode->i_op = &cpuset_dir_inode_operations;
  1384. inode->i_fop = &simple_dir_operations;
  1385. /* start off with i_nlink == 2 (for "." entry) */
  1386. inc_nlink(inode);
  1387. } else if (S_ISREG(mode)) {
  1388. inode->i_size = 0;
  1389. inode->i_fop = &cpuset_file_operations;
  1390. }
  1391. d_instantiate(dentry, inode);
  1392. dget(dentry); /* Extra count - pin the dentry in core */
  1393. return 0;
  1394. }
  1395. /*
  1396. * cpuset_create_dir - create a directory for an object.
  1397. * cs: the cpuset we create the directory for.
  1398. * It must have a valid ->parent field
  1399. * And we are going to fill its ->dentry field.
  1400. * name: The name to give to the cpuset directory. Will be copied.
  1401. * mode: mode to set on new directory.
  1402. */
  1403. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  1404. {
  1405. struct dentry *dentry = NULL;
  1406. struct dentry *parent;
  1407. int error = 0;
  1408. parent = cs->parent->dentry;
  1409. dentry = cpuset_get_dentry(parent, name);
  1410. if (IS_ERR(dentry))
  1411. return PTR_ERR(dentry);
  1412. error = cpuset_create_file(dentry, S_IFDIR | mode);
  1413. if (!error) {
  1414. dentry->d_fsdata = cs;
  1415. inc_nlink(parent->d_inode);
  1416. cs->dentry = dentry;
  1417. }
  1418. dput(dentry);
  1419. return error;
  1420. }
  1421. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  1422. {
  1423. struct dentry *dentry;
  1424. int error;
  1425. mutex_lock(&dir->d_inode->i_mutex);
  1426. dentry = cpuset_get_dentry(dir, cft->name);
  1427. if (!IS_ERR(dentry)) {
  1428. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  1429. if (!error)
  1430. dentry->d_fsdata = (void *)cft;
  1431. dput(dentry);
  1432. } else
  1433. error = PTR_ERR(dentry);
  1434. mutex_unlock(&dir->d_inode->i_mutex);
  1435. return error;
  1436. }
  1437. /*
  1438. * Stuff for reading the 'tasks' file.
  1439. *
  1440. * Reading this file can return large amounts of data if a cpuset has
  1441. * *lots* of attached tasks. So it may need several calls to read(),
  1442. * but we cannot guarantee that the information we produce is correct
  1443. * unless we produce it entirely atomically.
  1444. *
  1445. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1446. * will have a pointer to an array (also allocated here). The struct
  1447. * ctr_struct * is stored in file->private_data. Its resources will
  1448. * be freed by release() when the file is closed. The array is used
  1449. * to sprintf the PIDs and then used by read().
  1450. */
  1451. /* cpusets_tasks_read array */
  1452. struct ctr_struct {
  1453. char *buf;
  1454. int bufsz;
  1455. };
  1456. /*
  1457. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1458. * Return actual number of pids loaded. No need to task_lock(p)
  1459. * when reading out p->cpuset, as we don't really care if it changes
  1460. * on the next cycle, and we are not going to try to dereference it.
  1461. */
  1462. static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1463. {
  1464. int n = 0;
  1465. struct task_struct *g, *p;
  1466. read_lock(&tasklist_lock);
  1467. do_each_thread(g, p) {
  1468. if (p->cpuset == cs) {
  1469. pidarray[n++] = p->pid;
  1470. if (unlikely(n == npids))
  1471. goto array_full;
  1472. }
  1473. } while_each_thread(g, p);
  1474. array_full:
  1475. read_unlock(&tasklist_lock);
  1476. return n;
  1477. }
  1478. static int cmppid(const void *a, const void *b)
  1479. {
  1480. return *(pid_t *)a - *(pid_t *)b;
  1481. }
  1482. /*
  1483. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1484. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1485. * count 'cnt' of how many chars would be written if buf were large enough.
  1486. */
  1487. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1488. {
  1489. int cnt = 0;
  1490. int i;
  1491. for (i = 0; i < npids; i++)
  1492. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1493. return cnt;
  1494. }
  1495. /*
  1496. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1497. * process id's of tasks currently attached to the cpuset being opened.
  1498. *
  1499. * Does not require any specific cpuset mutexes, and does not take any.
  1500. */
  1501. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1502. {
  1503. struct cpuset *cs = __d_cs(file->f_path.dentry->d_parent);
  1504. struct ctr_struct *ctr;
  1505. pid_t *pidarray;
  1506. int npids;
  1507. char c;
  1508. if (!(file->f_mode & FMODE_READ))
  1509. return 0;
  1510. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1511. if (!ctr)
  1512. goto err0;
  1513. /*
  1514. * If cpuset gets more users after we read count, we won't have
  1515. * enough space - tough. This race is indistinguishable to the
  1516. * caller from the case that the additional cpuset users didn't
  1517. * show up until sometime later on.
  1518. */
  1519. npids = atomic_read(&cs->count);
  1520. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1521. if (!pidarray)
  1522. goto err1;
  1523. npids = pid_array_load(pidarray, npids, cs);
  1524. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1525. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1526. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1527. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1528. if (!ctr->buf)
  1529. goto err2;
  1530. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1531. kfree(pidarray);
  1532. file->private_data = ctr;
  1533. return 0;
  1534. err2:
  1535. kfree(pidarray);
  1536. err1:
  1537. kfree(ctr);
  1538. err0:
  1539. return -ENOMEM;
  1540. }
  1541. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1542. size_t nbytes, loff_t *ppos)
  1543. {
  1544. struct ctr_struct *ctr = file->private_data;
  1545. return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
  1546. }
  1547. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1548. {
  1549. struct ctr_struct *ctr;
  1550. if (file->f_mode & FMODE_READ) {
  1551. ctr = file->private_data;
  1552. kfree(ctr->buf);
  1553. kfree(ctr);
  1554. }
  1555. return 0;
  1556. }
  1557. /*
  1558. * for the common functions, 'private' gives the type of file
  1559. */
  1560. static struct cftype cft_tasks = {
  1561. .name = "tasks",
  1562. .open = cpuset_tasks_open,
  1563. .read = cpuset_tasks_read,
  1564. .release = cpuset_tasks_release,
  1565. .private = FILE_TASKLIST,
  1566. };
  1567. static struct cftype cft_cpus = {
  1568. .name = "cpus",
  1569. .private = FILE_CPULIST,
  1570. };
  1571. static struct cftype cft_mems = {
  1572. .name = "mems",
  1573. .private = FILE_MEMLIST,
  1574. };
  1575. static struct cftype cft_cpu_exclusive = {
  1576. .name = "cpu_exclusive",
  1577. .private = FILE_CPU_EXCLUSIVE,
  1578. };
  1579. static struct cftype cft_mem_exclusive = {
  1580. .name = "mem_exclusive",
  1581. .private = FILE_MEM_EXCLUSIVE,
  1582. };
  1583. static struct cftype cft_notify_on_release = {
  1584. .name = "notify_on_release",
  1585. .private = FILE_NOTIFY_ON_RELEASE,
  1586. };
  1587. static struct cftype cft_memory_migrate = {
  1588. .name = "memory_migrate",
  1589. .private = FILE_MEMORY_MIGRATE,
  1590. };
  1591. static struct cftype cft_memory_pressure_enabled = {
  1592. .name = "memory_pressure_enabled",
  1593. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1594. };
  1595. static struct cftype cft_memory_pressure = {
  1596. .name = "memory_pressure",
  1597. .private = FILE_MEMORY_PRESSURE,
  1598. };
  1599. static struct cftype cft_spread_page = {
  1600. .name = "memory_spread_page",
  1601. .private = FILE_SPREAD_PAGE,
  1602. };
  1603. static struct cftype cft_spread_slab = {
  1604. .name = "memory_spread_slab",
  1605. .private = FILE_SPREAD_SLAB,
  1606. };
  1607. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1608. {
  1609. int err;
  1610. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1611. return err;
  1612. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1613. return err;
  1614. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1615. return err;
  1616. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1617. return err;
  1618. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1619. return err;
  1620. if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
  1621. return err;
  1622. if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
  1623. return err;
  1624. if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0)
  1625. return err;
  1626. if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0)
  1627. return err;
  1628. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1629. return err;
  1630. return 0;
  1631. }
  1632. /*
  1633. * cpuset_create - create a cpuset
  1634. * parent: cpuset that will be parent of the new cpuset.
  1635. * name: name of the new cpuset. Will be strcpy'ed.
  1636. * mode: mode to set on new inode
  1637. *
  1638. * Must be called with the mutex on the parent inode held
  1639. */
  1640. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1641. {
  1642. struct cpuset *cs;
  1643. int err;
  1644. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1645. if (!cs)
  1646. return -ENOMEM;
  1647. mutex_lock(&manage_mutex);
  1648. cpuset_update_task_memory_state();
  1649. cs->flags = 0;
  1650. if (notify_on_release(parent))
  1651. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1652. if (is_spread_page(parent))
  1653. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1654. if (is_spread_slab(parent))
  1655. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1656. cs->cpus_allowed = CPU_MASK_NONE;
  1657. cs->mems_allowed = NODE_MASK_NONE;
  1658. atomic_set(&cs->count, 0);
  1659. INIT_LIST_HEAD(&cs->sibling);
  1660. INIT_LIST_HEAD(&cs->children);
  1661. cs->mems_generation = cpuset_mems_generation++;
  1662. fmeter_init(&cs->fmeter);
  1663. cs->parent = parent;
  1664. mutex_lock(&callback_mutex);
  1665. list_add(&cs->sibling, &cs->parent->children);
  1666. number_of_cpusets++;
  1667. mutex_unlock(&callback_mutex);
  1668. err = cpuset_create_dir(cs, name, mode);
  1669. if (err < 0)
  1670. goto err;
  1671. /*
  1672. * Release manage_mutex before cpuset_populate_dir() because it
  1673. * will down() this new directory's i_mutex and if we race with
  1674. * another mkdir, we might deadlock.
  1675. */
  1676. mutex_unlock(&manage_mutex);
  1677. err = cpuset_populate_dir(cs->dentry);
  1678. /* If err < 0, we have a half-filled directory - oh well ;) */
  1679. return 0;
  1680. err:
  1681. list_del(&cs->sibling);
  1682. mutex_unlock(&manage_mutex);
  1683. kfree(cs);
  1684. return err;
  1685. }
  1686. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1687. {
  1688. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1689. /* the vfs holds inode->i_mutex already */
  1690. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1691. }
  1692. /*
  1693. * Locking note on the strange update_flag() call below:
  1694. *
  1695. * If the cpuset being removed is marked cpu_exclusive, then simulate
  1696. * turning cpu_exclusive off, which will call update_cpu_domains().
  1697. * The lock_cpu_hotplug() call in update_cpu_domains() must not be
  1698. * made while holding callback_mutex. Elsewhere the kernel nests
  1699. * callback_mutex inside lock_cpu_hotplug() calls. So the reverse
  1700. * nesting would risk an ABBA deadlock.
  1701. */
  1702. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1703. {
  1704. struct cpuset *cs = dentry->d_fsdata;
  1705. struct dentry *d;
  1706. struct cpuset *parent;
  1707. char *pathbuf = NULL;
  1708. /* the vfs holds both inode->i_mutex already */
  1709. mutex_lock(&manage_mutex);
  1710. cpuset_update_task_memory_state();
  1711. if (atomic_read(&cs->count) > 0) {
  1712. mutex_unlock(&manage_mutex);
  1713. return -EBUSY;
  1714. }
  1715. if (!list_empty(&cs->children)) {
  1716. mutex_unlock(&manage_mutex);
  1717. return -EBUSY;
  1718. }
  1719. if (is_cpu_exclusive(cs)) {
  1720. int retval = update_flag(CS_CPU_EXCLUSIVE, cs, "0");
  1721. if (retval < 0) {
  1722. mutex_unlock(&manage_mutex);
  1723. return retval;
  1724. }
  1725. }
  1726. parent = cs->parent;
  1727. mutex_lock(&callback_mutex);
  1728. set_bit(CS_REMOVED, &cs->flags);
  1729. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1730. spin_lock(&cs->dentry->d_lock);
  1731. d = dget(cs->dentry);
  1732. cs->dentry = NULL;
  1733. spin_unlock(&d->d_lock);
  1734. cpuset_d_remove_dir(d);
  1735. dput(d);
  1736. number_of_cpusets--;
  1737. mutex_unlock(&callback_mutex);
  1738. if (list_empty(&parent->children))
  1739. check_for_release(parent, &pathbuf);
  1740. mutex_unlock(&manage_mutex);
  1741. cpuset_release_agent(pathbuf);
  1742. return 0;
  1743. }
  1744. /*
  1745. * cpuset_init_early - just enough so that the calls to
  1746. * cpuset_update_task_memory_state() in early init code
  1747. * are harmless.
  1748. */
  1749. int __init cpuset_init_early(void)
  1750. {
  1751. struct task_struct *tsk = current;
  1752. tsk->cpuset = &top_cpuset;
  1753. tsk->cpuset->mems_generation = cpuset_mems_generation++;
  1754. return 0;
  1755. }
  1756. /**
  1757. * cpuset_init - initialize cpusets at system boot
  1758. *
  1759. * Description: Initialize top_cpuset and the cpuset internal file system,
  1760. **/
  1761. int __init cpuset_init(void)
  1762. {
  1763. struct dentry *root;
  1764. int err;
  1765. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1766. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1767. fmeter_init(&top_cpuset.fmeter);
  1768. top_cpuset.mems_generation = cpuset_mems_generation++;
  1769. init_task.cpuset = &top_cpuset;
  1770. err = register_filesystem(&cpuset_fs_type);
  1771. if (err < 0)
  1772. goto out;
  1773. cpuset_mount = kern_mount(&cpuset_fs_type);
  1774. if (IS_ERR(cpuset_mount)) {
  1775. printk(KERN_ERR "cpuset: could not mount!\n");
  1776. err = PTR_ERR(cpuset_mount);
  1777. cpuset_mount = NULL;
  1778. goto out;
  1779. }
  1780. root = cpuset_mount->mnt_sb->s_root;
  1781. root->d_fsdata = &top_cpuset;
  1782. inc_nlink(root->d_inode);
  1783. top_cpuset.dentry = root;
  1784. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1785. number_of_cpusets = 1;
  1786. err = cpuset_populate_dir(root);
  1787. /* memory_pressure_enabled is in root cpuset only */
  1788. if (err == 0)
  1789. err = cpuset_add_file(root, &cft_memory_pressure_enabled);
  1790. out:
  1791. return err;
  1792. }
  1793. /*
  1794. * If common_cpu_mem_hotplug_unplug(), below, unplugs any CPUs
  1795. * or memory nodes, we need to walk over the cpuset hierarchy,
  1796. * removing that CPU or node from all cpusets. If this removes the
  1797. * last CPU or node from a cpuset, then the guarantee_online_cpus()
  1798. * or guarantee_online_mems() code will use that emptied cpusets
  1799. * parent online CPUs or nodes. Cpusets that were already empty of
  1800. * CPUs or nodes are left empty.
  1801. *
  1802. * This routine is intentionally inefficient in a couple of regards.
  1803. * It will check all cpusets in a subtree even if the top cpuset of
  1804. * the subtree has no offline CPUs or nodes. It checks both CPUs and
  1805. * nodes, even though the caller could have been coded to know that
  1806. * only one of CPUs or nodes needed to be checked on a given call.
  1807. * This was done to minimize text size rather than cpu cycles.
  1808. *
  1809. * Call with both manage_mutex and callback_mutex held.
  1810. *
  1811. * Recursive, on depth of cpuset subtree.
  1812. */
  1813. static void guarantee_online_cpus_mems_in_subtree(const struct cpuset *cur)
  1814. {
  1815. struct cpuset *c;
  1816. /* Each of our child cpusets mems must be online */
  1817. list_for_each_entry(c, &cur->children, sibling) {
  1818. guarantee_online_cpus_mems_in_subtree(c);
  1819. if (!cpus_empty(c->cpus_allowed))
  1820. guarantee_online_cpus(c, &c->cpus_allowed);
  1821. if (!nodes_empty(c->mems_allowed))
  1822. guarantee_online_mems(c, &c->mems_allowed);
  1823. }
  1824. }
  1825. /*
  1826. * The cpus_allowed and mems_allowed nodemasks in the top_cpuset track
  1827. * cpu_online_map and node_online_map. Force the top cpuset to track
  1828. * whats online after any CPU or memory node hotplug or unplug event.
  1829. *
  1830. * To ensure that we don't remove a CPU or node from the top cpuset
  1831. * that is currently in use by a child cpuset (which would violate
  1832. * the rule that cpusets must be subsets of their parent), we first
  1833. * call the recursive routine guarantee_online_cpus_mems_in_subtree().
  1834. *
  1835. * Since there are two callers of this routine, one for CPU hotplug
  1836. * events and one for memory node hotplug events, we could have coded
  1837. * two separate routines here. We code it as a single common routine
  1838. * in order to minimize text size.
  1839. */
  1840. static void common_cpu_mem_hotplug_unplug(void)
  1841. {
  1842. mutex_lock(&manage_mutex);
  1843. mutex_lock(&callback_mutex);
  1844. guarantee_online_cpus_mems_in_subtree(&top_cpuset);
  1845. top_cpuset.cpus_allowed = cpu_online_map;
  1846. top_cpuset.mems_allowed = node_online_map;
  1847. mutex_unlock(&callback_mutex);
  1848. mutex_unlock(&manage_mutex);
  1849. }
  1850. /*
  1851. * The top_cpuset tracks what CPUs and Memory Nodes are online,
  1852. * period. This is necessary in order to make cpusets transparent
  1853. * (of no affect) on systems that are actively using CPU hotplug
  1854. * but making no active use of cpusets.
  1855. *
  1856. * This routine ensures that top_cpuset.cpus_allowed tracks
  1857. * cpu_online_map on each CPU hotplug (cpuhp) event.
  1858. */
  1859. static int cpuset_handle_cpuhp(struct notifier_block *nb,
  1860. unsigned long phase, void *cpu)
  1861. {
  1862. common_cpu_mem_hotplug_unplug();
  1863. return 0;
  1864. }
  1865. #ifdef CONFIG_MEMORY_HOTPLUG
  1866. /*
  1867. * Keep top_cpuset.mems_allowed tracking node_online_map.
  1868. * Call this routine anytime after you change node_online_map.
  1869. * See also the previous routine cpuset_handle_cpuhp().
  1870. */
  1871. void cpuset_track_online_nodes(void)
  1872. {
  1873. common_cpu_mem_hotplug_unplug();
  1874. }
  1875. #endif
  1876. /**
  1877. * cpuset_init_smp - initialize cpus_allowed
  1878. *
  1879. * Description: Finish top cpuset after cpu, node maps are initialized
  1880. **/
  1881. void __init cpuset_init_smp(void)
  1882. {
  1883. top_cpuset.cpus_allowed = cpu_online_map;
  1884. top_cpuset.mems_allowed = node_online_map;
  1885. hotcpu_notifier(cpuset_handle_cpuhp, 0);
  1886. }
  1887. /**
  1888. * cpuset_fork - attach newly forked task to its parents cpuset.
  1889. * @tsk: pointer to task_struct of forking parent process.
  1890. *
  1891. * Description: A task inherits its parent's cpuset at fork().
  1892. *
  1893. * A pointer to the shared cpuset was automatically copied in fork.c
  1894. * by dup_task_struct(). However, we ignore that copy, since it was
  1895. * not made under the protection of task_lock(), so might no longer be
  1896. * a valid cpuset pointer. attach_task() might have already changed
  1897. * current->cpuset, allowing the previously referenced cpuset to
  1898. * be removed and freed. Instead, we task_lock(current) and copy
  1899. * its present value of current->cpuset for our freshly forked child.
  1900. *
  1901. * At the point that cpuset_fork() is called, 'current' is the parent
  1902. * task, and the passed argument 'child' points to the child task.
  1903. **/
  1904. void cpuset_fork(struct task_struct *child)
  1905. {
  1906. task_lock(current);
  1907. child->cpuset = current->cpuset;
  1908. atomic_inc(&child->cpuset->count);
  1909. task_unlock(current);
  1910. }
  1911. /**
  1912. * cpuset_exit - detach cpuset from exiting task
  1913. * @tsk: pointer to task_struct of exiting process
  1914. *
  1915. * Description: Detach cpuset from @tsk and release it.
  1916. *
  1917. * Note that cpusets marked notify_on_release force every task in
  1918. * them to take the global manage_mutex mutex when exiting.
  1919. * This could impact scaling on very large systems. Be reluctant to
  1920. * use notify_on_release cpusets where very high task exit scaling
  1921. * is required on large systems.
  1922. *
  1923. * Don't even think about derefencing 'cs' after the cpuset use count
  1924. * goes to zero, except inside a critical section guarded by manage_mutex
  1925. * or callback_mutex. Otherwise a zero cpuset use count is a license to
  1926. * any other task to nuke the cpuset immediately, via cpuset_rmdir().
  1927. *
  1928. * This routine has to take manage_mutex, not callback_mutex, because
  1929. * it is holding that mutex while calling check_for_release(),
  1930. * which calls kmalloc(), so can't be called holding callback_mutex().
  1931. *
  1932. * We don't need to task_lock() this reference to tsk->cpuset,
  1933. * because tsk is already marked PF_EXITING, so attach_task() won't
  1934. * mess with it, or task is a failed fork, never visible to attach_task.
  1935. *
  1936. * the_top_cpuset_hack:
  1937. *
  1938. * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
  1939. *
  1940. * Don't leave a task unable to allocate memory, as that is an
  1941. * accident waiting to happen should someone add a callout in
  1942. * do_exit() after the cpuset_exit() call that might allocate.
  1943. * If a task tries to allocate memory with an invalid cpuset,
  1944. * it will oops in cpuset_update_task_memory_state().
  1945. *
  1946. * We call cpuset_exit() while the task is still competent to
  1947. * handle notify_on_release(), then leave the task attached to
  1948. * the root cpuset (top_cpuset) for the remainder of its exit.
  1949. *
  1950. * To do this properly, we would increment the reference count on
  1951. * top_cpuset, and near the very end of the kernel/exit.c do_exit()
  1952. * code we would add a second cpuset function call, to drop that
  1953. * reference. This would just create an unnecessary hot spot on
  1954. * the top_cpuset reference count, to no avail.
  1955. *
  1956. * Normally, holding a reference to a cpuset without bumping its
  1957. * count is unsafe. The cpuset could go away, or someone could
  1958. * attach us to a different cpuset, decrementing the count on
  1959. * the first cpuset that we never incremented. But in this case,
  1960. * top_cpuset isn't going away, and either task has PF_EXITING set,
  1961. * which wards off any attach_task() attempts, or task is a failed
  1962. * fork, never visible to attach_task.
  1963. *
  1964. * Another way to do this would be to set the cpuset pointer
  1965. * to NULL here, and check in cpuset_update_task_memory_state()
  1966. * for a NULL pointer. This hack avoids that NULL check, for no
  1967. * cost (other than this way too long comment ;).
  1968. **/
  1969. void cpuset_exit(struct task_struct *tsk)
  1970. {
  1971. struct cpuset *cs;
  1972. cs = tsk->cpuset;
  1973. tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
  1974. if (notify_on_release(cs)) {
  1975. char *pathbuf = NULL;
  1976. mutex_lock(&manage_mutex);
  1977. if (atomic_dec_and_test(&cs->count))
  1978. check_for_release(cs, &pathbuf);
  1979. mutex_unlock(&manage_mutex);
  1980. cpuset_release_agent(pathbuf);
  1981. } else {
  1982. atomic_dec(&cs->count);
  1983. }
  1984. }
  1985. /**
  1986. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1987. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1988. *
  1989. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1990. * attached to the specified @tsk. Guaranteed to return some non-empty
  1991. * subset of cpu_online_map, even if this means going outside the
  1992. * tasks cpuset.
  1993. **/
  1994. cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
  1995. {
  1996. cpumask_t mask;
  1997. mutex_lock(&callback_mutex);
  1998. task_lock(tsk);
  1999. guarantee_online_cpus(tsk->cpuset, &mask);
  2000. task_unlock(tsk);
  2001. mutex_unlock(&callback_mutex);
  2002. return mask;
  2003. }
  2004. void cpuset_init_current_mems_allowed(void)
  2005. {
  2006. current->mems_allowed = NODE_MASK_ALL;
  2007. }
  2008. /**
  2009. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  2010. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  2011. *
  2012. * Description: Returns the nodemask_t mems_allowed of the cpuset
  2013. * attached to the specified @tsk. Guaranteed to return some non-empty
  2014. * subset of node_online_map, even if this means going outside the
  2015. * tasks cpuset.
  2016. **/
  2017. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  2018. {
  2019. nodemask_t mask;
  2020. mutex_lock(&callback_mutex);
  2021. task_lock(tsk);
  2022. guarantee_online_mems(tsk->cpuset, &mask);
  2023. task_unlock(tsk);
  2024. mutex_unlock(&callback_mutex);
  2025. return mask;
  2026. }
  2027. /**
  2028. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  2029. * @zl: the zonelist to be checked
  2030. *
  2031. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  2032. */
  2033. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  2034. {
  2035. int i;
  2036. for (i = 0; zl->zones[i]; i++) {
  2037. int nid = zone_to_nid(zl->zones[i]);
  2038. if (node_isset(nid, current->mems_allowed))
  2039. return 1;
  2040. }
  2041. return 0;
  2042. }
  2043. /*
  2044. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  2045. * ancestor to the specified cpuset. Call holding callback_mutex.
  2046. * If no ancestor is mem_exclusive (an unusual configuration), then
  2047. * returns the root cpuset.
  2048. */
  2049. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  2050. {
  2051. while (!is_mem_exclusive(cs) && cs->parent)
  2052. cs = cs->parent;
  2053. return cs;
  2054. }
  2055. /**
  2056. * cpuset_zone_allowed_softwall - Can we allocate on zone z's memory node?
  2057. * @z: is this zone on an allowed node?
  2058. * @gfp_mask: memory allocation flags
  2059. *
  2060. * If we're in interrupt, yes, we can always allocate. If
  2061. * __GFP_THISNODE is set, yes, we can always allocate. If zone
  2062. * z's node is in our tasks mems_allowed, yes. If it's not a
  2063. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  2064. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  2065. * Otherwise, no.
  2066. *
  2067. * If __GFP_HARDWALL is set, cpuset_zone_allowed_softwall()
  2068. * reduces to cpuset_zone_allowed_hardwall(). Otherwise,
  2069. * cpuset_zone_allowed_softwall() might sleep, and might allow a zone
  2070. * from an enclosing cpuset.
  2071. *
  2072. * cpuset_zone_allowed_hardwall() only handles the simpler case of
  2073. * hardwall cpusets, and never sleeps.
  2074. *
  2075. * The __GFP_THISNODE placement logic is really handled elsewhere,
  2076. * by forcibly using a zonelist starting at a specified node, and by
  2077. * (in get_page_from_freelist()) refusing to consider the zones for
  2078. * any node on the zonelist except the first. By the time any such
  2079. * calls get to this routine, we should just shut up and say 'yes'.
  2080. *
  2081. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  2082. * and do not allow allocations outside the current tasks cpuset.
  2083. * GFP_KERNEL allocations are not so marked, so can escape to the
  2084. * nearest enclosing mem_exclusive ancestor cpuset.
  2085. *
  2086. * Scanning up parent cpusets requires callback_mutex. The
  2087. * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
  2088. * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
  2089. * current tasks mems_allowed came up empty on the first pass over
  2090. * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
  2091. * cpuset are short of memory, might require taking the callback_mutex
  2092. * mutex.
  2093. *
  2094. * The first call here from mm/page_alloc:get_page_from_freelist()
  2095. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
  2096. * so no allocation on a node outside the cpuset is allowed (unless
  2097. * in interrupt, of course).
  2098. *
  2099. * The second pass through get_page_from_freelist() doesn't even call
  2100. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  2101. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  2102. * in alloc_flags. That logic and the checks below have the combined
  2103. * affect that:
  2104. * in_interrupt - any node ok (current task context irrelevant)
  2105. * GFP_ATOMIC - any node ok
  2106. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  2107. * GFP_USER - only nodes in current tasks mems allowed ok.
  2108. *
  2109. * Rule:
  2110. * Don't call cpuset_zone_allowed_softwall if you can't sleep, unless you
  2111. * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
  2112. * the code that might scan up ancestor cpusets and sleep.
  2113. */
  2114. int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask)
  2115. {
  2116. int node; /* node that zone z is on */
  2117. const struct cpuset *cs; /* current cpuset ancestors */
  2118. int allowed; /* is allocation in zone z allowed? */
  2119. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  2120. return 1;
  2121. node = zone_to_nid(z);
  2122. might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
  2123. if (node_isset(node, current->mems_allowed))
  2124. return 1;
  2125. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  2126. return 0;
  2127. if (current->flags & PF_EXITING) /* Let dying task have memory */
  2128. return 1;
  2129. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  2130. mutex_lock(&callback_mutex);
  2131. task_lock(current);
  2132. cs = nearest_exclusive_ancestor(current->cpuset);
  2133. task_unlock(current);
  2134. allowed = node_isset(node, cs->mems_allowed);
  2135. mutex_unlock(&callback_mutex);
  2136. return allowed;
  2137. }
  2138. /*
  2139. * cpuset_zone_allowed_hardwall - Can we allocate on zone z's memory node?
  2140. * @z: is this zone on an allowed node?
  2141. * @gfp_mask: memory allocation flags
  2142. *
  2143. * If we're in interrupt, yes, we can always allocate.
  2144. * If __GFP_THISNODE is set, yes, we can always allocate. If zone
  2145. * z's node is in our tasks mems_allowed, yes. Otherwise, no.
  2146. *
  2147. * The __GFP_THISNODE placement logic is really handled elsewhere,
  2148. * by forcibly using a zonelist starting at a specified node, and by
  2149. * (in get_page_from_freelist()) refusing to consider the zones for
  2150. * any node on the zonelist except the first. By the time any such
  2151. * calls get to this routine, we should just shut up and say 'yes'.
  2152. *
  2153. * Unlike the cpuset_zone_allowed_softwall() variant, above,
  2154. * this variant requires that the zone be in the current tasks
  2155. * mems_allowed or that we're in interrupt. It does not scan up the
  2156. * cpuset hierarchy for the nearest enclosing mem_exclusive cpuset.
  2157. * It never sleeps.
  2158. */
  2159. int __cpuset_zone_allowed_hardwall(struct zone *z, gfp_t gfp_mask)
  2160. {
  2161. int node; /* node that zone z is on */
  2162. if (in_interrupt() || (gfp_mask & __GFP_THISNODE))
  2163. return 1;
  2164. node = zone_to_nid(z);
  2165. if (node_isset(node, current->mems_allowed))
  2166. return 1;
  2167. return 0;
  2168. }
  2169. /**
  2170. * cpuset_lock - lock out any changes to cpuset structures
  2171. *
  2172. * The out of memory (oom) code needs to mutex_lock cpusets
  2173. * from being changed while it scans the tasklist looking for a
  2174. * task in an overlapping cpuset. Expose callback_mutex via this
  2175. * cpuset_lock() routine, so the oom code can lock it, before
  2176. * locking the task list. The tasklist_lock is a spinlock, so
  2177. * must be taken inside callback_mutex.
  2178. */
  2179. void cpuset_lock(void)
  2180. {
  2181. mutex_lock(&callback_mutex);
  2182. }
  2183. /**
  2184. * cpuset_unlock - release lock on cpuset changes
  2185. *
  2186. * Undo the lock taken in a previous cpuset_lock() call.
  2187. */
  2188. void cpuset_unlock(void)
  2189. {
  2190. mutex_unlock(&callback_mutex);
  2191. }
  2192. /**
  2193. * cpuset_mem_spread_node() - On which node to begin search for a page
  2194. *
  2195. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  2196. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  2197. * and if the memory allocation used cpuset_mem_spread_node()
  2198. * to determine on which node to start looking, as it will for
  2199. * certain page cache or slab cache pages such as used for file
  2200. * system buffers and inode caches, then instead of starting on the
  2201. * local node to look for a free page, rather spread the starting
  2202. * node around the tasks mems_allowed nodes.
  2203. *
  2204. * We don't have to worry about the returned node being offline
  2205. * because "it can't happen", and even if it did, it would be ok.
  2206. *
  2207. * The routines calling guarantee_online_mems() are careful to
  2208. * only set nodes in task->mems_allowed that are online. So it
  2209. * should not be possible for the following code to return an
  2210. * offline node. But if it did, that would be ok, as this routine
  2211. * is not returning the node where the allocation must be, only
  2212. * the node where the search should start. The zonelist passed to
  2213. * __alloc_pages() will include all nodes. If the slab allocator
  2214. * is passed an offline node, it will fall back to the local node.
  2215. * See kmem_cache_alloc_node().
  2216. */
  2217. int cpuset_mem_spread_node(void)
  2218. {
  2219. int node;
  2220. node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
  2221. if (node == MAX_NUMNODES)
  2222. node = first_node(current->mems_allowed);
  2223. current->cpuset_mem_spread_rotor = node;
  2224. return node;
  2225. }
  2226. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  2227. /**
  2228. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  2229. * @p: pointer to task_struct of some other task.
  2230. *
  2231. * Description: Return true if the nearest mem_exclusive ancestor
  2232. * cpusets of tasks @p and current overlap. Used by oom killer to
  2233. * determine if task @p's memory usage might impact the memory
  2234. * available to the current task.
  2235. *
  2236. * Call while holding callback_mutex.
  2237. **/
  2238. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  2239. {
  2240. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  2241. int overlap = 1; /* do cpusets overlap? */
  2242. task_lock(current);
  2243. if (current->flags & PF_EXITING) {
  2244. task_unlock(current);
  2245. goto done;
  2246. }
  2247. cs1 = nearest_exclusive_ancestor(current->cpuset);
  2248. task_unlock(current);
  2249. task_lock((struct task_struct *)p);
  2250. if (p->flags & PF_EXITING) {
  2251. task_unlock((struct task_struct *)p);
  2252. goto done;
  2253. }
  2254. cs2 = nearest_exclusive_ancestor(p->cpuset);
  2255. task_unlock((struct task_struct *)p);
  2256. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  2257. done:
  2258. return overlap;
  2259. }
  2260. /*
  2261. * Collection of memory_pressure is suppressed unless
  2262. * this flag is enabled by writing "1" to the special
  2263. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  2264. */
  2265. int cpuset_memory_pressure_enabled __read_mostly;
  2266. /**
  2267. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  2268. *
  2269. * Keep a running average of the rate of synchronous (direct)
  2270. * page reclaim efforts initiated by tasks in each cpuset.
  2271. *
  2272. * This represents the rate at which some task in the cpuset
  2273. * ran low on memory on all nodes it was allowed to use, and
  2274. * had to enter the kernels page reclaim code in an effort to
  2275. * create more free memory by tossing clean pages or swapping
  2276. * or writing dirty pages.
  2277. *
  2278. * Display to user space in the per-cpuset read-only file
  2279. * "memory_pressure". Value displayed is an integer
  2280. * representing the recent rate of entry into the synchronous
  2281. * (direct) page reclaim by any task attached to the cpuset.
  2282. **/
  2283. void __cpuset_memory_pressure_bump(void)
  2284. {
  2285. struct cpuset *cs;
  2286. task_lock(current);
  2287. cs = current->cpuset;
  2288. fmeter_markevent(&cs->fmeter);
  2289. task_unlock(current);
  2290. }
  2291. /*
  2292. * proc_cpuset_show()
  2293. * - Print tasks cpuset path into seq_file.
  2294. * - Used for /proc/<pid>/cpuset.
  2295. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  2296. * doesn't really matter if tsk->cpuset changes after we read it,
  2297. * and we take manage_mutex, keeping attach_task() from changing it
  2298. * anyway. No need to check that tsk->cpuset != NULL, thanks to
  2299. * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
  2300. * cpuset to top_cpuset.
  2301. */
  2302. static int proc_cpuset_show(struct seq_file *m, void *v)
  2303. {
  2304. struct pid *pid;
  2305. struct task_struct *tsk;
  2306. char *buf;
  2307. int retval;
  2308. retval = -ENOMEM;
  2309. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2310. if (!buf)
  2311. goto out;
  2312. retval = -ESRCH;
  2313. pid = m->private;
  2314. tsk = get_pid_task(pid, PIDTYPE_PID);
  2315. if (!tsk)
  2316. goto out_free;
  2317. retval = -EINVAL;
  2318. mutex_lock(&manage_mutex);
  2319. retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE);
  2320. if (retval < 0)
  2321. goto out_unlock;
  2322. seq_puts(m, buf);
  2323. seq_putc(m, '\n');
  2324. out_unlock:
  2325. mutex_unlock(&manage_mutex);
  2326. put_task_struct(tsk);
  2327. out_free:
  2328. kfree(buf);
  2329. out:
  2330. return retval;
  2331. }
  2332. static int cpuset_open(struct inode *inode, struct file *file)
  2333. {
  2334. struct pid *pid = PROC_I(inode)->pid;
  2335. return single_open(file, proc_cpuset_show, pid);
  2336. }
  2337. const struct file_operations proc_cpuset_operations = {
  2338. .open = cpuset_open,
  2339. .read = seq_read,
  2340. .llseek = seq_lseek,
  2341. .release = single_release,
  2342. };
  2343. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  2344. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  2345. {
  2346. buffer += sprintf(buffer, "Cpus_allowed:\t");
  2347. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  2348. buffer += sprintf(buffer, "\n");
  2349. buffer += sprintf(buffer, "Mems_allowed:\t");
  2350. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  2351. buffer += sprintf(buffer, "\n");
  2352. return buffer;
  2353. }