cpuset.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. /*
  529. * For a given cpuset cur, partition the system as follows
  530. * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
  531. * exclusive child cpusets
  532. * b. All cpus in the current cpuset's cpus_allowed that are not part of any
  533. * exclusive child cpusets
  534. * Build these two partitions by calling partition_sched_domains
  535. *
  536. * Call with cpuset_sem held. May nest a call to the
  537. * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
  538. */
  539. static void update_cpu_domains(struct cpuset *cur)
  540. {
  541. struct cpuset *c, *par = cur->parent;
  542. cpumask_t pspan, cspan;
  543. if (par == NULL || cpus_empty(cur->cpus_allowed))
  544. return;
  545. /*
  546. * Get all cpus from parent's cpus_allowed not part of exclusive
  547. * children
  548. */
  549. pspan = par->cpus_allowed;
  550. list_for_each_entry(c, &par->children, sibling) {
  551. if (is_cpu_exclusive(c))
  552. cpus_andnot(pspan, pspan, c->cpus_allowed);
  553. }
  554. if (is_removed(cur) || !is_cpu_exclusive(cur)) {
  555. cpus_or(pspan, pspan, cur->cpus_allowed);
  556. if (cpus_equal(pspan, cur->cpus_allowed))
  557. return;
  558. cspan = CPU_MASK_NONE;
  559. } else {
  560. if (cpus_empty(pspan))
  561. return;
  562. cspan = cur->cpus_allowed;
  563. /*
  564. * Get all cpus from current cpuset's cpus_allowed not part
  565. * of exclusive children
  566. */
  567. list_for_each_entry(c, &cur->children, sibling) {
  568. if (is_cpu_exclusive(c))
  569. cpus_andnot(cspan, cspan, c->cpus_allowed);
  570. }
  571. }
  572. lock_cpu_hotplug();
  573. partition_sched_domains(&pspan, &cspan);
  574. unlock_cpu_hotplug();
  575. }
  576. static int update_cpumask(struct cpuset *cs, char *buf)
  577. {
  578. struct cpuset trialcs;
  579. int retval, cpus_unchanged;
  580. trialcs = *cs;
  581. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  582. if (retval < 0)
  583. return retval;
  584. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  585. if (cpus_empty(trialcs.cpus_allowed))
  586. return -ENOSPC;
  587. retval = validate_change(cs, &trialcs);
  588. if (retval < 0)
  589. return retval;
  590. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  591. cs->cpus_allowed = trialcs.cpus_allowed;
  592. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  593. update_cpu_domains(cs);
  594. return 0;
  595. }
  596. static int update_nodemask(struct cpuset *cs, char *buf)
  597. {
  598. struct cpuset trialcs;
  599. int retval;
  600. trialcs = *cs;
  601. retval = nodelist_parse(buf, trialcs.mems_allowed);
  602. if (retval < 0)
  603. return retval;
  604. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  605. if (nodes_empty(trialcs.mems_allowed))
  606. return -ENOSPC;
  607. retval = validate_change(cs, &trialcs);
  608. if (retval == 0) {
  609. cs->mems_allowed = trialcs.mems_allowed;
  610. atomic_inc(&cpuset_mems_generation);
  611. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  612. }
  613. return retval;
  614. }
  615. /*
  616. * update_flag - read a 0 or a 1 in a file and update associated flag
  617. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  618. * CS_NOTIFY_ON_RELEASE)
  619. * cs: the cpuset to update
  620. * buf: the buffer where we read the 0 or 1
  621. */
  622. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  623. {
  624. int turning_on;
  625. struct cpuset trialcs;
  626. int err, cpu_exclusive_changed;
  627. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  628. trialcs = *cs;
  629. if (turning_on)
  630. set_bit(bit, &trialcs.flags);
  631. else
  632. clear_bit(bit, &trialcs.flags);
  633. err = validate_change(cs, &trialcs);
  634. if (err < 0)
  635. return err;
  636. cpu_exclusive_changed =
  637. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  638. if (turning_on)
  639. set_bit(bit, &cs->flags);
  640. else
  641. clear_bit(bit, &cs->flags);
  642. if (cpu_exclusive_changed)
  643. update_cpu_domains(cs);
  644. return 0;
  645. }
  646. static int attach_task(struct cpuset *cs, char *buf)
  647. {
  648. pid_t pid;
  649. struct task_struct *tsk;
  650. struct cpuset *oldcs;
  651. cpumask_t cpus;
  652. if (sscanf(buf, "%d", &pid) != 1)
  653. return -EIO;
  654. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  655. return -ENOSPC;
  656. if (pid) {
  657. read_lock(&tasklist_lock);
  658. tsk = find_task_by_pid(pid);
  659. if (!tsk) {
  660. read_unlock(&tasklist_lock);
  661. return -ESRCH;
  662. }
  663. get_task_struct(tsk);
  664. read_unlock(&tasklist_lock);
  665. if ((current->euid) && (current->euid != tsk->uid)
  666. && (current->euid != tsk->suid)) {
  667. put_task_struct(tsk);
  668. return -EACCES;
  669. }
  670. } else {
  671. tsk = current;
  672. get_task_struct(tsk);
  673. }
  674. task_lock(tsk);
  675. oldcs = tsk->cpuset;
  676. if (!oldcs) {
  677. task_unlock(tsk);
  678. put_task_struct(tsk);
  679. return -ESRCH;
  680. }
  681. atomic_inc(&cs->count);
  682. tsk->cpuset = cs;
  683. task_unlock(tsk);
  684. guarantee_online_cpus(cs, &cpus);
  685. set_cpus_allowed(tsk, cpus);
  686. put_task_struct(tsk);
  687. if (atomic_dec_and_test(&oldcs->count))
  688. check_for_release(oldcs);
  689. return 0;
  690. }
  691. /* The various types of files and directories in a cpuset file system */
  692. typedef enum {
  693. FILE_ROOT,
  694. FILE_DIR,
  695. FILE_CPULIST,
  696. FILE_MEMLIST,
  697. FILE_CPU_EXCLUSIVE,
  698. FILE_MEM_EXCLUSIVE,
  699. FILE_NOTIFY_ON_RELEASE,
  700. FILE_TASKLIST,
  701. } cpuset_filetype_t;
  702. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  703. size_t nbytes, loff_t *unused_ppos)
  704. {
  705. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  706. struct cftype *cft = __d_cft(file->f_dentry);
  707. cpuset_filetype_t type = cft->private;
  708. char *buffer;
  709. int retval = 0;
  710. /* Crude upper limit on largest legitimate cpulist user might write. */
  711. if (nbytes > 100 + 6 * NR_CPUS)
  712. return -E2BIG;
  713. /* +1 for nul-terminator */
  714. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  715. return -ENOMEM;
  716. if (copy_from_user(buffer, userbuf, nbytes)) {
  717. retval = -EFAULT;
  718. goto out1;
  719. }
  720. buffer[nbytes] = 0; /* nul-terminate */
  721. down(&cpuset_sem);
  722. if (is_removed(cs)) {
  723. retval = -ENODEV;
  724. goto out2;
  725. }
  726. switch (type) {
  727. case FILE_CPULIST:
  728. retval = update_cpumask(cs, buffer);
  729. break;
  730. case FILE_MEMLIST:
  731. retval = update_nodemask(cs, buffer);
  732. break;
  733. case FILE_CPU_EXCLUSIVE:
  734. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  735. break;
  736. case FILE_MEM_EXCLUSIVE:
  737. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  738. break;
  739. case FILE_NOTIFY_ON_RELEASE:
  740. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  741. break;
  742. case FILE_TASKLIST:
  743. retval = attach_task(cs, buffer);
  744. break;
  745. default:
  746. retval = -EINVAL;
  747. goto out2;
  748. }
  749. if (retval == 0)
  750. retval = nbytes;
  751. out2:
  752. up(&cpuset_sem);
  753. out1:
  754. kfree(buffer);
  755. return retval;
  756. }
  757. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  758. size_t nbytes, loff_t *ppos)
  759. {
  760. ssize_t retval = 0;
  761. struct cftype *cft = __d_cft(file->f_dentry);
  762. if (!cft)
  763. return -ENODEV;
  764. /* special function ? */
  765. if (cft->write)
  766. retval = cft->write(file, buf, nbytes, ppos);
  767. else
  768. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  769. return retval;
  770. }
  771. /*
  772. * These ascii lists should be read in a single call, by using a user
  773. * buffer large enough to hold the entire map. If read in smaller
  774. * chunks, there is no guarantee of atomicity. Since the display format
  775. * used, list of ranges of sequential numbers, is variable length,
  776. * and since these maps can change value dynamically, one could read
  777. * gibberish by doing partial reads while a list was changing.
  778. * A single large read to a buffer that crosses a page boundary is
  779. * ok, because the result being copied to user land is not recomputed
  780. * across a page fault.
  781. */
  782. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  783. {
  784. cpumask_t mask;
  785. down(&cpuset_sem);
  786. mask = cs->cpus_allowed;
  787. up(&cpuset_sem);
  788. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  789. }
  790. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  791. {
  792. nodemask_t mask;
  793. down(&cpuset_sem);
  794. mask = cs->mems_allowed;
  795. up(&cpuset_sem);
  796. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  797. }
  798. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  799. size_t nbytes, loff_t *ppos)
  800. {
  801. struct cftype *cft = __d_cft(file->f_dentry);
  802. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  803. cpuset_filetype_t type = cft->private;
  804. char *page;
  805. ssize_t retval = 0;
  806. char *s;
  807. char *start;
  808. size_t n;
  809. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  810. return -ENOMEM;
  811. s = page;
  812. switch (type) {
  813. case FILE_CPULIST:
  814. s += cpuset_sprintf_cpulist(s, cs);
  815. break;
  816. case FILE_MEMLIST:
  817. s += cpuset_sprintf_memlist(s, cs);
  818. break;
  819. case FILE_CPU_EXCLUSIVE:
  820. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  821. break;
  822. case FILE_MEM_EXCLUSIVE:
  823. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  824. break;
  825. case FILE_NOTIFY_ON_RELEASE:
  826. *s++ = notify_on_release(cs) ? '1' : '0';
  827. break;
  828. default:
  829. retval = -EINVAL;
  830. goto out;
  831. }
  832. *s++ = '\n';
  833. *s = '\0';
  834. start = page + *ppos;
  835. n = s - start;
  836. retval = n - copy_to_user(buf, start, min(n, nbytes));
  837. *ppos += retval;
  838. out:
  839. free_page((unsigned long)page);
  840. return retval;
  841. }
  842. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  843. loff_t *ppos)
  844. {
  845. ssize_t retval = 0;
  846. struct cftype *cft = __d_cft(file->f_dentry);
  847. if (!cft)
  848. return -ENODEV;
  849. /* special function ? */
  850. if (cft->read)
  851. retval = cft->read(file, buf, nbytes, ppos);
  852. else
  853. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  854. return retval;
  855. }
  856. static int cpuset_file_open(struct inode *inode, struct file *file)
  857. {
  858. int err;
  859. struct cftype *cft;
  860. err = generic_file_open(inode, file);
  861. if (err)
  862. return err;
  863. cft = __d_cft(file->f_dentry);
  864. if (!cft)
  865. return -ENODEV;
  866. if (cft->open)
  867. err = cft->open(inode, file);
  868. else
  869. err = 0;
  870. return err;
  871. }
  872. static int cpuset_file_release(struct inode *inode, struct file *file)
  873. {
  874. struct cftype *cft = __d_cft(file->f_dentry);
  875. if (cft->release)
  876. return cft->release(inode, file);
  877. return 0;
  878. }
  879. static struct file_operations cpuset_file_operations = {
  880. .read = cpuset_file_read,
  881. .write = cpuset_file_write,
  882. .llseek = generic_file_llseek,
  883. .open = cpuset_file_open,
  884. .release = cpuset_file_release,
  885. };
  886. static struct inode_operations cpuset_dir_inode_operations = {
  887. .lookup = simple_lookup,
  888. .mkdir = cpuset_mkdir,
  889. .rmdir = cpuset_rmdir,
  890. };
  891. static int cpuset_create_file(struct dentry *dentry, int mode)
  892. {
  893. struct inode *inode;
  894. if (!dentry)
  895. return -ENOENT;
  896. if (dentry->d_inode)
  897. return -EEXIST;
  898. inode = cpuset_new_inode(mode);
  899. if (!inode)
  900. return -ENOMEM;
  901. if (S_ISDIR(mode)) {
  902. inode->i_op = &cpuset_dir_inode_operations;
  903. inode->i_fop = &simple_dir_operations;
  904. /* start off with i_nlink == 2 (for "." entry) */
  905. inode->i_nlink++;
  906. } else if (S_ISREG(mode)) {
  907. inode->i_size = 0;
  908. inode->i_fop = &cpuset_file_operations;
  909. }
  910. d_instantiate(dentry, inode);
  911. dget(dentry); /* Extra count - pin the dentry in core */
  912. return 0;
  913. }
  914. /*
  915. * cpuset_create_dir - create a directory for an object.
  916. * cs: the cpuset we create the directory for.
  917. * It must have a valid ->parent field
  918. * And we are going to fill its ->dentry field.
  919. * name: The name to give to the cpuset directory. Will be copied.
  920. * mode: mode to set on new directory.
  921. */
  922. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  923. {
  924. struct dentry *dentry = NULL;
  925. struct dentry *parent;
  926. int error = 0;
  927. parent = cs->parent->dentry;
  928. dentry = cpuset_get_dentry(parent, name);
  929. if (IS_ERR(dentry))
  930. return PTR_ERR(dentry);
  931. error = cpuset_create_file(dentry, S_IFDIR | mode);
  932. if (!error) {
  933. dentry->d_fsdata = cs;
  934. parent->d_inode->i_nlink++;
  935. cs->dentry = dentry;
  936. }
  937. dput(dentry);
  938. return error;
  939. }
  940. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  941. {
  942. struct dentry *dentry;
  943. int error;
  944. down(&dir->d_inode->i_sem);
  945. dentry = cpuset_get_dentry(dir, cft->name);
  946. if (!IS_ERR(dentry)) {
  947. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  948. if (!error)
  949. dentry->d_fsdata = (void *)cft;
  950. dput(dentry);
  951. } else
  952. error = PTR_ERR(dentry);
  953. up(&dir->d_inode->i_sem);
  954. return error;
  955. }
  956. /*
  957. * Stuff for reading the 'tasks' file.
  958. *
  959. * Reading this file can return large amounts of data if a cpuset has
  960. * *lots* of attached tasks. So it may need several calls to read(),
  961. * but we cannot guarantee that the information we produce is correct
  962. * unless we produce it entirely atomically.
  963. *
  964. * Upon tasks file open(), a struct ctr_struct is allocated, that
  965. * will have a pointer to an array (also allocated here). The struct
  966. * ctr_struct * is stored in file->private_data. Its resources will
  967. * be freed by release() when the file is closed. The array is used
  968. * to sprintf the PIDs and then used by read().
  969. */
  970. /* cpusets_tasks_read array */
  971. struct ctr_struct {
  972. char *buf;
  973. int bufsz;
  974. };
  975. /*
  976. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  977. * Return actual number of pids loaded.
  978. */
  979. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  980. {
  981. int n = 0;
  982. struct task_struct *g, *p;
  983. read_lock(&tasklist_lock);
  984. do_each_thread(g, p) {
  985. if (p->cpuset == cs) {
  986. pidarray[n++] = p->pid;
  987. if (unlikely(n == npids))
  988. goto array_full;
  989. }
  990. } while_each_thread(g, p);
  991. array_full:
  992. read_unlock(&tasklist_lock);
  993. return n;
  994. }
  995. static int cmppid(const void *a, const void *b)
  996. {
  997. return *(pid_t *)a - *(pid_t *)b;
  998. }
  999. /*
  1000. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1001. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1002. * count 'cnt' of how many chars would be written if buf were large enough.
  1003. */
  1004. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1005. {
  1006. int cnt = 0;
  1007. int i;
  1008. for (i = 0; i < npids; i++)
  1009. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1010. return cnt;
  1011. }
  1012. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1013. {
  1014. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1015. struct ctr_struct *ctr;
  1016. pid_t *pidarray;
  1017. int npids;
  1018. char c;
  1019. if (!(file->f_mode & FMODE_READ))
  1020. return 0;
  1021. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1022. if (!ctr)
  1023. goto err0;
  1024. /*
  1025. * If cpuset gets more users after we read count, we won't have
  1026. * enough space - tough. This race is indistinguishable to the
  1027. * caller from the case that the additional cpuset users didn't
  1028. * show up until sometime later on.
  1029. */
  1030. npids = atomic_read(&cs->count);
  1031. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1032. if (!pidarray)
  1033. goto err1;
  1034. npids = pid_array_load(pidarray, npids, cs);
  1035. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1036. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1037. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1038. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1039. if (!ctr->buf)
  1040. goto err2;
  1041. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1042. kfree(pidarray);
  1043. file->private_data = ctr;
  1044. return 0;
  1045. err2:
  1046. kfree(pidarray);
  1047. err1:
  1048. kfree(ctr);
  1049. err0:
  1050. return -ENOMEM;
  1051. }
  1052. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1053. size_t nbytes, loff_t *ppos)
  1054. {
  1055. struct ctr_struct *ctr = file->private_data;
  1056. if (*ppos + nbytes > ctr->bufsz)
  1057. nbytes = ctr->bufsz - *ppos;
  1058. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1059. return -EFAULT;
  1060. *ppos += nbytes;
  1061. return nbytes;
  1062. }
  1063. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1064. {
  1065. struct ctr_struct *ctr;
  1066. if (file->f_mode & FMODE_READ) {
  1067. ctr = file->private_data;
  1068. kfree(ctr->buf);
  1069. kfree(ctr);
  1070. }
  1071. return 0;
  1072. }
  1073. /*
  1074. * for the common functions, 'private' gives the type of file
  1075. */
  1076. static struct cftype cft_tasks = {
  1077. .name = "tasks",
  1078. .open = cpuset_tasks_open,
  1079. .read = cpuset_tasks_read,
  1080. .release = cpuset_tasks_release,
  1081. .private = FILE_TASKLIST,
  1082. };
  1083. static struct cftype cft_cpus = {
  1084. .name = "cpus",
  1085. .private = FILE_CPULIST,
  1086. };
  1087. static struct cftype cft_mems = {
  1088. .name = "mems",
  1089. .private = FILE_MEMLIST,
  1090. };
  1091. static struct cftype cft_cpu_exclusive = {
  1092. .name = "cpu_exclusive",
  1093. .private = FILE_CPU_EXCLUSIVE,
  1094. };
  1095. static struct cftype cft_mem_exclusive = {
  1096. .name = "mem_exclusive",
  1097. .private = FILE_MEM_EXCLUSIVE,
  1098. };
  1099. static struct cftype cft_notify_on_release = {
  1100. .name = "notify_on_release",
  1101. .private = FILE_NOTIFY_ON_RELEASE,
  1102. };
  1103. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1104. {
  1105. int err;
  1106. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1107. return err;
  1108. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1109. return err;
  1110. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1111. return err;
  1112. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1113. return err;
  1114. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1115. return err;
  1116. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1117. return err;
  1118. return 0;
  1119. }
  1120. /*
  1121. * cpuset_create - create a cpuset
  1122. * parent: cpuset that will be parent of the new cpuset.
  1123. * name: name of the new cpuset. Will be strcpy'ed.
  1124. * mode: mode to set on new inode
  1125. *
  1126. * Must be called with the semaphore on the parent inode held
  1127. */
  1128. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1129. {
  1130. struct cpuset *cs;
  1131. int err;
  1132. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1133. if (!cs)
  1134. return -ENOMEM;
  1135. down(&cpuset_sem);
  1136. refresh_mems();
  1137. cs->flags = 0;
  1138. if (notify_on_release(parent))
  1139. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1140. cs->cpus_allowed = CPU_MASK_NONE;
  1141. cs->mems_allowed = NODE_MASK_NONE;
  1142. atomic_set(&cs->count, 0);
  1143. INIT_LIST_HEAD(&cs->sibling);
  1144. INIT_LIST_HEAD(&cs->children);
  1145. atomic_inc(&cpuset_mems_generation);
  1146. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1147. cs->parent = parent;
  1148. list_add(&cs->sibling, &cs->parent->children);
  1149. err = cpuset_create_dir(cs, name, mode);
  1150. if (err < 0)
  1151. goto err;
  1152. /*
  1153. * Release cpuset_sem before cpuset_populate_dir() because it
  1154. * will down() this new directory's i_sem and if we race with
  1155. * another mkdir, we might deadlock.
  1156. */
  1157. up(&cpuset_sem);
  1158. err = cpuset_populate_dir(cs->dentry);
  1159. /* If err < 0, we have a half-filled directory - oh well ;) */
  1160. return 0;
  1161. err:
  1162. list_del(&cs->sibling);
  1163. up(&cpuset_sem);
  1164. kfree(cs);
  1165. return err;
  1166. }
  1167. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1168. {
  1169. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1170. /* the vfs holds inode->i_sem already */
  1171. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1172. }
  1173. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1174. {
  1175. struct cpuset *cs = dentry->d_fsdata;
  1176. struct dentry *d;
  1177. struct cpuset *parent;
  1178. /* the vfs holds both inode->i_sem already */
  1179. down(&cpuset_sem);
  1180. refresh_mems();
  1181. if (atomic_read(&cs->count) > 0) {
  1182. up(&cpuset_sem);
  1183. return -EBUSY;
  1184. }
  1185. if (!list_empty(&cs->children)) {
  1186. up(&cpuset_sem);
  1187. return -EBUSY;
  1188. }
  1189. parent = cs->parent;
  1190. set_bit(CS_REMOVED, &cs->flags);
  1191. if (is_cpu_exclusive(cs))
  1192. update_cpu_domains(cs);
  1193. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1194. if (list_empty(&parent->children))
  1195. check_for_release(parent);
  1196. spin_lock(&cs->dentry->d_lock);
  1197. d = dget(cs->dentry);
  1198. cs->dentry = NULL;
  1199. spin_unlock(&d->d_lock);
  1200. cpuset_d_remove_dir(d);
  1201. dput(d);
  1202. up(&cpuset_sem);
  1203. return 0;
  1204. }
  1205. /**
  1206. * cpuset_init - initialize cpusets at system boot
  1207. *
  1208. * Description: Initialize top_cpuset and the cpuset internal file system,
  1209. **/
  1210. int __init cpuset_init(void)
  1211. {
  1212. struct dentry *root;
  1213. int err;
  1214. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1215. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1216. atomic_inc(&cpuset_mems_generation);
  1217. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1218. init_task.cpuset = &top_cpuset;
  1219. err = register_filesystem(&cpuset_fs_type);
  1220. if (err < 0)
  1221. goto out;
  1222. cpuset_mount = kern_mount(&cpuset_fs_type);
  1223. if (IS_ERR(cpuset_mount)) {
  1224. printk(KERN_ERR "cpuset: could not mount!\n");
  1225. err = PTR_ERR(cpuset_mount);
  1226. cpuset_mount = NULL;
  1227. goto out;
  1228. }
  1229. root = cpuset_mount->mnt_sb->s_root;
  1230. root->d_fsdata = &top_cpuset;
  1231. root->d_inode->i_nlink++;
  1232. top_cpuset.dentry = root;
  1233. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1234. err = cpuset_populate_dir(root);
  1235. out:
  1236. return err;
  1237. }
  1238. /**
  1239. * cpuset_init_smp - initialize cpus_allowed
  1240. *
  1241. * Description: Finish top cpuset after cpu, node maps are initialized
  1242. **/
  1243. void __init cpuset_init_smp(void)
  1244. {
  1245. top_cpuset.cpus_allowed = cpu_online_map;
  1246. top_cpuset.mems_allowed = node_online_map;
  1247. }
  1248. /**
  1249. * cpuset_fork - attach newly forked task to its parents cpuset.
  1250. * @p: pointer to task_struct of forking parent process.
  1251. *
  1252. * Description: By default, on fork, a task inherits its
  1253. * parents cpuset. The pointer to the shared cpuset is
  1254. * automatically copied in fork.c by dup_task_struct().
  1255. * This cpuset_fork() routine need only increment the usage
  1256. * counter in that cpuset.
  1257. **/
  1258. void cpuset_fork(struct task_struct *tsk)
  1259. {
  1260. atomic_inc(&tsk->cpuset->count);
  1261. }
  1262. /**
  1263. * cpuset_exit - detach cpuset from exiting task
  1264. * @tsk: pointer to task_struct of exiting process
  1265. *
  1266. * Description: Detach cpuset from @tsk and release it.
  1267. *
  1268. * Note that cpusets marked notify_on_release force every task
  1269. * in them to take the global cpuset_sem semaphore when exiting.
  1270. * This could impact scaling on very large systems. Be reluctant
  1271. * to use notify_on_release cpusets where very high task exit
  1272. * scaling is required on large systems.
  1273. *
  1274. * Don't even think about derefencing 'cs' after the cpuset use
  1275. * count goes to zero, except inside a critical section guarded
  1276. * by the cpuset_sem semaphore. If you don't hold cpuset_sem,
  1277. * then a zero cpuset use count is a license to any other task to
  1278. * nuke the cpuset immediately.
  1279. *
  1280. **/
  1281. void cpuset_exit(struct task_struct *tsk)
  1282. {
  1283. struct cpuset *cs;
  1284. task_lock(tsk);
  1285. cs = tsk->cpuset;
  1286. tsk->cpuset = NULL;
  1287. task_unlock(tsk);
  1288. if (notify_on_release(cs)) {
  1289. down(&cpuset_sem);
  1290. if (atomic_dec_and_test(&cs->count))
  1291. check_for_release(cs);
  1292. up(&cpuset_sem);
  1293. } else {
  1294. atomic_dec(&cs->count);
  1295. }
  1296. }
  1297. /**
  1298. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1299. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1300. *
  1301. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1302. * attached to the specified @tsk. Guaranteed to return some non-empty
  1303. * subset of cpu_online_map, even if this means going outside the
  1304. * tasks cpuset.
  1305. **/
  1306. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1307. {
  1308. cpumask_t mask;
  1309. down(&cpuset_sem);
  1310. task_lock((struct task_struct *)tsk);
  1311. guarantee_online_cpus(tsk->cpuset, &mask);
  1312. task_unlock((struct task_struct *)tsk);
  1313. up(&cpuset_sem);
  1314. return mask;
  1315. }
  1316. void cpuset_init_current_mems_allowed(void)
  1317. {
  1318. current->mems_allowed = NODE_MASK_ALL;
  1319. }
  1320. /*
  1321. * If the current tasks cpusets mems_allowed changed behind our backs,
  1322. * update current->mems_allowed and mems_generation to the new value.
  1323. * Do not call this routine if in_interrupt().
  1324. */
  1325. void cpuset_update_current_mems_allowed(void)
  1326. {
  1327. struct cpuset *cs = current->cpuset;
  1328. if (!cs)
  1329. return; /* task is exiting */
  1330. if (current->cpuset_mems_generation != cs->mems_generation) {
  1331. down(&cpuset_sem);
  1332. refresh_mems();
  1333. up(&cpuset_sem);
  1334. }
  1335. }
  1336. void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
  1337. {
  1338. bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
  1339. MAX_NUMNODES);
  1340. }
  1341. /*
  1342. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1343. */
  1344. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1345. {
  1346. int i;
  1347. for (i = 0; zl->zones[i]; i++) {
  1348. int nid = zl->zones[i]->zone_pgdat->node_id;
  1349. if (node_isset(nid, current->mems_allowed))
  1350. return 1;
  1351. }
  1352. return 0;
  1353. }
  1354. /*
  1355. * Is 'current' valid, and is zone z allowed in current->mems_allowed?
  1356. */
  1357. int cpuset_zone_allowed(struct zone *z)
  1358. {
  1359. return in_interrupt() ||
  1360. node_isset(z->zone_pgdat->node_id, current->mems_allowed);
  1361. }
  1362. /*
  1363. * proc_cpuset_show()
  1364. * - Print tasks cpuset path into seq_file.
  1365. * - Used for /proc/<pid>/cpuset.
  1366. */
  1367. static int proc_cpuset_show(struct seq_file *m, void *v)
  1368. {
  1369. struct cpuset *cs;
  1370. struct task_struct *tsk;
  1371. char *buf;
  1372. int retval = 0;
  1373. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1374. if (!buf)
  1375. return -ENOMEM;
  1376. tsk = m->private;
  1377. down(&cpuset_sem);
  1378. task_lock(tsk);
  1379. cs = tsk->cpuset;
  1380. task_unlock(tsk);
  1381. if (!cs) {
  1382. retval = -EINVAL;
  1383. goto out;
  1384. }
  1385. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1386. if (retval < 0)
  1387. goto out;
  1388. seq_puts(m, buf);
  1389. seq_putc(m, '\n');
  1390. out:
  1391. up(&cpuset_sem);
  1392. kfree(buf);
  1393. return retval;
  1394. }
  1395. static int cpuset_open(struct inode *inode, struct file *file)
  1396. {
  1397. struct task_struct *tsk = PROC_I(inode)->task;
  1398. return single_open(file, proc_cpuset_show, tsk);
  1399. }
  1400. struct file_operations proc_cpuset_operations = {
  1401. .open = cpuset_open,
  1402. .read = seq_read,
  1403. .llseek = seq_lseek,
  1404. .release = single_release,
  1405. };
  1406. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1407. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1408. {
  1409. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1410. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1411. buffer += sprintf(buffer, "\n");
  1412. buffer += sprintf(buffer, "Mems_allowed:\t");
  1413. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1414. buffer += sprintf(buffer, "\n");
  1415. return buffer;
  1416. }