cpuset.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  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. */
  687. static void update_cpu_domains(struct cpuset *cur)
  688. {
  689. struct cpuset *c, *par = cur->parent;
  690. cpumask_t pspan, cspan;
  691. if (par == NULL || cpus_empty(cur->cpus_allowed))
  692. return;
  693. /*
  694. * Get all cpus from parent's cpus_allowed not part of exclusive
  695. * children
  696. */
  697. pspan = par->cpus_allowed;
  698. list_for_each_entry(c, &par->children, sibling) {
  699. if (is_cpu_exclusive(c))
  700. cpus_andnot(pspan, pspan, c->cpus_allowed);
  701. }
  702. if (is_removed(cur) || !is_cpu_exclusive(cur)) {
  703. cpus_or(pspan, pspan, cur->cpus_allowed);
  704. if (cpus_equal(pspan, cur->cpus_allowed))
  705. return;
  706. cspan = CPU_MASK_NONE;
  707. } else {
  708. if (cpus_empty(pspan))
  709. return;
  710. cspan = cur->cpus_allowed;
  711. /*
  712. * Get all cpus from current cpuset's cpus_allowed not part
  713. * of exclusive children
  714. */
  715. list_for_each_entry(c, &cur->children, sibling) {
  716. if (is_cpu_exclusive(c))
  717. cpus_andnot(cspan, cspan, c->cpus_allowed);
  718. }
  719. }
  720. lock_cpu_hotplug();
  721. partition_sched_domains(&pspan, &cspan);
  722. unlock_cpu_hotplug();
  723. }
  724. /*
  725. * Call with manage_mutex held. May take callback_mutex during call.
  726. */
  727. static int update_cpumask(struct cpuset *cs, char *buf)
  728. {
  729. struct cpuset trialcs;
  730. int retval, cpus_unchanged;
  731. trialcs = *cs;
  732. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  733. if (retval < 0)
  734. return retval;
  735. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  736. if (cpus_empty(trialcs.cpus_allowed))
  737. return -ENOSPC;
  738. retval = validate_change(cs, &trialcs);
  739. if (retval < 0)
  740. return retval;
  741. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  742. mutex_lock(&callback_mutex);
  743. cs->cpus_allowed = trialcs.cpus_allowed;
  744. mutex_unlock(&callback_mutex);
  745. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  746. update_cpu_domains(cs);
  747. return 0;
  748. }
  749. /*
  750. * cpuset_migrate_mm
  751. *
  752. * Migrate memory region from one set of nodes to another.
  753. *
  754. * Temporarilly set tasks mems_allowed to target nodes of migration,
  755. * so that the migration code can allocate pages on these nodes.
  756. *
  757. * Call holding manage_mutex, so our current->cpuset won't change
  758. * during this call, as manage_mutex holds off any attach_task()
  759. * calls. Therefore we don't need to take task_lock around the
  760. * call to guarantee_online_mems(), as we know no one is changing
  761. * our tasks cpuset.
  762. *
  763. * Hold callback_mutex around the two modifications of our tasks
  764. * mems_allowed to synchronize with cpuset_mems_allowed().
  765. *
  766. * While the mm_struct we are migrating is typically from some
  767. * other task, the task_struct mems_allowed that we are hacking
  768. * is for our current task, which must allocate new pages for that
  769. * migrating memory region.
  770. *
  771. * We call cpuset_update_task_memory_state() before hacking
  772. * our tasks mems_allowed, so that we are assured of being in
  773. * sync with our tasks cpuset, and in particular, callbacks to
  774. * cpuset_update_task_memory_state() from nested page allocations
  775. * won't see any mismatch of our cpuset and task mems_generation
  776. * values, so won't overwrite our hacked tasks mems_allowed
  777. * nodemask.
  778. */
  779. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  780. const nodemask_t *to)
  781. {
  782. struct task_struct *tsk = current;
  783. cpuset_update_task_memory_state();
  784. mutex_lock(&callback_mutex);
  785. tsk->mems_allowed = *to;
  786. mutex_unlock(&callback_mutex);
  787. do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
  788. mutex_lock(&callback_mutex);
  789. guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed);
  790. mutex_unlock(&callback_mutex);
  791. }
  792. /*
  793. * Handle user request to change the 'mems' memory placement
  794. * of a cpuset. Needs to validate the request, update the
  795. * cpusets mems_allowed and mems_generation, and for each
  796. * task in the cpuset, rebind any vma mempolicies and if
  797. * the cpuset is marked 'memory_migrate', migrate the tasks
  798. * pages to the new memory.
  799. *
  800. * Call with manage_mutex held. May take callback_mutex during call.
  801. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  802. * lock each such tasks mm->mmap_sem, scan its vma's and rebind
  803. * their mempolicies to the cpusets new mems_allowed.
  804. */
  805. static int update_nodemask(struct cpuset *cs, char *buf)
  806. {
  807. struct cpuset trialcs;
  808. nodemask_t oldmem;
  809. struct task_struct *g, *p;
  810. struct mm_struct **mmarray;
  811. int i, n, ntasks;
  812. int migrate;
  813. int fudge;
  814. int retval;
  815. trialcs = *cs;
  816. retval = nodelist_parse(buf, trialcs.mems_allowed);
  817. if (retval < 0)
  818. goto done;
  819. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  820. oldmem = cs->mems_allowed;
  821. if (nodes_equal(oldmem, trialcs.mems_allowed)) {
  822. retval = 0; /* Too easy - nothing to do */
  823. goto done;
  824. }
  825. if (nodes_empty(trialcs.mems_allowed)) {
  826. retval = -ENOSPC;
  827. goto done;
  828. }
  829. retval = validate_change(cs, &trialcs);
  830. if (retval < 0)
  831. goto done;
  832. mutex_lock(&callback_mutex);
  833. cs->mems_allowed = trialcs.mems_allowed;
  834. cs->mems_generation = cpuset_mems_generation++;
  835. mutex_unlock(&callback_mutex);
  836. set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
  837. fudge = 10; /* spare mmarray[] slots */
  838. fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
  839. retval = -ENOMEM;
  840. /*
  841. * Allocate mmarray[] to hold mm reference for each task
  842. * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
  843. * tasklist_lock. We could use GFP_ATOMIC, but with a
  844. * few more lines of code, we can retry until we get a big
  845. * enough mmarray[] w/o using GFP_ATOMIC.
  846. */
  847. while (1) {
  848. ntasks = atomic_read(&cs->count); /* guess */
  849. ntasks += fudge;
  850. mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
  851. if (!mmarray)
  852. goto done;
  853. write_lock_irq(&tasklist_lock); /* block fork */
  854. if (atomic_read(&cs->count) <= ntasks)
  855. break; /* got enough */
  856. write_unlock_irq(&tasklist_lock); /* try again */
  857. kfree(mmarray);
  858. }
  859. n = 0;
  860. /* Load up mmarray[] with mm reference for each task in cpuset. */
  861. do_each_thread(g, p) {
  862. struct mm_struct *mm;
  863. if (n >= ntasks) {
  864. printk(KERN_WARNING
  865. "Cpuset mempolicy rebind incomplete.\n");
  866. continue;
  867. }
  868. if (p->cpuset != cs)
  869. continue;
  870. mm = get_task_mm(p);
  871. if (!mm)
  872. continue;
  873. mmarray[n++] = mm;
  874. } while_each_thread(g, p);
  875. write_unlock_irq(&tasklist_lock);
  876. /*
  877. * Now that we've dropped the tasklist spinlock, we can
  878. * rebind the vma mempolicies of each mm in mmarray[] to their
  879. * new cpuset, and release that mm. The mpol_rebind_mm()
  880. * call takes mmap_sem, which we couldn't take while holding
  881. * tasklist_lock. Forks can happen again now - the mpol_copy()
  882. * cpuset_being_rebound check will catch such forks, and rebind
  883. * their vma mempolicies too. Because we still hold the global
  884. * cpuset manage_mutex, we know that no other rebind effort will
  885. * be contending for the global variable cpuset_being_rebound.
  886. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  887. * is idempotent. Also migrate pages in each mm to new nodes.
  888. */
  889. migrate = is_memory_migrate(cs);
  890. for (i = 0; i < n; i++) {
  891. struct mm_struct *mm = mmarray[i];
  892. mpol_rebind_mm(mm, &cs->mems_allowed);
  893. if (migrate)
  894. cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
  895. mmput(mm);
  896. }
  897. /* We're done rebinding vma's to this cpusets new mems_allowed. */
  898. kfree(mmarray);
  899. set_cpuset_being_rebound(NULL);
  900. retval = 0;
  901. done:
  902. return retval;
  903. }
  904. /*
  905. * Call with manage_mutex held.
  906. */
  907. static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
  908. {
  909. if (simple_strtoul(buf, NULL, 10) != 0)
  910. cpuset_memory_pressure_enabled = 1;
  911. else
  912. cpuset_memory_pressure_enabled = 0;
  913. return 0;
  914. }
  915. /*
  916. * update_flag - read a 0 or a 1 in a file and update associated flag
  917. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  918. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
  919. * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
  920. * cs: the cpuset to update
  921. * buf: the buffer where we read the 0 or 1
  922. *
  923. * Call with manage_mutex held.
  924. */
  925. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  926. {
  927. int turning_on;
  928. struct cpuset trialcs;
  929. int err, cpu_exclusive_changed;
  930. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  931. trialcs = *cs;
  932. if (turning_on)
  933. set_bit(bit, &trialcs.flags);
  934. else
  935. clear_bit(bit, &trialcs.flags);
  936. err = validate_change(cs, &trialcs);
  937. if (err < 0)
  938. return err;
  939. cpu_exclusive_changed =
  940. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  941. mutex_lock(&callback_mutex);
  942. if (turning_on)
  943. set_bit(bit, &cs->flags);
  944. else
  945. clear_bit(bit, &cs->flags);
  946. mutex_unlock(&callback_mutex);
  947. if (cpu_exclusive_changed)
  948. update_cpu_domains(cs);
  949. return 0;
  950. }
  951. /*
  952. * Frequency meter - How fast is some event occurring?
  953. *
  954. * These routines manage a digitally filtered, constant time based,
  955. * event frequency meter. There are four routines:
  956. * fmeter_init() - initialize a frequency meter.
  957. * fmeter_markevent() - called each time the event happens.
  958. * fmeter_getrate() - returns the recent rate of such events.
  959. * fmeter_update() - internal routine used to update fmeter.
  960. *
  961. * A common data structure is passed to each of these routines,
  962. * which is used to keep track of the state required to manage the
  963. * frequency meter and its digital filter.
  964. *
  965. * The filter works on the number of events marked per unit time.
  966. * The filter is single-pole low-pass recursive (IIR). The time unit
  967. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  968. * simulate 3 decimal digits of precision (multiplied by 1000).
  969. *
  970. * With an FM_COEF of 933, and a time base of 1 second, the filter
  971. * has a half-life of 10 seconds, meaning that if the events quit
  972. * happening, then the rate returned from the fmeter_getrate()
  973. * will be cut in half each 10 seconds, until it converges to zero.
  974. *
  975. * It is not worth doing a real infinitely recursive filter. If more
  976. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  977. * just compute FM_MAXTICKS ticks worth, by which point the level
  978. * will be stable.
  979. *
  980. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  981. * arithmetic overflow in the fmeter_update() routine.
  982. *
  983. * Given the simple 32 bit integer arithmetic used, this meter works
  984. * best for reporting rates between one per millisecond (msec) and
  985. * one per 32 (approx) seconds. At constant rates faster than one
  986. * per msec it maxes out at values just under 1,000,000. At constant
  987. * rates between one per msec, and one per second it will stabilize
  988. * to a value N*1000, where N is the rate of events per second.
  989. * At constant rates between one per second and one per 32 seconds,
  990. * it will be choppy, moving up on the seconds that have an event,
  991. * and then decaying until the next event. At rates slower than
  992. * about one in 32 seconds, it decays all the way back to zero between
  993. * each event.
  994. */
  995. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  996. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  997. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  998. #define FM_SCALE 1000 /* faux fixed point scale */
  999. /* Initialize a frequency meter */
  1000. static void fmeter_init(struct fmeter *fmp)
  1001. {
  1002. fmp->cnt = 0;
  1003. fmp->val = 0;
  1004. fmp->time = 0;
  1005. spin_lock_init(&fmp->lock);
  1006. }
  1007. /* Internal meter update - process cnt events and update value */
  1008. static void fmeter_update(struct fmeter *fmp)
  1009. {
  1010. time_t now = get_seconds();
  1011. time_t ticks = now - fmp->time;
  1012. if (ticks == 0)
  1013. return;
  1014. ticks = min(FM_MAXTICKS, ticks);
  1015. while (ticks-- > 0)
  1016. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  1017. fmp->time = now;
  1018. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  1019. fmp->cnt = 0;
  1020. }
  1021. /* Process any previous ticks, then bump cnt by one (times scale). */
  1022. static void fmeter_markevent(struct fmeter *fmp)
  1023. {
  1024. spin_lock(&fmp->lock);
  1025. fmeter_update(fmp);
  1026. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  1027. spin_unlock(&fmp->lock);
  1028. }
  1029. /* Process any previous ticks, then return current value. */
  1030. static int fmeter_getrate(struct fmeter *fmp)
  1031. {
  1032. int val;
  1033. spin_lock(&fmp->lock);
  1034. fmeter_update(fmp);
  1035. val = fmp->val;
  1036. spin_unlock(&fmp->lock);
  1037. return val;
  1038. }
  1039. /*
  1040. * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
  1041. * writing the path of the old cpuset in 'ppathbuf' if it needs to be
  1042. * notified on release.
  1043. *
  1044. * Call holding manage_mutex. May take callback_mutex and task_lock of
  1045. * the task 'pid' during call.
  1046. */
  1047. static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
  1048. {
  1049. pid_t pid;
  1050. struct task_struct *tsk;
  1051. struct cpuset *oldcs;
  1052. cpumask_t cpus;
  1053. nodemask_t from, to;
  1054. struct mm_struct *mm;
  1055. int retval;
  1056. if (sscanf(pidbuf, "%d", &pid) != 1)
  1057. return -EIO;
  1058. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  1059. return -ENOSPC;
  1060. if (pid) {
  1061. read_lock(&tasklist_lock);
  1062. tsk = find_task_by_pid(pid);
  1063. if (!tsk || tsk->flags & PF_EXITING) {
  1064. read_unlock(&tasklist_lock);
  1065. return -ESRCH;
  1066. }
  1067. get_task_struct(tsk);
  1068. read_unlock(&tasklist_lock);
  1069. if ((current->euid) && (current->euid != tsk->uid)
  1070. && (current->euid != tsk->suid)) {
  1071. put_task_struct(tsk);
  1072. return -EACCES;
  1073. }
  1074. } else {
  1075. tsk = current;
  1076. get_task_struct(tsk);
  1077. }
  1078. retval = security_task_setscheduler(tsk, 0, NULL);
  1079. if (retval) {
  1080. put_task_struct(tsk);
  1081. return retval;
  1082. }
  1083. mutex_lock(&callback_mutex);
  1084. task_lock(tsk);
  1085. oldcs = tsk->cpuset;
  1086. if (!oldcs) {
  1087. task_unlock(tsk);
  1088. mutex_unlock(&callback_mutex);
  1089. put_task_struct(tsk);
  1090. return -ESRCH;
  1091. }
  1092. atomic_inc(&cs->count);
  1093. rcu_assign_pointer(tsk->cpuset, cs);
  1094. task_unlock(tsk);
  1095. guarantee_online_cpus(cs, &cpus);
  1096. set_cpus_allowed(tsk, cpus);
  1097. from = oldcs->mems_allowed;
  1098. to = cs->mems_allowed;
  1099. mutex_unlock(&callback_mutex);
  1100. mm = get_task_mm(tsk);
  1101. if (mm) {
  1102. mpol_rebind_mm(mm, &to);
  1103. if (is_memory_migrate(cs))
  1104. cpuset_migrate_mm(mm, &from, &to);
  1105. mmput(mm);
  1106. }
  1107. put_task_struct(tsk);
  1108. synchronize_rcu();
  1109. if (atomic_dec_and_test(&oldcs->count))
  1110. check_for_release(oldcs, ppathbuf);
  1111. return 0;
  1112. }
  1113. /* The various types of files and directories in a cpuset file system */
  1114. typedef enum {
  1115. FILE_ROOT,
  1116. FILE_DIR,
  1117. FILE_MEMORY_MIGRATE,
  1118. FILE_CPULIST,
  1119. FILE_MEMLIST,
  1120. FILE_CPU_EXCLUSIVE,
  1121. FILE_MEM_EXCLUSIVE,
  1122. FILE_NOTIFY_ON_RELEASE,
  1123. FILE_MEMORY_PRESSURE_ENABLED,
  1124. FILE_MEMORY_PRESSURE,
  1125. FILE_SPREAD_PAGE,
  1126. FILE_SPREAD_SLAB,
  1127. FILE_TASKLIST,
  1128. } cpuset_filetype_t;
  1129. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  1130. size_t nbytes, loff_t *unused_ppos)
  1131. {
  1132. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1133. struct cftype *cft = __d_cft(file->f_dentry);
  1134. cpuset_filetype_t type = cft->private;
  1135. char *buffer;
  1136. char *pathbuf = NULL;
  1137. int retval = 0;
  1138. /* Crude upper limit on largest legitimate cpulist user might write. */
  1139. if (nbytes > 100 + 6 * NR_CPUS)
  1140. return -E2BIG;
  1141. /* +1 for nul-terminator */
  1142. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  1143. return -ENOMEM;
  1144. if (copy_from_user(buffer, userbuf, nbytes)) {
  1145. retval = -EFAULT;
  1146. goto out1;
  1147. }
  1148. buffer[nbytes] = 0; /* nul-terminate */
  1149. mutex_lock(&manage_mutex);
  1150. if (is_removed(cs)) {
  1151. retval = -ENODEV;
  1152. goto out2;
  1153. }
  1154. switch (type) {
  1155. case FILE_CPULIST:
  1156. retval = update_cpumask(cs, buffer);
  1157. break;
  1158. case FILE_MEMLIST:
  1159. retval = update_nodemask(cs, buffer);
  1160. break;
  1161. case FILE_CPU_EXCLUSIVE:
  1162. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  1163. break;
  1164. case FILE_MEM_EXCLUSIVE:
  1165. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  1166. break;
  1167. case FILE_NOTIFY_ON_RELEASE:
  1168. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  1169. break;
  1170. case FILE_MEMORY_MIGRATE:
  1171. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  1172. break;
  1173. case FILE_MEMORY_PRESSURE_ENABLED:
  1174. retval = update_memory_pressure_enabled(cs, buffer);
  1175. break;
  1176. case FILE_MEMORY_PRESSURE:
  1177. retval = -EACCES;
  1178. break;
  1179. case FILE_SPREAD_PAGE:
  1180. retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
  1181. cs->mems_generation = cpuset_mems_generation++;
  1182. break;
  1183. case FILE_SPREAD_SLAB:
  1184. retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
  1185. cs->mems_generation = cpuset_mems_generation++;
  1186. break;
  1187. case FILE_TASKLIST:
  1188. retval = attach_task(cs, buffer, &pathbuf);
  1189. break;
  1190. default:
  1191. retval = -EINVAL;
  1192. goto out2;
  1193. }
  1194. if (retval == 0)
  1195. retval = nbytes;
  1196. out2:
  1197. mutex_unlock(&manage_mutex);
  1198. cpuset_release_agent(pathbuf);
  1199. out1:
  1200. kfree(buffer);
  1201. return retval;
  1202. }
  1203. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  1204. size_t nbytes, loff_t *ppos)
  1205. {
  1206. ssize_t retval = 0;
  1207. struct cftype *cft = __d_cft(file->f_dentry);
  1208. if (!cft)
  1209. return -ENODEV;
  1210. /* special function ? */
  1211. if (cft->write)
  1212. retval = cft->write(file, buf, nbytes, ppos);
  1213. else
  1214. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  1215. return retval;
  1216. }
  1217. /*
  1218. * These ascii lists should be read in a single call, by using a user
  1219. * buffer large enough to hold the entire map. If read in smaller
  1220. * chunks, there is no guarantee of atomicity. Since the display format
  1221. * used, list of ranges of sequential numbers, is variable length,
  1222. * and since these maps can change value dynamically, one could read
  1223. * gibberish by doing partial reads while a list was changing.
  1224. * A single large read to a buffer that crosses a page boundary is
  1225. * ok, because the result being copied to user land is not recomputed
  1226. * across a page fault.
  1227. */
  1228. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1229. {
  1230. cpumask_t mask;
  1231. mutex_lock(&callback_mutex);
  1232. mask = cs->cpus_allowed;
  1233. mutex_unlock(&callback_mutex);
  1234. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  1235. }
  1236. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1237. {
  1238. nodemask_t mask;
  1239. mutex_lock(&callback_mutex);
  1240. mask = cs->mems_allowed;
  1241. mutex_unlock(&callback_mutex);
  1242. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  1243. }
  1244. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  1245. size_t nbytes, loff_t *ppos)
  1246. {
  1247. struct cftype *cft = __d_cft(file->f_dentry);
  1248. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1249. cpuset_filetype_t type = cft->private;
  1250. char *page;
  1251. ssize_t retval = 0;
  1252. char *s;
  1253. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  1254. return -ENOMEM;
  1255. s = page;
  1256. switch (type) {
  1257. case FILE_CPULIST:
  1258. s += cpuset_sprintf_cpulist(s, cs);
  1259. break;
  1260. case FILE_MEMLIST:
  1261. s += cpuset_sprintf_memlist(s, cs);
  1262. break;
  1263. case FILE_CPU_EXCLUSIVE:
  1264. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  1265. break;
  1266. case FILE_MEM_EXCLUSIVE:
  1267. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  1268. break;
  1269. case FILE_NOTIFY_ON_RELEASE:
  1270. *s++ = notify_on_release(cs) ? '1' : '0';
  1271. break;
  1272. case FILE_MEMORY_MIGRATE:
  1273. *s++ = is_memory_migrate(cs) ? '1' : '0';
  1274. break;
  1275. case FILE_MEMORY_PRESSURE_ENABLED:
  1276. *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
  1277. break;
  1278. case FILE_MEMORY_PRESSURE:
  1279. s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
  1280. break;
  1281. case FILE_SPREAD_PAGE:
  1282. *s++ = is_spread_page(cs) ? '1' : '0';
  1283. break;
  1284. case FILE_SPREAD_SLAB:
  1285. *s++ = is_spread_slab(cs) ? '1' : '0';
  1286. break;
  1287. default:
  1288. retval = -EINVAL;
  1289. goto out;
  1290. }
  1291. *s++ = '\n';
  1292. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1293. out:
  1294. free_page((unsigned long)page);
  1295. return retval;
  1296. }
  1297. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  1298. loff_t *ppos)
  1299. {
  1300. ssize_t retval = 0;
  1301. struct cftype *cft = __d_cft(file->f_dentry);
  1302. if (!cft)
  1303. return -ENODEV;
  1304. /* special function ? */
  1305. if (cft->read)
  1306. retval = cft->read(file, buf, nbytes, ppos);
  1307. else
  1308. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  1309. return retval;
  1310. }
  1311. static int cpuset_file_open(struct inode *inode, struct file *file)
  1312. {
  1313. int err;
  1314. struct cftype *cft;
  1315. err = generic_file_open(inode, file);
  1316. if (err)
  1317. return err;
  1318. cft = __d_cft(file->f_dentry);
  1319. if (!cft)
  1320. return -ENODEV;
  1321. if (cft->open)
  1322. err = cft->open(inode, file);
  1323. else
  1324. err = 0;
  1325. return err;
  1326. }
  1327. static int cpuset_file_release(struct inode *inode, struct file *file)
  1328. {
  1329. struct cftype *cft = __d_cft(file->f_dentry);
  1330. if (cft->release)
  1331. return cft->release(inode, file);
  1332. return 0;
  1333. }
  1334. /*
  1335. * cpuset_rename - Only allow simple rename of directories in place.
  1336. */
  1337. static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
  1338. struct inode *new_dir, struct dentry *new_dentry)
  1339. {
  1340. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1341. return -ENOTDIR;
  1342. if (new_dentry->d_inode)
  1343. return -EEXIST;
  1344. if (old_dir != new_dir)
  1345. return -EIO;
  1346. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1347. }
  1348. static struct file_operations cpuset_file_operations = {
  1349. .read = cpuset_file_read,
  1350. .write = cpuset_file_write,
  1351. .llseek = generic_file_llseek,
  1352. .open = cpuset_file_open,
  1353. .release = cpuset_file_release,
  1354. };
  1355. static struct inode_operations cpuset_dir_inode_operations = {
  1356. .lookup = simple_lookup,
  1357. .mkdir = cpuset_mkdir,
  1358. .rmdir = cpuset_rmdir,
  1359. .rename = cpuset_rename,
  1360. };
  1361. static int cpuset_create_file(struct dentry *dentry, int mode)
  1362. {
  1363. struct inode *inode;
  1364. if (!dentry)
  1365. return -ENOENT;
  1366. if (dentry->d_inode)
  1367. return -EEXIST;
  1368. inode = cpuset_new_inode(mode);
  1369. if (!inode)
  1370. return -ENOMEM;
  1371. if (S_ISDIR(mode)) {
  1372. inode->i_op = &cpuset_dir_inode_operations;
  1373. inode->i_fop = &simple_dir_operations;
  1374. /* start off with i_nlink == 2 (for "." entry) */
  1375. inode->i_nlink++;
  1376. } else if (S_ISREG(mode)) {
  1377. inode->i_size = 0;
  1378. inode->i_fop = &cpuset_file_operations;
  1379. }
  1380. d_instantiate(dentry, inode);
  1381. dget(dentry); /* Extra count - pin the dentry in core */
  1382. return 0;
  1383. }
  1384. /*
  1385. * cpuset_create_dir - create a directory for an object.
  1386. * cs: the cpuset we create the directory for.
  1387. * It must have a valid ->parent field
  1388. * And we are going to fill its ->dentry field.
  1389. * name: The name to give to the cpuset directory. Will be copied.
  1390. * mode: mode to set on new directory.
  1391. */
  1392. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  1393. {
  1394. struct dentry *dentry = NULL;
  1395. struct dentry *parent;
  1396. int error = 0;
  1397. parent = cs->parent->dentry;
  1398. dentry = cpuset_get_dentry(parent, name);
  1399. if (IS_ERR(dentry))
  1400. return PTR_ERR(dentry);
  1401. error = cpuset_create_file(dentry, S_IFDIR | mode);
  1402. if (!error) {
  1403. dentry->d_fsdata = cs;
  1404. parent->d_inode->i_nlink++;
  1405. cs->dentry = dentry;
  1406. }
  1407. dput(dentry);
  1408. return error;
  1409. }
  1410. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  1411. {
  1412. struct dentry *dentry;
  1413. int error;
  1414. mutex_lock(&dir->d_inode->i_mutex);
  1415. dentry = cpuset_get_dentry(dir, cft->name);
  1416. if (!IS_ERR(dentry)) {
  1417. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  1418. if (!error)
  1419. dentry->d_fsdata = (void *)cft;
  1420. dput(dentry);
  1421. } else
  1422. error = PTR_ERR(dentry);
  1423. mutex_unlock(&dir->d_inode->i_mutex);
  1424. return error;
  1425. }
  1426. /*
  1427. * Stuff for reading the 'tasks' file.
  1428. *
  1429. * Reading this file can return large amounts of data if a cpuset has
  1430. * *lots* of attached tasks. So it may need several calls to read(),
  1431. * but we cannot guarantee that the information we produce is correct
  1432. * unless we produce it entirely atomically.
  1433. *
  1434. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1435. * will have a pointer to an array (also allocated here). The struct
  1436. * ctr_struct * is stored in file->private_data. Its resources will
  1437. * be freed by release() when the file is closed. The array is used
  1438. * to sprintf the PIDs and then used by read().
  1439. */
  1440. /* cpusets_tasks_read array */
  1441. struct ctr_struct {
  1442. char *buf;
  1443. int bufsz;
  1444. };
  1445. /*
  1446. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1447. * Return actual number of pids loaded. No need to task_lock(p)
  1448. * when reading out p->cpuset, as we don't really care if it changes
  1449. * on the next cycle, and we are not going to try to dereference it.
  1450. */
  1451. static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1452. {
  1453. int n = 0;
  1454. struct task_struct *g, *p;
  1455. read_lock(&tasklist_lock);
  1456. do_each_thread(g, p) {
  1457. if (p->cpuset == cs) {
  1458. pidarray[n++] = p->pid;
  1459. if (unlikely(n == npids))
  1460. goto array_full;
  1461. }
  1462. } while_each_thread(g, p);
  1463. array_full:
  1464. read_unlock(&tasklist_lock);
  1465. return n;
  1466. }
  1467. static int cmppid(const void *a, const void *b)
  1468. {
  1469. return *(pid_t *)a - *(pid_t *)b;
  1470. }
  1471. /*
  1472. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1473. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1474. * count 'cnt' of how many chars would be written if buf were large enough.
  1475. */
  1476. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1477. {
  1478. int cnt = 0;
  1479. int i;
  1480. for (i = 0; i < npids; i++)
  1481. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1482. return cnt;
  1483. }
  1484. /*
  1485. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1486. * process id's of tasks currently attached to the cpuset being opened.
  1487. *
  1488. * Does not require any specific cpuset mutexes, and does not take any.
  1489. */
  1490. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1491. {
  1492. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1493. struct ctr_struct *ctr;
  1494. pid_t *pidarray;
  1495. int npids;
  1496. char c;
  1497. if (!(file->f_mode & FMODE_READ))
  1498. return 0;
  1499. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1500. if (!ctr)
  1501. goto err0;
  1502. /*
  1503. * If cpuset gets more users after we read count, we won't have
  1504. * enough space - tough. This race is indistinguishable to the
  1505. * caller from the case that the additional cpuset users didn't
  1506. * show up until sometime later on.
  1507. */
  1508. npids = atomic_read(&cs->count);
  1509. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1510. if (!pidarray)
  1511. goto err1;
  1512. npids = pid_array_load(pidarray, npids, cs);
  1513. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1514. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1515. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1516. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1517. if (!ctr->buf)
  1518. goto err2;
  1519. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1520. kfree(pidarray);
  1521. file->private_data = ctr;
  1522. return 0;
  1523. err2:
  1524. kfree(pidarray);
  1525. err1:
  1526. kfree(ctr);
  1527. err0:
  1528. return -ENOMEM;
  1529. }
  1530. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1531. size_t nbytes, loff_t *ppos)
  1532. {
  1533. struct ctr_struct *ctr = file->private_data;
  1534. if (*ppos + nbytes > ctr->bufsz)
  1535. nbytes = ctr->bufsz - *ppos;
  1536. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1537. return -EFAULT;
  1538. *ppos += nbytes;
  1539. return nbytes;
  1540. }
  1541. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1542. {
  1543. struct ctr_struct *ctr;
  1544. if (file->f_mode & FMODE_READ) {
  1545. ctr = file->private_data;
  1546. kfree(ctr->buf);
  1547. kfree(ctr);
  1548. }
  1549. return 0;
  1550. }
  1551. /*
  1552. * for the common functions, 'private' gives the type of file
  1553. */
  1554. static struct cftype cft_tasks = {
  1555. .name = "tasks",
  1556. .open = cpuset_tasks_open,
  1557. .read = cpuset_tasks_read,
  1558. .release = cpuset_tasks_release,
  1559. .private = FILE_TASKLIST,
  1560. };
  1561. static struct cftype cft_cpus = {
  1562. .name = "cpus",
  1563. .private = FILE_CPULIST,
  1564. };
  1565. static struct cftype cft_mems = {
  1566. .name = "mems",
  1567. .private = FILE_MEMLIST,
  1568. };
  1569. static struct cftype cft_cpu_exclusive = {
  1570. .name = "cpu_exclusive",
  1571. .private = FILE_CPU_EXCLUSIVE,
  1572. };
  1573. static struct cftype cft_mem_exclusive = {
  1574. .name = "mem_exclusive",
  1575. .private = FILE_MEM_EXCLUSIVE,
  1576. };
  1577. static struct cftype cft_notify_on_release = {
  1578. .name = "notify_on_release",
  1579. .private = FILE_NOTIFY_ON_RELEASE,
  1580. };
  1581. static struct cftype cft_memory_migrate = {
  1582. .name = "memory_migrate",
  1583. .private = FILE_MEMORY_MIGRATE,
  1584. };
  1585. static struct cftype cft_memory_pressure_enabled = {
  1586. .name = "memory_pressure_enabled",
  1587. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1588. };
  1589. static struct cftype cft_memory_pressure = {
  1590. .name = "memory_pressure",
  1591. .private = FILE_MEMORY_PRESSURE,
  1592. };
  1593. static struct cftype cft_spread_page = {
  1594. .name = "memory_spread_page",
  1595. .private = FILE_SPREAD_PAGE,
  1596. };
  1597. static struct cftype cft_spread_slab = {
  1598. .name = "memory_spread_slab",
  1599. .private = FILE_SPREAD_SLAB,
  1600. };
  1601. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1602. {
  1603. int err;
  1604. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1605. return err;
  1606. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1607. return err;
  1608. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1609. return err;
  1610. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1611. return err;
  1612. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1613. return err;
  1614. if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
  1615. return err;
  1616. if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
  1617. return err;
  1618. if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0)
  1619. return err;
  1620. if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0)
  1621. return err;
  1622. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1623. return err;
  1624. return 0;
  1625. }
  1626. /*
  1627. * cpuset_create - create a cpuset
  1628. * parent: cpuset that will be parent of the new cpuset.
  1629. * name: name of the new cpuset. Will be strcpy'ed.
  1630. * mode: mode to set on new inode
  1631. *
  1632. * Must be called with the mutex on the parent inode held
  1633. */
  1634. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1635. {
  1636. struct cpuset *cs;
  1637. int err;
  1638. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1639. if (!cs)
  1640. return -ENOMEM;
  1641. mutex_lock(&manage_mutex);
  1642. cpuset_update_task_memory_state();
  1643. cs->flags = 0;
  1644. if (notify_on_release(parent))
  1645. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1646. if (is_spread_page(parent))
  1647. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1648. if (is_spread_slab(parent))
  1649. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1650. cs->cpus_allowed = CPU_MASK_NONE;
  1651. cs->mems_allowed = NODE_MASK_NONE;
  1652. atomic_set(&cs->count, 0);
  1653. INIT_LIST_HEAD(&cs->sibling);
  1654. INIT_LIST_HEAD(&cs->children);
  1655. cs->mems_generation = cpuset_mems_generation++;
  1656. fmeter_init(&cs->fmeter);
  1657. cs->parent = parent;
  1658. mutex_lock(&callback_mutex);
  1659. list_add(&cs->sibling, &cs->parent->children);
  1660. number_of_cpusets++;
  1661. mutex_unlock(&callback_mutex);
  1662. err = cpuset_create_dir(cs, name, mode);
  1663. if (err < 0)
  1664. goto err;
  1665. /*
  1666. * Release manage_mutex before cpuset_populate_dir() because it
  1667. * will down() this new directory's i_mutex and if we race with
  1668. * another mkdir, we might deadlock.
  1669. */
  1670. mutex_unlock(&manage_mutex);
  1671. err = cpuset_populate_dir(cs->dentry);
  1672. /* If err < 0, we have a half-filled directory - oh well ;) */
  1673. return 0;
  1674. err:
  1675. list_del(&cs->sibling);
  1676. mutex_unlock(&manage_mutex);
  1677. kfree(cs);
  1678. return err;
  1679. }
  1680. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1681. {
  1682. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1683. /* the vfs holds inode->i_mutex already */
  1684. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1685. }
  1686. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1687. {
  1688. struct cpuset *cs = dentry->d_fsdata;
  1689. struct dentry *d;
  1690. struct cpuset *parent;
  1691. char *pathbuf = NULL;
  1692. /* the vfs holds both inode->i_mutex already */
  1693. mutex_lock(&manage_mutex);
  1694. cpuset_update_task_memory_state();
  1695. if (atomic_read(&cs->count) > 0) {
  1696. mutex_unlock(&manage_mutex);
  1697. return -EBUSY;
  1698. }
  1699. if (!list_empty(&cs->children)) {
  1700. mutex_unlock(&manage_mutex);
  1701. return -EBUSY;
  1702. }
  1703. parent = cs->parent;
  1704. mutex_lock(&callback_mutex);
  1705. set_bit(CS_REMOVED, &cs->flags);
  1706. if (is_cpu_exclusive(cs))
  1707. update_cpu_domains(cs);
  1708. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1709. spin_lock(&cs->dentry->d_lock);
  1710. d = dget(cs->dentry);
  1711. cs->dentry = NULL;
  1712. spin_unlock(&d->d_lock);
  1713. cpuset_d_remove_dir(d);
  1714. dput(d);
  1715. number_of_cpusets--;
  1716. mutex_unlock(&callback_mutex);
  1717. if (list_empty(&parent->children))
  1718. check_for_release(parent, &pathbuf);
  1719. mutex_unlock(&manage_mutex);
  1720. cpuset_release_agent(pathbuf);
  1721. return 0;
  1722. }
  1723. /*
  1724. * cpuset_init_early - just enough so that the calls to
  1725. * cpuset_update_task_memory_state() in early init code
  1726. * are harmless.
  1727. */
  1728. int __init cpuset_init_early(void)
  1729. {
  1730. struct task_struct *tsk = current;
  1731. tsk->cpuset = &top_cpuset;
  1732. tsk->cpuset->mems_generation = cpuset_mems_generation++;
  1733. return 0;
  1734. }
  1735. /**
  1736. * cpuset_init - initialize cpusets at system boot
  1737. *
  1738. * Description: Initialize top_cpuset and the cpuset internal file system,
  1739. **/
  1740. int __init cpuset_init(void)
  1741. {
  1742. struct dentry *root;
  1743. int err;
  1744. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1745. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1746. fmeter_init(&top_cpuset.fmeter);
  1747. top_cpuset.mems_generation = cpuset_mems_generation++;
  1748. init_task.cpuset = &top_cpuset;
  1749. err = register_filesystem(&cpuset_fs_type);
  1750. if (err < 0)
  1751. goto out;
  1752. cpuset_mount = kern_mount(&cpuset_fs_type);
  1753. if (IS_ERR(cpuset_mount)) {
  1754. printk(KERN_ERR "cpuset: could not mount!\n");
  1755. err = PTR_ERR(cpuset_mount);
  1756. cpuset_mount = NULL;
  1757. goto out;
  1758. }
  1759. root = cpuset_mount->mnt_sb->s_root;
  1760. root->d_fsdata = &top_cpuset;
  1761. root->d_inode->i_nlink++;
  1762. top_cpuset.dentry = root;
  1763. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1764. number_of_cpusets = 1;
  1765. err = cpuset_populate_dir(root);
  1766. /* memory_pressure_enabled is in root cpuset only */
  1767. if (err == 0)
  1768. err = cpuset_add_file(root, &cft_memory_pressure_enabled);
  1769. out:
  1770. return err;
  1771. }
  1772. /**
  1773. * cpuset_init_smp - initialize cpus_allowed
  1774. *
  1775. * Description: Finish top cpuset after cpu, node maps are initialized
  1776. **/
  1777. void __init cpuset_init_smp(void)
  1778. {
  1779. top_cpuset.cpus_allowed = cpu_online_map;
  1780. top_cpuset.mems_allowed = node_online_map;
  1781. }
  1782. /**
  1783. * cpuset_fork - attach newly forked task to its parents cpuset.
  1784. * @tsk: pointer to task_struct of forking parent process.
  1785. *
  1786. * Description: A task inherits its parent's cpuset at fork().
  1787. *
  1788. * A pointer to the shared cpuset was automatically copied in fork.c
  1789. * by dup_task_struct(). However, we ignore that copy, since it was
  1790. * not made under the protection of task_lock(), so might no longer be
  1791. * a valid cpuset pointer. attach_task() might have already changed
  1792. * current->cpuset, allowing the previously referenced cpuset to
  1793. * be removed and freed. Instead, we task_lock(current) and copy
  1794. * its present value of current->cpuset for our freshly forked child.
  1795. *
  1796. * At the point that cpuset_fork() is called, 'current' is the parent
  1797. * task, and the passed argument 'child' points to the child task.
  1798. **/
  1799. void cpuset_fork(struct task_struct *child)
  1800. {
  1801. task_lock(current);
  1802. child->cpuset = current->cpuset;
  1803. atomic_inc(&child->cpuset->count);
  1804. task_unlock(current);
  1805. }
  1806. /**
  1807. * cpuset_exit - detach cpuset from exiting task
  1808. * @tsk: pointer to task_struct of exiting process
  1809. *
  1810. * Description: Detach cpuset from @tsk and release it.
  1811. *
  1812. * Note that cpusets marked notify_on_release force every task in
  1813. * them to take the global manage_mutex mutex when exiting.
  1814. * This could impact scaling on very large systems. Be reluctant to
  1815. * use notify_on_release cpusets where very high task exit scaling
  1816. * is required on large systems.
  1817. *
  1818. * Don't even think about derefencing 'cs' after the cpuset use count
  1819. * goes to zero, except inside a critical section guarded by manage_mutex
  1820. * or callback_mutex. Otherwise a zero cpuset use count is a license to
  1821. * any other task to nuke the cpuset immediately, via cpuset_rmdir().
  1822. *
  1823. * This routine has to take manage_mutex, not callback_mutex, because
  1824. * it is holding that mutex while calling check_for_release(),
  1825. * which calls kmalloc(), so can't be called holding callback_mutex().
  1826. *
  1827. * We don't need to task_lock() this reference to tsk->cpuset,
  1828. * because tsk is already marked PF_EXITING, so attach_task() won't
  1829. * mess with it, or task is a failed fork, never visible to attach_task.
  1830. *
  1831. * the_top_cpuset_hack:
  1832. *
  1833. * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
  1834. *
  1835. * Don't leave a task unable to allocate memory, as that is an
  1836. * accident waiting to happen should someone add a callout in
  1837. * do_exit() after the cpuset_exit() call that might allocate.
  1838. * If a task tries to allocate memory with an invalid cpuset,
  1839. * it will oops in cpuset_update_task_memory_state().
  1840. *
  1841. * We call cpuset_exit() while the task is still competent to
  1842. * handle notify_on_release(), then leave the task attached to
  1843. * the root cpuset (top_cpuset) for the remainder of its exit.
  1844. *
  1845. * To do this properly, we would increment the reference count on
  1846. * top_cpuset, and near the very end of the kernel/exit.c do_exit()
  1847. * code we would add a second cpuset function call, to drop that
  1848. * reference. This would just create an unnecessary hot spot on
  1849. * the top_cpuset reference count, to no avail.
  1850. *
  1851. * Normally, holding a reference to a cpuset without bumping its
  1852. * count is unsafe. The cpuset could go away, or someone could
  1853. * attach us to a different cpuset, decrementing the count on
  1854. * the first cpuset that we never incremented. But in this case,
  1855. * top_cpuset isn't going away, and either task has PF_EXITING set,
  1856. * which wards off any attach_task() attempts, or task is a failed
  1857. * fork, never visible to attach_task.
  1858. *
  1859. * Another way to do this would be to set the cpuset pointer
  1860. * to NULL here, and check in cpuset_update_task_memory_state()
  1861. * for a NULL pointer. This hack avoids that NULL check, for no
  1862. * cost (other than this way too long comment ;).
  1863. **/
  1864. void cpuset_exit(struct task_struct *tsk)
  1865. {
  1866. struct cpuset *cs;
  1867. cs = tsk->cpuset;
  1868. tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
  1869. if (notify_on_release(cs)) {
  1870. char *pathbuf = NULL;
  1871. mutex_lock(&manage_mutex);
  1872. if (atomic_dec_and_test(&cs->count))
  1873. check_for_release(cs, &pathbuf);
  1874. mutex_unlock(&manage_mutex);
  1875. cpuset_release_agent(pathbuf);
  1876. } else {
  1877. atomic_dec(&cs->count);
  1878. }
  1879. }
  1880. /**
  1881. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1882. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1883. *
  1884. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1885. * attached to the specified @tsk. Guaranteed to return some non-empty
  1886. * subset of cpu_online_map, even if this means going outside the
  1887. * tasks cpuset.
  1888. **/
  1889. cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
  1890. {
  1891. cpumask_t mask;
  1892. mutex_lock(&callback_mutex);
  1893. task_lock(tsk);
  1894. guarantee_online_cpus(tsk->cpuset, &mask);
  1895. task_unlock(tsk);
  1896. mutex_unlock(&callback_mutex);
  1897. return mask;
  1898. }
  1899. void cpuset_init_current_mems_allowed(void)
  1900. {
  1901. current->mems_allowed = NODE_MASK_ALL;
  1902. }
  1903. /**
  1904. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  1905. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  1906. *
  1907. * Description: Returns the nodemask_t mems_allowed of the cpuset
  1908. * attached to the specified @tsk. Guaranteed to return some non-empty
  1909. * subset of node_online_map, even if this means going outside the
  1910. * tasks cpuset.
  1911. **/
  1912. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  1913. {
  1914. nodemask_t mask;
  1915. mutex_lock(&callback_mutex);
  1916. task_lock(tsk);
  1917. guarantee_online_mems(tsk->cpuset, &mask);
  1918. task_unlock(tsk);
  1919. mutex_unlock(&callback_mutex);
  1920. return mask;
  1921. }
  1922. /**
  1923. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1924. * @zl: the zonelist to be checked
  1925. *
  1926. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1927. */
  1928. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1929. {
  1930. int i;
  1931. for (i = 0; zl->zones[i]; i++) {
  1932. int nid = zl->zones[i]->zone_pgdat->node_id;
  1933. if (node_isset(nid, current->mems_allowed))
  1934. return 1;
  1935. }
  1936. return 0;
  1937. }
  1938. /*
  1939. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1940. * ancestor to the specified cpuset. Call holding callback_mutex.
  1941. * If no ancestor is mem_exclusive (an unusual configuration), then
  1942. * returns the root cpuset.
  1943. */
  1944. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1945. {
  1946. while (!is_mem_exclusive(cs) && cs->parent)
  1947. cs = cs->parent;
  1948. return cs;
  1949. }
  1950. /**
  1951. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1952. * @z: is this zone on an allowed node?
  1953. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1954. *
  1955. * If we're in interrupt, yes, we can always allocate. If zone
  1956. * z's node is in our tasks mems_allowed, yes. If it's not a
  1957. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1958. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1959. * Otherwise, no.
  1960. *
  1961. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1962. * and do not allow allocations outside the current tasks cpuset.
  1963. * GFP_KERNEL allocations are not so marked, so can escape to the
  1964. * nearest mem_exclusive ancestor cpuset.
  1965. *
  1966. * Scanning up parent cpusets requires callback_mutex. The __alloc_pages()
  1967. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1968. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1969. * mems_allowed came up empty on the first pass over the zonelist.
  1970. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1971. * short of memory, might require taking the callback_mutex mutex.
  1972. *
  1973. * The first call here from mm/page_alloc:get_page_from_freelist()
  1974. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets, so
  1975. * no allocation on a node outside the cpuset is allowed (unless in
  1976. * interrupt, of course).
  1977. *
  1978. * The second pass through get_page_from_freelist() doesn't even call
  1979. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  1980. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  1981. * in alloc_flags. That logic and the checks below have the combined
  1982. * affect that:
  1983. * in_interrupt - any node ok (current task context irrelevant)
  1984. * GFP_ATOMIC - any node ok
  1985. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1986. * GFP_USER - only nodes in current tasks mems allowed ok.
  1987. *
  1988. * Rule:
  1989. * Don't call cpuset_zone_allowed() if you can't sleep, unless you
  1990. * pass in the __GFP_HARDWALL flag set in gfp_flag, which disables
  1991. * the code that might scan up ancestor cpusets and sleep.
  1992. **/
  1993. int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  1994. {
  1995. int node; /* node that zone z is on */
  1996. const struct cpuset *cs; /* current cpuset ancestors */
  1997. int allowed; /* is allocation in zone z allowed? */
  1998. if (in_interrupt())
  1999. return 1;
  2000. node = z->zone_pgdat->node_id;
  2001. might_sleep_if(!(gfp_mask & __GFP_HARDWALL));
  2002. if (node_isset(node, current->mems_allowed))
  2003. return 1;
  2004. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  2005. return 0;
  2006. if (current->flags & PF_EXITING) /* Let dying task have memory */
  2007. return 1;
  2008. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  2009. mutex_lock(&callback_mutex);
  2010. task_lock(current);
  2011. cs = nearest_exclusive_ancestor(current->cpuset);
  2012. task_unlock(current);
  2013. allowed = node_isset(node, cs->mems_allowed);
  2014. mutex_unlock(&callback_mutex);
  2015. return allowed;
  2016. }
  2017. /**
  2018. * cpuset_lock - lock out any changes to cpuset structures
  2019. *
  2020. * The out of memory (oom) code needs to mutex_lock cpusets
  2021. * from being changed while it scans the tasklist looking for a
  2022. * task in an overlapping cpuset. Expose callback_mutex via this
  2023. * cpuset_lock() routine, so the oom code can lock it, before
  2024. * locking the task list. The tasklist_lock is a spinlock, so
  2025. * must be taken inside callback_mutex.
  2026. */
  2027. void cpuset_lock(void)
  2028. {
  2029. mutex_lock(&callback_mutex);
  2030. }
  2031. /**
  2032. * cpuset_unlock - release lock on cpuset changes
  2033. *
  2034. * Undo the lock taken in a previous cpuset_lock() call.
  2035. */
  2036. void cpuset_unlock(void)
  2037. {
  2038. mutex_unlock(&callback_mutex);
  2039. }
  2040. /**
  2041. * cpuset_mem_spread_node() - On which node to begin search for a page
  2042. *
  2043. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  2044. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  2045. * and if the memory allocation used cpuset_mem_spread_node()
  2046. * to determine on which node to start looking, as it will for
  2047. * certain page cache or slab cache pages such as used for file
  2048. * system buffers and inode caches, then instead of starting on the
  2049. * local node to look for a free page, rather spread the starting
  2050. * node around the tasks mems_allowed nodes.
  2051. *
  2052. * We don't have to worry about the returned node being offline
  2053. * because "it can't happen", and even if it did, it would be ok.
  2054. *
  2055. * The routines calling guarantee_online_mems() are careful to
  2056. * only set nodes in task->mems_allowed that are online. So it
  2057. * should not be possible for the following code to return an
  2058. * offline node. But if it did, that would be ok, as this routine
  2059. * is not returning the node where the allocation must be, only
  2060. * the node where the search should start. The zonelist passed to
  2061. * __alloc_pages() will include all nodes. If the slab allocator
  2062. * is passed an offline node, it will fall back to the local node.
  2063. * See kmem_cache_alloc_node().
  2064. */
  2065. int cpuset_mem_spread_node(void)
  2066. {
  2067. int node;
  2068. node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
  2069. if (node == MAX_NUMNODES)
  2070. node = first_node(current->mems_allowed);
  2071. current->cpuset_mem_spread_rotor = node;
  2072. return node;
  2073. }
  2074. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  2075. /**
  2076. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  2077. * @p: pointer to task_struct of some other task.
  2078. *
  2079. * Description: Return true if the nearest mem_exclusive ancestor
  2080. * cpusets of tasks @p and current overlap. Used by oom killer to
  2081. * determine if task @p's memory usage might impact the memory
  2082. * available to the current task.
  2083. *
  2084. * Call while holding callback_mutex.
  2085. **/
  2086. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  2087. {
  2088. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  2089. int overlap = 0; /* do cpusets overlap? */
  2090. task_lock(current);
  2091. if (current->flags & PF_EXITING) {
  2092. task_unlock(current);
  2093. goto done;
  2094. }
  2095. cs1 = nearest_exclusive_ancestor(current->cpuset);
  2096. task_unlock(current);
  2097. task_lock((struct task_struct *)p);
  2098. if (p->flags & PF_EXITING) {
  2099. task_unlock((struct task_struct *)p);
  2100. goto done;
  2101. }
  2102. cs2 = nearest_exclusive_ancestor(p->cpuset);
  2103. task_unlock((struct task_struct *)p);
  2104. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  2105. done:
  2106. return overlap;
  2107. }
  2108. /*
  2109. * Collection of memory_pressure is suppressed unless
  2110. * this flag is enabled by writing "1" to the special
  2111. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  2112. */
  2113. int cpuset_memory_pressure_enabled __read_mostly;
  2114. /**
  2115. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  2116. *
  2117. * Keep a running average of the rate of synchronous (direct)
  2118. * page reclaim efforts initiated by tasks in each cpuset.
  2119. *
  2120. * This represents the rate at which some task in the cpuset
  2121. * ran low on memory on all nodes it was allowed to use, and
  2122. * had to enter the kernels page reclaim code in an effort to
  2123. * create more free memory by tossing clean pages or swapping
  2124. * or writing dirty pages.
  2125. *
  2126. * Display to user space in the per-cpuset read-only file
  2127. * "memory_pressure". Value displayed is an integer
  2128. * representing the recent rate of entry into the synchronous
  2129. * (direct) page reclaim by any task attached to the cpuset.
  2130. **/
  2131. void __cpuset_memory_pressure_bump(void)
  2132. {
  2133. struct cpuset *cs;
  2134. task_lock(current);
  2135. cs = current->cpuset;
  2136. fmeter_markevent(&cs->fmeter);
  2137. task_unlock(current);
  2138. }
  2139. /*
  2140. * proc_cpuset_show()
  2141. * - Print tasks cpuset path into seq_file.
  2142. * - Used for /proc/<pid>/cpuset.
  2143. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  2144. * doesn't really matter if tsk->cpuset changes after we read it,
  2145. * and we take manage_mutex, keeping attach_task() from changing it
  2146. * anyway. No need to check that tsk->cpuset != NULL, thanks to
  2147. * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
  2148. * cpuset to top_cpuset.
  2149. */
  2150. static int proc_cpuset_show(struct seq_file *m, void *v)
  2151. {
  2152. struct pid *pid;
  2153. struct task_struct *tsk;
  2154. char *buf;
  2155. int retval;
  2156. retval = -ENOMEM;
  2157. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2158. if (!buf)
  2159. goto out;
  2160. retval = -ESRCH;
  2161. pid = m->private;
  2162. tsk = get_pid_task(pid, PIDTYPE_PID);
  2163. if (!tsk)
  2164. goto out_free;
  2165. retval = -EINVAL;
  2166. mutex_lock(&manage_mutex);
  2167. retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE);
  2168. if (retval < 0)
  2169. goto out_unlock;
  2170. seq_puts(m, buf);
  2171. seq_putc(m, '\n');
  2172. out_unlock:
  2173. mutex_unlock(&manage_mutex);
  2174. put_task_struct(tsk);
  2175. out_free:
  2176. kfree(buf);
  2177. out:
  2178. return retval;
  2179. }
  2180. static int cpuset_open(struct inode *inode, struct file *file)
  2181. {
  2182. struct pid *pid = PROC_I(inode)->pid;
  2183. return single_open(file, proc_cpuset_show, pid);
  2184. }
  2185. struct file_operations proc_cpuset_operations = {
  2186. .open = cpuset_open,
  2187. .read = seq_read,
  2188. .llseek = seq_lseek,
  2189. .release = single_release,
  2190. };
  2191. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  2192. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  2193. {
  2194. buffer += sprintf(buffer, "Cpus_allowed:\t");
  2195. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  2196. buffer += sprintf(buffer, "\n");
  2197. buffer += sprintf(buffer, "Mems_allowed:\t");
  2198. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  2199. buffer += sprintf(buffer, "\n");
  2200. return buffer;
  2201. }