pageattr.c 27 KB

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