pageattr.c 27 KB

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