mempolicy.c 73 KB

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