pageattr.c 31 KB

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