pageattr.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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. /* Protect against CPA */
  55. spin_lock(&pgd_lock);
  56. direct_pages_count[level] += pages;
  57. spin_unlock(&pgd_lock);
  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_symbol(_text) >> PAGE_SHIFT;
  88. }
  89. static inline unsigned long highmap_end_pfn(void)
  90. {
  91. return __pa_symbol(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. * @vaddr: 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. #ifdef CONFIG_PCI_BIOS
  222. if (pcibios_enabled && within(pfn, BIOS_BEGIN >> PAGE_SHIFT, BIOS_END >> PAGE_SHIFT))
  223. pgprot_val(forbidden) |= _PAGE_NX;
  224. #endif
  225. /*
  226. * The kernel text needs to be executable for obvious reasons
  227. * Does not cover __inittext since that is gone later on. On
  228. * 64bit we do not enforce !NX on the low mapping
  229. */
  230. if (within(address, (unsigned long)_text, (unsigned long)_etext))
  231. pgprot_val(forbidden) |= _PAGE_NX;
  232. /*
  233. * The .rodata section needs to be read-only. Using the pfn
  234. * catches all aliases.
  235. */
  236. if (within(pfn, __pa_symbol(__start_rodata) >> PAGE_SHIFT,
  237. __pa_symbol(__end_rodata) >> PAGE_SHIFT))
  238. pgprot_val(forbidden) |= _PAGE_RW;
  239. #if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
  240. /*
  241. * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
  242. * kernel text mappings for the large page aligned text, rodata sections
  243. * will be always read-only. For the kernel identity mappings covering
  244. * the holes caused by this alignment can be anything that user asks.
  245. *
  246. * This will preserve the large page mappings for kernel text/data
  247. * at no extra cost.
  248. */
  249. if (kernel_set_to_readonly &&
  250. within(address, (unsigned long)_text,
  251. (unsigned long)__end_rodata_hpage_align)) {
  252. unsigned int level;
  253. /*
  254. * Don't enforce the !RW mapping for the kernel text mapping,
  255. * if the current mapping is already using small page mapping.
  256. * No need to work hard to preserve large page mappings in this
  257. * case.
  258. *
  259. * This also fixes the Linux Xen paravirt guest boot failure
  260. * (because of unexpected read-only mappings for kernel identity
  261. * mappings). In this paravirt guest case, the kernel text
  262. * mapping and the kernel identity mapping share the same
  263. * page-table pages. Thus we can't really use different
  264. * protections for the kernel text and identity mappings. Also,
  265. * these shared mappings are made of small page mappings.
  266. * Thus this don't enforce !RW mapping for small page kernel
  267. * text mapping logic will help Linux Xen parvirt guest boot
  268. * as well.
  269. */
  270. if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
  271. pgprot_val(forbidden) |= _PAGE_RW;
  272. }
  273. #endif
  274. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  275. return prot;
  276. }
  277. /*
  278. * Lookup the page table entry for a virtual address. Return a pointer
  279. * to the entry and the level of the mapping.
  280. *
  281. * Note: We return pud and pmd either when the entry is marked large
  282. * or when the present bit is not set. Otherwise we would return a
  283. * pointer to a nonexisting mapping.
  284. */
  285. pte_t *lookup_address(unsigned long address, unsigned int *level)
  286. {
  287. pgd_t *pgd = pgd_offset_k(address);
  288. pud_t *pud;
  289. pmd_t *pmd;
  290. *level = PG_LEVEL_NONE;
  291. if (pgd_none(*pgd))
  292. return NULL;
  293. pud = pud_offset(pgd, address);
  294. if (pud_none(*pud))
  295. return NULL;
  296. *level = PG_LEVEL_1G;
  297. if (pud_large(*pud) || !pud_present(*pud))
  298. return (pte_t *)pud;
  299. pmd = pmd_offset(pud, address);
  300. if (pmd_none(*pmd))
  301. return NULL;
  302. *level = PG_LEVEL_2M;
  303. if (pmd_large(*pmd) || !pmd_present(*pmd))
  304. return (pte_t *)pmd;
  305. *level = PG_LEVEL_4K;
  306. return pte_offset_kernel(pmd, address);
  307. }
  308. EXPORT_SYMBOL_GPL(lookup_address);
  309. /*
  310. * This is necessary because __pa() does not work on some
  311. * kinds of memory, like vmalloc() or the alloc_remap()
  312. * areas on 32-bit NUMA systems. The percpu areas can
  313. * end up in this kind of memory, for instance.
  314. *
  315. * This could be optimized, but it is only intended to be
  316. * used at inititalization time, and keeping it
  317. * unoptimized should increase the testing coverage for
  318. * the more obscure platforms.
  319. */
  320. phys_addr_t slow_virt_to_phys(void *__virt_addr)
  321. {
  322. unsigned long virt_addr = (unsigned long)__virt_addr;
  323. phys_addr_t phys_addr;
  324. unsigned long offset;
  325. enum pg_level level;
  326. unsigned long psize;
  327. unsigned long pmask;
  328. pte_t *pte;
  329. pte = lookup_address(virt_addr, &level);
  330. BUG_ON(!pte);
  331. psize = page_level_size(level);
  332. pmask = page_level_mask(level);
  333. offset = virt_addr & ~pmask;
  334. phys_addr = pte_pfn(*pte) << PAGE_SHIFT;
  335. return (phys_addr | offset);
  336. }
  337. EXPORT_SYMBOL_GPL(slow_virt_to_phys);
  338. /*
  339. * Set the new pmd in all the pgds we know about:
  340. */
  341. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  342. {
  343. /* change init_mm */
  344. set_pte_atomic(kpte, pte);
  345. #ifdef CONFIG_X86_32
  346. if (!SHARED_KERNEL_PMD) {
  347. struct page *page;
  348. list_for_each_entry(page, &pgd_list, lru) {
  349. pgd_t *pgd;
  350. pud_t *pud;
  351. pmd_t *pmd;
  352. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  353. pud = pud_offset(pgd, address);
  354. pmd = pmd_offset(pud, address);
  355. set_pte_atomic((pte_t *)pmd, pte);
  356. }
  357. }
  358. #endif
  359. }
  360. static int
  361. try_preserve_large_page(pte_t *kpte, unsigned long address,
  362. struct cpa_data *cpa)
  363. {
  364. unsigned long nextpage_addr, numpages, pmask, psize, addr, pfn;
  365. pte_t new_pte, old_pte, *tmp;
  366. pgprot_t old_prot, new_prot, req_prot;
  367. int i, do_split = 1;
  368. enum pg_level level;
  369. if (cpa->force_split)
  370. return 1;
  371. spin_lock(&pgd_lock);
  372. /*
  373. * Check for races, another CPU might have split this page
  374. * up already:
  375. */
  376. tmp = lookup_address(address, &level);
  377. if (tmp != kpte)
  378. goto out_unlock;
  379. switch (level) {
  380. case PG_LEVEL_2M:
  381. #ifdef CONFIG_X86_64
  382. case PG_LEVEL_1G:
  383. #endif
  384. psize = page_level_size(level);
  385. pmask = page_level_mask(level);
  386. break;
  387. default:
  388. do_split = -EINVAL;
  389. goto out_unlock;
  390. }
  391. /*
  392. * Calculate the number of pages, which fit into this large
  393. * page starting at address:
  394. */
  395. nextpage_addr = (address + psize) & pmask;
  396. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  397. if (numpages < cpa->numpages)
  398. cpa->numpages = numpages;
  399. /*
  400. * We are safe now. Check whether the new pgprot is the same:
  401. */
  402. old_pte = *kpte;
  403. old_prot = new_prot = req_prot = pte_pgprot(old_pte);
  404. pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
  405. pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
  406. /*
  407. * Set the PSE and GLOBAL flags only if the PRESENT flag is
  408. * set otherwise pmd_present/pmd_huge will return true even on
  409. * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
  410. * for the ancient hardware that doesn't support it.
  411. */
  412. if (pgprot_val(new_prot) & _PAGE_PRESENT)
  413. pgprot_val(new_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
  414. else
  415. pgprot_val(new_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
  416. new_prot = canon_pgprot(new_prot);
  417. /*
  418. * old_pte points to the large page base address. So we need
  419. * to add the offset of the virtual address:
  420. */
  421. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  422. cpa->pfn = pfn;
  423. new_prot = static_protections(req_prot, address, pfn);
  424. /*
  425. * We need to check the full range, whether
  426. * static_protection() requires a different pgprot for one of
  427. * the pages in the range we try to preserve:
  428. */
  429. addr = address & pmask;
  430. pfn = pte_pfn(old_pte);
  431. for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
  432. pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
  433. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  434. goto out_unlock;
  435. }
  436. /*
  437. * If there are no changes, return. maxpages has been updated
  438. * above:
  439. */
  440. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  441. do_split = 0;
  442. goto out_unlock;
  443. }
  444. /*
  445. * We need to change the attributes. Check, whether we can
  446. * change the large page in one go. We request a split, when
  447. * the address is not aligned and the number of pages is
  448. * smaller than the number of pages in the large page. Note
  449. * that we limited the number of possible pages already to
  450. * the number of pages in the large page.
  451. */
  452. if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
  453. /*
  454. * The address is aligned and the number of pages
  455. * covers the full page.
  456. */
  457. new_pte = pfn_pte(pte_pfn(old_pte), new_prot);
  458. __set_pmd_pte(kpte, address, new_pte);
  459. cpa->flags |= CPA_FLUSHTLB;
  460. do_split = 0;
  461. }
  462. out_unlock:
  463. spin_unlock(&pgd_lock);
  464. return do_split;
  465. }
  466. int __split_large_page(pte_t *kpte, unsigned long address, pte_t *pbase)
  467. {
  468. unsigned long pfn, pfninc = 1;
  469. unsigned int i, level;
  470. pte_t *tmp;
  471. pgprot_t ref_prot;
  472. struct page *base = virt_to_page(pbase);
  473. spin_lock(&pgd_lock);
  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. spin_unlock(&pgd_lock);
  481. return 1;
  482. }
  483. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  484. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  485. /*
  486. * If we ever want to utilize the PAT bit, we need to
  487. * update this function to make sure it's converted from
  488. * bit 12 to bit 7 when we cross from the 2MB level to
  489. * the 4K level:
  490. */
  491. WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
  492. #ifdef CONFIG_X86_64
  493. if (level == PG_LEVEL_1G) {
  494. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  495. /*
  496. * Set the PSE flags only if the PRESENT flag is set
  497. * otherwise pmd_present/pmd_huge will return true
  498. * even on a non present pmd.
  499. */
  500. if (pgprot_val(ref_prot) & _PAGE_PRESENT)
  501. pgprot_val(ref_prot) |= _PAGE_PSE;
  502. else
  503. pgprot_val(ref_prot) &= ~_PAGE_PSE;
  504. }
  505. #endif
  506. /*
  507. * Set the GLOBAL flags only if the PRESENT flag is set
  508. * otherwise pmd/pte_present will return true even on a non
  509. * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
  510. * for the ancient hardware that doesn't support it.
  511. */
  512. if (pgprot_val(ref_prot) & _PAGE_PRESENT)
  513. pgprot_val(ref_prot) |= _PAGE_GLOBAL;
  514. else
  515. pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
  516. /*
  517. * Get the target pfn from the original entry:
  518. */
  519. pfn = pte_pfn(*kpte);
  520. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  521. set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
  522. if (pfn_range_is_mapped(PFN_DOWN(__pa(address)),
  523. PFN_DOWN(__pa(address)) + 1))
  524. split_page_count(level);
  525. /*
  526. * Install the new, split up pagetable.
  527. *
  528. * We use the standard kernel pagetable protections for the new
  529. * pagetable protections, the actual ptes set above control the
  530. * primary protection behavior:
  531. */
  532. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  533. /*
  534. * Intel Atom errata AAH41 workaround.
  535. *
  536. * The real fix should be in hw or in a microcode update, but
  537. * we also probabilistically try to reduce the window of having
  538. * a large TLB mixed with 4K TLBs while instruction fetches are
  539. * going on.
  540. */
  541. __flush_tlb_all();
  542. spin_unlock(&pgd_lock);
  543. return 0;
  544. }
  545. static int split_large_page(pte_t *kpte, unsigned long address)
  546. {
  547. pte_t *pbase;
  548. struct page *base;
  549. if (!debug_pagealloc)
  550. spin_unlock(&cpa_lock);
  551. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  552. if (!debug_pagealloc)
  553. spin_lock(&cpa_lock);
  554. if (!base)
  555. return -ENOMEM;
  556. pbase = (pte_t *)page_address(base);
  557. if (__split_large_page(kpte, address, pbase))
  558. __free_page(base);
  559. return 0;
  560. }
  561. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  562. int primary)
  563. {
  564. /*
  565. * Ignore all non primary paths.
  566. */
  567. if (!primary)
  568. return 0;
  569. /*
  570. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  571. * to have holes.
  572. * Also set numpages to '1' indicating that we processed cpa req for
  573. * one virtual address page and its pfn. TBD: numpages can be set based
  574. * on the initial value and the level returned by lookup_address().
  575. */
  576. if (within(vaddr, PAGE_OFFSET,
  577. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  578. cpa->numpages = 1;
  579. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  580. return 0;
  581. } else {
  582. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  583. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  584. *cpa->vaddr);
  585. return -EFAULT;
  586. }
  587. }
  588. static int __change_page_attr(struct cpa_data *cpa, int primary)
  589. {
  590. unsigned long address;
  591. int do_split, err;
  592. unsigned int level;
  593. pte_t *kpte, old_pte;
  594. if (cpa->flags & CPA_PAGES_ARRAY) {
  595. struct page *page = cpa->pages[cpa->curpage];
  596. if (unlikely(PageHighMem(page)))
  597. return 0;
  598. address = (unsigned long)page_address(page);
  599. } else if (cpa->flags & CPA_ARRAY)
  600. address = cpa->vaddr[cpa->curpage];
  601. else
  602. address = *cpa->vaddr;
  603. repeat:
  604. kpte = lookup_address(address, &level);
  605. if (!kpte)
  606. return __cpa_process_fault(cpa, address, primary);
  607. old_pte = *kpte;
  608. if (!pte_val(old_pte))
  609. return __cpa_process_fault(cpa, address, primary);
  610. if (level == PG_LEVEL_4K) {
  611. pte_t new_pte;
  612. pgprot_t new_prot = pte_pgprot(old_pte);
  613. unsigned long pfn = pte_pfn(old_pte);
  614. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  615. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  616. new_prot = static_protections(new_prot, address, pfn);
  617. /*
  618. * Set the GLOBAL flags only if the PRESENT flag is
  619. * set otherwise pte_present will return true even on
  620. * a non present pte. The canon_pgprot will clear
  621. * _PAGE_GLOBAL for the ancient hardware that doesn't
  622. * support it.
  623. */
  624. if (pgprot_val(new_prot) & _PAGE_PRESENT)
  625. pgprot_val(new_prot) |= _PAGE_GLOBAL;
  626. else
  627. pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
  628. /*
  629. * We need to keep the pfn from the existing PTE,
  630. * after all we're only going to change it's attributes
  631. * not the memory it points to
  632. */
  633. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  634. cpa->pfn = pfn;
  635. /*
  636. * Do we really change anything ?
  637. */
  638. if (pte_val(old_pte) != pte_val(new_pte)) {
  639. set_pte_atomic(kpte, new_pte);
  640. cpa->flags |= CPA_FLUSHTLB;
  641. }
  642. cpa->numpages = 1;
  643. return 0;
  644. }
  645. /*
  646. * Check, whether we can keep the large page intact
  647. * and just change the pte:
  648. */
  649. do_split = try_preserve_large_page(kpte, address, cpa);
  650. /*
  651. * When the range fits into the existing large page,
  652. * return. cp->numpages and cpa->tlbflush have been updated in
  653. * try_large_page:
  654. */
  655. if (do_split <= 0)
  656. return do_split;
  657. /*
  658. * We have to split the large page:
  659. */
  660. err = split_large_page(kpte, address);
  661. if (!err) {
  662. /*
  663. * Do a global flush tlb after splitting the large page
  664. * and before we do the actual change page attribute in the PTE.
  665. *
  666. * With out this, we violate the TLB application note, that says
  667. * "The TLBs may contain both ordinary and large-page
  668. * translations for a 4-KByte range of linear addresses. This
  669. * may occur if software modifies the paging structures so that
  670. * the page size used for the address range changes. If the two
  671. * translations differ with respect to page frame or attributes
  672. * (e.g., permissions), processor behavior is undefined and may
  673. * be implementation-specific."
  674. *
  675. * We do this global tlb flush inside the cpa_lock, so that we
  676. * don't allow any other cpu, with stale tlb entries change the
  677. * page attribute in parallel, that also falls into the
  678. * just split large page entry.
  679. */
  680. flush_tlb_all();
  681. goto repeat;
  682. }
  683. return err;
  684. }
  685. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  686. static int cpa_process_alias(struct cpa_data *cpa)
  687. {
  688. struct cpa_data alias_cpa;
  689. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  690. unsigned long vaddr;
  691. int ret;
  692. if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
  693. return 0;
  694. /*
  695. * No need to redo, when the primary call touched the direct
  696. * mapping already:
  697. */
  698. if (cpa->flags & CPA_PAGES_ARRAY) {
  699. struct page *page = cpa->pages[cpa->curpage];
  700. if (unlikely(PageHighMem(page)))
  701. return 0;
  702. vaddr = (unsigned long)page_address(page);
  703. } else if (cpa->flags & CPA_ARRAY)
  704. vaddr = cpa->vaddr[cpa->curpage];
  705. else
  706. vaddr = *cpa->vaddr;
  707. if (!(within(vaddr, PAGE_OFFSET,
  708. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  709. alias_cpa = *cpa;
  710. alias_cpa.vaddr = &laddr;
  711. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  712. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  713. if (ret)
  714. return ret;
  715. }
  716. #ifdef CONFIG_X86_64
  717. /*
  718. * If the primary call didn't touch the high mapping already
  719. * and the physical address is inside the kernel map, we need
  720. * to touch the high mapped kernel as well:
  721. */
  722. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  723. within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
  724. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  725. __START_KERNEL_map - phys_base;
  726. alias_cpa = *cpa;
  727. alias_cpa.vaddr = &temp_cpa_vaddr;
  728. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  729. /*
  730. * The high mapping range is imprecise, so ignore the
  731. * return value.
  732. */
  733. __change_page_attr_set_clr(&alias_cpa, 0);
  734. }
  735. #endif
  736. return 0;
  737. }
  738. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  739. {
  740. int ret, numpages = cpa->numpages;
  741. while (numpages) {
  742. /*
  743. * Store the remaining nr of pages for the large page
  744. * preservation check.
  745. */
  746. cpa->numpages = numpages;
  747. /* for array changes, we can't use large page */
  748. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  749. cpa->numpages = 1;
  750. if (!debug_pagealloc)
  751. spin_lock(&cpa_lock);
  752. ret = __change_page_attr(cpa, checkalias);
  753. if (!debug_pagealloc)
  754. spin_unlock(&cpa_lock);
  755. if (ret)
  756. return ret;
  757. if (checkalias) {
  758. ret = cpa_process_alias(cpa);
  759. if (ret)
  760. return ret;
  761. }
  762. /*
  763. * Adjust the number of pages with the result of the
  764. * CPA operation. Either a large page has been
  765. * preserved or a single page update happened.
  766. */
  767. BUG_ON(cpa->numpages > numpages);
  768. numpages -= cpa->numpages;
  769. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  770. cpa->curpage++;
  771. else
  772. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  773. }
  774. return 0;
  775. }
  776. static inline int cache_attr(pgprot_t attr)
  777. {
  778. return pgprot_val(attr) &
  779. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  780. }
  781. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  782. pgprot_t mask_set, pgprot_t mask_clr,
  783. int force_split, int in_flag,
  784. struct page **pages)
  785. {
  786. struct cpa_data cpa;
  787. int ret, cache, checkalias;
  788. unsigned long baddr = 0;
  789. /*
  790. * Check, if we are requested to change a not supported
  791. * feature:
  792. */
  793. mask_set = canon_pgprot(mask_set);
  794. mask_clr = canon_pgprot(mask_clr);
  795. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  796. return 0;
  797. /* Ensure we are PAGE_SIZE aligned */
  798. if (in_flag & CPA_ARRAY) {
  799. int i;
  800. for (i = 0; i < numpages; i++) {
  801. if (addr[i] & ~PAGE_MASK) {
  802. addr[i] &= PAGE_MASK;
  803. WARN_ON_ONCE(1);
  804. }
  805. }
  806. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  807. /*
  808. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  809. * No need to cehck in that case
  810. */
  811. if (*addr & ~PAGE_MASK) {
  812. *addr &= PAGE_MASK;
  813. /*
  814. * People should not be passing in unaligned addresses:
  815. */
  816. WARN_ON_ONCE(1);
  817. }
  818. /*
  819. * Save address for cache flush. *addr is modified in the call
  820. * to __change_page_attr_set_clr() below.
  821. */
  822. baddr = *addr;
  823. }
  824. /* Must avoid aliasing mappings in the highmem code */
  825. kmap_flush_unused();
  826. vm_unmap_aliases();
  827. cpa.vaddr = addr;
  828. cpa.pages = pages;
  829. cpa.numpages = numpages;
  830. cpa.mask_set = mask_set;
  831. cpa.mask_clr = mask_clr;
  832. cpa.flags = 0;
  833. cpa.curpage = 0;
  834. cpa.force_split = force_split;
  835. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  836. cpa.flags |= in_flag;
  837. /* No alias checking for _NX bit modifications */
  838. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  839. ret = __change_page_attr_set_clr(&cpa, checkalias);
  840. /*
  841. * Check whether we really changed something:
  842. */
  843. if (!(cpa.flags & CPA_FLUSHTLB))
  844. goto out;
  845. /*
  846. * No need to flush, when we did not set any of the caching
  847. * attributes:
  848. */
  849. cache = cache_attr(mask_set);
  850. /*
  851. * On success we use clflush, when the CPU supports it to
  852. * avoid the wbindv. If the CPU does not support it and in the
  853. * error case we fall back to cpa_flush_all (which uses
  854. * wbindv):
  855. */
  856. if (!ret && cpu_has_clflush) {
  857. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  858. cpa_flush_array(addr, numpages, cache,
  859. cpa.flags, pages);
  860. } else
  861. cpa_flush_range(baddr, numpages, cache);
  862. } else
  863. cpa_flush_all(cache);
  864. out:
  865. return ret;
  866. }
  867. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  868. pgprot_t mask, int array)
  869. {
  870. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  871. (array ? CPA_ARRAY : 0), NULL);
  872. }
  873. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  874. pgprot_t mask, int array)
  875. {
  876. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  877. (array ? CPA_ARRAY : 0), NULL);
  878. }
  879. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  880. pgprot_t mask)
  881. {
  882. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  883. CPA_PAGES_ARRAY, pages);
  884. }
  885. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  886. pgprot_t mask)
  887. {
  888. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  889. CPA_PAGES_ARRAY, pages);
  890. }
  891. int _set_memory_uc(unsigned long addr, int numpages)
  892. {
  893. /*
  894. * for now UC MINUS. see comments in ioremap_nocache()
  895. */
  896. return change_page_attr_set(&addr, numpages,
  897. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  898. }
  899. int set_memory_uc(unsigned long addr, int numpages)
  900. {
  901. int ret;
  902. /*
  903. * for now UC MINUS. see comments in ioremap_nocache()
  904. */
  905. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  906. _PAGE_CACHE_UC_MINUS, NULL);
  907. if (ret)
  908. goto out_err;
  909. ret = _set_memory_uc(addr, numpages);
  910. if (ret)
  911. goto out_free;
  912. return 0;
  913. out_free:
  914. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  915. out_err:
  916. return ret;
  917. }
  918. EXPORT_SYMBOL(set_memory_uc);
  919. static int _set_memory_array(unsigned long *addr, int addrinarray,
  920. unsigned long new_type)
  921. {
  922. int i, j;
  923. int ret;
  924. /*
  925. * for now UC MINUS. see comments in ioremap_nocache()
  926. */
  927. for (i = 0; i < addrinarray; i++) {
  928. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  929. new_type, NULL);
  930. if (ret)
  931. goto out_free;
  932. }
  933. ret = change_page_attr_set(addr, addrinarray,
  934. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  935. if (!ret && new_type == _PAGE_CACHE_WC)
  936. ret = change_page_attr_set_clr(addr, addrinarray,
  937. __pgprot(_PAGE_CACHE_WC),
  938. __pgprot(_PAGE_CACHE_MASK),
  939. 0, CPA_ARRAY, NULL);
  940. if (ret)
  941. goto out_free;
  942. return 0;
  943. out_free:
  944. for (j = 0; j < i; j++)
  945. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  946. return ret;
  947. }
  948. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  949. {
  950. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
  951. }
  952. EXPORT_SYMBOL(set_memory_array_uc);
  953. int set_memory_array_wc(unsigned long *addr, int addrinarray)
  954. {
  955. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
  956. }
  957. EXPORT_SYMBOL(set_memory_array_wc);
  958. int _set_memory_wc(unsigned long addr, int numpages)
  959. {
  960. int ret;
  961. unsigned long addr_copy = addr;
  962. ret = change_page_attr_set(&addr, numpages,
  963. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  964. if (!ret) {
  965. ret = change_page_attr_set_clr(&addr_copy, numpages,
  966. __pgprot(_PAGE_CACHE_WC),
  967. __pgprot(_PAGE_CACHE_MASK),
  968. 0, 0, NULL);
  969. }
  970. return ret;
  971. }
  972. int set_memory_wc(unsigned long addr, int numpages)
  973. {
  974. int ret;
  975. if (!pat_enabled)
  976. return set_memory_uc(addr, numpages);
  977. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  978. _PAGE_CACHE_WC, NULL);
  979. if (ret)
  980. goto out_err;
  981. ret = _set_memory_wc(addr, numpages);
  982. if (ret)
  983. goto out_free;
  984. return 0;
  985. out_free:
  986. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  987. out_err:
  988. return ret;
  989. }
  990. EXPORT_SYMBOL(set_memory_wc);
  991. int _set_memory_wb(unsigned long addr, int numpages)
  992. {
  993. return change_page_attr_clear(&addr, numpages,
  994. __pgprot(_PAGE_CACHE_MASK), 0);
  995. }
  996. int set_memory_wb(unsigned long addr, int numpages)
  997. {
  998. int ret;
  999. ret = _set_memory_wb(addr, numpages);
  1000. if (ret)
  1001. return ret;
  1002. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1003. return 0;
  1004. }
  1005. EXPORT_SYMBOL(set_memory_wb);
  1006. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  1007. {
  1008. int i;
  1009. int ret;
  1010. ret = change_page_attr_clear(addr, addrinarray,
  1011. __pgprot(_PAGE_CACHE_MASK), 1);
  1012. if (ret)
  1013. return ret;
  1014. for (i = 0; i < addrinarray; i++)
  1015. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  1016. return 0;
  1017. }
  1018. EXPORT_SYMBOL(set_memory_array_wb);
  1019. int set_memory_x(unsigned long addr, int numpages)
  1020. {
  1021. if (!(__supported_pte_mask & _PAGE_NX))
  1022. return 0;
  1023. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1024. }
  1025. EXPORT_SYMBOL(set_memory_x);
  1026. int set_memory_nx(unsigned long addr, int numpages)
  1027. {
  1028. if (!(__supported_pte_mask & _PAGE_NX))
  1029. return 0;
  1030. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1031. }
  1032. EXPORT_SYMBOL(set_memory_nx);
  1033. int set_memory_ro(unsigned long addr, int numpages)
  1034. {
  1035. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1036. }
  1037. EXPORT_SYMBOL_GPL(set_memory_ro);
  1038. int set_memory_rw(unsigned long addr, int numpages)
  1039. {
  1040. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1041. }
  1042. EXPORT_SYMBOL_GPL(set_memory_rw);
  1043. int set_memory_np(unsigned long addr, int numpages)
  1044. {
  1045. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  1046. }
  1047. int set_memory_4k(unsigned long addr, int numpages)
  1048. {
  1049. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  1050. __pgprot(0), 1, 0, NULL);
  1051. }
  1052. int set_pages_uc(struct page *page, int numpages)
  1053. {
  1054. unsigned long addr = (unsigned long)page_address(page);
  1055. return set_memory_uc(addr, numpages);
  1056. }
  1057. EXPORT_SYMBOL(set_pages_uc);
  1058. static int _set_pages_array(struct page **pages, int addrinarray,
  1059. unsigned long new_type)
  1060. {
  1061. unsigned long start;
  1062. unsigned long end;
  1063. int i;
  1064. int free_idx;
  1065. int ret;
  1066. for (i = 0; i < addrinarray; i++) {
  1067. if (PageHighMem(pages[i]))
  1068. continue;
  1069. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1070. end = start + PAGE_SIZE;
  1071. if (reserve_memtype(start, end, new_type, NULL))
  1072. goto err_out;
  1073. }
  1074. ret = cpa_set_pages_array(pages, addrinarray,
  1075. __pgprot(_PAGE_CACHE_UC_MINUS));
  1076. if (!ret && new_type == _PAGE_CACHE_WC)
  1077. ret = change_page_attr_set_clr(NULL, addrinarray,
  1078. __pgprot(_PAGE_CACHE_WC),
  1079. __pgprot(_PAGE_CACHE_MASK),
  1080. 0, CPA_PAGES_ARRAY, pages);
  1081. if (ret)
  1082. goto err_out;
  1083. return 0; /* Success */
  1084. err_out:
  1085. free_idx = i;
  1086. for (i = 0; i < free_idx; i++) {
  1087. if (PageHighMem(pages[i]))
  1088. continue;
  1089. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1090. end = start + PAGE_SIZE;
  1091. free_memtype(start, end);
  1092. }
  1093. return -EINVAL;
  1094. }
  1095. int set_pages_array_uc(struct page **pages, int addrinarray)
  1096. {
  1097. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
  1098. }
  1099. EXPORT_SYMBOL(set_pages_array_uc);
  1100. int set_pages_array_wc(struct page **pages, int addrinarray)
  1101. {
  1102. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
  1103. }
  1104. EXPORT_SYMBOL(set_pages_array_wc);
  1105. int set_pages_wb(struct page *page, int numpages)
  1106. {
  1107. unsigned long addr = (unsigned long)page_address(page);
  1108. return set_memory_wb(addr, numpages);
  1109. }
  1110. EXPORT_SYMBOL(set_pages_wb);
  1111. int set_pages_array_wb(struct page **pages, int addrinarray)
  1112. {
  1113. int retval;
  1114. unsigned long start;
  1115. unsigned long end;
  1116. int i;
  1117. retval = cpa_clear_pages_array(pages, addrinarray,
  1118. __pgprot(_PAGE_CACHE_MASK));
  1119. if (retval)
  1120. return retval;
  1121. for (i = 0; i < addrinarray; i++) {
  1122. if (PageHighMem(pages[i]))
  1123. continue;
  1124. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1125. end = start + PAGE_SIZE;
  1126. free_memtype(start, end);
  1127. }
  1128. return 0;
  1129. }
  1130. EXPORT_SYMBOL(set_pages_array_wb);
  1131. int set_pages_x(struct page *page, int numpages)
  1132. {
  1133. unsigned long addr = (unsigned long)page_address(page);
  1134. return set_memory_x(addr, numpages);
  1135. }
  1136. EXPORT_SYMBOL(set_pages_x);
  1137. int set_pages_nx(struct page *page, int numpages)
  1138. {
  1139. unsigned long addr = (unsigned long)page_address(page);
  1140. return set_memory_nx(addr, numpages);
  1141. }
  1142. EXPORT_SYMBOL(set_pages_nx);
  1143. int set_pages_ro(struct page *page, int numpages)
  1144. {
  1145. unsigned long addr = (unsigned long)page_address(page);
  1146. return set_memory_ro(addr, numpages);
  1147. }
  1148. int set_pages_rw(struct page *page, int numpages)
  1149. {
  1150. unsigned long addr = (unsigned long)page_address(page);
  1151. return set_memory_rw(addr, numpages);
  1152. }
  1153. #ifdef CONFIG_DEBUG_PAGEALLOC
  1154. static int __set_pages_p(struct page *page, int numpages)
  1155. {
  1156. unsigned long tempaddr = (unsigned long) page_address(page);
  1157. struct cpa_data cpa = { .vaddr = &tempaddr,
  1158. .numpages = numpages,
  1159. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1160. .mask_clr = __pgprot(0),
  1161. .flags = 0};
  1162. /*
  1163. * No alias checking needed for setting present flag. otherwise,
  1164. * we may need to break large pages for 64-bit kernel text
  1165. * mappings (this adds to complexity if we want to do this from
  1166. * atomic context especially). Let's keep it simple!
  1167. */
  1168. return __change_page_attr_set_clr(&cpa, 0);
  1169. }
  1170. static int __set_pages_np(struct page *page, int numpages)
  1171. {
  1172. unsigned long tempaddr = (unsigned long) page_address(page);
  1173. struct cpa_data cpa = { .vaddr = &tempaddr,
  1174. .numpages = numpages,
  1175. .mask_set = __pgprot(0),
  1176. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1177. .flags = 0};
  1178. /*
  1179. * No alias checking needed for setting not present flag. otherwise,
  1180. * we may need to break large pages for 64-bit kernel text
  1181. * mappings (this adds to complexity if we want to do this from
  1182. * atomic context especially). Let's keep it simple!
  1183. */
  1184. return __change_page_attr_set_clr(&cpa, 0);
  1185. }
  1186. void kernel_map_pages(struct page *page, int numpages, int enable)
  1187. {
  1188. if (PageHighMem(page))
  1189. return;
  1190. if (!enable) {
  1191. debug_check_no_locks_freed(page_address(page),
  1192. numpages * PAGE_SIZE);
  1193. }
  1194. /*
  1195. * The return value is ignored as the calls cannot fail.
  1196. * Large pages for identity mappings are not used at boot time
  1197. * and hence no memory allocations during large page split.
  1198. */
  1199. if (enable)
  1200. __set_pages_p(page, numpages);
  1201. else
  1202. __set_pages_np(page, numpages);
  1203. /*
  1204. * We should perform an IPI and flush all tlbs,
  1205. * but that can deadlock->flush only current cpu:
  1206. */
  1207. __flush_tlb_all();
  1208. }
  1209. #ifdef CONFIG_HIBERNATION
  1210. bool kernel_page_present(struct page *page)
  1211. {
  1212. unsigned int level;
  1213. pte_t *pte;
  1214. if (PageHighMem(page))
  1215. return false;
  1216. pte = lookup_address((unsigned long)page_address(page), &level);
  1217. return (pte_val(*pte) & _PAGE_PRESENT);
  1218. }
  1219. #endif /* CONFIG_HIBERNATION */
  1220. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1221. /*
  1222. * The testcases use internal knowledge of the implementation that shouldn't
  1223. * be exposed to the rest of the kernel. Include these directly here.
  1224. */
  1225. #ifdef CONFIG_CPA_DEBUG
  1226. #include "pageattr-test.c"
  1227. #endif