cpuset.c 69 KB

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