pageattr.c 31 KB

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