pageattr.c 28 KB

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