pgtable.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include <linux/mm.h>
  2. #include <linux/gfp.h>
  3. #include <asm/pgalloc.h>
  4. #include <asm/pgtable.h>
  5. #include <asm/tlb.h>
  6. #include <asm/fixmap.h>
  7. #define PGALLOC_GFP GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __GFP_ZERO
  8. #ifdef CONFIG_HIGHPTE
  9. #define PGALLOC_USER_GFP __GFP_HIGHMEM
  10. #else
  11. #define PGALLOC_USER_GFP 0
  12. #endif
  13. gfp_t __userpte_alloc_gfp = PGALLOC_GFP | PGALLOC_USER_GFP;
  14. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  15. {
  16. return (pte_t *)__get_free_page(PGALLOC_GFP);
  17. }
  18. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  19. {
  20. struct page *pte;
  21. pte = alloc_pages(__userpte_alloc_gfp, 0);
  22. if (pte)
  23. pgtable_page_ctor(pte);
  24. return pte;
  25. }
  26. static int __init setup_userpte(char *arg)
  27. {
  28. if (!arg)
  29. return -EINVAL;
  30. /*
  31. * "userpte=nohigh" disables allocation of user pagetables in
  32. * high memory.
  33. */
  34. if (strcmp(arg, "nohigh") == 0)
  35. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  36. else
  37. return -EINVAL;
  38. return 0;
  39. }
  40. early_param("userpte", setup_userpte);
  41. void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
  42. {
  43. pgtable_page_dtor(pte);
  44. paravirt_release_pte(page_to_pfn(pte));
  45. tlb_remove_page(tlb, pte);
  46. }
  47. #if PAGETABLE_LEVELS > 2
  48. void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
  49. {
  50. paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
  51. tlb_remove_page(tlb, virt_to_page(pmd));
  52. }
  53. #if PAGETABLE_LEVELS > 3
  54. void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
  55. {
  56. paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
  57. tlb_remove_page(tlb, virt_to_page(pud));
  58. }
  59. #endif /* PAGETABLE_LEVELS > 3 */
  60. #endif /* PAGETABLE_LEVELS > 2 */
  61. static inline void pgd_list_add(pgd_t *pgd)
  62. {
  63. struct page *page = virt_to_page(pgd);
  64. list_add(&page->lru, &pgd_list);
  65. }
  66. static inline void pgd_list_del(pgd_t *pgd)
  67. {
  68. struct page *page = virt_to_page(pgd);
  69. list_del(&page->lru);
  70. }
  71. #define UNSHARED_PTRS_PER_PGD \
  72. (SHARED_KERNEL_PMD ? KERNEL_PGD_BOUNDARY : PTRS_PER_PGD)
  73. static void pgd_set_mm(pgd_t *pgd, struct mm_struct *mm)
  74. {
  75. BUILD_BUG_ON(sizeof(virt_to_page(pgd)->index) < sizeof(mm));
  76. virt_to_page(pgd)->index = (pgoff_t)mm;
  77. }
  78. struct mm_struct *pgd_page_get_mm(struct page *page)
  79. {
  80. return (struct mm_struct *)page->index;
  81. }
  82. static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
  83. {
  84. /* If the pgd points to a shared pagetable level (either the
  85. ptes in non-PAE, or shared PMD in PAE), then just copy the
  86. references from swapper_pg_dir. */
  87. if (PAGETABLE_LEVELS == 2 ||
  88. (PAGETABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
  89. PAGETABLE_LEVELS == 4) {
  90. clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
  91. swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  92. KERNEL_PGD_PTRS);
  93. }
  94. /* list required to sync kernel mapping updates */
  95. if (!SHARED_KERNEL_PMD) {
  96. pgd_set_mm(pgd, mm);
  97. pgd_list_add(pgd);
  98. }
  99. }
  100. static void pgd_dtor(pgd_t *pgd)
  101. {
  102. if (SHARED_KERNEL_PMD)
  103. return;
  104. spin_lock(&pgd_lock);
  105. pgd_list_del(pgd);
  106. spin_unlock(&pgd_lock);
  107. }
  108. /*
  109. * List of all pgd's needed for non-PAE so it can invalidate entries
  110. * in both cached and uncached pgd's; not needed for PAE since the
  111. * kernel pmd is shared. If PAE were not to share the pmd a similar
  112. * tactic would be needed. This is essentially codepath-based locking
  113. * against pageattr.c; it is the unique case in which a valid change
  114. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  115. * vmalloc faults work because attached pagetables are never freed.
  116. * -- nyc
  117. */
  118. #ifdef CONFIG_X86_PAE
  119. /*
  120. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  121. * updating the top-level pagetable entries to guarantee the
  122. * processor notices the update. Since this is expensive, and
  123. * all 4 top-level entries are used almost immediately in a
  124. * new process's life, we just pre-populate them here.
  125. *
  126. * Also, if we're in a paravirt environment where the kernel pmd is
  127. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  128. * and initialize the kernel pmds here.
  129. */
  130. #define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
  131. void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
  132. {
  133. paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
  134. /* Note: almost everything apart from _PAGE_PRESENT is
  135. reserved at the pmd (PDPT) level. */
  136. set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
  137. /*
  138. * According to Intel App note "TLBs, Paging-Structure Caches,
  139. * and Their Invalidation", April 2007, document 317080-001,
  140. * section 8.1: in PAE mode we explicitly have to flush the
  141. * TLB via cr3 if the top-level pgd is changed...
  142. */
  143. flush_tlb_mm(mm);
  144. }
  145. #else /* !CONFIG_X86_PAE */
  146. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  147. #define PREALLOCATED_PMDS 0
  148. #endif /* CONFIG_X86_PAE */
  149. static void free_pmds(pmd_t *pmds[])
  150. {
  151. int i;
  152. for(i = 0; i < PREALLOCATED_PMDS; i++)
  153. if (pmds[i])
  154. free_page((unsigned long)pmds[i]);
  155. }
  156. static int preallocate_pmds(pmd_t *pmds[])
  157. {
  158. int i;
  159. bool failed = false;
  160. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  161. pmd_t *pmd = (pmd_t *)__get_free_page(PGALLOC_GFP);
  162. if (pmd == NULL)
  163. failed = true;
  164. pmds[i] = pmd;
  165. }
  166. if (failed) {
  167. free_pmds(pmds);
  168. return -ENOMEM;
  169. }
  170. return 0;
  171. }
  172. /*
  173. * Mop up any pmd pages which may still be attached to the pgd.
  174. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  175. * preallocate which never got a corresponding vma will need to be
  176. * freed manually.
  177. */
  178. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  179. {
  180. int i;
  181. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  182. pgd_t pgd = pgdp[i];
  183. if (pgd_val(pgd) != 0) {
  184. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  185. pgdp[i] = native_make_pgd(0);
  186. paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
  187. pmd_free(mm, pmd);
  188. }
  189. }
  190. }
  191. static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
  192. {
  193. pud_t *pud;
  194. unsigned long addr;
  195. int i;
  196. if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
  197. return;
  198. pud = pud_offset(pgd, 0);
  199. for (addr = i = 0; i < PREALLOCATED_PMDS;
  200. i++, pud++, addr += PUD_SIZE) {
  201. pmd_t *pmd = pmds[i];
  202. if (i >= KERNEL_PGD_BOUNDARY)
  203. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  204. sizeof(pmd_t) * PTRS_PER_PMD);
  205. pud_populate(mm, pud, pmd);
  206. }
  207. }
  208. pgd_t *pgd_alloc(struct mm_struct *mm)
  209. {
  210. pgd_t *pgd;
  211. pmd_t *pmds[PREALLOCATED_PMDS];
  212. pgd = (pgd_t *)__get_free_page(PGALLOC_GFP);
  213. if (pgd == NULL)
  214. goto out;
  215. mm->pgd = pgd;
  216. if (preallocate_pmds(pmds) != 0)
  217. goto out_free_pgd;
  218. if (paravirt_pgd_alloc(mm) != 0)
  219. goto out_free_pmds;
  220. /*
  221. * Make sure that pre-populating the pmds is atomic with
  222. * respect to anything walking the pgd_list, so that they
  223. * never see a partially populated pgd.
  224. */
  225. spin_lock(&pgd_lock);
  226. pgd_ctor(mm, pgd);
  227. pgd_prepopulate_pmd(mm, pgd, pmds);
  228. spin_unlock(&pgd_lock);
  229. return pgd;
  230. out_free_pmds:
  231. free_pmds(pmds);
  232. out_free_pgd:
  233. free_page((unsigned long)pgd);
  234. out:
  235. return NULL;
  236. }
  237. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  238. {
  239. pgd_mop_up_pmds(mm, pgd);
  240. pgd_dtor(pgd);
  241. paravirt_pgd_free(mm, pgd);
  242. free_page((unsigned long)pgd);
  243. }
  244. /*
  245. * Used to set accessed or dirty bits in the page table entries
  246. * on other architectures. On x86, the accessed and dirty bits
  247. * are tracked by hardware. However, do_wp_page calls this function
  248. * to also make the pte writeable at the same time the dirty bit is
  249. * set. In that case we do actually need to write the PTE.
  250. */
  251. int ptep_set_access_flags(struct vm_area_struct *vma,
  252. unsigned long address, pte_t *ptep,
  253. pte_t entry, int dirty)
  254. {
  255. int changed = !pte_same(*ptep, entry);
  256. if (changed && dirty) {
  257. *ptep = entry;
  258. pte_update_defer(vma->vm_mm, address, ptep);
  259. }
  260. return changed;
  261. }
  262. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  263. int pmdp_set_access_flags(struct vm_area_struct *vma,
  264. unsigned long address, pmd_t *pmdp,
  265. pmd_t entry, int dirty)
  266. {
  267. int changed = !pmd_same(*pmdp, entry);
  268. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  269. if (changed && dirty) {
  270. *pmdp = entry;
  271. pmd_update_defer(vma->vm_mm, address, pmdp);
  272. /*
  273. * We had a write-protection fault here and changed the pmd
  274. * to to more permissive. No need to flush the TLB for that,
  275. * #PF is architecturally guaranteed to do that and in the
  276. * worst-case we'll generate a spurious fault.
  277. */
  278. }
  279. return changed;
  280. }
  281. #endif
  282. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  283. unsigned long addr, pte_t *ptep)
  284. {
  285. int ret = 0;
  286. if (pte_young(*ptep))
  287. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  288. (unsigned long *) &ptep->pte);
  289. if (ret)
  290. pte_update(vma->vm_mm, addr, ptep);
  291. return ret;
  292. }
  293. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  294. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  295. unsigned long addr, pmd_t *pmdp)
  296. {
  297. int ret = 0;
  298. if (pmd_young(*pmdp))
  299. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  300. (unsigned long *)pmdp);
  301. if (ret)
  302. pmd_update(vma->vm_mm, addr, pmdp);
  303. return ret;
  304. }
  305. #endif
  306. int ptep_clear_flush_young(struct vm_area_struct *vma,
  307. unsigned long address, pte_t *ptep)
  308. {
  309. int young;
  310. young = ptep_test_and_clear_young(vma, address, ptep);
  311. if (young)
  312. flush_tlb_page(vma, address);
  313. return young;
  314. }
  315. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  316. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  317. unsigned long address, pmd_t *pmdp)
  318. {
  319. int young;
  320. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  321. young = pmdp_test_and_clear_young(vma, address, pmdp);
  322. if (young)
  323. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  324. return young;
  325. }
  326. void pmdp_splitting_flush(struct vm_area_struct *vma,
  327. unsigned long address, pmd_t *pmdp)
  328. {
  329. int set;
  330. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  331. set = !test_and_set_bit(_PAGE_BIT_SPLITTING,
  332. (unsigned long *)pmdp);
  333. if (set) {
  334. pmd_update(vma->vm_mm, address, pmdp);
  335. /* need tlb flush only to serialize against gup-fast */
  336. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  337. }
  338. }
  339. #endif
  340. /**
  341. * reserve_top_address - reserves a hole in the top of kernel address space
  342. * @reserve - size of hole to reserve
  343. *
  344. * Can be used to relocate the fixmap area and poke a hole in the top
  345. * of kernel address space to make room for a hypervisor.
  346. */
  347. void __init reserve_top_address(unsigned long reserve)
  348. {
  349. #ifdef CONFIG_X86_32
  350. BUG_ON(fixmaps_set > 0);
  351. printk(KERN_INFO "Reserving virtual address space above 0x%08x\n",
  352. (int)-reserve);
  353. __FIXADDR_TOP = -reserve - PAGE_SIZE;
  354. #endif
  355. }
  356. int fixmaps_set;
  357. void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
  358. {
  359. unsigned long address = __fix_to_virt(idx);
  360. if (idx >= __end_of_fixed_addresses) {
  361. BUG();
  362. return;
  363. }
  364. set_pte_vaddr(address, pte);
  365. fixmaps_set++;
  366. }
  367. void native_set_fixmap(enum fixed_addresses idx, phys_addr_t phys,
  368. pgprot_t flags)
  369. {
  370. __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
  371. }