cpuset.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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. goto done;
  716. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  717. if (nodes_empty(trialcs.mems_allowed)) {
  718. retval = -ENOSPC;
  719. goto done;
  720. }
  721. retval = validate_change(cs, &trialcs);
  722. if (retval < 0)
  723. goto done;
  724. down(&callback_sem);
  725. cs->mems_allowed = trialcs.mems_allowed;
  726. atomic_inc(&cpuset_mems_generation);
  727. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  728. up(&callback_sem);
  729. done:
  730. return retval;
  731. }
  732. /*
  733. * Call with manage_sem held.
  734. */
  735. static int update_memory_pressure_enabled(struct cpuset *cs, char *buf)
  736. {
  737. if (simple_strtoul(buf, NULL, 10) != 0)
  738. cpuset_memory_pressure_enabled = 1;
  739. else
  740. cpuset_memory_pressure_enabled = 0;
  741. return 0;
  742. }
  743. /*
  744. * update_flag - read a 0 or a 1 in a file and update associated flag
  745. * bit: the bit to update (CS_CPU_EXCLUSIVE, CS_MEM_EXCLUSIVE,
  746. * CS_NOTIFY_ON_RELEASE, CS_MEMORY_MIGRATE)
  747. * cs: the cpuset to update
  748. * buf: the buffer where we read the 0 or 1
  749. *
  750. * Call with manage_sem held.
  751. */
  752. static int update_flag(cpuset_flagbits_t bit, struct cpuset *cs, char *buf)
  753. {
  754. int turning_on;
  755. struct cpuset trialcs;
  756. int err, cpu_exclusive_changed;
  757. turning_on = (simple_strtoul(buf, NULL, 10) != 0);
  758. trialcs = *cs;
  759. if (turning_on)
  760. set_bit(bit, &trialcs.flags);
  761. else
  762. clear_bit(bit, &trialcs.flags);
  763. err = validate_change(cs, &trialcs);
  764. if (err < 0)
  765. return err;
  766. cpu_exclusive_changed =
  767. (is_cpu_exclusive(cs) != is_cpu_exclusive(&trialcs));
  768. down(&callback_sem);
  769. if (turning_on)
  770. set_bit(bit, &cs->flags);
  771. else
  772. clear_bit(bit, &cs->flags);
  773. up(&callback_sem);
  774. if (cpu_exclusive_changed)
  775. update_cpu_domains(cs);
  776. return 0;
  777. }
  778. /*
  779. * Frequency meter - How fast is some event occuring?
  780. *
  781. * These routines manage a digitally filtered, constant time based,
  782. * event frequency meter. There are four routines:
  783. * fmeter_init() - initialize a frequency meter.
  784. * fmeter_markevent() - called each time the event happens.
  785. * fmeter_getrate() - returns the recent rate of such events.
  786. * fmeter_update() - internal routine used to update fmeter.
  787. *
  788. * A common data structure is passed to each of these routines,
  789. * which is used to keep track of the state required to manage the
  790. * frequency meter and its digital filter.
  791. *
  792. * The filter works on the number of events marked per unit time.
  793. * The filter is single-pole low-pass recursive (IIR). The time unit
  794. * is 1 second. Arithmetic is done using 32-bit integers scaled to
  795. * simulate 3 decimal digits of precision (multiplied by 1000).
  796. *
  797. * With an FM_COEF of 933, and a time base of 1 second, the filter
  798. * has a half-life of 10 seconds, meaning that if the events quit
  799. * happening, then the rate returned from the fmeter_getrate()
  800. * will be cut in half each 10 seconds, until it converges to zero.
  801. *
  802. * It is not worth doing a real infinitely recursive filter. If more
  803. * than FM_MAXTICKS ticks have elapsed since the last filter event,
  804. * just compute FM_MAXTICKS ticks worth, by which point the level
  805. * will be stable.
  806. *
  807. * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
  808. * arithmetic overflow in the fmeter_update() routine.
  809. *
  810. * Given the simple 32 bit integer arithmetic used, this meter works
  811. * best for reporting rates between one per millisecond (msec) and
  812. * one per 32 (approx) seconds. At constant rates faster than one
  813. * per msec it maxes out at values just under 1,000,000. At constant
  814. * rates between one per msec, and one per second it will stabilize
  815. * to a value N*1000, where N is the rate of events per second.
  816. * At constant rates between one per second and one per 32 seconds,
  817. * it will be choppy, moving up on the seconds that have an event,
  818. * and then decaying until the next event. At rates slower than
  819. * about one in 32 seconds, it decays all the way back to zero between
  820. * each event.
  821. */
  822. #define FM_COEF 933 /* coefficient for half-life of 10 secs */
  823. #define FM_MAXTICKS ((time_t)99) /* useless computing more ticks than this */
  824. #define FM_MAXCNT 1000000 /* limit cnt to avoid overflow */
  825. #define FM_SCALE 1000 /* faux fixed point scale */
  826. /* Initialize a frequency meter */
  827. static void fmeter_init(struct fmeter *fmp)
  828. {
  829. fmp->cnt = 0;
  830. fmp->val = 0;
  831. fmp->time = 0;
  832. spin_lock_init(&fmp->lock);
  833. }
  834. /* Internal meter update - process cnt events and update value */
  835. static void fmeter_update(struct fmeter *fmp)
  836. {
  837. time_t now = get_seconds();
  838. time_t ticks = now - fmp->time;
  839. if (ticks == 0)
  840. return;
  841. ticks = min(FM_MAXTICKS, ticks);
  842. while (ticks-- > 0)
  843. fmp->val = (FM_COEF * fmp->val) / FM_SCALE;
  844. fmp->time = now;
  845. fmp->val += ((FM_SCALE - FM_COEF) * fmp->cnt) / FM_SCALE;
  846. fmp->cnt = 0;
  847. }
  848. /* Process any previous ticks, then bump cnt by one (times scale). */
  849. static void fmeter_markevent(struct fmeter *fmp)
  850. {
  851. spin_lock(&fmp->lock);
  852. fmeter_update(fmp);
  853. fmp->cnt = min(FM_MAXCNT, fmp->cnt + FM_SCALE);
  854. spin_unlock(&fmp->lock);
  855. }
  856. /* Process any previous ticks, then return current value. */
  857. static int fmeter_getrate(struct fmeter *fmp)
  858. {
  859. int val;
  860. spin_lock(&fmp->lock);
  861. fmeter_update(fmp);
  862. val = fmp->val;
  863. spin_unlock(&fmp->lock);
  864. return val;
  865. }
  866. /*
  867. * Attack task specified by pid in 'pidbuf' to cpuset 'cs', possibly
  868. * writing the path of the old cpuset in 'ppathbuf' if it needs to be
  869. * notified on release.
  870. *
  871. * Call holding manage_sem. May take callback_sem and task_lock of
  872. * the task 'pid' during call.
  873. */
  874. static int attach_task(struct cpuset *cs, char *pidbuf, char **ppathbuf)
  875. {
  876. pid_t pid;
  877. struct task_struct *tsk;
  878. struct cpuset *oldcs;
  879. cpumask_t cpus;
  880. nodemask_t from, to;
  881. if (sscanf(pidbuf, "%d", &pid) != 1)
  882. return -EIO;
  883. if (cpus_empty(cs->cpus_allowed) || nodes_empty(cs->mems_allowed))
  884. return -ENOSPC;
  885. if (pid) {
  886. read_lock(&tasklist_lock);
  887. tsk = find_task_by_pid(pid);
  888. if (!tsk || tsk->flags & PF_EXITING) {
  889. read_unlock(&tasklist_lock);
  890. return -ESRCH;
  891. }
  892. get_task_struct(tsk);
  893. read_unlock(&tasklist_lock);
  894. if ((current->euid) && (current->euid != tsk->uid)
  895. && (current->euid != tsk->suid)) {
  896. put_task_struct(tsk);
  897. return -EACCES;
  898. }
  899. } else {
  900. tsk = current;
  901. get_task_struct(tsk);
  902. }
  903. down(&callback_sem);
  904. task_lock(tsk);
  905. oldcs = tsk->cpuset;
  906. if (!oldcs) {
  907. task_unlock(tsk);
  908. up(&callback_sem);
  909. put_task_struct(tsk);
  910. return -ESRCH;
  911. }
  912. atomic_inc(&cs->count);
  913. tsk->cpuset = cs;
  914. task_unlock(tsk);
  915. guarantee_online_cpus(cs, &cpus);
  916. set_cpus_allowed(tsk, cpus);
  917. from = oldcs->mems_allowed;
  918. to = cs->mems_allowed;
  919. up(&callback_sem);
  920. if (is_memory_migrate(cs))
  921. do_migrate_pages(tsk->mm, &from, &to, MPOL_MF_MOVE_ALL);
  922. put_task_struct(tsk);
  923. if (atomic_dec_and_test(&oldcs->count))
  924. check_for_release(oldcs, ppathbuf);
  925. return 0;
  926. }
  927. /* The various types of files and directories in a cpuset file system */
  928. typedef enum {
  929. FILE_ROOT,
  930. FILE_DIR,
  931. FILE_MEMORY_MIGRATE,
  932. FILE_CPULIST,
  933. FILE_MEMLIST,
  934. FILE_CPU_EXCLUSIVE,
  935. FILE_MEM_EXCLUSIVE,
  936. FILE_NOTIFY_ON_RELEASE,
  937. FILE_MEMORY_PRESSURE_ENABLED,
  938. FILE_MEMORY_PRESSURE,
  939. FILE_TASKLIST,
  940. } cpuset_filetype_t;
  941. static ssize_t cpuset_common_file_write(struct file *file, const char __user *userbuf,
  942. size_t nbytes, loff_t *unused_ppos)
  943. {
  944. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  945. struct cftype *cft = __d_cft(file->f_dentry);
  946. cpuset_filetype_t type = cft->private;
  947. char *buffer;
  948. char *pathbuf = NULL;
  949. int retval = 0;
  950. /* Crude upper limit on largest legitimate cpulist user might write. */
  951. if (nbytes > 100 + 6 * NR_CPUS)
  952. return -E2BIG;
  953. /* +1 for nul-terminator */
  954. if ((buffer = kmalloc(nbytes + 1, GFP_KERNEL)) == 0)
  955. return -ENOMEM;
  956. if (copy_from_user(buffer, userbuf, nbytes)) {
  957. retval = -EFAULT;
  958. goto out1;
  959. }
  960. buffer[nbytes] = 0; /* nul-terminate */
  961. down(&manage_sem);
  962. if (is_removed(cs)) {
  963. retval = -ENODEV;
  964. goto out2;
  965. }
  966. switch (type) {
  967. case FILE_CPULIST:
  968. retval = update_cpumask(cs, buffer);
  969. break;
  970. case FILE_MEMLIST:
  971. retval = update_nodemask(cs, buffer);
  972. break;
  973. case FILE_CPU_EXCLUSIVE:
  974. retval = update_flag(CS_CPU_EXCLUSIVE, cs, buffer);
  975. break;
  976. case FILE_MEM_EXCLUSIVE:
  977. retval = update_flag(CS_MEM_EXCLUSIVE, cs, buffer);
  978. break;
  979. case FILE_NOTIFY_ON_RELEASE:
  980. retval = update_flag(CS_NOTIFY_ON_RELEASE, cs, buffer);
  981. break;
  982. case FILE_MEMORY_MIGRATE:
  983. retval = update_flag(CS_MEMORY_MIGRATE, cs, buffer);
  984. break;
  985. case FILE_MEMORY_PRESSURE_ENABLED:
  986. retval = update_memory_pressure_enabled(cs, buffer);
  987. break;
  988. case FILE_MEMORY_PRESSURE:
  989. retval = -EACCES;
  990. break;
  991. case FILE_TASKLIST:
  992. retval = attach_task(cs, buffer, &pathbuf);
  993. break;
  994. default:
  995. retval = -EINVAL;
  996. goto out2;
  997. }
  998. if (retval == 0)
  999. retval = nbytes;
  1000. out2:
  1001. up(&manage_sem);
  1002. cpuset_release_agent(pathbuf);
  1003. out1:
  1004. kfree(buffer);
  1005. return retval;
  1006. }
  1007. static ssize_t cpuset_file_write(struct file *file, const char __user *buf,
  1008. size_t nbytes, loff_t *ppos)
  1009. {
  1010. ssize_t retval = 0;
  1011. struct cftype *cft = __d_cft(file->f_dentry);
  1012. if (!cft)
  1013. return -ENODEV;
  1014. /* special function ? */
  1015. if (cft->write)
  1016. retval = cft->write(file, buf, nbytes, ppos);
  1017. else
  1018. retval = cpuset_common_file_write(file, buf, nbytes, ppos);
  1019. return retval;
  1020. }
  1021. /*
  1022. * These ascii lists should be read in a single call, by using a user
  1023. * buffer large enough to hold the entire map. If read in smaller
  1024. * chunks, there is no guarantee of atomicity. Since the display format
  1025. * used, list of ranges of sequential numbers, is variable length,
  1026. * and since these maps can change value dynamically, one could read
  1027. * gibberish by doing partial reads while a list was changing.
  1028. * A single large read to a buffer that crosses a page boundary is
  1029. * ok, because the result being copied to user land is not recomputed
  1030. * across a page fault.
  1031. */
  1032. static int cpuset_sprintf_cpulist(char *page, struct cpuset *cs)
  1033. {
  1034. cpumask_t mask;
  1035. down(&callback_sem);
  1036. mask = cs->cpus_allowed;
  1037. up(&callback_sem);
  1038. return cpulist_scnprintf(page, PAGE_SIZE, mask);
  1039. }
  1040. static int cpuset_sprintf_memlist(char *page, struct cpuset *cs)
  1041. {
  1042. nodemask_t mask;
  1043. down(&callback_sem);
  1044. mask = cs->mems_allowed;
  1045. up(&callback_sem);
  1046. return nodelist_scnprintf(page, PAGE_SIZE, mask);
  1047. }
  1048. static ssize_t cpuset_common_file_read(struct file *file, char __user *buf,
  1049. size_t nbytes, loff_t *ppos)
  1050. {
  1051. struct cftype *cft = __d_cft(file->f_dentry);
  1052. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1053. cpuset_filetype_t type = cft->private;
  1054. char *page;
  1055. ssize_t retval = 0;
  1056. char *s;
  1057. if (!(page = (char *)__get_free_page(GFP_KERNEL)))
  1058. return -ENOMEM;
  1059. s = page;
  1060. switch (type) {
  1061. case FILE_CPULIST:
  1062. s += cpuset_sprintf_cpulist(s, cs);
  1063. break;
  1064. case FILE_MEMLIST:
  1065. s += cpuset_sprintf_memlist(s, cs);
  1066. break;
  1067. case FILE_CPU_EXCLUSIVE:
  1068. *s++ = is_cpu_exclusive(cs) ? '1' : '0';
  1069. break;
  1070. case FILE_MEM_EXCLUSIVE:
  1071. *s++ = is_mem_exclusive(cs) ? '1' : '0';
  1072. break;
  1073. case FILE_NOTIFY_ON_RELEASE:
  1074. *s++ = notify_on_release(cs) ? '1' : '0';
  1075. break;
  1076. case FILE_MEMORY_MIGRATE:
  1077. *s++ = is_memory_migrate(cs) ? '1' : '0';
  1078. break;
  1079. case FILE_MEMORY_PRESSURE_ENABLED:
  1080. *s++ = cpuset_memory_pressure_enabled ? '1' : '0';
  1081. break;
  1082. case FILE_MEMORY_PRESSURE:
  1083. s += sprintf(s, "%d", fmeter_getrate(&cs->fmeter));
  1084. break;
  1085. default:
  1086. retval = -EINVAL;
  1087. goto out;
  1088. }
  1089. *s++ = '\n';
  1090. retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
  1091. out:
  1092. free_page((unsigned long)page);
  1093. return retval;
  1094. }
  1095. static ssize_t cpuset_file_read(struct file *file, char __user *buf, size_t nbytes,
  1096. loff_t *ppos)
  1097. {
  1098. ssize_t retval = 0;
  1099. struct cftype *cft = __d_cft(file->f_dentry);
  1100. if (!cft)
  1101. return -ENODEV;
  1102. /* special function ? */
  1103. if (cft->read)
  1104. retval = cft->read(file, buf, nbytes, ppos);
  1105. else
  1106. retval = cpuset_common_file_read(file, buf, nbytes, ppos);
  1107. return retval;
  1108. }
  1109. static int cpuset_file_open(struct inode *inode, struct file *file)
  1110. {
  1111. int err;
  1112. struct cftype *cft;
  1113. err = generic_file_open(inode, file);
  1114. if (err)
  1115. return err;
  1116. cft = __d_cft(file->f_dentry);
  1117. if (!cft)
  1118. return -ENODEV;
  1119. if (cft->open)
  1120. err = cft->open(inode, file);
  1121. else
  1122. err = 0;
  1123. return err;
  1124. }
  1125. static int cpuset_file_release(struct inode *inode, struct file *file)
  1126. {
  1127. struct cftype *cft = __d_cft(file->f_dentry);
  1128. if (cft->release)
  1129. return cft->release(inode, file);
  1130. return 0;
  1131. }
  1132. /*
  1133. * cpuset_rename - Only allow simple rename of directories in place.
  1134. */
  1135. static int cpuset_rename(struct inode *old_dir, struct dentry *old_dentry,
  1136. struct inode *new_dir, struct dentry *new_dentry)
  1137. {
  1138. if (!S_ISDIR(old_dentry->d_inode->i_mode))
  1139. return -ENOTDIR;
  1140. if (new_dentry->d_inode)
  1141. return -EEXIST;
  1142. if (old_dir != new_dir)
  1143. return -EIO;
  1144. return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
  1145. }
  1146. static struct file_operations cpuset_file_operations = {
  1147. .read = cpuset_file_read,
  1148. .write = cpuset_file_write,
  1149. .llseek = generic_file_llseek,
  1150. .open = cpuset_file_open,
  1151. .release = cpuset_file_release,
  1152. };
  1153. static struct inode_operations cpuset_dir_inode_operations = {
  1154. .lookup = simple_lookup,
  1155. .mkdir = cpuset_mkdir,
  1156. .rmdir = cpuset_rmdir,
  1157. .rename = cpuset_rename,
  1158. };
  1159. static int cpuset_create_file(struct dentry *dentry, int mode)
  1160. {
  1161. struct inode *inode;
  1162. if (!dentry)
  1163. return -ENOENT;
  1164. if (dentry->d_inode)
  1165. return -EEXIST;
  1166. inode = cpuset_new_inode(mode);
  1167. if (!inode)
  1168. return -ENOMEM;
  1169. if (S_ISDIR(mode)) {
  1170. inode->i_op = &cpuset_dir_inode_operations;
  1171. inode->i_fop = &simple_dir_operations;
  1172. /* start off with i_nlink == 2 (for "." entry) */
  1173. inode->i_nlink++;
  1174. } else if (S_ISREG(mode)) {
  1175. inode->i_size = 0;
  1176. inode->i_fop = &cpuset_file_operations;
  1177. }
  1178. d_instantiate(dentry, inode);
  1179. dget(dentry); /* Extra count - pin the dentry in core */
  1180. return 0;
  1181. }
  1182. /*
  1183. * cpuset_create_dir - create a directory for an object.
  1184. * cs: the cpuset we create the directory for.
  1185. * It must have a valid ->parent field
  1186. * And we are going to fill its ->dentry field.
  1187. * name: The name to give to the cpuset directory. Will be copied.
  1188. * mode: mode to set on new directory.
  1189. */
  1190. static int cpuset_create_dir(struct cpuset *cs, const char *name, int mode)
  1191. {
  1192. struct dentry *dentry = NULL;
  1193. struct dentry *parent;
  1194. int error = 0;
  1195. parent = cs->parent->dentry;
  1196. dentry = cpuset_get_dentry(parent, name);
  1197. if (IS_ERR(dentry))
  1198. return PTR_ERR(dentry);
  1199. error = cpuset_create_file(dentry, S_IFDIR | mode);
  1200. if (!error) {
  1201. dentry->d_fsdata = cs;
  1202. parent->d_inode->i_nlink++;
  1203. cs->dentry = dentry;
  1204. }
  1205. dput(dentry);
  1206. return error;
  1207. }
  1208. static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
  1209. {
  1210. struct dentry *dentry;
  1211. int error;
  1212. down(&dir->d_inode->i_sem);
  1213. dentry = cpuset_get_dentry(dir, cft->name);
  1214. if (!IS_ERR(dentry)) {
  1215. error = cpuset_create_file(dentry, 0644 | S_IFREG);
  1216. if (!error)
  1217. dentry->d_fsdata = (void *)cft;
  1218. dput(dentry);
  1219. } else
  1220. error = PTR_ERR(dentry);
  1221. up(&dir->d_inode->i_sem);
  1222. return error;
  1223. }
  1224. /*
  1225. * Stuff for reading the 'tasks' file.
  1226. *
  1227. * Reading this file can return large amounts of data if a cpuset has
  1228. * *lots* of attached tasks. So it may need several calls to read(),
  1229. * but we cannot guarantee that the information we produce is correct
  1230. * unless we produce it entirely atomically.
  1231. *
  1232. * Upon tasks file open(), a struct ctr_struct is allocated, that
  1233. * will have a pointer to an array (also allocated here). The struct
  1234. * ctr_struct * is stored in file->private_data. Its resources will
  1235. * be freed by release() when the file is closed. The array is used
  1236. * to sprintf the PIDs and then used by read().
  1237. */
  1238. /* cpusets_tasks_read array */
  1239. struct ctr_struct {
  1240. char *buf;
  1241. int bufsz;
  1242. };
  1243. /*
  1244. * Load into 'pidarray' up to 'npids' of the tasks using cpuset 'cs'.
  1245. * Return actual number of pids loaded. No need to task_lock(p)
  1246. * when reading out p->cpuset, as we don't really care if it changes
  1247. * on the next cycle, and we are not going to try to dereference it.
  1248. */
  1249. static inline int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
  1250. {
  1251. int n = 0;
  1252. struct task_struct *g, *p;
  1253. read_lock(&tasklist_lock);
  1254. do_each_thread(g, p) {
  1255. if (p->cpuset == cs) {
  1256. pidarray[n++] = p->pid;
  1257. if (unlikely(n == npids))
  1258. goto array_full;
  1259. }
  1260. } while_each_thread(g, p);
  1261. array_full:
  1262. read_unlock(&tasklist_lock);
  1263. return n;
  1264. }
  1265. static int cmppid(const void *a, const void *b)
  1266. {
  1267. return *(pid_t *)a - *(pid_t *)b;
  1268. }
  1269. /*
  1270. * Convert array 'a' of 'npids' pid_t's to a string of newline separated
  1271. * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
  1272. * count 'cnt' of how many chars would be written if buf were large enough.
  1273. */
  1274. static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
  1275. {
  1276. int cnt = 0;
  1277. int i;
  1278. for (i = 0; i < npids; i++)
  1279. cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
  1280. return cnt;
  1281. }
  1282. /*
  1283. * Handle an open on 'tasks' file. Prepare a buffer listing the
  1284. * process id's of tasks currently attached to the cpuset being opened.
  1285. *
  1286. * Does not require any specific cpuset semaphores, and does not take any.
  1287. */
  1288. static int cpuset_tasks_open(struct inode *unused, struct file *file)
  1289. {
  1290. struct cpuset *cs = __d_cs(file->f_dentry->d_parent);
  1291. struct ctr_struct *ctr;
  1292. pid_t *pidarray;
  1293. int npids;
  1294. char c;
  1295. if (!(file->f_mode & FMODE_READ))
  1296. return 0;
  1297. ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
  1298. if (!ctr)
  1299. goto err0;
  1300. /*
  1301. * If cpuset gets more users after we read count, we won't have
  1302. * enough space - tough. This race is indistinguishable to the
  1303. * caller from the case that the additional cpuset users didn't
  1304. * show up until sometime later on.
  1305. */
  1306. npids = atomic_read(&cs->count);
  1307. pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
  1308. if (!pidarray)
  1309. goto err1;
  1310. npids = pid_array_load(pidarray, npids, cs);
  1311. sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
  1312. /* Call pid_array_to_buf() twice, first just to get bufsz */
  1313. ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
  1314. ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
  1315. if (!ctr->buf)
  1316. goto err2;
  1317. ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
  1318. kfree(pidarray);
  1319. file->private_data = ctr;
  1320. return 0;
  1321. err2:
  1322. kfree(pidarray);
  1323. err1:
  1324. kfree(ctr);
  1325. err0:
  1326. return -ENOMEM;
  1327. }
  1328. static ssize_t cpuset_tasks_read(struct file *file, char __user *buf,
  1329. size_t nbytes, loff_t *ppos)
  1330. {
  1331. struct ctr_struct *ctr = file->private_data;
  1332. if (*ppos + nbytes > ctr->bufsz)
  1333. nbytes = ctr->bufsz - *ppos;
  1334. if (copy_to_user(buf, ctr->buf + *ppos, nbytes))
  1335. return -EFAULT;
  1336. *ppos += nbytes;
  1337. return nbytes;
  1338. }
  1339. static int cpuset_tasks_release(struct inode *unused_inode, struct file *file)
  1340. {
  1341. struct ctr_struct *ctr;
  1342. if (file->f_mode & FMODE_READ) {
  1343. ctr = file->private_data;
  1344. kfree(ctr->buf);
  1345. kfree(ctr);
  1346. }
  1347. return 0;
  1348. }
  1349. /*
  1350. * for the common functions, 'private' gives the type of file
  1351. */
  1352. static struct cftype cft_tasks = {
  1353. .name = "tasks",
  1354. .open = cpuset_tasks_open,
  1355. .read = cpuset_tasks_read,
  1356. .release = cpuset_tasks_release,
  1357. .private = FILE_TASKLIST,
  1358. };
  1359. static struct cftype cft_cpus = {
  1360. .name = "cpus",
  1361. .private = FILE_CPULIST,
  1362. };
  1363. static struct cftype cft_mems = {
  1364. .name = "mems",
  1365. .private = FILE_MEMLIST,
  1366. };
  1367. static struct cftype cft_cpu_exclusive = {
  1368. .name = "cpu_exclusive",
  1369. .private = FILE_CPU_EXCLUSIVE,
  1370. };
  1371. static struct cftype cft_mem_exclusive = {
  1372. .name = "mem_exclusive",
  1373. .private = FILE_MEM_EXCLUSIVE,
  1374. };
  1375. static struct cftype cft_notify_on_release = {
  1376. .name = "notify_on_release",
  1377. .private = FILE_NOTIFY_ON_RELEASE,
  1378. };
  1379. static struct cftype cft_memory_migrate = {
  1380. .name = "memory_migrate",
  1381. .private = FILE_MEMORY_MIGRATE,
  1382. };
  1383. static struct cftype cft_memory_pressure_enabled = {
  1384. .name = "memory_pressure_enabled",
  1385. .private = FILE_MEMORY_PRESSURE_ENABLED,
  1386. };
  1387. static struct cftype cft_memory_pressure = {
  1388. .name = "memory_pressure",
  1389. .private = FILE_MEMORY_PRESSURE,
  1390. };
  1391. static int cpuset_populate_dir(struct dentry *cs_dentry)
  1392. {
  1393. int err;
  1394. if ((err = cpuset_add_file(cs_dentry, &cft_cpus)) < 0)
  1395. return err;
  1396. if ((err = cpuset_add_file(cs_dentry, &cft_mems)) < 0)
  1397. return err;
  1398. if ((err = cpuset_add_file(cs_dentry, &cft_cpu_exclusive)) < 0)
  1399. return err;
  1400. if ((err = cpuset_add_file(cs_dentry, &cft_mem_exclusive)) < 0)
  1401. return err;
  1402. if ((err = cpuset_add_file(cs_dentry, &cft_notify_on_release)) < 0)
  1403. return err;
  1404. if ((err = cpuset_add_file(cs_dentry, &cft_memory_migrate)) < 0)
  1405. return err;
  1406. if ((err = cpuset_add_file(cs_dentry, &cft_memory_pressure)) < 0)
  1407. return err;
  1408. if ((err = cpuset_add_file(cs_dentry, &cft_tasks)) < 0)
  1409. return err;
  1410. return 0;
  1411. }
  1412. /*
  1413. * cpuset_create - create a cpuset
  1414. * parent: cpuset that will be parent of the new cpuset.
  1415. * name: name of the new cpuset. Will be strcpy'ed.
  1416. * mode: mode to set on new inode
  1417. *
  1418. * Must be called with the semaphore on the parent inode held
  1419. */
  1420. static long cpuset_create(struct cpuset *parent, const char *name, int mode)
  1421. {
  1422. struct cpuset *cs;
  1423. int err;
  1424. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  1425. if (!cs)
  1426. return -ENOMEM;
  1427. down(&manage_sem);
  1428. refresh_mems();
  1429. cs->flags = 0;
  1430. if (notify_on_release(parent))
  1431. set_bit(CS_NOTIFY_ON_RELEASE, &cs->flags);
  1432. cs->cpus_allowed = CPU_MASK_NONE;
  1433. cs->mems_allowed = NODE_MASK_NONE;
  1434. atomic_set(&cs->count, 0);
  1435. INIT_LIST_HEAD(&cs->sibling);
  1436. INIT_LIST_HEAD(&cs->children);
  1437. atomic_inc(&cpuset_mems_generation);
  1438. cs->mems_generation = atomic_read(&cpuset_mems_generation);
  1439. fmeter_init(&cs->fmeter);
  1440. cs->parent = parent;
  1441. down(&callback_sem);
  1442. list_add(&cs->sibling, &cs->parent->children);
  1443. up(&callback_sem);
  1444. err = cpuset_create_dir(cs, name, mode);
  1445. if (err < 0)
  1446. goto err;
  1447. /*
  1448. * Release manage_sem before cpuset_populate_dir() because it
  1449. * will down() this new directory's i_sem and if we race with
  1450. * another mkdir, we might deadlock.
  1451. */
  1452. up(&manage_sem);
  1453. err = cpuset_populate_dir(cs->dentry);
  1454. /* If err < 0, we have a half-filled directory - oh well ;) */
  1455. return 0;
  1456. err:
  1457. list_del(&cs->sibling);
  1458. up(&manage_sem);
  1459. kfree(cs);
  1460. return err;
  1461. }
  1462. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1463. {
  1464. struct cpuset *c_parent = dentry->d_parent->d_fsdata;
  1465. /* the vfs holds inode->i_sem already */
  1466. return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
  1467. }
  1468. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
  1469. {
  1470. struct cpuset *cs = dentry->d_fsdata;
  1471. struct dentry *d;
  1472. struct cpuset *parent;
  1473. char *pathbuf = NULL;
  1474. /* the vfs holds both inode->i_sem already */
  1475. down(&manage_sem);
  1476. refresh_mems();
  1477. if (atomic_read(&cs->count) > 0) {
  1478. up(&manage_sem);
  1479. return -EBUSY;
  1480. }
  1481. if (!list_empty(&cs->children)) {
  1482. up(&manage_sem);
  1483. return -EBUSY;
  1484. }
  1485. parent = cs->parent;
  1486. down(&callback_sem);
  1487. set_bit(CS_REMOVED, &cs->flags);
  1488. if (is_cpu_exclusive(cs))
  1489. update_cpu_domains(cs);
  1490. list_del(&cs->sibling); /* delete my sibling from parent->children */
  1491. spin_lock(&cs->dentry->d_lock);
  1492. d = dget(cs->dentry);
  1493. cs->dentry = NULL;
  1494. spin_unlock(&d->d_lock);
  1495. cpuset_d_remove_dir(d);
  1496. dput(d);
  1497. up(&callback_sem);
  1498. if (list_empty(&parent->children))
  1499. check_for_release(parent, &pathbuf);
  1500. up(&manage_sem);
  1501. cpuset_release_agent(pathbuf);
  1502. return 0;
  1503. }
  1504. /**
  1505. * cpuset_init - initialize cpusets at system boot
  1506. *
  1507. * Description: Initialize top_cpuset and the cpuset internal file system,
  1508. **/
  1509. int __init cpuset_init(void)
  1510. {
  1511. struct dentry *root;
  1512. int err;
  1513. top_cpuset.cpus_allowed = CPU_MASK_ALL;
  1514. top_cpuset.mems_allowed = NODE_MASK_ALL;
  1515. fmeter_init(&top_cpuset.fmeter);
  1516. atomic_inc(&cpuset_mems_generation);
  1517. top_cpuset.mems_generation = atomic_read(&cpuset_mems_generation);
  1518. init_task.cpuset = &top_cpuset;
  1519. err = register_filesystem(&cpuset_fs_type);
  1520. if (err < 0)
  1521. goto out;
  1522. cpuset_mount = kern_mount(&cpuset_fs_type);
  1523. if (IS_ERR(cpuset_mount)) {
  1524. printk(KERN_ERR "cpuset: could not mount!\n");
  1525. err = PTR_ERR(cpuset_mount);
  1526. cpuset_mount = NULL;
  1527. goto out;
  1528. }
  1529. root = cpuset_mount->mnt_sb->s_root;
  1530. root->d_fsdata = &top_cpuset;
  1531. root->d_inode->i_nlink++;
  1532. top_cpuset.dentry = root;
  1533. root->d_inode->i_op = &cpuset_dir_inode_operations;
  1534. err = cpuset_populate_dir(root);
  1535. /* memory_pressure_enabled is in root cpuset only */
  1536. if (err == 0)
  1537. err = cpuset_add_file(root, &cft_memory_pressure_enabled);
  1538. out:
  1539. return err;
  1540. }
  1541. /**
  1542. * cpuset_init_smp - initialize cpus_allowed
  1543. *
  1544. * Description: Finish top cpuset after cpu, node maps are initialized
  1545. **/
  1546. void __init cpuset_init_smp(void)
  1547. {
  1548. top_cpuset.cpus_allowed = cpu_online_map;
  1549. top_cpuset.mems_allowed = node_online_map;
  1550. }
  1551. /**
  1552. * cpuset_fork - attach newly forked task to its parents cpuset.
  1553. * @tsk: pointer to task_struct of forking parent process.
  1554. *
  1555. * Description: A task inherits its parent's cpuset at fork().
  1556. *
  1557. * A pointer to the shared cpuset was automatically copied in fork.c
  1558. * by dup_task_struct(). However, we ignore that copy, since it was
  1559. * not made under the protection of task_lock(), so might no longer be
  1560. * a valid cpuset pointer. attach_task() might have already changed
  1561. * current->cpuset, allowing the previously referenced cpuset to
  1562. * be removed and freed. Instead, we task_lock(current) and copy
  1563. * its present value of current->cpuset for our freshly forked child.
  1564. *
  1565. * At the point that cpuset_fork() is called, 'current' is the parent
  1566. * task, and the passed argument 'child' points to the child task.
  1567. **/
  1568. void cpuset_fork(struct task_struct *child)
  1569. {
  1570. task_lock(current);
  1571. child->cpuset = current->cpuset;
  1572. atomic_inc(&child->cpuset->count);
  1573. task_unlock(current);
  1574. }
  1575. /**
  1576. * cpuset_exit - detach cpuset from exiting task
  1577. * @tsk: pointer to task_struct of exiting process
  1578. *
  1579. * Description: Detach cpuset from @tsk and release it.
  1580. *
  1581. * Note that cpusets marked notify_on_release force every task in
  1582. * them to take the global manage_sem semaphore when exiting.
  1583. * This could impact scaling on very large systems. Be reluctant to
  1584. * use notify_on_release cpusets where very high task exit scaling
  1585. * is required on large systems.
  1586. *
  1587. * Don't even think about derefencing 'cs' after the cpuset use count
  1588. * goes to zero, except inside a critical section guarded by manage_sem
  1589. * or callback_sem. Otherwise a zero cpuset use count is a license to
  1590. * any other task to nuke the cpuset immediately, via cpuset_rmdir().
  1591. *
  1592. * This routine has to take manage_sem, not callback_sem, because
  1593. * it is holding that semaphore while calling check_for_release(),
  1594. * which calls kmalloc(), so can't be called holding callback__sem().
  1595. *
  1596. * We don't need to task_lock() this reference to tsk->cpuset,
  1597. * because tsk is already marked PF_EXITING, so attach_task() won't
  1598. * mess with it, or task is a failed fork, never visible to attach_task.
  1599. **/
  1600. void cpuset_exit(struct task_struct *tsk)
  1601. {
  1602. struct cpuset *cs;
  1603. cs = tsk->cpuset;
  1604. tsk->cpuset = NULL;
  1605. if (notify_on_release(cs)) {
  1606. char *pathbuf = NULL;
  1607. down(&manage_sem);
  1608. if (atomic_dec_and_test(&cs->count))
  1609. check_for_release(cs, &pathbuf);
  1610. up(&manage_sem);
  1611. cpuset_release_agent(pathbuf);
  1612. } else {
  1613. atomic_dec(&cs->count);
  1614. }
  1615. }
  1616. /**
  1617. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1618. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1619. *
  1620. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1621. * attached to the specified @tsk. Guaranteed to return some non-empty
  1622. * subset of cpu_online_map, even if this means going outside the
  1623. * tasks cpuset.
  1624. **/
  1625. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1626. {
  1627. cpumask_t mask;
  1628. down(&callback_sem);
  1629. task_lock((struct task_struct *)tsk);
  1630. guarantee_online_cpus(tsk->cpuset, &mask);
  1631. task_unlock((struct task_struct *)tsk);
  1632. up(&callback_sem);
  1633. return mask;
  1634. }
  1635. void cpuset_init_current_mems_allowed(void)
  1636. {
  1637. current->mems_allowed = NODE_MASK_ALL;
  1638. }
  1639. /**
  1640. * cpuset_update_current_mems_allowed - update mems parameters to new values
  1641. *
  1642. * If the current tasks cpusets mems_allowed changed behind our backs,
  1643. * update current->mems_allowed and mems_generation to the new value.
  1644. * Do not call this routine if in_interrupt().
  1645. *
  1646. * Call without callback_sem or task_lock() held. May be called
  1647. * with or without manage_sem held. Unless exiting, it will acquire
  1648. * task_lock(). Also might acquire callback_sem during call to
  1649. * refresh_mems().
  1650. */
  1651. void cpuset_update_current_mems_allowed(void)
  1652. {
  1653. struct cpuset *cs;
  1654. int need_to_refresh = 0;
  1655. task_lock(current);
  1656. cs = current->cpuset;
  1657. if (!cs)
  1658. goto done;
  1659. if (current->cpuset_mems_generation != cs->mems_generation)
  1660. need_to_refresh = 1;
  1661. done:
  1662. task_unlock(current);
  1663. if (need_to_refresh)
  1664. refresh_mems();
  1665. }
  1666. /**
  1667. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1668. * @zl: the zonelist to be checked
  1669. *
  1670. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1671. */
  1672. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1673. {
  1674. int i;
  1675. for (i = 0; zl->zones[i]; i++) {
  1676. int nid = zl->zones[i]->zone_pgdat->node_id;
  1677. if (node_isset(nid, current->mems_allowed))
  1678. return 1;
  1679. }
  1680. return 0;
  1681. }
  1682. /*
  1683. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1684. * ancestor to the specified cpuset. Call holding callback_sem.
  1685. * If no ancestor is mem_exclusive (an unusual configuration), then
  1686. * returns the root cpuset.
  1687. */
  1688. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1689. {
  1690. while (!is_mem_exclusive(cs) && cs->parent)
  1691. cs = cs->parent;
  1692. return cs;
  1693. }
  1694. /**
  1695. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1696. * @z: is this zone on an allowed node?
  1697. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1698. *
  1699. * If we're in interrupt, yes, we can always allocate. If zone
  1700. * z's node is in our tasks mems_allowed, yes. If it's not a
  1701. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1702. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1703. * Otherwise, no.
  1704. *
  1705. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1706. * and do not allow allocations outside the current tasks cpuset.
  1707. * GFP_KERNEL allocations are not so marked, so can escape to the
  1708. * nearest mem_exclusive ancestor cpuset.
  1709. *
  1710. * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
  1711. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1712. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1713. * mems_allowed came up empty on the first pass over the zonelist.
  1714. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1715. * short of memory, might require taking the callback_sem semaphore.
  1716. *
  1717. * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
  1718. * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
  1719. * hardwall cpusets - no allocation on a node outside the cpuset is
  1720. * allowed (unless in interrupt, of course).
  1721. *
  1722. * The second loop doesn't even call here for GFP_ATOMIC requests
  1723. * (if the __alloc_pages() local variable 'wait' is set). That check
  1724. * and the checks below have the combined affect in the second loop of
  1725. * the __alloc_pages() routine that:
  1726. * in_interrupt - any node ok (current task context irrelevant)
  1727. * GFP_ATOMIC - any node ok
  1728. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1729. * GFP_USER - only nodes in current tasks mems allowed ok.
  1730. **/
  1731. int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  1732. {
  1733. int node; /* node that zone z is on */
  1734. const struct cpuset *cs; /* current cpuset ancestors */
  1735. int allowed = 1; /* is allocation in zone z allowed? */
  1736. if (in_interrupt())
  1737. return 1;
  1738. node = z->zone_pgdat->node_id;
  1739. if (node_isset(node, current->mems_allowed))
  1740. return 1;
  1741. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1742. return 0;
  1743. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1744. return 1;
  1745. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1746. down(&callback_sem);
  1747. task_lock(current);
  1748. cs = nearest_exclusive_ancestor(current->cpuset);
  1749. task_unlock(current);
  1750. allowed = node_isset(node, cs->mems_allowed);
  1751. up(&callback_sem);
  1752. return allowed;
  1753. }
  1754. /**
  1755. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  1756. * @p: pointer to task_struct of some other task.
  1757. *
  1758. * Description: Return true if the nearest mem_exclusive ancestor
  1759. * cpusets of tasks @p and current overlap. Used by oom killer to
  1760. * determine if task @p's memory usage might impact the memory
  1761. * available to the current task.
  1762. *
  1763. * Acquires callback_sem - not suitable for calling from a fast path.
  1764. **/
  1765. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  1766. {
  1767. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  1768. int overlap = 0; /* do cpusets overlap? */
  1769. down(&callback_sem);
  1770. task_lock(current);
  1771. if (current->flags & PF_EXITING) {
  1772. task_unlock(current);
  1773. goto done;
  1774. }
  1775. cs1 = nearest_exclusive_ancestor(current->cpuset);
  1776. task_unlock(current);
  1777. task_lock((struct task_struct *)p);
  1778. if (p->flags & PF_EXITING) {
  1779. task_unlock((struct task_struct *)p);
  1780. goto done;
  1781. }
  1782. cs2 = nearest_exclusive_ancestor(p->cpuset);
  1783. task_unlock((struct task_struct *)p);
  1784. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  1785. done:
  1786. up(&callback_sem);
  1787. return overlap;
  1788. }
  1789. /*
  1790. * Collection of memory_pressure is suppressed unless
  1791. * this flag is enabled by writing "1" to the special
  1792. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  1793. */
  1794. int cpuset_memory_pressure_enabled __read_mostly;
  1795. /**
  1796. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  1797. *
  1798. * Keep a running average of the rate of synchronous (direct)
  1799. * page reclaim efforts initiated by tasks in each cpuset.
  1800. *
  1801. * This represents the rate at which some task in the cpuset
  1802. * ran low on memory on all nodes it was allowed to use, and
  1803. * had to enter the kernels page reclaim code in an effort to
  1804. * create more free memory by tossing clean pages or swapping
  1805. * or writing dirty pages.
  1806. *
  1807. * Display to user space in the per-cpuset read-only file
  1808. * "memory_pressure". Value displayed is an integer
  1809. * representing the recent rate of entry into the synchronous
  1810. * (direct) page reclaim by any task attached to the cpuset.
  1811. **/
  1812. void __cpuset_memory_pressure_bump(void)
  1813. {
  1814. struct cpuset *cs;
  1815. task_lock(current);
  1816. cs = current->cpuset;
  1817. fmeter_markevent(&cs->fmeter);
  1818. task_unlock(current);
  1819. }
  1820. /*
  1821. * proc_cpuset_show()
  1822. * - Print tasks cpuset path into seq_file.
  1823. * - Used for /proc/<pid>/cpuset.
  1824. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  1825. * doesn't really matter if tsk->cpuset changes after we read it,
  1826. * and we take manage_sem, keeping attach_task() from changing it
  1827. * anyway.
  1828. */
  1829. static int proc_cpuset_show(struct seq_file *m, void *v)
  1830. {
  1831. struct cpuset *cs;
  1832. struct task_struct *tsk;
  1833. char *buf;
  1834. int retval = 0;
  1835. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1836. if (!buf)
  1837. return -ENOMEM;
  1838. tsk = m->private;
  1839. down(&manage_sem);
  1840. cs = tsk->cpuset;
  1841. if (!cs) {
  1842. retval = -EINVAL;
  1843. goto out;
  1844. }
  1845. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1846. if (retval < 0)
  1847. goto out;
  1848. seq_puts(m, buf);
  1849. seq_putc(m, '\n');
  1850. out:
  1851. up(&manage_sem);
  1852. kfree(buf);
  1853. return retval;
  1854. }
  1855. static int cpuset_open(struct inode *inode, struct file *file)
  1856. {
  1857. struct task_struct *tsk = PROC_I(inode)->task;
  1858. return single_open(file, proc_cpuset_show, tsk);
  1859. }
  1860. struct file_operations proc_cpuset_operations = {
  1861. .open = cpuset_open,
  1862. .read = seq_read,
  1863. .llseek = seq_lseek,
  1864. .release = single_release,
  1865. };
  1866. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1867. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1868. {
  1869. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1870. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1871. buffer += sprintf(buffer, "\n");
  1872. buffer += sprintf(buffer, "Mems_allowed:\t");
  1873. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1874. buffer += sprintf(buffer, "\n");
  1875. return buffer;
  1876. }