cpuset.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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
  554. * called with or without manage_mutex held. Thanks in part to
  555. * 'the_top_cpuset_hack', the tasks cpuset pointer will never
  556. * be NULL. This routine also might acquire callback_mutex and
  557. * current->mm->mmap_sem during call.
  558. *
  559. * Reading current->cpuset->mems_generation doesn't need task_lock
  560. * to guard the current->cpuset derefence, because it is guarded
  561. * from concurrent freeing of current->cpuset by attach_task(),
  562. * using RCU.
  563. *
  564. * The rcu_dereference() is technically probably not needed,
  565. * as I don't actually mind if I see a new cpuset pointer but
  566. * an old value of mems_generation. However this really only
  567. * matters on alpha systems using cpusets heavily. If I dropped
  568. * that rcu_dereference(), it would save them a memory barrier.
  569. * For all other arch's, rcu_dereference is a no-op anyway, and for
  570. * alpha systems not using cpusets, another planned optimization,
  571. * avoiding the rcu critical section for tasks in the root cpuset
  572. * which is statically allocated, so can't vanish, will make this
  573. * irrelevant. Better to use RCU as intended, than to engage in
  574. * some cute trick to save a memory barrier that is impossible to
  575. * test, for alpha systems using cpusets heavily, which might not
  576. * even exist.
  577. *
  578. * This routine is needed to update the per-task mems_allowed data,
  579. * within the tasks context, when it is trying to allocate memory
  580. * (in various mm/mempolicy.c routines) and notices that some other
  581. * task has been modifying its cpuset.
  582. */
  583. void cpuset_update_task_memory_state(void)
  584. {
  585. int my_cpusets_mem_gen;
  586. struct task_struct *tsk = current;
  587. struct cpuset *cs;
  588. if (tsk->cpuset == &top_cpuset) {
  589. /* Don't need rcu for top_cpuset. It's never freed. */
  590. my_cpusets_mem_gen = top_cpuset.mems_generation;
  591. } else {
  592. rcu_read_lock();
  593. cs = rcu_dereference(tsk->cpuset);
  594. my_cpusets_mem_gen = cs->mems_generation;
  595. rcu_read_unlock();
  596. }
  597. if (my_cpusets_mem_gen != tsk->cpuset_mems_generation) {
  598. mutex_lock(&callback_mutex);
  599. task_lock(tsk);
  600. cs = tsk->cpuset; /* Maybe changed when task not locked */
  601. guarantee_online_mems(cs, &tsk->mems_allowed);
  602. tsk->cpuset_mems_generation = cs->mems_generation;
  603. if (is_spread_page(cs))
  604. tsk->flags |= PF_SPREAD_PAGE;
  605. else
  606. tsk->flags &= ~PF_SPREAD_PAGE;
  607. if (is_spread_slab(cs))
  608. tsk->flags |= PF_SPREAD_SLAB;
  609. else
  610. tsk->flags &= ~PF_SPREAD_SLAB;
  611. task_unlock(tsk);
  612. mutex_unlock(&callback_mutex);
  613. mpol_rebind_task(tsk, &tsk->mems_allowed);
  614. }
  615. }
  616. /*
  617. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  618. *
  619. * One cpuset is a subset of another if all its allowed CPUs and
  620. * Memory Nodes are a subset of the other, and its exclusive flags
  621. * are only set if the other's are set. Call holding manage_mutex.
  622. */
  623. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  624. {
  625. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  626. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  627. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  628. is_mem_exclusive(p) <= is_mem_exclusive(q);
  629. }
  630. /*
  631. * validate_change() - Used to validate that any proposed cpuset change
  632. * follows the structural rules for cpusets.
  633. *
  634. * If we replaced the flag and mask values of the current cpuset
  635. * (cur) with those values in the trial cpuset (trial), would
  636. * our various subset and exclusive rules still be valid? Presumes
  637. * manage_mutex held.
  638. *
  639. * 'cur' is the address of an actual, in-use cpuset. Operations
  640. * such as list traversal that depend on the actual address of the
  641. * cpuset in the list must use cur below, not trial.
  642. *
  643. * 'trial' is the address of bulk structure copy of cur, with
  644. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  645. * or flags changed to new, trial values.
  646. *
  647. * Return 0 if valid, -errno if not.
  648. */
  649. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  650. {
  651. struct cpuset *c, *par;
  652. /* Each of our child cpusets must be a subset of us */
  653. list_for_each_entry(c, &cur->children, sibling) {
  654. if (!is_cpuset_subset(c, trial))
  655. return -EBUSY;
  656. }
  657. /* Remaining checks don't apply to root cpuset */
  658. if ((par = cur->parent) == NULL)
  659. return 0;
  660. /* We must be a subset of our parent cpuset */
  661. if (!is_cpuset_subset(trial, par))
  662. return -EACCES;
  663. /* If either I or some sibling (!= me) is exclusive, we can't overlap */
  664. list_for_each_entry(c, &par->children, sibling) {
  665. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  666. c != cur &&
  667. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  668. return -EINVAL;
  669. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  670. c != cur &&
  671. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  672. return -EINVAL;
  673. }
  674. return 0;
  675. }
  676. /*
  677. * For a given cpuset cur, partition the system as follows
  678. * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
  679. * exclusive child cpusets
  680. * b. All cpus in the current cpuset's cpus_allowed that are not part of any
  681. * exclusive child cpusets
  682. * Build these two partitions by calling partition_sched_domains
  683. *
  684. * Call with manage_mutex held. May nest a call to the
  685. * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
  686. */
  687. static void update_cpu_domains(struct cpuset *cur)
  688. {
  689. struct cpuset *c, *par = cur->parent;
  690. cpumask_t pspan, cspan;
  691. if (par == NULL || cpus_empty(cur->cpus_allowed))
  692. return;
  693. /*
  694. * Get all cpus from parent's cpus_allowed not part of exclusive
  695. * children
  696. */
  697. pspan = par->cpus_allowed;
  698. list_for_each_entry(c, &par->children, sibling) {
  699. if (is_cpu_exclusive(c))
  700. cpus_andnot(pspan, pspan, c->cpus_allowed);
  701. }
  702. if (is_removed(cur) || !is_cpu_exclusive(cur)) {
  703. cpus_or(pspan, pspan, cur->cpus_allowed);
  704. if (cpus_equal(pspan, cur->cpus_allowed))
  705. return;
  706. cspan = CPU_MASK_NONE;
  707. } else {
  708. if (cpus_empty(pspan))
  709. return;
  710. cspan = cur->cpus_allowed;
  711. /*
  712. * Get all cpus from current cpuset's cpus_allowed not part
  713. * of exclusive children
  714. */
  715. list_for_each_entry(c, &cur->children, sibling) {
  716. if (is_cpu_exclusive(c))
  717. cpus_andnot(cspan, cspan, c->cpus_allowed);
  718. }
  719. }
  720. lock_cpu_hotplug();
  721. partition_sched_domains(&pspan, &cspan);
  722. unlock_cpu_hotplug();
  723. }
  724. /*
  725. * Call with manage_mutex held. May take callback_mutex during call.
  726. */
  727. static int update_cpumask(struct cpuset *cs, char *buf)
  728. {
  729. struct cpuset trialcs;
  730. int retval, cpus_unchanged;
  731. trialcs = *cs;
  732. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  733. if (retval < 0)
  734. return retval;
  735. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  736. if (cpus_empty(trialcs.cpus_allowed))
  737. return -ENOSPC;
  738. retval = validate_change(cs, &trialcs);
  739. if (retval < 0)
  740. return retval;
  741. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  742. mutex_lock(&callback_mutex);
  743. cs->cpus_allowed = trialcs.cpus_allowed;
  744. mutex_unlock(&callback_mutex);
  745. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  746. update_cpu_domains(cs);
  747. return 0;
  748. }
  749. /*
  750. * cpuset_migrate_mm
  751. *
  752. * Migrate memory region from one set of nodes to another.
  753. *
  754. * Temporarilly set tasks mems_allowed to target nodes of migration,
  755. * so that the migration code can allocate pages on these nodes.
  756. *
  757. * Call holding manage_mutex, so our current->cpuset won't change
  758. * during this call, as manage_mutex holds off any attach_task()
  759. * calls. Therefore we don't need to take task_lock around the
  760. * call to guarantee_online_mems(), as we know no one is changing
  761. * our tasks cpuset.
  762. *
  763. * Hold callback_mutex around the two modifications of our tasks
  764. * mems_allowed to synchronize with cpuset_mems_allowed().
  765. *
  766. * While the mm_struct we are migrating is typically from some
  767. * other task, the task_struct mems_allowed that we are hacking
  768. * is for our current task, which must allocate new pages for that
  769. * migrating memory region.
  770. *
  771. * We call cpuset_update_task_memory_state() before hacking
  772. * our tasks mems_allowed, so that we are assured of being in
  773. * sync with our tasks cpuset, and in particular, callbacks to
  774. * cpuset_update_task_memory_state() from nested page allocations
  775. * won't see any mismatch of our cpuset and task mems_generation
  776. * values, so won't overwrite our hacked tasks mems_allowed
  777. * nodemask.
  778. */
  779. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  780. const nodemask_t *to)
  781. {
  782. struct task_struct *tsk = current;
  783. cpuset_update_task_memory_state();
  784. mutex_lock(&callback_mutex);
  785. tsk->mems_allowed = *to;
  786. mutex_unlock(&callback_mutex);
  787. do_migrate_pages(mm, from, to, MPOL_MF_MOVE_ALL);
  788. mutex_lock(&callback_mutex);
  789. guarantee_online_mems(tsk->cpuset, &tsk->mems_allowed);
  790. mutex_unlock(&callback_mutex);
  791. }
  792. /*
  793. * Handle user request to change the 'mems' memory placement
  794. * of a cpuset. Needs to validate the request, update the
  795. * cpusets mems_allowed and mems_generation, and for each
  796. * task in the cpuset, rebind any vma mempolicies and if
  797. * the cpuset is marked 'memory_migrate', migrate the tasks
  798. * pages to the new memory.
  799. *
  800. * Call with manage_mutex held. May take callback_mutex during call.
  801. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  802. * lock each such tasks mm->mmap_sem, scan its vma's and rebind
  803. * their mempolicies to the cpusets new mems_allowed.
  804. */
  805. static int update_nodemask(struct cpuset *cs, char *buf)
  806. {
  807. struct cpuset trialcs;
  808. nodemask_t oldmem;
  809. struct task_struct *g, *p;
  810. struct mm_struct **mmarray;
  811. int i, n, ntasks;
  812. int migrate;
  813. int fudge;
  814. int retval;
  815. trialcs = *cs;
  816. retval = nodelist_parse(buf, trialcs.mems_allowed);
  817. if (retval < 0)
  818. goto done;
  819. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  820. oldmem = cs->mems_allowed;
  821. if (nodes_equal(oldmem, trialcs.mems_allowed)) {
  822. retval = 0; /* Too easy - nothing to do */
  823. goto done;
  824. }
  825. if (nodes_empty(trialcs.mems_allowed)) {
  826. retval = -ENOSPC;
  827. goto done;
  828. }
  829. retval = validate_change(cs, &trialcs);
  830. if (retval < 0)
  831. goto done;
  832. mutex_lock(&callback_mutex);
  833. cs->mems_allowed = trialcs.mems_allowed;
  834. cs->mems_generation = cpuset_mems_generation++;
  835. mutex_unlock(&callback_mutex);
  836. set_cpuset_being_rebound(cs); /* causes mpol_copy() rebind */
  837. fudge = 10; /* spare mmarray[] slots */
  838. fudge += cpus_weight(cs->cpus_allowed); /* imagine one fork-bomb/cpu */
  839. retval = -ENOMEM;
  840. /*
  841. * Allocate mmarray[] to hold mm reference for each task
  842. * in cpuset cs. Can't kmalloc GFP_KERNEL while holding
  843. * tasklist_lock. We could use GFP_ATOMIC, but with a
  844. * few more lines of code, we can retry until we get a big
  845. * enough mmarray[] w/o using GFP_ATOMIC.
  846. */
  847. while (1) {
  848. ntasks = atomic_read(&cs->count); /* guess */
  849. ntasks += fudge;
  850. mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
  851. if (!mmarray)
  852. goto done;
  853. write_lock_irq(&tasklist_lock); /* block fork */
  854. if (atomic_read(&cs->count) <= ntasks)
  855. break; /* got enough */
  856. write_unlock_irq(&tasklist_lock); /* try again */
  857. kfree(mmarray);
  858. }
  859. n = 0;
  860. /* Load up mmarray[] with mm reference for each task in cpuset. */
  861. do_each_thread(g, p) {
  862. struct mm_struct *mm;
  863. if (n >= ntasks) {
  864. printk(KERN_WARNING
  865. "Cpuset mempolicy rebind incomplete.\n");
  866. continue;
  867. }
  868. if (p->cpuset != cs)
  869. continue;
  870. mm = get_task_mm(p);
  871. if (!mm)
  872. continue;
  873. mmarray[n++] = mm;
  874. } while_each_thread(g, p);
  875. write_unlock_irq(&tasklist_lock);
  876. /*
  877. * Now that we've dropped the tasklist spinlock, we can
  878. * rebind the vma mempolicies of each mm in mmarray[] to their
  879. * new cpuset, and release that mm. The mpol_rebind_mm()
  880. * call takes mmap_sem, which we couldn't take while holding
  881. * tasklist_lock. Forks can happen again now - the mpol_copy()
  882. * cpuset_being_rebound check will catch such forks, and rebind
  883. * their vma mempolicies too. Because we still hold the global
  884. * cpuset manage_mutex, we know that no other rebind effort will
  885. * be contending for the global variable cpuset_being_rebound.
  886. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  887. * is idempotent. Also migrate pages in each mm to new nodes.
  888. */
  889. migrate = is_memory_migrate(cs);
  890. for (i = 0; i < n; i++) {
  891. struct mm_struct *mm = mmarray[i];
  892. mpol_rebind_mm(mm, &cs->mems_allowed);
  893. if (migrate)
  894. cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
  895. mmput(mm);
  896. }
  897. /* We're done rebinding vma's to this cpusets new mems_allowed. */
  898. kfree(mmarray);
  899. set_cpuset_being_rebound(NULL);
  900. retval = 0;
  901. done:
  902. return retval;
  903. }
  904. /*
  905. * Call with manage_mutex held.
  906. */
  907. static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
  908. {
  909. if (simple_strtoul(buf, NULL, 10) != 0)
  910. cpuset_memory_pressure_enabled = 1;
  911. else
  912. cpuset_memory_pressure_enabled = 0;
  913. return 0;
  914. }
  915. /*
  916. * update_flag - read a 0 or a 1 in a file and update associated flag
  917. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  918. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE,
  919. * CS_SPREAD_PAGE, CS_SPREAD_SLAB)
  920. * cs: the cpuset to update
  921. * buf: the buffer where we read the 0 or 1
  922. *
  923. * Call with manage_mutex held.
  924. */
  925. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  926. {
  927. int turning_on;
  928. struct cpuset trialcs;
  929. int err, cpu_exclusive_changed;
  930. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  931. trialcs = *cs;
  932. if (turning_on)
  933. set_bit(bit, &trialcs.flags);
  934. else
  935. clear_bit(bit, &trialcs.flags);
  936. err = validate_change(cs, &trialcs);
  937. if (err < 0)
  938. return err;
  939. cpu_exclusive_changed =
  940. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  941. mutex_lock(&callback_mutex);
  942. if (turning_on)
  943. set_bit(bit, &cs->flags);
  944. else
  945. clear_bit(bit, &cs->flags);
  946. mutex_unlock(&callback_mutex);
  947. if (cpu_exclusive_changed)
  948. update_cpu_domains(cs);
  949. return 0;
  950. }
  951. /*
  952. * Frequency meter - How fast is some event occuring?
  953. *
  954. * These routines manage a digitally filtered, constant time based,
  955. * event frequency meter. There are four routines:
  956. * fmeter_init() - initialize a frequency meter.
  957. * fmeter_markevent() - called each time the event happens.
  958. * fmeter_getrate() - returns the recent rate of such events.
  959. * fmeter_update() - internal routine used to update fmeter.
  960. *
  961. * A common data structure is passed to each of these routines,
  962. * which is used to keep track of the state required to manage the
  963. * frequency meter and its digital filter.
  964. *
  965. * The filter works on the number of events marked per unit time.
  966. * The filter is single-pole low-pass recursive (IIR). The time unit
  967. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  968. * simulate 3 decimal digits of precision (multiplied by 1000).
  969. *
  970. * With an FM_COEF of 933, and a time base of 1 second, the filter
  971. * has a half-life of 10 seconds, meaning that if the events quit
  972. * happening, then the rate returned from the fmeter_getrate()
  973. * will be cut in half each 10 seconds, until it converges to zero.
  974. *
  975. * It is not worth doing a real infinitely recursive filter. If more
  976. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  977. * just compute FM_MAXTICKS ticks worth, by which point the level
  978. * will be stable.
  979. *
  980. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  981. * arithmetic overflow in the fmeter_update() routine.
  982. *
  983. * Given the simple 32 bit integer arithmetic used, this meter works
  984. * best for reporting rates between one per millisecond (msec) and
  985. * one per 32 (approx) seconds. At constant rates faster than one
  986. * per msec it maxes out at values just under 1,000,000. At constant
  987. * rates between one per msec, and one per second it will stabilize
  988. * to a value N*1000, where N is the rate of events per second.
  989. * At constant rates between one per second and one per 32 seconds,
  990. * it will be choppy, moving up on the seconds that have an event,
  991. * and then decaying until the next event. At rates slower than
  992. * about one in 32 seconds, it decays all the way back to zero between
  993. * each event.
  994. */
  995. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  996. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  997. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  998. #define FM_SCALE 1000 /* faux fixed point scale */
  999. /* Initialize a frequency meter */
  1000. static void fmeter_init(struct fmeter *fmp)
  1001. {
  1002. fmp->cnt = 0;
  1003. fmp->val = 0;
  1004. fmp->time = 0;
  1005. spin_lock_init(&fmp->lock);
  1006. }
  1007. /* Internal meter update - process cnt events and update value */
  1008. static void fmeter_update(struct fmeter *fmp)
  1009. {
  1010. time_t now = get_seconds();
  1011. time_t ticks = now - fmp->time;
  1012. if (ticks == 0)
  1013. return;
  1014. ticks = min(FM_MAXTICKS, ticks);
  1015. while (ticks-- > 0)
  1016. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  1017. fmp->time = now;
  1018. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  1019. fmp->cnt = 0;
  1020. }
  1021. /* Process any previous ticks, then bump cnt by one (times scale). */
  1022. static void fmeter_markevent(struct fmeter *fmp)
  1023. {
  1024. spin_lock(&fmp->lock);
  1025. fmeter_update(fmp);
  1026. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  1027. spin_unlock(&fmp->lock);
  1028. }
  1029. /* Process any previous ticks, then return current value. */
  1030. static int fmeter_getrate(struct fmeter *fmp)
  1031. {
  1032. int val;
  1033. spin_lock(&fmp->lock);
  1034. fmeter_update(fmp);
  1035. val = fmp->val;
  1036. spin_unlock(&fmp->lock);
  1037. return val;
  1038. }
  1039. /*
  1040. * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
  1041. * writing the path of the old cpuset in 'ppathbuf' if it needs to be
  1042. * notified on release.
  1043. *
  1044. * Call holding manage_mutex. May take callback_mutex and task_lock of
  1045. * the task 'pid' during call.
  1046. */
  1047. static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
  1048. {
  1049. pid_t pid;
  1050. struct task_struct *tsk;
  1051. struct cpuset *oldcs;
  1052. cpumask_t cpus;
  1053. nodemask_t from, to;
  1054. struct mm_struct *mm;
  1055. if (sscanf(pidbuf, "%d", &pid) != 1)
  1056. return -EIO;
  1057. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  1058. return -ENOSPC;
  1059. if (pid) {
  1060. read_lock(&tasklist_lock);
  1061. tsk = find_task_by_pid(pid);
  1062. if (!tsk || tsk->flags & PF_EXITING) {
  1063. read_unlock(&tasklist_lock);
  1064. return -ESRCH;
  1065. }
  1066. get_task_struct(tsk);
  1067. read_unlock(&tasklist_lock);
  1068. if ((current->euid) && (current->euid != tsk->uid)
  1069. && (current->euid != tsk->suid)) {
  1070. put_task_struct(tsk);
  1071. return -EACCES;
  1072. }
  1073. } else {
  1074. tsk = current;
  1075. get_task_struct(tsk);
  1076. }
  1077. mutex_lock(&callback_mutex);
  1078. task_lock(tsk);
  1079. oldcs = tsk->cpuset;
  1080. if (!oldcs) {
  1081. task_unlock(tsk);
  1082. mutex_unlock(&callback_mutex);
  1083. put_task_struct(tsk);
  1084. return -ESRCH;
  1085. }
  1086. atomic_inc(&cs->count);
  1087. rcu_assign_pointer(tsk->cpuset, cs);
  1088. task_unlock(tsk);
  1089. guarantee_online_cpus(cs, &cpus);
  1090. set_cpus_allowed(tsk, cpus);
  1091. from = oldcs->mems_allowed;
  1092. to = cs->mems_allowed;
  1093. mutex_unlock(&callback_mutex);
  1094. mm = get_task_mm(tsk);
  1095. if (mm) {
  1096. mpol_rebind_mm(mm, &to);
  1097. if (is_memory_migrate(cs))
  1098. cpuset_migrate_mm(mm, &from, &to);
  1099. mmput(mm);
  1100. }
  1101. put_task_struct(tsk);
  1102. synchronize_rcu();
  1103. if (atomic_dec_and_test(&oldcs->count))
  1104. check_for_release(oldcs, ppathbuf);
  1105. return 0;
  1106. }
  1107. /* The various types of files and directories in a cpuset file system */
  1108. typedef enum {
  1109. FILE_ROOT,
  1110. FILE_DIR,
  1111. FILE_MEMORY_MIGRATE,
  1112. FILE_CPULIST,
  1113. FILE_MEMLIST,
  1114. FILE_CPU_EXCLUSIVE,
  1115. FILE_MEM_EXCLUSIVE,
  1116. FILE_NOTIFY_ON_RELEASE,
  1117. FILE_MEMORY_PRESSURE_ENABLED,
  1118. FILE_MEMORY_PRESSURE,
  1119. FILE_SPREAD_PAGE,
  1120. FILE_SPREAD_SLAB,
  1121. FILE_TASKLIST,
  1122. } cpuset_filetype_t;
  1123. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  1124. size_t nbytes, loff_t *unused_ppos)
  1125. {
  1126. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1127. struct cftype *cft = __d_cft(file->f_dentry);
  1128. cpuset_filetype_t type = cft->private;
  1129. char *buffer;
  1130. char *pathbuf = NULL;
  1131. int retval = 0;
  1132. /* Crude upper limit on largest legitimate cpulist user might write. */
  1133. if (nbytes > 100 + 6 * NR_CPUS)
  1134. return -E2BIG;
  1135. /* +1 for nul-terminator */
  1136. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  1137. return -ENOMEM;
  1138. if (copy_from_user(buffer, userbuf, nbytes)) {
  1139. retval = -EFAULT;
  1140. goto out1;
  1141. }
  1142. buffer[nbytes] = 0; /* nul-terminate */
  1143. mutex_lock(&manage_mutex);
  1144. if (is_removed(cs)) {
  1145. retval = -ENODEV;
  1146. goto out2;
  1147. }
  1148. switch (type) {
  1149. case FILE_CPULIST:
  1150. retval = update_cpumask(cs, buffer);
  1151. break;
  1152. case FILE_MEMLIST:
  1153. retval = update_nodemask(cs, buffer);
  1154. break;
  1155. case FILE_CPU_EXCLUSIVE:
  1156. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  1157. break;
  1158. case FILE_MEM_EXCLUSIVE:
  1159. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  1160. break;
  1161. case FILE_NOTIFY_ON_RELEASE:
  1162. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  1163. break;
  1164. case FILE_MEMORY_MIGRATE:
  1165. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  1166. break;
  1167. case FILE_MEMORY_PRESSURE_ENABLED:
  1168. retval = update_memory_pressure_enabled(cs, buffer);
  1169. break;
  1170. case FILE_MEMORY_PRESSURE:
  1171. retval = -EACCES;
  1172. break;
  1173. case FILE_SPREAD_PAGE:
  1174. retval = update_flag(CS_SPREAD_PAGE, cs, buffer);
  1175. cs->mems_generation = cpuset_mems_generation++;
  1176. break;
  1177. case FILE_SPREAD_SLAB:
  1178. retval = update_flag(CS_SPREAD_SLAB, cs, buffer);
  1179. cs->mems_generation = cpuset_mems_generation++;
  1180. break;
  1181. case FILE_TASKLIST:
  1182. retval = attach_task(cs, buffer, &pathbuf);
  1183. break;
  1184. default:
  1185. retval = -EINVAL;
  1186. goto out2;
  1187. }
  1188. if (retval == 0)
  1189. retval = nbytes;
  1190. out2:
  1191. mutex_unlock(&manage_mutex);
  1192. cpuset_release_agent(pathbuf);
  1193. out1:
  1194. kfree(buffer);
  1195. return retval;
  1196. }
  1197. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  1198. size_t nbytes, loff_t *ppos)
  1199. {
  1200. ssize_t retval = 0;
  1201. struct cftype *cft = __d_cft(file->f_dentry);
  1202. if (!cft)
  1203. return -ENODEV;
  1204. /* special function ? */
  1205. if (cft->write)
  1206. retval = cft->write(file, buf, nbytes, ppos);
  1207. else
  1208. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  1209. return retval;
  1210. }
  1211. /*
  1212. * These ascii lists should be read in a single call, by using a user
  1213. * buffer large enough to hold the entire map. If read in smaller
  1214. * chunks, there is no guarantee of atomicity. Since the display format
  1215. * used, list of ranges of sequential numbers, is variable length,
  1216. * and since these maps can change value dynamically, one could read
  1217. * gibberish by doing partial reads while a list was changing.
  1218. * A single large read to a buffer that crosses a page boundary is
  1219. * ok, because the result being copied to user land is not recomputed
  1220. * across a page fault.
  1221. */
  1222. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1223. {
  1224. cpumask_t mask;
  1225. mutex_lock(&callback_mutex);
  1226. mask = cs->cpus_allowed;
  1227. mutex_unlock(&callback_mutex);
  1228. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  1229. }
  1230. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1231. {
  1232. nodemask_t mask;
  1233. mutex_lock(&callback_mutex);
  1234. mask = cs->mems_allowed;
  1235. mutex_unlock(&callback_mutex);
  1236. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  1237. }
  1238. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  1239. size_t nbytes, loff_t *ppos)
  1240. {
  1241. struct cftype *cft = __d_cft(file->f_dentry);
  1242. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1243. cpuset_filetype_t type = cft->private;
  1244. char *page;
  1245. ssize_t retval = 0;
  1246. char *s;
  1247. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  1248. return -ENOMEM;
  1249. s = page;
  1250. switch (type) {
  1251. case FILE_CPULIST:
  1252. s += cpuset_sprintf_cpulist(s, cs);
  1253. break;
  1254. case FILE_MEMLIST:
  1255. s += cpuset_sprintf_memlist(s, cs);
  1256. break;
  1257. case FILE_CPU_EXCLUSIVE:
  1258. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  1259. break;
  1260. case FILE_MEM_EXCLUSIVE:
  1261. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  1262. break;
  1263. case FILE_NOTIFY_ON_RELEASE:
  1264. *s++ = notify_on_release(cs) ? '1' : '0';
  1265. break;
  1266. case FILE_MEMORY_MIGRATE:
  1267. *s++ = is_memory_migrate(cs) ? '1' : '0';
  1268. break;
  1269. case FILE_MEMORY_PRESSURE_ENABLED:
  1270. *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
  1271. break;
  1272. case FILE_MEMORY_PRESSURE:
  1273. s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
  1274. break;
  1275. case FILE_SPREAD_PAGE:
  1276. *s++ = is_spread_page(cs) ? '1' : '0';
  1277. break;
  1278. case FILE_SPREAD_SLAB:
  1279. *s++ = is_spread_slab(cs) ? '1' : '0';
  1280. break;
  1281. default:
  1282. retval = -EINVAL;
  1283. goto out;
  1284. }
  1285. *s++ = '\n';
  1286. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1287. out:
  1288. free_page((unsigned long)page);
  1289. return retval;
  1290. }
  1291. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  1292. loff_t *ppos)
  1293. {
  1294. ssize_t retval = 0;
  1295. struct cftype *cft = __d_cft(file->f_dentry);
  1296. if (!cft)
  1297. return -ENODEV;
  1298. /* special function ? */
  1299. if (cft->read)
  1300. retval = cft->read(file, buf, nbytes, ppos);
  1301. else
  1302. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  1303. return retval;
  1304. }
  1305. static int cpuset_file_open(struct inode *inode, struct file *file)
  1306. {
  1307. int err;
  1308. struct cftype *cft;
  1309. err = generic_file_open(inode, file);
  1310. if (err)
  1311. return err;
  1312. cft = __d_cft(file->f_dentry);
  1313. if (!cft)
  1314. return -ENODEV;
  1315. if (cft->open)
  1316. err = cft->open(inode, file);
  1317. else
  1318. err = 0;
  1319. return err;
  1320. }
  1321. static int cpuset_file_release(struct inode *inode, struct file *file)
  1322. {
  1323. struct cftype *cft = __d_cft(file->f_dentry);
  1324. if (cft->release)
  1325. return cft->release(inode, file);
  1326. return 0;
  1327. }
  1328. /*
  1329. * cpuset_rename - Only allow simple rename of directories in place.
  1330. */
  1331. static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
  1332. struct inode *new_dir, struct dentry *new_dentry)
  1333. {
  1334. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1335. return -ENOTDIR;
  1336. if (new_dentry->d_inode)
  1337. return -EEXIST;
  1338. if (old_dir != new_dir)
  1339. return -EIO;
  1340. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1341. }
  1342. static struct file_operations cpuset_file_operations = {
  1343. .read = cpuset_file_read,
  1344. .write = cpuset_file_write,
  1345. .llseek = generic_file_llseek,
  1346. .open = cpuset_file_open,
  1347. .release = cpuset_file_release,
  1348. };
  1349. static struct inode_operations cpuset_dir_inode_operations = {
  1350. .lookup = simple_lookup,
  1351. .mkdir = cpuset_mkdir,
  1352. .rmdir = cpuset_rmdir,
  1353. .rename = cpuset_rename,
  1354. };
  1355. static int cpuset_create_file(struct dentry *dentry, int mode)
  1356. {
  1357. struct inode *inode;
  1358. if (!dentry)
  1359. return -ENOENT;
  1360. if (dentry->d_inode)
  1361. return -EEXIST;
  1362. inode = cpuset_new_inode(mode);
  1363. if (!inode)
  1364. return -ENOMEM;
  1365. if (S_ISDIR(mode)) {
  1366. inode->i_op = &cpuset_dir_inode_operations;
  1367. inode->i_fop = &simple_dir_operations;
  1368. /* start off with i_nlink == 2 (for "." entry) */
  1369. inode->i_nlink++;
  1370. } else if (S_ISREG(mode)) {
  1371. inode->i_size = 0;
  1372. inode->i_fop = &cpuset_file_operations;
  1373. }
  1374. d_instantiate(dentry, inode);
  1375. dget(dentry); /* Extra count - pin the dentry in core */
  1376. return 0;
  1377. }
  1378. /*
  1379. * cpuset_create_dir - create a directory for an object.
  1380. * cs: the cpuset we create the directory for.
  1381. * It must have a valid ->parent field
  1382. * And we are going to fill its ->dentry field.
  1383. * name: The name to give to the cpuset directory. Will be copied.
  1384. * mode: mode to set on new directory.
  1385. */
  1386. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  1387. {
  1388. struct dentry *dentry = NULL;
  1389. struct dentry *parent;
  1390. int error = 0;
  1391. parent = cs->parent->dentry;
  1392. dentry = cpuset_get_dentry(parent, name);
  1393. if (IS_ERR(dentry))
  1394. return PTR_ERR(dentry);
  1395. error = cpuset_create_file(dentry, S_IFDIR | mode);
  1396. if (!error) {
  1397. dentry->d_fsdata = cs;
  1398. parent->d_inode->i_nlink++;
  1399. cs->dentry = dentry;
  1400. }
  1401. dput(dentry);
  1402. return error;
  1403. }
  1404. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  1405. {
  1406. struct dentry *dentry;
  1407. int error;
  1408. mutex_lock(&dir->d_inode->i_mutex);
  1409. dentry = cpuset_get_dentry(dir, cft->name);
  1410. if (!IS_ERR(dentry)) {
  1411. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  1412. if (!error)
  1413. dentry->d_fsdata = (void *)cft;
  1414. dput(dentry);
  1415. } else
  1416. error = PTR_ERR(dentry);
  1417. mutex_unlock(&dir->d_inode->i_mutex);
  1418. return error;
  1419. }
  1420. /*
  1421. * Stuff for reading the 'tasks' file.
  1422. *
  1423. * Reading this file can return large amounts of data if a cpuset has
  1424. * *lots* of attached tasks. So it may need several calls to read(),
  1425. * but we cannot guarantee that the information we produce is correct
  1426. * unless we produce it entirely atomically.
  1427. *
  1428. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1429. * will have a pointer to an array (also allocated here). The struct
  1430. * ctr_struct * is stored in file->private_data. Its resources will
  1431. * be freed by release() when the file is closed. The array is used
  1432. * to sprintf the PIDs and then used by read().
  1433. */
  1434. /* cpusets_tasks_read array */
  1435. struct ctr_struct {
  1436. char *buf;
  1437. int bufsz;
  1438. };
  1439. /*
  1440. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1441. * Return actual number of pids loaded. No need to task_lock(p)
  1442. * when reading out p->cpuset, as we don't really care if it changes
  1443. * on the next cycle, and we are not going to try to dereference it.
  1444. */
  1445. static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1446. {
  1447. int n = 0;
  1448. struct task_struct *g, *p;
  1449. read_lock(&tasklist_lock);
  1450. do_each_thread(g, p) {
  1451. if (p->cpuset == cs) {
  1452. pidarray[n++] = p->pid;
  1453. if (unlikely(n == npids))
  1454. goto array_full;
  1455. }
  1456. } while_each_thread(g, p);
  1457. array_full:
  1458. read_unlock(&tasklist_lock);
  1459. return n;
  1460. }
  1461. static int cmppid(const void *a, const void *b)
  1462. {
  1463. return *(pid_t *)a - *(pid_t *)b;
  1464. }
  1465. /*
  1466. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1467. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1468. * count 'cnt' of how many chars would be written if buf were large enough.
  1469. */
  1470. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1471. {
  1472. int cnt = 0;
  1473. int i;
  1474. for (i = 0; i < npids; i++)
  1475. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1476. return cnt;
  1477. }
  1478. /*
  1479. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1480. * process id's of tasks currently attached to the cpuset being opened.
  1481. *
  1482. * Does not require any specific cpuset mutexes, and does not take any.
  1483. */
  1484. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1485. {
  1486. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1487. struct ctr_struct *ctr;
  1488. pid_t *pidarray;
  1489. int npids;
  1490. char c;
  1491. if (!(file->f_mode & FMODE_READ))
  1492. return 0;
  1493. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1494. if (!ctr)
  1495. goto err0;
  1496. /*
  1497. * If cpuset gets more users after we read count, we won't have
  1498. * enough space - tough. This race is indistinguishable to the
  1499. * caller from the case that the additional cpuset users didn't
  1500. * show up until sometime later on.
  1501. */
  1502. npids = atomic_read(&cs->count);
  1503. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1504. if (!pidarray)
  1505. goto err1;
  1506. npids = pid_array_load(pidarray, npids, cs);
  1507. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1508. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1509. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1510. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1511. if (!ctr->buf)
  1512. goto err2;
  1513. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1514. kfree(pidarray);
  1515. file->private_data = ctr;
  1516. return 0;
  1517. err2:
  1518. kfree(pidarray);
  1519. err1:
  1520. kfree(ctr);
  1521. err0:
  1522. return -ENOMEM;
  1523. }
  1524. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1525. size_t nbytes, loff_t *ppos)
  1526. {
  1527. struct ctr_struct *ctr = file->private_data;
  1528. if (*ppos + nbytes > ctr->bufsz)
  1529. nbytes = ctr->bufsz - *ppos;
  1530. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1531. return -EFAULT;
  1532. *ppos += nbytes;
  1533. return nbytes;
  1534. }
  1535. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1536. {
  1537. struct ctr_struct *ctr;
  1538. if (file->f_mode & FMODE_READ) {
  1539. ctr = file->private_data;
  1540. kfree(ctr->buf);
  1541. kfree(ctr);
  1542. }
  1543. return 0;
  1544. }
  1545. /*
  1546. * for the common functions, 'private' gives the type of file
  1547. */
  1548. static struct cftype cft_tasks = {
  1549. .name = "tasks",
  1550. .open = cpuset_tasks_open,
  1551. .read = cpuset_tasks_read,
  1552. .release = cpuset_tasks_release,
  1553. .private = FILE_TASKLIST,
  1554. };
  1555. static struct cftype cft_cpus = {
  1556. .name = "cpus",
  1557. .private = FILE_CPULIST,
  1558. };
  1559. static struct cftype cft_mems = {
  1560. .name = "mems",
  1561. .private = FILE_MEMLIST,
  1562. };
  1563. static struct cftype cft_cpu_exclusive = {
  1564. .name = "cpu_exclusive",
  1565. .private = FILE_CPU_EXCLUSIVE,
  1566. };
  1567. static struct cftype cft_mem_exclusive = {
  1568. .name = "mem_exclusive",
  1569. .private = FILE_MEM_EXCLUSIVE,
  1570. };
  1571. static struct cftype cft_notify_on_release = {
  1572. .name = "notify_on_release",
  1573. .private = FILE_NOTIFY_ON_RELEASE,
  1574. };
  1575. static struct cftype cft_memory_migrate = {
  1576. .name = "memory_migrate",
  1577. .private = FILE_MEMORY_MIGRATE,
  1578. };
  1579. static struct cftype cft_memory_pressure_enabled = {
  1580. .name = "memory_pressure_enabled",
  1581. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1582. };
  1583. static struct cftype cft_memory_pressure = {
  1584. .name = "memory_pressure",
  1585. .private = FILE_MEMORY_PRESSURE,
  1586. };
  1587. static struct cftype cft_spread_page = {
  1588. .name = "memory_spread_page",
  1589. .private = FILE_SPREAD_PAGE,
  1590. };
  1591. static struct cftype cft_spread_slab = {
  1592. .name = "memory_spread_slab",
  1593. .private = FILE_SPREAD_SLAB,
  1594. };
  1595. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1596. {
  1597. int err;
  1598. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1599. return err;
  1600. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1601. return err;
  1602. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1603. return err;
  1604. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1605. return err;
  1606. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1607. return err;
  1608. if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
  1609. return err;
  1610. if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
  1611. return err;
  1612. if ((err = cpuset_add_file(cs_dentry, &cft_spread_page)) < 0)
  1613. return err;
  1614. if ((err = cpuset_add_file(cs_dentry, &cft_spread_slab)) < 0)
  1615. return err;
  1616. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1617. return err;
  1618. return 0;
  1619. }
  1620. /*
  1621. * cpuset_create - create a cpuset
  1622. * parent: cpuset that will be parent of the new cpuset.
  1623. * name: name of the new cpuset. Will be strcpy'ed.
  1624. * mode: mode to set on new inode
  1625. *
  1626. * Must be called with the mutex on the parent inode held
  1627. */
  1628. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1629. {
  1630. struct cpuset *cs;
  1631. int err;
  1632. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1633. if (!cs)
  1634. return -ENOMEM;
  1635. mutex_lock(&manage_mutex);
  1636. cpuset_update_task_memory_state();
  1637. cs->flags = 0;
  1638. if (notify_on_release(parent))
  1639. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1640. if (is_spread_page(parent))
  1641. set_bit(CS_SPREAD_PAGE, &cs->flags);
  1642. if (is_spread_slab(parent))
  1643. set_bit(CS_SPREAD_SLAB, &cs->flags);
  1644. cs->cpus_allowed = CPU_MASK_NONE;
  1645. cs->mems_allowed = NODE_MASK_NONE;
  1646. atomic_set(&cs->count, 0);
  1647. INIT_LIST_HEAD(&cs->sibling);
  1648. INIT_LIST_HEAD(&cs->children);
  1649. cs->mems_generation = cpuset_mems_generation++;
  1650. fmeter_init(&cs->fmeter);
  1651. cs->parent = parent;
  1652. mutex_lock(&callback_mutex);
  1653. list_add(&cs->sibling, &cs->parent->children);
  1654. number_of_cpusets++;
  1655. mutex_unlock(&callback_mutex);
  1656. err = cpuset_create_dir(cs, name, mode);
  1657. if (err < 0)
  1658. goto err;
  1659. /*
  1660. * Release manage_mutex before cpuset_populate_dir() because it
  1661. * will down() this new directory's i_mutex and if we race with
  1662. * another mkdir, we might deadlock.
  1663. */
  1664. mutex_unlock(&manage_mutex);
  1665. err = cpuset_populate_dir(cs->dentry);
  1666. /* If err < 0, we have a half-filled directory - oh well ;) */
  1667. return 0;
  1668. err:
  1669. list_del(&cs->sibling);
  1670. mutex_unlock(&manage_mutex);
  1671. kfree(cs);
  1672. return err;
  1673. }
  1674. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1675. {
  1676. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1677. /* the vfs holds inode->i_mutex already */
  1678. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1679. }
  1680. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1681. {
  1682. struct cpuset *cs = dentry->d_fsdata;
  1683. struct dentry *d;
  1684. struct cpuset *parent;
  1685. char *pathbuf = NULL;
  1686. /* the vfs holds both inode->i_mutex already */
  1687. mutex_lock(&manage_mutex);
  1688. cpuset_update_task_memory_state();
  1689. if (atomic_read(&cs->count) > 0) {
  1690. mutex_unlock(&manage_mutex);
  1691. return -EBUSY;
  1692. }
  1693. if (!list_empty(&cs->children)) {
  1694. mutex_unlock(&manage_mutex);
  1695. return -EBUSY;
  1696. }
  1697. parent = cs->parent;
  1698. mutex_lock(&callback_mutex);
  1699. set_bit(CS_REMOVED, &cs->flags);
  1700. if (is_cpu_exclusive(cs))
  1701. update_cpu_domains(cs);
  1702. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1703. spin_lock(&cs->dentry->d_lock);
  1704. d = dget(cs->dentry);
  1705. cs->dentry = NULL;
  1706. spin_unlock(&d->d_lock);
  1707. cpuset_d_remove_dir(d);
  1708. dput(d);
  1709. number_of_cpusets--;
  1710. mutex_unlock(&callback_mutex);
  1711. if (list_empty(&parent->children))
  1712. check_for_release(parent, &pathbuf);
  1713. mutex_unlock(&manage_mutex);
  1714. cpuset_release_agent(pathbuf);
  1715. return 0;
  1716. }
  1717. /*
  1718. * cpuset_init_early - just enough so that the calls to
  1719. * cpuset_update_task_memory_state() in early init code
  1720. * are harmless.
  1721. */
  1722. int __init cpuset_init_early(void)
  1723. {
  1724. struct task_struct *tsk = current;
  1725. tsk->cpuset = &top_cpuset;
  1726. tsk->cpuset->mems_generation = cpuset_mems_generation++;
  1727. return 0;
  1728. }
  1729. /**
  1730. * cpuset_init - initialize cpusets at system boot
  1731. *
  1732. * Description: Initialize top_cpuset and the cpuset internal file system,
  1733. **/
  1734. int __init cpuset_init(void)
  1735. {
  1736. struct dentry *root;
  1737. int err;
  1738. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1739. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1740. fmeter_init(&top_cpuset.fmeter);
  1741. top_cpuset.mems_generation = cpuset_mems_generation++;
  1742. init_task.cpuset = &top_cpuset;
  1743. err = register_filesystem(&cpuset_fs_type);
  1744. if (err < 0)
  1745. goto out;
  1746. cpuset_mount = kern_mount(&cpuset_fs_type);
  1747. if (IS_ERR(cpuset_mount)) {
  1748. printk(KERN_ERR "cpuset: could not mount!\n");
  1749. err = PTR_ERR(cpuset_mount);
  1750. cpuset_mount = NULL;
  1751. goto out;
  1752. }
  1753. root = cpuset_mount->mnt_sb->s_root;
  1754. root->d_fsdata = &top_cpuset;
  1755. root->d_inode->i_nlink++;
  1756. top_cpuset.dentry = root;
  1757. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1758. number_of_cpusets = 1;
  1759. err = cpuset_populate_dir(root);
  1760. /* memory_pressure_enabled is in root cpuset only */
  1761. if (err == 0)
  1762. err = cpuset_add_file(root, &cft_memory_pressure_enabled);
  1763. out:
  1764. return err;
  1765. }
  1766. /**
  1767. * cpuset_init_smp - initialize cpus_allowed
  1768. *
  1769. * Description: Finish top cpuset after cpu, node maps are initialized
  1770. **/
  1771. void __init cpuset_init_smp(void)
  1772. {
  1773. top_cpuset.cpus_allowed = cpu_online_map;
  1774. top_cpuset.mems_allowed = node_online_map;
  1775. }
  1776. /**
  1777. * cpuset_fork - attach newly forked task to its parents cpuset.
  1778. * @tsk: pointer to task_struct of forking parent process.
  1779. *
  1780. * Description: A task inherits its parent's cpuset at fork().
  1781. *
  1782. * A pointer to the shared cpuset was automatically copied in fork.c
  1783. * by dup_task_struct(). However, we ignore that copy, since it was
  1784. * not made under the protection of task_lock(), so might no longer be
  1785. * a valid cpuset pointer. attach_task() might have already changed
  1786. * current->cpuset, allowing the previously referenced cpuset to
  1787. * be removed and freed. Instead, we task_lock(current) and copy
  1788. * its present value of current->cpuset for our freshly forked child.
  1789. *
  1790. * At the point that cpuset_fork() is called, 'current' is the parent
  1791. * task, and the passed argument 'child' points to the child task.
  1792. **/
  1793. void cpuset_fork(struct task_struct *child)
  1794. {
  1795. task_lock(current);
  1796. child->cpuset = current->cpuset;
  1797. atomic_inc(&child->cpuset->count);
  1798. task_unlock(current);
  1799. }
  1800. /**
  1801. * cpuset_exit - detach cpuset from exiting task
  1802. * @tsk: pointer to task_struct of exiting process
  1803. *
  1804. * Description: Detach cpuset from @tsk and release it.
  1805. *
  1806. * Note that cpusets marked notify_on_release force every task in
  1807. * them to take the global manage_mutex mutex when exiting.
  1808. * This could impact scaling on very large systems. Be reluctant to
  1809. * use notify_on_release cpusets where very high task exit scaling
  1810. * is required on large systems.
  1811. *
  1812. * Don't even think about derefencing 'cs' after the cpuset use count
  1813. * goes to zero, except inside a critical section guarded by manage_mutex
  1814. * or callback_mutex. Otherwise a zero cpuset use count is a license to
  1815. * any other task to nuke the cpuset immediately, via cpuset_rmdir().
  1816. *
  1817. * This routine has to take manage_mutex, not callback_mutex, because
  1818. * it is holding that mutex while calling check_for_release(),
  1819. * which calls kmalloc(), so can't be called holding callback_mutex().
  1820. *
  1821. * We don't need to task_lock() this reference to tsk->cpuset,
  1822. * because tsk is already marked PF_EXITING, so attach_task() won't
  1823. * mess with it, or task is a failed fork, never visible to attach_task.
  1824. *
  1825. * the_top_cpuset_hack:
  1826. *
  1827. * Set the exiting tasks cpuset to the root cpuset (top_cpuset).
  1828. *
  1829. * Don't leave a task unable to allocate memory, as that is an
  1830. * accident waiting to happen should someone add a callout in
  1831. * do_exit() after the cpuset_exit() call that might allocate.
  1832. * If a task tries to allocate memory with an invalid cpuset,
  1833. * it will oops in cpuset_update_task_memory_state().
  1834. *
  1835. * We call cpuset_exit() while the task is still competent to
  1836. * handle notify_on_release(), then leave the task attached to
  1837. * the root cpuset (top_cpuset) for the remainder of its exit.
  1838. *
  1839. * To do this properly, we would increment the reference count on
  1840. * top_cpuset, and near the very end of the kernel/exit.c do_exit()
  1841. * code we would add a second cpuset function call, to drop that
  1842. * reference. This would just create an unnecessary hot spot on
  1843. * the top_cpuset reference count, to no avail.
  1844. *
  1845. * Normally, holding a reference to a cpuset without bumping its
  1846. * count is unsafe. The cpuset could go away, or someone could
  1847. * attach us to a different cpuset, decrementing the count on
  1848. * the first cpuset that we never incremented. But in this case,
  1849. * top_cpuset isn't going away, and either task has PF_EXITING set,
  1850. * which wards off any attach_task() attempts, or task is a failed
  1851. * fork, never visible to attach_task.
  1852. *
  1853. * Another way to do this would be to set the cpuset pointer
  1854. * to NULL here, and check in cpuset_update_task_memory_state()
  1855. * for a NULL pointer. This hack avoids that NULL check, for no
  1856. * cost (other than this way too long comment ;).
  1857. **/
  1858. void cpuset_exit(struct task_struct *tsk)
  1859. {
  1860. struct cpuset *cs;
  1861. cs = tsk->cpuset;
  1862. tsk->cpuset = &top_cpuset; /* the_top_cpuset_hack - see above */
  1863. if (notify_on_release(cs)) {
  1864. char *pathbuf = NULL;
  1865. mutex_lock(&manage_mutex);
  1866. if (atomic_dec_and_test(&cs->count))
  1867. check_for_release(cs, &pathbuf);
  1868. mutex_unlock(&manage_mutex);
  1869. cpuset_release_agent(pathbuf);
  1870. } else {
  1871. atomic_dec(&cs->count);
  1872. }
  1873. }
  1874. /**
  1875. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1876. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1877. *
  1878. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1879. * attached to the specified @tsk. Guaranteed to return some non-empty
  1880. * subset of cpu_online_map, even if this means going outside the
  1881. * tasks cpuset.
  1882. **/
  1883. cpumask_t cpuset_cpus_allowed(struct task_struct *tsk)
  1884. {
  1885. cpumask_t mask;
  1886. mutex_lock(&callback_mutex);
  1887. task_lock(tsk);
  1888. guarantee_online_cpus(tsk->cpuset, &mask);
  1889. task_unlock(tsk);
  1890. mutex_unlock(&callback_mutex);
  1891. return mask;
  1892. }
  1893. void cpuset_init_current_mems_allowed(void)
  1894. {
  1895. current->mems_allowed = NODE_MASK_ALL;
  1896. }
  1897. /**
  1898. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  1899. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  1900. *
  1901. * Description: Returns the nodemask_t mems_allowed of the cpuset
  1902. * attached to the specified @tsk. Guaranteed to return some non-empty
  1903. * subset of node_online_map, even if this means going outside the
  1904. * tasks cpuset.
  1905. **/
  1906. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  1907. {
  1908. nodemask_t mask;
  1909. mutex_lock(&callback_mutex);
  1910. task_lock(tsk);
  1911. guarantee_online_mems(tsk->cpuset, &mask);
  1912. task_unlock(tsk);
  1913. mutex_unlock(&callback_mutex);
  1914. return mask;
  1915. }
  1916. /**
  1917. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1918. * @zl: the zonelist to be checked
  1919. *
  1920. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1921. */
  1922. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1923. {
  1924. int i;
  1925. for (i = 0; zl->zones[i]; i++) {
  1926. int nid = zl->zones[i]->zone_pgdat->node_id;
  1927. if (node_isset(nid, current->mems_allowed))
  1928. return 1;
  1929. }
  1930. return 0;
  1931. }
  1932. /*
  1933. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1934. * ancestor to the specified cpuset. Call holding callback_mutex.
  1935. * If no ancestor is mem_exclusive (an unusual configuration), then
  1936. * returns the root cpuset.
  1937. */
  1938. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1939. {
  1940. while (!is_mem_exclusive(cs) && cs->parent)
  1941. cs = cs->parent;
  1942. return cs;
  1943. }
  1944. /**
  1945. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1946. * @z: is this zone on an allowed node?
  1947. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1948. *
  1949. * If we're in interrupt, yes, we can always allocate. If zone
  1950. * z's node is in our tasks mems_allowed, yes. If it's not a
  1951. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1952. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1953. * Otherwise, no.
  1954. *
  1955. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1956. * and do not allow allocations outside the current tasks cpuset.
  1957. * GFP_KERNEL allocations are not so marked, so can escape to the
  1958. * nearest mem_exclusive ancestor cpuset.
  1959. *
  1960. * Scanning up parent cpusets requires callback_mutex. The __alloc_pages()
  1961. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1962. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1963. * mems_allowed came up empty on the first pass over the zonelist.
  1964. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1965. * short of memory, might require taking the callback_mutex mutex.
  1966. *
  1967. * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
  1968. * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
  1969. * hardwall cpusets - no allocation on a node outside the cpuset is
  1970. * allowed (unless in interrupt, of course).
  1971. *
  1972. * The second loop doesn't even call here for GFP_ATOMIC requests
  1973. * (if the __alloc_pages() local variable 'wait' is set). That check
  1974. * and the checks below have the combined affect in the second loop of
  1975. * the __alloc_pages() routine that:
  1976. * in_interrupt - any node ok (current task context irrelevant)
  1977. * GFP_ATOMIC - any node ok
  1978. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1979. * GFP_USER - only nodes in current tasks mems allowed ok.
  1980. **/
  1981. int __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  1982. {
  1983. int node; /* node that zone z is on */
  1984. const struct cpuset *cs; /* current cpuset ancestors */
  1985. int allowed; /* is allocation in zone z allowed? */
  1986. if (in_interrupt())
  1987. return 1;
  1988. node = z->zone_pgdat->node_id;
  1989. if (node_isset(node, current->mems_allowed))
  1990. return 1;
  1991. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1992. return 0;
  1993. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1994. return 1;
  1995. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1996. mutex_lock(&callback_mutex);
  1997. task_lock(current);
  1998. cs = nearest_exclusive_ancestor(current->cpuset);
  1999. task_unlock(current);
  2000. allowed = node_isset(node, cs->mems_allowed);
  2001. mutex_unlock(&callback_mutex);
  2002. return allowed;
  2003. }
  2004. /**
  2005. * cpuset_lock - lock out any changes to cpuset structures
  2006. *
  2007. * The out of memory (oom) code needs to mutex_lock cpusets
  2008. * from being changed while it scans the tasklist looking for a
  2009. * task in an overlapping cpuset. Expose callback_mutex via this
  2010. * cpuset_lock() routine, so the oom code can lock it, before
  2011. * locking the task list. The tasklist_lock is a spinlock, so
  2012. * must be taken inside callback_mutex.
  2013. */
  2014. void cpuset_lock(void)
  2015. {
  2016. mutex_lock(&callback_mutex);
  2017. }
  2018. /**
  2019. * cpuset_unlock - release lock on cpuset changes
  2020. *
  2021. * Undo the lock taken in a previous cpuset_lock() call.
  2022. */
  2023. void cpuset_unlock(void)
  2024. {
  2025. mutex_unlock(&callback_mutex);
  2026. }
  2027. /**
  2028. * cpuset_mem_spread_node() - On which node to begin search for a page
  2029. *
  2030. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  2031. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  2032. * and if the memory allocation used cpuset_mem_spread_node()
  2033. * to determine on which node to start looking, as it will for
  2034. * certain page cache or slab cache pages such as used for file
  2035. * system buffers and inode caches, then instead of starting on the
  2036. * local node to look for a free page, rather spread the starting
  2037. * node around the tasks mems_allowed nodes.
  2038. *
  2039. * We don't have to worry about the returned node being offline
  2040. * because "it can't happen", and even if it did, it would be ok.
  2041. *
  2042. * The routines calling guarantee_online_mems() are careful to
  2043. * only set nodes in task->mems_allowed that are online. So it
  2044. * should not be possible for the following code to return an
  2045. * offline node. But if it did, that would be ok, as this routine
  2046. * is not returning the node where the allocation must be, only
  2047. * the node where the search should start. The zonelist passed to
  2048. * __alloc_pages() will include all nodes. If the slab allocator
  2049. * is passed an offline node, it will fall back to the local node.
  2050. * See kmem_cache_alloc_node().
  2051. */
  2052. int cpuset_mem_spread_node(void)
  2053. {
  2054. int node;
  2055. node = next_node(current->cpuset_mem_spread_rotor, current->mems_allowed);
  2056. if (node == MAX_NUMNODES)
  2057. node = first_node(current->mems_allowed);
  2058. current->cpuset_mem_spread_rotor = node;
  2059. return node;
  2060. }
  2061. EXPORT_SYMBOL_GPL(cpuset_mem_spread_node);
  2062. /**
  2063. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  2064. * @p: pointer to task_struct of some other task.
  2065. *
  2066. * Description: Return true if the nearest mem_exclusive ancestor
  2067. * cpusets of tasks @p and current overlap. Used by oom killer to
  2068. * determine if task @p's memory usage might impact the memory
  2069. * available to the current task.
  2070. *
  2071. * Call while holding callback_mutex.
  2072. **/
  2073. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  2074. {
  2075. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  2076. int overlap = 0; /* do cpusets overlap? */
  2077. task_lock(current);
  2078. if (current->flags & PF_EXITING) {
  2079. task_unlock(current);
  2080. goto done;
  2081. }
  2082. cs1 = nearest_exclusive_ancestor(current->cpuset);
  2083. task_unlock(current);
  2084. task_lock((struct task_struct *)p);
  2085. if (p->flags & PF_EXITING) {
  2086. task_unlock((struct task_struct *)p);
  2087. goto done;
  2088. }
  2089. cs2 = nearest_exclusive_ancestor(p->cpuset);
  2090. task_unlock((struct task_struct *)p);
  2091. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  2092. done:
  2093. return overlap;
  2094. }
  2095. /*
  2096. * Collection of memory_pressure is suppressed unless
  2097. * this flag is enabled by writing "1" to the special
  2098. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  2099. */
  2100. int cpuset_memory_pressure_enabled __read_mostly;
  2101. /**
  2102. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  2103. *
  2104. * Keep a running average of the rate of synchronous (direct)
  2105. * page reclaim efforts initiated by tasks in each cpuset.
  2106. *
  2107. * This represents the rate at which some task in the cpuset
  2108. * ran low on memory on all nodes it was allowed to use, and
  2109. * had to enter the kernels page reclaim code in an effort to
  2110. * create more free memory by tossing clean pages or swapping
  2111. * or writing dirty pages.
  2112. *
  2113. * Display to user space in the per-cpuset read-only file
  2114. * "memory_pressure". Value displayed is an integer
  2115. * representing the recent rate of entry into the synchronous
  2116. * (direct) page reclaim by any task attached to the cpuset.
  2117. **/
  2118. void __cpuset_memory_pressure_bump(void)
  2119. {
  2120. struct cpuset *cs;
  2121. task_lock(current);
  2122. cs = current->cpuset;
  2123. fmeter_markevent(&cs->fmeter);
  2124. task_unlock(current);
  2125. }
  2126. /*
  2127. * proc_cpuset_show()
  2128. * - Print tasks cpuset path into seq_file.
  2129. * - Used for /proc/<pid>/cpuset.
  2130. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  2131. * doesn't really matter if tsk->cpuset changes after we read it,
  2132. * and we take manage_mutex, keeping attach_task() from changing it
  2133. * anyway. No need to check that tsk->cpuset != NULL, thanks to
  2134. * the_top_cpuset_hack in cpuset_exit(), which sets an exiting tasks
  2135. * cpuset to top_cpuset.
  2136. */
  2137. static int proc_cpuset_show(struct seq_file *m, void *v)
  2138. {
  2139. struct task_struct *tsk;
  2140. char *buf;
  2141. int retval = 0;
  2142. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2143. if (!buf)
  2144. return -ENOMEM;
  2145. tsk = m->private;
  2146. mutex_lock(&manage_mutex);
  2147. retval = cpuset_path(tsk->cpuset, buf, PAGE_SIZE);
  2148. if (retval < 0)
  2149. goto out;
  2150. seq_puts(m, buf);
  2151. seq_putc(m, '\n');
  2152. out:
  2153. mutex_unlock(&manage_mutex);
  2154. kfree(buf);
  2155. return retval;
  2156. }
  2157. static int cpuset_open(struct inode *inode, struct file *file)
  2158. {
  2159. struct task_struct *tsk = PROC_I(inode)->task;
  2160. return single_open(file, proc_cpuset_show, tsk);
  2161. }
  2162. struct file_operations proc_cpuset_operations = {
  2163. .open = cpuset_open,
  2164. .read = seq_read,
  2165. .llseek = seq_lseek,
  2166. .release = single_release,
  2167. };
  2168. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  2169. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  2170. {
  2171. buffer += sprintf(buffer, "Cpus_allowed:\t");
  2172. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  2173. buffer += sprintf(buffer, "\n");
  2174. buffer += sprintf(buffer, "Mems_allowed:\t");
  2175. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  2176. buffer += sprintf(buffer, "\n");
  2177. return buffer;
  2178. }