pageattr.c 23 KB

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