pageattr.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. * old_pte points to the large page base address. So we need
  408. * to add the offset of the virtual address:
  409. */
  410. pfn = pte_pfn(old_pte) + ((address & (psize - 1)) >> PAGE_SHIFT);
  411. cpa->pfn = pfn;
  412. new_prot = static_protections(req_prot, address, pfn);
  413. /*
  414. * We need to check the full range, whether
  415. * static_protection() requires a different pgprot for one of
  416. * the pages in the range we try to preserve:
  417. */
  418. addr = address & pmask;
  419. pfn = pte_pfn(old_pte);
  420. for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
  421. pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
  422. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  423. goto out_unlock;
  424. }
  425. /*
  426. * If there are no changes, return. maxpages has been updated
  427. * above:
  428. */
  429. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  430. do_split = 0;
  431. goto out_unlock;
  432. }
  433. /*
  434. * We need to change the attributes. Check, whether we can
  435. * change the large page in one go. We request a split, when
  436. * the address is not aligned and the number of pages is
  437. * smaller than the number of pages in the large page. Note
  438. * that we limited the number of possible pages already to
  439. * the number of pages in the large page.
  440. */
  441. if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
  442. /*
  443. * The address is aligned and the number of pages
  444. * covers the full page.
  445. */
  446. new_pte = pfn_pte(pte_pfn(old_pte), canon_pgprot(new_prot));
  447. __set_pmd_pte(kpte, address, new_pte);
  448. cpa->flags |= CPA_FLUSHTLB;
  449. do_split = 0;
  450. }
  451. out_unlock:
  452. spin_unlock(&pgd_lock);
  453. return do_split;
  454. }
  455. static int split_large_page(pte_t *kpte, unsigned long address)
  456. {
  457. unsigned long pfn, pfninc = 1;
  458. unsigned int i, level;
  459. pte_t *pbase, *tmp;
  460. pgprot_t ref_prot;
  461. struct page *base;
  462. if (!debug_pagealloc)
  463. spin_unlock(&cpa_lock);
  464. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  465. if (!debug_pagealloc)
  466. spin_lock(&cpa_lock);
  467. if (!base)
  468. return -ENOMEM;
  469. spin_lock(&pgd_lock);
  470. /*
  471. * Check for races, another CPU might have split this page
  472. * up for us already:
  473. */
  474. tmp = lookup_address(address, &level);
  475. if (tmp != kpte)
  476. goto out_unlock;
  477. pbase = (pte_t *)page_address(base);
  478. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  479. ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  480. /*
  481. * If we ever want to utilize the PAT bit, we need to
  482. * update this function to make sure it's converted from
  483. * bit 12 to bit 7 when we cross from the 2MB level to
  484. * the 4K level:
  485. */
  486. WARN_ON_ONCE(pgprot_val(ref_prot) & _PAGE_PAT_LARGE);
  487. #ifdef CONFIG_X86_64
  488. if (level == PG_LEVEL_1G) {
  489. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  490. pgprot_val(ref_prot) |= _PAGE_PSE;
  491. }
  492. #endif
  493. /*
  494. * Get the target pfn from the original entry:
  495. */
  496. pfn = pte_pfn(*kpte);
  497. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  498. set_pte(&pbase[i], pfn_pte(pfn, ref_prot));
  499. if (address >= (unsigned long)__va(0) &&
  500. address < (unsigned long)__va(max_low_pfn_mapped << PAGE_SHIFT))
  501. split_page_count(level);
  502. #ifdef CONFIG_X86_64
  503. if (address >= (unsigned long)__va(1UL<<32) &&
  504. address < (unsigned long)__va(max_pfn_mapped << PAGE_SHIFT))
  505. split_page_count(level);
  506. #endif
  507. /*
  508. * Install the new, split up pagetable.
  509. *
  510. * We use the standard kernel pagetable protections for the new
  511. * pagetable protections, the actual ptes set above control the
  512. * primary protection behavior:
  513. */
  514. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  515. /*
  516. * Intel Atom errata AAH41 workaround.
  517. *
  518. * The real fix should be in hw or in a microcode update, but
  519. * we also probabilistically try to reduce the window of having
  520. * a large TLB mixed with 4K TLBs while instruction fetches are
  521. * going on.
  522. */
  523. __flush_tlb_all();
  524. base = NULL;
  525. out_unlock:
  526. /*
  527. * If we dropped out via the lookup_address check under
  528. * pgd_lock then stick the page back into the pool:
  529. */
  530. if (base)
  531. __free_page(base);
  532. spin_unlock(&pgd_lock);
  533. return 0;
  534. }
  535. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  536. int primary)
  537. {
  538. /*
  539. * Ignore all non primary paths.
  540. */
  541. if (!primary)
  542. return 0;
  543. /*
  544. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  545. * to have holes.
  546. * Also set numpages to '1' indicating that we processed cpa req for
  547. * one virtual address page and its pfn. TBD: numpages can be set based
  548. * on the initial value and the level returned by lookup_address().
  549. */
  550. if (within(vaddr, PAGE_OFFSET,
  551. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  552. cpa->numpages = 1;
  553. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  554. return 0;
  555. } else {
  556. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  557. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  558. *cpa->vaddr);
  559. return -EFAULT;
  560. }
  561. }
  562. static int __change_page_attr(struct cpa_data *cpa, int primary)
  563. {
  564. unsigned long address;
  565. int do_split, err;
  566. unsigned int level;
  567. pte_t *kpte, old_pte;
  568. if (cpa->flags & CPA_PAGES_ARRAY) {
  569. struct page *page = cpa->pages[cpa->curpage];
  570. if (unlikely(PageHighMem(page)))
  571. return 0;
  572. address = (unsigned long)page_address(page);
  573. } else if (cpa->flags & CPA_ARRAY)
  574. address = cpa->vaddr[cpa->curpage];
  575. else
  576. address = *cpa->vaddr;
  577. repeat:
  578. kpte = lookup_address(address, &level);
  579. if (!kpte)
  580. return __cpa_process_fault(cpa, address, primary);
  581. old_pte = *kpte;
  582. if (!pte_val(old_pte))
  583. return __cpa_process_fault(cpa, address, primary);
  584. if (level == PG_LEVEL_4K) {
  585. pte_t new_pte;
  586. pgprot_t new_prot = pte_pgprot(old_pte);
  587. unsigned long pfn = pte_pfn(old_pte);
  588. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  589. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  590. new_prot = static_protections(new_prot, address, pfn);
  591. /*
  592. * We need to keep the pfn from the existing PTE,
  593. * after all we're only going to change it's attributes
  594. * not the memory it points to
  595. */
  596. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  597. cpa->pfn = pfn;
  598. /*
  599. * Do we really change anything ?
  600. */
  601. if (pte_val(old_pte) != pte_val(new_pte)) {
  602. set_pte_atomic(kpte, new_pte);
  603. cpa->flags |= CPA_FLUSHTLB;
  604. }
  605. cpa->numpages = 1;
  606. return 0;
  607. }
  608. /*
  609. * Check, whether we can keep the large page intact
  610. * and just change the pte:
  611. */
  612. do_split = try_preserve_large_page(kpte, address, cpa);
  613. /*
  614. * When the range fits into the existing large page,
  615. * return. cp->numpages and cpa->tlbflush have been updated in
  616. * try_large_page:
  617. */
  618. if (do_split <= 0)
  619. return do_split;
  620. /*
  621. * We have to split the large page:
  622. */
  623. err = split_large_page(kpte, address);
  624. if (!err) {
  625. /*
  626. * Do a global flush tlb after splitting the large page
  627. * and before we do the actual change page attribute in the PTE.
  628. *
  629. * With out this, we violate the TLB application note, that says
  630. * "The TLBs may contain both ordinary and large-page
  631. * translations for a 4-KByte range of linear addresses. This
  632. * may occur if software modifies the paging structures so that
  633. * the page size used for the address range changes. If the two
  634. * translations differ with respect to page frame or attributes
  635. * (e.g., permissions), processor behavior is undefined and may
  636. * be implementation-specific."
  637. *
  638. * We do this global tlb flush inside the cpa_lock, so that we
  639. * don't allow any other cpu, with stale tlb entries change the
  640. * page attribute in parallel, that also falls into the
  641. * just split large page entry.
  642. */
  643. flush_tlb_all();
  644. goto repeat;
  645. }
  646. return err;
  647. }
  648. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  649. static int cpa_process_alias(struct cpa_data *cpa)
  650. {
  651. struct cpa_data alias_cpa;
  652. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  653. unsigned long vaddr;
  654. int ret;
  655. if (cpa->pfn >= max_pfn_mapped)
  656. return 0;
  657. #ifdef CONFIG_X86_64
  658. if (cpa->pfn >= max_low_pfn_mapped && cpa->pfn < (1UL<<(32-PAGE_SHIFT)))
  659. return 0;
  660. #endif
  661. /*
  662. * No need to redo, when the primary call touched the direct
  663. * mapping already:
  664. */
  665. if (cpa->flags & CPA_PAGES_ARRAY) {
  666. struct page *page = cpa->pages[cpa->curpage];
  667. if (unlikely(PageHighMem(page)))
  668. return 0;
  669. vaddr = (unsigned long)page_address(page);
  670. } else if (cpa->flags & CPA_ARRAY)
  671. vaddr = cpa->vaddr[cpa->curpage];
  672. else
  673. vaddr = *cpa->vaddr;
  674. if (!(within(vaddr, PAGE_OFFSET,
  675. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  676. alias_cpa = *cpa;
  677. alias_cpa.vaddr = &laddr;
  678. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  679. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  680. if (ret)
  681. return ret;
  682. }
  683. #ifdef CONFIG_X86_64
  684. /*
  685. * If the primary call didn't touch the high mapping already
  686. * and the physical address is inside the kernel map, we need
  687. * to touch the high mapped kernel as well:
  688. */
  689. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  690. within(cpa->pfn, highmap_start_pfn(), highmap_end_pfn())) {
  691. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  692. __START_KERNEL_map - phys_base;
  693. alias_cpa = *cpa;
  694. alias_cpa.vaddr = &temp_cpa_vaddr;
  695. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  696. /*
  697. * The high mapping range is imprecise, so ignore the
  698. * return value.
  699. */
  700. __change_page_attr_set_clr(&alias_cpa, 0);
  701. }
  702. #endif
  703. return 0;
  704. }
  705. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  706. {
  707. int ret, numpages = cpa->numpages;
  708. while (numpages) {
  709. /*
  710. * Store the remaining nr of pages for the large page
  711. * preservation check.
  712. */
  713. cpa->numpages = numpages;
  714. /* for array changes, we can't use large page */
  715. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  716. cpa->numpages = 1;
  717. if (!debug_pagealloc)
  718. spin_lock(&cpa_lock);
  719. ret = __change_page_attr(cpa, checkalias);
  720. if (!debug_pagealloc)
  721. spin_unlock(&cpa_lock);
  722. if (ret)
  723. return ret;
  724. if (checkalias) {
  725. ret = cpa_process_alias(cpa);
  726. if (ret)
  727. return ret;
  728. }
  729. /*
  730. * Adjust the number of pages with the result of the
  731. * CPA operation. Either a large page has been
  732. * preserved or a single page update happened.
  733. */
  734. BUG_ON(cpa->numpages > numpages);
  735. numpages -= cpa->numpages;
  736. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  737. cpa->curpage++;
  738. else
  739. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  740. }
  741. return 0;
  742. }
  743. static inline int cache_attr(pgprot_t attr)
  744. {
  745. return pgprot_val(attr) &
  746. (_PAGE_PAT | _PAGE_PAT_LARGE | _PAGE_PWT | _PAGE_PCD);
  747. }
  748. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  749. pgprot_t mask_set, pgprot_t mask_clr,
  750. int force_split, int in_flag,
  751. struct page **pages)
  752. {
  753. struct cpa_data cpa;
  754. int ret, cache, checkalias;
  755. unsigned long baddr = 0;
  756. /*
  757. * Check, if we are requested to change a not supported
  758. * feature:
  759. */
  760. mask_set = canon_pgprot(mask_set);
  761. mask_clr = canon_pgprot(mask_clr);
  762. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  763. return 0;
  764. /* Ensure we are PAGE_SIZE aligned */
  765. if (in_flag & CPA_ARRAY) {
  766. int i;
  767. for (i = 0; i < numpages; i++) {
  768. if (addr[i] & ~PAGE_MASK) {
  769. addr[i] &= PAGE_MASK;
  770. WARN_ON_ONCE(1);
  771. }
  772. }
  773. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  774. /*
  775. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  776. * No need to cehck in that case
  777. */
  778. if (*addr & ~PAGE_MASK) {
  779. *addr &= PAGE_MASK;
  780. /*
  781. * People should not be passing in unaligned addresses:
  782. */
  783. WARN_ON_ONCE(1);
  784. }
  785. /*
  786. * Save address for cache flush. *addr is modified in the call
  787. * to __change_page_attr_set_clr() below.
  788. */
  789. baddr = *addr;
  790. }
  791. /* Must avoid aliasing mappings in the highmem code */
  792. kmap_flush_unused();
  793. vm_unmap_aliases();
  794. cpa.vaddr = addr;
  795. cpa.pages = pages;
  796. cpa.numpages = numpages;
  797. cpa.mask_set = mask_set;
  798. cpa.mask_clr = mask_clr;
  799. cpa.flags = 0;
  800. cpa.curpage = 0;
  801. cpa.force_split = force_split;
  802. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  803. cpa.flags |= in_flag;
  804. /* No alias checking for _NX bit modifications */
  805. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  806. ret = __change_page_attr_set_clr(&cpa, checkalias);
  807. /*
  808. * Check whether we really changed something:
  809. */
  810. if (!(cpa.flags & CPA_FLUSHTLB))
  811. goto out;
  812. /*
  813. * No need to flush, when we did not set any of the caching
  814. * attributes:
  815. */
  816. cache = cache_attr(mask_set);
  817. /*
  818. * On success we use clflush, when the CPU supports it to
  819. * avoid the wbindv. If the CPU does not support it and in the
  820. * error case we fall back to cpa_flush_all (which uses
  821. * wbindv):
  822. */
  823. if (!ret && cpu_has_clflush) {
  824. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  825. cpa_flush_array(addr, numpages, cache,
  826. cpa.flags, pages);
  827. } else
  828. cpa_flush_range(baddr, numpages, cache);
  829. } else
  830. cpa_flush_all(cache);
  831. out:
  832. return ret;
  833. }
  834. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  835. pgprot_t mask, int array)
  836. {
  837. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  838. (array ? CPA_ARRAY : 0), NULL);
  839. }
  840. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  841. pgprot_t mask, int array)
  842. {
  843. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  844. (array ? CPA_ARRAY : 0), NULL);
  845. }
  846. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  847. pgprot_t mask)
  848. {
  849. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  850. CPA_PAGES_ARRAY, pages);
  851. }
  852. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  853. pgprot_t mask)
  854. {
  855. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  856. CPA_PAGES_ARRAY, pages);
  857. }
  858. int _set_memory_uc(unsigned long addr, int numpages)
  859. {
  860. /*
  861. * for now UC MINUS. see comments in ioremap_nocache()
  862. */
  863. return change_page_attr_set(&addr, numpages,
  864. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  865. }
  866. int set_memory_uc(unsigned long addr, int numpages)
  867. {
  868. int ret;
  869. /*
  870. * for now UC MINUS. see comments in ioremap_nocache()
  871. */
  872. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  873. _PAGE_CACHE_UC_MINUS, NULL);
  874. if (ret)
  875. goto out_err;
  876. ret = _set_memory_uc(addr, numpages);
  877. if (ret)
  878. goto out_free;
  879. return 0;
  880. out_free:
  881. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  882. out_err:
  883. return ret;
  884. }
  885. EXPORT_SYMBOL(set_memory_uc);
  886. static int _set_memory_array(unsigned long *addr, int addrinarray,
  887. unsigned long new_type)
  888. {
  889. int i, j;
  890. int ret;
  891. /*
  892. * for now UC MINUS. see comments in ioremap_nocache()
  893. */
  894. for (i = 0; i < addrinarray; i++) {
  895. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  896. new_type, NULL);
  897. if (ret)
  898. goto out_free;
  899. }
  900. ret = change_page_attr_set(addr, addrinarray,
  901. __pgprot(_PAGE_CACHE_UC_MINUS), 1);
  902. if (!ret && new_type == _PAGE_CACHE_WC)
  903. ret = change_page_attr_set_clr(addr, addrinarray,
  904. __pgprot(_PAGE_CACHE_WC),
  905. __pgprot(_PAGE_CACHE_MASK),
  906. 0, CPA_ARRAY, NULL);
  907. if (ret)
  908. goto out_free;
  909. return 0;
  910. out_free:
  911. for (j = 0; j < i; j++)
  912. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  913. return ret;
  914. }
  915. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  916. {
  917. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_UC_MINUS);
  918. }
  919. EXPORT_SYMBOL(set_memory_array_uc);
  920. int set_memory_array_wc(unsigned long *addr, int addrinarray)
  921. {
  922. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_WC);
  923. }
  924. EXPORT_SYMBOL(set_memory_array_wc);
  925. int _set_memory_wc(unsigned long addr, int numpages)
  926. {
  927. int ret;
  928. unsigned long addr_copy = addr;
  929. ret = change_page_attr_set(&addr, numpages,
  930. __pgprot(_PAGE_CACHE_UC_MINUS), 0);
  931. if (!ret) {
  932. ret = change_page_attr_set_clr(&addr_copy, numpages,
  933. __pgprot(_PAGE_CACHE_WC),
  934. __pgprot(_PAGE_CACHE_MASK),
  935. 0, 0, NULL);
  936. }
  937. return ret;
  938. }
  939. int set_memory_wc(unsigned long addr, int numpages)
  940. {
  941. int ret;
  942. if (!pat_enabled)
  943. return set_memory_uc(addr, numpages);
  944. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  945. _PAGE_CACHE_WC, NULL);
  946. if (ret)
  947. goto out_err;
  948. ret = _set_memory_wc(addr, numpages);
  949. if (ret)
  950. goto out_free;
  951. return 0;
  952. out_free:
  953. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  954. out_err:
  955. return ret;
  956. }
  957. EXPORT_SYMBOL(set_memory_wc);
  958. int _set_memory_wb(unsigned long addr, int numpages)
  959. {
  960. return change_page_attr_clear(&addr, numpages,
  961. __pgprot(_PAGE_CACHE_MASK), 0);
  962. }
  963. int set_memory_wb(unsigned long addr, int numpages)
  964. {
  965. int ret;
  966. ret = _set_memory_wb(addr, numpages);
  967. if (ret)
  968. return ret;
  969. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  970. return 0;
  971. }
  972. EXPORT_SYMBOL(set_memory_wb);
  973. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  974. {
  975. int i;
  976. int ret;
  977. ret = change_page_attr_clear(addr, addrinarray,
  978. __pgprot(_PAGE_CACHE_MASK), 1);
  979. if (ret)
  980. return ret;
  981. for (i = 0; i < addrinarray; i++)
  982. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  983. return 0;
  984. }
  985. EXPORT_SYMBOL(set_memory_array_wb);
  986. int set_memory_x(unsigned long addr, int numpages)
  987. {
  988. if (!(__supported_pte_mask & _PAGE_NX))
  989. return 0;
  990. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  991. }
  992. EXPORT_SYMBOL(set_memory_x);
  993. int set_memory_nx(unsigned long addr, int numpages)
  994. {
  995. if (!(__supported_pte_mask & _PAGE_NX))
  996. return 0;
  997. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  998. }
  999. EXPORT_SYMBOL(set_memory_nx);
  1000. int set_memory_ro(unsigned long addr, int numpages)
  1001. {
  1002. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1003. }
  1004. EXPORT_SYMBOL_GPL(set_memory_ro);
  1005. int set_memory_rw(unsigned long addr, int numpages)
  1006. {
  1007. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1008. }
  1009. EXPORT_SYMBOL_GPL(set_memory_rw);
  1010. int set_memory_np(unsigned long addr, int numpages)
  1011. {
  1012. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  1013. }
  1014. int set_memory_4k(unsigned long addr, int numpages)
  1015. {
  1016. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  1017. __pgprot(0), 1, 0, NULL);
  1018. }
  1019. int set_pages_uc(struct page *page, int numpages)
  1020. {
  1021. unsigned long addr = (unsigned long)page_address(page);
  1022. return set_memory_uc(addr, numpages);
  1023. }
  1024. EXPORT_SYMBOL(set_pages_uc);
  1025. static int _set_pages_array(struct page **pages, int addrinarray,
  1026. unsigned long new_type)
  1027. {
  1028. unsigned long start;
  1029. unsigned long end;
  1030. int i;
  1031. int free_idx;
  1032. int ret;
  1033. for (i = 0; i < addrinarray; i++) {
  1034. if (PageHighMem(pages[i]))
  1035. continue;
  1036. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1037. end = start + PAGE_SIZE;
  1038. if (reserve_memtype(start, end, new_type, NULL))
  1039. goto err_out;
  1040. }
  1041. ret = cpa_set_pages_array(pages, addrinarray,
  1042. __pgprot(_PAGE_CACHE_UC_MINUS));
  1043. if (!ret && new_type == _PAGE_CACHE_WC)
  1044. ret = change_page_attr_set_clr(NULL, addrinarray,
  1045. __pgprot(_PAGE_CACHE_WC),
  1046. __pgprot(_PAGE_CACHE_MASK),
  1047. 0, CPA_PAGES_ARRAY, pages);
  1048. if (ret)
  1049. goto err_out;
  1050. return 0; /* Success */
  1051. err_out:
  1052. free_idx = i;
  1053. for (i = 0; i < free_idx; i++) {
  1054. if (PageHighMem(pages[i]))
  1055. continue;
  1056. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1057. end = start + PAGE_SIZE;
  1058. free_memtype(start, end);
  1059. }
  1060. return -EINVAL;
  1061. }
  1062. int set_pages_array_uc(struct page **pages, int addrinarray)
  1063. {
  1064. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_UC_MINUS);
  1065. }
  1066. EXPORT_SYMBOL(set_pages_array_uc);
  1067. int set_pages_array_wc(struct page **pages, int addrinarray)
  1068. {
  1069. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_WC);
  1070. }
  1071. EXPORT_SYMBOL(set_pages_array_wc);
  1072. int set_pages_wb(struct page *page, int numpages)
  1073. {
  1074. unsigned long addr = (unsigned long)page_address(page);
  1075. return set_memory_wb(addr, numpages);
  1076. }
  1077. EXPORT_SYMBOL(set_pages_wb);
  1078. int set_pages_array_wb(struct page **pages, int addrinarray)
  1079. {
  1080. int retval;
  1081. unsigned long start;
  1082. unsigned long end;
  1083. int i;
  1084. retval = cpa_clear_pages_array(pages, addrinarray,
  1085. __pgprot(_PAGE_CACHE_MASK));
  1086. if (retval)
  1087. return retval;
  1088. for (i = 0; i < addrinarray; i++) {
  1089. if (PageHighMem(pages[i]))
  1090. continue;
  1091. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1092. end = start + PAGE_SIZE;
  1093. free_memtype(start, end);
  1094. }
  1095. return 0;
  1096. }
  1097. EXPORT_SYMBOL(set_pages_array_wb);
  1098. int set_pages_x(struct page *page, int numpages)
  1099. {
  1100. unsigned long addr = (unsigned long)page_address(page);
  1101. return set_memory_x(addr, numpages);
  1102. }
  1103. EXPORT_SYMBOL(set_pages_x);
  1104. int set_pages_nx(struct page *page, int numpages)
  1105. {
  1106. unsigned long addr = (unsigned long)page_address(page);
  1107. return set_memory_nx(addr, numpages);
  1108. }
  1109. EXPORT_SYMBOL(set_pages_nx);
  1110. int set_pages_ro(struct page *page, int numpages)
  1111. {
  1112. unsigned long addr = (unsigned long)page_address(page);
  1113. return set_memory_ro(addr, numpages);
  1114. }
  1115. int set_pages_rw(struct page *page, int numpages)
  1116. {
  1117. unsigned long addr = (unsigned long)page_address(page);
  1118. return set_memory_rw(addr, numpages);
  1119. }
  1120. #ifdef CONFIG_DEBUG_PAGEALLOC
  1121. static int __set_pages_p(struct page *page, int numpages)
  1122. {
  1123. unsigned long tempaddr = (unsigned long) page_address(page);
  1124. struct cpa_data cpa = { .vaddr = &tempaddr,
  1125. .numpages = numpages,
  1126. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1127. .mask_clr = __pgprot(0),
  1128. .flags = 0};
  1129. /*
  1130. * No alias checking needed for setting present flag. otherwise,
  1131. * we may need to break large pages for 64-bit kernel text
  1132. * mappings (this adds to complexity if we want to do this from
  1133. * atomic context especially). Let's keep it simple!
  1134. */
  1135. return __change_page_attr_set_clr(&cpa, 0);
  1136. }
  1137. static int __set_pages_np(struct page *page, int numpages)
  1138. {
  1139. unsigned long tempaddr = (unsigned long) page_address(page);
  1140. struct cpa_data cpa = { .vaddr = &tempaddr,
  1141. .numpages = numpages,
  1142. .mask_set = __pgprot(0),
  1143. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1144. .flags = 0};
  1145. /*
  1146. * No alias checking needed for setting not present flag. otherwise,
  1147. * we may need to break large pages for 64-bit kernel text
  1148. * mappings (this adds to complexity if we want to do this from
  1149. * atomic context especially). Let's keep it simple!
  1150. */
  1151. return __change_page_attr_set_clr(&cpa, 0);
  1152. }
  1153. void kernel_map_pages(struct page *page, int numpages, int enable)
  1154. {
  1155. if (PageHighMem(page))
  1156. return;
  1157. if (!enable) {
  1158. debug_check_no_locks_freed(page_address(page),
  1159. numpages * PAGE_SIZE);
  1160. }
  1161. /*
  1162. * The return value is ignored as the calls cannot fail.
  1163. * Large pages for identity mappings are not used at boot time
  1164. * and hence no memory allocations during large page split.
  1165. */
  1166. if (enable)
  1167. __set_pages_p(page, numpages);
  1168. else
  1169. __set_pages_np(page, numpages);
  1170. /*
  1171. * We should perform an IPI and flush all tlbs,
  1172. * but that can deadlock->flush only current cpu:
  1173. */
  1174. __flush_tlb_all();
  1175. }
  1176. #ifdef CONFIG_HIBERNATION
  1177. bool kernel_page_present(struct page *page)
  1178. {
  1179. unsigned int level;
  1180. pte_t *pte;
  1181. if (PageHighMem(page))
  1182. return false;
  1183. pte = lookup_address((unsigned long)page_address(page), &level);
  1184. return (pte_val(*pte) & _PAGE_PRESENT);
  1185. }
  1186. #endif /* CONFIG_HIBERNATION */
  1187. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1188. /*
  1189. * The testcases use internal knowledge of the implementation that shouldn't
  1190. * be exposed to the rest of the kernel. Include these directly here.
  1191. */
  1192. #ifdef CONFIG_CPA_DEBUG
  1193. #include "pageattr-test.c"
  1194. #endif