cpuset.c 48 KB

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