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