pageattr.c 27 KB

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