pageattr.c 47 KB

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