mempolicy.c 59 KB

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