pageattr.c 24 KB

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