pageattr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * Thanks to Ben LaHaise for precious feedback.
  4. */
  5. #include <linux/highmem.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/debugfs.h>
  14. #include <asm/e820.h>
  15. #include <asm/processor.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/sections.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/proto.h>
  21. #include <asm/pat.h>
  22. /*
  23. * The current flushing context - we pass it instead of 5 arguments:
  24. */
  25. struct cpa_data {
  26. unsigned long *vaddr;
  27. pgprot_t mask_set;
  28. pgprot_t mask_clr;
  29. int numpages;
  30. int flags;
  31. unsigned long pfn;
  32. unsigned force_split : 1;
  33. int curpage;
  34. };
  35. /*
  36. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  37. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  38. * entries change the page attribute in parallel to some other cpu
  39. * splitting a large page entry along with changing the attribute.
  40. */
  41. static DEFINE_SPINLOCK(cpa_lock);
  42. #define CPA_FLUSHTLB 1
  43. #define CPA_ARRAY 2
  44. #ifdef CONFIG_PROC_FS
  45. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  46. void update_page_count(int level, unsigned long pages)
  47. {
  48. unsigned long flags;
  49. /* Protect against CPA */
  50. spin_lock_irqsave(&pgd_lock, flags);
  51. direct_pages_count[level] += pages;
  52. spin_unlock_irqrestore(&pgd_lock, flags);
  53. }
  54. static void split_page_count(int level)
  55. {
  56. direct_pages_count[level]--;
  57. direct_pages_count[level - 1] += PTRS_PER_PTE;
  58. }
  59. void arch_report_meminfo(struct seq_file *m)
  60. {
  61. seq_printf(m, "DirectMap4k: %8lu kB\n",
  62. direct_pages_count[PG_LEVEL_4K] << 2);
  63. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  64. seq_printf(m, "DirectMap2M: %8lu kB\n",
  65. direct_pages_count[PG_LEVEL_2M] << 11);
  66. #else
  67. seq_printf(m, "DirectMap4M: %8lu kB\n",
  68. direct_pages_count[PG_LEVEL_2M] << 12);
  69. #endif
  70. #ifdef CONFIG_X86_64
  71. if (direct_gbpages)
  72. seq_printf(m, "DirectMap1G: %8lu kB\n",
  73. direct_pages_count[PG_LEVEL_1G] << 20);
  74. #endif
  75. }
  76. #else
  77. static inline void split_page_count(int level) { }
  78. #endif
  79. #ifdef CONFIG_X86_64
  80. static inline unsigned long highmap_start_pfn(void)
  81. {
  82. return __pa(_text) >> PAGE_SHIFT;
  83. }
  84. static inline unsigned long highmap_end_pfn(void)
  85. {
  86. return __pa(roundup((unsigned long)_end, PMD_SIZE)) >> PAGE_SHIFT;
  87. }
  88. #endif
  89. #ifdef CONFIG_DEBUG_PAGEALLOC
  90. # define debug_pagealloc 1
  91. #else
  92. # define debug_pagealloc 0
  93. #endif
  94. static inline int
  95. within(unsigned long addr, unsigned long start, unsigned long end)
  96. {
  97. return addr >= start && addr < end;
  98. }
  99. /*
  100. * Flushing functions
  101. */
  102. /**
  103. * clflush_cache_range - flush a cache range with clflush
  104. * @addr: virtual start address
  105. * @size: number of bytes to flush
  106. *
  107. * clflush is an unordered instruction which needs fencing with mfence
  108. * to avoid ordering issues.
  109. */
  110. void clflush_cache_range(void *vaddr, unsigned int size)
  111. {
  112. void *vend = vaddr + size - 1;
  113. mb();
  114. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  115. clflush(vaddr);
  116. /*
  117. * Flush any possible final partial cacheline:
  118. */
  119. clflush(vend);
  120. mb();
  121. }
  122. static void __cpa_flush_all(void *arg)
  123. {
  124. unsigned long cache = (unsigned long)arg;
  125. /*
  126. * Flush all to work around Errata in early athlons regarding
  127. * large page flushing.
  128. */
  129. __flush_tlb_all();
  130. if (cache && boot_cpu_data.x86_model >= 4)
  131. wbinvd();
  132. }
  133. static void cpa_flush_all(unsigned long cache)
  134. {
  135. BUG_ON(irqs_disabled());
  136. on_each_cpu(__cpa_flush_all, (void *) cache, 1);
  137. }
  138. static void __cpa_flush_range(void *arg)
  139. {
  140. /*
  141. * We could optimize that further and do individual per page
  142. * tlb invalidates for a low number of pages. Caveat: we must
  143. * flush the high aliases on 64bit as well.
  144. */
  145. __flush_tlb_all();
  146. }
  147. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  148. {
  149. unsigned int i, level;
  150. unsigned long addr;
  151. BUG_ON(irqs_disabled());
  152. WARN_ON(PAGE_ALIGN(start) != start);
  153. on_each_cpu(__cpa_flush_range, NULL, 1);
  154. if (!cache)
  155. return;
  156. /*
  157. * We only need to flush on one CPU,
  158. * clflush is a MESI-coherent instruction that
  159. * will cause all other CPUs to flush the same
  160. * cachelines:
  161. */
  162. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  163. pte_t *pte = lookup_address(addr, &level);
  164. /*
  165. * Only flush present addresses:
  166. */
  167. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  168. clflush_cache_range((void *) addr, PAGE_SIZE);
  169. }
  170. }
  171. static void cpa_flush_array(unsigned long *start, int numpages, int cache)
  172. {
  173. unsigned int i, level;
  174. unsigned long *addr;
  175. BUG_ON(irqs_disabled());
  176. on_each_cpu(__cpa_flush_range, NULL, 1);
  177. if (!cache)
  178. return;
  179. /* 4M threshold */
  180. if (numpages >= 1024) {
  181. if (boot_cpu_data.x86_model >= 4)
  182. wbinvd();
  183. return;
  184. }
  185. /*
  186. * We only need to flush on one CPU,
  187. * clflush is a MESI-coherent instruction that
  188. * will cause all other CPUs to flush the same
  189. * cachelines:
  190. */
  191. for (i = 0, addr = start; i < numpages; i++, addr++) {
  192. pte_t *pte = lookup_address(*addr, &level);
  193. /*
  194. * Only flush present addresses:
  195. */
  196. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  197. clflush_cache_range((void *) *addr, PAGE_SIZE);
  198. }
  199. }
  200. /*
  201. * Certain areas of memory on x86 require very specific protection flags,
  202. * for example the BIOS area or kernel text. Callers don't always get this
  203. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  204. * checks and fixes these known static required protection bits.
  205. */
  206. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  207. unsigned long pfn)
  208. {
  209. pgprot_t forbidden = __pgprot(0);
  210. /*
  211. * The BIOS area between 640k and 1Mb needs to be executable for
  212. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  213. */
  214. if (within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  215. pgprot_val(forbidden) |= _PAGE_NX;
  216. /*
  217. * The kernel text needs to be executable for obvious reasons
  218. * Does not cover __inittext since that is gone later on. On
  219. * 64bit we do not enforce !NX on the low mapping
  220. */
  221. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  222. pgprot_val(forbidden) |= _PAGE_NX;
  223. /*
  224. * The .rodata section needs to be read-only. Using the pfn
  225. * catches all aliases.
  226. */
  227. if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
  228. __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
  229. pgprot_val(forbidden) |= _PAGE_RW;
  230. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  231. return prot;
  232. }
  233. /*
  234. * Lookup the page table entry for a virtual address. Return a pointer
  235. * to the entry and the level of the mapping.
  236. *
  237. * Note: We return pud and pmd either when the entry is marked large
  238. * or when the present bit is not set. Otherwise we would return a
  239. * pointer to a nonexisting mapping.
  240. */
  241. pte_t *lookup_address(unsigned long address, unsigned int *level)
  242. {
  243. pgd_t *pgd = pgd_offset_k(address);
  244. pud_t *pud;
  245. pmd_t *pmd;
  246. *level = PG_LEVEL_NONE;
  247. if (pgd_none(*pgd))
  248. return NULL;
  249. pud = pud_offset(pgd, address);
  250. if (pud_none(*pud))
  251. return NULL;
  252. *level = PG_LEVEL_1G;
  253. if (pud_large(*pud) || !pud_present(*pud))
  254. return (pte_t *)pud;
  255. pmd = pmd_offset(pud, address);
  256. if (pmd_none(*pmd))
  257. return NULL;
  258. *level = PG_LEVEL_2M;
  259. if (pmd_large(*pmd) || !pmd_present(*pmd))
  260. return (pte_t *)pmd;
  261. *level = PG_LEVEL_4K;
  262. return pte_offset_kernel(pmd, address);
  263. }
  264. EXPORT_SYMBOL_GPL(lookup_address);
  265. /*
  266. * Set the new pmd in all the pgds we know about:
  267. */
  268. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  269. {
  270. /* change init_mm */
  271. set_pte_atomic(kpte, pte);
  272. #ifdef CONFIG_X86_32
  273. if (!SHARED_KERNEL_PMD) {
  274. struct page *page;
  275. list_for_each_entry(page, &pgd_list, lru) {
  276. pgd_t *pgd;
  277. pud_t *pud;
  278. pmd_t *pmd;
  279. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  280. pud = pud_offset(pgd, address);
  281. pmd = pmd_offset(pud, address);
  282. set_pte_atomic((pte_t *)pmd, pte);
  283. }
  284. }
  285. #endif
  286. }
  287. static int
  288. try_preserve_large_page(pte_t *kpte, unsigned long address,
  289. struct cpa_data *cpa)
  290. {
  291. unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
  292. pte_t new_pte, old_pte, *tmp;
  293. pgprot_t old_prot, new_prot;
  294. int i, do_split = 1;
  295. unsigned int level;
  296. if (cpa->force_split)
  297. return 1;
  298. spin_lock_irqsave(&pgd_lock, flags);
  299. /*
  300. * Check for races, another CPU might have split this page
  301. * up already:
  302. */
  303. tmp = lookup_address(address, &level);
  304. if (tmp != kpte)
  305. goto out_unlock;
  306. switch (level) {
  307. case PG_LEVEL_2M:
  308. psize = PMD_PAGE_SIZE;
  309. pmask = PMD_PAGE_MASK;
  310. break;
  311. #ifdef CONFIG_X86_64
  312. case PG_LEVEL_1G:
  313. psize = PUD_PAGE_SIZE;
  314. pmask = PUD_PAGE_MASK;
  315. break;
  316. #endif
  317. default:
  318. do_split = -EINVAL;
  319. goto out_unlock;
  320. }
  321. /*
  322. * Calculate the number of pages, which fit into this large
  323. * page starting at address:
  324. */
  325. nextpage_addr = (address + psize) & pmask;
  326. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  327. if (numpages < cpa->numpages)
  328. cpa->numpages = numpages;
  329. /*
  330. * We are safe now. Check whether the new pgprot is the same:
  331. */
  332. old_pte = *kpte;
  333. old_prot = new_prot = pte_pgprot(old_pte);
  334. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  335. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  336. /*
  337. * old_pte points to the large page base address. So we need
  338. * to add the offset of the virtual address:
  339. */
  340. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  341. cpa->pfn = pfn;
  342. new_prot = static_protections(new_prot, address, pfn);
  343. /*
  344. * We need to check the full range, whether
  345. * static_protection() requires a different pgprot for one of
  346. * the pages in the range we try to preserve:
  347. */
  348. addr = address + PAGE_SIZE;
  349. pfn++;
  350. for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE, pfn++) {
  351. pgprot_t chk_prot = static_protections(new_prot, addr, pfn);
  352. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  353. goto out_unlock;
  354. }
  355. /*
  356. * If there are no changes, return. maxpages has been updated
  357. * above:
  358. */
  359. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  360. do_split = 0;
  361. goto out_unlock;
  362. }
  363. /*
  364. * We need to change the attributes. Check, whether we can
  365. * change the large page in one go. We request a split, when
  366. * the address is not aligned and the number of pages is
  367. * smaller than the number of pages in the large page. Note
  368. * that we limited the number of possible pages already to
  369. * the number of pages in the large page.
  370. */
  371. if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
  372. /*
  373. * The address is aligned and the number of pages
  374. * covers the full page.
  375. */
  376. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  377. __set_pmd_pte(kpte, address, new_pte);
  378. cpa->flags |= CPA_FLUSHTLB;
  379. do_split = 0;
  380. }
  381. out_unlock:
  382. spin_unlock_irqrestore(&pgd_lock, flags);
  383. return do_split;
  384. }
  385. static int split_large_page(pte_t *kpte, unsigned long address)
  386. {
  387. unsigned long flags, pfn, pfninc = 1;
  388. unsigned int i, level;
  389. pte_t *pbase, *tmp;
  390. pgprot_t ref_prot;
  391. struct page *base;
  392. if (!debug_pagealloc)
  393. spin_unlock(&cpa_lock);
  394. base = alloc_pages(GFP_KERNEL, 0);
  395. if (!debug_pagealloc)
  396. spin_lock(&cpa_lock);
  397. if (!base)
  398. return -ENOMEM;
  399. spin_lock_irqsave(&pgd_lock, flags);
  400. /*
  401. * Check for races, another CPU might have split this page
  402. * up for us already:
  403. */
  404. tmp = lookup_address(address, &level);
  405. if (tmp != kpte)
  406. goto out_unlock;
  407. pbase = (pte_t *)page_address(base);
  408. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  409. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  410. /*
  411. * If we ever want to utilize the PAT bit, we need to
  412. * update this function to make sure it's converted from
  413. * bit 12 to bit 7 when we cross from the 2MB level to
  414. * the 4K level:
  415. */
  416. WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
  417. #ifdef CONFIG_X86_64
  418. if (level == PG_LEVEL_1G) {
  419. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  420. pgprot_val(ref_prot) |= _PAGE_PSE;
  421. }
  422. #endif
  423. /*
  424. * Get the target pfn from the original entry:
  425. */
  426. pfn = pte_pfn(*kpte);
  427. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  428. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  429. if (address >= (unsigned long)__va(0) &&
  430. address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
  431. split_page_count(level);
  432. #ifdef CONFIG_X86_64
  433. if (address >= (unsigned long)__va(1UL<<32) &&
  434. address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
  435. split_page_count(level);
  436. #endif
  437. /*
  438. * Install the new, split up pagetable.
  439. *
  440. * We use the standard kernel pagetable protections for the new
  441. * pagetable protections, the actual ptes set above control the
  442. * primary protection behavior:
  443. */
  444. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  445. /*
  446. * Intel Atom errata AAH41 workaround.
  447. *
  448. * The real fix should be in hw or in a microcode update, but
  449. * we also probabilistically try to reduce the window of having
  450. * a large TLB mixed with 4K TLBs while instruction fetches are
  451. * going on.
  452. */
  453. __flush_tlb_all();
  454. base = NULL;
  455. out_unlock:
  456. /*
  457. * If we dropped out via the lookup_address check under
  458. * pgd_lock then stick the page back into the pool:
  459. */
  460. if (base)
  461. __free_page(base);
  462. spin_unlock_irqrestore(&pgd_lock, flags);
  463. return 0;
  464. }
  465. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  466. int primary)
  467. {
  468. /*
  469. * Ignore all non primary paths.
  470. */
  471. if (!primary)
  472. return 0;
  473. /*
  474. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  475. * to have holes.
  476. * Also set numpages to '1' indicating that we processed cpa req for
  477. * one virtual address page and its pfn. TBD: numpages can be set based
  478. * on the initial value and the level returned by lookup_address().
  479. */
  480. if (within(vaddr, PAGE_OFFSET,
  481. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  482. cpa->numpages = 1;
  483. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  484. return 0;
  485. } else {
  486. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  487. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  488. *cpa->vaddr);
  489. return -EFAULT;
  490. }
  491. }
  492. static int __change_page_attr(struct cpa_data *cpa, int primary)
  493. {
  494. unsigned long address;
  495. int do_split, err;
  496. unsigned int level;
  497. pte_t *kpte, old_pte;
  498. if (cpa->flags & CPA_ARRAY)
  499. address = cpa->vaddr[cpa->curpage];
  500. else
  501. address = *cpa->vaddr;
  502. repeat:
  503. kpte = lookup_address(address, &level);
  504. if (!kpte)
  505. return __cpa_process_fault(cpa, address, primary);
  506. old_pte = *kpte;
  507. if (!pte_val(old_pte))
  508. return __cpa_process_fault(cpa, address, primary);
  509. if (level == PG_LEVEL_4K) {
  510. pte_t new_pte;
  511. pgprot_t new_prot = pte_pgprot(old_pte);
  512. unsigned long pfn = pte_pfn(old_pte);
  513. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  514. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  515. new_prot = static_protections(new_prot, address, pfn);
  516. /*
  517. * We need to keep the pfn from the existing PTE,
  518. * after all we're only going to change it's attributes
  519. * not the memory it points to
  520. */
  521. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  522. cpa->pfn = pfn;
  523. /*
  524. * Do we really change anything ?
  525. */
  526. if (pte_val(old_pte) != pte_val(new_pte)) {
  527. set_pte_atomic(kpte, new_pte);
  528. cpa->flags |= CPA_FLUSHTLB;
  529. }
  530. cpa->numpages = 1;
  531. return 0;
  532. }
  533. /*
  534. * Check, whether we can keep the large page intact
  535. * and just change the pte:
  536. */
  537. do_split = try_preserve_large_page(kpte, address, cpa);
  538. /*
  539. * When the range fits into the existing large page,
  540. * return. cp->numpages and cpa->tlbflush have been updated in
  541. * try_large_page:
  542. */
  543. if (do_split <= 0)
  544. return do_split;
  545. /*
  546. * We have to split the large page:
  547. */
  548. err = split_large_page(kpte, address);
  549. if (!err) {
  550. /*
  551. * Do a global flush tlb after splitting the large page
  552. * and before we do the actual change page attribute in the PTE.
  553. *
  554. * With out this, we violate the TLB application note, that says
  555. * "The TLBs may contain both ordinary and large-page
  556. * translations for a 4-KByte range of linear addresses. This
  557. * may occur if software modifies the paging structures so that
  558. * the page size used for the address range changes. If the two
  559. * translations differ with respect to page frame or attributes
  560. * (e.g., permissions), processor behavior is undefined and may
  561. * be implementation-specific."
  562. *
  563. * We do this global tlb flush inside the cpa_lock, so that we
  564. * don't allow any other cpu, with stale tlb entries change the
  565. * page attribute in parallel, that also falls into the
  566. * just split large page entry.
  567. */
  568. flush_tlb_all();
  569. goto repeat;
  570. }
  571. return err;
  572. }
  573. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  574. static int cpa_process_alias(struct cpa_data *cpa)
  575. {
  576. struct cpa_data alias_cpa;
  577. int ret = 0;
  578. unsigned long temp_cpa_vaddr, vaddr;
  579. if (cpa->pfn >= max_pfn_mapped)
  580. return 0;
  581. #ifdef CONFIG_X86_64
  582. if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
  583. return 0;
  584. #endif
  585. /*
  586. * No need to redo, when the primary call touched the direct
  587. * mapping already:
  588. */
  589. if (cpa->flags & CPA_ARRAY)
  590. vaddr = cpa->vaddr[cpa->curpage];
  591. else
  592. vaddr = *cpa->vaddr;
  593. if (!(within(vaddr, PAGE_OFFSET,
  594. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  595. alias_cpa = *cpa;
  596. temp_cpa_vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
  597. alias_cpa.vaddr = &temp_cpa_vaddr;
  598. alias_cpa.flags &= ~CPA_ARRAY;
  599. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  600. }
  601. #ifdef CONFIG_X86_64
  602. if (ret)
  603. return ret;
  604. /*
  605. * No need to redo, when the primary call touched the high
  606. * mapping already:
  607. */
  608. if (within(vaddr, (unsigned long) _text, (unsigned long) _end))
  609. return 0;
  610. /*
  611. * If the physical address is inside the kernel map, we need
  612. * to touch the high mapped kernel as well:
  613. */
  614. if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
  615. return 0;
  616. alias_cpa = *cpa;
  617. temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
  618. alias_cpa.vaddr = &temp_cpa_vaddr;
  619. alias_cpa.flags &= ~CPA_ARRAY;
  620. /*
  621. * The high mapping range is imprecise, so ignore the return value.
  622. */
  623. __change_page_attr_set_clr(&alias_cpa, 0);
  624. #endif
  625. return ret;
  626. }
  627. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  628. {
  629. int ret, numpages = cpa->numpages;
  630. while (numpages) {
  631. /*
  632. * Store the remaining nr of pages for the large page
  633. * preservation check.
  634. */
  635. cpa->numpages = numpages;
  636. /* for array changes, we can't use large page */
  637. if (cpa->flags & CPA_ARRAY)
  638. cpa->numpages = 1;
  639. if (!debug_pagealloc)
  640. spin_lock(&cpa_lock);
  641. ret = __change_page_attr(cpa, checkalias);
  642. if (!debug_pagealloc)
  643. spin_unlock(&cpa_lock);
  644. if (ret)
  645. return ret;
  646. if (checkalias) {
  647. ret = cpa_process_alias(cpa);
  648. if (ret)
  649. return ret;
  650. }
  651. /*
  652. * Adjust the number of pages with the result of the
  653. * CPA operation. Either a large page has been
  654. * preserved or a single page update happened.
  655. */
  656. BUG_ON(cpa->numpages > numpages);
  657. numpages -= cpa->numpages;
  658. if (cpa->flags & CPA_ARRAY)
  659. cpa->curpage++;
  660. else
  661. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  662. }
  663. return 0;
  664. }
  665. static inline int cache_attr(pgprot_t attr)
  666. {
  667. return pgprot_val(attr) &
  668. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  669. }
  670. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  671. pgprot_t mask_set, pgprot_t mask_clr,
  672. int force_split, int array)
  673. {
  674. struct cpa_data cpa;
  675. int ret, cache, checkalias;
  676. /*
  677. * Check, if we are requested to change a not supported
  678. * feature:
  679. */
  680. mask_set = canon_pgprot(mask_set);
  681. mask_clr = canon_pgprot(mask_clr);
  682. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  683. return 0;
  684. /* Ensure we are PAGE_SIZE aligned */
  685. if (!array) {
  686. if (*addr & ~PAGE_MASK) {
  687. *addr &= PAGE_MASK;
  688. /*
  689. * People should not be passing in unaligned addresses:
  690. */
  691. WARN_ON_ONCE(1);
  692. }
  693. } else {
  694. int i;
  695. for (i = 0; i < numpages; i++) {
  696. if (addr[i] & ~PAGE_MASK) {
  697. addr[i] &= PAGE_MASK;
  698. WARN_ON_ONCE(1);
  699. }
  700. }
  701. }
  702. /* Must avoid aliasing mappings in the highmem code */
  703. kmap_flush_unused();
  704. vm_unmap_aliases();
  705. /*
  706. * If we're called with lazy mmu updates enabled, the
  707. * in-memory pte state may be stale. Flush pending updates to
  708. * bring them up to date.
  709. */
  710. arch_flush_lazy_mmu_mode();
  711. cpa.vaddr = addr;
  712. cpa.numpages = numpages;
  713. cpa.mask_set = mask_set;
  714. cpa.mask_clr = mask_clr;
  715. cpa.flags = 0;
  716. cpa.curpage = 0;
  717. cpa.force_split = force_split;
  718. if (array)
  719. cpa.flags |= CPA_ARRAY;
  720. /* No alias checking for _NX bit modifications */
  721. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  722. ret = __change_page_attr_set_clr(&cpa, checkalias);
  723. /*
  724. * Check whether we really changed something:
  725. */
  726. if (!(cpa.flags & CPA_FLUSHTLB))
  727. goto out;
  728. /*
  729. * No need to flush, when we did not set any of the caching
  730. * attributes:
  731. */
  732. cache = cache_attr(mask_set);
  733. /*
  734. * On success we use clflush, when the CPU supports it to
  735. * avoid the wbindv. If the CPU does not support it and in the
  736. * error case we fall back to cpa_flush_all (which uses
  737. * wbindv):
  738. */
  739. if (!ret && cpu_has_clflush) {
  740. if (cpa.flags & CPA_ARRAY)
  741. cpa_flush_array(addr, numpages, cache);
  742. else
  743. cpa_flush_range(*addr, numpages, cache);
  744. } else
  745. cpa_flush_all(cache);
  746. /*
  747. * If we've been called with lazy mmu updates enabled, then
  748. * make sure that everything gets flushed out before we
  749. * return.
  750. */
  751. arch_flush_lazy_mmu_mode();
  752. out:
  753. return ret;
  754. }
  755. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  756. pgprot_t mask, int array)
  757. {
  758. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  759. array);
  760. }
  761. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  762. pgprot_t mask, int array)
  763. {
  764. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  765. array);
  766. }
  767. int _set_memory_uc(unsigned long addr, int numpages)
  768. {
  769. /*
  770. * for now UC MINUS. see comments in ioremap_nocache()
  771. */
  772. return change_page_attr_set(&addr, numpages,
  773. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  774. }
  775. int set_memory_uc(unsigned long addr, int numpages)
  776. {
  777. /*
  778. * for now UC MINUS. see comments in ioremap_nocache()
  779. */
  780. if (reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  781. _PAGE_CACHE_UC_MINUS, NULL))
  782. return -EINVAL;
  783. return _set_memory_uc(addr, numpages);
  784. }
  785. EXPORT_SYMBOL(set_memory_uc);
  786. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  787. {
  788. unsigned long start;
  789. unsigned long end;
  790. int i;
  791. /*
  792. * for now UC MINUS. see comments in ioremap_nocache()
  793. */
  794. for (i = 0; i < addrinarray; i++) {
  795. start = __pa(addr[i]);
  796. for (end = start + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  797. if (end != __pa(addr[i + 1]))
  798. break;
  799. i++;
  800. }
  801. if (reserve_memtype(start, end, _PAGE_CACHE_UC_MINUS, NULL))
  802. goto out;
  803. }
  804. return change_page_attr_set(addr, addrinarray,
  805. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  806. out:
  807. for (i = 0; i < addrinarray; i++) {
  808. unsigned long tmp = __pa(addr[i]);
  809. if (tmp == start)
  810. break;
  811. for (end = tmp + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  812. if (end != __pa(addr[i + 1]))
  813. break;
  814. i++;
  815. }
  816. free_memtype(tmp, end);
  817. }
  818. return -EINVAL;
  819. }
  820. EXPORT_SYMBOL(set_memory_array_uc);
  821. int _set_memory_wc(unsigned long addr, int numpages)
  822. {
  823. return change_page_attr_set(&addr, numpages,
  824. __pgprot(_PAGE_CACHE_WC), 0);
  825. }
  826. int set_memory_wc(unsigned long addr, int numpages)
  827. {
  828. if (!pat_enabled)
  829. return set_memory_uc(addr, numpages);
  830. if (reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  831. _PAGE_CACHE_WC, NULL))
  832. return -EINVAL;
  833. return _set_memory_wc(addr, numpages);
  834. }
  835. EXPORT_SYMBOL(set_memory_wc);
  836. int _set_memory_wb(unsigned long addr, int numpages)
  837. {
  838. return change_page_attr_clear(&addr, numpages,
  839. __pgprot(_PAGE_CACHE_MASK), 0);
  840. }
  841. int set_memory_wb(unsigned long addr, int numpages)
  842. {
  843. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  844. return _set_memory_wb(addr, numpages);
  845. }
  846. EXPORT_SYMBOL(set_memory_wb);
  847. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  848. {
  849. int i;
  850. for (i = 0; i < addrinarray; i++) {
  851. unsigned long start = __pa(addr[i]);
  852. unsigned long end;
  853. for (end = start + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  854. if (end != __pa(addr[i + 1]))
  855. break;
  856. i++;
  857. }
  858. free_memtype(start, end);
  859. }
  860. return change_page_attr_clear(addr, addrinarray,
  861. __pgprot(_PAGE_CACHE_MASK), 1);
  862. }
  863. EXPORT_SYMBOL(set_memory_array_wb);
  864. int set_memory_x(unsigned long addr, int numpages)
  865. {
  866. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  867. }
  868. EXPORT_SYMBOL(set_memory_x);
  869. int set_memory_nx(unsigned long addr, int numpages)
  870. {
  871. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  872. }
  873. EXPORT_SYMBOL(set_memory_nx);
  874. int set_memory_ro(unsigned long addr, int numpages)
  875. {
  876. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  877. }
  878. EXPORT_SYMBOL_GPL(set_memory_ro);
  879. int set_memory_rw(unsigned long addr, int numpages)
  880. {
  881. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  882. }
  883. EXPORT_SYMBOL_GPL(set_memory_rw);
  884. int set_memory_np(unsigned long addr, int numpages)
  885. {
  886. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  887. }
  888. int set_memory_4k(unsigned long addr, int numpages)
  889. {
  890. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  891. __pgprot(0), 1, 0);
  892. }
  893. int set_pages_uc(struct page *page, int numpages)
  894. {
  895. unsigned long addr = (unsigned long)page_address(page);
  896. return set_memory_uc(addr, numpages);
  897. }
  898. EXPORT_SYMBOL(set_pages_uc);
  899. int set_pages_wb(struct page *page, int numpages)
  900. {
  901. unsigned long addr = (unsigned long)page_address(page);
  902. return set_memory_wb(addr, numpages);
  903. }
  904. EXPORT_SYMBOL(set_pages_wb);
  905. int set_pages_x(struct page *page, int numpages)
  906. {
  907. unsigned long addr = (unsigned long)page_address(page);
  908. return set_memory_x(addr, numpages);
  909. }
  910. EXPORT_SYMBOL(set_pages_x);
  911. int set_pages_nx(struct page *page, int numpages)
  912. {
  913. unsigned long addr = (unsigned long)page_address(page);
  914. return set_memory_nx(addr, numpages);
  915. }
  916. EXPORT_SYMBOL(set_pages_nx);
  917. int set_pages_ro(struct page *page, int numpages)
  918. {
  919. unsigned long addr = (unsigned long)page_address(page);
  920. return set_memory_ro(addr, numpages);
  921. }
  922. int set_pages_rw(struct page *page, int numpages)
  923. {
  924. unsigned long addr = (unsigned long)page_address(page);
  925. return set_memory_rw(addr, numpages);
  926. }
  927. #ifdef CONFIG_DEBUG_PAGEALLOC
  928. static int __set_pages_p(struct page *page, int numpages)
  929. {
  930. unsigned long tempaddr = (unsigned long) page_address(page);
  931. struct cpa_data cpa = { .vaddr = &tempaddr,
  932. .numpages = numpages,
  933. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  934. .mask_clr = __pgprot(0),
  935. .flags = 0};
  936. /*
  937. * No alias checking needed for setting present flag. otherwise,
  938. * we may need to break large pages for 64-bit kernel text
  939. * mappings (this adds to complexity if we want to do this from
  940. * atomic context especially). Let's keep it simple!
  941. */
  942. return __change_page_attr_set_clr(&cpa, 0);
  943. }
  944. static int __set_pages_np(struct page *page, int numpages)
  945. {
  946. unsigned long tempaddr = (unsigned long) page_address(page);
  947. struct cpa_data cpa = { .vaddr = &tempaddr,
  948. .numpages = numpages,
  949. .mask_set = __pgprot(0),
  950. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  951. .flags = 0};
  952. /*
  953. * No alias checking needed for setting not present flag. otherwise,
  954. * we may need to break large pages for 64-bit kernel text
  955. * mappings (this adds to complexity if we want to do this from
  956. * atomic context especially). Let's keep it simple!
  957. */
  958. return __change_page_attr_set_clr(&cpa, 0);
  959. }
  960. void kernel_map_pages(struct page *page, int numpages, int enable)
  961. {
  962. if (PageHighMem(page))
  963. return;
  964. if (!enable) {
  965. debug_check_no_locks_freed(page_address(page),
  966. numpages * PAGE_SIZE);
  967. }
  968. /*
  969. * If page allocator is not up yet then do not call c_p_a():
  970. */
  971. if (!debug_pagealloc_enabled)
  972. return;
  973. /*
  974. * The return value is ignored as the calls cannot fail.
  975. * Large pages for identity mappings are not used at boot time
  976. * and hence no memory allocations during large page split.
  977. */
  978. if (enable)
  979. __set_pages_p(page, numpages);
  980. else
  981. __set_pages_np(page, numpages);
  982. /*
  983. * We should perform an IPI and flush all tlbs,
  984. * but that can deadlock->flush only current cpu:
  985. */
  986. __flush_tlb_all();
  987. }
  988. #ifdef CONFIG_HIBERNATION
  989. bool kernel_page_present(struct page *page)
  990. {
  991. unsigned int level;
  992. pte_t *pte;
  993. if (PageHighMem(page))
  994. return false;
  995. pte = lookup_address((unsigned long)page_address(page), &level);
  996. return (pte_val(*pte) & _PAGE_PRESENT);
  997. }
  998. #endif /* CONFIG_HIBERNATION */
  999. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1000. /*
  1001. * The testcases use internal knowledge of the implementation that shouldn't
  1002. * be exposed to the rest of the kernel. Include these directly here.
  1003. */
  1004. #ifdef CONFIG_CPA_DEBUG
  1005. #include "pageattr-test.c"
  1006. #endif