pageattr.c 34 KB

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