pageattr.c 31 KB

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