cpuset.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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 Silicon Graphics, Inc.
  8. *
  9. * Portions derived from Patrick Mochel's sysfs code.
  10. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  11. * Portions Copyright (c) 2004 Silicon Graphics, Inc.
  12. *
  13. * 2003-10-10 Written by Simon Derr <simon.derr@bull.net>
  14. * 2003-10-22 Updates by Stephen Hemminger.
  15. * 2004 May-July Rework by Paul Jackson <pj@sgi.com>
  16. *
  17. * This file is subject to the terms and conditions of the GNU General Public
  18. * License. See the file COPYING in the main directory of the Linux
  19. * distribution for more details.
  20. */
  21. #include <linux/config.h>
  22. #include <linux/cpu.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/cpuset.h>
  25. #include <linux/err.h>
  26. #include <linux/errno.h>
  27. #include <linux/file.h>
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kmod.h>
  33. #include <linux/list.h>
  34. #include <linux/mempolicy.h>
  35. #include <linux/mm.h>
  36. #include <linux/module.h>
  37. #include <linux/mount.h>
  38. #include <linux/namei.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/sched.h>
  42. #include <linux/seq_file.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 <asm/semaphore.h>
  54. #define CPUSET_SUPER_MAGIC 0x27e0eb
  55. struct cpuset {
  56. unsigned long flags; /* "unsigned long" so bitops work */
  57. cpumask_t cpus_allowed; /* CPUs allowed to tasks in cpuset */
  58. nodemask_t mems_allowed; /* Memory Nodes allowed to tasks */
  59. /*
  60. * Count is atomic so can incr (fork) or decr (exit) without a lock.
  61. */
  62. atomic_t count; /* count tasks using this cpuset */
  63. /*
  64. * We link our 'sibling' struct into our parents 'children'.
  65. * Our children link their 'sibling' into our 'children'.
  66. */
  67. struct list_head sibling; /* my parents children */
  68. struct list_head children; /* my children */
  69. struct cpuset *parent; /* my parent */
  70. struct dentry *dentry; /* cpuset fs entry */
  71. /*
  72. * Copy of global cpuset_mems_generation as of the most
  73. * recent time this cpuset changed its mems_allowed.
  74. */
  75. int mems_generation;
  76. };
  77. /* bits in struct cpuset flags field */
  78. typedef enum {
  79. CS_CPU_EXCLUSIVE,
  80. CS_MEM_EXCLUSIVE,
  81. CS_MEMORY_MIGRATE,
  82. CS_REMOVED,
  83. CS_NOTIFY_ON_RELEASE
  84. } cpuset_flagbits_t;
  85. /* convenient tests for these bits */
  86. static inline int is_cpu_exclusive(const struct cpuset *cs)
  87. {
  88. return !!test_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  89. }
  90. static inline int is_mem_exclusive(const struct cpuset *cs)
  91. {
  92. return !!test_bit(CS_MEM_EXCLUSIVE, &cs->flags);
  93. }
  94. static inline int is_removed(const struct cpuset *cs)
  95. {
  96. return !!test_bit(CS_REMOVED, &cs->flags);
  97. }
  98. static inline int notify_on_release(const struct cpuset *cs)
  99. {
  100. return !!test_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  101. }
  102. static inline int is_memory_migrate(const struct cpuset *cs)
  103. {
  104. return !!test_bit(CS_MEMORY_MIGRATE, &cs->flags);
  105. }
  106. /*
  107. * Increment this atomic integer everytime any cpuset changes its
  108. * mems_allowed value. Users of cpusets can track this generation
  109. * number, and avoid having to lock and reload mems_allowed unless
  110. * the cpuset they're using changes generation.
  111. *
  112. * A single, global generation is needed because attach_task() could
  113. * reattach a task to a different cpuset, which must not have its
  114. * generation numbers aliased with those of that tasks previous cpuset.
  115. *
  116. * Generations are needed for mems_allowed because one task cannot
  117. * modify anothers memory placement. So we must enable every task,
  118. * on every visit to __alloc_pages(), to efficiently check whether
  119. * its current->cpuset->mems_allowed has changed, requiring an update
  120. * of its current->mems_allowed.
  121. */
  122. static atomic_t cpuset_mems_generation = ATOMIC_INIT(1);
  123. static struct cpuset top_cpuset = {
  124. .flags = ((1 << CS_CPU_EXCLUSIVE) | (1 << CS_MEM_EXCLUSIVE)),
  125. .cpus_allowed = CPU_MASK_ALL,
  126. .mems_allowed = NODE_MASK_ALL,
  127. .count = ATOMIC_INIT(0),
  128. .sibling = LIST_HEAD_INIT(top_cpuset.sibling),
  129. .children = LIST_HEAD_INIT(top_cpuset.children),
  130. .parent = NULL,
  131. .dentry = NULL,
  132. .mems_generation = 0,
  133. };
  134. static struct vfsmount *cpuset_mount;
  135. static struct super_block *cpuset_sb = NULL;
  136. /*
  137. * We have two global cpuset semaphores below. They can nest.
  138. * It is ok to first take manage_sem, then nest callback_sem. We also
  139. * require taking task_lock() when dereferencing a tasks cpuset pointer.
  140. * See "The task_lock() exception", at the end of this comment.
  141. *
  142. * A task must hold both semaphores to modify cpusets. If a task
  143. * holds manage_sem, then it blocks others wanting that semaphore,
  144. * ensuring that it is the only task able to also acquire callback_sem
  145. * and be able to modify cpusets. It can perform various checks on
  146. * the cpuset structure first, knowing nothing will change. It can
  147. * also allocate memory while just holding manage_sem. While it is
  148. * performing these checks, various callback routines can briefly
  149. * acquire callback_sem to query cpusets. Once it is ready to make
  150. * the changes, it takes callback_sem, blocking everyone else.
  151. *
  152. * Calls to the kernel memory allocator can not be made while holding
  153. * callback_sem, as that would risk double tripping on callback_sem
  154. * from one of the callbacks into the cpuset code from within
  155. * __alloc_pages().
  156. *
  157. * If a task is only holding callback_sem, then it has read-only
  158. * access to cpusets.
  159. *
  160. * The task_struct fields mems_allowed and mems_generation may only
  161. * be accessed in the context of that task, so require no locks.
  162. *
  163. * Any task can increment and decrement the count field without lock.
  164. * So in general, code holding manage_sem or callback_sem can't rely
  165. * on the count field not changing. However, if the count goes to
  166. * zero, then only attach_task(), which holds both semaphores, can
  167. * increment it again. Because a count of zero means that no tasks
  168. * are currently attached, therefore there is no way a task attached
  169. * to that cpuset can fork (the other way to increment the count).
  170. * So code holding manage_sem or callback_sem can safely assume that
  171. * if the count is zero, it will stay zero. Similarly, if a task
  172. * holds manage_sem or callback_sem on a cpuset with zero count, it
  173. * knows that the cpuset won't be removed, as cpuset_rmdir() needs
  174. * both of those semaphores.
  175. *
  176. * A possible optimization to improve parallelism would be to make
  177. * callback_sem a R/W semaphore (rwsem), allowing the callback routines
  178. * to proceed in parallel, with read access, until the holder of
  179. * manage_sem needed to take this rwsem for exclusive write access
  180. * and modify some cpusets.
  181. *
  182. * The cpuset_common_file_write handler for operations that modify
  183. * the cpuset hierarchy holds manage_sem across the entire operation,
  184. * single threading all such cpuset modifications across the system.
  185. *
  186. * The cpuset_common_file_read() handlers only hold callback_sem across
  187. * small pieces of code, such as when reading out possibly multi-word
  188. * cpumasks and nodemasks.
  189. *
  190. * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
  191. * (usually) take either semaphore. These are the two most performance
  192. * critical pieces of code here. The exception occurs on cpuset_exit(),
  193. * when a task in a notify_on_release cpuset exits. Then manage_sem
  194. * is taken, and if the cpuset count is zero, a usermode call made
  195. * to /sbin/cpuset_release_agent with the name of the cpuset (path
  196. * relative to the root of cpuset file system) as the argument.
  197. *
  198. * A cpuset can only be deleted if both its 'count' of using tasks
  199. * is zero, and its list of 'children' cpusets is empty. Since all
  200. * tasks in the system use _some_ cpuset, and since there is always at
  201. * least one task in the system (init, pid == 1), therefore, top_cpuset
  202. * always has either children cpusets and/or using tasks. So we don't
  203. * need a special hack to ensure that top_cpuset cannot be deleted.
  204. *
  205. * The above "Tale of Two Semaphores" would be complete, but for:
  206. *
  207. * The task_lock() exception
  208. *
  209. * The need for this exception arises from the action of attach_task(),
  210. * which overwrites one tasks cpuset pointer with another. It does
  211. * so using both semaphores, however there are several performance
  212. * critical places that need to reference task->cpuset without the
  213. * expense of grabbing a system global semaphore. Therefore except as
  214. * noted below, when dereferencing or, as in attach_task(), modifying
  215. * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
  216. * (task->alloc_lock) already in the task_struct routinely used for
  217. * such matters.
  218. */
  219. static DECLARE_MUTEX(manage_sem);
  220. static DECLARE_MUTEX(callback_sem);
  221. /*
  222. * A couple of forward declarations required, due to cyclic reference loop:
  223. * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
  224. * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
  225. */
  226. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  227. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
  228. static struct backing_dev_info cpuset_backing_dev_info = {
  229. .ra_pages = 0, /* No readahead */
  230. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  231. };
  232. static struct inode *cpuset_new_inode(mode_t mode)
  233. {
  234. struct inode *inode = new_inode(cpuset_sb);
  235. if (inode) {
  236. inode->i_mode = mode;
  237. inode->i_uid = current->fsuid;
  238. inode->i_gid = current->fsgid;
  239. inode->i_blksize = PAGE_CACHE_SIZE;
  240. inode->i_blocks = 0;
  241. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  242. inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
  243. }
  244. return inode;
  245. }
  246. static void cpuset_diput(struct dentry *dentry, struct inode *inode)
  247. {
  248. /* is dentry a directory ? if so, kfree() associated cpuset */
  249. if (S_ISDIR(inode->i_mode)) {
  250. struct cpuset *cs = dentry->d_fsdata;
  251. BUG_ON(!(is_removed(cs)));
  252. kfree(cs);
  253. }
  254. iput(inode);
  255. }
  256. static struct dentry_operations cpuset_dops = {
  257. .d_iput = cpuset_diput,
  258. };
  259. static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
  260. {
  261. struct dentry *d = lookup_one_len(name, parent, strlen(name));
  262. if (!IS_ERR(d))
  263. d->d_op = &cpuset_dops;
  264. return d;
  265. }
  266. static void remove_dir(struct dentry *d)
  267. {
  268. struct dentry *parent = dget(d->d_parent);
  269. d_delete(d);
  270. simple_rmdir(parent->d_inode, d);
  271. dput(parent);
  272. }
  273. /*
  274. * NOTE : the dentry must have been dget()'ed
  275. */
  276. static void cpuset_d_remove_dir(struct dentry *dentry)
  277. {
  278. struct list_head *node;
  279. spin_lock(&dcache_lock);
  280. node = dentry->d_subdirs.next;
  281. while (node != &dentry->d_subdirs) {
  282. struct dentry *d = list_entry(node, struct dentry, d_child);
  283. list_del_init(node);
  284. if (d->d_inode) {
  285. d = dget_locked(d);
  286. spin_unlock(&dcache_lock);
  287. d_delete(d);
  288. simple_unlink(dentry->d_inode, d);
  289. dput(d);
  290. spin_lock(&dcache_lock);
  291. }
  292. node = dentry->d_subdirs.next;
  293. }
  294. list_del_init(&dentry->d_child);
  295. spin_unlock(&dcache_lock);
  296. remove_dir(dentry);
  297. }
  298. static struct super_operations cpuset_ops = {
  299. .statfs = simple_statfs,
  300. .drop_inode = generic_delete_inode,
  301. };
  302. static int cpuset_fill_super(struct super_block *sb, void *unused_data,
  303. int unused_silent)
  304. {
  305. struct inode *inode;
  306. struct dentry *root;
  307. sb->s_blocksize = PAGE_CACHE_SIZE;
  308. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  309. sb->s_magic = CPUSET_SUPER_MAGIC;
  310. sb->s_op = &cpuset_ops;
  311. cpuset_sb = sb;
  312. inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
  313. if (inode) {
  314. inode->i_op = &simple_dir_inode_operations;
  315. inode->i_fop = &simple_dir_operations;
  316. /* directories start off with i_nlink == 2 (for "." entry) */
  317. inode->i_nlink++;
  318. } else {
  319. return -ENOMEM;
  320. }
  321. root = d_alloc_root(inode);
  322. if (!root) {
  323. iput(inode);
  324. return -ENOMEM;
  325. }
  326. sb->s_root = root;
  327. return 0;
  328. }
  329. static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
  330. int flags, const char *unused_dev_name,
  331. void *data)
  332. {
  333. return get_sb_single(fs_type, flags, data, cpuset_fill_super);
  334. }
  335. static struct file_system_type cpuset_fs_type = {
  336. .name = "cpuset",
  337. .get_sb = cpuset_get_sb,
  338. .kill_sb = kill_litter_super,
  339. };
  340. /* struct cftype:
  341. *
  342. * The files in the cpuset filesystem mostly have a very simple read/write
  343. * handling, some common function will take care of it. Nevertheless some cases
  344. * (read tasks) are special and therefore I define this structure for every
  345. * kind of file.
  346. *
  347. *
  348. * When reading/writing to a file:
  349. * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
  350. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  351. */
  352. struct cftype {
  353. char *name;
  354. int private;
  355. int (*open) (struct inode *inode, struct file *file);
  356. ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
  357. loff_t *ppos);
  358. int (*write) (struct file *file, const char __user *buf, size_t nbytes,
  359. loff_t *ppos);
  360. int (*release) (struct inode *inode, struct file *file);
  361. };
  362. static inline struct cpuset *__d_cs(struct dentry *dentry)
  363. {
  364. return dentry->d_fsdata;
  365. }
  366. static inline struct cftype *__d_cft(struct dentry *dentry)
  367. {
  368. return dentry->d_fsdata;
  369. }
  370. /*
  371. * Call with manage_sem held. Writes path of cpuset into buf.
  372. * Returns 0 on success, -errno on error.
  373. */
  374. static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
  375. {
  376. char *start;
  377. start = buf + buflen;
  378. *--start = '\0';
  379. for (;;) {
  380. int len = cs->dentry->d_name.len;
  381. if ((start -= len) < buf)
  382. return -ENAMETOOLONG;
  383. memcpy(start, cs->dentry->d_name.name, len);
  384. cs = cs->parent;
  385. if (!cs)
  386. break;
  387. if (!cs->parent)
  388. continue;
  389. if (--start < buf)
  390. return -ENAMETOOLONG;
  391. *start = '/';
  392. }
  393. memmove(buf, start, buf + buflen - start);
  394. return 0;
  395. }
  396. /*
  397. * Notify userspace when a cpuset is released, by running
  398. * /sbin/cpuset_release_agent with the name of the cpuset (path
  399. * relative to the root of cpuset file system) as the argument.
  400. *
  401. * Most likely, this user command will try to rmdir this cpuset.
  402. *
  403. * This races with the possibility that some other task will be
  404. * attached to this cpuset before it is removed, or that some other
  405. * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
  406. * The presumed 'rmdir' will fail quietly if this cpuset is no longer
  407. * unused, and this cpuset will be reprieved from its death sentence,
  408. * to continue to serve a useful existence. Next time it's released,
  409. * we will get notified again, if it still has 'notify_on_release' set.
  410. *
  411. * The final arg to call_usermodehelper() is 0, which means don't
  412. * wait. The separate /sbin/cpuset_release_agent task is forked by
  413. * call_usermodehelper(), then control in this thread returns here,
  414. * without waiting for the release agent task. We don't bother to
  415. * wait because the caller of this routine has no use for the exit
  416. * status of the /sbin/cpuset_release_agent task, so no sense holding
  417. * our caller up for that.
  418. *
  419. * When we had only one cpuset semaphore, we had to call this
  420. * without holding it, to avoid deadlock when call_usermodehelper()
  421. * allocated memory. With two locks, we could now call this while
  422. * holding manage_sem, but we still don't, so as to minimize
  423. * the time manage_sem is held.
  424. */
  425. static void cpuset_release_agent(const char *pathbuf)
  426. {
  427. char *argv[3], *envp[3];
  428. int i;
  429. if (!pathbuf)
  430. return;
  431. i = 0;
  432. argv[i++] = "/sbin/cpuset_release_agent";
  433. argv[i++] = (char *)pathbuf;
  434. argv[i] = NULL;
  435. i = 0;
  436. /* minimal command environment */
  437. envp[i++] = "HOME=/";
  438. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  439. envp[i] = NULL;
  440. call_usermodehelper(argv[0], argv, envp, 0);
  441. kfree(pathbuf);
  442. }
  443. /*
  444. * Either cs->count of using tasks transitioned to zero, or the
  445. * cs->children list of child cpusets just became empty. If this
  446. * cs is notify_on_release() and now both the user count is zero and
  447. * the list of children is empty, prepare cpuset path in a kmalloc'd
  448. * buffer, to be returned via ppathbuf, so that the caller can invoke
  449. * cpuset_release_agent() with it later on, once manage_sem is dropped.
  450. * Call here with manage_sem held.
  451. *
  452. * This check_for_release() routine is responsible for kmalloc'ing
  453. * pathbuf. The above cpuset_release_agent() is responsible for
  454. * kfree'ing pathbuf. The caller of these routines is responsible
  455. * for providing a pathbuf pointer, initialized to NULL, then
  456. * calling check_for_release() with manage_sem held and the address
  457. * of the pathbuf pointer, then dropping manage_sem, then calling
  458. * cpuset_release_agent() with pathbuf, as set by check_for_release().
  459. */
  460. static void check_for_release(struct cpuset *cs, char **ppathbuf)
  461. {
  462. if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
  463. list_empty(&cs->children)) {
  464. char *buf;
  465. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  466. if (!buf)
  467. return;
  468. if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
  469. kfree(buf);
  470. else
  471. *ppathbuf = buf;
  472. }
  473. }
  474. /*
  475. * Return in *pmask the portion of a cpusets's cpus_allowed that
  476. * are online. If none are online, walk up the cpuset hierarchy
  477. * until we find one that does have some online cpus. If we get
  478. * all the way to the top and still haven't found any online cpus,
  479. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  480. * task, return cpu_online_map.
  481. *
  482. * One way or another, we guarantee to return some non-empty subset
  483. * of cpu_online_map.
  484. *
  485. * Call with callback_sem held.
  486. */
  487. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  488. {
  489. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  490. cs = cs->parent;
  491. if (cs)
  492. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  493. else
  494. *pmask = cpu_online_map;
  495. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  496. }
  497. /*
  498. * Return in *pmask the portion of a cpusets's mems_allowed that
  499. * are online. If none are online, walk up the cpuset hierarchy
  500. * until we find one that does have some online mems. If we get
  501. * all the way to the top and still haven't found any online mems,
  502. * return node_online_map.
  503. *
  504. * One way or another, we guarantee to return some non-empty subset
  505. * of node_online_map.
  506. *
  507. * Call with callback_sem held.
  508. */
  509. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  510. {
  511. while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
  512. cs = cs->parent;
  513. if (cs)
  514. nodes_and(*pmask, cs->mems_allowed, node_online_map);
  515. else
  516. *pmask = node_online_map;
  517. BUG_ON(!nodes_intersects(*pmask, node_online_map));
  518. }
  519. /*
  520. * Refresh current tasks mems_allowed and mems_generation from current
  521. * tasks cpuset.
  522. *
  523. * Call without callback_sem or task_lock() held. May be called with
  524. * or without manage_sem held. Will acquire task_lock() and might
  525. * acquire callback_sem during call.
  526. *
  527. * The task_lock() is required to dereference current->cpuset safely.
  528. * Without it, we could pick up the pointer value of current->cpuset
  529. * in one instruction, and then attach_task could give us a different
  530. * cpuset, and then the cpuset we had could be removed and freed,
  531. * and then on our next instruction, we could dereference a no longer
  532. * valid cpuset pointer to get its mems_generation field.
  533. *
  534. * This routine is needed to update the per-task mems_allowed data,
  535. * within the tasks context, when it is trying to allocate memory
  536. * (in various mm/mempolicy.c routines) and notices that some other
  537. * task has been modifying its cpuset.
  538. */
  539. static void refresh_mems(void)
  540. {
  541. int my_cpusets_mem_gen;
  542. task_lock(current);
  543. my_cpusets_mem_gen = current->cpuset->mems_generation;
  544. task_unlock(current);
  545. if (current->cpuset_mems_generation != my_cpusets_mem_gen) {
  546. struct cpuset *cs;
  547. nodemask_t oldmem = current->mems_allowed;
  548. int migrate;
  549. down(&callback_sem);
  550. task_lock(current);
  551. cs = current->cpuset;
  552. migrate = is_memory_migrate(cs);
  553. guarantee_online_mems(cs, &current->mems_allowed);
  554. current->cpuset_mems_generation = cs->mems_generation;
  555. task_unlock(current);
  556. up(&callback_sem);
  557. if (!nodes_equal(oldmem, current->mems_allowed)) {
  558. numa_policy_rebind(&oldmem, &current->mems_allowed);
  559. if (migrate) {
  560. do_migrate_pages(current->mm, &oldmem,
  561. &current->mems_allowed,
  562. MPOL_MF_MOVE_ALL);
  563. }
  564. }
  565. }
  566. }
  567. /*
  568. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  569. *
  570. * One cpuset is a subset of another if all its allowed CPUs and
  571. * Memory Nodes are a subset of the other, and its exclusive flags
  572. * are only set if the other's are set. Call holding manage_sem.
  573. */
  574. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  575. {
  576. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  577. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  578. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  579. is_mem_exclusive(p) <= is_mem_exclusive(q);
  580. }
  581. /*
  582. * validate_change() - Used to validate that any proposed cpuset change
  583. * follows the structural rules for cpusets.
  584. *
  585. * If we replaced the flag and mask values of the current cpuset
  586. * (cur) with those values in the trial cpuset (trial), would
  587. * our various subset and exclusive rules still be valid? Presumes
  588. * manage_sem held.
  589. *
  590. * 'cur' is the address of an actual, in-use cpuset. Operations
  591. * such as list traversal that depend on the actual address of the
  592. * cpuset in the list must use cur below, not trial.
  593. *
  594. * 'trial' is the address of bulk structure copy of cur, with
  595. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  596. * or flags changed to new, trial values.
  597. *
  598. * Return 0 if valid, -errno if not.
  599. */
  600. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  601. {
  602. struct cpuset *c, *par;
  603. /* Each of our child cpusets must be a subset of us */
  604. list_for_each_entry(c, &cur->children, sibling) {
  605. if (!is_cpuset_subset(c, trial))
  606. return -EBUSY;
  607. }
  608. /* Remaining checks don't apply to root cpuset */
  609. if ((par = cur->parent) == NULL)
  610. return 0;
  611. /* We must be a subset of our parent cpuset */
  612. if (!is_cpuset_subset(trial, par))
  613. return -EACCES;
  614. /* If either I or some sibling (!= me) is exclusive, we can't overlap */
  615. list_for_each_entry(c, &par->children, sibling) {
  616. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  617. c != cur &&
  618. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  619. return -EINVAL;
  620. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  621. c != cur &&
  622. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  623. return -EINVAL;
  624. }
  625. return 0;
  626. }
  627. /*
  628. * For a given cpuset cur, partition the system as follows
  629. * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
  630. * exclusive child cpusets
  631. * b. All cpus in the current cpuset's cpus_allowed that are not part of any
  632. * exclusive child cpusets
  633. * Build these two partitions by calling partition_sched_domains
  634. *
  635. * Call with manage_sem held. May nest a call to the
  636. * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
  637. */
  638. static void update_cpu_domains(struct cpuset *cur)
  639. {
  640. struct cpuset *c, *par = cur->parent;
  641. cpumask_t pspan, cspan;
  642. if (par == NULL || cpus_empty(cur->cpus_allowed))
  643. return;
  644. /*
  645. * Get all cpus from parent's cpus_allowed not part of exclusive
  646. * children
  647. */
  648. pspan = par->cpus_allowed;
  649. list_for_each_entry(c, &par->children, sibling) {
  650. if (is_cpu_exclusive(c))
  651. cpus_andnot(pspan, pspan, c->cpus_allowed);
  652. }
  653. if (is_removed(cur) || !is_cpu_exclusive(cur)) {
  654. cpus_or(pspan, pspan, cur->cpus_allowed);
  655. if (cpus_equal(pspan, cur->cpus_allowed))
  656. return;
  657. cspan = CPU_MASK_NONE;
  658. } else {
  659. if (cpus_empty(pspan))
  660. return;
  661. cspan = cur->cpus_allowed;
  662. /*
  663. * Get all cpus from current cpuset's cpus_allowed not part
  664. * of exclusive children
  665. */
  666. list_for_each_entry(c, &cur->children, sibling) {
  667. if (is_cpu_exclusive(c))
  668. cpus_andnot(cspan, cspan, c->cpus_allowed);
  669. }
  670. }
  671. lock_cpu_hotplug();
  672. partition_sched_domains(&pspan, &cspan);
  673. unlock_cpu_hotplug();
  674. }
  675. /*
  676. * Call with manage_sem held. May take callback_sem during call.
  677. */
  678. static int update_cpumask(struct cpuset *cs, char *buf)
  679. {
  680. struct cpuset trialcs;
  681. int retval, cpus_unchanged;
  682. trialcs = *cs;
  683. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  684. if (retval < 0)
  685. return retval;
  686. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  687. if (cpus_empty(trialcs.cpus_allowed))
  688. return -ENOSPC;
  689. retval = validate_change(cs, &trialcs);
  690. if (retval < 0)
  691. return retval;
  692. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  693. down(&callback_sem);
  694. cs->cpus_allowed = trialcs.cpus_allowed;
  695. up(&callback_sem);
  696. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  697. update_cpu_domains(cs);
  698. return 0;
  699. }
  700. /*
  701. * Call with manage_sem held. May take callback_sem during call.
  702. */
  703. static int update_nodemask(struct cpuset *cs, char *buf)
  704. {
  705. struct cpuset trialcs;
  706. int retval;
  707. trialcs = *cs;
  708. retval = nodelist_parse(buf, trialcs.mems_allowed);
  709. if (retval < 0)
  710. return retval;
  711. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  712. if (nodes_empty(trialcs.mems_allowed))
  713. return -ENOSPC;
  714. retval = validate_change(cs, &trialcs);
  715. if (retval == 0) {
  716. down(&callback_sem);
  717. cs->mems_allowed = trialcs.mems_allowed;
  718. atomic_inc(&cpuset_mems_generation);
  719. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  720. up(&callback_sem);
  721. }
  722. return retval;
  723. }
  724. /*
  725. * update_flag - read a 0 or a 1 in a file and update associated flag
  726. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  727. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE)
  728. * cs: the cpuset to update
  729. * buf: the buffer where we read the 0 or 1
  730. *
  731. * Call with manage_sem held.
  732. */
  733. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  734. {
  735. int turning_on;
  736. struct cpuset trialcs;
  737. int err, cpu_exclusive_changed;
  738. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  739. trialcs = *cs;
  740. if (turning_on)
  741. set_bit(bit, &trialcs.flags);
  742. else
  743. clear_bit(bit, &trialcs.flags);
  744. err = validate_change(cs, &trialcs);
  745. if (err < 0)
  746. return err;
  747. cpu_exclusive_changed =
  748. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  749. down(&callback_sem);
  750. if (turning_on)
  751. set_bit(bit, &cs->flags);
  752. else
  753. clear_bit(bit, &cs->flags);
  754. up(&callback_sem);
  755. if (cpu_exclusive_changed)
  756. update_cpu_domains(cs);
  757. return 0;
  758. }
  759. /*
  760. * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
  761. * writing the path of the old cpuset in 'ppathbuf' if it needs to be
  762. * notified on release.
  763. *
  764. * Call holding manage_sem. May take callback_sem and task_lock of
  765. * the task 'pid' during call.
  766. */
  767. static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
  768. {
  769. pid_t pid;
  770. struct task_struct *tsk;
  771. struct cpuset *oldcs;
  772. cpumask_t cpus;
  773. nodemask_t from, to;
  774. if (sscanf(pidbuf, "%d", &pid) != 1)
  775. return -EIO;
  776. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  777. return -ENOSPC;
  778. if (pid) {
  779. read_lock(&tasklist_lock);
  780. tsk = find_task_by_pid(pid);
  781. if (!tsk || tsk->flags & PF_EXITING) {
  782. read_unlock(&tasklist_lock);
  783. return -ESRCH;
  784. }
  785. get_task_struct(tsk);
  786. read_unlock(&tasklist_lock);
  787. if ((current->euid) && (current->euid != tsk->uid)
  788. && (current->euid != tsk->suid)) {
  789. put_task_struct(tsk);
  790. return -EACCES;
  791. }
  792. } else {
  793. tsk = current;
  794. get_task_struct(tsk);
  795. }
  796. down(&callback_sem);
  797. task_lock(tsk);
  798. oldcs = tsk->cpuset;
  799. if (!oldcs) {
  800. task_unlock(tsk);
  801. up(&callback_sem);
  802. put_task_struct(tsk);
  803. return -ESRCH;
  804. }
  805. atomic_inc(&cs->count);
  806. tsk->cpuset = cs;
  807. task_unlock(tsk);
  808. guarantee_online_cpus(cs, &cpus);
  809. set_cpus_allowed(tsk, cpus);
  810. from = oldcs->mems_allowed;
  811. to = cs->mems_allowed;
  812. up(&callback_sem);
  813. if (is_memory_migrate(cs))
  814. do_migrate_pages(tsk->mm, &from, &to, MPOL_MF_MOVE_ALL);
  815. put_task_struct(tsk);
  816. if (atomic_dec_and_test(&oldcs->count))
  817. check_for_release(oldcs, ppathbuf);
  818. return 0;
  819. }
  820. /* The various types of files and directories in a cpuset file system */
  821. typedef enum {
  822. FILE_ROOT,
  823. FILE_DIR,
  824. FILE_MEMORY_MIGRATE,
  825. FILE_CPULIST,
  826. FILE_MEMLIST,
  827. FILE_CPU_EXCLUSIVE,
  828. FILE_MEM_EXCLUSIVE,
  829. FILE_NOTIFY_ON_RELEASE,
  830. FILE_TASKLIST,
  831. } cpuset_filetype_t;
  832. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  833. size_t nbytes, loff_t *unused_ppos)
  834. {
  835. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  836. struct cftype *cft = __d_cft(file->f_dentry);
  837. cpuset_filetype_t type = cft->private;
  838. char *buffer;
  839. char *pathbuf = NULL;
  840. int retval = 0;
  841. /* Crude upper limit on largest legitimate cpulist user might write. */
  842. if (nbytes > 100 + 6 * NR_CPUS)
  843. return -E2BIG;
  844. /* +1 for nul-terminator */
  845. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  846. return -ENOMEM;
  847. if (copy_from_user(buffer, userbuf, nbytes)) {
  848. retval = -EFAULT;
  849. goto out1;
  850. }
  851. buffer[nbytes] = 0; /* nul-terminate */
  852. down(&manage_sem);
  853. if (is_removed(cs)) {
  854. retval = -ENODEV;
  855. goto out2;
  856. }
  857. switch (type) {
  858. case FILE_CPULIST:
  859. retval = update_cpumask(cs, buffer);
  860. break;
  861. case FILE_MEMLIST:
  862. retval = update_nodemask(cs, buffer);
  863. break;
  864. case FILE_CPU_EXCLUSIVE:
  865. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  866. break;
  867. case FILE_MEM_EXCLUSIVE:
  868. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  869. break;
  870. case FILE_NOTIFY_ON_RELEASE:
  871. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  872. break;
  873. case FILE_MEMORY_MIGRATE:
  874. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  875. break;
  876. case FILE_TASKLIST:
  877. retval = attach_task(cs, buffer, &pathbuf);
  878. break;
  879. default:
  880. retval = -EINVAL;
  881. goto out2;
  882. }
  883. if (retval == 0)
  884. retval = nbytes;
  885. out2:
  886. up(&manage_sem);
  887. cpuset_release_agent(pathbuf);
  888. out1:
  889. kfree(buffer);
  890. return retval;
  891. }
  892. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  893. size_t nbytes, loff_t *ppos)
  894. {
  895. ssize_t retval = 0;
  896. struct cftype *cft = __d_cft(file->f_dentry);
  897. if (!cft)
  898. return -ENODEV;
  899. /* special function ? */
  900. if (cft->write)
  901. retval = cft->write(file, buf, nbytes, ppos);
  902. else
  903. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  904. return retval;
  905. }
  906. /*
  907. * These ascii lists should be read in a single call, by using a user
  908. * buffer large enough to hold the entire map. If read in smaller
  909. * chunks, there is no guarantee of atomicity. Since the display format
  910. * used, list of ranges of sequential numbers, is variable length,
  911. * and since these maps can change value dynamically, one could read
  912. * gibberish by doing partial reads while a list was changing.
  913. * A single large read to a buffer that crosses a page boundary is
  914. * ok, because the result being copied to user land is not recomputed
  915. * across a page fault.
  916. */
  917. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  918. {
  919. cpumask_t mask;
  920. down(&callback_sem);
  921. mask = cs->cpus_allowed;
  922. up(&callback_sem);
  923. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  924. }
  925. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  926. {
  927. nodemask_t mask;
  928. down(&callback_sem);
  929. mask = cs->mems_allowed;
  930. up(&callback_sem);
  931. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  932. }
  933. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  934. size_t nbytes, loff_t *ppos)
  935. {
  936. struct cftype *cft = __d_cft(file->f_dentry);
  937. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  938. cpuset_filetype_t type = cft->private;
  939. char *page;
  940. ssize_t retval = 0;
  941. char *s;
  942. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  943. return -ENOMEM;
  944. s = page;
  945. switch (type) {
  946. case FILE_CPULIST:
  947. s += cpuset_sprintf_cpulist(s, cs);
  948. break;
  949. case FILE_MEMLIST:
  950. s += cpuset_sprintf_memlist(s, cs);
  951. break;
  952. case FILE_CPU_EXCLUSIVE:
  953. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  954. break;
  955. case FILE_MEM_EXCLUSIVE:
  956. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  957. break;
  958. case FILE_NOTIFY_ON_RELEASE:
  959. *s++ = notify_on_release(cs) ? '1' : '0';
  960. break;
  961. case FILE_MEMORY_MIGRATE:
  962. *s++ = is_memory_migrate(cs) ? '1' : '0';
  963. break;
  964. default:
  965. retval = -EINVAL;
  966. goto out;
  967. }
  968. *s++ = '\n';
  969. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  970. out:
  971. free_page((unsigned long)page);
  972. return retval;
  973. }
  974. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  975. loff_t *ppos)
  976. {
  977. ssize_t retval = 0;
  978. struct cftype *cft = __d_cft(file->f_dentry);
  979. if (!cft)
  980. return -ENODEV;
  981. /* special function ? */
  982. if (cft->read)
  983. retval = cft->read(file, buf, nbytes, ppos);
  984. else
  985. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  986. return retval;
  987. }
  988. static int cpuset_file_open(struct inode *inode, struct file *file)
  989. {
  990. int err;
  991. struct cftype *cft;
  992. err = generic_file_open(inode, file);
  993. if (err)
  994. return err;
  995. cft = __d_cft(file->f_dentry);
  996. if (!cft)
  997. return -ENODEV;
  998. if (cft->open)
  999. err = cft->open(inode, file);
  1000. else
  1001. err = 0;
  1002. return err;
  1003. }
  1004. static int cpuset_file_release(struct inode *inode, struct file *file)
  1005. {
  1006. struct cftype *cft = __d_cft(file->f_dentry);
  1007. if (cft->release)
  1008. return cft->release(inode, file);
  1009. return 0;
  1010. }
  1011. /*
  1012. * cpuset_rename - Only allow simple rename of directories in place.
  1013. */
  1014. static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
  1015. struct inode *new_dir, struct dentry *new_dentry)
  1016. {
  1017. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1018. return -ENOTDIR;
  1019. if (new_dentry->d_inode)
  1020. return -EEXIST;
  1021. if (old_dir != new_dir)
  1022. return -EIO;
  1023. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1024. }
  1025. static struct file_operations cpuset_file_operations = {
  1026. .read = cpuset_file_read,
  1027. .write = cpuset_file_write,
  1028. .llseek = generic_file_llseek,
  1029. .open = cpuset_file_open,
  1030. .release = cpuset_file_release,
  1031. };
  1032. static struct inode_operations cpuset_dir_inode_operations = {
  1033. .lookup = simple_lookup,
  1034. .mkdir = cpuset_mkdir,
  1035. .rmdir = cpuset_rmdir,
  1036. .rename = cpuset_rename,
  1037. };
  1038. static int cpuset_create_file(struct dentry *dentry, int mode)
  1039. {
  1040. struct inode *inode;
  1041. if (!dentry)
  1042. return -ENOENT;
  1043. if (dentry->d_inode)
  1044. return -EEXIST;
  1045. inode = cpuset_new_inode(mode);
  1046. if (!inode)
  1047. return -ENOMEM;
  1048. if (S_ISDIR(mode)) {
  1049. inode->i_op = &cpuset_dir_inode_operations;
  1050. inode->i_fop = &simple_dir_operations;
  1051. /* start off with i_nlink == 2 (for "." entry) */
  1052. inode->i_nlink++;
  1053. } else if (S_ISREG(mode)) {
  1054. inode->i_size = 0;
  1055. inode->i_fop = &cpuset_file_operations;
  1056. }
  1057. d_instantiate(dentry, inode);
  1058. dget(dentry); /* Extra count - pin the dentry in core */
  1059. return 0;
  1060. }
  1061. /*
  1062. * cpuset_create_dir - create a directory for an object.
  1063. * cs: the cpuset we create the directory for.
  1064. * It must have a valid ->parent field
  1065. * And we are going to fill its ->dentry field.
  1066. * name: The name to give to the cpuset directory. Will be copied.
  1067. * mode: mode to set on new directory.
  1068. */
  1069. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  1070. {
  1071. struct dentry *dentry = NULL;
  1072. struct dentry *parent;
  1073. int error = 0;
  1074. parent = cs->parent->dentry;
  1075. dentry = cpuset_get_dentry(parent, name);
  1076. if (IS_ERR(dentry))
  1077. return PTR_ERR(dentry);
  1078. error = cpuset_create_file(dentry, S_IFDIR | mode);
  1079. if (!error) {
  1080. dentry->d_fsdata = cs;
  1081. parent->d_inode->i_nlink++;
  1082. cs->dentry = dentry;
  1083. }
  1084. dput(dentry);
  1085. return error;
  1086. }
  1087. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  1088. {
  1089. struct dentry *dentry;
  1090. int error;
  1091. down(&dir->d_inode->i_sem);
  1092. dentry = cpuset_get_dentry(dir, cft->name);
  1093. if (!IS_ERR(dentry)) {
  1094. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  1095. if (!error)
  1096. dentry->d_fsdata = (void *)cft;
  1097. dput(dentry);
  1098. } else
  1099. error = PTR_ERR(dentry);
  1100. up(&dir->d_inode->i_sem);
  1101. return error;
  1102. }
  1103. /*
  1104. * Stuff for reading the 'tasks' file.
  1105. *
  1106. * Reading this file can return large amounts of data if a cpuset has
  1107. * *lots* of attached tasks. So it may need several calls to read(),
  1108. * but we cannot guarantee that the information we produce is correct
  1109. * unless we produce it entirely atomically.
  1110. *
  1111. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1112. * will have a pointer to an array (also allocated here). The struct
  1113. * ctr_struct * is stored in file->private_data. Its resources will
  1114. * be freed by release() when the file is closed. The array is used
  1115. * to sprintf the PIDs and then used by read().
  1116. */
  1117. /* cpusets_tasks_read array */
  1118. struct ctr_struct {
  1119. char *buf;
  1120. int bufsz;
  1121. };
  1122. /*
  1123. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1124. * Return actual number of pids loaded. No need to task_lock(p)
  1125. * when reading out p->cpuset, as we don't really care if it changes
  1126. * on the next cycle, and we are not going to try to dereference it.
  1127. */
  1128. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1129. {
  1130. int n = 0;
  1131. struct task_struct *g, *p;
  1132. read_lock(&tasklist_lock);
  1133. do_each_thread(g, p) {
  1134. if (p->cpuset == cs) {
  1135. pidarray[n++] = p->pid;
  1136. if (unlikely(n == npids))
  1137. goto array_full;
  1138. }
  1139. } while_each_thread(g, p);
  1140. array_full:
  1141. read_unlock(&tasklist_lock);
  1142. return n;
  1143. }
  1144. static int cmppid(const void *a, const void *b)
  1145. {
  1146. return *(pid_t *)a - *(pid_t *)b;
  1147. }
  1148. /*
  1149. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1150. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1151. * count 'cnt' of how many chars would be written if buf were large enough.
  1152. */
  1153. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1154. {
  1155. int cnt = 0;
  1156. int i;
  1157. for (i = 0; i < npids; i++)
  1158. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1159. return cnt;
  1160. }
  1161. /*
  1162. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1163. * process id's of tasks currently attached to the cpuset being opened.
  1164. *
  1165. * Does not require any specific cpuset semaphores, and does not take any.
  1166. */
  1167. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1168. {
  1169. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1170. struct ctr_struct *ctr;
  1171. pid_t *pidarray;
  1172. int npids;
  1173. char c;
  1174. if (!(file->f_mode & FMODE_READ))
  1175. return 0;
  1176. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1177. if (!ctr)
  1178. goto err0;
  1179. /*
  1180. * If cpuset gets more users after we read count, we won't have
  1181. * enough space - tough. This race is indistinguishable to the
  1182. * caller from the case that the additional cpuset users didn't
  1183. * show up until sometime later on.
  1184. */
  1185. npids = atomic_read(&cs->count);
  1186. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1187. if (!pidarray)
  1188. goto err1;
  1189. npids = pid_array_load(pidarray, npids, cs);
  1190. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1191. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1192. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1193. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1194. if (!ctr->buf)
  1195. goto err2;
  1196. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1197. kfree(pidarray);
  1198. file->private_data = ctr;
  1199. return 0;
  1200. err2:
  1201. kfree(pidarray);
  1202. err1:
  1203. kfree(ctr);
  1204. err0:
  1205. return -ENOMEM;
  1206. }
  1207. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1208. size_t nbytes, loff_t *ppos)
  1209. {
  1210. struct ctr_struct *ctr = file->private_data;
  1211. if (*ppos + nbytes > ctr->bufsz)
  1212. nbytes = ctr->bufsz - *ppos;
  1213. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1214. return -EFAULT;
  1215. *ppos += nbytes;
  1216. return nbytes;
  1217. }
  1218. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1219. {
  1220. struct ctr_struct *ctr;
  1221. if (file->f_mode & FMODE_READ) {
  1222. ctr = file->private_data;
  1223. kfree(ctr->buf);
  1224. kfree(ctr);
  1225. }
  1226. return 0;
  1227. }
  1228. /*
  1229. * for the common functions, 'private' gives the type of file
  1230. */
  1231. static struct cftype cft_tasks = {
  1232. .name = "tasks",
  1233. .open = cpuset_tasks_open,
  1234. .read = cpuset_tasks_read,
  1235. .release = cpuset_tasks_release,
  1236. .private = FILE_TASKLIST,
  1237. };
  1238. static struct cftype cft_cpus = {
  1239. .name = "cpus",
  1240. .private = FILE_CPULIST,
  1241. };
  1242. static struct cftype cft_mems = {
  1243. .name = "mems",
  1244. .private = FILE_MEMLIST,
  1245. };
  1246. static struct cftype cft_cpu_exclusive = {
  1247. .name = "cpu_exclusive",
  1248. .private = FILE_CPU_EXCLUSIVE,
  1249. };
  1250. static struct cftype cft_mem_exclusive = {
  1251. .name = "mem_exclusive",
  1252. .private = FILE_MEM_EXCLUSIVE,
  1253. };
  1254. static struct cftype cft_notify_on_release = {
  1255. .name = "notify_on_release",
  1256. .private = FILE_NOTIFY_ON_RELEASE,
  1257. };
  1258. static struct cftype cft_memory_migrate = {
  1259. .name = "memory_migrate",
  1260. .private = FILE_MEMORY_MIGRATE,
  1261. };
  1262. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1263. {
  1264. int err;
  1265. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1266. return err;
  1267. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1268. return err;
  1269. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1270. return err;
  1271. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1272. return err;
  1273. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1274. return err;
  1275. if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
  1276. return err;
  1277. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1278. return err;
  1279. return 0;
  1280. }
  1281. /*
  1282. * cpuset_create - create a cpuset
  1283. * parent: cpuset that will be parent of the new cpuset.
  1284. * name: name of the new cpuset. Will be strcpy'ed.
  1285. * mode: mode to set on new inode
  1286. *
  1287. * Must be called with the semaphore on the parent inode held
  1288. */
  1289. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1290. {
  1291. struct cpuset *cs;
  1292. int err;
  1293. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1294. if (!cs)
  1295. return -ENOMEM;
  1296. down(&manage_sem);
  1297. refresh_mems();
  1298. cs->flags = 0;
  1299. if (notify_on_release(parent))
  1300. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1301. cs->cpus_allowed = CPU_MASK_NONE;
  1302. cs->mems_allowed = NODE_MASK_NONE;
  1303. atomic_set(&cs->count, 0);
  1304. INIT_LIST_HEAD(&cs->sibling);
  1305. INIT_LIST_HEAD(&cs->children);
  1306. atomic_inc(&cpuset_mems_generation);
  1307. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1308. cs->parent = parent;
  1309. down(&callback_sem);
  1310. list_add(&cs->sibling, &cs->parent->children);
  1311. up(&callback_sem);
  1312. err = cpuset_create_dir(cs, name, mode);
  1313. if (err < 0)
  1314. goto err;
  1315. /*
  1316. * Release manage_sem before cpuset_populate_dir() because it
  1317. * will down() this new directory's i_sem and if we race with
  1318. * another mkdir, we might deadlock.
  1319. */
  1320. up(&manage_sem);
  1321. err = cpuset_populate_dir(cs->dentry);
  1322. /* If err < 0, we have a half-filled directory - oh well ;) */
  1323. return 0;
  1324. err:
  1325. list_del(&cs->sibling);
  1326. up(&manage_sem);
  1327. kfree(cs);
  1328. return err;
  1329. }
  1330. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1331. {
  1332. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1333. /* the vfs holds inode->i_sem already */
  1334. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1335. }
  1336. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1337. {
  1338. struct cpuset *cs = dentry->d_fsdata;
  1339. struct dentry *d;
  1340. struct cpuset *parent;
  1341. char *pathbuf = NULL;
  1342. /* the vfs holds both inode->i_sem already */
  1343. down(&manage_sem);
  1344. refresh_mems();
  1345. if (atomic_read(&cs->count) > 0) {
  1346. up(&manage_sem);
  1347. return -EBUSY;
  1348. }
  1349. if (!list_empty(&cs->children)) {
  1350. up(&manage_sem);
  1351. return -EBUSY;
  1352. }
  1353. parent = cs->parent;
  1354. down(&callback_sem);
  1355. set_bit(CS_REMOVED, &cs->flags);
  1356. if (is_cpu_exclusive(cs))
  1357. update_cpu_domains(cs);
  1358. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1359. spin_lock(&cs->dentry->d_lock);
  1360. d = dget(cs->dentry);
  1361. cs->dentry = NULL;
  1362. spin_unlock(&d->d_lock);
  1363. cpuset_d_remove_dir(d);
  1364. dput(d);
  1365. up(&callback_sem);
  1366. if (list_empty(&parent->children))
  1367. check_for_release(parent, &pathbuf);
  1368. up(&manage_sem);
  1369. cpuset_release_agent(pathbuf);
  1370. return 0;
  1371. }
  1372. /**
  1373. * cpuset_init - initialize cpusets at system boot
  1374. *
  1375. * Description: Initialize top_cpuset and the cpuset internal file system,
  1376. **/
  1377. int __init cpuset_init(void)
  1378. {
  1379. struct dentry *root;
  1380. int err;
  1381. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1382. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1383. atomic_inc(&cpuset_mems_generation);
  1384. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1385. init_task.cpuset = &top_cpuset;
  1386. err = register_filesystem(&cpuset_fs_type);
  1387. if (err < 0)
  1388. goto out;
  1389. cpuset_mount = kern_mount(&cpuset_fs_type);
  1390. if (IS_ERR(cpuset_mount)) {
  1391. printk(KERN_ERR "cpuset: could not mount!\n");
  1392. err = PTR_ERR(cpuset_mount);
  1393. cpuset_mount = NULL;
  1394. goto out;
  1395. }
  1396. root = cpuset_mount->mnt_sb->s_root;
  1397. root->d_fsdata = &top_cpuset;
  1398. root->d_inode->i_nlink++;
  1399. top_cpuset.dentry = root;
  1400. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1401. err = cpuset_populate_dir(root);
  1402. out:
  1403. return err;
  1404. }
  1405. /**
  1406. * cpuset_init_smp - initialize cpus_allowed
  1407. *
  1408. * Description: Finish top cpuset after cpu, node maps are initialized
  1409. **/
  1410. void __init cpuset_init_smp(void)
  1411. {
  1412. top_cpuset.cpus_allowed = cpu_online_map;
  1413. top_cpuset.mems_allowed = node_online_map;
  1414. }
  1415. /**
  1416. * cpuset_fork - attach newly forked task to its parents cpuset.
  1417. * @tsk: pointer to task_struct of forking parent process.
  1418. *
  1419. * Description: A task inherits its parent's cpuset at fork().
  1420. *
  1421. * A pointer to the shared cpuset was automatically copied in fork.c
  1422. * by dup_task_struct(). However, we ignore that copy, since it was
  1423. * not made under the protection of task_lock(), so might no longer be
  1424. * a valid cpuset pointer. attach_task() might have already changed
  1425. * current->cpuset, allowing the previously referenced cpuset to
  1426. * be removed and freed. Instead, we task_lock(current) and copy
  1427. * its present value of current->cpuset for our freshly forked child.
  1428. *
  1429. * At the point that cpuset_fork() is called, 'current' is the parent
  1430. * task, and the passed argument 'child' points to the child task.
  1431. **/
  1432. void cpuset_fork(struct task_struct *child)
  1433. {
  1434. task_lock(current);
  1435. child->cpuset = current->cpuset;
  1436. atomic_inc(&child->cpuset->count);
  1437. task_unlock(current);
  1438. }
  1439. /**
  1440. * cpuset_exit - detach cpuset from exiting task
  1441. * @tsk: pointer to task_struct of exiting process
  1442. *
  1443. * Description: Detach cpuset from @tsk and release it.
  1444. *
  1445. * Note that cpusets marked notify_on_release force every task in
  1446. * them to take the global manage_sem semaphore when exiting.
  1447. * This could impact scaling on very large systems. Be reluctant to
  1448. * use notify_on_release cpusets where very high task exit scaling
  1449. * is required on large systems.
  1450. *
  1451. * Don't even think about derefencing 'cs' after the cpuset use count
  1452. * goes to zero, except inside a critical section guarded by manage_sem
  1453. * or callback_sem. Otherwise a zero cpuset use count is a license to
  1454. * any other task to nuke the cpuset immediately, via cpuset_rmdir().
  1455. *
  1456. * This routine has to take manage_sem, not callback_sem, because
  1457. * it is holding that semaphore while calling check_for_release(),
  1458. * which calls kmalloc(), so can't be called holding callback__sem().
  1459. *
  1460. * We don't need to task_lock() this reference to tsk->cpuset,
  1461. * because tsk is already marked PF_EXITING, so attach_task() won't
  1462. * mess with it.
  1463. **/
  1464. void cpuset_exit(struct task_struct *tsk)
  1465. {
  1466. struct cpuset *cs;
  1467. BUG_ON(!(tsk->flags & PF_EXITING));
  1468. cs = tsk->cpuset;
  1469. tsk->cpuset = NULL;
  1470. if (notify_on_release(cs)) {
  1471. char *pathbuf = NULL;
  1472. down(&manage_sem);
  1473. if (atomic_dec_and_test(&cs->count))
  1474. check_for_release(cs, &pathbuf);
  1475. up(&manage_sem);
  1476. cpuset_release_agent(pathbuf);
  1477. } else {
  1478. atomic_dec(&cs->count);
  1479. }
  1480. }
  1481. /**
  1482. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1483. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1484. *
  1485. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1486. * attached to the specified @tsk. Guaranteed to return some non-empty
  1487. * subset of cpu_online_map, even if this means going outside the
  1488. * tasks cpuset.
  1489. **/
  1490. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1491. {
  1492. cpumask_t mask;
  1493. down(&callback_sem);
  1494. task_lock((struct task_struct *)tsk);
  1495. guarantee_online_cpus(tsk->cpuset, &mask);
  1496. task_unlock((struct task_struct *)tsk);
  1497. up(&callback_sem);
  1498. return mask;
  1499. }
  1500. void cpuset_init_current_mems_allowed(void)
  1501. {
  1502. current->mems_allowed = NODE_MASK_ALL;
  1503. }
  1504. /**
  1505. * cpuset_update_current_mems_allowed - update mems parameters to new values
  1506. *
  1507. * If the current tasks cpusets mems_allowed changed behind our backs,
  1508. * update current->mems_allowed and mems_generation to the new value.
  1509. * Do not call this routine if in_interrupt().
  1510. *
  1511. * Call without callback_sem or task_lock() held. May be called
  1512. * with or without manage_sem held. Unless exiting, it will acquire
  1513. * task_lock(). Also might acquire callback_sem during call to
  1514. * refresh_mems().
  1515. */
  1516. void cpuset_update_current_mems_allowed(void)
  1517. {
  1518. struct cpuset *cs;
  1519. int need_to_refresh = 0;
  1520. task_lock(current);
  1521. cs = current->cpuset;
  1522. if (!cs)
  1523. goto done;
  1524. if (current->cpuset_mems_generation != cs->mems_generation)
  1525. need_to_refresh = 1;
  1526. done:
  1527. task_unlock(current);
  1528. if (need_to_refresh)
  1529. refresh_mems();
  1530. }
  1531. /**
  1532. * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
  1533. * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
  1534. */
  1535. void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
  1536. {
  1537. bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
  1538. MAX_NUMNODES);
  1539. }
  1540. /**
  1541. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1542. * @zl: the zonelist to be checked
  1543. *
  1544. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1545. */
  1546. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1547. {
  1548. int i;
  1549. for (i = 0; zl->zones[i]; i++) {
  1550. int nid = zl->zones[i]->zone_pgdat->node_id;
  1551. if (node_isset(nid, current->mems_allowed))
  1552. return 1;
  1553. }
  1554. return 0;
  1555. }
  1556. /*
  1557. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1558. * ancestor to the specified cpuset. Call holding callback_sem.
  1559. * If no ancestor is mem_exclusive (an unusual configuration), then
  1560. * returns the root cpuset.
  1561. */
  1562. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1563. {
  1564. while (!is_mem_exclusive(cs) && cs->parent)
  1565. cs = cs->parent;
  1566. return cs;
  1567. }
  1568. /**
  1569. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1570. * @z: is this zone on an allowed node?
  1571. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1572. *
  1573. * If we're in interrupt, yes, we can always allocate. If zone
  1574. * z's node is in our tasks mems_allowed, yes. If it's not a
  1575. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1576. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1577. * Otherwise, no.
  1578. *
  1579. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1580. * and do not allow allocations outside the current tasks cpuset.
  1581. * GFP_KERNEL allocations are not so marked, so can escape to the
  1582. * nearest mem_exclusive ancestor cpuset.
  1583. *
  1584. * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
  1585. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1586. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1587. * mems_allowed came up empty on the first pass over the zonelist.
  1588. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1589. * short of memory, might require taking the callback_sem semaphore.
  1590. *
  1591. * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
  1592. * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
  1593. * hardwall cpusets - no allocation on a node outside the cpuset is
  1594. * allowed (unless in interrupt, of course).
  1595. *
  1596. * The second loop doesn't even call here for GFP_ATOMIC requests
  1597. * (if the __alloc_pages() local variable 'wait' is set). That check
  1598. * and the checks below have the combined affect in the second loop of
  1599. * the __alloc_pages() routine that:
  1600. * in_interrupt - any node ok (current task context irrelevant)
  1601. * GFP_ATOMIC - any node ok
  1602. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1603. * GFP_USER - only nodes in current tasks mems allowed ok.
  1604. **/
  1605. int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  1606. {
  1607. int node; /* node that zone z is on */
  1608. const struct cpuset *cs; /* current cpuset ancestors */
  1609. int allowed = 1; /* is allocation in zone z allowed? */
  1610. if (in_interrupt())
  1611. return 1;
  1612. node = z->zone_pgdat->node_id;
  1613. if (node_isset(node, current->mems_allowed))
  1614. return 1;
  1615. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1616. return 0;
  1617. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1618. return 1;
  1619. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1620. down(&callback_sem);
  1621. task_lock(current);
  1622. cs = nearest_exclusive_ancestor(current->cpuset);
  1623. task_unlock(current);
  1624. allowed = node_isset(node, cs->mems_allowed);
  1625. up(&callback_sem);
  1626. return allowed;
  1627. }
  1628. /**
  1629. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  1630. * @p: pointer to task_struct of some other task.
  1631. *
  1632. * Description: Return true if the nearest mem_exclusive ancestor
  1633. * cpusets of tasks @p and current overlap. Used by oom killer to
  1634. * determine if task @p's memory usage might impact the memory
  1635. * available to the current task.
  1636. *
  1637. * Acquires callback_sem - not suitable for calling from a fast path.
  1638. **/
  1639. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  1640. {
  1641. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  1642. int overlap = 0; /* do cpusets overlap? */
  1643. down(&callback_sem);
  1644. task_lock(current);
  1645. if (current->flags & PF_EXITING) {
  1646. task_unlock(current);
  1647. goto done;
  1648. }
  1649. cs1 = nearest_exclusive_ancestor(current->cpuset);
  1650. task_unlock(current);
  1651. task_lock((struct task_struct *)p);
  1652. if (p->flags & PF_EXITING) {
  1653. task_unlock((struct task_struct *)p);
  1654. goto done;
  1655. }
  1656. cs2 = nearest_exclusive_ancestor(p->cpuset);
  1657. task_unlock((struct task_struct *)p);
  1658. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  1659. done:
  1660. up(&callback_sem);
  1661. return overlap;
  1662. }
  1663. /*
  1664. * proc_cpuset_show()
  1665. * - Print tasks cpuset path into seq_file.
  1666. * - Used for /proc/<pid>/cpuset.
  1667. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  1668. * doesn't really matter if tsk->cpuset changes after we read it,
  1669. * and we take manage_sem, keeping attach_task() from changing it
  1670. * anyway.
  1671. */
  1672. static int proc_cpuset_show(struct seq_file *m, void *v)
  1673. {
  1674. struct cpuset *cs;
  1675. struct task_struct *tsk;
  1676. char *buf;
  1677. int retval = 0;
  1678. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1679. if (!buf)
  1680. return -ENOMEM;
  1681. tsk = m->private;
  1682. down(&manage_sem);
  1683. cs = tsk->cpuset;
  1684. if (!cs) {
  1685. retval = -EINVAL;
  1686. goto out;
  1687. }
  1688. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1689. if (retval < 0)
  1690. goto out;
  1691. seq_puts(m, buf);
  1692. seq_putc(m, '\n');
  1693. out:
  1694. up(&manage_sem);
  1695. kfree(buf);
  1696. return retval;
  1697. }
  1698. static int cpuset_open(struct inode *inode, struct file *file)
  1699. {
  1700. struct task_struct *tsk = PROC_I(inode)->task;
  1701. return single_open(file, proc_cpuset_show, tsk);
  1702. }
  1703. struct file_operations proc_cpuset_operations = {
  1704. .open = cpuset_open,
  1705. .read = seq_read,
  1706. .llseek = seq_lseek,
  1707. .release = single_release,
  1708. };
  1709. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1710. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1711. {
  1712. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1713. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1714. buffer += sprintf(buffer, "\n");
  1715. buffer += sprintf(buffer, "Mems_allowed:\t");
  1716. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1717. buffer += sprintf(buffer, "\n");
  1718. return buffer;
  1719. }