cpuset.c 64 KB

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