pageattr.c 48 KB

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