cpuset.c 63 KB

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