cpuset.c 59 KB

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