cpuset.c 70 KB

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