pageattr.c 45 KB

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