pageattr.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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/mm.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/pfn.h>
  14. #include <linux/percpu.h>
  15. #include <linux/gfp.h>
  16. #include <asm/e820.h>
  17. #include <asm/processor.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/sections.h>
  20. #include <asm/setup.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/proto.h>
  24. #include <asm/pat.h>
  25. /*
  26. * The current flushing context - we pass it instead of 5 arguments:
  27. */
  28. struct cpa_data {
  29. unsigned long *vaddr;
  30. pgprot_t mask_set;
  31. pgprot_t mask_clr;
  32. int numpages;
  33. int flags;
  34. unsigned long pfn;
  35. unsigned force_split : 1;
  36. int curpage;
  37. struct page **pages;
  38. };
  39. /*
  40. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  41. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  42. * entries change the page attribute in parallel to some other cpu
  43. * splitting a large page entry along with changing the attribute.
  44. */
  45. static DEFINE_SPINLOCK(cpa_lock);
  46. #define CPA_FLUSHTLB 1
  47. #define CPA_ARRAY 2
  48. #define CPA_PAGES_ARRAY 4
  49. #ifdef CONFIG_PROC_FS
  50. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  51. void update_page_count(int level, unsigned long pages)
  52. {
  53. unsigned long flags;
  54. /* Protect against CPA */
  55. spin_lock_irqsave(&pgd_lock, flags);
  56. direct_pages_count[level] += pages;
  57. spin_unlock_irqrestore(&pgd_lock, flags);
  58. }
  59. static void split_page_count(int level)
  60. {
  61. direct_pages_count[level]--;
  62. direct_pages_count[level - 1] += PTRS_PER_PTE;
  63. }
  64. void arch_report_meminfo(struct seq_file *m)
  65. {
  66. seq_printf(m, "DirectMap4k: %8lu kB\n",
  67. direct_pages_count[PG_LEVEL_4K] << 2);
  68. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  69. seq_printf(m, "DirectMap2M: %8lu kB\n",
  70. direct_pages_count[PG_LEVEL_2M] << 11);
  71. #else
  72. seq_printf(m, "DirectMap4M: %8lu kB\n",
  73. direct_pages_count[PG_LEVEL_2M] << 12);
  74. #endif
  75. #ifdef CONFIG_X86_64
  76. if (direct_gbpages)
  77. seq_printf(m, "DirectMap1G: %8lu kB\n",
  78. direct_pages_count[PG_LEVEL_1G] << 20);
  79. #endif
  80. }
  81. #else
  82. static inline void split_page_count(int level) { }
  83. #endif
  84. #ifdef CONFIG_X86_64
  85. static inline unsigned long highmap_start_pfn(void)
  86. {
  87. return __pa(_text) >> PAGE_SHIFT;
  88. }
  89. static inline unsigned long highmap_end_pfn(void)
  90. {
  91. return __pa(roundup(_brk_end, PMD_SIZE)) >> PAGE_SHIFT;
  92. }
  93. #endif
  94. #ifdef CONFIG_DEBUG_PAGEALLOC
  95. # define debug_pagealloc 1
  96. #else
  97. # define debug_pagealloc 0
  98. #endif
  99. static inline int
  100. within(unsigned long addr, unsigned long start, unsigned long end)
  101. {
  102. return addr >= start && addr < end;
  103. }
  104. /*
  105. * Flushing functions
  106. */
  107. /**
  108. * clflush_cache_range - flush a cache range with clflush
  109. * @addr: virtual start address
  110. * @size: number of bytes to flush
  111. *
  112. * clflush is an unordered instruction which needs fencing with mfence
  113. * to avoid ordering issues.
  114. */
  115. void clflush_cache_range(void *vaddr, unsigned int size)
  116. {
  117. void *vend = vaddr + size - 1;
  118. mb();
  119. for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
  120. clflush(vaddr);
  121. /*
  122. * Flush any possible final partial cacheline:
  123. */
  124. clflush(vend);
  125. mb();
  126. }
  127. EXPORT_SYMBOL_GPL(clflush_cache_range);
  128. static void __cpa_flush_all(void *arg)
  129. {
  130. unsigned long cache = (unsigned long)arg;
  131. /*
  132. * Flush all to work around Errata in early athlons regarding
  133. * large page flushing.
  134. */
  135. __flush_tlb_all();
  136. if (cache && boot_cpu_data.x86 >= 4)
  137. wbinvd();
  138. }
  139. static void cpa_flush_all(unsigned long cache)
  140. {
  141. BUG_ON(irqs_disabled());
  142. on_each_cpu(__cpa_flush_all, (void *) cache, 1);
  143. }
  144. static void __cpa_flush_range(void *arg)
  145. {
  146. /*
  147. * We could optimize that further and do individual per page
  148. * tlb invalidates for a low number of pages. Caveat: we must
  149. * flush the high aliases on 64bit as well.
  150. */
  151. __flush_tlb_all();
  152. }
  153. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  154. {
  155. unsigned int i, level;
  156. unsigned long addr;
  157. BUG_ON(irqs_disabled());
  158. WARN_ON(PAGE_ALIGN(start) != start);
  159. on_each_cpu(__cpa_flush_range, NULL, 1);
  160. if (!cache)
  161. return;
  162. /*
  163. * We only need to flush on one CPU,
  164. * clflush is a MESI-coherent instruction that
  165. * will cause all other CPUs to flush the same
  166. * cachelines:
  167. */
  168. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  169. pte_t *pte = lookup_address(addr, &level);
  170. /*
  171. * Only flush present addresses:
  172. */
  173. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  174. clflush_cache_range((void *) addr, PAGE_SIZE);
  175. }
  176. }
  177. static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  178. int in_flags, struct page **pages)
  179. {
  180. unsigned int i, level;
  181. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  182. BUG_ON(irqs_disabled());
  183. on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
  184. if (!cache || do_wbinvd)
  185. return;
  186. /*
  187. * We only need to flush on one CPU,
  188. * clflush is a MESI-coherent instruction that
  189. * will cause all other CPUs to flush the same
  190. * cachelines:
  191. */
  192. for (i = 0; i < numpages; i++) {
  193. unsigned long addr;
  194. pte_t *pte;
  195. if (in_flags & CPA_PAGES_ARRAY)
  196. addr = (unsigned long)page_address(pages[i]);
  197. else
  198. addr = start[i];
  199. pte = lookup_address(addr, &level);
  200. /*
  201. * Only flush present addresses:
  202. */
  203. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  204. clflush_cache_range((void *)addr, PAGE_SIZE);
  205. }
  206. }
  207. /*
  208. * Certain areas of memory on x86 require very specific protection flags,
  209. * for example the BIOS area or kernel text. Callers don't always get this
  210. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  211. * checks and fixes these known static required protection bits.
  212. */
  213. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  214. unsigned long pfn)
  215. {
  216. pgprot_t forbidden = __pgprot(0);
  217. /*
  218. * The BIOS area between 640k and 1Mb needs to be executable for
  219. * PCI BIOS based config access (CONFIG_PCI_GOBIOS) support.
  220. */
  221. if (within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  222. pgprot_val(forbidden) |= _PAGE_NX;
  223. /*
  224. * The kernel text needs to be executable for obvious reasons
  225. * Does not cover __inittext since that is gone later on. On
  226. * 64bit we do not enforce !NX on the low mapping
  227. */
  228. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  229. pgprot_val(forbidden) |= _PAGE_NX;
  230. /*
  231. * The .rodata section needs to be read-only. Using the pfn
  232. * catches all aliases.
  233. */
  234. if (within(pfn, __pa((unsigned long)__start_rodata) >> PAGE_SHIFT,
  235. __pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
  236. pgprot_val(forbidden) |= _PAGE_RW;
  237. #if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
  238. /*
  239. * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
  240. * kernel text mappings for the large page aligned text, rodata sections
  241. * will be always read-only. For the kernel identity mappings covering
  242. * the holes caused by this alignment can be anything that user asks.
  243. *
  244. * This will preserve the large page mappings for kernel text/data
  245. * at no extra cost.
  246. */
  247. if (kernel_set_to_readonly &&
  248. within(address, (unsigned long)_text,
  249. (unsigned long)__end_rodata_hpage_align)) {
  250. unsigned int level;
  251. /*
  252. * Don't enforce the !RW mapping for the kernel text mapping,
  253. * if the current mapping is already using small page mapping.
  254. * No need to work hard to preserve large page mappings in this
  255. * case.
  256. *
  257. * This also fixes the Linux Xen paravirt guest boot failure
  258. * (because of unexpected read-only mappings for kernel identity
  259. * mappings). In this paravirt guest case, the kernel text
  260. * mapping and the kernel identity mapping share the same
  261. * page-table pages. Thus we can't really use different
  262. * protections for the kernel text and identity mappings. Also,
  263. * these shared mappings are made of small page mappings.
  264. * Thus this don't enforce !RW mapping for small page kernel
  265. * text mapping logic will help Linux Xen parvirt guest boot
  266. * aswell.
  267. */
  268. if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
  269. pgprot_val(forbidden) |= _PAGE_RW;
  270. }
  271. #endif
  272. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  273. return prot;
  274. }
  275. /*
  276. * Lookup the page table entry for a virtual address. Return a pointer
  277. * to the entry and the level of the mapping.
  278. *
  279. * Note: We return pud and pmd either when the entry is marked large
  280. * or when the present bit is not set. Otherwise we would return a
  281. * pointer to a nonexisting mapping.
  282. */
  283. pte_t *lookup_address(unsigned long address, unsigned int *level)
  284. {
  285. pgd_t *pgd = pgd_offset_k(address);
  286. pud_t *pud;
  287. pmd_t *pmd;
  288. *level = PG_LEVEL_NONE;
  289. if (pgd_none(*pgd))
  290. return NULL;
  291. pud = pud_offset(pgd, address);
  292. if (pud_none(*pud))
  293. return NULL;
  294. *level = PG_LEVEL_1G;
  295. if (pud_large(*pud) || !pud_present(*pud))
  296. return (pte_t *)pud;
  297. pmd = pmd_offset(pud, address);
  298. if (pmd_none(*pmd))
  299. return NULL;
  300. *level = PG_LEVEL_2M;
  301. if (pmd_large(*pmd) || !pmd_present(*pmd))
  302. return (pte_t *)pmd;
  303. *level = PG_LEVEL_4K;
  304. return pte_offset_kernel(pmd, address);
  305. }
  306. EXPORT_SYMBOL_GPL(lookup_address);
  307. /*
  308. * Set the new pmd in all the pgds we know about:
  309. */
  310. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  311. {
  312. /* change init_mm */
  313. set_pte_atomic(kpte, pte);
  314. #ifdef CONFIG_X86_32
  315. if (!SHARED_KERNEL_PMD) {
  316. struct page *page;
  317. list_for_each_entry(page, &pgd_list, lru) {
  318. pgd_t *pgd;
  319. pud_t *pud;
  320. pmd_t *pmd;
  321. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  322. pud = pud_offset(pgd, address);
  323. pmd = pmd_offset(pud, address);
  324. set_pte_atomic((pte_t *)pmd, pte);
  325. }
  326. }
  327. #endif
  328. }
  329. static int
  330. try_preserve_large_page(pte_t *kpte, unsigned long address,
  331. struct cpa_data *cpa)
  332. {
  333. unsigned long nextpage_addr, numpages, pmask, psize, flags, addr, pfn;
  334. pte_t new_pte, old_pte, *tmp;
  335. pgprot_t old_prot, new_prot;
  336. int i, do_split = 1;
  337. unsigned int level;
  338. if (cpa->force_split)
  339. return 1;
  340. spin_lock_irqsave(&pgd_lock, flags);
  341. /*
  342. * Check for races, another CPU might have split this page
  343. * up already:
  344. */
  345. tmp = lookup_address(address, &level);
  346. if (tmp != kpte)
  347. goto out_unlock;
  348. switch (level) {
  349. case PG_LEVEL_2M:
  350. psize = PMD_PAGE_SIZE;
  351. pmask = PMD_PAGE_MASK;
  352. break;
  353. #ifdef CONFIG_X86_64
  354. case PG_LEVEL_1G:
  355. psize = PUD_PAGE_SIZE;
  356. pmask = PUD_PAGE_MASK;
  357. break;
  358. #endif
  359. default:
  360. do_split = -EINVAL;
  361. goto out_unlock;
  362. }
  363. /*
  364. * Calculate the number of pages, which fit into this large
  365. * page starting at address:
  366. */
  367. nextpage_addr = (address + psize) & pmask;
  368. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  369. if (numpages < cpa->numpages)
  370. cpa->numpages = numpages;
  371. /*
  372. * We are safe now. Check whether the new pgprot is the same:
  373. */
  374. old_pte = *kpte;
  375. old_prot = new_prot = pte_pgprot(old_pte);
  376. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  377. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  378. /*
  379. * old_pte points to the large page base address. So we need
  380. * to add the offset of the virtual address:
  381. */
  382. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  383. cpa->pfn = pfn;
  384. new_prot = static_protections(new_prot, address, pfn);
  385. /*
  386. * We need to check the full range, whether
  387. * static_protection() requires a different pgprot for one of
  388. * the pages in the range we try to preserve:
  389. */
  390. addr = address + PAGE_SIZE;
  391. pfn++;
  392. for (i = 1; i < cpa->numpages; i++, addr += PAGE_SIZE, pfn++) {
  393. pgprot_t chk_prot = static_protections(new_prot, addr, pfn);
  394. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  395. goto out_unlock;
  396. }
  397. /*
  398. * If there are no changes, return. maxpages has been updated
  399. * above:
  400. */
  401. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  402. do_split = 0;
  403. goto out_unlock;
  404. }
  405. /*
  406. * We need to change the attributes. Check, whether we can
  407. * change the large page in one go. We request a split, when
  408. * the address is not aligned and the number of pages is
  409. * smaller than the number of pages in the large page. Note
  410. * that we limited the number of possible pages already to
  411. * the number of pages in the large page.
  412. */
  413. if (address == (nextpage_addr - psize) && cpa->numpages == numpages) {
  414. /*
  415. * The address is aligned and the number of pages
  416. * covers the full page.
  417. */
  418. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  419. __set_pmd_pte(kpte, address, new_pte);
  420. cpa->flags |= CPA_FLUSHTLB;
  421. do_split = 0;
  422. }
  423. out_unlock:
  424. spin_unlock_irqrestore(&pgd_lock, flags);
  425. return do_split;
  426. }
  427. static int split_large_page(pte_t *kpte, unsigned long address)
  428. {
  429. unsigned long flags, pfn, pfninc = 1;
  430. unsigned int i, level;
  431. pte_t *pbase, *tmp;
  432. pgprot_t ref_prot;
  433. struct page *base;
  434. if (!debug_pagealloc)
  435. spin_unlock(&cpa_lock);
  436. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  437. if (!debug_pagealloc)
  438. spin_lock(&cpa_lock);
  439. if (!base)
  440. return -ENOMEM;
  441. spin_lock_irqsave(&pgd_lock, flags);
  442. /*
  443. * Check for races, another CPU might have split this page
  444. * up for us already:
  445. */
  446. tmp = lookup_address(address, &level);
  447. if (tmp != kpte)
  448. goto out_unlock;
  449. pbase = (pte_t *)page_address(base);
  450. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  451. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  452. /*
  453. * If we ever want to utilize the PAT bit, we need to
  454. * update this function to make sure it's converted from
  455. * bit 12 to bit 7 when we cross from the 2MB level to
  456. * the 4K level:
  457. */
  458. WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
  459. #ifdef CONFIG_X86_64
  460. if (level == PG_LEVEL_1G) {
  461. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  462. pgprot_val(ref_prot) |= _PAGE_PSE;
  463. }
  464. #endif
  465. /*
  466. * Get the target pfn from the original entry:
  467. */
  468. pfn = pte_pfn(*kpte);
  469. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  470. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  471. if (address >= (unsigned long)__va(0) &&
  472. address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
  473. split_page_count(level);
  474. #ifdef CONFIG_X86_64
  475. if (address >= (unsigned long)__va(1UL<<32) &&
  476. address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
  477. split_page_count(level);
  478. #endif
  479. /*
  480. * Install the new, split up pagetable.
  481. *
  482. * We use the standard kernel pagetable protections for the new
  483. * pagetable protections, the actual ptes set above control the
  484. * primary protection behavior:
  485. */
  486. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  487. /*
  488. * Intel Atom errata AAH41 workaround.
  489. *
  490. * The real fix should be in hw or in a microcode update, but
  491. * we also probabilistically try to reduce the window of having
  492. * a large TLB mixed with 4K TLBs while instruction fetches are
  493. * going on.
  494. */
  495. __flush_tlb_all();
  496. base = NULL;
  497. out_unlock:
  498. /*
  499. * If we dropped out via the lookup_address check under
  500. * pgd_lock then stick the page back into the pool:
  501. */
  502. if (base)
  503. __free_page(base);
  504. spin_unlock_irqrestore(&pgd_lock, flags);
  505. return 0;
  506. }
  507. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  508. int primary)
  509. {
  510. /*
  511. * Ignore all non primary paths.
  512. */
  513. if (!primary)
  514. return 0;
  515. /*
  516. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  517. * to have holes.
  518. * Also set numpages to '1' indicating that we processed cpa req for
  519. * one virtual address page and its pfn. TBD: numpages can be set based
  520. * on the initial value and the level returned by lookup_address().
  521. */
  522. if (within(vaddr, PAGE_OFFSET,
  523. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  524. cpa->numpages = 1;
  525. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  526. return 0;
  527. } else {
  528. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  529. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  530. *cpa->vaddr);
  531. return -EFAULT;
  532. }
  533. }
  534. static int __change_page_attr(struct cpa_data *cpa, int primary)
  535. {
  536. unsigned long address;
  537. int do_split, err;
  538. unsigned int level;
  539. pte_t *kpte, old_pte;
  540. if (cpa->flags & CPA_PAGES_ARRAY) {
  541. struct page *page = cpa->pages[cpa->curpage];
  542. if (unlikely(PageHighMem(page)))
  543. return 0;
  544. address = (unsigned long)page_address(page);
  545. } else if (cpa->flags & CPA_ARRAY)
  546. address = cpa->vaddr[cpa->curpage];
  547. else
  548. address = *cpa->vaddr;
  549. repeat:
  550. kpte = lookup_address(address, &level);
  551. if (!kpte)
  552. return __cpa_process_fault(cpa, address, primary);
  553. old_pte = *kpte;
  554. if (!pte_val(old_pte))
  555. return __cpa_process_fault(cpa, address, primary);
  556. if (level == PG_LEVEL_4K) {
  557. pte_t new_pte;
  558. pgprot_t new_prot = pte_pgprot(old_pte);
  559. unsigned long pfn = pte_pfn(old_pte);
  560. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  561. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  562. new_prot = static_protections(new_prot, address, pfn);
  563. /*
  564. * We need to keep the pfn from the existing PTE,
  565. * after all we're only going to change it's attributes
  566. * not the memory it points to
  567. */
  568. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  569. cpa->pfn = pfn;
  570. /*
  571. * Do we really change anything ?
  572. */
  573. if (pte_val(old_pte) != pte_val(new_pte)) {
  574. set_pte_atomic(kpte, new_pte);
  575. cpa->flags |= CPA_FLUSHTLB;
  576. }
  577. cpa->numpages = 1;
  578. return 0;
  579. }
  580. /*
  581. * Check, whether we can keep the large page intact
  582. * and just change the pte:
  583. */
  584. do_split = try_preserve_large_page(kpte, address, cpa);
  585. /*
  586. * When the range fits into the existing large page,
  587. * return. cp->numpages and cpa->tlbflush have been updated in
  588. * try_large_page:
  589. */
  590. if (do_split <= 0)
  591. return do_split;
  592. /*
  593. * We have to split the large page:
  594. */
  595. err = split_large_page(kpte, address);
  596. if (!err) {
  597. /*
  598. * Do a global flush tlb after splitting the large page
  599. * and before we do the actual change page attribute in the PTE.
  600. *
  601. * With out this, we violate the TLB application note, that says
  602. * "The TLBs may contain both ordinary and large-page
  603. * translations for a 4-KByte range of linear addresses. This
  604. * may occur if software modifies the paging structures so that
  605. * the page size used for the address range changes. If the two
  606. * translations differ with respect to page frame or attributes
  607. * (e.g., permissions), processor behavior is undefined and may
  608. * be implementation-specific."
  609. *
  610. * We do this global tlb flush inside the cpa_lock, so that we
  611. * don't allow any other cpu, with stale tlb entries change the
  612. * page attribute in parallel, that also falls into the
  613. * just split large page entry.
  614. */
  615. flush_tlb_all();
  616. goto repeat;
  617. }
  618. return err;
  619. }
  620. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  621. static int cpa_process_alias(struct cpa_data *cpa)
  622. {
  623. struct cpa_data alias_cpa;
  624. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  625. unsigned long vaddr;
  626. int ret;
  627. if (cpa->pfn >= max_pfn_mapped)
  628. return 0;
  629. #ifdef CONFIG_X86_64
  630. if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
  631. return 0;
  632. #endif
  633. /*
  634. * No need to redo, when the primary call touched the direct
  635. * mapping already:
  636. */
  637. if (cpa->flags & CPA_PAGES_ARRAY) {
  638. struct page *page = cpa->pages[cpa->curpage];
  639. if (unlikely(PageHighMem(page)))
  640. return 0;
  641. vaddr = (unsigned long)page_address(page);
  642. } else if (cpa->flags & CPA_ARRAY)
  643. vaddr = cpa->vaddr[cpa->curpage];
  644. else
  645. vaddr = *cpa->vaddr;
  646. if (!(within(vaddr, PAGE_OFFSET,
  647. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  648. alias_cpa = *cpa;
  649. alias_cpa.vaddr = &laddr;
  650. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  651. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  652. if (ret)
  653. return ret;
  654. }
  655. #ifdef CONFIG_X86_64
  656. /*
  657. * If the primary call didn't touch the high mapping already
  658. * and the physical address is inside the kernel map, we need
  659. * to touch the high mapped kernel as well:
  660. */
  661. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  662. within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
  663. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  664. __START_KERNEL_map - phys_base;
  665. alias_cpa = *cpa;
  666. alias_cpa.vaddr = &temp_cpa_vaddr;
  667. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  668. /*
  669. * The high mapping range is imprecise, so ignore the
  670. * return value.
  671. */
  672. __change_page_attr_set_clr(&alias_cpa, 0);
  673. }
  674. #endif
  675. return 0;
  676. }
  677. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  678. {
  679. int ret, numpages = cpa->numpages;
  680. while (numpages) {
  681. /*
  682. * Store the remaining nr of pages for the large page
  683. * preservation check.
  684. */
  685. cpa->numpages = numpages;
  686. /* for array changes, we can't use large page */
  687. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  688. cpa->numpages = 1;
  689. if (!debug_pagealloc)
  690. spin_lock(&cpa_lock);
  691. ret = __change_page_attr(cpa, checkalias);
  692. if (!debug_pagealloc)
  693. spin_unlock(&cpa_lock);
  694. if (ret)
  695. return ret;
  696. if (checkalias) {
  697. ret = cpa_process_alias(cpa);
  698. if (ret)
  699. return ret;
  700. }
  701. /*
  702. * Adjust the number of pages with the result of the
  703. * CPA operation. Either a large page has been
  704. * preserved or a single page update happened.
  705. */
  706. BUG_ON(cpa->numpages > numpages);
  707. numpages -= cpa->numpages;
  708. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  709. cpa->curpage++;
  710. else
  711. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  712. }
  713. return 0;
  714. }
  715. static inline int cache_attr(pgprot_t attr)
  716. {
  717. return pgprot_val(attr) &
  718. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  719. }
  720. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  721. pgprot_t mask_set, pgprot_t mask_clr,
  722. int force_split, int in_flag,
  723. struct page **pages)
  724. {
  725. struct cpa_data cpa;
  726. int ret, cache, checkalias;
  727. unsigned long baddr = 0;
  728. /*
  729. * Check, if we are requested to change a not supported
  730. * feature:
  731. */
  732. mask_set = canon_pgprot(mask_set);
  733. mask_clr = canon_pgprot(mask_clr);
  734. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  735. return 0;
  736. /* Ensure we are PAGE_SIZE aligned */
  737. if (in_flag & CPA_ARRAY) {
  738. int i;
  739. for (i = 0; i < numpages; i++) {
  740. if (addr[i] & ~PAGE_MASK) {
  741. addr[i] &= PAGE_MASK;
  742. WARN_ON_ONCE(1);
  743. }
  744. }
  745. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  746. /*
  747. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  748. * No need to cehck in that case
  749. */
  750. if (*addr & ~PAGE_MASK) {
  751. *addr &= PAGE_MASK;
  752. /*
  753. * People should not be passing in unaligned addresses:
  754. */
  755. WARN_ON_ONCE(1);
  756. }
  757. /*
  758. * Save address for cache flush. *addr is modified in the call
  759. * to __change_page_attr_set_clr() below.
  760. */
  761. baddr = *addr;
  762. }
  763. /* Must avoid aliasing mappings in the highmem code */
  764. kmap_flush_unused();
  765. vm_unmap_aliases();
  766. cpa.vaddr = addr;
  767. cpa.pages = pages;
  768. cpa.numpages = numpages;
  769. cpa.mask_set = mask_set;
  770. cpa.mask_clr = mask_clr;
  771. cpa.flags = 0;
  772. cpa.curpage = 0;
  773. cpa.force_split = force_split;
  774. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  775. cpa.flags |= in_flag;
  776. /* No alias checking for _NX bit modifications */
  777. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  778. ret = __change_page_attr_set_clr(&cpa, checkalias);
  779. /*
  780. * Check whether we really changed something:
  781. */
  782. if (!(cpa.flags & CPA_FLUSHTLB))
  783. goto out;
  784. /*
  785. * No need to flush, when we did not set any of the caching
  786. * attributes:
  787. */
  788. cache = cache_attr(mask_set);
  789. /*
  790. * On success we use clflush, when the CPU supports it to
  791. * avoid the wbindv. If the CPU does not support it and in the
  792. * error case we fall back to cpa_flush_all (which uses
  793. * wbindv):
  794. */
  795. if (!ret && cpu_has_clflush) {
  796. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  797. cpa_flush_array(addr, numpages, cache,
  798. cpa.flags, pages);
  799. } else
  800. cpa_flush_range(baddr, numpages, cache);
  801. } else
  802. cpa_flush_all(cache);
  803. out:
  804. return ret;
  805. }
  806. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  807. pgprot_t mask, int array)
  808. {
  809. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  810. (array ? CPA_ARRAY : 0), NULL);
  811. }
  812. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  813. pgprot_t mask, int array)
  814. {
  815. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  816. (array ? CPA_ARRAY : 0), NULL);
  817. }
  818. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  819. pgprot_t mask)
  820. {
  821. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  822. CPA_PAGES_ARRAY, pages);
  823. }
  824. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  825. pgprot_t mask)
  826. {
  827. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  828. CPA_PAGES_ARRAY, pages);
  829. }
  830. int _set_memory_uc(unsigned long addr, int numpages)
  831. {
  832. /*
  833. * for now UC MINUS. see comments in ioremap_nocache()
  834. */
  835. return change_page_attr_set(&addr, numpages,
  836. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  837. }
  838. int set_memory_uc(unsigned long addr, int numpages)
  839. {
  840. int ret;
  841. /*
  842. * for now UC MINUS. see comments in ioremap_nocache()
  843. */
  844. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  845. _PAGE_CACHE_UC_MINUS, NULL);
  846. if (ret)
  847. goto out_err;
  848. ret = _set_memory_uc(addr, numpages);
  849. if (ret)
  850. goto out_free;
  851. return 0;
  852. out_free:
  853. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  854. out_err:
  855. return ret;
  856. }
  857. EXPORT_SYMBOL(set_memory_uc);
  858. int _set_memory_array(unsigned long *addr, int addrinarray,
  859. unsigned long new_type)
  860. {
  861. int i, j;
  862. int ret;
  863. /*
  864. * for now UC MINUS. see comments in ioremap_nocache()
  865. */
  866. for (i = 0; i < addrinarray; i++) {
  867. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  868. new_type, NULL);
  869. if (ret)
  870. goto out_free;
  871. }
  872. ret = change_page_attr_set(addr, addrinarray,
  873. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  874. if (!ret && new_type == _PAGE_CACHE_WC)
  875. ret = change_page_attr_set_clr(addr, addrinarray,
  876. __pgprot(_PAGE_CACHE_WC),
  877. __pgprot(_PAGE_CACHE_MASK),
  878. 0, CPA_ARRAY, NULL);
  879. if (ret)
  880. goto out_free;
  881. return 0;
  882. out_free:
  883. for (j = 0; j < i; j++)
  884. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  885. return ret;
  886. }
  887. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  888. {
  889. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
  890. }
  891. EXPORT_SYMBOL(set_memory_array_uc);
  892. int set_memory_array_wc(unsigned long *addr, int addrinarray)
  893. {
  894. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
  895. }
  896. EXPORT_SYMBOL(set_memory_array_wc);
  897. int _set_memory_wc(unsigned long addr, int numpages)
  898. {
  899. int ret;
  900. unsigned long addr_copy = addr;
  901. ret = change_page_attr_set(&addr, numpages,
  902. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  903. if (!ret) {
  904. ret = change_page_attr_set_clr(&addr_copy, numpages,
  905. __pgprot(_PAGE_CACHE_WC),
  906. __pgprot(_PAGE_CACHE_MASK),
  907. 0, 0, NULL);
  908. }
  909. return ret;
  910. }
  911. int set_memory_wc(unsigned long addr, int numpages)
  912. {
  913. int ret;
  914. if (!pat_enabled)
  915. return set_memory_uc(addr, numpages);
  916. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  917. _PAGE_CACHE_WC, NULL);
  918. if (ret)
  919. goto out_err;
  920. ret = _set_memory_wc(addr, numpages);
  921. if (ret)
  922. goto out_free;
  923. return 0;
  924. out_free:
  925. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  926. out_err:
  927. return ret;
  928. }
  929. EXPORT_SYMBOL(set_memory_wc);
  930. int _set_memory_wb(unsigned long addr, int numpages)
  931. {
  932. return change_page_attr_clear(&addr, numpages,
  933. __pgprot(_PAGE_CACHE_MASK), 0);
  934. }
  935. int set_memory_wb(unsigned long addr, int numpages)
  936. {
  937. int ret;
  938. ret = _set_memory_wb(addr, numpages);
  939. if (ret)
  940. return ret;
  941. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  942. return 0;
  943. }
  944. EXPORT_SYMBOL(set_memory_wb);
  945. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  946. {
  947. int i;
  948. int ret;
  949. ret = change_page_attr_clear(addr, addrinarray,
  950. __pgprot(_PAGE_CACHE_MASK), 1);
  951. if (ret)
  952. return ret;
  953. for (i = 0; i < addrinarray; i++)
  954. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  955. return 0;
  956. }
  957. EXPORT_SYMBOL(set_memory_array_wb);
  958. int set_memory_x(unsigned long addr, int numpages)
  959. {
  960. if (!(__supported_pte_mask & _PAGE_NX))
  961. return 0;
  962. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  963. }
  964. EXPORT_SYMBOL(set_memory_x);
  965. int set_memory_nx(unsigned long addr, int numpages)
  966. {
  967. if (!(__supported_pte_mask & _PAGE_NX))
  968. return 0;
  969. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  970. }
  971. EXPORT_SYMBOL(set_memory_nx);
  972. int set_memory_ro(unsigned long addr, int numpages)
  973. {
  974. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  975. }
  976. EXPORT_SYMBOL_GPL(set_memory_ro);
  977. int set_memory_rw(unsigned long addr, int numpages)
  978. {
  979. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  980. }
  981. EXPORT_SYMBOL_GPL(set_memory_rw);
  982. int set_memory_np(unsigned long addr, int numpages)
  983. {
  984. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  985. }
  986. int set_memory_4k(unsigned long addr, int numpages)
  987. {
  988. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  989. __pgprot(0), 1, 0, NULL);
  990. }
  991. int set_pages_uc(struct page *page, int numpages)
  992. {
  993. unsigned long addr = (unsigned long)page_address(page);
  994. return set_memory_uc(addr, numpages);
  995. }
  996. EXPORT_SYMBOL(set_pages_uc);
  997. static int _set_pages_array(struct page **pages, int addrinarray,
  998. unsigned long new_type)
  999. {
  1000. unsigned long start;
  1001. unsigned long end;
  1002. int i;
  1003. int free_idx;
  1004. int ret;
  1005. for (i = 0; i < addrinarray; i++) {
  1006. if (PageHighMem(pages[i]))
  1007. continue;
  1008. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1009. end = start + PAGE_SIZE;
  1010. if (reserve_memtype(start, end, new_type, NULL))
  1011. goto err_out;
  1012. }
  1013. ret = cpa_set_pages_array(pages, addrinarray,
  1014. __pgprot(_PAGE_CACHE_UC_MINUS));
  1015. if (!ret && new_type == _PAGE_CACHE_WC)
  1016. ret = change_page_attr_set_clr(NULL, addrinarray,
  1017. __pgprot(_PAGE_CACHE_WC),
  1018. __pgprot(_PAGE_CACHE_MASK),
  1019. 0, CPA_PAGES_ARRAY, pages);
  1020. if (ret)
  1021. goto err_out;
  1022. return 0; /* Success */
  1023. err_out:
  1024. free_idx = i;
  1025. for (i = 0; i < free_idx; i++) {
  1026. if (PageHighMem(pages[i]))
  1027. continue;
  1028. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1029. end = start + PAGE_SIZE;
  1030. free_memtype(start, end);
  1031. }
  1032. return -EINVAL;
  1033. }
  1034. int set_pages_array_uc(struct page **pages, int addrinarray)
  1035. {
  1036. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
  1037. }
  1038. EXPORT_SYMBOL(set_pages_array_uc);
  1039. int set_pages_array_wc(struct page **pages, int addrinarray)
  1040. {
  1041. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
  1042. }
  1043. EXPORT_SYMBOL(set_pages_array_wc);
  1044. int set_pages_wb(struct page *page, int numpages)
  1045. {
  1046. unsigned long addr = (unsigned long)page_address(page);
  1047. return set_memory_wb(addr, numpages);
  1048. }
  1049. EXPORT_SYMBOL(set_pages_wb);
  1050. int set_pages_array_wb(struct page **pages, int addrinarray)
  1051. {
  1052. int retval;
  1053. unsigned long start;
  1054. unsigned long end;
  1055. int i;
  1056. retval = cpa_clear_pages_array(pages, addrinarray,
  1057. __pgprot(_PAGE_CACHE_MASK));
  1058. if (retval)
  1059. return retval;
  1060. for (i = 0; i < addrinarray; i++) {
  1061. if (PageHighMem(pages[i]))
  1062. continue;
  1063. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1064. end = start + PAGE_SIZE;
  1065. free_memtype(start, end);
  1066. }
  1067. return 0;
  1068. }
  1069. EXPORT_SYMBOL(set_pages_array_wb);
  1070. int set_pages_x(struct page *page, int numpages)
  1071. {
  1072. unsigned long addr = (unsigned long)page_address(page);
  1073. return set_memory_x(addr, numpages);
  1074. }
  1075. EXPORT_SYMBOL(set_pages_x);
  1076. int set_pages_nx(struct page *page, int numpages)
  1077. {
  1078. unsigned long addr = (unsigned long)page_address(page);
  1079. return set_memory_nx(addr, numpages);
  1080. }
  1081. EXPORT_SYMBOL(set_pages_nx);
  1082. int set_pages_ro(struct page *page, int numpages)
  1083. {
  1084. unsigned long addr = (unsigned long)page_address(page);
  1085. return set_memory_ro(addr, numpages);
  1086. }
  1087. int set_pages_rw(struct page *page, int numpages)
  1088. {
  1089. unsigned long addr = (unsigned long)page_address(page);
  1090. return set_memory_rw(addr, numpages);
  1091. }
  1092. #ifdef CONFIG_DEBUG_PAGEALLOC
  1093. static int __set_pages_p(struct page *page, int numpages)
  1094. {
  1095. unsigned long tempaddr = (unsigned long) page_address(page);
  1096. struct cpa_data cpa = { .vaddr = &tempaddr,
  1097. .numpages = numpages,
  1098. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1099. .mask_clr = __pgprot(0),
  1100. .flags = 0};
  1101. /*
  1102. * No alias checking needed for setting present flag. otherwise,
  1103. * we may need to break large pages for 64-bit kernel text
  1104. * mappings (this adds to complexity if we want to do this from
  1105. * atomic context especially). Let's keep it simple!
  1106. */
  1107. return __change_page_attr_set_clr(&cpa, 0);
  1108. }
  1109. static int __set_pages_np(struct page *page, int numpages)
  1110. {
  1111. unsigned long tempaddr = (unsigned long) page_address(page);
  1112. struct cpa_data cpa = { .vaddr = &tempaddr,
  1113. .numpages = numpages,
  1114. .mask_set = __pgprot(0),
  1115. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1116. .flags = 0};
  1117. /*
  1118. * No alias checking needed for setting not present flag. otherwise,
  1119. * we may need to break large pages for 64-bit kernel text
  1120. * mappings (this adds to complexity if we want to do this from
  1121. * atomic context especially). Let's keep it simple!
  1122. */
  1123. return __change_page_attr_set_clr(&cpa, 0);
  1124. }
  1125. void kernel_map_pages(struct page *page, int numpages, int enable)
  1126. {
  1127. if (PageHighMem(page))
  1128. return;
  1129. if (!enable) {
  1130. debug_check_no_locks_freed(page_address(page),
  1131. numpages * PAGE_SIZE);
  1132. }
  1133. /*
  1134. * If page allocator is not up yet then do not call c_p_a():
  1135. */
  1136. if (!debug_pagealloc_enabled)
  1137. return;
  1138. /*
  1139. * The return value is ignored as the calls cannot fail.
  1140. * Large pages for identity mappings are not used at boot time
  1141. * and hence no memory allocations during large page split.
  1142. */
  1143. if (enable)
  1144. __set_pages_p(page, numpages);
  1145. else
  1146. __set_pages_np(page, numpages);
  1147. /*
  1148. * We should perform an IPI and flush all tlbs,
  1149. * but that can deadlock->flush only current cpu:
  1150. */
  1151. __flush_tlb_all();
  1152. }
  1153. #ifdef CONFIG_HIBERNATION
  1154. bool kernel_page_present(struct page *page)
  1155. {
  1156. unsigned int level;
  1157. pte_t *pte;
  1158. if (PageHighMem(page))
  1159. return false;
  1160. pte = lookup_address((unsigned long)page_address(page), &level);
  1161. return (pte_val(*pte) & _PAGE_PRESENT);
  1162. }
  1163. #endif /* CONFIG_HIBERNATION */
  1164. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1165. /*
  1166. * The testcases use internal knowledge of the implementation that shouldn't
  1167. * be exposed to the rest of the kernel. Include these directly here.
  1168. */
  1169. #ifdef CONFIG_CPA_DEBUG
  1170. #include "pageattr-test.c"
  1171. #endif