pageattr.c 31 KB

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