pgtable.c 16 KB

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