pageattr.c 50 KB

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