pageattr.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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/mm.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/pfn.h>
  14. #include <linux/percpu.h>
  15. #include <linux/gfp.h>
  16. #include <linux/pci.h>
  17. #include <asm/e820.h>
  18. #include <asm/processor.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/sections.h>
  21. #include <asm/setup.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/proto.h>
  25. #include <asm/pat.h>
  26. /*
  27. * The current flushing context - we pass it instead of 5 arguments:
  28. */
  29. struct cpa_data {
  30. unsigned long *vaddr;
  31. pgd_t *pgd;
  32. pgprot_t mask_set;
  33. pgprot_t mask_clr;
  34. int numpages;
  35. int flags;
  36. unsigned long pfn;
  37. unsigned force_split : 1;
  38. int curpage;
  39. struct page **pages;
  40. };
  41. /*
  42. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  43. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  44. * entries change the page attribute in parallel to some other cpu
  45. * splitting a large page entry along with changing the attribute.
  46. */
  47. static DEFINE_SPINLOCK(cpa_lock);
  48. #define CPA_FLUSHTLB 1
  49. #define CPA_ARRAY 2
  50. #define CPA_PAGES_ARRAY 4
  51. #ifdef CONFIG_PROC_FS
  52. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  53. void update_page_count(int level, unsigned long pages)
  54. {
  55. /* Protect against CPA */
  56. spin_lock(&pgd_lock);
  57. direct_pages_count[level] += pages;
  58. spin_unlock(&pgd_lock);
  59. }
  60. static void split_page_count(int level)
  61. {
  62. direct_pages_count[level]--;
  63. direct_pages_count[level - 1] += PTRS_PER_PTE;
  64. }
  65. void arch_report_meminfo(struct seq_file *m)
  66. {
  67. seq_printf(m, "DirectMap4k: %8lu kB\n",
  68. direct_pages_count[PG_LEVEL_4K] << 2);
  69. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  70. seq_printf(m, "DirectMap2M: %8lu kB\n",
  71. direct_pages_count[PG_LEVEL_2M] << 11);
  72. #else
  73. seq_printf(m, "DirectMap4M: %8lu kB\n",
  74. direct_pages_count[PG_LEVEL_2M] << 12);
  75. #endif
  76. #ifdef CONFIG_X86_64
  77. if (direct_gbpages)
  78. seq_printf(m, "DirectMap1G: %8lu kB\n",
  79. direct_pages_count[PG_LEVEL_1G] << 20);
  80. #endif
  81. }
  82. #else
  83. static inline void split_page_count(int level) { }
  84. #endif
  85. #ifdef CONFIG_X86_64
  86. static inline unsigned long highmap_start_pfn(void)
  87. {
  88. return __pa_symbol(_text) >> PAGE_SHIFT;
  89. }
  90. static inline unsigned long highmap_end_pfn(void)
  91. {
  92. return __pa_symbol(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
  93. }
  94. #endif
  95. #ifdef CONFIG_DEBUG_PAGEALLOC
  96. # define debug_pagealloc 1
  97. #else
  98. # define debug_pagealloc 0
  99. #endif
  100. static inline int
  101. within(unsigned long addr, unsigned long start, unsigned long end)
  102. {
  103. return addr >= start && addr < end;
  104. }
  105. /*
  106. * Flushing functions
  107. */
  108. /**
  109. * clflush_cache_range - flush a cache range with clflush
  110. * @vaddr: virtual start address
  111. * @size: number of bytes to flush
  112. *
  113. * clflushopt is an unordered instruction which needs fencing with mfence or
  114. * sfence to avoid ordering issues.
  115. */
  116. void clflush_cache_range(void *vaddr, unsigned int size)
  117. {
  118. void *vend = vaddr + size - 1;
  119. mb();
  120. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  121. clflushopt(vaddr);
  122. /*
  123. * Flush any possible final partial cacheline:
  124. */
  125. clflushopt(vend);
  126. mb();
  127. }
  128. EXPORT_SYMBOL_GPL(clflush_cache_range);
  129. static void __cpa_flush_all(void *arg)
  130. {
  131. unsigned long cache = (unsigned long)arg;
  132. /*
  133. * Flush all to work around Errata in early athlons regarding
  134. * large page flushing.
  135. */
  136. __flush_tlb_all();
  137. if (cache && boot_cpu_data.x86 >= 4)
  138. wbinvd();
  139. }
  140. static void cpa_flush_all(unsigned long cache)
  141. {
  142. BUG_ON(irqs_disabled());
  143. on_each_cpu(__cpa_flush_all, (void *) cache, 1);
  144. }
  145. static void __cpa_flush_range(void *arg)
  146. {
  147. /*
  148. * We could optimize that further and do individual per page
  149. * tlb invalidates for a low number of pages. Caveat: we must
  150. * flush the high aliases on 64bit as well.
  151. */
  152. __flush_tlb_all();
  153. }
  154. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  155. {
  156. unsigned int i, level;
  157. unsigned long addr;
  158. BUG_ON(irqs_disabled());
  159. WARN_ON(PAGE_ALIGN(start) != start);
  160. on_each_cpu(__cpa_flush_range, NULL, 1);
  161. if (!cache)
  162. return;
  163. /*
  164. * We only need to flush on one CPU,
  165. * clflush is a MESI-coherent instruction that
  166. * will cause all other CPUs to flush the same
  167. * cachelines:
  168. */
  169. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  170. pte_t *pte = lookup_address(addr, &level);
  171. /*
  172. * Only flush present addresses:
  173. */
  174. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  175. clflush_cache_range((void *) addr, PAGE_SIZE);
  176. }
  177. }
  178. static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  179. int in_flags, struct page **pages)
  180. {
  181. unsigned int i, level;
  182. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  183. BUG_ON(irqs_disabled());
  184. on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
  185. if (!cache || do_wbinvd)
  186. return;
  187. /*
  188. * We only need to flush on one CPU,
  189. * clflush is a MESI-coherent instruction that
  190. * will cause all other CPUs to flush the same
  191. * cachelines:
  192. */
  193. for (i = 0; i < numpages; i++) {
  194. unsigned long addr;
  195. pte_t *pte;
  196. if (in_flags & CPA_PAGES_ARRAY)
  197. addr = (unsigned long)page_address(pages[i]);
  198. else
  199. addr = start[i];
  200. pte = lookup_address(addr, &level);
  201. /*
  202. * Only flush present addresses:
  203. */
  204. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  205. clflush_cache_range((void *)addr, PAGE_SIZE);
  206. }
  207. }
  208. /*
  209. * Certain areas of memory on x86 require very specific protection flags,
  210. * for example the BIOS area or kernel text. Callers don't always get this
  211. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  212. * checks and fixes these known static required protection bits.
  213. */
  214. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  215. unsigned long pfn)
  216. {
  217. pgprot_t forbidden = __pgprot(0);
  218. /*
  219. * The BIOS area between 640k and 1Mb needs to be executable for
  220. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  221. */
  222. #ifdef CONFIG_PCI_BIOS
  223. if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  224. pgprot_val(forbidden) |= _PAGE_NX;
  225. #endif
  226. /*
  227. * The kernel text needs to be executable for obvious reasons
  228. * Does not cover __inittext since that is gone later on. On
  229. * 64bit we do not enforce !NX on the low mapping
  230. */
  231. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  232. pgprot_val(forbidden) |= _PAGE_NX;
  233. /*
  234. * The .rodata section needs to be read-only. Using the pfn
  235. * catches all aliases.
  236. */
  237. if (within(pfn, __pa_symbol(__start_rodata) >> PAGE_SHIFT,
  238. __pa_symbol(__end_rodata) >> PAGE_SHIFT))
  239. pgprot_val(forbidden) |= _PAGE_RW;
  240. #if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
  241. /*
  242. * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
  243. * kernel text mappings for the large page aligned text, rodata sections
  244. * will be always read-only. For the kernel identity mappings covering
  245. * the holes caused by this alignment can be anything that user asks.
  246. *
  247. * This will preserve the large page mappings for kernel text/data
  248. * at no extra cost.
  249. */
  250. if (kernel_set_to_readonly &&
  251. within(address, (unsigned long)_text,
  252. (unsigned long)__end_rodata_hpage_align)) {
  253. unsigned int level;
  254. /*
  255. * Don't enforce the !RW mapping for the kernel text mapping,
  256. * if the current mapping is already using small page mapping.
  257. * No need to work hard to preserve large page mappings in this
  258. * case.
  259. *
  260. * This also fixes the Linux Xen paravirt guest boot failure
  261. * (because of unexpected read-only mappings for kernel identity
  262. * mappings). In this paravirt guest case, the kernel text
  263. * mapping and the kernel identity mapping share the same
  264. * page-table pages. Thus we can't really use different
  265. * protections for the kernel text and identity mappings. Also,
  266. * these shared mappings are made of small page mappings.
  267. * Thus this don't enforce !RW mapping for small page kernel
  268. * text mapping logic will help Linux Xen parvirt guest boot
  269. * as well.
  270. */
  271. if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
  272. pgprot_val(forbidden) |= _PAGE_RW;
  273. }
  274. #endif
  275. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  276. return prot;
  277. }
  278. /*
  279. * Lookup the page table entry for a virtual address in a specific pgd.
  280. * Return a pointer to the entry and the level of the mapping.
  281. */
  282. pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
  283. unsigned int *level)
  284. {
  285. pud_t *pud;
  286. pmd_t *pmd;
  287. *level = PG_LEVEL_NONE;
  288. if (pgd_none(*pgd))
  289. return NULL;
  290. pud = pud_offset(pgd, address);
  291. if (pud_none(*pud))
  292. return NULL;
  293. *level = PG_LEVEL_1G;
  294. if (pud_large(*pud) || !pud_present(*pud))
  295. return (pte_t *)pud;
  296. pmd = pmd_offset(pud, address);
  297. if (pmd_none(*pmd))
  298. return NULL;
  299. *level = PG_LEVEL_2M;
  300. if (pmd_large(*pmd) || !pmd_present(*pmd))
  301. return (pte_t *)pmd;
  302. *level = PG_LEVEL_4K;
  303. return pte_offset_kernel(pmd, address);
  304. }
  305. /*
  306. * Lookup the page table entry for a virtual address. Return a pointer
  307. * to the entry and the level of the mapping.
  308. *
  309. * Note: We return pud and pmd either when the entry is marked large
  310. * or when the present bit is not set. Otherwise we would return a
  311. * pointer to a nonexisting mapping.
  312. */
  313. pte_t *lookup_address(unsigned long address, unsigned int *level)
  314. {
  315. return lookup_address_in_pgd(pgd_offset_k(address), address, level);
  316. }
  317. EXPORT_SYMBOL_GPL(lookup_address);
  318. static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
  319. unsigned int *level)
  320. {
  321. if (cpa->pgd)
  322. return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
  323. address, level);
  324. return lookup_address(address, level);
  325. }
  326. /*
  327. * This is necessary because __pa() does not work on some
  328. * kinds of memory, like vmalloc() or the alloc_remap()
  329. * areas on 32-bit NUMA systems. The percpu areas can
  330. * end up in this kind of memory, for instance.
  331. *
  332. * This could be optimized, but it is only intended to be
  333. * used at inititalization time, and keeping it
  334. * unoptimized should increase the testing coverage for
  335. * the more obscure platforms.
  336. */
  337. phys_addr_t slow_virt_to_phys(void *__virt_addr)
  338. {
  339. unsigned long virt_addr = (unsigned long)__virt_addr;
  340. phys_addr_t phys_addr;
  341. unsigned long offset;
  342. enum pg_level level;
  343. unsigned long psize;
  344. unsigned long pmask;
  345. pte_t *pte;
  346. pte = lookup_address(virt_addr, &level);
  347. BUG_ON(!pte);
  348. psize = page_level_size(level);
  349. pmask = page_level_mask(level);
  350. offset = virt_addr & ~pmask;
  351. phys_addr = pte_pfn(*pte) << PAGE_SHIFT;
  352. return (phys_addr | offset);
  353. }
  354. EXPORT_SYMBOL_GPL(slow_virt_to_phys);
  355. /*
  356. * Set the new pmd in all the pgds we know about:
  357. */
  358. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  359. {
  360. /* change init_mm */
  361. set_pte_atomic(kpte, pte);
  362. #ifdef CONFIG_X86_32
  363. if (!SHARED_KERNEL_PMD) {
  364. struct page *page;
  365. list_for_each_entry(page, &pgd_list, lru) {
  366. pgd_t *pgd;
  367. pud_t *pud;
  368. pmd_t *pmd;
  369. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  370. pud = pud_offset(pgd, address);
  371. pmd = pmd_offset(pud, address);
  372. set_pte_atomic((pte_t *)pmd, pte);
  373. }
  374. }
  375. #endif
  376. }
  377. static int
  378. try_preserve_large_page(pte_t *kpte, unsigned long address,
  379. struct cpa_data *cpa)
  380. {
  381. unsigned long nextpage_addr, numpages, pmask, psize, addr, pfn;
  382. pte_t new_pte, old_pte, *tmp;
  383. pgprot_t old_prot, new_prot, req_prot;
  384. int i, do_split = 1;
  385. enum pg_level level;
  386. if (cpa->force_split)
  387. return 1;
  388. spin_lock(&pgd_lock);
  389. /*
  390. * Check for races, another CPU might have split this page
  391. * up already:
  392. */
  393. tmp = _lookup_address_cpa(cpa, address, &level);
  394. if (tmp != kpte)
  395. goto out_unlock;
  396. switch (level) {
  397. case PG_LEVEL_2M:
  398. #ifdef CONFIG_X86_64
  399. case PG_LEVEL_1G:
  400. #endif
  401. psize = page_level_size(level);
  402. pmask = page_level_mask(level);
  403. break;
  404. default:
  405. do_split = -EINVAL;
  406. goto out_unlock;
  407. }
  408. /*
  409. * Calculate the number of pages, which fit into this large
  410. * page starting at address:
  411. */
  412. nextpage_addr = (address + psize) & pmask;
  413. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  414. if (numpages < cpa->numpages)
  415. cpa->numpages = numpages;
  416. /*
  417. * We are safe now. Check whether the new pgprot is the same:
  418. */
  419. old_pte = *kpte;
  420. old_prot = req_prot = pte_pgprot(old_pte);
  421. pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
  422. pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
  423. /*
  424. * Set the PSE and GLOBAL flags only if the PRESENT flag is
  425. * set otherwise pmd_present/pmd_huge will return true even on
  426. * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
  427. * for the ancient hardware that doesn't support it.
  428. */
  429. if (pgprot_val(req_prot) & _PAGE_PRESENT)
  430. pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
  431. else
  432. pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
  433. req_prot = canon_pgprot(req_prot);
  434. /*
  435. * old_pte points to the large page base address. So we need
  436. * to add the offset of the virtual address:
  437. */
  438. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  439. cpa->pfn = pfn;
  440. new_prot = static_protections(req_prot, address, pfn);
  441. /*
  442. * We need to check the full range, whether
  443. * static_protection() requires a different pgprot for one of
  444. * the pages in the range we try to preserve:
  445. */
  446. addr = address & pmask;
  447. pfn = pte_pfn(old_pte);
  448. for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
  449. pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
  450. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  451. goto out_unlock;
  452. }
  453. /*
  454. * If there are no changes, return. maxpages has been updated
  455. * above:
  456. */
  457. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  458. do_split = 0;
  459. goto out_unlock;
  460. }
  461. /*
  462. * We need to change the attributes. Check, whether we can
  463. * change the large page in one go. We request a split, when
  464. * the address is not aligned and the number of pages is
  465. * smaller than the number of pages in the large page. Note
  466. * that we limited the number of possible pages already to
  467. * the number of pages in the large page.
  468. */
  469. if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
  470. /*
  471. * The address is aligned and the number of pages
  472. * covers the full page.
  473. */
  474. new_pte = pfn_pte(pte_pfn(old_pte), new_prot);
  475. __set_pmd_pte(kpte, address, new_pte);
  476. cpa->flags |= CPA_FLUSHTLB;
  477. do_split = 0;
  478. }
  479. out_unlock:
  480. spin_unlock(&pgd_lock);
  481. return do_split;
  482. }
  483. static int
  484. __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
  485. struct page *base)
  486. {
  487. pte_t *pbase = (pte_t *)page_address(base);
  488. unsigned long pfn, pfninc = 1;
  489. unsigned int i, level;
  490. pte_t *tmp;
  491. pgprot_t ref_prot;
  492. spin_lock(&pgd_lock);
  493. /*
  494. * Check for races, another CPU might have split this page
  495. * up for us already:
  496. */
  497. tmp = _lookup_address_cpa(cpa, address, &level);
  498. if (tmp != kpte) {
  499. spin_unlock(&pgd_lock);
  500. return 1;
  501. }
  502. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  503. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  504. /*
  505. * If we ever want to utilize the PAT bit, we need to
  506. * update this function to make sure it's converted from
  507. * bit 12 to bit 7 when we cross from the 2MB level to
  508. * the 4K level:
  509. */
  510. WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
  511. #ifdef CONFIG_X86_64
  512. if (level == PG_LEVEL_1G) {
  513. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  514. /*
  515. * Set the PSE flags only if the PRESENT flag is set
  516. * otherwise pmd_present/pmd_huge will return true
  517. * even on a non present pmd.
  518. */
  519. if (pgprot_val(ref_prot) & _PAGE_PRESENT)
  520. pgprot_val(ref_prot) |= _PAGE_PSE;
  521. else
  522. pgprot_val(ref_prot) &= ~_PAGE_PSE;
  523. }
  524. #endif
  525. /*
  526. * Set the GLOBAL flags only if the PRESENT flag is set
  527. * otherwise pmd/pte_present will return true even on a non
  528. * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
  529. * for the ancient hardware that doesn't support it.
  530. */
  531. if (pgprot_val(ref_prot) & _PAGE_PRESENT)
  532. pgprot_val(ref_prot) |= _PAGE_GLOBAL;
  533. else
  534. pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
  535. /*
  536. * Get the target pfn from the original entry:
  537. */
  538. pfn = pte_pfn(*kpte);
  539. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  540. set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
  541. if (pfn_range_is_mapped(PFN_DOWN(__pa(address)),
  542. PFN_DOWN(__pa(address)) + 1))
  543. split_page_count(level);
  544. /*
  545. * Install the new, split up pagetable.
  546. *
  547. * We use the standard kernel pagetable protections for the new
  548. * pagetable protections, the actual ptes set above control the
  549. * primary protection behavior:
  550. */
  551. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  552. /*
  553. * Intel Atom errata AAH41 workaround.
  554. *
  555. * The real fix should be in hw or in a microcode update, but
  556. * we also probabilistically try to reduce the window of having
  557. * a large TLB mixed with 4K TLBs while instruction fetches are
  558. * going on.
  559. */
  560. __flush_tlb_all();
  561. spin_unlock(&pgd_lock);
  562. return 0;
  563. }
  564. static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
  565. unsigned long address)
  566. {
  567. struct page *base;
  568. if (!debug_pagealloc)
  569. spin_unlock(&cpa_lock);
  570. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  571. if (!debug_pagealloc)
  572. spin_lock(&cpa_lock);
  573. if (!base)
  574. return -ENOMEM;
  575. if (__split_large_page(cpa, kpte, address, base))
  576. __free_page(base);
  577. return 0;
  578. }
  579. static bool try_to_free_pte_page(pte_t *pte)
  580. {
  581. int i;
  582. for (i = 0; i < PTRS_PER_PTE; i++)
  583. if (!pte_none(pte[i]))
  584. return false;
  585. free_page((unsigned long)pte);
  586. return true;
  587. }
  588. static bool try_to_free_pmd_page(pmd_t *pmd)
  589. {
  590. int i;
  591. for (i = 0; i < PTRS_PER_PMD; i++)
  592. if (!pmd_none(pmd[i]))
  593. return false;
  594. free_page((unsigned long)pmd);
  595. return true;
  596. }
  597. static bool try_to_free_pud_page(pud_t *pud)
  598. {
  599. int i;
  600. for (i = 0; i < PTRS_PER_PUD; i++)
  601. if (!pud_none(pud[i]))
  602. return false;
  603. free_page((unsigned long)pud);
  604. return true;
  605. }
  606. static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
  607. {
  608. pte_t *pte = pte_offset_kernel(pmd, start);
  609. while (start < end) {
  610. set_pte(pte, __pte(0));
  611. start += PAGE_SIZE;
  612. pte++;
  613. }
  614. if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
  615. pmd_clear(pmd);
  616. return true;
  617. }
  618. return false;
  619. }
  620. static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
  621. unsigned long start, unsigned long end)
  622. {
  623. if (unmap_pte_range(pmd, start, end))
  624. if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
  625. pud_clear(pud);
  626. }
  627. static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
  628. {
  629. pmd_t *pmd = pmd_offset(pud, start);
  630. /*
  631. * Not on a 2MB page boundary?
  632. */
  633. if (start & (PMD_SIZE - 1)) {
  634. unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
  635. unsigned long pre_end = min_t(unsigned long, end, next_page);
  636. __unmap_pmd_range(pud, pmd, start, pre_end);
  637. start = pre_end;
  638. pmd++;
  639. }
  640. /*
  641. * Try to unmap in 2M chunks.
  642. */
  643. while (end - start >= PMD_SIZE) {
  644. if (pmd_large(*pmd))
  645. pmd_clear(pmd);
  646. else
  647. __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
  648. start += PMD_SIZE;
  649. pmd++;
  650. }
  651. /*
  652. * 4K leftovers?
  653. */
  654. if (start < end)
  655. return __unmap_pmd_range(pud, pmd, start, end);
  656. /*
  657. * Try again to free the PMD page if haven't succeeded above.
  658. */
  659. if (!pud_none(*pud))
  660. if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
  661. pud_clear(pud);
  662. }
  663. static void unmap_pud_range(pgd_t *pgd, unsigned long start, unsigned long end)
  664. {
  665. pud_t *pud = pud_offset(pgd, start);
  666. /*
  667. * Not on a GB page boundary?
  668. */
  669. if (start & (PUD_SIZE - 1)) {
  670. unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
  671. unsigned long pre_end = min_t(unsigned long, end, next_page);
  672. unmap_pmd_range(pud, start, pre_end);
  673. start = pre_end;
  674. pud++;
  675. }
  676. /*
  677. * Try to unmap in 1G chunks?
  678. */
  679. while (end - start >= PUD_SIZE) {
  680. if (pud_large(*pud))
  681. pud_clear(pud);
  682. else
  683. unmap_pmd_range(pud, start, start + PUD_SIZE);
  684. start += PUD_SIZE;
  685. pud++;
  686. }
  687. /*
  688. * 2M leftovers?
  689. */
  690. if (start < end)
  691. unmap_pmd_range(pud, start, end);
  692. /*
  693. * No need to try to free the PUD page because we'll free it in
  694. * populate_pgd's error path
  695. */
  696. }
  697. static void unmap_pgd_range(pgd_t *root, unsigned long addr, unsigned long end)
  698. {
  699. pgd_t *pgd_entry = root + pgd_index(addr);
  700. unmap_pud_range(pgd_entry, addr, end);
  701. if (try_to_free_pud_page((pud_t *)pgd_page_vaddr(*pgd_entry)))
  702. pgd_clear(pgd_entry);
  703. }
  704. static int alloc_pte_page(pmd_t *pmd)
  705. {
  706. pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  707. if (!pte)
  708. return -1;
  709. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
  710. return 0;
  711. }
  712. static int alloc_pmd_page(pud_t *pud)
  713. {
  714. pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  715. if (!pmd)
  716. return -1;
  717. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  718. return 0;
  719. }
  720. static void populate_pte(struct cpa_data *cpa,
  721. unsigned long start, unsigned long end,
  722. unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
  723. {
  724. pte_t *pte;
  725. pte = pte_offset_kernel(pmd, start);
  726. while (num_pages-- && start < end) {
  727. /* deal with the NX bit */
  728. if (!(pgprot_val(pgprot) & _PAGE_NX))
  729. cpa->pfn &= ~_PAGE_NX;
  730. set_pte(pte, pfn_pte(cpa->pfn >> PAGE_SHIFT, pgprot));
  731. start += PAGE_SIZE;
  732. cpa->pfn += PAGE_SIZE;
  733. pte++;
  734. }
  735. }
  736. static int populate_pmd(struct cpa_data *cpa,
  737. unsigned long start, unsigned long end,
  738. unsigned num_pages, pud_t *pud, pgprot_t pgprot)
  739. {
  740. unsigned int cur_pages = 0;
  741. pmd_t *pmd;
  742. /*
  743. * Not on a 2M boundary?
  744. */
  745. if (start & (PMD_SIZE - 1)) {
  746. unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
  747. unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
  748. pre_end = min_t(unsigned long, pre_end, next_page);
  749. cur_pages = (pre_end - start) >> PAGE_SHIFT;
  750. cur_pages = min_t(unsigned int, num_pages, cur_pages);
  751. /*
  752. * Need a PTE page?
  753. */
  754. pmd = pmd_offset(pud, start);
  755. if (pmd_none(*pmd))
  756. if (alloc_pte_page(pmd))
  757. return -1;
  758. populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
  759. start = pre_end;
  760. }
  761. /*
  762. * We mapped them all?
  763. */
  764. if (num_pages == cur_pages)
  765. return cur_pages;
  766. while (end - start >= PMD_SIZE) {
  767. /*
  768. * We cannot use a 1G page so allocate a PMD page if needed.
  769. */
  770. if (pud_none(*pud))
  771. if (alloc_pmd_page(pud))
  772. return -1;
  773. pmd = pmd_offset(pud, start);
  774. set_pmd(pmd, __pmd(cpa->pfn | _PAGE_PSE | massage_pgprot(pgprot)));
  775. start += PMD_SIZE;
  776. cpa->pfn += PMD_SIZE;
  777. cur_pages += PMD_SIZE >> PAGE_SHIFT;
  778. }
  779. /*
  780. * Map trailing 4K pages.
  781. */
  782. if (start < end) {
  783. pmd = pmd_offset(pud, start);
  784. if (pmd_none(*pmd))
  785. if (alloc_pte_page(pmd))
  786. return -1;
  787. populate_pte(cpa, start, end, num_pages - cur_pages,
  788. pmd, pgprot);
  789. }
  790. return num_pages;
  791. }
  792. static int populate_pud(struct cpa_data *cpa, unsigned long start, pgd_t *pgd,
  793. pgprot_t pgprot)
  794. {
  795. pud_t *pud;
  796. unsigned long end;
  797. int cur_pages = 0;
  798. end = start + (cpa->numpages << PAGE_SHIFT);
  799. /*
  800. * Not on a Gb page boundary? => map everything up to it with
  801. * smaller pages.
  802. */
  803. if (start & (PUD_SIZE - 1)) {
  804. unsigned long pre_end;
  805. unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
  806. pre_end = min_t(unsigned long, end, next_page);
  807. cur_pages = (pre_end - start) >> PAGE_SHIFT;
  808. cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
  809. pud = pud_offset(pgd, start);
  810. /*
  811. * Need a PMD page?
  812. */
  813. if (pud_none(*pud))
  814. if (alloc_pmd_page(pud))
  815. return -1;
  816. cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
  817. pud, pgprot);
  818. if (cur_pages < 0)
  819. return cur_pages;
  820. start = pre_end;
  821. }
  822. /* We mapped them all? */
  823. if (cpa->numpages == cur_pages)
  824. return cur_pages;
  825. pud = pud_offset(pgd, start);
  826. /*
  827. * Map everything starting from the Gb boundary, possibly with 1G pages
  828. */
  829. while (end - start >= PUD_SIZE) {
  830. set_pud(pud, __pud(cpa->pfn | _PAGE_PSE | massage_pgprot(pgprot)));
  831. start += PUD_SIZE;
  832. cpa->pfn += PUD_SIZE;
  833. cur_pages += PUD_SIZE >> PAGE_SHIFT;
  834. pud++;
  835. }
  836. /* Map trailing leftover */
  837. if (start < end) {
  838. int tmp;
  839. pud = pud_offset(pgd, start);
  840. if (pud_none(*pud))
  841. if (alloc_pmd_page(pud))
  842. return -1;
  843. tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
  844. pud, pgprot);
  845. if (tmp < 0)
  846. return cur_pages;
  847. cur_pages += tmp;
  848. }
  849. return cur_pages;
  850. }
  851. /*
  852. * Restrictions for kernel page table do not necessarily apply when mapping in
  853. * an alternate PGD.
  854. */
  855. static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
  856. {
  857. pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
  858. pud_t *pud = NULL; /* shut up gcc */
  859. pgd_t *pgd_entry;
  860. int ret;
  861. pgd_entry = cpa->pgd + pgd_index(addr);
  862. /*
  863. * Allocate a PUD page and hand it down for mapping.
  864. */
  865. if (pgd_none(*pgd_entry)) {
  866. pud = (pud_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  867. if (!pud)
  868. return -1;
  869. set_pgd(pgd_entry, __pgd(__pa(pud) | _KERNPG_TABLE));
  870. }
  871. pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
  872. pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
  873. ret = populate_pud(cpa, addr, pgd_entry, pgprot);
  874. if (ret < 0) {
  875. unmap_pgd_range(cpa->pgd, addr,
  876. addr + (cpa->numpages << PAGE_SHIFT));
  877. return ret;
  878. }
  879. cpa->numpages = ret;
  880. return 0;
  881. }
  882. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  883. int primary)
  884. {
  885. if (cpa->pgd)
  886. return populate_pgd(cpa, vaddr);
  887. /*
  888. * Ignore all non primary paths.
  889. */
  890. if (!primary)
  891. return 0;
  892. /*
  893. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  894. * to have holes.
  895. * Also set numpages to '1' indicating that we processed cpa req for
  896. * one virtual address page and its pfn. TBD: numpages can be set based
  897. * on the initial value and the level returned by lookup_address().
  898. */
  899. if (within(vaddr, PAGE_OFFSET,
  900. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  901. cpa->numpages = 1;
  902. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  903. return 0;
  904. } else {
  905. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  906. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  907. *cpa->vaddr);
  908. return -EFAULT;
  909. }
  910. }
  911. static int __change_page_attr(struct cpa_data *cpa, int primary)
  912. {
  913. unsigned long address;
  914. int do_split, err;
  915. unsigned int level;
  916. pte_t *kpte, old_pte;
  917. if (cpa->flags & CPA_PAGES_ARRAY) {
  918. struct page *page = cpa->pages[cpa->curpage];
  919. if (unlikely(PageHighMem(page)))
  920. return 0;
  921. address = (unsigned long)page_address(page);
  922. } else if (cpa->flags & CPA_ARRAY)
  923. address = cpa->vaddr[cpa->curpage];
  924. else
  925. address = *cpa->vaddr;
  926. repeat:
  927. kpte = _lookup_address_cpa(cpa, address, &level);
  928. if (!kpte)
  929. return __cpa_process_fault(cpa, address, primary);
  930. old_pte = *kpte;
  931. if (!pte_val(old_pte))
  932. return __cpa_process_fault(cpa, address, primary);
  933. if (level == PG_LEVEL_4K) {
  934. pte_t new_pte;
  935. pgprot_t new_prot = pte_pgprot(old_pte);
  936. unsigned long pfn = pte_pfn(old_pte);
  937. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  938. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  939. new_prot = static_protections(new_prot, address, pfn);
  940. /*
  941. * Set the GLOBAL flags only if the PRESENT flag is
  942. * set otherwise pte_present will return true even on
  943. * a non present pte. The canon_pgprot will clear
  944. * _PAGE_GLOBAL for the ancient hardware that doesn't
  945. * support it.
  946. */
  947. if (pgprot_val(new_prot) & _PAGE_PRESENT)
  948. pgprot_val(new_prot) |= _PAGE_GLOBAL;
  949. else
  950. pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
  951. /*
  952. * We need to keep the pfn from the existing PTE,
  953. * after all we're only going to change it's attributes
  954. * not the memory it points to
  955. */
  956. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  957. cpa->pfn = pfn;
  958. /*
  959. * Do we really change anything ?
  960. */
  961. if (pte_val(old_pte) != pte_val(new_pte)) {
  962. set_pte_atomic(kpte, new_pte);
  963. cpa->flags |= CPA_FLUSHTLB;
  964. }
  965. cpa->numpages = 1;
  966. return 0;
  967. }
  968. /*
  969. * Check, whether we can keep the large page intact
  970. * and just change the pte:
  971. */
  972. do_split = try_preserve_large_page(kpte, address, cpa);
  973. /*
  974. * When the range fits into the existing large page,
  975. * return. cp->numpages and cpa->tlbflush have been updated in
  976. * try_large_page:
  977. */
  978. if (do_split <= 0)
  979. return do_split;
  980. /*
  981. * We have to split the large page:
  982. */
  983. err = split_large_page(cpa, kpte, address);
  984. if (!err) {
  985. /*
  986. * Do a global flush tlb after splitting the large page
  987. * and before we do the actual change page attribute in the PTE.
  988. *
  989. * With out this, we violate the TLB application note, that says
  990. * "The TLBs may contain both ordinary and large-page
  991. * translations for a 4-KByte range of linear addresses. This
  992. * may occur if software modifies the paging structures so that
  993. * the page size used for the address range changes. If the two
  994. * translations differ with respect to page frame or attributes
  995. * (e.g., permissions), processor behavior is undefined and may
  996. * be implementation-specific."
  997. *
  998. * We do this global tlb flush inside the cpa_lock, so that we
  999. * don't allow any other cpu, with stale tlb entries change the
  1000. * page attribute in parallel, that also falls into the
  1001. * just split large page entry.
  1002. */
  1003. flush_tlb_all();
  1004. goto repeat;
  1005. }
  1006. return err;
  1007. }
  1008. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  1009. static int cpa_process_alias(struct cpa_data *cpa)
  1010. {
  1011. struct cpa_data alias_cpa;
  1012. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  1013. unsigned long vaddr;
  1014. int ret;
  1015. if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
  1016. return 0;
  1017. /*
  1018. * No need to redo, when the primary call touched the direct
  1019. * mapping already:
  1020. */
  1021. if (cpa->flags & CPA_PAGES_ARRAY) {
  1022. struct page *page = cpa->pages[cpa->curpage];
  1023. if (unlikely(PageHighMem(page)))
  1024. return 0;
  1025. vaddr = (unsigned long)page_address(page);
  1026. } else if (cpa->flags & CPA_ARRAY)
  1027. vaddr = cpa->vaddr[cpa->curpage];
  1028. else
  1029. vaddr = *cpa->vaddr;
  1030. if (!(within(vaddr, PAGE_OFFSET,
  1031. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  1032. alias_cpa = *cpa;
  1033. alias_cpa.vaddr = &laddr;
  1034. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  1035. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  1036. if (ret)
  1037. return ret;
  1038. }
  1039. #ifdef CONFIG_X86_64
  1040. /*
  1041. * If the primary call didn't touch the high mapping already
  1042. * and the physical address is inside the kernel map, we need
  1043. * to touch the high mapped kernel as well:
  1044. */
  1045. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  1046. within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
  1047. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  1048. __START_KERNEL_map - phys_base;
  1049. alias_cpa = *cpa;
  1050. alias_cpa.vaddr = &temp_cpa_vaddr;
  1051. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  1052. /*
  1053. * The high mapping range is imprecise, so ignore the
  1054. * return value.
  1055. */
  1056. __change_page_attr_set_clr(&alias_cpa, 0);
  1057. }
  1058. #endif
  1059. return 0;
  1060. }
  1061. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  1062. {
  1063. int ret, numpages = cpa->numpages;
  1064. while (numpages) {
  1065. /*
  1066. * Store the remaining nr of pages for the large page
  1067. * preservation check.
  1068. */
  1069. cpa->numpages = numpages;
  1070. /* for array changes, we can't use large page */
  1071. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  1072. cpa->numpages = 1;
  1073. if (!debug_pagealloc)
  1074. spin_lock(&cpa_lock);
  1075. ret = __change_page_attr(cpa, checkalias);
  1076. if (!debug_pagealloc)
  1077. spin_unlock(&cpa_lock);
  1078. if (ret)
  1079. return ret;
  1080. if (checkalias) {
  1081. ret = cpa_process_alias(cpa);
  1082. if (ret)
  1083. return ret;
  1084. }
  1085. /*
  1086. * Adjust the number of pages with the result of the
  1087. * CPA operation. Either a large page has been
  1088. * preserved or a single page update happened.
  1089. */
  1090. BUG_ON(cpa->numpages > numpages);
  1091. numpages -= cpa->numpages;
  1092. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  1093. cpa->curpage++;
  1094. else
  1095. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  1096. }
  1097. return 0;
  1098. }
  1099. static inline int cache_attr(pgprot_t attr)
  1100. {
  1101. return pgprot_val(attr) &
  1102. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  1103. }
  1104. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  1105. pgprot_t mask_set, pgprot_t mask_clr,
  1106. int force_split, int in_flag,
  1107. struct page **pages)
  1108. {
  1109. struct cpa_data cpa;
  1110. int ret, cache, checkalias;
  1111. unsigned long baddr = 0;
  1112. memset(&cpa, 0, sizeof(cpa));
  1113. /*
  1114. * Check, if we are requested to change a not supported
  1115. * feature:
  1116. */
  1117. mask_set = canon_pgprot(mask_set);
  1118. mask_clr = canon_pgprot(mask_clr);
  1119. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  1120. return 0;
  1121. /* Ensure we are PAGE_SIZE aligned */
  1122. if (in_flag & CPA_ARRAY) {
  1123. int i;
  1124. for (i = 0; i < numpages; i++) {
  1125. if (addr[i] & ~PAGE_MASK) {
  1126. addr[i] &= PAGE_MASK;
  1127. WARN_ON_ONCE(1);
  1128. }
  1129. }
  1130. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  1131. /*
  1132. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  1133. * No need to cehck in that case
  1134. */
  1135. if (*addr & ~PAGE_MASK) {
  1136. *addr &= PAGE_MASK;
  1137. /*
  1138. * People should not be passing in unaligned addresses:
  1139. */
  1140. WARN_ON_ONCE(1);
  1141. }
  1142. /*
  1143. * Save address for cache flush. *addr is modified in the call
  1144. * to __change_page_attr_set_clr() below.
  1145. */
  1146. baddr = *addr;
  1147. }
  1148. /* Must avoid aliasing mappings in the highmem code */
  1149. kmap_flush_unused();
  1150. vm_unmap_aliases();
  1151. cpa.vaddr = addr;
  1152. cpa.pages = pages;
  1153. cpa.numpages = numpages;
  1154. cpa.mask_set = mask_set;
  1155. cpa.mask_clr = mask_clr;
  1156. cpa.flags = 0;
  1157. cpa.curpage = 0;
  1158. cpa.force_split = force_split;
  1159. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  1160. cpa.flags |= in_flag;
  1161. /* No alias checking for _NX bit modifications */
  1162. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  1163. ret = __change_page_attr_set_clr(&cpa, checkalias);
  1164. /*
  1165. * Check whether we really changed something:
  1166. */
  1167. if (!(cpa.flags & CPA_FLUSHTLB))
  1168. goto out;
  1169. /*
  1170. * No need to flush, when we did not set any of the caching
  1171. * attributes:
  1172. */
  1173. cache = cache_attr(mask_set);
  1174. /*
  1175. * On success we use CLFLUSH, when the CPU supports it to
  1176. * avoid the WBINVD. If the CPU does not support it and in the
  1177. * error case we fall back to cpa_flush_all (which uses
  1178. * WBINVD):
  1179. */
  1180. if (!ret && cpu_has_clflush) {
  1181. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  1182. cpa_flush_array(addr, numpages, cache,
  1183. cpa.flags, pages);
  1184. } else
  1185. cpa_flush_range(baddr, numpages, cache);
  1186. } else
  1187. cpa_flush_all(cache);
  1188. out:
  1189. return ret;
  1190. }
  1191. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  1192. pgprot_t mask, int array)
  1193. {
  1194. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  1195. (array ? CPA_ARRAY : 0), NULL);
  1196. }
  1197. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  1198. pgprot_t mask, int array)
  1199. {
  1200. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  1201. (array ? CPA_ARRAY : 0), NULL);
  1202. }
  1203. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  1204. pgprot_t mask)
  1205. {
  1206. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  1207. CPA_PAGES_ARRAY, pages);
  1208. }
  1209. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  1210. pgprot_t mask)
  1211. {
  1212. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  1213. CPA_PAGES_ARRAY, pages);
  1214. }
  1215. int _set_memory_uc(unsigned long addr, int numpages)
  1216. {
  1217. /*
  1218. * for now UC MINUS. see comments in ioremap_nocache()
  1219. */
  1220. return change_page_attr_set(&addr, numpages,
  1221. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  1222. }
  1223. int set_memory_uc(unsigned long addr, int numpages)
  1224. {
  1225. int ret;
  1226. /*
  1227. * for now UC MINUS. see comments in ioremap_nocache()
  1228. */
  1229. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1230. _PAGE_CACHE_UC_MINUS, NULL);
  1231. if (ret)
  1232. goto out_err;
  1233. ret = _set_memory_uc(addr, numpages);
  1234. if (ret)
  1235. goto out_free;
  1236. return 0;
  1237. out_free:
  1238. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1239. out_err:
  1240. return ret;
  1241. }
  1242. EXPORT_SYMBOL(set_memory_uc);
  1243. static int _set_memory_array(unsigned long *addr, int addrinarray,
  1244. unsigned long new_type)
  1245. {
  1246. int i, j;
  1247. int ret;
  1248. /*
  1249. * for now UC MINUS. see comments in ioremap_nocache()
  1250. */
  1251. for (i = 0; i < addrinarray; i++) {
  1252. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  1253. new_type, NULL);
  1254. if (ret)
  1255. goto out_free;
  1256. }
  1257. ret = change_page_attr_set(addr, addrinarray,
  1258. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  1259. if (!ret && new_type == _PAGE_CACHE_WC)
  1260. ret = change_page_attr_set_clr(addr, addrinarray,
  1261. __pgprot(_PAGE_CACHE_WC),
  1262. __pgprot(_PAGE_CACHE_MASK),
  1263. 0, CPA_ARRAY, NULL);
  1264. if (ret)
  1265. goto out_free;
  1266. return 0;
  1267. out_free:
  1268. for (j = 0; j < i; j++)
  1269. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  1270. return ret;
  1271. }
  1272. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  1273. {
  1274. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
  1275. }
  1276. EXPORT_SYMBOL(set_memory_array_uc);
  1277. int set_memory_array_wc(unsigned long *addr, int addrinarray)
  1278. {
  1279. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
  1280. }
  1281. EXPORT_SYMBOL(set_memory_array_wc);
  1282. int _set_memory_wc(unsigned long addr, int numpages)
  1283. {
  1284. int ret;
  1285. unsigned long addr_copy = addr;
  1286. ret = change_page_attr_set(&addr, numpages,
  1287. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  1288. if (!ret) {
  1289. ret = change_page_attr_set_clr(&addr_copy, numpages,
  1290. __pgprot(_PAGE_CACHE_WC),
  1291. __pgprot(_PAGE_CACHE_MASK),
  1292. 0, 0, NULL);
  1293. }
  1294. return ret;
  1295. }
  1296. int set_memory_wc(unsigned long addr, int numpages)
  1297. {
  1298. int ret;
  1299. if (!pat_enabled)
  1300. return set_memory_uc(addr, numpages);
  1301. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1302. _PAGE_CACHE_WC, NULL);
  1303. if (ret)
  1304. goto out_err;
  1305. ret = _set_memory_wc(addr, numpages);
  1306. if (ret)
  1307. goto out_free;
  1308. return 0;
  1309. out_free:
  1310. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1311. out_err:
  1312. return ret;
  1313. }
  1314. EXPORT_SYMBOL(set_memory_wc);
  1315. int _set_memory_wb(unsigned long addr, int numpages)
  1316. {
  1317. return change_page_attr_clear(&addr, numpages,
  1318. __pgprot(_PAGE_CACHE_MASK), 0);
  1319. }
  1320. int set_memory_wb(unsigned long addr, int numpages)
  1321. {
  1322. int ret;
  1323. ret = _set_memory_wb(addr, numpages);
  1324. if (ret)
  1325. return ret;
  1326. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL(set_memory_wb);
  1330. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  1331. {
  1332. int i;
  1333. int ret;
  1334. ret = change_page_attr_clear(addr, addrinarray,
  1335. __pgprot(_PAGE_CACHE_MASK), 1);
  1336. if (ret)
  1337. return ret;
  1338. for (i = 0; i < addrinarray; i++)
  1339. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  1340. return 0;
  1341. }
  1342. EXPORT_SYMBOL(set_memory_array_wb);
  1343. int set_memory_x(unsigned long addr, int numpages)
  1344. {
  1345. if (!(__supported_pte_mask & _PAGE_NX))
  1346. return 0;
  1347. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1348. }
  1349. EXPORT_SYMBOL(set_memory_x);
  1350. int set_memory_nx(unsigned long addr, int numpages)
  1351. {
  1352. if (!(__supported_pte_mask & _PAGE_NX))
  1353. return 0;
  1354. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1355. }
  1356. EXPORT_SYMBOL(set_memory_nx);
  1357. int set_memory_ro(unsigned long addr, int numpages)
  1358. {
  1359. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1360. }
  1361. EXPORT_SYMBOL_GPL(set_memory_ro);
  1362. int set_memory_rw(unsigned long addr, int numpages)
  1363. {
  1364. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1365. }
  1366. EXPORT_SYMBOL_GPL(set_memory_rw);
  1367. int set_memory_np(unsigned long addr, int numpages)
  1368. {
  1369. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  1370. }
  1371. int set_memory_4k(unsigned long addr, int numpages)
  1372. {
  1373. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  1374. __pgprot(0), 1, 0, NULL);
  1375. }
  1376. int set_pages_uc(struct page *page, int numpages)
  1377. {
  1378. unsigned long addr = (unsigned long)page_address(page);
  1379. return set_memory_uc(addr, numpages);
  1380. }
  1381. EXPORT_SYMBOL(set_pages_uc);
  1382. static int _set_pages_array(struct page **pages, int addrinarray,
  1383. unsigned long new_type)
  1384. {
  1385. unsigned long start;
  1386. unsigned long end;
  1387. int i;
  1388. int free_idx;
  1389. int ret;
  1390. for (i = 0; i < addrinarray; i++) {
  1391. if (PageHighMem(pages[i]))
  1392. continue;
  1393. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1394. end = start + PAGE_SIZE;
  1395. if (reserve_memtype(start, end, new_type, NULL))
  1396. goto err_out;
  1397. }
  1398. ret = cpa_set_pages_array(pages, addrinarray,
  1399. __pgprot(_PAGE_CACHE_UC_MINUS));
  1400. if (!ret && new_type == _PAGE_CACHE_WC)
  1401. ret = change_page_attr_set_clr(NULL, addrinarray,
  1402. __pgprot(_PAGE_CACHE_WC),
  1403. __pgprot(_PAGE_CACHE_MASK),
  1404. 0, CPA_PAGES_ARRAY, pages);
  1405. if (ret)
  1406. goto err_out;
  1407. return 0; /* Success */
  1408. err_out:
  1409. free_idx = i;
  1410. for (i = 0; i < free_idx; i++) {
  1411. if (PageHighMem(pages[i]))
  1412. continue;
  1413. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1414. end = start + PAGE_SIZE;
  1415. free_memtype(start, end);
  1416. }
  1417. return -EINVAL;
  1418. }
  1419. int set_pages_array_uc(struct page **pages, int addrinarray)
  1420. {
  1421. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
  1422. }
  1423. EXPORT_SYMBOL(set_pages_array_uc);
  1424. int set_pages_array_wc(struct page **pages, int addrinarray)
  1425. {
  1426. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
  1427. }
  1428. EXPORT_SYMBOL(set_pages_array_wc);
  1429. int set_pages_wb(struct page *page, int numpages)
  1430. {
  1431. unsigned long addr = (unsigned long)page_address(page);
  1432. return set_memory_wb(addr, numpages);
  1433. }
  1434. EXPORT_SYMBOL(set_pages_wb);
  1435. int set_pages_array_wb(struct page **pages, int addrinarray)
  1436. {
  1437. int retval;
  1438. unsigned long start;
  1439. unsigned long end;
  1440. int i;
  1441. retval = cpa_clear_pages_array(pages, addrinarray,
  1442. __pgprot(_PAGE_CACHE_MASK));
  1443. if (retval)
  1444. return retval;
  1445. for (i = 0; i < addrinarray; i++) {
  1446. if (PageHighMem(pages[i]))
  1447. continue;
  1448. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1449. end = start + PAGE_SIZE;
  1450. free_memtype(start, end);
  1451. }
  1452. return 0;
  1453. }
  1454. EXPORT_SYMBOL(set_pages_array_wb);
  1455. int set_pages_x(struct page *page, int numpages)
  1456. {
  1457. unsigned long addr = (unsigned long)page_address(page);
  1458. return set_memory_x(addr, numpages);
  1459. }
  1460. EXPORT_SYMBOL(set_pages_x);
  1461. int set_pages_nx(struct page *page, int numpages)
  1462. {
  1463. unsigned long addr = (unsigned long)page_address(page);
  1464. return set_memory_nx(addr, numpages);
  1465. }
  1466. EXPORT_SYMBOL(set_pages_nx);
  1467. int set_pages_ro(struct page *page, int numpages)
  1468. {
  1469. unsigned long addr = (unsigned long)page_address(page);
  1470. return set_memory_ro(addr, numpages);
  1471. }
  1472. int set_pages_rw(struct page *page, int numpages)
  1473. {
  1474. unsigned long addr = (unsigned long)page_address(page);
  1475. return set_memory_rw(addr, numpages);
  1476. }
  1477. #ifdef CONFIG_DEBUG_PAGEALLOC
  1478. static int __set_pages_p(struct page *page, int numpages)
  1479. {
  1480. unsigned long tempaddr = (unsigned long) page_address(page);
  1481. struct cpa_data cpa = { .vaddr = &tempaddr,
  1482. .pgd = NULL,
  1483. .numpages = numpages,
  1484. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1485. .mask_clr = __pgprot(0),
  1486. .flags = 0};
  1487. /*
  1488. * No alias checking needed for setting present flag. otherwise,
  1489. * we may need to break large pages for 64-bit kernel text
  1490. * mappings (this adds to complexity if we want to do this from
  1491. * atomic context especially). Let's keep it simple!
  1492. */
  1493. return __change_page_attr_set_clr(&cpa, 0);
  1494. }
  1495. static int __set_pages_np(struct page *page, int numpages)
  1496. {
  1497. unsigned long tempaddr = (unsigned long) page_address(page);
  1498. struct cpa_data cpa = { .vaddr = &tempaddr,
  1499. .pgd = NULL,
  1500. .numpages = numpages,
  1501. .mask_set = __pgprot(0),
  1502. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1503. .flags = 0};
  1504. /*
  1505. * No alias checking needed for setting not present flag. otherwise,
  1506. * we may need to break large pages for 64-bit kernel text
  1507. * mappings (this adds to complexity if we want to do this from
  1508. * atomic context especially). Let's keep it simple!
  1509. */
  1510. return __change_page_attr_set_clr(&cpa, 0);
  1511. }
  1512. void kernel_map_pages(struct page *page, int numpages, int enable)
  1513. {
  1514. if (PageHighMem(page))
  1515. return;
  1516. if (!enable) {
  1517. debug_check_no_locks_freed(page_address(page),
  1518. numpages * PAGE_SIZE);
  1519. }
  1520. /*
  1521. * The return value is ignored as the calls cannot fail.
  1522. * Large pages for identity mappings are not used at boot time
  1523. * and hence no memory allocations during large page split.
  1524. */
  1525. if (enable)
  1526. __set_pages_p(page, numpages);
  1527. else
  1528. __set_pages_np(page, numpages);
  1529. /*
  1530. * We should perform an IPI and flush all tlbs,
  1531. * but that can deadlock->flush only current cpu:
  1532. */
  1533. __flush_tlb_all();
  1534. arch_flush_lazy_mmu_mode();
  1535. }
  1536. #ifdef CONFIG_HIBERNATION
  1537. bool kernel_page_present(struct page *page)
  1538. {
  1539. unsigned int level;
  1540. pte_t *pte;
  1541. if (PageHighMem(page))
  1542. return false;
  1543. pte = lookup_address((unsigned long)page_address(page), &level);
  1544. return (pte_val(*pte) & _PAGE_PRESENT);
  1545. }
  1546. #endif /* CONFIG_HIBERNATION */
  1547. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1548. int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
  1549. unsigned numpages, unsigned long page_flags)
  1550. {
  1551. int retval = -EINVAL;
  1552. struct cpa_data cpa = {
  1553. .vaddr = &address,
  1554. .pfn = pfn,
  1555. .pgd = pgd,
  1556. .numpages = numpages,
  1557. .mask_set = __pgprot(0),
  1558. .mask_clr = __pgprot(0),
  1559. .flags = 0,
  1560. };
  1561. if (!(__supported_pte_mask & _PAGE_NX))
  1562. goto out;
  1563. if (!(page_flags & _PAGE_NX))
  1564. cpa.mask_clr = __pgprot(_PAGE_NX);
  1565. cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
  1566. retval = __change_page_attr_set_clr(&cpa, 0);
  1567. __flush_tlb_all();
  1568. out:
  1569. return retval;
  1570. }
  1571. void kernel_unmap_pages_in_pgd(pgd_t *root, unsigned long address,
  1572. unsigned numpages)
  1573. {
  1574. unmap_pgd_range(root, address, address + (numpages << PAGE_SHIFT));
  1575. }
  1576. /*
  1577. * The testcases use internal knowledge of the implementation that shouldn't
  1578. * be exposed to the rest of the kernel. Include these directly here.
  1579. */
  1580. #ifdef CONFIG_CPA_DEBUG
  1581. #include "pageattr-test.c"
  1582. #endif