pageattr.c 44 KB

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