pageattr.c 20 KB

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