pageattr.c 25 KB

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