cpuset.c 48 KB

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