pageattr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. /*
  191. * Set the new pmd in all the pgds we know about:
  192. */
  193. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  194. {
  195. /* change init_mm */
  196. set_pte_atomic(kpte, pte);
  197. #ifdef CONFIG_X86_32
  198. if (!SHARED_KERNEL_PMD) {
  199. struct page *page;
  200. list_for_each_entry(page, &pgd_list, lru) {
  201. pgd_t *pgd;
  202. pud_t *pud;
  203. pmd_t *pmd;
  204. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  205. pud = pud_offset(pgd, address);
  206. pmd = pmd_offset(pud, address);
  207. set_pte_atomic((pte_t *)pmd, pte);
  208. }
  209. }
  210. #endif
  211. }
  212. static int
  213. try_preserve_large_page(pte_t *kpte, unsigned long address,
  214. struct cpa_data *cpa)
  215. {
  216. unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
  217. pte_t new_pte, old_pte, *tmp;
  218. pgprot_t old_prot, new_prot;
  219. int i, do_split = 1;
  220. unsigned int level;
  221. if (cpa->force_split)
  222. return 1;
  223. spin_lock_irqsave(&pgd_lock, flags);
  224. /*
  225. * Check for races, another CPU might have split this page
  226. * up already:
  227. */
  228. tmp = lookup_address(address, &level);
  229. if (tmp != kpte)
  230. goto out_unlock;
  231. switch (level) {
  232. case PG_LEVEL_2M:
  233. psize = PMD_PAGE_SIZE;
  234. pmask = PMD_PAGE_MASK;
  235. break;
  236. #ifdef CONFIG_X86_64
  237. case PG_LEVEL_1G:
  238. psize = PUD_PAGE_SIZE;
  239. pmask = PUD_PAGE_MASK;
  240. break;
  241. #endif
  242. default:
  243. do_split = -EINVAL;
  244. goto out_unlock;
  245. }
  246. /*
  247. * Calculate the number of pages, which fit into this large
  248. * page starting at address:
  249. */
  250. nextpage_addr = (address + psize) & pmask;
  251. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  252. if (numpages < cpa->numpages)
  253. cpa->numpages = numpages;
  254. /*
  255. * We are safe now. Check whether the new pgprot is the same:
  256. */
  257. old_pte = *kpte;
  258. old_prot = new_prot = pte_pgprot(old_pte);
  259. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  260. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  261. /*
  262. * old_pte points to the large page base address. So we need
  263. * to add the offset of the virtual address:
  264. */
  265. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  266. cpa->pfn = pfn;
  267. new_prot = static_protections(new_prot, address, pfn);
  268. /*
  269. * We need to check the full range, whether
  270. * static_protection() requires a different pgprot for one of
  271. * the pages in the range we try to preserve:
  272. */
  273. addr = address + PAGE_SIZE;
  274. pfn++;
  275. for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE, pfn++) {
  276. pgprot_t chk_prot = static_protections(new_prot, addr, pfn);
  277. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  278. goto out_unlock;
  279. }
  280. /*
  281. * If there are no changes, return. maxpages has been updated
  282. * above:
  283. */
  284. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  285. do_split = 0;
  286. goto out_unlock;
  287. }
  288. /*
  289. * We need to change the attributes. Check, whether we can
  290. * change the large page in one go. We request a split, when
  291. * the address is not aligned and the number of pages is
  292. * smaller than the number of pages in the large page. Note
  293. * that we limited the number of possible pages already to
  294. * the number of pages in the large page.
  295. */
  296. if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
  297. /*
  298. * The address is aligned and the number of pages
  299. * covers the full page.
  300. */
  301. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  302. __set_pmd_pte(kpte, address, new_pte);
  303. cpa->flushtlb = 1;
  304. do_split = 0;
  305. }
  306. out_unlock:
  307. spin_unlock_irqrestore(&pgd_lock, flags);
  308. return do_split;
  309. }
  310. static LIST_HEAD(page_pool);
  311. static unsigned long pool_size, pool_pages, pool_low;
  312. static unsigned long pool_used, pool_failed;
  313. static void cpa_fill_pool(struct page **ret)
  314. {
  315. gfp_t gfp = GFP_KERNEL;
  316. unsigned long flags;
  317. struct page *p;
  318. /*
  319. * Avoid recursion (on debug-pagealloc) and also signal
  320. * our priority to get to these pagetables:
  321. */
  322. if (current->flags & PF_MEMALLOC)
  323. return;
  324. current->flags |= PF_MEMALLOC;
  325. /*
  326. * Allocate atomically from atomic contexts:
  327. */
  328. if (in_atomic() || irqs_disabled() || debug_pagealloc)
  329. gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
  330. while (pool_pages < pool_size || (ret && !*ret)) {
  331. p = alloc_pages(gfp, 0);
  332. if (!p) {
  333. pool_failed++;
  334. break;
  335. }
  336. /*
  337. * If the call site needs a page right now, provide it:
  338. */
  339. if (ret && !*ret) {
  340. *ret = p;
  341. continue;
  342. }
  343. spin_lock_irqsave(&pgd_lock, flags);
  344. list_add(&p->lru, &page_pool);
  345. pool_pages++;
  346. spin_unlock_irqrestore(&pgd_lock, flags);
  347. }
  348. current->flags &= ~PF_MEMALLOC;
  349. }
  350. #define SHIFT_MB (20 - PAGE_SHIFT)
  351. #define ROUND_MB_GB ((1 << 10) - 1)
  352. #define SHIFT_MB_GB 10
  353. #define POOL_PAGES_PER_GB 16
  354. void __init cpa_init(void)
  355. {
  356. struct sysinfo si;
  357. unsigned long gb;
  358. si_meminfo(&si);
  359. /*
  360. * Calculate the number of pool pages:
  361. *
  362. * Convert totalram (nr of pages) to MiB and round to the next
  363. * GiB. Shift MiB to Gib and multiply the result by
  364. * POOL_PAGES_PER_GB:
  365. */
  366. if (debug_pagealloc) {
  367. gb = ((si.totalram >> SHIFT_MB) + ROUND_MB_GB) >> SHIFT_MB_GB;
  368. pool_size = POOL_PAGES_PER_GB * gb;
  369. } else {
  370. pool_size = 1;
  371. }
  372. pool_low = pool_size;
  373. cpa_fill_pool(NULL);
  374. printk(KERN_DEBUG
  375. "CPA: page pool initialized %lu of %lu pages preallocated\n",
  376. pool_pages, pool_size);
  377. }
  378. static int split_large_page(pte_t *kpte, unsigned long address)
  379. {
  380. unsigned long flags, pfn, pfninc = 1;
  381. unsigned int i, level;
  382. pte_t *pbase, *tmp;
  383. pgprot_t ref_prot;
  384. struct page *base;
  385. /*
  386. * Get a page from the pool. The pool list is protected by the
  387. * pgd_lock, which we have to take anyway for the split
  388. * operation:
  389. */
  390. spin_lock_irqsave(&pgd_lock, flags);
  391. if (list_empty(&page_pool)) {
  392. spin_unlock_irqrestore(&pgd_lock, flags);
  393. base = NULL;
  394. cpa_fill_pool(&base);
  395. if (!base)
  396. return -ENOMEM;
  397. spin_lock_irqsave(&pgd_lock, flags);
  398. } else {
  399. base = list_first_entry(&page_pool, struct page, lru);
  400. list_del(&base->lru);
  401. pool_pages--;
  402. if (pool_pages < pool_low)
  403. pool_low = pool_pages;
  404. }
  405. /*
  406. * Check for races, another CPU might have split this page
  407. * up for us already:
  408. */
  409. tmp = lookup_address(address, &level);
  410. if (tmp != kpte)
  411. goto out_unlock;
  412. pbase = (pte_t *)page_address(base);
  413. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  414. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  415. #ifdef CONFIG_X86_64
  416. if (level == PG_LEVEL_1G) {
  417. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  418. pgprot_val(ref_prot) |= _PAGE_PSE;
  419. }
  420. #endif
  421. /*
  422. * Get the target pfn from the original entry:
  423. */
  424. pfn = pte_pfn(*kpte);
  425. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  426. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  427. /*
  428. * Install the new, split up pagetable. Important details here:
  429. *
  430. * On Intel the NX bit of all levels must be cleared to make a
  431. * page executable. See section 4.13.2 of Intel 64 and IA-32
  432. * Architectures Software Developer's Manual).
  433. *
  434. * Mark the entry present. The current mapping might be
  435. * set to not present, which we preserved above.
  436. */
  437. ref_prot = pte_pgprot(pte_mkexec(pte_clrhuge(*kpte)));
  438. pgprot_val(ref_prot) |= _PAGE_PRESENT;
  439. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  440. base = NULL;
  441. out_unlock:
  442. /*
  443. * If we dropped out via the lookup_address check under
  444. * pgd_lock then stick the page back into the pool:
  445. */
  446. if (base) {
  447. list_add(&base->lru, &page_pool);
  448. pool_pages++;
  449. } else
  450. pool_used++;
  451. spin_unlock_irqrestore(&pgd_lock, flags);
  452. return 0;
  453. }
  454. static int __change_page_attr(struct cpa_data *cpa, int primary)
  455. {
  456. unsigned long address = cpa->vaddr;
  457. int do_split, err;
  458. unsigned int level;
  459. pte_t *kpte, old_pte;
  460. repeat:
  461. kpte = lookup_address(address, &level);
  462. if (!kpte)
  463. return 0;
  464. old_pte = *kpte;
  465. if (!pte_val(old_pte)) {
  466. if (!primary)
  467. return 0;
  468. printk(KERN_WARNING "CPA: called for zero pte. "
  469. "vaddr = %lx cpa->vaddr = %lx\n", address,
  470. cpa->vaddr);
  471. WARN_ON(1);
  472. return -EINVAL;
  473. }
  474. if (level == PG_LEVEL_4K) {
  475. pte_t new_pte;
  476. pgprot_t new_prot = pte_pgprot(old_pte);
  477. unsigned long pfn = pte_pfn(old_pte);
  478. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  479. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  480. new_prot = static_protections(new_prot, address, pfn);
  481. /*
  482. * We need to keep the pfn from the existing PTE,
  483. * after all we're only going to change it's attributes
  484. * not the memory it points to
  485. */
  486. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  487. cpa->pfn = pfn;
  488. /*
  489. * Do we really change anything ?
  490. */
  491. if (pte_val(old_pte) != pte_val(new_pte)) {
  492. set_pte_atomic(kpte, new_pte);
  493. cpa->flushtlb = 1;
  494. }
  495. cpa->numpages = 1;
  496. return 0;
  497. }
  498. /*
  499. * Check, whether we can keep the large page intact
  500. * and just change the pte:
  501. */
  502. do_split = try_preserve_large_page(kpte, address, cpa);
  503. /*
  504. * When the range fits into the existing large page,
  505. * return. cp->numpages and cpa->tlbflush have been updated in
  506. * try_large_page:
  507. */
  508. if (do_split <= 0)
  509. return do_split;
  510. /*
  511. * We have to split the large page:
  512. */
  513. err = split_large_page(kpte, address);
  514. if (!err) {
  515. cpa->flushtlb = 1;
  516. goto repeat;
  517. }
  518. return err;
  519. }
  520. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  521. static int cpa_process_alias(struct cpa_data *cpa)
  522. {
  523. struct cpa_data alias_cpa;
  524. int ret = 0;
  525. if (cpa->pfn > max_pfn_mapped)
  526. return 0;
  527. /*
  528. * No need to redo, when the primary call touched the direct
  529. * mapping already:
  530. */
  531. if (!within(cpa->vaddr, PAGE_OFFSET,
  532. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  533. alias_cpa = *cpa;
  534. alias_cpa.vaddr = (unsigned long) __va(cpa->pfn << PAGE_SHIFT);
  535. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  536. }
  537. #ifdef CONFIG_X86_64
  538. if (ret)
  539. return ret;
  540. /*
  541. * No need to redo, when the primary call touched the high
  542. * mapping already:
  543. */
  544. if (within(cpa->vaddr, (unsigned long) _text, (unsigned long) _end))
  545. return 0;
  546. /*
  547. * If the physical address is inside the kernel map, we need
  548. * to touch the high mapped kernel as well:
  549. */
  550. if (!within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn()))
  551. return 0;
  552. alias_cpa = *cpa;
  553. alias_cpa.vaddr =
  554. (cpa->pfn << PAGE_SHIFT) + __START_KERNEL_map - phys_base;
  555. /*
  556. * The high mapping range is imprecise, so ignore the return value.
  557. */
  558. __change_page_attr_set_clr(&alias_cpa, 0);
  559. #endif
  560. return ret;
  561. }
  562. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  563. {
  564. int ret, numpages = cpa->numpages;
  565. while (numpages) {
  566. /*
  567. * Store the remaining nr of pages for the large page
  568. * preservation check.
  569. */
  570. cpa->numpages = numpages;
  571. ret = __change_page_attr(cpa, checkalias);
  572. if (ret)
  573. return ret;
  574. if (checkalias) {
  575. ret = cpa_process_alias(cpa);
  576. if (ret)
  577. return ret;
  578. }
  579. /*
  580. * Adjust the number of pages with the result of the
  581. * CPA operation. Either a large page has been
  582. * preserved or a single page update happened.
  583. */
  584. BUG_ON(cpa->numpages > numpages);
  585. numpages -= cpa->numpages;
  586. cpa->vaddr += cpa->numpages * PAGE_SIZE;
  587. }
  588. return 0;
  589. }
  590. static inline int cache_attr(pgprot_t attr)
  591. {
  592. return pgprot_val(attr) &
  593. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  594. }
  595. static int change_page_attr_set_clr(unsigned long addr, int numpages,
  596. pgprot_t mask_set, pgprot_t mask_clr,
  597. int force_split)
  598. {
  599. struct cpa_data cpa;
  600. int ret, cache, checkalias;
  601. /*
  602. * Check, if we are requested to change a not supported
  603. * feature:
  604. */
  605. mask_set = canon_pgprot(mask_set);
  606. mask_clr = canon_pgprot(mask_clr);
  607. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  608. return 0;
  609. /* Ensure we are PAGE_SIZE aligned */
  610. if (addr & ~PAGE_MASK) {
  611. addr &= PAGE_MASK;
  612. /*
  613. * People should not be passing in unaligned addresses:
  614. */
  615. WARN_ON_ONCE(1);
  616. }
  617. cpa.vaddr = addr;
  618. cpa.numpages = numpages;
  619. cpa.mask_set = mask_set;
  620. cpa.mask_clr = mask_clr;
  621. cpa.flushtlb = 0;
  622. cpa.force_split = force_split;
  623. /* No alias checking for _NX bit modifications */
  624. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  625. ret = __change_page_attr_set_clr(&cpa, checkalias);
  626. /*
  627. * Check whether we really changed something:
  628. */
  629. if (!cpa.flushtlb)
  630. goto out;
  631. /*
  632. * No need to flush, when we did not set any of the caching
  633. * attributes:
  634. */
  635. cache = cache_attr(mask_set);
  636. /*
  637. * On success we use clflush, when the CPU supports it to
  638. * avoid the wbindv. If the CPU does not support it and in the
  639. * error case we fall back to cpa_flush_all (which uses
  640. * wbindv):
  641. */
  642. if (!ret && cpu_has_clflush)
  643. cpa_flush_range(addr, numpages, cache);
  644. else
  645. cpa_flush_all(cache);
  646. out:
  647. cpa_fill_pool(NULL);
  648. return ret;
  649. }
  650. static inline int change_page_attr_set(unsigned long addr, int numpages,
  651. pgprot_t mask)
  652. {
  653. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0);
  654. }
  655. static inline int change_page_attr_clear(unsigned long addr, int numpages,
  656. pgprot_t mask)
  657. {
  658. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0);
  659. }
  660. int _set_memory_uc(unsigned long addr, int numpages)
  661. {
  662. return change_page_attr_set(addr, numpages,
  663. __pgprot(_PAGE_CACHE_UC));
  664. }
  665. int set_memory_uc(unsigned long addr, int numpages)
  666. {
  667. if (reserve_memtype(addr, addr + numpages * PAGE_SIZE,
  668. _PAGE_CACHE_UC, NULL))
  669. return -EINVAL;
  670. return _set_memory_uc(addr, numpages);
  671. }
  672. EXPORT_SYMBOL(set_memory_uc);
  673. int _set_memory_wc(unsigned long addr, int numpages)
  674. {
  675. return change_page_attr_set(addr, numpages,
  676. __pgprot(_PAGE_CACHE_WC));
  677. }
  678. int set_memory_wc(unsigned long addr, int numpages)
  679. {
  680. if (!pat_wc_enabled)
  681. return set_memory_uc(addr, numpages);
  682. if (reserve_memtype(addr, addr + numpages * PAGE_SIZE,
  683. _PAGE_CACHE_WC, NULL))
  684. return -EINVAL;
  685. return _set_memory_wc(addr, numpages);
  686. }
  687. EXPORT_SYMBOL(set_memory_wc);
  688. int _set_memory_wb(unsigned long addr, int numpages)
  689. {
  690. return change_page_attr_clear(addr, numpages,
  691. __pgprot(_PAGE_CACHE_MASK));
  692. }
  693. int set_memory_wb(unsigned long addr, int numpages)
  694. {
  695. free_memtype(addr, addr + numpages * PAGE_SIZE);
  696. return _set_memory_wb(addr, numpages);
  697. }
  698. EXPORT_SYMBOL(set_memory_wb);
  699. int set_memory_x(unsigned long addr, int numpages)
  700. {
  701. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_NX));
  702. }
  703. EXPORT_SYMBOL(set_memory_x);
  704. int set_memory_nx(unsigned long addr, int numpages)
  705. {
  706. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_NX));
  707. }
  708. EXPORT_SYMBOL(set_memory_nx);
  709. int set_memory_ro(unsigned long addr, int numpages)
  710. {
  711. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_RW));
  712. }
  713. int set_memory_rw(unsigned long addr, int numpages)
  714. {
  715. return change_page_attr_set(addr, numpages, __pgprot(_PAGE_RW));
  716. }
  717. int set_memory_np(unsigned long addr, int numpages)
  718. {
  719. return change_page_attr_clear(addr, numpages, __pgprot(_PAGE_PRESENT));
  720. }
  721. int set_memory_4k(unsigned long addr, int numpages)
  722. {
  723. return change_page_attr_set_clr(addr, numpages, __pgprot(0),
  724. __pgprot(0), 1);
  725. }
  726. int set_pages_uc(struct page *page, int numpages)
  727. {
  728. unsigned long addr = (unsigned long)page_address(page);
  729. return set_memory_uc(addr, numpages);
  730. }
  731. EXPORT_SYMBOL(set_pages_uc);
  732. int set_pages_wb(struct page *page, int numpages)
  733. {
  734. unsigned long addr = (unsigned long)page_address(page);
  735. return set_memory_wb(addr, numpages);
  736. }
  737. EXPORT_SYMBOL(set_pages_wb);
  738. int set_pages_x(struct page *page, int numpages)
  739. {
  740. unsigned long addr = (unsigned long)page_address(page);
  741. return set_memory_x(addr, numpages);
  742. }
  743. EXPORT_SYMBOL(set_pages_x);
  744. int set_pages_nx(struct page *page, int numpages)
  745. {
  746. unsigned long addr = (unsigned long)page_address(page);
  747. return set_memory_nx(addr, numpages);
  748. }
  749. EXPORT_SYMBOL(set_pages_nx);
  750. int set_pages_ro(struct page *page, int numpages)
  751. {
  752. unsigned long addr = (unsigned long)page_address(page);
  753. return set_memory_ro(addr, numpages);
  754. }
  755. int set_pages_rw(struct page *page, int numpages)
  756. {
  757. unsigned long addr = (unsigned long)page_address(page);
  758. return set_memory_rw(addr, numpages);
  759. }
  760. #ifdef CONFIG_DEBUG_PAGEALLOC
  761. static int __set_pages_p(struct page *page, int numpages)
  762. {
  763. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  764. .numpages = numpages,
  765. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  766. .mask_clr = __pgprot(0)};
  767. return __change_page_attr_set_clr(&cpa, 1);
  768. }
  769. static int __set_pages_np(struct page *page, int numpages)
  770. {
  771. struct cpa_data cpa = { .vaddr = (unsigned long) page_address(page),
  772. .numpages = numpages,
  773. .mask_set = __pgprot(0),
  774. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW)};
  775. return __change_page_attr_set_clr(&cpa, 1);
  776. }
  777. void kernel_map_pages(struct page *page, int numpages, int enable)
  778. {
  779. if (PageHighMem(page))
  780. return;
  781. if (!enable) {
  782. debug_check_no_locks_freed(page_address(page),
  783. numpages * PAGE_SIZE);
  784. }
  785. /*
  786. * If page allocator is not up yet then do not call c_p_a():
  787. */
  788. if (!debug_pagealloc_enabled)
  789. return;
  790. /*
  791. * The return value is ignored as the calls cannot fail.
  792. * Large pages are kept enabled at boot time, and are
  793. * split up quickly with DEBUG_PAGEALLOC. If a splitup
  794. * fails here (due to temporary memory shortage) no damage
  795. * is done because we just keep the largepage intact up
  796. * to the next attempt when it will likely be split up:
  797. */
  798. if (enable)
  799. __set_pages_p(page, numpages);
  800. else
  801. __set_pages_np(page, numpages);
  802. /*
  803. * We should perform an IPI and flush all tlbs,
  804. * but that can deadlock->flush only current cpu:
  805. */
  806. __flush_tlb_all();
  807. /*
  808. * Try to refill the page pool here. We can do this only after
  809. * the tlb flush.
  810. */
  811. cpa_fill_pool(NULL);
  812. }
  813. #ifdef CONFIG_DEBUG_FS
  814. static int dpa_show(struct seq_file *m, void *v)
  815. {
  816. seq_puts(m, "DEBUG_PAGEALLOC\n");
  817. seq_printf(m, "pool_size : %lu\n", pool_size);
  818. seq_printf(m, "pool_pages : %lu\n", pool_pages);
  819. seq_printf(m, "pool_low : %lu\n", pool_low);
  820. seq_printf(m, "pool_used : %lu\n", pool_used);
  821. seq_printf(m, "pool_failed : %lu\n", pool_failed);
  822. return 0;
  823. }
  824. static int dpa_open(struct inode *inode, struct file *filp)
  825. {
  826. return single_open(filp, dpa_show, NULL);
  827. }
  828. static const struct file_operations dpa_fops = {
  829. .open = dpa_open,
  830. .read = seq_read,
  831. .llseek = seq_lseek,
  832. .release = single_release,
  833. };
  834. static int __init debug_pagealloc_proc_init(void)
  835. {
  836. struct dentry *de;
  837. de = debugfs_create_file("debug_pagealloc", 0600, NULL, NULL,
  838. &dpa_fops);
  839. if (!de)
  840. return -ENOMEM;
  841. return 0;
  842. }
  843. __initcall(debug_pagealloc_proc_init);
  844. #endif
  845. #ifdef CONFIG_HIBERNATION
  846. bool kernel_page_present(struct page *page)
  847. {
  848. unsigned int level;
  849. pte_t *pte;
  850. if (PageHighMem(page))
  851. return false;
  852. pte = lookup_address((unsigned long)page_address(page), &level);
  853. return (pte_val(*pte) & _PAGE_PRESENT);
  854. }
  855. #endif /* CONFIG_HIBERNATION */
  856. #endif /* CONFIG_DEBUG_PAGEALLOC */
  857. /*
  858. * The testcases use internal knowledge of the implementation that shouldn't
  859. * be exposed to the rest of the kernel. Include these directly here.
  860. */
  861. #ifdef CONFIG_CPA_DEBUG
  862. #include "pageattr-test.c"
  863. #endif