pageattr.c 24 KB

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