pageattr.c 45 KB

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