cpuset.c 63 KB

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