cpuset.c 67 KB

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