pageattr.c 34 KB

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