pageattr.c 47 KB

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