cpuset.c 53 KB

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