pageattr.c 48 KB

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