pageattr.c 52 KB

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