pgtable.c 8.5 KB

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