pageattr.c 25 KB

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