cpuset.c 77 KB

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