PageRenderTime 1194ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/mm/mempolicy.c

https://github.com/Mengqi/linux-2.6
C | 1886 lines | 1237 code | 220 blank | 429 comment | 302 complexity | d24a4e1d31ba2f2c995679ceaacd0aad MD5 | raw file
  1. /*
  2. * Simple NUMA memory policy for the Linux kernel.
  3. *
  4. * Copyright 2003,2004 Andi Kleen, SuSE Labs.
  5. * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
  6. * Subject to the GNU Public License, version 2.
  7. *
  8. * NUMA policy allows the user to give hints in which node(s) memory should
  9. * be allocated.
  10. *
  11. * Support four policies per VMA and per process:
  12. *
  13. * The VMA policy has priority over the process policy for a page fault.
  14. *
  15. * interleave Allocate memory interleaved over a set of nodes,
  16. * with normal fallback if it fails.
  17. * For VMA based allocations this interleaves based on the
  18. * offset into the backing object or offset into the mapping
  19. * for anonymous memory. For process policy an process counter
  20. * is used.
  21. *
  22. * bind Only allocate memory on a specific set of nodes,
  23. * no fallback.
  24. * FIXME: memory is allocated starting with the first node
  25. * to the last. It would be better if bind would truly restrict
  26. * the allocation to memory nodes instead
  27. *
  28. * preferred Try a specific node first before normal fallback.
  29. * As a special case node -1 here means do the allocation
  30. * on the local CPU. This is normally identical to default,
  31. * but useful to set in a VMA when you have a non default
  32. * process policy.
  33. *
  34. * default Allocate on the local node first, or when on a VMA
  35. * use the process policy. This is what Linux always did
  36. * in a NUMA aware kernel and still does by, ahem, default.
  37. *
  38. * The process policy is applied for most non interrupt memory allocations
  39. * in that process' context. Interrupts ignore the policies and always
  40. * try to allocate on the local CPU. The VMA policy is only applied for memory
  41. * allocations for a VMA in the VM.
  42. *
  43. * Currently there are a few corner cases in swapping where the policy
  44. * is not applied, but the majority should be handled. When process policy
  45. * is used it is not remembered over swap outs/swap ins.
  46. *
  47. * Only the highest zone in the zone hierarchy gets policied. Allocations
  48. * requesting a lower zone just use default policy. This implies that
  49. * on systems with highmem kernel lowmem allocation don't get policied.
  50. * Same with GFP_DMA allocations.
  51. *
  52. * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
  53. * all users and remembered even when nobody has memory mapped.
  54. */
  55. /* Notebook:
  56. fix mmap readahead to honour policy and enable policy for any page cache
  57. object
  58. statistics for bigpages
  59. global policy for page cache? currently it uses process policy. Requires
  60. first item above.
  61. handle mremap for shared memory (currently ignored for the policy)
  62. grows down?
  63. make bind policy root only? It can trigger oom much faster and the
  64. kernel is not always grateful with that.
  65. */
  66. #include <linux/mempolicy.h>
  67. #include <linux/mm.h>
  68. #include <linux/highmem.h>
  69. #include <linux/hugetlb.h>
  70. #include <linux/kernel.h>
  71. #include <linux/sched.h>
  72. #include <linux/nodemask.h>
  73. #include <linux/cpuset.h>
  74. #include <linux/slab.h>
  75. #include <linux/string.h>
  76. #include <linux/module.h>
  77. #include <linux/nsproxy.h>
  78. #include <linux/interrupt.h>
  79. #include <linux/init.h>
  80. #include <linux/compat.h>
  81. #include <linux/swap.h>
  82. #include <linux/seq_file.h>
  83. #include <linux/proc_fs.h>
  84. #include <linux/migrate.h>
  85. #include <linux/ksm.h>
  86. #include <linux/rmap.h>
  87. #include <linux/security.h>
  88. #include <linux/syscalls.h>
  89. #include <linux/ctype.h>
  90. #include <linux/mm_inline.h>
  91. #include <asm/tlbflush.h>
  92. #include <asm/uaccess.h>
  93. #include <linux/random.h>
  94. #include "internal.h"
  95. /* Internal flags */
  96. #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */
  97. #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */
  98. static struct kmem_cache *policy_cache;
  99. static struct kmem_cache *sn_cache;
  100. /* Highest zone. An specific allocation for a zone below that is not
  101. policied. */
  102. enum zone_type policy_zone = 0;
  103. /*
  104. * run-time system-wide default policy => local allocation
  105. */
  106. struct mempolicy default_policy = {
  107. .refcnt = ATOMIC_INIT(1), /* never free it */
  108. .mode = MPOL_PREFERRED,
  109. .flags = MPOL_F_LOCAL,
  110. };
  111. static const struct mempolicy_operations {
  112. int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
  113. /*
  114. * If read-side task has no lock to protect task->mempolicy, write-side
  115. * task will rebind the task->mempolicy by two step. The first step is
  116. * setting all the newly nodes, and the second step is cleaning all the
  117. * disallowed nodes. In this way, we can avoid finding no node to alloc
  118. * page.
  119. * If we have a lock to protect task->mempolicy in read-side, we do
  120. * rebind directly.
  121. *
  122. * step:
  123. * MPOL_REBIND_ONCE - do rebind work at once
  124. * MPOL_REBIND_STEP1 - set all the newly nodes
  125. * MPOL_REBIND_STEP2 - clean all the disallowed nodes
  126. */
  127. void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes,
  128. enum mpol_rebind_step step);
  129. } mpol_ops[MPOL_MAX];
  130. /* Check that the nodemask contains at least one populated zone */
  131. static int is_valid_nodemask(const nodemask_t *nodemask)
  132. {
  133. int nd, k;
  134. for_each_node_mask(nd, *nodemask) {
  135. struct zone *z;
  136. for (k = 0; k <= policy_zone; k++) {
  137. z = &NODE_DATA(nd)->node_zones[k];
  138. if (z->present_pages > 0)
  139. return 1;
  140. }
  141. }
  142. return 0;
  143. }
  144. static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
  145. {
  146. return pol->flags & MPOL_MODE_FLAGS;
  147. }
  148. static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
  149. const nodemask_t *rel)
  150. {
  151. nodemask_t tmp;
  152. nodes_fold(tmp, *orig, nodes_weight(*rel));
  153. nodes_onto(*ret, tmp, *rel);
  154. }
  155. static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
  156. {
  157. if (nodes_empty(*nodes))
  158. return -EINVAL;
  159. pol->v.nodes = *nodes;
  160. return 0;
  161. }
  162. static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
  163. {
  164. if (!nodes)
  165. pol->flags |= MPOL_F_LOCAL; /* local allocation */
  166. else if (nodes_empty(*nodes))
  167. return -EINVAL; /* no allowed nodes */
  168. else
  169. pol->v.preferred_node = first_node(*nodes);
  170. return 0;
  171. }
  172. static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
  173. {
  174. if (!is_valid_nodemask(nodes))
  175. return -EINVAL;
  176. pol->v.nodes = *nodes;
  177. return 0;
  178. }
  179. /*
  180. * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if
  181. * any, for the new policy. mpol_new() has already validated the nodes
  182. * parameter with respect to the policy mode and flags. But, we need to
  183. * handle an empty nodemask with MPOL_PREFERRED here.
  184. *
  185. * Must be called holding task's alloc_lock to protect task's mems_allowed
  186. * and mempolicy. May also be called holding the mmap_semaphore for write.
  187. */
  188. static int mpol_set_nodemask(struct mempolicy *pol,
  189. const nodemask_t *nodes, struct nodemask_scratch *nsc)
  190. {
  191. int ret;
  192. /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */
  193. if (pol == NULL)
  194. return 0;
  195. /* Check N_HIGH_MEMORY */
  196. nodes_and(nsc->mask1,
  197. cpuset_current_mems_allowed, node_states[N_HIGH_MEMORY]);
  198. VM_BUG_ON(!nodes);
  199. if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes))
  200. nodes = NULL; /* explicit local allocation */
  201. else {
  202. if (pol->flags & MPOL_F_RELATIVE_NODES)
  203. mpol_relative_nodemask(&nsc->mask2, nodes,&nsc->mask1);
  204. else
  205. nodes_and(nsc->mask2, *nodes, nsc->mask1);
  206. if (mpol_store_user_nodemask(pol))
  207. pol->w.user_nodemask = *nodes;
  208. else
  209. pol->w.cpuset_mems_allowed =
  210. cpuset_current_mems_allowed;
  211. }
  212. if (nodes)
  213. ret = mpol_ops[pol->mode].create(pol, &nsc->mask2);
  214. else
  215. ret = mpol_ops[pol->mode].create(pol, NULL);
  216. return ret;
  217. }
  218. /*
  219. * This function just creates a new policy, does some check and simple
  220. * initialization. You must invoke mpol_set_nodemask() to set nodes.
  221. */
  222. static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
  223. nodemask_t *nodes)
  224. {
  225. struct mempolicy *policy;
  226. pr_debug("setting mode %d flags %d nodes[0] %lx\n",
  227. mode, flags, nodes ? nodes_addr(*nodes)[0] : -1);
  228. if (mode == MPOL_DEFAULT) {
  229. if (nodes && !nodes_empty(*nodes))
  230. return ERR_PTR(-EINVAL);
  231. return NULL; /* simply delete any existing policy */
  232. }
  233. VM_BUG_ON(!nodes);
  234. /*
  235. * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
  236. * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
  237. * All other modes require a valid pointer to a non-empty nodemask.
  238. */
  239. if (mode == MPOL_PREFERRED) {
  240. if (nodes_empty(*nodes)) {
  241. if (((flags & MPOL_F_STATIC_NODES) ||
  242. (flags & MPOL_F_RELATIVE_NODES)))
  243. return ERR_PTR(-EINVAL);
  244. }
  245. } else if (nodes_empty(*nodes))
  246. return ERR_PTR(-EINVAL);
  247. policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
  248. if (!policy)
  249. return ERR_PTR(-ENOMEM);
  250. atomic_set(&policy->refcnt, 1);
  251. policy->mode = mode;
  252. policy->flags = flags;
  253. return policy;
  254. }
  255. /* Slow path of a mpol destructor. */
  256. void __mpol_put(struct mempolicy *p)
  257. {
  258. if (!atomic_dec_and_test(&p->refcnt))
  259. return;
  260. kmem_cache_free(policy_cache, p);
  261. }
  262. static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes,
  263. enum mpol_rebind_step step)
  264. {
  265. }
  266. /*
  267. * step:
  268. * MPOL_REBIND_ONCE - do rebind work at once
  269. * MPOL_REBIND_STEP1 - set all the newly nodes
  270. * MPOL_REBIND_STEP2 - clean all the disallowed nodes
  271. */
  272. static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes,
  273. enum mpol_rebind_step step)
  274. {
  275. nodemask_t tmp;
  276. if (pol->flags & MPOL_F_STATIC_NODES)
  277. nodes_and(tmp, pol->w.user_nodemask, *nodes);
  278. else if (pol->flags & MPOL_F_RELATIVE_NODES)
  279. mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
  280. else {
  281. /*
  282. * if step == 1, we use ->w.cpuset_mems_allowed to cache the
  283. * result
  284. */
  285. if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP1) {
  286. nodes_remap(tmp, pol->v.nodes,
  287. pol->w.cpuset_mems_allowed, *nodes);
  288. pol->w.cpuset_mems_allowed = step ? tmp : *nodes;
  289. } else if (step == MPOL_REBIND_STEP2) {
  290. tmp = pol->w.cpuset_mems_allowed;
  291. pol->w.cpuset_mems_allowed = *nodes;
  292. } else
  293. BUG();
  294. }
  295. if (nodes_empty(tmp))
  296. tmp = *nodes;
  297. if (step == MPOL_REBIND_STEP1)
  298. nodes_or(pol->v.nodes, pol->v.nodes, tmp);
  299. else if (step == MPOL_REBIND_ONCE || step == MPOL_REBIND_STEP2)
  300. pol->v.nodes = tmp;
  301. else
  302. BUG();
  303. if (!node_isset(current->il_next, tmp)) {
  304. current->il_next = next_node(current->il_next, tmp);
  305. if (current->il_next >= MAX_NUMNODES)
  306. current->il_next = first_node(tmp);
  307. if (current->il_next >= MAX_NUMNODES)
  308. current->il_next = numa_node_id();
  309. }
  310. }
  311. static void mpol_rebind_preferred(struct mempolicy *pol,
  312. const nodemask_t *nodes,
  313. enum mpol_rebind_step step)
  314. {
  315. nodemask_t tmp;
  316. if (pol->flags & MPOL_F_STATIC_NODES) {
  317. int node = first_node(pol->w.user_nodemask);
  318. if (node_isset(node, *nodes)) {
  319. pol->v.preferred_node = node;
  320. pol->flags &= ~MPOL_F_LOCAL;
  321. } else
  322. pol->flags |= MPOL_F_LOCAL;
  323. } else if (pol->flags & MPOL_F_RELATIVE_NODES) {
  324. mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
  325. pol->v.preferred_node = first_node(tmp);
  326. } else if (!(pol->flags & MPOL_F_LOCAL)) {
  327. pol->v.preferred_node = node_remap(pol->v.preferred_node,
  328. pol->w.cpuset_mems_allowed,
  329. *nodes);
  330. pol->w.cpuset_mems_allowed = *nodes;
  331. }
  332. }
  333. /*
  334. * mpol_rebind_policy - Migrate a policy to a different set of nodes
  335. *
  336. * If read-side task has no lock to protect task->mempolicy, write-side
  337. * task will rebind the task->mempolicy by two step. The first step is
  338. * setting all the newly nodes, and the second step is cleaning all the
  339. * disallowed nodes. In this way, we can avoid finding no node to alloc
  340. * page.
  341. * If we have a lock to protect task->mempolicy in read-side, we do
  342. * rebind directly.
  343. *
  344. * step:
  345. * MPOL_REBIND_ONCE - do rebind work at once
  346. * MPOL_REBIND_STEP1 - set all the newly nodes
  347. * MPOL_REBIND_STEP2 - clean all the disallowed nodes
  348. */
  349. static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask,
  350. enum mpol_rebind_step step)
  351. {
  352. if (!pol)
  353. return;
  354. if (!mpol_store_user_nodemask(pol) && step == 0 &&
  355. nodes_equal(pol->w.cpuset_mems_allowed, *newmask))
  356. return;
  357. if (step == MPOL_REBIND_STEP1 && (pol->flags & MPOL_F_REBINDING))
  358. return;
  359. if (step == MPOL_REBIND_STEP2 && !(pol->flags & MPOL_F_REBINDING))
  360. BUG();
  361. if (step == MPOL_REBIND_STEP1)
  362. pol->flags |= MPOL_F_REBINDING;
  363. else if (step == MPOL_REBIND_STEP2)
  364. pol->flags &= ~MPOL_F_REBINDING;
  365. else if (step >= MPOL_REBIND_NSTEP)
  366. BUG();
  367. mpol_ops[pol->mode].rebind(pol, newmask, step);
  368. }
  369. /*
  370. * Wrapper for mpol_rebind_policy() that just requires task
  371. * pointer, and updates task mempolicy.
  372. *
  373. * Called with task's alloc_lock held.
  374. */
  375. void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
  376. enum mpol_rebind_step step)
  377. {
  378. mpol_rebind_policy(tsk->mempolicy, new, step);
  379. }
  380. /*
  381. * Rebind each vma in mm to new nodemask.
  382. *
  383. * Call holding a reference to mm. Takes mm->mmap_sem during call.
  384. */
  385. void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
  386. {
  387. struct vm_area_struct *vma;
  388. down_write(&mm->mmap_sem);
  389. for (vma = mm->mmap; vma; vma = vma->vm_next)
  390. mpol_rebind_policy(vma->vm_policy, new, MPOL_REBIND_ONCE);
  391. up_write(&mm->mmap_sem);
  392. }
  393. static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
  394. [MPOL_DEFAULT] = {
  395. .rebind = mpol_rebind_default,
  396. },
  397. [MPOL_INTERLEAVE] = {
  398. .create = mpol_new_interleave,
  399. .rebind = mpol_rebind_nodemask,
  400. },
  401. [MPOL_PREFERRED] = {
  402. .create = mpol_new_preferred,
  403. .rebind = mpol_rebind_preferred,
  404. },
  405. [MPOL_BIND] = {
  406. .create = mpol_new_bind,
  407. .rebind = mpol_rebind_nodemask,
  408. },
  409. };
  410. static void migrate_page_add(struct page *page, struct list_head *pagelist,
  411. unsigned long flags);
  412. /* Scan through pages checking if pages follow certain conditions. */
  413. static int check_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  414. unsigned long addr, unsigned long end,
  415. const nodemask_t *nodes, unsigned long flags,
  416. void *private)
  417. {
  418. pte_t *orig_pte;
  419. pte_t *pte;
  420. spinlock_t *ptl;
  421. orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  422. do {
  423. struct page *page;
  424. int nid;
  425. if (!pte_present(*pte))
  426. continue;
  427. page = vm_normal_page(vma, addr, *pte);
  428. if (!page)
  429. continue;
  430. /*
  431. * vm_normal_page() filters out zero pages, but there might
  432. * still be PageReserved pages to skip, perhaps in a VDSO.
  433. * And we cannot move PageKsm pages sensibly or safely yet.
  434. */
  435. if (PageReserved(page) || PageKsm(page))
  436. continue;
  437. nid = page_to_nid(page);
  438. if (node_isset(nid, *nodes) == !!(flags & MPOL_MF_INVERT))
  439. continue;
  440. if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
  441. migrate_page_add(page, private, flags);
  442. else
  443. break;
  444. } while (pte++, addr += PAGE_SIZE, addr != end);
  445. pte_unmap_unlock(orig_pte, ptl);
  446. return addr != end;
  447. }
  448. static inline int check_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  449. unsigned long addr, unsigned long end,
  450. const nodemask_t *nodes, unsigned long flags,
  451. void *private)
  452. {
  453. pmd_t *pmd;
  454. unsigned long next;
  455. pmd = pmd_offset(pud, addr);
  456. do {
  457. next = pmd_addr_end(addr, end);
  458. split_huge_page_pmd(vma->vm_mm, pmd);
  459. if (pmd_none_or_clear_bad(pmd))
  460. continue;
  461. if (check_pte_range(vma, pmd, addr, next, nodes,
  462. flags, private))
  463. return -EIO;
  464. } while (pmd++, addr = next, addr != end);
  465. return 0;
  466. }
  467. static inline int check_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  468. unsigned long addr, unsigned long end,
  469. const nodemask_t *nodes, unsigned long flags,
  470. void *private)
  471. {
  472. pud_t *pud;
  473. unsigned long next;
  474. pud = pud_offset(pgd, addr);
  475. do {
  476. next = pud_addr_end(addr, end);
  477. if (pud_none_or_clear_bad(pud))
  478. continue;
  479. if (check_pmd_range(vma, pud, addr, next, nodes,
  480. flags, private))
  481. return -EIO;
  482. } while (pud++, addr = next, addr != end);
  483. return 0;
  484. }
  485. static inline int check_pgd_range(struct vm_area_struct *vma,
  486. unsigned long addr, unsigned long end,
  487. const nodemask_t *nodes, unsigned long flags,
  488. void *private)
  489. {
  490. pgd_t *pgd;
  491. unsigned long next;
  492. pgd = pgd_offset(vma->vm_mm, addr);
  493. do {
  494. next = pgd_addr_end(addr, end);
  495. if (pgd_none_or_clear_bad(pgd))
  496. continue;
  497. if (check_pud_range(vma, pgd, addr, next, nodes,
  498. flags, private))
  499. return -EIO;
  500. } while (pgd++, addr = next, addr != end);
  501. return 0;
  502. }
  503. /*
  504. * Check if all pages in a range are on a set of nodes.
  505. * If pagelist != NULL then isolate pages from the LRU and
  506. * put them on the pagelist.
  507. */
  508. static struct vm_area_struct *
  509. check_range(struct mm_struct *mm, unsigned long start, unsigned long end,
  510. const nodemask_t *nodes, unsigned long flags, void *private)
  511. {
  512. int err;
  513. struct vm_area_struct *first, *vma, *prev;
  514. first = find_vma(mm, start);
  515. if (!first)
  516. return ERR_PTR(-EFAULT);
  517. prev = NULL;
  518. for (vma = first; vma && vma->vm_start < end; vma = vma->vm_next) {
  519. if (!(flags & MPOL_MF_DISCONTIG_OK)) {
  520. if (!vma->vm_next && vma->vm_end < end)
  521. return ERR_PTR(-EFAULT);
  522. if (prev && prev->vm_end < vma->vm_start)
  523. return ERR_PTR(-EFAULT);
  524. }
  525. if (!is_vm_hugetlb_page(vma) &&
  526. ((flags & MPOL_MF_STRICT) ||
  527. ((flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) &&
  528. vma_migratable(vma)))) {
  529. unsigned long endvma = vma->vm_end;
  530. if (endvma > end)
  531. endvma = end;
  532. if (vma->vm_start > start)
  533. start = vma->vm_start;
  534. err = check_pgd_range(vma, start, endvma, nodes,
  535. flags, private);
  536. if (err) {
  537. first = ERR_PTR(err);
  538. break;
  539. }
  540. }
  541. prev = vma;
  542. }
  543. return first;
  544. }
  545. /* Apply policy to a single VMA */
  546. static int policy_vma(struct vm_area_struct *vma, struct mempolicy *new)
  547. {
  548. int err = 0;
  549. struct mempolicy *old = vma->vm_policy;
  550. pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n",
  551. vma->vm_start, vma->vm_end, vma->vm_pgoff,
  552. vma->vm_ops, vma->vm_file,
  553. vma->vm_ops ? vma->vm_ops->set_policy : NULL);
  554. if (vma->vm_ops && vma->vm_ops->set_policy)
  555. err = vma->vm_ops->set_policy(vma, new);
  556. if (!err) {
  557. mpol_get(new);
  558. vma->vm_policy = new;
  559. mpol_put(old);
  560. }
  561. return err;
  562. }
  563. /* Step 2: apply policy to a range and do splits. */
  564. static int mbind_range(struct mm_struct *mm, unsigned long start,
  565. unsigned long end, struct mempolicy *new_pol)
  566. {
  567. struct vm_area_struct *next;
  568. struct vm_area_struct *prev;
  569. struct vm_area_struct *vma;
  570. int err = 0;
  571. pgoff_t pgoff;
  572. unsigned long vmstart;
  573. unsigned long vmend;
  574. vma = find_vma_prev(mm, start, &prev);
  575. if (!vma || vma->vm_start > start)
  576. return -EFAULT;
  577. for (; vma && vma->vm_start < end; prev = vma, vma = next) {
  578. next = vma->vm_next;
  579. vmstart = max(start, vma->vm_start);
  580. vmend = min(end, vma->vm_end);
  581. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  582. prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
  583. vma->anon_vma, vma->vm_file, pgoff, new_pol);
  584. if (prev) {
  585. vma = prev;
  586. next = vma->vm_next;
  587. continue;
  588. }
  589. if (vma->vm_start != vmstart) {
  590. err = split_vma(vma->vm_mm, vma, vmstart, 1);
  591. if (err)
  592. goto out;
  593. }
  594. if (vma->vm_end != vmend) {
  595. err = split_vma(vma->vm_mm, vma, vmend, 0);
  596. if (err)
  597. goto out;
  598. }
  599. err = policy_vma(vma, new_pol);
  600. if (err)
  601. goto out;
  602. }
  603. out:
  604. return err;
  605. }
  606. /*
  607. * Update task->flags PF_MEMPOLICY bit: set iff non-default
  608. * mempolicy. Allows more rapid checking of this (combined perhaps
  609. * with other PF_* flag bits) on memory allocation hot code paths.
  610. *
  611. * If called from outside this file, the task 'p' should -only- be
  612. * a newly forked child not yet visible on the task list, because
  613. * manipulating the task flags of a visible task is not safe.
  614. *
  615. * The above limitation is why this routine has the funny name
  616. * mpol_fix_fork_child_flag().
  617. *
  618. * It is also safe to call this with a task pointer of current,
  619. * which the static wrapper mpol_set_task_struct_flag() does,
  620. * for use within this file.
  621. */
  622. void mpol_fix_fork_child_flag(struct task_struct *p)
  623. {
  624. if (p->mempolicy)
  625. p->flags |= PF_MEMPOLICY;
  626. else
  627. p->flags &= ~PF_MEMPOLICY;
  628. }
  629. static void mpol_set_task_struct_flag(void)
  630. {
  631. mpol_fix_fork_child_flag(current);
  632. }
  633. /* Set the process memory policy */
  634. static long do_set_mempolicy(unsigned short mode, unsigned short flags,
  635. nodemask_t *nodes)
  636. {
  637. struct mempolicy *new, *old;
  638. struct mm_struct *mm = current->mm;
  639. NODEMASK_SCRATCH(scratch);
  640. int ret;
  641. if (!scratch)
  642. return -ENOMEM;
  643. new = mpol_new(mode, flags, nodes);
  644. if (IS_ERR(new)) {
  645. ret = PTR_ERR(new);
  646. goto out;
  647. }
  648. /*
  649. * prevent changing our mempolicy while show_numa_maps()
  650. * is using it.
  651. * Note: do_set_mempolicy() can be called at init time
  652. * with no 'mm'.
  653. */
  654. if (mm)
  655. down_write(&mm->mmap_sem);
  656. task_lock(current);
  657. ret = mpol_set_nodemask(new, nodes, scratch);
  658. if (ret) {
  659. task_unlock(current);
  660. if (mm)
  661. up_write(&mm->mmap_sem);
  662. mpol_put(new);
  663. goto out;
  664. }
  665. old = current->mempolicy;
  666. current->mempolicy = new;
  667. mpol_set_task_struct_flag();
  668. if (new && new->mode == MPOL_INTERLEAVE &&
  669. nodes_weight(new->v.nodes))
  670. current->il_next = first_node(new->v.nodes);
  671. task_unlock(current);
  672. if (mm)
  673. up_write(&mm->mmap_sem);
  674. mpol_put(old);
  675. ret = 0;
  676. out:
  677. NODEMASK_SCRATCH_FREE(scratch);
  678. return ret;
  679. }
  680. /*
  681. * Return nodemask for policy for get_mempolicy() query
  682. *
  683. * Called with task's alloc_lock held
  684. */
  685. static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes)
  686. {
  687. nodes_clear(*nodes);
  688. if (p == &default_policy)
  689. return;
  690. switch (p->mode) {
  691. case MPOL_BIND:
  692. /* Fall through */
  693. case MPOL_INTERLEAVE:
  694. *nodes = p->v.nodes;
  695. break;
  696. case MPOL_PREFERRED:
  697. if (!(p->flags & MPOL_F_LOCAL))
  698. node_set(p->v.preferred_node, *nodes);
  699. /* else return empty node mask for local allocation */
  700. break;
  701. default:
  702. BUG();
  703. }
  704. }
  705. static int lookup_node(struct mm_struct *mm, unsigned long addr)
  706. {
  707. struct page *p;
  708. int err;
  709. err = get_user_pages(current, mm, addr & PAGE_MASK, 1, 0, 0, &p, NULL);
  710. if (err >= 0) {
  711. err = page_to_nid(p);
  712. put_page(p);
  713. }
  714. return err;
  715. }
  716. /* Retrieve NUMA policy */
  717. static long do_get_mempolicy(int *policy, nodemask_t *nmask,
  718. unsigned long addr, unsigned long flags)
  719. {
  720. int err;
  721. struct mm_struct *mm = current->mm;
  722. struct vm_area_struct *vma = NULL;
  723. struct mempolicy *pol = current->mempolicy;
  724. if (flags &
  725. ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
  726. return -EINVAL;
  727. if (flags & MPOL_F_MEMS_ALLOWED) {
  728. if (flags & (MPOL_F_NODE|MPOL_F_ADDR))
  729. return -EINVAL;
  730. *policy = 0; /* just so it's initialized */
  731. task_lock(current);
  732. *nmask = cpuset_current_mems_allowed;
  733. task_unlock(current);
  734. return 0;
  735. }
  736. if (flags & MPOL_F_ADDR) {
  737. /*
  738. * Do NOT fall back to task policy if the
  739. * vma/shared policy at addr is NULL. We
  740. * want to return MPOL_DEFAULT in this case.
  741. */
  742. down_read(&mm->mmap_sem);
  743. vma = find_vma_intersection(mm, addr, addr+1);
  744. if (!vma) {
  745. up_read(&mm->mmap_sem);
  746. return -EFAULT;
  747. }
  748. if (vma->vm_ops && vma->vm_ops->get_policy)
  749. pol = vma->vm_ops->get_policy(vma, addr);
  750. else
  751. pol = vma->vm_policy;
  752. } else if (addr)
  753. return -EINVAL;
  754. if (!pol)
  755. pol = &default_policy; /* indicates default behavior */
  756. if (flags & MPOL_F_NODE) {
  757. if (flags & MPOL_F_ADDR) {
  758. err = lookup_node(mm, addr);
  759. if (err < 0)
  760. goto out;
  761. *policy = err;
  762. } else if (pol == current->mempolicy &&
  763. pol->mode == MPOL_INTERLEAVE) {
  764. *policy = current->il_next;
  765. } else {
  766. err = -EINVAL;
  767. goto out;
  768. }
  769. } else {
  770. *policy = pol == &default_policy ? MPOL_DEFAULT :
  771. pol->mode;
  772. /*
  773. * Internal mempolicy flags must be masked off before exposing
  774. * the policy to userspace.
  775. */
  776. *policy |= (pol->flags & MPOL_MODE_FLAGS);
  777. }
  778. if (vma) {
  779. up_read(&current->mm->mmap_sem);
  780. vma = NULL;
  781. }
  782. err = 0;
  783. if (nmask) {
  784. if (mpol_store_user_nodemask(pol)) {
  785. *nmask = pol->w.user_nodemask;
  786. } else {
  787. task_lock(current);
  788. get_policy_nodemask(pol, nmask);
  789. task_unlock(current);
  790. }
  791. }
  792. out:
  793. mpol_cond_put(pol);
  794. if (vma)
  795. up_read(&current->mm->mmap_sem);
  796. return err;
  797. }
  798. #ifdef CONFIG_MIGRATION
  799. /*
  800. * page migration
  801. */
  802. static void migrate_page_add(struct page *page, struct list_head *pagelist,
  803. unsigned long flags)
  804. {
  805. /*
  806. * Avoid migrating a page that is shared with others.
  807. */
  808. if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(page) == 1) {
  809. if (!isolate_lru_page(page)) {
  810. list_add_tail(&page->lru, pagelist);
  811. inc_zone_page_state(page, NR_ISOLATED_ANON +
  812. page_is_file_cache(page));
  813. }
  814. }
  815. }
  816. static struct page *new_node_page(struct page *page, unsigned long node, int **x)
  817. {
  818. return alloc_pages_exact_node(node, GFP_HIGHUSER_MOVABLE, 0);
  819. }
  820. /*
  821. * Migrate pages from one node to a target node.
  822. * Returns error or the number of pages not migrated.
  823. */
  824. static int migrate_to_node(struct mm_struct *mm, int source, int dest,
  825. int flags)
  826. {
  827. nodemask_t nmask;
  828. LIST_HEAD(pagelist);
  829. int err = 0;
  830. struct vm_area_struct *vma;
  831. nodes_clear(nmask);
  832. node_set(source, nmask);
  833. vma = check_range(mm, mm->mmap->vm_start, mm->task_size, &nmask,
  834. flags | MPOL_MF_DISCONTIG_OK, &pagelist);
  835. if (IS_ERR(vma))
  836. return PTR_ERR(vma);
  837. if (!list_empty(&pagelist)) {
  838. err = migrate_pages(&pagelist, new_node_page, dest,
  839. false, true);
  840. if (err)
  841. putback_lru_pages(&pagelist);
  842. }
  843. return err;
  844. }
  845. /*
  846. * Move pages between the two nodesets so as to preserve the physical
  847. * layout as much as possible.
  848. *
  849. * Returns the number of page that could not be moved.
  850. */
  851. int do_migrate_pages(struct mm_struct *mm,
  852. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
  853. {
  854. int busy = 0;
  855. int err;
  856. nodemask_t tmp;
  857. err = migrate_prep();
  858. if (err)
  859. return err;
  860. down_read(&mm->mmap_sem);
  861. err = migrate_vmas(mm, from_nodes, to_nodes, flags);
  862. if (err)
  863. goto out;
  864. /*
  865. * Find a 'source' bit set in 'tmp' whose corresponding 'dest'
  866. * bit in 'to' is not also set in 'tmp'. Clear the found 'source'
  867. * bit in 'tmp', and return that <source, dest> pair for migration.
  868. * The pair of nodemasks 'to' and 'from' define the map.
  869. *
  870. * If no pair of bits is found that way, fallback to picking some
  871. * pair of 'source' and 'dest' bits that are not the same. If the
  872. * 'source' and 'dest' bits are the same, this represents a node
  873. * that will be migrating to itself, so no pages need move.
  874. *
  875. * If no bits are left in 'tmp', or if all remaining bits left
  876. * in 'tmp' correspond to the same bit in 'to', return false
  877. * (nothing left to migrate).
  878. *
  879. * This lets us pick a pair of nodes to migrate between, such that
  880. * if possible the dest node is not already occupied by some other
  881. * source node, minimizing the risk of overloading the memory on a
  882. * node that would happen if we migrated incoming memory to a node
  883. * before migrating outgoing memory source that same node.
  884. *
  885. * A single scan of tmp is sufficient. As we go, we remember the
  886. * most recent <s, d> pair that moved (s != d). If we find a pair
  887. * that not only moved, but what's better, moved to an empty slot
  888. * (d is not set in tmp), then we break out then, with that pair.
  889. * Otherwise when we finish scanning from_tmp, we at least have the
  890. * most recent <s, d> pair that moved. If we get all the way through
  891. * the scan of tmp without finding any node that moved, much less
  892. * moved to an empty node, then there is nothing left worth migrating.
  893. */
  894. tmp = *from_nodes;
  895. while (!nodes_empty(tmp)) {
  896. int s,d;
  897. int source = -1;
  898. int dest = 0;
  899. for_each_node_mask(s, tmp) {
  900. d = node_remap(s, *from_nodes, *to_nodes);
  901. if (s == d)
  902. continue;
  903. source = s; /* Node moved. Memorize */
  904. dest = d;
  905. /* dest not in remaining from nodes? */
  906. if (!node_isset(dest, tmp))
  907. break;
  908. }
  909. if (source == -1)
  910. break;
  911. node_clear(source, tmp);
  912. err = migrate_to_node(mm, source, dest, flags);
  913. if (err > 0)
  914. busy += err;
  915. if (err < 0)
  916. break;
  917. }
  918. out:
  919. up_read(&mm->mmap_sem);
  920. if (err < 0)
  921. return err;
  922. return busy;
  923. }
  924. /*
  925. * Allocate a new page for page migration based on vma policy.
  926. * Start assuming that page is mapped by vma pointed to by @private.
  927. * Search forward from there, if not. N.B., this assumes that the
  928. * list of pages handed to migrate_pages()--which is how we get here--
  929. * is in virtual address order.
  930. */
  931. static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
  932. {
  933. struct vm_area_struct *vma = (struct vm_area_struct *)private;
  934. unsigned long uninitialized_var(address);
  935. while (vma) {
  936. address = page_address_in_vma(page, vma);
  937. if (address != -EFAULT)
  938. break;
  939. vma = vma->vm_next;
  940. }
  941. /*
  942. * if !vma, alloc_page_vma() will use task or system default policy
  943. */
  944. return alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  945. }
  946. #else
  947. static void migrate_page_add(struct page *page, struct list_head *pagelist,
  948. unsigned long flags)
  949. {
  950. }
  951. int do_migrate_pages(struct mm_struct *mm,
  952. const nodemask_t *from_nodes, const nodemask_t *to_nodes, int flags)
  953. {
  954. return -ENOSYS;
  955. }
  956. static struct page *new_vma_page(struct page *page, unsigned long private, int **x)
  957. {
  958. return NULL;
  959. }
  960. #endif
  961. static long do_mbind(unsigned long start, unsigned long len,
  962. unsigned short mode, unsigned short mode_flags,
  963. nodemask_t *nmask, unsigned long flags)
  964. {
  965. struct vm_area_struct *vma;
  966. struct mm_struct *mm = current->mm;
  967. struct mempolicy *new;
  968. unsigned long end;
  969. int err;
  970. LIST_HEAD(pagelist);
  971. if (flags & ~(unsigned long)(MPOL_MF_STRICT |
  972. MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))
  973. return -EINVAL;
  974. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  975. return -EPERM;
  976. if (start & ~PAGE_MASK)
  977. return -EINVAL;
  978. if (mode == MPOL_DEFAULT)
  979. flags &= ~MPOL_MF_STRICT;
  980. len = (len + PAGE_SIZE - 1) & PAGE_MASK;
  981. end = start + len;
  982. if (end < start)
  983. return -EINVAL;
  984. if (end == start)
  985. return 0;
  986. new = mpol_new(mode, mode_flags, nmask);
  987. if (IS_ERR(new))
  988. return PTR_ERR(new);
  989. /*
  990. * If we are using the default policy then operation
  991. * on discontinuous address spaces is okay after all
  992. */
  993. if (!new)
  994. flags |= MPOL_MF_DISCONTIG_OK;
  995. pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n",
  996. start, start + len, mode, mode_flags,
  997. nmask ? nodes_addr(*nmask)[0] : -1);
  998. if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) {
  999. err = migrate_prep();
  1000. if (err)
  1001. goto mpol_out;
  1002. }
  1003. {
  1004. NODEMASK_SCRATCH(scratch);
  1005. if (scratch) {
  1006. down_write(&mm->mmap_sem);
  1007. task_lock(current);
  1008. err = mpol_set_nodemask(new, nmask, scratch);
  1009. task_unlock(current);
  1010. if (err)
  1011. up_write(&mm->mmap_sem);
  1012. } else
  1013. err = -ENOMEM;
  1014. NODEMASK_SCRATCH_FREE(scratch);
  1015. }
  1016. if (err)
  1017. goto mpol_out;
  1018. vma = check_range(mm, start, end, nmask,
  1019. flags | MPOL_MF_INVERT, &pagelist);
  1020. err = PTR_ERR(vma);
  1021. if (!IS_ERR(vma)) {
  1022. int nr_failed = 0;
  1023. err = mbind_range(mm, start, end, new);
  1024. if (!list_empty(&pagelist)) {
  1025. nr_failed = migrate_pages(&pagelist, new_vma_page,
  1026. (unsigned long)vma,
  1027. false, true);
  1028. if (nr_failed)
  1029. putback_lru_pages(&pagelist);
  1030. }
  1031. if (!err && nr_failed && (flags & MPOL_MF_STRICT))
  1032. err = -EIO;
  1033. } else
  1034. putback_lru_pages(&pagelist);
  1035. up_write(&mm->mmap_sem);
  1036. mpol_out:
  1037. mpol_put(new);
  1038. return err;
  1039. }
  1040. /*
  1041. * User space interface with variable sized bitmaps for nodelists.
  1042. */
  1043. /* Copy a node mask from user space. */
  1044. static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
  1045. unsigned long maxnode)
  1046. {
  1047. unsigned long k;
  1048. unsigned long nlongs;
  1049. unsigned long endmask;
  1050. --maxnode;
  1051. nodes_clear(*nodes);
  1052. if (maxnode == 0 || !nmask)
  1053. return 0;
  1054. if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
  1055. return -EINVAL;
  1056. nlongs = BITS_TO_LONGS(maxnode);
  1057. if ((maxnode % BITS_PER_LONG) == 0)
  1058. endmask = ~0UL;
  1059. else
  1060. endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1;
  1061. /* When the user specified more nodes than supported just check
  1062. if the non supported part is all zero. */
  1063. if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) {
  1064. if (nlongs > PAGE_SIZE/sizeof(long))
  1065. return -EINVAL;
  1066. for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) {
  1067. unsigned long t;
  1068. if (get_user(t, nmask + k))
  1069. return -EFAULT;
  1070. if (k == nlongs - 1) {
  1071. if (t & endmask)
  1072. return -EINVAL;
  1073. } else if (t)
  1074. return -EINVAL;
  1075. }
  1076. nlongs = BITS_TO_LONGS(MAX_NUMNODES);
  1077. endmask = ~0UL;
  1078. }
  1079. if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long)))
  1080. return -EFAULT;
  1081. nodes_addr(*nodes)[nlongs-1] &= endmask;
  1082. return 0;
  1083. }
  1084. /* Copy a kernel node mask to user space */
  1085. static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
  1086. nodemask_t *nodes)
  1087. {
  1088. unsigned long copy = ALIGN(maxnode-1, 64) / 8;
  1089. const int nbytes = BITS_TO_LONGS(MAX_NUMNODES) * sizeof(long);
  1090. if (copy > nbytes) {
  1091. if (copy > PAGE_SIZE)
  1092. return -EINVAL;
  1093. if (clear_user((char __user *)mask + nbytes, copy - nbytes))
  1094. return -EFAULT;
  1095. copy = nbytes;
  1096. }
  1097. return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
  1098. }
  1099. SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
  1100. unsigned long, mode, unsigned long __user *, nmask,
  1101. unsigned long, maxnode, unsigned, flags)
  1102. {
  1103. nodemask_t nodes;
  1104. int err;
  1105. unsigned short mode_flags;
  1106. mode_flags = mode & MPOL_MODE_FLAGS;
  1107. mode &= ~MPOL_MODE_FLAGS;
  1108. if (mode >= MPOL_MAX)
  1109. return -EINVAL;
  1110. if ((mode_flags & MPOL_F_STATIC_NODES) &&
  1111. (mode_flags & MPOL_F_RELATIVE_NODES))
  1112. return -EINVAL;
  1113. err = get_nodes(&nodes, nmask, maxnode);
  1114. if (err)
  1115. return err;
  1116. return do_mbind(start, len, mode, mode_flags, &nodes, flags);
  1117. }
  1118. /* Set the process memory policy */
  1119. SYSCALL_DEFINE3(set_mempolicy, int, mode, unsigned long __user *, nmask,
  1120. unsigned long, maxnode)
  1121. {
  1122. int err;
  1123. nodemask_t nodes;
  1124. unsigned short flags;
  1125. flags = mode & MPOL_MODE_FLAGS;
  1126. mode &= ~MPOL_MODE_FLAGS;
  1127. if ((unsigned int)mode >= MPOL_MAX)
  1128. return -EINVAL;
  1129. if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
  1130. return -EINVAL;
  1131. err = get_nodes(&nodes, nmask, maxnode);
  1132. if (err)
  1133. return err;
  1134. return do_set_mempolicy(mode, flags, &nodes);
  1135. }
  1136. SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode,
  1137. const unsigned long __user *, old_nodes,
  1138. const unsigned long __user *, new_nodes)
  1139. {
  1140. const struct cred *cred = current_cred(), *tcred;
  1141. struct mm_struct *mm = NULL;
  1142. struct task_struct *task;
  1143. nodemask_t task_nodes;
  1144. int err;
  1145. nodemask_t *old;
  1146. nodemask_t *new;
  1147. NODEMASK_SCRATCH(scratch);
  1148. if (!scratch)
  1149. return -ENOMEM;
  1150. old = &scratch->mask1;
  1151. new = &scratch->mask2;
  1152. err = get_nodes(old, old_nodes, maxnode);
  1153. if (err)
  1154. goto out;
  1155. err = get_nodes(new, new_nodes, maxnode);
  1156. if (err)
  1157. goto out;
  1158. /* Find the mm_struct */
  1159. rcu_read_lock();
  1160. task = pid ? find_task_by_vpid(pid) : current;
  1161. if (!task) {
  1162. rcu_read_unlock();
  1163. err = -ESRCH;
  1164. goto out;
  1165. }
  1166. mm = get_task_mm(task);
  1167. rcu_read_unlock();
  1168. err = -EINVAL;
  1169. if (!mm)
  1170. goto out;
  1171. /*
  1172. * Check if this process has the right to modify the specified
  1173. * process. The right exists if the process has administrative
  1174. * capabilities, superuser privileges or the same
  1175. * userid as the target process.
  1176. */
  1177. rcu_read_lock();
  1178. tcred = __task_cred(task);
  1179. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1180. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1181. !capable(CAP_SYS_NICE)) {
  1182. rcu_read_unlock();
  1183. err = -EPERM;
  1184. goto out;
  1185. }
  1186. rcu_read_unlock();
  1187. task_nodes = cpuset_mems_allowed(task);
  1188. /* Is the user allowed to access the target nodes? */
  1189. if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) {
  1190. err = -EPERM;
  1191. goto out;
  1192. }
  1193. if (!nodes_subset(*new, node_states[N_HIGH_MEMORY])) {
  1194. err = -EINVAL;
  1195. goto out;
  1196. }
  1197. err = security_task_movememory(task);
  1198. if (err)
  1199. goto out;
  1200. err = do_migrate_pages(mm, old, new,
  1201. capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE);
  1202. out:
  1203. if (mm)
  1204. mmput(mm);
  1205. NODEMASK_SCRATCH_FREE(scratch);
  1206. return err;
  1207. }
  1208. /* Retrieve NUMA policy */
  1209. SYSCALL_DEFINE5(get_mempolicy, int __user *, policy,
  1210. unsigned long __user *, nmask, unsigned long, maxnode,
  1211. unsigned long, addr, unsigned long, flags)
  1212. {
  1213. int err;
  1214. int uninitialized_var(pval);
  1215. nodemask_t nodes;
  1216. if (nmask != NULL && maxnode < MAX_NUMNODES)
  1217. return -EINVAL;
  1218. err = do_get_mempolicy(&pval, &nodes, addr, flags);
  1219. if (err)
  1220. return err;
  1221. if (policy && put_user(pval, policy))
  1222. return -EFAULT;
  1223. if (nmask)
  1224. err = copy_nodes_to_user(nmask, maxnode, &nodes);
  1225. return err;
  1226. }
  1227. #ifdef CONFIG_COMPAT
  1228. asmlinkage long compat_sys_get_mempolicy(int __user *policy,
  1229. compat_ulong_t __user *nmask,
  1230. compat_ulong_t maxnode,
  1231. compat_ulong_t addr, compat_ulong_t flags)
  1232. {
  1233. long err;
  1234. unsigned long __user *nm = NULL;
  1235. unsigned long nr_bits, alloc_size;
  1236. DECLARE_BITMAP(bm, MAX_NUMNODES);
  1237. nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
  1238. alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  1239. if (nmask)
  1240. nm = compat_alloc_user_space(alloc_size);
  1241. err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags);
  1242. if (!err && nmask) {
  1243. err = copy_from_user(bm, nm, alloc_size);
  1244. /* ensure entire bitmap is zeroed */
  1245. err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8);
  1246. err |= compat_put_bitmap(nmask, bm, nr_bits);
  1247. }
  1248. return err;
  1249. }
  1250. asmlinkage long compat_sys_set_mempolicy(int mode, compat_ulong_t __user *nmask,
  1251. compat_ulong_t maxnode)
  1252. {
  1253. long err = 0;
  1254. unsigned long __user *nm = NULL;
  1255. unsigned long nr_bits, alloc_size;
  1256. DECLARE_BITMAP(bm, MAX_NUMNODES);
  1257. nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
  1258. alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  1259. if (nmask) {
  1260. err = compat_get_bitmap(bm, nmask, nr_bits);
  1261. nm = compat_alloc_user_space(alloc_size);
  1262. err |= copy_to_user(nm, bm, alloc_size);
  1263. }
  1264. if (err)
  1265. return -EFAULT;
  1266. return sys_set_mempolicy(mode, nm, nr_bits+1);
  1267. }
  1268. asmlinkage long compat_sys_mbind(compat_ulong_t start, compat_ulong_t len,
  1269. compat_ulong_t mode, compat_ulong_t __user *nmask,
  1270. compat_ulong_t maxnode, compat_ulong_t flags)
  1271. {
  1272. long err = 0;
  1273. unsigned long __user *nm = NULL;
  1274. unsigned long nr_bits, alloc_size;
  1275. nodemask_t bm;
  1276. nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
  1277. alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
  1278. if (nmask) {
  1279. err = compat_get_bitmap(nodes_addr(bm), nmask, nr_bits);
  1280. nm = compat_alloc_user_space(alloc_size);
  1281. err |= copy_to_user(nm, nodes_addr(bm), alloc_size);
  1282. }
  1283. if (err)
  1284. return -EFAULT;
  1285. return sys_mbind(start, len, mode, nm, nr_bits+1, flags);
  1286. }
  1287. #endif
  1288. /*
  1289. * get_vma_policy(@task, @vma, @addr)
  1290. * @task - task for fallback if vma policy == default
  1291. * @vma - virtual memory area whose policy is sought
  1292. * @addr - address in @vma for shared policy lookup
  1293. *
  1294. * Returns effective policy for a VMA at specified address.
  1295. * Falls back to @task or system default policy, as necessary.
  1296. * Current or other task's task mempolicy and non-shared vma policies
  1297. * are protected by the task's mmap_sem, which must be held for read by
  1298. * the caller.
  1299. * Shared policies [those marked as MPOL_F_SHARED] require an extra reference
  1300. * count--added by the get_policy() vm_op, as appropriate--to protect against
  1301. * freeing by another task. It is the caller's responsibility to free the
  1302. * extra reference for shared policies.
  1303. */
  1304. struct mempolicy *get_vma_policy(struct task_struct *task,
  1305. struct vm_area_struct *vma, unsigned long addr)
  1306. {
  1307. struct mempolicy *pol = task->mempolicy;
  1308. if (vma) {
  1309. if (vma->vm_ops && vma->vm_ops->get_policy) {
  1310. struct mempolicy *vpol = vma->vm_ops->get_policy(vma,
  1311. addr);
  1312. if (vpol)
  1313. pol = vpol;
  1314. } else if (vma->vm_policy)
  1315. pol = vma->vm_policy;
  1316. }
  1317. if (!pol)
  1318. pol = &default_policy;
  1319. return pol;
  1320. }
  1321. /*
  1322. * Return a nodemask representing a mempolicy for filtering nodes for
  1323. * page allocation
  1324. */
  1325. static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
  1326. {
  1327. /* Lower zones don't get a nodemask applied for MPOL_BIND */
  1328. if (unlikely(policy->mode == MPOL_BIND) &&
  1329. gfp_zone(gfp) >= policy_zone &&
  1330. cpuset_nodemask_valid_mems_allowed(&policy->v.nodes))
  1331. return &policy->v.nodes;
  1332. return NULL;
  1333. }
  1334. /* Return a zonelist indicated by gfp for node representing a mempolicy */
  1335. static struct zonelist *policy_zonelist(gfp_t gfp, struct mempolicy *policy,
  1336. int nd)
  1337. {
  1338. switch (policy->mode) {
  1339. case MPOL_PREFERRED:
  1340. if (!(policy->flags & MPOL_F_LOCAL))
  1341. nd = policy->v.preferred_node;
  1342. break;
  1343. case MPOL_BIND:
  1344. /*
  1345. * Normally, MPOL_BIND allocations are node-local within the
  1346. * allowed nodemask. However, if __GFP_THISNODE is set and the
  1347. * current node isn't part of the mask, we use the zonelist for
  1348. * the first node in the mask instead.
  1349. */
  1350. if (unlikely(gfp & __GFP_THISNODE) &&
  1351. unlikely(!node_isset(nd, policy->v.nodes)))
  1352. nd = first_node(policy->v.nodes);
  1353. break;
  1354. default:
  1355. BUG();
  1356. }
  1357. return node_zonelist(nd, gfp);
  1358. }
  1359. /* Do dynamic interleaving for a process */
  1360. static unsigned interleave_nodes(struct mempolicy *policy)
  1361. {
  1362. unsigned nid, next;
  1363. struct task_struct *me = current;
  1364. nid = me->il_next;
  1365. next = next_node(nid, policy->v.nodes);
  1366. if (next >= MAX_NUMNODES)
  1367. next = first_node(policy->v.nodes);
  1368. if (next < MAX_NUMNODES)
  1369. me->il_next = next;
  1370. return nid;
  1371. }
  1372. /*
  1373. * Depending on the memory policy provide a node from which to allocate the
  1374. * next slab entry.
  1375. * @policy must be protected by freeing by the caller. If @policy is
  1376. * the current task's mempolicy, this protection is implicit, as only the
  1377. * task can change it's policy. The system default policy requires no
  1378. * such protection.
  1379. */
  1380. unsigned slab_node(struct mempolicy *policy)
  1381. {
  1382. if (!policy || policy->flags & MPOL_F_LOCAL)
  1383. return numa_node_id();
  1384. switch (policy->mode) {
  1385. case MPOL_PREFERRED:
  1386. /*
  1387. * handled MPOL_F_LOCAL above
  1388. */
  1389. return policy->v.preferred_node;
  1390. case MPOL_INTERLEAVE:
  1391. return interleave_nodes(policy);
  1392. case MPOL_BIND: {
  1393. /*
  1394. * Follow bind policy behavior and start allocation at the
  1395. * first node.
  1396. */
  1397. struct zonelist *zonelist;
  1398. struct zone *zone;
  1399. enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL);
  1400. zonelist = &NODE_DATA(numa_node_id())->node_zonelists[0];
  1401. (void)first_zones_zonelist(zonelist, highest_zoneidx,
  1402. &policy->v.nodes,
  1403. &zone);
  1404. return zone ? zone->node : numa_node_id();
  1405. }
  1406. default:
  1407. BUG();
  1408. }
  1409. }
  1410. /* Do static interleaving for a VMA with known offset. */
  1411. static unsigned offset_il_node(struct mempolicy *pol,
  1412. struct vm_area_struct *vma, unsigned long off)
  1413. {
  1414. unsigned nnodes = nodes_weight(pol->v.nodes);
  1415. unsigned target;
  1416. int c;
  1417. int nid = -1;
  1418. if (!nnodes)
  1419. return numa_node_id();
  1420. target = (unsigned int)off % nnodes;
  1421. c = 0;
  1422. do {
  1423. nid = next_node(nid, pol->v.nodes);
  1424. c++;
  1425. } while (c <= target);
  1426. return nid;
  1427. }
  1428. /* Determine a node number for interleave */
  1429. static inline unsigned interleave_nid(struct mempolicy *pol,
  1430. struct vm_area_struct *vma, unsigned long addr, int shift)
  1431. {
  1432. if (vma) {
  1433. unsigned long off;
  1434. /*
  1435. * for small pages, there is no difference between
  1436. * shift and PAGE_SHIFT, so the bit-shift is safe.
  1437. * for huge pages, since vm_pgoff is in units of small
  1438. * pages, we need to shift off the always 0 bits to get
  1439. * a useful offset.
  1440. */
  1441. BUG_ON(shift < PAGE_SHIFT);
  1442. off = vma->vm_pgoff >> (shift - PAGE_SHIFT);
  1443. off += (addr - vma->vm_start) >> shift;
  1444. return offset_il_node(pol, vma, off);
  1445. } else
  1446. return interleave_nodes(pol);
  1447. }
  1448. /*
  1449. * Return the bit number of a random bit set in the nodemask.
  1450. * (returns -1 if nodemask is empty)
  1451. */
  1452. int node_random(const nodemask_t *maskp)
  1453. {
  1454. int w, bit = -1;
  1455. w = nodes_weight(*maskp);
  1456. if (w)
  1457. bit = bitmap_ord_to_pos(maskp->bits,
  1458. get_random_int() % w, MAX_NUMNODES);
  1459. return bit;
  1460. }
  1461. #ifdef CONFIG_HUGETLBFS
  1462. /*
  1463. * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
  1464. * @vma = virtual memory area whose policy is sought
  1465. * @addr = address in @vma for shared policy lookup and interleave policy
  1466. * @gfp_flags = for requested zone
  1467. * @mpol = pointer to mempolicy pointer for reference counted mempolicy
  1468. * @nodemask = pointer to nodemask pointer for MPOL_BIND nodemask
  1469. *
  1470. * Returns a zonelist suitable for a huge page allocation and a pointer
  1471. * to the struct mempolicy for conditional unref after allocation.
  1472. * If the effective policy is 'BIND, returns a pointer to the mempolicy's
  1473. * @nodemask for filtering the zonelist.
  1474. *
  1475. * Must be protected by get_mems_allowed()
  1476. */
  1477. struct zonelist *huge_zonelist(struct vm_area_struct *vma, unsigned long addr,
  1478. gfp_t gfp_flags, struct mempolicy **mpol,
  1479. nodemask_t **nodemask)
  1480. {
  1481. struct zonelist *zl;
  1482. *mpol = get_vma_policy(current, vma, addr);
  1483. *nodemask = NULL; /* assume !MPOL_BIND */
  1484. if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) {
  1485. zl = node_zonelist(interleave_nid(*mpol, vma, addr,
  1486. huge_page_shift(hstate_vma(vma))), gfp_flags);
  1487. } else {
  1488. zl = policy_zonelist(gfp_flags, *mpol, numa_node_id());
  1489. if ((*mpol)->mode == MPOL_BIND)
  1490. *nodemask = &(*mpol)->v.nodes;
  1491. }
  1492. return zl;
  1493. }
  1494. /*
  1495. * init_nodemask_of_mempolicy
  1496. *
  1497. * If the current task's mempolicy is "default" [NULL], return 'false'
  1498. * to indicate default policy. Otherwise, extract the policy nodemask
  1499. * for 'bind' or 'interleave' policy into the argument nodemask, or
  1500. * initialize the argument nodemask to contain the single node for
  1501. * 'preferred' or 'local' policy and return 'true' to indicate presence
  1502. * of non-default mempolicy.
  1503. *
  1504. * We don't bother with reference counting the mempolicy [mpol_get/put]
  1505. * because the current task is examining it's own mempolicy and a task's
  1506. * mempolicy is only ever changed by the task itself.
  1507. *
  1508. * N.B., it is the caller's responsibility to free a returned nodemask.
  1509. */
  1510. bool init_nodemask_of_mempolicy(nodemask_t *mask)
  1511. {
  1512. struct mempolicy *mempolicy;
  1513. int nid;
  1514. if (!(mask && current->mempolicy))
  1515. return false;
  1516. task_lock(current);
  1517. mempolicy = current->mempolicy;
  1518. switch (mempolicy->mode) {
  1519. case MPOL_PREFERRED:
  1520. if (mempolicy->flags & MPOL_F_LOCAL)
  1521. nid = numa_node_id();
  1522. else
  1523. nid = mempolicy->v.preferred_node;
  1524. init_nodemask_of_node(mask, nid);
  1525. break;
  1526. case MPOL_BIND:
  1527. /* Fall through */
  1528. case MPOL_INTERLEAVE:
  1529. *mask = mempolicy->v.nodes;
  1530. break;
  1531. default:
  1532. BUG();
  1533. }
  1534. task_unlock(current);
  1535. return true;
  1536. }
  1537. #endif
  1538. /*
  1539. * mempolicy_nodemask_intersects
  1540. *
  1541. * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
  1542. * policy. Otherwise, check for intersection between mask and the policy
  1543. * nodemask for 'bind' or 'interleave' policy. For 'perferred' or 'local'
  1544. * policy, always return true since it may allocate elsewhere on fallback.
  1545. *
  1546. * Takes task_lock(tsk) to prevent freeing of its mempolicy.
  1547. */
  1548. bool mempolicy_nodemask_intersects(struct task_struct *tsk,
  1549. const nodemask_t *mask)
  1550. {
  1551. struct mempolicy *mempolicy;
  1552. bool ret = true;
  1553. if (!mask)
  1554. return ret;
  1555. task_lock(tsk);
  1556. mempolicy = tsk->mempolicy;
  1557. if (!mempolicy)
  1558. goto out;
  1559. switch (mempolicy->mode) {
  1560. case MPOL_PREFERRED:
  1561. /*
  1562. * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
  1563. * allocate from, they may fallback to other nodes when oom.
  1564. * Thus, it's possible for tsk to have allocated memory from
  1565. * nodes in mask.
  1566. */
  1567. break;
  1568. case MPOL_BIND:
  1569. case MPOL_INTERLEAVE:
  1570. ret = nodes_intersects(mempolicy->v.nodes, *mask);
  1571. break;
  1572. default:
  1573. BUG();
  1574. }
  1575. out:
  1576. task_unlock(tsk);
  1577. return ret;
  1578. }
  1579. /* Allocate a page in interleaved policy.
  1580. Own path because it needs to do special accounting. */
  1581. static struct page *alloc_page_interleave(gfp_t gfp, unsigned order,
  1582. unsigned nid)
  1583. {
  1584. struct zonelist *zl;
  1585. struct page *page;
  1586. zl = node_zonelist(nid, gfp);
  1587. page = __alloc_pages(gfp, order, zl);
  1588. if (page && page_zone(page) == zonelist_zone(&zl->_zonerefs[0]))
  1589. inc_zone_page_state(page, NUMA_INTERLEAVE_HIT);
  1590. return page;
  1591. }
  1592. /**
  1593. * alloc_pages_vma - Allocate a page for a VMA.
  1594. *
  1595. * @gfp:
  1596. * %GFP_USER user allocation.
  1597. * %GFP_KERNEL kernel allocations,
  1598. * %GFP_HIGHMEM highmem/user allocations,
  1599. * %GFP_FS allocation should not call back into a file system.
  1600. * %GFP_ATOMIC don't sleep.
  1601. *
  1602. * @order:Order of the GFP allocation.
  1603. * @vma: Pointer to VMA or NULL if not available.
  1604. * @addr: Virtual Address of the allocation. Must be inside the VMA.
  1605. *
  1606. * This function allocates a page from the kernel page pool and applies
  1607. * a NUMA policy associated with the VMA or the current process.
  1608. * When VMA is not NULL caller must hold down_read on the mmap_sem of the
  1609. * mm_struct of the VMA to prevent it from going away. Should be used for
  1610. * all allocations for pages that will be mapped into
  1611. * user space. Returns NULL when no page can be allocated.
  1612. *
  1613. * Should be called with the mm_sem of the vma hold.
  1614. */
  1615. struct page *
  1616. alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma,
  1617. unsigned long addr, int node)
  1618. {
  1619. struct mempolicy *pol = get_vma_policy(current, vma, addr);
  1620. struct zonelist *zl;
  1621. struct page *page;
  1622. get_mems_allowed();
  1623. if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
  1624. unsigned nid;
  1625. nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order);
  1626. mpol_cond_put(pol);
  1627. page = alloc_page_interleave(gfp, order, nid);
  1628. put_mems_allowed();
  1629. return page;
  1630. }
  1631. zl = policy_zonelist(gfp, pol, node);
  1632. if (unlikely(mpol_needs_cond_ref(pol))) {
  1633. /*
  1634. * slow path: ref counted shared policy
  1635. */
  1636. struct page *page = __alloc_pages_nodemask(gfp, order,
  1637. zl, policy_nodemask(gfp, pol));
  1638. __mpol_put(pol);
  1639. put_mems_allowed();
  1640. return page;
  1641. }
  1642. /*
  1643. * fast path: default or task policy
  1644. */
  1645. page = __alloc_pages_nodemask(gfp, order, zl,
  1646. policy_nodemask(gfp, pol));
  1647. put_mems_allowed();
  1648. return page;
  1649. }
  1650. /**
  1651. * alloc_pages_current - Allocate pages.
  1652. *
  1653. * @gfp:
  1654. * %GFP_USER user allocation,
  1655. * %GFP_KERNEL kernel allocation,
  1656. * %GFP_HIGHMEM highmem allocation,
  1657. * %GFP_FS don't call back into a file system.
  1658. * %GFP_ATOMIC don't sleep.
  1659. * @order: Power of two of allocation size in pages. 0 is a single page.
  1660. *
  1661. * Allocate a page from the kernel page pool. When not in
  1662. * interrupt context and apply the current process NUMA policy.
  1663. * Returns NULL when no page can be allocated.
  1664. *
  1665. * Don't call cpuset_update_task_memory_state() unless
  1666. * 1) it's ok to take cpuset_s