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. .parent = NULL,
  139. .dentry = NULL,
  140. .mems_generation = 0,
  141. };
  142. static struct vfsmount *cpuset_mount;
  143. static struct super_block *cpuset_sb;
  144. /*
  145. * We have two global cpuset semaphores below. They can nest.
  146. * It is ok to first take manage_sem, then nest callback_sem. We also
  147. * require taking task_lock() when dereferencing a tasks cpuset pointer.
  148. * See "The task_lock() exception", at the end of this comment.
  149. *
  150. * A task must hold both semaphores to modify cpusets. If a task
  151. * holds manage_sem, then it blocks others wanting that semaphore,
  152. * ensuring that it is the only task able to also acquire callback_sem
  153. * and be able to modify cpusets. It can perform various checks on
  154. * the cpuset structure first, knowing nothing will change. It can
  155. * also allocate memory while just holding manage_sem. While it is
  156. * performing these checks, various callback routines can briefly
  157. * acquire callback_sem to query cpusets. Once it is ready to make
  158. * the changes, it takes callback_sem, blocking everyone else.
  159. *
  160. * Calls to the kernel memory allocator can not be made while holding
  161. * callback_sem, as that would risk double tripping on callback_sem
  162. * from one of the callbacks into the cpuset code from within
  163. * __alloc_pages().
  164. *
  165. * If a task is only holding callback_sem, then it has read-only
  166. * access to cpusets.
  167. *
  168. * The task_struct fields mems_allowed and mems_generation may only
  169. * be accessed in the context of that task, so require no locks.
  170. *
  171. * Any task can increment and decrement the count field without lock.
  172. * So in general, code holding manage_sem or callback_sem can't rely
  173. * on the count field not changing. However, if the count goes to
  174. * zero, then only attach_task(), which holds both semaphores, can
  175. * increment it again. Because a count of zero means that no tasks
  176. * are currently attached, therefore there is no way a task attached
  177. * to that cpuset can fork (the other way to increment the count).
  178. * So code holding manage_sem or callback_sem can safely assume that
  179. * if the count is zero, it will stay zero. Similarly, if a task
  180. * holds manage_sem or callback_sem on a cpuset with zero count, it
  181. * knows that the cpuset won't be removed, as cpuset_rmdir() needs
  182. * both of those semaphores.
  183. *
  184. * A possible optimization to improve parallelism would be to make
  185. * callback_sem a R/W semaphore (rwsem), allowing the callback routines
  186. * to proceed in parallel, with read access, until the holder of
  187. * manage_sem needed to take this rwsem for exclusive write access
  188. * and modify some cpusets.
  189. *
  190. * The cpuset_common_file_write handler for operations that modify
  191. * the cpuset hierarchy holds manage_sem across the entire operation,
  192. * single threading all such cpuset modifications across the system.
  193. *
  194. * The cpuset_common_file_read() handlers only hold callback_sem across
  195. * small pieces of code, such as when reading out possibly multi-word
  196. * cpumasks and nodemasks.
  197. *
  198. * The fork and exit callbacks cpuset_fork() and cpuset_exit(), don't
  199. * (usually) take either semaphore. These are the two most performance
  200. * critical pieces of code here. The exception occurs on cpuset_exit(),
  201. * when a task in a notify_on_release cpuset exits. Then manage_sem
  202. * is taken, and if the cpuset count is zero, a usermode call made
  203. * to /sbin/cpuset_release_agent with the name of the cpuset (path
  204. * relative to the root of cpuset file system) as the argument.
  205. *
  206. * A cpuset can only be deleted if both its 'count' of using tasks
  207. * is zero, and its list of 'children' cpusets is empty. Since all
  208. * tasks in the system use _some_ cpuset, and since there is always at
  209. * least one task in the system (init, pid == 1), therefore, top_cpuset
  210. * always has either children cpusets and/or using tasks. So we don't
  211. * need a special hack to ensure that top_cpuset cannot be deleted.
  212. *
  213. * The above "Tale of Two Semaphores" would be complete, but for:
  214. *
  215. * The task_lock() exception
  216. *
  217. * The need for this exception arises from the action of attach_task(),
  218. * which overwrites one tasks cpuset pointer with another. It does
  219. * so using both semaphores, however there are several performance
  220. * critical places that need to reference task->cpuset without the
  221. * expense of grabbing a system global semaphore. Therefore except as
  222. * noted below, when dereferencing or, as in attach_task(), modifying
  223. * a tasks cpuset pointer we use task_lock(), which acts on a spinlock
  224. * (task->alloc_lock) already in the task_struct routinely used for
  225. * such matters.
  226. */
  227. static DECLARE_MUTEX(manage_sem);
  228. static DECLARE_MUTEX(callback_sem);
  229. /*
  230. * A couple of forward declarations required, due to cyclic reference loop:
  231. * cpuset_mkdir -> cpuset_create -> cpuset_populate_dir -> cpuset_add_file
  232. * -> cpuset_create_file -> cpuset_dir_inode_operations -> cpuset_mkdir.
  233. */
  234. static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  235. static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry);
  236. static struct backing_dev_info cpuset_backing_dev_info = {
  237. .ra_pages = 0, /* No readahead */
  238. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  239. };
  240. static struct inode *cpuset_new_inode(mode_t mode)
  241. {
  242. struct inode *inode = new_inode(cpuset_sb);
  243. if (inode) {
  244. inode->i_mode = mode;
  245. inode->i_uid = current->fsuid;
  246. inode->i_gid = current->fsgid;
  247. inode->i_blksize = PAGE_CACHE_SIZE;
  248. inode->i_blocks = 0;
  249. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  250. inode->i_mapping->backing_dev_info = &cpuset_backing_dev_info;
  251. }
  252. return inode;
  253. }
  254. static void cpuset_diput(struct dentry *dentry, struct inode *inode)
  255. {
  256. /* is dentry a directory ? if so, kfree() associated cpuset */
  257. if (S_ISDIR(inode->i_mode)) {
  258. struct cpuset *cs = dentry->d_fsdata;
  259. BUG_ON(!(is_removed(cs)));
  260. kfree(cs);
  261. }
  262. iput(inode);
  263. }
  264. static struct dentry_operations cpuset_dops = {
  265. .d_iput = cpuset_diput,
  266. };
  267. static struct dentry *cpuset_get_dentry(struct dentry *parent, const char *name)
  268. {
  269. struct dentry *d = lookup_one_len(name, parent, strlen(name));
  270. if (!IS_ERR(d))
  271. d->d_op = &cpuset_dops;
  272. return d;
  273. }
  274. static void remove_dir(struct dentry *d)
  275. {
  276. struct dentry *parent = dget(d->d_parent);
  277. d_delete(d);
  278. simple_rmdir(parent->d_inode, d);
  279. dput(parent);
  280. }
  281. /*
  282. * NOTE : the dentry must have been dget()'ed
  283. */
  284. static void cpuset_d_remove_dir(struct dentry *dentry)
  285. {
  286. struct list_head *node;
  287. spin_lock(&dcache_lock);
  288. node = dentry->d_subdirs.next;
  289. while (node != &dentry->d_subdirs) {
  290. struct dentry *d = list_entry(node, struct dentry, d_child);
  291. list_del_init(node);
  292. if (d->d_inode) {
  293. d = dget_locked(d);
  294. spin_unlock(&dcache_lock);
  295. d_delete(d);
  296. simple_unlink(dentry->d_inode, d);
  297. dput(d);
  298. spin_lock(&dcache_lock);
  299. }
  300. node = dentry->d_subdirs.next;
  301. }
  302. list_del_init(&dentry->d_child);
  303. spin_unlock(&dcache_lock);
  304. remove_dir(dentry);
  305. }
  306. static struct super_operations cpuset_ops = {
  307. .statfs = simple_statfs,
  308. .drop_inode = generic_delete_inode,
  309. };
  310. static int cpuset_fill_super(struct super_block *sb, void *unused_data,
  311. int unused_silent)
  312. {
  313. struct inode *inode;
  314. struct dentry *root;
  315. sb->s_blocksize = PAGE_CACHE_SIZE;
  316. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  317. sb->s_magic = CPUSET_SUPER_MAGIC;
  318. sb->s_op = &cpuset_ops;
  319. cpuset_sb = sb;
  320. inode = cpuset_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR);
  321. if (inode) {
  322. inode->i_op = &simple_dir_inode_operations;
  323. inode->i_fop = &simple_dir_operations;
  324. /* directories start off with i_nlink == 2 (for "." entry) */
  325. inode->i_nlink++;
  326. } else {
  327. return -ENOMEM;
  328. }
  329. root = d_alloc_root(inode);
  330. if (!root) {
  331. iput(inode);
  332. return -ENOMEM;
  333. }
  334. sb->s_root = root;
  335. return 0;
  336. }
  337. static struct super_block *cpuset_get_sb(struct file_system_type *fs_type,
  338. int flags, const char *unused_dev_name,
  339. void *data)
  340. {
  341. return get_sb_single(fs_type, flags, data, cpuset_fill_super);
  342. }
  343. static struct file_system_type cpuset_fs_type = {
  344. .name = "cpuset",
  345. .get_sb = cpuset_get_sb,
  346. .kill_sb = kill_litter_super,
  347. };
  348. /* struct cftype:
  349. *
  350. * The files in the cpuset filesystem mostly have a very simple read/write
  351. * handling, some common function will take care of it. Nevertheless some cases
  352. * (read tasks) are special and therefore I define this structure for every
  353. * kind of file.
  354. *
  355. *
  356. * When reading/writing to a file:
  357. * - the cpuset to use in file->f_dentry->d_parent->d_fsdata
  358. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  359. */
  360. struct cftype {
  361. char *name;
  362. int private;
  363. int (*open) (struct inode *inode, struct file *file);
  364. ssize_t (*read) (struct file *file, char __user *buf, size_t nbytes,
  365. loff_t *ppos);
  366. int (*write) (struct file *file, const char __user *buf, size_t nbytes,
  367. loff_t *ppos);
  368. int (*release) (struct inode *inode, struct file *file);
  369. };
  370. static inline struct cpuset *__d_cs(struct dentry *dentry)
  371. {
  372. return dentry->d_fsdata;
  373. }
  374. static inline struct cftype *__d_cft(struct dentry *dentry)
  375. {
  376. return dentry->d_fsdata;
  377. }
  378. /*
  379. * Call with manage_sem held. Writes path of cpuset into buf.
  380. * Returns 0 on success, -errno on error.
  381. */
  382. static int cpuset_path(const struct cpuset *cs, char *buf, int buflen)
  383. {
  384. char *start;
  385. start = buf + buflen;
  386. *--start = '\0';
  387. for (;;) {
  388. int len = cs->dentry->d_name.len;
  389. if ((start -= len) < buf)
  390. return -ENAMETOOLONG;
  391. memcpy(start, cs->dentry->d_name.name, len);
  392. cs = cs->parent;
  393. if (!cs)
  394. break;
  395. if (!cs->parent)
  396. continue;
  397. if (--start < buf)
  398. return -ENAMETOOLONG;
  399. *start = '/';
  400. }
  401. memmove(buf, start, buf + buflen - start);
  402. return 0;
  403. }
  404. /*
  405. * Notify userspace when a cpuset is released, by running
  406. * /sbin/cpuset_release_agent with the name of the cpuset (path
  407. * relative to the root of cpuset file system) as the argument.
  408. *
  409. * Most likely, this user command will try to rmdir this cpuset.
  410. *
  411. * This races with the possibility that some other task will be
  412. * attached to this cpuset before it is removed, or that some other
  413. * user task will 'mkdir' a child cpuset of this cpuset. That's ok.
  414. * The presumed 'rmdir' will fail quietly if this cpuset is no longer
  415. * unused, and this cpuset will be reprieved from its death sentence,
  416. * to continue to serve a useful existence. Next time it's released,
  417. * we will get notified again, if it still has 'notify_on_release' set.
  418. *
  419. * The final arg to call_usermodehelper() is 0, which means don't
  420. * wait. The separate /sbin/cpuset_release_agent task is forked by
  421. * call_usermodehelper(), then control in this thread returns here,
  422. * without waiting for the release agent task. We don't bother to
  423. * wait because the caller of this routine has no use for the exit
  424. * status of the /sbin/cpuset_release_agent task, so no sense holding
  425. * our caller up for that.
  426. *
  427. * When we had only one cpuset semaphore, we had to call this
  428. * without holding it, to avoid deadlock when call_usermodehelper()
  429. * allocated memory. With two locks, we could now call this while
  430. * holding manage_sem, but we still don't, so as to minimize
  431. * the time manage_sem is held.
  432. */
  433. static void cpuset_release_agent(const char *pathbuf)
  434. {
  435. char *argv[3], *envp[3];
  436. int i;
  437. if (!pathbuf)
  438. return;
  439. i = 0;
  440. argv[i++] = "/sbin/cpuset_release_agent";
  441. argv[i++] = (char *)pathbuf;
  442. argv[i] = NULL;
  443. i = 0;
  444. /* minimal command environment */
  445. envp[i++] = "HOME=/";
  446. envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  447. envp[i] = NULL;
  448. call_usermodehelper(argv[0], argv, envp, 0);
  449. kfree(pathbuf);
  450. }
  451. /*
  452. * Either cs->count of using tasks transitioned to zero, or the
  453. * cs->children list of child cpusets just became empty. If this
  454. * cs is notify_on_release() and now both the user count is zero and
  455. * the list of children is empty, prepare cpuset path in a kmalloc'd
  456. * buffer, to be returned via ppathbuf, so that the caller can invoke
  457. * cpuset_release_agent() with it later on, once manage_sem is dropped.
  458. * Call here with manage_sem held.
  459. *
  460. * This check_for_release() routine is responsible for kmalloc'ing
  461. * pathbuf. The above cpuset_release_agent() is responsible for
  462. * kfree'ing pathbuf. The caller of these routines is responsible
  463. * for providing a pathbuf pointer, initialized to NULL, then
  464. * calling check_for_release() with manage_sem held and the address
  465. * of the pathbuf pointer, then dropping manage_sem, then calling
  466. * cpuset_release_agent() with pathbuf, as set by check_for_release().
  467. */
  468. static void check_for_release(struct cpuset *cs, char **ppathbuf)
  469. {
  470. if (notify_on_release(cs) && atomic_read(&cs->count) == 0 &&
  471. list_empty(&cs->children)) {
  472. char *buf;
  473. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  474. if (!buf)
  475. return;
  476. if (cpuset_path(cs, buf, PAGE_SIZE) < 0)
  477. kfree(buf);
  478. else
  479. *ppathbuf = buf;
  480. }
  481. }
  482. /*
  483. * Return in *pmask the portion of a cpusets's cpus_allowed that
  484. * are online. If none are online, walk up the cpuset hierarchy
  485. * until we find one that does have some online cpus. If we get
  486. * all the way to the top and still haven't found any online cpus,
  487. * return cpu_online_map. Or if passed a NULL cs from an exit'ing
  488. * task, return cpu_online_map.
  489. *
  490. * One way or another, we guarantee to return some non-empty subset
  491. * of cpu_online_map.
  492. *
  493. * Call with callback_sem held.
  494. */
  495. static void guarantee_online_cpus(const struct cpuset *cs, cpumask_t *pmask)
  496. {
  497. while (cs && !cpus_intersects(cs->cpus_allowed, cpu_online_map))
  498. cs = cs->parent;
  499. if (cs)
  500. cpus_and(*pmask, cs->cpus_allowed, cpu_online_map);
  501. else
  502. *pmask = cpu_online_map;
  503. BUG_ON(!cpus_intersects(*pmask, cpu_online_map));
  504. }
  505. /*
  506. * Return in *pmask the portion of a cpusets's mems_allowed that
  507. * are online. If none are online, walk up the cpuset hierarchy
  508. * until we find one that does have some online mems. If we get
  509. * all the way to the top and still haven't found any online mems,
  510. * return node_online_map.
  511. *
  512. * One way or another, we guarantee to return some non-empty subset
  513. * of node_online_map.
  514. *
  515. * Call with callback_sem held.
  516. */
  517. static void guarantee_online_mems(const struct cpuset *cs, nodemask_t *pmask)
  518. {
  519. while (cs && !nodes_intersects(cs->mems_allowed, node_online_map))
  520. cs = cs->parent;
  521. if (cs)
  522. nodes_and(*pmask, cs->mems_allowed, node_online_map);
  523. else
  524. *pmask = node_online_map;
  525. BUG_ON(!nodes_intersects(*pmask, node_online_map));
  526. }
  527. /*
  528. * Refresh current tasks mems_allowed and mems_generation from current
  529. * tasks cpuset.
  530. *
  531. * Call without callback_sem or task_lock() held. May be called with
  532. * or without manage_sem held. Will acquire task_lock() and might
  533. * acquire callback_sem during call.
  534. *
  535. * The task_lock() is required to dereference current->cpuset safely.
  536. * Without it, we could pick up the pointer value of current->cpuset
  537. * in one instruction, and then attach_task could give us a different
  538. * cpuset, and then the cpuset we had could be removed and freed,
  539. * and then on our next instruction, we could dereference a no longer
  540. * valid cpuset pointer to get its mems_generation field.
  541. *
  542. * This routine is needed to update the per-task mems_allowed data,
  543. * within the tasks context, when it is trying to allocate memory
  544. * (in various mm/mempolicy.c routines) and notices that some other
  545. * task has been modifying its cpuset.
  546. */
  547. static void refresh_mems(void)
  548. {
  549. int my_cpusets_mem_gen;
  550. task_lock(current);
  551. my_cpusets_mem_gen = current->cpuset->mems_generation;
  552. task_unlock(current);
  553. if (current->cpuset_mems_generation != my_cpusets_mem_gen) {
  554. struct cpuset *cs;
  555. nodemask_t oldmem = current->mems_allowed;
  556. int migrate;
  557. down(&callback_sem);
  558. task_lock(current);
  559. cs = current->cpuset;
  560. migrate = is_memory_migrate(cs);
  561. guarantee_online_mems(cs, &current->mems_allowed);
  562. current->cpuset_mems_generation = cs->mems_generation;
  563. task_unlock(current);
  564. up(&callback_sem);
  565. if (!nodes_equal(oldmem, current->mems_allowed)) {
  566. numa_policy_rebind(&oldmem, &current->mems_allowed);
  567. if (migrate) {
  568. do_migrate_pages(current->mm, &oldmem,
  569. &current->mems_allowed,
  570. MPOL_MF_MOVE_ALL);
  571. }
  572. }
  573. }
  574. }
  575. /*
  576. * is_cpuset_subset(p, q) - Is cpuset p a subset of cpuset q?
  577. *
  578. * One cpuset is a subset of another if all its allowed CPUs and
  579. * Memory Nodes are a subset of the other, and its exclusive flags
  580. * are only set if the other's are set. Call holding manage_sem.
  581. */
  582. static int is_cpuset_subset(const struct cpuset *p, const struct cpuset *q)
  583. {
  584. return cpus_subset(p->cpus_allowed, q->cpus_allowed) &&
  585. nodes_subset(p->mems_allowed, q->mems_allowed) &&
  586. is_cpu_exclusive(p) <= is_cpu_exclusive(q) &&
  587. is_mem_exclusive(p) <= is_mem_exclusive(q);
  588. }
  589. /*
  590. * validate_change() - Used to validate that any proposed cpuset change
  591. * follows the structural rules for cpusets.
  592. *
  593. * If we replaced the flag and mask values of the current cpuset
  594. * (cur) with those values in the trial cpuset (trial), would
  595. * our various subset and exclusive rules still be valid? Presumes
  596. * manage_sem held.
  597. *
  598. * 'cur' is the address of an actual, in-use cpuset. Operations
  599. * such as list traversal that depend on the actual address of the
  600. * cpuset in the list must use cur below, not trial.
  601. *
  602. * 'trial' is the address of bulk structure copy of cur, with
  603. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  604. * or flags changed to new, trial values.
  605. *
  606. * Return 0 if valid, -errno if not.
  607. */
  608. static int validate_change(const struct cpuset *cur, const struct cpuset *trial)
  609. {
  610. struct cpuset *c, *par;
  611. /* Each of our child cpusets must be a subset of us */
  612. list_for_each_entry(c, &cur->children, sibling) {
  613. if (!is_cpuset_subset(c, trial))
  614. return -EBUSY;
  615. }
  616. /* Remaining checks don't apply to root cpuset */
  617. if ((par = cur->parent) == NULL)
  618. return 0;
  619. /* We must be a subset of our parent cpuset */
  620. if (!is_cpuset_subset(trial, par))
  621. return -EACCES;
  622. /* If either I or some sibling (!= me) is exclusive, we can't overlap */
  623. list_for_each_entry(c, &par->children, sibling) {
  624. if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
  625. c != cur &&
  626. cpus_intersects(trial->cpus_allowed, c->cpus_allowed))
  627. return -EINVAL;
  628. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  629. c != cur &&
  630. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  631. return -EINVAL;
  632. }
  633. return 0;
  634. }
  635. /*
  636. * For a given cpuset cur, partition the system as follows
  637. * a. All cpus in the parent cpuset's cpus_allowed that are not part of any
  638. * exclusive child cpusets
  639. * b. All cpus in the current cpuset's cpus_allowed that are not part of any
  640. * exclusive child cpusets
  641. * Build these two partitions by calling partition_sched_domains
  642. *
  643. * Call with manage_sem held. May nest a call to the
  644. * lock_cpu_hotplug()/unlock_cpu_hotplug() pair.
  645. */
  646. static void update_cpu_domains(struct cpuset *cur)
  647. {
  648. struct cpuset *c, *par = cur->parent;
  649. cpumask_t pspan, cspan;
  650. if (par == NULL || cpus_empty(cur->cpus_allowed))
  651. return;
  652. /*
  653. * Get all cpus from parent's cpus_allowed not part of exclusive
  654. * children
  655. */
  656. pspan = par->cpus_allowed;
  657. list_for_each_entry(c, &par->children, sibling) {
  658. if (is_cpu_exclusive(c))
  659. cpus_andnot(pspan, pspan, c->cpus_allowed);
  660. }
  661. if (is_removed(cur) || !is_cpu_exclusive(cur)) {
  662. cpus_or(pspan, pspan, cur->cpus_allowed);
  663. if (cpus_equal(pspan, cur->cpus_allowed))
  664. return;
  665. cspan = CPU_MASK_NONE;
  666. } else {
  667. if (cpus_empty(pspan))
  668. return;
  669. cspan = cur->cpus_allowed;
  670. /*
  671. * Get all cpus from current cpuset's cpus_allowed not part
  672. * of exclusive children
  673. */
  674. list_for_each_entry(c, &cur->children, sibling) {
  675. if (is_cpu_exclusive(c))
  676. cpus_andnot(cspan, cspan, c->cpus_allowed);
  677. }
  678. }
  679. lock_cpu_hotplug();
  680. partition_sched_domains(&pspan, &cspan);
  681. unlock_cpu_hotplug();
  682. }
  683. /*
  684. * Call with manage_sem held. May take callback_sem during call.
  685. */
  686. static int update_cpumask(struct cpuset *cs, char *buf)
  687. {
  688. struct cpuset trialcs;
  689. int retval, cpus_unchanged;
  690. trialcs = *cs;
  691. retval = cpulist_parse(buf, trialcs.cpus_allowed);
  692. if (retval < 0)
  693. return retval;
  694. cpus_and(trialcs.cpus_allowed, trialcs.cpus_allowed, cpu_online_map);
  695. if (cpus_empty(trialcs.cpus_allowed))
  696. return -ENOSPC;
  697. retval = validate_change(cs, &trialcs);
  698. if (retval < 0)
  699. return retval;
  700. cpus_unchanged = cpus_equal(cs->cpus_allowed, trialcs.cpus_allowed);
  701. down(&callback_sem);
  702. cs->cpus_allowed = trialcs.cpus_allowed;
  703. up(&callback_sem);
  704. if (is_cpu_exclusive(cs) && !cpus_unchanged)
  705. update_cpu_domains(cs);
  706. return 0;
  707. }
  708. /*
  709. * Call with manage_sem held. May take callback_sem during call.
  710. */
  711. static int update_nodemask(struct cpuset *cs, char *buf)
  712. {
  713. struct cpuset trialcs;
  714. int retval;
  715. trialcs = *cs;
  716. retval = nodelist_parse(buf, trialcs.mems_allowed);
  717. if (retval < 0)
  718. return retval;
  719. nodes_and(trialcs.mems_allowed, trialcs.mems_allowed, node_online_map);
  720. if (nodes_empty(trialcs.mems_allowed))
  721. return -ENOSPC;
  722. retval = validate_change(cs, &trialcs);
  723. if (retval == 0) {
  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. }
  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.
  1599. **/
  1600. void cpuset_exit(struct task_struct *tsk)
  1601. {
  1602. struct cpuset *cs;
  1603. BUG_ON(!(tsk->flags & PF_EXITING));
  1604. cs = tsk->cpuset;
  1605. tsk->cpuset = NULL;
  1606. if (notify_on_release(cs)) {
  1607. char *pathbuf = NULL;
  1608. down(&manage_sem);
  1609. if (atomic_dec_and_test(&cs->count))
  1610. check_for_release(cs, &pathbuf);
  1611. up(&manage_sem);
  1612. cpuset_release_agent(pathbuf);
  1613. } else {
  1614. atomic_dec(&cs->count);
  1615. }
  1616. }
  1617. /**
  1618. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  1619. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  1620. *
  1621. * Description: Returns the cpumask_t cpus_allowed of the cpuset
  1622. * attached to the specified @tsk. Guaranteed to return some non-empty
  1623. * subset of cpu_online_map, even if this means going outside the
  1624. * tasks cpuset.
  1625. **/
  1626. cpumask_t cpuset_cpus_allowed(const struct task_struct *tsk)
  1627. {
  1628. cpumask_t mask;
  1629. down(&callback_sem);
  1630. task_lock((struct task_struct *)tsk);
  1631. guarantee_online_cpus(tsk->cpuset, &mask);
  1632. task_unlock((struct task_struct *)tsk);
  1633. up(&callback_sem);
  1634. return mask;
  1635. }
  1636. void cpuset_init_current_mems_allowed(void)
  1637. {
  1638. current->mems_allowed = NODE_MASK_ALL;
  1639. }
  1640. /**
  1641. * cpuset_update_current_mems_allowed - update mems parameters to new values
  1642. *
  1643. * If the current tasks cpusets mems_allowed changed behind our backs,
  1644. * update current->mems_allowed and mems_generation to the new value.
  1645. * Do not call this routine if in_interrupt().
  1646. *
  1647. * Call without callback_sem or task_lock() held. May be called
  1648. * with or without manage_sem held. Unless exiting, it will acquire
  1649. * task_lock(). Also might acquire callback_sem during call to
  1650. * refresh_mems().
  1651. */
  1652. void cpuset_update_current_mems_allowed(void)
  1653. {
  1654. struct cpuset *cs;
  1655. int need_to_refresh = 0;
  1656. task_lock(current);
  1657. cs = current->cpuset;
  1658. if (!cs)
  1659. goto done;
  1660. if (current->cpuset_mems_generation != cs->mems_generation)
  1661. need_to_refresh = 1;
  1662. done:
  1663. task_unlock(current);
  1664. if (need_to_refresh)
  1665. refresh_mems();
  1666. }
  1667. /**
  1668. * cpuset_zonelist_valid_mems_allowed - check zonelist vs. curremt mems_allowed
  1669. * @zl: the zonelist to be checked
  1670. *
  1671. * Are any of the nodes on zonelist zl allowed in current->mems_allowed?
  1672. */
  1673. int cpuset_zonelist_valid_mems_allowed(struct zonelist *zl)
  1674. {
  1675. int i;
  1676. for (i = 0; zl->zones[i]; i++) {
  1677. int nid = zl->zones[i]->zone_pgdat->node_id;
  1678. if (node_isset(nid, current->mems_allowed))
  1679. return 1;
  1680. }
  1681. return 0;
  1682. }
  1683. /*
  1684. * nearest_exclusive_ancestor() - Returns the nearest mem_exclusive
  1685. * ancestor to the specified cpuset. Call holding callback_sem.
  1686. * If no ancestor is mem_exclusive (an unusual configuration), then
  1687. * returns the root cpuset.
  1688. */
  1689. static const struct cpuset *nearest_exclusive_ancestor(const struct cpuset *cs)
  1690. {
  1691. while (!is_mem_exclusive(cs) && cs->parent)
  1692. cs = cs->parent;
  1693. return cs;
  1694. }
  1695. /**
  1696. * cpuset_zone_allowed - Can we allocate memory on zone z's memory node?
  1697. * @z: is this zone on an allowed node?
  1698. * @gfp_mask: memory allocation flags (we use __GFP_HARDWALL)
  1699. *
  1700. * If we're in interrupt, yes, we can always allocate. If zone
  1701. * z's node is in our tasks mems_allowed, yes. If it's not a
  1702. * __GFP_HARDWALL request and this zone's nodes is in the nearest
  1703. * mem_exclusive cpuset ancestor to this tasks cpuset, yes.
  1704. * Otherwise, no.
  1705. *
  1706. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  1707. * and do not allow allocations outside the current tasks cpuset.
  1708. * GFP_KERNEL allocations are not so marked, so can escape to the
  1709. * nearest mem_exclusive ancestor cpuset.
  1710. *
  1711. * Scanning up parent cpusets requires callback_sem. The __alloc_pages()
  1712. * routine only calls here with __GFP_HARDWALL bit _not_ set if
  1713. * it's a GFP_KERNEL allocation, and all nodes in the current tasks
  1714. * mems_allowed came up empty on the first pass over the zonelist.
  1715. * So only GFP_KERNEL allocations, if all nodes in the cpuset are
  1716. * short of memory, might require taking the callback_sem semaphore.
  1717. *
  1718. * The first loop over the zonelist in mm/page_alloc.c:__alloc_pages()
  1719. * calls here with __GFP_HARDWALL always set in gfp_mask, enforcing
  1720. * hardwall cpusets - no allocation on a node outside the cpuset is
  1721. * allowed (unless in interrupt, of course).
  1722. *
  1723. * The second loop doesn't even call here for GFP_ATOMIC requests
  1724. * (if the __alloc_pages() local variable 'wait' is set). That check
  1725. * and the checks below have the combined affect in the second loop of
  1726. * the __alloc_pages() routine that:
  1727. * in_interrupt - any node ok (current task context irrelevant)
  1728. * GFP_ATOMIC - any node ok
  1729. * GFP_KERNEL - any node in enclosing mem_exclusive cpuset ok
  1730. * GFP_USER - only nodes in current tasks mems allowed ok.
  1731. **/
  1732. int cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
  1733. {
  1734. int node; /* node that zone z is on */
  1735. const struct cpuset *cs; /* current cpuset ancestors */
  1736. int allowed = 1; /* is allocation in zone z allowed? */
  1737. if (in_interrupt())
  1738. return 1;
  1739. node = z->zone_pgdat->node_id;
  1740. if (node_isset(node, current->mems_allowed))
  1741. return 1;
  1742. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  1743. return 0;
  1744. if (current->flags & PF_EXITING) /* Let dying task have memory */
  1745. return 1;
  1746. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  1747. down(&callback_sem);
  1748. task_lock(current);
  1749. cs = nearest_exclusive_ancestor(current->cpuset);
  1750. task_unlock(current);
  1751. allowed = node_isset(node, cs->mems_allowed);
  1752. up(&callback_sem);
  1753. return allowed;
  1754. }
  1755. /**
  1756. * cpuset_excl_nodes_overlap - Do we overlap @p's mem_exclusive ancestors?
  1757. * @p: pointer to task_struct of some other task.
  1758. *
  1759. * Description: Return true if the nearest mem_exclusive ancestor
  1760. * cpusets of tasks @p and current overlap. Used by oom killer to
  1761. * determine if task @p's memory usage might impact the memory
  1762. * available to the current task.
  1763. *
  1764. * Acquires callback_sem - not suitable for calling from a fast path.
  1765. **/
  1766. int cpuset_excl_nodes_overlap(const struct task_struct *p)
  1767. {
  1768. const struct cpuset *cs1, *cs2; /* my and p's cpuset ancestors */
  1769. int overlap = 0; /* do cpusets overlap? */
  1770. down(&callback_sem);
  1771. task_lock(current);
  1772. if (current->flags & PF_EXITING) {
  1773. task_unlock(current);
  1774. goto done;
  1775. }
  1776. cs1 = nearest_exclusive_ancestor(current->cpuset);
  1777. task_unlock(current);
  1778. task_lock((struct task_struct *)p);
  1779. if (p->flags & PF_EXITING) {
  1780. task_unlock((struct task_struct *)p);
  1781. goto done;
  1782. }
  1783. cs2 = nearest_exclusive_ancestor(p->cpuset);
  1784. task_unlock((struct task_struct *)p);
  1785. overlap = nodes_intersects(cs1->mems_allowed, cs2->mems_allowed);
  1786. done:
  1787. up(&callback_sem);
  1788. return overlap;
  1789. }
  1790. /*
  1791. * Collection of memory_pressure is suppressed unless
  1792. * this flag is enabled by writing "1" to the special
  1793. * cpuset file 'memory_pressure_enabled' in the root cpuset.
  1794. */
  1795. int cpuset_memory_pressure_enabled;
  1796. /**
  1797. * cpuset_memory_pressure_bump - keep stats of per-cpuset reclaims.
  1798. *
  1799. * Keep a running average of the rate of synchronous (direct)
  1800. * page reclaim efforts initiated by tasks in each cpuset.
  1801. *
  1802. * This represents the rate at which some task in the cpuset
  1803. * ran low on memory on all nodes it was allowed to use, and
  1804. * had to enter the kernels page reclaim code in an effort to
  1805. * create more free memory by tossing clean pages or swapping
  1806. * or writing dirty pages.
  1807. *
  1808. * Display to user space in the per-cpuset read-only file
  1809. * "memory_pressure". Value displayed is an integer
  1810. * representing the recent rate of entry into the synchronous
  1811. * (direct) page reclaim by any task attached to the cpuset.
  1812. **/
  1813. void __cpuset_memory_pressure_bump(void)
  1814. {
  1815. struct cpuset *cs;
  1816. task_lock(current);
  1817. cs = current->cpuset;
  1818. fmeter_markevent(&cs->fmeter);
  1819. task_unlock(current);
  1820. }
  1821. /*
  1822. * proc_cpuset_show()
  1823. * - Print tasks cpuset path into seq_file.
  1824. * - Used for /proc/<pid>/cpuset.
  1825. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  1826. * doesn't really matter if tsk->cpuset changes after we read it,
  1827. * and we take manage_sem, keeping attach_task() from changing it
  1828. * anyway.
  1829. */
  1830. static int proc_cpuset_show(struct seq_file *m, void *v)
  1831. {
  1832. struct cpuset *cs;
  1833. struct task_struct *tsk;
  1834. char *buf;
  1835. int retval = 0;
  1836. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1837. if (!buf)
  1838. return -ENOMEM;
  1839. tsk = m->private;
  1840. down(&manage_sem);
  1841. cs = tsk->cpuset;
  1842. if (!cs) {
  1843. retval = -EINVAL;
  1844. goto out;
  1845. }
  1846. retval = cpuset_path(cs, buf, PAGE_SIZE);
  1847. if (retval < 0)
  1848. goto out;
  1849. seq_puts(m, buf);
  1850. seq_putc(m, '\n');
  1851. out:
  1852. up(&manage_sem);
  1853. kfree(buf);
  1854. return retval;
  1855. }
  1856. static int cpuset_open(struct inode *inode, struct file *file)
  1857. {
  1858. struct task_struct *tsk = PROC_I(inode)->task;
  1859. return single_open(file, proc_cpuset_show, tsk);
  1860. }
  1861. struct file_operations proc_cpuset_operations = {
  1862. .open = cpuset_open,
  1863. .read = seq_read,
  1864. .llseek = seq_lseek,
  1865. .release = single_release,
  1866. };
  1867. /* Display task cpus_allowed, mems_allowed in /proc/<pid>/status file. */
  1868. char *cpuset_task_status_allowed(struct task_struct *task, char *buffer)
  1869. {
  1870. buffer += sprintf(buffer, "Cpus_allowed:\t");
  1871. buffer += cpumask_scnprintf(buffer, PAGE_SIZE, task->cpus_allowed);
  1872. buffer += sprintf(buffer, "\n");
  1873. buffer += sprintf(buffer, "Mems_allowed:\t");
  1874. buffer += nodemask_scnprintf(buffer, PAGE_SIZE, task->mems_allowed);
  1875. buffer += sprintf(buffer, "\n");
  1876. return buffer;
  1877. }