cpuset.c 48 KB

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