pageattr.c 25 KB

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