pageattr.c 32 KB

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