pageattr.c 31 KB

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