pageattr.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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 | __GFP_NOTRACK, 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 | __GFP_NOTRACK);
  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 | __GFP_NOTRACK);
  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 | __GFP_NOTRACK);
  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 | __GFP_NOTRACK);
  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. int set_pages_uc(struct page *page, int numpages)
  1494. {
  1495. unsigned long addr = (unsigned long)page_address(page);
  1496. return set_memory_uc(addr, numpages);
  1497. }
  1498. EXPORT_SYMBOL(set_pages_uc);
  1499. static int _set_pages_array(struct page **pages, int addrinarray,
  1500. enum page_cache_mode new_type)
  1501. {
  1502. unsigned long start;
  1503. unsigned long end;
  1504. enum page_cache_mode set_type;
  1505. int i;
  1506. int free_idx;
  1507. int ret;
  1508. for (i = 0; i < addrinarray; i++) {
  1509. if (PageHighMem(pages[i]))
  1510. continue;
  1511. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1512. end = start + PAGE_SIZE;
  1513. if (reserve_memtype(start, end, new_type, NULL))
  1514. goto err_out;
  1515. }
  1516. /* If WC, set to UC- first and then WC */
  1517. set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
  1518. _PAGE_CACHE_MODE_UC_MINUS : new_type;
  1519. ret = cpa_set_pages_array(pages, addrinarray,
  1520. cachemode2pgprot(set_type));
  1521. if (!ret && new_type == _PAGE_CACHE_MODE_WC)
  1522. ret = change_page_attr_set_clr(NULL, addrinarray,
  1523. cachemode2pgprot(
  1524. _PAGE_CACHE_MODE_WC),
  1525. __pgprot(_PAGE_CACHE_MASK),
  1526. 0, CPA_PAGES_ARRAY, pages);
  1527. if (ret)
  1528. goto err_out;
  1529. return 0; /* Success */
  1530. err_out:
  1531. free_idx = i;
  1532. for (i = 0; i < free_idx; i++) {
  1533. if (PageHighMem(pages[i]))
  1534. continue;
  1535. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1536. end = start + PAGE_SIZE;
  1537. free_memtype(start, end);
  1538. }
  1539. return -EINVAL;
  1540. }
  1541. int set_pages_array_uc(struct page **pages, int addrinarray)
  1542. {
  1543. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
  1544. }
  1545. EXPORT_SYMBOL(set_pages_array_uc);
  1546. int set_pages_array_wc(struct page **pages, int addrinarray)
  1547. {
  1548. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WC);
  1549. }
  1550. EXPORT_SYMBOL(set_pages_array_wc);
  1551. int set_pages_array_wt(struct page **pages, int addrinarray)
  1552. {
  1553. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WT);
  1554. }
  1555. EXPORT_SYMBOL_GPL(set_pages_array_wt);
  1556. int set_pages_wb(struct page *page, int numpages)
  1557. {
  1558. unsigned long addr = (unsigned long)page_address(page);
  1559. return set_memory_wb(addr, numpages);
  1560. }
  1561. EXPORT_SYMBOL(set_pages_wb);
  1562. int set_pages_array_wb(struct page **pages, int addrinarray)
  1563. {
  1564. int retval;
  1565. unsigned long start;
  1566. unsigned long end;
  1567. int i;
  1568. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1569. retval = cpa_clear_pages_array(pages, addrinarray,
  1570. __pgprot(_PAGE_CACHE_MASK));
  1571. if (retval)
  1572. return retval;
  1573. for (i = 0; i < addrinarray; i++) {
  1574. if (PageHighMem(pages[i]))
  1575. continue;
  1576. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1577. end = start + PAGE_SIZE;
  1578. free_memtype(start, end);
  1579. }
  1580. return 0;
  1581. }
  1582. EXPORT_SYMBOL(set_pages_array_wb);
  1583. int set_pages_x(struct page *page, int numpages)
  1584. {
  1585. unsigned long addr = (unsigned long)page_address(page);
  1586. return set_memory_x(addr, numpages);
  1587. }
  1588. EXPORT_SYMBOL(set_pages_x);
  1589. int set_pages_nx(struct page *page, int numpages)
  1590. {
  1591. unsigned long addr = (unsigned long)page_address(page);
  1592. return set_memory_nx(addr, numpages);
  1593. }
  1594. EXPORT_SYMBOL(set_pages_nx);
  1595. int set_pages_ro(struct page *page, int numpages)
  1596. {
  1597. unsigned long addr = (unsigned long)page_address(page);
  1598. return set_memory_ro(addr, numpages);
  1599. }
  1600. int set_pages_rw(struct page *page, int numpages)
  1601. {
  1602. unsigned long addr = (unsigned long)page_address(page);
  1603. return set_memory_rw(addr, numpages);
  1604. }
  1605. #ifdef CONFIG_DEBUG_PAGEALLOC
  1606. static int __set_pages_p(struct page *page, int numpages)
  1607. {
  1608. unsigned long tempaddr = (unsigned long) page_address(page);
  1609. struct cpa_data cpa = { .vaddr = &tempaddr,
  1610. .pgd = NULL,
  1611. .numpages = numpages,
  1612. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1613. .mask_clr = __pgprot(0),
  1614. .flags = 0};
  1615. /*
  1616. * No alias checking needed for setting present flag. otherwise,
  1617. * we may need to break large pages for 64-bit kernel text
  1618. * mappings (this adds to complexity if we want to do this from
  1619. * atomic context especially). Let's keep it simple!
  1620. */
  1621. return __change_page_attr_set_clr(&cpa, 0);
  1622. }
  1623. static int __set_pages_np(struct page *page, int numpages)
  1624. {
  1625. unsigned long tempaddr = (unsigned long) page_address(page);
  1626. struct cpa_data cpa = { .vaddr = &tempaddr,
  1627. .pgd = NULL,
  1628. .numpages = numpages,
  1629. .mask_set = __pgprot(0),
  1630. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1631. .flags = 0};
  1632. /*
  1633. * No alias checking needed for setting not present flag. otherwise,
  1634. * we may need to break large pages for 64-bit kernel text
  1635. * mappings (this adds to complexity if we want to do this from
  1636. * atomic context especially). Let's keep it simple!
  1637. */
  1638. return __change_page_attr_set_clr(&cpa, 0);
  1639. }
  1640. void __kernel_map_pages(struct page *page, int numpages, int enable)
  1641. {
  1642. if (PageHighMem(page))
  1643. return;
  1644. if (!enable) {
  1645. debug_check_no_locks_freed(page_address(page),
  1646. numpages * PAGE_SIZE);
  1647. }
  1648. /*
  1649. * The return value is ignored as the calls cannot fail.
  1650. * Large pages for identity mappings are not used at boot time
  1651. * and hence no memory allocations during large page split.
  1652. */
  1653. if (enable)
  1654. __set_pages_p(page, numpages);
  1655. else
  1656. __set_pages_np(page, numpages);
  1657. /*
  1658. * We should perform an IPI and flush all tlbs,
  1659. * but that can deadlock->flush only current cpu:
  1660. */
  1661. __flush_tlb_all();
  1662. arch_flush_lazy_mmu_mode();
  1663. }
  1664. #ifdef CONFIG_HIBERNATION
  1665. bool kernel_page_present(struct page *page)
  1666. {
  1667. unsigned int level;
  1668. pte_t *pte;
  1669. if (PageHighMem(page))
  1670. return false;
  1671. pte = lookup_address((unsigned long)page_address(page), &level);
  1672. return (pte_val(*pte) & _PAGE_PRESENT);
  1673. }
  1674. #endif /* CONFIG_HIBERNATION */
  1675. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1676. int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
  1677. unsigned numpages, unsigned long page_flags)
  1678. {
  1679. int retval = -EINVAL;
  1680. struct cpa_data cpa = {
  1681. .vaddr = &address,
  1682. .pfn = pfn,
  1683. .pgd = pgd,
  1684. .numpages = numpages,
  1685. .mask_set = __pgprot(0),
  1686. .mask_clr = __pgprot(0),
  1687. .flags = 0,
  1688. };
  1689. if (!(__supported_pte_mask & _PAGE_NX))
  1690. goto out;
  1691. if (!(page_flags & _PAGE_NX))
  1692. cpa.mask_clr = __pgprot(_PAGE_NX);
  1693. if (!(page_flags & _PAGE_RW))
  1694. cpa.mask_clr = __pgprot(_PAGE_RW);
  1695. cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
  1696. retval = __change_page_attr_set_clr(&cpa, 0);
  1697. __flush_tlb_all();
  1698. out:
  1699. return retval;
  1700. }
  1701. /*
  1702. * The testcases use internal knowledge of the implementation that shouldn't
  1703. * be exposed to the rest of the kernel. Include these directly here.
  1704. */
  1705. #ifdef CONFIG_CPA_DEBUG
  1706. #include "pageattr-test.c"
  1707. #endif