pageattr.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/pfn.h>
  13. #include <linux/percpu.h>
  14. #include <linux/gfp.h>
  15. #include <linux/pci.h>
  16. #include <linux/vmalloc.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. pgd_t *pgd;
  32. pgprot_t mask_set;
  33. pgprot_t mask_clr;
  34. unsigned long numpages;
  35. int flags;
  36. unsigned long pfn;
  37. unsigned force_split : 1;
  38. int curpage;
  39. struct page **pages;
  40. };
  41. /*
  42. * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
  43. * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
  44. * entries change the page attribute in parallel to some other cpu
  45. * splitting a large page entry along with changing the attribute.
  46. */
  47. static DEFINE_SPINLOCK(cpa_lock);
  48. #define CPA_FLUSHTLB 1
  49. #define CPA_ARRAY 2
  50. #define CPA_PAGES_ARRAY 4
  51. #ifdef CONFIG_PROC_FS
  52. static unsigned long direct_pages_count[PG_LEVEL_NUM];
  53. void update_page_count(int level, unsigned long pages)
  54. {
  55. /* Protect against CPA */
  56. spin_lock(&pgd_lock);
  57. direct_pages_count[level] += pages;
  58. spin_unlock(&pgd_lock);
  59. }
  60. static void split_page_count(int level)
  61. {
  62. if (direct_pages_count[level] == 0)
  63. return;
  64. direct_pages_count[level]--;
  65. direct_pages_count[level - 1] += PTRS_PER_PTE;
  66. }
  67. void arch_report_meminfo(struct seq_file *m)
  68. {
  69. seq_printf(m, "DirectMap4k: %8lu kB\n",
  70. direct_pages_count[PG_LEVEL_4K] << 2);
  71. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  72. seq_printf(m, "DirectMap2M: %8lu kB\n",
  73. direct_pages_count[PG_LEVEL_2M] << 11);
  74. #else
  75. seq_printf(m, "DirectMap4M: %8lu kB\n",
  76. direct_pages_count[PG_LEVEL_2M] << 12);
  77. #endif
  78. if (direct_gbpages)
  79. seq_printf(m, "DirectMap1G: %8lu kB\n",
  80. direct_pages_count[PG_LEVEL_1G] << 20);
  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_symbol(_text) >> PAGE_SHIFT;
  89. }
  90. static inline unsigned long highmap_end_pfn(void)
  91. {
  92. /* Do not reference physical address outside the kernel. */
  93. return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
  94. }
  95. #endif
  96. static inline int
  97. within(unsigned long addr, unsigned long start, unsigned long end)
  98. {
  99. return addr >= start && addr < end;
  100. }
  101. static inline int
  102. within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
  103. {
  104. return addr >= start && addr <= end;
  105. }
  106. /*
  107. * Flushing functions
  108. */
  109. /**
  110. * clflush_cache_range - flush a cache range with clflush
  111. * @vaddr: virtual start address
  112. * @size: number of bytes to flush
  113. *
  114. * clflushopt is an unordered instruction which needs fencing with mfence or
  115. * sfence to avoid ordering issues.
  116. */
  117. void clflush_cache_range(void *vaddr, unsigned int size)
  118. {
  119. const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
  120. void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
  121. void *vend = vaddr + size;
  122. if (p >= vend)
  123. return;
  124. mb();
  125. for (; p < vend; p += clflush_size)
  126. clflushopt(p);
  127. mb();
  128. }
  129. EXPORT_SYMBOL_GPL(clflush_cache_range);
  130. static void __cpa_flush_all(void *arg)
  131. {
  132. unsigned long cache = (unsigned long)arg;
  133. /*
  134. * Flush all to work around Errata in early athlons regarding
  135. * large page flushing.
  136. */
  137. __flush_tlb_all();
  138. if (cache && boot_cpu_data.x86 >= 4)
  139. wbinvd();
  140. }
  141. static void cpa_flush_all(unsigned long cache)
  142. {
  143. BUG_ON(irqs_disabled());
  144. on_each_cpu(__cpa_flush_all, (void *) cache, 1);
  145. }
  146. static void __cpa_flush_range(void *arg)
  147. {
  148. /*
  149. * We could optimize that further and do individual per page
  150. * tlb invalidates for a low number of pages. Caveat: we must
  151. * flush the high aliases on 64bit as well.
  152. */
  153. __flush_tlb_all();
  154. }
  155. static void cpa_flush_range(unsigned long start, int numpages, int cache)
  156. {
  157. unsigned int i, level;
  158. unsigned long addr;
  159. BUG_ON(irqs_disabled());
  160. WARN_ON(PAGE_ALIGN(start) != start);
  161. on_each_cpu(__cpa_flush_range, NULL, 1);
  162. if (!cache)
  163. return;
  164. /*
  165. * We only need to flush on one CPU,
  166. * clflush is a MESI-coherent instruction that
  167. * will cause all other CPUs to flush the same
  168. * cachelines:
  169. */
  170. for (i = 0, addr = start; i < numpages; i++, addr += PAGE_SIZE) {
  171. pte_t *pte = lookup_address(addr, &level);
  172. /*
  173. * Only flush present addresses:
  174. */
  175. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  176. clflush_cache_range((void *) addr, PAGE_SIZE);
  177. }
  178. }
  179. static void cpa_flush_array(unsigned long *start, int numpages, int cache,
  180. int in_flags, struct page **pages)
  181. {
  182. unsigned int i, level;
  183. unsigned long do_wbinvd = cache && numpages >= 1024; /* 4M threshold */
  184. BUG_ON(irqs_disabled());
  185. on_each_cpu(__cpa_flush_all, (void *) do_wbinvd, 1);
  186. if (!cache || do_wbinvd)
  187. return;
  188. /*
  189. * We only need to flush on one CPU,
  190. * clflush is a MESI-coherent instruction that
  191. * will cause all other CPUs to flush the same
  192. * cachelines:
  193. */
  194. for (i = 0; i < numpages; i++) {
  195. unsigned long addr;
  196. pte_t *pte;
  197. if (in_flags & CPA_PAGES_ARRAY)
  198. addr = (unsigned long)page_address(pages[i]);
  199. else
  200. addr = start[i];
  201. pte = lookup_address(addr, &level);
  202. /*
  203. * Only flush present addresses:
  204. */
  205. if (pte && (pte_val(*pte) & _PAGE_PRESENT))
  206. clflush_cache_range((void *)addr, PAGE_SIZE);
  207. }
  208. }
  209. /*
  210. * Certain areas of memory on x86 require very specific protection flags,
  211. * for example the BIOS area or kernel text. Callers don't always get this
  212. * right (again, ioremap() on BIOS memory is not uncommon) so this function
  213. * checks and fixes these known static required protection bits.
  214. */
  215. static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
  216. unsigned long pfn)
  217. {
  218. pgprot_t forbidden = __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_symbol(__start_rodata) >> PAGE_SHIFT,
  239. __pa_symbol(__end_rodata) >> PAGE_SHIFT))
  240. pgprot_val(forbidden) |= _PAGE_RW;
  241. #if defined(CONFIG_X86_64)
  242. /*
  243. * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
  244. * kernel text mappings for the large page aligned text, rodata sections
  245. * will be always read-only. For the kernel identity mappings covering
  246. * the holes caused by this alignment can be anything that user asks.
  247. *
  248. * This will preserve the large page mappings for kernel text/data
  249. * at no extra cost.
  250. */
  251. if (kernel_set_to_readonly &&
  252. within(address, (unsigned long)_text,
  253. (unsigned long)__end_rodata_hpage_align)) {
  254. unsigned int level;
  255. /*
  256. * Don't enforce the !RW mapping for the kernel text mapping,
  257. * if the current mapping is already using small page mapping.
  258. * No need to work hard to preserve large page mappings in this
  259. * case.
  260. *
  261. * This also fixes the Linux Xen paravirt guest boot failure
  262. * (because of unexpected read-only mappings for kernel identity
  263. * mappings). In this paravirt guest case, the kernel text
  264. * mapping and the kernel identity mapping share the same
  265. * page-table pages. Thus we can't really use different
  266. * protections for the kernel text and identity mappings. Also,
  267. * these shared mappings are made of small page mappings.
  268. * Thus this don't enforce !RW mapping for small page kernel
  269. * text mapping logic will help Linux Xen parvirt guest boot
  270. * as well.
  271. */
  272. if (lookup_address(address, &level) && (level != PG_LEVEL_4K))
  273. pgprot_val(forbidden) |= _PAGE_RW;
  274. }
  275. #endif
  276. prot = __pgprot(pgprot_val(prot) & ~pgprot_val(forbidden));
  277. return prot;
  278. }
  279. /*
  280. * Lookup the page table entry for a virtual address in a specific pgd.
  281. * Return a pointer to the entry and the level of the mapping.
  282. */
  283. pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
  284. unsigned int *level)
  285. {
  286. pud_t *pud;
  287. pmd_t *pmd;
  288. *level = PG_LEVEL_NONE;
  289. if (pgd_none(*pgd))
  290. return NULL;
  291. pud = pud_offset(pgd, address);
  292. if (pud_none(*pud))
  293. return NULL;
  294. *level = PG_LEVEL_1G;
  295. if (pud_large(*pud) || !pud_present(*pud))
  296. return (pte_t *)pud;
  297. pmd = pmd_offset(pud, address);
  298. if (pmd_none(*pmd))
  299. return NULL;
  300. *level = PG_LEVEL_2M;
  301. if (pmd_large(*pmd) || !pmd_present(*pmd))
  302. return (pte_t *)pmd;
  303. *level = PG_LEVEL_4K;
  304. return pte_offset_kernel(pmd, address);
  305. }
  306. /*
  307. * Lookup the page table entry for a virtual address. Return a pointer
  308. * to the entry and the level of the mapping.
  309. *
  310. * Note: We return pud and pmd either when the entry is marked large
  311. * or when the present bit is not set. Otherwise we would return a
  312. * pointer to a nonexisting mapping.
  313. */
  314. pte_t *lookup_address(unsigned long address, unsigned int *level)
  315. {
  316. return lookup_address_in_pgd(pgd_offset_k(address), address, level);
  317. }
  318. EXPORT_SYMBOL_GPL(lookup_address);
  319. static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
  320. unsigned int *level)
  321. {
  322. if (cpa->pgd)
  323. return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
  324. address, level);
  325. return lookup_address(address, level);
  326. }
  327. /*
  328. * Lookup the PMD entry for a virtual address. Return a pointer to the entry
  329. * or NULL if not present.
  330. */
  331. pmd_t *lookup_pmd_address(unsigned long address)
  332. {
  333. pgd_t *pgd;
  334. pud_t *pud;
  335. pgd = pgd_offset_k(address);
  336. if (pgd_none(*pgd))
  337. return NULL;
  338. pud = pud_offset(pgd, address);
  339. if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
  340. return NULL;
  341. return pmd_offset(pud, address);
  342. }
  343. /*
  344. * This is necessary because __pa() does not work on some
  345. * kinds of memory, like vmalloc() or the alloc_remap()
  346. * areas on 32-bit NUMA systems. The percpu areas can
  347. * end up in this kind of memory, for instance.
  348. *
  349. * This could be optimized, but it is only intended to be
  350. * used at inititalization time, and keeping it
  351. * unoptimized should increase the testing coverage for
  352. * the more obscure platforms.
  353. */
  354. phys_addr_t slow_virt_to_phys(void *__virt_addr)
  355. {
  356. unsigned long virt_addr = (unsigned long)__virt_addr;
  357. phys_addr_t phys_addr;
  358. unsigned long offset;
  359. enum pg_level level;
  360. pte_t *pte;
  361. pte = lookup_address(virt_addr, &level);
  362. BUG_ON(!pte);
  363. /*
  364. * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
  365. * before being left-shifted PAGE_SHIFT bits -- this trick is to
  366. * make 32-PAE kernel work correctly.
  367. */
  368. switch (level) {
  369. case PG_LEVEL_1G:
  370. phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
  371. offset = virt_addr & ~PUD_PAGE_MASK;
  372. break;
  373. case PG_LEVEL_2M:
  374. phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
  375. offset = virt_addr & ~PMD_PAGE_MASK;
  376. break;
  377. default:
  378. phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
  379. offset = virt_addr & ~PAGE_MASK;
  380. }
  381. return (phys_addr_t)(phys_addr | offset);
  382. }
  383. EXPORT_SYMBOL_GPL(slow_virt_to_phys);
  384. /*
  385. * Set the new pmd in all the pgds we know about:
  386. */
  387. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  388. {
  389. /* change init_mm */
  390. set_pte_atomic(kpte, pte);
  391. #ifdef CONFIG_X86_32
  392. if (!SHARED_KERNEL_PMD) {
  393. struct page *page;
  394. list_for_each_entry(page, &pgd_list, lru) {
  395. pgd_t *pgd;
  396. pud_t *pud;
  397. pmd_t *pmd;
  398. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  399. pud = pud_offset(pgd, address);
  400. pmd = pmd_offset(pud, address);
  401. set_pte_atomic((pte_t *)pmd, pte);
  402. }
  403. }
  404. #endif
  405. }
  406. static int
  407. try_preserve_large_page(pte_t *kpte, unsigned long address,
  408. struct cpa_data *cpa)
  409. {
  410. unsigned long nextpage_addr, numpages, pmask, psize, addr, pfn, old_pfn;
  411. pte_t new_pte, old_pte, *tmp;
  412. pgprot_t old_prot, new_prot, req_prot;
  413. int i, do_split = 1;
  414. enum pg_level level;
  415. if (cpa->force_split)
  416. return 1;
  417. spin_lock(&pgd_lock);
  418. /*
  419. * Check for races, another CPU might have split this page
  420. * up already:
  421. */
  422. tmp = _lookup_address_cpa(cpa, address, &level);
  423. if (tmp != kpte)
  424. goto out_unlock;
  425. switch (level) {
  426. case PG_LEVEL_2M:
  427. old_prot = pmd_pgprot(*(pmd_t *)kpte);
  428. old_pfn = pmd_pfn(*(pmd_t *)kpte);
  429. break;
  430. case PG_LEVEL_1G:
  431. old_prot = pud_pgprot(*(pud_t *)kpte);
  432. old_pfn = pud_pfn(*(pud_t *)kpte);
  433. break;
  434. default:
  435. do_split = -EINVAL;
  436. goto out_unlock;
  437. }
  438. psize = page_level_size(level);
  439. pmask = page_level_mask(level);
  440. /*
  441. * Calculate the number of pages, which fit into this large
  442. * page starting at address:
  443. */
  444. nextpage_addr = (address + psize) & pmask;
  445. numpages = (nextpage_addr - address) >> PAGE_SHIFT;
  446. if (numpages < cpa->numpages)
  447. cpa->numpages = numpages;
  448. /*
  449. * We are safe now. Check whether the new pgprot is the same:
  450. * Convert protection attributes to 4k-format, as cpa->mask* are set
  451. * up accordingly.
  452. */
  453. old_pte = *kpte;
  454. req_prot = pgprot_large_2_4k(old_prot);
  455. pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
  456. pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
  457. /*
  458. * req_prot is in format of 4k pages. It must be converted to large
  459. * page format: the caching mode includes the PAT bit located at
  460. * different bit positions in the two formats.
  461. */
  462. req_prot = pgprot_4k_2_large(req_prot);
  463. /*
  464. * Set the PSE and GLOBAL flags only if the PRESENT flag is
  465. * set otherwise pmd_present/pmd_huge will return true even on
  466. * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL
  467. * for the ancient hardware that doesn't support it.
  468. */
  469. if (pgprot_val(req_prot) & _PAGE_PRESENT)
  470. pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL;
  471. else
  472. pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL);
  473. req_prot = canon_pgprot(req_prot);
  474. /*
  475. * old_pfn points to the large page base pfn. So we need
  476. * to add the offset of the virtual address:
  477. */
  478. pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
  479. cpa->pfn = pfn;
  480. new_prot = static_protections(req_prot, address, pfn);
  481. /*
  482. * We need to check the full range, whether
  483. * static_protection() requires a different pgprot for one of
  484. * the pages in the range we try to preserve:
  485. */
  486. addr = address & pmask;
  487. pfn = old_pfn;
  488. for (i = 0; i < (psize >> PAGE_SHIFT); i++, addr += PAGE_SIZE, pfn++) {
  489. pgprot_t chk_prot = static_protections(req_prot, addr, pfn);
  490. if (pgprot_val(chk_prot) != pgprot_val(new_prot))
  491. goto out_unlock;
  492. }
  493. /*
  494. * If there are no changes, return. maxpages has been updated
  495. * above:
  496. */
  497. if (pgprot_val(new_prot) == pgprot_val(old_prot)) {
  498. do_split = 0;
  499. goto out_unlock;
  500. }
  501. /*
  502. * We need to change the attributes. Check, whether we can
  503. * change the large page in one go. We request a split, when
  504. * the address is not aligned and the number of pages is
  505. * smaller than the number of pages in the large page. Note
  506. * that we limited the number of possible pages already to
  507. * the number of pages in the large page.
  508. */
  509. if (address == (address & pmask) && cpa->numpages == (psize >> PAGE_SHIFT)) {
  510. /*
  511. * The address is aligned and the number of pages
  512. * covers the full page.
  513. */
  514. new_pte = pfn_pte(old_pfn, new_prot);
  515. __set_pmd_pte(kpte, address, new_pte);
  516. cpa->flags |= CPA_FLUSHTLB;
  517. do_split = 0;
  518. }
  519. out_unlock:
  520. spin_unlock(&pgd_lock);
  521. return do_split;
  522. }
  523. static int
  524. __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
  525. struct page *base)
  526. {
  527. pte_t *pbase = (pte_t *)page_address(base);
  528. unsigned long ref_pfn, pfn, pfninc = 1;
  529. unsigned int i, level;
  530. pte_t *tmp;
  531. pgprot_t ref_prot;
  532. spin_lock(&pgd_lock);
  533. /*
  534. * Check for races, another CPU might have split this page
  535. * up for us already:
  536. */
  537. tmp = _lookup_address_cpa(cpa, address, &level);
  538. if (tmp != kpte) {
  539. spin_unlock(&pgd_lock);
  540. return 1;
  541. }
  542. paravirt_alloc_pte(&init_mm, page_to_pfn(base));
  543. switch (level) {
  544. case PG_LEVEL_2M:
  545. ref_prot = pmd_pgprot(*(pmd_t *)kpte);
  546. /* clear PSE and promote PAT bit to correct position */
  547. ref_prot = pgprot_large_2_4k(ref_prot);
  548. ref_pfn = pmd_pfn(*(pmd_t *)kpte);
  549. break;
  550. case PG_LEVEL_1G:
  551. ref_prot = pud_pgprot(*(pud_t *)kpte);
  552. ref_pfn = pud_pfn(*(pud_t *)kpte);
  553. pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
  554. /*
  555. * Clear the PSE flags if the PRESENT flag is not set
  556. * otherwise pmd_present/pmd_huge will return true
  557. * even on a non present pmd.
  558. */
  559. if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
  560. pgprot_val(ref_prot) &= ~_PAGE_PSE;
  561. break;
  562. default:
  563. spin_unlock(&pgd_lock);
  564. return 1;
  565. }
  566. /*
  567. * Set the GLOBAL flags only if the PRESENT flag is set
  568. * otherwise pmd/pte_present will return true even on a non
  569. * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL
  570. * for the ancient hardware that doesn't support it.
  571. */
  572. if (pgprot_val(ref_prot) & _PAGE_PRESENT)
  573. pgprot_val(ref_prot) |= _PAGE_GLOBAL;
  574. else
  575. pgprot_val(ref_prot) &= ~_PAGE_GLOBAL;
  576. /*
  577. * Get the target pfn from the original entry:
  578. */
  579. pfn = ref_pfn;
  580. for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc)
  581. set_pte(&pbase[i], pfn_pte(pfn, canon_pgprot(ref_prot)));
  582. if (virt_addr_valid(address)) {
  583. unsigned long pfn = PFN_DOWN(__pa(address));
  584. if (pfn_range_is_mapped(pfn, pfn + 1))
  585. split_page_count(level);
  586. }
  587. /*
  588. * Install the new, split up pagetable.
  589. *
  590. * We use the standard kernel pagetable protections for the new
  591. * pagetable protections, the actual ptes set above control the
  592. * primary protection behavior:
  593. */
  594. __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
  595. /*
  596. * Intel Atom errata AAH41 workaround.
  597. *
  598. * The real fix should be in hw or in a microcode update, but
  599. * we also probabilistically try to reduce the window of having
  600. * a large TLB mixed with 4K TLBs while instruction fetches are
  601. * going on.
  602. */
  603. __flush_tlb_all();
  604. spin_unlock(&pgd_lock);
  605. return 0;
  606. }
  607. static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
  608. unsigned long address)
  609. {
  610. struct page *base;
  611. if (!debug_pagealloc_enabled())
  612. spin_unlock(&cpa_lock);
  613. base = alloc_pages(GFP_KERNEL | __GFP_NOTRACK, 0);
  614. if (!debug_pagealloc_enabled())
  615. spin_lock(&cpa_lock);
  616. if (!base)
  617. return -ENOMEM;
  618. if (__split_large_page(cpa, kpte, address, base))
  619. __free_page(base);
  620. return 0;
  621. }
  622. static bool try_to_free_pte_page(pte_t *pte)
  623. {
  624. int i;
  625. for (i = 0; i < PTRS_PER_PTE; i++)
  626. if (!pte_none(pte[i]))
  627. return false;
  628. free_page((unsigned long)pte);
  629. return true;
  630. }
  631. static bool try_to_free_pmd_page(pmd_t *pmd)
  632. {
  633. int i;
  634. for (i = 0; i < PTRS_PER_PMD; i++)
  635. if (!pmd_none(pmd[i]))
  636. return false;
  637. free_page((unsigned long)pmd);
  638. return true;
  639. }
  640. static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
  641. {
  642. pte_t *pte = pte_offset_kernel(pmd, start);
  643. while (start < end) {
  644. set_pte(pte, __pte(0));
  645. start += PAGE_SIZE;
  646. pte++;
  647. }
  648. if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
  649. pmd_clear(pmd);
  650. return true;
  651. }
  652. return false;
  653. }
  654. static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
  655. unsigned long start, unsigned long end)
  656. {
  657. if (unmap_pte_range(pmd, start, end))
  658. if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
  659. pud_clear(pud);
  660. }
  661. static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
  662. {
  663. pmd_t *pmd = pmd_offset(pud, start);
  664. /*
  665. * Not on a 2MB page boundary?
  666. */
  667. if (start & (PMD_SIZE - 1)) {
  668. unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
  669. unsigned long pre_end = min_t(unsigned long, end, next_page);
  670. __unmap_pmd_range(pud, pmd, start, pre_end);
  671. start = pre_end;
  672. pmd++;
  673. }
  674. /*
  675. * Try to unmap in 2M chunks.
  676. */
  677. while (end - start >= PMD_SIZE) {
  678. if (pmd_large(*pmd))
  679. pmd_clear(pmd);
  680. else
  681. __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
  682. start += PMD_SIZE;
  683. pmd++;
  684. }
  685. /*
  686. * 4K leftovers?
  687. */
  688. if (start < end)
  689. return __unmap_pmd_range(pud, pmd, start, end);
  690. /*
  691. * Try again to free the PMD page if haven't succeeded above.
  692. */
  693. if (!pud_none(*pud))
  694. if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
  695. pud_clear(pud);
  696. }
  697. static void unmap_pud_range(pgd_t *pgd, unsigned long start, unsigned long end)
  698. {
  699. pud_t *pud = pud_offset(pgd, start);
  700. /*
  701. * Not on a GB page boundary?
  702. */
  703. if (start & (PUD_SIZE - 1)) {
  704. unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
  705. unsigned long pre_end = min_t(unsigned long, end, next_page);
  706. unmap_pmd_range(pud, start, pre_end);
  707. start = pre_end;
  708. pud++;
  709. }
  710. /*
  711. * Try to unmap in 1G chunks?
  712. */
  713. while (end - start >= PUD_SIZE) {
  714. if (pud_large(*pud))
  715. pud_clear(pud);
  716. else
  717. unmap_pmd_range(pud, start, start + PUD_SIZE);
  718. start += PUD_SIZE;
  719. pud++;
  720. }
  721. /*
  722. * 2M leftovers?
  723. */
  724. if (start < end)
  725. unmap_pmd_range(pud, start, end);
  726. /*
  727. * No need to try to free the PUD page because we'll free it in
  728. * populate_pgd's error path
  729. */
  730. }
  731. static int alloc_pte_page(pmd_t *pmd)
  732. {
  733. pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  734. if (!pte)
  735. return -1;
  736. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
  737. return 0;
  738. }
  739. static int alloc_pmd_page(pud_t *pud)
  740. {
  741. pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  742. if (!pmd)
  743. return -1;
  744. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
  745. return 0;
  746. }
  747. static void populate_pte(struct cpa_data *cpa,
  748. unsigned long start, unsigned long end,
  749. unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
  750. {
  751. pte_t *pte;
  752. pte = pte_offset_kernel(pmd, start);
  753. /*
  754. * Set the GLOBAL flags only if the PRESENT flag is
  755. * set otherwise pte_present will return true even on
  756. * a non present pte. The canon_pgprot will clear
  757. * _PAGE_GLOBAL for the ancient hardware that doesn't
  758. * support it.
  759. */
  760. if (pgprot_val(pgprot) & _PAGE_PRESENT)
  761. pgprot_val(pgprot) |= _PAGE_GLOBAL;
  762. else
  763. pgprot_val(pgprot) &= ~_PAGE_GLOBAL;
  764. pgprot = canon_pgprot(pgprot);
  765. while (num_pages-- && start < end) {
  766. set_pte(pte, pfn_pte(cpa->pfn, pgprot));
  767. start += PAGE_SIZE;
  768. cpa->pfn++;
  769. pte++;
  770. }
  771. }
  772. static long populate_pmd(struct cpa_data *cpa,
  773. unsigned long start, unsigned long end,
  774. unsigned num_pages, pud_t *pud, pgprot_t pgprot)
  775. {
  776. long cur_pages = 0;
  777. pmd_t *pmd;
  778. pgprot_t pmd_pgprot;
  779. /*
  780. * Not on a 2M boundary?
  781. */
  782. if (start & (PMD_SIZE - 1)) {
  783. unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
  784. unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
  785. pre_end = min_t(unsigned long, pre_end, next_page);
  786. cur_pages = (pre_end - start) >> PAGE_SHIFT;
  787. cur_pages = min_t(unsigned int, num_pages, cur_pages);
  788. /*
  789. * Need a PTE page?
  790. */
  791. pmd = pmd_offset(pud, start);
  792. if (pmd_none(*pmd))
  793. if (alloc_pte_page(pmd))
  794. return -1;
  795. populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
  796. start = pre_end;
  797. }
  798. /*
  799. * We mapped them all?
  800. */
  801. if (num_pages == cur_pages)
  802. return cur_pages;
  803. pmd_pgprot = pgprot_4k_2_large(pgprot);
  804. while (end - start >= PMD_SIZE) {
  805. /*
  806. * We cannot use a 1G page so allocate a PMD page if needed.
  807. */
  808. if (pud_none(*pud))
  809. if (alloc_pmd_page(pud))
  810. return -1;
  811. pmd = pmd_offset(pud, start);
  812. set_pmd(pmd, __pmd(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
  813. massage_pgprot(pmd_pgprot)));
  814. start += PMD_SIZE;
  815. cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
  816. cur_pages += PMD_SIZE >> PAGE_SHIFT;
  817. }
  818. /*
  819. * Map trailing 4K pages.
  820. */
  821. if (start < end) {
  822. pmd = pmd_offset(pud, start);
  823. if (pmd_none(*pmd))
  824. if (alloc_pte_page(pmd))
  825. return -1;
  826. populate_pte(cpa, start, end, num_pages - cur_pages,
  827. pmd, pgprot);
  828. }
  829. return num_pages;
  830. }
  831. static long populate_pud(struct cpa_data *cpa, unsigned long start, pgd_t *pgd,
  832. pgprot_t pgprot)
  833. {
  834. pud_t *pud;
  835. unsigned long end;
  836. long cur_pages = 0;
  837. pgprot_t pud_pgprot;
  838. end = start + (cpa->numpages << PAGE_SHIFT);
  839. /*
  840. * Not on a Gb page boundary? => map everything up to it with
  841. * smaller pages.
  842. */
  843. if (start & (PUD_SIZE - 1)) {
  844. unsigned long pre_end;
  845. unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
  846. pre_end = min_t(unsigned long, end, next_page);
  847. cur_pages = (pre_end - start) >> PAGE_SHIFT;
  848. cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
  849. pud = pud_offset(pgd, start);
  850. /*
  851. * Need a PMD page?
  852. */
  853. if (pud_none(*pud))
  854. if (alloc_pmd_page(pud))
  855. return -1;
  856. cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
  857. pud, pgprot);
  858. if (cur_pages < 0)
  859. return cur_pages;
  860. start = pre_end;
  861. }
  862. /* We mapped them all? */
  863. if (cpa->numpages == cur_pages)
  864. return cur_pages;
  865. pud = pud_offset(pgd, start);
  866. pud_pgprot = pgprot_4k_2_large(pgprot);
  867. /*
  868. * Map everything starting from the Gb boundary, possibly with 1G pages
  869. */
  870. while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
  871. set_pud(pud, __pud(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
  872. massage_pgprot(pud_pgprot)));
  873. start += PUD_SIZE;
  874. cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
  875. cur_pages += PUD_SIZE >> PAGE_SHIFT;
  876. pud++;
  877. }
  878. /* Map trailing leftover */
  879. if (start < end) {
  880. long tmp;
  881. pud = pud_offset(pgd, start);
  882. if (pud_none(*pud))
  883. if (alloc_pmd_page(pud))
  884. return -1;
  885. tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
  886. pud, pgprot);
  887. if (tmp < 0)
  888. return cur_pages;
  889. cur_pages += tmp;
  890. }
  891. return cur_pages;
  892. }
  893. /*
  894. * Restrictions for kernel page table do not necessarily apply when mapping in
  895. * an alternate PGD.
  896. */
  897. static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
  898. {
  899. pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
  900. pud_t *pud = NULL; /* shut up gcc */
  901. pgd_t *pgd_entry;
  902. long ret;
  903. pgd_entry = cpa->pgd + pgd_index(addr);
  904. /*
  905. * Allocate a PUD page and hand it down for mapping.
  906. */
  907. if (pgd_none(*pgd_entry)) {
  908. pud = (pud_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
  909. if (!pud)
  910. return -1;
  911. set_pgd(pgd_entry, __pgd(__pa(pud) | _KERNPG_TABLE));
  912. }
  913. pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
  914. pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
  915. ret = populate_pud(cpa, addr, pgd_entry, pgprot);
  916. if (ret < 0) {
  917. /*
  918. * Leave the PUD page in place in case some other CPU or thread
  919. * already found it, but remove any useless entries we just
  920. * added to it.
  921. */
  922. unmap_pud_range(pgd_entry, addr,
  923. addr + (cpa->numpages << PAGE_SHIFT));
  924. return ret;
  925. }
  926. cpa->numpages = ret;
  927. return 0;
  928. }
  929. static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
  930. int primary)
  931. {
  932. if (cpa->pgd) {
  933. /*
  934. * Right now, we only execute this code path when mapping
  935. * the EFI virtual memory map regions, no other users
  936. * provide a ->pgd value. This may change in the future.
  937. */
  938. return populate_pgd(cpa, vaddr);
  939. }
  940. /*
  941. * Ignore all non primary paths.
  942. */
  943. if (!primary) {
  944. cpa->numpages = 1;
  945. return 0;
  946. }
  947. /*
  948. * Ignore the NULL PTE for kernel identity mapping, as it is expected
  949. * to have holes.
  950. * Also set numpages to '1' indicating that we processed cpa req for
  951. * one virtual address page and its pfn. TBD: numpages can be set based
  952. * on the initial value and the level returned by lookup_address().
  953. */
  954. if (within(vaddr, PAGE_OFFSET,
  955. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
  956. cpa->numpages = 1;
  957. cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
  958. return 0;
  959. } else {
  960. WARN(1, KERN_WARNING "CPA: called for zero pte. "
  961. "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
  962. *cpa->vaddr);
  963. return -EFAULT;
  964. }
  965. }
  966. static int __change_page_attr(struct cpa_data *cpa, int primary)
  967. {
  968. unsigned long address;
  969. int do_split, err;
  970. unsigned int level;
  971. pte_t *kpte, old_pte;
  972. if (cpa->flags & CPA_PAGES_ARRAY) {
  973. struct page *page = cpa->pages[cpa->curpage];
  974. if (unlikely(PageHighMem(page)))
  975. return 0;
  976. address = (unsigned long)page_address(page);
  977. } else if (cpa->flags & CPA_ARRAY)
  978. address = cpa->vaddr[cpa->curpage];
  979. else
  980. address = *cpa->vaddr;
  981. repeat:
  982. kpte = _lookup_address_cpa(cpa, address, &level);
  983. if (!kpte)
  984. return __cpa_process_fault(cpa, address, primary);
  985. old_pte = *kpte;
  986. if (pte_none(old_pte))
  987. return __cpa_process_fault(cpa, address, primary);
  988. if (level == PG_LEVEL_4K) {
  989. pte_t new_pte;
  990. pgprot_t new_prot = pte_pgprot(old_pte);
  991. unsigned long pfn = pte_pfn(old_pte);
  992. pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
  993. pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
  994. new_prot = static_protections(new_prot, address, pfn);
  995. /*
  996. * Set the GLOBAL flags only if the PRESENT flag is
  997. * set otherwise pte_present will return true even on
  998. * a non present pte. The canon_pgprot will clear
  999. * _PAGE_GLOBAL for the ancient hardware that doesn't
  1000. * support it.
  1001. */
  1002. if (pgprot_val(new_prot) & _PAGE_PRESENT)
  1003. pgprot_val(new_prot) |= _PAGE_GLOBAL;
  1004. else
  1005. pgprot_val(new_prot) &= ~_PAGE_GLOBAL;
  1006. /*
  1007. * We need to keep the pfn from the existing PTE,
  1008. * after all we're only going to change it's attributes
  1009. * not the memory it points to
  1010. */
  1011. new_pte = pfn_pte(pfn, canon_pgprot(new_prot));
  1012. cpa->pfn = pfn;
  1013. /*
  1014. * Do we really change anything ?
  1015. */
  1016. if (pte_val(old_pte) != pte_val(new_pte)) {
  1017. set_pte_atomic(kpte, new_pte);
  1018. cpa->flags |= CPA_FLUSHTLB;
  1019. }
  1020. cpa->numpages = 1;
  1021. return 0;
  1022. }
  1023. /*
  1024. * Check, whether we can keep the large page intact
  1025. * and just change the pte:
  1026. */
  1027. do_split = try_preserve_large_page(kpte, address, cpa);
  1028. /*
  1029. * When the range fits into the existing large page,
  1030. * return. cp->numpages and cpa->tlbflush have been updated in
  1031. * try_large_page:
  1032. */
  1033. if (do_split <= 0)
  1034. return do_split;
  1035. /*
  1036. * We have to split the large page:
  1037. */
  1038. err = split_large_page(cpa, kpte, address);
  1039. if (!err) {
  1040. /*
  1041. * Do a global flush tlb after splitting the large page
  1042. * and before we do the actual change page attribute in the PTE.
  1043. *
  1044. * With out this, we violate the TLB application note, that says
  1045. * "The TLBs may contain both ordinary and large-page
  1046. * translations for a 4-KByte range of linear addresses. This
  1047. * may occur if software modifies the paging structures so that
  1048. * the page size used for the address range changes. If the two
  1049. * translations differ with respect to page frame or attributes
  1050. * (e.g., permissions), processor behavior is undefined and may
  1051. * be implementation-specific."
  1052. *
  1053. * We do this global tlb flush inside the cpa_lock, so that we
  1054. * don't allow any other cpu, with stale tlb entries change the
  1055. * page attribute in parallel, that also falls into the
  1056. * just split large page entry.
  1057. */
  1058. flush_tlb_all();
  1059. goto repeat;
  1060. }
  1061. return err;
  1062. }
  1063. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
  1064. static int cpa_process_alias(struct cpa_data *cpa)
  1065. {
  1066. struct cpa_data alias_cpa;
  1067. unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
  1068. unsigned long vaddr;
  1069. int ret;
  1070. if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
  1071. return 0;
  1072. /*
  1073. * No need to redo, when the primary call touched the direct
  1074. * mapping already:
  1075. */
  1076. if (cpa->flags & CPA_PAGES_ARRAY) {
  1077. struct page *page = cpa->pages[cpa->curpage];
  1078. if (unlikely(PageHighMem(page)))
  1079. return 0;
  1080. vaddr = (unsigned long)page_address(page);
  1081. } else if (cpa->flags & CPA_ARRAY)
  1082. vaddr = cpa->vaddr[cpa->curpage];
  1083. else
  1084. vaddr = *cpa->vaddr;
  1085. if (!(within(vaddr, PAGE_OFFSET,
  1086. PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
  1087. alias_cpa = *cpa;
  1088. alias_cpa.vaddr = &laddr;
  1089. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  1090. ret = __change_page_attr_set_clr(&alias_cpa, 0);
  1091. if (ret)
  1092. return ret;
  1093. }
  1094. #ifdef CONFIG_X86_64
  1095. /*
  1096. * If the primary call didn't touch the high mapping already
  1097. * and the physical address is inside the kernel map, we need
  1098. * to touch the high mapped kernel as well:
  1099. */
  1100. if (!within(vaddr, (unsigned long)_text, _brk_end) &&
  1101. within_inclusive(cpa->pfn, highmap_start_pfn(),
  1102. highmap_end_pfn())) {
  1103. unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
  1104. __START_KERNEL_map - phys_base;
  1105. alias_cpa = *cpa;
  1106. alias_cpa.vaddr = &temp_cpa_vaddr;
  1107. alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
  1108. /*
  1109. * The high mapping range is imprecise, so ignore the
  1110. * return value.
  1111. */
  1112. __change_page_attr_set_clr(&alias_cpa, 0);
  1113. }
  1114. #endif
  1115. return 0;
  1116. }
  1117. static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
  1118. {
  1119. unsigned long numpages = cpa->numpages;
  1120. int ret;
  1121. while (numpages) {
  1122. /*
  1123. * Store the remaining nr of pages for the large page
  1124. * preservation check.
  1125. */
  1126. cpa->numpages = numpages;
  1127. /* for array changes, we can't use large page */
  1128. if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
  1129. cpa->numpages = 1;
  1130. if (!debug_pagealloc_enabled())
  1131. spin_lock(&cpa_lock);
  1132. ret = __change_page_attr(cpa, checkalias);
  1133. if (!debug_pagealloc_enabled())
  1134. spin_unlock(&cpa_lock);
  1135. if (ret)
  1136. return ret;
  1137. if (checkalias) {
  1138. ret = cpa_process_alias(cpa);
  1139. if (ret)
  1140. return ret;
  1141. }
  1142. /*
  1143. * Adjust the number of pages with the result of the
  1144. * CPA operation. Either a large page has been
  1145. * preserved or a single page update happened.
  1146. */
  1147. BUG_ON(cpa->numpages > numpages || !cpa->numpages);
  1148. numpages -= cpa->numpages;
  1149. if (cpa->flags & (CPA_PAGES_ARRAY | CPA_ARRAY))
  1150. cpa->curpage++;
  1151. else
  1152. *cpa->vaddr += cpa->numpages * PAGE_SIZE;
  1153. }
  1154. return 0;
  1155. }
  1156. static int change_page_attr_set_clr(unsigned long *addr, int numpages,
  1157. pgprot_t mask_set, pgprot_t mask_clr,
  1158. int force_split, int in_flag,
  1159. struct page **pages)
  1160. {
  1161. struct cpa_data cpa;
  1162. int ret, cache, checkalias;
  1163. unsigned long baddr = 0;
  1164. memset(&cpa, 0, sizeof(cpa));
  1165. /*
  1166. * Check, if we are requested to change a not supported
  1167. * feature:
  1168. */
  1169. mask_set = canon_pgprot(mask_set);
  1170. mask_clr = canon_pgprot(mask_clr);
  1171. if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
  1172. return 0;
  1173. /* Ensure we are PAGE_SIZE aligned */
  1174. if (in_flag & CPA_ARRAY) {
  1175. int i;
  1176. for (i = 0; i < numpages; i++) {
  1177. if (addr[i] & ~PAGE_MASK) {
  1178. addr[i] &= PAGE_MASK;
  1179. WARN_ON_ONCE(1);
  1180. }
  1181. }
  1182. } else if (!(in_flag & CPA_PAGES_ARRAY)) {
  1183. /*
  1184. * in_flag of CPA_PAGES_ARRAY implies it is aligned.
  1185. * No need to cehck in that case
  1186. */
  1187. if (*addr & ~PAGE_MASK) {
  1188. *addr &= PAGE_MASK;
  1189. /*
  1190. * People should not be passing in unaligned addresses:
  1191. */
  1192. WARN_ON_ONCE(1);
  1193. }
  1194. /*
  1195. * Save address for cache flush. *addr is modified in the call
  1196. * to __change_page_attr_set_clr() below.
  1197. */
  1198. baddr = *addr;
  1199. }
  1200. /* Must avoid aliasing mappings in the highmem code */
  1201. kmap_flush_unused();
  1202. vm_unmap_aliases();
  1203. cpa.vaddr = addr;
  1204. cpa.pages = pages;
  1205. cpa.numpages = numpages;
  1206. cpa.mask_set = mask_set;
  1207. cpa.mask_clr = mask_clr;
  1208. cpa.flags = 0;
  1209. cpa.curpage = 0;
  1210. cpa.force_split = force_split;
  1211. if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
  1212. cpa.flags |= in_flag;
  1213. /* No alias checking for _NX bit modifications */
  1214. checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
  1215. ret = __change_page_attr_set_clr(&cpa, checkalias);
  1216. /*
  1217. * Check whether we really changed something:
  1218. */
  1219. if (!(cpa.flags & CPA_FLUSHTLB))
  1220. goto out;
  1221. /*
  1222. * No need to flush, when we did not set any of the caching
  1223. * attributes:
  1224. */
  1225. cache = !!pgprot2cachemode(mask_set);
  1226. /*
  1227. * On success we use CLFLUSH, when the CPU supports it to
  1228. * avoid the WBINVD. If the CPU does not support it and in the
  1229. * error case we fall back to cpa_flush_all (which uses
  1230. * WBINVD):
  1231. */
  1232. if (!ret && boot_cpu_has(X86_FEATURE_CLFLUSH)) {
  1233. if (cpa.flags & (CPA_PAGES_ARRAY | CPA_ARRAY)) {
  1234. cpa_flush_array(addr, numpages, cache,
  1235. cpa.flags, pages);
  1236. } else
  1237. cpa_flush_range(baddr, numpages, cache);
  1238. } else
  1239. cpa_flush_all(cache);
  1240. out:
  1241. return ret;
  1242. }
  1243. static inline int change_page_attr_set(unsigned long *addr, int numpages,
  1244. pgprot_t mask, int array)
  1245. {
  1246. return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
  1247. (array ? CPA_ARRAY : 0), NULL);
  1248. }
  1249. static inline int change_page_attr_clear(unsigned long *addr, int numpages,
  1250. pgprot_t mask, int array)
  1251. {
  1252. return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
  1253. (array ? CPA_ARRAY : 0), NULL);
  1254. }
  1255. static inline int cpa_set_pages_array(struct page **pages, int numpages,
  1256. pgprot_t mask)
  1257. {
  1258. return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
  1259. CPA_PAGES_ARRAY, pages);
  1260. }
  1261. static inline int cpa_clear_pages_array(struct page **pages, int numpages,
  1262. pgprot_t mask)
  1263. {
  1264. return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
  1265. CPA_PAGES_ARRAY, pages);
  1266. }
  1267. int _set_memory_uc(unsigned long addr, int numpages)
  1268. {
  1269. /*
  1270. * for now UC MINUS. see comments in ioremap_nocache()
  1271. * If you really need strong UC use ioremap_uc(), but note
  1272. * that you cannot override IO areas with set_memory_*() as
  1273. * these helpers cannot work with IO memory.
  1274. */
  1275. return change_page_attr_set(&addr, numpages,
  1276. cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
  1277. 0);
  1278. }
  1279. int set_memory_uc(unsigned long addr, int numpages)
  1280. {
  1281. int ret;
  1282. /*
  1283. * for now UC MINUS. see comments in ioremap_nocache()
  1284. */
  1285. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1286. _PAGE_CACHE_MODE_UC_MINUS, NULL);
  1287. if (ret)
  1288. goto out_err;
  1289. ret = _set_memory_uc(addr, numpages);
  1290. if (ret)
  1291. goto out_free;
  1292. return 0;
  1293. out_free:
  1294. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1295. out_err:
  1296. return ret;
  1297. }
  1298. EXPORT_SYMBOL(set_memory_uc);
  1299. static int _set_memory_array(unsigned long *addr, int addrinarray,
  1300. enum page_cache_mode new_type)
  1301. {
  1302. enum page_cache_mode set_type;
  1303. int i, j;
  1304. int ret;
  1305. for (i = 0; i < addrinarray; i++) {
  1306. ret = reserve_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE,
  1307. new_type, NULL);
  1308. if (ret)
  1309. goto out_free;
  1310. }
  1311. /* If WC, set to UC- first and then WC */
  1312. set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
  1313. _PAGE_CACHE_MODE_UC_MINUS : new_type;
  1314. ret = change_page_attr_set(addr, addrinarray,
  1315. cachemode2pgprot(set_type), 1);
  1316. if (!ret && new_type == _PAGE_CACHE_MODE_WC)
  1317. ret = change_page_attr_set_clr(addr, addrinarray,
  1318. cachemode2pgprot(
  1319. _PAGE_CACHE_MODE_WC),
  1320. __pgprot(_PAGE_CACHE_MASK),
  1321. 0, CPA_ARRAY, NULL);
  1322. if (ret)
  1323. goto out_free;
  1324. return 0;
  1325. out_free:
  1326. for (j = 0; j < i; j++)
  1327. free_memtype(__pa(addr[j]), __pa(addr[j]) + PAGE_SIZE);
  1328. return ret;
  1329. }
  1330. int set_memory_array_uc(unsigned long *addr, int addrinarray)
  1331. {
  1332. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
  1333. }
  1334. EXPORT_SYMBOL(set_memory_array_uc);
  1335. int set_memory_array_wc(unsigned long *addr, int addrinarray)
  1336. {
  1337. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WC);
  1338. }
  1339. EXPORT_SYMBOL(set_memory_array_wc);
  1340. int set_memory_array_wt(unsigned long *addr, int addrinarray)
  1341. {
  1342. return _set_memory_array(addr, addrinarray, _PAGE_CACHE_MODE_WT);
  1343. }
  1344. EXPORT_SYMBOL_GPL(set_memory_array_wt);
  1345. int _set_memory_wc(unsigned long addr, int numpages)
  1346. {
  1347. int ret;
  1348. unsigned long addr_copy = addr;
  1349. ret = change_page_attr_set(&addr, numpages,
  1350. cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
  1351. 0);
  1352. if (!ret) {
  1353. ret = change_page_attr_set_clr(&addr_copy, numpages,
  1354. cachemode2pgprot(
  1355. _PAGE_CACHE_MODE_WC),
  1356. __pgprot(_PAGE_CACHE_MASK),
  1357. 0, 0, NULL);
  1358. }
  1359. return ret;
  1360. }
  1361. int set_memory_wc(unsigned long addr, int numpages)
  1362. {
  1363. int ret;
  1364. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1365. _PAGE_CACHE_MODE_WC, NULL);
  1366. if (ret)
  1367. return ret;
  1368. ret = _set_memory_wc(addr, numpages);
  1369. if (ret)
  1370. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1371. return ret;
  1372. }
  1373. EXPORT_SYMBOL(set_memory_wc);
  1374. int _set_memory_wt(unsigned long addr, int numpages)
  1375. {
  1376. return change_page_attr_set(&addr, numpages,
  1377. cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
  1378. }
  1379. int set_memory_wt(unsigned long addr, int numpages)
  1380. {
  1381. int ret;
  1382. ret = reserve_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
  1383. _PAGE_CACHE_MODE_WT, NULL);
  1384. if (ret)
  1385. return ret;
  1386. ret = _set_memory_wt(addr, numpages);
  1387. if (ret)
  1388. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1389. return ret;
  1390. }
  1391. EXPORT_SYMBOL_GPL(set_memory_wt);
  1392. int _set_memory_wb(unsigned long addr, int numpages)
  1393. {
  1394. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1395. return change_page_attr_clear(&addr, numpages,
  1396. __pgprot(_PAGE_CACHE_MASK), 0);
  1397. }
  1398. int set_memory_wb(unsigned long addr, int numpages)
  1399. {
  1400. int ret;
  1401. ret = _set_memory_wb(addr, numpages);
  1402. if (ret)
  1403. return ret;
  1404. free_memtype(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
  1405. return 0;
  1406. }
  1407. EXPORT_SYMBOL(set_memory_wb);
  1408. int set_memory_array_wb(unsigned long *addr, int addrinarray)
  1409. {
  1410. int i;
  1411. int ret;
  1412. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1413. ret = change_page_attr_clear(addr, addrinarray,
  1414. __pgprot(_PAGE_CACHE_MASK), 1);
  1415. if (ret)
  1416. return ret;
  1417. for (i = 0; i < addrinarray; i++)
  1418. free_memtype(__pa(addr[i]), __pa(addr[i]) + PAGE_SIZE);
  1419. return 0;
  1420. }
  1421. EXPORT_SYMBOL(set_memory_array_wb);
  1422. int set_memory_x(unsigned long addr, int numpages)
  1423. {
  1424. if (!(__supported_pte_mask & _PAGE_NX))
  1425. return 0;
  1426. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1427. }
  1428. EXPORT_SYMBOL(set_memory_x);
  1429. int set_memory_nx(unsigned long addr, int numpages)
  1430. {
  1431. if (!(__supported_pte_mask & _PAGE_NX))
  1432. return 0;
  1433. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
  1434. }
  1435. EXPORT_SYMBOL(set_memory_nx);
  1436. int set_memory_ro(unsigned long addr, int numpages)
  1437. {
  1438. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1439. }
  1440. int set_memory_rw(unsigned long addr, int numpages)
  1441. {
  1442. return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
  1443. }
  1444. int set_memory_np(unsigned long addr, int numpages)
  1445. {
  1446. return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
  1447. }
  1448. int set_memory_4k(unsigned long addr, int numpages)
  1449. {
  1450. return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
  1451. __pgprot(0), 1, 0, NULL);
  1452. }
  1453. int set_pages_uc(struct page *page, int numpages)
  1454. {
  1455. unsigned long addr = (unsigned long)page_address(page);
  1456. return set_memory_uc(addr, numpages);
  1457. }
  1458. EXPORT_SYMBOL(set_pages_uc);
  1459. static int _set_pages_array(struct page **pages, int addrinarray,
  1460. enum page_cache_mode new_type)
  1461. {
  1462. unsigned long start;
  1463. unsigned long end;
  1464. enum page_cache_mode set_type;
  1465. int i;
  1466. int free_idx;
  1467. int ret;
  1468. for (i = 0; i < addrinarray; i++) {
  1469. if (PageHighMem(pages[i]))
  1470. continue;
  1471. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1472. end = start + PAGE_SIZE;
  1473. if (reserve_memtype(start, end, new_type, NULL))
  1474. goto err_out;
  1475. }
  1476. /* If WC, set to UC- first and then WC */
  1477. set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
  1478. _PAGE_CACHE_MODE_UC_MINUS : new_type;
  1479. ret = cpa_set_pages_array(pages, addrinarray,
  1480. cachemode2pgprot(set_type));
  1481. if (!ret && new_type == _PAGE_CACHE_MODE_WC)
  1482. ret = change_page_attr_set_clr(NULL, addrinarray,
  1483. cachemode2pgprot(
  1484. _PAGE_CACHE_MODE_WC),
  1485. __pgprot(_PAGE_CACHE_MASK),
  1486. 0, CPA_PAGES_ARRAY, pages);
  1487. if (ret)
  1488. goto err_out;
  1489. return 0; /* Success */
  1490. err_out:
  1491. free_idx = i;
  1492. for (i = 0; i < free_idx; i++) {
  1493. if (PageHighMem(pages[i]))
  1494. continue;
  1495. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1496. end = start + PAGE_SIZE;
  1497. free_memtype(start, end);
  1498. }
  1499. return -EINVAL;
  1500. }
  1501. int set_pages_array_uc(struct page **pages, int addrinarray)
  1502. {
  1503. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_UC_MINUS);
  1504. }
  1505. EXPORT_SYMBOL(set_pages_array_uc);
  1506. int set_pages_array_wc(struct page **pages, int addrinarray)
  1507. {
  1508. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WC);
  1509. }
  1510. EXPORT_SYMBOL(set_pages_array_wc);
  1511. int set_pages_array_wt(struct page **pages, int addrinarray)
  1512. {
  1513. return _set_pages_array(pages, addrinarray, _PAGE_CACHE_MODE_WT);
  1514. }
  1515. EXPORT_SYMBOL_GPL(set_pages_array_wt);
  1516. int set_pages_wb(struct page *page, int numpages)
  1517. {
  1518. unsigned long addr = (unsigned long)page_address(page);
  1519. return set_memory_wb(addr, numpages);
  1520. }
  1521. EXPORT_SYMBOL(set_pages_wb);
  1522. int set_pages_array_wb(struct page **pages, int addrinarray)
  1523. {
  1524. int retval;
  1525. unsigned long start;
  1526. unsigned long end;
  1527. int i;
  1528. /* WB cache mode is hard wired to all cache attribute bits being 0 */
  1529. retval = cpa_clear_pages_array(pages, addrinarray,
  1530. __pgprot(_PAGE_CACHE_MASK));
  1531. if (retval)
  1532. return retval;
  1533. for (i = 0; i < addrinarray; i++) {
  1534. if (PageHighMem(pages[i]))
  1535. continue;
  1536. start = page_to_pfn(pages[i]) << PAGE_SHIFT;
  1537. end = start + PAGE_SIZE;
  1538. free_memtype(start, end);
  1539. }
  1540. return 0;
  1541. }
  1542. EXPORT_SYMBOL(set_pages_array_wb);
  1543. int set_pages_x(struct page *page, int numpages)
  1544. {
  1545. unsigned long addr = (unsigned long)page_address(page);
  1546. return set_memory_x(addr, numpages);
  1547. }
  1548. EXPORT_SYMBOL(set_pages_x);
  1549. int set_pages_nx(struct page *page, int numpages)
  1550. {
  1551. unsigned long addr = (unsigned long)page_address(page);
  1552. return set_memory_nx(addr, numpages);
  1553. }
  1554. EXPORT_SYMBOL(set_pages_nx);
  1555. int set_pages_ro(struct page *page, int numpages)
  1556. {
  1557. unsigned long addr = (unsigned long)page_address(page);
  1558. return set_memory_ro(addr, numpages);
  1559. }
  1560. int set_pages_rw(struct page *page, int numpages)
  1561. {
  1562. unsigned long addr = (unsigned long)page_address(page);
  1563. return set_memory_rw(addr, numpages);
  1564. }
  1565. #ifdef CONFIG_DEBUG_PAGEALLOC
  1566. static int __set_pages_p(struct page *page, int numpages)
  1567. {
  1568. unsigned long tempaddr = (unsigned long) page_address(page);
  1569. struct cpa_data cpa = { .vaddr = &tempaddr,
  1570. .pgd = NULL,
  1571. .numpages = numpages,
  1572. .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1573. .mask_clr = __pgprot(0),
  1574. .flags = 0};
  1575. /*
  1576. * No alias checking needed for setting present flag. otherwise,
  1577. * we may need to break large pages for 64-bit kernel text
  1578. * mappings (this adds to complexity if we want to do this from
  1579. * atomic context especially). Let's keep it simple!
  1580. */
  1581. return __change_page_attr_set_clr(&cpa, 0);
  1582. }
  1583. static int __set_pages_np(struct page *page, int numpages)
  1584. {
  1585. unsigned long tempaddr = (unsigned long) page_address(page);
  1586. struct cpa_data cpa = { .vaddr = &tempaddr,
  1587. .pgd = NULL,
  1588. .numpages = numpages,
  1589. .mask_set = __pgprot(0),
  1590. .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
  1591. .flags = 0};
  1592. /*
  1593. * No alias checking needed for setting not present flag. otherwise,
  1594. * we may need to break large pages for 64-bit kernel text
  1595. * mappings (this adds to complexity if we want to do this from
  1596. * atomic context especially). Let's keep it simple!
  1597. */
  1598. return __change_page_attr_set_clr(&cpa, 0);
  1599. }
  1600. void __kernel_map_pages(struct page *page, int numpages, int enable)
  1601. {
  1602. if (PageHighMem(page))
  1603. return;
  1604. if (!enable) {
  1605. debug_check_no_locks_freed(page_address(page),
  1606. numpages * PAGE_SIZE);
  1607. }
  1608. /*
  1609. * The return value is ignored as the calls cannot fail.
  1610. * Large pages for identity mappings are not used at boot time
  1611. * and hence no memory allocations during large page split.
  1612. */
  1613. if (enable)
  1614. __set_pages_p(page, numpages);
  1615. else
  1616. __set_pages_np(page, numpages);
  1617. /*
  1618. * We should perform an IPI and flush all tlbs,
  1619. * but that can deadlock->flush only current cpu:
  1620. */
  1621. __flush_tlb_all();
  1622. arch_flush_lazy_mmu_mode();
  1623. }
  1624. #ifdef CONFIG_HIBERNATION
  1625. bool kernel_page_present(struct page *page)
  1626. {
  1627. unsigned int level;
  1628. pte_t *pte;
  1629. if (PageHighMem(page))
  1630. return false;
  1631. pte = lookup_address((unsigned long)page_address(page), &level);
  1632. return (pte_val(*pte) & _PAGE_PRESENT);
  1633. }
  1634. #endif /* CONFIG_HIBERNATION */
  1635. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1636. int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
  1637. unsigned numpages, unsigned long page_flags)
  1638. {
  1639. int retval = -EINVAL;
  1640. struct cpa_data cpa = {
  1641. .vaddr = &address,
  1642. .pfn = pfn,
  1643. .pgd = pgd,
  1644. .numpages = numpages,
  1645. .mask_set = __pgprot(0),
  1646. .mask_clr = __pgprot(0),
  1647. .flags = 0,
  1648. };
  1649. if (!(__supported_pte_mask & _PAGE_NX))
  1650. goto out;
  1651. if (!(page_flags & _PAGE_NX))
  1652. cpa.mask_clr = __pgprot(_PAGE_NX);
  1653. if (!(page_flags & _PAGE_RW))
  1654. cpa.mask_clr = __pgprot(_PAGE_RW);
  1655. cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
  1656. retval = __change_page_attr_set_clr(&cpa, 0);
  1657. __flush_tlb_all();
  1658. out:
  1659. return retval;
  1660. }
  1661. /*
  1662. * The testcases use internal knowledge of the implementation that shouldn't
  1663. * be exposed to the rest of the kernel. Include these directly here.
  1664. */
  1665. #ifdef CONFIG_CPA_DEBUG
  1666. #include "pageattr-test.c"
  1667. #endif