pageattr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. #ifdef CONFIG_X86_64
  411. if (level == PG_LEVEL_1G) {
  412. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  413. pgprot_val(ref_prot) |= _PAGE_PSE;
  414. }
  415. #endif
  416. /*
  417. * Get the target pfn from the original entry:
  418. */
  419. pfn = pte_pfn(*kpte);
  420. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  421. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  422. if (address >= (unsigned long)__va(0) &&
  423. address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
  424. split_page_count(level);
  425. #ifdef CONFIG_X86_64
  426. if (address >= (unsigned long)__va(1UL<<32) &&
  427. address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
  428. split_page_count(level);
  429. #endif
  430. /*
  431. * Install the new, split up pagetable.
  432. *
  433. * We use the standard kernel pagetable protections for the new
  434. * pagetable protections, the actual ptes set above control the
  435. * primary protection behavior:
  436. */
  437. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  438. base = NULL;
  439. out_unlock:
  440. /*
  441. * If we dropped out via the lookup_address check under
  442. * pgd_lock then stick the page back into the pool:
  443. */
  444. if (base)
  445. __free_page(base);
  446. spin_unlock_irqrestore(&pgd_lock, flags);
  447. return 0;
  448. }
  449. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  450. int primary)
  451. {
  452. /*
  453. * Ignore all non primary paths.
  454. */
  455. if (!primary)
  456. return 0;
  457. /*
  458. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  459. * to have holes.
  460. * Also set numpages to '1' indicating that we processed cpa req for
  461. * one virtual address page and its pfn. TBD: numpages can be set based
  462. * on the initial value and the level returned by lookup_address().
  463. */
  464. if (within(vaddr, PAGE_OFFSET,
  465. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  466. cpa->numpages = 1;
  467. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  468. return 0;
  469. } else {
  470. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  471. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  472. *cpa->vaddr);
  473. return -EFAULT;
  474. }
  475. }
  476. static int __change_page_attr(struct cpa_data *cpa, int primary)
  477. {
  478. unsigned long address;
  479. int do_split, err;
  480. unsigned int level;
  481. pte_t *kpte, old_pte;
  482. if (cpa->flags & CPA_ARRAY)
  483. address = cpa->vaddr[cpa->curpage];
  484. else
  485. address = *cpa->vaddr;
  486. repeat:
  487. kpte = lookup_address(address, &level);
  488. if (!kpte)
  489. return __cpa_process_fault(cpa, address, primary);
  490. old_pte = *kpte;
  491. if (!pte_val(old_pte))
  492. return __cpa_process_fault(cpa, address, primary);
  493. if (level == PG_LEVEL_4K) {
  494. pte_t new_pte;
  495. pgprot_t new_prot = pte_pgprot(old_pte);
  496. unsigned long pfn = pte_pfn(old_pte);
  497. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  498. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  499. new_prot = static_protections(new_prot, address, pfn);
  500. /*
  501. * We need to keep the pfn from the existing PTE,
  502. * after all we're only going to change it's attributes
  503. * not the memory it points to
  504. */
  505. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  506. cpa->pfn = pfn;
  507. /*
  508. * Do we really change anything ?
  509. */
  510. if (pte_val(old_pte) != pte_val(new_pte)) {
  511. set_pte_atomic(kpte, new_pte);
  512. cpa->flags |= CPA_FLUSHTLB;
  513. }
  514. cpa->numpages = 1;
  515. return 0;
  516. }
  517. /*
  518. * Check, whether we can keep the large page intact
  519. * and just change the pte:
  520. */
  521. do_split = try_preserve_large_page(kpte, address, cpa);
  522. /*
  523. * When the range fits into the existing large page,
  524. * return. cp->numpages and cpa->tlbflush have been updated in
  525. * try_large_page:
  526. */
  527. if (do_split <= 0)
  528. return do_split;
  529. /*
  530. * We have to split the large page:
  531. */
  532. err = split_large_page(kpte, address);
  533. if (!err) {
  534. /*
  535. * Do a global flush tlb after splitting the large page
  536. * and before we do the actual change page attribute in the PTE.
  537. *
  538. * With out this, we violate the TLB application note, that says
  539. * "The TLBs may contain both ordinary and large-page
  540. * translations for a 4-KByte range of linear addresses. This
  541. * may occur if software modifies the paging structures so that
  542. * the page size used for the address range changes. If the two
  543. * translations differ with respect to page frame or attributes
  544. * (e.g., permissions), processor behavior is undefined and may
  545. * be implementation-specific."
  546. *
  547. * We do this global tlb flush inside the cpa_lock, so that we
  548. * don't allow any other cpu, with stale tlb entries change the
  549. * page attribute in parallel, that also falls into the
  550. * just split large page entry.
  551. */
  552. flush_tlb_all();
  553. goto repeat;
  554. }
  555. return err;
  556. }
  557. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  558. static int cpa_process_alias(struct cpa_data *cpa)
  559. {
  560. struct cpa_data alias_cpa;
  561. int ret = 0;
  562. unsigned long temp_cpa_vaddr, vaddr;
  563. if (cpa->pfn >= max_pfn_mapped)
  564. return 0;
  565. #ifdef CONFIG_X86_64
  566. if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
  567. return 0;
  568. #endif
  569. /*
  570. * No need to redo, when the primary call touched the direct
  571. * mapping already:
  572. */
  573. if (cpa->flags & CPA_ARRAY)
  574. vaddr = cpa->vaddr[cpa->curpage];
  575. else
  576. vaddr = *cpa->vaddr;
  577. if (!(within(vaddr, PAGE_OFFSET,
  578. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  579. alias_cpa = *cpa;
  580. temp_cpa_vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
  581. alias_cpa.vaddr = &temp_cpa_vaddr;
  582. alias_cpa.flags &= ~CPA_ARRAY;
  583. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  584. }
  585. #ifdef CONFIG_X86_64
  586. if (ret)
  587. return ret;
  588. /*
  589. * No need to redo, when the primary call touched the high
  590. * mapping already:
  591. */
  592. if (within(vaddr, (unsigned long) _text, (unsigned long) _end))
  593. return 0;
  594. /*
  595. * If the physical address is inside the kernel map, we need
  596. * to touch the high mapped kernel as well:
  597. */
  598. if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
  599. return 0;
  600. alias_cpa = *cpa;
  601. temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
  602. alias_cpa.vaddr = &temp_cpa_vaddr;
  603. alias_cpa.flags &= ~CPA_ARRAY;
  604. /*
  605. * The high mapping range is imprecise, so ignore the return value.
  606. */
  607. __change_page_attr_set_clr(&alias_cpa, 0);
  608. #endif
  609. return ret;
  610. }
  611. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  612. {
  613. int ret, numpages = cpa->numpages;
  614. while (numpages) {
  615. /*
  616. * Store the remaining nr of pages for the large page
  617. * preservation check.
  618. */
  619. cpa->numpages = numpages;
  620. /* for array changes, we can't use large page */
  621. if (cpa->flags & CPA_ARRAY)
  622. cpa->numpages = 1;
  623. if (!debug_pagealloc)
  624. spin_lock(&cpa_lock);
  625. ret = __change_page_attr(cpa, checkalias);
  626. if (!debug_pagealloc)
  627. spin_unlock(&cpa_lock);
  628. if (ret)
  629. return ret;
  630. if (checkalias) {
  631. ret = cpa_process_alias(cpa);
  632. if (ret)
  633. return ret;
  634. }
  635. /*
  636. * Adjust the number of pages with the result of the
  637. * CPA operation. Either a large page has been
  638. * preserved or a single page update happened.
  639. */
  640. BUG_ON(cpa->numpages > numpages);
  641. numpages -= cpa->numpages;
  642. if (cpa->flags & CPA_ARRAY)
  643. cpa->curpage++;
  644. else
  645. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  646. }
  647. return 0;
  648. }
  649. static inline int cache_attr(pgprot_t attr)
  650. {
  651. return pgprot_val(attr) &
  652. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  653. }
  654. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  655. pgprot_t mask_set, pgprot_t mask_clr,
  656. int force_split, int array)
  657. {
  658. struct cpa_data cpa;
  659. int ret, cache, checkalias;
  660. /*
  661. * Check, if we are requested to change a not supported
  662. * feature:
  663. */
  664. mask_set = canon_pgprot(mask_set);
  665. mask_clr = canon_pgprot(mask_clr);
  666. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  667. return 0;
  668. /* Ensure we are PAGE_SIZE aligned */
  669. if (!array) {
  670. if (*addr & ~PAGE_MASK) {
  671. *addr &= PAGE_MASK;
  672. /*
  673. * People should not be passing in unaligned addresses:
  674. */
  675. WARN_ON_ONCE(1);
  676. }
  677. } else {
  678. int i;
  679. for (i = 0; i < numpages; i++) {
  680. if (addr[i] & ~PAGE_MASK) {
  681. addr[i] &= PAGE_MASK;
  682. WARN_ON_ONCE(1);
  683. }
  684. }
  685. }
  686. /* Must avoid aliasing mappings in the highmem code */
  687. kmap_flush_unused();
  688. vm_unmap_aliases();
  689. /*
  690. * If we're called with lazy mmu updates enabled, the
  691. * in-memory pte state may be stale. Flush pending updates to
  692. * bring them up to date.
  693. */
  694. arch_flush_lazy_mmu_mode();
  695. cpa.vaddr = addr;
  696. cpa.numpages = numpages;
  697. cpa.mask_set = mask_set;
  698. cpa.mask_clr = mask_clr;
  699. cpa.flags = 0;
  700. cpa.curpage = 0;
  701. cpa.force_split = force_split;
  702. if (array)
  703. cpa.flags |= CPA_ARRAY;
  704. /* No alias checking for _NX bit modifications */
  705. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  706. ret = __change_page_attr_set_clr(&cpa, checkalias);
  707. /*
  708. * Check whether we really changed something:
  709. */
  710. if (!(cpa.flags & CPA_FLUSHTLB))
  711. goto out;
  712. /*
  713. * No need to flush, when we did not set any of the caching
  714. * attributes:
  715. */
  716. cache = cache_attr(mask_set);
  717. /*
  718. * On success we use clflush, when the CPU supports it to
  719. * avoid the wbindv. If the CPU does not support it and in the
  720. * error case we fall back to cpa_flush_all (which uses
  721. * wbindv):
  722. */
  723. if (!ret && cpu_has_clflush) {
  724. if (cpa.flags & CPA_ARRAY)
  725. cpa_flush_array(addr, numpages, cache);
  726. else
  727. cpa_flush_range(*addr, numpages, cache);
  728. } else
  729. cpa_flush_all(cache);
  730. /*
  731. * If we've been called with lazy mmu updates enabled, then
  732. * make sure that everything gets flushed out before we
  733. * return.
  734. */
  735. arch_flush_lazy_mmu_mode();
  736. out:
  737. return ret;
  738. }
  739. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  740. pgprot_t mask, int array)
  741. {
  742. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  743. array);
  744. }
  745. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  746. pgprot_t mask, int array)
  747. {
  748. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  749. array);
  750. }
  751. int _set_memory_uc(unsigned long addr, int numpages)
  752. {
  753. /*
  754. * for now UC MINUS. see comments in ioremap_nocache()
  755. */
  756. return change_page_attr_set(&addr, numpages,
  757. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  758. }
  759. int set_memory_uc(unsigned long addr, int numpages)
  760. {
  761. /*
  762. * for now UC MINUS. see comments in ioremap_nocache()
  763. */
  764. if (reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  765. _PAGE_CACHE_UC_MINUS, NULL))
  766. return -EINVAL;
  767. return _set_memory_uc(addr, numpages);
  768. }
  769. EXPORT_SYMBOL(set_memory_uc);
  770. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  771. {
  772. unsigned long start;
  773. unsigned long end;
  774. int i;
  775. /*
  776. * for now UC MINUS. see comments in ioremap_nocache()
  777. */
  778. for (i = 0; i < addrinarray; i++) {
  779. start = __pa(addr[i]);
  780. for (end = start + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  781. if (end != __pa(addr[i + 1]))
  782. break;
  783. i++;
  784. }
  785. if (reserve_memtype(start, end, _PAGE_CACHE_UC_MINUS, NULL))
  786. goto out;
  787. }
  788. return change_page_attr_set(addr, addrinarray,
  789. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  790. out:
  791. for (i = 0; i < addrinarray; i++) {
  792. unsigned long tmp = __pa(addr[i]);
  793. if (tmp == start)
  794. break;
  795. for (end = tmp + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  796. if (end != __pa(addr[i + 1]))
  797. break;
  798. i++;
  799. }
  800. free_memtype(tmp, end);
  801. }
  802. return -EINVAL;
  803. }
  804. EXPORT_SYMBOL(set_memory_array_uc);
  805. int _set_memory_wc(unsigned long addr, int numpages)
  806. {
  807. return change_page_attr_set(&addr, numpages,
  808. __pgprot(_PAGE_CACHE_WC), 0);
  809. }
  810. int set_memory_wc(unsigned long addr, int numpages)
  811. {
  812. if (!pat_enabled)
  813. return set_memory_uc(addr, numpages);
  814. if (reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  815. _PAGE_CACHE_WC, NULL))
  816. return -EINVAL;
  817. return _set_memory_wc(addr, numpages);
  818. }
  819. EXPORT_SYMBOL(set_memory_wc);
  820. int _set_memory_wb(unsigned long addr, int numpages)
  821. {
  822. return change_page_attr_clear(&addr, numpages,
  823. __pgprot(_PAGE_CACHE_MASK), 0);
  824. }
  825. int set_memory_wb(unsigned long addr, int numpages)
  826. {
  827. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  828. return _set_memory_wb(addr, numpages);
  829. }
  830. EXPORT_SYMBOL(set_memory_wb);
  831. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  832. {
  833. int i;
  834. for (i = 0; i < addrinarray; i++) {
  835. unsigned long start = __pa(addr[i]);
  836. unsigned long end;
  837. for (end = start + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  838. if (end != __pa(addr[i + 1]))
  839. break;
  840. i++;
  841. }
  842. free_memtype(start, end);
  843. }
  844. return change_page_attr_clear(addr, addrinarray,
  845. __pgprot(_PAGE_CACHE_MASK), 1);
  846. }
  847. EXPORT_SYMBOL(set_memory_array_wb);
  848. int set_memory_x(unsigned long addr, int numpages)
  849. {
  850. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  851. }
  852. EXPORT_SYMBOL(set_memory_x);
  853. int set_memory_nx(unsigned long addr, int numpages)
  854. {
  855. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  856. }
  857. EXPORT_SYMBOL(set_memory_nx);
  858. int set_memory_ro(unsigned long addr, int numpages)
  859. {
  860. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  861. }
  862. EXPORT_SYMBOL_GPL(set_memory_ro);
  863. int set_memory_rw(unsigned long addr, int numpages)
  864. {
  865. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  866. }
  867. EXPORT_SYMBOL_GPL(set_memory_rw);
  868. int set_memory_np(unsigned long addr, int numpages)
  869. {
  870. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  871. }
  872. int set_memory_4k(unsigned long addr, int numpages)
  873. {
  874. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  875. __pgprot(0), 1, 0);
  876. }
  877. int set_pages_uc(struct page *page, int numpages)
  878. {
  879. unsigned long addr = (unsigned long)page_address(page);
  880. return set_memory_uc(addr, numpages);
  881. }
  882. EXPORT_SYMBOL(set_pages_uc);
  883. int set_pages_wb(struct page *page, int numpages)
  884. {
  885. unsigned long addr = (unsigned long)page_address(page);
  886. return set_memory_wb(addr, numpages);
  887. }
  888. EXPORT_SYMBOL(set_pages_wb);
  889. int set_pages_x(struct page *page, int numpages)
  890. {
  891. unsigned long addr = (unsigned long)page_address(page);
  892. return set_memory_x(addr, numpages);
  893. }
  894. EXPORT_SYMBOL(set_pages_x);
  895. int set_pages_nx(struct page *page, int numpages)
  896. {
  897. unsigned long addr = (unsigned long)page_address(page);
  898. return set_memory_nx(addr, numpages);
  899. }
  900. EXPORT_SYMBOL(set_pages_nx);
  901. int set_pages_ro(struct page *page, int numpages)
  902. {
  903. unsigned long addr = (unsigned long)page_address(page);
  904. return set_memory_ro(addr, numpages);
  905. }
  906. int set_pages_rw(struct page *page, int numpages)
  907. {
  908. unsigned long addr = (unsigned long)page_address(page);
  909. return set_memory_rw(addr, numpages);
  910. }
  911. #ifdef CONFIG_DEBUG_PAGEALLOC
  912. static int __set_pages_p(struct page *page, int numpages)
  913. {
  914. unsigned long tempaddr = (unsigned long) page_address(page);
  915. struct cpa_data cpa = { .vaddr = &tempaddr,
  916. .numpages = numpages,
  917. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  918. .mask_clr = __pgprot(0),
  919. .flags = 0};
  920. /*
  921. * No alias checking needed for setting present flag. otherwise,
  922. * we may need to break large pages for 64-bit kernel text
  923. * mappings (this adds to complexity if we want to do this from
  924. * atomic context especially). Let's keep it simple!
  925. */
  926. return __change_page_attr_set_clr(&cpa, 0);
  927. }
  928. static int __set_pages_np(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(0),
  934. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  935. .flags = 0};
  936. /*
  937. * No alias checking needed for setting not 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. void kernel_map_pages(struct page *page, int numpages, int enable)
  945. {
  946. if (PageHighMem(page))
  947. return;
  948. if (!enable) {
  949. debug_check_no_locks_freed(page_address(page),
  950. numpages * PAGE_SIZE);
  951. }
  952. /*
  953. * If page allocator is not up yet then do not call c_p_a():
  954. */
  955. if (!debug_pagealloc_enabled)
  956. return;
  957. /*
  958. * The return value is ignored as the calls cannot fail.
  959. * Large pages for identity mappings are not used at boot time
  960. * and hence no memory allocations during large page split.
  961. */
  962. if (enable)
  963. __set_pages_p(page, numpages);
  964. else
  965. __set_pages_np(page, numpages);
  966. /*
  967. * We should perform an IPI and flush all tlbs,
  968. * but that can deadlock->flush only current cpu:
  969. */
  970. __flush_tlb_all();
  971. }
  972. #ifdef CONFIG_HIBERNATION
  973. bool kernel_page_present(struct page *page)
  974. {
  975. unsigned int level;
  976. pte_t *pte;
  977. if (PageHighMem(page))
  978. return false;
  979. pte = lookup_address((unsigned long)page_address(page), &level);
  980. return (pte_val(*pte) & _PAGE_PRESENT);
  981. }
  982. #endif /* CONFIG_HIBERNATION */
  983. #endif /* CONFIG_DEBUG_PAGEALLOC */
  984. /*
  985. * The testcases use internal knowledge of the implementation that shouldn't
  986. * be exposed to the rest of the kernel. Include these directly here.
  987. */
  988. #ifdef CONFIG_CPA_DEBUG
  989. #include "pageattr-test.c"
  990. #endif