pageattr.c 28 KB

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