pageattr.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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 <asm/e820.h>
  16. #include <asm/processor.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/sections.h>
  19. #include <asm/setup.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/proto.h>
  23. #include <asm/pat.h>
  24. /*
  25. * The current flushing context - we pass it instead of 5 arguments:
  26. */
  27. struct cpa_data {
  28. unsigned long *vaddr;
  29. pgprot_t mask_set;
  30. pgprot_t mask_clr;
  31. int numpages;
  32. int flags;
  33. unsigned long pfn;
  34. unsigned force_split : 1;
  35. int curpage;
  36. struct page **pages;
  37. };
  38. /*
  39. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  40. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  41. * entries change the page attribute in parallel to some other cpu
  42. * splitting a large page entry along with changing the attribute.
  43. */
  44. static DEFINE_SPINLOCK(cpa_lock);
  45. #define CPA_FLUSHTLB 1
  46. #define CPA_ARRAY 2
  47. #define CPA_PAGES_ARRAY 4
  48. #ifdef CONFIG_PROC_FS
  49. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  50. void update_page_count(int level, unsigned long pages)
  51. {
  52. unsigned long flags;
  53. /* Protect against CPA */
  54. spin_lock_irqsave(&pgd_lock, flags);
  55. direct_pages_count[level] += pages;
  56. spin_unlock_irqrestore(&pgd_lock, flags);
  57. }
  58. static void split_page_count(int level)
  59. {
  60. direct_pages_count[level]--;
  61. direct_pages_count[level - 1] += PTRS_PER_PTE;
  62. }
  63. void arch_report_meminfo(struct seq_file *m)
  64. {
  65. seq_printf(m, "DirectMap4k: %8lu kB\n",
  66. direct_pages_count[PG_LEVEL_4K] << 2);
  67. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  68. seq_printf(m, "DirectMap2M: %8lu kB\n",
  69. direct_pages_count[PG_LEVEL_2M] << 11);
  70. #else
  71. seq_printf(m, "DirectMap4M: %8lu kB\n",
  72. direct_pages_count[PG_LEVEL_2M] << 12);
  73. #endif
  74. #ifdef CONFIG_X86_64
  75. if (direct_gbpages)
  76. seq_printf(m, "DirectMap1G: %8lu kB\n",
  77. direct_pages_count[PG_LEVEL_1G] << 20);
  78. #endif
  79. }
  80. #else
  81. static inline void split_page_count(int level) { }
  82. #endif
  83. #ifdef CONFIG_X86_64
  84. static inline unsigned long highmap_start_pfn(void)
  85. {
  86. return __pa(_text) >> PAGE_SHIFT;
  87. }
  88. static inline unsigned long highmap_end_pfn(void)
  89. {
  90. return __pa(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
  91. }
  92. #endif
  93. #ifdef CONFIG_DEBUG_PAGEALLOC
  94. # define debug_pagealloc 1
  95. #else
  96. # define debug_pagealloc 0
  97. #endif
  98. static inline int
  99. within(unsigned long addr, unsigned long start, unsigned long end)
  100. {
  101. return addr >= start && addr < end;
  102. }
  103. /*
  104. * Flushing functions
  105. */
  106. /**
  107. * clflush_cache_range - flush a cache range with clflush
  108. * @addr: virtual start address
  109. * @size: number of bytes to flush
  110. *
  111. * clflush is an unordered instruction which needs fencing with mfence
  112. * to avoid ordering issues.
  113. */
  114. void clflush_cache_range(void *vaddr, unsigned int size)
  115. {
  116. void *vend = vaddr + size - 1;
  117. mb();
  118. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  119. clflush(vaddr);
  120. /*
  121. * Flush any possible final partial cacheline:
  122. */
  123. clflush(vend);
  124. mb();
  125. }
  126. EXPORT_SYMBOL_GPL(clflush_cache_range);
  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, remapped;
  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. /*
  640. * If the PMD page was partially used for per-cpu remapping,
  641. * the recycled area needs to be split and modified. Because
  642. * the area is always proper subset of a PMD page
  643. * cpa->numpages is guaranteed to be 1 for these areas, so
  644. * there's no need to loop over and check for further remaps.
  645. */
  646. remapped = (unsigned long)pcpu_lpage_remapped((void *)laddr);
  647. if (remapped) {
  648. WARN_ON(cpa->numpages > 1);
  649. alias_cpa = *cpa;
  650. alias_cpa.vaddr = &remapped;
  651. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  652. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  653. if (ret)
  654. return ret;
  655. }
  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. /*
  709. * Check, if we are requested to change a not supported
  710. * feature:
  711. */
  712. mask_set = canon_pgprot(mask_set);
  713. mask_clr = canon_pgprot(mask_clr);
  714. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  715. return 0;
  716. /* Ensure we are PAGE_SIZE aligned */
  717. if (in_flag & CPA_ARRAY) {
  718. int i;
  719. for (i = 0; i < numpages; i++) {
  720. if (addr[i] & ~PAGE_MASK) {
  721. addr[i] &= PAGE_MASK;
  722. WARN_ON_ONCE(1);
  723. }
  724. }
  725. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  726. /*
  727. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  728. * No need to cehck in that case
  729. */
  730. if (*addr & ~PAGE_MASK) {
  731. *addr &= PAGE_MASK;
  732. /*
  733. * People should not be passing in unaligned addresses:
  734. */
  735. WARN_ON_ONCE(1);
  736. }
  737. }
  738. /* Must avoid aliasing mappings in the highmem code */
  739. kmap_flush_unused();
  740. vm_unmap_aliases();
  741. cpa.vaddr = addr;
  742. cpa.pages = pages;
  743. cpa.numpages = numpages;
  744. cpa.mask_set = mask_set;
  745. cpa.mask_clr = mask_clr;
  746. cpa.flags = 0;
  747. cpa.curpage = 0;
  748. cpa.force_split = force_split;
  749. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  750. cpa.flags |= in_flag;
  751. /* No alias checking for _NX bit modifications */
  752. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  753. ret = __change_page_attr_set_clr(&cpa, checkalias);
  754. /*
  755. * Check whether we really changed something:
  756. */
  757. if (!(cpa.flags & CPA_FLUSHTLB))
  758. goto out;
  759. /*
  760. * No need to flush, when we did not set any of the caching
  761. * attributes:
  762. */
  763. cache = cache_attr(mask_set);
  764. /*
  765. * On success we use clflush, when the CPU supports it to
  766. * avoid the wbindv. If the CPU does not support it and in the
  767. * error case we fall back to cpa_flush_all (which uses
  768. * wbindv):
  769. */
  770. if (!ret && cpu_has_clflush) {
  771. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  772. cpa_flush_array(addr, numpages, cache,
  773. cpa.flags, pages);
  774. } else
  775. cpa_flush_range(*addr, numpages, cache);
  776. } else
  777. cpa_flush_all(cache);
  778. out:
  779. return ret;
  780. }
  781. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  782. pgprot_t mask, int array)
  783. {
  784. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  785. (array ? CPA_ARRAY : 0), NULL);
  786. }
  787. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  788. pgprot_t mask, int array)
  789. {
  790. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  791. (array ? CPA_ARRAY : 0), NULL);
  792. }
  793. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  794. pgprot_t mask)
  795. {
  796. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  797. CPA_PAGES_ARRAY, pages);
  798. }
  799. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  800. pgprot_t mask)
  801. {
  802. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  803. CPA_PAGES_ARRAY, pages);
  804. }
  805. int _set_memory_uc(unsigned long addr, int numpages)
  806. {
  807. /*
  808. * for now UC MINUS. see comments in ioremap_nocache()
  809. */
  810. return change_page_attr_set(&addr, numpages,
  811. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  812. }
  813. int set_memory_uc(unsigned long addr, int numpages)
  814. {
  815. int ret;
  816. /*
  817. * for now UC MINUS. see comments in ioremap_nocache()
  818. */
  819. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  820. _PAGE_CACHE_UC_MINUS, NULL);
  821. if (ret)
  822. goto out_err;
  823. ret = _set_memory_uc(addr, numpages);
  824. if (ret)
  825. goto out_free;
  826. return 0;
  827. out_free:
  828. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  829. out_err:
  830. return ret;
  831. }
  832. EXPORT_SYMBOL(set_memory_uc);
  833. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  834. {
  835. int i, j;
  836. int ret;
  837. /*
  838. * for now UC MINUS. see comments in ioremap_nocache()
  839. */
  840. for (i = 0; i < addrinarray; i++) {
  841. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  842. _PAGE_CACHE_UC_MINUS, NULL);
  843. if (ret)
  844. goto out_free;
  845. }
  846. ret = change_page_attr_set(addr, addrinarray,
  847. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  848. if (ret)
  849. goto out_free;
  850. return 0;
  851. out_free:
  852. for (j = 0; j < i; j++)
  853. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  854. return ret;
  855. }
  856. EXPORT_SYMBOL(set_memory_array_uc);
  857. int _set_memory_wc(unsigned long addr, int numpages)
  858. {
  859. int ret;
  860. unsigned long addr_copy = addr;
  861. ret = change_page_attr_set(&addr, numpages,
  862. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  863. if (!ret) {
  864. ret = change_page_attr_set_clr(&addr_copy, numpages,
  865. __pgprot(_PAGE_CACHE_WC),
  866. __pgprot(_PAGE_CACHE_MASK),
  867. 0, 0, NULL);
  868. }
  869. return ret;
  870. }
  871. int set_memory_wc(unsigned long addr, int numpages)
  872. {
  873. int ret;
  874. if (!pat_enabled)
  875. return set_memory_uc(addr, numpages);
  876. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  877. _PAGE_CACHE_WC, NULL);
  878. if (ret)
  879. goto out_err;
  880. ret = _set_memory_wc(addr, numpages);
  881. if (ret)
  882. goto out_free;
  883. return 0;
  884. out_free:
  885. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  886. out_err:
  887. return ret;
  888. }
  889. EXPORT_SYMBOL(set_memory_wc);
  890. int _set_memory_wb(unsigned long addr, int numpages)
  891. {
  892. return change_page_attr_clear(&addr, numpages,
  893. __pgprot(_PAGE_CACHE_MASK), 0);
  894. }
  895. int set_memory_wb(unsigned long addr, int numpages)
  896. {
  897. int ret;
  898. ret = _set_memory_wb(addr, numpages);
  899. if (ret)
  900. return ret;
  901. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  902. return 0;
  903. }
  904. EXPORT_SYMBOL(set_memory_wb);
  905. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  906. {
  907. int i;
  908. int ret;
  909. ret = change_page_attr_clear(addr, addrinarray,
  910. __pgprot(_PAGE_CACHE_MASK), 1);
  911. if (ret)
  912. return ret;
  913. for (i = 0; i < addrinarray; i++)
  914. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  915. return 0;
  916. }
  917. EXPORT_SYMBOL(set_memory_array_wb);
  918. int set_memory_x(unsigned long addr, int numpages)
  919. {
  920. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  921. }
  922. EXPORT_SYMBOL(set_memory_x);
  923. int set_memory_nx(unsigned long addr, int numpages)
  924. {
  925. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  926. }
  927. EXPORT_SYMBOL(set_memory_nx);
  928. int set_memory_ro(unsigned long addr, int numpages)
  929. {
  930. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  931. }
  932. EXPORT_SYMBOL_GPL(set_memory_ro);
  933. int set_memory_rw(unsigned long addr, int numpages)
  934. {
  935. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  936. }
  937. EXPORT_SYMBOL_GPL(set_memory_rw);
  938. int set_memory_np(unsigned long addr, int numpages)
  939. {
  940. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  941. }
  942. int set_memory_4k(unsigned long addr, int numpages)
  943. {
  944. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  945. __pgprot(0), 1, 0, NULL);
  946. }
  947. int set_pages_uc(struct page *page, int numpages)
  948. {
  949. unsigned long addr = (unsigned long)page_address(page);
  950. return set_memory_uc(addr, numpages);
  951. }
  952. EXPORT_SYMBOL(set_pages_uc);
  953. int set_pages_array_uc(struct page **pages, int addrinarray)
  954. {
  955. unsigned long start;
  956. unsigned long end;
  957. int i;
  958. int free_idx;
  959. for (i = 0; i < addrinarray; i++) {
  960. if (PageHighMem(pages[i]))
  961. continue;
  962. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  963. end = start + PAGE_SIZE;
  964. if (reserve_memtype(start, end, _PAGE_CACHE_UC_MINUS, NULL))
  965. goto err_out;
  966. }
  967. if (cpa_set_pages_array(pages, addrinarray,
  968. __pgprot(_PAGE_CACHE_UC_MINUS)) == 0) {
  969. return 0; /* Success */
  970. }
  971. err_out:
  972. free_idx = i;
  973. for (i = 0; i < free_idx; i++) {
  974. if (PageHighMem(pages[i]))
  975. continue;
  976. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  977. end = start + PAGE_SIZE;
  978. free_memtype(start, end);
  979. }
  980. return -EINVAL;
  981. }
  982. EXPORT_SYMBOL(set_pages_array_uc);
  983. int set_pages_wb(struct page *page, int numpages)
  984. {
  985. unsigned long addr = (unsigned long)page_address(page);
  986. return set_memory_wb(addr, numpages);
  987. }
  988. EXPORT_SYMBOL(set_pages_wb);
  989. int set_pages_array_wb(struct page **pages, int addrinarray)
  990. {
  991. int retval;
  992. unsigned long start;
  993. unsigned long end;
  994. int i;
  995. retval = cpa_clear_pages_array(pages, addrinarray,
  996. __pgprot(_PAGE_CACHE_MASK));
  997. if (retval)
  998. return retval;
  999. for (i = 0; i < addrinarray; i++) {
  1000. if (PageHighMem(pages[i]))
  1001. continue;
  1002. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1003. end = start + PAGE_SIZE;
  1004. free_memtype(start, end);
  1005. }
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL(set_pages_array_wb);
  1009. int set_pages_x(struct page *page, int numpages)
  1010. {
  1011. unsigned long addr = (unsigned long)page_address(page);
  1012. return set_memory_x(addr, numpages);
  1013. }
  1014. EXPORT_SYMBOL(set_pages_x);
  1015. int set_pages_nx(struct page *page, int numpages)
  1016. {
  1017. unsigned long addr = (unsigned long)page_address(page);
  1018. return set_memory_nx(addr, numpages);
  1019. }
  1020. EXPORT_SYMBOL(set_pages_nx);
  1021. int set_pages_ro(struct page *page, int numpages)
  1022. {
  1023. unsigned long addr = (unsigned long)page_address(page);
  1024. return set_memory_ro(addr, numpages);
  1025. }
  1026. int set_pages_rw(struct page *page, int numpages)
  1027. {
  1028. unsigned long addr = (unsigned long)page_address(page);
  1029. return set_memory_rw(addr, numpages);
  1030. }
  1031. #ifdef CONFIG_DEBUG_PAGEALLOC
  1032. static int __set_pages_p(struct page *page, int numpages)
  1033. {
  1034. unsigned long tempaddr = (unsigned long) page_address(page);
  1035. struct cpa_data cpa = { .vaddr = &tempaddr,
  1036. .numpages = numpages,
  1037. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1038. .mask_clr = __pgprot(0),
  1039. .flags = 0};
  1040. /*
  1041. * No alias checking needed for setting present flag. otherwise,
  1042. * we may need to break large pages for 64-bit kernel text
  1043. * mappings (this adds to complexity if we want to do this from
  1044. * atomic context especially). Let's keep it simple!
  1045. */
  1046. return __change_page_attr_set_clr(&cpa, 0);
  1047. }
  1048. static int __set_pages_np(struct page *page, int numpages)
  1049. {
  1050. unsigned long tempaddr = (unsigned long) page_address(page);
  1051. struct cpa_data cpa = { .vaddr = &tempaddr,
  1052. .numpages = numpages,
  1053. .mask_set = __pgprot(0),
  1054. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1055. .flags = 0};
  1056. /*
  1057. * No alias checking needed for setting not present flag. otherwise,
  1058. * we may need to break large pages for 64-bit kernel text
  1059. * mappings (this adds to complexity if we want to do this from
  1060. * atomic context especially). Let's keep it simple!
  1061. */
  1062. return __change_page_attr_set_clr(&cpa, 0);
  1063. }
  1064. void kernel_map_pages(struct page *page, int numpages, int enable)
  1065. {
  1066. if (PageHighMem(page))
  1067. return;
  1068. if (!enable) {
  1069. debug_check_no_locks_freed(page_address(page),
  1070. numpages * PAGE_SIZE);
  1071. }
  1072. /*
  1073. * If page allocator is not up yet then do not call c_p_a():
  1074. */
  1075. if (!debug_pagealloc_enabled)
  1076. return;
  1077. /*
  1078. * The return value is ignored as the calls cannot fail.
  1079. * Large pages for identity mappings are not used at boot time
  1080. * and hence no memory allocations during large page split.
  1081. */
  1082. if (enable)
  1083. __set_pages_p(page, numpages);
  1084. else
  1085. __set_pages_np(page, numpages);
  1086. /*
  1087. * We should perform an IPI and flush all tlbs,
  1088. * but that can deadlock->flush only current cpu:
  1089. */
  1090. __flush_tlb_all();
  1091. }
  1092. #ifdef CONFIG_HIBERNATION
  1093. bool kernel_page_present(struct page *page)
  1094. {
  1095. unsigned int level;
  1096. pte_t *pte;
  1097. if (PageHighMem(page))
  1098. return false;
  1099. pte = lookup_address((unsigned long)page_address(page), &level);
  1100. return (pte_val(*pte) & _PAGE_PRESENT);
  1101. }
  1102. #endif /* CONFIG_HIBERNATION */
  1103. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1104. /*
  1105. * The testcases use internal knowledge of the implementation that shouldn't
  1106. * be exposed to the rest of the kernel. Include these directly here.
  1107. */
  1108. #ifdef CONFIG_CPA_DEBUG
  1109. #include "pageattr-test.c"
  1110. #endif