cpuset.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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. static struct file_operations cpuset_file_operations = {
  983. .read = cpuset_file_read,
  984. .write = cpuset_file_write,
  985. .llseek = generic_file_llseek,
  986. .open = cpuset_file_open,
  987. .release = cpuset_file_release,
  988. };
  989. static struct inode_operations cpuset_dir_inode_operations = {
  990. .lookup = simple_lookup,
  991. .mkdir = cpuset_mkdir,
  992. .rmdir = cpuset_rmdir,
  993. };
  994. static int cpuset_create_file(struct dentry *dentry, int mode)
  995. {
  996. struct inode *inode;
  997. if (!dentry)
  998. return -ENOENT;
  999. if (dentry->d_inode)
  1000. return -EEXIST;
  1001. inode = cpuset_new_inode(mode);
  1002. if (!inode)
  1003. return -ENOMEM;
  1004. if (S_ISDIR(mode)) {
  1005. inode->i_op = &cpuset_dir_inode_operations;
  1006. inode->i_fop = &simple_dir_operations;
  1007. /* start off with i_nlink == 2 (for "." entry) */
  1008. inode->i_nlink++;
  1009. } else if (S_ISREG(mode)) {
  1010. inode->i_size = 0;
  1011. inode->i_fop = &cpuset_file_operations;
  1012. }
  1013. d_instantiate(dentry, inode);
  1014. dget(dentry); /* Extra count - pin the dentry in core */
  1015. return 0;
  1016. }
  1017. /*
  1018. * cpuset_create_dir - create a directory for an object.
  1019. * cs: the cpuset we create the directory for.
  1020. * It must have a valid ->parent field
  1021. * And we are going to fill its ->dentry field.
  1022. * name: The name to give to the cpuset directory. Will be copied.
  1023. * mode: mode to set on new directory.
  1024. */
  1025. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  1026. {
  1027. struct dentry *dentry = NULL;
  1028. struct dentry *parent;
  1029. int error = 0;
  1030. parent = cs->parent->dentry;
  1031. dentry = cpuset_get_dentry(parent, name);
  1032. if (IS_ERR(dentry))
  1033. return PTR_ERR(dentry);
  1034. error = cpuset_create_file(dentry, S_IFDIR | mode);
  1035. if (!error) {
  1036. dentry->d_fsdata = cs;
  1037. parent->d_inode->i_nlink++;
  1038. cs->dentry = dentry;
  1039. }
  1040. dput(dentry);
  1041. return error;
  1042. }
  1043. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  1044. {
  1045. struct dentry *dentry;
  1046. int error;
  1047. down(&dir->d_inode->i_sem);
  1048. dentry = cpuset_get_dentry(dir, cft->name);
  1049. if (!IS_ERR(dentry)) {
  1050. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  1051. if (!error)
  1052. dentry->d_fsdata = (void *)cft;
  1053. dput(dentry);
  1054. } else
  1055. error = PTR_ERR(dentry);
  1056. up(&dir->d_inode->i_sem);
  1057. return error;
  1058. }
  1059. /*
  1060. * Stuff for reading the 'tasks' file.
  1061. *
  1062. * Reading this file can return large amounts of data if a cpuset has
  1063. * *lots* of attached tasks. So it may need several calls to read(),
  1064. * but we cannot guarantee that the information we produce is correct
  1065. * unless we produce it entirely atomically.
  1066. *
  1067. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1068. * will have a pointer to an array (also allocated here). The struct
  1069. * ctr_struct * is stored in file->private_data. Its resources will
  1070. * be freed by release() when the file is closed. The array is used
  1071. * to sprintf the PIDs and then used by read().
  1072. */
  1073. /* cpusets_tasks_read array */
  1074. struct ctr_struct {
  1075. char *buf;
  1076. int bufsz;
  1077. };
  1078. /*
  1079. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1080. * Return actual number of pids loaded. No need to task_lock(p)
  1081. * when reading out p->cpuset, as we don't really care if it changes
  1082. * on the next cycle, and we are not going to try to dereference it.
  1083. */
  1084. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1085. {
  1086. int n = 0;
  1087. struct task_struct *g, *p;
  1088. read_lock(&tasklist_lock);
  1089. do_each_thread(g, p) {
  1090. if (p->cpuset == cs) {
  1091. pidarray[n++] = p->pid;
  1092. if (unlikely(n == npids))
  1093. goto array_full;
  1094. }
  1095. } while_each_thread(g, p);
  1096. array_full:
  1097. read_unlock(&tasklist_lock);
  1098. return n;
  1099. }
  1100. static int cmppid(const void *a, const void *b)
  1101. {
  1102. return *(pid_t *)a - *(pid_t *)b;
  1103. }
  1104. /*
  1105. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1106. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1107. * count 'cnt' of how many chars would be written if buf were large enough.
  1108. */
  1109. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1110. {
  1111. int cnt = 0;
  1112. int i;
  1113. for (i = 0; i < npids; i++)
  1114. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1115. return cnt;
  1116. }
  1117. /*
  1118. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1119. * process id's of tasks currently attached to the cpuset being opened.
  1120. *
  1121. * Does not require any specific cpuset semaphores, and does not take any.
  1122. */
  1123. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1124. {
  1125. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1126. struct ctr_struct *ctr;
  1127. pid_t *pidarray;
  1128. int npids;
  1129. char c;
  1130. if (!(file->f_mode & FMODE_READ))
  1131. return 0;
  1132. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1133. if (!ctr)
  1134. goto err0;
  1135. /*
  1136. * If cpuset gets more users after we read count, we won't have
  1137. * enough space - tough. This race is indistinguishable to the
  1138. * caller from the case that the additional cpuset users didn't
  1139. * show up until sometime later on.
  1140. */
  1141. npids = atomic_read(&cs->count);
  1142. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1143. if (!pidarray)
  1144. goto err1;
  1145. npids = pid_array_load(pidarray, npids, cs);
  1146. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1147. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1148. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1149. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1150. if (!ctr->buf)
  1151. goto err2;
  1152. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1153. kfree(pidarray);
  1154. file->private_data = ctr;
  1155. return 0;
  1156. err2:
  1157. kfree(pidarray);
  1158. err1:
  1159. kfree(ctr);
  1160. err0:
  1161. return -ENOMEM;
  1162. }
  1163. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1164. size_t nbytes, loff_t *ppos)
  1165. {
  1166. struct ctr_struct *ctr = file->private_data;
  1167. if (*ppos + nbytes > ctr->bufsz)
  1168. nbytes = ctr->bufsz - *ppos;
  1169. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1170. return -EFAULT;
  1171. *ppos += nbytes;
  1172. return nbytes;
  1173. }
  1174. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1175. {
  1176. struct ctr_struct *ctr;
  1177. if (file->f_mode & FMODE_READ) {
  1178. ctr = file->private_data;
  1179. kfree(ctr->buf);
  1180. kfree(ctr);
  1181. }
  1182. return 0;
  1183. }
  1184. /*
  1185. * for the common functions, 'private' gives the type of file
  1186. */
  1187. static struct cftype cft_tasks = {
  1188. .name = "tasks",
  1189. .open = cpuset_tasks_open,
  1190. .read = cpuset_tasks_read,
  1191. .release = cpuset_tasks_release,
  1192. .private = FILE_TASKLIST,
  1193. };
  1194. static struct cftype cft_cpus = {
  1195. .name = "cpus",
  1196. .private = FILE_CPULIST,
  1197. };
  1198. static struct cftype cft_mems = {
  1199. .name = "mems",
  1200. .private = FILE_MEMLIST,
  1201. };
  1202. static struct cftype cft_cpu_exclusive = {
  1203. .name = "cpu_exclusive",
  1204. .private = FILE_CPU_EXCLUSIVE,
  1205. };
  1206. static struct cftype cft_mem_exclusive = {
  1207. .name = "mem_exclusive",
  1208. .private = FILE_MEM_EXCLUSIVE,
  1209. };
  1210. static struct cftype cft_notify_on_release = {
  1211. .name = "notify_on_release",
  1212. .private = FILE_NOTIFY_ON_RELEASE,
  1213. };
  1214. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1215. {
  1216. int err;
  1217. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1218. return err;
  1219. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1220. return err;
  1221. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1222. return err;
  1223. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1224. return err;
  1225. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1226. return err;
  1227. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1228. return err;
  1229. return 0;
  1230. }
  1231. /*
  1232. * cpuset_create - create a cpuset
  1233. * parent: cpuset that will be parent of the new cpuset.
  1234. * name: name of the new cpuset. Will be strcpy'ed.
  1235. * mode: mode to set on new inode
  1236. *
  1237. * Must be called with the semaphore on the parent inode held
  1238. */
  1239. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1240. {
  1241. struct cpuset *cs;
  1242. int err;
  1243. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1244. if (!cs)
  1245. return -ENOMEM;
  1246. down(&manage_sem);
  1247. refresh_mems();
  1248. cs->flags = 0;
  1249. if (notify_on_release(parent))
  1250. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1251. cs->cpus_allowed = CPU_MASK_NONE;
  1252. cs->mems_allowed = NODE_MASK_NONE;
  1253. atomic_set(&cs->count, 0);
  1254. INIT_LIST_HEAD(&cs->sibling);
  1255. INIT_LIST_HEAD(&cs->children);
  1256. atomic_inc(&cpuset_mems_generation);
  1257. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1258. cs->parent = parent;
  1259. down(&callback_sem);
  1260. list_add(&cs->sibling, &cs->parent->children);
  1261. up(&callback_sem);
  1262. err = cpuset_create_dir(cs, name, mode);
  1263. if (err < 0)
  1264. goto err;
  1265. /*
  1266. * Release manage_sem before cpuset_populate_dir() because it
  1267. * will down() this new directory's i_sem and if we race with
  1268. * another mkdir, we might deadlock.
  1269. */
  1270. up(&manage_sem);
  1271. err = cpuset_populate_dir(cs->dentry);
  1272. /* If err < 0, we have a half-filled directory - oh well ;) */
  1273. return 0;
  1274. err:
  1275. list_del(&cs->sibling);
  1276. up(&manage_sem);
  1277. kfree(cs);
  1278. return err;
  1279. }
  1280. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1281. {
  1282. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1283. /* the vfs holds inode->i_sem already */
  1284. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1285. }
  1286. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1287. {
  1288. struct cpuset *cs = dentry->d_fsdata;
  1289. struct dentry *d;
  1290. struct cpuset *parent;
  1291. char *pathbuf = NULL;
  1292. /* the vfs holds both inode->i_sem already */
  1293. down(&manage_sem);
  1294. refresh_mems();
  1295. if (atomic_read(&cs->count) > 0) {
  1296. up(&manage_sem);
  1297. return -EBUSY;
  1298. }
  1299. if (!list_empty(&cs->children)) {
  1300. up(&manage_sem);
  1301. return -EBUSY;
  1302. }
  1303. parent = cs->parent;
  1304. down(&callback_sem);
  1305. set_bit(CS_REMOVED, &cs->flags);
  1306. if (is_cpu_exclusive(cs))
  1307. update_cpu_domains(cs);
  1308. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1309. spin_lock(&cs->dentry->d_lock);
  1310. d = dget(cs->dentry);
  1311. cs->dentry = NULL;
  1312. spin_unlock(&d->d_lock);
  1313. cpuset_d_remove_dir(d);
  1314. dput(d);
  1315. up(&callback_sem);
  1316. if (list_empty(&parent->children))
  1317. check_for_release(parent, &pathbuf);
  1318. up(&manage_sem);
  1319. cpuset_release_agent(pathbuf);
  1320. return 0;
  1321. }
  1322. /**
  1323. * cpuset_init - initialize cpusets at system boot
  1324. *
  1325. * Description: Initialize top_cpuset and the cpuset internal file system,
  1326. **/
  1327. int __init cpuset_init(void)
  1328. {
  1329. struct dentry *root;
  1330. int err;
  1331. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1332. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1333. atomic_inc(&cpuset_mems_generation);
  1334. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1335. init_task.cpuset = &top_cpuset;
  1336. err = register_filesystem(&cpuset_fs_type);
  1337. if (err < 0)
  1338. goto out;
  1339. cpuset_mount = kern_mount(&cpuset_fs_type);
  1340. if (IS_ERR(cpuset_mount)) {
  1341. printk(KERN_ERR "cpuset: could not mount!\n");
  1342. err = PTR_ERR(cpuset_mount);
  1343. cpuset_mount = NULL;
  1344. goto out;
  1345. }
  1346. root = cpuset_mount->mnt_sb->s_root;
  1347. root->d_fsdata = &top_cpuset;
  1348. root->d_inode->i_nlink++;
  1349. top_cpuset.dentry = root;
  1350. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1351. err = cpuset_populate_dir(root);
  1352. out:
  1353. return err;
  1354. }
  1355. /**
  1356. * cpuset_init_smp - initialize cpus_allowed
  1357. *
  1358. * Description: Finish top cpuset after cpu, node maps are initialized
  1359. **/
  1360. void __init cpuset_init_smp(void)
  1361. {
  1362. top_cpuset.cpus_allowed = cpu_online_map;
  1363. top_cpuset.mems_allowed = node_online_map;
  1364. }
  1365. /**
  1366. * cpuset_fork - attach newly forked task to its parents cpuset.
  1367. * @tsk: pointer to task_struct of forking parent process.
  1368. *
  1369. * Description: A task inherits its parent's cpuset at fork().
  1370. *
  1371. * A pointer to the shared cpuset was automatically copied in fork.c
  1372. * by dup_task_struct(). However, we ignore that copy, since it was
  1373. * not made under the protection of task_lock(), so might no longer be
  1374. * a valid cpuset pointer. attach_task() might have already changed
  1375. * current->cpuset, allowing the previously referenced cpuset to
  1376. * be removed and freed. Instead, we task_lock(current) and copy
  1377. * its present value of current->cpuset for our freshly forked child.
  1378. *
  1379. * At the point that cpuset_fork() is called, 'current' is the parent
  1380. * task, and the passed argument 'child' points to the child task.
  1381. **/
  1382. void cpuset_fork(struct task_struct *child)
  1383. {
  1384. task_lock(current);
  1385. child->cpuset = current->cpuset;
  1386. atomic_inc(&child->cpuset->count);
  1387. task_unlock(current);
  1388. }
  1389. /**
  1390. * cpuset_exit - detach cpuset from exiting task
  1391. * @tsk: pointer to task_struct of exiting process
  1392. *
  1393. * Description: Detach cpuset from @tsk and release it.
  1394. *
  1395. * Note that cpusets marked notify_on_release force every task in
  1396. * them to take the global manage_sem semaphore when exiting.
  1397. * This could impact scaling on very large systems. Be reluctant to
  1398. * use notify_on_release cpusets where very high task exit scaling
  1399. * is required on large systems.
  1400. *
  1401. * Don't even think about derefencing 'cs' after the cpuset use count
  1402. * goes to zero, except inside a critical section guarded by manage_sem
  1403. * or callback_sem. Otherwise a zero cpuset use count is a license to
  1404. * any other task to nuke the cpuset immediately, via cpuset_rmdir().
  1405. *
  1406. * This routine has to take manage_sem, not callback_sem, because
  1407. * it is holding that semaphore while calling check_for_release(),
  1408. * which calls kmalloc(), so can't be called holding callback__sem().
  1409. *
  1410. * We don't need to task_lock() this reference to tsk->cpuset,
  1411. * because tsk is already marked PF_EXITING, so attach_task() won't
  1412. * mess with it.
  1413. **/
  1414. void cpuset_exit(struct task_struct *tsk)
  1415. {
  1416. struct cpuset *cs;
  1417. BUG_ON(!(tsk->flags & PF_EXITING));
  1418. cs = tsk->cpuset;
  1419. tsk->cpuset = NULL;
  1420. if (notify_on_release(cs)) {
  1421. char *pathbuf = NULL;
  1422. down(&manage_sem);
  1423. if (atomic_dec_and_test(&cs->count))
  1424. check_for_release(cs, &pathbuf);
  1425. up(&manage_sem);
  1426. cpuset_release_agent(pathbuf);
  1427. } else {
  1428. atomic_dec(&cs->count);
  1429. }
  1430. }
  1431. /**
  1432. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1433. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1434. *
  1435. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1436. * attached to the specified @tsk. Guaranteed to return some non-empty
  1437. * subset of cpu_online_map, even if this means going outside the
  1438. * tasks cpuset.
  1439. **/
  1440. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1441. {
  1442. cpumask_t mask;
  1443. down(&callback_sem);
  1444. task_lock((struct task_struct *)tsk);
  1445. guarantee_online_cpus(tsk->cpuset, &mask);
  1446. task_unlock((struct task_struct *)tsk);
  1447. up(&callback_sem);
  1448. return mask;
  1449. }
  1450. void cpuset_init_current_mems_allowed(void)
  1451. {
  1452. current->mems_allowed = NODE_MASK_ALL;
  1453. }
  1454. /**
  1455. * cpuset_update_current_mems_allowed - update mems parameters to new values
  1456. *
  1457. * If the current tasks cpusets mems_allowed changed behind our backs,
  1458. * update current->mems_allowed and mems_generation to the new value.
  1459. * Do not call this routine if in_interrupt().
  1460. *
  1461. * Call without callback_sem or task_lock() held. May be called
  1462. * with or without manage_sem held. Unless exiting, it will acquire
  1463. * task_lock(). Also might acquire callback_sem during call to
  1464. * refresh_mems().
  1465. */
  1466. void cpuset_update_current_mems_allowed(void)
  1467. {
  1468. struct cpuset *cs;
  1469. int need_to_refresh = 0;
  1470. task_lock(current);
  1471. cs = current->cpuset;
  1472. if (!cs)
  1473. goto done;
  1474. if (current->cpuset_mems_generation != cs->mems_generation)
  1475. need_to_refresh = 1;
  1476. done:
  1477. task_unlock(current);
  1478. if (need_to_refresh)
  1479. refresh_mems();
  1480. }
  1481. /**
  1482. * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
  1483. * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
  1484. */
  1485. void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
  1486. {
  1487. bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
  1488. MAX_NUMNODES);
  1489. }
  1490. /**
  1491. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1492. * @zl: the zonelist to be checked
  1493. *
  1494. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1495. */
  1496. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1497. {
  1498. int i;
  1499. for (i = 0; zl->zones[i]; i++) {
  1500. int nid = zl->zones[i]->zone_pgdat->node_id;
  1501. if (node_isset(nid, current->mems_allowed))
  1502. return 1;
  1503. }
  1504. return 0;
  1505. }
  1506. /*
  1507. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1508. * ancestor to the specified cpuset. Call holding callback_sem.
  1509. * If no ancestor is mem_exclusive (an unusual configuration), then
  1510. * returns the root cpuset.
  1511. */
  1512. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1513. {
  1514. while (!is_mem_exclusive(cs) && cs->parent)
  1515. cs = cs->parent;
  1516. return cs;
  1517. }
  1518. /**
  1519. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1520. * @z: is this zone on an allowed node?
  1521. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1522. *
  1523. * If we're in interrupt, yes, we can always allocate. If zone
  1524. * z's node is in our tasks mems_allowed, yes. If it's not a
  1525. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1526. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1527. * Otherwise, no.
  1528. *
  1529. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1530. * and do not allow allocations outside the current tasks cpuset.
  1531. * GFP_KERNEL allocations are not so marked, so can escape to the
  1532. * nearest mem_exclusive ancestor cpuset.
  1533. *
  1534. * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
  1535. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1536. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1537. * mems_allowed came up empty on the first pass over the zonelist.
  1538. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1539. * short of memory, might require taking the callback_sem semaphore.
  1540. *
  1541. * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
  1542. * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
  1543. * hardwall cpusets - no allocation on a node outside the cpuset is
  1544. * allowed (unless in interrupt, of course).
  1545. *
  1546. * The second loop doesn't even call here for GFP_ATOMIC requests
  1547. * (if the __alloc_pages() local variable 'wait' is set). That check
  1548. * and the checks below have the combined affect in the second loop of
  1549. * the __alloc_pages() routine that:
  1550. * in_interrupt - any node ok (current task context irrelevant)
  1551. * GFP_ATOMIC - any node ok
  1552. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1553. * GFP_USER - only nodes in current tasks mems allowed ok.
  1554. **/
  1555. int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  1556. {
  1557. int node; /* node that zone z is on */
  1558. const struct cpuset *cs; /* current cpuset ancestors */
  1559. int allowed = 1; /* is allocation in zone z allowed? */
  1560. if (in_interrupt())
  1561. return 1;
  1562. node = z->zone_pgdat->node_id;
  1563. if (node_isset(node, current->mems_allowed))
  1564. return 1;
  1565. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1566. return 0;
  1567. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1568. down(&callback_sem);
  1569. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1570. return 1;
  1571. task_lock(current);
  1572. cs = nearest_exclusive_ancestor(current->cpuset);
  1573. task_unlock(current);
  1574. allowed = node_isset(node, cs->mems_allowed);
  1575. up(&callback_sem);
  1576. return allowed;
  1577. }
  1578. /**
  1579. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  1580. * @p: pointer to task_struct of some other task.
  1581. *
  1582. * Description: Return true if the nearest mem_exclusive ancestor
  1583. * cpusets of tasks @p and current overlap. Used by oom killer to
  1584. * determine if task @p's memory usage might impact the memory
  1585. * available to the current task.
  1586. *
  1587. * Acquires callback_sem - not suitable for calling from a fast path.
  1588. **/
  1589. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  1590. {
  1591. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  1592. int overlap = 0; /* do cpusets overlap? */
  1593. down(&callback_sem);
  1594. task_lock(current);
  1595. if (current->flags & PF_EXITING) {
  1596. task_unlock(current);
  1597. goto done;
  1598. }
  1599. cs1 = nearest_exclusive_ancestor(current->cpuset);
  1600. task_unlock(current);
  1601. task_lock((struct task_struct *)p);
  1602. if (p->flags & PF_EXITING) {
  1603. task_unlock((struct task_struct *)p);
  1604. goto done;
  1605. }
  1606. cs2 = nearest_exclusive_ancestor(p->cpuset);
  1607. task_unlock((struct task_struct *)p);
  1608. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  1609. done:
  1610. up(&callback_sem);
  1611. return overlap;
  1612. }
  1613. /*
  1614. * proc_cpuset_show()
  1615. * - Print tasks cpuset path into seq_file.
  1616. * - Used for /proc/<pid>/cpuset.
  1617. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  1618. * doesn't really matter if tsk->cpuset changes after we read it,
  1619. * and we take manage_sem, keeping attach_task() from changing it
  1620. * anyway.
  1621. */
  1622. static int proc_cpuset_show(struct seq_file *m, void *v)
  1623. {
  1624. struct cpuset *cs;
  1625. struct task_struct *tsk;
  1626. char *buf;
  1627. int retval = 0;
  1628. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1629. if (!buf)
  1630. return -ENOMEM;
  1631. tsk = m->private;
  1632. down(&manage_sem);
  1633. cs = tsk->cpuset;
  1634. if (!cs) {
  1635. retval = -EINVAL;
  1636. goto out;
  1637. }
  1638. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1639. if (retval < 0)
  1640. goto out;
  1641. seq_puts(m, buf);
  1642. seq_putc(m, '\n');
  1643. out:
  1644. up(&manage_sem);
  1645. kfree(buf);
  1646. return retval;
  1647. }
  1648. static int cpuset_open(struct inode *inode, struct file *file)
  1649. {
  1650. struct task_struct *tsk = PROC_I(inode)->task;
  1651. return single_open(file, proc_cpuset_show, tsk);
  1652. }
  1653. struct file_operations proc_cpuset_operations = {
  1654. .open = cpuset_open,
  1655. .read = seq_read,
  1656. .llseek = seq_lseek,
  1657. .release = single_release,
  1658. };
  1659. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1660. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1661. {
  1662. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1663. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1664. buffer += sprintf(buffer, "\n");
  1665. buffer += sprintf(buffer, "Mems_allowed:\t");
  1666. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1667. buffer += sprintf(buffer, "\n");
  1668. return buffer;
  1669. }