pgtable.c 16 KB

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