pageattr.c 28 KB

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