cpuset.c 80 KB

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