cpuset.c 47 KB

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