cpuset.c 73 KB

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