pageattr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. Important details here:
  432. *
  433. * On Intel the NX bit of all levels must be cleared to make a
  434. * page executable. See section 4.13.2 of Intel 64 and IA-32
  435. * Architectures Software Developer's Manual).
  436. *
  437. * Mark the entry present. The current mapping might be
  438. * set to not present, which we preserved above.
  439. */
  440. ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
  441. pgprot_val(ref_prot) |= _PAGE_PRESENT;
  442. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  443. base = NULL;
  444. out_unlock:
  445. /*
  446. * If we dropped out via the lookup_address check under
  447. * pgd_lock then stick the page back into the pool:
  448. */
  449. if (base)
  450. __free_page(base);
  451. spin_unlock_irqrestore(&pgd_lock, flags);
  452. return 0;
  453. }
  454. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  455. int primary)
  456. {
  457. /*
  458. * Ignore all non primary paths.
  459. */
  460. if (!primary)
  461. return 0;
  462. /*
  463. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  464. * to have holes.
  465. * Also set numpages to '1' indicating that we processed cpa req for
  466. * one virtual address page and its pfn. TBD: numpages can be set based
  467. * on the initial value and the level returned by lookup_address().
  468. */
  469. if (within(vaddr, PAGE_OFFSET,
  470. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  471. cpa->numpages = 1;
  472. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  473. return 0;
  474. } else {
  475. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  476. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  477. *cpa->vaddr);
  478. return -EFAULT;
  479. }
  480. }
  481. static int __change_page_attr(struct cpa_data *cpa, int primary)
  482. {
  483. unsigned long address;
  484. int do_split, err;
  485. unsigned int level;
  486. pte_t *kpte, old_pte;
  487. if (cpa->flags & CPA_ARRAY)
  488. address = cpa->vaddr[cpa->curpage];
  489. else
  490. address = *cpa->vaddr;
  491. repeat:
  492. kpte = lookup_address(address, &level);
  493. if (!kpte)
  494. return __cpa_process_fault(cpa, address, primary);
  495. old_pte = *kpte;
  496. if (!pte_val(old_pte))
  497. return __cpa_process_fault(cpa, address, primary);
  498. if (level == PG_LEVEL_4K) {
  499. pte_t new_pte;
  500. pgprot_t new_prot = pte_pgprot(old_pte);
  501. unsigned long pfn = pte_pfn(old_pte);
  502. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  503. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  504. new_prot = static_protections(new_prot, address, pfn);
  505. /*
  506. * We need to keep the pfn from the existing PTE,
  507. * after all we're only going to change it's attributes
  508. * not the memory it points to
  509. */
  510. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  511. cpa->pfn = pfn;
  512. /*
  513. * Do we really change anything ?
  514. */
  515. if (pte_val(old_pte) != pte_val(new_pte)) {
  516. set_pte_atomic(kpte, new_pte);
  517. cpa->flags |= CPA_FLUSHTLB;
  518. }
  519. cpa->numpages = 1;
  520. return 0;
  521. }
  522. /*
  523. * Check, whether we can keep the large page intact
  524. * and just change the pte:
  525. */
  526. do_split = try_preserve_large_page(kpte, address, cpa);
  527. /*
  528. * When the range fits into the existing large page,
  529. * return. cp->numpages and cpa->tlbflush have been updated in
  530. * try_large_page:
  531. */
  532. if (do_split <= 0)
  533. return do_split;
  534. /*
  535. * We have to split the large page:
  536. */
  537. err = split_large_page(kpte, address);
  538. if (!err) {
  539. /*
  540. * Do a global flush tlb after splitting the large page
  541. * and before we do the actual change page attribute in the PTE.
  542. *
  543. * With out this, we violate the TLB application note, that says
  544. * "The TLBs may contain both ordinary and large-page
  545. * translations for a 4-KByte range of linear addresses. This
  546. * may occur if software modifies the paging structures so that
  547. * the page size used for the address range changes. If the two
  548. * translations differ with respect to page frame or attributes
  549. * (e.g., permissions), processor behavior is undefined and may
  550. * be implementation-specific."
  551. *
  552. * We do this global tlb flush inside the cpa_lock, so that we
  553. * don't allow any other cpu, with stale tlb entries change the
  554. * page attribute in parallel, that also falls into the
  555. * just split large page entry.
  556. */
  557. flush_tlb_all();
  558. goto repeat;
  559. }
  560. return err;
  561. }
  562. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  563. static int cpa_process_alias(struct cpa_data *cpa)
  564. {
  565. struct cpa_data alias_cpa;
  566. int ret = 0;
  567. unsigned long temp_cpa_vaddr, vaddr;
  568. if (cpa->pfn >= max_pfn_mapped)
  569. return 0;
  570. #ifdef CONFIG_X86_64
  571. if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
  572. return 0;
  573. #endif
  574. /*
  575. * No need to redo, when the primary call touched the direct
  576. * mapping already:
  577. */
  578. if (cpa->flags & CPA_ARRAY)
  579. vaddr = cpa->vaddr[cpa->curpage];
  580. else
  581. vaddr = *cpa->vaddr;
  582. if (!(within(vaddr, PAGE_OFFSET,
  583. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  584. alias_cpa = *cpa;
  585. temp_cpa_vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
  586. alias_cpa.vaddr = &temp_cpa_vaddr;
  587. alias_cpa.flags &= ~CPA_ARRAY;
  588. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  589. }
  590. #ifdef CONFIG_X86_64
  591. if (ret)
  592. return ret;
  593. /*
  594. * No need to redo, when the primary call touched the high
  595. * mapping already:
  596. */
  597. if (within(vaddr, (unsigned long) _text, (unsigned long) _end))
  598. return 0;
  599. /*
  600. * If the physical address is inside the kernel map, we need
  601. * to touch the high mapped kernel as well:
  602. */
  603. if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
  604. return 0;
  605. alias_cpa = *cpa;
  606. temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
  607. alias_cpa.vaddr = &temp_cpa_vaddr;
  608. alias_cpa.flags &= ~CPA_ARRAY;
  609. /*
  610. * The high mapping range is imprecise, so ignore the return value.
  611. */
  612. __change_page_attr_set_clr(&alias_cpa, 0);
  613. #endif
  614. return ret;
  615. }
  616. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  617. {
  618. int ret, numpages = cpa->numpages;
  619. while (numpages) {
  620. /*
  621. * Store the remaining nr of pages for the large page
  622. * preservation check.
  623. */
  624. cpa->numpages = numpages;
  625. /* for array changes, we can't use large page */
  626. if (cpa->flags & CPA_ARRAY)
  627. cpa->numpages = 1;
  628. if (!debug_pagealloc)
  629. spin_lock(&cpa_lock);
  630. ret = __change_page_attr(cpa, checkalias);
  631. if (!debug_pagealloc)
  632. spin_unlock(&cpa_lock);
  633. if (ret)
  634. return ret;
  635. if (checkalias) {
  636. ret = cpa_process_alias(cpa);
  637. if (ret)
  638. return ret;
  639. }
  640. /*
  641. * Adjust the number of pages with the result of the
  642. * CPA operation. Either a large page has been
  643. * preserved or a single page update happened.
  644. */
  645. BUG_ON(cpa->numpages > numpages);
  646. numpages -= cpa->numpages;
  647. if (cpa->flags & CPA_ARRAY)
  648. cpa->curpage++;
  649. else
  650. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  651. }
  652. return 0;
  653. }
  654. static inline int cache_attr(pgprot_t attr)
  655. {
  656. return pgprot_val(attr) &
  657. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  658. }
  659. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  660. pgprot_t mask_set, pgprot_t mask_clr,
  661. int force_split, int array)
  662. {
  663. struct cpa_data cpa;
  664. int ret, cache, checkalias;
  665. /*
  666. * Check, if we are requested to change a not supported
  667. * feature:
  668. */
  669. mask_set = canon_pgprot(mask_set);
  670. mask_clr = canon_pgprot(mask_clr);
  671. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  672. return 0;
  673. /* Ensure we are PAGE_SIZE aligned */
  674. if (!array) {
  675. if (*addr & ~PAGE_MASK) {
  676. *addr &= PAGE_MASK;
  677. /*
  678. * People should not be passing in unaligned addresses:
  679. */
  680. WARN_ON_ONCE(1);
  681. }
  682. } else {
  683. int i;
  684. for (i = 0; i < numpages; i++) {
  685. if (addr[i] & ~PAGE_MASK) {
  686. addr[i] &= PAGE_MASK;
  687. WARN_ON_ONCE(1);
  688. }
  689. }
  690. }
  691. /* Must avoid aliasing mappings in the highmem code */
  692. kmap_flush_unused();
  693. vm_unmap_aliases();
  694. /*
  695. * If we're called with lazy mmu updates enabled, the
  696. * in-memory pte state may be stale. Flush pending updates to
  697. * bring them up to date.
  698. */
  699. arch_flush_lazy_mmu_mode();
  700. cpa.vaddr = addr;
  701. cpa.numpages = numpages;
  702. cpa.mask_set = mask_set;
  703. cpa.mask_clr = mask_clr;
  704. cpa.flags = 0;
  705. cpa.curpage = 0;
  706. cpa.force_split = force_split;
  707. if (array)
  708. cpa.flags |= CPA_ARRAY;
  709. /* No alias checking for _NX bit modifications */
  710. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  711. ret = __change_page_attr_set_clr(&cpa, checkalias);
  712. /*
  713. * Check whether we really changed something:
  714. */
  715. if (!(cpa.flags & CPA_FLUSHTLB))
  716. goto out;
  717. /*
  718. * No need to flush, when we did not set any of the caching
  719. * attributes:
  720. */
  721. cache = cache_attr(mask_set);
  722. /*
  723. * On success we use clflush, when the CPU supports it to
  724. * avoid the wbindv. If the CPU does not support it and in the
  725. * error case we fall back to cpa_flush_all (which uses
  726. * wbindv):
  727. */
  728. if (!ret && cpu_has_clflush) {
  729. if (cpa.flags & CPA_ARRAY)
  730. cpa_flush_array(addr, numpages, cache);
  731. else
  732. cpa_flush_range(*addr, numpages, cache);
  733. } else
  734. cpa_flush_all(cache);
  735. /*
  736. * If we've been called with lazy mmu updates enabled, then
  737. * make sure that everything gets flushed out before we
  738. * return.
  739. */
  740. arch_flush_lazy_mmu_mode();
  741. out:
  742. return ret;
  743. }
  744. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  745. pgprot_t mask, int array)
  746. {
  747. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  748. array);
  749. }
  750. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  751. pgprot_t mask, int array)
  752. {
  753. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  754. array);
  755. }
  756. int _set_memory_uc(unsigned long addr, int numpages)
  757. {
  758. /*
  759. * for now UC MINUS. see comments in ioremap_nocache()
  760. */
  761. return change_page_attr_set(&addr, numpages,
  762. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  763. }
  764. int set_memory_uc(unsigned long addr, int numpages)
  765. {
  766. /*
  767. * for now UC MINUS. see comments in ioremap_nocache()
  768. */
  769. if (reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  770. _PAGE_CACHE_UC_MINUS, NULL))
  771. return -EINVAL;
  772. return _set_memory_uc(addr, numpages);
  773. }
  774. EXPORT_SYMBOL(set_memory_uc);
  775. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  776. {
  777. unsigned long start;
  778. unsigned long end;
  779. int i;
  780. /*
  781. * for now UC MINUS. see comments in ioremap_nocache()
  782. */
  783. for (i = 0; i < addrinarray; i++) {
  784. start = __pa(addr[i]);
  785. for (end = start + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  786. if (end != __pa(addr[i + 1]))
  787. break;
  788. i++;
  789. }
  790. if (reserve_memtype(start, end, _PAGE_CACHE_UC_MINUS, NULL))
  791. goto out;
  792. }
  793. return change_page_attr_set(addr, addrinarray,
  794. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  795. out:
  796. for (i = 0; i < addrinarray; i++) {
  797. unsigned long tmp = __pa(addr[i]);
  798. if (tmp == start)
  799. break;
  800. for (end = tmp + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  801. if (end != __pa(addr[i + 1]))
  802. break;
  803. i++;
  804. }
  805. free_memtype(tmp, end);
  806. }
  807. return -EINVAL;
  808. }
  809. EXPORT_SYMBOL(set_memory_array_uc);
  810. int _set_memory_wc(unsigned long addr, int numpages)
  811. {
  812. return change_page_attr_set(&addr, numpages,
  813. __pgprot(_PAGE_CACHE_WC), 0);
  814. }
  815. int set_memory_wc(unsigned long addr, int numpages)
  816. {
  817. if (!pat_enabled)
  818. return set_memory_uc(addr, numpages);
  819. if (reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  820. _PAGE_CACHE_WC, NULL))
  821. return -EINVAL;
  822. return _set_memory_wc(addr, numpages);
  823. }
  824. EXPORT_SYMBOL(set_memory_wc);
  825. int _set_memory_wb(unsigned long addr, int numpages)
  826. {
  827. return change_page_attr_clear(&addr, numpages,
  828. __pgprot(_PAGE_CACHE_MASK), 0);
  829. }
  830. int set_memory_wb(unsigned long addr, int numpages)
  831. {
  832. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  833. return _set_memory_wb(addr, numpages);
  834. }
  835. EXPORT_SYMBOL(set_memory_wb);
  836. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  837. {
  838. int i;
  839. for (i = 0; i < addrinarray; i++) {
  840. unsigned long start = __pa(addr[i]);
  841. unsigned long end;
  842. for (end = start + PAGE_SIZE; i < addrinarray - 1; end += PAGE_SIZE) {
  843. if (end != __pa(addr[i + 1]))
  844. break;
  845. i++;
  846. }
  847. free_memtype(start, end);
  848. }
  849. return change_page_attr_clear(addr, addrinarray,
  850. __pgprot(_PAGE_CACHE_MASK), 1);
  851. }
  852. EXPORT_SYMBOL(set_memory_array_wb);
  853. int set_memory_x(unsigned long addr, int numpages)
  854. {
  855. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  856. }
  857. EXPORT_SYMBOL(set_memory_x);
  858. int set_memory_nx(unsigned long addr, int numpages)
  859. {
  860. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  861. }
  862. EXPORT_SYMBOL(set_memory_nx);
  863. int set_memory_ro(unsigned long addr, int numpages)
  864. {
  865. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  866. }
  867. EXPORT_SYMBOL_GPL(set_memory_ro);
  868. int set_memory_rw(unsigned long addr, int numpages)
  869. {
  870. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  871. }
  872. EXPORT_SYMBOL_GPL(set_memory_rw);
  873. int set_memory_np(unsigned long addr, int numpages)
  874. {
  875. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  876. }
  877. int set_memory_4k(unsigned long addr, int numpages)
  878. {
  879. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  880. __pgprot(0), 1, 0);
  881. }
  882. int set_pages_uc(struct page *page, int numpages)
  883. {
  884. unsigned long addr = (unsigned long)page_address(page);
  885. return set_memory_uc(addr, numpages);
  886. }
  887. EXPORT_SYMBOL(set_pages_uc);
  888. int set_pages_wb(struct page *page, int numpages)
  889. {
  890. unsigned long addr = (unsigned long)page_address(page);
  891. return set_memory_wb(addr, numpages);
  892. }
  893. EXPORT_SYMBOL(set_pages_wb);
  894. int set_pages_x(struct page *page, int numpages)
  895. {
  896. unsigned long addr = (unsigned long)page_address(page);
  897. return set_memory_x(addr, numpages);
  898. }
  899. EXPORT_SYMBOL(set_pages_x);
  900. int set_pages_nx(struct page *page, int numpages)
  901. {
  902. unsigned long addr = (unsigned long)page_address(page);
  903. return set_memory_nx(addr, numpages);
  904. }
  905. EXPORT_SYMBOL(set_pages_nx);
  906. int set_pages_ro(struct page *page, int numpages)
  907. {
  908. unsigned long addr = (unsigned long)page_address(page);
  909. return set_memory_ro(addr, numpages);
  910. }
  911. int set_pages_rw(struct page *page, int numpages)
  912. {
  913. unsigned long addr = (unsigned long)page_address(page);
  914. return set_memory_rw(addr, numpages);
  915. }
  916. #ifdef CONFIG_DEBUG_PAGEALLOC
  917. static int __set_pages_p(struct page *page, int numpages)
  918. {
  919. unsigned long tempaddr = (unsigned long) page_address(page);
  920. struct cpa_data cpa = { .vaddr = &tempaddr,
  921. .numpages = numpages,
  922. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  923. .mask_clr = __pgprot(0),
  924. .flags = 0};
  925. /*
  926. * No alias checking needed for setting present flag. otherwise,
  927. * we may need to break large pages for 64-bit kernel text
  928. * mappings (this adds to complexity if we want to do this from
  929. * atomic context especially). Let's keep it simple!
  930. */
  931. return __change_page_attr_set_clr(&cpa, 0);
  932. }
  933. static int __set_pages_np(struct page *page, int numpages)
  934. {
  935. unsigned long tempaddr = (unsigned long) page_address(page);
  936. struct cpa_data cpa = { .vaddr = &tempaddr,
  937. .numpages = numpages,
  938. .mask_set = __pgprot(0),
  939. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  940. .flags = 0};
  941. /*
  942. * No alias checking needed for setting not present flag. otherwise,
  943. * we may need to break large pages for 64-bit kernel text
  944. * mappings (this adds to complexity if we want to do this from
  945. * atomic context especially). Let's keep it simple!
  946. */
  947. return __change_page_attr_set_clr(&cpa, 0);
  948. }
  949. void kernel_map_pages(struct page *page, int numpages, int enable)
  950. {
  951. if (PageHighMem(page))
  952. return;
  953. if (!enable) {
  954. debug_check_no_locks_freed(page_address(page),
  955. numpages * PAGE_SIZE);
  956. }
  957. /*
  958. * If page allocator is not up yet then do not call c_p_a():
  959. */
  960. if (!debug_pagealloc_enabled)
  961. return;
  962. /*
  963. * The return value is ignored as the calls cannot fail.
  964. * Large pages for identity mappings are not used at boot time
  965. * and hence no memory allocations during large page split.
  966. */
  967. if (enable)
  968. __set_pages_p(page, numpages);
  969. else
  970. __set_pages_np(page, numpages);
  971. /*
  972. * We should perform an IPI and flush all tlbs,
  973. * but that can deadlock->flush only current cpu:
  974. */
  975. __flush_tlb_all();
  976. }
  977. #ifdef CONFIG_HIBERNATION
  978. bool kernel_page_present(struct page *page)
  979. {
  980. unsigned int level;
  981. pte_t *pte;
  982. if (PageHighMem(page))
  983. return false;
  984. pte = lookup_address((unsigned long)page_address(page), &level);
  985. return (pte_val(*pte) & _PAGE_PRESENT);
  986. }
  987. #endif /* CONFIG_HIBERNATION */
  988. #endif /* CONFIG_DEBUG_PAGEALLOC */
  989. /*
  990. * The testcases use internal knowledge of the implementation that shouldn't
  991. * be exposed to the rest of the kernel. Include these directly here.
  992. */
  993. #ifdef CONFIG_CPA_DEBUG
  994. #include "pageattr-test.c"
  995. #endif