mempolicy.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. /*
  2. * Simple NUMA memory policy for the Linux kernel.
  3. *
  4. * Copyright 2003,2004 Andi Kleen, SuSE Labs.
  5. * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
  6. * Subject to the GNU Public License, version 2.
  7. *
  8. * NUMA policy allows the user to give hints in which node(s) memory should
  9. * be allocated.
  10. *
  11. * Support four policies per VMA and per process:
  12. *
  13. * The VMA policy has priority over the process policy for a page fault.
  14. *
  15. * interleave Allocate memory interleaved over a set of nodes,
  16. * with normal fallback if it fails.
  17. * For VMA based allocations this interleaves based on the
  18. * offset into the backing object or offset into the mapping
  19. * for anonymous memory. For process policy an process counter
  20. * is used.
  21. *
  22. * bind Only allocate memory on a specific set of nodes,
  23. * no fallback.
  24. * FIXME: memory is allocated starting with the first node
  25. * to the last. It would be better if bind would truly restrict
  26. * the allocation to memory nodes instead
  27. *
  28. * preferred Try a specific node first before normal fallback.
  29. * As a special case node -1 here means do the allocation
  30. * on the local CPU. This is normally identical to default,
  31. * but useful to set in a VMA when you have a non default
  32. * process policy.
  33. *
  34. * default Allocate on the local node first, or when on a VMA
  35. * use the process policy. This is what Linux always did
  36. * in a NUMA aware kernel and still does by, ahem, default.
  37. *
  38. * The process policy is applied for most non interrupt memory allocations
  39. * in that process' context. Interrupts ignore the policies and always
  40. * try to allocate on the local CPU. The VMA policy is only applied for memory
  41. * allocations for a VMA in the VM.
  42. *
  43. * Currently there are a few corner cases in swapping where the policy
  44. * is not applied, but the majority should be handled. When process policy
  45. * is used it is not remembered over swap outs/swap ins.
  46. *
  47. * Only the highest zone in the zone hierarchy gets policied. Allocations
  48. * requesting a lower zone just use default policy. This implies that
  49. * on systems with highmem kernel lowmem allocation don't get policied.
  50. * Same with GFP_DMA allocations.
  51. *
  52. * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
  53. * all users and remembered even when nobody has memory mapped.
  54. */
  55. /* Notebook:
  56. fix mmap readahead to honour policy and enable policy for any page cache
  57. object
  58. statistics for bigpages
  59. global policy for page cache? currently it uses process policy. Requires
  60. first item above.
  61. handle mremap for shared memory (currently ignored for the policy)
  62. grows down?
  63. make bind policy root only? It can trigger oom much faster and the
  64. kernel is not always grateful with that.
  65. */
  66. #include <linux/mempolicy.h>
  67. #include <linux/mm.h>
  68. #include <linux/highmem.h>
  69. #include <linux/hugetlb.h>
  70. #include <linux/kernel.h>
  71. #include <linux/sched.h>
  72. #include <linux/nodemask.h>
  73. #include <linux/cpuset.h>
  74. #include <linux/slab.h>
  75. #include <linux/string.h>
  76. #include <linux/export.h>
  77. #include <linux/nsproxy.h>
  78. #include <linux/interrupt.h>
  79. #include <linux/init.h>
  80. #include <linux/compat.h>
  81. #include <linux/swap.h>
  82. #include <linux/seq_file.h>
  83. #include <linux/proc_fs.h>
  84. #include <linux/migrate.h>
  85. #include <linux/ksm.h>
  86. #include <linux/rmap.h>
  87. #include <linux/security.h>
  88. #include <linux/syscalls.h>
  89. #include <linux/ctype.h>
  90. #include <linux/mm_inline.h>
  91. #include <asm/tlbflush.h>
  92. #include <asm/uaccess.h>
  93. #include <linux/random.h>
  94. #include "internal.h"
  95. /* Internal flags */
  96. #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */
  97. #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */
  98. static struct kmem_cache *policy_cache;
  99. static struct kmem_cache *sn_cache;
  100. /* Highest zone. An specific allocation for a zone below that is not
  101. policied. */
  102. enum zone_type policy_zone = 0;
  103. /*
  104. * run-time system-wide default policy => local allocation
  105. */
  106. static struct mempolicy default_policy = {
  107. .refcnt = ATOMIC_INIT(1), /* never free it */
  108. .mode = MPOL_PREFERRED,
  109. .flags = MPOL_F_LOCAL,
  110. };
  111. static const struct mempolicy_operations {
  112. int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
  113. /*
  114. * If read-side task has no lock to protect task->mempolicy, write-side
  115. * task will rebind the task->mempolicy by two step. The first step is
  116. * setting all the newly nodes, and the second step is cleaning all the
  117. * disallowed nodes. In this way, we can avoid finding no node to alloc
  118. * page.
  119. * If we have a lock to protect task->mempolicy in read-side, we do
  120. * rebind directly.
  121. *
  122. * step:
  123. * MPOL_REBIND_ONCE - do rebind work at once
  124. * MPOL_REBIND_STEP1 - set all the newly nodes
  125. * MPOL_REBIND_STEP2 - clean all the disallowed nodes
  126. */
  127. void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes,
  128. enum mpol_rebind_step step);
  129. } mpol_ops[MPOL_MAX];
  130. /* Check that the nodemask contains at least one populated zone */
  131. static int is_valid_nodemask(const nodemask_t *nodemask)
  132. {
  133. int nd, k;
  134. for_each_node_mask(nd, *nodemask) {
  135. struct zone *z;
  136. for (k = 0; k <= policy_zone; k++) {
  137. z = &NODE_DATA(nd)->node_zones[k];
  138. if (z->present_pages > 0)
  139. return 1;
  140. }
  141. }
  142. return 0;
  143. }
  144. static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
  145. {
  146. return pol->flags & MPOL_MODE_FLAGS;
  147. }
  148. static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
  149. const nodemask_t *rel)
  150. {
  151. nodemask_t tmp;
  152. nodes_fold(tmp, *orig, nodes_weight(*rel));
  153. nodes_onto(*ret, tmp, *rel);
  154. }
  155. static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
  156. {
  157. if (nodes_empty(*nodes))
  158. return -EINVAL;
  159. pol->v.nodes = *nodes;
  160. return 0;
  161. }
  162. static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
  163. {
  164. if (!nodes)
  165. pol->flags |= MPOL_F_LOCAL; /* local allocation */
  166. else if (nodes_empty(*nodes))
  167. return -EINVAL; /* no allowed nodes */
  168. else
  169. pol->v.preferred_node = first_node(*nodes);
  170. return 0;
  171. }
  172. static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
  173. {
  174. if (!is_valid_nodemask(nodes))
  175. return -EINVAL;
  176. pol->v.nodes = *nodes;
  177. return 0;
  178. }
  179. /*
  180. * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
  181. * any, for the new policy. mpol_new() has already validated the nodes
  182. * parameter with respect to the policy mode and flags. But, we need to
  183. * handle an empty nodemask with MPOL_PREFERRED here.
  184. *
  185. * Must be called holding task's alloc_lock to protect task's mems_allowed
  186. * and mempolicy. May also be called holding the mmap_semaphore for write.
  187. */
  188. static int mpol_set_nodemask(struct mempolicy *pol,
  189. const nodemask_t *nodes, struct nodemask_scratch *nsc)
  190. {
  191. int ret;
  192. /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
  193. if (pol == NULL)
  194. return 0;
  195. /* Check N_MEMORY */
  196. nodes_and(nsc->mask1,
  197. cpuset_current_mems_allowed, node_states[N_MEMORY]);
  198. VM_BUG_ON(!nodes);
  199. if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
  200. nodes = NULL; /* explicit local allocation */
  201. else {
  202. if (pol->flags & MPOL_F_RELATIVE_NODES)
  203. mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1);
  204. else
  205. nodes_and(nsc->mask2, *nodes, nsc->mask1);
  206. if (mpol_store_user_nodemask(pol))
  207. pol->w.user_nodemask = *nodes;
  208. else
  209. pol->w.cpuset_mems_allowed =
  210. cpuset_current_mems_allowed;
  211. }
  212. if (nodes)
  213. ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
  214. else
  215. ret = mpol_ops[pol->mode].create(pol, NULL);
  216. return ret;
  217. }
  218. /*
  219. * This function just creates a new policy, does some check and simple
  220. * initialization. You must invoke mpol_set_nodemask() to set nodes.
  221. */
  222. static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
  223. nodemask_t *nodes)
  224. {
  225. struct mempolicy *policy;
  226. pr_debug("setting mode %d flags %d nodes[0] %lx\n",
  227. mode, flags, nodes ? nodes_addr(*nodes)[0] : -1);
  228. if (mode == MPOL_DEFAULT) {
  229. if (nodes && !nodes_empty(*nodes))
  230. return ERR_PTR(-EINVAL);
  231. return NULL; /* simply delete any existing policy */
  232. }
  233. VM_BUG_ON(!nodes);
  234. /*
  235. * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
  236. * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
  237. * All other modes require a valid pointer to a non-empty nodemask.
  238. */
  239. if (mode == MPOL_PREFERRED) {
  240. if (nodes_empty(*nodes)) {
  241. if (((flags & MPOL_F_STATIC_NODES) ||
  242. (flags & MPOL_F_RELATIVE_NODES)))
  243. return ERR_PTR(-EINVAL);
  244. }
  245. } else if (nodes_empty(*nodes))
  246. return ERR_PTR(-EINVAL);
  247. policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
  248. if (!policy)
  249. return ERR_PTR(-ENOMEM);
  250. atomic_set(&policy->refcnt, 1);
  251. policy->mode = mode;
  252. policy->flags = flags;
  253. return policy;
  254. }
  255. /* Slow path of a mpol destructor. */
  256. void __mpol_put(struct mempolicy *p)
  257. {
  258. if (!atomic_dec_and_test(&p->refcnt))
  259. return;
  260. kmem_cache_free(policy_cache, p);
  261. }
  262. static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes,
  263. enum mpol_rebind_step step)
  264. {
  265. }
  266. /*
  267. * step:
  268. * MPOL_REBIND_ONCE - do rebind work at once
  269. * MPOL_REBIND_STEP1 - set all the newly nodes
  270. * MPOL_REBIND_STEP2 - clean all the disallowed nodes
  271. */
  272. static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes,
  273. enum mpol_rebind_step step)
  274. {
  275. nodemask_t tmp;
  276. if (pol->flags & MPOL_F_STATIC_NODES)
  277. nodes_and(tmp, pol->w.user_nodemask, *nodes);
  278. else if (pol->flags & MPOL_F_RELATIVE_NODES)
  279. mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
  280. else {
  281. /*
  282. * if step == 1, we use ->w.cpuset_mems_allowed to cache the
  283. * result
  284. */
  285. if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) {
  286. nodes_remap(tmp, pol->v.nodes,
  287. pol->w.cpuset_mems_allowed, *nodes);
  288. pol->w.cpuset_mems_allowed = step ? tmp : *nodes;
  289. } else if (step == MPOL_REBIND_STEP2) {
  290. tmp = pol->w.cpuset_mems_allowed;
  291. pol->w.cpuset_mems_allowed = *nodes;
  292. } else
  293. BUG();
  294. }
  295. if (nodes_empty(tmp))
  296. tmp = *nodes;
  297. if (step == MPOL_REBIND_STEP1)
  298. nodes_or(pol->v.nodes, pol->v.nodes, tmp);
  299. else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2)
  300. pol->v.nodes = tmp;
  301. else
  302. BUG();
  303. if (!node_isset(current->il_next, tmp)) {
  304. current->il_next = next_node(current->il_next, tmp);
  305. if (current->il_next >= MAX_NUMNODES)
  306. current->il_next = first_node(tmp);
  307. if (current->il_next >= MAX_NUMNODES)
  308. current->il_next = numa_node_id();
  309. }
  310. }
  311. static void mpol_rebind_preferred(struct mempolicy *pol,
  312. const nodemask_t *nodes,
  313. enum mpol_rebind_step step)
  314. {
  315. nodemask_t tmp;
  316. if (pol->flags & MPOL_F_STATIC_NODES) {
  317. int node = first_node(pol->w.user_nodemask);
  318. if (node_isset(node, *nodes)) {
  319. pol->v.preferred_node = node;
  320. pol->flags &= ~MPOL_F_LOCAL;
  321. } else
  322. pol->flags |= MPOL_F_LOCAL;
  323. } else if (pol->flags & MPOL_F_RELATIVE_NODES) {
  324. mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
  325. pol->v.preferred_node = first_node(tmp);
  326. } else if (!(pol->flags & MPOL_F_LOCAL)) {
  327. pol->v.preferred_node = node_remap(pol->v.preferred_node,
  328. pol->w.cpuset_mems_allowed,
  329. *nodes);
  330. pol->w.cpuset_mems_allowed = *nodes;
  331. }
  332. }
  333. /*
  334. * mpol_rebind_policy - Migrate a policy to a different set of nodes
  335. *
  336. * If read-side task has no lock to protect task->mempolicy, write-side
  337. * task will rebind the task->mempolicy by two step. The first step is
  338. * setting all the newly nodes, and the second step is cleaning all the
  339. * disallowed nodes. In this way, we can avoid finding no node to alloc
  340. * page.
  341. * If we have a lock to protect task->mempolicy in read-side, we do
  342. * rebind directly.
  343. *
  344. * step:
  345. * MPOL_REBIND_ONCE - do rebind work at once
  346. * MPOL_REBIND_STEP1 - set all the newly nodes
  347. * MPOL_REBIND_STEP2 - clean all the disallowed nodes
  348. */
  349. static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask,
  350. enum mpol_rebind_step step)
  351. {
  352. if (!pol)
  353. return;
  354. if (!mpol_store_user_nodemask(pol) && step == 0 &&
  355. nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
  356. return;
  357. if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING))
  358. return;
  359. if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING))
  360. BUG();
  361. if (step == MPOL_REBIND_STEP1)
  362. pol->flags |= MPOL_F_REBINDING;
  363. else if (step == MPOL_REBIND_STEP2)
  364. pol->flags &= ~MPOL_F_REBINDING;
  365. else if (step >= MPOL_REBIND_NSTEP)
  366. BUG();
  367. mpol_ops[pol->mode].rebind(pol, newmask, step);
  368. }
  369. /*
  370. * Wrapper for mpol_rebind_policy() that just requires task
  371. * pointer, and updates task mempolicy.
  372. *
  373. * Called with task's alloc_lock held.
  374. */
  375. void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
  376. enum mpol_rebind_step step)
  377. {
  378. mpol_rebind_policy(tsk->mempolicy, new, step);
  379. }
  380. /*
  381. * Rebind each vma in mm to new nodemask.
  382. *
  383. * Call holding a reference to mm. Takes mm->mmap_sem during call.
  384. */
  385. void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  386. {
  387. struct vm_area_struct *vma;
  388. down_write(&mm->mmap_sem);
  389. for (vma = mm->mmap; vma; vma = vma->vm_next)
  390. mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE);
  391. up_write(&mm->mmap_sem);
  392. }
  393. static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
  394. [MPOL_DEFAULT] = {
  395. .rebind = mpol_rebind_default,
  396. },
  397. [MPOL_INTERLEAVE] = {
  398. .create = mpol_new_interleave,
  399. .rebind = mpol_rebind_nodemask,
  400. },
  401. [MPOL_PREFERRED] = {
  402. .create = mpol_new_preferred,
  403. .rebind = mpol_rebind_preferred,
  404. },
  405. [MPOL_BIND] = {
  406. .create = mpol_new_bind,
  407. .rebind = mpol_rebind_nodemask,
  408. },
  409. };
  410. static void migrate_page_add(struct page *page, struct list_head *pagelist,
  411. unsigned long flags);
  412. /* Scan through pages checking if pages follow certain conditions. */
  413. static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  414. unsigned long addr, unsigned long end,
  415. const nodemask_t *nodes, unsigned long flags,
  416. void *private)
  417. {
  418. pte_t *orig_pte;
  419. pte_t *pte;
  420. spinlock_t *ptl;
  421. orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  422. do {
  423. struct page *page;
  424. int nid;
  425. if (!pte_present(*pte))
  426. continue;
  427. page = vm_normal_page(vma, addr, *pte);
  428. if (!page)
  429. continue;
  430. /*
  431. * vm_normal_page() filters out zero pages, but there might
  432. * still be PageReserved pages to skip, perhaps in a VDSO.
  433. * And we cannot move PageKsm pages sensibly or safely yet.
  434. */
  435. if (PageReserved(page) || PageKsm(page))
  436. continue;
  437. nid = page_to_nid(page);
  438. if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
  439. continue;
  440. if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
  441. migrate_page_add(page, private, flags);
  442. else
  443. break;
  444. } while (pte++, addr += PAGE_SIZE, addr != end);
  445. pte_unmap_unlock(orig_pte, ptl);
  446. return addr != end;
  447. }
  448. static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  449. unsigned long addr, unsigned long end,
  450. const nodemask_t *nodes, unsigned long flags,
  451. void *private)
  452. {
  453. pmd_t *pmd;
  454. unsigned long next;
  455. pmd = pmd_offset(pud, addr);
  456. do {
  457. next = pmd_addr_end(addr, end);
  458. split_huge_page_pmd(vma->vm_mm, pmd);
  459. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  460. continue;
  461. if (check_pte_range(vma, pmd, addr, next, nodes,
  462. flags, private))
  463. return -EIO;
  464. } while (pmd++, addr = next, addr != end);
  465. return 0;
  466. }
  467. static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  468. unsigned long addr, unsigned long end,
  469. const nodemask_t *nodes, unsigned long flags,
  470. void *private)
  471. {
  472. pud_t *pud;
  473. unsigned long next;
  474. pud = pud_offset(pgd, addr);
  475. do {
  476. next = pud_addr_end(addr, end);
  477. if (pud_none_or_clear_bad(pud))
  478. continue;
  479. if (check_pmd_range(vma, pud, addr, next, nodes,
  480. flags, private))
  481. return -EIO;
  482. } while (pud++, addr = next, addr != end);
  483. return 0;
  484. }
  485. static inline int check_pgd_range(struct vm_area_struct *vma,
  486. unsigned long addr, unsigned long end,
  487. const nodemask_t *nodes, unsigned long flags,
  488. void *private)
  489. {
  490. pgd_t *pgd;
  491. unsigned long next;
  492. pgd = pgd_offset(vma->vm_mm, addr);
  493. do {
  494. next = pgd_addr_end(addr, end);
  495. if (pgd_none_or_clear_bad(pgd))
  496. continue;
  497. if (check_pud_range(vma, pgd, addr, next, nodes,
  498. flags, private))
  499. return -EIO;
  500. } while (pgd++, addr = next, addr != end);
  501. return 0;
  502. }
  503. /*
  504. * Check if all pages in a range are on a set of nodes.
  505. * If pagelist != NULL then isolate pages from the LRU and
  506. * put them on the pagelist.
  507. */
  508. static int
  509. check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
  510. const nodemask_t *nodes, unsigned long flags, void *private)
  511. {
  512. int err = 0;
  513. struct vm_area_struct *vma, *prev;
  514. vma = find_vma(mm, start);
  515. if (!vma)
  516. return -EFAULT;
  517. prev = NULL;
  518. for (; vma && vma->vm_start < end; vma = vma->vm_next) {
  519. if (!(flags & MPOL_MF_DISCONTIG_OK)) {
  520. if (!vma->vm_next && vma->vm_end < end)
  521. return -EFAULT;
  522. if (prev && prev->vm_end < vma->vm_start)
  523. return -EFAULT;
  524. }
  525. if (!is_vm_hugetlb_page(vma) &&
  526. ((flags & MPOL_MF_STRICT) ||
  527. ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
  528. vma_migratable(vma)))) {
  529. unsigned long endvma = vma->vm_end;
  530. if (endvma > end)
  531. endvma = end;
  532. if (vma->vm_start > start)
  533. start = vma->vm_start;
  534. err = check_pgd_range(vma, start, endvma, nodes,
  535. flags, private);
  536. if (err)
  537. break;
  538. }
  539. prev = vma;
  540. }
  541. return err;
  542. }
  543. /*
  544. * Apply policy to a single VMA
  545. * This must be called with the mmap_sem held for writing.
  546. */
  547. static int vma_replace_policy(struct vm_area_struct *vma,
  548. struct mempolicy *pol)
  549. {
  550. int err;
  551. struct mempolicy *old;
  552. struct mempolicy *new;
  553. pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
  554. vma->vm_start, vma->vm_end, vma->vm_pgoff,
  555. vma->vm_ops, vma->vm_file,
  556. vma->vm_ops ? vma->vm_ops->set_policy : NULL);
  557. new = mpol_dup(pol);
  558. if (IS_ERR(new))
  559. return PTR_ERR(new);
  560. if (vma->vm_ops && vma->vm_ops->set_policy) {
  561. err = vma->vm_ops->set_policy(vma, new);
  562. if (err)
  563. goto err_out;
  564. }
  565. old = vma->vm_policy;
  566. vma->vm_policy = new; /* protected by mmap_sem */
  567. mpol_put(old);
  568. return 0;
  569. err_out:
  570. mpol_put(new);
  571. return err;
  572. }
  573. /* Step 2: apply policy to a range and do splits. */
  574. static int mbind_range(struct mm_struct *mm, unsigned long start,
  575. unsigned long end, struct mempolicy *new_pol)
  576. {
  577. struct vm_area_struct *next;
  578. struct vm_area_struct *prev;
  579. struct vm_area_struct *vma;
  580. int err = 0;
  581. pgoff_t pgoff;
  582. unsigned long vmstart;
  583. unsigned long vmend;
  584. vma = find_vma(mm, start);
  585. if (!vma || vma->vm_start > start)
  586. return -EFAULT;
  587. prev = vma->vm_prev;
  588. if (start > vma->vm_start)
  589. prev = vma;
  590. for (; vma && vma->vm_start < end; prev = vma, vma = next) {
  591. next = vma->vm_next;
  592. vmstart = max(start, vma->vm_start);
  593. vmend = min(end, vma->vm_end);
  594. if (mpol_equal(vma_policy(vma), new_pol))
  595. continue;
  596. pgoff = vma->vm_pgoff +
  597. ((vmstart - vma->vm_start) >> PAGE_SHIFT);
  598. prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
  599. vma->anon_vma, vma->vm_file, pgoff,
  600. new_pol, vma_get_anon_name(vma));
  601. if (prev) {
  602. vma = prev;
  603. next = vma->vm_next;
  604. continue;
  605. }
  606. if (vma->vm_start != vmstart) {
  607. err = split_vma(vma->vm_mm, vma, vmstart, 1);
  608. if (err)
  609. goto out;
  610. }
  611. if (vma->vm_end != vmend) {
  612. err = split_vma(vma->vm_mm, vma, vmend, 0);
  613. if (err)
  614. goto out;
  615. }
  616. err = vma_replace_policy(vma, new_pol);
  617. if (err)
  618. goto out;
  619. }
  620. out:
  621. return err;
  622. }
  623. /*
  624. * Update task->flags PF_MEMPOLICY bit: set iff non-default
  625. * mempolicy. Allows more rapid checking of this (combined perhaps
  626. * with other PF_* flag bits) on memory allocation hot code paths.
  627. *
  628. * If called from outside this file, the task 'p' should -only- be
  629. * a newly forked child not yet visible on the task list, because
  630. * manipulating the task flags of a visible task is not safe.
  631. *
  632. * The above limitation is why this routine has the funny name
  633. * mpol_fix_fork_child_flag().
  634. *
  635. * It is also safe to call this with a task pointer of current,
  636. * which the static wrapper mpol_set_task_struct_flag() does,
  637. * for use within this file.
  638. */
  639. void mpol_fix_fork_child_flag(struct task_struct *p)
  640. {
  641. if (p->mempolicy)
  642. p->flags |= PF_MEMPOLICY;
  643. else
  644. p->flags &= ~PF_MEMPOLICY;
  645. }
  646. static void mpol_set_task_struct_flag(void)
  647. {
  648. mpol_fix_fork_child_flag(current);
  649. }
  650. /* Set the process memory policy */
  651. static long do_set_mempolicy(unsigned short mode, unsigned short flags,
  652. nodemask_t *nodes)
  653. {
  654. struct mempolicy *new, *old;
  655. struct mm_struct *mm = current->mm;
  656. NODEMASK_SCRATCH(scratch);
  657. int ret;
  658. if (!scratch)
  659. return -ENOMEM;
  660. new = mpol_new(mode, flags, nodes);
  661. if (IS_ERR(new)) {
  662. ret = PTR_ERR(new);
  663. goto out;
  664. }
  665. /*
  666. * prevent changing our mempolicy while show_numa_maps()
  667. * is using it.
  668. * Note: do_set_mempolicy() can be called at init time
  669. * with no 'mm'.
  670. */
  671. if (mm)
  672. down_write(&mm->mmap_sem);
  673. task_lock(current);
  674. ret = mpol_set_nodemask(new, nodes, scratch);
  675. if (ret) {
  676. task_unlock(current);
  677. if (mm)
  678. up_write(&mm->mmap_sem);
  679. mpol_put(new);
  680. goto out;
  681. }
  682. old = current->mempolicy;
  683. current->mempolicy = new;
  684. mpol_set_task_struct_flag();
  685. if (new && new->mode == MPOL_INTERLEAVE &&
  686. nodes_weight(new->v.nodes))
  687. current->il_next = first_node(new->v.nodes);
  688. task_unlock(current);
  689. if (mm)
  690. up_write(&mm->mmap_sem);
  691. mpol_put(old);
  692. ret = 0;
  693. out:
  694. NODEMASK_SCRATCH_FREE(scratch);
  695. return ret;
  696. }
  697. /*
  698. * Return nodemask for policy for get_mempolicy() query
  699. *
  700. * Called with task's alloc_lock held
  701. */
  702. static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
  703. {
  704. nodes_clear(*nodes);
  705. if (p == &default_policy)
  706. return;
  707. switch (p->mode) {
  708. case MPOL_BIND:
  709. /* Fall through */
  710. case MPOL_INTERLEAVE:
  711. *nodes = p->v.nodes;
  712. break;
  713. case MPOL_PREFERRED:
  714. if (!(p->flags & MPOL_F_LOCAL))
  715. node_set(p->v.preferred_node, *nodes);
  716. /* else return empty node mask for local allocation */
  717. break;
  718. default:
  719. BUG();
  720. }
  721. }
  722. static int lookup_node(struct mm_struct *mm, unsigned long addr)
  723. {
  724. struct page *p;
  725. int err;
  726. err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
  727. if (err >= 0) {
  728. err = page_to_nid(p);
  729. put_page(p);
  730. }
  731. return err;
  732. }
  733. /* Retrieve NUMA policy */
  734. static long do_get_mempolicy(int *policy, nodemask_t *nmask,
  735. unsigned long addr, unsigned long flags)
  736. {
  737. int err;
  738. struct mm_struct *mm = current->mm;
  739. struct vm_area_struct *vma = NULL;
  740. struct mempolicy *pol = current->mempolicy;
  741. if (flags &
  742. ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
  743. return -EINVAL;
  744. if (flags & MPOL_F_MEMS_ALLOWED) {
  745. if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
  746. return -EINVAL;
  747. *policy = 0; /* just so it's initialized */
  748. task_lock(current);
  749. *nmask = cpuset_current_mems_allowed;
  750. task_unlock(current);
  751. return 0;
  752. }
  753. if (flags & MPOL_F_ADDR) {
  754. /*
  755. * Do NOT fall back to task policy if the
  756. * vma/shared policy at addr is NULL. We
  757. * want to return MPOL_DEFAULT in this case.
  758. */
  759. down_read(&mm->mmap_sem);
  760. vma = find_vma_intersection(mm, addr, addr+1);
  761. if (!vma) {
  762. up_read(&mm->mmap_sem);
  763. return -EFAULT;
  764. }
  765. if (vma->vm_ops && vma->vm_ops->get_policy)
  766. pol = vma->vm_ops->get_policy(vma, addr);
  767. else
  768. pol = vma->vm_policy;
  769. } else if (addr)
  770. return -EINVAL;
  771. if (!pol)
  772. pol = &default_policy; /* indicates default behavior */
  773. if (flags & MPOL_F_NODE) {
  774. if (flags & MPOL_F_ADDR) {
  775. err = lookup_node(mm, addr);
  776. if (err < 0)
  777. goto out;
  778. *policy = err;
  779. } else if (pol == current->mempolicy &&
  780. pol->mode == MPOL_INTERLEAVE) {
  781. *policy = current->il_next;
  782. } else {
  783. err = -EINVAL;
  784. goto out;
  785. }
  786. } else {
  787. *policy = pol == &default_policy ? MPOL_DEFAULT :
  788. pol->mode;
  789. /*
  790. * Internal mempolicy flags must be masked off before exposing
  791. * the policy to userspace.
  792. */
  793. *policy |= (pol->flags & MPOL_MODE_FLAGS);
  794. }
  795. err = 0;
  796. if (nmask) {
  797. if (mpol_store_user_nodemask(pol)) {
  798. *nmask = pol->w.user_nodemask;
  799. } else {
  800. task_lock(current);
  801. get_policy_nodemask(pol, nmask);
  802. task_unlock(current);
  803. }
  804. }
  805. out:
  806. mpol_cond_put(pol);
  807. if (vma)
  808. up_read(&current->mm->mmap_sem);
  809. return err;
  810. }
  811. #ifdef CONFIG_MIGRATION
  812. /*
  813. * page migration
  814. */
  815. static void migrate_page_add(struct page *page, struct list_head *pagelist,
  816. unsigned long flags)
  817. {
  818. /*
  819. * Avoid migrating a page that is shared with others.
  820. */
  821. if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
  822. if (!isolate_lru_page(page)) {
  823. list_add_tail(&page->lru, pagelist);
  824. inc_zone_page_state(page, NR_ISOLATED_ANON +
  825. page_is_file_cache(page));
  826. }
  827. }
  828. }
  829. static struct page *new_node_page(struct page *page, unsigned long node, int **x)
  830. {
  831. return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
  832. }
  833. /*
  834. * Migrate pages from one node to a target node.
  835. * Returns error or the number of pages not migrated.
  836. */
  837. static int migrate_to_node(struct mm_struct *mm, int source, int dest,
  838. int flags)
  839. {
  840. nodemask_t nmask;
  841. LIST_HEAD(pagelist);
  842. int err;
  843. nodes_clear(nmask);
  844. node_set(source, nmask);
  845. err = check_range(mm, mm->mmap->vm_start, mm->task_size, &nmask,
  846. flags | MPOL_MF_DISCONTIG_OK, &pagelist);
  847. if (err)
  848. return err;
  849. if (!list_empty(&pagelist)) {
  850. err = migrate_pages(&pagelist, new_node_page, dest,
  851. false, MIGRATE_SYNC);
  852. if (err)
  853. putback_lru_pages(&pagelist);
  854. }
  855. return err;
  856. }
  857. /*
  858. * Move pages between the two nodesets so as to preserve the physical
  859. * layout as much as possible.
  860. *
  861. * Returns the number of page that could not be moved.
  862. */
  863. int do_migrate_pages(struct mm_struct *mm,
  864. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
  865. {
  866. int busy = 0;
  867. int err;
  868. nodemask_t tmp;
  869. err = migrate_prep();
  870. if (err)
  871. return err;
  872. down_read(&mm->mmap_sem);
  873. err = migrate_vmas(mm, from_nodes, to_nodes, flags);
  874. if (err)
  875. goto out;
  876. /*
  877. * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
  878. * bit in 'to' is not also set in 'tmp'. Clear the found 'source'
  879. * bit in 'tmp', and return that <source, dest> pair for migration.
  880. * The pair of nodemasks 'to' and 'from' define the map.
  881. *
  882. * If no pair of bits is found that way, fallback to picking some
  883. * pair of 'source' and 'dest' bits that are not the same. If the
  884. * 'source' and 'dest' bits are the same, this represents a node
  885. * that will be migrating to itself, so no pages need move.
  886. *
  887. * If no bits are left in 'tmp', or if all remaining bits left
  888. * in 'tmp' correspond to the same bit in 'to', return false
  889. * (nothing left to migrate).
  890. *
  891. * This lets us pick a pair of nodes to migrate between, such that
  892. * if possible the dest node is not already occupied by some other
  893. * source node, minimizing the risk of overloading the memory on a
  894. * node that would happen if we migrated incoming memory to a node
  895. * before migrating outgoing memory source that same node.
  896. *
  897. * A single scan of tmp is sufficient. As we go, we remember the
  898. * most recent <s, d> pair that moved (s != d). If we find a pair
  899. * that not only moved, but what's better, moved to an empty slot
  900. * (d is not set in tmp), then we break out then, with that pair.
  901. * Otherwise when we finish scanning from_tmp, we at least have the
  902. * most recent <s, d> pair that moved. If we get all the way through
  903. * the scan of tmp without finding any node that moved, much less
  904. * moved to an empty node, then there is nothing left worth migrating.
  905. */
  906. tmp = *from_nodes;
  907. while (!nodes_empty(tmp)) {
  908. int s,d;
  909. int source = -1;
  910. int dest = 0;
  911. for_each_node_mask(s, tmp) {
  912. d = node_remap(s, *from_nodes, *to_nodes);
  913. if (s == d)
  914. continue;
  915. source = s; /* Node moved. Memorize */
  916. dest = d;
  917. /* dest not in remaining from nodes? */
  918. if (!node_isset(dest, tmp))
  919. break;
  920. }
  921. if (source == -1)
  922. break;
  923. node_clear(source, tmp);
  924. err = migrate_to_node(mm, source, dest, flags);
  925. if (err > 0)
  926. busy += err;
  927. if (err < 0)
  928. break;
  929. }
  930. out:
  931. up_read(&mm->mmap_sem);
  932. if (err < 0)
  933. return err;
  934. return busy;
  935. }
  936. /*
  937. * Allocate a new page for page migration based on vma policy.
  938. * Start by assuming the page is mapped by the same vma as contains @start.
  939. * Search forward from there, if not. N.B., this assumes that the
  940. * list of pages handed to migrate_pages()--which is how we get here--
  941. * is in virtual address order.
  942. */
  943. static struct page *new_page(struct page *page, unsigned long start, int **x)
  944. {
  945. struct vm_area_struct *vma;
  946. unsigned long uninitialized_var(address);
  947. vma = find_vma(current->mm, start);
  948. while (vma) {
  949. address = page_address_in_vma(page, vma);
  950. if (address != -EFAULT)
  951. break;
  952. vma = vma->vm_next;
  953. }
  954. /*
  955. * if !vma, alloc_page_vma() will use task or system default policy
  956. */
  957. return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  958. }
  959. #else
  960. static void migrate_page_add(struct page *page, struct list_head *pagelist,
  961. unsigned long flags)
  962. {
  963. }
  964. int do_migrate_pages(struct mm_struct *mm,
  965. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
  966. {
  967. return -ENOSYS;
  968. }
  969. static struct page *new_page(struct page *page, unsigned long start, int **x)
  970. {
  971. return NULL;
  972. }
  973. #endif
  974. static long do_mbind(unsigned long start, unsigned long len,
  975. unsigned short mode, unsigned short mode_flags,
  976. nodemask_t *nmask, unsigned long flags)
  977. {
  978. struct mm_struct *mm = current->mm;
  979. struct mempolicy *new;
  980. unsigned long end;
  981. int err;
  982. LIST_HEAD(pagelist);
  983. if (flags & ~(unsigned long)(MPOL_MF_STRICT |
  984. MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
  985. return -EINVAL;
  986. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  987. return -EPERM;
  988. if (start & ~PAGE_MASK)
  989. return -EINVAL;
  990. if (mode == MPOL_DEFAULT)
  991. flags &= ~MPOL_MF_STRICT;
  992. len = (len + PAGE_SIZE - 1) & PAGE_MASK;
  993. end = start + len;
  994. if (end < start)
  995. return -EINVAL;
  996. if (end == start)
  997. return 0;
  998. new = mpol_new(mode, mode_flags, nmask);
  999. if (IS_ERR(new))
  1000. return PTR_ERR(new);
  1001. /*
  1002. * If we are using the default policy then operation
  1003. * on discontinuous address spaces is okay after all
  1004. */
  1005. if (!new)
  1006. flags |= MPOL_MF_DISCONTIG_OK;
  1007. pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
  1008. start, start + len, mode, mode_flags,
  1009. nmask ? nodes_addr(*nmask)[0] : -1);
  1010. if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
  1011. err = migrate_prep();
  1012. if (err)
  1013. goto mpol_out;
  1014. }
  1015. {
  1016. NODEMASK_SCRATCH(scratch);
  1017. if (scratch) {
  1018. down_write(&mm->mmap_sem);
  1019. task_lock(current);
  1020. err = mpol_set_nodemask(new, nmask, scratch);
  1021. task_unlock(current);
  1022. if (err)
  1023. up_write(&mm->mmap_sem);
  1024. } else
  1025. err = -ENOMEM;
  1026. NODEMASK_SCRATCH_FREE(scratch);
  1027. }
  1028. if (err)
  1029. goto mpol_out;
  1030. err = check_range(mm, start, end, nmask,
  1031. flags | MPOL_MF_INVERT, &pagelist);
  1032. if (!err) {
  1033. int nr_failed = 0;
  1034. err = mbind_range(mm, start, end, new);
  1035. if (!list_empty(&pagelist)) {
  1036. nr_failed = migrate_pages(&pagelist, new_page,
  1037. start, false, true);
  1038. if (nr_failed)
  1039. putback_lru_pages(&pagelist);
  1040. }
  1041. if (!err && nr_failed && (flags & MPOL_MF_STRICT))
  1042. err = -EIO;
  1043. } else
  1044. putback_lru_pages(&pagelist);
  1045. up_write(&mm->mmap_sem);
  1046. mpol_out:
  1047. mpol_put(new);
  1048. return err;
  1049. }
  1050. /*
  1051. * User space interface with variable sized bitmaps for nodelists.
  1052. */
  1053. /* Copy a node mask from user space. */
  1054. static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
  1055. unsigned long maxnode)
  1056. {
  1057. unsigned long k;
  1058. unsigned long nlongs;
  1059. unsigned long endmask;
  1060. --maxnode;
  1061. nodes_clear(*nodes);
  1062. if (maxnode == 0 || !nmask)
  1063. return 0;
  1064. if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
  1065. return -EINVAL;
  1066. nlongs = BITS_TO_LONGS(maxnode);
  1067. if ((maxnode % BITS_PER_LONG) == 0)
  1068. endmask = ~0UL;
  1069. else
  1070. endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
  1071. /* When the user specified more nodes than supported just check
  1072. if the non supported part is all zero. */
  1073. if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
  1074. if (nlongs > PAGE_SIZE/sizeof(long))
  1075. return -EINVAL;
  1076. for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
  1077. unsigned long t;
  1078. if (get_user(t, nmask + k))
  1079. return -EFAULT;
  1080. if (k == nlongs - 1) {
  1081. if (t & endmask)
  1082. return -EINVAL;
  1083. } else if (t)
  1084. return -EINVAL;
  1085. }
  1086. nlongs = BITS_TO_LONGS(MAX_NUMNODES);
  1087. endmask = ~0UL;
  1088. }
  1089. if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
  1090. return -EFAULT;
  1091. nodes_addr(*nodes)[nlongs-1] &= endmask;
  1092. return 0;
  1093. }
  1094. /* Copy a kernel node mask to user space */
  1095. static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
  1096. nodemask_t *nodes)
  1097. {
  1098. unsigned long copy = ALIGN(maxnode-1, 64) / 8;
  1099. const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
  1100. if (copy > nbytes) {
  1101. if (copy > PAGE_SIZE)
  1102. return -EINVAL;
  1103. if (clear_user((char __user *)mask + nbytes, copy - nbytes))
  1104. return -EFAULT;
  1105. copy = nbytes;
  1106. }
  1107. return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
  1108. }
  1109. SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
  1110. unsigned long, mode, unsigned long __user *, nmask,
  1111. unsigned long, maxnode, unsigned, flags)
  1112. {
  1113. nodemask_t nodes;
  1114. int err;
  1115. unsigned short mode_flags;
  1116. mode_flags = mode & MPOL_MODE_FLAGS;
  1117. mode &= ~MPOL_MODE_FLAGS;
  1118. if (mode >= MPOL_MAX)
  1119. return -EINVAL;
  1120. if ((mode_flags & MPOL_F_STATIC_NODES) &&
  1121. (mode_flags & MPOL_F_RELATIVE_NODES))
  1122. return -EINVAL;
  1123. err = get_nodes(&nodes, nmask, maxnode);
  1124. if (err)
  1125. return err;
  1126. return do_mbind(start, len, mode, mode_flags, &nodes, flags);
  1127. }
  1128. /* Set the process memory policy */
  1129. SYSCALL_DEFINE3(set_mempolicy, int, mode, unsigned long __user *, nmask,
  1130. unsigned long, maxnode)
  1131. {
  1132. int err;
  1133. nodemask_t nodes;
  1134. unsigned short flags;
  1135. flags = mode & MPOL_MODE_FLAGS;
  1136. mode &= ~MPOL_MODE_FLAGS;
  1137. if ((unsigned int)mode >= MPOL_MAX)
  1138. return -EINVAL;
  1139. if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
  1140. return -EINVAL;
  1141. err = get_nodes(&nodes, nmask, maxnode);
  1142. if (err)
  1143. return err;
  1144. return do_set_mempolicy(mode, flags, &nodes);
  1145. }
  1146. SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
  1147. const unsigned long __user *, old_nodes,
  1148. const unsigned long __user *, new_nodes)
  1149. {
  1150. const struct cred *cred = current_cred(), *tcred;
  1151. struct mm_struct *mm = NULL;
  1152. struct task_struct *task;
  1153. nodemask_t task_nodes;
  1154. int err;
  1155. nodemask_t *old;
  1156. nodemask_t *new;
  1157. NODEMASK_SCRATCH(scratch);
  1158. if (!scratch)
  1159. return -ENOMEM;
  1160. old = &scratch->mask1;
  1161. new = &scratch->mask2;
  1162. err = get_nodes(old, old_nodes, maxnode);
  1163. if (err)
  1164. goto out;
  1165. err = get_nodes(new, new_nodes, maxnode);
  1166. if (err)
  1167. goto out;
  1168. /* Find the mm_struct */
  1169. rcu_read_lock();
  1170. task = pid ? find_task_by_vpid(pid) : current;
  1171. if (!task) {
  1172. rcu_read_unlock();
  1173. err = -ESRCH;
  1174. goto out;
  1175. }
  1176. get_task_struct(task);
  1177. err = -EINVAL;
  1178. /*
  1179. * Check if this process has the right to modify the specified
  1180. * process. The right exists if the process has administrative
  1181. * capabilities, superuser privileges or the same
  1182. * userid as the target process.
  1183. */
  1184. tcred = __task_cred(task);
  1185. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1186. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1187. !capable(CAP_SYS_NICE)) {
  1188. rcu_read_unlock();
  1189. err = -EPERM;
  1190. goto out_put;
  1191. }
  1192. rcu_read_unlock();
  1193. task_nodes = cpuset_mems_allowed(task);
  1194. /* Is the user allowed to access the target nodes? */
  1195. if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
  1196. err = -EPERM;
  1197. goto out_put;
  1198. }
  1199. if (!nodes_subset(*new, node_states[N_MEMORY])) {
  1200. err = -EINVAL;
  1201. goto out_put;
  1202. }
  1203. err = security_task_movememory(task);
  1204. if (err)
  1205. goto out_put;
  1206. mm = get_task_mm(task);
  1207. put_task_struct(task);
  1208. if (!mm) {
  1209. err = -EINVAL;
  1210. goto out;
  1211. }
  1212. err = do_migrate_pages(mm, old, new,
  1213. capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
  1214. mmput(mm);
  1215. out:
  1216. NODEMASK_SCRATCH_FREE(scratch);
  1217. return err;
  1218. out_put:
  1219. put_task_struct(task);
  1220. goto out;
  1221. }
  1222. /* Retrieve NUMA policy */
  1223. SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
  1224. unsigned long __user *, nmask, unsigned long, maxnode,
  1225. unsigned long, addr, unsigned long, flags)
  1226. {
  1227. int err;
  1228. int uninitialized_var(pval);
  1229. nodemask_t nodes;
  1230. if (nmask != NULL && maxnode < MAX_NUMNODES)
  1231. return -EINVAL;
  1232. err = do_get_mempolicy(&pval, &nodes, addr, flags);
  1233. if (err)
  1234. return err;
  1235. if (policy && put_user(pval, policy))
  1236. return -EFAULT;
  1237. if (nmask)
  1238. err = copy_nodes_to_user(nmask, maxnode, &nodes);
  1239. return err;
  1240. }
  1241. #ifdef CONFIG_COMPAT
  1242. asmlinkage long compat_sys_get_mempolicy(int __user *policy,
  1243. compat_ulong_t __user *nmask,
  1244. compat_ulong_t maxnode,
  1245. compat_ulong_t addr, compat_ulong_t flags)
  1246. {
  1247. long err;
  1248. unsigned long __user *nm = NULL;
  1249. unsigned long nr_bits, alloc_size;
  1250. DECLARE_BITMAP(bm, MAX_NUMNODES);
  1251. nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
  1252. alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  1253. if (nmask)
  1254. nm = compat_alloc_user_space(alloc_size);
  1255. err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
  1256. if (!err && nmask) {
  1257. unsigned long copy_size;
  1258. copy_size = min_t(unsigned long, sizeof(bm), alloc_size);
  1259. err = copy_from_user(bm, nm, copy_size);
  1260. /* ensure entire bitmap is zeroed */
  1261. err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
  1262. err |= compat_put_bitmap(nmask, bm, nr_bits);
  1263. }
  1264. return err;
  1265. }
  1266. asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
  1267. compat_ulong_t maxnode)
  1268. {
  1269. unsigned long __user *nm = NULL;
  1270. unsigned long nr_bits, alloc_size;
  1271. DECLARE_BITMAP(bm, MAX_NUMNODES);
  1272. nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
  1273. alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  1274. if (nmask) {
  1275. if (compat_get_bitmap(bm, nmask, nr_bits))
  1276. return -EFAULT;
  1277. nm = compat_alloc_user_space(alloc_size);
  1278. if (copy_to_user(nm, bm, alloc_size))
  1279. return -EFAULT;
  1280. }
  1281. return sys_set_mempolicy(mode, nm, nr_bits+1);
  1282. }
  1283. asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
  1284. compat_ulong_t mode, compat_ulong_t __user *nmask,
  1285. compat_ulong_t maxnode, compat_ulong_t flags)
  1286. {
  1287. unsigned long __user *nm = NULL;
  1288. unsigned long nr_bits, alloc_size;
  1289. nodemask_t bm;
  1290. nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
  1291. alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  1292. if (nmask) {
  1293. if (compat_get_bitmap(nodes_addr(bm), nmask, nr_bits))
  1294. return -EFAULT;
  1295. nm = compat_alloc_user_space(alloc_size);
  1296. if (copy_to_user(nm, nodes_addr(bm), alloc_size))
  1297. return -EFAULT;
  1298. }
  1299. return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
  1300. }
  1301. #endif
  1302. /*
  1303. * get_vma_policy(@task, @vma, @addr)
  1304. * @task - task for fallback if vma policy == default
  1305. * @vma - virtual memory area whose policy is sought
  1306. * @addr - address in @vma for shared policy lookup
  1307. *
  1308. * Returns effective policy for a VMA at specified address.
  1309. * Falls back to @task or system default policy, as necessary.
  1310. * Current or other task's task mempolicy and non-shared vma policies
  1311. * are protected by the task's mmap_sem, which must be held for read by
  1312. * the caller.
  1313. * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
  1314. * count--added by the get_policy() vm_op, as appropriate--to protect against
  1315. * freeing by another task. It is the caller's responsibility to free the
  1316. * extra reference for shared policies.
  1317. */
  1318. struct mempolicy *get_vma_policy(struct task_struct *task,
  1319. struct vm_area_struct *vma, unsigned long addr)
  1320. {
  1321. struct mempolicy *pol = task->mempolicy;
  1322. if (vma) {
  1323. if (vma->vm_ops && vma->vm_ops->get_policy) {
  1324. struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
  1325. addr);
  1326. if (vpol)
  1327. pol = vpol;
  1328. } else if (vma->vm_policy) {
  1329. pol = vma->vm_policy;
  1330. /*
  1331. * shmem_alloc_page() passes MPOL_F_SHARED policy with
  1332. * a pseudo vma whose vma->vm_ops=NULL. Take a reference
  1333. * count on these policies which will be dropped by
  1334. * mpol_cond_put() later
  1335. */
  1336. if (mpol_needs_cond_ref(pol))
  1337. mpol_get(pol);
  1338. }
  1339. }
  1340. if (!pol)
  1341. pol = &default_policy;
  1342. return pol;
  1343. }
  1344. /*
  1345. * Return a nodemask representing a mempolicy for filtering nodes for
  1346. * page allocation
  1347. */
  1348. static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
  1349. {
  1350. /* Lower zones don't get a nodemask applied for MPOL_BIND */
  1351. if (unlikely(policy->mode == MPOL_BIND) &&
  1352. gfp_zone(gfp) >= policy_zone &&
  1353. cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
  1354. return &policy->v.nodes;
  1355. return NULL;
  1356. }
  1357. /* Return a zonelist indicated by gfp for node representing a mempolicy */
  1358. static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy,
  1359. int nd)
  1360. {
  1361. switch (policy->mode) {
  1362. case MPOL_PREFERRED:
  1363. if (!(policy->flags & MPOL_F_LOCAL))
  1364. nd = policy->v.preferred_node;
  1365. break;
  1366. case MPOL_BIND:
  1367. /*
  1368. * Normally, MPOL_BIND allocations are node-local within the
  1369. * allowed nodemask. However, if __GFP_THISNODE is set and the
  1370. * current node isn't part of the mask, we use the zonelist for
  1371. * the first node in the mask instead.
  1372. */
  1373. if (unlikely(gfp & __GFP_THISNODE) &&
  1374. unlikely(!node_isset(nd, policy->v.nodes)))
  1375. nd = first_node(policy->v.nodes);
  1376. break;
  1377. default:
  1378. BUG();
  1379. }
  1380. return node_zonelist(nd, gfp);
  1381. }
  1382. /* Do dynamic interleaving for a process */
  1383. static unsigned interleave_nodes(struct mempolicy *policy)
  1384. {
  1385. unsigned nid, next;
  1386. struct task_struct *me = current;
  1387. nid = me->il_next;
  1388. next = next_node(nid, policy->v.nodes);
  1389. if (next >= MAX_NUMNODES)
  1390. next = first_node(policy->v.nodes);
  1391. if (next < MAX_NUMNODES)
  1392. me->il_next = next;
  1393. return nid;
  1394. }
  1395. /*
  1396. * Depending on the memory policy provide a node from which to allocate the
  1397. * next slab entry.
  1398. * @policy must be protected by freeing by the caller. If @policy is
  1399. * the current task's mempolicy, this protection is implicit, as only the
  1400. * task can change it's policy. The system default policy requires no
  1401. * such protection.
  1402. */
  1403. unsigned slab_node(void)
  1404. {
  1405. struct mempolicy *policy;
  1406. if (in_interrupt())
  1407. return numa_node_id();
  1408. policy = current->mempolicy;
  1409. if (!policy || policy->flags & MPOL_F_LOCAL)
  1410. return numa_node_id();
  1411. switch (policy->mode) {
  1412. case MPOL_PREFERRED:
  1413. /*
  1414. * handled MPOL_F_LOCAL above
  1415. */
  1416. return policy->v.preferred_node;
  1417. case MPOL_INTERLEAVE:
  1418. return interleave_nodes(policy);
  1419. case MPOL_BIND: {
  1420. /*
  1421. * Follow bind policy behavior and start allocation at the
  1422. * first node.
  1423. */
  1424. struct zonelist *zonelist;
  1425. struct zone *zone;
  1426. enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
  1427. zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0];
  1428. (void)first_zones_zonelist(zonelist, highest_zoneidx,
  1429. &policy->v.nodes,
  1430. &zone);
  1431. return zone ? zone->node : numa_node_id();
  1432. }
  1433. default:
  1434. BUG();
  1435. }
  1436. }
  1437. /* Do static interleaving for a VMA with known offset. */
  1438. static unsigned offset_il_node(struct mempolicy *pol,
  1439. struct vm_area_struct *vma, unsigned long off)
  1440. {
  1441. unsigned nnodes = nodes_weight(pol->v.nodes);
  1442. unsigned target;
  1443. int c;
  1444. int nid = -1;
  1445. if (!nnodes)
  1446. return numa_node_id();
  1447. target = (unsigned int)off % nnodes;
  1448. c = 0;
  1449. do {
  1450. nid = next_node(nid, pol->v.nodes);
  1451. c++;
  1452. } while (c <= target);
  1453. return nid;
  1454. }
  1455. /* Determine a node number for interleave */
  1456. static inline unsigned interleave_nid(struct mempolicy *pol,
  1457. struct vm_area_struct *vma, unsigned long addr, int shift)
  1458. {
  1459. if (vma) {
  1460. unsigned long off;
  1461. /*
  1462. * for small pages, there is no difference between
  1463. * shift and PAGE_SHIFT, so the bit-shift is safe.
  1464. * for huge pages, since vm_pgoff is in units of small
  1465. * pages, we need to shift off the always 0 bits to get
  1466. * a useful offset.
  1467. */
  1468. BUG_ON(shift < PAGE_SHIFT);
  1469. off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
  1470. off += (addr - vma->vm_start) >> shift;
  1471. return offset_il_node(pol, vma, off);
  1472. } else
  1473. return interleave_nodes(pol);
  1474. }
  1475. /*
  1476. * Return the bit number of a random bit set in the nodemask.
  1477. * (returns -1 if nodemask is empty)
  1478. */
  1479. int node_random(const nodemask_t *maskp)
  1480. {
  1481. int w, bit = -1;
  1482. w = nodes_weight(*maskp);
  1483. if (w)
  1484. bit = bitmap_ord_to_pos(maskp->bits,
  1485. get_random_int() % w, MAX_NUMNODES);
  1486. return bit;
  1487. }
  1488. #ifdef CONFIG_HUGETLBFS
  1489. /*
  1490. * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
  1491. * @vma = virtual memory area whose policy is sought
  1492. * @addr = address in @vma for shared policy lookup and interleave policy
  1493. * @gfp_flags = for requested zone
  1494. * @mpol = pointer to mempolicy pointer for reference counted mempolicy
  1495. * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask
  1496. *
  1497. * Returns a zonelist suitable for a huge page allocation and a pointer
  1498. * to the struct mempolicy for conditional unref after allocation.
  1499. * If the effective policy is 'BIND, returns a pointer to the mempolicy's
  1500. * @nodemask for filtering the zonelist.
  1501. *
  1502. * Must be protected by get_mems_allowed()
  1503. */
  1504. struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
  1505. gfp_t gfp_flags, struct mempolicy **mpol,
  1506. nodemask_t **nodemask)
  1507. {
  1508. struct zonelist *zl;
  1509. *mpol = get_vma_policy(current, vma, addr);
  1510. *nodemask = NULL; /* assume !MPOL_BIND */
  1511. if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
  1512. zl = node_zonelist(interleave_nid(*mpol, vma, addr,
  1513. huge_page_shift(hstate_vma(vma))), gfp_flags);
  1514. } else {
  1515. zl = policy_zonelist(gfp_flags, *mpol, numa_node_id());
  1516. if ((*mpol)->mode == MPOL_BIND)
  1517. *nodemask = &(*mpol)->v.nodes;
  1518. }
  1519. return zl;
  1520. }
  1521. /*
  1522. * init_nodemask_of_mempolicy
  1523. *
  1524. * If the current task's mempolicy is "default" [NULL], return 'false'
  1525. * to indicate default policy. Otherwise, extract the policy nodemask
  1526. * for 'bind' or 'interleave' policy into the argument nodemask, or
  1527. * initialize the argument nodemask to contain the single node for
  1528. * 'preferred' or 'local' policy and return 'true' to indicate presence
  1529. * of non-default mempolicy.
  1530. *
  1531. * We don't bother with reference counting the mempolicy [mpol_get/put]
  1532. * because the current task is examining it's own mempolicy and a task's
  1533. * mempolicy is only ever changed by the task itself.
  1534. *
  1535. * N.B., it is the caller's responsibility to free a returned nodemask.
  1536. */
  1537. bool init_nodemask_of_mempolicy(nodemask_t *mask)
  1538. {
  1539. struct mempolicy *mempolicy;
  1540. int nid;
  1541. if (!(mask && current->mempolicy))
  1542. return false;
  1543. task_lock(current);
  1544. mempolicy = current->mempolicy;
  1545. switch (mempolicy->mode) {
  1546. case MPOL_PREFERRED:
  1547. if (mempolicy->flags & MPOL_F_LOCAL)
  1548. nid = numa_node_id();
  1549. else
  1550. nid = mempolicy->v.preferred_node;
  1551. init_nodemask_of_node(mask, nid);
  1552. break;
  1553. case MPOL_BIND:
  1554. /* Fall through */
  1555. case MPOL_INTERLEAVE:
  1556. *mask = mempolicy->v.nodes;
  1557. break;
  1558. default:
  1559. BUG();
  1560. }
  1561. task_unlock(current);
  1562. return true;
  1563. }
  1564. #endif
  1565. /*
  1566. * mempolicy_nodemask_intersects
  1567. *
  1568. * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
  1569. * policy. Otherwise, check for intersection between mask and the policy
  1570. * nodemask for 'bind' or 'interleave' policy. For 'perferred' or 'local'
  1571. * policy, always return true since it may allocate elsewhere on fallback.
  1572. *
  1573. * Takes task_lock(tsk) to prevent freeing of its mempolicy.
  1574. */
  1575. bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  1576. const nodemask_t *mask)
  1577. {
  1578. struct mempolicy *mempolicy;
  1579. bool ret = true;
  1580. if (!mask)
  1581. return ret;
  1582. task_lock(tsk);
  1583. mempolicy = tsk->mempolicy;
  1584. if (!mempolicy)
  1585. goto out;
  1586. switch (mempolicy->mode) {
  1587. case MPOL_PREFERRED:
  1588. /*
  1589. * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
  1590. * allocate from, they may fallback to other nodes when oom.
  1591. * Thus, it's possible for tsk to have allocated memory from
  1592. * nodes in mask.
  1593. */
  1594. break;
  1595. case MPOL_BIND:
  1596. case MPOL_INTERLEAVE:
  1597. ret = nodes_intersects(mempolicy->v.nodes, *mask);
  1598. break;
  1599. default:
  1600. BUG();
  1601. }
  1602. out:
  1603. task_unlock(tsk);
  1604. return ret;
  1605. }
  1606. /* Allocate a page in interleaved policy.
  1607. Own path because it needs to do special accounting. */
  1608. static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
  1609. unsigned nid)
  1610. {
  1611. struct zonelist *zl;
  1612. struct page *page;
  1613. zl = node_zonelist(nid, gfp);
  1614. page = __alloc_pages(gfp, order, zl);
  1615. if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
  1616. inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
  1617. return page;
  1618. }
  1619. /**
  1620. * alloc_pages_vma - Allocate a page for a VMA.
  1621. *
  1622. * @gfp:
  1623. * %GFP_USER user allocation.
  1624. * %GFP_KERNEL kernel allocations,
  1625. * %GFP_HIGHMEM highmem/user allocations,
  1626. * %GFP_FS allocation should not call back into a file system.
  1627. * %GFP_ATOMIC don't sleep.
  1628. *
  1629. * @order:Order of the GFP allocation.
  1630. * @vma: Pointer to VMA or NULL if not available.
  1631. * @addr: Virtual Address of the allocation. Must be inside the VMA.
  1632. *
  1633. * This function allocates a page from the kernel page pool and applies
  1634. * a NUMA policy associated with the VMA or the current process.
  1635. * When VMA is not NULL caller must hold down_read on the mmap_sem of the
  1636. * mm_struct of the VMA to prevent it from going away. Should be used for
  1637. * all allocations for pages that will be mapped into
  1638. * user space. Returns NULL when no page can be allocated.
  1639. *
  1640. * Should be called with the mm_sem of the vma hold.
  1641. */
  1642. struct page *
  1643. alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
  1644. unsigned long addr, int node)
  1645. {
  1646. struct mempolicy *pol;
  1647. struct zonelist *zl;
  1648. struct page *page;
  1649. unsigned int cpuset_mems_cookie;
  1650. retry_cpuset:
  1651. pol = get_vma_policy(current, vma, addr);
  1652. cpuset_mems_cookie = get_mems_allowed();
  1653. if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
  1654. unsigned nid;
  1655. nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
  1656. mpol_cond_put(pol);
  1657. page = alloc_page_interleave(gfp, order, nid);
  1658. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  1659. goto retry_cpuset;
  1660. return page;
  1661. }
  1662. zl = policy_zonelist(gfp, pol, node);
  1663. if (unlikely(mpol_needs_cond_ref(pol))) {
  1664. /*
  1665. * slow path: ref counted shared policy
  1666. */
  1667. struct page *page = __alloc_pages_nodemask(gfp, order,
  1668. zl, policy_nodemask(gfp, pol));
  1669. __mpol_put(pol);
  1670. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  1671. goto retry_cpuset;
  1672. return page;
  1673. }
  1674. /*
  1675. * fast path: default or task policy
  1676. */
  1677. page = __alloc_pages_nodemask(gfp, order, zl,
  1678. policy_nodemask(gfp, pol));
  1679. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  1680. goto retry_cpuset;
  1681. return page;
  1682. }
  1683. /**
  1684. * alloc_pages_current - Allocate pages.
  1685. *
  1686. * @gfp:
  1687. * %GFP_USER user allocation,
  1688. * %GFP_KERNEL kernel allocation,
  1689. * %GFP_HIGHMEM highmem allocation,
  1690. * %GFP_FS don't call back into a file system.
  1691. * %GFP_ATOMIC don't sleep.
  1692. * @order: Power of two of allocation size in pages. 0 is a single page.
  1693. *
  1694. * Allocate a page from the kernel page pool. When not in
  1695. * interrupt context and apply the current process NUMA policy.
  1696. * Returns NULL when no page can be allocated.
  1697. *
  1698. * Don't call cpuset_update_task_memory_state() unless
  1699. * 1) it's ok to take cpuset_sem (can WAIT), and
  1700. * 2) allocating for current task (not interrupt).
  1701. */
  1702. struct page *alloc_pages_current(gfp_t gfp, unsigned order)
  1703. {
  1704. struct mempolicy *pol = current->mempolicy;
  1705. struct page *page;
  1706. unsigned int cpuset_mems_cookie;
  1707. if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
  1708. pol = &default_policy;
  1709. retry_cpuset:
  1710. cpuset_mems_cookie = get_mems_allowed();
  1711. /*
  1712. * No reference counting needed for current->mempolicy
  1713. * nor system default_policy
  1714. */
  1715. if (pol->mode == MPOL_INTERLEAVE)
  1716. page = alloc_page_interleave(gfp, order, interleave_nodes(pol));
  1717. else
  1718. page = __alloc_pages_nodemask(gfp, order,
  1719. policy_zonelist(gfp, pol, numa_node_id()),
  1720. policy_nodemask(gfp, pol));
  1721. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  1722. goto retry_cpuset;
  1723. return page;
  1724. }
  1725. EXPORT_SYMBOL(alloc_pages_current);
  1726. int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst)
  1727. {
  1728. struct mempolicy *pol = mpol_dup(vma_policy(src));
  1729. if (IS_ERR(pol))
  1730. return PTR_ERR(pol);
  1731. dst->vm_policy = pol;
  1732. return 0;
  1733. }
  1734. /*
  1735. * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it
  1736. * rebinds the mempolicy its copying by calling mpol_rebind_policy()
  1737. * with the mems_allowed returned by cpuset_mems_allowed(). This
  1738. * keeps mempolicies cpuset relative after its cpuset moves. See
  1739. * further kernel/cpuset.c update_nodemask().
  1740. *
  1741. * current's mempolicy may be rebinded by the other task(the task that changes
  1742. * cpuset's mems), so we needn't do rebind work for current task.
  1743. */
  1744. /* Slow path of a mempolicy duplicate */
  1745. struct mempolicy *__mpol_dup(struct mempolicy *old)
  1746. {
  1747. struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL);
  1748. if (!new)
  1749. return ERR_PTR(-ENOMEM);
  1750. /* task's mempolicy is protected by alloc_lock */
  1751. if (old == current->mempolicy) {
  1752. task_lock(current);
  1753. *new = *old;
  1754. task_unlock(current);
  1755. } else
  1756. *new = *old;
  1757. if (current_cpuset_is_being_rebound()) {
  1758. nodemask_t mems = cpuset_mems_allowed(current);
  1759. if (new->flags & MPOL_F_REBINDING)
  1760. mpol_rebind_policy(new, &mems, MPOL_REBIND_STEP2);
  1761. else
  1762. mpol_rebind_policy(new, &mems, MPOL_REBIND_ONCE);
  1763. }
  1764. atomic_set(&new->refcnt, 1);
  1765. return new;
  1766. }
  1767. /* Slow path of a mempolicy comparison */
  1768. bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
  1769. {
  1770. if (!a || !b)
  1771. return false;
  1772. if (a->mode != b->mode)
  1773. return false;
  1774. if (a->flags != b->flags)
  1775. return false;
  1776. if (mpol_store_user_nodemask(a))
  1777. if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask))
  1778. return false;
  1779. switch (a->mode) {
  1780. case MPOL_BIND:
  1781. /* Fall through */
  1782. case MPOL_INTERLEAVE:
  1783. return !!nodes_equal(a->v.nodes, b->v.nodes);
  1784. case MPOL_PREFERRED:
  1785. return a->v.preferred_node == b->v.preferred_node;
  1786. default:
  1787. BUG();
  1788. return false;
  1789. }
  1790. }
  1791. /*
  1792. * Shared memory backing store policy support.
  1793. *
  1794. * Remember policies even when nobody has shared memory mapped.
  1795. * The policies are kept in Red-Black tree linked from the inode.
  1796. * They are protected by the sp->lock spinlock, which should be held
  1797. * for any accesses to the tree.
  1798. */
  1799. /* lookup first element intersecting start-end */
  1800. /* Caller holds sp->mutex */
  1801. static struct sp_node *
  1802. sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end)
  1803. {
  1804. struct rb_node *n = sp->root.rb_node;
  1805. while (n) {
  1806. struct sp_node *p = rb_entry(n, struct sp_node, nd);
  1807. if (start >= p->end)
  1808. n = n->rb_right;
  1809. else if (end <= p->start)
  1810. n = n->rb_left;
  1811. else
  1812. break;
  1813. }
  1814. if (!n)
  1815. return NULL;
  1816. for (;;) {
  1817. struct sp_node *w = NULL;
  1818. struct rb_node *prev = rb_prev(n);
  1819. if (!prev)
  1820. break;
  1821. w = rb_entry(prev, struct sp_node, nd);
  1822. if (w->end <= start)
  1823. break;
  1824. n = prev;
  1825. }
  1826. return rb_entry(n, struct sp_node, nd);
  1827. }
  1828. /* Insert a new shared policy into the list. */
  1829. /* Caller holds sp->lock */
  1830. static void sp_insert(struct shared_policy *sp, struct sp_node *new)
  1831. {
  1832. struct rb_node **p = &sp->root.rb_node;
  1833. struct rb_node *parent = NULL;
  1834. struct sp_node *nd;
  1835. while (*p) {
  1836. parent = *p;
  1837. nd = rb_entry(parent, struct sp_node, nd);
  1838. if (new->start < nd->start)
  1839. p = &(*p)->rb_left;
  1840. else if (new->end > nd->end)
  1841. p = &(*p)->rb_right;
  1842. else
  1843. BUG();
  1844. }
  1845. rb_link_node(&new->nd, parent, p);
  1846. rb_insert_color(&new->nd, &sp->root);
  1847. pr_debug("inserting %lx-%lx: %d\n", new->start, new->end,
  1848. new->policy ? new->policy->mode : 0);
  1849. }
  1850. /* Find shared policy intersecting idx */
  1851. struct mempolicy *
  1852. mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
  1853. {
  1854. struct mempolicy *pol = NULL;
  1855. struct sp_node *sn;
  1856. if (!sp->root.rb_node)
  1857. return NULL;
  1858. mutex_lock(&sp->mutex);
  1859. sn = sp_lookup(sp, idx, idx+1);
  1860. if (sn) {
  1861. mpol_get(sn->policy);
  1862. pol = sn->policy;
  1863. }
  1864. mutex_unlock(&sp->mutex);
  1865. return pol;
  1866. }
  1867. static void sp_free(struct sp_node *n)
  1868. {
  1869. mpol_put(n->policy);
  1870. kmem_cache_free(sn_cache, n);
  1871. }
  1872. static void sp_delete(struct shared_policy *sp, struct sp_node *n)
  1873. {
  1874. pr_debug("deleting %lx-l%lx\n", n->start, n->end);
  1875. rb_erase(&n->nd, &sp->root);
  1876. sp_free(n);
  1877. }
  1878. static struct sp_node *sp_alloc(unsigned long start, unsigned long end,
  1879. struct mempolicy *pol)
  1880. {
  1881. struct sp_node *n;
  1882. struct mempolicy *newpol;
  1883. n = kmem_cache_alloc(sn_cache, GFP_KERNEL);
  1884. if (!n)
  1885. return NULL;
  1886. newpol = mpol_dup(pol);
  1887. if (IS_ERR(newpol)) {
  1888. kmem_cache_free(sn_cache, n);
  1889. return NULL;
  1890. }
  1891. newpol->flags |= MPOL_F_SHARED;
  1892. n->start = start;
  1893. n->end = end;
  1894. n->policy = newpol;
  1895. return n;
  1896. }
  1897. /* Replace a policy range. */
  1898. static int shared_policy_replace(struct shared_policy *sp, unsigned long start,
  1899. unsigned long end, struct sp_node *new)
  1900. {
  1901. struct sp_node *n;
  1902. int ret = 0;
  1903. mutex_lock(&sp->mutex);
  1904. n = sp_lookup(sp, start, end);
  1905. /* Take care of old policies in the same range. */
  1906. while (n && n->start < end) {
  1907. struct rb_node *next = rb_next(&n->nd);
  1908. if (n->start >= start) {
  1909. if (n->end <= end)
  1910. sp_delete(sp, n);
  1911. else
  1912. n->start = end;
  1913. } else {
  1914. /* Old policy spanning whole new range. */
  1915. if (n->end > end) {
  1916. struct sp_node *new2;
  1917. new2 = sp_alloc(end, n->end, n->policy);
  1918. if (!new2) {
  1919. ret = -ENOMEM;
  1920. goto out;
  1921. }
  1922. n->end = start;
  1923. sp_insert(sp, new2);
  1924. break;
  1925. } else
  1926. n->end = start;
  1927. }
  1928. if (!next)
  1929. break;
  1930. n = rb_entry(next, struct sp_node, nd);
  1931. }
  1932. if (new)
  1933. sp_insert(sp, new);
  1934. out:
  1935. mutex_unlock(&sp->mutex);
  1936. return ret;
  1937. }
  1938. /**
  1939. * mpol_shared_policy_init - initialize shared policy for inode
  1940. * @sp: pointer to inode shared policy
  1941. * @mpol: struct mempolicy to install
  1942. *
  1943. * Install non-NULL @mpol in inode's shared policy rb-tree.
  1944. * On entry, the current task has a reference on a non-NULL @mpol.
  1945. * This must be released on exit.
  1946. * This is called at get_inode() calls and we can use GFP_KERNEL.
  1947. */
  1948. void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)
  1949. {
  1950. int ret;
  1951. sp->root = RB_ROOT; /* empty tree == default mempolicy */
  1952. mutex_init(&sp->mutex);
  1953. if (mpol) {
  1954. struct vm_area_struct pvma;
  1955. struct mempolicy *new;
  1956. NODEMASK_SCRATCH(scratch);
  1957. if (!scratch)
  1958. goto put_mpol;
  1959. /* contextualize the tmpfs mount point mempolicy */
  1960. new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask);
  1961. if (IS_ERR(new))
  1962. goto free_scratch; /* no valid nodemask intersection */
  1963. task_lock(current);
  1964. ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch);
  1965. task_unlock(current);
  1966. if (ret)
  1967. goto put_new;
  1968. /* Create pseudo-vma that contains just the policy */
  1969. memset(&pvma, 0, sizeof(struct vm_area_struct));
  1970. pvma.vm_end = TASK_SIZE; /* policy covers entire file */
  1971. mpol_set_shared_policy(sp, &pvma, new); /* adds ref */
  1972. put_new:
  1973. mpol_put(new); /* drop initial ref */
  1974. free_scratch:
  1975. NODEMASK_SCRATCH_FREE(scratch);
  1976. put_mpol:
  1977. mpol_put(mpol); /* drop our incoming ref on sb mpol */
  1978. }
  1979. }
  1980. int mpol_set_shared_policy(struct shared_policy *info,
  1981. struct vm_area_struct *vma, struct mempolicy *npol)
  1982. {
  1983. int err;
  1984. struct sp_node *new = NULL;
  1985. unsigned long sz = vma_pages(vma);
  1986. pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n",
  1987. vma->vm_pgoff,
  1988. sz, npol ? npol->mode : -1,
  1989. npol ? npol->flags : -1,
  1990. npol ? nodes_addr(npol->v.nodes)[0] : -1);
  1991. if (npol) {
  1992. new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol);
  1993. if (!new)
  1994. return -ENOMEM;
  1995. }
  1996. err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new);
  1997. if (err && new)
  1998. sp_free(new);
  1999. return err;
  2000. }
  2001. /* Free a backing policy store on inode delete. */
  2002. void mpol_free_shared_policy(struct shared_policy *p)
  2003. {
  2004. struct sp_node *n;
  2005. struct rb_node *next;
  2006. if (!p->root.rb_node)
  2007. return;
  2008. mutex_lock(&p->mutex);
  2009. next = rb_first(&p->root);
  2010. while (next) {
  2011. n = rb_entry(next, struct sp_node, nd);
  2012. next = rb_next(&n->nd);
  2013. sp_delete(p, n);
  2014. }
  2015. mutex_unlock(&p->mutex);
  2016. }
  2017. /* assumes fs == KERNEL_DS */
  2018. void __init numa_policy_init(void)
  2019. {
  2020. nodemask_t interleave_nodes;
  2021. unsigned long largest = 0;
  2022. int nid, prefer = 0;
  2023. policy_cache = kmem_cache_create("numa_policy",
  2024. sizeof(struct mempolicy),
  2025. 0, SLAB_PANIC, NULL);
  2026. sn_cache = kmem_cache_create("shared_policy_node",
  2027. sizeof(struct sp_node),
  2028. 0, SLAB_PANIC, NULL);
  2029. /*
  2030. * Set interleaving policy for system init. Interleaving is only
  2031. * enabled across suitably sized nodes (default is >= 16MB), or
  2032. * fall back to the largest node if they're all smaller.
  2033. */
  2034. nodes_clear(interleave_nodes);
  2035. for_each_node_state(nid, N_MEMORY) {
  2036. unsigned long total_pages = node_present_pages(nid);
  2037. /* Preserve the largest node */
  2038. if (largest < total_pages) {
  2039. largest = total_pages;
  2040. prefer = nid;
  2041. }
  2042. /* Interleave this node? */
  2043. if ((total_pages << PAGE_SHIFT) >= (16 << 20))
  2044. node_set(nid, interleave_nodes);
  2045. }
  2046. /* All too small, use the largest */
  2047. if (unlikely(nodes_empty(interleave_nodes)))
  2048. node_set(prefer, interleave_nodes);
  2049. if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes))
  2050. printk("numa_policy_init: interleaving failed\n");
  2051. }
  2052. /* Reset policy of current process to default */
  2053. void numa_default_policy(void)
  2054. {
  2055. do_set_mempolicy(MPOL_DEFAULT, 0, NULL);
  2056. }
  2057. /*
  2058. * Parse and format mempolicy from/to strings
  2059. */
  2060. /*
  2061. * "local" is implemented internally by MPOL_PREFERRED with MPOL_F_LOCAL flag.
  2062. */
  2063. #define MPOL_LOCAL MPOL_MAX
  2064. static const char * const policy_modes[] =
  2065. {
  2066. [MPOL_DEFAULT] = "default",
  2067. [MPOL_PREFERRED] = "prefer",
  2068. [MPOL_BIND] = "bind",
  2069. [MPOL_INTERLEAVE] = "interleave",
  2070. [MPOL_LOCAL] = "local"
  2071. };
  2072. #ifdef CONFIG_TMPFS
  2073. /**
  2074. * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option.
  2075. * @str: string containing mempolicy to parse
  2076. * @mpol: pointer to struct mempolicy pointer, returned on success.
  2077. * @unused: redundant argument, to be removed later.
  2078. *
  2079. * Format of input:
  2080. * <mode>[=<flags>][:<nodelist>]
  2081. *
  2082. * On success, returns 0, else 1
  2083. */
  2084. int mpol_parse_str(char *str, struct mempolicy **mpol, int unused)
  2085. {
  2086. struct mempolicy *new = NULL;
  2087. unsigned short mode;
  2088. unsigned short mode_flags;
  2089. nodemask_t nodes;
  2090. char *nodelist = strchr(str, ':');
  2091. char *flags = strchr(str, '=');
  2092. int err = 1;
  2093. if (nodelist) {
  2094. /* NUL-terminate mode or flags string */
  2095. *nodelist++ = '\0';
  2096. if (nodelist_parse(nodelist, nodes))
  2097. goto out;
  2098. if (!nodes_subset(nodes, node_states[N_MEMORY]))
  2099. goto out;
  2100. } else
  2101. nodes_clear(nodes);
  2102. if (flags)
  2103. *flags++ = '\0'; /* terminate mode string */
  2104. for (mode = 0; mode <= MPOL_LOCAL; mode++) {
  2105. if (!strcmp(str, policy_modes[mode])) {
  2106. break;
  2107. }
  2108. }
  2109. if (mode > MPOL_LOCAL)
  2110. goto out;
  2111. switch (mode) {
  2112. case MPOL_PREFERRED:
  2113. /*
  2114. * Insist on a nodelist of one node only, although later
  2115. * we use first_node(nodes) to grab a single node, so here
  2116. * nodelist (or nodes) cannot be empty.
  2117. */
  2118. if (nodelist) {
  2119. char *rest = nodelist;
  2120. while (isdigit(*rest))
  2121. rest++;
  2122. if (*rest)
  2123. goto out;
  2124. if (nodes_empty(nodes))
  2125. goto out;
  2126. }
  2127. break;
  2128. case MPOL_INTERLEAVE:
  2129. /*
  2130. * Default to online nodes with memory if no nodelist
  2131. */
  2132. if (!nodelist)
  2133. nodes = node_states[N_MEMORY];
  2134. break;
  2135. case MPOL_LOCAL:
  2136. /*
  2137. * Don't allow a nodelist; mpol_new() checks flags
  2138. */
  2139. if (nodelist)
  2140. goto out;
  2141. mode = MPOL_PREFERRED;
  2142. break;
  2143. case MPOL_DEFAULT:
  2144. /*
  2145. * Insist on a empty nodelist
  2146. */
  2147. if (!nodelist)
  2148. err = 0;
  2149. goto out;
  2150. case MPOL_BIND:
  2151. /*
  2152. * Insist on a nodelist
  2153. */
  2154. if (!nodelist)
  2155. goto out;
  2156. }
  2157. mode_flags = 0;
  2158. if (flags) {
  2159. /*
  2160. * Currently, we only support two mutually exclusive
  2161. * mode flags.
  2162. */
  2163. if (!strcmp(flags, "static"))
  2164. mode_flags |= MPOL_F_STATIC_NODES;
  2165. else if (!strcmp(flags, "relative"))
  2166. mode_flags |= MPOL_F_RELATIVE_NODES;
  2167. else
  2168. goto out;
  2169. }
  2170. new = mpol_new(mode, mode_flags, &nodes);
  2171. if (IS_ERR(new))
  2172. goto out;
  2173. /*
  2174. * Save nodes for mpol_to_str() to show the tmpfs mount options
  2175. * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo.
  2176. */
  2177. if (mode != MPOL_PREFERRED)
  2178. new->v.nodes = nodes;
  2179. else if (nodelist)
  2180. new->v.preferred_node = first_node(nodes);
  2181. else
  2182. new->flags |= MPOL_F_LOCAL;
  2183. /*
  2184. * Save nodes for contextualization: this will be used to "clone"
  2185. * the mempolicy in a specific context [cpuset] at a later time.
  2186. */
  2187. new->w.user_nodemask = nodes;
  2188. err = 0;
  2189. out:
  2190. /* Restore string for error message */
  2191. if (nodelist)
  2192. *--nodelist = ':';
  2193. if (flags)
  2194. *--flags = '=';
  2195. if (!err)
  2196. *mpol = new;
  2197. return err;
  2198. }
  2199. #endif /* CONFIG_TMPFS */
  2200. /**
  2201. * mpol_to_str - format a mempolicy structure for printing
  2202. * @buffer: to contain formatted mempolicy string
  2203. * @maxlen: length of @buffer
  2204. * @pol: pointer to mempolicy to be formatted
  2205. * @unused: redundant argument, to be removed later.
  2206. *
  2207. * Convert a mempolicy into a string.
  2208. * Returns the number of characters in buffer (if positive)
  2209. * or an error (negative)
  2210. */
  2211. int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol, int unused)
  2212. {
  2213. char *p = buffer;
  2214. int l;
  2215. nodemask_t nodes;
  2216. unsigned short mode;
  2217. unsigned short flags = pol ? pol->flags : 0;
  2218. /*
  2219. * Sanity check: room for longest mode, flag and some nodes
  2220. */
  2221. VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16);
  2222. if (!pol || pol == &default_policy)
  2223. mode = MPOL_DEFAULT;
  2224. else
  2225. mode = pol->mode;
  2226. switch (mode) {
  2227. case MPOL_DEFAULT:
  2228. nodes_clear(nodes);
  2229. break;
  2230. case MPOL_PREFERRED:
  2231. nodes_clear(nodes);
  2232. if (flags & MPOL_F_LOCAL)
  2233. mode = MPOL_LOCAL;
  2234. else
  2235. node_set(pol->v.preferred_node, nodes);
  2236. break;
  2237. case MPOL_BIND:
  2238. /* Fall through */
  2239. case MPOL_INTERLEAVE:
  2240. nodes = pol->v.nodes;
  2241. break;
  2242. default:
  2243. return -EINVAL;
  2244. }
  2245. l = strlen(policy_modes[mode]);
  2246. if (buffer + maxlen < p + l + 1)
  2247. return -ENOSPC;
  2248. strcpy(p, policy_modes[mode]);
  2249. p += l;
  2250. if (flags & MPOL_MODE_FLAGS) {
  2251. if (buffer + maxlen < p + 2)
  2252. return -ENOSPC;
  2253. *p++ = '=';
  2254. /*
  2255. * Currently, the only defined flags are mutually exclusive
  2256. */
  2257. if (flags & MPOL_F_STATIC_NODES)
  2258. p += snprintf(p, buffer + maxlen - p, "static");
  2259. else if (flags & MPOL_F_RELATIVE_NODES)
  2260. p += snprintf(p, buffer + maxlen - p, "relative");
  2261. }
  2262. if (!nodes_empty(nodes)) {
  2263. if (buffer + maxlen < p + 2)
  2264. return -ENOSPC;
  2265. *p++ = ':';
  2266. p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
  2267. }
  2268. return p - buffer;
  2269. }