cpuset.c 79 KB

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