cpuset.c 40 KB

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