cpuset.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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. /* Do nothing if *ppos is at the eof or beyond the eof. */
  857. if (s - page <= *ppos)
  858. return 0;
  859. start = page + *ppos;
  860. n = s - start;
  861. retval = n - copy_to_user(buf, start, min(n, nbytes));
  862. *ppos += retval;
  863. out:
  864. free_page((unsigned long)page);
  865. return retval;
  866. }
  867. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  868. loff_t *ppos)
  869. {
  870. ssize_t retval = 0;
  871. struct cftype *cft = __d_cft(file->f_dentry);
  872. if (!cft)
  873. return -ENODEV;
  874. /* special function ? */
  875. if (cft->read)
  876. retval = cft->read(file, buf, nbytes, ppos);
  877. else
  878. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  879. return retval;
  880. }
  881. static int cpuset_file_open(struct inode *inode, struct file *file)
  882. {
  883. int err;
  884. struct cftype *cft;
  885. err = generic_file_open(inode, file);
  886. if (err)
  887. return err;
  888. cft = __d_cft(file->f_dentry);
  889. if (!cft)
  890. return -ENODEV;
  891. if (cft->open)
  892. err = cft->open(inode, file);
  893. else
  894. err = 0;
  895. return err;
  896. }
  897. static int cpuset_file_release(struct inode *inode, struct file *file)
  898. {
  899. struct cftype *cft = __d_cft(file->f_dentry);
  900. if (cft->release)
  901. return cft->release(inode, file);
  902. return 0;
  903. }
  904. static struct file_operations cpuset_file_operations = {
  905. .read = cpuset_file_read,
  906. .write = cpuset_file_write,
  907. .llseek = generic_file_llseek,
  908. .open = cpuset_file_open,
  909. .release = cpuset_file_release,
  910. };
  911. static struct inode_operations cpuset_dir_inode_operations = {
  912. .lookup = simple_lookup,
  913. .mkdir = cpuset_mkdir,
  914. .rmdir = cpuset_rmdir,
  915. };
  916. static int cpuset_create_file(struct dentry *dentry, int mode)
  917. {
  918. struct inode *inode;
  919. if (!dentry)
  920. return -ENOENT;
  921. if (dentry->d_inode)
  922. return -EEXIST;
  923. inode = cpuset_new_inode(mode);
  924. if (!inode)
  925. return -ENOMEM;
  926. if (S_ISDIR(mode)) {
  927. inode->i_op = &cpuset_dir_inode_operations;
  928. inode->i_fop = &simple_dir_operations;
  929. /* start off with i_nlink == 2 (for "." entry) */
  930. inode->i_nlink++;
  931. } else if (S_ISREG(mode)) {
  932. inode->i_size = 0;
  933. inode->i_fop = &cpuset_file_operations;
  934. }
  935. d_instantiate(dentry, inode);
  936. dget(dentry); /* Extra count - pin the dentry in core */
  937. return 0;
  938. }
  939. /*
  940. * cpuset_create_dir - create a directory for an object.
  941. * cs: the cpuset we create the directory for.
  942. * It must have a valid ->parent field
  943. * And we are going to fill its ->dentry field.
  944. * name: The name to give to the cpuset directory. Will be copied.
  945. * mode: mode to set on new directory.
  946. */
  947. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  948. {
  949. struct dentry *dentry = NULL;
  950. struct dentry *parent;
  951. int error = 0;
  952. parent = cs->parent->dentry;
  953. dentry = cpuset_get_dentry(parent, name);
  954. if (IS_ERR(dentry))
  955. return PTR_ERR(dentry);
  956. error = cpuset_create_file(dentry, S_IFDIR | mode);
  957. if (!error) {
  958. dentry->d_fsdata = cs;
  959. parent->d_inode->i_nlink++;
  960. cs->dentry = dentry;
  961. }
  962. dput(dentry);
  963. return error;
  964. }
  965. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  966. {
  967. struct dentry *dentry;
  968. int error;
  969. down(&dir->d_inode->i_sem);
  970. dentry = cpuset_get_dentry(dir, cft->name);
  971. if (!IS_ERR(dentry)) {
  972. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  973. if (!error)
  974. dentry->d_fsdata = (void *)cft;
  975. dput(dentry);
  976. } else
  977. error = PTR_ERR(dentry);
  978. up(&dir->d_inode->i_sem);
  979. return error;
  980. }
  981. /*
  982. * Stuff for reading the 'tasks' file.
  983. *
  984. * Reading this file can return large amounts of data if a cpuset has
  985. * *lots* of attached tasks. So it may need several calls to read(),
  986. * but we cannot guarantee that the information we produce is correct
  987. * unless we produce it entirely atomically.
  988. *
  989. * Upon tasks file open(), a struct ctr_struct is allocated, that
  990. * will have a pointer to an array (also allocated here). The struct
  991. * ctr_struct * is stored in file->private_data. Its resources will
  992. * be freed by release() when the file is closed. The array is used
  993. * to sprintf the PIDs and then used by read().
  994. */
  995. /* cpusets_tasks_read array */
  996. struct ctr_struct {
  997. char *buf;
  998. int bufsz;
  999. };
  1000. /*
  1001. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1002. * Return actual number of pids loaded.
  1003. */
  1004. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1005. {
  1006. int n = 0;
  1007. struct task_struct *g, *p;
  1008. read_lock(&tasklist_lock);
  1009. do_each_thread(g, p) {
  1010. if (p->cpuset == cs) {
  1011. pidarray[n++] = p->pid;
  1012. if (unlikely(n == npids))
  1013. goto array_full;
  1014. }
  1015. } while_each_thread(g, p);
  1016. array_full:
  1017. read_unlock(&tasklist_lock);
  1018. return n;
  1019. }
  1020. static int cmppid(const void *a, const void *b)
  1021. {
  1022. return *(pid_t *)a - *(pid_t *)b;
  1023. }
  1024. /*
  1025. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1026. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1027. * count 'cnt' of how many chars would be written if buf were large enough.
  1028. */
  1029. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1030. {
  1031. int cnt = 0;
  1032. int i;
  1033. for (i = 0; i < npids; i++)
  1034. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1035. return cnt;
  1036. }
  1037. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1038. {
  1039. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1040. struct ctr_struct *ctr;
  1041. pid_t *pidarray;
  1042. int npids;
  1043. char c;
  1044. if (!(file->f_mode & FMODE_READ))
  1045. return 0;
  1046. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1047. if (!ctr)
  1048. goto err0;
  1049. /*
  1050. * If cpuset gets more users after we read count, we won't have
  1051. * enough space - tough. This race is indistinguishable to the
  1052. * caller from the case that the additional cpuset users didn't
  1053. * show up until sometime later on.
  1054. */
  1055. npids = atomic_read(&cs->count);
  1056. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1057. if (!pidarray)
  1058. goto err1;
  1059. npids = pid_array_load(pidarray, npids, cs);
  1060. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1061. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1062. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1063. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1064. if (!ctr->buf)
  1065. goto err2;
  1066. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1067. kfree(pidarray);
  1068. file->private_data = ctr;
  1069. return 0;
  1070. err2:
  1071. kfree(pidarray);
  1072. err1:
  1073. kfree(ctr);
  1074. err0:
  1075. return -ENOMEM;
  1076. }
  1077. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1078. size_t nbytes, loff_t *ppos)
  1079. {
  1080. struct ctr_struct *ctr = file->private_data;
  1081. if (*ppos + nbytes > ctr->bufsz)
  1082. nbytes = ctr->bufsz - *ppos;
  1083. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1084. return -EFAULT;
  1085. *ppos += nbytes;
  1086. return nbytes;
  1087. }
  1088. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1089. {
  1090. struct ctr_struct *ctr;
  1091. if (file->f_mode & FMODE_READ) {
  1092. ctr = file->private_data;
  1093. kfree(ctr->buf);
  1094. kfree(ctr);
  1095. }
  1096. return 0;
  1097. }
  1098. /*
  1099. * for the common functions, 'private' gives the type of file
  1100. */
  1101. static struct cftype cft_tasks = {
  1102. .name = "tasks",
  1103. .open = cpuset_tasks_open,
  1104. .read = cpuset_tasks_read,
  1105. .release = cpuset_tasks_release,
  1106. .private = FILE_TASKLIST,
  1107. };
  1108. static struct cftype cft_cpus = {
  1109. .name = "cpus",
  1110. .private = FILE_CPULIST,
  1111. };
  1112. static struct cftype cft_mems = {
  1113. .name = "mems",
  1114. .private = FILE_MEMLIST,
  1115. };
  1116. static struct cftype cft_cpu_exclusive = {
  1117. .name = "cpu_exclusive",
  1118. .private = FILE_CPU_EXCLUSIVE,
  1119. };
  1120. static struct cftype cft_mem_exclusive = {
  1121. .name = "mem_exclusive",
  1122. .private = FILE_MEM_EXCLUSIVE,
  1123. };
  1124. static struct cftype cft_notify_on_release = {
  1125. .name = "notify_on_release",
  1126. .private = FILE_NOTIFY_ON_RELEASE,
  1127. };
  1128. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1129. {
  1130. int err;
  1131. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1132. return err;
  1133. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1134. return err;
  1135. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1136. return err;
  1137. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1138. return err;
  1139. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1140. return err;
  1141. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1142. return err;
  1143. return 0;
  1144. }
  1145. /*
  1146. * cpuset_create - create a cpuset
  1147. * parent: cpuset that will be parent of the new cpuset.
  1148. * name: name of the new cpuset. Will be strcpy'ed.
  1149. * mode: mode to set on new inode
  1150. *
  1151. * Must be called with the semaphore on the parent inode held
  1152. */
  1153. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1154. {
  1155. struct cpuset *cs;
  1156. int err;
  1157. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1158. if (!cs)
  1159. return -ENOMEM;
  1160. down(&cpuset_sem);
  1161. refresh_mems();
  1162. cs->flags = 0;
  1163. if (notify_on_release(parent))
  1164. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1165. cs->cpus_allowed = CPU_MASK_NONE;
  1166. cs->mems_allowed = NODE_MASK_NONE;
  1167. atomic_set(&cs->count, 0);
  1168. INIT_LIST_HEAD(&cs->sibling);
  1169. INIT_LIST_HEAD(&cs->children);
  1170. atomic_inc(&cpuset_mems_generation);
  1171. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1172. cs->parent = parent;
  1173. list_add(&cs->sibling, &cs->parent->children);
  1174. err = cpuset_create_dir(cs, name, mode);
  1175. if (err < 0)
  1176. goto err;
  1177. /*
  1178. * Release cpuset_sem before cpuset_populate_dir() because it
  1179. * will down() this new directory's i_sem and if we race with
  1180. * another mkdir, we might deadlock.
  1181. */
  1182. up(&cpuset_sem);
  1183. err = cpuset_populate_dir(cs->dentry);
  1184. /* If err < 0, we have a half-filled directory - oh well ;) */
  1185. return 0;
  1186. err:
  1187. list_del(&cs->sibling);
  1188. up(&cpuset_sem);
  1189. kfree(cs);
  1190. return err;
  1191. }
  1192. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1193. {
  1194. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1195. /* the vfs holds inode->i_sem already */
  1196. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1197. }
  1198. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1199. {
  1200. struct cpuset *cs = dentry->d_fsdata;
  1201. struct dentry *d;
  1202. struct cpuset *parent;
  1203. char *pathbuf = NULL;
  1204. /* the vfs holds both inode->i_sem already */
  1205. down(&cpuset_sem);
  1206. refresh_mems();
  1207. if (atomic_read(&cs->count) > 0) {
  1208. up(&cpuset_sem);
  1209. return -EBUSY;
  1210. }
  1211. if (!list_empty(&cs->children)) {
  1212. up(&cpuset_sem);
  1213. return -EBUSY;
  1214. }
  1215. parent = cs->parent;
  1216. set_bit(CS_REMOVED, &cs->flags);
  1217. if (is_cpu_exclusive(cs))
  1218. update_cpu_domains(cs);
  1219. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1220. if (list_empty(&parent->children))
  1221. check_for_release(parent, &pathbuf);
  1222. spin_lock(&cs->dentry->d_lock);
  1223. d = dget(cs->dentry);
  1224. cs->dentry = NULL;
  1225. spin_unlock(&d->d_lock);
  1226. cpuset_d_remove_dir(d);
  1227. dput(d);
  1228. up(&cpuset_sem);
  1229. cpuset_release_agent(pathbuf);
  1230. return 0;
  1231. }
  1232. /**
  1233. * cpuset_init - initialize cpusets at system boot
  1234. *
  1235. * Description: Initialize top_cpuset and the cpuset internal file system,
  1236. **/
  1237. int __init cpuset_init(void)
  1238. {
  1239. struct dentry *root;
  1240. int err;
  1241. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1242. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1243. atomic_inc(&cpuset_mems_generation);
  1244. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1245. init_task.cpuset = &top_cpuset;
  1246. err = register_filesystem(&cpuset_fs_type);
  1247. if (err < 0)
  1248. goto out;
  1249. cpuset_mount = kern_mount(&cpuset_fs_type);
  1250. if (IS_ERR(cpuset_mount)) {
  1251. printk(KERN_ERR "cpuset: could not mount!\n");
  1252. err = PTR_ERR(cpuset_mount);
  1253. cpuset_mount = NULL;
  1254. goto out;
  1255. }
  1256. root = cpuset_mount->mnt_sb->s_root;
  1257. root->d_fsdata = &top_cpuset;
  1258. root->d_inode->i_nlink++;
  1259. top_cpuset.dentry = root;
  1260. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1261. err = cpuset_populate_dir(root);
  1262. out:
  1263. return err;
  1264. }
  1265. /**
  1266. * cpuset_init_smp - initialize cpus_allowed
  1267. *
  1268. * Description: Finish top cpuset after cpu, node maps are initialized
  1269. **/
  1270. void __init cpuset_init_smp(void)
  1271. {
  1272. top_cpuset.cpus_allowed = cpu_online_map;
  1273. top_cpuset.mems_allowed = node_online_map;
  1274. }
  1275. /**
  1276. * cpuset_fork - attach newly forked task to its parents cpuset.
  1277. * @tsk: pointer to task_struct of forking parent process.
  1278. *
  1279. * Description: By default, on fork, a task inherits its
  1280. * parent's cpuset. The pointer to the shared cpuset is
  1281. * automatically copied in fork.c by dup_task_struct().
  1282. * This cpuset_fork() routine need only increment the usage
  1283. * counter in that cpuset.
  1284. **/
  1285. void cpuset_fork(struct task_struct *tsk)
  1286. {
  1287. atomic_inc(&tsk->cpuset->count);
  1288. }
  1289. /**
  1290. * cpuset_exit - detach cpuset from exiting task
  1291. * @tsk: pointer to task_struct of exiting process
  1292. *
  1293. * Description: Detach cpuset from @tsk and release it.
  1294. *
  1295. * Note that cpusets marked notify_on_release force every task
  1296. * in them to take the global cpuset_sem semaphore when exiting.
  1297. * This could impact scaling on very large systems. Be reluctant
  1298. * to use notify_on_release cpusets where very high task exit
  1299. * scaling is required on large systems.
  1300. *
  1301. * Don't even think about derefencing 'cs' after the cpuset use
  1302. * count goes to zero, except inside a critical section guarded
  1303. * by the cpuset_sem semaphore. If you don't hold cpuset_sem,
  1304. * then a zero cpuset use count is a license to any other task to
  1305. * nuke the cpuset immediately.
  1306. **/
  1307. void cpuset_exit(struct task_struct *tsk)
  1308. {
  1309. struct cpuset *cs;
  1310. task_lock(tsk);
  1311. cs = tsk->cpuset;
  1312. tsk->cpuset = NULL;
  1313. task_unlock(tsk);
  1314. if (notify_on_release(cs)) {
  1315. char *pathbuf = NULL;
  1316. down(&cpuset_sem);
  1317. if (atomic_dec_and_test(&cs->count))
  1318. check_for_release(cs, &pathbuf);
  1319. up(&cpuset_sem);
  1320. cpuset_release_agent(pathbuf);
  1321. } else {
  1322. atomic_dec(&cs->count);
  1323. }
  1324. }
  1325. /**
  1326. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1327. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1328. *
  1329. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1330. * attached to the specified @tsk. Guaranteed to return some non-empty
  1331. * subset of cpu_online_map, even if this means going outside the
  1332. * tasks cpuset.
  1333. **/
  1334. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1335. {
  1336. cpumask_t mask;
  1337. down(&cpuset_sem);
  1338. task_lock((struct task_struct *)tsk);
  1339. guarantee_online_cpus(tsk->cpuset, &mask);
  1340. task_unlock((struct task_struct *)tsk);
  1341. up(&cpuset_sem);
  1342. return mask;
  1343. }
  1344. void cpuset_init_current_mems_allowed(void)
  1345. {
  1346. current->mems_allowed = NODE_MASK_ALL;
  1347. }
  1348. /**
  1349. * cpuset_update_current_mems_allowed - update mems parameters to new values
  1350. *
  1351. * If the current tasks cpusets mems_allowed changed behind our backs,
  1352. * update current->mems_allowed and mems_generation to the new value.
  1353. * Do not call this routine if in_interrupt().
  1354. */
  1355. void cpuset_update_current_mems_allowed(void)
  1356. {
  1357. struct cpuset *cs = current->cpuset;
  1358. if (!cs)
  1359. return; /* task is exiting */
  1360. if (current->cpuset_mems_generation != cs->mems_generation) {
  1361. down(&cpuset_sem);
  1362. refresh_mems();
  1363. up(&cpuset_sem);
  1364. }
  1365. }
  1366. /**
  1367. * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
  1368. * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
  1369. */
  1370. void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
  1371. {
  1372. bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
  1373. MAX_NUMNODES);
  1374. }
  1375. /**
  1376. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1377. * @zl: the zonelist to be checked
  1378. *
  1379. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1380. */
  1381. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1382. {
  1383. int i;
  1384. for (i = 0; zl->zones[i]; i++) {
  1385. int nid = zl->zones[i]->zone_pgdat->node_id;
  1386. if (node_isset(nid, current->mems_allowed))
  1387. return 1;
  1388. }
  1389. return 0;
  1390. }
  1391. /*
  1392. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1393. * ancestor to the specified cpuset. Call while holding cpuset_sem.
  1394. * If no ancestor is mem_exclusive (an unusual configuration), then
  1395. * returns the root cpuset.
  1396. */
  1397. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1398. {
  1399. while (!is_mem_exclusive(cs) && cs->parent)
  1400. cs = cs->parent;
  1401. return cs;
  1402. }
  1403. /**
  1404. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1405. * @z: is this zone on an allowed node?
  1406. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1407. *
  1408. * If we're in interrupt, yes, we can always allocate. If zone
  1409. * z's node is in our tasks mems_allowed, yes. If it's not a
  1410. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1411. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1412. * Otherwise, no.
  1413. *
  1414. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1415. * and do not allow allocations outside the current tasks cpuset.
  1416. * GFP_KERNEL allocations are not so marked, so can escape to the
  1417. * nearest mem_exclusive ancestor cpuset.
  1418. *
  1419. * Scanning up parent cpusets requires cpuset_sem. The __alloc_pages()
  1420. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1421. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1422. * mems_allowed came up empty on the first pass over the zonelist.
  1423. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1424. * short of memory, might require taking the cpuset_sem semaphore.
  1425. *
  1426. * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
  1427. * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
  1428. * hardwall cpusets - no allocation on a node outside the cpuset is
  1429. * allowed (unless in interrupt, of course).
  1430. *
  1431. * The second loop doesn't even call here for GFP_ATOMIC requests
  1432. * (if the __alloc_pages() local variable 'wait' is set). That check
  1433. * and the checks below have the combined affect in the second loop of
  1434. * the __alloc_pages() routine that:
  1435. * in_interrupt - any node ok (current task context irrelevant)
  1436. * GFP_ATOMIC - any node ok
  1437. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1438. * GFP_USER - only nodes in current tasks mems allowed ok.
  1439. **/
  1440. int cpuset_zone_allowed(struct zone *z, unsigned int __nocast gfp_mask)
  1441. {
  1442. int node; /* node that zone z is on */
  1443. const struct cpuset *cs; /* current cpuset ancestors */
  1444. int allowed = 1; /* is allocation in zone z allowed? */
  1445. if (in_interrupt())
  1446. return 1;
  1447. node = z->zone_pgdat->node_id;
  1448. if (node_isset(node, current->mems_allowed))
  1449. return 1;
  1450. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1451. return 0;
  1452. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1453. down(&cpuset_sem);
  1454. cs = current->cpuset;
  1455. if (!cs)
  1456. goto done; /* current task exiting */
  1457. cs = nearest_exclusive_ancestor(cs);
  1458. allowed = node_isset(node, cs->mems_allowed);
  1459. done:
  1460. up(&cpuset_sem);
  1461. return allowed;
  1462. }
  1463. /**
  1464. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  1465. * @p: pointer to task_struct of some other task.
  1466. *
  1467. * Description: Return true if the nearest mem_exclusive ancestor
  1468. * cpusets of tasks @p and current overlap. Used by oom killer to
  1469. * determine if task @p's memory usage might impact the memory
  1470. * available to the current task.
  1471. *
  1472. * Acquires cpuset_sem - not suitable for calling from a fast path.
  1473. **/
  1474. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  1475. {
  1476. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  1477. int overlap = 0; /* do cpusets overlap? */
  1478. down(&cpuset_sem);
  1479. cs1 = current->cpuset;
  1480. if (!cs1)
  1481. goto done; /* current task exiting */
  1482. cs2 = p->cpuset;
  1483. if (!cs2)
  1484. goto done; /* task p is exiting */
  1485. cs1 = nearest_exclusive_ancestor(cs1);
  1486. cs2 = nearest_exclusive_ancestor(cs2);
  1487. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  1488. done:
  1489. up(&cpuset_sem);
  1490. return overlap;
  1491. }
  1492. /*
  1493. * proc_cpuset_show()
  1494. * - Print tasks cpuset path into seq_file.
  1495. * - Used for /proc/<pid>/cpuset.
  1496. */
  1497. static int proc_cpuset_show(struct seq_file *m, void *v)
  1498. {
  1499. struct cpuset *cs;
  1500. struct task_struct *tsk;
  1501. char *buf;
  1502. int retval = 0;
  1503. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1504. if (!buf)
  1505. return -ENOMEM;
  1506. tsk = m->private;
  1507. down(&cpuset_sem);
  1508. task_lock(tsk);
  1509. cs = tsk->cpuset;
  1510. task_unlock(tsk);
  1511. if (!cs) {
  1512. retval = -EINVAL;
  1513. goto out;
  1514. }
  1515. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1516. if (retval < 0)
  1517. goto out;
  1518. seq_puts(m, buf);
  1519. seq_putc(m, '\n');
  1520. out:
  1521. up(&cpuset_sem);
  1522. kfree(buf);
  1523. return retval;
  1524. }
  1525. static int cpuset_open(struct inode *inode, struct file *file)
  1526. {
  1527. struct task_struct *tsk = PROC_I(inode)->task;
  1528. return single_open(file, proc_cpuset_show, tsk);
  1529. }
  1530. struct file_operations proc_cpuset_operations = {
  1531. .open = cpuset_open,
  1532. .read = seq_read,
  1533. .llseek = seq_lseek,
  1534. .release = single_release,
  1535. };
  1536. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1537. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1538. {
  1539. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1540. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1541. buffer += sprintf(buffer, "\n");
  1542. buffer += sprintf(buffer, "Mems_allowed:\t");
  1543. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1544. buffer += sprintf(buffer, "\n");
  1545. return buffer;
  1546. }