pageattr.c 28 KB

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