pageattr.c 28 KB

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