pageattr.c 21 KB

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