pageattr.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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/slab.h>
  10. #include <linux/mm.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/pfn.h>
  15. #include <linux/percpu.h>
  16. #include <asm/e820.h>
  17. #include <asm/processor.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/sections.h>
  20. #include <asm/setup.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/proto.h>
  24. #include <asm/pat.h>
  25. /*
  26. * The current flushing context - we pass it instead of 5 arguments:
  27. */
  28. struct cpa_data {
  29. unsigned long *vaddr;
  30. pgprot_t mask_set;
  31. pgprot_t mask_clr;
  32. int numpages;
  33. int flags;
  34. unsigned long pfn;
  35. unsigned force_split : 1;
  36. int curpage;
  37. struct page **pages;
  38. };
  39. /*
  40. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  41. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  42. * entries change the page attribute in parallel to some other cpu
  43. * splitting a large page entry along with changing the attribute.
  44. */
  45. static DEFINE_SPINLOCK(cpa_lock);
  46. #define CPA_FLUSHTLB 1
  47. #define CPA_ARRAY 2
  48. #define CPA_PAGES_ARRAY 4
  49. #ifdef CONFIG_PROC_FS
  50. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  51. void update_page_count(int level, unsigned long pages)
  52. {
  53. unsigned long flags;
  54. /* Protect against CPA */
  55. spin_lock_irqsave(&pgd_lock, flags);
  56. direct_pages_count[level] += pages;
  57. spin_unlock_irqrestore(&pgd_lock, flags);
  58. }
  59. static void split_page_count(int level)
  60. {
  61. direct_pages_count[level]--;
  62. direct_pages_count[level - 1] += PTRS_PER_PTE;
  63. }
  64. void arch_report_meminfo(struct seq_file *m)
  65. {
  66. seq_printf(m, "DirectMap4k: %8lu kB\n",
  67. direct_pages_count[PG_LEVEL_4K] << 2);
  68. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  69. seq_printf(m, "DirectMap2M: %8lu kB\n",
  70. direct_pages_count[PG_LEVEL_2M] << 11);
  71. #else
  72. seq_printf(m, "DirectMap4M: %8lu kB\n",
  73. direct_pages_count[PG_LEVEL_2M] << 12);
  74. #endif
  75. #ifdef CONFIG_X86_64
  76. if (direct_gbpages)
  77. seq_printf(m, "DirectMap1G: %8lu kB\n",
  78. direct_pages_count[PG_LEVEL_1G] << 20);
  79. #endif
  80. }
  81. #else
  82. static inline void split_page_count(int level) { }
  83. #endif
  84. #ifdef CONFIG_X86_64
  85. static inline unsigned long highmap_start_pfn(void)
  86. {
  87. return __pa(_text) >> PAGE_SHIFT;
  88. }
  89. static inline unsigned long highmap_end_pfn(void)
  90. {
  91. return __pa(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
  92. }
  93. #endif
  94. #ifdef CONFIG_DEBUG_PAGEALLOC
  95. # define debug_pagealloc 1
  96. #else
  97. # define debug_pagealloc 0
  98. #endif
  99. static inline int
  100. within(unsigned long addr, unsigned long start, unsigned long end)
  101. {
  102. return addr >= start && addr < end;
  103. }
  104. /*
  105. * Flushing functions
  106. */
  107. /**
  108. * clflush_cache_range - flush a cache range with clflush
  109. * @addr: virtual start address
  110. * @size: number of bytes to flush
  111. *
  112. * clflush is an unordered instruction which needs fencing with mfence
  113. * to avoid ordering issues.
  114. */
  115. void clflush_cache_range(void *vaddr, unsigned int size)
  116. {
  117. void *vend = vaddr + size - 1;
  118. mb();
  119. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  120. clflush(vaddr);
  121. /*
  122. * Flush any possible final partial cacheline:
  123. */
  124. clflush(vend);
  125. mb();
  126. }
  127. EXPORT_SYMBOL_GPL(clflush_cache_range);
  128. static void __cpa_flush_all(void *arg)
  129. {
  130. unsigned long cache = (unsigned long)arg;
  131. /*
  132. * Flush all to work around Errata in early athlons regarding
  133. * large page flushing.
  134. */
  135. __flush_tlb_all();
  136. if (cache && boot_cpu_data.x86 >= 4)
  137. wbinvd();
  138. }
  139. static void cpa_flush_all(unsigned long cache)
  140. {
  141. BUG_ON(irqs_disabled());
  142. on_each_cpu(__cpa_flush_all, (void *) cache, 1);
  143. }
  144. static void __cpa_flush_range(void *arg)
  145. {
  146. /*
  147. * We could optimize that further and do individual per page
  148. * tlb invalidates for a low number of pages. Caveat: we must
  149. * flush the high aliases on 64bit as well.
  150. */
  151. __flush_tlb_all();
  152. }
  153. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  154. {
  155. unsigned int i, level;
  156. unsigned long addr;
  157. BUG_ON(irqs_disabled());
  158. WARN_ON(PAGE_ALIGN(start) != start);
  159. on_each_cpu(__cpa_flush_range, NULL, 1);
  160. if (!cache)
  161. return;
  162. /*
  163. * We only need to flush on one CPU,
  164. * clflush is a MESI-coherent instruction that
  165. * will cause all other CPUs to flush the same
  166. * cachelines:
  167. */
  168. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  169. pte_t *pte = lookup_address(addr, &level);
  170. /*
  171. * Only flush present addresses:
  172. */
  173. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  174. clflush_cache_range((void *) addr, PAGE_SIZE);
  175. }
  176. }
  177. static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  178. int in_flags, struct page **pages)
  179. {
  180. unsigned int i, level;
  181. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  182. BUG_ON(irqs_disabled());
  183. on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
  184. if (!cache || do_wbinvd)
  185. return;
  186. /*
  187. * We only need to flush on one CPU,
  188. * clflush is a MESI-coherent instruction that
  189. * will cause all other CPUs to flush the same
  190. * cachelines:
  191. */
  192. for (i = 0; i < numpages; i++) {
  193. unsigned long addr;
  194. pte_t *pte;
  195. if (in_flags & CPA_PAGES_ARRAY)
  196. addr = (unsigned long)page_address(pages[i]);
  197. else
  198. addr = start[i];
  199. pte = lookup_address(addr, &level);
  200. /*
  201. * Only flush present addresses:
  202. */
  203. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  204. clflush_cache_range((void *)addr, PAGE_SIZE);
  205. }
  206. }
  207. /*
  208. * Certain areas of memory on x86 require very specific protection flags,
  209. * for example the BIOS area or kernel text. Callers don't always get this
  210. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  211. * checks and fixes these known static required protection bits.
  212. */
  213. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  214. unsigned long pfn)
  215. {
  216. pgprot_t forbidden = __pgprot(0);
  217. /*
  218. * The BIOS area between 640k and 1Mb needs to be executable for
  219. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  220. */
  221. if (within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  222. pgprot_val(forbidden) |= _PAGE_NX;
  223. /*
  224. * The kernel text needs to be executable for obvious reasons
  225. * Does not cover __inittext since that is gone later on. On
  226. * 64bit we do not enforce !NX on the low mapping
  227. */
  228. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  229. pgprot_val(forbidden) |= _PAGE_NX;
  230. /*
  231. * The .rodata section needs to be read-only. Using the pfn
  232. * catches all aliases.
  233. */
  234. if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
  235. __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
  236. pgprot_val(forbidden) |= _PAGE_RW;
  237. #if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA) && \
  238. !defined(CONFIG_DYNAMIC_FTRACE)
  239. /*
  240. * Kernel text mappings for the large page aligned .rodata section
  241. * will be read-only. For the kernel identity mappings covering
  242. * the holes caused by this alignment can be anything.
  243. *
  244. * This will preserve the large page mappings for kernel text/data
  245. * at no extra cost.
  246. */
  247. if (within(address, (unsigned long)_text,
  248. (unsigned long)__end_rodata_hpage_align))
  249. pgprot_val(forbidden) |= _PAGE_RW;
  250. #endif
  251. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  252. return prot;
  253. }
  254. /*
  255. * Lookup the page table entry for a virtual address. Return a pointer
  256. * to the entry and the level of the mapping.
  257. *
  258. * Note: We return pud and pmd either when the entry is marked large
  259. * or when the present bit is not set. Otherwise we would return a
  260. * pointer to a nonexisting mapping.
  261. */
  262. pte_t *lookup_address(unsigned long address, unsigned int *level)
  263. {
  264. pgd_t *pgd = pgd_offset_k(address);
  265. pud_t *pud;
  266. pmd_t *pmd;
  267. *level = PG_LEVEL_NONE;
  268. if (pgd_none(*pgd))
  269. return NULL;
  270. pud = pud_offset(pgd, address);
  271. if (pud_none(*pud))
  272. return NULL;
  273. *level = PG_LEVEL_1G;
  274. if (pud_large(*pud) || !pud_present(*pud))
  275. return (pte_t *)pud;
  276. pmd = pmd_offset(pud, address);
  277. if (pmd_none(*pmd))
  278. return NULL;
  279. *level = PG_LEVEL_2M;
  280. if (pmd_large(*pmd) || !pmd_present(*pmd))
  281. return (pte_t *)pmd;
  282. *level = PG_LEVEL_4K;
  283. return pte_offset_kernel(pmd, address);
  284. }
  285. EXPORT_SYMBOL_GPL(lookup_address);
  286. /*
  287. * Set the new pmd in all the pgds we know about:
  288. */
  289. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  290. {
  291. /* change init_mm */
  292. set_pte_atomic(kpte, pte);
  293. #ifdef CONFIG_X86_32
  294. if (!SHARED_KERNEL_PMD) {
  295. struct page *page;
  296. list_for_each_entry(page, &pgd_list, lru) {
  297. pgd_t *pgd;
  298. pud_t *pud;
  299. pmd_t *pmd;
  300. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  301. pud = pud_offset(pgd, address);
  302. pmd = pmd_offset(pud, address);
  303. set_pte_atomic((pte_t *)pmd, pte);
  304. }
  305. }
  306. #endif
  307. }
  308. static int
  309. try_preserve_large_page(pte_t *kpte, unsigned long address,
  310. struct cpa_data *cpa)
  311. {
  312. unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
  313. pte_t new_pte, old_pte, *tmp;
  314. pgprot_t old_prot, new_prot;
  315. int i, do_split = 1;
  316. unsigned int level;
  317. if (cpa->force_split)
  318. return 1;
  319. spin_lock_irqsave(&pgd_lock, flags);
  320. /*
  321. * Check for races, another CPU might have split this page
  322. * up already:
  323. */
  324. tmp = lookup_address(address, &level);
  325. if (tmp != kpte)
  326. goto out_unlock;
  327. switch (level) {
  328. case PG_LEVEL_2M:
  329. psize = PMD_PAGE_SIZE;
  330. pmask = PMD_PAGE_MASK;
  331. break;
  332. #ifdef CONFIG_X86_64
  333. case PG_LEVEL_1G:
  334. psize = PUD_PAGE_SIZE;
  335. pmask = PUD_PAGE_MASK;
  336. break;
  337. #endif
  338. default:
  339. do_split = -EINVAL;
  340. goto out_unlock;
  341. }
  342. /*
  343. * Calculate the number of pages, which fit into this large
  344. * page starting at address:
  345. */
  346. nextpage_addr = (address + psize) & pmask;
  347. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  348. if (numpages < cpa->numpages)
  349. cpa->numpages = numpages;
  350. /*
  351. * We are safe now. Check whether the new pgprot is the same:
  352. */
  353. old_pte = *kpte;
  354. old_prot = new_prot = pte_pgprot(old_pte);
  355. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  356. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  357. /*
  358. * old_pte points to the large page base address. So we need
  359. * to add the offset of the virtual address:
  360. */
  361. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  362. cpa->pfn = pfn;
  363. new_prot = static_protections(new_prot, address, pfn);
  364. /*
  365. * We need to check the full range, whether
  366. * static_protection() requires a different pgprot for one of
  367. * the pages in the range we try to preserve:
  368. */
  369. addr = address + PAGE_SIZE;
  370. pfn++;
  371. for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE, pfn++) {
  372. pgprot_t chk_prot = static_protections(new_prot, addr, pfn);
  373. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  374. goto out_unlock;
  375. }
  376. /*
  377. * If there are no changes, return. maxpages has been updated
  378. * above:
  379. */
  380. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  381. do_split = 0;
  382. goto out_unlock;
  383. }
  384. /*
  385. * We need to change the attributes. Check, whether we can
  386. * change the large page in one go. We request a split, when
  387. * the address is not aligned and the number of pages is
  388. * smaller than the number of pages in the large page. Note
  389. * that we limited the number of possible pages already to
  390. * the number of pages in the large page.
  391. */
  392. if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
  393. /*
  394. * The address is aligned and the number of pages
  395. * covers the full page.
  396. */
  397. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  398. __set_pmd_pte(kpte, address, new_pte);
  399. cpa->flags |= CPA_FLUSHTLB;
  400. do_split = 0;
  401. }
  402. out_unlock:
  403. spin_unlock_irqrestore(&pgd_lock, flags);
  404. return do_split;
  405. }
  406. static int split_large_page(pte_t *kpte, unsigned long address)
  407. {
  408. unsigned long flags, pfn, pfninc = 1;
  409. unsigned int i, level;
  410. pte_t *pbase, *tmp;
  411. pgprot_t ref_prot;
  412. struct page *base;
  413. if (!debug_pagealloc)
  414. spin_unlock(&cpa_lock);
  415. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  416. if (!debug_pagealloc)
  417. spin_lock(&cpa_lock);
  418. if (!base)
  419. return -ENOMEM;
  420. spin_lock_irqsave(&pgd_lock, flags);
  421. /*
  422. * Check for races, another CPU might have split this page
  423. * up for us already:
  424. */
  425. tmp = lookup_address(address, &level);
  426. if (tmp != kpte)
  427. goto out_unlock;
  428. pbase = (pte_t *)page_address(base);
  429. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  430. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  431. /*
  432. * If we ever want to utilize the PAT bit, we need to
  433. * update this function to make sure it's converted from
  434. * bit 12 to bit 7 when we cross from the 2MB level to
  435. * the 4K level:
  436. */
  437. WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
  438. #ifdef CONFIG_X86_64
  439. if (level == PG_LEVEL_1G) {
  440. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  441. pgprot_val(ref_prot) |= _PAGE_PSE;
  442. }
  443. #endif
  444. /*
  445. * Get the target pfn from the original entry:
  446. */
  447. pfn = pte_pfn(*kpte);
  448. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  449. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  450. if (address >= (unsigned long)__va(0) &&
  451. address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
  452. split_page_count(level);
  453. #ifdef CONFIG_X86_64
  454. if (address >= (unsigned long)__va(1UL<<32) &&
  455. address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
  456. split_page_count(level);
  457. #endif
  458. /*
  459. * Install the new, split up pagetable.
  460. *
  461. * We use the standard kernel pagetable protections for the new
  462. * pagetable protections, the actual ptes set above control the
  463. * primary protection behavior:
  464. */
  465. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  466. /*
  467. * Intel Atom errata AAH41 workaround.
  468. *
  469. * The real fix should be in hw or in a microcode update, but
  470. * we also probabilistically try to reduce the window of having
  471. * a large TLB mixed with 4K TLBs while instruction fetches are
  472. * going on.
  473. */
  474. __flush_tlb_all();
  475. base = NULL;
  476. out_unlock:
  477. /*
  478. * If we dropped out via the lookup_address check under
  479. * pgd_lock then stick the page back into the pool:
  480. */
  481. if (base)
  482. __free_page(base);
  483. spin_unlock_irqrestore(&pgd_lock, flags);
  484. return 0;
  485. }
  486. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  487. int primary)
  488. {
  489. /*
  490. * Ignore all non primary paths.
  491. */
  492. if (!primary)
  493. return 0;
  494. /*
  495. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  496. * to have holes.
  497. * Also set numpages to '1' indicating that we processed cpa req for
  498. * one virtual address page and its pfn. TBD: numpages can be set based
  499. * on the initial value and the level returned by lookup_address().
  500. */
  501. if (within(vaddr, PAGE_OFFSET,
  502. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  503. cpa->numpages = 1;
  504. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  505. return 0;
  506. } else {
  507. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  508. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  509. *cpa->vaddr);
  510. return -EFAULT;
  511. }
  512. }
  513. static int __change_page_attr(struct cpa_data *cpa, int primary)
  514. {
  515. unsigned long address;
  516. int do_split, err;
  517. unsigned int level;
  518. pte_t *kpte, old_pte;
  519. if (cpa->flags & CPA_PAGES_ARRAY) {
  520. struct page *page = cpa->pages[cpa->curpage];
  521. if (unlikely(PageHighMem(page)))
  522. return 0;
  523. address = (unsigned long)page_address(page);
  524. } else if (cpa->flags & CPA_ARRAY)
  525. address = cpa->vaddr[cpa->curpage];
  526. else
  527. address = *cpa->vaddr;
  528. repeat:
  529. kpte = lookup_address(address, &level);
  530. if (!kpte)
  531. return __cpa_process_fault(cpa, address, primary);
  532. old_pte = *kpte;
  533. if (!pte_val(old_pte))
  534. return __cpa_process_fault(cpa, address, primary);
  535. if (level == PG_LEVEL_4K) {
  536. pte_t new_pte;
  537. pgprot_t new_prot = pte_pgprot(old_pte);
  538. unsigned long pfn = pte_pfn(old_pte);
  539. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  540. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  541. new_prot = static_protections(new_prot, address, pfn);
  542. /*
  543. * We need to keep the pfn from the existing PTE,
  544. * after all we're only going to change it's attributes
  545. * not the memory it points to
  546. */
  547. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  548. cpa->pfn = pfn;
  549. /*
  550. * Do we really change anything ?
  551. */
  552. if (pte_val(old_pte) != pte_val(new_pte)) {
  553. set_pte_atomic(kpte, new_pte);
  554. cpa->flags |= CPA_FLUSHTLB;
  555. }
  556. cpa->numpages = 1;
  557. return 0;
  558. }
  559. /*
  560. * Check, whether we can keep the large page intact
  561. * and just change the pte:
  562. */
  563. do_split = try_preserve_large_page(kpte, address, cpa);
  564. /*
  565. * When the range fits into the existing large page,
  566. * return. cp->numpages and cpa->tlbflush have been updated in
  567. * try_large_page:
  568. */
  569. if (do_split <= 0)
  570. return do_split;
  571. /*
  572. * We have to split the large page:
  573. */
  574. err = split_large_page(kpte, address);
  575. if (!err) {
  576. /*
  577. * Do a global flush tlb after splitting the large page
  578. * and before we do the actual change page attribute in the PTE.
  579. *
  580. * With out this, we violate the TLB application note, that says
  581. * "The TLBs may contain both ordinary and large-page
  582. * translations for a 4-KByte range of linear addresses. This
  583. * may occur if software modifies the paging structures so that
  584. * the page size used for the address range changes. If the two
  585. * translations differ with respect to page frame or attributes
  586. * (e.g., permissions), processor behavior is undefined and may
  587. * be implementation-specific."
  588. *
  589. * We do this global tlb flush inside the cpa_lock, so that we
  590. * don't allow any other cpu, with stale tlb entries change the
  591. * page attribute in parallel, that also falls into the
  592. * just split large page entry.
  593. */
  594. flush_tlb_all();
  595. goto repeat;
  596. }
  597. return err;
  598. }
  599. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  600. static int cpa_process_alias(struct cpa_data *cpa)
  601. {
  602. struct cpa_data alias_cpa;
  603. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  604. unsigned long vaddr;
  605. int ret;
  606. if (cpa->pfn >= max_pfn_mapped)
  607. return 0;
  608. #ifdef CONFIG_X86_64
  609. if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
  610. return 0;
  611. #endif
  612. /*
  613. * No need to redo, when the primary call touched the direct
  614. * mapping already:
  615. */
  616. if (cpa->flags & CPA_PAGES_ARRAY) {
  617. struct page *page = cpa->pages[cpa->curpage];
  618. if (unlikely(PageHighMem(page)))
  619. return 0;
  620. vaddr = (unsigned long)page_address(page);
  621. } else if (cpa->flags & CPA_ARRAY)
  622. vaddr = cpa->vaddr[cpa->curpage];
  623. else
  624. vaddr = *cpa->vaddr;
  625. if (!(within(vaddr, PAGE_OFFSET,
  626. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  627. alias_cpa = *cpa;
  628. alias_cpa.vaddr = &laddr;
  629. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  630. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  631. if (ret)
  632. return ret;
  633. }
  634. #ifdef CONFIG_X86_64
  635. /*
  636. * If the primary call didn't touch the high mapping already
  637. * and the physical address is inside the kernel map, we need
  638. * to touch the high mapped kernel as well:
  639. */
  640. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  641. within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
  642. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  643. __START_KERNEL_map - phys_base;
  644. alias_cpa = *cpa;
  645. alias_cpa.vaddr = &temp_cpa_vaddr;
  646. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  647. /*
  648. * The high mapping range is imprecise, so ignore the
  649. * return value.
  650. */
  651. __change_page_attr_set_clr(&alias_cpa, 0);
  652. }
  653. #endif
  654. return 0;
  655. }
  656. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  657. {
  658. int ret, numpages = cpa->numpages;
  659. while (numpages) {
  660. /*
  661. * Store the remaining nr of pages for the large page
  662. * preservation check.
  663. */
  664. cpa->numpages = numpages;
  665. /* for array changes, we can't use large page */
  666. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  667. cpa->numpages = 1;
  668. if (!debug_pagealloc)
  669. spin_lock(&cpa_lock);
  670. ret = __change_page_attr(cpa, checkalias);
  671. if (!debug_pagealloc)
  672. spin_unlock(&cpa_lock);
  673. if (ret)
  674. return ret;
  675. if (checkalias) {
  676. ret = cpa_process_alias(cpa);
  677. if (ret)
  678. return ret;
  679. }
  680. /*
  681. * Adjust the number of pages with the result of the
  682. * CPA operation. Either a large page has been
  683. * preserved or a single page update happened.
  684. */
  685. BUG_ON(cpa->numpages > numpages);
  686. numpages -= cpa->numpages;
  687. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  688. cpa->curpage++;
  689. else
  690. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  691. }
  692. return 0;
  693. }
  694. static inline int cache_attr(pgprot_t attr)
  695. {
  696. return pgprot_val(attr) &
  697. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  698. }
  699. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  700. pgprot_t mask_set, pgprot_t mask_clr,
  701. int force_split, int in_flag,
  702. struct page **pages)
  703. {
  704. struct cpa_data cpa;
  705. int ret, cache, checkalias;
  706. unsigned long baddr = 0;
  707. /*
  708. * Check, if we are requested to change a not supported
  709. * feature:
  710. */
  711. mask_set = canon_pgprot(mask_set);
  712. mask_clr = canon_pgprot(mask_clr);
  713. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  714. return 0;
  715. /* Ensure we are PAGE_SIZE aligned */
  716. if (in_flag & CPA_ARRAY) {
  717. int i;
  718. for (i = 0; i < numpages; i++) {
  719. if (addr[i] & ~PAGE_MASK) {
  720. addr[i] &= PAGE_MASK;
  721. WARN_ON_ONCE(1);
  722. }
  723. }
  724. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  725. /*
  726. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  727. * No need to cehck in that case
  728. */
  729. if (*addr & ~PAGE_MASK) {
  730. *addr &= PAGE_MASK;
  731. /*
  732. * People should not be passing in unaligned addresses:
  733. */
  734. WARN_ON_ONCE(1);
  735. }
  736. /*
  737. * Save address for cache flush. *addr is modified in the call
  738. * to __change_page_attr_set_clr() below.
  739. */
  740. baddr = *addr;
  741. }
  742. /* Must avoid aliasing mappings in the highmem code */
  743. kmap_flush_unused();
  744. vm_unmap_aliases();
  745. cpa.vaddr = addr;
  746. cpa.pages = pages;
  747. cpa.numpages = numpages;
  748. cpa.mask_set = mask_set;
  749. cpa.mask_clr = mask_clr;
  750. cpa.flags = 0;
  751. cpa.curpage = 0;
  752. cpa.force_split = force_split;
  753. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  754. cpa.flags |= in_flag;
  755. /* No alias checking for _NX bit modifications */
  756. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  757. ret = __change_page_attr_set_clr(&cpa, checkalias);
  758. /*
  759. * Check whether we really changed something:
  760. */
  761. if (!(cpa.flags & CPA_FLUSHTLB))
  762. goto out;
  763. /*
  764. * No need to flush, when we did not set any of the caching
  765. * attributes:
  766. */
  767. cache = cache_attr(mask_set);
  768. /*
  769. * On success we use clflush, when the CPU supports it to
  770. * avoid the wbindv. If the CPU does not support it and in the
  771. * error case we fall back to cpa_flush_all (which uses
  772. * wbindv):
  773. */
  774. if (!ret && cpu_has_clflush) {
  775. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  776. cpa_flush_array(addr, numpages, cache,
  777. cpa.flags, pages);
  778. } else
  779. cpa_flush_range(baddr, numpages, cache);
  780. } else
  781. cpa_flush_all(cache);
  782. out:
  783. return ret;
  784. }
  785. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  786. pgprot_t mask, int array)
  787. {
  788. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  789. (array ? CPA_ARRAY : 0), NULL);
  790. }
  791. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  792. pgprot_t mask, int array)
  793. {
  794. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  795. (array ? CPA_ARRAY : 0), NULL);
  796. }
  797. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  798. pgprot_t mask)
  799. {
  800. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  801. CPA_PAGES_ARRAY, pages);
  802. }
  803. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  804. pgprot_t mask)
  805. {
  806. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  807. CPA_PAGES_ARRAY, pages);
  808. }
  809. int _set_memory_uc(unsigned long addr, int numpages)
  810. {
  811. /*
  812. * for now UC MINUS. see comments in ioremap_nocache()
  813. */
  814. return change_page_attr_set(&addr, numpages,
  815. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  816. }
  817. int set_memory_uc(unsigned long addr, int numpages)
  818. {
  819. int ret;
  820. /*
  821. * for now UC MINUS. see comments in ioremap_nocache()
  822. */
  823. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  824. _PAGE_CACHE_UC_MINUS, NULL);
  825. if (ret)
  826. goto out_err;
  827. ret = _set_memory_uc(addr, numpages);
  828. if (ret)
  829. goto out_free;
  830. return 0;
  831. out_free:
  832. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  833. out_err:
  834. return ret;
  835. }
  836. EXPORT_SYMBOL(set_memory_uc);
  837. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  838. {
  839. int i, j;
  840. int ret;
  841. /*
  842. * for now UC MINUS. see comments in ioremap_nocache()
  843. */
  844. for (i = 0; i < addrinarray; i++) {
  845. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  846. _PAGE_CACHE_UC_MINUS, NULL);
  847. if (ret)
  848. goto out_free;
  849. }
  850. ret = change_page_attr_set(addr, addrinarray,
  851. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  852. if (ret)
  853. goto out_free;
  854. return 0;
  855. out_free:
  856. for (j = 0; j < i; j++)
  857. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  858. return ret;
  859. }
  860. EXPORT_SYMBOL(set_memory_array_uc);
  861. int _set_memory_wc(unsigned long addr, int numpages)
  862. {
  863. int ret;
  864. unsigned long addr_copy = addr;
  865. ret = change_page_attr_set(&addr, numpages,
  866. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  867. if (!ret) {
  868. ret = change_page_attr_set_clr(&addr_copy, numpages,
  869. __pgprot(_PAGE_CACHE_WC),
  870. __pgprot(_PAGE_CACHE_MASK),
  871. 0, 0, NULL);
  872. }
  873. return ret;
  874. }
  875. int set_memory_wc(unsigned long addr, int numpages)
  876. {
  877. int ret;
  878. if (!pat_enabled)
  879. return set_memory_uc(addr, numpages);
  880. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  881. _PAGE_CACHE_WC, NULL);
  882. if (ret)
  883. goto out_err;
  884. ret = _set_memory_wc(addr, numpages);
  885. if (ret)
  886. goto out_free;
  887. return 0;
  888. out_free:
  889. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  890. out_err:
  891. return ret;
  892. }
  893. EXPORT_SYMBOL(set_memory_wc);
  894. int _set_memory_wb(unsigned long addr, int numpages)
  895. {
  896. return change_page_attr_clear(&addr, numpages,
  897. __pgprot(_PAGE_CACHE_MASK), 0);
  898. }
  899. int set_memory_wb(unsigned long addr, int numpages)
  900. {
  901. int ret;
  902. ret = _set_memory_wb(addr, numpages);
  903. if (ret)
  904. return ret;
  905. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  906. return 0;
  907. }
  908. EXPORT_SYMBOL(set_memory_wb);
  909. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  910. {
  911. int i;
  912. int ret;
  913. ret = change_page_attr_clear(addr, addrinarray,
  914. __pgprot(_PAGE_CACHE_MASK), 1);
  915. if (ret)
  916. return ret;
  917. for (i = 0; i < addrinarray; i++)
  918. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  919. return 0;
  920. }
  921. EXPORT_SYMBOL(set_memory_array_wb);
  922. int set_memory_x(unsigned long addr, int numpages)
  923. {
  924. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  925. }
  926. EXPORT_SYMBOL(set_memory_x);
  927. int set_memory_nx(unsigned long addr, int numpages)
  928. {
  929. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  930. }
  931. EXPORT_SYMBOL(set_memory_nx);
  932. int set_memory_ro(unsigned long addr, int numpages)
  933. {
  934. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  935. }
  936. EXPORT_SYMBOL_GPL(set_memory_ro);
  937. int set_memory_rw(unsigned long addr, int numpages)
  938. {
  939. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  940. }
  941. EXPORT_SYMBOL_GPL(set_memory_rw);
  942. int set_memory_np(unsigned long addr, int numpages)
  943. {
  944. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  945. }
  946. int set_memory_4k(unsigned long addr, int numpages)
  947. {
  948. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  949. __pgprot(0), 1, 0, NULL);
  950. }
  951. int set_pages_uc(struct page *page, int numpages)
  952. {
  953. unsigned long addr = (unsigned long)page_address(page);
  954. return set_memory_uc(addr, numpages);
  955. }
  956. EXPORT_SYMBOL(set_pages_uc);
  957. int set_pages_array_uc(struct page **pages, int addrinarray)
  958. {
  959. unsigned long start;
  960. unsigned long end;
  961. int i;
  962. int free_idx;
  963. for (i = 0; i < addrinarray; i++) {
  964. if (PageHighMem(pages[i]))
  965. continue;
  966. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  967. end = start + PAGE_SIZE;
  968. if (reserve_memtype(start, end, _PAGE_CACHE_UC_MINUS, NULL))
  969. goto err_out;
  970. }
  971. if (cpa_set_pages_array(pages, addrinarray,
  972. __pgprot(_PAGE_CACHE_UC_MINUS)) == 0) {
  973. return 0; /* Success */
  974. }
  975. err_out:
  976. free_idx = i;
  977. for (i = 0; i < free_idx; i++) {
  978. if (PageHighMem(pages[i]))
  979. continue;
  980. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  981. end = start + PAGE_SIZE;
  982. free_memtype(start, end);
  983. }
  984. return -EINVAL;
  985. }
  986. EXPORT_SYMBOL(set_pages_array_uc);
  987. int set_pages_wb(struct page *page, int numpages)
  988. {
  989. unsigned long addr = (unsigned long)page_address(page);
  990. return set_memory_wb(addr, numpages);
  991. }
  992. EXPORT_SYMBOL(set_pages_wb);
  993. int set_pages_array_wb(struct page **pages, int addrinarray)
  994. {
  995. int retval;
  996. unsigned long start;
  997. unsigned long end;
  998. int i;
  999. retval = cpa_clear_pages_array(pages, addrinarray,
  1000. __pgprot(_PAGE_CACHE_MASK));
  1001. if (retval)
  1002. return retval;
  1003. for (i = 0; i < addrinarray; i++) {
  1004. if (PageHighMem(pages[i]))
  1005. continue;
  1006. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1007. end = start + PAGE_SIZE;
  1008. free_memtype(start, end);
  1009. }
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(set_pages_array_wb);
  1013. int set_pages_x(struct page *page, int numpages)
  1014. {
  1015. unsigned long addr = (unsigned long)page_address(page);
  1016. return set_memory_x(addr, numpages);
  1017. }
  1018. EXPORT_SYMBOL(set_pages_x);
  1019. int set_pages_nx(struct page *page, int numpages)
  1020. {
  1021. unsigned long addr = (unsigned long)page_address(page);
  1022. return set_memory_nx(addr, numpages);
  1023. }
  1024. EXPORT_SYMBOL(set_pages_nx);
  1025. int set_pages_ro(struct page *page, int numpages)
  1026. {
  1027. unsigned long addr = (unsigned long)page_address(page);
  1028. return set_memory_ro(addr, numpages);
  1029. }
  1030. int set_pages_rw(struct page *page, int numpages)
  1031. {
  1032. unsigned long addr = (unsigned long)page_address(page);
  1033. return set_memory_rw(addr, numpages);
  1034. }
  1035. #ifdef CONFIG_DEBUG_PAGEALLOC
  1036. static int __set_pages_p(struct page *page, int numpages)
  1037. {
  1038. unsigned long tempaddr = (unsigned long) page_address(page);
  1039. struct cpa_data cpa = { .vaddr = &tempaddr,
  1040. .numpages = numpages,
  1041. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1042. .mask_clr = __pgprot(0),
  1043. .flags = 0};
  1044. /*
  1045. * No alias checking needed for setting present flag. otherwise,
  1046. * we may need to break large pages for 64-bit kernel text
  1047. * mappings (this adds to complexity if we want to do this from
  1048. * atomic context especially). Let's keep it simple!
  1049. */
  1050. return __change_page_attr_set_clr(&cpa, 0);
  1051. }
  1052. static int __set_pages_np(struct page *page, int numpages)
  1053. {
  1054. unsigned long tempaddr = (unsigned long) page_address(page);
  1055. struct cpa_data cpa = { .vaddr = &tempaddr,
  1056. .numpages = numpages,
  1057. .mask_set = __pgprot(0),
  1058. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1059. .flags = 0};
  1060. /*
  1061. * No alias checking needed for setting not present flag. otherwise,
  1062. * we may need to break large pages for 64-bit kernel text
  1063. * mappings (this adds to complexity if we want to do this from
  1064. * atomic context especially). Let's keep it simple!
  1065. */
  1066. return __change_page_attr_set_clr(&cpa, 0);
  1067. }
  1068. void kernel_map_pages(struct page *page, int numpages, int enable)
  1069. {
  1070. if (PageHighMem(page))
  1071. return;
  1072. if (!enable) {
  1073. debug_check_no_locks_freed(page_address(page),
  1074. numpages * PAGE_SIZE);
  1075. }
  1076. /*
  1077. * If page allocator is not up yet then do not call c_p_a():
  1078. */
  1079. if (!debug_pagealloc_enabled)
  1080. return;
  1081. /*
  1082. * The return value is ignored as the calls cannot fail.
  1083. * Large pages for identity mappings are not used at boot time
  1084. * and hence no memory allocations during large page split.
  1085. */
  1086. if (enable)
  1087. __set_pages_p(page, numpages);
  1088. else
  1089. __set_pages_np(page, numpages);
  1090. /*
  1091. * We should perform an IPI and flush all tlbs,
  1092. * but that can deadlock->flush only current cpu:
  1093. */
  1094. __flush_tlb_all();
  1095. }
  1096. #ifdef CONFIG_HIBERNATION
  1097. bool kernel_page_present(struct page *page)
  1098. {
  1099. unsigned int level;
  1100. pte_t *pte;
  1101. if (PageHighMem(page))
  1102. return false;
  1103. pte = lookup_address((unsigned long)page_address(page), &level);
  1104. return (pte_val(*pte) & _PAGE_PRESENT);
  1105. }
  1106. #endif /* CONFIG_HIBERNATION */
  1107. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1108. /*
  1109. * The testcases use internal knowledge of the implementation that shouldn't
  1110. * be exposed to the rest of the kernel. Include these directly here.
  1111. */
  1112. #ifdef CONFIG_CPA_DEBUG
  1113. #include "pageattr-test.c"
  1114. #endif