cpuset.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. * if the last task using a cpuset exits, and the cpuset was marked
  153. * notify_on_release. In that case, the cpuset_sem is taken, the
  154. * path to the released cpuset calculated, and a usermode call made
  155. * to /sbin/cpuset_release_agent with the name of the cpuset (path
  156. * relative to the root of cpuset file system) as the argument.
  157. *
  158. * A cpuset can only be deleted if both its 'count' of using tasks is
  159. * zero, and its list of 'children' cpusets is empty. Since all tasks
  160. * in the system use _some_ cpuset, and since there is always at least
  161. * one task in the system (init, pid == 1), therefore, top_cpuset
  162. * always has either children cpusets and/or using tasks. So no need
  163. * for any special hack to ensure that top_cpuset cannot be deleted.
  164. */
  165. static DECLARE_MUTEX(cpuset_sem);
  166. /*
  167. * A couple of forward declarations required, due to cyclic reference loop:
  168. * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
  169. * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
  170. */
  171. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  172. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
  173. static struct backing_dev_info cpuset_backing_dev_info = {
  174. .ra_pages = 0, /* No readahead */
  175. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  176. };
  177. static struct inode *cpuset_new_inode(mode_t mode)
  178. {
  179. struct inode *inode = new_inode(cpuset_sb);
  180. if (inode) {
  181. inode->i_mode = mode;
  182. inode->i_uid = current->fsuid;
  183. inode->i_gid = current->fsgid;
  184. inode->i_blksize = PAGE_CACHE_SIZE;
  185. inode->i_blocks = 0;
  186. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  187. inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
  188. }
  189. return inode;
  190. }
  191. static void cpuset_diput(struct dentry *dentry, struct inode *inode)
  192. {
  193. /* is dentry a directory ? if so, kfree() associated cpuset */
  194. if (S_ISDIR(inode->i_mode)) {
  195. struct cpuset *cs = dentry->d_fsdata;
  196. BUG_ON(!(is_removed(cs)));
  197. kfree(cs);
  198. }
  199. iput(inode);
  200. }
  201. static struct dentry_operations cpuset_dops = {
  202. .d_iput = cpuset_diput,
  203. };
  204. static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
  205. {
  206. struct qstr qstr;
  207. struct dentry *d;
  208. qstr.name = name;
  209. qstr.len = strlen(name);
  210. qstr.hash = full_name_hash(name, qstr.len);
  211. d = lookup_hash(&qstr, parent);
  212. if (!IS_ERR(d))
  213. d->d_op = &cpuset_dops;
  214. return d;
  215. }
  216. static void remove_dir(struct dentry *d)
  217. {
  218. struct dentry *parent = dget(d->d_parent);
  219. d_delete(d);
  220. simple_rmdir(parent->d_inode, d);
  221. dput(parent);
  222. }
  223. /*
  224. * NOTE : the dentry must have been dget()'ed
  225. */
  226. static void cpuset_d_remove_dir(struct dentry *dentry)
  227. {
  228. struct list_head *node;
  229. spin_lock(&dcache_lock);
  230. node = dentry->d_subdirs.next;
  231. while (node != &dentry->d_subdirs) {
  232. struct dentry *d = list_entry(node, struct dentry, d_child);
  233. list_del_init(node);
  234. if (d->d_inode) {
  235. d = dget_locked(d);
  236. spin_unlock(&dcache_lock);
  237. d_delete(d);
  238. simple_unlink(dentry->d_inode, d);
  239. dput(d);
  240. spin_lock(&dcache_lock);
  241. }
  242. node = dentry->d_subdirs.next;
  243. }
  244. list_del_init(&dentry->d_child);
  245. spin_unlock(&dcache_lock);
  246. remove_dir(dentry);
  247. }
  248. static struct super_operations cpuset_ops = {
  249. .statfs = simple_statfs,
  250. .drop_inode = generic_delete_inode,
  251. };
  252. static int cpuset_fill_super(struct super_block *sb, void *unused_data,
  253. int unused_silent)
  254. {
  255. struct inode *inode;
  256. struct dentry *root;
  257. sb->s_blocksize = PAGE_CACHE_SIZE;
  258. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  259. sb->s_magic = CPUSET_SUPER_MAGIC;
  260. sb->s_op = &cpuset_ops;
  261. cpuset_sb = sb;
  262. inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
  263. if (inode) {
  264. inode->i_op = &simple_dir_inode_operations;
  265. inode->i_fop = &simple_dir_operations;
  266. /* directories start off with i_nlink == 2 (for "." entry) */
  267. inode->i_nlink++;
  268. } else {
  269. return -ENOMEM;
  270. }
  271. root = d_alloc_root(inode);
  272. if (!root) {
  273. iput(inode);
  274. return -ENOMEM;
  275. }
  276. sb->s_root = root;
  277. return 0;
  278. }
  279. static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
  280. int flags, const char *unused_dev_name,
  281. void *data)
  282. {
  283. return get_sb_single(fs_type, flags, data, cpuset_fill_super);
  284. }
  285. static struct file_system_type cpuset_fs_type = {
  286. .name = "cpuset",
  287. .get_sb = cpuset_get_sb,
  288. .kill_sb = kill_litter_super,
  289. };
  290. /* struct cftype:
  291. *
  292. * The files in the cpuset filesystem mostly have a very simple read/write
  293. * handling, some common function will take care of it. Nevertheless some cases
  294. * (read tasks) are special and therefore I define this structure for every
  295. * kind of file.
  296. *
  297. *
  298. * When reading/writing to a file:
  299. * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
  300. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  301. */
  302. struct cftype {
  303. char *name;
  304. int private;
  305. int (*open) (struct inode *inode, struct file *file);
  306. ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
  307. loff_t *ppos);
  308. int (*write) (struct file *file, const char __user *buf, size_t nbytes,
  309. loff_t *ppos);
  310. int (*release) (struct inode *inode, struct file *file);
  311. };
  312. static inline struct cpuset *__d_cs(struct dentry *dentry)
  313. {
  314. return dentry->d_fsdata;
  315. }
  316. static inline struct cftype *__d_cft(struct dentry *dentry)
  317. {
  318. return dentry->d_fsdata;
  319. }
  320. /*
  321. * Call with cpuset_sem held. Writes path of cpuset into buf.
  322. * Returns 0 on success, -errno on error.
  323. */
  324. static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
  325. {
  326. char *start;
  327. start = buf + buflen;
  328. *--start = '\0';
  329. for (;;) {
  330. int len = cs->dentry->d_name.len;
  331. if ((start -= len) < buf)
  332. return -ENAMETOOLONG;
  333. memcpy(start, cs->dentry->d_name.name, len);
  334. cs = cs->parent;
  335. if (!cs)
  336. break;
  337. if (!cs->parent)
  338. continue;
  339. if (--start < buf)
  340. return -ENAMETOOLONG;
  341. *start = '/';
  342. }
  343. memmove(buf, start, buf + buflen - start);
  344. return 0;
  345. }
  346. /*
  347. * Notify userspace when a cpuset is released, by running
  348. * /sbin/cpuset_release_agent with the name of the cpuset (path
  349. * relative to the root of cpuset file system) as the argument.
  350. *
  351. * Most likely, this user command will try to rmdir this cpuset.
  352. *
  353. * This races with the possibility that some other task will be
  354. * attached to this cpuset before it is removed, or that some other
  355. * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
  356. * The presumed 'rmdir' will fail quietly if this cpuset is no longer
  357. * unused, and this cpuset will be reprieved from its death sentence,
  358. * to continue to serve a useful existence. Next time it's released,
  359. * we will get notified again, if it still has 'notify_on_release' set.
  360. *
  361. * Note final arg to call_usermodehelper() is 0 - that means
  362. * don't wait. Since we are holding the global cpuset_sem here,
  363. * and we are asking another thread (started from keventd) to rmdir a
  364. * cpuset, we can't wait - or we'd deadlock with the removing thread
  365. * on cpuset_sem.
  366. */
  367. static int cpuset_release_agent(char *cpuset_str)
  368. {
  369. char *argv[3], *envp[3];
  370. int i;
  371. i = 0;
  372. argv[i++] = "/sbin/cpuset_release_agent";
  373. argv[i++] = cpuset_str;
  374. argv[i] = NULL;
  375. i = 0;
  376. /* minimal command environment */
  377. envp[i++] = "HOME=/";
  378. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  379. envp[i] = NULL;
  380. return call_usermodehelper(argv[0], argv, envp, 0);
  381. }
  382. /*
  383. * Either cs->count of using tasks transitioned to zero, or the
  384. * cs->children list of child cpusets just became empty. If this
  385. * cs is notify_on_release() and now both the user count is zero and
  386. * the list of children is empty, send notice to user land.
  387. */
  388. static void check_for_release(struct cpuset *cs)
  389. {
  390. if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
  391. list_empty(&cs->children)) {
  392. char *buf;
  393. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  394. if (!buf)
  395. return;
  396. if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
  397. goto out;
  398. cpuset_release_agent(buf);
  399. out:
  400. kfree(buf);
  401. }
  402. }
  403. /*
  404. * Return in *pmask the portion of a cpusets's cpus_allowed that
  405. * are online. If none are online, walk up the cpuset hierarchy
  406. * until we find one that does have some online cpus. If we get
  407. * all the way to the top and still haven't found any online cpus,
  408. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  409. * task, return cpu_online_map.
  410. *
  411. * One way or another, we guarantee to return some non-empty subset
  412. * of cpu_online_map.
  413. *
  414. * Call with cpuset_sem held.
  415. */
  416. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  417. {
  418. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  419. cs = cs->parent;
  420. if (cs)
  421. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  422. else
  423. *pmask = cpu_online_map;
  424. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  425. }
  426. /*
  427. * Return in *pmask the portion of a cpusets's mems_allowed that
  428. * are online. If none are online, walk up the cpuset hierarchy
  429. * until we find one that does have some online mems. If we get
  430. * all the way to the top and still haven't found any online mems,
  431. * return node_online_map.
  432. *
  433. * One way or another, we guarantee to return some non-empty subset
  434. * of node_online_map.
  435. *
  436. * Call with cpuset_sem held.
  437. */
  438. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  439. {
  440. while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
  441. cs = cs->parent;
  442. if (cs)
  443. nodes_and(*pmask, cs->mems_allowed, node_online_map);
  444. else
  445. *pmask = node_online_map;
  446. BUG_ON(!nodes_intersects(*pmask, node_online_map));
  447. }
  448. /*
  449. * Refresh current tasks mems_allowed and mems_generation from
  450. * current tasks cpuset. Call with cpuset_sem held.
  451. *
  452. * Be sure to call refresh_mems() on any cpuset operation which
  453. * (1) holds cpuset_sem, and (2) might possibly alloc memory.
  454. * Call after obtaining cpuset_sem lock, before any possible
  455. * allocation. Otherwise one risks trying to allocate memory
  456. * while the task cpuset_mems_generation is not the same as
  457. * the mems_generation in its cpuset, which would deadlock on
  458. * cpuset_sem in cpuset_update_current_mems_allowed().
  459. *
  460. * Since we hold cpuset_sem, once refresh_mems() is called, the
  461. * test (current->cpuset_mems_generation != cs->mems_generation)
  462. * in cpuset_update_current_mems_allowed() will remain false,
  463. * until we drop cpuset_sem. Anyone else who would change our
  464. * cpusets mems_generation needs to lock cpuset_sem first.
  465. */
  466. static void refresh_mems(void)
  467. {
  468. struct cpuset *cs = current->cpuset;
  469. if (current->cpuset_mems_generation != cs->mems_generation) {
  470. guarantee_online_mems(cs, &current->mems_allowed);
  471. current->cpuset_mems_generation = cs->mems_generation;
  472. }
  473. }
  474. /*
  475. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  476. *
  477. * One cpuset is a subset of another if all its allowed CPUs and
  478. * Memory Nodes are a subset of the other, and its exclusive flags
  479. * are only set if the other's are set.
  480. */
  481. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  482. {
  483. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  484. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  485. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  486. is_mem_exclusive(p) <= is_mem_exclusive(q);
  487. }
  488. /*
  489. * validate_change() - Used to validate that any proposed cpuset change
  490. * follows the structural rules for cpusets.
  491. *
  492. * If we replaced the flag and mask values of the current cpuset
  493. * (cur) with those values in the trial cpuset (trial), would
  494. * our various subset and exclusive rules still be valid? Presumes
  495. * cpuset_sem held.
  496. *
  497. * 'cur' is the address of an actual, in-use cpuset. Operations
  498. * such as list traversal that depend on the actual address of the
  499. * cpuset in the list must use cur below, not trial.
  500. *
  501. * 'trial' is the address of bulk structure copy of cur, with
  502. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  503. * or flags changed to new, trial values.
  504. *
  505. * Return 0 if valid, -errno if not.
  506. */
  507. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  508. {
  509. struct cpuset *c, *par;
  510. /* Each of our child cpusets must be a subset of us */
  511. list_for_each_entry(c, &cur->children, sibling) {
  512. if (!is_cpuset_subset(c, trial))
  513. return -EBUSY;
  514. }
  515. /* Remaining checks don't apply to root cpuset */
  516. if ((par = cur->parent) == NULL)
  517. return 0;
  518. /* We must be a subset of our parent cpuset */
  519. if (!is_cpuset_subset(trial, par))
  520. return -EACCES;
  521. /* If either I or some sibling (!= me) is exclusive, we can't overlap */
  522. list_for_each_entry(c, &par->children, sibling) {
  523. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  524. c != cur &&
  525. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  526. return -EINVAL;
  527. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  528. c != cur &&
  529. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  530. return -EINVAL;
  531. }
  532. return 0;
  533. }
  534. static int update_cpumask(struct cpuset *cs, char *buf)
  535. {
  536. struct cpuset trialcs;
  537. int retval;
  538. trialcs = *cs;
  539. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  540. if (retval < 0)
  541. return retval;
  542. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  543. if (cpus_empty(trialcs.cpus_allowed))
  544. return -ENOSPC;
  545. retval = validate_change(cs, &trialcs);
  546. if (retval == 0)
  547. cs->cpus_allowed = trialcs.cpus_allowed;
  548. return retval;
  549. }
  550. static int update_nodemask(struct cpuset *cs, char *buf)
  551. {
  552. struct cpuset trialcs;
  553. int retval;
  554. trialcs = *cs;
  555. retval = nodelist_parse(buf, trialcs.mems_allowed);
  556. if (retval < 0)
  557. return retval;
  558. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  559. if (nodes_empty(trialcs.mems_allowed))
  560. return -ENOSPC;
  561. retval = validate_change(cs, &trialcs);
  562. if (retval == 0) {
  563. cs->mems_allowed = trialcs.mems_allowed;
  564. atomic_inc(&cpuset_mems_generation);
  565. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  566. }
  567. return retval;
  568. }
  569. /*
  570. * update_flag - read a 0 or a 1 in a file and update associated flag
  571. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  572. * CS_NOTIFY_ON_RELEASE)
  573. * cs: the cpuset to update
  574. * buf: the buffer where we read the 0 or 1
  575. */
  576. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  577. {
  578. int turning_on;
  579. struct cpuset trialcs;
  580. int err;
  581. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  582. trialcs = *cs;
  583. if (turning_on)
  584. set_bit(bit, &trialcs.flags);
  585. else
  586. clear_bit(bit, &trialcs.flags);
  587. err = validate_change(cs, &trialcs);
  588. if (err == 0) {
  589. if (turning_on)
  590. set_bit(bit, &cs->flags);
  591. else
  592. clear_bit(bit, &cs->flags);
  593. }
  594. return err;
  595. }
  596. static int attach_task(struct cpuset *cs, char *buf)
  597. {
  598. pid_t pid;
  599. struct task_struct *tsk;
  600. struct cpuset *oldcs;
  601. cpumask_t cpus;
  602. if (sscanf(buf, "%d", &pid) != 1)
  603. return -EIO;
  604. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  605. return -ENOSPC;
  606. if (pid) {
  607. read_lock(&tasklist_lock);
  608. tsk = find_task_by_pid(pid);
  609. if (!tsk) {
  610. read_unlock(&tasklist_lock);
  611. return -ESRCH;
  612. }
  613. get_task_struct(tsk);
  614. read_unlock(&tasklist_lock);
  615. if ((current->euid) && (current->euid != tsk->uid)
  616. && (current->euid != tsk->suid)) {
  617. put_task_struct(tsk);
  618. return -EACCES;
  619. }
  620. } else {
  621. tsk = current;
  622. get_task_struct(tsk);
  623. }
  624. task_lock(tsk);
  625. oldcs = tsk->cpuset;
  626. if (!oldcs) {
  627. task_unlock(tsk);
  628. put_task_struct(tsk);
  629. return -ESRCH;
  630. }
  631. atomic_inc(&cs->count);
  632. tsk->cpuset = cs;
  633. task_unlock(tsk);
  634. guarantee_online_cpus(cs, &cpus);
  635. set_cpus_allowed(tsk, cpus);
  636. put_task_struct(tsk);
  637. if (atomic_dec_and_test(&oldcs->count))
  638. check_for_release(oldcs);
  639. return 0;
  640. }
  641. /* The various types of files and directories in a cpuset file system */
  642. typedef enum {
  643. FILE_ROOT,
  644. FILE_DIR,
  645. FILE_CPULIST,
  646. FILE_MEMLIST,
  647. FILE_CPU_EXCLUSIVE,
  648. FILE_MEM_EXCLUSIVE,
  649. FILE_NOTIFY_ON_RELEASE,
  650. FILE_TASKLIST,
  651. } cpuset_filetype_t;
  652. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  653. size_t nbytes, loff_t *unused_ppos)
  654. {
  655. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  656. struct cftype *cft = __d_cft(file->f_dentry);
  657. cpuset_filetype_t type = cft->private;
  658. char *buffer;
  659. int retval = 0;
  660. /* Crude upper limit on largest legitimate cpulist user might write. */
  661. if (nbytes > 100 + 6 * NR_CPUS)
  662. return -E2BIG;
  663. /* +1 for nul-terminator */
  664. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  665. return -ENOMEM;
  666. if (copy_from_user(buffer, userbuf, nbytes)) {
  667. retval = -EFAULT;
  668. goto out1;
  669. }
  670. buffer[nbytes] = 0; /* nul-terminate */
  671. down(&cpuset_sem);
  672. if (is_removed(cs)) {
  673. retval = -ENODEV;
  674. goto out2;
  675. }
  676. switch (type) {
  677. case FILE_CPULIST:
  678. retval = update_cpumask(cs, buffer);
  679. break;
  680. case FILE_MEMLIST:
  681. retval = update_nodemask(cs, buffer);
  682. break;
  683. case FILE_CPU_EXCLUSIVE:
  684. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  685. break;
  686. case FILE_MEM_EXCLUSIVE:
  687. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  688. break;
  689. case FILE_NOTIFY_ON_RELEASE:
  690. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  691. break;
  692. case FILE_TASKLIST:
  693. retval = attach_task(cs, buffer);
  694. break;
  695. default:
  696. retval = -EINVAL;
  697. goto out2;
  698. }
  699. if (retval == 0)
  700. retval = nbytes;
  701. out2:
  702. up(&cpuset_sem);
  703. out1:
  704. kfree(buffer);
  705. return retval;
  706. }
  707. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  708. size_t nbytes, loff_t *ppos)
  709. {
  710. ssize_t retval = 0;
  711. struct cftype *cft = __d_cft(file->f_dentry);
  712. if (!cft)
  713. return -ENODEV;
  714. /* special function ? */
  715. if (cft->write)
  716. retval = cft->write(file, buf, nbytes, ppos);
  717. else
  718. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  719. return retval;
  720. }
  721. /*
  722. * These ascii lists should be read in a single call, by using a user
  723. * buffer large enough to hold the entire map. If read in smaller
  724. * chunks, there is no guarantee of atomicity. Since the display format
  725. * used, list of ranges of sequential numbers, is variable length,
  726. * and since these maps can change value dynamically, one could read
  727. * gibberish by doing partial reads while a list was changing.
  728. * A single large read to a buffer that crosses a page boundary is
  729. * ok, because the result being copied to user land is not recomputed
  730. * across a page fault.
  731. */
  732. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  733. {
  734. cpumask_t mask;
  735. down(&cpuset_sem);
  736. mask = cs->cpus_allowed;
  737. up(&cpuset_sem);
  738. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  739. }
  740. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  741. {
  742. nodemask_t mask;
  743. down(&cpuset_sem);
  744. mask = cs->mems_allowed;
  745. up(&cpuset_sem);
  746. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  747. }
  748. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  749. size_t nbytes, loff_t *ppos)
  750. {
  751. struct cftype *cft = __d_cft(file->f_dentry);
  752. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  753. cpuset_filetype_t type = cft->private;
  754. char *page;
  755. ssize_t retval = 0;
  756. char *s;
  757. char *start;
  758. size_t n;
  759. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  760. return -ENOMEM;
  761. s = page;
  762. switch (type) {
  763. case FILE_CPULIST:
  764. s += cpuset_sprintf_cpulist(s, cs);
  765. break;
  766. case FILE_MEMLIST:
  767. s += cpuset_sprintf_memlist(s, cs);
  768. break;
  769. case FILE_CPU_EXCLUSIVE:
  770. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  771. break;
  772. case FILE_MEM_EXCLUSIVE:
  773. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  774. break;
  775. case FILE_NOTIFY_ON_RELEASE:
  776. *s++ = notify_on_release(cs) ? '1' : '0';
  777. break;
  778. default:
  779. retval = -EINVAL;
  780. goto out;
  781. }
  782. *s++ = '\n';
  783. *s = '\0';
  784. start = page + *ppos;
  785. n = s - start;
  786. retval = n - copy_to_user(buf, start, min(n, nbytes));
  787. *ppos += retval;
  788. out:
  789. free_page((unsigned long)page);
  790. return retval;
  791. }
  792. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  793. loff_t *ppos)
  794. {
  795. ssize_t retval = 0;
  796. struct cftype *cft = __d_cft(file->f_dentry);
  797. if (!cft)
  798. return -ENODEV;
  799. /* special function ? */
  800. if (cft->read)
  801. retval = cft->read(file, buf, nbytes, ppos);
  802. else
  803. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  804. return retval;
  805. }
  806. static int cpuset_file_open(struct inode *inode, struct file *file)
  807. {
  808. int err;
  809. struct cftype *cft;
  810. err = generic_file_open(inode, file);
  811. if (err)
  812. return err;
  813. cft = __d_cft(file->f_dentry);
  814. if (!cft)
  815. return -ENODEV;
  816. if (cft->open)
  817. err = cft->open(inode, file);
  818. else
  819. err = 0;
  820. return err;
  821. }
  822. static int cpuset_file_release(struct inode *inode, struct file *file)
  823. {
  824. struct cftype *cft = __d_cft(file->f_dentry);
  825. if (cft->release)
  826. return cft->release(inode, file);
  827. return 0;
  828. }
  829. static struct file_operations cpuset_file_operations = {
  830. .read = cpuset_file_read,
  831. .write = cpuset_file_write,
  832. .llseek = generic_file_llseek,
  833. .open = cpuset_file_open,
  834. .release = cpuset_file_release,
  835. };
  836. static struct inode_operations cpuset_dir_inode_operations = {
  837. .lookup = simple_lookup,
  838. .mkdir = cpuset_mkdir,
  839. .rmdir = cpuset_rmdir,
  840. };
  841. static int cpuset_create_file(struct dentry *dentry, int mode)
  842. {
  843. struct inode *inode;
  844. if (!dentry)
  845. return -ENOENT;
  846. if (dentry->d_inode)
  847. return -EEXIST;
  848. inode = cpuset_new_inode(mode);
  849. if (!inode)
  850. return -ENOMEM;
  851. if (S_ISDIR(mode)) {
  852. inode->i_op = &cpuset_dir_inode_operations;
  853. inode->i_fop = &simple_dir_operations;
  854. /* start off with i_nlink == 2 (for "." entry) */
  855. inode->i_nlink++;
  856. } else if (S_ISREG(mode)) {
  857. inode->i_size = 0;
  858. inode->i_fop = &cpuset_file_operations;
  859. }
  860. d_instantiate(dentry, inode);
  861. dget(dentry); /* Extra count - pin the dentry in core */
  862. return 0;
  863. }
  864. /*
  865. * cpuset_create_dir - create a directory for an object.
  866. * cs: the cpuset we create the directory for.
  867. * It must have a valid ->parent field
  868. * And we are going to fill its ->dentry field.
  869. * name: The name to give to the cpuset directory. Will be copied.
  870. * mode: mode to set on new directory.
  871. */
  872. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  873. {
  874. struct dentry *dentry = NULL;
  875. struct dentry *parent;
  876. int error = 0;
  877. parent = cs->parent->dentry;
  878. dentry = cpuset_get_dentry(parent, name);
  879. if (IS_ERR(dentry))
  880. return PTR_ERR(dentry);
  881. error = cpuset_create_file(dentry, S_IFDIR | mode);
  882. if (!error) {
  883. dentry->d_fsdata = cs;
  884. parent->d_inode->i_nlink++;
  885. cs->dentry = dentry;
  886. }
  887. dput(dentry);
  888. return error;
  889. }
  890. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  891. {
  892. struct dentry *dentry;
  893. int error;
  894. down(&dir->d_inode->i_sem);
  895. dentry = cpuset_get_dentry(dir, cft->name);
  896. if (!IS_ERR(dentry)) {
  897. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  898. if (!error)
  899. dentry->d_fsdata = (void *)cft;
  900. dput(dentry);
  901. } else
  902. error = PTR_ERR(dentry);
  903. up(&dir->d_inode->i_sem);
  904. return error;
  905. }
  906. /*
  907. * Stuff for reading the 'tasks' file.
  908. *
  909. * Reading this file can return large amounts of data if a cpuset has
  910. * *lots* of attached tasks. So it may need several calls to read(),
  911. * but we cannot guarantee that the information we produce is correct
  912. * unless we produce it entirely atomically.
  913. *
  914. * Upon tasks file open(), a struct ctr_struct is allocated, that
  915. * will have a pointer to an array (also allocated here). The struct
  916. * ctr_struct * is stored in file->private_data. Its resources will
  917. * be freed by release() when the file is closed. The array is used
  918. * to sprintf the PIDs and then used by read().
  919. */
  920. /* cpusets_tasks_read array */
  921. struct ctr_struct {
  922. char *buf;
  923. int bufsz;
  924. };
  925. /*
  926. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  927. * Return actual number of pids loaded.
  928. */
  929. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  930. {
  931. int n = 0;
  932. struct task_struct *g, *p;
  933. read_lock(&tasklist_lock);
  934. do_each_thread(g, p) {
  935. if (p->cpuset == cs) {
  936. pidarray[n++] = p->pid;
  937. if (unlikely(n == npids))
  938. goto array_full;
  939. }
  940. } while_each_thread(g, p);
  941. array_full:
  942. read_unlock(&tasklist_lock);
  943. return n;
  944. }
  945. static int cmppid(const void *a, const void *b)
  946. {
  947. return *(pid_t *)a - *(pid_t *)b;
  948. }
  949. /*
  950. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  951. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  952. * count 'cnt' of how many chars would be written if buf were large enough.
  953. */
  954. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  955. {
  956. int cnt = 0;
  957. int i;
  958. for (i = 0; i < npids; i++)
  959. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  960. return cnt;
  961. }
  962. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  963. {
  964. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  965. struct ctr_struct *ctr;
  966. pid_t *pidarray;
  967. int npids;
  968. char c;
  969. if (!(file->f_mode & FMODE_READ))
  970. return 0;
  971. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  972. if (!ctr)
  973. goto err0;
  974. /*
  975. * If cpuset gets more users after we read count, we won't have
  976. * enough space - tough. This race is indistinguishable to the
  977. * caller from the case that the additional cpuset users didn't
  978. * show up until sometime later on.
  979. */
  980. npids = atomic_read(&cs->count);
  981. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  982. if (!pidarray)
  983. goto err1;
  984. npids = pid_array_load(pidarray, npids, cs);
  985. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  986. /* Call pid_array_to_buf() twice, first just to get bufsz */
  987. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  988. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  989. if (!ctr->buf)
  990. goto err2;
  991. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  992. kfree(pidarray);
  993. file->private_data = ctr;
  994. return 0;
  995. err2:
  996. kfree(pidarray);
  997. err1:
  998. kfree(ctr);
  999. err0:
  1000. return -ENOMEM;
  1001. }
  1002. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1003. size_t nbytes, loff_t *ppos)
  1004. {
  1005. struct ctr_struct *ctr = file->private_data;
  1006. if (*ppos + nbytes > ctr->bufsz)
  1007. nbytes = ctr->bufsz - *ppos;
  1008. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1009. return -EFAULT;
  1010. *ppos += nbytes;
  1011. return nbytes;
  1012. }
  1013. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1014. {
  1015. struct ctr_struct *ctr;
  1016. if (file->f_mode & FMODE_READ) {
  1017. ctr = file->private_data;
  1018. kfree(ctr->buf);
  1019. kfree(ctr);
  1020. }
  1021. return 0;
  1022. }
  1023. /*
  1024. * for the common functions, 'private' gives the type of file
  1025. */
  1026. static struct cftype cft_tasks = {
  1027. .name = "tasks",
  1028. .open = cpuset_tasks_open,
  1029. .read = cpuset_tasks_read,
  1030. .release = cpuset_tasks_release,
  1031. .private = FILE_TASKLIST,
  1032. };
  1033. static struct cftype cft_cpus = {
  1034. .name = "cpus",
  1035. .private = FILE_CPULIST,
  1036. };
  1037. static struct cftype cft_mems = {
  1038. .name = "mems",
  1039. .private = FILE_MEMLIST,
  1040. };
  1041. static struct cftype cft_cpu_exclusive = {
  1042. .name = "cpu_exclusive",
  1043. .private = FILE_CPU_EXCLUSIVE,
  1044. };
  1045. static struct cftype cft_mem_exclusive = {
  1046. .name = "mem_exclusive",
  1047. .private = FILE_MEM_EXCLUSIVE,
  1048. };
  1049. static struct cftype cft_notify_on_release = {
  1050. .name = "notify_on_release",
  1051. .private = FILE_NOTIFY_ON_RELEASE,
  1052. };
  1053. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1054. {
  1055. int err;
  1056. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1057. return err;
  1058. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1059. return err;
  1060. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1061. return err;
  1062. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1063. return err;
  1064. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1065. return err;
  1066. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1067. return err;
  1068. return 0;
  1069. }
  1070. /*
  1071. * cpuset_create - create a cpuset
  1072. * parent: cpuset that will be parent of the new cpuset.
  1073. * name: name of the new cpuset. Will be strcpy'ed.
  1074. * mode: mode to set on new inode
  1075. *
  1076. * Must be called with the semaphore on the parent inode held
  1077. */
  1078. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1079. {
  1080. struct cpuset *cs;
  1081. int err;
  1082. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1083. if (!cs)
  1084. return -ENOMEM;
  1085. down(&cpuset_sem);
  1086. refresh_mems();
  1087. cs->flags = 0;
  1088. if (notify_on_release(parent))
  1089. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1090. cs->cpus_allowed = CPU_MASK_NONE;
  1091. cs->mems_allowed = NODE_MASK_NONE;
  1092. atomic_set(&cs->count, 0);
  1093. INIT_LIST_HEAD(&cs->sibling);
  1094. INIT_LIST_HEAD(&cs->children);
  1095. atomic_inc(&cpuset_mems_generation);
  1096. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1097. cs->parent = parent;
  1098. list_add(&cs->sibling, &cs->parent->children);
  1099. err = cpuset_create_dir(cs, name, mode);
  1100. if (err < 0)
  1101. goto err;
  1102. /*
  1103. * Release cpuset_sem before cpuset_populate_dir() because it
  1104. * will down() this new directory's i_sem and if we race with
  1105. * another mkdir, we might deadlock.
  1106. */
  1107. up(&cpuset_sem);
  1108. err = cpuset_populate_dir(cs->dentry);
  1109. /* If err < 0, we have a half-filled directory - oh well ;) */
  1110. return 0;
  1111. err:
  1112. list_del(&cs->sibling);
  1113. up(&cpuset_sem);
  1114. kfree(cs);
  1115. return err;
  1116. }
  1117. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1118. {
  1119. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1120. /* the vfs holds inode->i_sem already */
  1121. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1122. }
  1123. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1124. {
  1125. struct cpuset *cs = dentry->d_fsdata;
  1126. struct dentry *d;
  1127. struct cpuset *parent;
  1128. /* the vfs holds both inode->i_sem already */
  1129. down(&cpuset_sem);
  1130. refresh_mems();
  1131. if (atomic_read(&cs->count) > 0) {
  1132. up(&cpuset_sem);
  1133. return -EBUSY;
  1134. }
  1135. if (!list_empty(&cs->children)) {
  1136. up(&cpuset_sem);
  1137. return -EBUSY;
  1138. }
  1139. spin_lock(&cs->dentry->d_lock);
  1140. parent = cs->parent;
  1141. set_bit(CS_REMOVED, &cs->flags);
  1142. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1143. if (list_empty(&parent->children))
  1144. check_for_release(parent);
  1145. d = dget(cs->dentry);
  1146. cs->dentry = NULL;
  1147. spin_unlock(&d->d_lock);
  1148. cpuset_d_remove_dir(d);
  1149. dput(d);
  1150. up(&cpuset_sem);
  1151. return 0;
  1152. }
  1153. /**
  1154. * cpuset_init - initialize cpusets at system boot
  1155. *
  1156. * Description: Initialize top_cpuset and the cpuset internal file system,
  1157. **/
  1158. int __init cpuset_init(void)
  1159. {
  1160. struct dentry *root;
  1161. int err;
  1162. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1163. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1164. atomic_inc(&cpuset_mems_generation);
  1165. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1166. init_task.cpuset = &top_cpuset;
  1167. err = register_filesystem(&cpuset_fs_type);
  1168. if (err < 0)
  1169. goto out;
  1170. cpuset_mount = kern_mount(&cpuset_fs_type);
  1171. if (IS_ERR(cpuset_mount)) {
  1172. printk(KERN_ERR "cpuset: could not mount!\n");
  1173. err = PTR_ERR(cpuset_mount);
  1174. cpuset_mount = NULL;
  1175. goto out;
  1176. }
  1177. root = cpuset_mount->mnt_sb->s_root;
  1178. root->d_fsdata = &top_cpuset;
  1179. root->d_inode->i_nlink++;
  1180. top_cpuset.dentry = root;
  1181. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1182. err = cpuset_populate_dir(root);
  1183. out:
  1184. return err;
  1185. }
  1186. /**
  1187. * cpuset_init_smp - initialize cpus_allowed
  1188. *
  1189. * Description: Finish top cpuset after cpu, node maps are initialized
  1190. **/
  1191. void __init cpuset_init_smp(void)
  1192. {
  1193. top_cpuset.cpus_allowed = cpu_online_map;
  1194. top_cpuset.mems_allowed = node_online_map;
  1195. }
  1196. /**
  1197. * cpuset_fork - attach newly forked task to its parents cpuset.
  1198. * @p: pointer to task_struct of forking parent process.
  1199. *
  1200. * Description: By default, on fork, a task inherits its
  1201. * parents cpuset. The pointer to the shared cpuset is
  1202. * automatically copied in fork.c by dup_task_struct().
  1203. * This cpuset_fork() routine need only increment the usage
  1204. * counter in that cpuset.
  1205. **/
  1206. void cpuset_fork(struct task_struct *tsk)
  1207. {
  1208. atomic_inc(&tsk->cpuset->count);
  1209. }
  1210. /**
  1211. * cpuset_exit - detach cpuset from exiting task
  1212. * @tsk: pointer to task_struct of exiting process
  1213. *
  1214. * Description: Detach cpuset from @tsk and release it.
  1215. *
  1216. **/
  1217. void cpuset_exit(struct task_struct *tsk)
  1218. {
  1219. struct cpuset *cs;
  1220. task_lock(tsk);
  1221. cs = tsk->cpuset;
  1222. tsk->cpuset = NULL;
  1223. task_unlock(tsk);
  1224. if (atomic_dec_and_test(&cs->count)) {
  1225. down(&cpuset_sem);
  1226. check_for_release(cs);
  1227. up(&cpuset_sem);
  1228. }
  1229. }
  1230. /**
  1231. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1232. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1233. *
  1234. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1235. * attached to the specified @tsk. Guaranteed to return some non-empty
  1236. * subset of cpu_online_map, even if this means going outside the
  1237. * tasks cpuset.
  1238. **/
  1239. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1240. {
  1241. cpumask_t mask;
  1242. down(&cpuset_sem);
  1243. task_lock((struct task_struct *)tsk);
  1244. guarantee_online_cpus(tsk->cpuset, &mask);
  1245. task_unlock((struct task_struct *)tsk);
  1246. up(&cpuset_sem);
  1247. return mask;
  1248. }
  1249. void cpuset_init_current_mems_allowed(void)
  1250. {
  1251. current->mems_allowed = NODE_MASK_ALL;
  1252. }
  1253. /*
  1254. * If the current tasks cpusets mems_allowed changed behind our backs,
  1255. * update current->mems_allowed and mems_generation to the new value.
  1256. * Do not call this routine if in_interrupt().
  1257. */
  1258. void cpuset_update_current_mems_allowed(void)
  1259. {
  1260. struct cpuset *cs = current->cpuset;
  1261. if (!cs)
  1262. return; /* task is exiting */
  1263. if (current->cpuset_mems_generation != cs->mems_generation) {
  1264. down(&cpuset_sem);
  1265. refresh_mems();
  1266. up(&cpuset_sem);
  1267. }
  1268. }
  1269. void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
  1270. {
  1271. bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
  1272. MAX_NUMNODES);
  1273. }
  1274. /*
  1275. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1276. */
  1277. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1278. {
  1279. int i;
  1280. for (i = 0; zl->zones[i]; i++) {
  1281. int nid = zl->zones[i]->zone_pgdat->node_id;
  1282. if (node_isset(nid, current->mems_allowed))
  1283. return 1;
  1284. }
  1285. return 0;
  1286. }
  1287. /*
  1288. * Is 'current' valid, and is zone z allowed in current->mems_allowed?
  1289. */
  1290. int cpuset_zone_allowed(struct zone *z)
  1291. {
  1292. return in_interrupt() ||
  1293. node_isset(z->zone_pgdat->node_id, current->mems_allowed);
  1294. }
  1295. /*
  1296. * proc_cpuset_show()
  1297. * - Print tasks cpuset path into seq_file.
  1298. * - Used for /proc/<pid>/cpuset.
  1299. */
  1300. static int proc_cpuset_show(struct seq_file *m, void *v)
  1301. {
  1302. struct cpuset *cs;
  1303. struct task_struct *tsk;
  1304. char *buf;
  1305. int retval = 0;
  1306. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1307. if (!buf)
  1308. return -ENOMEM;
  1309. tsk = m->private;
  1310. down(&cpuset_sem);
  1311. task_lock(tsk);
  1312. cs = tsk->cpuset;
  1313. task_unlock(tsk);
  1314. if (!cs) {
  1315. retval = -EINVAL;
  1316. goto out;
  1317. }
  1318. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1319. if (retval < 0)
  1320. goto out;
  1321. seq_puts(m, buf);
  1322. seq_putc(m, '\n');
  1323. out:
  1324. up(&cpuset_sem);
  1325. kfree(buf);
  1326. return retval;
  1327. }
  1328. static int cpuset_open(struct inode *inode, struct file *file)
  1329. {
  1330. struct task_struct *tsk = PROC_I(inode)->task;
  1331. return single_open(file, proc_cpuset_show, tsk);
  1332. }
  1333. struct file_operations proc_cpuset_operations = {
  1334. .open = cpuset_open,
  1335. .read = seq_read,
  1336. .llseek = seq_lseek,
  1337. .release = single_release,
  1338. };
  1339. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1340. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1341. {
  1342. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1343. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1344. buffer += sprintf(buffer, "\n");
  1345. buffer += sprintf(buffer, "Mems_allowed:\t");
  1346. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1347. buffer += sprintf(buffer, "\n");
  1348. return buffer;
  1349. }