cpuset.c 53 KB

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