CPA encodes page table entries in three places: when it writes a PTE, when it updates a large page it keeps and when it splits a large page. Each place converts between the PTE and the large page formats and fixes up PSE and PROTNONE. The split of a PUD does it differently from the split of a PMD. Add cpa_pte() that encodes an entry for a page table level and use it everywhere. The check for an unchanged large page no longer clears _PAGE_GLOBAL when _PAGE_PRESENT is clear, so set_memory_global() on a non-present large page rewrites or splits it, although nothing changes. Its only caller changes the kernel text, which is always present. Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/mm/pat/set_memory.c | 48 +++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 9ccc08cf220f..068ab1e2bbc8 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -969,6 +969,24 @@ static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot) return prot; } +/* + * The caching mode includes the PAT bit located at different bit positions + * in the 4K and large page formats, and _PAGE_PSE is only valid for present + * large pages. + */ +static pte_t cpa_pte(enum pg_level level, unsigned long pfn, pgprot_t prot) +{ + prot = pgprot_clear_protnone_bits(prot); + + if (level != PG_LEVEL_4K) { + prot = pgprot_4k_2_large(prot); + if (pgprot_val(prot) & _PAGE_PRESENT) + pgprot_val(prot) |= _PAGE_PSE; + } + + return pfn_pte(pfn, prot); +} + static int should_split_large_page(pte_t *kpte, unsigned long address, enum pg_level level, struct cpa_data *cpa) { @@ -1018,16 +1036,6 @@ static int should_split_large_page(pte_t *kpte, unsigned long address, pgprot_val(req_prot) &= ~pgprot_val(cpa->mask_clr); pgprot_val(req_prot) |= pgprot_val(cpa->mask_set); - /* - * req_prot is in format of 4k pages. It must be converted to large - * page format: the caching mode includes the PAT bit located at - * different bit positions in the two formats. - */ - req_prot = pgprot_4k_2_large(req_prot); - req_prot = pgprot_clear_protnone_bits(req_prot); - if (pgprot_val(req_prot) & _PAGE_PRESENT) - pgprot_val(req_prot) |= _PAGE_PSE; - /* * old_pfn points to the large page base pfn. So we need to add the * offset of the virtual address: @@ -1048,7 +1056,7 @@ static int should_split_large_page(pte_t *kpte, unsigned long address, * required independent of alignment and length of the requested * range. */ - if (pgprot_val(req_prot) == pgprot_val(old_prot)) { + if (pgprot_val(req_prot) == pgprot_val(pgprot_large_2_4k(old_prot))) { cpa_inc_lp_sameprot(level); return 0; } @@ -1082,7 +1090,7 @@ static int should_split_large_page(pte_t *kpte, unsigned long address, return 1; /* All checks passed. Update the large page mapping. */ - new_pte = pfn_pte(old_pfn, new_prot); + new_pte = cpa_pte(level, old_pfn, new_prot); __set_pmd_pte(kpte, address, new_pte); cpa->state |= CPA_ST_FLUSHTLB; cpa_inc_lp_preserved(level); @@ -1115,27 +1123,19 @@ __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address, ref_prot = pud_pgprot(*(pud_t *)kpte); ref_pfn = pud_pfn(*(pud_t *)kpte); pfninc = PMD_SIZE >> PAGE_SHIFT; - /* - * Clear the PSE flags if the PRESENT flag is not set - * otherwise pmd_present() will return true even on a non - * present pmd. - */ - if (!(pgprot_val(ref_prot) & _PAGE_PRESENT)) - pgprot_val(ref_prot) &= ~_PAGE_PSE; + ref_prot = pgprot_large_2_4k(ref_prot); break; default: return -EINVAL; } - ref_prot = pgprot_clear_protnone_bits(ref_prot); - /* * Get the target pfn from the original entry: */ pfn = ref_pfn; for (i = 0; i < PTRS_PER_PTE; i++, pfn += pfninc) - set_pte(pbase + i, pfn_pte(pfn, ref_prot)); + set_pte(pbase + i, cpa_pte(level - 1, pfn, ref_prot)); if (within(address, PAGE_OFFSET, PAGE_OFFSET + (max_pfn_mapped << PAGE_SHIFT))) @@ -1833,14 +1833,12 @@ static int __change_page_attr(struct cpa_data *cpa) new_prot = verify_rwx(cpa, old_prot, new_prot, address, pfn, 1); - new_prot = pgprot_clear_protnone_bits(new_prot); - /* * We need to keep the pfn from the existing PTE, * after all we're only going to change its attributes * not the memory it points to */ - new_pte = pfn_pte(pfn, new_prot); + new_pte = cpa_pte(level, pfn, new_prot); cpa->pfn = pfn; /* * Do we really change anything ? -- 2.53.0