pageattr.c 49 KB

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