Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2002 Andi Kleen, SuSE Labs.
4 * Thanks to Ben LaHaise for precious feedback.
5 */
6#include <linux/highmem.h>
7#include <linux/memblock.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/interrupt.h>
11#include <linux/seq_file.h>
12#include <linux/debugfs.h>
13#include <linux/pfn.h>
14#include <linux/percpu.h>
15#include <linux/gfp.h>
16#include <linux/pci.h>
17#include <linux/vmalloc.h>
18#include <linux/libnvdimm.h>
19
20#include <asm/e820/api.h>
21#include <asm/processor.h>
22#include <asm/tlbflush.h>
23#include <asm/sections.h>
24#include <asm/setup.h>
25#include <linux/uaccess.h>
26#include <asm/pgalloc.h>
27#include <asm/proto.h>
28#include <asm/memtype.h>
29#include <asm/set_memory.h>
30
31#include "../mm_internal.h"
32
33/*
34 * The current flushing context - we pass it instead of 5 arguments:
35 */
36struct cpa_data {
37 unsigned long *vaddr;
38 pgd_t *pgd;
39 pgprot_t mask_set;
40 pgprot_t mask_clr;
41 unsigned long numpages;
42 unsigned long curpage;
43 unsigned long pfn;
44 unsigned int flags;
45 unsigned int force_split : 1,
46 force_static_prot : 1,
47 force_flush_all : 1;
48 struct page **pages;
49};
50
51enum cpa_warn {
52 CPA_CONFLICT,
53 CPA_PROTECT,
54 CPA_DETECT,
55};
56
57static const int cpa_warn_level = CPA_PROTECT;
58
59/*
60 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
61 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
62 * entries change the page attribute in parallel to some other cpu
63 * splitting a large page entry along with changing the attribute.
64 */
65static DEFINE_SPINLOCK(cpa_lock);
66
67#define CPA_FLUSHTLB 1
68#define CPA_ARRAY 2
69#define CPA_PAGES_ARRAY 4
70#define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */
71
72static inline pgprot_t cachemode2pgprot(enum page_cache_mode pcm)
73{
74 return __pgprot(cachemode2protval(pcm));
75}
76
77#ifdef CONFIG_PROC_FS
78static unsigned long direct_pages_count[PG_LEVEL_NUM];
79
80void update_page_count(int level, unsigned long pages)
81{
82 /* Protect against CPA */
83 spin_lock(&pgd_lock);
84 direct_pages_count[level] += pages;
85 spin_unlock(&pgd_lock);
86}
87
88static void split_page_count(int level)
89{
90 if (direct_pages_count[level] == 0)
91 return;
92
93 direct_pages_count[level]--;
94 direct_pages_count[level - 1] += PTRS_PER_PTE;
95}
96
97void arch_report_meminfo(struct seq_file *m)
98{
99 seq_printf(m, "DirectMap4k: %8lu kB\n",
100 direct_pages_count[PG_LEVEL_4K] << 2);
101#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
102 seq_printf(m, "DirectMap2M: %8lu kB\n",
103 direct_pages_count[PG_LEVEL_2M] << 11);
104#else
105 seq_printf(m, "DirectMap4M: %8lu kB\n",
106 direct_pages_count[PG_LEVEL_2M] << 12);
107#endif
108 if (direct_gbpages)
109 seq_printf(m, "DirectMap1G: %8lu kB\n",
110 direct_pages_count[PG_LEVEL_1G] << 20);
111}
112#else
113static inline void split_page_count(int level) { }
114#endif
115
116#ifdef CONFIG_X86_CPA_STATISTICS
117
118static unsigned long cpa_1g_checked;
119static unsigned long cpa_1g_sameprot;
120static unsigned long cpa_1g_preserved;
121static unsigned long cpa_2m_checked;
122static unsigned long cpa_2m_sameprot;
123static unsigned long cpa_2m_preserved;
124static unsigned long cpa_4k_install;
125
126static inline void cpa_inc_1g_checked(void)
127{
128 cpa_1g_checked++;
129}
130
131static inline void cpa_inc_2m_checked(void)
132{
133 cpa_2m_checked++;
134}
135
136static inline void cpa_inc_4k_install(void)
137{
138 data_race(cpa_4k_install++);
139}
140
141static inline void cpa_inc_lp_sameprot(int level)
142{
143 if (level == PG_LEVEL_1G)
144 cpa_1g_sameprot++;
145 else
146 cpa_2m_sameprot++;
147}
148
149static inline void cpa_inc_lp_preserved(int level)
150{
151 if (level == PG_LEVEL_1G)
152 cpa_1g_preserved++;
153 else
154 cpa_2m_preserved++;
155}
156
157static int cpastats_show(struct seq_file *m, void *p)
158{
159 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked);
160 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot);
161 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved);
162 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked);
163 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot);
164 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved);
165 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install);
166 return 0;
167}
168
169static int cpastats_open(struct inode *inode, struct file *file)
170{
171 return single_open(file, cpastats_show, NULL);
172}
173
174static const struct file_operations cpastats_fops = {
175 .open = cpastats_open,
176 .read = seq_read,
177 .llseek = seq_lseek,
178 .release = single_release,
179};
180
181static int __init cpa_stats_init(void)
182{
183 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL,
184 &cpastats_fops);
185 return 0;
186}
187late_initcall(cpa_stats_init);
188#else
189static inline void cpa_inc_1g_checked(void) { }
190static inline void cpa_inc_2m_checked(void) { }
191static inline void cpa_inc_4k_install(void) { }
192static inline void cpa_inc_lp_sameprot(int level) { }
193static inline void cpa_inc_lp_preserved(int level) { }
194#endif
195
196
197static inline int
198within(unsigned long addr, unsigned long start, unsigned long end)
199{
200 return addr >= start && addr < end;
201}
202
203static inline int
204within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
205{
206 return addr >= start && addr <= end;
207}
208
209#ifdef CONFIG_X86_64
210
211static inline unsigned long highmap_start_pfn(void)
212{
213 return __pa_symbol(_text) >> PAGE_SHIFT;
214}
215
216static inline unsigned long highmap_end_pfn(void)
217{
218 /* Do not reference physical address outside the kernel. */
219 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
220}
221
222static bool __cpa_pfn_in_highmap(unsigned long pfn)
223{
224 /*
225 * Kernel text has an alias mapping at a high address, known
226 * here as "highmap".
227 */
228 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
229}
230
231#else
232
233static bool __cpa_pfn_in_highmap(unsigned long pfn)
234{
235 /* There is no highmap on 32-bit */
236 return false;
237}
238
239#endif
240
241/*
242 * See set_mce_nospec().
243 *
244 * Machine check recovery code needs to change cache mode of poisoned pages to
245 * UC to avoid speculative access logging another error. But passing the
246 * address of the 1:1 mapping to set_memory_uc() is a fine way to encourage a
247 * speculative access. So we cheat and flip the top bit of the address. This
248 * works fine for the code that updates the page tables. But at the end of the
249 * process we need to flush the TLB and cache and the non-canonical address
250 * causes a #GP fault when used by the INVLPG and CLFLUSH instructions.
251 *
252 * But in the common case we already have a canonical address. This code
253 * will fix the top bit if needed and is a no-op otherwise.
254 */
255static inline unsigned long fix_addr(unsigned long addr)
256{
257#ifdef CONFIG_X86_64
258 return (long)(addr << 1) >> 1;
259#else
260 return addr;
261#endif
262}
263
264static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx)
265{
266 if (cpa->flags & CPA_PAGES_ARRAY) {
267 struct page *page = cpa->pages[idx];
268
269 if (unlikely(PageHighMem(page)))
270 return 0;
271
272 return (unsigned long)page_address(page);
273 }
274
275 if (cpa->flags & CPA_ARRAY)
276 return cpa->vaddr[idx];
277
278 return *cpa->vaddr + idx * PAGE_SIZE;
279}
280
281/*
282 * Flushing functions
283 */
284
285static void clflush_cache_range_opt(void *vaddr, unsigned int size)
286{
287 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
288 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
289 void *vend = vaddr + size;
290
291 if (p >= vend)
292 return;
293
294 for (; p < vend; p += clflush_size)
295 clflushopt(p);
296}
297
298/**
299 * clflush_cache_range - flush a cache range with clflush
300 * @vaddr: virtual start address
301 * @size: number of bytes to flush
302 *
303 * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or
304 * SFENCE to avoid ordering issues.
305 */
306void clflush_cache_range(void *vaddr, unsigned int size)
307{
308 mb();
309 clflush_cache_range_opt(vaddr, size);
310 mb();
311}
312EXPORT_SYMBOL_GPL(clflush_cache_range);
313
314#ifdef CONFIG_ARCH_HAS_PMEM_API
315void arch_invalidate_pmem(void *addr, size_t size)
316{
317 clflush_cache_range(addr, size);
318}
319EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
320#endif
321
322static void __cpa_flush_all(void *arg)
323{
324 unsigned long cache = (unsigned long)arg;
325
326 /*
327 * Flush all to work around Errata in early athlons regarding
328 * large page flushing.
329 */
330 __flush_tlb_all();
331
332 if (cache && boot_cpu_data.x86 >= 4)
333 wbinvd();
334}
335
336static void cpa_flush_all(unsigned long cache)
337{
338 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
339
340 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
341}
342
343static void __cpa_flush_tlb(void *data)
344{
345 struct cpa_data *cpa = data;
346 unsigned int i;
347
348 for (i = 0; i < cpa->numpages; i++)
349 flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i)));
350}
351
352static void cpa_flush(struct cpa_data *data, int cache)
353{
354 struct cpa_data *cpa = data;
355 unsigned int i;
356
357 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
358
359 if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
360 cpa_flush_all(cache);
361 return;
362 }
363
364 if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling)
365 flush_tlb_all();
366 else
367 on_each_cpu(__cpa_flush_tlb, cpa, 1);
368
369 if (!cache)
370 return;
371
372 mb();
373 for (i = 0; i < cpa->numpages; i++) {
374 unsigned long addr = __cpa_addr(cpa, i);
375 unsigned int level;
376
377 pte_t *pte = lookup_address(addr, &level);
378
379 /*
380 * Only flush present addresses:
381 */
382 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
383 clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE);
384 }
385 mb();
386}
387
388static bool overlaps(unsigned long r1_start, unsigned long r1_end,
389 unsigned long r2_start, unsigned long r2_end)
390{
391 return (r1_start <= r2_end && r1_end >= r2_start) ||
392 (r2_start <= r1_end && r2_end >= r1_start);
393}
394
395#ifdef CONFIG_PCI_BIOS
396/*
397 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS
398 * based config access (CONFIG_PCI_GOBIOS) support.
399 */
400#define BIOS_PFN PFN_DOWN(BIOS_BEGIN)
401#define BIOS_PFN_END PFN_DOWN(BIOS_END - 1)
402
403static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
404{
405 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END))
406 return _PAGE_NX;
407 return 0;
408}
409#else
410static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
411{
412 return 0;
413}
414#endif
415
416/*
417 * The .rodata section needs to be read-only. Using the pfn catches all
418 * aliases. This also includes __ro_after_init, so do not enforce until
419 * kernel_set_to_readonly is true.
420 */
421static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn)
422{
423 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata));
424
425 /*
426 * Note: __end_rodata is at page aligned and not inclusive, so
427 * subtract 1 to get the last enforced PFN in the rodata area.
428 */
429 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1;
430
431 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro))
432 return _PAGE_RW;
433 return 0;
434}
435
436/*
437 * Protect kernel text against becoming non executable by forbidding
438 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext)
439 * out of which the kernel actually executes. Do not protect the low
440 * mapping.
441 *
442 * This does not cover __inittext since that is gone after boot.
443 */
444static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end)
445{
446 unsigned long t_end = (unsigned long)_etext - 1;
447 unsigned long t_start = (unsigned long)_text;
448
449 if (overlaps(start, end, t_start, t_end))
450 return _PAGE_NX;
451 return 0;
452}
453
454#if defined(CONFIG_X86_64)
455/*
456 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
457 * kernel text mappings for the large page aligned text, rodata sections
458 * will be always read-only. For the kernel identity mappings covering the
459 * holes caused by this alignment can be anything that user asks.
460 *
461 * This will preserve the large page mappings for kernel text/data at no
462 * extra cost.
463 */
464static pgprotval_t protect_kernel_text_ro(unsigned long start,
465 unsigned long end)
466{
467 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1;
468 unsigned long t_start = (unsigned long)_text;
469 unsigned int level;
470
471 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end))
472 return 0;
473 /*
474 * Don't enforce the !RW mapping for the kernel text mapping, if
475 * the current mapping is already using small page mapping. No
476 * need to work hard to preserve large page mappings in this case.
477 *
478 * This also fixes the Linux Xen paravirt guest boot failure caused
479 * by unexpected read-only mappings for kernel identity
480 * mappings. In this paravirt guest case, the kernel text mapping
481 * and the kernel identity mapping share the same page-table pages,
482 * so the protections for kernel text and identity mappings have to
483 * be the same.
484 */
485 if (lookup_address(start, &level) && (level != PG_LEVEL_4K))
486 return _PAGE_RW;
487 return 0;
488}
489#else
490static pgprotval_t protect_kernel_text_ro(unsigned long start,
491 unsigned long end)
492{
493 return 0;
494}
495#endif
496
497static inline bool conflicts(pgprot_t prot, pgprotval_t val)
498{
499 return (pgprot_val(prot) & ~val) != pgprot_val(prot);
500}
501
502static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
503 unsigned long start, unsigned long end,
504 unsigned long pfn, const char *txt)
505{
506 static const char *lvltxt[] = {
507 [CPA_CONFLICT] = "conflict",
508 [CPA_PROTECT] = "protect",
509 [CPA_DETECT] = "detect",
510 };
511
512 if (warnlvl > cpa_warn_level || !conflicts(prot, val))
513 return;
514
515 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
516 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
517 (unsigned long long)val);
518}
519
520/*
521 * Certain areas of memory on x86 require very specific protection flags,
522 * for example the BIOS area or kernel text. Callers don't always get this
523 * right (again, ioremap() on BIOS memory is not uncommon) so this function
524 * checks and fixes these known static required protection bits.
525 */
526static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
527 unsigned long pfn, unsigned long npg,
528 unsigned long lpsize, int warnlvl)
529{
530 pgprotval_t forbidden, res;
531 unsigned long end;
532
533 /*
534 * There is no point in checking RW/NX conflicts when the requested
535 * mapping is setting the page !PRESENT.
536 */
537 if (!(pgprot_val(prot) & _PAGE_PRESENT))
538 return prot;
539
540 /* Operate on the virtual address */
541 end = start + npg * PAGE_SIZE - 1;
542
543 res = protect_kernel_text(start, end);
544 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX");
545 forbidden = res;
546
547 /*
548 * Special case to preserve a large page. If the change spawns the
549 * full large page mapping then there is no point to split it
550 * up. Happens with ftrace and is going to be removed once ftrace
551 * switched to text_poke().
552 */
553 if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) {
554 res = protect_kernel_text_ro(start, end);
555 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO");
556 forbidden |= res;
557 }
558
559 /* Check the PFN directly */
560 res = protect_pci_bios(pfn, pfn + npg - 1);
561 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX");
562 forbidden |= res;
563
564 res = protect_rodata(pfn, pfn + npg - 1);
565 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO");
566 forbidden |= res;
567
568 return __pgprot(pgprot_val(prot) & ~forbidden);
569}
570
571/*
572 * Lookup the page table entry for a virtual address in a specific pgd.
573 * Return a pointer to the entry and the level of the mapping.
574 */
575pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
576 unsigned int *level)
577{
578 p4d_t *p4d;
579 pud_t *pud;
580 pmd_t *pmd;
581
582 *level = PG_LEVEL_NONE;
583
584 if (pgd_none(*pgd))
585 return NULL;
586
587 p4d = p4d_offset(pgd, address);
588 if (p4d_none(*p4d))
589 return NULL;
590
591 *level = PG_LEVEL_512G;
592 if (p4d_large(*p4d) || !p4d_present(*p4d))
593 return (pte_t *)p4d;
594
595 pud = pud_offset(p4d, address);
596 if (pud_none(*pud))
597 return NULL;
598
599 *level = PG_LEVEL_1G;
600 if (pud_large(*pud) || !pud_present(*pud))
601 return (pte_t *)pud;
602
603 pmd = pmd_offset(pud, address);
604 if (pmd_none(*pmd))
605 return NULL;
606
607 *level = PG_LEVEL_2M;
608 if (pmd_large(*pmd) || !pmd_present(*pmd))
609 return (pte_t *)pmd;
610
611 *level = PG_LEVEL_4K;
612
613 return pte_offset_kernel(pmd, address);
614}
615
616/*
617 * Lookup the page table entry for a virtual address. Return a pointer
618 * to the entry and the level of the mapping.
619 *
620 * Note: We return pud and pmd either when the entry is marked large
621 * or when the present bit is not set. Otherwise we would return a
622 * pointer to a nonexisting mapping.
623 */
624pte_t *lookup_address(unsigned long address, unsigned int *level)
625{
626 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
627}
628EXPORT_SYMBOL_GPL(lookup_address);
629
630/*
631 * Lookup the page table entry for a virtual address in a given mm. Return a
632 * pointer to the entry and the level of the mapping.
633 */
634pte_t *lookup_address_in_mm(struct mm_struct *mm, unsigned long address,
635 unsigned int *level)
636{
637 return lookup_address_in_pgd(pgd_offset(mm, address), address, level);
638}
639EXPORT_SYMBOL_GPL(lookup_address_in_mm);
640
641static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
642 unsigned int *level)
643{
644 if (cpa->pgd)
645 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
646 address, level);
647
648 return lookup_address(address, level);
649}
650
651/*
652 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
653 * or NULL if not present.
654 */
655pmd_t *lookup_pmd_address(unsigned long address)
656{
657 pgd_t *pgd;
658 p4d_t *p4d;
659 pud_t *pud;
660
661 pgd = pgd_offset_k(address);
662 if (pgd_none(*pgd))
663 return NULL;
664
665 p4d = p4d_offset(pgd, address);
666 if (p4d_none(*p4d) || p4d_large(*p4d) || !p4d_present(*p4d))
667 return NULL;
668
669 pud = pud_offset(p4d, address);
670 if (pud_none(*pud) || pud_large(*pud) || !pud_present(*pud))
671 return NULL;
672
673 return pmd_offset(pud, address);
674}
675
676/*
677 * This is necessary because __pa() does not work on some
678 * kinds of memory, like vmalloc() or the alloc_remap()
679 * areas on 32-bit NUMA systems. The percpu areas can
680 * end up in this kind of memory, for instance.
681 *
682 * This could be optimized, but it is only intended to be
683 * used at inititalization time, and keeping it
684 * unoptimized should increase the testing coverage for
685 * the more obscure platforms.
686 */
687phys_addr_t slow_virt_to_phys(void *__virt_addr)
688{
689 unsigned long virt_addr = (unsigned long)__virt_addr;
690 phys_addr_t phys_addr;
691 unsigned long offset;
692 enum pg_level level;
693 pte_t *pte;
694
695 pte = lookup_address(virt_addr, &level);
696 BUG_ON(!pte);
697
698 /*
699 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
700 * before being left-shifted PAGE_SHIFT bits -- this trick is to
701 * make 32-PAE kernel work correctly.
702 */
703 switch (level) {
704 case PG_LEVEL_1G:
705 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
706 offset = virt_addr & ~PUD_PAGE_MASK;
707 break;
708 case PG_LEVEL_2M:
709 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
710 offset = virt_addr & ~PMD_PAGE_MASK;
711 break;
712 default:
713 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
714 offset = virt_addr & ~PAGE_MASK;
715 }
716
717 return (phys_addr_t)(phys_addr | offset);
718}
719EXPORT_SYMBOL_GPL(slow_virt_to_phys);
720
721/*
722 * Set the new pmd in all the pgds we know about:
723 */
724static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
725{
726 /* change init_mm */
727 set_pte_atomic(kpte, pte);
728#ifdef CONFIG_X86_32
729 if (!SHARED_KERNEL_PMD) {
730 struct page *page;
731
732 list_for_each_entry(page, &pgd_list, lru) {
733 pgd_t *pgd;
734 p4d_t *p4d;
735 pud_t *pud;
736 pmd_t *pmd;
737
738 pgd = (pgd_t *)page_address(page) + pgd_index(address);
739 p4d = p4d_offset(pgd, address);
740 pud = pud_offset(p4d, address);
741 pmd = pmd_offset(pud, address);
742 set_pte_atomic((pte_t *)pmd, pte);
743 }
744 }
745#endif
746}
747
748static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
749{
750 /*
751 * _PAGE_GLOBAL means "global page" for present PTEs.
752 * But, it is also used to indicate _PAGE_PROTNONE
753 * for non-present PTEs.
754 *
755 * This ensures that a _PAGE_GLOBAL PTE going from
756 * present to non-present is not confused as
757 * _PAGE_PROTNONE.
758 */
759 if (!(pgprot_val(prot) & _PAGE_PRESENT))
760 pgprot_val(prot) &= ~_PAGE_GLOBAL;
761
762 return prot;
763}
764
765static int __should_split_large_page(pte_t *kpte, unsigned long address,
766 struct cpa_data *cpa)
767{
768 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
769 pgprot_t old_prot, new_prot, req_prot, chk_prot;
770 pte_t new_pte, *tmp;
771 enum pg_level level;
772
773 /*
774 * Check for races, another CPU might have split this page
775 * up already:
776 */
777 tmp = _lookup_address_cpa(cpa, address, &level);
778 if (tmp != kpte)
779 return 1;
780
781 switch (level) {
782 case PG_LEVEL_2M:
783 old_prot = pmd_pgprot(*(pmd_t *)kpte);
784 old_pfn = pmd_pfn(*(pmd_t *)kpte);
785 cpa_inc_2m_checked();
786 break;
787 case PG_LEVEL_1G:
788 old_prot = pud_pgprot(*(pud_t *)kpte);
789 old_pfn = pud_pfn(*(pud_t *)kpte);
790 cpa_inc_1g_checked();
791 break;
792 default:
793 return -EINVAL;
794 }
795
796 psize = page_level_size(level);
797 pmask = page_level_mask(level);
798
799 /*
800 * Calculate the number of pages, which fit into this large
801 * page starting at address:
802 */
803 lpaddr = (address + psize) & pmask;
804 numpages = (lpaddr - address) >> PAGE_SHIFT;
805 if (numpages < cpa->numpages)
806 cpa->numpages = numpages;
807
808 /*
809 * We are safe now. Check whether the new pgprot is the same:
810 * Convert protection attributes to 4k-format, as cpa->mask* are set
811 * up accordingly.
812 */
813
814 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */
815 req_prot = pgprot_large_2_4k(old_prot);
816
817 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
818 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
819
820 /*
821 * req_prot is in format of 4k pages. It must be converted to large
822 * page format: the caching mode includes the PAT bit located at
823 * different bit positions in the two formats.
824 */
825 req_prot = pgprot_4k_2_large(req_prot);
826 req_prot = pgprot_clear_protnone_bits(req_prot);
827 if (pgprot_val(req_prot) & _PAGE_PRESENT)
828 pgprot_val(req_prot) |= _PAGE_PSE;
829
830 /*
831 * old_pfn points to the large page base pfn. So we need to add the
832 * offset of the virtual address:
833 */
834 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
835 cpa->pfn = pfn;
836
837 /*
838 * Calculate the large page base address and the number of 4K pages
839 * in the large page
840 */
841 lpaddr = address & pmask;
842 numpages = psize >> PAGE_SHIFT;
843
844 /*
845 * Sanity check that the existing mapping is correct versus the static
846 * protections. static_protections() guards against !PRESENT, so no
847 * extra conditional required here.
848 */
849 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages,
850 psize, CPA_CONFLICT);
851
852 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) {
853 /*
854 * Split the large page and tell the split code to
855 * enforce static protections.
856 */
857 cpa->force_static_prot = 1;
858 return 1;
859 }
860
861 /*
862 * Optimization: If the requested pgprot is the same as the current
863 * pgprot, then the large page can be preserved and no updates are
864 * required independent of alignment and length of the requested
865 * range. The above already established that the current pgprot is
866 * correct, which in consequence makes the requested pgprot correct
867 * as well if it is the same. The static protection scan below will
868 * not come to a different conclusion.
869 */
870 if (pgprot_val(req_prot) == pgprot_val(old_prot)) {
871 cpa_inc_lp_sameprot(level);
872 return 0;
873 }
874
875 /*
876 * If the requested range does not cover the full page, split it up
877 */
878 if (address != lpaddr || cpa->numpages != numpages)
879 return 1;
880
881 /*
882 * Check whether the requested pgprot is conflicting with a static
883 * protection requirement in the large page.
884 */
885 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
886 psize, CPA_DETECT);
887
888 /*
889 * If there is a conflict, split the large page.
890 *
891 * There used to be a 4k wise evaluation trying really hard to
892 * preserve the large pages, but experimentation has shown, that this
893 * does not help at all. There might be corner cases which would
894 * preserve one large page occasionally, but it's really not worth the
895 * extra code and cycles for the common case.
896 */
897 if (pgprot_val(req_prot) != pgprot_val(new_prot))
898 return 1;
899
900 /* All checks passed. Update the large page mapping. */
901 new_pte = pfn_pte(old_pfn, new_prot);
902 __set_pmd_pte(kpte, address, new_pte);
903 cpa->flags |= CPA_FLUSHTLB;
904 cpa_inc_lp_preserved(level);
905 return 0;
906}
907
908static int should_split_large_page(pte_t *kpte, unsigned long address,
909 struct cpa_data *cpa)
910{
911 int do_split;
912
913 if (cpa->force_split)
914 return 1;
915
916 spin_lock(&pgd_lock);
917 do_split = __should_split_large_page(kpte, address, cpa);
918 spin_unlock(&pgd_lock);
919
920 return do_split;
921}
922
923static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,
924 pgprot_t ref_prot, unsigned long address,
925 unsigned long size)
926{
927 unsigned int npg = PFN_DOWN(size);
928 pgprot_t prot;
929
930 /*
931 * If should_split_large_page() discovered an inconsistent mapping,
932 * remove the invalid protection in the split mapping.
933 */
934 if (!cpa->force_static_prot)
935 goto set;
936
937 /* Hand in lpsize = 0 to enforce the protection mechanism */
938 prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT);
939
940 if (pgprot_val(prot) == pgprot_val(ref_prot))
941 goto set;
942
943 /*
944 * If this is splitting a PMD, fix it up. PUD splits cannot be
945 * fixed trivially as that would require to rescan the newly
946 * installed PMD mappings after returning from split_large_page()
947 * so an eventual further split can allocate the necessary PTE
948 * pages. Warn for now and revisit it in case this actually
949 * happens.
950 */
951 if (size == PAGE_SIZE)
952 ref_prot = prot;
953 else
954 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n");
955set:
956 set_pte(pte, pfn_pte(pfn, ref_prot));
957}
958
959static int
960__split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
961 struct page *base)
962{
963 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
964 pte_t *pbase = (pte_t *)page_address(base);
965 unsigned int i, level;
966 pgprot_t ref_prot;
967 pte_t *tmp;
968
969 spin_lock(&pgd_lock);
970 /*
971 * Check for races, another CPU might have split this page
972 * up for us already:
973 */
974 tmp = _lookup_address_cpa(cpa, address, &level);
975 if (tmp != kpte) {
976 spin_unlock(&pgd_lock);
977 return 1;
978 }
979
980 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
981
982 switch (level) {
983 case PG_LEVEL_2M:
984 ref_prot = pmd_pgprot(*(pmd_t *)kpte);
985 /*
986 * Clear PSE (aka _PAGE_PAT) and move
987 * PAT bit to correct position.
988 */
989 ref_prot = pgprot_large_2_4k(ref_prot);
990 ref_pfn = pmd_pfn(*(pmd_t *)kpte);
991 lpaddr = address & PMD_MASK;
992 lpinc = PAGE_SIZE;
993 break;
994
995 case PG_LEVEL_1G:
996 ref_prot = pud_pgprot(*(pud_t *)kpte);
997 ref_pfn = pud_pfn(*(pud_t *)kpte);
998 pfninc = PMD_PAGE_SIZE >> PAGE_SHIFT;
999 lpaddr = address & PUD_MASK;
1000 lpinc = PMD_SIZE;
1001 /*
1002 * Clear the PSE flags if the PRESENT flag is not set
1003 * otherwise pmd_present/pmd_huge will return true
1004 * even on a non present pmd.
1005 */
1006 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
1007 pgprot_val(ref_prot) &= ~_PAGE_PSE;
1008 break;
1009
1010 default:
1011 spin_unlock(&pgd_lock);
1012 return 1;
1013 }
1014
1015 ref_prot = pgprot_clear_protnone_bits(ref_prot);
1016
1017 /*
1018 * Get the target pfn from the original entry:
1019 */
1020 pfn = ref_pfn;
1021 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
1022 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);
1023
1024 if (virt_addr_valid(address)) {
1025 unsigned long pfn = PFN_DOWN(__pa(address));
1026
1027 if (pfn_range_is_mapped(pfn, pfn + 1))
1028 split_page_count(level);
1029 }
1030
1031 /*
1032 * Install the new, split up pagetable.
1033 *
1034 * We use the standard kernel pagetable protections for the new
1035 * pagetable protections, the actual ptes set above control the
1036 * primary protection behavior:
1037 */
1038 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
1039
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 * Without 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 spin_unlock(&pgd_lock);
1060
1061 return 0;
1062}
1063
1064static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
1065 unsigned long address)
1066{
1067 struct page *base;
1068
1069 if (!debug_pagealloc_enabled())
1070 spin_unlock(&cpa_lock);
1071 base = alloc_pages(GFP_KERNEL, 0);
1072 if (!debug_pagealloc_enabled())
1073 spin_lock(&cpa_lock);
1074 if (!base)
1075 return -ENOMEM;
1076
1077 if (__split_large_page(cpa, kpte, address, base))
1078 __free_page(base);
1079
1080 return 0;
1081}
1082
1083static bool try_to_free_pte_page(pte_t *pte)
1084{
1085 int i;
1086
1087 for (i = 0; i < PTRS_PER_PTE; i++)
1088 if (!pte_none(pte[i]))
1089 return false;
1090
1091 free_page((unsigned long)pte);
1092 return true;
1093}
1094
1095static bool try_to_free_pmd_page(pmd_t *pmd)
1096{
1097 int i;
1098
1099 for (i = 0; i < PTRS_PER_PMD; i++)
1100 if (!pmd_none(pmd[i]))
1101 return false;
1102
1103 free_page((unsigned long)pmd);
1104 return true;
1105}
1106
1107static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
1108{
1109 pte_t *pte = pte_offset_kernel(pmd, start);
1110
1111 while (start < end) {
1112 set_pte(pte, __pte(0));
1113
1114 start += PAGE_SIZE;
1115 pte++;
1116 }
1117
1118 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
1119 pmd_clear(pmd);
1120 return true;
1121 }
1122 return false;
1123}
1124
1125static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
1126 unsigned long start, unsigned long end)
1127{
1128 if (unmap_pte_range(pmd, start, end))
1129 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1130 pud_clear(pud);
1131}
1132
1133static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
1134{
1135 pmd_t *pmd = pmd_offset(pud, start);
1136
1137 /*
1138 * Not on a 2MB page boundary?
1139 */
1140 if (start & (PMD_SIZE - 1)) {
1141 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1142 unsigned long pre_end = min_t(unsigned long, end, next_page);
1143
1144 __unmap_pmd_range(pud, pmd, start, pre_end);
1145
1146 start = pre_end;
1147 pmd++;
1148 }
1149
1150 /*
1151 * Try to unmap in 2M chunks.
1152 */
1153 while (end - start >= PMD_SIZE) {
1154 if (pmd_large(*pmd))
1155 pmd_clear(pmd);
1156 else
1157 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
1158
1159 start += PMD_SIZE;
1160 pmd++;
1161 }
1162
1163 /*
1164 * 4K leftovers?
1165 */
1166 if (start < end)
1167 return __unmap_pmd_range(pud, pmd, start, end);
1168
1169 /*
1170 * Try again to free the PMD page if haven't succeeded above.
1171 */
1172 if (!pud_none(*pud))
1173 if (try_to_free_pmd_page((pmd_t *)pud_page_vaddr(*pud)))
1174 pud_clear(pud);
1175}
1176
1177static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end)
1178{
1179 pud_t *pud = pud_offset(p4d, start);
1180
1181 /*
1182 * Not on a GB page boundary?
1183 */
1184 if (start & (PUD_SIZE - 1)) {
1185 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1186 unsigned long pre_end = min_t(unsigned long, end, next_page);
1187
1188 unmap_pmd_range(pud, start, pre_end);
1189
1190 start = pre_end;
1191 pud++;
1192 }
1193
1194 /*
1195 * Try to unmap in 1G chunks?
1196 */
1197 while (end - start >= PUD_SIZE) {
1198
1199 if (pud_large(*pud))
1200 pud_clear(pud);
1201 else
1202 unmap_pmd_range(pud, start, start + PUD_SIZE);
1203
1204 start += PUD_SIZE;
1205 pud++;
1206 }
1207
1208 /*
1209 * 2M leftovers?
1210 */
1211 if (start < end)
1212 unmap_pmd_range(pud, start, end);
1213
1214 /*
1215 * No need to try to free the PUD page because we'll free it in
1216 * populate_pgd's error path
1217 */
1218}
1219
1220static int alloc_pte_page(pmd_t *pmd)
1221{
1222 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
1223 if (!pte)
1224 return -1;
1225
1226 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
1227 return 0;
1228}
1229
1230static int alloc_pmd_page(pud_t *pud)
1231{
1232 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
1233 if (!pmd)
1234 return -1;
1235
1236 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
1237 return 0;
1238}
1239
1240static void populate_pte(struct cpa_data *cpa,
1241 unsigned long start, unsigned long end,
1242 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
1243{
1244 pte_t *pte;
1245
1246 pte = pte_offset_kernel(pmd, start);
1247
1248 pgprot = pgprot_clear_protnone_bits(pgprot);
1249
1250 while (num_pages-- && start < end) {
1251 set_pte(pte, pfn_pte(cpa->pfn, pgprot));
1252
1253 start += PAGE_SIZE;
1254 cpa->pfn++;
1255 pte++;
1256 }
1257}
1258
1259static long populate_pmd(struct cpa_data *cpa,
1260 unsigned long start, unsigned long end,
1261 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
1262{
1263 long cur_pages = 0;
1264 pmd_t *pmd;
1265 pgprot_t pmd_pgprot;
1266
1267 /*
1268 * Not on a 2M boundary?
1269 */
1270 if (start & (PMD_SIZE - 1)) {
1271 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
1272 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1273
1274 pre_end = min_t(unsigned long, pre_end, next_page);
1275 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1276 cur_pages = min_t(unsigned int, num_pages, cur_pages);
1277
1278 /*
1279 * Need a PTE page?
1280 */
1281 pmd = pmd_offset(pud, start);
1282 if (pmd_none(*pmd))
1283 if (alloc_pte_page(pmd))
1284 return -1;
1285
1286 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
1287
1288 start = pre_end;
1289 }
1290
1291 /*
1292 * We mapped them all?
1293 */
1294 if (num_pages == cur_pages)
1295 return cur_pages;
1296
1297 pmd_pgprot = pgprot_4k_2_large(pgprot);
1298
1299 while (end - start >= PMD_SIZE) {
1300
1301 /*
1302 * We cannot use a 1G page so allocate a PMD page if needed.
1303 */
1304 if (pud_none(*pud))
1305 if (alloc_pmd_page(pud))
1306 return -1;
1307
1308 pmd = pmd_offset(pud, start);
1309
1310 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
1311 canon_pgprot(pmd_pgprot))));
1312
1313 start += PMD_SIZE;
1314 cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
1315 cur_pages += PMD_SIZE >> PAGE_SHIFT;
1316 }
1317
1318 /*
1319 * Map trailing 4K pages.
1320 */
1321 if (start < end) {
1322 pmd = pmd_offset(pud, start);
1323 if (pmd_none(*pmd))
1324 if (alloc_pte_page(pmd))
1325 return -1;
1326
1327 populate_pte(cpa, start, end, num_pages - cur_pages,
1328 pmd, pgprot);
1329 }
1330 return num_pages;
1331}
1332
1333static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
1334 pgprot_t pgprot)
1335{
1336 pud_t *pud;
1337 unsigned long end;
1338 long cur_pages = 0;
1339 pgprot_t pud_pgprot;
1340
1341 end = start + (cpa->numpages << PAGE_SHIFT);
1342
1343 /*
1344 * Not on a Gb page boundary? => map everything up to it with
1345 * smaller pages.
1346 */
1347 if (start & (PUD_SIZE - 1)) {
1348 unsigned long pre_end;
1349 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1350
1351 pre_end = min_t(unsigned long, end, next_page);
1352 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1353 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
1354
1355 pud = pud_offset(p4d, start);
1356
1357 /*
1358 * Need a PMD page?
1359 */
1360 if (pud_none(*pud))
1361 if (alloc_pmd_page(pud))
1362 return -1;
1363
1364 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1365 pud, pgprot);
1366 if (cur_pages < 0)
1367 return cur_pages;
1368
1369 start = pre_end;
1370 }
1371
1372 /* We mapped them all? */
1373 if (cpa->numpages == cur_pages)
1374 return cur_pages;
1375
1376 pud = pud_offset(p4d, start);
1377 pud_pgprot = pgprot_4k_2_large(pgprot);
1378
1379 /*
1380 * Map everything starting from the Gb boundary, possibly with 1G pages
1381 */
1382 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
1383 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
1384 canon_pgprot(pud_pgprot))));
1385
1386 start += PUD_SIZE;
1387 cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
1388 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1389 pud++;
1390 }
1391
1392 /* Map trailing leftover */
1393 if (start < end) {
1394 long tmp;
1395
1396 pud = pud_offset(p4d, start);
1397 if (pud_none(*pud))
1398 if (alloc_pmd_page(pud))
1399 return -1;
1400
1401 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1402 pud, pgprot);
1403 if (tmp < 0)
1404 return cur_pages;
1405
1406 cur_pages += tmp;
1407 }
1408 return cur_pages;
1409}
1410
1411/*
1412 * Restrictions for kernel page table do not necessarily apply when mapping in
1413 * an alternate PGD.
1414 */
1415static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1416{
1417 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1418 pud_t *pud = NULL; /* shut up gcc */
1419 p4d_t *p4d;
1420 pgd_t *pgd_entry;
1421 long ret;
1422
1423 pgd_entry = cpa->pgd + pgd_index(addr);
1424
1425 if (pgd_none(*pgd_entry)) {
1426 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
1427 if (!p4d)
1428 return -1;
1429
1430 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE));
1431 }
1432
1433 /*
1434 * Allocate a PUD page and hand it down for mapping.
1435 */
1436 p4d = p4d_offset(pgd_entry, addr);
1437 if (p4d_none(*p4d)) {
1438 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
1439 if (!pud)
1440 return -1;
1441
1442 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
1443 }
1444
1445 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1446 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1447
1448 ret = populate_pud(cpa, addr, p4d, pgprot);
1449 if (ret < 0) {
1450 /*
1451 * Leave the PUD page in place in case some other CPU or thread
1452 * already found it, but remove any useless entries we just
1453 * added to it.
1454 */
1455 unmap_pud_range(p4d, addr,
1456 addr + (cpa->numpages << PAGE_SHIFT));
1457 return ret;
1458 }
1459
1460 cpa->numpages = ret;
1461 return 0;
1462}
1463
1464static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1465 int primary)
1466{
1467 if (cpa->pgd) {
1468 /*
1469 * Right now, we only execute this code path when mapping
1470 * the EFI virtual memory map regions, no other users
1471 * provide a ->pgd value. This may change in the future.
1472 */
1473 return populate_pgd(cpa, vaddr);
1474 }
1475
1476 /*
1477 * Ignore all non primary paths.
1478 */
1479 if (!primary) {
1480 cpa->numpages = 1;
1481 return 0;
1482 }
1483
1484 /*
1485 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1486 * to have holes.
1487 * Also set numpages to '1' indicating that we processed cpa req for
1488 * one virtual address page and its pfn. TBD: numpages can be set based
1489 * on the initial value and the level returned by lookup_address().
1490 */
1491 if (within(vaddr, PAGE_OFFSET,
1492 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1493 cpa->numpages = 1;
1494 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1495 return 0;
1496
1497 } else if (__cpa_pfn_in_highmap(cpa->pfn)) {
1498 /* Faults in the highmap are OK, so do not warn: */
1499 return -EFAULT;
1500 } else {
1501 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1502 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1503 *cpa->vaddr);
1504
1505 return -EFAULT;
1506 }
1507}
1508
1509static int __change_page_attr(struct cpa_data *cpa, int primary)
1510{
1511 unsigned long address;
1512 int do_split, err;
1513 unsigned int level;
1514 pte_t *kpte, old_pte;
1515
1516 address = __cpa_addr(cpa, cpa->curpage);
1517repeat:
1518 kpte = _lookup_address_cpa(cpa, address, &level);
1519 if (!kpte)
1520 return __cpa_process_fault(cpa, address, primary);
1521
1522 old_pte = *kpte;
1523 if (pte_none(old_pte))
1524 return __cpa_process_fault(cpa, address, primary);
1525
1526 if (level == PG_LEVEL_4K) {
1527 pte_t new_pte;
1528 pgprot_t new_prot = pte_pgprot(old_pte);
1529 unsigned long pfn = pte_pfn(old_pte);
1530
1531 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1532 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
1533
1534 cpa_inc_4k_install();
1535 /* Hand in lpsize = 0 to enforce the protection mechanism */
1536 new_prot = static_protections(new_prot, address, pfn, 1, 0,
1537 CPA_PROTECT);
1538
1539 new_prot = pgprot_clear_protnone_bits(new_prot);
1540
1541 /*
1542 * We need to keep the pfn from the existing PTE,
1543 * after all we're only going to change it's attributes
1544 * not the memory it points to
1545 */
1546 new_pte = pfn_pte(pfn, new_prot);
1547 cpa->pfn = pfn;
1548 /*
1549 * Do we really change anything ?
1550 */
1551 if (pte_val(old_pte) != pte_val(new_pte)) {
1552 set_pte_atomic(kpte, new_pte);
1553 cpa->flags |= CPA_FLUSHTLB;
1554 }
1555 cpa->numpages = 1;
1556 return 0;
1557 }
1558
1559 /*
1560 * Check, whether we can keep the large page intact
1561 * and just change the pte:
1562 */
1563 do_split = should_split_large_page(kpte, address, cpa);
1564 /*
1565 * When the range fits into the existing large page,
1566 * return. cp->numpages and cpa->tlbflush have been updated in
1567 * try_large_page:
1568 */
1569 if (do_split <= 0)
1570 return do_split;
1571
1572 /*
1573 * We have to split the large page:
1574 */
1575 err = split_large_page(cpa, kpte, address);
1576 if (!err)
1577 goto repeat;
1578
1579 return err;
1580}
1581
1582static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias);
1583
1584static int cpa_process_alias(struct cpa_data *cpa)
1585{
1586 struct cpa_data alias_cpa;
1587 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
1588 unsigned long vaddr;
1589 int ret;
1590
1591 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
1592 return 0;
1593
1594 /*
1595 * No need to redo, when the primary call touched the direct
1596 * mapping already:
1597 */
1598 vaddr = __cpa_addr(cpa, cpa->curpage);
1599 if (!(within(vaddr, PAGE_OFFSET,
1600 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
1601
1602 alias_cpa = *cpa;
1603 alias_cpa.vaddr = &laddr;
1604 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1605 alias_cpa.curpage = 0;
1606
1607 cpa->force_flush_all = 1;
1608
1609 ret = __change_page_attr_set_clr(&alias_cpa, 0);
1610 if (ret)
1611 return ret;
1612 }
1613
1614#ifdef CONFIG_X86_64
1615 /*
1616 * If the primary call didn't touch the high mapping already
1617 * and the physical address is inside the kernel map, we need
1618 * to touch the high mapped kernel as well:
1619 */
1620 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1621 __cpa_pfn_in_highmap(cpa->pfn)) {
1622 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1623 __START_KERNEL_map - phys_base;
1624 alias_cpa = *cpa;
1625 alias_cpa.vaddr = &temp_cpa_vaddr;
1626 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1627 alias_cpa.curpage = 0;
1628
1629 cpa->force_flush_all = 1;
1630 /*
1631 * The high mapping range is imprecise, so ignore the
1632 * return value.
1633 */
1634 __change_page_attr_set_clr(&alias_cpa, 0);
1635 }
1636#endif
1637
1638 return 0;
1639}
1640
1641static int __change_page_attr_set_clr(struct cpa_data *cpa, int checkalias)
1642{
1643 unsigned long numpages = cpa->numpages;
1644 unsigned long rempages = numpages;
1645 int ret = 0;
1646
1647 while (rempages) {
1648 /*
1649 * Store the remaining nr of pages for the large page
1650 * preservation check.
1651 */
1652 cpa->numpages = rempages;
1653 /* for array changes, we can't use large page */
1654 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
1655 cpa->numpages = 1;
1656
1657 if (!debug_pagealloc_enabled())
1658 spin_lock(&cpa_lock);
1659 ret = __change_page_attr(cpa, checkalias);
1660 if (!debug_pagealloc_enabled())
1661 spin_unlock(&cpa_lock);
1662 if (ret)
1663 goto out;
1664
1665 if (checkalias) {
1666 ret = cpa_process_alias(cpa);
1667 if (ret)
1668 goto out;
1669 }
1670
1671 /*
1672 * Adjust the number of pages with the result of the
1673 * CPA operation. Either a large page has been
1674 * preserved or a single page update happened.
1675 */
1676 BUG_ON(cpa->numpages > rempages || !cpa->numpages);
1677 rempages -= cpa->numpages;
1678 cpa->curpage += cpa->numpages;
1679 }
1680
1681out:
1682 /* Restore the original numpages */
1683 cpa->numpages = numpages;
1684 return ret;
1685}
1686
1687static int change_page_attr_set_clr(unsigned long *addr, int numpages,
1688 pgprot_t mask_set, pgprot_t mask_clr,
1689 int force_split, int in_flag,
1690 struct page **pages)
1691{
1692 struct cpa_data cpa;
1693 int ret, cache, checkalias;
1694
1695 memset(&cpa, 0, sizeof(cpa));
1696
1697 /*
1698 * Check, if we are requested to set a not supported
1699 * feature. Clearing non-supported features is OK.
1700 */
1701 mask_set = canon_pgprot(mask_set);
1702
1703 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
1704 return 0;
1705
1706 /* Ensure we are PAGE_SIZE aligned */
1707 if (in_flag & CPA_ARRAY) {
1708 int i;
1709 for (i = 0; i < numpages; i++) {
1710 if (addr[i] & ~PAGE_MASK) {
1711 addr[i] &= PAGE_MASK;
1712 WARN_ON_ONCE(1);
1713 }
1714 }
1715 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1716 /*
1717 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1718 * No need to check in that case
1719 */
1720 if (*addr & ~PAGE_MASK) {
1721 *addr &= PAGE_MASK;
1722 /*
1723 * People should not be passing in unaligned addresses:
1724 */
1725 WARN_ON_ONCE(1);
1726 }
1727 }
1728
1729 /* Must avoid aliasing mappings in the highmem code */
1730 kmap_flush_unused();
1731
1732 vm_unmap_aliases();
1733
1734 cpa.vaddr = addr;
1735 cpa.pages = pages;
1736 cpa.numpages = numpages;
1737 cpa.mask_set = mask_set;
1738 cpa.mask_clr = mask_clr;
1739 cpa.flags = 0;
1740 cpa.curpage = 0;
1741 cpa.force_split = force_split;
1742
1743 if (in_flag & (CPA_ARRAY | CPA_PAGES_ARRAY))
1744 cpa.flags |= in_flag;
1745
1746 /* No alias checking for _NX bit modifications */
1747 checkalias = (pgprot_val(mask_set) | pgprot_val(mask_clr)) != _PAGE_NX;
1748 /* Has caller explicitly disabled alias checking? */
1749 if (in_flag & CPA_NO_CHECK_ALIAS)
1750 checkalias = 0;
1751
1752 ret = __change_page_attr_set_clr(&cpa, checkalias);
1753
1754 /*
1755 * Check whether we really changed something:
1756 */
1757 if (!(cpa.flags & CPA_FLUSHTLB))
1758 goto out;
1759
1760 /*
1761 * No need to flush, when we did not set any of the caching
1762 * attributes:
1763 */
1764 cache = !!pgprot2cachemode(mask_set);
1765
1766 /*
1767 * On error; flush everything to be sure.
1768 */
1769 if (ret) {
1770 cpa_flush_all(cache);
1771 goto out;
1772 }
1773
1774 cpa_flush(&cpa, cache);
1775out:
1776 return ret;
1777}
1778
1779static inline int change_page_attr_set(unsigned long *addr, int numpages,
1780 pgprot_t mask, int array)
1781{
1782 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
1783 (array ? CPA_ARRAY : 0), NULL);
1784}
1785
1786static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1787 pgprot_t mask, int array)
1788{
1789 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
1790 (array ? CPA_ARRAY : 0), NULL);
1791}
1792
1793static inline int cpa_set_pages_array(struct page **pages, int numpages,
1794 pgprot_t mask)
1795{
1796 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1797 CPA_PAGES_ARRAY, pages);
1798}
1799
1800static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1801 pgprot_t mask)
1802{
1803 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1804 CPA_PAGES_ARRAY, pages);
1805}
1806
1807/*
1808 * _set_memory_prot is an internal helper for callers that have been passed
1809 * a pgprot_t value from upper layers and a reservation has already been taken.
1810 * If you want to set the pgprot to a specific page protocol, use the
1811 * set_memory_xx() functions.
1812 */
1813int __set_memory_prot(unsigned long addr, int numpages, pgprot_t prot)
1814{
1815 return change_page_attr_set_clr(&addr, numpages, prot,
1816 __pgprot(~pgprot_val(prot)), 0, 0,
1817 NULL);
1818}
1819
1820int _set_memory_uc(unsigned long addr, int numpages)
1821{
1822 /*
1823 * for now UC MINUS. see comments in ioremap()
1824 * If you really need strong UC use ioremap_uc(), but note
1825 * that you cannot override IO areas with set_memory_*() as
1826 * these helpers cannot work with IO memory.
1827 */
1828 return change_page_attr_set(&addr, numpages,
1829 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1830 0);
1831}
1832
1833int set_memory_uc(unsigned long addr, int numpages)
1834{
1835 int ret;
1836
1837 /*
1838 * for now UC MINUS. see comments in ioremap()
1839 */
1840 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1841 _PAGE_CACHE_MODE_UC_MINUS, NULL);
1842 if (ret)
1843 goto out_err;
1844
1845 ret = _set_memory_uc(addr, numpages);
1846 if (ret)
1847 goto out_free;
1848
1849 return 0;
1850
1851out_free:
1852 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1853out_err:
1854 return ret;
1855}
1856EXPORT_SYMBOL(set_memory_uc);
1857
1858int _set_memory_wc(unsigned long addr, int numpages)
1859{
1860 int ret;
1861
1862 ret = change_page_attr_set(&addr, numpages,
1863 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1864 0);
1865 if (!ret) {
1866 ret = change_page_attr_set_clr(&addr, numpages,
1867 cachemode2pgprot(_PAGE_CACHE_MODE_WC),
1868 __pgprot(_PAGE_CACHE_MASK),
1869 0, 0, NULL);
1870 }
1871 return ret;
1872}
1873
1874int set_memory_wc(unsigned long addr, int numpages)
1875{
1876 int ret;
1877
1878 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1879 _PAGE_CACHE_MODE_WC, NULL);
1880 if (ret)
1881 return ret;
1882
1883 ret = _set_memory_wc(addr, numpages);
1884 if (ret)
1885 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1886
1887 return ret;
1888}
1889EXPORT_SYMBOL(set_memory_wc);
1890
1891int _set_memory_wt(unsigned long addr, int numpages)
1892{
1893 return change_page_attr_set(&addr, numpages,
1894 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
1895}
1896
1897int _set_memory_wb(unsigned long addr, int numpages)
1898{
1899 /* WB cache mode is hard wired to all cache attribute bits being 0 */
1900 return change_page_attr_clear(&addr, numpages,
1901 __pgprot(_PAGE_CACHE_MASK), 0);
1902}
1903
1904int set_memory_wb(unsigned long addr, int numpages)
1905{
1906 int ret;
1907
1908 ret = _set_memory_wb(addr, numpages);
1909 if (ret)
1910 return ret;
1911
1912 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1913 return 0;
1914}
1915EXPORT_SYMBOL(set_memory_wb);
1916
1917int set_memory_x(unsigned long addr, int numpages)
1918{
1919 if (!(__supported_pte_mask & _PAGE_NX))
1920 return 0;
1921
1922 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
1923}
1924
1925int set_memory_nx(unsigned long addr, int numpages)
1926{
1927 if (!(__supported_pte_mask & _PAGE_NX))
1928 return 0;
1929
1930 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
1931}
1932
1933int set_memory_ro(unsigned long addr, int numpages)
1934{
1935 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0);
1936}
1937
1938int set_memory_rw(unsigned long addr, int numpages)
1939{
1940 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
1941}
1942
1943int set_memory_np(unsigned long addr, int numpages)
1944{
1945 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
1946}
1947
1948int set_memory_np_noalias(unsigned long addr, int numpages)
1949{
1950 int cpa_flags = CPA_NO_CHECK_ALIAS;
1951
1952 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
1953 __pgprot(_PAGE_PRESENT), 0,
1954 cpa_flags, NULL);
1955}
1956
1957int set_memory_4k(unsigned long addr, int numpages)
1958{
1959 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
1960 __pgprot(0), 1, 0, NULL);
1961}
1962
1963int set_memory_nonglobal(unsigned long addr, int numpages)
1964{
1965 return change_page_attr_clear(&addr, numpages,
1966 __pgprot(_PAGE_GLOBAL), 0);
1967}
1968
1969int set_memory_global(unsigned long addr, int numpages)
1970{
1971 return change_page_attr_set(&addr, numpages,
1972 __pgprot(_PAGE_GLOBAL), 0);
1973}
1974
1975static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
1976{
1977 struct cpa_data cpa;
1978 int ret;
1979
1980 /* Nothing to do if memory encryption is not active */
1981 if (!mem_encrypt_active())
1982 return 0;
1983
1984 /* Should not be working on unaligned addresses */
1985 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr))
1986 addr &= PAGE_MASK;
1987
1988 memset(&cpa, 0, sizeof(cpa));
1989 cpa.vaddr = &addr;
1990 cpa.numpages = numpages;
1991 cpa.mask_set = enc ? __pgprot(_PAGE_ENC) : __pgprot(0);
1992 cpa.mask_clr = enc ? __pgprot(0) : __pgprot(_PAGE_ENC);
1993 cpa.pgd = init_mm.pgd;
1994
1995 /* Must avoid aliasing mappings in the highmem code */
1996 kmap_flush_unused();
1997 vm_unmap_aliases();
1998
1999 /*
2000 * Before changing the encryption attribute, we need to flush caches.
2001 */
2002 cpa_flush(&cpa, 1);
2003
2004 ret = __change_page_attr_set_clr(&cpa, 1);
2005
2006 /*
2007 * After changing the encryption attribute, we need to flush TLBs again
2008 * in case any speculative TLB caching occurred (but no need to flush
2009 * caches again). We could just use cpa_flush_all(), but in case TLB
2010 * flushing gets optimized in the cpa_flush() path use the same logic
2011 * as above.
2012 */
2013 cpa_flush(&cpa, 0);
2014
2015 return ret;
2016}
2017
2018int set_memory_encrypted(unsigned long addr, int numpages)
2019{
2020 return __set_memory_enc_dec(addr, numpages, true);
2021}
2022EXPORT_SYMBOL_GPL(set_memory_encrypted);
2023
2024int set_memory_decrypted(unsigned long addr, int numpages)
2025{
2026 return __set_memory_enc_dec(addr, numpages, false);
2027}
2028EXPORT_SYMBOL_GPL(set_memory_decrypted);
2029
2030int set_pages_uc(struct page *page, int numpages)
2031{
2032 unsigned long addr = (unsigned long)page_address(page);
2033
2034 return set_memory_uc(addr, numpages);
2035}
2036EXPORT_SYMBOL(set_pages_uc);
2037
2038static int _set_pages_array(struct page **pages, int numpages,
2039 enum page_cache_mode new_type)
2040{
2041 unsigned long start;
2042 unsigned long end;
2043 enum page_cache_mode set_type;
2044 int i;
2045 int free_idx;
2046 int ret;
2047
2048 for (i = 0; i < numpages; i++) {
2049 if (PageHighMem(pages[i]))
2050 continue;
2051 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2052 end = start + PAGE_SIZE;
2053 if (memtype_reserve(start, end, new_type, NULL))
2054 goto err_out;
2055 }
2056
2057 /* If WC, set to UC- first and then WC */
2058 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
2059 _PAGE_CACHE_MODE_UC_MINUS : new_type;
2060
2061 ret = cpa_set_pages_array(pages, numpages,
2062 cachemode2pgprot(set_type));
2063 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
2064 ret = change_page_attr_set_clr(NULL, numpages,
2065 cachemode2pgprot(
2066 _PAGE_CACHE_MODE_WC),
2067 __pgprot(_PAGE_CACHE_MASK),
2068 0, CPA_PAGES_ARRAY, pages);
2069 if (ret)
2070 goto err_out;
2071 return 0; /* Success */
2072err_out:
2073 free_idx = i;
2074 for (i = 0; i < free_idx; i++) {
2075 if (PageHighMem(pages[i]))
2076 continue;
2077 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2078 end = start + PAGE_SIZE;
2079 memtype_free(start, end);
2080 }
2081 return -EINVAL;
2082}
2083
2084int set_pages_array_uc(struct page **pages, int numpages)
2085{
2086 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS);
2087}
2088EXPORT_SYMBOL(set_pages_array_uc);
2089
2090int set_pages_array_wc(struct page **pages, int numpages)
2091{
2092 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC);
2093}
2094EXPORT_SYMBOL(set_pages_array_wc);
2095
2096int set_pages_array_wt(struct page **pages, int numpages)
2097{
2098 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WT);
2099}
2100EXPORT_SYMBOL_GPL(set_pages_array_wt);
2101
2102int set_pages_wb(struct page *page, int numpages)
2103{
2104 unsigned long addr = (unsigned long)page_address(page);
2105
2106 return set_memory_wb(addr, numpages);
2107}
2108EXPORT_SYMBOL(set_pages_wb);
2109
2110int set_pages_array_wb(struct page **pages, int numpages)
2111{
2112 int retval;
2113 unsigned long start;
2114 unsigned long end;
2115 int i;
2116
2117 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2118 retval = cpa_clear_pages_array(pages, numpages,
2119 __pgprot(_PAGE_CACHE_MASK));
2120 if (retval)
2121 return retval;
2122
2123 for (i = 0; i < numpages; i++) {
2124 if (PageHighMem(pages[i]))
2125 continue;
2126 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2127 end = start + PAGE_SIZE;
2128 memtype_free(start, end);
2129 }
2130
2131 return 0;
2132}
2133EXPORT_SYMBOL(set_pages_array_wb);
2134
2135int set_pages_ro(struct page *page, int numpages)
2136{
2137 unsigned long addr = (unsigned long)page_address(page);
2138
2139 return set_memory_ro(addr, numpages);
2140}
2141
2142int set_pages_rw(struct page *page, int numpages)
2143{
2144 unsigned long addr = (unsigned long)page_address(page);
2145
2146 return set_memory_rw(addr, numpages);
2147}
2148
2149static int __set_pages_p(struct page *page, int numpages)
2150{
2151 unsigned long tempaddr = (unsigned long) page_address(page);
2152 struct cpa_data cpa = { .vaddr = &tempaddr,
2153 .pgd = NULL,
2154 .numpages = numpages,
2155 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2156 .mask_clr = __pgprot(0),
2157 .flags = 0};
2158
2159 /*
2160 * No alias checking needed for setting present flag. otherwise,
2161 * we may need to break large pages for 64-bit kernel text
2162 * mappings (this adds to complexity if we want to do this from
2163 * atomic context especially). Let's keep it simple!
2164 */
2165 return __change_page_attr_set_clr(&cpa, 0);
2166}
2167
2168static int __set_pages_np(struct page *page, int numpages)
2169{
2170 unsigned long tempaddr = (unsigned long) page_address(page);
2171 struct cpa_data cpa = { .vaddr = &tempaddr,
2172 .pgd = NULL,
2173 .numpages = numpages,
2174 .mask_set = __pgprot(0),
2175 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2176 .flags = 0};
2177
2178 /*
2179 * No alias checking needed for setting not present flag. otherwise,
2180 * we may need to break large pages for 64-bit kernel text
2181 * mappings (this adds to complexity if we want to do this from
2182 * atomic context especially). Let's keep it simple!
2183 */
2184 return __change_page_attr_set_clr(&cpa, 0);
2185}
2186
2187int set_direct_map_invalid_noflush(struct page *page)
2188{
2189 return __set_pages_np(page, 1);
2190}
2191
2192int set_direct_map_default_noflush(struct page *page)
2193{
2194 return __set_pages_p(page, 1);
2195}
2196
2197void __kernel_map_pages(struct page *page, int numpages, int enable)
2198{
2199 if (PageHighMem(page))
2200 return;
2201 if (!enable) {
2202 debug_check_no_locks_freed(page_address(page),
2203 numpages * PAGE_SIZE);
2204 }
2205
2206 /*
2207 * The return value is ignored as the calls cannot fail.
2208 * Large pages for identity mappings are not used at boot time
2209 * and hence no memory allocations during large page split.
2210 */
2211 if (enable)
2212 __set_pages_p(page, numpages);
2213 else
2214 __set_pages_np(page, numpages);
2215
2216 /*
2217 * We should perform an IPI and flush all tlbs,
2218 * but that can deadlock->flush only current cpu.
2219 * Preemption needs to be disabled around __flush_tlb_all() due to
2220 * CR3 reload in __native_flush_tlb().
2221 */
2222 preempt_disable();
2223 __flush_tlb_all();
2224 preempt_enable();
2225
2226 arch_flush_lazy_mmu_mode();
2227}
2228
2229#ifdef CONFIG_HIBERNATION
2230bool kernel_page_present(struct page *page)
2231{
2232 unsigned int level;
2233 pte_t *pte;
2234
2235 if (PageHighMem(page))
2236 return false;
2237
2238 pte = lookup_address((unsigned long)page_address(page), &level);
2239 return (pte_val(*pte) & _PAGE_PRESENT);
2240}
2241#endif /* CONFIG_HIBERNATION */
2242
2243int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
2244 unsigned numpages, unsigned long page_flags)
2245{
2246 int retval = -EINVAL;
2247
2248 struct cpa_data cpa = {
2249 .vaddr = &address,
2250 .pfn = pfn,
2251 .pgd = pgd,
2252 .numpages = numpages,
2253 .mask_set = __pgprot(0),
2254 .mask_clr = __pgprot(~page_flags & (_PAGE_NX|_PAGE_RW)),
2255 .flags = 0,
2256 };
2257
2258 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2259
2260 if (!(__supported_pte_mask & _PAGE_NX))
2261 goto out;
2262
2263 if (!(page_flags & _PAGE_ENC))
2264 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
2265
2266 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
2267
2268 retval = __change_page_attr_set_clr(&cpa, 0);
2269 __flush_tlb_all();
2270
2271out:
2272 return retval;
2273}
2274
2275/*
2276 * __flush_tlb_all() flushes mappings only on current CPU and hence this
2277 * function shouldn't be used in an SMP environment. Presently, it's used only
2278 * during boot (way before smp_init()) by EFI subsystem and hence is ok.
2279 */
2280int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address,
2281 unsigned long numpages)
2282{
2283 int retval;
2284
2285 /*
2286 * The typical sequence for unmapping is to find a pte through
2287 * lookup_address_in_pgd() (ideally, it should never return NULL because
2288 * the address is already mapped) and change it's protections. As pfn is
2289 * the *target* of a mapping, it's not useful while unmapping.
2290 */
2291 struct cpa_data cpa = {
2292 .vaddr = &address,
2293 .pfn = 0,
2294 .pgd = pgd,
2295 .numpages = numpages,
2296 .mask_set = __pgprot(0),
2297 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2298 .flags = 0,
2299 };
2300
2301 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2302
2303 retval = __change_page_attr_set_clr(&cpa, 0);
2304 __flush_tlb_all();
2305
2306 return retval;
2307}
2308
2309/*
2310 * The testcases use internal knowledge of the implementation that shouldn't
2311 * be exposed to the rest of the kernel. Include these directly here.
2312 */
2313#ifdef CONFIG_CPA_DEBUG
2314#include "cpa-test.c"
2315#endif
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2002 Andi Kleen, SuSE Labs.
4 * Thanks to Ben LaHaise for precious feedback.
5 */
6#include <linux/highmem.h>
7#include <linux/memblock.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/interrupt.h>
11#include <linux/seq_file.h>
12#include <linux/proc_fs.h>
13#include <linux/debugfs.h>
14#include <linux/pfn.h>
15#include <linux/percpu.h>
16#include <linux/gfp.h>
17#include <linux/pci.h>
18#include <linux/vmalloc.h>
19#include <linux/libnvdimm.h>
20#include <linux/vmstat.h>
21#include <linux/kernel.h>
22#include <linux/cc_platform.h>
23#include <linux/set_memory.h>
24#include <linux/memregion.h>
25
26#include <asm/e820/api.h>
27#include <asm/processor.h>
28#include <asm/tlbflush.h>
29#include <asm/sections.h>
30#include <asm/setup.h>
31#include <linux/uaccess.h>
32#include <asm/pgalloc.h>
33#include <asm/proto.h>
34#include <asm/memtype.h>
35#include <asm/hyperv-tlfs.h>
36#include <asm/mshyperv.h>
37
38#include "../mm_internal.h"
39
40/*
41 * The current flushing context - we pass it instead of 5 arguments:
42 */
43struct cpa_data {
44 unsigned long *vaddr;
45 pgd_t *pgd;
46 pgprot_t mask_set;
47 pgprot_t mask_clr;
48 unsigned long numpages;
49 unsigned long curpage;
50 unsigned long pfn;
51 unsigned int flags;
52 unsigned int force_split : 1,
53 force_static_prot : 1,
54 force_flush_all : 1;
55 struct page **pages;
56};
57
58enum cpa_warn {
59 CPA_CONFLICT,
60 CPA_PROTECT,
61 CPA_DETECT,
62};
63
64static const int cpa_warn_level = CPA_PROTECT;
65
66/*
67 * Serialize cpa() (for !DEBUG_PAGEALLOC which uses large identity mappings)
68 * using cpa_lock. So that we don't allow any other cpu, with stale large tlb
69 * entries change the page attribute in parallel to some other cpu
70 * splitting a large page entry along with changing the attribute.
71 */
72static DEFINE_SPINLOCK(cpa_lock);
73
74#define CPA_FLUSHTLB 1
75#define CPA_ARRAY 2
76#define CPA_PAGES_ARRAY 4
77#define CPA_NO_CHECK_ALIAS 8 /* Do not search for aliases */
78
79static inline pgprot_t cachemode2pgprot(enum page_cache_mode pcm)
80{
81 return __pgprot(cachemode2protval(pcm));
82}
83
84#ifdef CONFIG_PROC_FS
85static unsigned long direct_pages_count[PG_LEVEL_NUM];
86
87void update_page_count(int level, unsigned long pages)
88{
89 /* Protect against CPA */
90 spin_lock(&pgd_lock);
91 direct_pages_count[level] += pages;
92 spin_unlock(&pgd_lock);
93}
94
95static void split_page_count(int level)
96{
97 if (direct_pages_count[level] == 0)
98 return;
99
100 direct_pages_count[level]--;
101 if (system_state == SYSTEM_RUNNING) {
102 if (level == PG_LEVEL_2M)
103 count_vm_event(DIRECT_MAP_LEVEL2_SPLIT);
104 else if (level == PG_LEVEL_1G)
105 count_vm_event(DIRECT_MAP_LEVEL3_SPLIT);
106 }
107 direct_pages_count[level - 1] += PTRS_PER_PTE;
108}
109
110void arch_report_meminfo(struct seq_file *m)
111{
112 seq_printf(m, "DirectMap4k: %8lu kB\n",
113 direct_pages_count[PG_LEVEL_4K] << 2);
114#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
115 seq_printf(m, "DirectMap2M: %8lu kB\n",
116 direct_pages_count[PG_LEVEL_2M] << 11);
117#else
118 seq_printf(m, "DirectMap4M: %8lu kB\n",
119 direct_pages_count[PG_LEVEL_2M] << 12);
120#endif
121 if (direct_gbpages)
122 seq_printf(m, "DirectMap1G: %8lu kB\n",
123 direct_pages_count[PG_LEVEL_1G] << 20);
124}
125#else
126static inline void split_page_count(int level) { }
127#endif
128
129#ifdef CONFIG_X86_CPA_STATISTICS
130
131static unsigned long cpa_1g_checked;
132static unsigned long cpa_1g_sameprot;
133static unsigned long cpa_1g_preserved;
134static unsigned long cpa_2m_checked;
135static unsigned long cpa_2m_sameprot;
136static unsigned long cpa_2m_preserved;
137static unsigned long cpa_4k_install;
138
139static inline void cpa_inc_1g_checked(void)
140{
141 cpa_1g_checked++;
142}
143
144static inline void cpa_inc_2m_checked(void)
145{
146 cpa_2m_checked++;
147}
148
149static inline void cpa_inc_4k_install(void)
150{
151 data_race(cpa_4k_install++);
152}
153
154static inline void cpa_inc_lp_sameprot(int level)
155{
156 if (level == PG_LEVEL_1G)
157 cpa_1g_sameprot++;
158 else
159 cpa_2m_sameprot++;
160}
161
162static inline void cpa_inc_lp_preserved(int level)
163{
164 if (level == PG_LEVEL_1G)
165 cpa_1g_preserved++;
166 else
167 cpa_2m_preserved++;
168}
169
170static int cpastats_show(struct seq_file *m, void *p)
171{
172 seq_printf(m, "1G pages checked: %16lu\n", cpa_1g_checked);
173 seq_printf(m, "1G pages sameprot: %16lu\n", cpa_1g_sameprot);
174 seq_printf(m, "1G pages preserved: %16lu\n", cpa_1g_preserved);
175 seq_printf(m, "2M pages checked: %16lu\n", cpa_2m_checked);
176 seq_printf(m, "2M pages sameprot: %16lu\n", cpa_2m_sameprot);
177 seq_printf(m, "2M pages preserved: %16lu\n", cpa_2m_preserved);
178 seq_printf(m, "4K pages set-checked: %16lu\n", cpa_4k_install);
179 return 0;
180}
181
182static int cpastats_open(struct inode *inode, struct file *file)
183{
184 return single_open(file, cpastats_show, NULL);
185}
186
187static const struct file_operations cpastats_fops = {
188 .open = cpastats_open,
189 .read = seq_read,
190 .llseek = seq_lseek,
191 .release = single_release,
192};
193
194static int __init cpa_stats_init(void)
195{
196 debugfs_create_file("cpa_stats", S_IRUSR, arch_debugfs_dir, NULL,
197 &cpastats_fops);
198 return 0;
199}
200late_initcall(cpa_stats_init);
201#else
202static inline void cpa_inc_1g_checked(void) { }
203static inline void cpa_inc_2m_checked(void) { }
204static inline void cpa_inc_4k_install(void) { }
205static inline void cpa_inc_lp_sameprot(int level) { }
206static inline void cpa_inc_lp_preserved(int level) { }
207#endif
208
209
210static inline int
211within(unsigned long addr, unsigned long start, unsigned long end)
212{
213 return addr >= start && addr < end;
214}
215
216static inline int
217within_inclusive(unsigned long addr, unsigned long start, unsigned long end)
218{
219 return addr >= start && addr <= end;
220}
221
222#ifdef CONFIG_X86_64
223
224/*
225 * The kernel image is mapped into two places in the virtual address space
226 * (addresses without KASLR, of course):
227 *
228 * 1. The kernel direct map (0xffff880000000000)
229 * 2. The "high kernel map" (0xffffffff81000000)
230 *
231 * We actually execute out of #2. If we get the address of a kernel symbol, it
232 * points to #2, but almost all physical-to-virtual translations point to #1.
233 *
234 * This is so that we can have both a directmap of all physical memory *and*
235 * take full advantage of the limited (s32) immediate addressing range (2G)
236 * of x86_64.
237 *
238 * See Documentation/arch/x86/x86_64/mm.rst for more detail.
239 */
240
241static inline unsigned long highmap_start_pfn(void)
242{
243 return __pa_symbol(_text) >> PAGE_SHIFT;
244}
245
246static inline unsigned long highmap_end_pfn(void)
247{
248 /* Do not reference physical address outside the kernel. */
249 return __pa_symbol(roundup(_brk_end, PMD_SIZE) - 1) >> PAGE_SHIFT;
250}
251
252static bool __cpa_pfn_in_highmap(unsigned long pfn)
253{
254 /*
255 * Kernel text has an alias mapping at a high address, known
256 * here as "highmap".
257 */
258 return within_inclusive(pfn, highmap_start_pfn(), highmap_end_pfn());
259}
260
261#else
262
263static bool __cpa_pfn_in_highmap(unsigned long pfn)
264{
265 /* There is no highmap on 32-bit */
266 return false;
267}
268
269#endif
270
271/*
272 * See set_mce_nospec().
273 *
274 * Machine check recovery code needs to change cache mode of poisoned pages to
275 * UC to avoid speculative access logging another error. But passing the
276 * address of the 1:1 mapping to set_memory_uc() is a fine way to encourage a
277 * speculative access. So we cheat and flip the top bit of the address. This
278 * works fine for the code that updates the page tables. But at the end of the
279 * process we need to flush the TLB and cache and the non-canonical address
280 * causes a #GP fault when used by the INVLPG and CLFLUSH instructions.
281 *
282 * But in the common case we already have a canonical address. This code
283 * will fix the top bit if needed and is a no-op otherwise.
284 */
285static inline unsigned long fix_addr(unsigned long addr)
286{
287#ifdef CONFIG_X86_64
288 return (long)(addr << 1) >> 1;
289#else
290 return addr;
291#endif
292}
293
294static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx)
295{
296 if (cpa->flags & CPA_PAGES_ARRAY) {
297 struct page *page = cpa->pages[idx];
298
299 if (unlikely(PageHighMem(page)))
300 return 0;
301
302 return (unsigned long)page_address(page);
303 }
304
305 if (cpa->flags & CPA_ARRAY)
306 return cpa->vaddr[idx];
307
308 return *cpa->vaddr + idx * PAGE_SIZE;
309}
310
311/*
312 * Flushing functions
313 */
314
315static void clflush_cache_range_opt(void *vaddr, unsigned int size)
316{
317 const unsigned long clflush_size = boot_cpu_data.x86_clflush_size;
318 void *p = (void *)((unsigned long)vaddr & ~(clflush_size - 1));
319 void *vend = vaddr + size;
320
321 if (p >= vend)
322 return;
323
324 for (; p < vend; p += clflush_size)
325 clflushopt(p);
326}
327
328/**
329 * clflush_cache_range - flush a cache range with clflush
330 * @vaddr: virtual start address
331 * @size: number of bytes to flush
332 *
333 * CLFLUSHOPT is an unordered instruction which needs fencing with MFENCE or
334 * SFENCE to avoid ordering issues.
335 */
336void clflush_cache_range(void *vaddr, unsigned int size)
337{
338 mb();
339 clflush_cache_range_opt(vaddr, size);
340 mb();
341}
342EXPORT_SYMBOL_GPL(clflush_cache_range);
343
344#ifdef CONFIG_ARCH_HAS_PMEM_API
345void arch_invalidate_pmem(void *addr, size_t size)
346{
347 clflush_cache_range(addr, size);
348}
349EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
350#endif
351
352#ifdef CONFIG_ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
353bool cpu_cache_has_invalidate_memregion(void)
354{
355 return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);
356}
357EXPORT_SYMBOL_NS_GPL(cpu_cache_has_invalidate_memregion, "DEVMEM");
358
359int cpu_cache_invalidate_memregion(int res_desc)
360{
361 if (WARN_ON_ONCE(!cpu_cache_has_invalidate_memregion()))
362 return -ENXIO;
363 wbinvd_on_all_cpus();
364 return 0;
365}
366EXPORT_SYMBOL_NS_GPL(cpu_cache_invalidate_memregion, "DEVMEM");
367#endif
368
369static void __cpa_flush_all(void *arg)
370{
371 unsigned long cache = (unsigned long)arg;
372
373 /*
374 * Flush all to work around Errata in early athlons regarding
375 * large page flushing.
376 */
377 __flush_tlb_all();
378
379 if (cache && boot_cpu_data.x86 >= 4)
380 wbinvd();
381}
382
383static void cpa_flush_all(unsigned long cache)
384{
385 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
386
387 on_each_cpu(__cpa_flush_all, (void *) cache, 1);
388}
389
390static void __cpa_flush_tlb(void *data)
391{
392 struct cpa_data *cpa = data;
393 unsigned int i;
394
395 for (i = 0; i < cpa->numpages; i++)
396 flush_tlb_one_kernel(fix_addr(__cpa_addr(cpa, i)));
397}
398
399static void cpa_flush(struct cpa_data *data, int cache)
400{
401 struct cpa_data *cpa = data;
402 unsigned int i;
403
404 BUG_ON(irqs_disabled() && !early_boot_irqs_disabled);
405
406 if (cache && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
407 cpa_flush_all(cache);
408 return;
409 }
410
411 if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling)
412 flush_tlb_all();
413 else
414 on_each_cpu(__cpa_flush_tlb, cpa, 1);
415
416 if (!cache)
417 return;
418
419 mb();
420 for (i = 0; i < cpa->numpages; i++) {
421 unsigned long addr = __cpa_addr(cpa, i);
422 unsigned int level;
423
424 pte_t *pte = lookup_address(addr, &level);
425
426 /*
427 * Only flush present addresses:
428 */
429 if (pte && (pte_val(*pte) & _PAGE_PRESENT))
430 clflush_cache_range_opt((void *)fix_addr(addr), PAGE_SIZE);
431 }
432 mb();
433}
434
435static bool overlaps(unsigned long r1_start, unsigned long r1_end,
436 unsigned long r2_start, unsigned long r2_end)
437{
438 return (r1_start <= r2_end && r1_end >= r2_start) ||
439 (r2_start <= r1_end && r2_end >= r1_start);
440}
441
442#ifdef CONFIG_PCI_BIOS
443/*
444 * The BIOS area between 640k and 1Mb needs to be executable for PCI BIOS
445 * based config access (CONFIG_PCI_GOBIOS) support.
446 */
447#define BIOS_PFN PFN_DOWN(BIOS_BEGIN)
448#define BIOS_PFN_END PFN_DOWN(BIOS_END - 1)
449
450static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
451{
452 if (pcibios_enabled && overlaps(spfn, epfn, BIOS_PFN, BIOS_PFN_END))
453 return _PAGE_NX;
454 return 0;
455}
456#else
457static pgprotval_t protect_pci_bios(unsigned long spfn, unsigned long epfn)
458{
459 return 0;
460}
461#endif
462
463/*
464 * The .rodata section needs to be read-only. Using the pfn catches all
465 * aliases. This also includes __ro_after_init, so do not enforce until
466 * kernel_set_to_readonly is true.
467 */
468static pgprotval_t protect_rodata(unsigned long spfn, unsigned long epfn)
469{
470 unsigned long epfn_ro, spfn_ro = PFN_DOWN(__pa_symbol(__start_rodata));
471
472 /*
473 * Note: __end_rodata is at page aligned and not inclusive, so
474 * subtract 1 to get the last enforced PFN in the rodata area.
475 */
476 epfn_ro = PFN_DOWN(__pa_symbol(__end_rodata)) - 1;
477
478 if (kernel_set_to_readonly && overlaps(spfn, epfn, spfn_ro, epfn_ro))
479 return _PAGE_RW;
480 return 0;
481}
482
483/*
484 * Protect kernel text against becoming non executable by forbidding
485 * _PAGE_NX. This protects only the high kernel mapping (_text -> _etext)
486 * out of which the kernel actually executes. Do not protect the low
487 * mapping.
488 *
489 * This does not cover __inittext since that is gone after boot.
490 */
491static pgprotval_t protect_kernel_text(unsigned long start, unsigned long end)
492{
493 unsigned long t_end = (unsigned long)_etext - 1;
494 unsigned long t_start = (unsigned long)_text;
495
496 if (overlaps(start, end, t_start, t_end))
497 return _PAGE_NX;
498 return 0;
499}
500
501#if defined(CONFIG_X86_64)
502/*
503 * Once the kernel maps the text as RO (kernel_set_to_readonly is set),
504 * kernel text mappings for the large page aligned text, rodata sections
505 * will be always read-only. For the kernel identity mappings covering the
506 * holes caused by this alignment can be anything that user asks.
507 *
508 * This will preserve the large page mappings for kernel text/data at no
509 * extra cost.
510 */
511static pgprotval_t protect_kernel_text_ro(unsigned long start,
512 unsigned long end)
513{
514 unsigned long t_end = (unsigned long)__end_rodata_hpage_align - 1;
515 unsigned long t_start = (unsigned long)_text;
516 unsigned int level;
517
518 if (!kernel_set_to_readonly || !overlaps(start, end, t_start, t_end))
519 return 0;
520 /*
521 * Don't enforce the !RW mapping for the kernel text mapping, if
522 * the current mapping is already using small page mapping. No
523 * need to work hard to preserve large page mappings in this case.
524 *
525 * This also fixes the Linux Xen paravirt guest boot failure caused
526 * by unexpected read-only mappings for kernel identity
527 * mappings. In this paravirt guest case, the kernel text mapping
528 * and the kernel identity mapping share the same page-table pages,
529 * so the protections for kernel text and identity mappings have to
530 * be the same.
531 */
532 if (lookup_address(start, &level) && (level != PG_LEVEL_4K))
533 return _PAGE_RW;
534 return 0;
535}
536#else
537static pgprotval_t protect_kernel_text_ro(unsigned long start,
538 unsigned long end)
539{
540 return 0;
541}
542#endif
543
544static inline bool conflicts(pgprot_t prot, pgprotval_t val)
545{
546 return (pgprot_val(prot) & ~val) != pgprot_val(prot);
547}
548
549static inline void check_conflict(int warnlvl, pgprot_t prot, pgprotval_t val,
550 unsigned long start, unsigned long end,
551 unsigned long pfn, const char *txt)
552{
553 static const char *lvltxt[] = {
554 [CPA_CONFLICT] = "conflict",
555 [CPA_PROTECT] = "protect",
556 [CPA_DETECT] = "detect",
557 };
558
559 if (warnlvl > cpa_warn_level || !conflicts(prot, val))
560 return;
561
562 pr_warn("CPA %8s %10s: 0x%016lx - 0x%016lx PFN %lx req %016llx prevent %016llx\n",
563 lvltxt[warnlvl], txt, start, end, pfn, (unsigned long long)pgprot_val(prot),
564 (unsigned long long)val);
565}
566
567/*
568 * Certain areas of memory on x86 require very specific protection flags,
569 * for example the BIOS area or kernel text. Callers don't always get this
570 * right (again, ioremap() on BIOS memory is not uncommon) so this function
571 * checks and fixes these known static required protection bits.
572 */
573static inline pgprot_t static_protections(pgprot_t prot, unsigned long start,
574 unsigned long pfn, unsigned long npg,
575 unsigned long lpsize, int warnlvl)
576{
577 pgprotval_t forbidden, res;
578 unsigned long end;
579
580 /*
581 * There is no point in checking RW/NX conflicts when the requested
582 * mapping is setting the page !PRESENT.
583 */
584 if (!(pgprot_val(prot) & _PAGE_PRESENT))
585 return prot;
586
587 /* Operate on the virtual address */
588 end = start + npg * PAGE_SIZE - 1;
589
590 res = protect_kernel_text(start, end);
591 check_conflict(warnlvl, prot, res, start, end, pfn, "Text NX");
592 forbidden = res;
593
594 /*
595 * Special case to preserve a large page. If the change spawns the
596 * full large page mapping then there is no point to split it
597 * up. Happens with ftrace and is going to be removed once ftrace
598 * switched to text_poke().
599 */
600 if (lpsize != (npg * PAGE_SIZE) || (start & (lpsize - 1))) {
601 res = protect_kernel_text_ro(start, end);
602 check_conflict(warnlvl, prot, res, start, end, pfn, "Text RO");
603 forbidden |= res;
604 }
605
606 /* Check the PFN directly */
607 res = protect_pci_bios(pfn, pfn + npg - 1);
608 check_conflict(warnlvl, prot, res, start, end, pfn, "PCIBIOS NX");
609 forbidden |= res;
610
611 res = protect_rodata(pfn, pfn + npg - 1);
612 check_conflict(warnlvl, prot, res, start, end, pfn, "Rodata RO");
613 forbidden |= res;
614
615 return __pgprot(pgprot_val(prot) & ~forbidden);
616}
617
618/*
619 * Validate strict W^X semantics.
620 */
621static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long start,
622 unsigned long pfn, unsigned long npg,
623 bool nx, bool rw)
624{
625 unsigned long end;
626
627 /*
628 * 32-bit has some unfixable W+X issues, like EFI code
629 * and writeable data being in the same page. Disable
630 * detection and enforcement there.
631 */
632 if (IS_ENABLED(CONFIG_X86_32))
633 return new;
634
635 /* Only verify when NX is supported: */
636 if (!(__supported_pte_mask & _PAGE_NX))
637 return new;
638
639 if (!((pgprot_val(old) ^ pgprot_val(new)) & (_PAGE_RW | _PAGE_NX)))
640 return new;
641
642 if ((pgprot_val(new) & (_PAGE_RW | _PAGE_NX)) != _PAGE_RW)
643 return new;
644
645 /* Non-leaf translation entries can disable writing or execution. */
646 if (!rw || nx)
647 return new;
648
649 end = start + npg * PAGE_SIZE - 1;
650 WARN_ONCE(1, "CPA detected W^X violation: %016llx -> %016llx range: 0x%016lx - 0x%016lx PFN %lx\n",
651 (unsigned long long)pgprot_val(old),
652 (unsigned long long)pgprot_val(new),
653 start, end, pfn);
654
655 /*
656 * For now, allow all permission change attempts by returning the
657 * attempted permissions. This can 'return old' to actively
658 * refuse the permission change at a later time.
659 */
660 return new;
661}
662
663/*
664 * Lookup the page table entry for a virtual address in a specific pgd.
665 * Return a pointer to the entry (or NULL if the entry does not exist),
666 * the level of the entry, and the effective NX and RW bits of all
667 * page table levels.
668 */
669pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
670 unsigned int *level, bool *nx, bool *rw)
671{
672 p4d_t *p4d;
673 pud_t *pud;
674 pmd_t *pmd;
675
676 *level = PG_LEVEL_256T;
677 *nx = false;
678 *rw = true;
679
680 if (pgd_none(*pgd))
681 return NULL;
682
683 *level = PG_LEVEL_512G;
684 *nx |= pgd_flags(*pgd) & _PAGE_NX;
685 *rw &= pgd_flags(*pgd) & _PAGE_RW;
686
687 p4d = p4d_offset(pgd, address);
688 if (p4d_none(*p4d))
689 return NULL;
690
691 if (p4d_leaf(*p4d) || !p4d_present(*p4d))
692 return (pte_t *)p4d;
693
694 *level = PG_LEVEL_1G;
695 *nx |= p4d_flags(*p4d) & _PAGE_NX;
696 *rw &= p4d_flags(*p4d) & _PAGE_RW;
697
698 pud = pud_offset(p4d, address);
699 if (pud_none(*pud))
700 return NULL;
701
702 if (pud_leaf(*pud) || !pud_present(*pud))
703 return (pte_t *)pud;
704
705 *level = PG_LEVEL_2M;
706 *nx |= pud_flags(*pud) & _PAGE_NX;
707 *rw &= pud_flags(*pud) & _PAGE_RW;
708
709 pmd = pmd_offset(pud, address);
710 if (pmd_none(*pmd))
711 return NULL;
712
713 if (pmd_leaf(*pmd) || !pmd_present(*pmd))
714 return (pte_t *)pmd;
715
716 *level = PG_LEVEL_4K;
717 *nx |= pmd_flags(*pmd) & _PAGE_NX;
718 *rw &= pmd_flags(*pmd) & _PAGE_RW;
719
720 return pte_offset_kernel(pmd, address);
721}
722
723/*
724 * Lookup the page table entry for a virtual address in a specific pgd.
725 * Return a pointer to the entry and the level of the mapping.
726 */
727pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
728 unsigned int *level)
729{
730 bool nx, rw;
731
732 return lookup_address_in_pgd_attr(pgd, address, level, &nx, &rw);
733}
734
735/*
736 * Lookup the page table entry for a virtual address. Return a pointer
737 * to the entry and the level of the mapping.
738 *
739 * Note: the function returns p4d, pud or pmd either when the entry is marked
740 * large or when the present bit is not set. Otherwise it returns NULL.
741 */
742pte_t *lookup_address(unsigned long address, unsigned int *level)
743{
744 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
745}
746EXPORT_SYMBOL_GPL(lookup_address);
747
748static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
749 unsigned int *level, bool *nx, bool *rw)
750{
751 pgd_t *pgd;
752
753 if (!cpa->pgd)
754 pgd = pgd_offset_k(address);
755 else
756 pgd = cpa->pgd + pgd_index(address);
757
758 return lookup_address_in_pgd_attr(pgd, address, level, nx, rw);
759}
760
761/*
762 * Lookup the PMD entry for a virtual address. Return a pointer to the entry
763 * or NULL if not present.
764 */
765pmd_t *lookup_pmd_address(unsigned long address)
766{
767 pgd_t *pgd;
768 p4d_t *p4d;
769 pud_t *pud;
770
771 pgd = pgd_offset_k(address);
772 if (pgd_none(*pgd))
773 return NULL;
774
775 p4d = p4d_offset(pgd, address);
776 if (p4d_none(*p4d) || p4d_leaf(*p4d) || !p4d_present(*p4d))
777 return NULL;
778
779 pud = pud_offset(p4d, address);
780 if (pud_none(*pud) || pud_leaf(*pud) || !pud_present(*pud))
781 return NULL;
782
783 return pmd_offset(pud, address);
784}
785
786/*
787 * This is necessary because __pa() does not work on some
788 * kinds of memory, like vmalloc() or the alloc_remap()
789 * areas on 32-bit NUMA systems. The percpu areas can
790 * end up in this kind of memory, for instance.
791 *
792 * Note that as long as the PTEs are well-formed with correct PFNs, this
793 * works without checking the PRESENT bit in the leaf PTE. This is unlike
794 * the similar vmalloc_to_page() and derivatives. Callers may depend on
795 * this behavior.
796 *
797 * This could be optimized, but it is only used in paths that are not perf
798 * sensitive, and keeping it unoptimized should increase the testing coverage
799 * for the more obscure platforms.
800 */
801phys_addr_t slow_virt_to_phys(void *__virt_addr)
802{
803 unsigned long virt_addr = (unsigned long)__virt_addr;
804 phys_addr_t phys_addr;
805 unsigned long offset;
806 enum pg_level level;
807 pte_t *pte;
808
809 pte = lookup_address(virt_addr, &level);
810 BUG_ON(!pte);
811
812 /*
813 * pXX_pfn() returns unsigned long, which must be cast to phys_addr_t
814 * before being left-shifted PAGE_SHIFT bits -- this trick is to
815 * make 32-PAE kernel work correctly.
816 */
817 switch (level) {
818 case PG_LEVEL_1G:
819 phys_addr = (phys_addr_t)pud_pfn(*(pud_t *)pte) << PAGE_SHIFT;
820 offset = virt_addr & ~PUD_MASK;
821 break;
822 case PG_LEVEL_2M:
823 phys_addr = (phys_addr_t)pmd_pfn(*(pmd_t *)pte) << PAGE_SHIFT;
824 offset = virt_addr & ~PMD_MASK;
825 break;
826 default:
827 phys_addr = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
828 offset = virt_addr & ~PAGE_MASK;
829 }
830
831 return (phys_addr_t)(phys_addr | offset);
832}
833EXPORT_SYMBOL_GPL(slow_virt_to_phys);
834
835/*
836 * Set the new pmd in all the pgds we know about:
837 */
838static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
839{
840 /* change init_mm */
841 set_pte_atomic(kpte, pte);
842#ifdef CONFIG_X86_32
843 if (!SHARED_KERNEL_PMD) {
844 struct page *page;
845
846 list_for_each_entry(page, &pgd_list, lru) {
847 pgd_t *pgd;
848 p4d_t *p4d;
849 pud_t *pud;
850 pmd_t *pmd;
851
852 pgd = (pgd_t *)page_address(page) + pgd_index(address);
853 p4d = p4d_offset(pgd, address);
854 pud = pud_offset(p4d, address);
855 pmd = pmd_offset(pud, address);
856 set_pte_atomic((pte_t *)pmd, pte);
857 }
858 }
859#endif
860}
861
862static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot)
863{
864 /*
865 * _PAGE_GLOBAL means "global page" for present PTEs.
866 * But, it is also used to indicate _PAGE_PROTNONE
867 * for non-present PTEs.
868 *
869 * This ensures that a _PAGE_GLOBAL PTE going from
870 * present to non-present is not confused as
871 * _PAGE_PROTNONE.
872 */
873 if (!(pgprot_val(prot) & _PAGE_PRESENT))
874 pgprot_val(prot) &= ~_PAGE_GLOBAL;
875
876 return prot;
877}
878
879static int __should_split_large_page(pte_t *kpte, unsigned long address,
880 struct cpa_data *cpa)
881{
882 unsigned long numpages, pmask, psize, lpaddr, pfn, old_pfn;
883 pgprot_t old_prot, new_prot, req_prot, chk_prot;
884 pte_t new_pte, *tmp;
885 enum pg_level level;
886 bool nx, rw;
887
888 /*
889 * Check for races, another CPU might have split this page
890 * up already:
891 */
892 tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw);
893 if (tmp != kpte)
894 return 1;
895
896 switch (level) {
897 case PG_LEVEL_2M:
898 old_prot = pmd_pgprot(*(pmd_t *)kpte);
899 old_pfn = pmd_pfn(*(pmd_t *)kpte);
900 cpa_inc_2m_checked();
901 break;
902 case PG_LEVEL_1G:
903 old_prot = pud_pgprot(*(pud_t *)kpte);
904 old_pfn = pud_pfn(*(pud_t *)kpte);
905 cpa_inc_1g_checked();
906 break;
907 default:
908 return -EINVAL;
909 }
910
911 psize = page_level_size(level);
912 pmask = page_level_mask(level);
913
914 /*
915 * Calculate the number of pages, which fit into this large
916 * page starting at address:
917 */
918 lpaddr = (address + psize) & pmask;
919 numpages = (lpaddr - address) >> PAGE_SHIFT;
920 if (numpages < cpa->numpages)
921 cpa->numpages = numpages;
922
923 /*
924 * We are safe now. Check whether the new pgprot is the same:
925 * Convert protection attributes to 4k-format, as cpa->mask* are set
926 * up accordingly.
927 */
928
929 /* Clear PSE (aka _PAGE_PAT) and move PAT bit to correct position */
930 req_prot = pgprot_large_2_4k(old_prot);
931
932 pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr);
933 pgprot_val(req_prot) |= pgprot_val(cpa->mask_set);
934
935 /*
936 * req_prot is in format of 4k pages. It must be converted to large
937 * page format: the caching mode includes the PAT bit located at
938 * different bit positions in the two formats.
939 */
940 req_prot = pgprot_4k_2_large(req_prot);
941 req_prot = pgprot_clear_protnone_bits(req_prot);
942 if (pgprot_val(req_prot) & _PAGE_PRESENT)
943 pgprot_val(req_prot) |= _PAGE_PSE;
944
945 /*
946 * old_pfn points to the large page base pfn. So we need to add the
947 * offset of the virtual address:
948 */
949 pfn = old_pfn + ((address & (psize - 1)) >> PAGE_SHIFT);
950 cpa->pfn = pfn;
951
952 /*
953 * Calculate the large page base address and the number of 4K pages
954 * in the large page
955 */
956 lpaddr = address & pmask;
957 numpages = psize >> PAGE_SHIFT;
958
959 /*
960 * Sanity check that the existing mapping is correct versus the static
961 * protections. static_protections() guards against !PRESENT, so no
962 * extra conditional required here.
963 */
964 chk_prot = static_protections(old_prot, lpaddr, old_pfn, numpages,
965 psize, CPA_CONFLICT);
966
967 if (WARN_ON_ONCE(pgprot_val(chk_prot) != pgprot_val(old_prot))) {
968 /*
969 * Split the large page and tell the split code to
970 * enforce static protections.
971 */
972 cpa->force_static_prot = 1;
973 return 1;
974 }
975
976 /*
977 * Optimization: If the requested pgprot is the same as the current
978 * pgprot, then the large page can be preserved and no updates are
979 * required independent of alignment and length of the requested
980 * range. The above already established that the current pgprot is
981 * correct, which in consequence makes the requested pgprot correct
982 * as well if it is the same. The static protection scan below will
983 * not come to a different conclusion.
984 */
985 if (pgprot_val(req_prot) == pgprot_val(old_prot)) {
986 cpa_inc_lp_sameprot(level);
987 return 0;
988 }
989
990 /*
991 * If the requested range does not cover the full page, split it up
992 */
993 if (address != lpaddr || cpa->numpages != numpages)
994 return 1;
995
996 /*
997 * Check whether the requested pgprot is conflicting with a static
998 * protection requirement in the large page.
999 */
1000 new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages,
1001 psize, CPA_DETECT);
1002
1003 new_prot = verify_rwx(old_prot, new_prot, lpaddr, old_pfn, numpages,
1004 nx, rw);
1005
1006 /*
1007 * If there is a conflict, split the large page.
1008 *
1009 * There used to be a 4k wise evaluation trying really hard to
1010 * preserve the large pages, but experimentation has shown, that this
1011 * does not help at all. There might be corner cases which would
1012 * preserve one large page occasionally, but it's really not worth the
1013 * extra code and cycles for the common case.
1014 */
1015 if (pgprot_val(req_prot) != pgprot_val(new_prot))
1016 return 1;
1017
1018 /* All checks passed. Update the large page mapping. */
1019 new_pte = pfn_pte(old_pfn, new_prot);
1020 __set_pmd_pte(kpte, address, new_pte);
1021 cpa->flags |= CPA_FLUSHTLB;
1022 cpa_inc_lp_preserved(level);
1023 return 0;
1024}
1025
1026static int should_split_large_page(pte_t *kpte, unsigned long address,
1027 struct cpa_data *cpa)
1028{
1029 int do_split;
1030
1031 if (cpa->force_split)
1032 return 1;
1033
1034 spin_lock(&pgd_lock);
1035 do_split = __should_split_large_page(kpte, address, cpa);
1036 spin_unlock(&pgd_lock);
1037
1038 return do_split;
1039}
1040
1041static void split_set_pte(struct cpa_data *cpa, pte_t *pte, unsigned long pfn,
1042 pgprot_t ref_prot, unsigned long address,
1043 unsigned long size)
1044{
1045 unsigned int npg = PFN_DOWN(size);
1046 pgprot_t prot;
1047
1048 /*
1049 * If should_split_large_page() discovered an inconsistent mapping,
1050 * remove the invalid protection in the split mapping.
1051 */
1052 if (!cpa->force_static_prot)
1053 goto set;
1054
1055 /* Hand in lpsize = 0 to enforce the protection mechanism */
1056 prot = static_protections(ref_prot, address, pfn, npg, 0, CPA_PROTECT);
1057
1058 if (pgprot_val(prot) == pgprot_val(ref_prot))
1059 goto set;
1060
1061 /*
1062 * If this is splitting a PMD, fix it up. PUD splits cannot be
1063 * fixed trivially as that would require to rescan the newly
1064 * installed PMD mappings after returning from split_large_page()
1065 * so an eventual further split can allocate the necessary PTE
1066 * pages. Warn for now and revisit it in case this actually
1067 * happens.
1068 */
1069 if (size == PAGE_SIZE)
1070 ref_prot = prot;
1071 else
1072 pr_warn_once("CPA: Cannot fixup static protections for PUD split\n");
1073set:
1074 set_pte(pte, pfn_pte(pfn, ref_prot));
1075}
1076
1077static int
1078__split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address,
1079 struct page *base)
1080{
1081 unsigned long lpaddr, lpinc, ref_pfn, pfn, pfninc = 1;
1082 pte_t *pbase = (pte_t *)page_address(base);
1083 unsigned int i, level;
1084 pgprot_t ref_prot;
1085 bool nx, rw;
1086 pte_t *tmp;
1087
1088 spin_lock(&pgd_lock);
1089 /*
1090 * Check for races, another CPU might have split this page
1091 * up for us already:
1092 */
1093 tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw);
1094 if (tmp != kpte) {
1095 spin_unlock(&pgd_lock);
1096 return 1;
1097 }
1098
1099 paravirt_alloc_pte(&init_mm, page_to_pfn(base));
1100
1101 switch (level) {
1102 case PG_LEVEL_2M:
1103 ref_prot = pmd_pgprot(*(pmd_t *)kpte);
1104 /*
1105 * Clear PSE (aka _PAGE_PAT) and move
1106 * PAT bit to correct position.
1107 */
1108 ref_prot = pgprot_large_2_4k(ref_prot);
1109 ref_pfn = pmd_pfn(*(pmd_t *)kpte);
1110 lpaddr = address & PMD_MASK;
1111 lpinc = PAGE_SIZE;
1112 break;
1113
1114 case PG_LEVEL_1G:
1115 ref_prot = pud_pgprot(*(pud_t *)kpte);
1116 ref_pfn = pud_pfn(*(pud_t *)kpte);
1117 pfninc = PMD_SIZE >> PAGE_SHIFT;
1118 lpaddr = address & PUD_MASK;
1119 lpinc = PMD_SIZE;
1120 /*
1121 * Clear the PSE flags if the PRESENT flag is not set
1122 * otherwise pmd_present() will return true even on a non
1123 * present pmd.
1124 */
1125 if (!(pgprot_val(ref_prot) & _PAGE_PRESENT))
1126 pgprot_val(ref_prot) &= ~_PAGE_PSE;
1127 break;
1128
1129 default:
1130 spin_unlock(&pgd_lock);
1131 return 1;
1132 }
1133
1134 ref_prot = pgprot_clear_protnone_bits(ref_prot);
1135
1136 /*
1137 * Get the target pfn from the original entry:
1138 */
1139 pfn = ref_pfn;
1140 for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc, lpaddr += lpinc)
1141 split_set_pte(cpa, pbase + i, pfn, ref_prot, lpaddr, lpinc);
1142
1143 if (virt_addr_valid(address)) {
1144 unsigned long pfn = PFN_DOWN(__pa(address));
1145
1146 if (pfn_range_is_mapped(pfn, pfn + 1))
1147 split_page_count(level);
1148 }
1149
1150 /*
1151 * Install the new, split up pagetable.
1152 *
1153 * We use the standard kernel pagetable protections for the new
1154 * pagetable protections, the actual ptes set above control the
1155 * primary protection behavior:
1156 */
1157 __set_pmd_pte(kpte, address, mk_pte(base, __pgprot(_KERNPG_TABLE)));
1158
1159 /*
1160 * Do a global flush tlb after splitting the large page
1161 * and before we do the actual change page attribute in the PTE.
1162 *
1163 * Without this, we violate the TLB application note, that says:
1164 * "The TLBs may contain both ordinary and large-page
1165 * translations for a 4-KByte range of linear addresses. This
1166 * may occur if software modifies the paging structures so that
1167 * the page size used for the address range changes. If the two
1168 * translations differ with respect to page frame or attributes
1169 * (e.g., permissions), processor behavior is undefined and may
1170 * be implementation-specific."
1171 *
1172 * We do this global tlb flush inside the cpa_lock, so that we
1173 * don't allow any other cpu, with stale tlb entries change the
1174 * page attribute in parallel, that also falls into the
1175 * just split large page entry.
1176 */
1177 flush_tlb_all();
1178 spin_unlock(&pgd_lock);
1179
1180 return 0;
1181}
1182
1183static int split_large_page(struct cpa_data *cpa, pte_t *kpte,
1184 unsigned long address)
1185{
1186 struct page *base;
1187
1188 if (!debug_pagealloc_enabled())
1189 spin_unlock(&cpa_lock);
1190 base = alloc_pages(GFP_KERNEL, 0);
1191 if (!debug_pagealloc_enabled())
1192 spin_lock(&cpa_lock);
1193 if (!base)
1194 return -ENOMEM;
1195
1196 if (__split_large_page(cpa, kpte, address, base))
1197 __free_page(base);
1198
1199 return 0;
1200}
1201
1202static bool try_to_free_pte_page(pte_t *pte)
1203{
1204 int i;
1205
1206 for (i = 0; i < PTRS_PER_PTE; i++)
1207 if (!pte_none(pte[i]))
1208 return false;
1209
1210 free_page((unsigned long)pte);
1211 return true;
1212}
1213
1214static bool try_to_free_pmd_page(pmd_t *pmd)
1215{
1216 int i;
1217
1218 for (i = 0; i < PTRS_PER_PMD; i++)
1219 if (!pmd_none(pmd[i]))
1220 return false;
1221
1222 free_page((unsigned long)pmd);
1223 return true;
1224}
1225
1226static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
1227{
1228 pte_t *pte = pte_offset_kernel(pmd, start);
1229
1230 while (start < end) {
1231 set_pte(pte, __pte(0));
1232
1233 start += PAGE_SIZE;
1234 pte++;
1235 }
1236
1237 if (try_to_free_pte_page((pte_t *)pmd_page_vaddr(*pmd))) {
1238 pmd_clear(pmd);
1239 return true;
1240 }
1241 return false;
1242}
1243
1244static void __unmap_pmd_range(pud_t *pud, pmd_t *pmd,
1245 unsigned long start, unsigned long end)
1246{
1247 if (unmap_pte_range(pmd, start, end))
1248 if (try_to_free_pmd_page(pud_pgtable(*pud)))
1249 pud_clear(pud);
1250}
1251
1252static void unmap_pmd_range(pud_t *pud, unsigned long start, unsigned long end)
1253{
1254 pmd_t *pmd = pmd_offset(pud, start);
1255
1256 /*
1257 * Not on a 2MB page boundary?
1258 */
1259 if (start & (PMD_SIZE - 1)) {
1260 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1261 unsigned long pre_end = min_t(unsigned long, end, next_page);
1262
1263 __unmap_pmd_range(pud, pmd, start, pre_end);
1264
1265 start = pre_end;
1266 pmd++;
1267 }
1268
1269 /*
1270 * Try to unmap in 2M chunks.
1271 */
1272 while (end - start >= PMD_SIZE) {
1273 if (pmd_leaf(*pmd))
1274 pmd_clear(pmd);
1275 else
1276 __unmap_pmd_range(pud, pmd, start, start + PMD_SIZE);
1277
1278 start += PMD_SIZE;
1279 pmd++;
1280 }
1281
1282 /*
1283 * 4K leftovers?
1284 */
1285 if (start < end)
1286 return __unmap_pmd_range(pud, pmd, start, end);
1287
1288 /*
1289 * Try again to free the PMD page if haven't succeeded above.
1290 */
1291 if (!pud_none(*pud))
1292 if (try_to_free_pmd_page(pud_pgtable(*pud)))
1293 pud_clear(pud);
1294}
1295
1296static void unmap_pud_range(p4d_t *p4d, unsigned long start, unsigned long end)
1297{
1298 pud_t *pud = pud_offset(p4d, start);
1299
1300 /*
1301 * Not on a GB page boundary?
1302 */
1303 if (start & (PUD_SIZE - 1)) {
1304 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1305 unsigned long pre_end = min_t(unsigned long, end, next_page);
1306
1307 unmap_pmd_range(pud, start, pre_end);
1308
1309 start = pre_end;
1310 pud++;
1311 }
1312
1313 /*
1314 * Try to unmap in 1G chunks?
1315 */
1316 while (end - start >= PUD_SIZE) {
1317
1318 if (pud_leaf(*pud))
1319 pud_clear(pud);
1320 else
1321 unmap_pmd_range(pud, start, start + PUD_SIZE);
1322
1323 start += PUD_SIZE;
1324 pud++;
1325 }
1326
1327 /*
1328 * 2M leftovers?
1329 */
1330 if (start < end)
1331 unmap_pmd_range(pud, start, end);
1332
1333 /*
1334 * No need to try to free the PUD page because we'll free it in
1335 * populate_pgd's error path
1336 */
1337}
1338
1339static int alloc_pte_page(pmd_t *pmd)
1340{
1341 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
1342 if (!pte)
1343 return -1;
1344
1345 set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE));
1346 return 0;
1347}
1348
1349static int alloc_pmd_page(pud_t *pud)
1350{
1351 pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_KERNEL);
1352 if (!pmd)
1353 return -1;
1354
1355 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
1356 return 0;
1357}
1358
1359static void populate_pte(struct cpa_data *cpa,
1360 unsigned long start, unsigned long end,
1361 unsigned num_pages, pmd_t *pmd, pgprot_t pgprot)
1362{
1363 pte_t *pte;
1364
1365 pte = pte_offset_kernel(pmd, start);
1366
1367 pgprot = pgprot_clear_protnone_bits(pgprot);
1368
1369 while (num_pages-- && start < end) {
1370 set_pte(pte, pfn_pte(cpa->pfn, pgprot));
1371
1372 start += PAGE_SIZE;
1373 cpa->pfn++;
1374 pte++;
1375 }
1376}
1377
1378static long populate_pmd(struct cpa_data *cpa,
1379 unsigned long start, unsigned long end,
1380 unsigned num_pages, pud_t *pud, pgprot_t pgprot)
1381{
1382 long cur_pages = 0;
1383 pmd_t *pmd;
1384 pgprot_t pmd_pgprot;
1385
1386 /*
1387 * Not on a 2M boundary?
1388 */
1389 if (start & (PMD_SIZE - 1)) {
1390 unsigned long pre_end = start + (num_pages << PAGE_SHIFT);
1391 unsigned long next_page = (start + PMD_SIZE) & PMD_MASK;
1392
1393 pre_end = min_t(unsigned long, pre_end, next_page);
1394 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1395 cur_pages = min_t(unsigned int, num_pages, cur_pages);
1396
1397 /*
1398 * Need a PTE page?
1399 */
1400 pmd = pmd_offset(pud, start);
1401 if (pmd_none(*pmd))
1402 if (alloc_pte_page(pmd))
1403 return -1;
1404
1405 populate_pte(cpa, start, pre_end, cur_pages, pmd, pgprot);
1406
1407 start = pre_end;
1408 }
1409
1410 /*
1411 * We mapped them all?
1412 */
1413 if (num_pages == cur_pages)
1414 return cur_pages;
1415
1416 pmd_pgprot = pgprot_4k_2_large(pgprot);
1417
1418 while (end - start >= PMD_SIZE) {
1419
1420 /*
1421 * We cannot use a 1G page so allocate a PMD page if needed.
1422 */
1423 if (pud_none(*pud))
1424 if (alloc_pmd_page(pud))
1425 return -1;
1426
1427 pmd = pmd_offset(pud, start);
1428
1429 set_pmd(pmd, pmd_mkhuge(pfn_pmd(cpa->pfn,
1430 canon_pgprot(pmd_pgprot))));
1431
1432 start += PMD_SIZE;
1433 cpa->pfn += PMD_SIZE >> PAGE_SHIFT;
1434 cur_pages += PMD_SIZE >> PAGE_SHIFT;
1435 }
1436
1437 /*
1438 * Map trailing 4K pages.
1439 */
1440 if (start < end) {
1441 pmd = pmd_offset(pud, start);
1442 if (pmd_none(*pmd))
1443 if (alloc_pte_page(pmd))
1444 return -1;
1445
1446 populate_pte(cpa, start, end, num_pages - cur_pages,
1447 pmd, pgprot);
1448 }
1449 return num_pages;
1450}
1451
1452static int populate_pud(struct cpa_data *cpa, unsigned long start, p4d_t *p4d,
1453 pgprot_t pgprot)
1454{
1455 pud_t *pud;
1456 unsigned long end;
1457 long cur_pages = 0;
1458 pgprot_t pud_pgprot;
1459
1460 end = start + (cpa->numpages << PAGE_SHIFT);
1461
1462 /*
1463 * Not on a Gb page boundary? => map everything up to it with
1464 * smaller pages.
1465 */
1466 if (start & (PUD_SIZE - 1)) {
1467 unsigned long pre_end;
1468 unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
1469
1470 pre_end = min_t(unsigned long, end, next_page);
1471 cur_pages = (pre_end - start) >> PAGE_SHIFT;
1472 cur_pages = min_t(int, (int)cpa->numpages, cur_pages);
1473
1474 pud = pud_offset(p4d, start);
1475
1476 /*
1477 * Need a PMD page?
1478 */
1479 if (pud_none(*pud))
1480 if (alloc_pmd_page(pud))
1481 return -1;
1482
1483 cur_pages = populate_pmd(cpa, start, pre_end, cur_pages,
1484 pud, pgprot);
1485 if (cur_pages < 0)
1486 return cur_pages;
1487
1488 start = pre_end;
1489 }
1490
1491 /* We mapped them all? */
1492 if (cpa->numpages == cur_pages)
1493 return cur_pages;
1494
1495 pud = pud_offset(p4d, start);
1496 pud_pgprot = pgprot_4k_2_large(pgprot);
1497
1498 /*
1499 * Map everything starting from the Gb boundary, possibly with 1G pages
1500 */
1501 while (boot_cpu_has(X86_FEATURE_GBPAGES) && end - start >= PUD_SIZE) {
1502 set_pud(pud, pud_mkhuge(pfn_pud(cpa->pfn,
1503 canon_pgprot(pud_pgprot))));
1504
1505 start += PUD_SIZE;
1506 cpa->pfn += PUD_SIZE >> PAGE_SHIFT;
1507 cur_pages += PUD_SIZE >> PAGE_SHIFT;
1508 pud++;
1509 }
1510
1511 /* Map trailing leftover */
1512 if (start < end) {
1513 long tmp;
1514
1515 pud = pud_offset(p4d, start);
1516 if (pud_none(*pud))
1517 if (alloc_pmd_page(pud))
1518 return -1;
1519
1520 tmp = populate_pmd(cpa, start, end, cpa->numpages - cur_pages,
1521 pud, pgprot);
1522 if (tmp < 0)
1523 return cur_pages;
1524
1525 cur_pages += tmp;
1526 }
1527 return cur_pages;
1528}
1529
1530/*
1531 * Restrictions for kernel page table do not necessarily apply when mapping in
1532 * an alternate PGD.
1533 */
1534static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1535{
1536 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1537 pud_t *pud = NULL; /* shut up gcc */
1538 p4d_t *p4d;
1539 pgd_t *pgd_entry;
1540 long ret;
1541
1542 pgd_entry = cpa->pgd + pgd_index(addr);
1543
1544 if (pgd_none(*pgd_entry)) {
1545 p4d = (p4d_t *)get_zeroed_page(GFP_KERNEL);
1546 if (!p4d)
1547 return -1;
1548
1549 set_pgd(pgd_entry, __pgd(__pa(p4d) | _KERNPG_TABLE));
1550 }
1551
1552 /*
1553 * Allocate a PUD page and hand it down for mapping.
1554 */
1555 p4d = p4d_offset(pgd_entry, addr);
1556 if (p4d_none(*p4d)) {
1557 pud = (pud_t *)get_zeroed_page(GFP_KERNEL);
1558 if (!pud)
1559 return -1;
1560
1561 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE));
1562 }
1563
1564 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
1565 pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
1566
1567 ret = populate_pud(cpa, addr, p4d, pgprot);
1568 if (ret < 0) {
1569 /*
1570 * Leave the PUD page in place in case some other CPU or thread
1571 * already found it, but remove any useless entries we just
1572 * added to it.
1573 */
1574 unmap_pud_range(p4d, addr,
1575 addr + (cpa->numpages << PAGE_SHIFT));
1576 return ret;
1577 }
1578
1579 cpa->numpages = ret;
1580 return 0;
1581}
1582
1583static int __cpa_process_fault(struct cpa_data *cpa, unsigned long vaddr,
1584 int primary)
1585{
1586 if (cpa->pgd) {
1587 /*
1588 * Right now, we only execute this code path when mapping
1589 * the EFI virtual memory map regions, no other users
1590 * provide a ->pgd value. This may change in the future.
1591 */
1592 return populate_pgd(cpa, vaddr);
1593 }
1594
1595 /*
1596 * Ignore all non primary paths.
1597 */
1598 if (!primary) {
1599 cpa->numpages = 1;
1600 return 0;
1601 }
1602
1603 /*
1604 * Ignore the NULL PTE for kernel identity mapping, as it is expected
1605 * to have holes.
1606 * Also set numpages to '1' indicating that we processed cpa req for
1607 * one virtual address page and its pfn. TBD: numpages can be set based
1608 * on the initial value and the level returned by lookup_address().
1609 */
1610 if (within(vaddr, PAGE_OFFSET,
1611 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) {
1612 cpa->numpages = 1;
1613 cpa->pfn = __pa(vaddr) >> PAGE_SHIFT;
1614 return 0;
1615
1616 } else if (__cpa_pfn_in_highmap(cpa->pfn)) {
1617 /* Faults in the highmap are OK, so do not warn: */
1618 return -EFAULT;
1619 } else {
1620 WARN(1, KERN_WARNING "CPA: called for zero pte. "
1621 "vaddr = %lx cpa->vaddr = %lx\n", vaddr,
1622 *cpa->vaddr);
1623
1624 return -EFAULT;
1625 }
1626}
1627
1628static int __change_page_attr(struct cpa_data *cpa, int primary)
1629{
1630 unsigned long address;
1631 int do_split, err;
1632 unsigned int level;
1633 pte_t *kpte, old_pte;
1634 bool nx, rw;
1635
1636 address = __cpa_addr(cpa, cpa->curpage);
1637repeat:
1638 kpte = _lookup_address_cpa(cpa, address, &level, &nx, &rw);
1639 if (!kpte)
1640 return __cpa_process_fault(cpa, address, primary);
1641
1642 old_pte = *kpte;
1643 if (pte_none(old_pte))
1644 return __cpa_process_fault(cpa, address, primary);
1645
1646 if (level == PG_LEVEL_4K) {
1647 pte_t new_pte;
1648 pgprot_t old_prot = pte_pgprot(old_pte);
1649 pgprot_t new_prot = pte_pgprot(old_pte);
1650 unsigned long pfn = pte_pfn(old_pte);
1651
1652 pgprot_val(new_prot) &= ~pgprot_val(cpa->mask_clr);
1653 pgprot_val(new_prot) |= pgprot_val(cpa->mask_set);
1654
1655 cpa_inc_4k_install();
1656 /* Hand in lpsize = 0 to enforce the protection mechanism */
1657 new_prot = static_protections(new_prot, address, pfn, 1, 0,
1658 CPA_PROTECT);
1659
1660 new_prot = verify_rwx(old_prot, new_prot, address, pfn, 1,
1661 nx, rw);
1662
1663 new_prot = pgprot_clear_protnone_bits(new_prot);
1664
1665 /*
1666 * We need to keep the pfn from the existing PTE,
1667 * after all we're only going to change its attributes
1668 * not the memory it points to
1669 */
1670 new_pte = pfn_pte(pfn, new_prot);
1671 cpa->pfn = pfn;
1672 /*
1673 * Do we really change anything ?
1674 */
1675 if (pte_val(old_pte) != pte_val(new_pte)) {
1676 set_pte_atomic(kpte, new_pte);
1677 cpa->flags |= CPA_FLUSHTLB;
1678 }
1679 cpa->numpages = 1;
1680 return 0;
1681 }
1682
1683 /*
1684 * Check, whether we can keep the large page intact
1685 * and just change the pte:
1686 */
1687 do_split = should_split_large_page(kpte, address, cpa);
1688 /*
1689 * When the range fits into the existing large page,
1690 * return. cp->numpages and cpa->tlbflush have been updated in
1691 * try_large_page:
1692 */
1693 if (do_split <= 0)
1694 return do_split;
1695
1696 /*
1697 * We have to split the large page:
1698 */
1699 err = split_large_page(cpa, kpte, address);
1700 if (!err)
1701 goto repeat;
1702
1703 return err;
1704}
1705
1706static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary);
1707
1708/*
1709 * Check the directmap and "high kernel map" 'aliases'.
1710 */
1711static int cpa_process_alias(struct cpa_data *cpa)
1712{
1713 struct cpa_data alias_cpa;
1714 unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
1715 unsigned long vaddr;
1716 int ret;
1717
1718 if (!pfn_range_is_mapped(cpa->pfn, cpa->pfn + 1))
1719 return 0;
1720
1721 /*
1722 * No need to redo, when the primary call touched the direct
1723 * mapping already:
1724 */
1725 vaddr = __cpa_addr(cpa, cpa->curpage);
1726 if (!(within(vaddr, PAGE_OFFSET,
1727 PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT)))) {
1728
1729 alias_cpa = *cpa;
1730 alias_cpa.vaddr = &laddr;
1731 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1732 alias_cpa.curpage = 0;
1733
1734 /* Directmap always has NX set, do not modify. */
1735 if (__supported_pte_mask & _PAGE_NX) {
1736 alias_cpa.mask_clr.pgprot &= ~_PAGE_NX;
1737 alias_cpa.mask_set.pgprot &= ~_PAGE_NX;
1738 }
1739
1740 cpa->force_flush_all = 1;
1741
1742 ret = __change_page_attr_set_clr(&alias_cpa, 0);
1743 if (ret)
1744 return ret;
1745 }
1746
1747#ifdef CONFIG_X86_64
1748 /*
1749 * If the primary call didn't touch the high mapping already
1750 * and the physical address is inside the kernel map, we need
1751 * to touch the high mapped kernel as well:
1752 */
1753 if (!within(vaddr, (unsigned long)_text, _brk_end) &&
1754 __cpa_pfn_in_highmap(cpa->pfn)) {
1755 unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
1756 __START_KERNEL_map - phys_base;
1757 alias_cpa = *cpa;
1758 alias_cpa.vaddr = &temp_cpa_vaddr;
1759 alias_cpa.flags &= ~(CPA_PAGES_ARRAY | CPA_ARRAY);
1760 alias_cpa.curpage = 0;
1761
1762 /*
1763 * [_text, _brk_end) also covers data, do not modify NX except
1764 * in cases where the highmap is the primary target.
1765 */
1766 if (__supported_pte_mask & _PAGE_NX) {
1767 alias_cpa.mask_clr.pgprot &= ~_PAGE_NX;
1768 alias_cpa.mask_set.pgprot &= ~_PAGE_NX;
1769 }
1770
1771 cpa->force_flush_all = 1;
1772 /*
1773 * The high mapping range is imprecise, so ignore the
1774 * return value.
1775 */
1776 __change_page_attr_set_clr(&alias_cpa, 0);
1777 }
1778#endif
1779
1780 return 0;
1781}
1782
1783static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary)
1784{
1785 unsigned long numpages = cpa->numpages;
1786 unsigned long rempages = numpages;
1787 int ret = 0;
1788
1789 /*
1790 * No changes, easy!
1791 */
1792 if (!(pgprot_val(cpa->mask_set) | pgprot_val(cpa->mask_clr)) &&
1793 !cpa->force_split)
1794 return ret;
1795
1796 while (rempages) {
1797 /*
1798 * Store the remaining nr of pages for the large page
1799 * preservation check.
1800 */
1801 cpa->numpages = rempages;
1802 /* for array changes, we can't use large page */
1803 if (cpa->flags & (CPA_ARRAY | CPA_PAGES_ARRAY))
1804 cpa->numpages = 1;
1805
1806 if (!debug_pagealloc_enabled())
1807 spin_lock(&cpa_lock);
1808 ret = __change_page_attr(cpa, primary);
1809 if (!debug_pagealloc_enabled())
1810 spin_unlock(&cpa_lock);
1811 if (ret)
1812 goto out;
1813
1814 if (primary && !(cpa->flags & CPA_NO_CHECK_ALIAS)) {
1815 ret = cpa_process_alias(cpa);
1816 if (ret)
1817 goto out;
1818 }
1819
1820 /*
1821 * Adjust the number of pages with the result of the
1822 * CPA operation. Either a large page has been
1823 * preserved or a single page update happened.
1824 */
1825 BUG_ON(cpa->numpages > rempages || !cpa->numpages);
1826 rempages -= cpa->numpages;
1827 cpa->curpage += cpa->numpages;
1828 }
1829
1830out:
1831 /* Restore the original numpages */
1832 cpa->numpages = numpages;
1833 return ret;
1834}
1835
1836static int change_page_attr_set_clr(unsigned long *addr, int numpages,
1837 pgprot_t mask_set, pgprot_t mask_clr,
1838 int force_split, int in_flag,
1839 struct page **pages)
1840{
1841 struct cpa_data cpa;
1842 int ret, cache;
1843
1844 memset(&cpa, 0, sizeof(cpa));
1845
1846 /*
1847 * Check, if we are requested to set a not supported
1848 * feature. Clearing non-supported features is OK.
1849 */
1850 mask_set = canon_pgprot(mask_set);
1851
1852 if (!pgprot_val(mask_set) && !pgprot_val(mask_clr) && !force_split)
1853 return 0;
1854
1855 /* Ensure we are PAGE_SIZE aligned */
1856 if (in_flag & CPA_ARRAY) {
1857 int i;
1858 for (i = 0; i < numpages; i++) {
1859 if (addr[i] & ~PAGE_MASK) {
1860 addr[i] &= PAGE_MASK;
1861 WARN_ON_ONCE(1);
1862 }
1863 }
1864 } else if (!(in_flag & CPA_PAGES_ARRAY)) {
1865 /*
1866 * in_flag of CPA_PAGES_ARRAY implies it is aligned.
1867 * No need to check in that case
1868 */
1869 if (*addr & ~PAGE_MASK) {
1870 *addr &= PAGE_MASK;
1871 /*
1872 * People should not be passing in unaligned addresses:
1873 */
1874 WARN_ON_ONCE(1);
1875 }
1876 }
1877
1878 /* Must avoid aliasing mappings in the highmem code */
1879 kmap_flush_unused();
1880
1881 vm_unmap_aliases();
1882
1883 cpa.vaddr = addr;
1884 cpa.pages = pages;
1885 cpa.numpages = numpages;
1886 cpa.mask_set = mask_set;
1887 cpa.mask_clr = mask_clr;
1888 cpa.flags = in_flag;
1889 cpa.curpage = 0;
1890 cpa.force_split = force_split;
1891
1892 ret = __change_page_attr_set_clr(&cpa, 1);
1893
1894 /*
1895 * Check whether we really changed something:
1896 */
1897 if (!(cpa.flags & CPA_FLUSHTLB))
1898 goto out;
1899
1900 /*
1901 * No need to flush, when we did not set any of the caching
1902 * attributes:
1903 */
1904 cache = !!pgprot2cachemode(mask_set);
1905
1906 /*
1907 * On error; flush everything to be sure.
1908 */
1909 if (ret) {
1910 cpa_flush_all(cache);
1911 goto out;
1912 }
1913
1914 cpa_flush(&cpa, cache);
1915out:
1916 return ret;
1917}
1918
1919static inline int change_page_attr_set(unsigned long *addr, int numpages,
1920 pgprot_t mask, int array)
1921{
1922 return change_page_attr_set_clr(addr, numpages, mask, __pgprot(0), 0,
1923 (array ? CPA_ARRAY : 0), NULL);
1924}
1925
1926static inline int change_page_attr_clear(unsigned long *addr, int numpages,
1927 pgprot_t mask, int array)
1928{
1929 return change_page_attr_set_clr(addr, numpages, __pgprot(0), mask, 0,
1930 (array ? CPA_ARRAY : 0), NULL);
1931}
1932
1933static inline int cpa_set_pages_array(struct page **pages, int numpages,
1934 pgprot_t mask)
1935{
1936 return change_page_attr_set_clr(NULL, numpages, mask, __pgprot(0), 0,
1937 CPA_PAGES_ARRAY, pages);
1938}
1939
1940static inline int cpa_clear_pages_array(struct page **pages, int numpages,
1941 pgprot_t mask)
1942{
1943 return change_page_attr_set_clr(NULL, numpages, __pgprot(0), mask, 0,
1944 CPA_PAGES_ARRAY, pages);
1945}
1946
1947/*
1948 * __set_memory_prot is an internal helper for callers that have been passed
1949 * a pgprot_t value from upper layers and a reservation has already been taken.
1950 * If you want to set the pgprot to a specific page protocol, use the
1951 * set_memory_xx() functions.
1952 */
1953int __set_memory_prot(unsigned long addr, int numpages, pgprot_t prot)
1954{
1955 return change_page_attr_set_clr(&addr, numpages, prot,
1956 __pgprot(~pgprot_val(prot)), 0, 0,
1957 NULL);
1958}
1959
1960int _set_memory_uc(unsigned long addr, int numpages)
1961{
1962 /*
1963 * for now UC MINUS. see comments in ioremap()
1964 * If you really need strong UC use ioremap_uc(), but note
1965 * that you cannot override IO areas with set_memory_*() as
1966 * these helpers cannot work with IO memory.
1967 */
1968 return change_page_attr_set(&addr, numpages,
1969 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
1970 0);
1971}
1972
1973int set_memory_uc(unsigned long addr, int numpages)
1974{
1975 int ret;
1976
1977 /*
1978 * for now UC MINUS. see comments in ioremap()
1979 */
1980 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
1981 _PAGE_CACHE_MODE_UC_MINUS, NULL);
1982 if (ret)
1983 goto out_err;
1984
1985 ret = _set_memory_uc(addr, numpages);
1986 if (ret)
1987 goto out_free;
1988
1989 return 0;
1990
1991out_free:
1992 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
1993out_err:
1994 return ret;
1995}
1996EXPORT_SYMBOL(set_memory_uc);
1997
1998int _set_memory_wc(unsigned long addr, int numpages)
1999{
2000 int ret;
2001
2002 ret = change_page_attr_set(&addr, numpages,
2003 cachemode2pgprot(_PAGE_CACHE_MODE_UC_MINUS),
2004 0);
2005 if (!ret) {
2006 ret = change_page_attr_set_clr(&addr, numpages,
2007 cachemode2pgprot(_PAGE_CACHE_MODE_WC),
2008 __pgprot(_PAGE_CACHE_MASK),
2009 0, 0, NULL);
2010 }
2011 return ret;
2012}
2013
2014int set_memory_wc(unsigned long addr, int numpages)
2015{
2016 int ret;
2017
2018 ret = memtype_reserve(__pa(addr), __pa(addr) + numpages * PAGE_SIZE,
2019 _PAGE_CACHE_MODE_WC, NULL);
2020 if (ret)
2021 return ret;
2022
2023 ret = _set_memory_wc(addr, numpages);
2024 if (ret)
2025 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
2026
2027 return ret;
2028}
2029EXPORT_SYMBOL(set_memory_wc);
2030
2031int _set_memory_wt(unsigned long addr, int numpages)
2032{
2033 return change_page_attr_set(&addr, numpages,
2034 cachemode2pgprot(_PAGE_CACHE_MODE_WT), 0);
2035}
2036
2037int _set_memory_wb(unsigned long addr, int numpages)
2038{
2039 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2040 return change_page_attr_clear(&addr, numpages,
2041 __pgprot(_PAGE_CACHE_MASK), 0);
2042}
2043
2044int set_memory_wb(unsigned long addr, int numpages)
2045{
2046 int ret;
2047
2048 ret = _set_memory_wb(addr, numpages);
2049 if (ret)
2050 return ret;
2051
2052 memtype_free(__pa(addr), __pa(addr) + numpages * PAGE_SIZE);
2053 return 0;
2054}
2055EXPORT_SYMBOL(set_memory_wb);
2056
2057/* Prevent speculative access to a page by marking it not-present */
2058#ifdef CONFIG_X86_64
2059int set_mce_nospec(unsigned long pfn)
2060{
2061 unsigned long decoy_addr;
2062 int rc;
2063
2064 /* SGX pages are not in the 1:1 map */
2065 if (arch_is_platform_page(pfn << PAGE_SHIFT))
2066 return 0;
2067 /*
2068 * We would like to just call:
2069 * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1);
2070 * but doing that would radically increase the odds of a
2071 * speculative access to the poison page because we'd have
2072 * the virtual address of the kernel 1:1 mapping sitting
2073 * around in registers.
2074 * Instead we get tricky. We create a non-canonical address
2075 * that looks just like the one we want, but has bit 63 flipped.
2076 * This relies on set_memory_XX() properly sanitizing any __pa()
2077 * results with __PHYSICAL_MASK or PTE_PFN_MASK.
2078 */
2079 decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
2080
2081 rc = set_memory_np(decoy_addr, 1);
2082 if (rc)
2083 pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
2084 return rc;
2085}
2086
2087/* Restore full speculative operation to the pfn. */
2088int clear_mce_nospec(unsigned long pfn)
2089{
2090 unsigned long addr = (unsigned long) pfn_to_kaddr(pfn);
2091
2092 return set_memory_p(addr, 1);
2093}
2094EXPORT_SYMBOL_GPL(clear_mce_nospec);
2095#endif /* CONFIG_X86_64 */
2096
2097int set_memory_x(unsigned long addr, int numpages)
2098{
2099 if (!(__supported_pte_mask & _PAGE_NX))
2100 return 0;
2101
2102 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_NX), 0);
2103}
2104
2105int set_memory_nx(unsigned long addr, int numpages)
2106{
2107 if (!(__supported_pte_mask & _PAGE_NX))
2108 return 0;
2109
2110 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_NX), 0);
2111}
2112
2113int set_memory_ro(unsigned long addr, int numpages)
2114{
2115 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW | _PAGE_DIRTY), 0);
2116}
2117
2118int set_memory_rox(unsigned long addr, int numpages)
2119{
2120 pgprot_t clr = __pgprot(_PAGE_RW | _PAGE_DIRTY);
2121
2122 if (__supported_pte_mask & _PAGE_NX)
2123 clr.pgprot |= _PAGE_NX;
2124
2125 return change_page_attr_clear(&addr, numpages, clr, 0);
2126}
2127
2128int set_memory_rw(unsigned long addr, int numpages)
2129{
2130 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_RW), 0);
2131}
2132
2133int set_memory_np(unsigned long addr, int numpages)
2134{
2135 return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
2136}
2137
2138int set_memory_np_noalias(unsigned long addr, int numpages)
2139{
2140 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2141 __pgprot(_PAGE_PRESENT), 0,
2142 CPA_NO_CHECK_ALIAS, NULL);
2143}
2144
2145int set_memory_p(unsigned long addr, int numpages)
2146{
2147 return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
2148}
2149
2150int set_memory_4k(unsigned long addr, int numpages)
2151{
2152 return change_page_attr_set_clr(&addr, numpages, __pgprot(0),
2153 __pgprot(0), 1, 0, NULL);
2154}
2155
2156int set_memory_nonglobal(unsigned long addr, int numpages)
2157{
2158 return change_page_attr_clear(&addr, numpages,
2159 __pgprot(_PAGE_GLOBAL), 0);
2160}
2161
2162int set_memory_global(unsigned long addr, int numpages)
2163{
2164 return change_page_attr_set(&addr, numpages,
2165 __pgprot(_PAGE_GLOBAL), 0);
2166}
2167
2168/*
2169 * __set_memory_enc_pgtable() is used for the hypervisors that get
2170 * informed about "encryption" status via page tables.
2171 */
2172static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
2173{
2174 pgprot_t empty = __pgprot(0);
2175 struct cpa_data cpa;
2176 int ret;
2177
2178 /* Should not be working on unaligned addresses */
2179 if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr))
2180 addr &= PAGE_MASK;
2181
2182 memset(&cpa, 0, sizeof(cpa));
2183 cpa.vaddr = &addr;
2184 cpa.numpages = numpages;
2185 cpa.mask_set = enc ? pgprot_encrypted(empty) : pgprot_decrypted(empty);
2186 cpa.mask_clr = enc ? pgprot_decrypted(empty) : pgprot_encrypted(empty);
2187 cpa.pgd = init_mm.pgd;
2188
2189 /* Must avoid aliasing mappings in the highmem code */
2190 kmap_flush_unused();
2191 vm_unmap_aliases();
2192
2193 /* Flush the caches as needed before changing the encryption attribute. */
2194 if (x86_platform.guest.enc_tlb_flush_required(enc))
2195 cpa_flush(&cpa, x86_platform.guest.enc_cache_flush_required());
2196
2197 /* Notify hypervisor that we are about to set/clr encryption attribute. */
2198 ret = x86_platform.guest.enc_status_change_prepare(addr, numpages, enc);
2199 if (ret)
2200 goto vmm_fail;
2201
2202 ret = __change_page_attr_set_clr(&cpa, 1);
2203
2204 /*
2205 * After changing the encryption attribute, we need to flush TLBs again
2206 * in case any speculative TLB caching occurred (but no need to flush
2207 * caches again). We could just use cpa_flush_all(), but in case TLB
2208 * flushing gets optimized in the cpa_flush() path use the same logic
2209 * as above.
2210 */
2211 cpa_flush(&cpa, 0);
2212
2213 if (ret)
2214 return ret;
2215
2216 /* Notify hypervisor that we have successfully set/clr encryption attribute. */
2217 ret = x86_platform.guest.enc_status_change_finish(addr, numpages, enc);
2218 if (ret)
2219 goto vmm_fail;
2220
2221 return 0;
2222
2223vmm_fail:
2224 WARN_ONCE(1, "CPA VMM failure to convert memory (addr=%p, numpages=%d) to %s: %d\n",
2225 (void *)addr, numpages, enc ? "private" : "shared", ret);
2226
2227 return ret;
2228}
2229
2230/*
2231 * The lock serializes conversions between private and shared memory.
2232 *
2233 * It is taken for read on conversion. A write lock guarantees that no
2234 * concurrent conversions are in progress.
2235 */
2236static DECLARE_RWSEM(mem_enc_lock);
2237
2238/*
2239 * Stop new private<->shared conversions.
2240 *
2241 * Taking the exclusive mem_enc_lock waits for in-flight conversions to complete.
2242 * The lock is not released to prevent new conversions from being started.
2243 */
2244bool set_memory_enc_stop_conversion(void)
2245{
2246 /*
2247 * In a crash scenario, sleep is not allowed. Try to take the lock.
2248 * Failure indicates that there is a race with the conversion.
2249 */
2250 if (oops_in_progress)
2251 return down_write_trylock(&mem_enc_lock);
2252
2253 down_write(&mem_enc_lock);
2254
2255 return true;
2256}
2257
2258static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
2259{
2260 int ret = 0;
2261
2262 if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
2263 if (!down_read_trylock(&mem_enc_lock))
2264 return -EBUSY;
2265
2266 ret = __set_memory_enc_pgtable(addr, numpages, enc);
2267
2268 up_read(&mem_enc_lock);
2269 }
2270
2271 return ret;
2272}
2273
2274int set_memory_encrypted(unsigned long addr, int numpages)
2275{
2276 return __set_memory_enc_dec(addr, numpages, true);
2277}
2278EXPORT_SYMBOL_GPL(set_memory_encrypted);
2279
2280int set_memory_decrypted(unsigned long addr, int numpages)
2281{
2282 return __set_memory_enc_dec(addr, numpages, false);
2283}
2284EXPORT_SYMBOL_GPL(set_memory_decrypted);
2285
2286int set_pages_uc(struct page *page, int numpages)
2287{
2288 unsigned long addr = (unsigned long)page_address(page);
2289
2290 return set_memory_uc(addr, numpages);
2291}
2292EXPORT_SYMBOL(set_pages_uc);
2293
2294static int _set_pages_array(struct page **pages, int numpages,
2295 enum page_cache_mode new_type)
2296{
2297 unsigned long start;
2298 unsigned long end;
2299 enum page_cache_mode set_type;
2300 int i;
2301 int free_idx;
2302 int ret;
2303
2304 for (i = 0; i < numpages; i++) {
2305 if (PageHighMem(pages[i]))
2306 continue;
2307 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2308 end = start + PAGE_SIZE;
2309 if (memtype_reserve(start, end, new_type, NULL))
2310 goto err_out;
2311 }
2312
2313 /* If WC, set to UC- first and then WC */
2314 set_type = (new_type == _PAGE_CACHE_MODE_WC) ?
2315 _PAGE_CACHE_MODE_UC_MINUS : new_type;
2316
2317 ret = cpa_set_pages_array(pages, numpages,
2318 cachemode2pgprot(set_type));
2319 if (!ret && new_type == _PAGE_CACHE_MODE_WC)
2320 ret = change_page_attr_set_clr(NULL, numpages,
2321 cachemode2pgprot(
2322 _PAGE_CACHE_MODE_WC),
2323 __pgprot(_PAGE_CACHE_MASK),
2324 0, CPA_PAGES_ARRAY, pages);
2325 if (ret)
2326 goto err_out;
2327 return 0; /* Success */
2328err_out:
2329 free_idx = i;
2330 for (i = 0; i < free_idx; i++) {
2331 if (PageHighMem(pages[i]))
2332 continue;
2333 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2334 end = start + PAGE_SIZE;
2335 memtype_free(start, end);
2336 }
2337 return -EINVAL;
2338}
2339
2340int set_pages_array_uc(struct page **pages, int numpages)
2341{
2342 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_UC_MINUS);
2343}
2344EXPORT_SYMBOL(set_pages_array_uc);
2345
2346int set_pages_array_wc(struct page **pages, int numpages)
2347{
2348 return _set_pages_array(pages, numpages, _PAGE_CACHE_MODE_WC);
2349}
2350EXPORT_SYMBOL(set_pages_array_wc);
2351
2352int set_pages_wb(struct page *page, int numpages)
2353{
2354 unsigned long addr = (unsigned long)page_address(page);
2355
2356 return set_memory_wb(addr, numpages);
2357}
2358EXPORT_SYMBOL(set_pages_wb);
2359
2360int set_pages_array_wb(struct page **pages, int numpages)
2361{
2362 int retval;
2363 unsigned long start;
2364 unsigned long end;
2365 int i;
2366
2367 /* WB cache mode is hard wired to all cache attribute bits being 0 */
2368 retval = cpa_clear_pages_array(pages, numpages,
2369 __pgprot(_PAGE_CACHE_MASK));
2370 if (retval)
2371 return retval;
2372
2373 for (i = 0; i < numpages; i++) {
2374 if (PageHighMem(pages[i]))
2375 continue;
2376 start = page_to_pfn(pages[i]) << PAGE_SHIFT;
2377 end = start + PAGE_SIZE;
2378 memtype_free(start, end);
2379 }
2380
2381 return 0;
2382}
2383EXPORT_SYMBOL(set_pages_array_wb);
2384
2385int set_pages_ro(struct page *page, int numpages)
2386{
2387 unsigned long addr = (unsigned long)page_address(page);
2388
2389 return set_memory_ro(addr, numpages);
2390}
2391
2392int set_pages_rw(struct page *page, int numpages)
2393{
2394 unsigned long addr = (unsigned long)page_address(page);
2395
2396 return set_memory_rw(addr, numpages);
2397}
2398
2399static int __set_pages_p(struct page *page, int numpages)
2400{
2401 unsigned long tempaddr = (unsigned long) page_address(page);
2402 struct cpa_data cpa = { .vaddr = &tempaddr,
2403 .pgd = NULL,
2404 .numpages = numpages,
2405 .mask_set = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2406 .mask_clr = __pgprot(0),
2407 .flags = CPA_NO_CHECK_ALIAS };
2408
2409 /*
2410 * No alias checking needed for setting present flag. otherwise,
2411 * we may need to break large pages for 64-bit kernel text
2412 * mappings (this adds to complexity if we want to do this from
2413 * atomic context especially). Let's keep it simple!
2414 */
2415 return __change_page_attr_set_clr(&cpa, 1);
2416}
2417
2418static int __set_pages_np(struct page *page, int numpages)
2419{
2420 unsigned long tempaddr = (unsigned long) page_address(page);
2421 struct cpa_data cpa = { .vaddr = &tempaddr,
2422 .pgd = NULL,
2423 .numpages = numpages,
2424 .mask_set = __pgprot(0),
2425 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2426 .flags = CPA_NO_CHECK_ALIAS };
2427
2428 /*
2429 * No alias checking needed for setting not present flag. otherwise,
2430 * we may need to break large pages for 64-bit kernel text
2431 * mappings (this adds to complexity if we want to do this from
2432 * atomic context especially). Let's keep it simple!
2433 */
2434 return __change_page_attr_set_clr(&cpa, 1);
2435}
2436
2437int set_direct_map_invalid_noflush(struct page *page)
2438{
2439 return __set_pages_np(page, 1);
2440}
2441
2442int set_direct_map_default_noflush(struct page *page)
2443{
2444 return __set_pages_p(page, 1);
2445}
2446
2447int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)
2448{
2449 if (valid)
2450 return __set_pages_p(page, nr);
2451
2452 return __set_pages_np(page, nr);
2453}
2454
2455#ifdef CONFIG_DEBUG_PAGEALLOC
2456void __kernel_map_pages(struct page *page, int numpages, int enable)
2457{
2458 if (PageHighMem(page))
2459 return;
2460 if (!enable) {
2461 debug_check_no_locks_freed(page_address(page),
2462 numpages * PAGE_SIZE);
2463 }
2464
2465 /*
2466 * The return value is ignored as the calls cannot fail.
2467 * Large pages for identity mappings are not used at boot time
2468 * and hence no memory allocations during large page split.
2469 */
2470 if (enable)
2471 __set_pages_p(page, numpages);
2472 else
2473 __set_pages_np(page, numpages);
2474
2475 /*
2476 * We should perform an IPI and flush all tlbs,
2477 * but that can deadlock->flush only current cpu.
2478 * Preemption needs to be disabled around __flush_tlb_all() due to
2479 * CR3 reload in __native_flush_tlb().
2480 */
2481 preempt_disable();
2482 __flush_tlb_all();
2483 preempt_enable();
2484
2485 arch_flush_lazy_mmu_mode();
2486}
2487#endif /* CONFIG_DEBUG_PAGEALLOC */
2488
2489bool kernel_page_present(struct page *page)
2490{
2491 unsigned int level;
2492 pte_t *pte;
2493
2494 if (PageHighMem(page))
2495 return false;
2496
2497 pte = lookup_address((unsigned long)page_address(page), &level);
2498 return (pte_val(*pte) & _PAGE_PRESENT);
2499}
2500
2501int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
2502 unsigned numpages, unsigned long page_flags)
2503{
2504 int retval = -EINVAL;
2505
2506 struct cpa_data cpa = {
2507 .vaddr = &address,
2508 .pfn = pfn,
2509 .pgd = pgd,
2510 .numpages = numpages,
2511 .mask_set = __pgprot(0),
2512 .mask_clr = __pgprot(~page_flags & (_PAGE_NX|_PAGE_RW)),
2513 .flags = CPA_NO_CHECK_ALIAS,
2514 };
2515
2516 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2517
2518 if (!(__supported_pte_mask & _PAGE_NX))
2519 goto out;
2520
2521 if (!(page_flags & _PAGE_ENC))
2522 cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
2523
2524 cpa.mask_set = __pgprot(_PAGE_PRESENT | page_flags);
2525
2526 retval = __change_page_attr_set_clr(&cpa, 1);
2527 __flush_tlb_all();
2528
2529out:
2530 return retval;
2531}
2532
2533/*
2534 * __flush_tlb_all() flushes mappings only on current CPU and hence this
2535 * function shouldn't be used in an SMP environment. Presently, it's used only
2536 * during boot (way before smp_init()) by EFI subsystem and hence is ok.
2537 */
2538int __init kernel_unmap_pages_in_pgd(pgd_t *pgd, unsigned long address,
2539 unsigned long numpages)
2540{
2541 int retval;
2542
2543 /*
2544 * The typical sequence for unmapping is to find a pte through
2545 * lookup_address_in_pgd() (ideally, it should never return NULL because
2546 * the address is already mapped) and change its protections. As pfn is
2547 * the *target* of a mapping, it's not useful while unmapping.
2548 */
2549 struct cpa_data cpa = {
2550 .vaddr = &address,
2551 .pfn = 0,
2552 .pgd = pgd,
2553 .numpages = numpages,
2554 .mask_set = __pgprot(0),
2555 .mask_clr = __pgprot(_PAGE_PRESENT | _PAGE_RW),
2556 .flags = CPA_NO_CHECK_ALIAS,
2557 };
2558
2559 WARN_ONCE(num_online_cpus() > 1, "Don't call after initializing SMP");
2560
2561 retval = __change_page_attr_set_clr(&cpa, 1);
2562 __flush_tlb_all();
2563
2564 return retval;
2565}
2566
2567/*
2568 * The testcases use internal knowledge of the implementation that shouldn't
2569 * be exposed to the rest of the kernel. Include these directly here.
2570 */
2571#ifdef CONFIG_CPA_DEBUG
2572#include "cpa-test.c"
2573#endif