pageattr.c 31 KB

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