mempolicy.c 55 KB

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