cpuset.c 44 KB

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