pageattr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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, addr;
  215. pte_t new_pte, old_pte, *tmp;
  216. pgprot_t old_prot, new_prot;
  217. int i, 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 = PUD_PAGE_SIZE;
  235. pmask = PUD_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. * We need to check the full range, whether
  260. * static_protection() requires a different pgprot for one of
  261. * the pages in the range we try to preserve:
  262. */
  263. addr = address + PAGE_SIZE;
  264. for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE) {
  265. pgprot_t chk_prot = static_protections(new_prot, addr);
  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(unsigned long address, struct cpa_data *cpa)
  435. {
  436. int do_split, err;
  437. unsigned int level;
  438. struct page *kpte_page;
  439. pte_t *kpte;
  440. repeat:
  441. kpte = lookup_address(address, &level);
  442. if (!kpte)
  443. return -EINVAL;
  444. kpte_page = virt_to_page(kpte);
  445. BUG_ON(PageLRU(kpte_page));
  446. BUG_ON(PageCompound(kpte_page));
  447. if (level == PG_LEVEL_4K) {
  448. pte_t new_pte, old_pte = *kpte;
  449. pgprot_t new_prot = pte_pgprot(old_pte);
  450. if(!pte_val(old_pte)) {
  451. printk(KERN_WARNING "CPA: called for zero pte. "
  452. "vaddr = %lx cpa->vaddr = %lx\n", address,
  453. cpa->vaddr);
  454. WARN_ON(1);
  455. return -EINVAL;
  456. }
  457. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  458. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  459. new_prot = static_protections(new_prot, address);
  460. /*
  461. * We need to keep the pfn from the existing PTE,
  462. * after all we're only going to change it's attributes
  463. * not the memory it points to
  464. */
  465. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  466. /*
  467. * Do we really change anything ?
  468. */
  469. if (pte_val(old_pte) != pte_val(new_pte)) {
  470. set_pte_atomic(kpte, new_pte);
  471. cpa->flushtlb = 1;
  472. }
  473. cpa->numpages = 1;
  474. return 0;
  475. }
  476. /*
  477. * Check, whether we can keep the large page intact
  478. * and just change the pte:
  479. */
  480. do_split = try_preserve_large_page(kpte, address, cpa);
  481. /*
  482. * When the range fits into the existing large page,
  483. * return. cp->numpages and cpa->tlbflush have been updated in
  484. * try_large_page:
  485. */
  486. if (do_split <= 0)
  487. return do_split;
  488. /*
  489. * We have to split the large page:
  490. */
  491. err = split_large_page(kpte, address);
  492. if (!err) {
  493. cpa->flushtlb = 1;
  494. goto repeat;
  495. }
  496. return err;
  497. }
  498. /**
  499. * change_page_attr_addr - Change page table attributes in linear mapping
  500. * @address: Virtual address in linear mapping.
  501. * @prot: New page table attribute (PAGE_*)
  502. *
  503. * Change page attributes of a page in the direct mapping. This is a variant
  504. * of change_page_attr() that also works on memory holes that do not have
  505. * mem_map entry (pfn_valid() is false).
  506. *
  507. * See change_page_attr() documentation for more details.
  508. *
  509. * Modules and drivers should use the set_memory_* APIs instead.
  510. */
  511. static int change_page_attr_addr(struct cpa_data *cpa)
  512. {
  513. int err;
  514. unsigned long address = cpa->vaddr;
  515. #ifdef CONFIG_X86_64
  516. unsigned long phys_addr = __pa(address);
  517. /*
  518. * If we are inside the high mapped kernel range, then we
  519. * fixup the low mapping first. __va() returns the virtual
  520. * address in the linear mapping:
  521. */
  522. if (within(address, HIGH_MAP_START, HIGH_MAP_END))
  523. address = (unsigned long) __va(phys_addr);
  524. #endif
  525. err = __change_page_attr(address, cpa);
  526. if (err)
  527. return err;
  528. #ifdef CONFIG_X86_64
  529. /*
  530. * If the physical address is inside the kernel map, we need
  531. * to touch the high mapped kernel as well:
  532. */
  533. if (within(phys_addr, 0, KERNEL_TEXT_SIZE)) {
  534. /*
  535. * Calc the high mapping address. See __phys_addr()
  536. * for the non obvious details.
  537. *
  538. * Note that NX and other required permissions are
  539. * checked in static_protections().
  540. */
  541. address = phys_addr + HIGH_MAP_START - phys_base;
  542. /*
  543. * Our high aliases are imprecise, because we check
  544. * everything between 0 and KERNEL_TEXT_SIZE, so do
  545. * not propagate lookup failures back to users:
  546. */
  547. __change_page_attr(address, cpa);
  548. }
  549. #endif
  550. return err;
  551. }
  552. static int __change_page_attr_set_clr(struct cpa_data *cpa)
  553. {
  554. int ret, numpages = cpa->numpages;
  555. while (numpages) {
  556. /*
  557. * Store the remaining nr of pages for the large page
  558. * preservation check.
  559. */
  560. cpa->numpages = numpages;
  561. ret = change_page_attr_addr(cpa);
  562. if (ret)
  563. return ret;
  564. /*
  565. * Adjust the number of pages with the result of the
  566. * CPA operation. Either a large page has been
  567. * preserved or a single page update happened.
  568. */
  569. BUG_ON(cpa->numpages > numpages);
  570. numpages -= cpa->numpages;
  571. cpa->vaddr += cpa->numpages * PAGE_SIZE;
  572. }
  573. return 0;
  574. }
  575. static inline int cache_attr(pgprot_t attr)
  576. {
  577. return pgprot_val(attr) &
  578. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  579. }
  580. static int change_page_attr_set_clr(unsigned long addr, int numpages,
  581. pgprot_t mask_set, pgprot_t mask_clr)
  582. {
  583. struct cpa_data cpa;
  584. int ret, cache;
  585. /*
  586. * Check, if we are requested to change a not supported
  587. * feature:
  588. */
  589. mask_set = canon_pgprot(mask_set);
  590. mask_clr = canon_pgprot(mask_clr);
  591. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr))
  592. return 0;
  593. /* Ensure we are PAGE_SIZE aligned */
  594. if (addr & ~PAGE_MASK) {
  595. addr &= PAGE_MASK;
  596. /*
  597. * People should not be passing in unaligned addresses:
  598. */
  599. WARN_ON_ONCE(1);
  600. }
  601. cpa.vaddr = addr;
  602. cpa.numpages = numpages;
  603. cpa.mask_set = mask_set;
  604. cpa.mask_clr = mask_clr;
  605. cpa.flushtlb = 0;
  606. ret = __change_page_attr_set_clr(&cpa);
  607. /*
  608. * Check whether we really changed something:
  609. */
  610. if (!cpa.flushtlb)
  611. goto out;
  612. /*
  613. * No need to flush, when we did not set any of the caching
  614. * attributes:
  615. */
  616. cache = cache_attr(mask_set);
  617. /*
  618. * On success we use clflush, when the CPU supports it to
  619. * avoid the wbindv. If the CPU does not support it and in the
  620. * error case we fall back to cpa_flush_all (which uses
  621. * wbindv):
  622. */
  623. if (!ret && cpu_has_clflush)
  624. cpa_flush_range(addr, numpages, cache);
  625. else
  626. cpa_flush_all(cache);
  627. out:
  628. cpa_fill_pool();
  629. return ret;
  630. }
  631. static inline int change_page_attr_set(unsigned long addr, int numpages,
  632. pgprot_t mask)
  633. {
  634. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0));
  635. }
  636. static inline int change_page_attr_clear(unsigned long addr, int numpages,
  637. pgprot_t mask)
  638. {
  639. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask);
  640. }
  641. int set_memory_uc(unsigned long addr, int numpages)
  642. {
  643. return change_page_attr_set(addr, numpages,
  644. __pgprot(_PAGE_PCD | _PAGE_PWT));
  645. }
  646. EXPORT_SYMBOL(set_memory_uc);
  647. int set_memory_wb(unsigned long addr, int numpages)
  648. {
  649. return change_page_attr_clear(addr, numpages,
  650. __pgprot(_PAGE_PCD | _PAGE_PWT));
  651. }
  652. EXPORT_SYMBOL(set_memory_wb);
  653. int set_memory_x(unsigned long addr, int numpages)
  654. {
  655. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
  656. }
  657. EXPORT_SYMBOL(set_memory_x);
  658. int set_memory_nx(unsigned long addr, int numpages)
  659. {
  660. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
  661. }
  662. EXPORT_SYMBOL(set_memory_nx);
  663. int set_memory_ro(unsigned long addr, int numpages)
  664. {
  665. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
  666. }
  667. int set_memory_rw(unsigned long addr, int numpages)
  668. {
  669. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
  670. }
  671. int set_memory_np(unsigned long addr, int numpages)
  672. {
  673. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  674. }
  675. int set_pages_uc(struct page *page, int numpages)
  676. {
  677. unsigned long addr = (unsigned long)page_address(page);
  678. return set_memory_uc(addr, numpages);
  679. }
  680. EXPORT_SYMBOL(set_pages_uc);
  681. int set_pages_wb(struct page *page, int numpages)
  682. {
  683. unsigned long addr = (unsigned long)page_address(page);
  684. return set_memory_wb(addr, numpages);
  685. }
  686. EXPORT_SYMBOL(set_pages_wb);
  687. int set_pages_x(struct page *page, int numpages)
  688. {
  689. unsigned long addr = (unsigned long)page_address(page);
  690. return set_memory_x(addr, numpages);
  691. }
  692. EXPORT_SYMBOL(set_pages_x);
  693. int set_pages_nx(struct page *page, int numpages)
  694. {
  695. unsigned long addr = (unsigned long)page_address(page);
  696. return set_memory_nx(addr, numpages);
  697. }
  698. EXPORT_SYMBOL(set_pages_nx);
  699. int set_pages_ro(struct page *page, int numpages)
  700. {
  701. unsigned long addr = (unsigned long)page_address(page);
  702. return set_memory_ro(addr, numpages);
  703. }
  704. int set_pages_rw(struct page *page, int numpages)
  705. {
  706. unsigned long addr = (unsigned long)page_address(page);
  707. return set_memory_rw(addr, numpages);
  708. }
  709. #ifdef CONFIG_DEBUG_PAGEALLOC
  710. static int __set_pages_p(struct page *page, int numpages)
  711. {
  712. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  713. .numpages = numpages,
  714. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  715. .mask_clr = __pgprot(0)};
  716. return __change_page_attr_set_clr(&cpa);
  717. }
  718. static int __set_pages_np(struct page *page, int numpages)
  719. {
  720. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  721. .numpages = numpages,
  722. .mask_set = __pgprot(0),
  723. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
  724. return __change_page_attr_set_clr(&cpa);
  725. }
  726. void kernel_map_pages(struct page *page, int numpages, int enable)
  727. {
  728. if (PageHighMem(page))
  729. return;
  730. if (!enable) {
  731. debug_check_no_locks_freed(page_address(page),
  732. numpages * PAGE_SIZE);
  733. }
  734. /*
  735. * If page allocator is not up yet then do not call c_p_a():
  736. */
  737. if (!debug_pagealloc_enabled)
  738. return;
  739. /*
  740. * The return value is ignored as the calls cannot fail.
  741. * Large pages are kept enabled at boot time, and are
  742. * split up quickly with DEBUG_PAGEALLOC. If a splitup
  743. * fails here (due to temporary memory shortage) no damage
  744. * is done because we just keep the largepage intact up
  745. * to the next attempt when it will likely be split up:
  746. */
  747. if (enable)
  748. __set_pages_p(page, numpages);
  749. else
  750. __set_pages_np(page, numpages);
  751. /*
  752. * We should perform an IPI and flush all tlbs,
  753. * but that can deadlock->flush only current cpu:
  754. */
  755. __flush_tlb_all();
  756. /*
  757. * Try to refill the page pool here. We can do this only after
  758. * the tlb flush.
  759. */
  760. cpa_fill_pool();
  761. }
  762. #endif
  763. /*
  764. * The testcases use internal knowledge of the implementation that shouldn't
  765. * be exposed to the rest of the kernel. Include these directly here.
  766. */
  767. #ifdef CONFIG_CPA_DEBUG
  768. #include "pageattr-test.c"
  769. #endif