cpuset.c 65 KB

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