cpuset.c 44 KB

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