pageattr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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 <asm/e820.h>
  13. #include <asm/processor.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/sections.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/pgalloc.h>
  18. #include <asm/proto.h>
  19. /*
  20. * The current flushing context - we pass it instead of 5 arguments:
  21. */
  22. struct cpa_data {
  23. unsigned long vaddr;
  24. pgprot_t mask_set;
  25. pgprot_t mask_clr;
  26. int numpages;
  27. int flushtlb;
  28. unsigned long pfn;
  29. };
  30. #ifdef CONFIG_X86_64
  31. static inline unsigned long highmap_start_pfn(void)
  32. {
  33. return __pa(_text) >> PAGE_SHIFT;
  34. }
  35. static inline unsigned long highmap_end_pfn(void)
  36. {
  37. return __pa(round_up((unsigned long)_end, PMD_SIZE)) >> PAGE_SHIFT;
  38. }
  39. #endif
  40. static inline int
  41. within(unsigned long addr, unsigned long start, unsigned long end)
  42. {
  43. return addr >= start && addr < end;
  44. }
  45. /*
  46. * Flushing functions
  47. */
  48. /**
  49. * clflush_cache_range - flush a cache range with clflush
  50. * @addr: virtual start address
  51. * @size: number of bytes to flush
  52. *
  53. * clflush is an unordered instruction which needs fencing with mfence
  54. * to avoid ordering issues.
  55. */
  56. void clflush_cache_range(void *vaddr, unsigned int size)
  57. {
  58. void *vend = vaddr + size - 1;
  59. mb();
  60. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  61. clflush(vaddr);
  62. /*
  63. * Flush any possible final partial cacheline:
  64. */
  65. clflush(vend);
  66. mb();
  67. }
  68. static void __cpa_flush_all(void *arg)
  69. {
  70. unsigned long cache = (unsigned long)arg;
  71. /*
  72. * Flush all to work around Errata in early athlons regarding
  73. * large page flushing.
  74. */
  75. __flush_tlb_all();
  76. if (cache && boot_cpu_data.x86_model >= 4)
  77. wbinvd();
  78. }
  79. static void cpa_flush_all(unsigned long cache)
  80. {
  81. BUG_ON(irqs_disabled());
  82. on_each_cpu(__cpa_flush_all, (void *) cache, 1, 1);
  83. }
  84. static void __cpa_flush_range(void *arg)
  85. {
  86. /*
  87. * We could optimize that further and do individual per page
  88. * tlb invalidates for a low number of pages. Caveat: we must
  89. * flush the high aliases on 64bit as well.
  90. */
  91. __flush_tlb_all();
  92. }
  93. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  94. {
  95. unsigned int i, level;
  96. unsigned long addr;
  97. BUG_ON(irqs_disabled());
  98. WARN_ON(PAGE_ALIGN(start) != start);
  99. on_each_cpu(__cpa_flush_range, NULL, 1, 1);
  100. if (!cache)
  101. return;
  102. /*
  103. * We only need to flush on one CPU,
  104. * clflush is a MESI-coherent instruction that
  105. * will cause all other CPUs to flush the same
  106. * cachelines:
  107. */
  108. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  109. pte_t *pte = lookup_address(addr, &level);
  110. /*
  111. * Only flush present addresses:
  112. */
  113. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  114. clflush_cache_range((void *) addr, PAGE_SIZE);
  115. }
  116. }
  117. /*
  118. * Certain areas of memory on x86 require very specific protection flags,
  119. * for example the BIOS area or kernel text. Callers don't always get this
  120. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  121. * checks and fixes these known static required protection bits.
  122. */
  123. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  124. unsigned long pfn)
  125. {
  126. pgprot_t forbidden = __pgprot(0);
  127. /*
  128. * The BIOS area between 640k and 1Mb needs to be executable for
  129. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  130. */
  131. if (within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  132. pgprot_val(forbidden) |= _PAGE_NX;
  133. /*
  134. * The kernel text needs to be executable for obvious reasons
  135. * Does not cover __inittext since that is gone later on. On
  136. * 64bit we do not enforce !NX on the low mapping
  137. */
  138. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  139. pgprot_val(forbidden) |= _PAGE_NX;
  140. /*
  141. * The .rodata section needs to be read-only. Using the pfn
  142. * catches all aliases.
  143. */
  144. if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
  145. __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
  146. pgprot_val(forbidden) |= _PAGE_RW;
  147. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  148. return prot;
  149. }
  150. /*
  151. * Lookup the page table entry for a virtual address. Return a pointer
  152. * to the entry and the level of the mapping.
  153. *
  154. * Note: We return pud and pmd either when the entry is marked large
  155. * or when the present bit is not set. Otherwise we would return a
  156. * pointer to a nonexisting mapping.
  157. */
  158. pte_t *lookup_address(unsigned long address, unsigned int *level)
  159. {
  160. pgd_t *pgd = pgd_offset_k(address);
  161. pud_t *pud;
  162. pmd_t *pmd;
  163. *level = PG_LEVEL_NONE;
  164. if (pgd_none(*pgd))
  165. return NULL;
  166. pud = pud_offset(pgd, address);
  167. if (pud_none(*pud))
  168. return NULL;
  169. *level = PG_LEVEL_1G;
  170. if (pud_large(*pud) || !pud_present(*pud))
  171. return (pte_t *)pud;
  172. pmd = pmd_offset(pud, address);
  173. if (pmd_none(*pmd))
  174. return NULL;
  175. *level = PG_LEVEL_2M;
  176. if (pmd_large(*pmd) || !pmd_present(*pmd))
  177. return (pte_t *)pmd;
  178. *level = PG_LEVEL_4K;
  179. return pte_offset_kernel(pmd, address);
  180. }
  181. /*
  182. * Set the new pmd in all the pgds we know about:
  183. */
  184. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  185. {
  186. /* change init_mm */
  187. set_pte_atomic(kpte, pte);
  188. #ifdef CONFIG_X86_32
  189. if (!SHARED_KERNEL_PMD) {
  190. struct page *page;
  191. list_for_each_entry(page, &pgd_list, lru) {
  192. pgd_t *pgd;
  193. pud_t *pud;
  194. pmd_t *pmd;
  195. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  196. pud = pud_offset(pgd, address);
  197. pmd = pmd_offset(pud, address);
  198. set_pte_atomic((pte_t *)pmd, pte);
  199. }
  200. }
  201. #endif
  202. }
  203. static int
  204. try_preserve_large_page(pte_t *kpte, unsigned long address,
  205. struct cpa_data *cpa)
  206. {
  207. unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
  208. pte_t new_pte, old_pte, *tmp;
  209. pgprot_t old_prot, new_prot;
  210. int i, do_split = 1;
  211. unsigned int level;
  212. spin_lock_irqsave(&pgd_lock, flags);
  213. /*
  214. * Check for races, another CPU might have split this page
  215. * up already:
  216. */
  217. tmp = lookup_address(address, &level);
  218. if (tmp != kpte)
  219. goto out_unlock;
  220. switch (level) {
  221. case PG_LEVEL_2M:
  222. psize = PMD_PAGE_SIZE;
  223. pmask = PMD_PAGE_MASK;
  224. break;
  225. #ifdef CONFIG_X86_64
  226. case PG_LEVEL_1G:
  227. psize = PUD_PAGE_SIZE;
  228. pmask = PUD_PAGE_MASK;
  229. break;
  230. #endif
  231. default:
  232. do_split = -EINVAL;
  233. goto out_unlock;
  234. }
  235. /*
  236. * Calculate the number of pages, which fit into this large
  237. * page starting at address:
  238. */
  239. nextpage_addr = (address + psize) & pmask;
  240. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  241. if (numpages < cpa->numpages)
  242. cpa->numpages = numpages;
  243. /*
  244. * We are safe now. Check whether the new pgprot is the same:
  245. */
  246. old_pte = *kpte;
  247. old_prot = new_prot = pte_pgprot(old_pte);
  248. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  249. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  250. /*
  251. * old_pte points to the large page base address. So we need
  252. * to add the offset of the virtual address:
  253. */
  254. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  255. cpa->pfn = pfn;
  256. new_prot = static_protections(new_prot, address, pfn);
  257. /*
  258. * We need to check the full range, whether
  259. * static_protection() requires a different pgprot for one of
  260. * the pages in the range we try to preserve:
  261. */
  262. addr = address + PAGE_SIZE;
  263. pfn++;
  264. for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE, pfn++) {
  265. pgprot_t chk_prot = static_protections(new_prot, addr, pfn);
  266. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  267. goto out_unlock;
  268. }
  269. /*
  270. * If there are no changes, return. maxpages has been updated
  271. * above:
  272. */
  273. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  274. do_split = 0;
  275. goto out_unlock;
  276. }
  277. /*
  278. * We need to change the attributes. Check, whether we can
  279. * change the large page in one go. We request a split, when
  280. * the address is not aligned and the number of pages is
  281. * smaller than the number of pages in the large page. Note
  282. * that we limited the number of possible pages already to
  283. * the number of pages in the large page.
  284. */
  285. if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
  286. /*
  287. * The address is aligned and the number of pages
  288. * covers the full page.
  289. */
  290. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  291. __set_pmd_pte(kpte, address, new_pte);
  292. cpa->flushtlb = 1;
  293. do_split = 0;
  294. }
  295. out_unlock:
  296. spin_unlock_irqrestore(&pgd_lock, flags);
  297. return do_split;
  298. }
  299. static LIST_HEAD(page_pool);
  300. static unsigned long pool_size, pool_pages, pool_low;
  301. static unsigned long pool_used, pool_failed, pool_refill;
  302. static void cpa_fill_pool(void)
  303. {
  304. struct page *p;
  305. gfp_t gfp = GFP_KERNEL;
  306. /* Do not allocate from interrupt context */
  307. if (in_irq() || irqs_disabled())
  308. return;
  309. /*
  310. * Check unlocked. I does not matter when we have one more
  311. * page in the pool. The bit lock avoids recursive pool
  312. * allocations:
  313. */
  314. if (pool_pages >= pool_size || test_and_set_bit_lock(0, &pool_refill))
  315. return;
  316. #ifdef CONFIG_DEBUG_PAGEALLOC
  317. /*
  318. * We could do:
  319. * gfp = in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
  320. * but this fails on !PREEMPT kernels
  321. */
  322. gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
  323. #endif
  324. while (pool_pages < pool_size) {
  325. p = alloc_pages(gfp, 0);
  326. if (!p) {
  327. pool_failed++;
  328. break;
  329. }
  330. spin_lock_irq(&pgd_lock);
  331. list_add(&p->lru, &page_pool);
  332. pool_pages++;
  333. spin_unlock_irq(&pgd_lock);
  334. }
  335. clear_bit_unlock(0, &pool_refill);
  336. }
  337. #define SHIFT_MB (20 - PAGE_SHIFT)
  338. #define ROUND_MB_GB ((1 << 10) - 1)
  339. #define SHIFT_MB_GB 10
  340. #define POOL_PAGES_PER_GB 16
  341. void __init cpa_init(void)
  342. {
  343. struct sysinfo si;
  344. unsigned long gb;
  345. si_meminfo(&si);
  346. /*
  347. * Calculate the number of pool pages:
  348. *
  349. * Convert totalram (nr of pages) to MiB and round to the next
  350. * GiB. Shift MiB to Gib and multiply the result by
  351. * POOL_PAGES_PER_GB:
  352. */
  353. gb = ((si.totalram >> SHIFT_MB) + ROUND_MB_GB) >> SHIFT_MB_GB;
  354. pool_size = POOL_PAGES_PER_GB * gb;
  355. pool_low = pool_size;
  356. cpa_fill_pool();
  357. printk(KERN_DEBUG
  358. "CPA: page pool initialized %lu of %lu pages preallocated\n",
  359. pool_pages, pool_size);
  360. }
  361. static int split_large_page(pte_t *kpte, unsigned long address)
  362. {
  363. unsigned long flags, pfn, pfninc = 1;
  364. unsigned int i, level;
  365. pte_t *pbase, *tmp;
  366. pgprot_t ref_prot;
  367. struct page *base;
  368. /*
  369. * Get a page from the pool. The pool list is protected by the
  370. * pgd_lock, which we have to take anyway for the split
  371. * operation:
  372. */
  373. spin_lock_irqsave(&pgd_lock, flags);
  374. if (list_empty(&page_pool)) {
  375. spin_unlock_irqrestore(&pgd_lock, flags);
  376. return -ENOMEM;
  377. }
  378. base = list_first_entry(&page_pool, struct page, lru);
  379. list_del(&base->lru);
  380. pool_pages--;
  381. if (pool_pages < pool_low)
  382. pool_low = pool_pages;
  383. /*
  384. * Check for races, another CPU might have split this page
  385. * up for us already:
  386. */
  387. tmp = lookup_address(address, &level);
  388. if (tmp != kpte)
  389. goto out_unlock;
  390. pbase = (pte_t *)page_address(base);
  391. #ifdef CONFIG_X86_32
  392. paravirt_alloc_pt(&init_mm, page_to_pfn(base));
  393. #endif
  394. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  395. #ifdef CONFIG_X86_64
  396. if (level == PG_LEVEL_1G) {
  397. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  398. pgprot_val(ref_prot) |= _PAGE_PSE;
  399. }
  400. #endif
  401. /*
  402. * Get the target pfn from the original entry:
  403. */
  404. pfn = pte_pfn(*kpte);
  405. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  406. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  407. /*
  408. * Install the new, split up pagetable. Important details here:
  409. *
  410. * On Intel the NX bit of all levels must be cleared to make a
  411. * page executable. See section 4.13.2 of Intel 64 and IA-32
  412. * Architectures Software Developer's Manual).
  413. *
  414. * Mark the entry present. The current mapping might be
  415. * set to not present, which we preserved above.
  416. */
  417. ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
  418. pgprot_val(ref_prot) |= _PAGE_PRESENT;
  419. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  420. base = NULL;
  421. out_unlock:
  422. /*
  423. * If we dropped out via the lookup_address check under
  424. * pgd_lock then stick the page back into the pool:
  425. */
  426. if (base) {
  427. list_add(&base->lru, &page_pool);
  428. pool_pages++;
  429. } else
  430. pool_used++;
  431. spin_unlock_irqrestore(&pgd_lock, flags);
  432. return 0;
  433. }
  434. static int __change_page_attr(struct cpa_data *cpa, int primary)
  435. {
  436. unsigned long address = cpa->vaddr;
  437. int do_split, err;
  438. unsigned int level;
  439. pte_t *kpte, old_pte;
  440. repeat:
  441. kpte = lookup_address(address, &level);
  442. if (!kpte)
  443. return primary ? -EINVAL : 0;
  444. old_pte = *kpte;
  445. if (!pte_val(old_pte)) {
  446. if (!primary)
  447. return 0;
  448. printk(KERN_WARNING "CPA: called for zero pte. "
  449. "vaddr = %lx cpa->vaddr = %lx\n", address,
  450. cpa->vaddr);
  451. WARN_ON(1);
  452. return -EINVAL;
  453. }
  454. if (level == PG_LEVEL_4K) {
  455. pte_t new_pte;
  456. pgprot_t new_prot = pte_pgprot(old_pte);
  457. unsigned long pfn = pte_pfn(old_pte);
  458. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  459. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  460. new_prot = static_protections(new_prot, address, pfn);
  461. /*
  462. * We need to keep the pfn from the existing PTE,
  463. * after all we're only going to change it's attributes
  464. * not the memory it points to
  465. */
  466. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  467. cpa->pfn = pfn;
  468. /*
  469. * Do we really change anything ?
  470. */
  471. if (pte_val(old_pte) != pte_val(new_pte)) {
  472. set_pte_atomic(kpte, new_pte);
  473. cpa->flushtlb = 1;
  474. }
  475. cpa->numpages = 1;
  476. return 0;
  477. }
  478. /*
  479. * Check, whether we can keep the large page intact
  480. * and just change the pte:
  481. */
  482. do_split = try_preserve_large_page(kpte, address, cpa);
  483. /*
  484. * When the range fits into the existing large page,
  485. * return. cp->numpages and cpa->tlbflush have been updated in
  486. * try_large_page:
  487. */
  488. if (do_split <= 0)
  489. return do_split;
  490. /*
  491. * We have to split the large page:
  492. */
  493. err = split_large_page(kpte, address);
  494. if (!err) {
  495. cpa->flushtlb = 1;
  496. goto repeat;
  497. }
  498. return err;
  499. }
  500. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  501. static int cpa_process_alias(struct cpa_data *cpa)
  502. {
  503. struct cpa_data alias_cpa;
  504. int ret = 0;
  505. if (cpa->pfn > max_pfn_mapped)
  506. return 0;
  507. /*
  508. * No need to redo, when the primary call touched the direct
  509. * mapping already:
  510. */
  511. if (!within(cpa->vaddr, PAGE_OFFSET,
  512. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  513. alias_cpa = *cpa;
  514. alias_cpa.vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
  515. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  516. }
  517. #ifdef CONFIG_X86_64
  518. if (ret)
  519. return ret;
  520. /*
  521. * No need to redo, when the primary call touched the high
  522. * mapping already:
  523. */
  524. if (within(cpa->vaddr, (unsigned long) _text, (unsigned long) _end))
  525. return 0;
  526. /*
  527. * If the physical address is inside the kernel map, we need
  528. * to touch the high mapped kernel as well:
  529. */
  530. if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
  531. return 0;
  532. alias_cpa = *cpa;
  533. alias_cpa.vaddr =
  534. (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
  535. /*
  536. * The high mapping range is imprecise, so ignore the return value.
  537. */
  538. __change_page_attr_set_clr(&alias_cpa, 0);
  539. #endif
  540. return ret;
  541. }
  542. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  543. {
  544. int ret, numpages = cpa->numpages;
  545. while (numpages) {
  546. /*
  547. * Store the remaining nr of pages for the large page
  548. * preservation check.
  549. */
  550. cpa->numpages = numpages;
  551. ret = __change_page_attr(cpa, checkalias);
  552. if (ret)
  553. return ret;
  554. if (checkalias) {
  555. ret = cpa_process_alias(cpa);
  556. if (ret)
  557. return ret;
  558. }
  559. /*
  560. * Adjust the number of pages with the result of the
  561. * CPA operation. Either a large page has been
  562. * preserved or a single page update happened.
  563. */
  564. BUG_ON(cpa->numpages > numpages);
  565. numpages -= cpa->numpages;
  566. cpa->vaddr += cpa->numpages * PAGE_SIZE;
  567. }
  568. return 0;
  569. }
  570. static inline int cache_attr(pgprot_t attr)
  571. {
  572. return pgprot_val(attr) &
  573. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  574. }
  575. static int change_page_attr_set_clr(unsigned long addr, int numpages,
  576. pgprot_t mask_set, pgprot_t mask_clr)
  577. {
  578. struct cpa_data cpa;
  579. int ret, cache, checkalias;
  580. /*
  581. * Check, if we are requested to change a not supported
  582. * feature:
  583. */
  584. mask_set = canon_pgprot(mask_set);
  585. mask_clr = canon_pgprot(mask_clr);
  586. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
  587. return 0;
  588. /* Ensure we are PAGE_SIZE aligned */
  589. if (addr & ~PAGE_MASK) {
  590. addr &= PAGE_MASK;
  591. /*
  592. * People should not be passing in unaligned addresses:
  593. */
  594. WARN_ON_ONCE(1);
  595. }
  596. cpa.vaddr = addr;
  597. cpa.numpages = numpages;
  598. cpa.mask_set = mask_set;
  599. cpa.mask_clr = mask_clr;
  600. cpa.flushtlb = 0;
  601. /* No alias checking for _NX bit modifications */
  602. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  603. ret = __change_page_attr_set_clr(&cpa, checkalias);
  604. /*
  605. * Check whether we really changed something:
  606. */
  607. if (!cpa.flushtlb)
  608. goto out;
  609. /*
  610. * No need to flush, when we did not set any of the caching
  611. * attributes:
  612. */
  613. cache = cache_attr(mask_set);
  614. /*
  615. * On success we use clflush, when the CPU supports it to
  616. * avoid the wbindv. If the CPU does not support it and in the
  617. * error case we fall back to cpa_flush_all (which uses
  618. * wbindv):
  619. */
  620. if (!ret && cpu_has_clflush)
  621. cpa_flush_range(addr, numpages, cache);
  622. else
  623. cpa_flush_all(cache);
  624. out:
  625. cpa_fill_pool();
  626. return ret;
  627. }
  628. static inline int change_page_attr_set(unsigned long addr, int numpages,
  629. pgprot_t mask)
  630. {
  631. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  632. }
  633. static inline int change_page_attr_clear(unsigned long addr, int numpages,
  634. pgprot_t mask)
  635. {
  636. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  637. }
  638. int set_memory_uc(unsigned long addr, int numpages)
  639. {
  640. return change_page_attr_set(addr, numpages,
  641. __pgprot(_PAGE_PCD | _PAGE_PWT));
  642. }
  643. EXPORT_SYMBOL(set_memory_uc);
  644. int set_memory_wb(unsigned long addr, int numpages)
  645. {
  646. return change_page_attr_clear(addr, numpages,
  647. __pgprot(_PAGE_PCD | _PAGE_PWT));
  648. }
  649. EXPORT_SYMBOL(set_memory_wb);
  650. int set_memory_x(unsigned long addr, int numpages)
  651. {
  652. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
  653. }
  654. EXPORT_SYMBOL(set_memory_x);
  655. int set_memory_nx(unsigned long addr, int numpages)
  656. {
  657. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
  658. }
  659. EXPORT_SYMBOL(set_memory_nx);
  660. int set_memory_ro(unsigned long addr, int numpages)
  661. {
  662. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
  663. }
  664. int set_memory_rw(unsigned long addr, int numpages)
  665. {
  666. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
  667. }
  668. int set_memory_np(unsigned long addr, int numpages)
  669. {
  670. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  671. }
  672. int set_pages_uc(struct page *page, int numpages)
  673. {
  674. unsigned long addr = (unsigned long)page_address(page);
  675. return set_memory_uc(addr, numpages);
  676. }
  677. EXPORT_SYMBOL(set_pages_uc);
  678. int set_pages_wb(struct page *page, int numpages)
  679. {
  680. unsigned long addr = (unsigned long)page_address(page);
  681. return set_memory_wb(addr, numpages);
  682. }
  683. EXPORT_SYMBOL(set_pages_wb);
  684. int set_pages_x(struct page *page, int numpages)
  685. {
  686. unsigned long addr = (unsigned long)page_address(page);
  687. return set_memory_x(addr, numpages);
  688. }
  689. EXPORT_SYMBOL(set_pages_x);
  690. int set_pages_nx(struct page *page, int numpages)
  691. {
  692. unsigned long addr = (unsigned long)page_address(page);
  693. return set_memory_nx(addr, numpages);
  694. }
  695. EXPORT_SYMBOL(set_pages_nx);
  696. int set_pages_ro(struct page *page, int numpages)
  697. {
  698. unsigned long addr = (unsigned long)page_address(page);
  699. return set_memory_ro(addr, numpages);
  700. }
  701. int set_pages_rw(struct page *page, int numpages)
  702. {
  703. unsigned long addr = (unsigned long)page_address(page);
  704. return set_memory_rw(addr, numpages);
  705. }
  706. #ifdef CONFIG_DEBUG_PAGEALLOC
  707. static int __set_pages_p(struct page *page, int numpages)
  708. {
  709. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  710. .numpages = numpages,
  711. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  712. .mask_clr = __pgprot(0)};
  713. return __change_page_attr_set_clr(&cpa, 1);
  714. }
  715. static int __set_pages_np(struct page *page, int numpages)
  716. {
  717. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  718. .numpages = numpages,
  719. .mask_set = __pgprot(0),
  720. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
  721. return __change_page_attr_set_clr(&cpa, 1);
  722. }
  723. void kernel_map_pages(struct page *page, int numpages, int enable)
  724. {
  725. if (PageHighMem(page))
  726. return;
  727. if (!enable) {
  728. debug_check_no_locks_freed(page_address(page),
  729. numpages * PAGE_SIZE);
  730. }
  731. /*
  732. * If page allocator is not up yet then do not call c_p_a():
  733. */
  734. if (!debug_pagealloc_enabled)
  735. return;
  736. /*
  737. * The return value is ignored as the calls cannot fail.
  738. * Large pages are kept enabled at boot time, and are
  739. * split up quickly with DEBUG_PAGEALLOC. If a splitup
  740. * fails here (due to temporary memory shortage) no damage
  741. * is done because we just keep the largepage intact up
  742. * to the next attempt when it will likely be split up:
  743. */
  744. if (enable)
  745. __set_pages_p(page, numpages);
  746. else
  747. __set_pages_np(page, numpages);
  748. /*
  749. * We should perform an IPI and flush all tlbs,
  750. * but that can deadlock->flush only current cpu:
  751. */
  752. __flush_tlb_all();
  753. /*
  754. * Try to refill the page pool here. We can do this only after
  755. * the tlb flush.
  756. */
  757. cpa_fill_pool();
  758. }
  759. #ifdef CONFIG_HIBERNATION
  760. bool kernel_page_present(struct page *page)
  761. {
  762. unsigned int level;
  763. pte_t *pte;
  764. if (PageHighMem(page))
  765. return false;
  766. pte = lookup_address((unsigned long)page_address(page), &level);
  767. return (pte_val(*pte) & _PAGE_PRESENT);
  768. }
  769. #endif /* CONFIG_HIBERNATION */
  770. #endif /* CONFIG_DEBUG_PAGEALLOC */
  771. /*
  772. * The testcases use internal knowledge of the implementation that shouldn't
  773. * be exposed to the rest of the kernel. Include these directly here.
  774. */
  775. #ifdef CONFIG_CPA_DEBUG
  776. #include "pageattr-test.c"
  777. #endif