cpuset.c 73 KB

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