pgtable.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include <linux/mm.h>
  2. #include <linux/gfp.h>
  3. #include <linux/hugetlb.h>
  4. #include <asm/pgalloc.h>
  5. #include <asm/pgtable.h>
  6. #include <asm/tlb.h>
  7. #include <asm/fixmap.h>
  8. #include <asm/mtrr.h>
  9. #define PGALLOC_GFP (GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __GFP_ZERO)
  10. #ifdef CONFIG_HIGHPTE
  11. #define PGALLOC_USER_GFP __GFP_HIGHMEM
  12. #else
  13. #define PGALLOC_USER_GFP 0
  14. #endif
  15. gfp_t __userpte_alloc_gfp = PGALLOC_GFP | PGALLOC_USER_GFP;
  16. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  17. {
  18. return (pte_t *)__get_free_page(PGALLOC_GFP);
  19. }
  20. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  21. {
  22. struct page *pte;
  23. pte = alloc_pages(__userpte_alloc_gfp, 0);
  24. if (!pte)
  25. return NULL;
  26. if (!pgtable_page_ctor(pte)) {
  27. __free_page(pte);
  28. return NULL;
  29. }
  30. return pte;
  31. }
  32. static int __init setup_userpte(char *arg)
  33. {
  34. if (!arg)
  35. return -EINVAL;
  36. /*
  37. * "userpte=nohigh" disables allocation of user pagetables in
  38. * high memory.
  39. */
  40. if (strcmp(arg, "nohigh") == 0)
  41. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  42. else
  43. return -EINVAL;
  44. return 0;
  45. }
  46. early_param("userpte", setup_userpte);
  47. void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
  48. {
  49. pgtable_page_dtor(pte);
  50. paravirt_release_pte(page_to_pfn(pte));
  51. tlb_remove_page(tlb, pte);
  52. }
  53. #if CONFIG_PGTABLE_LEVELS > 2
  54. void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
  55. {
  56. struct page *page = virt_to_page(pmd);
  57. paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
  58. /*
  59. * NOTE! For PAE, any changes to the top page-directory-pointer-table
  60. * entries need a full cr3 reload to flush.
  61. */
  62. #ifdef CONFIG_X86_PAE
  63. tlb->need_flush_all = 1;
  64. #endif
  65. pgtable_pmd_page_dtor(page);
  66. tlb_remove_page(tlb, page);
  67. }
  68. #if CONFIG_PGTABLE_LEVELS > 3
  69. void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
  70. {
  71. paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
  72. tlb_remove_page(tlb, virt_to_page(pud));
  73. }
  74. #endif /* CONFIG_PGTABLE_LEVELS > 3 */
  75. #endif /* CONFIG_PGTABLE_LEVELS > 2 */
  76. static inline void pgd_list_add(pgd_t *pgd)
  77. {
  78. struct page *page = virt_to_page(pgd);
  79. list_add(&page->lru, &pgd_list);
  80. }
  81. static inline void pgd_list_del(pgd_t *pgd)
  82. {
  83. struct page *page = virt_to_page(pgd);
  84. list_del(&page->lru);
  85. }
  86. #define UNSHARED_PTRS_PER_PGD \
  87. (SHARED_KERNEL_PMD ? KERNEL_PGD_BOUNDARY : PTRS_PER_PGD)
  88. static void pgd_set_mm(pgd_t *pgd, struct mm_struct *mm)
  89. {
  90. BUILD_BUG_ON(sizeof(virt_to_page(pgd)->index) < sizeof(mm));
  91. virt_to_page(pgd)->index = (pgoff_t)mm;
  92. }
  93. struct mm_struct *pgd_page_get_mm(struct page *page)
  94. {
  95. return (struct mm_struct *)page->index;
  96. }
  97. static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
  98. {
  99. /* If the pgd points to a shared pagetable level (either the
  100. ptes in non-PAE, or shared PMD in PAE), then just copy the
  101. references from swapper_pg_dir. */
  102. if (CONFIG_PGTABLE_LEVELS == 2 ||
  103. (CONFIG_PGTABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
  104. CONFIG_PGTABLE_LEVELS == 4) {
  105. clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
  106. swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  107. KERNEL_PGD_PTRS);
  108. }
  109. /* list required to sync kernel mapping updates */
  110. if (!SHARED_KERNEL_PMD) {
  111. pgd_set_mm(pgd, mm);
  112. pgd_list_add(pgd);
  113. }
  114. }
  115. static void pgd_dtor(pgd_t *pgd)
  116. {
  117. if (SHARED_KERNEL_PMD)
  118. return;
  119. spin_lock(&pgd_lock);
  120. pgd_list_del(pgd);
  121. spin_unlock(&pgd_lock);
  122. }
  123. /*
  124. * List of all pgd's needed for non-PAE so it can invalidate entries
  125. * in both cached and uncached pgd's; not needed for PAE since the
  126. * kernel pmd is shared. If PAE were not to share the pmd a similar
  127. * tactic would be needed. This is essentially codepath-based locking
  128. * against pageattr.c; it is the unique case in which a valid change
  129. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  130. * vmalloc faults work because attached pagetables are never freed.
  131. * -- nyc
  132. */
  133. #ifdef CONFIG_X86_PAE
  134. /*
  135. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  136. * updating the top-level pagetable entries to guarantee the
  137. * processor notices the update. Since this is expensive, and
  138. * all 4 top-level entries are used almost immediately in a
  139. * new process's life, we just pre-populate them here.
  140. *
  141. * Also, if we're in a paravirt environment where the kernel pmd is
  142. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  143. * and initialize the kernel pmds here.
  144. */
  145. #define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
  146. void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
  147. {
  148. paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
  149. /* Note: almost everything apart from _PAGE_PRESENT is
  150. reserved at the pmd (PDPT) level. */
  151. set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
  152. /*
  153. * According to Intel App note "TLBs, Paging-Structure Caches,
  154. * and Their Invalidation", April 2007, document 317080-001,
  155. * section 8.1: in PAE mode we explicitly have to flush the
  156. * TLB via cr3 if the top-level pgd is changed...
  157. */
  158. flush_tlb_mm(mm);
  159. }
  160. #else /* !CONFIG_X86_PAE */
  161. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  162. #define PREALLOCATED_PMDS 0
  163. #endif /* CONFIG_X86_PAE */
  164. static void free_pmds(struct mm_struct *mm, pmd_t *pmds[])
  165. {
  166. int i;
  167. for(i = 0; i < PREALLOCATED_PMDS; i++)
  168. if (pmds[i]) {
  169. pgtable_pmd_page_dtor(virt_to_page(pmds[i]));
  170. free_page((unsigned long)pmds[i]);
  171. mm_dec_nr_pmds(mm);
  172. }
  173. }
  174. static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[])
  175. {
  176. int i;
  177. bool failed = false;
  178. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  179. pmd_t *pmd = (pmd_t *)__get_free_page(PGALLOC_GFP);
  180. if (!pmd)
  181. failed = true;
  182. if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
  183. free_page((unsigned long)pmd);
  184. pmd = NULL;
  185. failed = true;
  186. }
  187. if (pmd)
  188. mm_inc_nr_pmds(mm);
  189. pmds[i] = pmd;
  190. }
  191. if (failed) {
  192. free_pmds(mm, pmds);
  193. return -ENOMEM;
  194. }
  195. return 0;
  196. }
  197. /*
  198. * Mop up any pmd pages which may still be attached to the pgd.
  199. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  200. * preallocate which never got a corresponding vma will need to be
  201. * freed manually.
  202. */
  203. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  204. {
  205. int i;
  206. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  207. pgd_t pgd = pgdp[i];
  208. if (pgd_val(pgd) != 0) {
  209. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  210. pgdp[i] = native_make_pgd(0);
  211. paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
  212. pmd_free(mm, pmd);
  213. mm_dec_nr_pmds(mm);
  214. }
  215. }
  216. }
  217. static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
  218. {
  219. pud_t *pud;
  220. int i;
  221. if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
  222. return;
  223. pud = pud_offset(pgd, 0);
  224. for (i = 0; i < PREALLOCATED_PMDS; i++, pud++) {
  225. pmd_t *pmd = pmds[i];
  226. if (i >= KERNEL_PGD_BOUNDARY)
  227. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  228. sizeof(pmd_t) * PTRS_PER_PMD);
  229. pud_populate(mm, pud, pmd);
  230. }
  231. }
  232. /*
  233. * Xen paravirt assumes pgd table should be in one page. 64 bit kernel also
  234. * assumes that pgd should be in one page.
  235. *
  236. * But kernel with PAE paging that is not running as a Xen domain
  237. * only needs to allocate 32 bytes for pgd instead of one page.
  238. */
  239. #ifdef CONFIG_X86_PAE
  240. #include <linux/slab.h>
  241. #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
  242. #define PGD_ALIGN 32
  243. static struct kmem_cache *pgd_cache;
  244. static int __init pgd_cache_init(void)
  245. {
  246. /*
  247. * When PAE kernel is running as a Xen domain, it does not use
  248. * shared kernel pmd. And this requires a whole page for pgd.
  249. */
  250. if (!SHARED_KERNEL_PMD)
  251. return 0;
  252. /*
  253. * when PAE kernel is not running as a Xen domain, it uses
  254. * shared kernel pmd. Shared kernel pmd does not require a whole
  255. * page for pgd. We are able to just allocate a 32-byte for pgd.
  256. * During boot time, we create a 32-byte slab for pgd table allocation.
  257. */
  258. pgd_cache = kmem_cache_create("pgd_cache", PGD_SIZE, PGD_ALIGN,
  259. SLAB_PANIC, NULL);
  260. if (!pgd_cache)
  261. return -ENOMEM;
  262. return 0;
  263. }
  264. core_initcall(pgd_cache_init);
  265. static inline pgd_t *_pgd_alloc(void)
  266. {
  267. /*
  268. * If no SHARED_KERNEL_PMD, PAE kernel is running as a Xen domain.
  269. * We allocate one page for pgd.
  270. */
  271. if (!SHARED_KERNEL_PMD)
  272. return (pgd_t *)__get_free_page(PGALLOC_GFP);
  273. /*
  274. * Now PAE kernel is not running as a Xen domain. We can allocate
  275. * a 32-byte slab for pgd to save memory space.
  276. */
  277. return kmem_cache_alloc(pgd_cache, PGALLOC_GFP);
  278. }
  279. static inline void _pgd_free(pgd_t *pgd)
  280. {
  281. if (!SHARED_KERNEL_PMD)
  282. free_page((unsigned long)pgd);
  283. else
  284. kmem_cache_free(pgd_cache, pgd);
  285. }
  286. #else
  287. /*
  288. * Instead of one pgd, Kaiser acquires two pgds. Being order-1, it is
  289. * both 8k in size and 8k-aligned. That lets us just flip bit 12
  290. * in a pointer to swap between the two 4k halves.
  291. */
  292. #define PGD_ALLOCATION_ORDER kaiser_enabled
  293. static inline pgd_t *_pgd_alloc(void)
  294. {
  295. /* No __GFP_REPEAT: to avoid page allocation stalls in order-1 case */
  296. return (pgd_t *)__get_free_pages(PGALLOC_GFP & ~__GFP_REPEAT,
  297. PGD_ALLOCATION_ORDER);
  298. }
  299. static inline void _pgd_free(pgd_t *pgd)
  300. {
  301. free_pages((unsigned long)pgd, PGD_ALLOCATION_ORDER);
  302. }
  303. #endif /* CONFIG_X86_PAE */
  304. pgd_t *pgd_alloc(struct mm_struct *mm)
  305. {
  306. pgd_t *pgd;
  307. pmd_t *pmds[PREALLOCATED_PMDS];
  308. pgd = _pgd_alloc();
  309. if (pgd == NULL)
  310. goto out;
  311. mm->pgd = pgd;
  312. if (preallocate_pmds(mm, pmds) != 0)
  313. goto out_free_pgd;
  314. if (paravirt_pgd_alloc(mm) != 0)
  315. goto out_free_pmds;
  316. /*
  317. * Make sure that pre-populating the pmds is atomic with
  318. * respect to anything walking the pgd_list, so that they
  319. * never see a partially populated pgd.
  320. */
  321. spin_lock(&pgd_lock);
  322. pgd_ctor(mm, pgd);
  323. pgd_prepopulate_pmd(mm, pgd, pmds);
  324. spin_unlock(&pgd_lock);
  325. return pgd;
  326. out_free_pmds:
  327. free_pmds(mm, pmds);
  328. out_free_pgd:
  329. _pgd_free(pgd);
  330. out:
  331. return NULL;
  332. }
  333. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  334. {
  335. pgd_mop_up_pmds(mm, pgd);
  336. pgd_dtor(pgd);
  337. paravirt_pgd_free(mm, pgd);
  338. _pgd_free(pgd);
  339. }
  340. /*
  341. * Used to set accessed or dirty bits in the page table entries
  342. * on other architectures. On x86, the accessed and dirty bits
  343. * are tracked by hardware. However, do_wp_page calls this function
  344. * to also make the pte writeable at the same time the dirty bit is
  345. * set. In that case we do actually need to write the PTE.
  346. */
  347. int ptep_set_access_flags(struct vm_area_struct *vma,
  348. unsigned long address, pte_t *ptep,
  349. pte_t entry, int dirty)
  350. {
  351. int changed = !pte_same(*ptep, entry);
  352. if (changed && dirty) {
  353. *ptep = entry;
  354. pte_update_defer(vma->vm_mm, address, ptep);
  355. }
  356. return changed;
  357. }
  358. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  359. int pmdp_set_access_flags(struct vm_area_struct *vma,
  360. unsigned long address, pmd_t *pmdp,
  361. pmd_t entry, int dirty)
  362. {
  363. int changed = !pmd_same(*pmdp, entry);
  364. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  365. if (changed && dirty) {
  366. *pmdp = entry;
  367. pmd_update_defer(vma->vm_mm, address, pmdp);
  368. /*
  369. * We had a write-protection fault here and changed the pmd
  370. * to to more permissive. No need to flush the TLB for that,
  371. * #PF is architecturally guaranteed to do that and in the
  372. * worst-case we'll generate a spurious fault.
  373. */
  374. }
  375. return changed;
  376. }
  377. #endif
  378. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  379. unsigned long addr, pte_t *ptep)
  380. {
  381. int ret = 0;
  382. if (pte_young(*ptep))
  383. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  384. (unsigned long *) &ptep->pte);
  385. if (ret)
  386. pte_update(vma->vm_mm, addr, ptep);
  387. return ret;
  388. }
  389. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  390. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  391. unsigned long addr, pmd_t *pmdp)
  392. {
  393. int ret = 0;
  394. if (pmd_young(*pmdp))
  395. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  396. (unsigned long *)pmdp);
  397. if (ret)
  398. pmd_update(vma->vm_mm, addr, pmdp);
  399. return ret;
  400. }
  401. #endif
  402. int ptep_clear_flush_young(struct vm_area_struct *vma,
  403. unsigned long address, pte_t *ptep)
  404. {
  405. /*
  406. * On x86 CPUs, clearing the accessed bit without a TLB flush
  407. * doesn't cause data corruption. [ It could cause incorrect
  408. * page aging and the (mistaken) reclaim of hot pages, but the
  409. * chance of that should be relatively low. ]
  410. *
  411. * So as a performance optimization don't flush the TLB when
  412. * clearing the accessed bit, it will eventually be flushed by
  413. * a context switch or a VM operation anyway. [ In the rare
  414. * event of it not getting flushed for a long time the delay
  415. * shouldn't really matter because there's no real memory
  416. * pressure for swapout to react to. ]
  417. */
  418. return ptep_test_and_clear_young(vma, address, ptep);
  419. }
  420. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  421. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  422. unsigned long address, pmd_t *pmdp)
  423. {
  424. int young;
  425. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  426. young = pmdp_test_and_clear_young(vma, address, pmdp);
  427. if (young)
  428. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  429. return young;
  430. }
  431. void pmdp_splitting_flush(struct vm_area_struct *vma,
  432. unsigned long address, pmd_t *pmdp)
  433. {
  434. int set;
  435. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  436. set = !test_and_set_bit(_PAGE_BIT_SPLITTING,
  437. (unsigned long *)pmdp);
  438. if (set) {
  439. pmd_update(vma->vm_mm, address, pmdp);
  440. /* need tlb flush only to serialize against gup-fast */
  441. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  442. }
  443. }
  444. #endif
  445. /**
  446. * reserve_top_address - reserves a hole in the top of kernel address space
  447. * @reserve - size of hole to reserve
  448. *
  449. * Can be used to relocate the fixmap area and poke a hole in the top
  450. * of kernel address space to make room for a hypervisor.
  451. */
  452. void __init reserve_top_address(unsigned long reserve)
  453. {
  454. #ifdef CONFIG_X86_32
  455. BUG_ON(fixmaps_set > 0);
  456. __FIXADDR_TOP = round_down(-reserve, 1 << PMD_SHIFT) - PAGE_SIZE;
  457. printk(KERN_INFO "Reserving virtual address space above 0x%08lx (rounded to 0x%08lx)\n",
  458. -reserve, __FIXADDR_TOP + PAGE_SIZE);
  459. #endif
  460. }
  461. int fixmaps_set;
  462. void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
  463. {
  464. unsigned long address = __fix_to_virt(idx);
  465. if (idx >= __end_of_fixed_addresses) {
  466. BUG();
  467. return;
  468. }
  469. set_pte_vaddr(address, pte);
  470. fixmaps_set++;
  471. }
  472. void native_set_fixmap(enum fixed_addresses idx, phys_addr_t phys,
  473. pgprot_t flags)
  474. {
  475. __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
  476. }
  477. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  478. /**
  479. * pud_set_huge - setup kernel PUD mapping
  480. *
  481. * MTRRs can override PAT memory types with 4KiB granularity. Therefore, this
  482. * function sets up a huge page only if any of the following conditions are met:
  483. *
  484. * - MTRRs are disabled, or
  485. *
  486. * - MTRRs are enabled and the range is completely covered by a single MTRR, or
  487. *
  488. * - MTRRs are enabled and the corresponding MTRR memory type is WB, which
  489. * has no effect on the requested PAT memory type.
  490. *
  491. * Callers should try to decrease page size (1GB -> 2MB -> 4K) if the bigger
  492. * page mapping attempt fails.
  493. *
  494. * Returns 1 on success and 0 on failure.
  495. */
  496. int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
  497. {
  498. u8 mtrr, uniform;
  499. mtrr = mtrr_type_lookup(addr, addr + PUD_SIZE, &uniform);
  500. if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
  501. (mtrr != MTRR_TYPE_WRBACK))
  502. return 0;
  503. /* Bail out if we are we on a populated non-leaf entry: */
  504. if (pud_present(*pud) && !pud_huge(*pud))
  505. return 0;
  506. prot = pgprot_4k_2_large(prot);
  507. set_pte((pte_t *)pud, pfn_pte(
  508. (u64)addr >> PAGE_SHIFT,
  509. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  510. return 1;
  511. }
  512. /**
  513. * pmd_set_huge - setup kernel PMD mapping
  514. *
  515. * See text over pud_set_huge() above.
  516. *
  517. * Returns 1 on success and 0 on failure.
  518. */
  519. int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
  520. {
  521. u8 mtrr, uniform;
  522. mtrr = mtrr_type_lookup(addr, addr + PMD_SIZE, &uniform);
  523. if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
  524. (mtrr != MTRR_TYPE_WRBACK)) {
  525. pr_warn_once("%s: Cannot satisfy [mem %#010llx-%#010llx] with a huge-page mapping due to MTRR override.\n",
  526. __func__, addr, addr + PMD_SIZE);
  527. return 0;
  528. }
  529. /* Bail out if we are we on a populated non-leaf entry: */
  530. if (pmd_present(*pmd) && !pmd_huge(*pmd))
  531. return 0;
  532. prot = pgprot_4k_2_large(prot);
  533. set_pte((pte_t *)pmd, pfn_pte(
  534. (u64)addr >> PAGE_SHIFT,
  535. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  536. return 1;
  537. }
  538. /**
  539. * pud_clear_huge - clear kernel PUD mapping when it is set
  540. *
  541. * Returns 1 on success and 0 on failure (no PUD map is found).
  542. */
  543. int pud_clear_huge(pud_t *pud)
  544. {
  545. if (pud_large(*pud)) {
  546. pud_clear(pud);
  547. return 1;
  548. }
  549. return 0;
  550. }
  551. /**
  552. * pmd_clear_huge - clear kernel PMD mapping when it is set
  553. *
  554. * Returns 1 on success and 0 on failure (no PMD map is found).
  555. */
  556. int pmd_clear_huge(pmd_t *pmd)
  557. {
  558. if (pmd_large(*pmd)) {
  559. pmd_clear(pmd);
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. #ifdef CONFIG_X86_64
  565. /**
  566. * pud_free_pmd_page - Clear pud entry and free pmd page.
  567. * @pud: Pointer to a PUD.
  568. * @addr: Virtual address associated with pud.
  569. *
  570. * Context: The pud range has been unmapped and TLB purged.
  571. * Return: 1 if clearing the entry succeeded. 0 otherwise.
  572. *
  573. * NOTE: Callers must allow a single page allocation.
  574. */
  575. int pud_free_pmd_page(pud_t *pud, unsigned long addr)
  576. {
  577. pmd_t *pmd, *pmd_sv;
  578. pte_t *pte;
  579. int i;
  580. if (pud_none(*pud))
  581. return 1;
  582. pmd = (pmd_t *)pud_page_vaddr(*pud);
  583. pmd_sv = (pmd_t *)__get_free_page(GFP_KERNEL);
  584. if (!pmd_sv)
  585. return 0;
  586. for (i = 0; i < PTRS_PER_PMD; i++) {
  587. pmd_sv[i] = pmd[i];
  588. if (!pmd_none(pmd[i]))
  589. pmd_clear(&pmd[i]);
  590. }
  591. pud_clear(pud);
  592. /* INVLPG to clear all paging-structure caches */
  593. flush_tlb_kernel_range(addr, addr + PAGE_SIZE-1);
  594. for (i = 0; i < PTRS_PER_PMD; i++) {
  595. if (!pmd_none(pmd_sv[i])) {
  596. pte = (pte_t *)pmd_page_vaddr(pmd_sv[i]);
  597. free_page((unsigned long)pte);
  598. }
  599. }
  600. free_page((unsigned long)pmd_sv);
  601. free_page((unsigned long)pmd);
  602. return 1;
  603. }
  604. /**
  605. * pmd_free_pte_page - Clear pmd entry and free pte page.
  606. * @pmd: Pointer to a PMD.
  607. * @addr: Virtual address associated with pmd.
  608. *
  609. * Context: The pmd range has been unmapped and TLB purged.
  610. * Return: 1 if clearing the entry succeeded. 0 otherwise.
  611. */
  612. int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  613. {
  614. pte_t *pte;
  615. if (pmd_none(*pmd))
  616. return 1;
  617. pte = (pte_t *)pmd_page_vaddr(*pmd);
  618. pmd_clear(pmd);
  619. /* INVLPG to clear all paging-structure caches */
  620. flush_tlb_kernel_range(addr, addr + PAGE_SIZE-1);
  621. free_page((unsigned long)pte);
  622. return 1;
  623. }
  624. #else /* !CONFIG_X86_64 */
  625. int pud_free_pmd_page(pud_t *pud, unsigned long addr)
  626. {
  627. return pud_none(*pud);
  628. }
  629. /*
  630. * Disable free page handling on x86-PAE. This assures that ioremap()
  631. * does not update sync'd pmd entries. See vmalloc_sync_one().
  632. */
  633. int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  634. {
  635. return pmd_none(*pmd);
  636. }
  637. #endif /* CONFIG_X86_64 */
  638. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */