pageattr.c 23 KB

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