cpuset.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  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. /*
  560. * Hack to avoid 2.6.13 partial node dynamic sched domain bug.
  561. * Disable letting 'cpu_exclusive' cpusets define dynamic sched
  562. * domains, until the sched domain can handle partial nodes.
  563. * Remove this #if hackery when sched domains fixed.
  564. */
  565. #if 0
  566. static void update_cpu_domains(struct cpuset *cur)
  567. {
  568. struct cpuset *c, *par = cur->parent;
  569. cpumask_t pspan, cspan;
  570. if (par == NULL || cpus_empty(cur->cpus_allowed))
  571. return;
  572. /*
  573. * Get all cpus from parent's cpus_allowed not part of exclusive
  574. * children
  575. */
  576. pspan = par->cpus_allowed;
  577. list_for_each_entry(c, &par->children, sibling) {
  578. if (is_cpu_exclusive(c))
  579. cpus_andnot(pspan, pspan, c->cpus_allowed);
  580. }
  581. if (is_removed(cur) || !is_cpu_exclusive(cur)) {
  582. cpus_or(pspan, pspan, cur->cpus_allowed);
  583. if (cpus_equal(pspan, cur->cpus_allowed))
  584. return;
  585. cspan = CPU_MASK_NONE;
  586. } else {
  587. if (cpus_empty(pspan))
  588. return;
  589. cspan = cur->cpus_allowed;
  590. /*
  591. * Get all cpus from current cpuset's cpus_allowed not part
  592. * of exclusive children
  593. */
  594. list_for_each_entry(c, &cur->children, sibling) {
  595. if (is_cpu_exclusive(c))
  596. cpus_andnot(cspan, cspan, c->cpus_allowed);
  597. }
  598. }
  599. lock_cpu_hotplug();
  600. partition_sched_domains(&pspan, &cspan);
  601. unlock_cpu_hotplug();
  602. }
  603. #else
  604. static void update_cpu_domains(struct cpuset *cur)
  605. {
  606. }
  607. #endif
  608. static int update_cpumask(struct cpuset *cs, char *buf)
  609. {
  610. struct cpuset trialcs;
  611. int retval, cpus_unchanged;
  612. trialcs = *cs;
  613. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  614. if (retval < 0)
  615. return retval;
  616. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  617. if (cpus_empty(trialcs.cpus_allowed))
  618. return -ENOSPC;
  619. retval = validate_change(cs, &trialcs);
  620. if (retval < 0)
  621. return retval;
  622. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  623. cs->cpus_allowed = trialcs.cpus_allowed;
  624. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  625. update_cpu_domains(cs);
  626. return 0;
  627. }
  628. static int update_nodemask(struct cpuset *cs, char *buf)
  629. {
  630. struct cpuset trialcs;
  631. int retval;
  632. trialcs = *cs;
  633. retval = nodelist_parse(buf, trialcs.mems_allowed);
  634. if (retval < 0)
  635. return retval;
  636. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  637. if (nodes_empty(trialcs.mems_allowed))
  638. return -ENOSPC;
  639. retval = validate_change(cs, &trialcs);
  640. if (retval == 0) {
  641. cs->mems_allowed = trialcs.mems_allowed;
  642. atomic_inc(&cpuset_mems_generation);
  643. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  644. }
  645. return retval;
  646. }
  647. /*
  648. * update_flag - read a 0 or a 1 in a file and update associated flag
  649. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  650. * CS_NOTIFY_ON_RELEASE)
  651. * cs: the cpuset to update
  652. * buf: the buffer where we read the 0 or 1
  653. */
  654. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  655. {
  656. int turning_on;
  657. struct cpuset trialcs;
  658. int err, cpu_exclusive_changed;
  659. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  660. trialcs = *cs;
  661. if (turning_on)
  662. set_bit(bit, &trialcs.flags);
  663. else
  664. clear_bit(bit, &trialcs.flags);
  665. err = validate_change(cs, &trialcs);
  666. if (err < 0)
  667. return err;
  668. cpu_exclusive_changed =
  669. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  670. if (turning_on)
  671. set_bit(bit, &cs->flags);
  672. else
  673. clear_bit(bit, &cs->flags);
  674. if (cpu_exclusive_changed)
  675. update_cpu_domains(cs);
  676. return 0;
  677. }
  678. static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
  679. {
  680. pid_t pid;
  681. struct task_struct *tsk;
  682. struct cpuset *oldcs;
  683. cpumask_t cpus;
  684. if (sscanf(pidbuf, "%d", &pid) != 1)
  685. return -EIO;
  686. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  687. return -ENOSPC;
  688. if (pid) {
  689. read_lock(&tasklist_lock);
  690. tsk = find_task_by_pid(pid);
  691. if (!tsk) {
  692. read_unlock(&tasklist_lock);
  693. return -ESRCH;
  694. }
  695. get_task_struct(tsk);
  696. read_unlock(&tasklist_lock);
  697. if ((current->euid) && (current->euid != tsk->uid)
  698. && (current->euid != tsk->suid)) {
  699. put_task_struct(tsk);
  700. return -EACCES;
  701. }
  702. } else {
  703. tsk = current;
  704. get_task_struct(tsk);
  705. }
  706. task_lock(tsk);
  707. oldcs = tsk->cpuset;
  708. if (!oldcs) {
  709. task_unlock(tsk);
  710. put_task_struct(tsk);
  711. return -ESRCH;
  712. }
  713. atomic_inc(&cs->count);
  714. tsk->cpuset = cs;
  715. task_unlock(tsk);
  716. guarantee_online_cpus(cs, &cpus);
  717. set_cpus_allowed(tsk, cpus);
  718. put_task_struct(tsk);
  719. if (atomic_dec_and_test(&oldcs->count))
  720. check_for_release(oldcs, ppathbuf);
  721. return 0;
  722. }
  723. /* The various types of files and directories in a cpuset file system */
  724. typedef enum {
  725. FILE_ROOT,
  726. FILE_DIR,
  727. FILE_CPULIST,
  728. FILE_MEMLIST,
  729. FILE_CPU_EXCLUSIVE,
  730. FILE_MEM_EXCLUSIVE,
  731. FILE_NOTIFY_ON_RELEASE,
  732. FILE_TASKLIST,
  733. } cpuset_filetype_t;
  734. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  735. size_t nbytes, loff_t *unused_ppos)
  736. {
  737. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  738. struct cftype *cft = __d_cft(file->f_dentry);
  739. cpuset_filetype_t type = cft->private;
  740. char *buffer;
  741. char *pathbuf = NULL;
  742. int retval = 0;
  743. /* Crude upper limit on largest legitimate cpulist user might write. */
  744. if (nbytes > 100 + 6 * NR_CPUS)
  745. return -E2BIG;
  746. /* +1 for nul-terminator */
  747. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  748. return -ENOMEM;
  749. if (copy_from_user(buffer, userbuf, nbytes)) {
  750. retval = -EFAULT;
  751. goto out1;
  752. }
  753. buffer[nbytes] = 0; /* nul-terminate */
  754. down(&cpuset_sem);
  755. if (is_removed(cs)) {
  756. retval = -ENODEV;
  757. goto out2;
  758. }
  759. switch (type) {
  760. case FILE_CPULIST:
  761. retval = update_cpumask(cs, buffer);
  762. break;
  763. case FILE_MEMLIST:
  764. retval = update_nodemask(cs, buffer);
  765. break;
  766. case FILE_CPU_EXCLUSIVE:
  767. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  768. break;
  769. case FILE_MEM_EXCLUSIVE:
  770. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  771. break;
  772. case FILE_NOTIFY_ON_RELEASE:
  773. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  774. break;
  775. case FILE_TASKLIST:
  776. retval = attach_task(cs, buffer, &pathbuf);
  777. break;
  778. default:
  779. retval = -EINVAL;
  780. goto out2;
  781. }
  782. if (retval == 0)
  783. retval = nbytes;
  784. out2:
  785. up(&cpuset_sem);
  786. cpuset_release_agent(pathbuf);
  787. out1:
  788. kfree(buffer);
  789. return retval;
  790. }
  791. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  792. size_t nbytes, loff_t *ppos)
  793. {
  794. ssize_t retval = 0;
  795. struct cftype *cft = __d_cft(file->f_dentry);
  796. if (!cft)
  797. return -ENODEV;
  798. /* special function ? */
  799. if (cft->write)
  800. retval = cft->write(file, buf, nbytes, ppos);
  801. else
  802. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  803. return retval;
  804. }
  805. /*
  806. * These ascii lists should be read in a single call, by using a user
  807. * buffer large enough to hold the entire map. If read in smaller
  808. * chunks, there is no guarantee of atomicity. Since the display format
  809. * used, list of ranges of sequential numbers, is variable length,
  810. * and since these maps can change value dynamically, one could read
  811. * gibberish by doing partial reads while a list was changing.
  812. * A single large read to a buffer that crosses a page boundary is
  813. * ok, because the result being copied to user land is not recomputed
  814. * across a page fault.
  815. */
  816. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  817. {
  818. cpumask_t mask;
  819. down(&cpuset_sem);
  820. mask = cs->cpus_allowed;
  821. up(&cpuset_sem);
  822. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  823. }
  824. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  825. {
  826. nodemask_t mask;
  827. down(&cpuset_sem);
  828. mask = cs->mems_allowed;
  829. up(&cpuset_sem);
  830. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  831. }
  832. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  833. size_t nbytes, loff_t *ppos)
  834. {
  835. struct cftype *cft = __d_cft(file->f_dentry);
  836. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  837. cpuset_filetype_t type = cft->private;
  838. char *page;
  839. ssize_t retval = 0;
  840. char *s;
  841. char *start;
  842. size_t n;
  843. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  844. return -ENOMEM;
  845. s = page;
  846. switch (type) {
  847. case FILE_CPULIST:
  848. s += cpuset_sprintf_cpulist(s, cs);
  849. break;
  850. case FILE_MEMLIST:
  851. s += cpuset_sprintf_memlist(s, cs);
  852. break;
  853. case FILE_CPU_EXCLUSIVE:
  854. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  855. break;
  856. case FILE_MEM_EXCLUSIVE:
  857. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  858. break;
  859. case FILE_NOTIFY_ON_RELEASE:
  860. *s++ = notify_on_release(cs) ? '1' : '0';
  861. break;
  862. default:
  863. retval = -EINVAL;
  864. goto out;
  865. }
  866. *s++ = '\n';
  867. *s = '\0';
  868. start = page + *ppos;
  869. n = s - start;
  870. retval = n - copy_to_user(buf, start, min(n, nbytes));
  871. *ppos += retval;
  872. out:
  873. free_page((unsigned long)page);
  874. return retval;
  875. }
  876. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  877. loff_t *ppos)
  878. {
  879. ssize_t retval = 0;
  880. struct cftype *cft = __d_cft(file->f_dentry);
  881. if (!cft)
  882. return -ENODEV;
  883. /* special function ? */
  884. if (cft->read)
  885. retval = cft->read(file, buf, nbytes, ppos);
  886. else
  887. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  888. return retval;
  889. }
  890. static int cpuset_file_open(struct inode *inode, struct file *file)
  891. {
  892. int err;
  893. struct cftype *cft;
  894. err = generic_file_open(inode, file);
  895. if (err)
  896. return err;
  897. cft = __d_cft(file->f_dentry);
  898. if (!cft)
  899. return -ENODEV;
  900. if (cft->open)
  901. err = cft->open(inode, file);
  902. else
  903. err = 0;
  904. return err;
  905. }
  906. static int cpuset_file_release(struct inode *inode, struct file *file)
  907. {
  908. struct cftype *cft = __d_cft(file->f_dentry);
  909. if (cft->release)
  910. return cft->release(inode, file);
  911. return 0;
  912. }
  913. static struct file_operations cpuset_file_operations = {
  914. .read = cpuset_file_read,
  915. .write = cpuset_file_write,
  916. .llseek = generic_file_llseek,
  917. .open = cpuset_file_open,
  918. .release = cpuset_file_release,
  919. };
  920. static struct inode_operations cpuset_dir_inode_operations = {
  921. .lookup = simple_lookup,
  922. .mkdir = cpuset_mkdir,
  923. .rmdir = cpuset_rmdir,
  924. };
  925. static int cpuset_create_file(struct dentry *dentry, int mode)
  926. {
  927. struct inode *inode;
  928. if (!dentry)
  929. return -ENOENT;
  930. if (dentry->d_inode)
  931. return -EEXIST;
  932. inode = cpuset_new_inode(mode);
  933. if (!inode)
  934. return -ENOMEM;
  935. if (S_ISDIR(mode)) {
  936. inode->i_op = &cpuset_dir_inode_operations;
  937. inode->i_fop = &simple_dir_operations;
  938. /* start off with i_nlink == 2 (for "." entry) */
  939. inode->i_nlink++;
  940. } else if (S_ISREG(mode)) {
  941. inode->i_size = 0;
  942. inode->i_fop = &cpuset_file_operations;
  943. }
  944. d_instantiate(dentry, inode);
  945. dget(dentry); /* Extra count - pin the dentry in core */
  946. return 0;
  947. }
  948. /*
  949. * cpuset_create_dir - create a directory for an object.
  950. * cs: the cpuset we create the directory for.
  951. * It must have a valid ->parent field
  952. * And we are going to fill its ->dentry field.
  953. * name: The name to give to the cpuset directory. Will be copied.
  954. * mode: mode to set on new directory.
  955. */
  956. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  957. {
  958. struct dentry *dentry = NULL;
  959. struct dentry *parent;
  960. int error = 0;
  961. parent = cs->parent->dentry;
  962. dentry = cpuset_get_dentry(parent, name);
  963. if (IS_ERR(dentry))
  964. return PTR_ERR(dentry);
  965. error = cpuset_create_file(dentry, S_IFDIR | mode);
  966. if (!error) {
  967. dentry->d_fsdata = cs;
  968. parent->d_inode->i_nlink++;
  969. cs->dentry = dentry;
  970. }
  971. dput(dentry);
  972. return error;
  973. }
  974. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  975. {
  976. struct dentry *dentry;
  977. int error;
  978. down(&dir->d_inode->i_sem);
  979. dentry = cpuset_get_dentry(dir, cft->name);
  980. if (!IS_ERR(dentry)) {
  981. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  982. if (!error)
  983. dentry->d_fsdata = (void *)cft;
  984. dput(dentry);
  985. } else
  986. error = PTR_ERR(dentry);
  987. up(&dir->d_inode->i_sem);
  988. return error;
  989. }
  990. /*
  991. * Stuff for reading the 'tasks' file.
  992. *
  993. * Reading this file can return large amounts of data if a cpuset has
  994. * *lots* of attached tasks. So it may need several calls to read(),
  995. * but we cannot guarantee that the information we produce is correct
  996. * unless we produce it entirely atomically.
  997. *
  998. * Upon tasks file open(), a struct ctr_struct is allocated, that
  999. * will have a pointer to an array (also allocated here). The struct
  1000. * ctr_struct * is stored in file->private_data. Its resources will
  1001. * be freed by release() when the file is closed. The array is used
  1002. * to sprintf the PIDs and then used by read().
  1003. */
  1004. /* cpusets_tasks_read array */
  1005. struct ctr_struct {
  1006. char *buf;
  1007. int bufsz;
  1008. };
  1009. /*
  1010. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1011. * Return actual number of pids loaded.
  1012. */
  1013. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1014. {
  1015. int n = 0;
  1016. struct task_struct *g, *p;
  1017. read_lock(&tasklist_lock);
  1018. do_each_thread(g, p) {
  1019. if (p->cpuset == cs) {
  1020. pidarray[n++] = p->pid;
  1021. if (unlikely(n == npids))
  1022. goto array_full;
  1023. }
  1024. } while_each_thread(g, p);
  1025. array_full:
  1026. read_unlock(&tasklist_lock);
  1027. return n;
  1028. }
  1029. static int cmppid(const void *a, const void *b)
  1030. {
  1031. return *(pid_t *)a - *(pid_t *)b;
  1032. }
  1033. /*
  1034. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1035. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1036. * count 'cnt' of how many chars would be written if buf were large enough.
  1037. */
  1038. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1039. {
  1040. int cnt = 0;
  1041. int i;
  1042. for (i = 0; i < npids; i++)
  1043. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1044. return cnt;
  1045. }
  1046. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1047. {
  1048. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1049. struct ctr_struct *ctr;
  1050. pid_t *pidarray;
  1051. int npids;
  1052. char c;
  1053. if (!(file->f_mode & FMODE_READ))
  1054. return 0;
  1055. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1056. if (!ctr)
  1057. goto err0;
  1058. /*
  1059. * If cpuset gets more users after we read count, we won't have
  1060. * enough space - tough. This race is indistinguishable to the
  1061. * caller from the case that the additional cpuset users didn't
  1062. * show up until sometime later on.
  1063. */
  1064. npids = atomic_read(&cs->count);
  1065. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1066. if (!pidarray)
  1067. goto err1;
  1068. npids = pid_array_load(pidarray, npids, cs);
  1069. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1070. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1071. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1072. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1073. if (!ctr->buf)
  1074. goto err2;
  1075. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1076. kfree(pidarray);
  1077. file->private_data = ctr;
  1078. return 0;
  1079. err2:
  1080. kfree(pidarray);
  1081. err1:
  1082. kfree(ctr);
  1083. err0:
  1084. return -ENOMEM;
  1085. }
  1086. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1087. size_t nbytes, loff_t *ppos)
  1088. {
  1089. struct ctr_struct *ctr = file->private_data;
  1090. if (*ppos + nbytes > ctr->bufsz)
  1091. nbytes = ctr->bufsz - *ppos;
  1092. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1093. return -EFAULT;
  1094. *ppos += nbytes;
  1095. return nbytes;
  1096. }
  1097. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1098. {
  1099. struct ctr_struct *ctr;
  1100. if (file->f_mode & FMODE_READ) {
  1101. ctr = file->private_data;
  1102. kfree(ctr->buf);
  1103. kfree(ctr);
  1104. }
  1105. return 0;
  1106. }
  1107. /*
  1108. * for the common functions, 'private' gives the type of file
  1109. */
  1110. static struct cftype cft_tasks = {
  1111. .name = "tasks",
  1112. .open = cpuset_tasks_open,
  1113. .read = cpuset_tasks_read,
  1114. .release = cpuset_tasks_release,
  1115. .private = FILE_TASKLIST,
  1116. };
  1117. static struct cftype cft_cpus = {
  1118. .name = "cpus",
  1119. .private = FILE_CPULIST,
  1120. };
  1121. static struct cftype cft_mems = {
  1122. .name = "mems",
  1123. .private = FILE_MEMLIST,
  1124. };
  1125. static struct cftype cft_cpu_exclusive = {
  1126. .name = "cpu_exclusive",
  1127. .private = FILE_CPU_EXCLUSIVE,
  1128. };
  1129. static struct cftype cft_mem_exclusive = {
  1130. .name = "mem_exclusive",
  1131. .private = FILE_MEM_EXCLUSIVE,
  1132. };
  1133. static struct cftype cft_notify_on_release = {
  1134. .name = "notify_on_release",
  1135. .private = FILE_NOTIFY_ON_RELEASE,
  1136. };
  1137. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1138. {
  1139. int err;
  1140. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1141. return err;
  1142. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1143. return err;
  1144. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1145. return err;
  1146. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1147. return err;
  1148. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1149. return err;
  1150. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1151. return err;
  1152. return 0;
  1153. }
  1154. /*
  1155. * cpuset_create - create a cpuset
  1156. * parent: cpuset that will be parent of the new cpuset.
  1157. * name: name of the new cpuset. Will be strcpy'ed.
  1158. * mode: mode to set on new inode
  1159. *
  1160. * Must be called with the semaphore on the parent inode held
  1161. */
  1162. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1163. {
  1164. struct cpuset *cs;
  1165. int err;
  1166. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1167. if (!cs)
  1168. return -ENOMEM;
  1169. down(&cpuset_sem);
  1170. refresh_mems();
  1171. cs->flags = 0;
  1172. if (notify_on_release(parent))
  1173. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1174. cs->cpus_allowed = CPU_MASK_NONE;
  1175. cs->mems_allowed = NODE_MASK_NONE;
  1176. atomic_set(&cs->count, 0);
  1177. INIT_LIST_HEAD(&cs->sibling);
  1178. INIT_LIST_HEAD(&cs->children);
  1179. atomic_inc(&cpuset_mems_generation);
  1180. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1181. cs->parent = parent;
  1182. list_add(&cs->sibling, &cs->parent->children);
  1183. err = cpuset_create_dir(cs, name, mode);
  1184. if (err < 0)
  1185. goto err;
  1186. /*
  1187. * Release cpuset_sem before cpuset_populate_dir() because it
  1188. * will down() this new directory's i_sem and if we race with
  1189. * another mkdir, we might deadlock.
  1190. */
  1191. up(&cpuset_sem);
  1192. err = cpuset_populate_dir(cs->dentry);
  1193. /* If err < 0, we have a half-filled directory - oh well ;) */
  1194. return 0;
  1195. err:
  1196. list_del(&cs->sibling);
  1197. up(&cpuset_sem);
  1198. kfree(cs);
  1199. return err;
  1200. }
  1201. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1202. {
  1203. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1204. /* the vfs holds inode->i_sem already */
  1205. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1206. }
  1207. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1208. {
  1209. struct cpuset *cs = dentry->d_fsdata;
  1210. struct dentry *d;
  1211. struct cpuset *parent;
  1212. char *pathbuf = NULL;
  1213. /* the vfs holds both inode->i_sem already */
  1214. down(&cpuset_sem);
  1215. refresh_mems();
  1216. if (atomic_read(&cs->count) > 0) {
  1217. up(&cpuset_sem);
  1218. return -EBUSY;
  1219. }
  1220. if (!list_empty(&cs->children)) {
  1221. up(&cpuset_sem);
  1222. return -EBUSY;
  1223. }
  1224. parent = cs->parent;
  1225. set_bit(CS_REMOVED, &cs->flags);
  1226. if (is_cpu_exclusive(cs))
  1227. update_cpu_domains(cs);
  1228. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1229. if (list_empty(&parent->children))
  1230. check_for_release(parent, &pathbuf);
  1231. spin_lock(&cs->dentry->d_lock);
  1232. d = dget(cs->dentry);
  1233. cs->dentry = NULL;
  1234. spin_unlock(&d->d_lock);
  1235. cpuset_d_remove_dir(d);
  1236. dput(d);
  1237. up(&cpuset_sem);
  1238. cpuset_release_agent(pathbuf);
  1239. return 0;
  1240. }
  1241. /**
  1242. * cpuset_init - initialize cpusets at system boot
  1243. *
  1244. * Description: Initialize top_cpuset and the cpuset internal file system,
  1245. **/
  1246. int __init cpuset_init(void)
  1247. {
  1248. struct dentry *root;
  1249. int err;
  1250. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1251. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1252. atomic_inc(&cpuset_mems_generation);
  1253. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1254. init_task.cpuset = &top_cpuset;
  1255. err = register_filesystem(&cpuset_fs_type);
  1256. if (err < 0)
  1257. goto out;
  1258. cpuset_mount = kern_mount(&cpuset_fs_type);
  1259. if (IS_ERR(cpuset_mount)) {
  1260. printk(KERN_ERR "cpuset: could not mount!\n");
  1261. err = PTR_ERR(cpuset_mount);
  1262. cpuset_mount = NULL;
  1263. goto out;
  1264. }
  1265. root = cpuset_mount->mnt_sb->s_root;
  1266. root->d_fsdata = &top_cpuset;
  1267. root->d_inode->i_nlink++;
  1268. top_cpuset.dentry = root;
  1269. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1270. err = cpuset_populate_dir(root);
  1271. out:
  1272. return err;
  1273. }
  1274. /**
  1275. * cpuset_init_smp - initialize cpus_allowed
  1276. *
  1277. * Description: Finish top cpuset after cpu, node maps are initialized
  1278. **/
  1279. void __init cpuset_init_smp(void)
  1280. {
  1281. top_cpuset.cpus_allowed = cpu_online_map;
  1282. top_cpuset.mems_allowed = node_online_map;
  1283. }
  1284. /**
  1285. * cpuset_fork - attach newly forked task to its parents cpuset.
  1286. * @tsk: pointer to task_struct of forking parent process.
  1287. *
  1288. * Description: By default, on fork, a task inherits its
  1289. * parent's cpuset. The pointer to the shared cpuset is
  1290. * automatically copied in fork.c by dup_task_struct().
  1291. * This cpuset_fork() routine need only increment the usage
  1292. * counter in that cpuset.
  1293. **/
  1294. void cpuset_fork(struct task_struct *tsk)
  1295. {
  1296. atomic_inc(&tsk->cpuset->count);
  1297. }
  1298. /**
  1299. * cpuset_exit - detach cpuset from exiting task
  1300. * @tsk: pointer to task_struct of exiting process
  1301. *
  1302. * Description: Detach cpuset from @tsk and release it.
  1303. *
  1304. * Note that cpusets marked notify_on_release force every task
  1305. * in them to take the global cpuset_sem semaphore when exiting.
  1306. * This could impact scaling on very large systems. Be reluctant
  1307. * to use notify_on_release cpusets where very high task exit
  1308. * scaling is required on large systems.
  1309. *
  1310. * Don't even think about derefencing 'cs' after the cpuset use
  1311. * count goes to zero, except inside a critical section guarded
  1312. * by the cpuset_sem semaphore. If you don't hold cpuset_sem,
  1313. * then a zero cpuset use count is a license to any other task to
  1314. * nuke the cpuset immediately.
  1315. **/
  1316. void cpuset_exit(struct task_struct *tsk)
  1317. {
  1318. struct cpuset *cs;
  1319. task_lock(tsk);
  1320. cs = tsk->cpuset;
  1321. tsk->cpuset = NULL;
  1322. task_unlock(tsk);
  1323. if (notify_on_release(cs)) {
  1324. char *pathbuf = NULL;
  1325. down(&cpuset_sem);
  1326. if (atomic_dec_and_test(&cs->count))
  1327. check_for_release(cs, &pathbuf);
  1328. up(&cpuset_sem);
  1329. cpuset_release_agent(pathbuf);
  1330. } else {
  1331. atomic_dec(&cs->count);
  1332. }
  1333. }
  1334. /**
  1335. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1336. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1337. *
  1338. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1339. * attached to the specified @tsk. Guaranteed to return some non-empty
  1340. * subset of cpu_online_map, even if this means going outside the
  1341. * tasks cpuset.
  1342. **/
  1343. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1344. {
  1345. cpumask_t mask;
  1346. down(&cpuset_sem);
  1347. task_lock((struct task_struct *)tsk);
  1348. guarantee_online_cpus(tsk->cpuset, &mask);
  1349. task_unlock((struct task_struct *)tsk);
  1350. up(&cpuset_sem);
  1351. return mask;
  1352. }
  1353. void cpuset_init_current_mems_allowed(void)
  1354. {
  1355. current->mems_allowed = NODE_MASK_ALL;
  1356. }
  1357. /**
  1358. * cpuset_update_current_mems_allowed - update mems parameters to new values
  1359. *
  1360. * If the current tasks cpusets mems_allowed changed behind our backs,
  1361. * update current->mems_allowed and mems_generation to the new value.
  1362. * Do not call this routine if in_interrupt().
  1363. */
  1364. void cpuset_update_current_mems_allowed(void)
  1365. {
  1366. struct cpuset *cs = current->cpuset;
  1367. if (!cs)
  1368. return; /* task is exiting */
  1369. if (current->cpuset_mems_generation != cs->mems_generation) {
  1370. down(&cpuset_sem);
  1371. refresh_mems();
  1372. up(&cpuset_sem);
  1373. }
  1374. }
  1375. /**
  1376. * cpuset_restrict_to_mems_allowed - limit nodes to current mems_allowed
  1377. * @nodes: pointer to a node bitmap that is and-ed with mems_allowed
  1378. */
  1379. void cpuset_restrict_to_mems_allowed(unsigned long *nodes)
  1380. {
  1381. bitmap_and(nodes, nodes, nodes_addr(current->mems_allowed),
  1382. MAX_NUMNODES);
  1383. }
  1384. /**
  1385. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1386. * @zl: the zonelist to be checked
  1387. *
  1388. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1389. */
  1390. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1391. {
  1392. int i;
  1393. for (i = 0; zl->zones[i]; i++) {
  1394. int nid = zl->zones[i]->zone_pgdat->node_id;
  1395. if (node_isset(nid, current->mems_allowed))
  1396. return 1;
  1397. }
  1398. return 0;
  1399. }
  1400. /**
  1401. * cpuset_zone_allowed - is zone z allowed in current->mems_allowed
  1402. * @z: zone in question
  1403. *
  1404. * Is zone z allowed in current->mems_allowed, or is
  1405. * the CPU in interrupt context? (zone is always allowed in this case)
  1406. */
  1407. int cpuset_zone_allowed(struct zone *z)
  1408. {
  1409. return in_interrupt() ||
  1410. node_isset(z->zone_pgdat->node_id, current->mems_allowed);
  1411. }
  1412. /*
  1413. * proc_cpuset_show()
  1414. * - Print tasks cpuset path into seq_file.
  1415. * - Used for /proc/<pid>/cpuset.
  1416. */
  1417. static int proc_cpuset_show(struct seq_file *m, void *v)
  1418. {
  1419. struct cpuset *cs;
  1420. struct task_struct *tsk;
  1421. char *buf;
  1422. int retval = 0;
  1423. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1424. if (!buf)
  1425. return -ENOMEM;
  1426. tsk = m->private;
  1427. down(&cpuset_sem);
  1428. task_lock(tsk);
  1429. cs = tsk->cpuset;
  1430. task_unlock(tsk);
  1431. if (!cs) {
  1432. retval = -EINVAL;
  1433. goto out;
  1434. }
  1435. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1436. if (retval < 0)
  1437. goto out;
  1438. seq_puts(m, buf);
  1439. seq_putc(m, '\n');
  1440. out:
  1441. up(&cpuset_sem);
  1442. kfree(buf);
  1443. return retval;
  1444. }
  1445. static int cpuset_open(struct inode *inode, struct file *file)
  1446. {
  1447. struct task_struct *tsk = PROC_I(inode)->task;
  1448. return single_open(file, proc_cpuset_show, tsk);
  1449. }
  1450. struct file_operations proc_cpuset_operations = {
  1451. .open = cpuset_open,
  1452. .read = seq_read,
  1453. .llseek = seq_lseek,
  1454. .release = single_release,
  1455. };
  1456. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1457. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1458. {
  1459. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1460. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1461. buffer += sprintf(buffer, "\n");
  1462. buffer += sprintf(buffer, "Mems_allowed:\t");
  1463. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1464. buffer += sprintf(buffer, "\n");
  1465. return buffer;
  1466. }