mempolicy.c 59 KB

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