pageattr.c 22 KB

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